-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathassoclist.dats
68 lines (52 loc) · 1.75 KB
/
assoclist.dats
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
#define ATS_DYNLOADFLAG 0
#include "share/atspre_staload.hats"
staload "./assoclist.sats"
staload "./list.sats"
staload _ = "./list.dats"
staload "./maybe.sats"
staload _ = "./maybe.dats"
#define :: ListCons
#define nil ListNil
assume assoclist (a, b) = list (@(a, b))
(******************************)
staload "./order.sats"
staload _ = "./order.dats"
implement (a,b) order_compare<assoclist (a,b)> (xs, ys) =
case+ (xs, ys) of
| (nil _, nil _) => 0
| (nil _, ys) => ~(list_len ys)
| (xs, nil _) => list_len xs
| (x :: xs, y :: ys) =>
let
val cmp = order_compare<@(a,b)> (x, y)
in
if cmp = 0 then order_compare (xs, ys) else cmp
end
(******************************)
staload "./show.sats"
staload _ = "./show.dats"
implement (a:t@ype,b:t@ype) show_any<assoclist (a,b)> (xs) = let
fun show_list (xs: assoclist (a, b)): void =
case+ xs of
| nil () => ()
| x :: nil () => ignoret (show_any x)
| x :: xs => ignoret (show_any x; show_sep<> (); show_list xs)
in
ignoret (show_begin (); show_list xs; show_end ())
end
(******************************)
implement {a,b} assoclist_make () = nil ()
implement {a,b} assoclist_empty (xs) = list_empty xs
implement {a,b} assoclist_insert (xs, k, v) = @(k,v) :: xs
implement {a,b} assoclist_delete (xs, k) =
case+ xs of
| nil () => nil ()
| @(kk, vv) :: xs => if k \order_eq kk then xs else @(kk, vv) :: assoclist_delete (xs, k)
implement {a,b} assoclist_update (xs, k, v) =
case+ xs of
| nil () => nil ()
| @(kk, vv) :: xs => if k \order_eq kk then @(k, v) :: xs else @(kk, vv) :: assoclist_update (xs, k, v)
implement {a,b} assoclist_find (xs, k) =
case+ xs of
| nil () => Nothing ()
| @(kk, vv) :: xs => if k \order_eq kk then Just vv else assoclist_find (xs, k)