1use <prelude.kav>
2use <int.kav> as int
3use <maybe.kav> as maybe
4use <list.kav> as list
5use <bool.kav> as bool
6
7linear datatype Tree[k: utype, v: type, n: nat] {
8 | Leaf : Tree[k, v, 0]
9 | [a: nat, b: nat] Node(Tree[k, v, a], k, v, Tree[k, v, b], int) : Tree[k, v, a + b + 1]
10}
11
12fn height[k: utype, v: type, n: nat](tree: !Tree[k, v, n]) -> int {
13 case tree of {
14 | Leaf => 0
15 | Node(_, _, _, _, h) => h
16 }
17}
18
19fn free_with_impl[k: utype, v: type, n: nat](tree: Tree[k, v, n], f: fn(v) -> one) -> one {
20 case tree of {
21 | ~Leaf => one
22 | ~Node(left, _, value, right, _) => {
23 val _ = free_with_impl(left, f)
24 val _ = f(value)
25 free_with_impl(right, f)
26 }
27 }
28}
29
30fn size_impl[k: utype, v: type, n: nat](tree: !Tree[k, v, n]) -> int {
31 case tree of {
32 | Leaf => 0
33 | Node(left, _, _, right, _) => size_impl(left) + 1 + size_impl(right)
34 }
35}
36
37fn mem_impl[k: utype, v: type, n: nat](cmp: fn(k, k) -> int, tree: !Tree[k, v, n], key: k) -> bool {
38 case tree of {
39 | Leaf => false
40 | Node(left, node_key, _, right, _) => {
41 val order = cmp(key, node_key)
42 if order == 0 then true
43 else if order < 0 then mem_impl(cmp, left, key)
44 else mem_impl(cmp, right, key)
45 }
46 }
47}
48
49fn find_impl[k: utype, v: utype, n: nat](cmp: fn(k, k) -> int, tree: !Tree[k, v, n], key: k) -> maybe::Maybe[v] {
50 case tree of {
51 | Leaf => maybe::None
52 | Node(left, node_key, value, right, _) => {
53 val order = cmp(key, node_key)
54 if order == 0 then maybe::Some(value)
55 else if order < 0 then find_impl(cmp, left, key)
56 else find_impl(cmp, right, key)
57 }
58 }
59}
60
61fn node[k: utype, v: type, a: nat, b: nat](left: Tree[k, v, a], key: k, value: v, right: Tree[k, v, b]) -> Tree[k, v, a + b + 1] {
62 val hl = height(left)
63 val hr = height(right)
64 Node(left, key, value, right, 1 + int::max(hl, hr))
65}
66
67fn bal[k: utype, v: type, a: nat, b: nat](l: Tree[k, v, a], k: k, v: v, r: Tree[k, v, b]) -> Tree[k, v, a + b + 1] {
68 val hl = height(l) val hr = height(r)
69 if hl > hr + 2 then {
70 case l of {
71 | ~Leaf => node(Leaf, k, v, r)
72 | ~Node(ll, lk, lv, lr, _) => {
73 if height(ll) >= height(lr) then node(ll, lk, lv, node(lr, k, v, r))
74 else {
75 case lr of {
76 | ~Leaf => node(node(ll, lk, lv, Leaf), k, v, r)
77 | ~Node(lrl, lrk, lrv, lrr, _) =>
78 node(node(ll, lk, lv, lrl), lrk, lrv, node(lrr, k, v, r))
79 }
80 }
81 }
82 }
83 } else if hr > hl + 2 then {
84 case r of {
85 | ~Leaf => node(l, k, v, Leaf)
86 | ~Node(rl, rk, rv, rr, _) => {
87 if height(rr) >= height(rl) then node(node(l, k, v, rl), rk, rv, rr)
88 else {
89 case rl of {
90 | ~Leaf => node(node(l, k, v, Leaf), rk, rv, rr)
91 | ~Node(rll, rlk, rlv, rlr, _) =>
92 node(node(l, k, v, rll), rlk, rlv, node(rlr, rk, rv, rr))
93 }
94 }
95 }
96 }
97 } else node(l, k, v, r)
98}
99
100fn addmin[k: utype, v: type, n: nat](t: Tree[k, v, n], k: k, v: v) -> Tree[k, v, n + 1] {
101 case t of {
102 | ~Leaf => node(Leaf, k, v, Leaf)
103 | ~Node(l, k_, v_, r, _) => bal(addmin(l, k, v), k_, v_, r)
104 }
105}
106
107fn addmax[k: utype, v: type, n: nat](t: Tree[k, v, n], k: k, v: v) -> Tree[k, v, n + 1] {
108 case t of {
109 | ~Leaf => node(Leaf, k, v, Leaf)
110 | ~Node(l, k_, v_, r, _) => bal(l, k_, v_, addmax(r, k, v))
111 }
112}
113
114fn join[k: utype, v: type, a: nat, b: nat](l: Tree[k, v, a], k: k, v: v, r: Tree[k, v, b]) -> Tree[k, v, a + b + 1] {
115 case (l, r) of {
116 | ~(Leaf, r) => addmin(r, k, v)
117 | ~(l, Leaf) => addmax(l, k, v)
118 | ~(l, r) => {
119 val lh = height(l) val rh = height(r)
120 if lh > rh + 2 then {
121 val ~Node(ll, lk, lv, lr, _) = l
122 bal(ll, lk, lv, join(lr, k, v, r))
123 } else if rh > lh + 2 then {
124 val ~Node(rl, rk, rv, rr, _) = r
125 bal(join(l, k, v, rl), rk, rv, rr)
126 } else node(l, k, v, r)
127 }
128 }
129}
130
131fn remove_min_binding[k: utype, v: type, n: nat](tree: Tree[k, v, n + 1]) -> (k, v, Tree[k, v, n]) {
132 case tree of {
133 | ~Node(left, key, value, right, _) => {
134 case left of {
135 | ~Leaf => (key, value, right)
136 | ~Node(ll, lk, lv, lr, lh) => {
137 val (min_key, min_value, left_rest) = remove_min_binding(Node(ll, lk, lv, lr, lh))
138 (min_key, min_value, bal(left_rest, key, value, right))
139 }
140 }
141 }
142 }
143}
144
145// every key in left must be smaller than every key in right
146fn concat[k: utype, v: type, a: nat, b: nat](left: Tree[k, v, a], right: Tree[k, v, b]) -> Tree[k, v, a + b] {
147 case right of {
148 | ~Leaf => left
149 | ~Node(rl, rk, rv, rr, rh) => {
150 val (key, value, right_rest) = remove_min_binding(Node(rl, rk, rv, rr, rh))
151 join(left, key, value, right_rest)
152 }
153 }
154}
155
156fn split_impl[k: utype, v: type, n: nat](cmp: fn(k, k) -> int, x: k, t: Tree[k, v, n]) -> [a: nat, b: nat] (Tree[k, v, a], maybe::Maybe[v], Tree[k, v, b]) {
157 case t of {
158 | ~Leaf => (Leaf, maybe::None, Leaf)
159 | ~Node(l, k, v, r, _) => {
160 val order = cmp(x, k)
161 if order == 0 then (l, maybe::Some(v), r)
162 else if order < 0 then {
163 val (ll, found, lr) = split_impl(cmp, x, l)
164 (ll, found, join(lr, k, v, r))
165 } else {
166 val (rl, found, rr) = split_impl(cmp, x, r)
167 (join(l, k, v, rl), found, rr)
168 }
169 }
170 }
171}
172
173fn union_impl[k: utype, v: type, a: nat, b: nat](
174 cmp: fn(k, k) -> int,
175 f: fn(k, v, v) -> maybe::Maybe[v],
176 t1: Tree[k, v, a],
177 t2: Tree[k, v, b]) -> [r: nat] Tree[k, v, r] {
178 case (t1, t2) of {
179 | ~(Leaf, t) => t
180 | ~(t, Leaf) => t
181 | (t1, t2) => {
182 val h1 = height(t1) val h2 = height(t2)
183 if h1 >= h2 then {
184 val ~Node(l1, k1, v1, r1, _) = t1
185 val (l2_, found, r2_) = split_impl(cmp, k1, t2)
186 val l = union_impl(cmp, f, l1, l2_)
187 val r = union_impl(cmp, f, r1, r2_)
188 case found of {
189 | ~maybe::None => join(l, k1, v1, r)
190 | ~maybe::Some(v2) => {
191 case f(k1, v1, v2) of {
192 | ~maybe::Some(v) => join(l, k1, v, r)
193 | ~maybe::None => concat(l, r)
194 }
195 }
196 }
197 } else {
198 val ~Node(l2, k2, v2, r2, _) = t2
199 val (l1_, found, r1_) = split_impl(cmp, k2, t1)
200 val l = union_impl(cmp, f, l1_, l2)
201 val r = union_impl(cmp, f, r1_, r2)
202 case found of {
203 | ~maybe::None => join(l, k2, v2, r)
204 | ~maybe::Some(v1) => {
205 case f(k2, v1, v2) of {
206 | ~maybe::Some(v) => join(l, k2, v, r)
207 | ~maybe::None => concat(l, r)
208 }
209 }
210 }
211 }
212 }
213 }
214}
215
216fn map_impl[k: utype, v: type, w: type, n: nat](tree: Tree[k, v, n], f: fn(v) -> w) -> Tree[k, w, n] {
217 case tree of {
218 | ~Leaf => Leaf
219 | ~Node(left, key, value, right, h) => {
220 val new_left = map_impl(left, f)
221 val new_value = f(value)
222 val new_right = map_impl(right, f)
223 Node(new_left, key, new_value, new_right, h)
224 }
225 }
226}
227
228fn to_list_impl[k: utype, v: type, n: nat, m: nat](tree: Tree[k, v, n], rest: list::List[(k, v), m]) -> list::List[(k, v), n + m] {
229 case tree of {
230 | ~Leaf => rest
231 | ~Node(left, key, value, right, _) => {
232 val right_list = to_list_impl(right, rest)
233 to_list_impl(left, list::Cons((key, value), right_list))
234 }
235 }
236}
237
238fn fold_impl[k: utype, v: utype, acc: type, n: nat](tree: !Tree[k, v, n], init: acc, f: fn(k, v, acc) -> acc) -> acc {
239 case tree of {
240 | Leaf => init
241 | Node(left, key, value, right, _) => {
242 val after_left = fold_impl(left, init, f)
243 val after_node = f(key, value, after_left)
244 fold_impl(right, after_node, f)
245 }
246 }
247}
248
249fn of_list_impl[k: utype, v: type, n: nat, s: nat](m: Map[k, v, s], bindings: list::List[(k, v), n], combine: fn(k, v, v) -> v) -> [r: nat] Map[k, v, r] {
250 case bindings of {
251 | ~list::Nil => m
252 | ~list::Cons((key, x), rest) => of_list_impl(addwith(m, key, x, combine), rest, combine)
253 }
254}
255
256fn filtermap_impl[k: utype, v: type, w: type, n: nat](tree: Tree[k, v, n], f: fn(k, v) -> maybe::Maybe[w]) -> [r: nat] Tree[k, w, r] {
257 case tree of {
258 | ~Leaf => Leaf
259 | ~Node(left, key, value, right, _) => {
260 val new_left = filtermap_impl(left, f)
261 val result = f(key, value)
262 val new_right = filtermap_impl(right, f)
263 case result of {
264 | ~maybe::None => concat(new_left, new_right)
265 | ~maybe::Some(y) => join(new_left, key, y, new_right)
266 }
267 }
268 }
269}
270
271fn filter_impl[k: utype, v: utype, n: nat](tree: Tree[k, v, n], f: fn(k, v) -> bool) -> [r: nat] Tree[k, v, r] {
272 case tree of {
273 | ~Leaf => Leaf
274 | ~Node(left, key, value, right, _) => {
275 val new_left = filter_impl(left, f)
276 val result = f(key, value)
277 val new_right = filter_impl(right, f)
278 if result then join(new_left, key, value, new_right)
279 else concat(new_left, new_right)
280 }
281 }
282}
283
284fn partition_impl[k: utype, v: utype, n: nat](t: Tree[k, v, n], f: fn(k, v) -> bool)
285 -> [a: nat, b: nat] (Tree[k, v, a], Tree[k, v, b]) {
286 case t of {
287 | ~Leaf => (Leaf, Leaf)
288 | ~Node(l, k, v, r, _) => {
289 val (lt, lf) = partition_impl(l, f)
290 val fkv = f(k, v)
291 val (rt, rf) = partition_impl(r, f)
292 if fkv then (join(lt, k, v, rt), concat(lf, rf))
293 else (concat(lt, rt), join(lf, k, v, rf))
294 }
295 }
296}
297
298fn all_impl[k: utype, v: type, n: nat](t: !Tree[k, v, n], f: fn(k, !v) -> bool) -> bool {
299 case t of {
300 | Leaf => true
301 | Node(l, k, v, r, _) =>
302 bool::and(f(k, v),
303 bool::and(all_impl(l, f), all_impl(r, f)))
304 }
305}
306
307fn any_impl[k: utype, v: type, n: nat](t: !Tree[k, v, n], f: fn(k, !v) -> bool) -> bool {
308 case t of {
309 | Leaf => false
310 | Node(l, k, v, r, _) =>
311 bool::or(f(k, v),
312 bool::or(any_impl(l, f), any_impl(r, f)))
313 }
314}
315
316fn add_impl[k: utype, v: type, n: nat](cmp: fn(k, k) -> int, tree: Tree[k, v, n], key: k, value: v, f: fn(k, v, v) -> v)
317 -> [r: nat] Tree[k, v, r] {
318 case tree of {
319 | ~Leaf => node(Leaf, key, value, Leaf)
320 | ~Node(left, node_key, node_value, right, _) => {
321 val order = cmp(key, node_key)
322 if order == 0 then
323 node(left, key, f(key, node_value, value), right)
324 else if order < 0 then
325 bal(add_impl(cmp, left, key, value, f), node_key, node_value, right)
326 else
327 bal(left, node_key, node_value, add_impl(cmp, right, key, value, f))
328 }
329 }
330}
331
332fn remove_impl[k: utype, v: type, n: nat](cmp: fn(k, k) -> int, tree: Tree[k, v, n], key: k) -> [r: nat] (maybe::Maybe[v], Tree[k, v, r]) {
333 case tree of {
334 | ~Leaf => (maybe::None, Leaf)
335 | ~Node(left, node_key, value, right, _) => {
336 val order = cmp(key, node_key)
337 if order == 0 then (maybe::Some(value), concat(left, right))
338 else if order < 0 then {
339 val (removed, new_left) = remove_impl(cmp, left, key)
340 (removed, bal(new_left, node_key, value, right))
341 }
342 else {
343 val (removed, new_right) = remove_impl(cmp, right, key)
344 (removed, bal(left, node_key, value, new_right))
345 }
346 }
347 }
348}
349
350fn update_impl[k: utype, v: type, n: nat](cmp: fn(k, k) -> int, tree: Tree[k, v, n], key: k, f: fn(maybe::Maybe[v]) -> maybe::Maybe[v]) -> [r: nat] Tree[k, v, r] {
351 case tree of {
352 | ~Leaf => {
353 case f(maybe::None) of {
354 | ~maybe::None => Leaf
355 | ~maybe::Some(new_value) => node(Leaf, key, new_value, Leaf)
356 }
357 }
358 | ~Node(left, node_key, node_value, right, _) => {
359 val order = cmp(key, node_key)
360 if order == 0 then {
361 case f(maybe::Some(node_value)) of {
362 | ~maybe::None => concat(left, right)
363 | ~maybe::Some(new_value) => node(left, key, new_value, right)
364 }
365 } else if order < 0 then {
366 val l_ = update_impl(cmp, left, key, f)
367 bal(l_, node_key, node_value, right)
368 } else {
369 val r_ = update_impl(cmp, right, key, f)
370 bal(left, node_key, node_value, r_)
371 }
372 }
373 }
374}
375
376/%
377A map from keys of type `k` to values of type `v`, with `n` bindings.
378%/
379pub abstract linear datatype Map[k: utype, v: type, n: nat] {
380 | Map(fn(k, k) -> int, Tree[k, v, n]) : Map[k, v, n]
381}
382
383/%
384Creates an empty map ordered by `cmp`.
385%/
386pub fn empty[k: utype, v: type](cmp: fn(k, k) -> int) -> Map[k, v, 0] {
387 Map(cmp, Leaf)
388}
389
390/%
391Builds a map from a list of bindings.
392
393If a key occurs several times, `combine(key, old, new)` merges the values,
394where `old` comes earlier in the list.
395%/
396pub fn oflistwith[k: utype, v: type, n: nat](
397 cmp: fn(k, k) -> int, bindings: list::List[(k, v), n], combine: fn(k, v, v) -> v
398) -> [r: nat] Map[k, v, r] {
399 of_list_impl(empty(cmp), bindings, combine)
400}
401
402/%
403Builds a map from a list of bindings.
404
405If a key occurs several times, the last binding is used in the map.
406%/
407pub fn oflist[k: utype, v: utype, n: nat](cmp: fn(k, k) -> int, bindings: list::List[(k, v), n]) -> [r: nat] Map[k, v, r] {
408 of_list_impl(empty(cmp), bindings, fn(k: k, v1: v, v2: v) -> v { v2 })
409}
410
411/%
412Binds `key` to `x`. If `key` was already bound to `y`, it is bound to `combine(key, y, x)` instead.
413%/
414pub fn addwith[k: utype, v: type, n: nat](m: Map[k, v, n], key: k, x: v, f: fn(k, v, v) -> v) -> [r: nat] Map[k, v, r] {
415 val ~Map(cmp, t) = m
416 Map(cmp, add_impl(cmp, t, key, x, f))
417}
418
419/%
420Binds `key` to `x`.
421%/
422pub fn add[k: utype, v: utype, n: nat](m: Map[k, v, n], key: k, x: v) -> [r: nat] Map[k, v, r] {
423 val ~Map(cmp, t) = m
424 val t = add_impl(cmp, t, key, x, fn(g: k, gg: v, nv: v) -> v { nv })
425 Map(cmp, t)
426}
427
428/%
429Removes the binding for `key` and returns its value, if there was one.
430%/
431pub fn remove[k: utype, v: type, n: nat](m: Map[k, v, n], key: k) -> [r: nat] (maybe::Maybe[v], Map[k, v, r]) {
432 val ~Map(cmp, t) = m
433 val (found, t2) = remove_impl(cmp, t, key)
434 (found, Map(cmp, t2))
435}
436
437/%
438Updates the binding for `key` with `f`.
439
440`f` receives the current value (`None` if absent) and returns the new one:
441`Some(y)` stores `y`, and `None` leaves the key unbound.
442%/
443pub fn update[k: utype, v: type, n: nat](m: Map[k, v, n], key: k, f: fn(maybe::Maybe[v]) -> maybe::Maybe[v]) -> [r: nat] Map[k, v, r] {
444 val ~Map(cmp, t) = m
445 Map(cmp, update_impl(cmp, t, key, f))
446}
447
448
449/%
450Merges two maps.
451
452Both maps must use the same comparison function.
453For a key bound in both, `f(key, x1, x2)` decides the result, where `x1` comes
454from `m1` and `x2` from `m2`: `Some(y)` binds the key to `y`, and `None` leaves
455it unbound.
456%/
457pub fn union[k: utype, v: type, a: nat, b: nat](m1: Map[k, v, a], m2: Map[k, v, b], f: fn(k, v, v) -> maybe::Maybe[v]) -> [r: nat] Map[k, v, r] {
458 val ~Map(cmp, t1) = m1
459 val ~Map(_, t2) = m2
460 Map(cmp, union_impl(cmp, f, t1, t2))
461}
462
463/%
464Applies `f` to every value, keeping the keys.
465%/
466pub fn map[k: utype, v: type, w: type, n: nat](m: Map[k, v, n], f: fn(v) -> w) -> Map[k, w, n] {
467 val ~Map(cmp, t) = m
468 Map(cmp, map_impl(t, f))
469}
470
471
472/%
473Maps and filters a map in a single pass.
474
475Applies `f` to each binding in increasing key order. `Some(y)` keeps the key
476with value `y`, and `None` drops the binding.
477%/
478pub fn filtermap[k: utype, v: type, w: type, n: nat](m: Map[k, v, n], f: fn(k, v) -> maybe::Maybe[w]) -> [r: nat] Map[k, w, r] {
479 val ~Map(cmp, t) = m
480 Map(cmp, filtermap_impl(t, f))
481}
482
483/%
484Keeps only the bindings for which `f` returns `true`.
485%/
486pub fn filter[k: utype, v: utype, n: nat](m: Map[k, v, n], f: fn(k, v) -> bool) -> [r: nat] Map[k, v, r] {
487 val ~Map(cmp, t) = m
488 Map(cmp, filter_impl(t, f))
489}
490
491/%
492Tests whether the map is empty.
493%/
494pub fn isempty[k: utype, v: type, n: nat](m: !Map[k, v, n]) -> bool {
495 case m of {
496 | Map(_, Leaf) => true
497 | Map(_, _) => false
498 }
499}
500
501/%
502Number of bindings in the map.
503%/
504pub fn size[k: utype, v: type, n: nat](m: !Map[k, v, n]) -> int {
505 val Map(_, t) = m
506 size_impl(t)
507}
508
509/%
510Tests whether `key` is bound in the map.
511%/
512pub fn mem[k: utype, v: type, n: nat](m: !Map[k, v, n], key: k) -> bool {
513 val Map(cmp, t) = m
514 mem_impl(cmp, t, key)
515}
516
517/%
518Returns the value bound to `key`, if any.
519%/
520pub fn find[k: utype, v: utype, n: nat](m: !Map[k, v, n], key: k) -> maybe::Maybe[v] {
521 val Map(cmp, t) = m
522 find_impl(cmp, t, key)
523}
524
525/%
526Folds over the bindings in increasing key order.
527
528For a map with bindings `[(k1, x1), (k2, x2)]` this computes
529`f(k2, x2, f(k1, x1, init))`.
530%/
531pub fn fold[k: utype, v: utype, acc: type, n: nat](m: !Map[k, v, n], init: acc, f: fn(k, v, acc) -> acc) -> acc {
532 val Map(_, t) = m
533 fold_impl(t, init, f)
534}
535
536/%
537Converts a map to a list of bindings in increasing key order, consuming it.
538The list has exactly as many elements as the map has bindings.
539%/
540pub fn tolist[k: utype, v: type, n: nat](m: Map[k, v, n]) -> list::List[(k, v), n] {
541 val ~Map(_, t) = m
542 to_list_impl(t, list::Nil)
543}
544
545/%
546Returns a pair of maps `(m1, m2)`, where `m1` contains all the bindings of m that satisfy the predicate `f`,
547and `m2` is the map with all the bindings of m that do not satisfy f.
548%/
549pub fn partition[k: utype, v: utype, n: nat](m: Map[k, v, n], f: fn(k, v) -> bool) -> [a: nat, b: nat] (Map[k, v, a], Map[k, v, b]) {
550 val ~Map(cmp, t) = m
551 val (tt, tf) = partition_impl(t, f)
552 (Map(cmp, tt), Map(cmp, tf))
553}
554
555/%
556Returns a tuple `(l, data, r)`, where l is a map with the bindings of m with keys strictly less than k,
557r is a map with the bindings of m with key strictly greater than x and data is None if m contains no
558binding for x or Some(v) if m binds v to x.
559%/
560pub fn split[k: utype, v: type, n: nat](m: Map[k, v, n], k: k) -> [a: nat, b: nat] (Map[k, v, a], maybe::Maybe[v], Map[k, v, b]) {
561 val ~Map(cmp, t) = m
562 val (t1, d, t2) = split_impl(cmp, k, t)
563 (Map(cmp, t1), d, Map(cmp, t2))
564}
565
566/%
567Checks if all the bindings in the map satisfy the predicate `f`.
568%/
569pub fn all[k: utype, v: type, n: nat](m: !Map[k, v, n], f: fn(k, !v) -> bool) -> bool {
570 val Map(_, t) = m
571 all_impl(t, f)
572}
573
574/%
575Checks if at least one binding in the map satisfies the predicate f.
576%/
577pub fn any[k: utype, v: type, n: nat](m: !Map[k, v, n], f: fn(k, !v) -> bool) -> bool {
578 val Map(_, t) = m
579 any_impl(t, f)
580}
581
582/%
583Frees the map, running `f` on each value to dispose of it.
584%/
585pub fn free[k: utype, v: type, n: nat](m: Map[k, v, n], f: fn(v) -> one) -> one {
586 val ~Map(_, t) = m
587 free_with_impl(t, f)
588}