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