Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6533909
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T10:11:12+00:00 2026-05-25T10:11:12+00:00

Consider the next example. I have a monad MyM that is just a StateT

  • 0

Consider the next example. I have a monad MyM that is just a StateT

{-# LANGUAGE TypeFamilies #-}

import Control.Monad.State
import Control.Monad.Reader

type MyS = Int
type MyM = StateT MyS

Usually MyM is used for reading and writing MyS state, so I have functions like the next:

f1 :: (MonadState m, StateType m ~ MyS) => m ()
f1 = modify (+1)

But sometimes I need just to read MyS, so I want MonadReader context instead of MonadState:

f2 :: (MonadReader m, EnvType m ~ MyS) => m Int
f2 = liftM (+1) ask

And I want to write something like:

f3 :: (MonadState m, StateType m ~ MyS) => m Int
f3 = f1 >> f2

So basically I need every MonadState instance to be MonadReader instance too with the correspondent family type. Something like

instance MonadState m => MonadReader where
  type EnvType m = StateType m
  ...

But I can’t find the way how to make it type check. Is it possible to express such the relation between MonadState and MonadReader?

Thanks.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T10:11:13+00:00Added an answer on May 25, 2026 at 10:11 am

    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 <- get
        modify f
        x <- m
        put s
        return x
    

    Then if you have a polymorphic reader value like f2, you can pull a MonadState out of it with unRS. If you want to use some more devious extensions, try this with RankNTypes:

    useStateAsEnv :: (MonadState n) => (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 <- runReaderT reader env
      return (res, env)
    

    Edit: thinking about this later you could probably generalise this to any state monad transformer with lift . runReaderT reader =<< get, but the type starts to be rather unwieldy:

    :: (Monad m, MonadTrans t, MonadState (t m)) => ReaderT (StateType (t m)) m b -> t m b
    

    which is a generalisation of the above but may not actually be a useful one.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Consider the following example program: next :: Int -> Int next i | 0
Consider the next example: public List<Allergy> GetAllergies(int? ingredientId = null) { var allergies =
Consider next example : #include <iostream> #include <typeinfo> template< int N, typename T >
Consider next example : #include <iostream> template< int a > void foo(); int main(int
In a database consider that I have a table with 4000 rows. I am
Over the next few months, we need to consider the best technology/technique for periodically
Consider this problem: I have a program which should fetch (let's say) 100 records
I would like to determine the next date that has a day of the
Consider this example: When the user clicks a button, ClassA fires OnUserInteraction event rapidly
Consider the following example, where grep is used to search in binary mode: $

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.