0 | module Libraries.Data.UserNameMap
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
25 | branch4 a b c d e f g =
26 | Branch2 (Branch2 a b c) d (Branch2 e f g)
35 | branch5 a b c d e f g h i =
36 | Branch2 (Branch2 a b c) d (Branch3 e f g h i)
46 | branch6 a b c d e f g h i j k =
47 | Branch3 (Branch2 a b c) d (Branch2 e f g) h (Branch2 i j k)
58 | branch7 a b c d e f g h i j k l m =
59 | Branch3 (Branch3 a b c d e) f (Branch2 g h i) j (Branch2 k l m)
61 | merge1 : Tree n v -> Key -> Tree (S n) v -> Key -> Tree (S n) v -> Tree (S (S n)) v
62 | merge1 a b (Branch2 c d e) f (Branch2 g h i) = branch5 a b c d e f g h i
63 | 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
64 | 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
65 | 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
67 | merge2 : Tree (S n) v -> Key -> Tree n v -> Key -> Tree (S n) v -> Tree (S (S n)) v
68 | merge2 (Branch2 a b c) d e f (Branch2 g h i) = branch5 a b c d e f g h i
69 | 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
70 | 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
71 | 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
73 | merge3 : Tree (S n) v -> Key -> Tree (S n) v -> Key -> Tree n v -> Tree (S (S n)) v
74 | merge3 (Branch2 a b c) d (Branch2 e f g) h i = branch5 a b c d e f g h i
75 | 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
76 | 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
77 | 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
79 | treeLookup : Key -> Tree n v -> Maybe v
80 | treeLookup k (Leaf k' v) =
85 | treeLookup k (Branch2 t1 k' t2) =
90 | treeLookup k (Branch3 t1 k1 t2 k2 t3) =
93 | else if k <= k2 then
98 | treeInsert' : Key -> v -> Tree n v -> Either (Tree n v) (Tree n v, Key, Tree n v)
99 | treeInsert' k v (Leaf k' v') =
100 | case compare k k' of
101 | LT => Right (Leaf k v, k, Leaf k' v')
102 | EQ => Left (Leaf k v)
103 | GT => Right (Leaf k' v', k', Leaf k v)
104 | treeInsert' k v (Branch2 t1 k' t2) =
106 | case treeInsert' k v t1 of
107 | Left t1' => Left (Branch2 t1' k' t2)
108 | Right (a, b, c) => Left (Branch3 a b c k' t2)
110 | case treeInsert' k v t2 of
111 | Left t2' => Left (Branch2 t1 k' t2')
112 | Right (a, b, c) => Left (Branch3 t1 k' a b c)
113 | treeInsert' k v (Branch3 t1 k1 t2 k2 t3) =
115 | case treeInsert' k v t1 of
116 | Left t1' => Left (Branch3 t1' k1 t2 k2 t3)
117 | Right (a, b, c) => Right (Branch2 a b c, k1, Branch2 t2 k2 t3)
120 | case treeInsert' k v t2 of
121 | Left t2' => Left (Branch3 t1 k1 t2' k2 t3)
122 | Right (a, b, c) => Right (Branch2 t1 k1 a, b, Branch2 c k2 t3)
124 | case treeInsert' k v t3 of
125 | Left t3' => Left (Branch3 t1 k1 t2 k2 t3')
126 | Right (a, b, c) => Right (Branch2 t1 k1 t2, k2, Branch2 a b c)
128 | treeInsert : Key -> v -> Tree n v -> Either (Tree n v) (Tree (S n) v)
130 | case treeInsert' k v t of
132 | Right (a, b, c) => Right (Branch2 a b c)
134 | delType : Nat -> Type -> Type
136 | delType (S n) v = Tree n v
138 | treeDelete : {n : _} -> Key -> Tree n v -> Either (Tree n v) (delType n v)
139 | treeDelete k (Leaf k' v) =
144 | treeDelete {n=S Z} k (Branch2 t1 k' t2) =
146 | case treeDelete k t1 of
147 | Left t1' => Left (Branch2 t1' k' t2)
148 | Right () => Right t2
150 | case treeDelete k t2 of
151 | Left t2' => Left (Branch2 t1 k' t2')
152 | Right () => Right t1
153 | treeDelete {n=S Z} k (Branch3 t1 k1 t2 k2 t3) =
155 | case treeDelete k t1 of
156 | Left t1' => Left (Branch3 t1' k1 t2 k2 t3)
157 | Right () => Left (Branch2 t2 k2 t3)
158 | else if k <= k2 then
159 | case treeDelete k t2 of
160 | Left t2' => Left (Branch3 t1 k1 t2' k2 t3)
161 | Right () => Left (Branch2 t1 k1 t3)
163 | case treeDelete k t3 of
164 | Left t3' => Left (Branch3 t1 k1 t2 k2 t3')
165 | Right () => Left (Branch2 t1 k1 t2)
166 | treeDelete {n=S (S _)} k (Branch2 t1 k' t2) =
168 | case treeDelete k t1 of
169 | Left t1' => Left (Branch2 t1' k' t2)
172 | Branch2 a b c => Right (Branch3 t1' k' a b c)
173 | Branch3 a b c d e => Left (branch4 t1' k' a b c d e)
175 | case treeDelete k t2 of
176 | Left t2' => Left (Branch2 t1 k' t2')
179 | Branch2 a b c => Right (Branch3 a b c k' t2')
180 | Branch3 a b c d e => Left (branch4 a b c d e k' t2')
181 | treeDelete {n=(S (S _))} k (Branch3 t1 k1 t2 k2 t3) =
183 | case treeDelete k t1 of
184 | Left t1' => Left (Branch3 t1' k1 t2 k2 t3)
185 | Right t1' => Left (merge1 t1' k1 t2 k2 t3)
186 | else if k <= k2 then
187 | case treeDelete k t2 of
188 | Left t2' => Left (Branch3 t1 k1 t2' k2 t3)
189 | Right t2' => Left (merge2 t1 k1 t2' k2 t3)
191 | case treeDelete k t3 of
192 | Left t3' => Left (Branch3 t1 k1 t2 k2 t3')
193 | Right t3' => Left (merge3 t1 k1 t2 k2 t3')
195 | treeToList : Tree n v -> List (Key, v)
196 | treeToList = treeToList' (:: [])
198 | treeToList' : forall n . ((Key, v) -> List (Key, v)) -> Tree n v -> List (Key, v)
199 | treeToList' cont (Leaf k v) = cont (k, v)
200 | treeToList' cont (Branch2 t1 _ t2) = treeToList' (:: treeToList' cont t2) t1
201 | treeToList' cont (Branch3 t1 _ t2 _ t3) = treeToList' (:: treeToList' (:: treeToList' cont t3) t2) t1
204 | data UserNameMap : Type -> Type where
205 | Empty : UserNameMap v
206 | M : (n : Nat) -> Tree n v -> UserNameMap v
209 | empty : UserNameMap v
213 | singleton : UserName -> v -> UserNameMap v
214 | singleton k v = M Z (Leaf k v)
217 | lookup : UserName -> UserNameMap v -> Maybe v
218 | lookup _ Empty = Nothing
219 | lookup k (M _ t) = treeLookup k t
222 | lookupName : (n : Name) -> (dict : UserNameMap v) -> Maybe v
223 | lookupName n dict = case userNameRoot n of
225 | Just un => lookup un dict
228 | insert : UserName -> v -> UserNameMap v -> UserNameMap v
229 | insert k v Empty = M Z (Leaf k v)
230 | insert k v (M _ t) =
231 | case treeInsert k v t of
232 | Left t' => (M _ t')
233 | Right t' => (M _ t')
236 | insertFrom : List (UserName, v) -> UserNameMap v -> UserNameMap v
237 | insertFrom = flip $
foldl $
flip $
uncurry insert
240 | delete : UserName -> UserNameMap v -> UserNameMap v
241 | delete _ Empty = Empty
243 | case treeDelete k t of
244 | Left t' => (M _ t')
246 | delete k (M (S _) t) =
247 | case treeDelete k t of
248 | Left t' => (M _ t')
249 | Right t' => (M _ t')
252 | fromList : List (UserName, v) -> UserNameMap v
253 | fromList l = foldl (flip (uncurry insert)) empty l
256 | toList : UserNameMap v -> List (UserName, v)
258 | toList (M _ t) = treeToList t
262 | keys : UserNameMap v -> List UserName
263 | keys = map fst . UserNameMap.toList
267 | values : UserNameMap v -> List v
268 | values = map snd . UserNameMap.toList
270 | treeMap : (a -> b) -> Tree n a -> Tree n b
271 | treeMap f (Leaf k v) = Leaf k (f v)
272 | treeMap f (Branch2 t1 k t2) = Branch2 (treeMap f t1) k (treeMap f t2)
273 | treeMap f (Branch3 t1 k1 t2 k2 t3)
274 | = Branch3 (treeMap f t1) k1 (treeMap f t2) k2 (treeMap f t3)
277 | implementation Functor UserNameMap where
278 | map _ Empty = Empty
279 | map f (M n t) = M _ (treeMap f t)
284 | mergeWith : (v -> v -> v) -> UserNameMap v -> UserNameMap v -> UserNameMap v
285 | mergeWith f x y = insertFrom inserted x where
286 | inserted : List (Key, v)
288 | (k, v) <- UserNameMap.toList y
289 | let v' = (maybe id f $
lookup k x) v
295 | merge : Semigroup v => UserNameMap v -> UserNameMap v -> UserNameMap v
296 | merge = mergeWith (<+>)
300 | mergeLeft : UserNameMap v -> UserNameMap v -> UserNameMap v
301 | mergeLeft x y = mergeWith const x y
304 | adjust : UserName -> (v -> v) -> UserNameMap v -> UserNameMap v
308 | Just v => insert k (f v) m
311 | Show v => Show (UserNameMap v) where
312 | show m = show $
map (\(k,v) => show k ++ "->" ++ show v) $
UserNameMap.toList m
320 | Semigroup v => Semigroup (UserNameMap v) where
324 | (Semigroup v) => Monoid (UserNameMap v) where