0 | module Libraries.Data.StringMap
12 | data Tree : Nat -> Type -> Type where
13 | Leaf : Key -> v -> Tree Z v
14 | Branch2 : Tree n v -> Key -> Tree n v -> Tree (S n) v
15 | Branch3 : Tree n v -> Key -> Tree n v -> Key -> Tree n v -> Tree (S n) v
23 | branch4 a b c d e f g =
24 | Branch2 (Branch2 a b c) d (Branch2 e f g)
33 | branch5 a b c d e f g h i =
34 | Branch2 (Branch2 a b c) d (Branch3 e f g h i)
44 | branch6 a b c d e f g h i j k =
45 | Branch3 (Branch2 a b c) d (Branch2 e f g) h (Branch2 i j k)
56 | branch7 a b c d e f g h i j k l m =
57 | Branch3 (Branch3 a b c d e) f (Branch2 g h i) j (Branch2 k l m)
59 | merge1 : Tree n v -> Key -> Tree (S n) v -> Key -> Tree (S n) v -> Tree (S (S n)) v
60 | merge1 a b (Branch2 c d e) f (Branch2 g h i) = branch5 a b c d e f g h i
61 | merge1 a b (Branch2 c d e) f (Branch3 g h i j k) = branch6 a b c d e f g h i j k
62 | merge1 a b (Branch3 c d e f g) h (Branch2 i j k) = branch6 a b c d e f g h i j k
63 | merge1 a b (Branch3 c d e f g) h (Branch3 i j k l m) = branch7 a b c d e f g h i j k l m
65 | merge2 : Tree (S n) v -> Key -> Tree n v -> Key -> Tree (S n) v -> Tree (S (S n)) v
66 | merge2 (Branch2 a b c) d e f (Branch2 g h i) = branch5 a b c d e f g h i
67 | merge2 (Branch2 a b c) d e f (Branch3 g h i j k) = branch6 a b c d e f g h i j k
68 | merge2 (Branch3 a b c d e) f g h (Branch2 i j k) = branch6 a b c d e f g h i j k
69 | merge2 (Branch3 a b c d e) f g h (Branch3 i j k l m) = branch7 a b c d e f g h i j k l m
71 | merge3 : Tree (S n) v -> Key -> Tree (S n) v -> Key -> Tree n v -> Tree (S (S n)) v
72 | merge3 (Branch2 a b c) d (Branch2 e f g) h i = branch5 a b c d e f g h i
73 | merge3 (Branch2 a b c) d (Branch3 e f g h i) j k = branch6 a b c d e f g h i j k
74 | merge3 (Branch3 a b c d e) f (Branch2 g h i) j k = branch6 a b c d e f g h i j k
75 | merge3 (Branch3 a b c d e) f (Branch3 g h i j k) l m = branch7 a b c d e f g h i j k l m
77 | treeLookup : Key -> Tree n v -> Maybe v
78 | treeLookup k (Leaf k' v) =
83 | treeLookup k (Branch2 t1 k' t2) =
88 | treeLookup k (Branch3 t1 k1 t2 k2 t3) =
91 | else if k <= k2 then
96 | treeInsert' : Key -> v -> Tree n v -> Either (Tree n v) (Tree n v, Key, Tree n v)
97 | treeInsert' k v (Leaf k' v') =
98 | case compare k k' of
99 | LT => Right (Leaf k v, k, Leaf k' v')
100 | EQ => Left (Leaf k v)
101 | GT => Right (Leaf k' v', k', Leaf k v)
102 | treeInsert' k v (Branch2 t1 k' t2) =
104 | case treeInsert' k v t1 of
105 | Left t1' => Left (Branch2 t1' k' t2)
106 | Right (a, b, c) => Left (Branch3 a b c k' t2)
108 | case treeInsert' k v t2 of
109 | Left t2' => Left (Branch2 t1 k' t2')
110 | Right (a, b, c) => Left (Branch3 t1 k' a b c)
111 | treeInsert' k v (Branch3 t1 k1 t2 k2 t3) =
113 | case treeInsert' k v t1 of
114 | Left t1' => Left (Branch3 t1' k1 t2 k2 t3)
115 | Right (a, b, c) => Right (Branch2 a b c, k1, Branch2 t2 k2 t3)
118 | case treeInsert' k v t2 of
119 | Left t2' => Left (Branch3 t1 k1 t2' k2 t3)
120 | Right (a, b, c) => Right (Branch2 t1 k1 a, b, Branch2 c k2 t3)
122 | case treeInsert' k v t3 of
123 | Left t3' => Left (Branch3 t1 k1 t2 k2 t3')
124 | Right (a, b, c) => Right (Branch2 t1 k1 t2, k2, Branch2 a b c)
126 | treeInsert : Key -> v -> Tree n v -> Either (Tree n v) (Tree (S n) v)
128 | case treeInsert' k v t of
130 | Right (a, b, c) => Right (Branch2 a b c)
132 | delType : Nat -> Type -> Type
134 | delType (S n) v = Tree n v
136 | treeDelete : {n : _} -> Key -> Tree n v -> Either (Tree n v) (delType n v)
137 | treeDelete k (Leaf k' v) =
142 | treeDelete {n=S Z} k (Branch2 t1 k' t2) =
144 | case treeDelete k t1 of
145 | Left t1' => Left (Branch2 t1' k' t2)
146 | Right () => Right t2
148 | case treeDelete k t2 of
149 | Left t2' => Left (Branch2 t1 k' t2')
150 | Right () => Right t1
151 | treeDelete {n=S Z} k (Branch3 t1 k1 t2 k2 t3) =
153 | case treeDelete k t1 of
154 | Left t1' => Left (Branch3 t1' k1 t2 k2 t3)
155 | Right () => Left (Branch2 t2 k2 t3)
156 | else if k <= k2 then
157 | case treeDelete k t2 of
158 | Left t2' => Left (Branch3 t1 k1 t2' k2 t3)
159 | Right () => Left (Branch2 t1 k1 t3)
161 | case treeDelete k t3 of
162 | Left t3' => Left (Branch3 t1 k1 t2 k2 t3')
163 | Right () => Left (Branch2 t1 k1 t2)
164 | treeDelete {n=S (S _)} k (Branch2 t1 k' t2) =
166 | case treeDelete k t1 of
167 | Left t1' => Left (Branch2 t1' k' t2)
170 | Branch2 a b c => Right (Branch3 t1' k' a b c)
171 | Branch3 a b c d e => Left (branch4 t1' k' a b c d e)
173 | case treeDelete k t2 of
174 | Left t2' => Left (Branch2 t1 k' t2')
177 | Branch2 a b c => Right (Branch3 a b c k' t2')
178 | Branch3 a b c d e => Left (branch4 a b c d e k' t2')
179 | treeDelete {n=(S (S _))} k (Branch3 t1 k1 t2 k2 t3) =
181 | case treeDelete k t1 of
182 | Left t1' => Left (Branch3 t1' k1 t2 k2 t3)
183 | Right t1' => Left (merge1 t1' k1 t2 k2 t3)
184 | else if k <= k2 then
185 | case treeDelete k t2 of
186 | Left t2' => Left (Branch3 t1 k1 t2' k2 t3)
187 | Right t2' => Left (merge2 t1 k1 t2' k2 t3)
189 | case treeDelete k t3 of
190 | Left t3' => Left (Branch3 t1 k1 t2 k2 t3')
191 | Right t3' => Left (merge3 t1 k1 t2 k2 t3')
193 | treeToList : Tree n v -> List (Key, v)
194 | treeToList = treeToList' (:: [])
196 | treeToList' : forall n . ((Key, v) -> List (Key, v)) -> Tree n v -> List (Key, v)
197 | treeToList' cont (Leaf k v) = cont (k, v)
198 | treeToList' cont (Branch2 t1 _ t2) = treeToList' (:: treeToList' cont t2) t1
199 | treeToList' cont (Branch3 t1 _ t2 _ t3) = treeToList' (:: treeToList' (:: treeToList' cont t3) t2) t1
202 | data StringMap : Type -> Type where
203 | Empty : StringMap v
204 | M : (n : Nat) -> Tree n v -> StringMap v
207 | empty : StringMap v
211 | singleton : String -> v -> StringMap v
212 | singleton k v = M Z (Leaf k v)
215 | lookup : String -> StringMap v -> Maybe v
216 | lookup _ Empty = Nothing
217 | lookup k (M _ t) = treeLookup k t
220 | insert : String -> v -> StringMap v -> StringMap v
221 | insert k v Empty = M Z (Leaf k v)
222 | insert k v (M _ t) =
223 | case treeInsert k v t of
224 | Left t' => (M _ t')
225 | Right t' => (M _ t')
228 | insertFrom : List (String, v) -> StringMap v -> StringMap v
229 | insertFrom = flip $
foldl $
flip $
uncurry insert
232 | delete : String -> StringMap v -> StringMap v
233 | delete _ Empty = Empty
235 | case treeDelete k t of
236 | Left t' => (M _ t')
238 | delete k (M (S _) t) =
239 | case treeDelete k t of
240 | Left t' => (M _ t')
241 | Right t' => (M _ t')
244 | fromList : List (String, v) -> StringMap v
245 | fromList l = foldl (flip (uncurry insert)) empty l
248 | toList : StringMap v -> List (String, v)
250 | toList (M _ t) = treeToList t
254 | keys : StringMap v -> List String
255 | keys = map fst . StringMap.toList
259 | values : StringMap v -> List v
260 | values = map snd . StringMap.toList
262 | treeMap : (a -> b) -> Tree n a -> Tree n b
263 | treeMap f (Leaf k v) = Leaf k (f v)
264 | treeMap f (Branch2 t1 k t2) = Branch2 (treeMap f t1) k (treeMap f t2)
265 | treeMap f (Branch3 t1 k1 t2 k2 t3)
266 | = Branch3 (treeMap f t1) k1 (treeMap f t2) k2 (treeMap f t3)
269 | implementation Functor StringMap where
270 | map _ Empty = Empty
271 | map f (M n t) = M _ (treeMap f t)
275 | sharedSupport : StringMap v -> StringMap v -> StringMap (v, v)
277 | = foldl (\acc, (k, v) => case lookup k l of
278 | Just v' => insert k (v, v') acc
280 | empty . StringMap.toList
285 | mergeWith : (v -> v -> v) -> StringMap v -> StringMap v -> StringMap v
286 | mergeWith f x y = insertFrom inserted x where
287 | inserted : List (Key, v)
289 | (k, v) <- StringMap.toList y
290 | let v' = (maybe id f $
lookup k x) v
296 | merge : Semigroup v => StringMap v -> StringMap v -> StringMap v
297 | merge = mergeWith (<+>)
301 | mergeLeft : StringMap v -> StringMap v -> StringMap v
302 | mergeLeft x y = mergeWith const x y
305 | adjust : String -> (v -> v) -> StringMap v -> StringMap v
309 | Just v => insert k (f v) m
312 | Show v => Show (StringMap v) where
313 | show m = show $
map {b=String} (\(k,v) => k ++ "->" ++ show v) $
StringMap.toList m
321 | Semigroup v => Semigroup (StringMap v) where
325 | (Semigroup v) => Monoid (StringMap v) where