Advertisement
Guest User

Untitled

a guest
Jul 26th, 2019
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. {-# LANGUAGE GeneralizedNewtypeDeriving #-}
  2. {-# LANGUAGE ScopedTypeVariables #-}
  3.  
  4. import Control.Monad (void)
  5.  
  6. newtype AppEnv a = AppEnv (ReaderT () (StateT () IO) a)  -- pretend the () are useful types
  7.     deriving (Functor, Applicative, Monad)
  8.  
  9. instance MsgsRead AppEnv where
  10.     readMsgPayloads = return []  -- implementation omitted
  11. instance MsgsWrite AppEnv where
  12.     writeMsgs _ = return ()      -- implementation omitted
  13.  
  14. runAppEnv :: AppEnv () -> IO ()
  15. runAppEnv (AppEnv appEnv) = evalStateT (runReaderT appEnv ()) ()
  16.  
  17. f1 :: forall m. MsgsRead m => m ()
  18. f1 = void (readMsgPayloads :: m [FooMsgPayload])
  19.  
  20. f2 :: MsgsWrite m => m ()
  21. f2 = writeMsgs [initMsg ZzzMsgA]
  22.  
  23. f3 :: forall m. (MsgsRead m, MsgsWrite m) => m ()
  24. f3 = do
  25.     void (readMsgPayloads :: m [BarMsgPayload])
  26.     writeMsgs [initMsg ZzzMsgC]
  27.  
  28. main :: IO ()
  29. main = runAppEnv $ f1 >> f2 >> f3  -- pretend this is doing something useful
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement