4 | import Control.Monad.State
7 | import public Data.DFunctor
8 | import public Data.DFoldable
9 | import public Data.DSum
10 | import public Data.DOrd
11 | import public Data.DEq
12 | import public Data.Some
17 | error msg = assert_total $
idris_crash msg
22 | ShowS = String -> String
25 | showString : String -> ShowS
48 | data DMap : (k : a -> Type) -> (f : a -> Type) -> Type where
53 | -> (left : DMap k f)
54 | -> (right : DMap k f)
78 | singleton : {0 v : a} -> k v -> f v -> DMap k f
79 | singleton k x = Bin 1 k x Tip Tip
87 | null : DMap k f -> Bool
89 | null (Bin _ _ _ _ _) = False
93 | size : DMap k f -> Int
95 | size (Bin n _ _ _ _) = n
102 | lookup : (impl : DOrd k) => k v -> DMap k f -> Maybe (f v)
103 | lookup k Tip = Nothing
104 | lookup k (Bin _ kx x l r) = case dcompare k kx @{impl} of
110 | lookupAssoc : (impl : DOrd k) => Some k -> DMap k f -> Maybe (DSum k f)
111 | lookupAssoc sk = withSome sk $
\key =>
113 | go : DMap k f -> Maybe (DSum k f)
115 | go (Bin _ kx x l r) =
116 | case dcompare key kx @{impl} of
119 | DEQ => Just (kx :=> x)
160 | bin : {0 v : a} -> k v -> f v -> DMap k f -> DMap k f -> DMap k f
162 | = Bin (size l + size r + 1) k x l r
168 | singleL, singleR : {0 v : a} -> k v -> f v -> DMap k f -> DMap k f -> DMap k f
169 | singleL k1 x1 t1 (Bin _ k2 x2 t2 t3) = bin k2 x2 (bin k1 x1 t1 t2) t3
170 | singleL _ _ _ Tip = error "error: singleL Tip"
171 | singleR k1 x1 (Bin _ k2 x2 t1 t2) t3 = bin k2 x2 t1 (bin k1 x1 t2 t3)
172 | singleR _ _ Tip _ = error "error: singleR Tip"
175 | doubleL, doubleR : {0 v : a} -> k v -> f v -> DMap k f -> DMap k f -> DMap k f
176 | doubleL k1 x1 t1 (Bin _ k2 x2 (Bin _ k3 x3 t2 t3) t4) = bin k3 x3 (bin k1 x1 t1 t2) (bin k2 x2 t3 t4)
177 | doubleL _ _ _ _ = error "error: doubleL"
178 | doubleR k1 x1 (Bin _ k2 x2 t1 (Bin _ k3 x3 t2 t3)) t4 = bin k3 x3 (bin k2 x2 t1 t2) (bin k1 x1 t3 t4)
179 | doubleR _ _ _ _ = error "error: doubleR"
184 | rotateL : {0 v : a} -> k v -> f v -> DMap k f -> DMap k f -> DMap k f
185 | rotateL k x l r@(Bin _ _ _ ly ry)
186 | = if size ly < ratio*size ry then singleL k x l r
187 | else doubleL k x l r
188 | rotateL _ _ _ Tip = error "error: rotateL Tip"
191 | rotateR : {0 v : a} -> k v -> f v -> DMap k f -> DMap k f -> DMap k f
192 | rotateR k x l@(Bin _ _ _ ly ry) r
193 | = if size ry < ratio*size ly then singleR k x l r
194 | else doubleR k x l r
195 | rotateR _ _ Tip _ = error "error: rotateR Tip"
229 | balance : {0 v : a} -> k v -> f v -> DMap k f -> DMap k f -> DMap k f
231 | = if sizeL + sizeR <= 1 then Bin sizeX k x l r
232 | else if sizeR >= delta*sizeL then rotateL k x l r
233 | else if sizeL >= delta*sizeR then rotateR k x l r
234 | else Bin sizeX k x l r
237 | sizeL, sizeR, sizeX : Int
240 | sizeX = sizeL + sizeR + 1
249 | insertMax, insertMin : {0 v : a} -> k v -> f v -> DMap k f -> DMap k f
252 | Tip => singleton kx x
254 | => balance ky y l (insertMax kx x r)
258 | Tip => singleton kx x
260 | => balance ky y (insertMin kx x l) r
263 | combine : DOrd k => k v -> f v -> DMap k f -> DMap k f -> DMap k f
264 | combine kx x Tip r = insertMin kx x r
265 | combine kx x l Tip = insertMax kx x l
266 | combine kx x l@(Bin sizeL ky y ly ry) r@(Bin sizeR kz z lz rz)
267 | = if delta*sizeL <= sizeR then balance kz z (combine kx x l lz) rz
268 | else if delta*sizeR <= sizeL then balance ky y ly (combine kx x ry r)
274 | minViewWithKey : DMap k f -> Maybe (DSum k f, DMap k f)
275 | minViewWithKey Tip = Nothing
276 | minViewWithKey (Bin _ k0 x0 l0 r0) = Just $
go k0 x0 l0 r0
278 | go : {0 k, f : a -> Type} -> k v -> f v -> DMap k f -> DMap k f -> (DSum k f, DMap k f)
279 | go k x Tip r = (k :=> x, r)
280 | go k x (Bin _ kl xl ll lr) r =
281 | let (km, l') = go kl xl ll lr
282 | in (km, balance k x l' r)
287 | maxViewWithKey : DMap k f -> Maybe (DSum k f, DMap k f)
288 | maxViewWithKey Tip = Nothing
289 | maxViewWithKey (Bin _ k0 x0 l0 r0) = Just $
go k0 x0 l0 r0
291 | go : k v -> f v -> DMap k f -> DMap k f -> (DSum k f, DMap k f)
292 | go k x l Tip = (k :=> x, l)
293 | go k x l (Bin _ kr xr rl rr) =
294 | let (km, r') = go kr xr rl rr
295 | in (km, balance k x l r')
305 | deleteFindMax : DMap k f -> (DSum k f, DMap k f)
308 | Bin _ k x l Tip => (k :=> x,l)
309 | Bin _ k x l r => let (km,r') = deleteFindMax r in (km,balance k x l r')
310 | Tip => (error "DMap.deleteFindMax: can not return the maximal element of an empty map", Tip)
320 | deleteFindMin : DMap k f -> (DSum k f, DMap k f)
321 | deleteFindMin t = case minViewWithKey t of
322 | Nothing => (error "DMap.deleteFindMin: can not return the minimal element of an empty map", Tip)
330 | glue : DMap k f -> DMap k f -> DMap k f
334 | if size l > size r then case deleteFindMax l of (km :=> m, l') => balance km m l' r
335 | else case deleteFindMin r of (km :=> m, r') => balance km m l r'
341 | merge : DMap k f -> DMap k f -> DMap k f
344 | merge l@(Bin sizeL kx x lx rx) r@(Bin sizeR ky y ly ry)
345 | = if delta*sizeL <= sizeR then balance ky y (merge l ly) ry
346 | else if delta*sizeR <= sizeL then balance kx x lx (merge rx r)
373 | trim : (Some k -> Ordering) -> (Some k -> Ordering) -> DMap k f -> DMap k f
375 | trim cmplo cmphi t@(Bin _ kx _ l r)
376 | = case cmplo (MkSome kx) of
377 | LT => case cmphi (MkSome kx) of
379 | _ => trim cmplo cmphi l
380 | _ => trim cmplo cmphi r
383 | trimLookupLo : DOrd k => Some k -> (Some k -> Ordering) -> DMap k f -> (Maybe (DSum k f), DMap k f)
384 | trimLookupLo _ _ Tip = (Nothing,Tip)
385 | trimLookupLo lo cmphi t@(Bin _ kx x l r)
386 | = case compare lo (MkSome kx) of
387 | LT => case cmphi (MkSome kx) of
388 | GT => (lookupAssoc lo t, t)
389 | _ => trimLookupLo lo cmphi l
390 | GT => trimLookupLo lo cmphi r
391 | EQ => (Just (kx :=> x), trim (compare lo) cmphi r)
398 | filterGt : DOrd k => (Some k -> Ordering) -> DMap k f -> DMap k f
401 | go : DMap k f -> DMap k f
403 | go (Bin _ kx x l r) = case cmp (MkSome kx) of
404 | LT => combine kx x (go l) r
409 | filterLt : DOrd k => (Some k -> Ordering) -> DMap k f -> DMap k f
412 | go : DMap k f -> DMap k f
414 | go (Bin _ kx x l r) = case cmp (MkSome kx) of
416 | GT => combine kx x l (go r)
431 | member : DOrd k => k a -> DMap k f -> Bool
432 | member k = isJust . lookup k
436 | notMember : DOrd k => k v -> DMap k f -> Bool
437 | notMember k m = not (member k m)
443 | find : DOrd k => k v -> DMap k f -> f v
444 | find k m = case lookup k m of
445 | Nothing => error "DMap.find: element not in the map"
452 | findWithDefault : DOrd k => f v -> k v -> DMap k f -> f v
453 | findWithDefault def k m = case lookup k m of
466 | insert : (impl : DOrd k) => k v -> f v -> DMap k f -> DMap k f
467 | insert kx x = evalState False . go
469 | go : DMap k f -> State Bool (DMap k f)
470 | go Tip = put True >> pure (singleton kx x)
471 | go t@(Bin sz ky y l r) = case dcompare kx ky @{impl} of
476 | if sizeChange then pure (balance ky y l' r)
477 | else pure (Bin sz ky y l' r)
482 | if sizeChange then pure (balance ky y l r')
483 | else pure (Bin sz ky y l r')
484 | DEQ => pure (Bin sz kx x l r)
490 | insertR : (impl : DOrd k) => k v -> f v -> DMap k f -> DMap k f
493 | go : DMap k f -> DMap k f
494 | go Tip = singleton kx x
495 | go (Bin sz ky y l r) = case dcompare kx ky @{impl} of
496 | DLT => let l' = go l
498 | in if size l' == size l
499 | then Bin sz ky y l' r
500 | else balance ky y l' r
501 | DGT => let r' = go r
503 | in if size r' == size r
504 | then Bin sz ky y l r'
505 | else balance ky y l r'
506 | DEQ => Bin sz ky y l r
515 | insertWithKey : (impl : DOrd k) => (k v -> f v -> f v -> f v) -> k v -> f v -> DMap k f -> DMap k f
516 | insertWithKey func kx x = go
518 | go : DMap k f -> DMap k f
519 | go Tip = singleton kx x
520 | go (Bin sy ky y l r) =
521 | case dcompare kx ky @{impl} of
522 | DLT => balance ky y (go l) r
523 | DGT => balance ky y l (go r)
524 | DEQ => Bin sy kx (func kx x y) l r
532 | insertWith : DOrd k => (f v -> f v -> f v) -> k v -> f v -> DMap k f -> DMap k f
533 | insertWith f = insertWithKey (\_ => \x' => \y' => f x' y')
539 | insertLookupWithKey : (impl : DOrd k)
540 | => (k v -> f v -> f v -> f v)
544 | -> (Maybe (f v), DMap k f)
545 | insertLookupWithKey func kx x = go
547 | go : DMap k f -> (Maybe (f v), DMap k f)
548 | go Tip = (Nothing, singleton kx x)
549 | go (Bin sy ky y l r) =
550 | case dcompare kx ky @{impl} of
551 | DLT => let (found, l') = go l
552 | in (found, balance ky y l' r)
553 | DGT => let (found, r') = go r
554 | in (found, balance ky y l r')
555 | DEQ => (Just y, Bin sy kx (func kx x y) l r)
566 | delete : (impl : DOrd k) => k v -> DMap k f -> DMap k f
569 | go : DMap k f -> DMap k f
571 | go (Bin _ kx x l r) =
572 | case dcompare k' kx @{impl} of
573 | DLT => balance kx x (go l) r
574 | DGT => balance kx x l (go r)
579 | adjustWithKey : (impl : DOrd k) => (k v -> f v -> f v) -> k v -> DMap k f -> DMap k f
580 | adjustWithKey f0 k0 = go f0 k0
582 | go : (k v -> f v -> f v) -> k v -> DMap k f -> DMap k f
584 | go f k (Bin sx kx x l r) =
585 | case dcompare k kx @{impl} of
586 | DLT => Bin sx kx x (go f k l) r
587 | DGT => Bin sx kx x l (go f k r)
588 | DEQ => Bin sx kx (f kx x) l r
593 | adjust : DOrd k => (f v -> f v) -> k v -> DMap k f -> DMap k f
594 | adjust f = adjustWithKey (\_ => \x => f x)
601 | updateWithKey : (impl : DOrd k) => (k v -> f v -> Maybe (f v)) -> k v -> DMap k f -> DMap k f
602 | updateWithKey func key = go
604 | go : DMap k f -> DMap k f
606 | go (Bin sx kx x l r) =
607 | case dcompare key kx @{impl} of
608 | DLT => balance kx x (go l) r
609 | DGT => balance kx x l (go r)
610 | DEQ => case func kx x of
611 | Just x' => Bin sx kx x' l r
612 | Nothing => glue l r
618 | update : DOrd k => (f v -> Maybe (f v)) -> k v -> DMap k f -> DMap k f
619 | update f = updateWithKey (\_ => \x => f x)
625 | updateLookupWithKey : (impl : DOrd k) => (k v -> f v -> Maybe (f v)) -> k v -> DMap k f -> (Maybe (f v), DMap k f)
626 | updateLookupWithKey func key = go
628 | go : DMap k f -> (Maybe (f v), DMap k f)
629 | go Tip = (Nothing,Tip)
630 | go (Bin sx kx x l r) =
631 | case dcompare key kx @{impl} of
632 | DLT => let (found,l') = go l in (found,balance kx x l' r)
633 | DGT => let (found,r') = go r in (found,balance kx x l r')
634 | DEQ => case func kx x of
635 | Just x' => (Just x',Bin sx kx x' l r)
636 | Nothing => (Just x,glue l r)
642 | alter : (impl : DOrd k) => (Maybe (f v) -> Maybe (f v)) -> k v -> DMap k f -> DMap k f
643 | alter func key = go
645 | go : DMap k f -> DMap k f
646 | go Tip = case func Nothing of
648 | Just x => singleton key x
650 | go (Bin sx kx x l r) = case dcompare key kx @{impl} of
651 | DLT => balance kx x (go l) r
652 | DGT => balance kx x l (go r)
653 | DEQ => case func (Just x) of
654 | Just x' => Bin sx kx x' l r
655 | Nothing => glue l r
660 | alterF : (impl : DOrd k) => Functor f => k v -> (Maybe (g v) -> f (Maybe (g v))) -> DMap k g -> f (DMap k g)
661 | alterF key func = go
663 | go : DMap k g -> f (DMap k g)
664 | go Tip = maybe Tip (singleton key) <$> func Nothing
666 | go (Bin sx kx x l r) = case dcompare key kx @{impl} of
667 | DLT => (\l' => balance kx x l' r) <$> go l
668 | DGT => (\r' => balance kx x l r') <$> go r
669 | DEQ => maybe (glue l r) (\x' => Bin sx kx x' l r) <$> func (Just x)
678 | lookupIndex : (impl : DOrd k) => k v -> DMap k f -> Maybe Int
679 | lookupIndex key = go 0
681 | go : Int -> DMap k f -> Maybe Int
682 | go idx Tip = Nothing
683 | go idx (Bin _ kx _ l r)
684 | = case dcompare key kx @{impl} of
686 | DGT => go (idx + size l + 1) r
687 | DEQ => Just (idx + size l)
693 | findIndex : DOrd k => k v -> DMap k f -> Int
695 | = case lookupIndex k t of
696 | Nothing => error "DMap.findIndex: element is not in the map"
702 | elemAt : Int -> DMap k f -> DSum k f
703 | elemAt _ Tip = error "DMap.elemAt: index out of range"
704 | elemAt i (Bin _ kx x l r)
705 | = case compare i sizeL of
707 | GT => elemAt (i - sizeL - 1) r
716 | updateAt : ({0 v : a} -> k v -> f v -> Maybe (f v)) -> Int -> DMap k f -> DMap k f
717 | updateAt func i0 t = go i0 t
719 | go : Int -> DMap k f -> DMap k f
721 | go i (Bin sx kx x l r) = case compare i sizeL of
722 | LT => balance kx x (go i l) r
723 | GT => balance kx x l (go (i-sizeL-1) r)
724 | EQ => case func kx x of
725 | Just x' => Bin sx kx x' l r
726 | Nothing => glue l r
734 | deleteAt : Int -> DMap k f -> DMap k f
736 | = updateAt (\_ => \_ => Nothing) i m
743 | lookupMin : DMap k f -> Maybe (DSum k f)
744 | lookupMin m = case m of
746 | Bin _ kx x l _ => Just $
go kx x l
748 | go : k v -> f v -> DMap k f -> DSum k f
749 | go kx x Tip = kx :=> x
750 | go _ _ (Bin _ kx x l _) = go kx x l
754 | findMin : DMap k f -> DSum k f
755 | findMin m = case lookupMin m of
757 | Nothing => error "DMap.findMin: empty map has no minimal element"
760 | lookupMax : DMap k f -> Maybe (DSum k f)
761 | lookupMax m = case m of
763 | Bin _ kx x _ r => Just $
go kx x r
765 | go : k v -> f v -> DMap k f -> DSum k f
766 | go kx x Tip = kx :=> x
767 | go _ _ (Bin _ kx x _ r) = go kx x r
771 | findMax : DMap k f -> DSum k f
772 | findMax m = case lookupMax m of
774 | Nothing => error "DMap.findMax: empty map has no maximal element"
778 | deleteMin : DMap k f -> DMap k f
779 | deleteMin (Bin _ _ _ Tip r) = r
780 | deleteMin (Bin _ kx x l r) = balance kx x (deleteMin l) r
781 | deleteMin Tip = Tip
785 | deleteMax : DMap k f -> DMap k f
786 | deleteMax (Bin _ _ _ l Tip) = l
787 | deleteMax (Bin _ kx x l r) = balance kx x l (deleteMax r)
788 | deleteMax Tip = Tip
792 | updateMinWithKey : ({0 v : a} -> k v -> f v -> Maybe (f v)) -> DMap k f -> DMap k f
793 | updateMinWithKey func = go
795 | go : DMap k f -> DMap k f
796 | go (Bin sx kx x Tip r) = case func kx x of
798 | Just x' => Bin sx kx x' Tip r
799 | go (Bin _ kx x l r) = balance kx x (go l) r
804 | updateMaxWithKey : ({0 v : a} -> k v -> f v -> Maybe (f v)) -> DMap k f -> DMap k f
805 | updateMaxWithKey func = go
807 | go : DMap k f -> DMap k f
808 | go (Bin sx kx x l Tip) = case func kx x of
810 | Just x' => Bin sx kx x' l Tip
811 | go (Bin _ kx x l r) = balance kx x l (go r)
822 | split : (impl : DOrd k) => k v -> DMap k f -> (DMap k f, DMap k f)
825 | go : DMap k f -> (DMap k f, DMap k f)
826 | go Tip = (Tip, Tip)
827 | go (Bin _ kx x l r) = case dcompare key kx @{impl} of
828 | DLT => let (lt, gt) = go l in (lt, combine kx x gt r)
829 | DGT => let (lt, gt) = go r in (combine kx x l lt, gt)
835 | splitLookup : (impl : DOrd k) => k v -> DMap k f -> (DMap k f, Maybe (f v), DMap k f)
836 | splitLookup key = go
838 | go : DMap k f -> (DMap k f, Maybe (f v), DMap k f)
839 | go Tip = (Tip, Nothing, Tip)
840 | go (Bin _ kx x l r) = case dcompare key kx @{impl} of
841 | DLT => let (lt, z, gt) = go l in (lt, z, combine kx x gt r)
842 | DGT => let (lt, z, gt) = go r in (combine kx x l lt, z, gt)
843 | DEQ => (l, Just x, r)
848 | splitMember : (impl : DOrd k) => k v -> DMap k f -> (DMap k f, Bool, DMap k f)
849 | splitMember key = go
851 | go : DMap k f -> (DMap k f, Bool, DMap k f)
852 | go Tip = (Tip, False, Tip)
853 | go (Bin _ kx x l r) = case dcompare key kx @{impl} of
854 | DLT => let (lt, z, gt) = go l in (lt, z, combine kx x gt r)
855 | DGT => let (lt, z, gt) = go r in (combine kx x l lt, z, gt)
856 | DEQ => (l, True, r)
860 | splitLookupWithKey : (impl : DOrd k) => k v -> DMap k f -> (DMap k f, Maybe (k v, f v), DMap k f)
861 | splitLookupWithKey key = go
863 | go : DMap k f -> (DMap k f, Maybe (k v, f v), DMap k f)
864 | go Tip = (Tip, Nothing, Tip)
865 | go (Bin _ kx x l r) = case dcompare key kx @{impl} of
866 | DLT => let (lt, z, gt) = go l in (lt, z, combine kx x gt r)
867 | DGT => let (lt, z, gt) = go r in (combine kx x l lt, z, gt)
868 | DEQ => (l, Just (kx, x), r)
879 | union : DOrd k => DMap k f -> DMap k f -> DMap k f
881 | union t1 (Bin _ kx x Tip Tip) = insertR kx x t1
883 | union (Bin _ kx x Tip Tip) t2 = insert kx x t2
884 | union t1@(Bin _ k1 x1 l1 r1) t2 = case split k1 t2 of
885 | (l2, r2) => combine k1 x1 (l1 `union` l2) (r1 `union` r2)
890 | unions : DOrd k => List (DMap k f) -> DMap k f
891 | unions ts = foldl union empty ts
898 | implementation (DOrd k) => Semigroup (DMap k f) where
902 | implementation (DOrd k) => Monoid (DMap k f) where
912 | unionWithKey : DOrd k => ({0 v : a} -> k v -> f v -> f v -> f v) -> DMap k f -> DMap k f -> DMap k f
913 | unionWithKey _ t1 Tip = t1
914 | unionWithKey _ Tip t2 = t2
915 | unionWithKey f (Bin _ k1 x1 l1 r1) t2 = case splitLookup k1 t2 of
916 | (l2, mx2, r2) => let
917 | l1l2 = unionWithKey f l1 l2
918 | r1r2 = unionWithKey f r1 r2
920 | Nothing => combine k1 x1 l1l2 r1r2
921 | Just x2 => combine k1 (f k1 x1 x2) l1l2 r1r2
926 | unionsWithKey : DOrd k => ({0 v : a} -> k v -> f v -> f v -> f v) -> List (DMap k f) -> DMap k f
927 | unionsWithKey f ts = foldl (unionWithKey f) empty ts
936 | difference : DOrd k => DMap k f -> DMap k g -> DMap k f
937 | difference Tip _ = Tip
938 | difference t1 Tip = t1
939 | difference t1 (Bin _ k2 x2 l2 r2) = case split k2 t1 of
941 | l1l2 = l1 `difference` l2
942 | r1r2 = r1 `difference` r2
943 | in if size t1 == size l1l2 + size r1r2 then t1
944 | else merge l1l2 r1r2
951 | differenceWithKey : (impl : DOrd k)
952 | => ({0 v : a} -> k v -> f v -> g v -> Maybe (f v))
956 | differenceWithKey _ Tip _ = Tip
957 | differenceWithKey _ t1 Tip = t1
958 | differenceWithKey f (Bin _ k1 x1 l1 r1) t2 = case splitLookup k1 t2 of
959 | (l2, mx2, r2) => let
960 | l1l2 = differenceWithKey f l1 l2
961 | r1r2 = differenceWithKey f r1 r2
963 | Nothing => combine k1 x1 l1l2 r1r2
964 | Just x2 => case f k1 x1 x2 of
965 | Nothing => merge l1l2 r1r2
966 | Just x1x2 => combine k1 x1x2 l1l2 r1r2
977 | intersection : DOrd k => DMap k f -> DMap k f -> DMap k f
978 | intersection Tip _ = Tip
979 | intersection _ Tip = Tip
980 | intersection t1@(Bin s1 k1 x1 l1 r1) t2 =
981 | let (l2, found, r2) = splitMember k1 t2
982 | l1l2 = intersection l1 l2
983 | r1r2 = intersection r1 r2
985 | then if size l1l2 == size l1 && size r1r2 == size r1
987 | else combine k1 x1 l1l2 r1r2
988 | else merge l1l2 r1r2
993 | intersectionWithKey : DOrd k => ({0 v : a} -> k v -> f v -> g v -> h v) -> DMap k f -> DMap k g -> DMap k h
994 | intersectionWithKey _ Tip _ = Tip
995 | intersectionWithKey _ _ Tip = Tip
996 | intersectionWithKey f (Bin s1 k1 x1 l1 r1) t2 =
997 | let (l2, found, r2) = splitLookup k1 t2
998 | l1l2 = intersectionWithKey f l1 l2
999 | r1r2 = intersectionWithKey f r1 r2
1001 | Nothing => merge l1l2 r1r2
1002 | Just x2 => combine k1 (f k1 x1 x2) l1l2 r1r2
1008 | submap' : DOrd k => ({0 v : a} -> k v -> k v -> f v -> g v -> Bool) -> DMap k f -> DMap k g -> Bool
1010 | submap' _ _ Tip = False
1011 | submap' f (Bin _ kx x l r) t
1013 | (lt, found, gt) = splitLookupWithKey kx t
1016 | Just (ky, y) => f kx ky x y && submap' f l lt && submap' f r gt
1023 | isSubmapOfBy : DOrd k => ({0 v : a} -> k v -> k v -> f v -> g v -> Bool) -> DMap k f -> DMap k g -> Bool
1024 | isSubmapOfBy f t1 t2 = (size t1 <= size t2) && (submap' f t1 t2)
1044 | isProperSubmapOfBy : DOrd k => ({0 v : a} -> k v -> k v -> f v -> g v -> Bool) -> DMap k f -> DMap k g -> Bool
1045 | isProperSubmapOfBy f t1 t2 = (size t1 < size t2) && (submap' f t1 t2)
1064 | filterWithKey : DOrd k => ({0 v : a} -> k v -> f v -> Bool) -> DMap k f -> DMap k f
1067 | go : DMap k f -> DMap k f
1073 | in if p kx x then if size l' == size l && size r' == size r
1075 | else combine kx x l' r'
1082 | partitionWithKey : (impl : DOrd k) => ({0 v : a} -> k v -> f v -> Bool) -> DMap k f -> (DMap k f, DMap k f)
1083 | partitionWithKey p0 m0 = go p0 m0
1085 | go : ({0 v : a} -> k v -> f v -> Bool) -> DMap k f -> (DMap k f, DMap k f)
1087 | go p (Bin _ kx x l r) = let
1090 | in if p kx x then (combine kx x l1 r1, merge l2 r2)
1091 | else (merge l1 r1, combine kx x l2 r2)
1094 | mapMaybeWithKey : (impl : DOrd k) => ({0 v : a} -> k v -> f v -> Maybe (g v)) -> DMap k f -> DMap k g
1095 | mapMaybeWithKey func = go
1097 | go : DMap k f -> DMap k g
1099 | go (Bin _ kx x l r) = case func kx x of
1100 | Just y => combine kx y (go l) (go r)
1101 | Nothing => merge (go l) (go r)
1105 | mapMaybe : DOrd k => ({0 v : a} -> f v -> Maybe (g v)) -> DMap k f -> DMap k g
1106 | mapMaybe f = mapMaybeWithKey (const f)
1110 | mapEitherWithKey : DOrd k =>
1111 | ({0 v : a} -> k v -> f v -> Either (g v) (h v)) -> DMap k f -> (DMap k g, DMap k h)
1112 | mapEitherWithKey f0 = go f0
1114 | go : ({0 v : a} -> k v -> f v -> Either (g v) (h v))
1116 | -> (DMap k g, DMap k h)
1118 | go f (Bin _ kx x l r) = let
1119 | (l1, l2) = mapEitherWithKey f l
1120 | (r1, r2) = mapEitherWithKey f r
1122 | Left y => (combine kx y l1 r1, merge l2 r2)
1123 | Right z => (merge l1 r1, combine kx z l2 r2)
1132 | foldrWithKey : ({0 v : a} -> k v -> f v -> b -> b) -> b -> DMap k f -> b
1135 | go : b -> DMap k f -> b
1137 | go z (Bin _ kx x l r) = go (func kx x (go z r)) l
1142 | foldlWithKey : ({0 v : a} -> b -> k v -> f v -> b) -> b -> DMap k f -> b
1145 | go : b -> DMap k f -> b
1147 | go z (Bin _ kx x l r) = go (func (go z l) kx x) r
1150 | implementation DFoldable (DMap k) where
1151 | dfoldr f = foldrWithKey (\k => f)
1162 | fromList : DOrd k => List (DSum k f) -> DMap k f
1166 | ins : DMap k f -> DSum k f -> DMap k f
1167 | ins t (k :=> x) = insert k x t
1171 | fromListWithKey : DOrd k => ({0 v : a} -> k v -> f v -> f v -> f v) -> List (DSum k f) -> DMap k f
1172 | fromListWithKey func xs
1173 | = foldl (ins func) empty xs
1175 | ins : ({0 v : a} -> k v -> f v -> f v -> f v) -> DMap k f -> DSum k f -> DMap k f
1176 | ins func t (k :=> x) = insertWithKey func k x t
1180 | toAscList : DMap k f -> List (DSum k f)
1181 | toAscList t = foldrWithKey (\k => \x => \xs => (k :=> x) :: xs) [] t
1185 | toList : DMap k f -> List (DSum k f)
1190 | toDescList : DMap k f -> List (DSum k f)
1191 | toDescList t = foldlWithKey (\xs => \k => \x => (k :=> x) :: xs) [] t
1204 | fromDistinctAscList : List (DSum k f) -> DMap k f
1205 | fromDistinctAscList xs = build const (cast $
length xs) xs
1210 | buildB : DMap k f -> k v -> f v -> (DMap k f -> a -> b) -> DMap k f -> a -> b
1211 | buildB l k x c r zs = c (bin k x l r) zs
1214 | build : (DMap k f -> List (DSum k f) -> b) -> Int -> List (DSum k f) -> b
1215 | build c 0 xs' = c Tip xs'
1216 | build c 5 xs' = case xs' of
1217 | ((k1 :=> x1) :: (k2 :=> x2) :: (k3 :=> x3) :: (k4 :=> x4) :: (k5 :=> x5) :: xx)
1218 | => c (bin k4 x4 (bin k2 x2 (singleton k1 x1) (singleton k3 x3)) (singleton k5 x5)) xx
1219 | _ => error "fromDistinctAscList build"
1220 | build c n xs' = build (buildR nr c) nl xs'
1226 | buildR : Int -> (DMap k f -> List (DSum k f) -> b) -> DMap k f -> List (DSum k f) -> b
1227 | buildR n c l ((k :=> x) :: ys) = build (buildB l k x c) n ys
1228 | buildR _ _ _ [] = error "fromDistinctAscList buildR []"
1236 | fromAscListWithKey : (impl : DEq k) => ({0 v : a} -> k v -> f v -> f v -> f v) -> List (DSum k f) -> DMap k f
1237 | fromAscListWithKey func xs = fromDistinctAscList (combineEq func xs)
1239 | combineEq' : ({0 v : a} -> k v -> f v -> f v -> f v) -> DSum k f -> List (DSum k f) -> List (DSum k f)
1240 | combineEq' f z [] = [z]
1241 | combineEq' f z@(kz :=> zz) (x@(kx :=> xx) :: xs') = case deq kx kz @{impl} of
1242 | Just Refl => let yy = func kx xx zz in combineEq' f (kx :=> yy) xs'
1243 | Nothing => z :: combineEq' func x xs'
1246 | combineEq : ({0 v : a} -> k v -> f v -> f v -> f v) -> List (DSum k f) -> List (DSum k f)
1247 | combineEq _ xs' = case xs' of
1250 | (x :: xx) => combineEq' func x xx
1256 | fromAscList : DEq k => List (DSum k f) -> DMap k f
1257 | fromAscList xs = fromAscListWithKey (\_ => \x => \_ => x) xs
1266 | implementation DEq k => DEq f => Eq (DMap k f) where
1267 | t1 == t2 = (size t1 == size t2) && (toAscList t1 == toAscList t2)
1276 | assocs : DMap k f -> List (DSum k f)
1286 | keys : DMap k f -> List (Some k)
1287 | keys m = [MkSome k | (k :=> _) <- assocs m]
1295 | map : ({0 v : a} -> f v -> g v) -> DMap k f -> DMap k g
1298 | go : DMap k f -> DMap k g
1300 | go (Bin sx kx x l r) = Bin sx kx (func x) (go l) (go r)
1303 | implementation DFunctor (DMap k) where
1310 | ffor : DMap k f -> ({0 v : a} -> f v -> g v) -> DMap k g
1315 | mapWithKey : ({0 v : a} -> k v -> f v -> g v) -> DMap k f -> DMap k g
1318 | go : DMap k f -> DMap k g
1320 | go (Bin sx kx x l r) = Bin sx kx (func kx x) (go l) (go r)
1326 | fforWithKey : DMap k f -> ({0 v : a} -> k v -> f v -> g v) -> DMap k g
1327 | fforWithKey m f = mapWithKey f m
1334 | traverseWithKey_ : Applicative t => ({0 v : a} -> k v -> f v -> t ()) -> DMap k f -> t ()
1335 | traverseWithKey_ func = go
1339 | go (Bin 1 k v _ _) = func k v
1340 | go (Bin s k v l r) = go l *> func k v *> go r
1346 | forWithKey_ : Applicative t => DMap k f -> ({0 v : a} -> k v -> f v -> t ()) -> t ()
1347 | forWithKey_ m f = traverseWithKey_ f m
1354 | traverseWithKey : Applicative t => ({0 v : a} -> k v -> f v -> t (g v)) -> DMap k f -> t (DMap k g)
1355 | traverseWithKey func = go
1357 | go : DMap k f -> t (DMap k g)
1359 | go (Bin 1 k v _ _) = (\v' => Bin 1 k v' Tip Tip) <$> func k v
1360 | go (Bin s k v l r) = flip (Bin s k) <$> go l <*> func k v <*> go r
1366 | forWithKey : Applicative t => DMap k f -> ({0 v : a} -> k v -> f v -> t (g v)) -> t (DMap k g)
1367 | forWithKey m f = traverseWithKey f m
1372 | mapAccumLWithKey : ({0 v : a'} -> a -> k v -> f v -> (a, g v)) -> a -> DMap k f -> (a, DMap k g)
1373 | mapAccumLWithKey func = go
1375 | go : a -> DMap k f -> (a, DMap k g)
1377 | go a (Bin sx kx x l r) =
1379 | (a2, x') = func a1 kx x
1381 | in (a3, Bin sx kx x' l' r')
1386 | mapAccumRWithKey : ({0 v : a'} -> a -> k v -> f v -> (a, g v)) -> a -> DMap k f -> (a, DMap k g)
1387 | mapAccumRWithKey func = go
1389 | go : a -> DMap k f -> (a, DMap k g)
1391 | go a (Bin sx kx x l r) =
1393 | (a2, x') = func a1 kx x
1395 | in (a3, Bin sx kx x' l' r')
1405 | => ({0 v : a} -> k2 v -> f v -> f v -> f v)
1406 | -> ({0 v : a} -> k1 v -> k2 v) -> DMap k1 f -> DMap k2 f
1407 | mapKeysWith c func = fromListWithKey c . Prelude.map fFirst . toList
1409 | fFirst : DSum k1 f -> DSum k2 f
1410 | fFirst (x :=> y) = (func x :=> y)
1428 | mapKeysMonotonic : ({0 v : a} -> k1 v -> k2 v) -> DMap k1 f -> DMap k2 f
1429 | mapKeysMonotonic _ Tip = Tip
1430 | mapKeysMonotonic f (Bin sz k x l r) =
1431 | Bin sz (f k) x (mapKeysMonotonic f l) (mapKeysMonotonic f r)
1438 | implementation DOrd k => DOrd f => Ord (DMap k f) where
1439 | compare m1 m2 = compare (toAscList m1) (toAscList m2)
1459 | implementation DShow k => DShow f => Show (DMap k f) where
1460 | show m = "fromList " ++ show (toList m)
1463 | showWide : Bool -> List (String) -> String -> String
1465 | = if wide then showString (concat (reverse bars)) . showString "|\n"
1473 | showsBars : List (String) -> ShowS
1477 | (bar :: bars) => showString (concat (reverse bars)) . showString node
1480 | withBar, withEmpty : List (String) -> List (String)
1481 | withBar bars = "| " :: bars
1482 | withEmpty bars = " " :: bars
1485 | showsTreeHang : ({0 v : a} -> k v -> f v -> String) -> Bool -> List (String) -> DMap k f -> ShowS
1486 | showsTreeHang showelem wide bars t
1488 | Tip => showsBars bars . showString "|\n"
1490 | => showsBars bars . showString (showelem kx x) . showString "\n"
1492 | => showsBars bars . showString (showelem kx x) . showString "\n" .
1494 | showsTreeHang showelem wide (withBar bars) l .
1496 | showsTreeHang showelem wide (withEmpty bars) r
1499 | showsTree : ({0 v : a} -> k v -> f v -> String) -> Bool -> List (String) -> List (String) -> DMap k f -> ShowS
1500 | showsTree showelem wide lbars rbars t
1502 | Tip => showsBars lbars . showString "|\n"
1504 | => showsBars lbars . showString (showelem kx x) . showString "\n"
1506 | => showsTree showelem wide (withBar rbars) (withEmpty rbars) r .
1508 | showsBars lbars . showString (showelem kx x) . showString "\n" .
1510 | showsTree showelem wide (withEmpty lbars) (withBar lbars) l
1517 | showTreeWith : ({0 v : a} -> k v -> f v -> String) -> Bool -> Bool -> DMap k f -> String
1518 | showTreeWith showelem hang wide t
1519 | = if hang then (showsTreeHang showelem wide [] t) ""
1520 | else (showsTree showelem wide [] [] t) ""
1525 | showTree : DShow k => DShow f => DMap k f -> String
1527 | = showTreeWith showElem True False m
1529 | showElem : k v -> f v -> String
1530 | showElem kx fx = show (kx :=> fx) {ty = DSum k f}
1537 | ordered : DOrd k => DMap k f -> Bool
1539 | = bounded (const True) (const True) t
1541 | bounded : (Some k -> Bool) -> (Some k -> Bool) -> DMap k f -> Bool
1545 | Bin _ kx _ l r => lo (MkSome kx)
1547 | && bounded lo (\s => (<) s (MkSome kx)) l
1548 | && bounded (\s => (>) s (MkSome kx)) hi r
1552 | balanced : DMap k f -> Bool
1556 | Bin _ _ _ l r => (size l + size r <= 1 || (size l <= delta*size r && size r <= delta*size l)) &&
1557 | balanced l && balanced r
1560 | validsize : DMap k f -> Bool
1562 | = (realsize t == Just (size t))
1564 | realsize : DMap k f -> Maybe Int
1568 | Bin sz _ _ l r => case (realsize l,realsize r) of
1569 | (Just n, Just m) => if n + m + 1 == sz then Just sz
1575 | valid : DOrd k => DMap k f -> Bool
1576 | valid t = balanced t && ordered t && validsize t
1584 | genDMap : DOrd k => Hedgehog.Range Nat -> Gen (DSum k v) -> Gen (DMap k v)
1585 | genDMap range gen = fromList <$> list range gen