Skip to content

Instantly share code, notes, and snippets.

@lotz84
Created September 20, 2015 15:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lotz84/ec6b7cb737f12211ebd3 to your computer and use it in GitHub Desktop.
Save lotz84/ec6b7cb737f12211ebd3 to your computer and use it in GitHub Desktop.
StateIO s a ≡ StateT s IO a
{-# LANGUAGE GADTs #-}
import Control.Monad.Operational
data IStateIO s a where
Get :: IStateIO s s
Put :: s -> IStateIO s ()
LiftIO :: IO a -> IStateIO s a
get' = singleton Get
put' = singleton . Put
liftIO' = singleton . LiftIO
type StateIO s = Program (IStateIO s)
runStateIO :: StateIO s a -> s -> IO a
runStateIO = eval . view
where
eval :: ProgramView (IStateIO s) a -> s -> IO a
eval (Get :>>= is) s = runStateIO (is s) s
eval (Put s' :>>= is) s = runStateIO (is ()) s'
eval (LiftIO io :>>= is) s = io >>= (\a -> runStateIO (is a) s)
eval (Return a) s = return a
@lotz84
Copy link
Author

lotz84 commented Sep 20, 2015

proc :: StateIO Int ()
proc = do
    x <- get'
    liftIO' $ (putStr.show)  x
    put' 514
    y <- get'
    liftIO' $  print y

main = runStateIO proc 114
-- 114514

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment