-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathKeyM.hs
50 lines (35 loc) · 1.12 KB
/
KeyM.hs
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
{-# LANGUAGE Rank2Types,GeneralizedNewtypeDeriving #-}
module KeyM(module Data.Type.Equality, KeyM, Key, newKey, runKeyM) where
import Unsafe.Coerce
import Data.Type.Equality
import Control.Applicative
import Control.Monad
data TreePath = Start | Leftc TreePath | Rightc TreePath deriving (Eq)
type Name = TreePath
type NameSupply = TreePath
newNameSupply :: NameSupply
newNameSupply = Start
split :: NameSupply -> (NameSupply, NameSupply)
split s = (Leftc s, Rightc s)
supplyName :: NameSupply -> Name
supplyName = id
instance TestEquality (Key s) where
testEquality (Key i) (Key j)
| i == j = Just (unsafeCoerce Refl)
| otherwise = Nothing
data KeyM s a = KeyM { getKeyM :: NameSupply -> a }
data Key s a = Key Name
newKey :: KeyM s (Key s a)
newKey = KeyM $ \s -> Key (supplyName s)
instance Functor (KeyM s) where
fmap = liftM
instance Applicative (KeyM s) where
pure = return
(<*>) = ap
instance Monad (KeyM s) where
return x = KeyM $ \_ -> x
m >>= f = KeyM $ \s ->
let (sl,sr) = split s
in getKeyM (f (getKeyM m sl)) sr
runKeyM :: (forall s. KeyM s a) -> a
runKeyM (KeyM f) = f newNameSupply