summaryrefslogtreecommitdiff
path: root/TrieMap/Reflection.hs
blob: a07c552fe96f16cce0f1338ba3308b86e0b8dba0 (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
{-# LANGUAGE TypeOperators, TypeFamilies, FlexibleContexts, UndecidableInstances #-}

module TrieMap.Reflection where

-- import TrieMap.Fixpoint
import TrieMap.MapTypes
import TrieMap.TrieAlgebraic
import TrieMap.Algebraic
import TrieMap.Applicative
import TrieMap.RadixTrie()
import qualified TrieMap.TrieAlgebraic as TA

instance Algebraic v => Algebraic (Elem v) where
	type AlgRep (Elem v) = AlgRep v
	toAlg (Elem v) = toAlg v
	fromAlg v = Elem (fromAlg v)

-- instance Algebraic (t1 k (m2 v)) => Algebraic (ProdMap m1 m2 v) where
-- 	type AlgRep (ProdMap m1 m2 v) = AlgRep (m1 (m2 v))
-- 	toAlg (PMap m) = toAlg m
-- 	fromAlg = PMap . fromAlg
-- 
-- instance (Ord k, Algebraic k, Sized v, Algebraic v, TrieKey k m) => Algebraic (RadixTrie k m v) where
-- 	type AlgRep (RadixTrie k m v) = AlgRep [([k], v)]
-- 	toAlg m = toAlg (build (\ c n -> foldWithKeyAlg (curry c) n m))
-- 	fromAlg = fromDistAscListAlg . fromAlg

instance (Algebraic k, TrieKey k m) => SAlgebraicT (RadixTrie k m) where
	type SAlgRepT (RadixTrie k m) = AlgRepT ([] `O` ((,) [k]))
	toSAlgT = toAlgT . o . assocsAlg
	fromSAlgT = fromDistAscListAlg . unO . fromAlgT

-- instance (AlgebraicT m, Algebraic k) => SAlgebraicT (Edge k m) where
-- 	type SAlgRepT (Edge k m) = AlgRepT (O Fix (O ((:*:) (Const Int :*: Co{--}nst [k] :*: AlgRepT m)) (O Const Maybe)))


instance (AlgebraicT m, Algebraic k,  Algebraic a) => Algebraic (Edge k m a) where
	type AlgRep (Edge k m a) = Fix (AlgRepT (Const (Int, [k], Maybe a)) :*: AlgRepT m)
	toAlg (Edge s ks v ts) = Fix (toAlgT (Const (s, ks, v)) :*: fmap toAlg (toAlgT ts))
	fromAlg (Fix (a :*: b)) = case (fromAlgT a, fmap fromAlg b) of
		(Const (s, ks, v), ts) ->
			Edge s ks v (fromAlgT ts)

instance (AlgebraicT m, Algebraic k, Algebraic a) => Algebraic (RadixTrie k m a) where
	type AlgRep (RadixTrie k m a) = AlgRep (Maybe (Edge k m a))
	toAlg (Radix e) = toAlg e
	fromAlg = Radix . fromAlg