From 3402da7f76c3552d34e6b658f23703ab4eff3705 Mon Sep 17 00:00:00 2001 From: Oscar Boykin Date: Tue, 10 Dec 2024 16:51:25 -1000 Subject: [PATCH] Make a simple fib computing program for naive benchmarks --- test_workspace/FibBench.bosatsu | 36 +++++++++++++++++++++++++++++++++ test_workspace/Prog.bosatsu | 4 ++-- 2 files changed, 38 insertions(+), 2 deletions(-) create mode 100644 test_workspace/FibBench.bosatsu diff --git a/test_workspace/FibBench.bosatsu b/test_workspace/FibBench.bosatsu new file mode 100644 index 000000000..1ee42aa1e --- /dev/null +++ b/test_workspace/FibBench.bosatsu @@ -0,0 +1,36 @@ +package Bosatsu/FibBench + +from Bosatsu/Prog import Prog, Main, println, read_env, await, ignore_env, pure +from Bosatsu/Nat import Nat, to_Nat, Zero as NZero, Succ as NSucc + +def fib_Nat(n: Nat) -> Int: + recur n: + case NZero | NSucc(NZero): 1 + case NSucc(NSucc(p1) as prev): + fp = fib_Nat(prev) + fp1 = fib_Nat(p1) + add(fp, fp1) + +def fib(i: Int) -> Int: fib_Nat(to_Nat(i)) + +def print_fib(str: String): + match string_to_Int(str): + case Some(i): println("fib(${str}) = ${int_to_String(fib(i))}") + case None: println("could not parse ${str}") + +def list_len(lst, acc): + recur lst: + case []: acc + case [_, *tail]: list_len(tail, add(acc, 1)) + +def main(args: List[String]): + match args: + case [_, n]: print_fib(n) + case _: println("expected exactly one arg, got: ${int_to_String(list_len(args, 0))}") + +main = Main(( + args <- read_env.await() + _ <- main(args).ignore_env().await() + pure(0) +)) + \ No newline at end of file diff --git a/test_workspace/Prog.bosatsu b/test_workspace/Prog.bosatsu index f18563ef3..d1c22521d 100644 --- a/test_workspace/Prog.bosatsu +++ b/test_workspace/Prog.bosatsu @@ -1,8 +1,8 @@ package Bosatsu/Prog -export (unit, pure, raise_error, read_env, recover, remap_env, +export (unit, pure, raise_error, read_env, ignore_env, recover, remap_env, println, await, recursive, map, map_err, - with_env, Prog, Main) + with_env, Prog, Main()) external struct Prog[env: -*, err: +*, res: +*]