Skip to content

Instantly share code, notes, and snippets.

@nurpax
Created February 20, 2012 08:51
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 nurpax/1868472 to your computer and use it in GitHub Desktop.
Save nurpax/1868472 to your computer and use it in GitHub Desktop.
Reader + IO monad transformer
import Control.Monad.Reader
data Db = Db { conn :: String }
type DbIO = ReaderT Db IO
insert :: String -> DbIO ()
insert s = do
conn <- asks conn
liftIO $ putStrLn ("Using connection " ++ conn ++ " inserting " ++ s)
run :: DbIO ()
run = do
conn <- asks conn
liftIO $ putStrLn ("Using connection " ++ conn)
insert "foo"
return ()
dbContext =
Db "this is my cnxn"
main :: IO ()
main = do
(runReaderT run dbContext)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment