commit 4ac6780
shrub
·
2026-11-20 23:10:19 +0000 UTC
parent c3a43cb
new examples courtesy of ricardo
14 files changed,
+396,
-62
M
TODO
+0,
-3
1@@ -1,8 +1,5 @@
2-qbe codegen
3 pragmas
4 incremental
5-first calss functions
6 sort arrows
7-doc comments
8 struct stuff
9 solver
+0,
-15
1@@ -1,15 +0,0 @@
2-use "prelude.kav"
3-use "list.kav"
4-
5-fn bad[n: nat](l: List[int, n]) -> int {
6- case l of {
7- | Nil => {
8- val u = list_free(l)
9- 0
10- }
11- | Cons(x, r) => {
12- val u = list_free(l)
13- x
14- }
15- }
16-}
+11,
-0
1@@ -0,0 +1,11 @@
2+pub fn and(x: bool, y: bool) -> bool {
3+ if x then y else x
4+}
5+
6+pub fn or(x: bool, y: bool) -> bool {
7+ if x then x else y
8+}
9+
10+pub fn not(x: bool) -> bool {
11+ if x then false else true
12+}
+12,
-0
1@@ -0,0 +1,12 @@
2+use "prelude.kav"
3+use "int.kav"
4+
5+fn collatz(n: int) -> int {
6+ if n == 1 then 0
7+ else if is_even(n) then 1 + collatz(n / 2)
8+ else 1 + collatz(3 * n + 1)
9+}
10+
11+fn main() -> int {
12+ collatz(12)
13+}
+6,
-19
1@@ -1,24 +1,11 @@
2 use "prelude.kav"
3-use "list.kav"
4-
5-fn evens[n: nat](l: List[int, n]) -> [m: nat] List[int, m] {
6- case l of {
7- | ~Nil => Nil
8- | ~Cons(x, rest) => if x % 2 == 0 then Cons(x, evens(rest)) else evens(rest)
9- }
10-}
11-
12-fn sum[n: nat](l: !List[int, n]) -> int {
13- case l of {
14- | Nil => 0
15- | Cons(x, rest) => x + sum(rest)
16- }
17-}
18+use "int.kav"
19+use "list.kav" as list
20
21 fn main() -> int {
22- val l = Cons(1, Cons(2, Cons(3, Cons(4, Nil))))
23- val e: [m: nat] List[int, m] = evens(l)
24- val s = sum(e)
25- val u = list_free(e)
26+ val l = list::Cons(1, list::Cons(2, list::Cons(3, list::Cons(4, list::Nil))))
27+ val e = list::filter(l, is_even)
28+ val s = list::foldl(e, 0, add)
29+ val _ = list::free(e)
30 s
31 }
+7,
-0
1@@ -0,0 +1,7 @@
2+pub fn id[a: utype](x: a) -> a {
3+ x
4+}
5+
6+pub fn const[a: utype, b: utype](x: a, y: b) -> a {
7+ x
8+}
+31,
-0
1@@ -0,0 +1,31 @@
2+pub fn add(x: int, y: int) -> int {
3+ x + y
4+}
5+
6+pub fn sub(x: int, y: int) -> int {
7+ x - y
8+}
9+
10+pub fn mul(x: int, y: int) -> int {
11+ x * y
12+}
13+
14+pub fn div(x: int, y: int) -> int {
15+ x / y
16+}
17+
18+pub fn min(x: int, y: int) -> int {
19+ if x > y then y else x
20+}
21+
22+pub fn max(x: int, y: int) -> int {
23+ if x > y then x else y
24+}
25+
26+pub fn is_even(n: int) -> bool {
27+ n % 2 == 0
28+}
29+
30+pub fn is_odd(n: int) -> bool {
31+ n % 2 == 1
32+}
+162,
-3
1@@ -1,4 +1,7 @@
2 use "prelude.kav"
3+use "bool.kav" as bool
4+use "pair.kav" as pair
5+use "maybe.kav" as maybe
6
7 pub linear datatype List[a: type, n: nat] {
8 | Nil : List[a, 0]
9@@ -7,10 +10,166 @@ pub linear datatype List[a: type, n: nat] {
10
11 typedef List1[a: type, n: nat] = List[a, n + 1]
12
13-pub fn list_free[a: utype, n: nat](list: List[a, n]) -> one {
14+pub fn free[a: utype, n: nat](list: List[a, n]) -> one {
15 case list of {
16 /* ~ consumes the list wrapper */
17- | ~Nil => one
18- | ~Cons(_, rest) => list_free(rest)
19+ | ~Nil => one
20+ | ~Cons(_, rest) => free(rest)
21+ }
22+}
23+
24+pub fn copy[a: utype, n: nat](list: !List[a, n]) -> List[a, n] {
25+ case list of {
26+ | Nil => Nil
27+ | Cons(x, xs) => Cons(x, copy(xs))
28+ }
29+}
30+
31+pub fn cons[a: utype, n: nat](list: List[a, n], x: a) -> List[a, n + 1] {
32+ Cons(x, list)
33+}
34+
35+pub fn append[a: utype, n: nat, m: nat](list1: List[a, n], list2: List[a, m]) -> List[a, m + n] {
36+ case list1 of {
37+ | ~Nil => list2
38+ | ~Cons(x, xs) => Cons(x, append(xs, list2))
39+ }
40+}
41+
42+pub fn equal[n: nat](list1: !List[int, n], list2: !List[int, n]) -> bool {
43+ case (list1, list2) of {
44+ | (Nil, Nil) => true
45+ | (Cons(x, xs), Cons(y, ys)) => bool::and(x == y, equal(xs, ys))
46+ }
47+}
48+
49+pub fn map[a: utype, b: type, n: nat](list: List[a, n], f: fn(a) -> b) -> List[b, n] {
50+ case list of {
51+ | ~Nil => Nil
52+ | ~Cons(x, xs) => Cons(f(x), map(xs, f))
53+ }
54+}
55+
56+pub fn iter[a: utype, n: nat](list: !List[a, n], f: fn(a) -> one) -> one {
57+ case list of {
58+ | Nil => one
59+ | Cons(x, xs) => {
60+ val _ = f(x)
61+ iter(xs, f)
62+ }
63+ }
64+}
65+
66+pub fn filter[a: utype, n: nat](list: List[a, n], f: fn(a) -> bool) -> [m: nat] List[a, m] {
67+ case list of {
68+ | ~Nil => Nil
69+ | ~Cons(x, xs) => {
70+ val rec = filter(xs, f)
71+ if f(x) then Cons(x, rec) else rec
72+ }
73+ }
74+}
75+
76+pub fn filter_map[a: utype, b: utype, n: nat](list: List[a, n], f: fn(a) -> maybe::Maybe[b]) -> [m: nat] List[b, m] {
77+ case list of {
78+ | ~Nil => Nil
79+ | ~Cons(x, xs) => {
80+ val rec = filter_map(xs, f)
81+ case f(x) of {
82+ | maybe::None => rec
83+ | maybe::Some(y) => Cons(y, rec)
84+ }
85+ }
86+ }
87+}
88+
89+pub fn foldl[a: utype, b: type, n: nat](list: !List[a, n], init: b, f: fn(b, a) -> b) -> b {
90+ case list of {
91+ | Nil => init
92+ | Cons(x, xs) => foldl(xs, f(init, x), f)
93+ }
94+}
95+
96+pub fn foldr[a: utype, b: type, n: nat](list: !List[a, n], init: b, f: fn(a, b) -> b) -> b {
97+ case list of {
98+ | Nil => init
99+ | Cons(x, xs) => f(x, foldr(xs, init, f))
100+ }
101+}
102+
103+fn reverse_impl[a: type,n: nat, m: nat](l: List[a, n], acc: List[a, m]) -> List[a, n + m] {
104+ case l of {
105+ | ~Nil => acc
106+ | ~Cons(entry, rest) => reverse_impl(rest, Cons(entry, acc))
107+ }
108+}
109+
110+pub fn reverse[a: utype, n: nat](list: List[a, n]) -> List[a, n] {
111+ reverse_impl(list, Nil)
112+}
113+
114+pub fn zip_with[a: utype, b: utype, c: type, n: nat](list1: List[a, n], list2: List[b, n], f: fn(a, b) -> c) -> List[c, n] {
115+ case (list1, list2) of {
116+ | (~Nil, ~Nil) => Nil
117+ | (~Cons(x, xs), ~Cons(y, ys)) => Cons(f(x, y), zip_with(xs, ys, f))
118+ }
119+}
120+
121+pub fn zip[a: utype, b: utype, n: nat](list1: List[a, n], list2: List[b, n]) -> List[(a, b), n] {
122+ zip_with(list1, list2, pair::make)
123+}
124+
125+/* tuples are linear so they can't go through map, split them by hand */
126+pub fn unzip[a: utype, b: utype, n: nat](list: List[(a, b), n]) -> (List[a, n], List[b, n]) {
127+ case list of {
128+ | ~Nil => (Nil, Nil)
129+ | ~Cons((x, y), rest) => {
130+ val (xs, ys) = unzip(rest)
131+ (Cons(x, xs), Cons(y, ys))
132+ }
133+ }
134+}
135+
136+pub fn tail[a: utype, n: nat](list: List[a, n]) -> List[a, n - 1] {
137+ case list of {
138+ | ~Cons(_, xs) => xs
139+ }
140+}
141+
142+pub fn head[a: utype, n: pos](list: !List[a, n]) -> a {
143+ case list of {
144+ | Cons(x, _) => x
145+ }
146+}
147+
148+pub fn find_map[a: utype, b: type, n: nat](list: !List[a, n], f: fn(a) -> maybe::Maybe[b]) -> maybe::Maybe[b] {
149+ case list of {
150+ | Nil => maybe::None
151+ | Cons(x, xs) =>
152+ case f(x) of {
153+ | maybe::None => find_map(xs, f)
154+ | res => res
155+ }
156+ }
157+}
158+
159+pub fn find[a: utype, n: nat](list: !List[a, n], f: fn(a) -> bool) -> maybe::Maybe[a] {
160+ case list of {
161+ | Nil => maybe::None
162+ | Cons(x, xs) => if f(x) then maybe::Some(x) else find(xs, f)
163+ }
164+}
165+
166+pub fn any[a: utype, n: nat](list: !List[a, n], f: fn(a) -> bool) -> bool {
167+ case find(list, f) of {
168+ | None => false
169+ | _ => true
170+ }
171+}
172+
173+pub fn all[a: utype, n: nat](list: !List[a, n], f: fn(a) -> bool) -> bool {
174+ case list of {
175+ | Nil => true
176+ | Cons(x, xs) => if bool::not(f(x)) then false else all(xs, f)
177 }
178 }
+55,
-0
1@@ -0,0 +1,55 @@
2+use "prelude.kav"
3+use "fun.kav" as fun
4+
5+pub datatype Maybe[a:type] {
6+ | None
7+ | Some(a)
8+}
9+
10+pub fn equal(maybe1: !Maybe[int], maybe2: !Maybe[int]) -> bool {
11+ case (maybe1, maybe2) of {
12+ | (None, None) => true
13+ | (Some(x), Some(y)) => x == y
14+ | (_, _) => false
15+ }
16+}
17+
18+pub fn some_if[a: utype](cond: bool, x: a) -> Maybe[a] {
19+ if cond then Some(x) else None
20+}
21+
22+pub fn map[a: utype, b: type](maybe: !Maybe[a], f: fn(a) -> b) -> Maybe[b] {
23+ case maybe of {
24+ | None => None
25+ | Some(x) => Some(f(x))
26+ }
27+}
28+
29+pub fn pure[a: utype](x: a) -> Maybe[a] {
30+ Some(x)
31+}
32+
33+pub fn ap[a: utype, b: utype, c: utype](m1: Maybe[a], m2: Maybe[b], f: fn(a, b) -> c) -> Maybe[c] {
34+ case (m1, m2) of {
35+ | (Some(x), Some(y)) => Some(f(x, y))
36+ | (_, _) => None
37+ }
38+}
39+
40+pub fn bind[a: utype, b: type](maybe: !Maybe[a], f: fn(a) -> Maybe[b]) -> Maybe[b] {
41+ case maybe of {
42+ | None => None
43+ | Some(x) => f(x)
44+ }
45+}
46+
47+pub fn value_map[a: utype, b: utype](default: b, maybe: !Maybe[a], f: fn(a) -> b) -> b {
48+ case maybe of {
49+ | None => default
50+ | Some(x) => f(x)
51+ }
52+}
53+
54+pub fn value_or[a: utype](default: a, maybe: !Maybe[a]) -> a {
55+ value_map(default, maybe, fun::id)
56+}
+22,
-0
1@@ -0,0 +1,22 @@
2+pub fn make[a: utype, b: utype](x: a, y: b) -> (a, b) {
3+ (x, y)
4+}
5+
6+pub fn fst[a: utype, b: utype](pair: (a, b)) -> a {
7+ case pair of {
8+ | ~(x, _) => x
9+ }
10+}
11+
12+pub fn snd[a: utype, b: utype](pair: (a, b)) -> b {
13+ case pair of {
14+ | ~(_, x) => x
15+ }
16+}
17+
18+pub fn swap[a: utype, b: utype](pair: (a, b)) -> (b, a) {
19+ case pair of {
20+ | ~(x, y) => (y, x)
21+ }
22+}
23+
+0,
-7
1@@ -1,9 +1,2 @@
2 pub sortdef nat = [n: int | n >= 0]
3 pub sortdef pos = [n: int | n >= 1]
4-
5-pub datatype Maybe[a:type] {
6- | None
7- | Some(a)
8-}
9-
10-
+4,
-11
1@@ -1,15 +1,8 @@
2 use "prelude.kav"
3-use "list.kav"
4-
5-fn reverse[a: type,n: nat, m: nat](l: List[a, n], acc: List[a, m]) -> List[a, n + m] {
6- case l of {
7- | ~Nil => acc
8- | ~Cons(entry, rest) => reverse(rest, Cons(entry, acc))
9- }
10-}
11+use "list.kav" as list
12
13 pub fn main() -> one {
14- val list: List[int, 3] = Cons(1, Cons(2, Cons(3, Nil)))
15- val reversed = reverse(list, Nil)
16- list_free(reversed)
17+ val l: list::List[int, 3] = list::Cons(1, list::Cons(2, list::Cons(3, list::Nil)))
18+ val reversed = list::reverse(l)
19+ list::free(reversed)
20 }
+82,
-0
1@@ -0,0 +1,82 @@
2+use "prelude.kav"
3+use "list.kav" as list
4+use "bool.kav" as bool
5+
6+pub linear datatype Tree[a: type, n: nat] {
7+ | Leaf: Tree[a, 0]
8+ | [n:nat, m:nat] Node(a, Tree[a, n], Tree[a, m]): Tree[a, m + n + 1]
9+}
10+
11+pub fn free[a: utype, n: nat](tree: Tree[a, n]) -> one {
12+ case tree of {
13+ | ~Leaf => one
14+ | ~Node(_, l, r) => {
15+ val _ = free(l)
16+ val _ = free(r)
17+ one
18+ }
19+ }
20+}
21+
22+pub fn inorder_traversal[a: utype, n: nat](tree: !Tree[a, n]) -> list::List[a, n] {
23+ case tree of {
24+ | Leaf => list::Nil
25+ | Node(x, l, r) => list::append(inorder_traversal(l), list::Cons(x, inorder_traversal(r)))
26+ }
27+}
28+
29+pub fn map[a: utype, b: type, n: nat](tree: !Tree[a, n], f: fn(a) -> b) -> Tree[b, n] {
30+ case tree of {
31+ | Leaf => Leaf
32+ | Node(x, l, r) => Node(f(x), map(l, f), map(r, f))
33+ }
34+}
35+
36+pub fn sum[n: nat](tree: !Tree[int, n]) -> int {
37+ case tree of {
38+ | Leaf => 0
39+ | Node(x, l, r) => x + sum(l) + sum(r)
40+ }
41+}
42+
43+pub fn equal[n: nat](tree1: !Tree[int, n], tree2: !Tree[int, n]) -> bool {
44+ case (tree1, tree2) of {
45+ | (Leaf, Leaf) => true
46+ | (Node(x1, l1, r1), Node(x2, l2, r2)) => bool::and(x1 == x2, bool::and(equal(l1, l2), equal(r1, r2)))
47+ }
48+}
49+
50+/*fn main() -> int {
51+ val left = Node(3, Leaf, Leaf)
52+ val right = Node(1, Leaf, Leaf)
53+ val tree = Node(2, left, right)
54+ val expected = list::Cons(3, list::Cons(2, list::Cons(1, list::Nil)))
55+ val traversal = inorder_traversal(tree)
56+ val res = list::equal(expected, traversal)
57+ val _ = list::free(traversal)
58+ val _ = list::free(expected)
59+ val _ = free(tree)
60+ if res then 0 else 1
61+}*/
62+
63+/*fn main() -> int {
64+ val left = Node(3, Leaf, Leaf)
65+ val right = Node(1, Leaf, Leaf)
66+ val tree1 = Node(2, left, right)
67+ val left = Node(3, Leaf, Leaf)
68+ val right = Node(1, Leaf, Leaf)
69+ val tree2 = Node(2, left, right)
70+ val res = equal(tree1, tree2)
71+ val _ = free(tree1)
72+ val _ = free(tree2)
73+ if res then 0 else 1
74+}*/
75+
76+fn main() -> int {
77+ val left = Node(3, Leaf, Leaf)
78+ val right = Node(1, Leaf, Leaf)
79+ val tree = Node(2, left, right)
80+ val sum = sum(tree)
81+ val _ = free(tree)
82+ sum
83+}
+4,
-4
1@@ -9,15 +9,15 @@ fn swapsies(p: (int, int)) -> (int, int) {
2
3 fn sum3(t: (int, int, int)) -> int {
4 case t of {
5- | (a, b, c) => a + b + c
6+ | ~(a, b, c) => a + b + c
7 }
8 }
9
10 fn sign(p: (int, int)) -> int {
11 case p of {
12- | (0, _) => 0
13- | (_, 0) => 1
14- | _ => 2
15+ | ~(0, _) => 0
16+ | ~(_, 0) => 1
17+ | ~(_, _) => 2
18 }
19 }
20