Skip to content

Instantly share code, notes, and snippets.

@tokiwoousaka
Last active August 29, 2015 14:24
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 tokiwoousaka/34c865acdfdec625b10c to your computer and use it in GitHub Desktop.
Save tokiwoousaka/34c865acdfdec625b10c to your computer and use it in GitHub Desktop.
skeleton使ってみた
{-# LANGUAGE GADTs #-}
{-# LANGUAGE RankNTypes #-}
{-# LANGUAGE MultiParamTypeClasses #-}
{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE ScopedTypeVariables #-}
module MonadSkeletonTest where
import Control.Monad.Skeleton
import Control.Monad.State
data MyProgBase a where
PrintString :: MyProgBase ()
GetString :: MyProgBase String
PutString :: String -> MyProgBase ()
type MyProg = Skeleton MyProgBase
instance MonadState String MyProg where
get = getString
put = putString
----
printString :: MyProg ()
printString = bone PrintString
getString :: MyProg String
getString = bone GetString
putString :: String -> MyProg ()
putString = bone . PutString
runMyProg :: String -> MyProg a -> IO a
runMyProg s p = evalStateT (interpret run p) s
where
run :: MyProgBase a -> StateT String IO a
run GetString = get
run PrintString = get >>= liftIO . putStrLn
run (PutString s) = put s
interpret :: forall instr m b. Monad m => (forall a. instr a -> m a) -> Skeleton instr b -> m b
interpret f p = run $ unbone p
where
run :: MonadView instr (Skeleton instr) a -> m a
run (Return x) = return x
run (v :>>= n) = f v >>= run . unbone . n
----
test :: MyProg String
test = do
x <- getString
put "Hoge"
printString
y <- get
put "Piyo"
printString
z <- get
return $ x ++ " - " ++ y ++ " - " ++ z
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment