Monads-tf: MonadReader instance for MonadState?

It sounds like what you want is essentially ask to have the same effect as get I can't help but wonder why you don't just use get in that case :).

It sounds like what you want is essentially ask to have the same effect as get. I can't help but wonder why you don't just use get in that case :) If your aim is to write code that either reads the state or the env depending on what is available, you have to ask what you plan to do with, say, ReaderT r (StateT s m) a, where you have both. For that reason, you can't just do: instance MonadState m => MonadReader m where type EnvType m = StateType m ask = get because you'll conflict with existing instances.

You can, however, do: {-# LANGUAGE GeneralizedNewtypeDeriving, TypeFamilies #-} newtype ReadState m a = RS { unRS :: m a } deriving (Monad) instance MonadState m => MonadReader (ReadState m) where type EnvType (ReadState m) = StateType m ask = RS get local f (RS m) = RS $ do s (forall m . (MonadReader m, EnvType m ~ StateType n) => m a) -> n a useStateAsEnv m = unRS m Then you can do useStateAsEnv (liftM (+1) ask) and get a MonadState value. I haven't thoroughly investigated how useful this is in practice, however – to produce a value of type forall m.

MonadReader m => m a, you can pretty much only use ask, local, and monadic functions. Here's a similar, less general but probably more useful thing, using standard transformers: readerToState :: (Monad m) => ReaderT r m a -> StateT r m a readerToState reader = StateT $ \env -> do res ReaderT (StateType (t m)) m be -> t m be which is a generalisation of the above but may not actually be a useful one.

Thank you for the answer. I don't want to use get since I want to force f2 not to modify state. Using get will not allow compiler to say me that put is used somewhere in f2 – Yuras Sep 3 '11 at 13:05 Hmm.

On second thoughts, the ReadState newtype may not actually be all that useful. If you're sticking to standard transformers, the readerToState that I just edited in is probably what you're looking for. – benmachine Sep 4 '11 at 0:40 Great!

ReaderToState is ok for me. I'm going to accept the answer. Thanks.

– Yuras Sep 4 '11 at 10:54.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions