41 lines
1.1 KiB
Plaintext
41 lines
1.1 KiB
Plaintext
// compute the fixpoint of f
|
|
[int] -> int fix([[int] -> int] -> [int] -> int f) {
|
|
[T] -> [int] -> int g = fun(T x) {
|
|
[int] -> int h = fun (int y) { // will become: ((outT(x))(x))(y)
|
|
[T] -> [int] -> int g = outT(x);
|
|
[int] -> int i = g(x);
|
|
return i(y);
|
|
};
|
|
return f(h);
|
|
};
|
|
return g(inT(g));
|
|
}
|
|
|
|
// type T = Y of T -> int -> int
|
|
|
|
struct Y() : T {
|
|
init [T] -> [int] -> int f;
|
|
}
|
|
|
|
[T] -> [int] -> int outT(T x) { return x.f; }
|
|
T inT([T] -> [int] -> int x) { return new Y(f = x); }
|
|
|
|
// -----------------------------------------------------------------------------
|
|
|
|
[int] -> int factorialor([int] -> int factorial) {
|
|
return
|
|
fun (int x) {
|
|
if (x == 0) {
|
|
return 1;
|
|
}
|
|
else {
|
|
return x * factorial(x - 1);
|
|
};
|
|
};
|
|
}
|
|
|
|
int main(int n) {
|
|
[int] -> int factorial = fix(factorialor);
|
|
return factorial(n);
|
|
}
|