ReaderT

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
import Control.Monad.Trans.Reader

data Context = Context
  { contextA :: Int
  , contextB :: String
  } deriving (Eq, Show)

funA :: Int -> ReaderT Context IO Int
funA i = do
  context <- ask
  return $ i + contextA context

funB :: String -> ReaderT Context IO String
funB s = do
  context <- ask
  return $ contextB context ++ " " ++ s

funC :: ReaderT Context IO String
funC = do
  i <- funA 2
  funB $ show i

main = do
  let c = Context 1 "Hello"
  runReaderT funC c >>= putStrLn