fork download
  1. import Control.Monad.Reader
  2.  
  3. data Connection = Connection
  4. data Builder = Builder
  5.  
  6. data Context = Context { connection :: Connection, builder :: Builder }
  7.  
  8. no_action :: Connection -> Builder -> Int -> IO Int
  9. no_action _ _ i = return (i + 1)
  10.  
  11.  
  12. type CBIO b = ReaderT Context IO b
  13.  
  14. liftCBIO :: (Connection -> Builder -> a -> IO b) -> (a -> CBIO b)
  15. liftCBIO f v = do
  16. context <- ask
  17. liftIO (f (connection context) (builder context) v)
  18.  
  19. cbio_no_action = liftCBIO no_action
  20.  
  21. runWithInIO = flip runReaderT
  22.  
  23.  
  24. main = do
  25. i <- runWithInIO (Context Connection Builder) $ do
  26. a <- cbio_no_action 20
  27. liftIO $ putStrLn "Halfway through!"
  28. b <- cbio_no_action 30
  29. return (a + b)
  30.  
Success #stdin #stdout 0s 0KB
stdin
Standard input is empty
stdout
Halfway through!
52