-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstateMonadFromScratch.hs
More file actions
45 lines (33 loc) · 881 Bytes
/
stateMonadFromScratch.hs
File metadata and controls
45 lines (33 loc) · 881 Bytes
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
data Tree a = Leaf a
| Node (Tree a) (Tree a)
deriving Show
tree :: Tree Char
tree = Node (Node (Leaf 'a') (Leaf 'b')) (Leaf 'c')
type State = Int
data ST a = S (State -> (a, State))
apply :: ST a -> State -> (a, State)
apply (S f) x = f x
instance Monad ST where
return x = S (\s -> (x, s))
st >>= f = S (\s ->
let (x, s') = apply st s
in apply (f x) s')
fresh :: ST Int
fresh = S (\n -> (n, n+1))
app :: (State -> State) -> ST State
app f = S (\n -> (n, f n))
fresh' = app (+1)
run :: ST a -> State -> a
run x v = fst $ apply x v
label' t = run (mlabel t) 0
mlabel :: Tree a -> ST (Tree (a, Int))
mlabel (Leaf l) = do
n <- fresh'
return (Leaf (l, n))
mlabel (Node left right) = do
l <- mlabel left
r <- mlabel right
return (Node l r)
label :: Tree a -> Tree (a, Int)
label t = fst $ apply (mlabel t) 0
test = label' tree