Created
November 27, 2018 19:48
-
-
Save Rembane/5be737cae9b680d72dd12805c528cf54 to your computer and use it in GitHub Desktop.
The order of StateT and ReaderT.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{-# LANGUAGE FlexibleContexts, GeneralizedNewtypeDeriving #-} | |
module Main where | |
import Control.Monad.Reader | |
import Control.Monad.State.Strict | |
newtype MyStackT1 m r s a = MyStackT1 | |
{ runMyStackT1 :: ReaderT r (StateT s m) a } | |
deriving (Functor, Applicative, Monad, MonadReader r, MonadState s, MonadIO) | |
newtype MyStackT2 m r s a = MyStackT2 | |
{ runMyStackT2 :: StateT s (ReaderT r m) a } | |
deriving (Functor, Applicative, Monad, MonadReader r, MonadState s, MonadIO) | |
aContrivedExample | |
:: (Monad m, MonadReader Int m, MonadState String m, MonadIO m) => m String | |
aContrivedExample = do | |
n <- ask | |
s <- get | |
liftIO $ putStrLn s | |
put $ concat $ replicate n s | |
pure "It is done." | |
main :: IO () | |
main = do | |
putStr $ unlines | |
[ "If these are any different, they should output a different result..." | |
, "It could also be so that I haven't found an example where they dont." | |
, "Feel free to experiment!" | |
, "Variant one:" | |
] | |
print =<< runStateT (runReaderT (runMyStackT1 aContrivedExample) 5) "Hej!" | |
putStrLn "Variant two:" | |
print =<< runReaderT (runStateT (runMyStackT2 aContrivedExample) "Hej!") 5 | |
pure () |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment