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
|
module ErrorState where
infixl 1 >+, >+=
infixl 4 <$>, <*>
type ES e s a = s -> Either e (a, s)
evalES :: ES e s a -> s -> Either e a
evalES m s = case m s of
Left e -> Left e
Right (x, _) -> Right x
returnES :: a -> ES e s a
returnES x s = Right (x, s)
failES :: e -> ES e s a
failES e _ = Left e
(>+=) :: ES e s a -> (a -> ES e s b) -> ES e s b
m >+= f = \s -> case m s of
Left e -> Left e
Right (x, s') -> f x s'
(>+) :: ES e s a -> ES e s b -> ES e s b
m >+ n = m >+= \_ -> n
(<$>) :: (a -> b) -> ES e s a -> ES e s b
f <$> act = act >+= \x -> returnES (f x)
(<*>) :: ES e s (a -> b) -> ES e s a -> ES e s b
sf <*> sx = sf >+= \f -> sx >+= \x -> returnES (f x)
gets :: ES e s s
gets s = Right (s, s)
puts :: s -> ES e s ()
puts s _ = Right ((), s)
modify :: (s -> s) -> ES e s ()
modify f s = Right ((), f s)
mapES :: (a -> ES e s b) -> [a] -> ES e s [b]
mapES _ [] = returnES []
mapES f (x : xs) = f x >+= \y ->
mapES f xs >+= \ys ->
returnES (y:ys)
concatMapES :: (a -> ES e s [b]) -> [a] -> ES e s [b]
concatMapES f xs = concat <$> mapES f xs
mapAccumES :: (a -> b -> ES e s (a, c)) -> a -> [b] -> ES e s (a, [c])
mapAccumES _ s [] = returnES (s, [])
mapAccumES f s (x : xs) = f s x >+= \(s', y) ->
mapAccumES f s' xs >+= \(s'', ys) ->
returnES (s'', y:ys)
|