Created
January 22, 2013 18:17
-
-
Save nudded/4596849 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{-# LANGUAGE GADTs, FlexibleInstances, MultiParamTypeClasses #-} | |
import Control.Monad.State.Class | |
import Control.Monad.Trans.Class | |
data StateD s m a where | |
Get :: StateD s m s | |
Put :: s -> StateD s m () | |
Return :: a -> StateD s m a | |
Bind :: StateD s m a -> (a -> StateD s m b) -> StateD s m b | |
Lift :: m a -> StateD s m a | |
instance Monad (StateD s m) where | |
return a = Return a | |
ma >>= f = Bind ma f | |
instance MonadState s (StateD s m) where | |
get = Get | |
put s = Put s | |
instance MonadTrans (StateD s) where | |
lift = Lift | |
run :: Monad m => StateD s m a -> s -> m (s, a) | |
run Get s = return (s, s) | |
run (Put s') s = return (s', ()) | |
run (Bind ma f) s = (run ma s) >>= (\(s',a) -> run (f a) s') | |
run (Return a) s = return (s, a) | |
run (Lift ma) s = ma >>= (\a -> return (s, a)) | |
testAction :: StateD Int IO Int | |
testAction = do | |
s <- get | |
put (s+1) | |
lift $ print "test" | |
get |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment