lean4-htt/tests/playground/fix1.lean
Leonardo de Moura 42fbe3c18c chore(library/init,runtime,library/compiler): add fix primitive back
The new `partial def`s allow us to define `fix` in Lean, but the Lean
implementation is not as efficient as the native one. The native one
in C++ use weak pointers to prevent a closure allocation at every
recursive invocation.

This commit also fixes the `fixCore` helper functions that were broken
after we switched to camelCase.

We have updated the test `fix1.lean` to demonstrate the native
implementation is faster. Here are the numbers on my desktop.

```
./run.sh fix1.lean 24
721420279
Time for 'native fix': 816ms
721420279
Time for 'fix in lean': 1.34s
```
2019-03-27 17:13:53 -07:00

21 lines
612 B
Text
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

def foo (rec : Nat → Nat → Nat) : Nat → Nat → Nat
| 0 a := a
| (n+1) a := rec n a + a + rec n (a+1)
partial def fix' (f: (Nat → Nat → Nat) → (Nat → Nat → Nat)) : Nat → Nat → Nat
| a b := f fix' a b
def prof {α : Type} (msg : String) (p : IO α) : IO α :=
let msg := "Time for '" ++ msg ++ "':" in
timeit msg p
def fix_test (n : Nat) : IO Unit :=
IO.println (fix foo n 10)
def fix'_test (n : Nat) : IO Unit :=
IO.println (fix' foo n 10)
def main (xs : List String) : IO Unit :=
prof "native fix" (fix_test xs.head.toNat) *>
prof "fix in lean" (fix'_test xs.head.toNat) *>
pure ()