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 7084879
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T07:21:23+00:00 2026-05-28T07:21:23+00:00

This is my first acquaintance with Monad Transformers, so the answer might be obvious.

  • 0

This is my first acquaintance with Monad Transformers, so the answer might be obvious.

Let’s say I am inside a do block of type StateT MyMonad MyType, I want to make another function of the same type both modify the state and return a value of type MyMonad MyType. How can I achieve that? I think the examples here show it in guessSession, yet I can’t seem to understand how to apply it!

  • 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-28T07:21:24+00:00Added an answer on May 28, 2026 at 7:21 am

    If you want to use the underlying monad in a monad transformer, you can use lift:

    lift :: (MonadTrans t, Monad m) => m a -> t m a
    

    In this case, t is StateT MyState, and m is MyMonad. So, for example:

    foo :: StateT MyState MyMonad MyType
    foo = do
      modify $ \s -> s+1
      lift $ doSomethingInMyMonad 42
    

    Monad transformers aren’t “layered on” in the sense that you’d return a value of type MyMonad MyType from inside; it’s a more literal transformation: they turn a monad into a new one that has the ability to run actions in the transformed monad. So, you can think of StateT s m as just the regular State s monad, except that you can also use lift to run turn actions in m into actions in StateT s m.

    If you’re using the standard Monad Transformer Library (mtl) transformers like StateT, ReaderT, etc., you don’t actually have to use lift; things like modify and ask work in any monad with the right transformer somewhere in the stack. (A stack is just a tower of transformed monads, like StateT s (ReaderT r IO).)

    Additionally, if you have a large stack with IO at the bottom, there’s a convenience function for lifting an IO action up any number of layers:

    liftIO :: (MonadIO m) => IO a -> m a
    

    So liftIO (putStrLn "Hello, world!") works in IO, StateT Int IO, ContT r (WriterT [String] IO), and so on.

    (As an additional note, foo here isn’t actually a function; a more accurate term is action or computation.)

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

Sidebar

Related Questions

Let's say I have this: first \n\n\n second I want to display that on
I did this: [User.first, User.last].to_xml and got this: <users type=array> <user> <created-at type=datetime>2010-03-16T06:40:51Z</created-at> <id
This first block of code works as expected. It's a foreach to print values
I'm using Text.ParserCombinators.Parsec and Text.XHtml to parse an input like this: - First type
<input type=button onclick=executes second /> $('input').click(function() { //this first }} p.s. onclick=executes second -
Inside of this first step towards a bootstrapped scheme interpreter I find the following
This first bit works: $my_id = 617; $post_id_7 = get_post($my_id); $title = $post_id_7->post_excerpt; echo
Saw this piece of code in a Ruby on Rails book. This first one
problem euler #5 i found the solution but i don't know why this first
Using Dozer to map two objects, I have: /** /* This first class uses

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.