aboutsummaryrefslogtreecommitdiff
path: root/entries/lizard-business/fib.dats
diff options
context:
space:
mode:
authorfunemy2022-10-25 07:14:52 +0000
committerfunemy2022-10-25 07:14:52 +0000
commit998ef837b43203319397f191036a97a5adc42220 (patch)
tree920558bd60d282c663fd2da1cd4144cac1849a1d /entries/lizard-business/fib.dats
parentfdac6c60e115297a58f5b81da0c4b7f18ac758f2 (diff)
parentce7544a6db594f7d3dfad0d7dc65d01515e57ad6 (diff)
Merge branch 'main' of github.com:braxtonhall/fib
Diffstat (limited to 'entries/lizard-business/fib.dats')
-rw-r--r--entries/lizard-business/fib.dats25
1 files changed, 25 insertions, 0 deletions
diff --git a/entries/lizard-business/fib.dats b/entries/lizard-business/fib.dats
new file mode 100644
index 0000000..f6bd5a1
--- /dev/null
+++ b/entries/lizard-business/fib.dats
@@ -0,0 +1,25 @@
+#include "share/atspre_define.hats"
+#include "share/atspre_staload.hats"
+
+dataprop is_fib(int, int) =
+| F0(0, 0) | F1(1, 1)
+| {i, m, n : nat} Fplus(i + 2, m + n) of (is_fib(i, m), is_fib(i + 1, n))
+
+typedef fib(i : int) = [n : nat] (is_fib(i, n) | int(n))
+
+
+fun fib {t : nat} .<>. (t : int(t)) :<> fib(t) =
+let
+ fun go {m, n, i : nat | i <= t} .<t - i>.
+ (M : is_fib(i, m), N : is_fib(i + 1, n) |
+ m : int(m), n : int(n), i : int(i)) :<>
+ fib(t) =
+ if i = t then (M | m)
+ else go(N, Fplus(M, N) | n, m + n, i + 1)
+in
+ go(F0, F1 | 0, 1, 0)
+end
+
+fun fib_(i : Nat) : Nat = let val (_ | res) = fib(i) in res end
+
+implement main0 () = println!("fib(15) = ", fib_(15))