Skip to content

Instantly share code, notes, and snippets.

@ssanj
Created December 18, 2017 14:07
Show Gist options
  • Save ssanj/a59716b4656e76cac2fa6d47be97e6d2 to your computer and use it in GitHub Desktop.
Save ssanj/a59716b4656e76cac2fa6d47be97e6d2 to your computer and use it in GitHub Desktop.
{-# LANGUAGE TypeSynonymInstances #-}
{-# LANGUAGE FlexibleInstances #-}
module Sample(perform, runAsIO, runAsSt) where
import Text.Printf (printf)
import qualified Control.Monad.Trans.State as S
class Monad m => ConsoleR m where
writeLine :: String -> m ()
readLine :: m String
perform :: ConsoleR m => m ()
perform = do writeLine "What is your name?"
name <- readLine
writeLine (printf "Hello %s" name)
instance ConsoleR IO where
writeLine = putStrLn
readLine = getLine
instance ConsoleR (S.State String) where
writeLine l = S.modify (++ "WL:" ++ l)
readLine = S.state (\s -> ("some_name", s ++ "RL:some_name"))
runAsIO :: IO ()
runAsIO = perform :: IO ()
runAsSt :: S.State String ()
runAsSt = perform :: S.State String ()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment