summaryrefslogtreecommitdiff
path: root/TrieMap/TrieAlgebraic.hs
blob: b2893aa589d627c7bfde555212691c9dd3a6dd0f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
{-# LANGUAGE TypeFamilies, FlexibleInstances, TypeOperators, MultiParamTypeClasses, FunctionalDependencies, UndecidableInstances, PatternGuards, IncoherentInstances, TypeOperators #-}
module TrieMap.TrieAlgebraic where

import Control.Arrow ((***))
import Data.Traversable
import Data.Foldable
import Data.Either
import Data.Sequence (Seq)
import Data.Maybe
import Data.Monoid
import Data.IntMap (IntMap)
import Data.Map (Map)
import qualified Data.Sequence as Seq
import qualified Data.IntMap as IMap
import qualified Data.Map as Map

import Control.Monad
import Control.Applicative hiding (Alternative(..), Const(..))
import GHC.Exts (build)

import TrieMap.Applicative
-- import TrieMap.Algebraic (Ordered (..))
import TrieMap.MapTypes
import Prelude hiding (foldr, foldl, all, any)

newtype Ordered k = Ord {unOrd :: k} deriving (Eq, Ord)

instance Show k => Show (Ordered k) where
	show = show . unOrd
	showsPrec x = showsPrec x . unOrd

instance Functor Ordered where
	fmap f (Ord x) = Ord (f x)

type L a = Fix (Const () :+: (Const a :*: Id))

class EqT f => TrieKeyT f t | f -> t, t -> f where
	compareKeyT :: TrieKey k m => f k -> f k -> Ordering
	emptyT :: (Sized a, TrieKey k m) => t k m a
	nullT :: (Sized a, TrieKey k m) => t k m a -> Bool
	guardNullT :: (Sized a, TrieKey k m) => t k m a -> Maybe (t k m a)
	sizeT :: (Sized a, TrieKey k m) => t k m a -> Int
	getSingleT :: (Sized a, TrieKey k m) => t k m a -> Maybe (f k, a)
	alterLookupT :: (Sized a, TrieKey k m) =>
		(Maybe a -> (b, Maybe a)) -> f k -> t k m a -> (b, t k m a)
	lookupT :: (Sized a, TrieKey k m) => f k -> t k m a -> Maybe a
	foldWithKeyT :: (TrieKey k m) => (f k -> a -> b -> b) -> b -> t k m a -> b
	mapAppT :: (Applicative g, Sized a, Sized b, TrieKey k m) =>
		(f k -> a -> g b) -> t k m a -> g (t k m b)
	mapMaybeT :: (Sized a, Sized b, TrieKey k m) =>
		(f k -> a -> Maybe b) -> t k m a -> t k m b
	mapEitherT :: (Sized a, Sized b, Sized c, TrieKey k m) => 
		(f k -> a -> (Maybe b, Maybe c)) -> t k m a -> (t k m b, t k m c)
	unionT :: (Sized a, TrieKey k m) => (f k -> a -> a -> Maybe a) -> t k m a -> t k m a -> t k m a
	intersectT :: (Sized a, Sized b, Sized c, TrieKey k m) =>
		(f k -> a -> b -> Maybe c) -> t k m a -> t k m b -> t k m c
	differenceT :: (Sized a, Sized b, TrieKey k m) => (f k -> a -> b -> Maybe a) -> t k m a -> t k m b -> t k m a
	fromDistAscListT :: (Sized a, TrieKey k m) => [(f k, a)] -> t k m a
	fromAscListT :: (Sized a, TrieKey k m) => (f k -> a -> a -> a) -> [(f k, a)] -> t k m a
	fromListT :: (Sized a, TrieKey k m) => (f k -> a -> a -> a) -> [(f k, a)] -> t k m a
	getMinT :: (Sized a, TrieKey k m) => t k m a -> Maybe ((f k, a), t k m a)
	getMaxT :: (Sized a, TrieKey k m) => t k m a -> Maybe ((f k, a), t k m a)
	updateMinT :: (Sized a, TrieKey k m) => (f k -> a -> (Bool, Maybe a)) -> t k m a -> (Bool, t k m a)
	updateMaxT :: (Sized a, TrieKey k m) => (f k -> a -> (Bool, Maybe a)) -> t k m a -> (Bool, t k m a)
	isSubmapT :: (Sized a, Sized b, TrieKey k m) => (a -> b -> Bool) -> t k m a -> t k m b -> Bool
	splitLookupT :: (Sized a, TrieKey k m) => (a -> (Maybe a, Maybe b, Maybe a)) -> f k -> t k m a -> (t k m a, Maybe b, t k m a)

	guardNullT m
		| nullT m	= Nothing
		| otherwise	= Just m

eqKey :: TrieKey k m => k -> k -> Bool
eqKey a b = compareKey a b == EQ

eqKeyT :: (TrieKey k m, TrieKeyT f t) => f k -> f k -> Bool
eqKeyT a b = compareKeyT a b == EQ

-- | TrieKey defines a bijection between map types and algebraic key types.
class Eq k => TrieKey k m | k -> m, m -> k where
	compareKey :: k -> k -> Ordering
	emptyAlg :: Sized a => m a
	nullAlg :: Sized a => m a -> Bool
	sizeAlg :: Sized a => m a -> Int
	getSingleAlg :: Sized a => m a -> Maybe (k, a)
	guardNullAlg :: Sized a => m a -> Maybe (m a)
-- 	{-# SPECIALIZE alterAlg :: Sized v => (Maybe v -> Id (b, Maybe v)) -> a -> m v -> Id (b, m v) #-}
	alterLookupAlg :: Sized a => (Maybe a -> (b, Maybe a)) -> k -> m a -> (b, m a)
	lookupAlg :: Sized a => k -> m a -> Maybe a
	foldWithKeyAlg :: (k -> a -> b -> b) -> b -> m a -> b
	mapAppAlg :: (Applicative f, Sized a, Sized b) => (k -> a -> f b) -> m a -> f (m b)
	mapMaybeAlg :: (Sized a, Sized b) => (k -> a -> Maybe b) -> m a -> m b
	mapEitherAlg :: (Sized a, Sized b, Sized c) => (k -> a -> (Maybe b, Maybe c)) -> m a -> (m b, m c)
	unionMaybeAlg :: Sized a => (k -> a -> a -> Maybe a) -> m a -> m a -> m a
	intersectAlg :: (Sized a, Sized b, Sized c) => (k -> a -> b -> Maybe c) -> m a -> m b -> m c
	differenceAlg :: (Sized a, Sized b) => (k -> a -> b -> Maybe a) -> m a -> m b -> m a
	fromDistAscListAlg :: Sized a => [(k, a)] -> m a
	fromAscListAlg :: Sized a => (k -> a -> a -> a) -> [(k, a)] -> m a
	fromListAlg :: Sized a => (k -> a -> a -> a) -> [(k, a)] -> m a
	getMinAlg :: Sized a => m a -> Maybe ((k, a), m a)
	getMaxAlg :: Sized a => m a -> Maybe ((k, a), m a)
	updateMinAlg :: Sized a => (k -> a -> (Bool, Maybe a)) -> m a -> (Bool, m a)
	updateMaxAlg :: Sized a => (k -> a -> (Bool, Maybe a)) -> m a -> (Bool, m a)
	valid :: Sized a =>  m a -> Bool
	isSubmapAlg :: (Sized a, Sized b) => (a -> b -> Bool) -> m a -> m b -> Bool
	splitLookupAlg :: (Sized a) => (a -> (Maybe a, Maybe b, Maybe a)) -> k -> m a -> (m a, Maybe b, m a)

	lookupAlg k = fst . alterLookupAlg (\ v -> (v, v)) k
	guardNullAlg m
		| nullAlg m	= Nothing
		| otherwise	= Just m
	getSingleAlg m = do
		((k, v), m') <- getMinAlg m
		guard (nullAlg m')
		return (k, v)
	fromListAlg f = foldr (\ (k, v) -> alterAlg (Just . maybe v (f k v)) k) emptyAlg
	fromAscListAlg _ [] = emptyAlg
	fromAscListAlg f ((k, v):xs) = fromDistAscListAlg (distinct k v xs) where
		distinct k v ((k', v'):xs)
			| k `eqKey` k'	= distinct k (f k v v') xs
			| otherwise	= (k, v):distinct k' v' xs
		distinct k v [] = [(k, v)]
	fromDistAscListAlg = fromListAlg'
	sizeAlg = foldWithKeyAlg (\ _ x n -> n + getSize x) 0

	updateMinAlg f m = maybe (False, m) (\ ((k, v), m') -> maybe m' (\ v' -> insertAlg k v' m) <$> f k v) (getMinAlg m)
	updateMaxAlg f m = maybe (False, m) (\ ((k, v), m') -> maybe m' (\ v' -> insertAlg k v' m) <$> f k v) (getMaxAlg m)
	valid = (`seq` True)

instance TrieKeyT f t => TrieKey (Fix f) (FixMap f t) where
	compareKey (Fix a) (Fix b) = compareKeyT a b
	emptyAlg = FixMap emptyT
	nullAlg (FixMap m) = nullT m
	sizeAlg (FixMap m) = sizeT m
	getSingleAlg (FixMap m) = do
		(k, v) <- getSingleT m
		return (Fix k, v)
	lookupAlg (Fix k) (FixMap m) = lookupT k m
	alterLookupAlg f (Fix k) (FixMap m) = FixMap <$> alterLookupT f k m
	foldWithKeyAlg f z (FixMap m) = foldWithKeyT (f . Fix) z m
	mapAppAlg f (FixMap m) = FixMap <$> mapAppT (f . Fix) m
	mapMaybeAlg f (FixMap m) = FixMap (mapMaybeT (f . Fix) m)
	mapEitherAlg f (FixMap m) = case mapEitherT (f . Fix) m of
		(mL, mR) -> (FixMap mL, FixMap mR)
	unionMaybeAlg f (FixMap m1) (FixMap m2) = FixMap (unionT (f . Fix) m1 m2)
	intersectAlg f (FixMap m1) (FixMap m2) = FixMap (intersectT (f . Fix) m1 m2)
	differenceAlg f (FixMap m1) (FixMap m2) = FixMap (differenceT (f . Fix) m1 m2)
	getMinAlg (FixMap m) = do
		(~(k, v), m') <- getMinT m
		return ((Fix k, v), FixMap m')
	getMaxAlg (FixMap m) = do
		(~(k, v), m') <- getMaxT m
		return ((Fix k, v), FixMap m')
	updateMinAlg f (FixMap m) = FixMap <$> updateMinT (f . Fix) m
	updateMaxAlg f (FixMap m) = FixMap <$> updateMaxT (f . Fix) m
	isSubmapAlg (<=) (FixMap m1) (FixMap m2) = isSubmapT (<=) m1 m2
	splitLookupAlg f (Fix k) (FixMap m) = case splitLookupT f k m of
		(mL, ans, mR) -> (FixMap mL, ans, FixMap mR)

instance (Sized a, TrieKey k m) => Sized (m a) where
	getSize = sizeAlg

instance (Sized a, TrieKey k m, TrieKeyT f t) => Sized (t k m a) where
	getSize = sizeT

fromListAlg' :: (Sized v, TrieKey k m) => [(k, v)] -> m v
fromListAlg' = fromListAlg (const const)

singletonAlg :: (Sized v, TrieKey k m) => k -> v -> m v
singletonAlg k v = insertAlg k v emptyAlg

mapWithKeyAlg :: (Sized v, Sized w, TrieKey k m) => (k -> v -> w) -> m v -> m w
mapWithKeyAlg f m = unId (mapAppAlg (\ k v -> Id (f k v)) m)

mapWithKeyT :: (Sized v, Sized w, TrieKeyT f t, TrieKey k m) => (f k -> v -> w) -> t k m v -> t k m w
mapWithKeyT f m = unId (mapAppT (\ k v -> Id (f k v)) m)

mapAlg :: (Sized v, Sized w, TrieKey k m) => (v -> w) -> m v -> m w
mapAlg = mapWithKeyAlg . const

mapT :: (Sized v, Sized w, TrieKeyT f t, TrieKey k m) => (v -> w) -> t k m v -> t k m w
mapT = mapWithKeyT . const

-- mapMaybeWithKeyAlg :: TrieKey k m => (k -> v -> Maybe w) -> m v -> m w
-- mapMaybeWithKeyAlg f m = unId (mapAppMaybeAlg (\ k v -> Id (f k v)) m)

insertAlg :: (Sized v, TrieKey k m) => k -> v -> m v -> m v
insertAlg k v = alterAlg (const (Just v)) k

insertT :: (Sized v, TrieKey k m, TrieKeyT f t) => f k -> v -> t k m v -> t k m v
insertT k v = alterT (const (Just v)) k

alterAlg :: (Sized v, TrieKey k m) => (Maybe v -> Maybe v) -> k -> m v -> m v
alterAlg f k = snd . alterLookupAlg (\ x -> ((), f x)) k

alterT :: (Sized v, TrieKey k m, TrieKeyT f t) => (Maybe v -> Maybe v) -> f k -> t k m v -> t k m v
alterT f k = snd . alterLookupT (\ x -> ((), f x)) k

-- alterLookupAlg :: TrieKey k m => (Maybe a -> (b, Maybe a)) -> k -> m a -> (b, m a)
-- alterLookupAlg f = unId .: alterAppAlg (Id . f)

foldrAlg :: (Sized a, TrieKey k m) => (a -> b -> b) -> b -> m a -> b
foldrAlg = foldWithKeyAlg . const

unionMaybe :: (a -> a -> Maybe a) -> Maybe a -> Maybe a -> Maybe a
unionMaybe f (Just x) (Just y) = f x y
unionMaybe _ Nothing y = y
unionMaybe _ x Nothing = x

intersectMaybe :: (a -> b -> Maybe c) -> Maybe a -> Maybe b -> Maybe c
intersectMaybe f (Just x) (Just y) = f x y
intersectMaybe _ _ _ = Nothing

differenceMaybe :: (a -> b -> Maybe a) -> Maybe a -> Maybe b -> Maybe a
differenceMaybe _ Nothing _ = Nothing
differenceMaybe _ x Nothing = x
differenceMaybe f (Just x) (Just y) = f x y

filterLeft :: a -> Either b c -> Maybe b
filterLeft _ (Left x) = Just x
filterLeft _ _ = Nothing

filterRight :: a -> Either b c -> Maybe c
filterRight _ (Right x) = Just x
filterRight _ _ = Nothing

{-# INLINE assocsAlg #-}
assocsAlg :: (TrieKey k m) => m a -> [(k, a)]
assocsAlg m = build (\ c n -> foldWithKeyAlg (\ k v xs -> (k,v) `c` xs) n m)

instance (TrieKeyT f1 t1, TrieKeyT f2 t2) => TrieKeyT (f1 :*: f2) (t1 `ProdMap` t2) where
	compareKeyT (a :*: x) (b :*: y) = compareKeyT a b `mappend` compareKeyT x y
	emptyT = PMap emptyT
	nullT (PMap m) = nullT m
	sizeT (PMap m) = sizeT m
	getSingleT (PMap m) = do
		(k1, m') <- getSingleT m
		(k2, v) <- getSingleT m'
		return (k1 :*: k2, v)
	lookupT (k1 :*: k2) (PMap m) = lookupT k1 m >>= lookupT k2
	alterLookupT f (k1 :*: k2) (PMap m) = PMap <$> alterLookupT g k1 m where
		g = fmap guardNullT . alterLookupT f k2 . fromMaybe emptyT
	foldWithKeyT f z (PMap m) = foldWithKeyT (\ k1 -> flip (foldWithKeyT (\ k2 -> f (k1 :*: k2)))) z m
	mapAppT f (PMap m) = PMap <$> mapAppT (\ k1 -> mapAppT (\ k2 -> f (k1 :*: k2))) m
	mapMaybeT f (PMap m) = PMap (mapMaybeT (\ k1 -> guardNullT . mapMaybeT (\ k2 -> f (k1 :*: k2))) m)
	mapEitherT f (PMap m) = (PMap *** PMap) (mapEitherT (\ k1 -> (guardNullT *** guardNullT) . mapEitherT (\ k2 -> f (k1 :*: k2))) m)
	unionT f (PMap m1) (PMap m2) = PMap (unionT (\ k1 -> guardNullT .: unionT (\ k2 -> f (k1 :*: k2))) m1 m2)
	intersectT f (PMap m1) (PMap m2) = PMap (intersectT (\ k1 -> guardNullT .: intersectT (\ k2 -> f (k1 :*: k2))) m1 m2)
	differenceT f (PMap m1) (PMap m2) = PMap (differenceT (\ k1 -> guardNullT .: differenceT (\ k2 -> f (k1 :*: k2))) m1 m2)
	fromListT f xs = PMap $ mapWithKeyT (\ k1 (Elem xs) -> fromListT (\ k2 -> f (k1 :*:  k2)) xs) $
		fromListT (\ _ (Elem x) (Elem y) -> Elem (x ++ y)) [(k1, Elem [(k2, v)]) | ((k1 :*: k2), v) <- xs]
	fromDistAscListT xs = PMap $ fromDistAscListT [(k1, fromDistAscListT ys) | (k1, ys) <- breakFst eqKeyT xs]	
	fromAscListT f xs = PMap $ fromDistAscListT [(k1, fromAscListT (\ k2 -> f (k1 :*: k2)) ys) | (k1, ys) <- breakFst eqKeyT xs]
	getMinT (PMap m) = do
		((k1, m'), m1') <- getMinT m
		((k2, v), m2') <- getMinT m'
		return ((k1 :*: k2, v), PMap (maybe m1' (\ m2' -> insertT k1 m2' m) (guardNullT m2')))
	getMaxT (PMap m) = do
		((k1, m'), m1') <- getMaxT m
		((k2, v), m2') <- getMaxT m'
		return ((k1 :*: k2, v), PMap (maybe m1' (\ m2' -> insertT k1 m2' m) (guardNullT m2')))
	updateMinT f (PMap m) = 
		PMap <$> updateMinT (\ k1 -> guardNullT <.> updateMinT (\ k2 -> f (k1 :*: k2))) m
	updateMaxT f (PMap m) =
		PMap <$> updateMaxT (\ k1 -> guardNullT <.> updateMaxT (\ k2 -> f (k1 :*: k2))) m
	isSubmapT (<=) (PMap m1) (PMap m2) =
		isSubmapT (isSubmapT (<=)) m1 m2
	splitLookupT f (k1 :*: k2) (PMap m) = case splitLookupT g k1 m of
			(mL, ans, mR)	-> (PMap mL, ans, PMap mR)
		where g m' = case splitLookupT f k2 m' of
			(mL, ans, mR)	-> (guardNullT mL, ans, guardNullT mR)

instance (Eq (f1 k), Eq (f2 k), TrieKey k m, TrieKeyT f1 t1, TrieKeyT f2 t2) => TrieKey ((f1 :*: f2) k) (ProdMap t1 t2 k m) where
	compareKey = compareKeyT
	emptyAlg = emptyT
	nullAlg = nullT
	getSingleAlg = getSingleT
	guardNullAlg = guardNullT
	sizeAlg = sizeT
	lookupAlg = lookupT
	alterLookupAlg = alterLookupT
	mapAppAlg = mapAppT
	mapMaybeAlg = mapMaybeT
	mapEitherAlg = mapEitherT
	foldWithKeyAlg = foldWithKeyT
	unionMaybeAlg = unionT
	intersectAlg = intersectT
	differenceAlg = differenceT
	getMinAlg = getMinT
	getMaxAlg = getMaxT
	updateMinAlg = updateMinT
	updateMaxAlg = updateMaxT
	isSubmapAlg = isSubmapT
	splitLookupAlg = splitLookupT

breakFst :: (f1 k -> f1 k -> Bool) -> [((f1 :*: f2) k, v)] -> [(f1 k, [(f2 k, v)])]
breakFst _ [] = []
breakFst eq (((k1 :*: k2), x):xs) = breakFst' k1 (Seq.singleton (k2, x)) xs where
	breakFst' k xs (((k' :*: k2), x):xss)
		| k `eq` k'	= breakFst' k ((Seq.|>) xs (k2, x)) xss
		| otherwise	= (k, toList xs):breakFst' k' (Seq.singleton (k2, x)) xss
	breakFst' k xs [] = [(k, toList xs)]

instance (TrieKeyT f1 t1, TrieKeyT f2 t2) => TrieKeyT (f1 :+: f2) (UnionMap t1 t2) where
	compareKeyT (A a) (A b) = compareKeyT a b
	compareKeyT (B x) (B y) = compareKeyT x y
	compareKeyT A{} B{} = LT
	compareKeyT B{} A{} = GT
	emptyT = UMap emptyT emptyT
	nullT (UMap m1 m2) = nullT m1 && nullT m2
	getSingleT (UMap m1 m2) = case (getSingleT m1, getSingleT m2) of
		(Just (k, v), Nothing)	-> Just (A k, v)
		(Nothing, Just (k, v))	-> Just (B k, v)
		_			-> Nothing
	sizeT (UMap m1 m2) = sizeT m1 + sizeT m2
	lookupT (A k) (UMap m1 _) = lookupT k m1
	lookupT (B k) (UMap _ m2) = lookupT k m2
	alterLookupT f (A k) (UMap m1 m2) = (`UMap` m2) <$> alterLookupT f k m1
	alterLookupT f (B k) (UMap m1 m2) = UMap m1 <$> alterLookupT f k m2
	foldWithKeyT f z (UMap m1 m2) = foldWithKeyT (f . A) (foldWithKeyT (f . B) z m2) m1
	mapAppT f (UMap m1 m2) = UMap <$> mapAppT (f . A) m1 <*> mapAppT (f . B) m2
	mapMaybeT f (UMap m1 m2) = UMap (mapMaybeT (f . A) m1) (mapMaybeT (f . B) m2)
	mapEitherT f (UMap m1 m2) = (UMap m1L m2L, UMap m1R m2R)
		where	(m1L, m1R) = mapEitherT (f . A) m1
			(m2L, m2R) = mapEitherT (f . B) m2
	unionT f (UMap m11 m12) (UMap m21 m22) = 
		UMap (unionT (f . A) m11 m21) (unionT (f . B) m12 m22)
	intersectT f (UMap m11 m12) (UMap m21 m22) =
		UMap (intersectT (f . A) m11 m21) (intersectT (f . B) m12 m22)
	differenceT f (UMap m11 m12) (UMap m21 m22) =
		UMap (differenceT (f . A) m11 m21) (differenceT (f . B) m12 m22)
	getMinT (UMap m1 m2) 
		| Just ~(~(k, v), m1') <- getMinT m1
			= Just ((A k, v), UMap m1' m2)
		| Just ~(~(k, v), m2') <- getMinT m2
			 = Just ((B k, v), UMap m1 m2')
		| otherwise = Nothing
	getMaxT (UMap m1 m2)
		| Just ~(~(k, v), m2') <- getMaxT m2
			= Just ((B k, v), UMap m1 m2')
		| Just ~(~(k, v), m1') <- getMaxT m1
			= Just ((A k, v), UMap m1' m2)
		| otherwise = Nothing
	updateMinT f (UMap m1 m2)
		| nullT m1	= UMap m1 <$> updateMinT (f . B) m2
		| otherwise	= (`UMap` m2) <$> updateMinT (f . A) m1
	updateMaxT f (UMap m1 m2)
		| nullT m2	= (`UMap` m2) <$> updateMaxT (f . A) m1
		| otherwise	= UMap m1 <$> updateMaxT (f . B) m2
	fromDistAscListT xs = UMap (fromDistAscListT ys) (fromDistAscListT zs)
		where	(ys, zs) = partitionEithers' (map pullEither xs)
	fromAscListT f xs = UMap (fromAscListT (f . A) ys) (fromAscListT (f . B) zs) 
		where	(ys, zs) = partitionEithers' (map pullEither xs)
	fromListT f xs = UMap (fromListT (f . A) ys) (fromListT (f . B) zs)
		where	(ys, zs) = partitionEithers' (map pullEither xs)
	isSubmapT (<=) (UMap m11 m12) (UMap m21 m22) = isSubmapT (<=) m11 m21 && isSubmapT (<=) m12 m22
	splitLookupT f (A k) (UMap m1 m2) = case splitLookupT f k m1 of
		(m1L, ans, m1R)	-> (UMap m1L emptyT, ans, UMap m1R m2)
	splitLookupT f (B k) (UMap m1 m2) = case splitLookupT f k m2 of
		(m2L, ans, m2R)	-> (UMap m1 m2L, ans, UMap emptyT m2R)

instance (Eq (f1 k), Eq (f2 k), TrieKey k m, TrieKeyT f1 t1, TrieKeyT f2 t2) => TrieKey ((f1 :+: f2) k) (UnionMap t1 t2 k m) where
	compareKey = compareKeyT
	emptyAlg = emptyT
	nullAlg = nullT
	getSingleAlg = getSingleT
	guardNullAlg = guardNullT
	sizeAlg = sizeT
	lookupAlg = lookupT
	alterLookupAlg = alterLookupT
	mapAppAlg = mapAppT
	mapMaybeAlg = mapMaybeT
	mapEitherAlg = mapEitherT
	foldWithKeyAlg = foldWithKeyT
	unionMaybeAlg = unionT
	intersectAlg = intersectT
	differenceAlg = differenceT
	getMinAlg = getMinT
	getMaxAlg = getMaxT
	updateMinAlg = updateMinT
	updateMaxAlg = updateMaxT
	isSubmapAlg = isSubmapT
	splitLookupAlg = splitLookupT

instance TrieKey k m => TrieKeyT ((,) k) (CProdMap m) where
	compareKeyT (a, x) (b, y) = compareKey a b `mappend` compareKey x y
	emptyT = CPMap emptyAlg
	nullT (CPMap m) = nullAlg m
	getSingleT (CPMap m) = do
		(k1, m') <- getSingleAlg m
		(k2, v) <- getSingleAlg m'
		return ((k1, k2), v)
	guardNullT (CPMap m) = CPMap <$> guardNullAlg m
	sizeT (CPMap m) = sizeAlg m
	lookupT (k1, k2) (CPMap m) = lookupAlg k1 m >>= lookupAlg k2
	alterLookupT f (k1, k2) (CPMap m) = CPMap <$> alterLookupAlg g k1 m where
		g = guardNullAlg <.> alterLookupAlg f k2 . fromMaybe emptyAlg
	foldWithKeyT f z (CPMap m) = foldWithKeyAlg (\ k1 -> flip $ foldWithKeyAlg (\ k2 -> f (k1, k2))) z m
	mapAppT f (CPMap m) = CPMap <$> mapAppAlg (\ k1 -> mapAppAlg (\ k2 -> f (k1, k2))) m
	mapMaybeT f (CPMap m) = CPMap (mapMaybeAlg (\ k1 -> guardNullAlg . mapMaybeAlg (\ k2 -> f (k1, k2))) m)
	mapEitherT f (CPMap m) = (CPMap *** CPMap) (mapEitherAlg (\ k1 -> (guardNullAlg *** guardNullAlg) . mapEitherAlg (\ k2 -> f (k1, k2))) m)
	unionT f (CPMap m1) (CPMap m2) =
		CPMap (unionMaybeAlg (\ k1 -> guardNullAlg .: unionMaybeAlg (\ k2 -> f (k1, k2))) m1 m2)
	intersectT f (CPMap m1) (CPMap m2) =
		CPMap (intersectAlg (\ k1 -> guardNullAlg .: intersectAlg (\ k2 -> f (k1, k2))) m1 m2)
	differenceT f (CPMap m1) (CPMap m2) = 
		CPMap (differenceAlg (\ k1 -> guardNullAlg .: differenceAlg (\ k2 -> f (k1, k2))) m1 m2)
	getMinT (CPMap m) = do
		((k1, m1), m') <- getMinAlg m
		((k2, v), m1') <- getMinAlg m1
		return (((k1, k2), v), CPMap $ maybe m' (\ m1' -> snd $ updateMinAlg (\ _ _ -> (False, Just m1')) m) (guardNullAlg m1'))
	getMaxT (CPMap m) = do
		((k1, m1), m') <- getMaxAlg m
		((k2, v), m1') <- getMaxAlg m1
		return (((k1, k2), v), CPMap $ maybe m' (\ m1' -> snd $ updateMaxAlg (\ _ _ -> (False, Just m1')) m) (guardNullAlg m1'))	
	updateMinT f (CPMap m) = 
		CPMap <$> updateMinAlg (\ k1 -> guardNullAlg <.> updateMinAlg (\ k2 -> f (k1, k2))) m
	updateMaxT f (CPMap m) =
		CPMap <$> updateMaxAlg (\ k1 -> guardNullAlg <.> updateMaxAlg (\ k2 -> f (k1, k2))) m
	isSubmapT (<=) (CPMap m1) (CPMap m2) = isSubmapAlg (isSubmapAlg (<=)) m1 m2
	splitLookupT f (k1, k2) (CPMap m) = case splitLookupAlg g k1 m of
		(mL, ans, mR)	-> (CPMap mL, ans, CPMap mR)
		where g m = case splitLookupAlg f k2 m of
			(mL, ans, mR)	-> (guardNullAlg mL, ans, guardNullAlg mR)
	fromDistAscListT xs = CPMap (fromDistAscListAlg [(k1, fromDistAscListAlg ys) | (k1, ys) <- breakFst' (==) xs])
	fromAscListT f xs = CPMap (fromDistAscListAlg [(k1, fromAscListAlg (\ k2 -> f (k1, k2)) ys) | (k1, ys) <- breakFst' (==) xs])
	fromListT f xs = CPMap (mapWithKeyAlg (\ k1 (Elem ys) -> fromListAlg (\ k2 -> f (k1, k2)) ys) $
		fromListAlg (\ _ (Elem ys) (Elem zs) -> Elem (ys ++ zs)) [(k1, Elem [(k2, v)]) | ((k1, k2), v) <- xs])

breakFst' :: (k1 -> k1 -> Bool) -> [((k1, k2), v)] -> [(k1, [(k2, v)])]
breakFst' _ [] = []
breakFst' eq (((k1, k2), x):xs) = breakFst'' k1 (Seq.singleton (k2, x)) xs where
	breakFst'' k xs (((k', k2), x):xss)
		| k `eq` k'	= breakFst'' k ((Seq.|>) xs (k2, x)) xss
		| otherwise	= (k, toList xs):breakFst'' k' (Seq.singleton (k2, x)) xss
	breakFst'' k xs [] = [(k, toList xs)]

instance (TrieKey k1 m1, TrieKey k2 m2) => TrieKey (k1, k2) (CProdMap m1 k2 m2) where
	compareKey = compareKeyT
	emptyAlg = emptyT
	nullAlg = nullT
	getSingleAlg = getSingleT
	guardNullAlg = guardNullT
	sizeAlg = sizeT
	lookupAlg = lookupT
	alterLookupAlg = alterLookupT
	mapAppAlg = mapAppT
	mapMaybeAlg = mapMaybeT
	mapEitherAlg = mapEitherT
	foldWithKeyAlg = foldWithKeyT
	unionMaybeAlg = unionT
	intersectAlg = intersectT
	differenceAlg = differenceT
	getMinAlg = getMinT
	getMaxAlg = getMaxT
	updateMinAlg = updateMinT
	updateMaxAlg = updateMaxT
	isSubmapAlg = isSubmapT
	splitLookupAlg = splitLookupT

instance TrieKey k m => TrieKeyT (Either k) (CUnionMap m) where
	{-# SPECIALIZE instance TrieKeyT (Either ()) (CUnionMap Maybe) #-}
	compareKeyT (Left a) (Left b) = compareKey a b
	compareKeyT (Right a) (Right b) = compareKey a b
	compareKeyT Left{} Right{} = LT
	compareKeyT Right{} Left{} = GT
	emptyT = CUMap emptyAlg emptyAlg
	nullT (CUMap m1 m2) = nullAlg m1 && nullAlg m2
	sizeT (CUMap m1 m2) = sizeAlg m1 + sizeAlg m2
	getSingleT (CUMap m1 m2) = case (getSingleAlg m1, getSingleAlg m2) of
		(Just (k, v), Nothing)	-> Just (Left k, v)
		(Nothing, Just (k, v))	-> Just (Right k, v)
		_			-> Nothing
	lookupT k (CUMap m1 m2) = either (`lookupAlg` m1) (`lookupAlg` m2) k
	alterLookupT f (Left k) (CUMap m1 m2) = (`CUMap` m2) <$> alterLookupAlg f k m1
	alterLookupT f (Right k) (CUMap m1 m2) = CUMap m1 <$> alterLookupAlg f k m2
	foldWithKeyT f z (CUMap m1 m2) = foldWithKeyAlg (f . Left) (foldWithKeyAlg (f . Right) z m2) m1
	mapAppT f (CUMap m1 m2) = CUMap <$> mapAppAlg (f . Left) m1 <*> mapAppAlg (f . Right) m2
	mapMaybeT f (CUMap m1 m2) = CUMap (mapMaybeAlg (f . Left) m1) (mapMaybeAlg (f . Right) m2)
	mapEitherT f (CUMap m1 m2) = (CUMap m1L m2L, CUMap m1R m2R)
		where	(m1L, m1R) = mapEitherAlg (f . Left) m1
			(m2L, m2R) = mapEitherAlg (f . Right) m2
	unionT f (CUMap m11 m12) (CUMap m21 m22) = 
		CUMap (unionMaybeAlg (f . Left) m11 m21) (unionMaybeAlg (f . Right) m12 m22)
	intersectT f (CUMap m11 m12) (CUMap m21 m22) =
		CUMap (intersectAlg (f . Left) m11 m21) (intersectAlg (f . Right) m12 m22)
	differenceT f (CUMap m11 m12) (CUMap m21 m22) = 
		CUMap (differenceAlg (f . Left) m11 m21) (differenceAlg (f . Right) m12 m22)
	isSubmapT (<=) (CUMap m11 m12) (CUMap m21 m22) = 
		isSubmapAlg (<=) m11 m21 && isSubmapAlg (<=) m12 m22
	splitLookupT f (Left k) (CUMap m1 m2) = case splitLookupAlg f k m1 of
		(m1L, ans, m1R)	-> (CUMap m1L emptyAlg, ans, CUMap m1R m2)
	splitLookupT f (Right k) (CUMap m1 m2) = case splitLookupAlg f k m2 of
		(m2L, ans, m2R)	-> (CUMap m1 m2L, ans, CUMap emptyAlg m2R)
	getMinT (CUMap m1 m2) = case (getMinAlg m1, getMinAlg m2) of
		(Just ((k, v), m1'), _)	-> Just ((Left k, v), CUMap m1' m2)
		(_, Just ((k, v), m2'))	-> Just ((Right k, v), CUMap m1 m2')
		_			-> Nothing
	getMaxT (CUMap m1 m2) = case (getMaxAlg m1, getMaxAlg m2) of
		(_, Just ((k, v), m2'))	-> Just ((Right k, v), CUMap m1 m2')
		(Just ((k, v), m1'), _)	-> Just ((Left k, v), CUMap m1' m2)
		_			-> Nothing
	updateMinT f (CUMap m1 m2)
		| nullAlg m1	= CUMap m1 <$> updateMinAlg (f . Right) m2
		| otherwise	= (`CUMap` m2) <$> updateMinAlg (f . Left) m1
	updateMaxT f (CUMap m1 m2)
		| nullAlg m2	= (`CUMap` m2) <$> updateMaxAlg (f . Left) m1
		| otherwise	= CUMap m1 <$> updateMaxAlg (f . Right) m2
	fromListT f xs = CUMap (fromListAlg (f . Left) ys) (fromListAlg (f . Right) zs)
		where	(ys, zs) = partitionEithers (map pullEither' xs)
	fromAscListT f xs = CUMap (fromAscListAlg (f . Left) ys) (fromAscListAlg (f . Right) zs)
		where	(ys, zs) = partitionEithers (map pullEither' xs)
	fromDistAscListT xs = CUMap (fromDistAscListAlg ys) (fromDistAscListAlg zs) 
		where	(ys, zs) = partitionEithers (map pullEither' xs)

instance (TrieKey k1 m1, TrieKey k2 m2) => TrieKey (Either k1 k2) (CUnionMap m1 k2 m2) where
	{-# SPECIALIZE instance TrieKey k m => TrieKey (Either () k) (CUnionMap Maybe k m) #-}
	compareKey = compareKeyT
	emptyAlg = emptyT
	nullAlg = nullT
	getSingleAlg = getSingleT
	guardNullAlg = guardNullT
	sizeAlg = sizeT
	lookupAlg = lookupT
	alterLookupAlg = alterLookupT
	mapAppAlg = mapAppT
	mapMaybeAlg = mapMaybeT
	mapEitherAlg = mapEitherT
	foldWithKeyAlg = foldWithKeyT
	unionMaybeAlg = unionT
	intersectAlg = intersectT
	differenceAlg = differenceT
	getMinAlg = getMinT
	getMaxAlg = getMaxT
	updateMinAlg = updateMinT
	updateMaxAlg = updateMaxT
	isSubmapAlg = isSubmapT
	splitLookupAlg = splitLookupT

partitionEithers' :: [Either a b] -> ([a], [b])
partitionEithers' = foldr part ([], []) where
	part (Left x) (xs, ys) = (x:xs, ys)
	part (Right y) (xs, ys) = (xs, y:ys)

pullEither :: ((f1 :+: f2) k, v) -> Either (f1 k, v) (f2 k, v)
pullEither (A k, v) = Left (k, v)
pullEither (B k, v) = Right (k, v)

pullEither' :: (Either k1 k2, v) -> Either (k1, v) (k2, v)
pullEither' (Left k, v) = Left (k, v)
pullEither' (Right k, v) = Right (k, v)

instance TrieKey k m => TrieKeyT (Const k) (ConstMap m) where
	compareKeyT (Const a) (Const b) = compareKey a b
	emptyT = ConstMap emptyAlg
	nullT (ConstMap m) = nullAlg m
	sizeT (ConstMap m) = sizeAlg m
	getSingleT (ConstMap m) = do
		(k, v) <- getSingleAlg m
		return (Const k, v)
	lookupT (Const k) (ConstMap m) = lookupAlg k m
	alterLookupT f (Const k) (ConstMap m) = ConstMap <$> alterLookupAlg f k m
	foldWithKeyT f z (ConstMap m) = foldWithKeyAlg (f . Const) z m
	mapAppT f (ConstMap m) = ConstMap <$> mapAppAlg (f . Const) m
	mapMaybeT f (ConstMap m) = ConstMap (mapMaybeAlg (f . Const) m)
	mapEitherT f (ConstMap m) = case mapEitherAlg (f . Const) m of
		(mL, mR) -> (ConstMap mL, ConstMap mR)
	unionT f (ConstMap m1) (ConstMap m2) = ConstMap (unionMaybeAlg (f . Const) m1 m2)
	intersectT f (ConstMap m1) (ConstMap m2) = ConstMap (intersectAlg (f . Const) m1 m2)
	differenceT f (ConstMap m1) (ConstMap m2) = ConstMap (differenceAlg (f . Const) m1 m2)
	fromDistAscListT xs = ConstMap (fromDistAscListAlg [(k, v) | (Const k, v) <- xs])
	fromAscListT f xs = ConstMap (fromAscListAlg (f . Const) [(k, v) | (Const k, v) <- xs])
	fromListT f xs = ConstMap (fromListAlg (f . Const) [(k, v) | (Const k, v) <- xs])
	getMinT (ConstMap m) = do
		(~(k, v), m') <- getMinAlg m
		return ((Const k, v), ConstMap m')
	getMaxT (ConstMap m) = do
		(~(k, v), m') <- getMaxAlg m
		return ((Const k, v), ConstMap m')
	updateMinT f (ConstMap m) = ConstMap <$> updateMinAlg (f . Const) m
	updateMaxT f (ConstMap m) = ConstMap <$> updateMaxAlg (f . Const) m
	isSubmapT (<=) (ConstMap m1) (ConstMap m2) = isSubmapAlg (<=) m1 m2
	splitLookupT f (Const k) (ConstMap m) = case splitLookupAlg f k m of
		(mL, ans, mR) -> (ConstMap mL, ans, ConstMap mR)

instance (TrieKey k m, TrieKey k' m') => TrieKey (Const k k') (ConstMap m k' m') where
	compareKey = compareKeyT
	emptyAlg = emptyT
	nullAlg = nullT
	getSingleAlg = getSingleT
	guardNullAlg = guardNullT
	sizeAlg = sizeT
	lookupAlg = lookupT
	alterLookupAlg = alterLookupT
	mapAppAlg = mapAppT
	mapMaybeAlg = mapMaybeT
	mapEitherAlg = mapEitherT
	foldWithKeyAlg = foldWithKeyT
	unionMaybeAlg = unionT
	intersectAlg = intersectT
	differenceAlg = differenceT
	getMinAlg = getMinT
	getMaxAlg = getMaxT
	updateMinAlg = updateMinT
	updateMaxAlg = updateMaxT
	isSubmapAlg = isSubmapT
	splitLookupAlg = splitLookupT

instance TrieKeyT Id IdMap where
	compareKeyT (Id a) (Id b) = compareKey a b
	emptyT = IdMap emptyAlg
	nullT (IdMap m) = nullAlg m
	sizeT (IdMap m) = sizeAlg m
	getSingleT (IdMap m) = do
		(k, v) <- getSingleAlg m
		return (Id k, v)
	lookupT (Id k) (IdMap m) = lookupAlg k m
	alterLookupT f (Id k) (IdMap m) = IdMap <$> alterLookupAlg f k m
	foldWithKeyT f z (IdMap m) = foldWithKeyAlg (f . Id) z m
	mapAppT f (IdMap m) = IdMap <$> mapAppAlg (f . Id) m
	mapMaybeT f (IdMap m) = IdMap (mapMaybeAlg (f . Id) m)
	mapEitherT f (IdMap m) = case mapEitherAlg (f . Id) m of
		(mL, mR) -> (IdMap mL, IdMap mR)
	unionT f (IdMap m1) (IdMap m2) = IdMap (unionMaybeAlg (f . Id) m1 m2)
	intersectT f (IdMap m1) (IdMap m2) = IdMap (intersectAlg (f . Id) m1 m2)
	differenceT f (IdMap m1) (IdMap m2) = IdMap (differenceAlg (f . Id) m1 m2)
	fromDistAscListT xs = IdMap (fromDistAscListAlg [(k, v) | (Id k, v) <- xs])
	fromAscListT f xs = IdMap (fromAscListAlg (f . Id) [(k, v) | (Id k, v) <- xs])
	fromListT f xs = IdMap (fromListAlg (f . Id) [(k, v) | (Id k, v) <- xs])
	getMinT (IdMap m) = do
		(~(k, v), m') <- getMinAlg m
		return ((Id k, v), IdMap m')
	getMaxT (IdMap m) = do
		(~(k, v), m') <- getMaxAlg m
		return ((Id k, v), IdMap m')
	updateMinT f (IdMap m) = IdMap <$> updateMinAlg (f . Id) m
	updateMaxT f (IdMap m) = IdMap <$> updateMaxAlg (f . Id) m
	isSubmapT (<=) (IdMap m1) (IdMap m2) = isSubmapAlg (<=) m1 m2
	splitLookupT f (Id k) (IdMap m) = case splitLookupAlg f k m of
		(mL, ans, mR) -> (IdMap mL, ans, IdMap mR)

instance TrieKey k m => TrieKey (Id k) (IdMap k m) where
	compareKey = compareKeyT
	emptyAlg = emptyT
	nullAlg = nullT
	getSingleAlg = getSingleT
	guardNullAlg = guardNullT
	sizeAlg = sizeT
	lookupAlg = lookupT
	alterLookupAlg = alterLookupT
	mapAppAlg = mapAppT
	mapMaybeAlg = mapMaybeT
	mapEitherAlg = mapEitherT
	foldWithKeyAlg = foldWithKeyT
	unionMaybeAlg = unionT
	intersectAlg = intersectT
	differenceAlg = differenceT
	getMinAlg = getMinT
	getMaxAlg = getMaxT
	updateMinAlg = updateMinT
	updateMaxAlg = updateMaxT
	isSubmapAlg = isSubmapT
	splitLookupAlg = splitLookupT

-- instance (Sized k, TrieKey k m) => TrieKey (SizeElem k) (SizedMap k m) where
-- 	compareKey (SElem a) (SElem b) = compareKey a b
-- 	emptyAlg = SizedMap emptyAlg
-- 	nullAlg (SizedMap m) = nullAlg m
-- 	sizeAlg (SizedMap m) = sizeAlg m
-- 	getSingleAlg (SizedMap m) = do
-- 		(k, v) <- getSingleAlg m
-- 		return (SElem k, v)
-- 	lookupAlg (SElem k) (SizedMap m) = lookupAlg k m
-- 	alterLookupAlg f (SElem k) (SizedMap m) = SizedMap <$> alterLookupAlg f k m
-- 	foldWithKeyAlg f z (SizedMap m) = foldWithKeyAlg (f . SElem) z m
-- 	mapAppAlg f (SizedMap m) = SizedMap <$> mapAppAlg (f . SElem) m
-- 	mapMaybeAlg f (SizedMap m) = SizedMap (mapMaybeAlg (f . SElem) m)
-- 	mapEitherAlg f (SizedMap m) = case mapEitherAlg (f . SElem) m of
-- 		(mL, mR) -> (SizedMap mL, SizedMap mR)
-- 	unionMaybeAlg f (SizedMap m1) (SizedMap m2) = SizedMap (unionMaybeAlg (f . SElem) m1 m2)
-- 	intersectAlg f (SizedMap m1) (SizedMap m2) = SizedMap (intersectAlg (f . SElem) m1 m2)
-- 	differenceAlg f (SizedMap m1) (SizedMap m2) = SizedMap (differenceAlg (f . SElem) m1 m2)
-- 	fromDistAscListAlg xs = SizedMap (fromDistAscListAlg [(k, v) | (SElem k, v) <- xs])
-- 	fromAscListAlg f xs = SizedMap (fromAscListAlg (f . SElem) [(k, v) | (SElem k, v) <- xs])
-- 	fromListAlg f xs = SizedMap (fromListAlg (f . SElem) [(k, v) | (SElem k, v) <- xs])
-- 	getMinAlg (SizedMap m) = do
-- 		(~(k, v), m') <- getMinAlg m
-- 		return ((SElem k, v), SizedMap m')
-- 	getMaxAlg (SizedMap m) = do
-- 		(~(k, v), m') <- getMaxAlg m
-- 		return ((SElem k, v), SizedMap m')
-- 	updateMinAlg f (SizedMap m) = SizedMap <$> updateMinAlg (f . SElem) m
-- 	updateMaxAlg f (SizedMap m) = SizedMap <$> updateMaxAlg (f . SElem) m
-- 	isSubmapAlg (<=) (SizedMap m1) (SizedMap m2) = isSubmapAlg (<=) m1 m2
-- 	splitLookupAlg f (SElem k) (SizedMap m) = case splitLookupAlg f k m of
-- 		(mL, ans, mR) -> (SizedMap mL, ans, SizedMap mR)

instance TrieKey Int IntMap where
	compareKey = compare
	emptyAlg = IMap.empty
	nullAlg = IMap.null
	getSingleAlg m
		| IMap.size m == 1, [(k, v)] <- IMap.toList m
			= Just (k, v)
	getSingleAlg _ = Nothing
	lookupAlg = IMap.lookup
	alterLookupAlg f k m = fmap (\ v' -> IMap.alter (const v') k m) (f x)
		where x = IMap.lookup k m
	foldWithKeyAlg = IMap.foldWithKey
	mapAppAlg = sequenceA .: IMap.mapWithKey
	mapMaybeAlg = IMap.mapMaybeWithKey
	mapEitherAlg f m = (IMap.mapMaybeWithKey (fst .: f) m, IMap.mapMaybeWithKey (snd .: f) m)
	unionMaybeAlg f m1 m2 = IMap.mapMaybe (either Just id) (IMap.unionWithKey g (fmap Left m1) (fmap Left m2)) where
		g k (Left v1) (Left v2) = Right (f k v1 v2)
		g k (Right v) _ = Right v
		g k _ (Right v) = Right v
	intersectAlg f m1 m2 = IMap.mapMaybe (either (const Nothing) Just) $ IMap.intersectionWithKey g (fmap Left m1) m2 where
		g k (Left x) = maybe (Left x) Right . f k x
		g _ (Right x) = const (Right x)
	differenceAlg = IMap.differenceWithKey
	fromListAlg = IMap.fromListWithKey
	fromAscListAlg = IMap.fromAscListWithKey
	fromDistAscListAlg = IMap.fromDistinctAscList
	getMinAlg = IMap.minViewWithKey
	getMaxAlg = IMap.maxViewWithKey
	updateMinAlg f m = case IMap.minViewWithKey m of
		Just ((k, v), m')	-> let (ans, v') = f k v in (ans, maybe m' (\ v' -> IMap.updateMin (const v') m) v')
		_			-> (False, m)
	updateMaxAlg f m = case IMap.maxViewWithKey m of
		Just ((k, v), m')	-> let (ans, v') = f k v in (ans, maybe m' (\ v' -> IMap.updateMax (const v') m) v')
		_			-> (False, m)
	isSubmapAlg = IMap.isSubmapOfBy
	splitLookupAlg f k m = case IMap.splitLookup k m of
		(mL, Nothing, mR)	-> (mL, Nothing, mR)
		(mL, Just v, mR) -> case f v of
			(vL, ans, vR)	-> (maybe mL (flip (IMap.insert k) mL) vL, ans, maybe mR (flip (IMap.insert k) mR) vR)

instance Ord k => TrieKey (Ordered k) (Map k) where
	compareKey = compare
	emptyAlg = Map.empty
	nullAlg = Map.null
-- 	sizeAlg = foldl' (\ n x -> n + getSize x) 0
	getSingleAlg m
		| Map.size m == 1, (k, v) <- Map.findMin m
			= Just (Ord k, v)
	lookupAlg = Map.lookup . unOrd
	alterLookupAlg f (Ord k) m = fmap (\ v -> Map.alter (const v) k m) (f x)
		where x = Map.lookup k m
	foldWithKeyAlg f = Map.foldWithKey (f . Ord)
	mapAppAlg f = sequenceA . Map.mapWithKey (f . Ord)
 	mapMaybeAlg f = Map.mapMaybeWithKey (f . Ord)
	mapEitherAlg f m = (Map.mapMaybeWithKey (fst .: f . Ord) m, Map.mapMaybeWithKey (snd .: f . Ord) m)
	unionMaybeAlg f m1 m2 = Map.mapMaybe (either Just id) (Map.unionWithKey g (fmap Left m1) (fmap Left m2)) where
		g k (Left v1) (Left v2) = Right (f (Ord k) v1 v2)
		g k (Right v) _ = Right v
		g k _ (Right v) = Right v
	intersectAlg f = Map.mapMaybe id .: Map.intersectionWithKey (f . Ord)
	differenceAlg f = Map.differenceWithKey (f . Ord)
	fromListAlg f xs = Map.fromListWithKey (f . Ord) [(k, v) | (Ord k, v) <- xs]
	fromAscListAlg f xs = Map.fromAscListWithKey (f . Ord) [(k, v) | (Ord k, v) <- xs]
	fromDistAscListAlg xs = Map.fromDistinctAscList [(k, v) | (Ord k, v) <- xs]
	getMinAlg m = do	(~(k, v), m') <- Map.minViewWithKey m
				return ((Ord k, v), m')
	getMaxAlg m = do	(~(k, v), m') <- Map.maxViewWithKey m
				return ((Ord k, v), m')
	updateMinAlg f m
		| Map.null m	= (False, m)
		| otherwise	= case Map.findMin m of
			(k, v)	-> let (ans, v') = f (Ord k) v in (ans, Map.updateMin (const v') m)
	updateMaxAlg f m
		| Map.null m	= (False, m)
		| otherwise	= case Map.findMin m of
			(k, v)	-> let (ans, v') = f (Ord k) v in (ans, Map.updateMax (const v') m)
	isSubmapAlg = Map.isSubmapOfBy
	splitLookupAlg f (Ord k) m = case Map.splitLookup k m of
		(mL, Nothing, mR)	-> (mL, Nothing, mR)
		(mL, Just v, mR) -> case f v of
			(vL, ans, vR) -> (maybe mL (flip (Map.insert k) mL) vL, ans, maybe mR (flip (Map.insert k) mR) vR) 

instance TrieKey () Maybe where
	compareKey _ _ = EQ
	emptyAlg = Nothing
	nullAlg = isNothing
	sizeAlg = maybe 0 getSize
	getSingleAlg = fmap ((,) ())
	lookupAlg _ = id
	alterLookupAlg f _ = f
	foldWithKeyAlg f = foldr (f ())
	mapAppAlg f = traverse (f ())
	mapMaybeAlg f = (>>= f ())
	mapEitherAlg f = maybe (Nothing, Nothing) (f ())
	unionMaybeAlg f = unionMaybe (f ())
	intersectAlg f = intersectMaybe (f ())
	differenceAlg f = differenceMaybe (f ())
	fromListAlg _ [] = Nothing
	fromListAlg f ((_, v):xs) = Just (foldr (f () . snd) v xs)
	fromAscListAlg = fromListAlg
	fromDistAscListAlg = fmap snd . listToMaybe
	getMinAlg = fmap g where
		g v = (((), v), Nothing)
	getMaxAlg = fmap g where
		g v = (((), v), Nothing)
	updateMinAlg f = maybe (False, Nothing) (f ())
	updateMaxAlg f = maybe (False, Nothing) (f ())
	isSubmapAlg _ Nothing _ = True
	isSubmapAlg _ _ Nothing = False
	isSubmapAlg (<=) (Just x) (Just y) = x <= y
	splitLookupAlg f _ = maybe (Nothing, Nothing, Nothing) f

{-# RULES
	"sizeAlg/Map/Elem" forall (m :: Map k (Elem v)) . sizeAlg m = Map.size m;
	"sizeAlg/IMap/Elem" forall (m :: IntMap (Elem v)) . sizeAlg m = IMap.size m;
	#-}

instance (TrieKeyT f t, TrieKey k m) => TrieKey (App f k) (App (t k m)) where
	compareKey (App a) (App b) = compareKeyT a b
	emptyAlg = App emptyT
	nullAlg (App m) = nullT m
	getSingleAlg (App m) = do
		(k, v) <- getSingleT m
		return (App k, v)
	alterLookupAlg f (App k) (App m) = App <$> alterLookupT f k m
	foldWithKeyAlg f z (App m) = foldWithKeyT (f . App) z m
	mapAppAlg f (App m) = App <$> mapAppT (f . App) m
	mapMaybeAlg f (App m) = App (mapMaybeT (f . App) m)
	mapEitherAlg f (App m) = (App *** App) (mapEitherT (f . App) m)
	fromListAlg f xs = App (fromListT (f . App) [(k, v) | (App k, v) <- xs])
	fromAscListAlg f xs = App (fromAscListT (f . App) [(k, v) | (App k, v) <- xs])
	fromDistAscListAlg xs = App (fromDistAscListT [(k, v) | (App k, v) <- xs])
	unionMaybeAlg f (App m1) (App m2) = App (unionT (f . App) m1 m2)
	intersectAlg f (App m1) (App m2) = App (intersectT (f . App) m1 m2)
	differenceAlg f (App m1) (App m2) = App (differenceT (f . App) m1 m2)
	getMinAlg (App m) = do
		((k, v), m') <- getMinT m
		return ((App k, v), App m')
	getMaxAlg (App m) = do
		((k, v), m') <- getMaxT m
		return ((App k, v), App m')
	updateMinAlg f (App m) = App <$> updateMinT (f . App) m
	updateMaxAlg f (App m) = App <$> updateMaxT (f . App) m
	isSubmapAlg (<=) (App m1) (App m2) = isSubmapT (<=) m1 m2
	splitLookupAlg f (App k) (App m) = case splitLookupT f k m of
		(mL, ans, mR)	-> (App mL, ans, App mR)

instance (TrieKeyT f1 t1, TrieKeyT f2 t2) => TrieKeyT (f1 `O` f2) (CompMap t1 f2 t2) where
	compareKeyT (O a) (O b) = compareKeyT a b
	emptyT = CompMap emptyT
	nullT (CompMap m) = nullT m
	guardNullT (CompMap m) = CompMap <$> guardNullT m
	sizeT (CompMap m) = sizeT m
	getSingleT (CompMap m) = do
		(k, v) <- getSingleT m
		return (O k, v)
	lookupT (O k) (CompMap m) = lookupT k m
	alterLookupT f (O k) (CompMap m) = CompMap <$> alterLookupT f k m
	foldWithKeyT f z (CompMap m) = foldWithKeyT (f . O) z m
	mapAppT f (CompMap m) = CompMap <$> mapAppT (f . O) m
	mapMaybeT f (CompMap m) = CompMap (mapMaybeT (f . O) m)
	mapEitherT f (CompMap m) = (CompMap *** CompMap) (mapEitherT (f . O) m)
	unionT f (CompMap m1) (CompMap m2) = CompMap (unionT (f . O) m1 m2)
	intersectT f (CompMap m1) (CompMap m2) = CompMap (intersectT (f . O) m1 m2)
	differenceT f (CompMap m1) (CompMap m2) = CompMap (differenceT (f . O) m1 m2)
	fromDistAscListT xs = CompMap (fromDistAscListT [(k, v) | (O k, v) <- xs])
	fromAscListT f xs = CompMap (fromAscListT (f . O) [(k, v) | (O k, v) <- xs])
	fromListT f xs = CompMap (fromListT (f . O) [(k, v) | (O k, v) <- xs])
	getMinT (CompMap m) = do
		((k, v), m') <- getMinT m
		return ((O k, v), CompMap m')
	getMaxT (CompMap m) = do
		((k, v), m') <- getMaxT m
		return ((O k, v), CompMap m')
	updateMinT f (CompMap m) = CompMap <$> updateMinT (f . O) m
	updateMaxT f (CompMap m) = CompMap <$> updateMaxT (f . O) m
	isSubmapT (<=) (CompMap m1) (CompMap m2) = isSubmapT (<=) m1 m2
	splitLookupT f (O k) (CompMap m) = case splitLookupT f k m of
		(mL, ans, mR)	-> (CompMap mL, ans, CompMap mR)

instance (TrieKey k m, TrieKeyT f1 t1, TrieKeyT f2 t2) => TrieKey ((f1 `O` f2) k) (CompMap t1 f2 t2 k m) where
	compareKey = compareKeyT
	emptyAlg = emptyT
	nullAlg = nullT
	getSingleAlg = getSingleT
	guardNullAlg = guardNullT
	sizeAlg = sizeT
	lookupAlg = lookupT
	alterLookupAlg = alterLookupT
	mapAppAlg = mapAppT
	mapMaybeAlg = mapMaybeT
	mapEitherAlg = mapEitherT
	foldWithKeyAlg = foldWithKeyT
	unionMaybeAlg = unionT
	intersectAlg = intersectT
	differenceAlg = differenceT
	getMinAlg = getMinT
	getMaxAlg = getMaxT
	updateMinAlg = updateMinT
	updateMaxAlg = updateMaxT
	isSubmapAlg = isSubmapT
	splitLookupAlg = splitLookupT