summaryrefslogtreecommitdiff
path: root/TrieMap/Reflection.hs
blob: 1118c6dbe6368585db2e305fac82ea9951d9ca41 (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
{-# LANGUAGE 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 Alg (Elem v) = Alg v
	toAlg (Elem v) = toAlg v
	fromAlg v = Elem (fromAlg v)

instance Algebraic (m1 (m2 v)) => Algebraic (ProdMap m1 m2 v) where
	type Alg (ProdMap m1 m2 v) = Alg (m1 (m2 v))
	toAlg (PMap m) = toAlg m
	fromAlg = PMap . fromAlg

instance (Algebraic (m1 v), Algebraic (m2 v)) => Algebraic (UnionMap m1 m2 v) where
	type Alg (UnionMap m1 m2 v) = (Alg (m1 v), Alg (m2 v))
	toAlg (m1 :+: m2) = (toAlg m1, toAlg m2)
	fromAlg (m1, m2) = fromAlg m1 :+: fromAlg m2

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