-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathStateT.hs
40 lines (30 loc) · 939 Bytes
/
StateT.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
{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE MultiParamTypeClasses #-}
module StateT where
import Control.Monad
newtype MyStateT s m a = MyStateT
{ runMyStateT :: s -> m (a, s)
}
class MonadTrans t where
lift :: Monad m => m a -> t m a
class (MonadState s m) where
get :: m s
put :: s -> m ()
modify :: (s -> s) -> m ()
instance Monad m => Functor (MyStateT s m) where
fmap = liftM
instance Monad m => Applicative (MyStateT s m) where
pure a = MyStateT $ \s -> return (a, s)
(<*>) = ap
instance Monad m => Monad (MyStateT s m) where
return = pure
(MyStateT a) >>= b =
MyStateT $ \s -> do
(a', s') <- a s
runMyStateT (b a') s'
instance MonadTrans (MyStateT s) where
lift m = MyStateT $ \s -> flip (,) s <$> m
instance Monad m => MonadState s (MyStateT s m) where
get = MyStateT $ \s -> return (s, s)
put s = MyStateT $ \_ -> return ((), s)
modify f = get >>= put . f