lean4-htt/tests/playground/eval.lean
euprunin 4b47a10bef
chore: fix spelling mistakes in tests (#5439)
Co-authored-by: euprunin <euprunin@users.noreply.github.com>
2024-09-24 03:22:53 +00:00

35 lines
1.1 KiB
Text
Raw Permalink 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.

import init.lean.name
open Lean
abbrev ConstantTable (α : Type) := HashMap Name α
def mkSimpleFnTable : IO (IO.Ref (ConstantTable (Nat → Nat))) :=
IO.mkRef {}
@[init mkSimpleFnTable]
constant simpleFnTable : IO.Ref (ConstantTable (Nat → Nat)) := default _
def registerSimpleFn (n : Name) (fn : Nat → Nat) : IO Unit :=
do ini ← IO.initializing,
unless ini (throw "we should only register functions during initialization"),
simpleFnTable.modify (λ m, m.insert n fn)
def add10 (n : Nat) := n+10
def mul10 (n : Nat) := n*10
def inc (n : Nat) := n+1
@[init] def regAdd10 : IO Unit := registerSimpleFn `add10 add10
@[init] def regMul10 : IO Unit := registerSimpleFn `mul10 mul10
@[init] def regInc : IO Unit := registerSimpleFn `inc inc
def eval (fName : Name) (n : Nat) : IO Nat :=
do m ← simpleFnTable.get,
match m.find fName with
| some f := pure $ f n
| none := throw (IO.userError "unknown function")
def main (xs : List String) : IO Unit :=
do [f, x] ← pure xs | throw "invalid number of arguments",
r ← eval (mkSimpleName f) x.toNat,
IO.println r,
pure ()