commit 14f78dc
ricmaps
·
2026-11-22 18:28:00 +0000 UTC
parent 514d15e
update examples
7 files changed,
+555,
-78
+7,
-0
1@@ -0,0 +1,7 @@
2+fn fib(i: int) -> int {
3+ if i <= 1 then 1 else fib(i-1) + fib(i-2)
4+}
5+
6+fn main() -> int {
7+ fib(6)
8+}
+15,
-2
1@@ -1,7 +1,20 @@
2-pub fn id[a: utype](x: a) -> a {
3+/%
4+The identity function. Returns whatever it's given
5+%/
6+pub fn id[a: type](x: a) -> a {
7 x
8 }
9
10-pub fn const[a: utype, b: utype](x: a, y: b) -> a {
11+/%
12+Returns the first argument and discards the second.
13+%/
14+pub fn const[a: type, b: utype](x: a, y: b) -> a {
15 x
16 }
17+
18+/%
19+Applies a function to an argument.
20+%/
21+pub fn apply[a: type, b: type](f: fn(a) -> b, x: a) -> b {
22+ f(x)
23+}
+24,
-0
1@@ -1,31 +1,55 @@
2+/%
3+Adds two ints.
4+%/
5 pub fn add(x: int, y: int) -> int {
6 x + y
7 }
8
9+/%
10+Subtracts two ints.
11+%/
12 pub fn sub(x: int, y: int) -> int {
13 x - y
14 }
15
16+/%
17+Multiplies two ints.
18+%/
19 pub fn mul(x: int, y: int) -> int {
20 x * y
21 }
22
23+/%
24+Divides two ints.
25+%/
26 pub fn div(x: int, y: int) -> int {
27 x / y
28 }
29
30+/%
31+Returns the smaller of two ints.
32+%/
33 pub fn min(x: int, y: int) -> int {
34 if x > y then y else x
35 }
36
37+/%
38+Returns the bigger of two ints.
39+%/
40 pub fn max(x: int, y: int) -> int {
41 if x > y then x else y
42 }
43
44+/%
45+Returns whether an int is even.
46+%/
47 pub fn is_even(n: int) -> bool {
48 n % 2 == 0
49 }
50
51+/%
52+Returns whether an int is odd.
53+%/
54 pub fn is_odd(n: int) -> bool {
55 n % 2 == 1
56 }
+297,
-22
1@@ -1,15 +1,26 @@
2-use <prelude.kav>
3+use "prelude.kav"
4 use "bool.kav" as bool
5 use "pair.kav" as pair
6 use "maybe.kav" as maybe
7+use "int.kav" as int
8+use "fun.kav" as fun
9
10+/%
11+A linear, length-indexed list.
12+%/
13 pub linear datatype List[a: type, n: nat] {
14 | Nil : List[a, 0]
15 | [m: nat] Cons(a, List[a, m]) : List[a, m+1]
16 }
17
18+/%
19+A list known to be non-empty, i.e. has length `n + 1`.
20+%/
21 typedef List1[a: type, n: nat] = List[a, n + 1]
22
23+/%
24+Deallocates a list.
25+%/
26 pub fn free[a: utype, n: nat](list: List[a, n]) -> one {
27 case list of {
28 /* ~ consumes the list wrapper */
29@@ -18,6 +29,9 @@ pub fn free[a: utype, n: nat](list: List[a, n]) -> one {
30 }
31 }
32
33+/%
34+Returns a copy of a list
35+%/
36 pub fn copy[a: utype, n: nat](list: !List[a, n]) -> List[a, n] {
37 case list of {
38 | Nil => Nil
39@@ -25,31 +39,66 @@ pub fn copy[a: utype, n: nat](list: !List[a, n]) -> List[a, n] {
40 }
41 }
42
43-pub fn cons[a: utype, n: nat](list: List[a, n], x: a) -> List[a, n + 1] {
44+/%
45+Adds an element to the front of a list.
46+
47+Consumes `list` and returns a list of length `n + 1` with `x` as its head.
48+%/
49+pub fn cons[a: type, n: nat](x: a, list: List[a, n]) -> List[a, n + 1] {
50 Cons(x, list)
51 }
52
53-pub fn append[a: utype, n: nat, m: nat](list1: List[a, n], list2: List[a, m]) -> List[a, m + n] {
54+/%
55+Concatenates two lists.
56+
57+Consumes both lists and returns a list containing the elements of `list1`
58+followed by the elements of `list2`.
59+%/
60+pub fn append[a: type, n: nat, m: nat](list1: List[a, n], list2: List[a, m]) -> List[a, m + n] {
61 case list1 of {
62 | ~Nil => list2
63 | ~Cons(x, xs) => Cons(x, append(xs, list2))
64 }
65 }
66
67+/%
68+Tests whether two integer lists are equal.
69+%/
70 pub fn equal[n: nat](list1: !List[int, n], list2: !List[int, n]) -> bool {
71 case (list1, list2) of {
72 | (Nil, Nil) => true
73- | (Cons(x, xs), Cons(y, ys)) => bool::and(x == y, equal(xs, ys))
74+ | (Cons(x, xs), Cons(y, ys)) => if x == y then equal(xs, ys) else false
75 }
76 }
77
78-pub fn map[a: utype, b: type, n: nat](list: List[a, n], f: fn(a) -> b) -> List[b, n] {
79+fn length_impl[a: type, n: nat](list: !List[a, n], acc: int) -> int {
80+ case list of {
81+ | Nil => acc
82+ | Cons(_, xs) => length_impl(xs, acc + 1)
83+ }
84+}
85+
86+
87+/%
88+Returns the number of elements in a list.
89+%/
90+pub fn length[a: type, n: nat](list: !List[a, n]) -> int {
91+ length_impl(list, 0)
92+}
93+
94+/%
95+Applies `f` to every element of a list.
96+%/
97+pub fn map[a: type, b: type, n: nat](list: List[a, n], f: fn(a) -> b) -> List[b, n] {
98 case list of {
99 | ~Nil => Nil
100 | ~Cons(x, xs) => Cons(f(x), map(xs, f))
101 }
102 }
103
104+/%
105+Calls `f` on every element of a list, in order, for its side effects.
106+%/
107 pub fn iter[a: utype, n: nat](list: !List[a, n], f: fn(a) -> one) -> one {
108 case list of {
109 | Nil => one
110@@ -60,6 +109,9 @@ pub fn iter[a: utype, n: nat](list: !List[a, n], f: fn(a) -> one) -> one {
111 }
112 }
113
114+/%
115+Keeps only the elements for which `f` returns `true`.
116+%/
117 pub fn filter[a: utype, n: nat](list: List[a, n], f: fn(a) -> bool) -> [m: nat] List[a, m] {
118 case list of {
119 | ~Nil => Nil
120@@ -70,19 +122,30 @@ pub fn filter[a: utype, n: nat](list: List[a, n], f: fn(a) -> bool) -> [m: nat]
121 }
122 }
123
124-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] {
125+/%
126+Maps and filters a list in a single pass.
127+
128+Applies `f` to each element. If `f` returns `Some(y)`, `y` is included in the
129+result; if it returns `None`, the element is dropped.
130+%/
131+pub fn filter_map[a: type, b: type, n: nat](list: List[a, n], f: fn(a) -> maybe::Maybe[b]) -> [m: nat] List[b, m] {
132 case list of {
133 | ~Nil => Nil
134 | ~Cons(x, xs) => {
135 val rec = filter_map(xs, f)
136 case f(x) of {
137- | maybe::None => rec
138- | maybe::Some(y) => Cons(y, rec)
139+ | ~maybe::None => rec
140+ | ~maybe::Some(y) => Cons(y, rec)
141 }
142 }
143 }
144 }
145
146+/%
147+Left fold over a list.
148+
149+For a list `[x1, x2, x3]` this computes `f(f(f(init, x1), x2), x3)`.
150+%/
151 pub fn foldl[a: utype, b: type, n: nat](list: !List[a, n], init: b, f: fn(b, a) -> b) -> b {
152 case list of {
153 | Nil => init
154@@ -90,6 +153,12 @@ pub fn foldl[a: utype, b: type, n: nat](list: !List[a, n], init: b, f: fn(b, a)
155 }
156 }
157
158+/%
159+Right fold over a list.
160+
161+For a list `[x1, x2, x3]` this computes `f(x1, f(x2, f(x3, init)))`.
162+Not tail-recursive.
163+%/
164 pub fn foldr[a: utype, b: type, n: nat](list: !List[a, n], init: b, f: fn(a, b) -> b) -> b {
165 case list of {
166 | Nil => init
167@@ -97,6 +166,117 @@ pub fn foldr[a: utype, b: type, n: nat](list: !List[a, n], init: b, f: fn(a, b)
168 }
169 }
170
171+/%
172+Splits a list at the first element that fails a predicate.
173+
174+Equivalent to `(take_while(list, f), drop_while(list, f))`.
175+%/
176+pub fn span[a: utype, n: nat](list: List[a, n], f: fn(a) -> bool) -> [m: nat, p: nat] (List[a, m], List[a, p]) {
177+ case list of {
178+ | ~Nil => (Nil, Nil)
179+ | ~Cons(x, xs) =>
180+ if f(x) then {
181+ val (prefix, suffix) = span(xs, f)
182+ (Cons(x, prefix), suffix)
183+ } else (Nil, Cons(x, xs))
184+ }
185+}
186+
187+/%
188+Keeps the longest prefix of a list whose elements all satisfy `f`.
189+
190+Stops at the first element for which `f` returns `false`,
191+that element and everything after it is deallocated.
192+%/
193+pub fn take_while[a: utype, n: nat](list: List[a, n], f: fn(a) -> bool) -> [m: nat] List[a, m] {
194+ case list of {
195+ | ~Nil => Nil
196+ | ~Cons(x, xs) =>
197+ if f(x) then Cons(x, take_while(xs, f))
198+ else {
199+ val _ = free(xs)
200+ Nil
201+ }
202+ }
203+}
204+
205+/%
206+Removes the longest prefix of a list whose elements all satisfy `f`.
207+%/
208+pub fn drop_while[a: utype, n: nat](list: List[a, n], f: fn(a) -> bool) -> [m: nat] List[a, m] {
209+ case list of {
210+ | ~Nil => Nil
211+ | ~Cons(x, xs) =>
212+ if f(x) then drop_while(xs, f) else Cons(x, xs)
213+ }
214+}
215+
216+/%
217+Returns the largest element of a non-empty integer list.
218+%/
219+pub fn maximum[n: nat](list: !List1[int, n]) -> int {
220+ case list of {
221+ | Cons(x, xs) => foldl(xs, x, int::max)
222+ }
223+}
224+
225+/%
226+Returns the smallest element of a non-empty integer list.
227+%/
228+pub fn minimum[n: nat](list: !List1[int, n]) -> int {
229+ case list of {
230+ | Cons(x, xs) => foldl(xs, x, int::min)
231+ }
232+}
233+
234+/%
235+Returns the sum of the elements of an integer list.
236+
237+The sum of the empty list is `0`.
238+%/
239+pub fn sum[n: nat](list: !List[int, n]) -> int {
240+ foldl(list, 0, int::add)
241+}
242+
243+/%
244+Returns the product of the elements of an integer list.
245+
246+The product of the empty list is `1`.
247+%/
248+pub fn product[n: nat](list: !List[int, n]) -> int {
249+ foldl(list, 1, int::mul)
250+}
251+
252+/%
253+Left scan: like `foldl`, but keeps every intermediate accumulator.
254+
255+For a list `[x1, x2, x3]` this returns
256+`[init, f(init, x1), f(f(init, x1), x2), f(f(f(init, x1), x2), x3)]`.
257+%/
258+pub fn scan_left[a: utype, b: utype, n: nat](list: List[a, n], init: b, f: fn(b, a) -> b) -> List[b, n + 1] {
259+ case list of {
260+ | ~Nil => Cons(init, Nil)
261+ | ~Cons(x, xs) => Cons(init, scan_left(xs, f(init, x), f))
262+ }
263+}
264+
265+/%
266+Right scan: like `foldr`, but keeps every intermediate accumulator.
267+
268+For a list `[x1, x2, x3]` this returns
269+`[f(x1, f(x2, f(x3, init))), f(x2, f(x3, init)), f(x3, init), init]`.
270+%/
271+pub fn scan_right[a: utype, b: utype, n: nat](list: List[a, n], init: b, f: fn(a, b) -> b) -> List[b, n + 1] {
272+ case list of {
273+ | ~Nil => Cons(init, Nil)
274+ | ~Cons(x, xs) => {
275+ val rest = scan_right(xs, init, f)
276+ val y = f(x, head(rest))
277+ Cons(y, rest)
278+ }
279+ }
280+}
281+
282 fn reverse_impl[a: type,n: nat, m: nat](l: List[a, n], acc: List[a, m]) -> List[a, n + m] {
283 case l of {
284 | ~Nil => acc
285@@ -104,55 +284,95 @@ fn reverse_impl[a: type,n: nat, m: nat](l: List[a, n], acc: List[a, m]) -> List[
286 }
287 }
288
289-pub fn reverse[a: utype, n: nat](list: List[a, n]) -> List[a, n] {
290+/%
291+Reverses a list.
292+%/
293+pub fn reverse[a: type, n: nat](list: List[a, n]) -> List[a, n] {
294 reverse_impl(list, Nil)
295 }
296
297-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] {
298+/%
299+Combines two lists of same length element-wise using `f`.
300+
301+The `i`-th element of the result is `f(x, y)`, where `x` and `y` are the `i`-th elements of
302+the first and second list respectively.
303+%/
304+pub fn zip_with[a: type, b: type, c: type, n: nat](list1: List[a, n], list2: List[b, n], f: fn(a, b) -> c) -> List[c, n] {
305 case (list1, list2) of {
306 | (~Nil, ~Nil) => Nil
307 | (~Cons(x, xs), ~Cons(y, ys)) => Cons(f(x, y), zip_with(xs, ys, f))
308 }
309 }
310
311-pub fn zip[a: utype, b: utype, n: nat](list1: List[a, n], list2: List[b, n]) -> List[(a, b), n] {
312+/%
313+Pairs up the elements of two lists of same length.
314+%/
315+pub fn zip[a: type, b: type, n: nat](list1: List[a, n], list2: List[b, n]) -> List[(a, b), n] {
316 zip_with(list1, list2, pair::make)
317 }
318
319-/* tuples are linear so they can't go through map, split them by hand */
320-pub fn unzip[a: utype, b: utype, n: nat](list: List[(a, b), n]) -> (List[a, n], List[b, n]) {
321+/%
322+Splits a list of pairs into a pair of lists.
323+
324+This is the inverse of `zip`.
325+%/
326+pub fn unzip[a: type, b: type, n: nat](list: List[(a, b), n]) -> (List[a, n], List[b, n]) {
327 case list of {
328 | ~Nil => (Nil, Nil)
329- | ~Cons((x, y), rest) => {
330- val (xs, ys) = unzip(rest)
331- (Cons(x, xs), Cons(y, ys))
332- }
333+ | ~Cons((x, y), rest) =>
334+ case unzip(rest) of {
335+ | ~(xs, ys) => (Cons(x, xs), Cons(y, ys))
336+ }
337 }
338 }
339
340-pub fn tail[a: utype, n: nat](list: List[a, n]) -> List[a, n - 1] {
341+/%
342+Returns everything after the first element of a non-empty list.
343+%/
344+pub fn tail[a: utype, n: nat](list: List1[a, n]) -> List[a, n] {
345 case list of {
346 | ~Cons(_, xs) => xs
347 }
348 }
349
350-pub fn head[a: utype, n: pos](list: !List[a, n]) -> a {
351+/%
352+Returns the first element of a non-empty list.
353+%/
354+pub fn head[a: utype, n: nat](list: !List1[a, n]) -> a {
355 case list of {
356 | Cons(x, _) => x
357 }
358 }
359
360+/%
361+Returns the last element of a non-empty list.
362+%/
363+pub fn last[a: utype, n: nat](list: !List1[a, n]) -> a {
364+ case list of {
365+ | Cons(x, Nil) => x
366+ | Cons(_, xs) => last(xs)
367+ }
368+}
369+
370+/%
371+Finds the first element for which `f` returns `Some`, and returns that result.
372+%/
373 pub fn find_map[a: utype, b: type, n: nat](list: !List[a, n], f: fn(a) -> maybe::Maybe[b]) -> maybe::Maybe[b] {
374 case list of {
375 | Nil => maybe::None
376 | Cons(x, xs) =>
377 case f(x) of {
378- | maybe::None => find_map(xs, f)
379+ | ~maybe::None => find_map(xs, f)
380 | res => res
381 }
382 }
383 }
384
385+/%
386+Finds the first element satisfying a predicate.
387+
388+Returns `Some(x)` for the first element where `f(x)` is `true`, or `None` if there is no such element.
389+%/
390 pub fn find[a: utype, n: nat](list: !List[a, n], f: fn(a) -> bool) -> maybe::Maybe[a] {
391 case list of {
392 | Nil => maybe::None
393@@ -160,16 +380,71 @@ pub fn find[a: utype, n: nat](list: !List[a, n], f: fn(a) -> bool) -> maybe::May
394 }
395 }
396
397+/%
398+Tests whether any element satisfies a predicate.
399+
400+Returns `true` if `f(x)` holds for at least one element, and `false` otherwise (including for the empty list).
401+%/
402 pub fn any[a: utype, n: nat](list: !List[a, n], f: fn(a) -> bool) -> bool {
403 case find(list, f) of {
404- | None => false
405- | _ => true
406+ | ~maybe::None => false
407+ | ~maybe::Some(_) => true
408 }
409 }
410
411+/%
412+Tests whether every element satisfies a predicate.
413+
414+Returns `true` if `f(x)` holds for all elements (including vacuously for the empty list)
415+%/
416 pub fn all[a: utype, n: nat](list: !List[a, n], f: fn(a) -> bool) -> bool {
417 case list of {
418 | Nil => true
419 | Cons(x, xs) => if bool::not(f(x)) then false else all(xs, f)
420 }
421 }
422+
423+/%
424+Returns `true` if every element of a boolean list is `true`.
425+
426+Returns `true` for the empty list.
427+%/
428+pub fn and[n: nat](list: !List[bool, n]) -> bool {
429+ all(list, fun::id)
430+}
431+
432+/%
433+Returns `true` if at least one element of a boolean list is `true`.
434+
435+Returns `false` for the empty list.
436+%/
437+pub fn or[n: nat](list: !List[bool, n]) -> bool {
438+ any(list, fun::id)
439+}
440+
441+/%
442+Tests whether a list has no elements.
443+%/
444+pub fn is_empty[a: type, n: nat](list: !List[a, n]) -> bool {
445+ case list of {
446+ | Nil => true
447+ | Cons(_, _) => false
448+ }
449+}
450+
451+/%
452+Splits a list into the elements that satisfy a predicate and those that don't.
453+%/
454+pub fn partition[a: utype, n: nat](list: List[a, n], f: fn(a) -> bool) -> [m: nat, p: nat] (List[a, m], List[a, p]) {
455+ case list of {
456+ | ~Nil => (Nil, Nil)
457+ | ~Cons(x, xs) => {
458+ val (pass, fails) = partition(xs, f)
459+ if f(x) then (Cons(x, pass), fails) else (pass, Cons(x, fails))
460+ }
461+ }
462+}
463+
464+fn main() -> int {
465+ 0
466+}
+65,
-17
1@@ -1,55 +1,103 @@
2-use <prelude.kav>
3+use "prelude.kav"
4 use "fun.kav" as fun
5
6-pub datatype Maybe[a:type] {
7+/%
8+An optional value: either `None`, or `Some(x)` holding a value of type `a`.
9+%/
10+pub linear datatype Maybe[a:type] {
11 | None
12 | Some(a)
13 }
14
15+pub fn free[a: utype](maybe: Maybe[a]) -> one {
16+ case maybe of {
17+ | ~Some(_) => one
18+ | ~None => one
19+ }
20+}
21+
22+/%
23+Tests whether two optional integers are equal.
24+%/
25 pub fn equal(maybe1: !Maybe[int], maybe2: !Maybe[int]) -> bool {
26 case (maybe1, maybe2) of {
27 | (None, None) => true
28 | (Some(x), Some(y)) => x == y
29- | (_, _) => false
30+ | (None, Some(_)) => false
31+ | (Some(_), None) => false
32 }
33 }
34
35+/%
36+Wraps `x` in `Some` if `cond` is `true`, and returns `None` otherwise.
37+%/
38 pub fn some_if[a: utype](cond: bool, x: a) -> Maybe[a] {
39 if cond then Some(x) else None
40 }
41
42-pub fn map[a: utype, b: type](maybe: !Maybe[a], f: fn(a) -> b) -> Maybe[b] {
43+/%
44+Applies `f` to the value inside a `Maybe`, if there is one.
45+%/
46+pub fn map[a: type, b: type](maybe: Maybe[a], f: fn(a) -> b) -> Maybe[b] {
47 case maybe of {
48- | None => None
49- | Some(x) => Some(f(x))
50+ | ~None => None
51+ | ~Some(x) => Some(f(x))
52 }
53 }
54
55-pub fn pure[a: utype](x: a) -> Maybe[a] {
56+/%
57+Wraps a value in `Some`.
58+%/
59+pub fn pure[a: type](x: a) -> Maybe[a] {
60 Some(x)
61 }
62
63-pub fn ap[a: utype, b: utype, c: utype](m1: Maybe[a], m2: Maybe[b], f: fn(a, b) -> c) -> Maybe[c] {
64+/%
65+Combines the values of two `Maybe`s using `f`.
66+
67+Returns `Some(f(x, y))` if both hold a value, and `None` if either is empty.
68+%/
69+pub fn zip_with[a: utype, b: utype, c: type](m1: Maybe[a], m2: Maybe[b], f: fn(a, b) -> c) -> Maybe[c] {
70 case (m1, m2) of {
71- | (Some(x), Some(y)) => Some(f(x, y))
72- | (_, _) => None
73+ | (~Some(x), ~Some(y)) => Some(f(x, y))
74+ | (x, y) => {
75+ val _ = free(x)
76+ val _ = free(y)
77+ None
78+ }
79 }
80 }
81
82-pub fn bind[a: utype, b: type](maybe: !Maybe[a], f: fn(a) -> Maybe[b]) -> Maybe[b] {
83+/%
84+Chains a computation that may itself fail.
85+%/
86+pub fn bind[a: type, b: type](maybe: Maybe[a], f: fn(a) -> Maybe[b]) -> Maybe[b] {
87 case maybe of {
88- | None => None
89- | Some(x) => f(x)
90+ | ~None => None
91+ | ~Some(x) => f(x)
92 }
93 }
94
95-pub fn value_map[a: utype, b: utype](default: b, maybe: !Maybe[a], f: fn(a) -> b) -> b {
96+/%
97+Eliminates a `Maybe` into a plain value.
98+
99+If it holds `x`, returns `f(x)`; if it is empty, returns
100+`default`.
101+%/
102+pub fn value_map[a: type, b: utype](default: b, maybe: Maybe[a], f: fn(a) -> b) -> b {
103 case maybe of {
104- | None => default
105- | Some(x) => f(x)
106+ | ~None => default
107+ | ~Some(x) => f(x)
108 }
109 }
110
111-pub fn value_or[a: utype](default: a, maybe: !Maybe[a]) -> a {
112+/%
113+Returns the value inside a `Maybe`, or `default` if it is empty.
114+%/
115+pub fn value_or[a: utype](default: a, maybe: Maybe[a]) -> a {
116 value_map(default, maybe, fun::id)
117 }
118+
119+fn main() -> int {
120+ 0
121+}
+50,
-7
1@@ -1,22 +1,65 @@
2-pub fn make[a: utype, b: utype](x: a, y: b) -> (a, b) {
3+use "bool.kav" as bool
4+
5+/%
6+Builds a pair from two values.
7+
8+Consumes `x` and `y` and moves them into the tuple.
9+%/
10+pub fn make[a: type, b: type](x: a, y: b) -> (a, b) {
11 (x, y)
12 }
13
14-pub fn fst[a: utype, b: utype](pair: (a, b)) -> a {
15+/%
16+Returns the first component of a pair.
17+%/
18+pub fn fst[a: utype, b: utype](pair: !(a, b)) -> a {
19 case pair of {
20- | ~(x, _) => x
21+ | (x, _) => x
22+ }
23+}
24+
25+/%
26+Returns the second component of a pair.
27+%/
28+pub fn snd[a: utype, b: utype](pair: !(a, b)) -> b {
29+ case pair of {
30+ | (_, x) => x
31+ }
32+}
33+
34+/%
35+Returns a new pair with the components swapped.
36+%/
37+pub fn swap[a: type, b: type](pair: (a, b)) -> (b, a) {
38+ case pair of {
39+ | ~(x, y) => (y, x)
40+ }
41+}
42+
43+/%
44+Tests whether two integer pairs are equal.
45+%/
46+pub fn equal(pair1: !(int, int), pair2: !(int, int)) -> bool {
47+ case (pair1, pair2) of {
48+ | ((x1, y1), (x2, y2)) => bool::and(x1 == x2, y1 == y2)
49 }
50 }
51
52-pub fn snd[a: utype, b: utype](pair: (a, b)) -> b {
53+/%
54+Applies `f` to the first element of a pair.
55+%/
56+pub fn map_fst[a: type, b: type, c: type](pair: (a, b), f: fn(a) -> c) -> (c, b) {
57 case pair of {
58- | ~(_, x) => x
59+ | ~(x, y) => (f(x), y)
60 }
61 }
62
63-pub fn swap[a: utype, b: utype](pair: (a, b)) -> (b, a) {
64+/%
65+Applies `f` to the second element of a pair.
66+%/
67+pub fn map_snd[a: type, b: type, c: type](pair: (a, b), f: fn(b) -> c) -> (a, c) {
68 case pair of {
69- | ~(x, y) => (y, x)
70+ | ~(x, y) => (x, f(y))
71 }
72 }
73
+97,
-30
1@@ -1,12 +1,18 @@
2-use <prelude.kav>
3+use "prelude.kav"
4 use "list.kav" as list
5 use "bool.kav" as bool
6
7+/%
8+A linear, size-indexed binary tree.
9+%/
10 pub linear datatype Tree[a: type, n: nat] {
11 | Leaf: Tree[a, 0]
12 | [n:nat, m:nat] Node(a, Tree[a, n], Tree[a, m]): Tree[a, m + n + 1]
13 }
14
15+/%
16+Deallocates a tree.
17+%/
18 pub fn free[a: utype, n: nat](tree: Tree[a, n]) -> one {
19 case tree of {
20 | ~Leaf => one
21@@ -18,6 +24,79 @@ pub fn free[a: utype, n: nat](tree: Tree[a, n]) -> one {
22 }
23 }
24
25+/%
26+Returns the number of nodes in a tree.
27+%/
28+pub fn size[a: type, n: nat](tree: !Tree[a, n]) -> int {
29+ case tree of {
30+ | Leaf => 0
31+ | Node(_, l, r) => 1 + size(l) + size(r)
32+ }
33+}
34+
35+/%
36+Returns the height of a tree.
37+%/
38+pub fn height[a: type, n: nat](tree: !Tree[a, n]) -> int {
39+ case tree of {
40+ | Leaf => 0
41+ | Node(_, l, r) => 1 + int::max(height(l), height(r))
42+ }
43+}
44+
45+/%
46+Mirrors a tree by swapping the left and right subtree of every node.
47+%/
48+pub fn mirror[a: type, n: nat](tree: Tree[a, n]) -> Tree[a, n] {
49+ case tree of {
50+ | ~Leaf => Leaf
51+ | ~Node(x, l, r) => Node(x, mirror(r), mirror(l))
52+ }
53+}
54+
55+/%
56+Returns the elements of a tree in pre-order.
57+%/
58+pub fn preorder_traversal[a: utype, n: nat](tree: !Tree[a, n]) -> list::List[a, n] {
59+ case tree of {
60+ | Leaf => list::Nil
61+ | Node(x, l, r) =>
62+ list::Cons(x, list::append(preorder_traversal(l), preorder_traversal(r)))
63+ }
64+}
65+
66+/%
67+Returns the elements of a tree in post-order.
68+%/
69+pub fn postorder_traversal[a: utype, n: nat](tree: !Tree[a, n]) -> list::List[a, n] {
70+ case tree of {
71+ | Leaf => list::Nil
72+ | Node(x, l, r) =>
73+ list::append(
74+ list::append(postorder_traversal(l), postorder_traversal(r)),
75+ list::Cons(x, list::Nil))
76+ }
77+}
78+
79+/%
80+Fold over a tree, traversing in in-order.
81+
82+For a tree whose in-order traversal is `[x1, x2, x3]` this computes
83+`f(f(f(init, x1), x2), x3)`.
84+%/
85+pub fn fold[a: utype, b: type, n: nat](tree: !Tree[a, n], init: b, f: fn(b, a) -> b) -> b {
86+ case tree of {
87+ | Leaf => init
88+ | Node(x, l, r) => {
89+ val acc = fold(l, init, f)
90+ fold(r, f(acc, x), f)
91+ }
92+ }
93+}
94+
95+/%
96+Returns the elements of a tree in in-order.
97+%/
98 pub fn inorder_traversal[a: utype, n: nat](tree: !Tree[a, n]) -> list::List[a, n] {
99 case tree of {
100 | Leaf => list::Nil
101@@ -25,13 +104,22 @@ pub fn inorder_traversal[a: utype, n: nat](tree: !Tree[a, n]) -> list::List[a, n
102 }
103 }
104
105-pub fn map[a: utype, b: type, n: nat](tree: !Tree[a, n], f: fn(a) -> b) -> Tree[b, n] {
106+/%
107+Returns a tree with the same shape, where each element is replaced by the result of applying `f`.
108+%/
109+pub fn map[a: type, b: type, n: nat](tree: Tree[a, n], f: fn(a) -> b) -> Tree[b, n] {
110 case tree of {
111- | Leaf => Leaf
112- | Node(x, l, r) => Node(f(x), map(l, f), map(r, f))
113+ | ~Leaf => Leaf
114+ | ~Node(x, l, r) => Node(f(x), map(l, f), map(r, f))
115 }
116 }
117
118+
119+/%
120+Returns the sum of the elements of an integer tree.
121+
122+The sum of the empty tree is `0`.
123+%/
124 pub fn sum[n: nat](tree: !Tree[int, n]) -> int {
125 case tree of {
126 | Leaf => 0
127@@ -39,6 +127,11 @@ pub fn sum[n: nat](tree: !Tree[int, n]) -> int {
128 }
129 }
130
131+/%
132+Tests whether two integer trees are equal.
133+
134+Two trees are equal if they have the same shape and their corresponding elements are equal.
135+%/
136 pub fn equal[n: nat](tree1: !Tree[int, n], tree2: !Tree[int, n]) -> bool {
137 case (tree1, tree2) of {
138 | (Leaf, Leaf) => true
139@@ -46,32 +139,6 @@ pub fn equal[n: nat](tree1: !Tree[int, n], tree2: !Tree[int, n]) -> bool {
140 }
141 }
142
143-/*fn main() -> int {
144- val left = Node(3, Leaf, Leaf)
145- val right = Node(1, Leaf, Leaf)
146- val tree = Node(2, left, right)
147- val expected = list::Cons(3, list::Cons(2, list::Cons(1, list::Nil)))
148- val traversal = inorder_traversal(tree)
149- val res = list::equal(expected, traversal)
150- val _ = list::free(traversal)
151- val _ = list::free(expected)
152- val _ = free(tree)
153- if res then 0 else 1
154-}*/
155-
156-/*fn main() -> int {
157- val left = Node(3, Leaf, Leaf)
158- val right = Node(1, Leaf, Leaf)
159- val tree1 = Node(2, left, right)
160- val left = Node(3, Leaf, Leaf)
161- val right = Node(1, Leaf, Leaf)
162- val tree2 = Node(2, left, right)
163- val res = equal(tree1, tree2)
164- val _ = free(tree1)
165- val _ = free(tree2)
166- if res then 0 else 1
167-}*/
168-
169 fn main() -> int {
170 val left = Node(3, Leaf, Leaf)
171 val right = Node(1, Leaf, Leaf)