`fix.lean` and `fix_1.lean` are very similar, but fix.lean is almost twice as fast. Reason: `fix.lean` uses `fix_2` instead of `fix_1` and avoid the creation of many closures. Here are runtime numbers on my machine. ``` time ./fix_1.lean.out 23 352321527 real 0m0.729s user 0m0.724s sys 0m0.000s ``` ``` ~/projects/lean4/tests/playground (master +)$ time ./fix.lean.out 23 352321527 real 0m0.396s user 0m0.388s sys 0m0.004s ``` TODO: modify the compiler to replace `fix_core_n f a_1 ... a_m` with `fix_core_m f a_1 ... a_m` whenever `n < m`. This feature is quite useful for writing reusable/generic code. For example, we cannot write an efficient `rec_t` without it because we don't know the arity of `m A` when we write `rec_t`.
8 lines
234 B
Text
8 lines
234 B
Text
def foo (rec : nat → nat → nat) : nat → nat → nat
|
|
| 0 a := a
|
|
| (n+1) a := rec n a + a + rec n (a+1)
|
|
|
|
def main (xs : list string) : io uint32 :=
|
|
let v := fix_1 foo (xs.head.to_nat) 10 in
|
|
io.println' (to_string v) *>
|
|
pure 0
|