commit f8b22a6
ricmaps
·
2026-11-23 19:23:15 +0000 UTC
parent 36ea487
more functions, new result module
8 files changed,
+435,
-107
+30,
-0
1@@ -0,0 +1,30 @@
2+use <list.kav> as list
3+use <fun.kav>
4+
5+fn sum_of_squares(n: int) -> int {
6+ val squares = list::rangeto(1, n) |> list::map(fn(n: int) -> int { n * n })
7+ val sum = list::sum(squares)
8+ val _ = list::free(squares)
9+ sum
10+}
11+
12+fn square_of_sum(n: int) -> int {
13+ val nums = list::rangeto(1, n)
14+ val sum = list::sum(nums)
15+ val _ = list::free(nums)
16+ sum * sum
17+}
18+
19+fn difference_of_squares(n: int) -> int {
20+ square_of_sum(n) - sum_of_squares(n)
21+}
22+
23+fn main() -> int {
24+ val test1 = difference_of_squares(0) == 0
25+ val test2 = difference_of_squares(3) == 22
26+ val test3 = difference_of_squares(5) == 170
27+ val tests = list::Cons(test1, list::Cons(test2, list::Cons(test3, list::Nil)))
28+ val res = list::and(tests)
29+ val _ = list::free(tests)
30+ if res then 0 else 1
31+}
+119,
-1
1@@ -1,4 +1,122 @@
2+use <prelude.kav>
3+use <maybe.kav> as maybe
4+use <list.kav> as list
5+
6+/%
7+A linear sum type: a value that is either a `Left` holding an `a`
8+or a `Right` holding a `b`.
9+
10+Neither side is privileged, this is a generic sum type, not an error type.
11+%/
12 pub linear datatype Either[a: type, b: type] {
13 | Left(a)
14 | Right(b)
15-}
16+}
17+
18+/%
19+Deallocates an either.
20+%/
21+pub fn free[a: utype, b: utype](e: Either[a, b]) -> one {
22+ case e of {
23+ | ~Left(_) => one
24+ | ~Right(_) => one
25+ }
26+}
27+
28+/%
29+Returns a copy of an either.
30+%/
31+pub fn copy[a: utype, b: utype](e: !Either[a, b]) -> Either[a, b] {
32+ case e of {
33+ | Left(x) => Left(x)
34+ | Right(y) => Right(y)
35+ }
36+}
37+
38+/%
39+Tests whether an either is a `Left`.
40+%/
41+pub fn isleft[a: type, b: type](e: !Either[a, b]) -> bool {
42+ case e of {
43+ | Left(_) => true
44+ | Right(_) => false
45+ }
46+}
47+
48+/%
49+Tests whether an either is a `Right`.
50+%/
51+pub fn isright[a: type, b: type](e: !Either[a, b]) -> bool {
52+ case e of {
53+ | Left(_) => false
54+ | Right(_) => true
55+ }
56+}
57+
58+/%
59+Swaps the sides of an either: `Left(x)` becomes `Right(x)` and vice versa.
60+%/
61+pub fn swap[a: type, b: type](e: Either[a, b]) -> Either[b, a] {
62+ case e of {
63+ | ~Left(x) => Right(x)
64+ | ~Right(y) => Left(y)
65+ }
66+}
67+
68+/%
69+Applies `f` to the content of a `Left`, leaving a `Right` untouched.
70+%/
71+pub fn mapleft[a: type, b: type, c: type](e: Either[a, b], f: fn(a) -> c) -> Either[c, b] {
72+ case e of {
73+ | ~Left(x) => Left(f(x))
74+ | ~Right(y) => Right(y)
75+ }
76+}
77+
78+/%
79+Applies `f` to the content of a `Right`, leaving a `Left` untouched.
80+%/
81+pub fn mapright[a: type, b: type, c: type](e: Either[a, b], f: fn(b) -> c) -> Either[a, c] {
82+ case e of {
83+ | ~Left(x) => Left(x)
84+ | ~Right(y) => Right(f(y))
85+ }
86+}
87+
88+/%
89+Applies `f` if the either is a `Left` and `g` if it is a `Right`,
90+keeping the side.
91+%/
92+pub fn map[a: type, b: type, c: type, d: type](e: Either[a, b], f: fn(a) -> c, g: fn(b) -> d) -> Either[c, d] {
93+ case e of {
94+ | ~Left(x) => Left(f(x))
95+ | ~Right(y) => Right(g(y))
96+ }
97+}
98+
99+/%
100+Collapses an either into a single value.
101+
102+Applies `f` if the either is a `Left` and `g` if it is a `Right`.
103+%/
104+pub fn fold[a: type, b: type, c: type](e: Either[a, b], f: fn(a) -> c, g: fn(b) -> c) -> c {
105+ case e of {
106+ | ~Left(x) => f(x)
107+ | ~Right(y) => g(y)
108+ }
109+}
110+
111+/%
112+Tests whether two eithers are equal.
113+
114+Two eithers are equal if they are on the same side and their contents are
115+equal according to `eqleft` (for `Left`) or `eqright` (for `Right`).
116+%/
117+pub fn equal[a: utype, b: utype](e1: !Either[a, b], e2: !Either[a, b], eqleft: fn(a, a) -> bool, eqright: fn(b, b) -> bool) -> bool {
118+ case (e1, e2) of {
119+ | (Left(x), Left(y)) => eqleft(x, y)
120+ | (Right(x), Right(y)) => eqright(x, y)
121+ | (Left(_), Right(_)) => false
122+ | (Right(_), Left(_)) => false
123+ }
124+}
+4,
-0
1@@ -1,3 +1,7 @@
2+pub fn equal(x: int, y: int) -> bool {
3+ x == y
4+}
5+
6 /%
7 Adds two ints.
8 %/
+139,
-87
1@@ -16,7 +16,7 @@ pub linear datatype List[a: type, n: nat] {
2 /%
3 A list known to be non-empty, i.e. has length `n + 1`.
4 %/
5-typedef List1[a: type, n: nat] = List[a, n + 1]
6+pub typedef List1[a: type, n: nat] = List[a, n + 1]
7
8 /%
9 Deallocates a list.
10@@ -29,16 +29,32 @@ pub fn free[a: utype, n: nat](list: List[a, n]) -> one {
11 }
12 }
13
14+fn copy_impl[a: utype, n: nat, m: nat](list: !List[a, n], acc: List[a, m]) -> List[a, n + m] {
15+ case list of {
16+ | Nil => acc
17+ | Cons(x, xs) => copy_impl(xs, Cons(x, acc))
18+ }
19+}
20+
21 /%
22 Returns a copy of a list
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+ copy_impl(list, Nil) |> reverse()
30 }
31
32+fn rangeto_impl[s: nat](n: int, m: int, acc: List[int, s]) -> [r: nat] List[int, r] {
33+ if n > m then acc
34+ else rangeto_impl(n + 1, m, Cons(n, acc))
35+}
36+
37+/%
38+Returns an inclusive strictly increasing range from `n` to `m`
39+%/
40+pub fn rangeto(n: int, m: int) -> [n: nat] List[int, n] {
41+ rangeto_impl(n, m, Nil) |> reverse()
42+}
43+
44 /%
45 Adds an element to the front of a list.
46
47@@ -55,19 +71,16 @@ Consumes both lists and returns a list containing the elements of `list1`
48 followed by the elements of `list2`.
49 %/
50 pub fn append[a: type, n: nat, m: nat](list1: List[a, n], list2: List[a, m]) -> List[a, m + n] {
51- case list1 of {
52- | ~Nil => list2
53- | ~Cons(x, xs) => Cons(x, append(xs, list2))
54- }
55+ reverse(list1) |> reverse_impl(list2)
56 }
57
58 /%
59 Tests whether two integer lists are equal.
60 %/
61-pub fn equal[n: nat](list1: !List[int, n], list2: !List[int, n]) -> bool {
62+pub fn equal[a: utype, n: nat](list1: !List[a, n], list2: !List[a, n], eq: fn(a, a) -> bool) -> bool {
63 case (list1, list2) of {
64 | (Nil, Nil) => true
65- | (Cons(x, xs), Cons(y, ys)) => if x == y then equal(xs, ys) else false
66+ | (Cons(x, xs), Cons(y, ys)) => if eq(x, y) then equal(xs, ys, eq) else false
67 }
68 }
69
70@@ -78,7 +91,6 @@ fn length_impl[a: type, n: nat](list: !List[a, n], acc: int) -> int {
71 }
72 }
73
74-
75 /%
76 Returns the number of elements in a list.
77 %/
78@@ -86,14 +98,27 @@ pub fn length[a: type, n: nat](list: !List[a, n]) -> int {
79 length_impl(list, 0)
80 }
81
82+// underwhelming type for the index
83+pub fn index[a: utype, n: nat](list: !List[a, n], idx: int) -> maybe::Maybe[a] {
84+ case (list, idx) of {
85+ | (Nil, _) => maybe::None
86+ | (Cons(x, _), 0) => maybe::Some(x)
87+ | (Cons(x, xs), i) => index(xs, i - 1)
88+ }
89+}
90+
91+fn map_impl[a: type, b: type, n: nat, m: nat](list: List[a, n], f: fn(a) -> b, acc: List[b, m]) -> List[b, n + m] {
92+ case list of {
93+ | ~Nil => acc
94+ | ~Cons(x, xs) => map_impl(xs, f, Cons(f(x), acc))
95+ }
96+}
97+
98 /%
99 Applies `f` to every element of a list.
100 %/
101 pub fn map[a: type, b: type, n: nat](list: List[a, n], f: fn(a) -> b) -> List[b, n] {
102- case list of {
103- | ~Nil => Nil
104- | ~Cons(x, xs) => Cons(f(x), map(xs, f))
105- }
106+ map_impl(list, f, Nil) |> reverse()
107 }
108
109 /%
110@@ -109,16 +134,30 @@ pub fn iter[a: utype, n: nat](list: !List[a, n], f: fn(a) -> one) -> one {
111 }
112 }
113
114+fn filter_impl[a: utype, n: nat, m: nat](list: List[a, n], f: fn(a) -> bool, acc: List[a, m]) -> [k: nat] List[a, k] {
115+ case list of {
116+ | ~Nil => acc
117+ | ~Cons(x, xs) =>
118+ if f(x) then filter_impl(xs, f, Cons(x, acc))
119+ else filter_impl(xs, f, acc)
120+ }
121+}
122+
123 /%
124 Keeps only the elements for which `f` returns `true`.
125 %/
126 pub fn filter[a: utype, n: nat](list: List[a, n], f: fn(a) -> bool) -> [m: nat] List[a, m] {
127+ filter_impl(list, f, Nil) |> reverse()
128+}
129+
130+fn filtermap_impl[a: type, b: type, n: nat, m: nat](list: List[a, n], f: fn(a) -> maybe::Maybe[b], acc: List[b, m]) -> [k: nat] List[b, k] {
131 case list of {
132- | ~Nil => Nil
133- | ~Cons(x, xs) => {
134- val rec = filter(xs, f)
135- if f(x) then Cons(x, rec) else rec
136- }
137+ | ~Nil => acc
138+ | ~Cons(x, xs) =>
139+ case f(x) of {
140+ | ~maybe::None => filtermap_impl(xs, f, acc)
141+ | ~maybe::Some(y) => filtermap_impl(xs, f, Cons(y, acc))
142+ }
143 }
144 }
145
146@@ -128,17 +167,8 @@ Maps and filters a list in a single pass.
147 Applies `f` to each element. If `f` returns `Some(y)`, `y` is included in the
148 result; if it returns `None`, the element is dropped.
149 %/
150-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] {
151- case list of {
152- | ~Nil => Nil
153- | ~Cons(x, xs) => {
154- val rec = filter_map(xs, f)
155- case f(x) of {
156- | ~maybe::None => rec
157- | ~maybe::Some(y) => Cons(y, rec)
158- }
159- }
160- }
161+pub fn filtermap[a: type, b: type, n: nat](list: List[a, n], f: fn(a) -> maybe::Maybe[b]) -> [m: nat] List[b, m] {
162+ filtermap_impl(list, f, Nil) |> reverse()
163 }
164
165 /%
166@@ -166,19 +196,34 @@ pub fn foldr[a: utype, b: type, n: nat](list: !List[a, n], init: b, f: fn(a, b)
167 }
168 }
169
170+fn span_impl[a: utype, n: nat, m: nat](list: List[a, n], f: fn(a) -> bool, acc: List[a, m]) -> [k: nat, p: nat] (List[a, k], List[a, p]) {
171+ case list of {
172+ | ~Nil => (acc, Nil)
173+ | ~Cons(x, xs) =>
174+ if f(x) then span_impl(xs, f, Cons(x, acc))
175+ else (acc, Cons(x, xs))
176+ }
177+}
178+
179 /%
180 Splits a list at the first element that fails a predicate.
181
182 Equivalent to `(take_while(list, f), drop_while(list, f))`.
183 %/
184 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]) {
185+ val res = span_impl(list, f, Nil)
186+ pair::mapfst(res, reverse)
187+}
188+
189+fn takewhile_impl[a: utype, n: nat, m: nat](list: List[a, n], f: fn(a) -> bool, acc: List[a, m]) -> [k: nat] List[a, k] {
190 case list of {
191- | ~Nil => (Nil, Nil)
192+ | ~Nil => acc
193 | ~Cons(x, xs) =>
194- if f(x) then {
195- val (prefix, suffix) = span(xs, f)
196- (Cons(x, prefix), suffix)
197- } else (Nil, Cons(x, xs))
198+ if f(x) then takewhile_impl(xs, f, Cons(x, acc))
199+ else {
200+ val _ = free(xs)
201+ acc
202+ }
203 }
204 }
205
206@@ -188,26 +233,18 @@ Keeps the longest prefix of a list whose elements all satisfy `f`.
207 Stops at the first element for which `f` returns `false`,
208 that element and everything after it is deallocated.
209 %/
210-pub fn take_while[a: utype, n: nat](list: List[a, n], f: fn(a) -> bool) -> [m: nat] List[a, m] {
211- case list of {
212- | ~Nil => Nil
213- | ~Cons(x, xs) =>
214- if f(x) then Cons(x, take_while(xs, f))
215- else {
216- val _ = free(xs)
217- Nil
218- }
219- }
220+pub fn takewhile[a: utype, n: nat](list: List[a, n], f: fn(a) -> bool) -> [m: nat] List[a, m] {
221+ takewhile_impl(list, f, Nil) |> reverse()
222 }
223
224 /%
225 Removes the longest prefix of a list whose elements all satisfy `f`.
226 %/
227-pub fn drop_while[a: utype, n: nat](list: List[a, n], f: fn(a) -> bool) -> [m: nat] List[a, m] {
228+pub fn dropwhile[a: utype, n: nat](list: List[a, n], f: fn(a) -> bool) -> [m: nat] List[a, m] {
229 case list of {
230 | ~Nil => Nil
231 | ~Cons(x, xs) =>
232- if f(x) then drop_while(xs, f) else Cons(x, xs)
233+ if f(x) then dropwhile(xs, f) else Cons(x, xs)
234 }
235 }
236
237@@ -247,16 +284,31 @@ pub fn product[n: nat](list: !List[int, n]) -> int {
238 foldl(list, 1, int::mul)
239 }
240
241+fn scanl_impl[a: utype, b: utype, n: nat, m: nat](list: List[a, n], cur: b, f: fn(b, a) -> b, acc: List[b, m]) -> List[b, n + m + 1] {
242+ case list of {
243+ | ~Nil => Cons(cur, acc)
244+ | ~Cons(x, xs) => scanl_impl(xs, f(cur, x), f, Cons(cur, acc))
245+ }
246+}
247+
248 /%
249 Left scan: like `foldl`, but keeps every intermediate accumulator.
250
251 For a list `[x1, x2, x3]` this returns
252 `[init, f(init, x1), f(f(init, x1), x2), f(f(f(init, x1), x2), x3)]`.
253 %/
254-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] {
255+pub fn scanl[a: utype, b: utype, n: nat](list: List[a, n], init: b, f: fn(b, a) -> b) -> List[b, n + 1] {
256+ scanl_impl(list, init, f, Nil) |> reverse()
257+}
258+
259+/* `list` must already be reversed: elements are consumed from the last to the first. */
260+fn scanr_impl[a: utype, b: utype, n: nat, m: nat](list: List[a, n], f: fn(a, b) -> b, acc: List[b, m + 1]) -> List[b, n + m + 1] {
261 case list of {
262- | ~Nil => Cons(init, Nil)
263- | ~Cons(x, xs) => Cons(init, scan_left(xs, f(init, x), f))
264+ | ~Nil => acc
265+ | ~Cons(x, xs) => {
266+ val y = f(x, head(acc))
267+ scanr_impl(xs, f, Cons(y, acc))
268+ }
269 }
270 }
271
272@@ -266,15 +318,8 @@ Right scan: like `foldr`, but keeps every intermediate accumulator.
273 For a list `[x1, x2, x3]` this returns
274 `[f(x1, f(x2, f(x3, init))), f(x2, f(x3, init)), f(x3, init), init]`.
275 %/
276-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] {
277- case list of {
278- | ~Nil => Cons(init, Nil)
279- | ~Cons(x, xs) => {
280- val rest = scan_right(xs, init, f)
281- val y = f(x, head(rest))
282- Cons(y, rest)
283- }
284- }
285+pub fn scanr[a: utype, b: utype, n: nat](list: List[a, n], init: b, f: fn(a, b) -> b) -> List[b, n + 1] {
286+ reverse(list) |> scanr_impl(f, Cons(init, Nil))
287 }
288
289 fn reverse_impl[a: type,n: nat, m: nat](l: List[a, n], acc: List[a, m]) -> List[a, n + m] {
290@@ -291,24 +336,35 @@ pub fn reverse[a: type, n: nat](list: List[a, n]) -> List[a, n] {
291 reverse_impl(list, Nil)
292 }
293
294+fn zipwith_impl[a: type, b: type, c: type, n: nat, m: nat](list1: List[a, n], list2: List[b, n], f: fn(a, b) -> c, acc: List[c, m]) -> List[c, n + m] {
295+ case (list1, list2) of {
296+ | (~Nil, ~Nil) => acc
297+ | (~Cons(x, xs), ~Cons(y, ys)) => zipwith_impl(xs, ys, f, Cons(f(x, y), acc))
298+ }
299+}
300+
301 /%
302 Combines two lists of same length element-wise using `f`.
303
304 The `i`-th element of the result is `f(x, y)`, where `x` and `y` are the `i`-th elements of
305 the first and second list respectively.
306 %/
307-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] {
308- case (list1, list2) of {
309- | (~Nil, ~Nil) => Nil
310- | (~Cons(x, xs), ~Cons(y, ys)) => Cons(f(x, y), zip_with(xs, ys, f))
311- }
312+pub fn zipwith[a: type, b: type, c: type, n: nat](list1: List[a, n], list2: List[b, n], f: fn(a, b) -> c) -> List[c, n] {
313+ zipwith_impl(list1, list2, f, Nil) |> reverse()
314 }
315
316 /%
317 Pairs up the elements of two lists of same length.
318 %/
319 pub fn zip[a: type, b: type, n: nat](list1: List[a, n], list2: List[b, n]) -> List[(a, b), n] {
320- zip_with(list1, list2, pair::make)
321+ zipwith(list1, list2, pair::make)
322+}
323+
324+fn unzip_impl[a: type, b: type, n: nat, m: nat](list: List[(a, b), n], xs: List[a, m], ys: List[b, m]) -> (List[a, n + m], List[b, n + m]) {
325+ case list of {
326+ | ~Nil => (xs, ys)
327+ | ~Cons((x, y), rest) => unzip_impl(rest, Cons(x, xs), Cons(y, ys))
328+ }
329 }
330
331 /%
332@@ -317,12 +373,8 @@ Splits a list of pairs into a pair of lists.
333 This is the inverse of `zip`.
334 %/
335 pub fn unzip[a: type, b: type, n: nat](list: List[(a, b), n]) -> (List[a, n], List[b, n]) {
336- case list of {
337- | ~Nil => (Nil, Nil)
338- | ~Cons((x, y), rest) =>
339- case unzip(rest) of {
340- | ~(xs, ys) => (Cons(x, xs), Cons(y, ys))
341- }
342+ case unzip_impl(list, Nil, Nil) of {
343+ | ~(xs, ys) => (reverse(xs), reverse(ys))
344 }
345 }
346
347@@ -357,12 +409,12 @@ pub fn last[a: utype, n: nat](list: !List1[a, n]) -> a {
348 /%
349 Finds the first element for which `f` returns `Some`, and returns that result.
350 %/
351-pub fn find_map[a: utype, b: type, n: nat](list: !List[a, n], f: fn(a) -> maybe::Maybe[b]) -> maybe::Maybe[b] {
352+pub fn findmap[a: utype, b: type, n: nat](list: !List[a, n], f: fn(a) -> maybe::Maybe[b]) -> maybe::Maybe[b] {
353 case list of {
354 | Nil => maybe::None
355 | Cons(x, xs) =>
356 case f(x) of {
357- | ~maybe::None => find_map(xs, f)
358+ | ~maybe::None => findmap(xs, f)
359 | res => res
360 }
361 }
362@@ -425,26 +477,26 @@ pub fn or[n: nat](list: !List[bool, n]) -> bool {
363 /%
364 Tests whether a list has no elements.
365 %/
366-pub fn is_empty[a: type, n: nat](list: !List[a, n]) -> bool {
367+pub fn empty[a: type, n: nat](list: !List[a, n]) -> bool {
368 case list of {
369 | Nil => true
370 | Cons(_, _) => false
371 }
372 }
373
374-/%
375-Splits a list into the elements that satisfy a predicate and those that don't.
376-%/
377-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]) {
378+fn partition_impl[a: utype, n: nat, m: nat, p: nat](list: List[a, n], f: fn(a) -> bool, pass: List[a, m], fails: List[a, p]) -> [k: nat, q: nat] (List[a, k], List[a, q]) {
379 case list of {
380- | ~Nil => (Nil, Nil)
381- | ~Cons(x, xs) => {
382- val (pass, fails) = partition(xs, f)
383- if f(x) then (Cons(x, pass), fails) else (pass, Cons(x, fails))
384- }
385+ | ~Nil => (pass, fails)
386+ | ~Cons(x, xs) =>
387+ if f(x) then partition_impl(xs, f, Cons(x, pass), fails)
388+ else partition_impl(xs, f, pass, Cons(x, fails))
389 }
390 }
391
392-fn main() -> int {
393- 0
394+/%
395+Splits a list into the elements that satisfy a predicate and those that don't.
396+%/
397+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]) {
398+ val (pass, fails) = partition_impl(list, f, Nil, Nil)
399+ (reverse(pass), reverse(fails))
400 }
+4,
-8
1@@ -57,7 +57,7 @@ Combines the values of two `Maybe`s using `f`.
2
3 Returns `Some(f(x, y))` if both hold a value, and `None` if either is empty.
4 %/
5-pub fn zip_with[a: utype, b: utype, c: type](m1: Maybe[a], m2: Maybe[b], f: fn(a, b) -> c) -> Maybe[c] {
6+pub fn map2[a: utype, b: utype, c: type](m1: Maybe[a], m2: Maybe[b], f: fn(a, b) -> c) -> Maybe[c] {
7 case (m1, m2) of {
8 | (~Some(x), ~Some(y)) => Some(f(x, y))
9 | (x, y) => {
10@@ -84,7 +84,7 @@ Eliminates a `Maybe` into a plain value.
11 If it holds `x`, returns `f(x)`; if it is empty, returns
12 `default`.
13 %/
14-pub fn value_map[a: type, b: utype](default: b, maybe: Maybe[a], f: fn(a) -> b) -> b {
15+pub fn fold[a: type, b: utype](default: b, maybe: Maybe[a], f: fn(a) -> b) -> b {
16 case maybe of {
17 | ~None => default
18 | ~Some(x) => f(x)
19@@ -94,10 +94,6 @@ pub fn value_map[a: type, b: utype](default: b, maybe: Maybe[a], f: fn(a) -> b)
20 /%
21 Returns the value inside a `Maybe`, or `default` if it is empty.
22 %/
23-pub fn value_or[a: utype](default: a, maybe: Maybe[a]) -> a {
24- value_map(default, maybe, fun::id)
25-}
26-
27-fn main() -> int {
28- 0
29+pub fn value[a: utype](default: a, maybe: Maybe[a]) -> a {
30+ fold(default, maybe, fun::id)
31 }
+11,
-2
1@@ -48,7 +48,7 @@ pub fn equal(pair1: !(int, int), pair2: !(int, int)) -> bool {
2 /%
3 Applies `f` to the first element of a pair.
4 %/
5-pub fn map_fst[a: type, b: type, c: type](pair: (a, b), f: fn(a) -> c) -> (c, b) {
6+pub fn mapfst[a: type, b: type, c: type](pair: (a, b), f: fn(a) -> c) -> (c, b) {
7 case pair of {
8 | ~(x, y) => (f(x), y)
9 }
10@@ -57,9 +57,18 @@ pub fn map_fst[a: type, b: type, c: type](pair: (a, b), f: fn(a) -> c) -> (c, b)
11 /%
12 Applies `f` to the second element of a pair.
13 %/
14-pub fn map_snd[a: type, b: type, c: type](pair: (a, b), f: fn(b) -> c) -> (a, c) {
15+pub fn mapsnd[a: type, b: type, c: type](pair: (a, b), f: fn(b) -> c) -> (a, c) {
16 case pair of {
17 | ~(x, y) => (x, f(y))
18 }
19 }
20
21+/%
22+Applies `f` to the first and `g` to the second component of a pair.
23+%/
24+pub fn map[a: type, b: type, c: type, d: type](pair: (a, b), f: fn(a) -> c, g: fn(b) -> d) -> (c, d) {
25+ case pair of {
26+ | ~(x, y) => (f(x), g(y))
27+ }
28+}
29+
+127,
-0
1@@ -0,0 +1,127 @@
2+use <prelude.kav>
3+use <maybe.kav> as maybe
4+
5+/%
6+A linear result type: either `Ok` holding a value of type `a`,
7+or `Err` holding an error of type `e`.
8+%/
9+pub linear datatype Result[a: type, e: type] {
10+ | Ok(a)
11+ | Err(e)
12+}
13+
14+/%
15+Deallocates a result.
16+%/
17+pub fn free[a: utype, e: utype](r: Result[a, e]) -> one {
18+ case r of {
19+ | ~Ok(_) => one
20+ | ~Err(_) => one
21+ }
22+}
23+
24+/%
25+Returns a copy of a result.
26+%/
27+pub fn copy[a: utype, e: utype](r: !Result[a, e]) -> Result[a, e] {
28+ case r of {
29+ | Ok(x) => Ok(x)
30+ | Err(y) => Err(y)
31+ }
32+}
33+
34+/%
35+Tests whether a result is an `Ok`.
36+%/
37+pub fn isok[a: type, e: type](r: !Result[a, e]) -> bool {
38+ case r of {
39+ | Ok(_) => true
40+ | Err(_) => false
41+ }
42+}
43+
44+/%
45+Tests whether a result is an `Err`.
46+%/
47+pub fn iserr[a: type, e: type](r: !Result[a, e]) -> bool {
48+ case r of {
49+ | Ok(_) => false
50+ | Err(_) => true
51+ }
52+}
53+
54+/%
55+Returns the value of an `Ok`, or `default` if the result is an `Err`.
56+%/
57+pub fn okor[a: utype, e: utype](r: Result[a, e], default: a) -> a {
58+ case r of {
59+ | ~Ok(x) => x
60+ | ~Err(_) => default
61+ }
62+}
63+
64+/%
65+Applies `f` to the value of an `Ok`, leaving an `Err` untouched.
66+%/
67+pub fn map[a: type, b: type, e: type](r: Result[a, e], f: fn(a) -> b) -> Result[b, e] {
68+ case r of {
69+ | ~Ok(x) => Ok(f(x))
70+ | ~Err(y) => Err(y)
71+ }
72+}
73+
74+/%
75+Applies `f` to the error of an `Err`, leaving an `Ok` untouched.
76+%/
77+pub fn maperr[a: type, e: type, d: type](r: Result[a, e], f: fn(e) -> d) -> Result[a, d] {
78+ case r of {
79+ | ~Ok(x) => Ok(x)
80+ | ~Err(y) => Err(f(y))
81+ }
82+}
83+
84+/%
85+Chains a computation that may fail.
86+
87+If the result is `Ok(x)`, returns `f(x)`; if it is an `Err`, returns it unchanged.
88+%/
89+pub fn bind[a: type, b: type, e: type](r: Result[a, e], f: fn(a) -> Result[b, e]) -> Result[b, e] {
90+ case r of {
91+ | ~Ok(x) => f(x)
92+ | ~Err(y) => Err(y)
93+ }
94+}
95+
96+/%
97+Flattens a nested result.
98+%/
99+pub fn join[a: type, e: type](r: Result[Result[a, e], e]) -> Result[a, e] {
100+ case r of {
101+ | ~Ok(inner) => inner
102+ | ~Err(y) => Err(y)
103+ }
104+}
105+
106+/%
107+Collapses a result into a single value.
108+
109+Applies `f` if the result is an `Ok` and `g` if it is an `Err`.
110+%/
111+pub fn fold[a: type, e: type, c: type](r: Result[a, e], f: fn(a) -> c, g: fn(e) -> c) -> c {
112+ case r of {
113+ | ~Ok(x) => f(x)
114+ | ~Err(y) => g(y)
115+ }
116+}
117+
118+/%
119+Tests whether two results are equal.
120+%/
121+pub fn equal[a: utype, e: utype](r1: !Result[a, e], r2: !Result[a, e], eqok: fn(a, a) -> bool, eqerr: fn(e, e) -> bool) -> bool {
122+ case (r1, r2) of {
123+ | (Ok(x), Ok(y)) => eqok(x, y)
124+ | (Err(x), Err(y)) => eqerr(x, y)
125+ | (Ok(_), Err(_)) => false
126+ | (Err(_), Ok(_)) => false
127+ }
128+}
+1,
-9
1@@ -1,6 +1,7 @@
2 use <prelude.kav>
3 use <list.kav> as list
4 use <bool.kav> as bool
5+use <int.kav> as int
6
7 /%
8 A linear, size-indexed binary tree.
9@@ -138,12 +139,3 @@ pub fn equal[n: nat](tree1: !Tree[int, n], tree2: !Tree[int, n]) -> bool {
10 | (Node(x1, l1, r1), Node(x2, l2, r2)) => bool::and(x1 == x2, bool::and(equal(l1, l2), equal(r1, r2)))
11 }
12 }
13-
14-fn main() -> int {
15- val left = Node(3, Leaf, Leaf)
16- val right = Node(1, Leaf, Leaf)
17- val tree = Node(2, left, right)
18- val sum = sum(tree)
19- val _ = free(tree)
20- sum
21-}