std:Map
lib/Map.omg
lib/Map.omg216 lines
1{- Finite maps: dictionaries from keys to values.
2
3 A `Map k v` is a list of entries `{ key : k, value : v }`, each key at
4 most once, in the order the keys were first inserted. There are no type
5 classes: the functions that look keys up take the test of equality of
6 keys as their first argument, e.g. `Text/equal` or `std:Map.natural`.
7
8 let Map = std:Map
9 let ages = Map.fromList Text/equal [ { key = "Ada", value = 36 }, { key = "Alan", value = 41 } ]
10 in Map.lookup Text/equal "Ada" ages
11
12 Keys and values can be of any universe, each its own: a `Map Text Type`
13 names types, and lives in `Type 1`.
14
15 Two keys that the test says are equal are the same key, even if they
16 differ otherwise: a map keeps the key it was given first.
17
18 Looking up, inserting or deleting one key takes a time proportional to
19 the size of the map; `fromList` and `union` take the product of the
20 sizes.
21-}
22
23let Entry
24 : forall {lk lv : Level} -> Type lk -> Type lv -> Type (Level/max lk lv)
25 = \k v -> { key : k, value : v }
26
27let Map
28 : forall {lk lv : Level} -> Type lk -> Type lv -> Type (Level/max lk lv)
29 = \k v -> List (Entry k v)
30
31-- Equality tests for the keys of the builtin types.
32let natural : Natural -> Natural -> Bool = std:Natural.equal
33
34let text : Text -> Text -> Bool = Text/equal
35
36let bool : Bool -> Bool -> Bool = Bool/equal
37
38let empty
39 : forall {lk lv : Level} {k : Type lk} {v : Type lv} -> Map k v
40 = \{lk} {lv} {k} {v} -> ([] : Map k v)
41
42let singleton
43 : forall {lk lv : Level} {k : Type lk} {v : Type lv} -> k -> v -> Map k v
44 = \key value -> [ { key, value } ]
45
46let size
47 : forall {lk lv : Level} {k : Type lk} {v : Type lv} -> Map k v -> Natural
48 = std:List.length
49
50-- The value of a key, if the map has the key.
51let lookup
52 : forall {lk lv : Level} {k : Type lk} {v : Type lv}
53 -> (k -> k -> Bool)
54 -> k
55 -> Map k v
56 -> Optional v
57 = \{lk} {lv} {k} {v} equal key m ->
58 std:Optional.map
59 (\(e : Entry k v) -> e.value)
60 (std:List.find (\(e : Entry k v) -> equal e.key key) m)
61
62let member
63 : forall {lk lv : Level} {k : Type lk} {v : Type lv} -> (k -> k -> Bool) -> k -> Map k v -> Bool
64 = \{lk} {lv} {k} {v} equal key m -> std:List.any (\(e : Entry k v) -> equal e.key key) m
65
66-- The map without the key (the same map if it does not have it).
67let delete
68 : forall {lk lv : Level} {k : Type lk} {v : Type lv}
69 -> (k -> k -> Bool)
70 -> k
71 -> Map k v
72 -> Map k v
73 = \{lk} {lv} {k} {v} equal key m ->
74 std:List.filter (\(e : Entry k v) -> std:Bool.not (equal e.key key)) m
75
76-- The map with the key set to the value: in place if the key is there
77-- (which keeps the key the map has), at the end otherwise.
78let insert
79 : forall {lk lv : Level} {k : Type lk} {v : Type lv}
80 -> (k -> k -> Bool)
81 -> k
82 -> v
83 -> Map k v
84 -> Map k v
85 = \{lk} {lv} {k} {v} equal key value m ->
86 if member equal key m
87 then std:List.map
88 (\(e : Entry k v) -> if equal e.key key then { key = e.key, value } else e)
89 m
90 else m # [ { key, value } ]
91
92-- The map with the value of the key changed by a function (the same map if
93-- it does not have the key).
94let adjust
95 : forall {lk lv : Level} {k : Type lk} {v : Type lv}
96 -> (k -> k -> Bool)
97 -> k
98 -> (v -> v)
99 -> Map k v
100 -> Map k v
101 = \{lk} {lv} {k} {v} equal key f m ->
102 std:List.map
103 (\(e : Entry k v) -> if equal e.key key then { key = e.key, value = f e.value } else e)
104 m
105
106-- The map with the key set to the value, or, if the key is there, to the
107-- new value combined with the old one (in the key's place):
108-- insertWith Text/equal (\new old -> new + old) word 1 counts one more word.
109let insertWith
110 : forall {lk lv : Level} {k : Type lk} {v : Type lv}
111 -> (k -> k -> Bool)
112 -> (v -> v -> v)
113 -> k
114 -> v
115 -> Map k v
116 -> Map k v
117 = \{lk} {lv} {k} {v} equal combine key value m ->
118 if member equal key m
119 then adjust equal key (\(old : v) -> combine value old) m
120 else m # [ { key, value } ]
121
122-- The map with the value of the key changed by a function of the value it
123-- has, if any: the key is deleted when the function gives `None`, and set
124-- (as `insert` does) when it gives a value.
125let alter
126 : forall {lk lv : Level} {k : Type lk} {v : Type lv}
127 -> (k -> k -> Bool)
128 -> k
129 -> (Optional v -> Optional v)
130 -> Map k v
131 -> Map k v
132 = \{lk} {lv} {k} {v} equal key f m ->
133 Optional/elim
134 (\(_ : Optional v) -> Map k v)
135 (delete equal key m)
136 (\(value : v) -> insert equal key value m)
137 (f (lookup equal key m))
138
139-- A map of entries; of two entries with the same key, the map keeps the
140-- key of the first and the value of the last.
141let fromList
142 : forall {lk lv : Level} {k : Type lk} {v : Type lv}
143 -> (k -> k -> Bool)
144 -> List (Entry k v)
145 -> Map k v
146 = \{lk} {lv} {k} {v} equal ->
147 std:List.foldLeft (\(m : Map k v) (e : Entry k v) -> insert equal e.key e.value m) empty
148
149let toList
150 : forall {lk lv : Level} {k : Type lk} {v : Type lv} -> Map k v -> List (Entry k v)
151 = \m -> m
152
153let keys
154 : forall {lk lv : Level} {k : Type lk} {v : Type lv} -> Map k v -> List k
155 = \{lk} {lv} {k} {v} -> std:List.map (\(e : Entry k v) -> e.key)
156
157let values
158 : forall {lk lv : Level} {k : Type lk} {v : Type lv} -> Map k v -> List v
159 = \{lk} {lv} {k} {v} -> std:List.map (\(e : Entry k v) -> e.value)
160
161let map
162 : forall {lk la lb : Level} {k : Type lk} {a : Type la} {b : Type lb}
163 -> (a -> b)
164 -> Map k a
165 -> Map k b
166 = \{lk} {la} {lb} {k} {a} {b} f ->
167 std:List.map (\(e : Entry k a) -> { key = e.key, value = f e.value })
168
169let filter
170 : forall {lk lv : Level} {k : Type lk} {v : Type lv} -> (k -> v -> Bool) -> Map k v -> Map k v
171 = \{lk} {lv} {k} {v} keep -> std:List.filter (\(e : Entry k v) -> keep e.key e.value)
172
173let fold
174 : forall {lk lv lb : Level} {k : Type lk} {v : Type lv} {b : Type lb}
175 -> (k -> v -> b -> b)
176 -> b
177 -> Map k v
178 -> b
179 = \{lk} {lv} {lb} {k} {v} {b} f ->
180 std:List.fold (\(e : Entry k v) (acc : b) -> f e.key e.value acc)
181
182-- The entries of both maps; for a key in both, the entry (the key and the
183-- value) of the first.
184let union
185 : forall {lk lv : Level} {k : Type lk} {v : Type lv}
186 -> (k -> k -> Bool)
187 -> Map k v
188 -> Map k v
189 -> Map k v
190 = \{lk} {lv} {k} {v} equal first second ->
191 first # std:List.filter (\(e : Entry k v) -> std:Bool.not (member equal e.key first)) second
192
193in { Entry
194 , Map
195 , natural
196 , text
197 , bool
198 , empty
199 , singleton
200 , size
201 , lookup
202 , member
203 , delete
204 , insert
205 , insertWith
206 , adjust
207 , alter
208 , fromList
209 , toList
210 , keys
211 , values
212 , map
213 , filter
214 , fold
215 , union
216 }