1use <prelude.kav>
2use <bool.kav> as bool
3use <pair.kav> as pair
4use <maybe.kav> as maybe
5use <int.kav> as int
6use <fun.kav> as fun
7
8/%
9A linear, length-indexed list.
10%/
11pub linear datatype List[a: type, n: nat] {
12 | Nil : List[a, 0]
13 | [m: nat] Cons(a, List[a, m]) : List[a, m+1]
14}
15
16/%
17A list known to be non-empty, i.e. has length `n + 1`.
18%/
19pub typedef List1[a: type, n: nat] = List[a, n + 1]
20
21/%
22Deallocates a list.
23%/
24pub fn free[a: utype, n: nat](list: List[a, n]) -> one {
25 case list of {
26 /* ~ consumes the list wrapper */
27 | ~Nil => one
28 | ~Cons(_, rest) => free(rest)
29 }
30}
31
32fn copy_impl[a: utype, n: nat, m: nat](list: !List[a, n], acc: List[a, m]) -> List[a, n + m] {
33 case list of {
34 | Nil => acc
35 | Cons(x, xs) => copy_impl(xs, Cons(x, acc))
36 }
37}
38
39/%
40Returns a copy of a list
41%/
42pub fn copy[a: utype, n: nat](list: !List[a, n]) -> List[a, n] {
43 copy_impl(list, Nil) |> reverse()
44}
45
46fn rangeto_impl[s: nat](n: int, m: int, acc: List[int, s]) -> [r: nat] List[int, r] {
47 if n > m then acc
48 else rangeto_impl(n + 1, m, Cons(n, acc))
49}
50
51/%
52Returns an inclusive strictly increasing range from `n` to `m`
53%/
54pub fn rangeto(n: int, m: int) -> [n: nat] List[int, n] {
55 rangeto_impl(n, m, Nil) |> reverse()
56}
57
58/%
59Adds an element to the front of a list.
60
61Consumes `list` and returns a list of length `n + 1` with `x` as its head.
62%/
63pub fn cons[a: type, n: nat](x: a, list: List[a, n]) -> List[a, n + 1] {
64 Cons(x, list)
65}
66
67/%
68Concatenates two lists.
69
70Consumes both lists and returns a list containing the elements of `list1`
71followed by the elements of `list2`.
72%/
73pub fn append[a: type, n: nat, m: nat](list1: List[a, n], list2: List[a, m]) -> List[a, m + n] {
74 reverse(list1) |> reverse_impl(list2)
75}
76
77/%
78Tests whether two integer lists are equal.
79%/
80pub fn equal[a: utype, n: nat](list1: !List[a, n], list2: !List[a, n], eq: fn(a, a) -> bool) -> bool {
81 case (list1, list2) of {
82 | (Nil, Nil) => true
83 | (Cons(x, xs), Cons(y, ys)) => if eq(x, y) then equal(xs, ys, eq) else false
84 }
85}
86
87fn length_impl[a: type, n: nat](list: !List[a, n], acc: int) -> int {
88 case list of {
89 | Nil => acc
90 | Cons(_, xs) => length_impl(xs, acc + 1)
91 }
92}
93
94/%
95Returns the number of elements in a list.
96%/
97pub fn length[a: type, n: nat](list: !List[a, n]) -> int {
98 length_impl(list, 0)
99}
100
101// underwhelming type for the index
102pub fn index[a: utype, n: nat](list: !List[a, n], idx: int) -> maybe::Maybe[a] {
103 case (list, idx) of {
104 | (Nil, _) => maybe::None
105 | (Cons(x, _), 0) => maybe::Some(x)
106 | (Cons(x, xs), i) => index(xs, i - 1)
107 }
108}
109
110fn 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] {
111 case list of {
112 | ~Nil => acc
113 | ~Cons(x, xs) => map_impl(xs, f, Cons(f(x), acc))
114 }
115}
116
117/%
118Applies `f` to every element of a list.
119%/
120pub fn map[a: type, b: type, n: nat](list: List[a, n], f: fn(a) -> b) -> List[b, n] {
121 map_impl(list, f, Nil) |> reverse()
122}
123
124/%
125Calls `f` on every element of a list, in order, for its side effects.
126%/
127pub fn iter[a: utype, n: nat](list: !List[a, n], f: fn(a) -> one) -> one {
128 case list of {
129 | Nil => one
130 | Cons(x, xs) => {
131 val _ = f(x)
132 iter(xs, f)
133 }
134 }
135}
136
137fn 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] {
138 case list of {
139 | ~Nil => acc
140 | ~Cons(x, xs) =>
141 if f(x) then filter_impl(xs, f, Cons(x, acc))
142 else filter_impl(xs, f, acc)
143 }
144}
145
146/%
147Keeps only the elements for which `f` returns `true`.
148%/
149pub fn filter[a: utype, n: nat](list: List[a, n], f: fn(a) -> bool) -> [m: nat] List[a, m] {
150 filter_impl(list, f, Nil) |> reverse()
151}
152
153fn 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] {
154 case list of {
155 | ~Nil => acc
156 | ~Cons(x, xs) =>
157 case f(x) of {
158 | ~maybe::None => filtermap_impl(xs, f, acc)
159 | ~maybe::Some(y) => filtermap_impl(xs, f, Cons(y, acc))
160 }
161 }
162}
163
164/%
165Maps and filters a list in a single pass.
166
167Applies `f` to each element. If `f` returns `Some(y)`, `y` is included in the
168result; if it returns `None`, the element is dropped.
169%/
170pub fn filtermap[a: type, b: type, n: nat](list: List[a, n], f: fn(a) -> maybe::Maybe[b]) -> [m: nat] List[b, m] {
171 filtermap_impl(list, f, Nil) |> reverse()
172}
173
174/%
175Left fold over a list.
176
177For a list `[x1, x2, x3]` this computes `f(f(f(init, x1), x2), x3)`.
178%/
179pub fn foldl[a: utype, b: type, n: nat](list: !List[a, n], init: b, f: fn(b, a) -> b) -> b {
180 case list of {
181 | Nil => init
182 | Cons(x, xs) => foldl(xs, f(init, x), f)
183 }
184}
185
186/%
187Right fold over a list.
188
189For a list `[x1, x2, x3]` this computes `f(x1, f(x2, f(x3, init)))`.
190Not tail-recursive.
191%/
192pub fn foldr[a: utype, b: type, n: nat](list: !List[a, n], init: b, f: fn(a, b) -> b) -> b {
193 case list of {
194 | Nil => init
195 | Cons(x, xs) => f(x, foldr(xs, init, f))
196 }
197}
198
199fn 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]) {
200 case list of {
201 | ~Nil => (acc, Nil)
202 | ~Cons(x, xs) =>
203 if f(x) then span_impl(xs, f, Cons(x, acc))
204 else (acc, Cons(x, xs))
205 }
206}
207
208/%
209Splits a list at the first element that fails a predicate.
210
211Equivalent to `(take_while(list, f), drop_while(list, f))`.
212%/
213pub fn span[a: utype, n: nat](list: List[a, n], f: fn(a) -> bool) -> [m: nat, p: nat] (List[a, m], List[a, p]) {
214 val res = span_impl(list, f, Nil)
215 pair::mapfst(res, reverse)
216}
217
218fn 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] {
219 case list of {
220 | ~Nil => acc
221 | ~Cons(x, xs) =>
222 if f(x) then takewhile_impl(xs, f, Cons(x, acc))
223 else {
224 val _ = free(xs)
225 acc
226 }
227 }
228}
229
230/%
231Keeps the longest prefix of a list whose elements all satisfy `f`.
232
233Stops at the first element for which `f` returns `false`,
234that element and everything after it is deallocated.
235%/
236pub fn takewhile[a: utype, n: nat](list: List[a, n], f: fn(a) -> bool) -> [m: nat] List[a, m] {
237 takewhile_impl(list, f, Nil) |> reverse()
238}
239
240/%
241Removes the longest prefix of a list whose elements all satisfy `f`.
242%/
243pub fn dropwhile[a: utype, n: nat](list: List[a, n], f: fn(a) -> bool) -> [m: nat] List[a, m] {
244 case list of {
245 | ~Nil => Nil
246 | ~Cons(x, xs) =>
247 if f(x) then dropwhile(xs, f) else Cons(x, xs)
248 }
249}
250
251/%
252Returns the largest element of a non-empty integer list.
253%/
254pub fn maximum[n: nat](list: !List1[int, n]) -> int {
255 case list of {
256 | Cons(x, xs) => foldl(xs, x, int::max)
257 }
258}
259
260/%
261Returns the smallest element of a non-empty integer list.
262%/
263pub fn minimum[n: nat](list: !List1[int, n]) -> int {
264 case list of {
265 | Cons(x, xs) => foldl(xs, x, int::min)
266 }
267}
268
269/%
270Returns the sum of the elements of an integer list.
271
272The sum of the empty list is `0`.
273%/
274pub fn sum[n: nat](list: !List[int, n]) -> int {
275 foldl(list, 0, int::add)
276}
277
278/%
279Returns the product of the elements of an integer list.
280
281The product of the empty list is `1`.
282%/
283pub fn product[n: nat](list: !List[int, n]) -> int {
284 foldl(list, 1, int::mul)
285}
286
287fn 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] {
288 case list of {
289 | ~Nil => Cons(cur, acc)
290 | ~Cons(x, xs) => scanl_impl(xs, f(cur, x), f, Cons(cur, acc))
291 }
292}
293
294/%
295Left scan: like `foldl`, but keeps every intermediate accumulator.
296
297For a list `[x1, x2, x3]` this returns
298`[init, f(init, x1), f(f(init, x1), x2), f(f(f(init, x1), x2), x3)]`.
299%/
300pub fn scanl[a: utype, b: utype, n: nat](list: List[a, n], init: b, f: fn(b, a) -> b) -> List[b, n + 1] {
301 scanl_impl(list, init, f, Nil) |> reverse()
302}
303
304/* `list` must already be reversed: elements are consumed from the last to the first. */
305fn 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] {
306 case list of {
307 | ~Nil => acc
308 | ~Cons(x, xs) => {
309 val y = f(x, head(acc))
310 scanr_impl(xs, f, Cons(y, acc))
311 }
312 }
313}
314
315/%
316Right scan: like `foldr`, but keeps every intermediate accumulator.
317
318For a list `[x1, x2, x3]` this returns
319`[f(x1, f(x2, f(x3, init))), f(x2, f(x3, init)), f(x3, init), init]`.
320%/
321pub fn scanr[a: utype, b: utype, n: nat](list: List[a, n], init: b, f: fn(a, b) -> b) -> List[b, n + 1] {
322 reverse(list) |> scanr_impl(f, Cons(init, Nil))
323}
324
325fn reverse_impl[a: type,n: nat, m: nat](l: List[a, n], acc: List[a, m]) -> List[a, n + m] {
326 case l of {
327 | ~Nil => acc
328 | ~Cons(entry, rest) => reverse_impl(rest, Cons(entry, acc))
329 }
330}
331
332/%
333Reverses a list.
334%/
335pub fn reverse[a: type, n: nat](list: List[a, n]) -> List[a, n] {
336 reverse_impl(list, Nil)
337}
338
339fn 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] {
340 case (list1, list2) of {
341 | (~Nil, ~Nil) => acc
342 | (~Cons(x, xs), ~Cons(y, ys)) => zipwith_impl(xs, ys, f, Cons(f(x, y), acc))
343 }
344}
345
346/%
347Combines two lists of same length element-wise using `f`.
348
349The `i`-th element of the result is `f(x, y)`, where `x` and `y` are the `i`-th elements of
350the first and second list respectively.
351%/
352pub 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] {
353 zipwith_impl(list1, list2, f, Nil) |> reverse()
354}
355
356/%
357Pairs up the elements of two lists of same length.
358%/
359pub fn zip[a: type, b: type, n: nat](list1: List[a, n], list2: List[b, n]) -> List[(a, b), n] {
360 zipwith(list1, list2, pair::make)
361}
362
363fn 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]) {
364 case list of {
365 | ~Nil => (xs, ys)
366 | ~Cons((x, y), rest) => unzip_impl(rest, Cons(x, xs), Cons(y, ys))
367 }
368}
369
370/%
371Splits a list of pairs into a pair of lists.
372
373This is the inverse of `zip`.
374%/
375pub fn unzip[a: type, b: type, n: nat](list: List[(a, b), n]) -> (List[a, n], List[b, n]) {
376 case unzip_impl(list, Nil, Nil) of {
377 | ~(xs, ys) => (reverse(xs), reverse(ys))
378 }
379}
380
381/%
382Returns everything after the first element of a non-empty list.
383%/
384pub fn tail[a: utype, n: nat](list: List1[a, n]) -> List[a, n] {
385 case list of {
386 | ~Cons(_, xs) => xs
387 }
388}
389
390/%
391Returns the first element of a non-empty list.
392%/
393pub fn head[a: utype, n: nat](list: !List1[a, n]) -> a {
394 case list of {
395 | Cons(x, _) => x
396 }
397}
398
399/%
400Returns the last element of a non-empty list.
401%/
402pub fn last[a: utype, n: nat](list: !List1[a, n]) -> a {
403 case list of {
404 | Cons(x, Nil) => x
405 | Cons(_, xs) => last(xs)
406 }
407}
408
409/%
410Finds the first element for which `f` returns `Some`, and returns that result.
411%/
412pub fn findmap[a: utype, b: type, n: nat](list: !List[a, n], f: fn(a) -> maybe::Maybe[b]) -> maybe::Maybe[b] {
413 case list of {
414 | Nil => maybe::None
415 | Cons(x, xs) =>
416 case f(x) of {
417 | ~maybe::None => findmap(xs, f)
418 | res => res
419 }
420 }
421}
422
423/%
424Finds the first element satisfying a predicate.
425
426Returns `Some(x)` for the first element where `f(x)` is `true`, or `None` if there is no such element.
427%/
428pub fn find[a: utype, n: nat](list: !List[a, n], f: fn(a) -> bool) -> maybe::Maybe[a] {
429 case list of {
430 | Nil => maybe::None
431 | Cons(x, xs) => if f(x) then maybe::Some(x) else find(xs, f)
432 }
433}
434
435/%
436Tests whether any element satisfies a predicate.
437
438Returns `true` if `f(x)` holds for at least one element, and `false` otherwise (including for the empty list).
439%/
440pub fn any[a: utype, n: nat](list: !List[a, n], f: fn(a) -> bool) -> bool {
441 case find(list, f) of {
442 | ~maybe::None => false
443 | ~maybe::Some(_) => true
444 }
445}
446
447/%
448Tests whether every element satisfies a predicate.
449
450Returns `true` if `f(x)` holds for all elements (including vacuously for the empty list)
451%/
452pub fn all[a: utype, n: nat](list: !List[a, n], f: fn(a) -> bool) -> bool {
453 case list of {
454 | Nil => true
455 | Cons(x, xs) => if bool::not(f(x)) then false else all(xs, f)
456 }
457}
458
459/%
460Returns `true` if every element of a boolean list is `true`.
461
462Returns `true` for the empty list.
463%/
464pub fn and[n: nat](list: !List[bool, n]) -> bool {
465 all(list, fun::id)
466}
467
468/%
469Returns `true` if at least one element of a boolean list is `true`.
470
471Returns `false` for the empty list.
472%/
473pub fn or[n: nat](list: !List[bool, n]) -> bool {
474 any(list, fun::id)
475}
476
477/%
478Tests whether a list has no elements.
479%/
480pub fn empty[a: type, n: nat](list: !List[a, n]) -> bool {
481 case list of {
482 | Nil => true
483 | Cons(_, _) => false
484 }
485}
486
487fn 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]) {
488 case list of {
489 | ~Nil => (pass, fails)
490 | ~Cons(x, xs) =>
491 if f(x) then partition_impl(xs, f, Cons(x, pass), fails)
492 else partition_impl(xs, f, pass, Cons(x, fails))
493 }
494}
495
496/%
497Splits a list into the elements that satisfy a predicate and those that don't.
498%/
499pub fn partition[a: utype, n: nat](list: List[a, n], f: fn(a) -> bool) -> [m: nat, p: nat] (List[a, m], List[a, p]) {
500 val (pass, fails) = partition_impl(list, f, Nil, Nil)
501 (reverse(pass), reverse(fails))
502}