blob: 70352f3cbb8f303d90bb69be9560d185d45db441 (
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
|
module Moi where
newtype Moi s a = Moi { runMoi :: s -> (a, s) }
instance Functor (Moi s) where
-- fmap :: (a -> b) -> Moi s a -> Moi s b
fmap f (Moi g) = Moi (\x -> h $ g x)
where h (a, b) = (f a, b)
instance Applicative (Moi s) where
-- pure :: a -> Moi s a
pure a = Moi (\s -> (a, s))
-- (<*>) :: Moi s (a -> b) -> Moi s a -> Moi s b
(Moi f) <*> (Moi g) = Moi (\s -> ((fst (f s)) (fst (g s)), s))
instance Monad (Moi s) where
return = pure
-- (>>=) :: Moi s a -> (a -> Moi s b) -> Moi s b
(Moi f) >>= g = Moi (\s -> (runMoi (g (fst (f s)))) (snd (f s)))
get :: Moi s s
get = Moi $ \a -> (a, a)
put :: s -> Moi s ()
put s = Moi $ \_ -> ((), s)
exec :: Moi s a -> s -> s
exec (Moi sa) s = snd (sa s)
eval :: Moi s a -> s -> a
eval (Moi sa) s = fst (sa s)
modify :: (s -> s) -> Moi s ()
modify f = Moi $ \a -> ((), f a)
|