0 | module Libraries.Data.NameMap
14 | data Tree : Nat -> Type -> Type where
15 | Leaf : Key -> v -> Tree Z v
16 | Branch2 : Tree n v -> Key -> Tree n v -> Tree (S n) v
17 | Branch3 : Tree n v -> Key -> Tree n v -> Key -> Tree n v -> Tree (S n) v
19 | Show v => Show (Tree n v) where
20 | show (Leaf k v) = "Leaf: " ++ show k ++ " -> " ++ show v ++ "\n"
21 | show (Branch2 t1 k t2) = "Branch2: " ++ show t1 ++ "\n < " ++ show k ++ "\n" ++ show t2 ++ "\n"
22 | show (Branch3 t1 k1 t2 k2 t3) = "Branch3: " ++ show t1 ++ "\n < " ++ show k1 ++ "\n" ++ show t2 ++ "\n < " ++ show k2 ++ "\n" ++ show t3 ++ "\n"
30 | branch4 a b c d e f g =
31 | Branch2 (Branch2 a b c) d (Branch2 e f g)
40 | branch5 a b c d e f g h i =
41 | Branch2 (Branch2 a b c) d (Branch3 e f g h i)
51 | branch6 a b c d e f g h i j k =
52 | Branch3 (Branch2 a b c) d (Branch2 e f g) h (Branch2 i j k)
63 | branch7 a b c d e f g h i j k l m =
64 | Branch3 (Branch3 a b c d e) f (Branch2 g h i) j (Branch2 k l m)
66 | merge1 : Tree n v -> Key -> Tree (S n) v -> Key -> Tree (S n) v -> Tree (S (S n)) v
67 | merge1 a b (Branch2 c d e) f (Branch2 g h i) = branch5 a b c d e f g h i
68 | 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
69 | 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
70 | 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
72 | merge2 : Tree (S n) v -> Key -> Tree n v -> Key -> Tree (S n) v -> Tree (S (S n)) v
73 | merge2 (Branch2 a b c) d e f (Branch2 g h i) = branch5 a b c d e f g h i
74 | 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
75 | 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
76 | 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
78 | merge3 : Tree (S n) v -> Key -> Tree (S n) v -> Key -> Tree n v -> Tree (S (S n)) v
79 | merge3 (Branch2 a b c) d (Branch2 e f g) h i = branch5 a b c d e f g h i
80 | 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
81 | 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
82 | 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
84 | treeLookup : Key -> Tree n v -> Maybe v
85 | treeLookup k (Leaf k' v) =
90 | treeLookup k (Branch2 t1 k' t2) =
95 | treeLookup k (Branch3 t1 k1 t2 k2 t3) =
98 | else if k <= k2 then
103 | treeInsert' : Key -> v -> Tree n v -> Either (Tree n v) (Tree n v, Key, Tree n v)
104 | treeInsert' k v (Leaf k' v') =
105 | case compare k k' of
106 | LT => Right (Leaf k v, k, Leaf k' v')
107 | EQ => Left (Leaf k v)
108 | GT => Right (Leaf k' v', k', Leaf k v)
109 | treeInsert' k v (Branch2 t1 k' t2) =
111 | case treeInsert' k v t1 of
112 | Left t1' => Left (Branch2 t1' k' t2)
113 | Right (a, b, c) => Left (Branch3 a b c k' t2)
115 | case treeInsert' k v t2 of
116 | Left t2' => Left (Branch2 t1 k' t2')
117 | Right (a, b, c) => Left (Branch3 t1 k' a b c)
118 | treeInsert' k v (Branch3 t1 k1 t2 k2 t3) =
120 | case treeInsert' k v t1 of
121 | Left t1' => Left (Branch3 t1' k1 t2 k2 t3)
122 | Right (a, b, c) => Right (Branch2 a b c, k1, Branch2 t2 k2 t3)
125 | case treeInsert' k v t2 of
126 | Left t2' => Left (Branch3 t1 k1 t2' k2 t3)
127 | Right (a, b, c) => Right (Branch2 t1 k1 a, b, Branch2 c k2 t3)
129 | case treeInsert' k v t3 of
130 | Left t3' => Left (Branch3 t1 k1 t2 k2 t3')
131 | Right (a, b, c) => Right (Branch2 t1 k1 t2, k2, Branch2 a b c)
133 | treeInsert : Key -> v -> Tree n v -> Either (Tree n v) (Tree (S n) v)
135 | case treeInsert' k v t of
137 | Right (a, b, c) => Right (Branch2 a b c)
139 | delType : Nat -> Type -> Type
141 | delType (S n) v = Tree n v
143 | treeDelete : {n : _} ->
144 | Key -> Tree n v -> Either (Tree n v) (delType n v)
145 | treeDelete k (Leaf k' v) =
150 | treeDelete {n=S Z} k (Branch2 t1 k' t2) =
152 | case treeDelete k t1 of
153 | Left t1' => Left (Branch2 t1' k' t2)
154 | Right () => Right t2
156 | case treeDelete k t2 of
157 | Left t2' => Left (Branch2 t1 k' t2')
158 | Right () => Right t1
159 | treeDelete {n=S Z} k (Branch3 t1 k1 t2 k2 t3) =
161 | case treeDelete k t1 of
162 | Left t1' => Left (Branch3 t1' k1 t2 k2 t3)
163 | Right () => Left (Branch2 t2 k2 t3)
164 | else if k <= k2 then
165 | case treeDelete k t2 of
166 | Left t2' => Left (Branch3 t1 k1 t2' k2 t3)
167 | Right () => Left (Branch2 t1 k1 t3)
169 | case treeDelete k t3 of
170 | Left t3' => Left (Branch3 t1 k1 t2 k2 t3')
171 | Right () => Left (Branch2 t1 k1 t2)
172 | treeDelete {n=S (S _)} k (Branch2 t1 k' t2) =
174 | case treeDelete k t1 of
175 | Left t1' => Left (Branch2 t1' k' t2)
178 | Branch2 a b c => Right (Branch3 t1' k' a b c)
179 | Branch3 a b c d e => Left (branch4 t1' k' a b c d e)
181 | case treeDelete k t2 of
182 | Left t2' => Left (Branch2 t1 k' t2')
185 | Branch2 a b c => Right (Branch3 a b c k' t2')
186 | Branch3 a b c d e => Left (branch4 a b c d e k' t2')
187 | treeDelete {n=(S (S _))} k (Branch3 t1 k1 t2 k2 t3) =
189 | case treeDelete k t1 of
190 | Left t1' => Left (Branch3 t1' k1 t2 k2 t3)
191 | Right t1' => Left (merge1 t1' k1 t2 k2 t3)
192 | else if k <= k2 then
193 | case treeDelete k t2 of
194 | Left t2' => Left (Branch3 t1 k1 t2' k2 t3)
195 | Right t2' => Left (merge2 t1 k1 t2' k2 t3)
197 | case treeDelete k t3 of
198 | Left t3' => Left (Branch3 t1 k1 t2 k2 t3')
199 | Right t3' => Left (merge3 t1 k1 t2 k2 t3')
201 | treeToList : Tree n v -> List (Key, v)
202 | treeToList = treeToList' []
204 | treeToList' : forall n . List (Key, v) -> Tree n v -> List (Key, v)
205 | treeToList' rest (Leaf k v) = (k, v) :: rest
206 | treeToList' rest (Branch2 t1 _ t2)
207 | = treeToList' (treeToList' rest t2) t1
208 | treeToList' rest (Branch3 t1 _ t2 _ t3)
209 | = treeToList' (treeToList' (treeToList' rest t3) t2) t1
212 | data NameMap : Type -> Type where
214 | M : (n : Nat) -> Tree n v -> NameMap v
217 | Show v => Show (NameMap v) where
218 | show Empty = "Empty NameMap"
219 | show (M n t) = "NameMap M(" ++ show n ++ "):\n" ++ show t
226 | singleton : Name -> v -> NameMap v
227 | singleton n v = M Z $
Leaf n v
230 | null : NameMap v -> Bool
235 | lookup : Name -> NameMap v -> Maybe v
236 | lookup _ Empty = Nothing
237 | lookup k (M _ t) = treeLookup k t
240 | insert : Name -> v -> NameMap v -> NameMap v
241 | insert k v Empty = M Z (Leaf k v)
242 | insert k v (M _ t) =
243 | case treeInsert k v t of
244 | Left t' => (M _ t')
245 | Right t' => (M _ t')
248 | insertFrom : List (Name, v) -> NameMap v -> NameMap v
249 | insertFrom = flip $
foldl $
flip $
uncurry insert
252 | delete : Name -> NameMap v -> NameMap v
253 | delete _ Empty = Empty
255 | case treeDelete k t of
256 | Left t' => (M _ t')
258 | delete k (M (S _) t) =
259 | case treeDelete k t of
260 | Left t' => (M _ t')
261 | Right t' => (M _ t')
264 | fromList : List (Name, v) -> NameMap v
265 | fromList l = foldl (flip (uncurry insert)) empty l
268 | toList : NameMap v -> List (Name, v)
270 | toList (M _ t) = treeToList t
274 | keys : NameMap v -> List Name
275 | keys = map fst . toList
279 | values : NameMap v -> List v
280 | values = map snd . toList
282 | treeMap : (a -> b) -> Tree n a -> Tree n b
283 | treeMap f (Leaf k v) = Leaf k (f v)
284 | treeMap f (Branch2 t1 k t2) = Branch2 (treeMap f t1) k (treeMap f t2)
285 | treeMap f (Branch3 t1 k1 t2 k2 t3)
286 | = Branch3 (treeMap f t1) k1 (treeMap f t2) k2 (treeMap f t3)
289 | implementation Functor NameMap where
290 | map _ Empty = Empty
291 | map f (M n t) = M _ (treeMap f t)
293 | treeMapWithKey : (Name -> a -> b) -> Tree n a -> Tree n b
294 | treeMapWithKey f (Leaf k v) = Leaf k (f k v)
295 | treeMapWithKey f (Branch2 t1 k t2) = Branch2 (treeMapWithKey f t1) k (treeMapWithKey f t2)
296 | treeMapWithKey f (Branch3 t1 k1 t2 k2 t3)
297 | = Branch3 (treeMapWithKey f t1) k1 (treeMapWithKey f t2) k2 (treeMapWithKey f t3)
300 | mapWithKey : (Name -> a -> b) -> NameMap a -> NameMap b
301 | mapWithKey f Empty = Empty
302 | mapWithKey f (M n t) = M _ (treeMapWithKey f t)
307 | mergeWith : (v -> v -> v) -> NameMap v -> NameMap v -> NameMap v
308 | mergeWith f x y = insertFrom inserted x where
309 | inserted : List (Key, v)
312 | let v' = (maybe id f $
lookup k x) v
318 | merge : Semigroup v => NameMap v -> NameMap v -> NameMap v
319 | merge = mergeWith (<+>)
323 | mergeLeft : NameMap v -> NameMap v -> NameMap v
324 | mergeLeft = mergeWith const
332 | Semigroup v => Semigroup (NameMap v) where
336 | (Semigroup v) => Monoid (NameMap v) where
340 | treeFilterBy : (Key -> Bool) -> Tree n v -> NameMap v
341 | treeFilterBy test = loop empty where
343 | loop : NameMap v -> Tree _ v -> NameMap v
344 | loop acc (Leaf k v)
345 | = let True = test k | _ => acc in
347 | loop acc (Branch2 t1 _ t2)
348 | = loop (loop acc t1) t2
349 | loop acc (Branch3 t1 _ t2 _ t3)
350 | = loop (loop (loop acc t1) t2) t3
352 | treeFilterByM : Monad m => (Key -> m Bool) -> Tree n v -> m (NameMap v)
353 | treeFilterByM test = loop empty where
355 | loop : NameMap v -> Tree _ v -> m (NameMap v)
356 | loop acc (Leaf k v)
357 | = do True <- test k | _ => pure acc
358 | pure (insert k v acc)
359 | loop acc (Branch2 t1 _ t2)
360 | = do acc <- loop acc t1
362 | loop acc (Branch3 t1 _ t2 _ t3)
363 | = do acc <- loop acc t1
368 | filterBy : (Name -> Bool) -> NameMap v -> NameMap v
369 | filterBy test Empty = Empty
370 | filterBy test (M _ t) = treeFilterBy test t
373 | filterByM : Monad m => (Name -> m Bool) -> NameMap v -> m (NameMap v)
374 | filterByM test Empty = pure Empty
375 | filterByM test (M _ t) = treeFilterByM test t
377 | treeMapMaybeM : Monad m => (Key -> m (Maybe a)) -> Tree _ v -> m (NameMap a)
378 | treeMapMaybeM test = loop empty where
380 | loop : NameMap a -> Tree _ v -> m (NameMap a)
381 | loop acc (Leaf k v)
382 | = do Just a <- test k | _ => pure acc
383 | pure (insert k a acc)
384 | loop acc (Branch2 t1 _ t2)
385 | = do acc <- loop acc t1
387 | loop acc (Branch3 t1 _ t2 _ t3)
388 | = do acc <- loop acc t1
393 | mapMaybeM : Monad m => (Name -> m (Maybe a)) -> NameMap v -> m (NameMap a)
394 | mapMaybeM test Empty = pure Empty
395 | mapMaybeM test (M _ t) = treeMapMaybeM test t
397 | treeFoldl : (acc -> Name -> v -> acc) -> acc -> Tree _ v -> acc
398 | treeFoldl f z (Leaf k v) = f z k v
399 | treeFoldl f z (Branch2 l _ r) = treeFoldl f (treeFoldl f z l) r
400 | treeFoldl f z (Branch3 l _ m _ r) = treeFoldl f (treeFoldl f (treeFoldl f z l) m) r
403 | foldlNames : (acc -> Name -> v -> acc) -> acc -> NameMap v -> acc
404 | foldlNames f z Empty = z
405 | foldlNames f z (M _ t) = treeFoldl f z t