Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make a simple fib computing program for naive benchmarks #1308

Merged
merged 1 commit into from
Dec 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 36 additions & 0 deletions test_workspace/FibBench.bosatsu
Original file line number Diff line number Diff line change
@@ -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)
))

4 changes: 2 additions & 2 deletions test_workspace/Prog.bosatsu
Original file line number Diff line number Diff line change
@@ -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: +*]

Expand Down
Loading