0 | module Data.SortedMap.Dependent
4 | import Decidable.Equality
13 | data Tree : Nat -> (k : Type) -> (v : k -> Type) -> Ord k -> Type where
14 | Leaf : (x : k) -> v x -> Tree Z k v o
15 | Branch2 : Tree n k v o -> k -> Tree n k v o -> Tree (S n) k v o
16 | Branch3 : Tree n k v o -> k -> Tree n k v o -> k -> Tree n k v o -> Tree (S n) k v o
19 | Tree n k v o -> k ->
20 | Tree n k v o -> k ->
21 | Tree n k v o -> k ->
23 | Tree (S (S n)) k v o
24 | branch4 a b c d e f g =
25 | Branch2 (Branch2 a b c) d (Branch2 e f g)
28 | Tree n k v o -> k ->
29 | Tree n k v o -> k ->
30 | Tree n k v o -> k ->
31 | Tree n k v o -> k ->
33 | Tree (S (S n)) k v o
34 | branch5 a b c d e f g h i =
35 | Branch2 (Branch2 a b c) d (Branch3 e f g h i)
38 | Tree n k v o -> k ->
39 | Tree n k v o -> k ->
40 | Tree n k v o -> k ->
41 | Tree n k v o -> k ->
42 | Tree n k v o -> k ->
44 | Tree (S (S n)) k v o
45 | branch6 a b c d e f g h i j k =
46 | Branch3 (Branch2 a b c) d (Branch2 e f g) h (Branch2 i j k)
49 | Tree n k v o -> k ->
50 | Tree n k v o -> k ->
51 | Tree n k v o -> k ->
52 | Tree n k v o -> k ->
53 | Tree n k v o -> k ->
54 | Tree n k v o -> k ->
56 | Tree (S (S n)) k v o
57 | branch7 a b c d e f g h i j k l m =
58 | Branch3 (Branch3 a b c d e) f (Branch2 g h i) j (Branch2 k l m)
60 | merge1 : Tree n k v o -> k -> Tree (S n) k v o -> k -> Tree (S n) k v o -> Tree (S (S n)) k v o
61 | merge1 a b (Branch2 c d e) f (Branch2 g h i) = branch5 a b c d e f g h i
62 | 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
63 | 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
64 | 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
66 | merge2 : Tree (S n) k v o -> k -> Tree n k v o -> k -> Tree (S n) k v o -> Tree (S (S n)) k v o
67 | merge2 (Branch2 a b c) d e f (Branch2 g h i) = branch5 a b c d e f g h i
68 | 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
69 | 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
70 | 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
72 | merge3 : Tree (S n) k v o -> k -> Tree (S n) k v o -> k -> Tree n k v o -> Tree (S (S n)) k v o
73 | merge3 (Branch2 a b c) d (Branch2 e f g) h i = branch5 a b c d e f g h i
74 | 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
75 | 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
76 | 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
78 | treeLookup : Ord k => (x : k) -> Tree n k v o -> Maybe (y : k ** v y)
79 | treeLookup k (Leaf k' v) =
84 | treeLookup k (Branch2 t1 k' t2) =
89 | treeLookup k (Branch3 t1 k1 t2 k2 t3) =
92 | else if k <= k2 then
97 | treeInsert' : Ord k => (x : k) -> v x -> Tree n k v o -> Either (Tree n k v o) (Tree n k v o, k, Tree n k v o)
98 | treeInsert' k v (Leaf k' v') =
99 | case compare k k' of
100 | LT => Right (Leaf k v, k, Leaf k' v')
101 | EQ => Left (Leaf k v)
102 | GT => Right (Leaf k' v', k', Leaf k v)
103 | treeInsert' k v (Branch2 t1 k' t2) =
105 | case treeInsert' k v t1 of
106 | Left t1' => Left (Branch2 t1' k' t2)
107 | Right (a, b, c) => Left (Branch3 a b c k' t2)
109 | case treeInsert' k v t2 of
110 | Left t2' => Left (Branch2 t1 k' t2')
111 | Right (a, b, c) => Left (Branch3 t1 k' a b c)
112 | treeInsert' k v (Branch3 t1 k1 t2 k2 t3) =
114 | case treeInsert' k v t1 of
115 | Left t1' => Left (Branch3 t1' k1 t2 k2 t3)
116 | Right (a, b, c) => Right (Branch2 a b c, k1, Branch2 t2 k2 t3)
119 | case treeInsert' k v t2 of
120 | Left t2' => Left (Branch3 t1 k1 t2' k2 t3)
121 | Right (a, b, c) => Right (Branch2 t1 k1 a, b, Branch2 c k2 t3)
123 | case treeInsert' k v t3 of
124 | Left t3' => Left (Branch3 t1 k1 t2 k2 t3')
125 | Right (a, b, c) => Right (Branch2 t1 k1 t2, k2, Branch2 a b c)
127 | treeInsert : Ord k => (x : k) -> v x -> Tree n k v o -> Either (Tree n k v o) (Tree (S n) k v o)
129 | case treeInsert' k v t of
131 | Right (a, b, c) => Right (Branch2 a b c)
133 | delType : Nat -> (k : Type) -> (v : k -> Type) -> Ord k -> Type
134 | delType Z k v o = ()
135 | delType (S n) k v o = Tree n k v o
137 | treeDelete : Ord k => (n : Nat) -> k -> Tree n k v o -> Either (Tree n k v o) (delType n k v o)
138 | treeDelete _ k (Leaf k' v) =
143 | treeDelete (S Z) k (Branch2 t1 k' t2) =
145 | case treeDelete Z k t1 of
146 | Left t1' => Left (Branch2 t1' k' t2)
147 | Right () => Right t2
149 | case treeDelete Z k t2 of
150 | Left t2' => Left (Branch2 t1 k' t2')
151 | Right () => Right t1
152 | treeDelete (S Z) k (Branch3 t1 k1 t2 k2 t3) =
154 | case treeDelete Z k t1 of
155 | Left t1' => Left (Branch3 t1' k1 t2 k2 t3)
156 | Right () => Left (Branch2 t2 k2 t3)
157 | else if k <= k2 then
158 | case treeDelete Z k t2 of
159 | Left t2' => Left (Branch3 t1 k1 t2' k2 t3)
160 | Right () => Left (Branch2 t1 k1 t3)
162 | case treeDelete Z k t3 of
163 | Left t3' => Left (Branch3 t1 k1 t2 k2 t3')
164 | Right () => Left (Branch2 t1 k1 t2)
165 | treeDelete (S (S n)) k (Branch2 t1 k' t2) =
167 | case treeDelete (S n) k t1 of
168 | Left t1' => Left (Branch2 t1' k' t2)
171 | Branch2 a b c => Right (Branch3 t1' k' a b c)
172 | Branch3 a b c d e => Left (branch4 t1' k' a b c d e)
174 | case treeDelete (S n) k t2 of
175 | Left t2' => Left (Branch2 t1 k' t2')
178 | Branch2 a b c => Right (Branch3 a b c k' t2')
179 | Branch3 a b c d e => Left (branch4 a b c d e k' t2')
180 | treeDelete (S (S n)) k (Branch3 t1 k1 t2 k2 t3) =
182 | case treeDelete (S n) k t1 of
183 | Left t1' => Left (Branch3 t1' k1 t2 k2 t3)
184 | Right t1' => Left (merge1 t1' k1 t2 k2 t3)
185 | else if k <= k2 then
186 | case treeDelete (S n) k t2 of
187 | Left t2' => Left (Branch3 t1 k1 t2' k2 t3)
188 | Right t2' => Left (merge2 t1 k1 t2' k2 t3)
190 | case treeDelete (S n) k t3 of
191 | Left t3' => Left (Branch3 t1 k1 t2 k2 t3')
192 | Right t3' => Left (merge3 t1 k1 t2 k2 t3')
194 | treeToList : Tree n k v o -> List (x : k ** v x)
195 | treeToList = treeToList' (:: [])
198 | treeToList' : {0 n : Nat} -> ((x : k ** v x) -> List (x : k ** v x)) -> Tree n k v o -> List (x : k ** v x)
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
208 | data SortedDMap : (k : Type) -> (v : k -> Type) -> Type where
209 | Empty : Ord k => SortedDMap k v
210 | M : (o : Ord k) => (n : Nat) -> Tree n k v o -> SortedDMap k v
213 | empty : Ord k => SortedDMap k v
217 | lookup : (x : k) -> SortedDMap k v -> Maybe (y : k ** v y)
218 | lookup _ Empty = Nothing
219 | lookup k (M _ t) = treeLookup k t
221 | public export %inline
222 | lookup' : SortedDMap k v -> (x : k) -> Maybe (y : k ** v y)
223 | lookup' = flip lookup
226 | lookupPrecise : DecEq k => (x : k) -> SortedDMap k v -> Maybe (v x)
227 | lookupPrecise x = lookup x >=> \(
y ** v)
=>
232 | public export %inline
233 | lookupPrecise' : DecEq k => SortedDMap k v -> (x : k) -> Maybe (v x)
234 | lookupPrecise' m x = lookupPrecise x m
237 | insert : (x : k) -> v x -> SortedDMap k v -> SortedDMap k v
238 | insert k v Empty = M Z (Leaf k v)
239 | insert k v (M _ t) =
240 | case treeInsert k v t of
241 | Left t' => (M _ t')
242 | Right t' => (M _ t')
244 | public export %inline
245 | insert' : SortedDMap k v -> (x : k ** v x) -> SortedDMap k v
246 | insert' m (
x ** v)
= insert x v m
249 | singleton : Ord k => (x : k) -> v x -> SortedDMap k v
250 | singleton k v = insert k v empty
253 | insertFrom : Foldable f => f (x : k ** v x) -> SortedDMap k v -> SortedDMap k v
254 | insertFrom = flip $
foldl insert'
256 | public export %inline
257 | insertFrom' : Foldable f => SortedDMap k v -> f (x : k ** v x) -> SortedDMap k v
258 | insertFrom' = flip insertFrom
261 | delete : k -> SortedDMap k v -> SortedDMap k v
262 | delete _ Empty = Empty
264 | case treeDelete Z k t of
265 | Left t' => (M _ t')
267 | delete k (M (S n) t) =
268 | case treeDelete (S n) k t of
269 | Left t' => (M _ t')
270 | Right t' => (M _ t')
272 | public export %inline
273 | delete' : SortedDMap k v -> k -> SortedDMap k v
274 | delete' = flip delete
284 | update : DecEq k => (x : k) -> (Maybe (v x) -> Maybe (v x)) -> SortedDMap k v -> SortedDMap k v
285 | update k f m = case f $
lookupPrecise k m of
286 | Just v => insert k v m
287 | Nothing => delete k m
289 | public export %inline
290 | update' : DecEq k => SortedDMap k v -> (x : k ** Maybe (v x) -> Maybe (v x)) -> SortedDMap k v
291 | update' m (
x ** f)
= update x f m
297 | updateExisting : DecEq k => (x : k) -> (v x -> v x) -> SortedDMap k v -> SortedDMap k v
298 | updateExisting k f m = case lookupPrecise k m of
299 | Just v => insert k (f v) m
302 | public export %inline
303 | updateExisting' : DecEq k => SortedDMap k v -> (x : k ** v x -> v x) -> SortedDMap k v
304 | updateExisting' m (
x ** f)
= updateExisting x f m
307 | fromList : Ord k => List (x : k ** v x) -> SortedDMap k v
308 | fromList = foldl insert' empty
311 | toList : SortedDMap k v -> List (x : k ** v x)
313 | toList (M _ t) = treeToList t
316 | public export %inline
317 | kvList : SortedDMap k v -> List (x : k ** v x)
322 | keys : SortedDMap k v -> List k
323 | keys = map fst . kvList
326 | values : SortedDMap k v -> List (x : k ** v x)
329 | treeMap : ({x : k} -> a x -> b x) -> Tree n k a o -> Tree n k b o
330 | treeMap f (Leaf k v) = Leaf k (f v)
331 | treeMap f (Branch2 t1 k t2) = Branch2 (treeMap f t1) k (treeMap f t2)
332 | treeMap f (Branch3 t1 k1 t2 k2 t3)
333 | = Branch3 (treeMap f t1) k1 (treeMap f t2) k2 (treeMap f t3)
335 | treeTraverse : Applicative f => ({x : k} -> a x -> f (b x)) -> Tree n k a o -> f (Tree n k b o)
336 | treeTraverse f (Leaf k v) = Leaf k <$> f v
337 | treeTraverse f (Branch2 t1 k t2) =
339 | <$> treeTraverse f t1
341 | <*> treeTraverse f t2
342 | treeTraverse f (Branch3 t1 k1 t2 k2 t3) =
344 | <$> treeTraverse f t1
346 | <*> treeTraverse f t2
348 | <*> treeTraverse f t3
351 | map : ({x : k} -> v x -> w x) -> SortedDMap k v -> SortedDMap k w
352 | map _ Empty = Empty
353 | map f (M _ t) = M _ $
treeMap f t
356 | foldl : (acc -> (x : k ** v x) -> acc) -> acc -> SortedDMap k v -> acc
357 | foldl f i = foldl f i . values
360 | foldr : ((x : k ** v x) -> acc -> acc) -> acc -> SortedDMap k v -> acc
361 | foldr f i = foldr f i . values
364 | foldlM : Monad m => (acc -> (x : k ** v x) -> m acc) -> acc -> SortedDMap k v -> m acc
365 | foldlM f i = foldl (\ma, b => ma >>= flip f b) (pure i)
368 | foldMap : Monoid m => (f : (x : k) -> v x -> m) -> SortedDMap k v -> m
369 | foldMap f = foldr ((<+>) . uncurry f) neutral
372 | null : SortedDMap k v -> Bool
374 | null (M _ _) = False
377 | traverse : Applicative f => ({x : k} -> v x -> f (w x)) -> SortedDMap k v -> f (SortedDMap k w)
378 | traverse _ Empty = pure Empty
379 | traverse f (M _ t) = M _ <$> treeTraverse f t
381 | public export %inline
382 | for : Applicative f => SortedDMap k v -> ({x : k} -> v x -> f (w x)) -> f (SortedDMap k w)
383 | for = flip traverse
388 | mergeWith : DecEq k => ({x : k} -> v x -> v x -> v x) -> SortedDMap k v -> SortedDMap k v -> SortedDMap k v
389 | mergeWith f x y = insertFrom inserted x where
390 | inserted : List (x : k ** v x)
392 | (
k ** v)
<- kvList y
393 | let v' = (maybe id f $
lookupPrecise k x) v
399 | merge : DecEq k => ({x : k} -> Semigroup (v x)) => SortedDMap k v -> SortedDMap k v -> SortedDMap k v
400 | merge = mergeWith (<+>)
404 | mergeLeft : DecEq k => SortedDMap k v -> SortedDMap k v -> SortedDMap k v
405 | mergeLeft = mergeWith const
407 | treeLeftMost : Tree n k v o -> (x : k ** v x)
408 | treeLeftMost (Leaf x y) = (
x ** y)
409 | treeLeftMost (Branch2 x _ _) = treeLeftMost x
410 | treeLeftMost (Branch3 x _ _ _ _) = treeLeftMost x
412 | treeRightMost : Tree n k v o -> (x : k ** v x)
413 | treeRightMost (Leaf x y) = (
x ** y)
414 | treeRightMost (Branch2 _ _ x) = treeRightMost x
415 | treeRightMost (Branch3 _ _ _ _ x) = treeRightMost x
417 | treeLookupBetween : Ord k => k -> Tree n k v o -> (Maybe (x : k ** v x), Maybe (x : k ** v x))
418 | treeLookupBetween k (Leaf k' v) with (k < k')
419 | treeLookupBetween k (Leaf k' v) | True = (Nothing, Just (
k' ** v)
)
420 | treeLookupBetween k (Leaf k' v) | False = (Just (
k' ** v)
, Nothing)
421 | treeLookupBetween k (Branch2 t1 k' t2) with (k < k')
422 | treeLookupBetween k (Branch2 t1 k' t2) | True =
423 | let (lower, upper) = treeLookupBetween k t1 in
424 | (lower, upper <|> pure (treeLeftMost t2))
425 | treeLookupBetween k (Branch2 t1 k' t2) | False =
426 | let (lower, upper) = treeLookupBetween k t2 in
427 | (lower <|> pure (treeRightMost t1), upper)
428 | treeLookupBetween k (Branch3 t1 k1 t2 k2 t3) with (k < k1)
429 | treeLookupBetween k (Branch3 t1 k1 t2 k2 t3) | True = treeLookupBetween k (Branch2 t1 k1 t2)
430 | treeLookupBetween k (Branch3 t1 k1 t2 k2 t3) | False with (k < k2)
431 | treeLookupBetween k (Branch3 t1 k1 t2 k2 t3) | False | False = treeLookupBetween k (Branch2 t2 k2 t3)
432 | treeLookupBetween k (Branch3 t1 k1 t2 k2 t3) | False | True =
433 | let (lower, upper) = treeLookupBetween k (Branch2 t1 k1 t2) in
434 | (lower, upper <|> pure (treeLeftMost t3))
440 | lookupBetween : k -> SortedDMap k v -> (Maybe (x : k ** v x), Maybe (x : k ** v x))
441 | lookupBetween k Empty = (Nothing, Nothing)
442 | lookupBetween k (M _ t) = treeLookupBetween k t
447 | leftMost : SortedDMap k v -> Maybe (x : k ** v x)
448 | leftMost Empty = Nothing
449 | leftMost (M _ t) = Just $
treeLeftMost t
454 | rightMost : SortedDMap k v -> Maybe (x : k ** v x)
455 | rightMost Empty = Nothing
456 | rightMost (M _ t) = Just $
treeRightMost t
460 | pop : SortedDMap k v -> Maybe ((x : k ** v x), SortedDMap k v)
462 | kv@(
k ** _)
<- leftMost m
463 | pure (kv, delete k m)
466 | (Show k, {x : k} -> Show (v x)) => Show (SortedDMap k v) where
467 | show m = "fromList " ++ (show $
kvList m)
470 | (DecEq k, {x : k} -> Eq (v x)) => Eq (SortedDMap k v) where
471 | (==) = (==) `on` kvList
474 | strictSubmap : DecEq k => ({x : k} -> Eq (v x)) => (sub : SortedDMap k v) -> (sup : SortedDMap k v) -> Bool
475 | strictSubmap sub sup = all (\(
k ** v)
=> Just v == lookupPrecise k sup) $
kvList sub
483 | DecEq k => ({x : k} -> Semigroup (v x)) => Semigroup (SortedDMap k v) where
489 | DecEq k => Ord k => ({x : k} -> Semigroup (v x)) => Monoid (SortedDMap k v) where