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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T17:06:04+00:00 2026-05-16T17:06:04+00:00

Suppose I have a state monad such as: data Registers = Reg {…} data

  • 0

Suppose I have a state monad such as:

data Registers = Reg {...}

data ST = ST {registers :: Registers,
              memory    :: Array Int Int}

newtype Op a = Op {runOp :: ST -> (ST, a)}

instance Monad Op where
 return a    = Op $ \st -> (st, a)
 (>>=) stf f = Op $ \st -> let (st1, a1) = runOp stf st
                               (st2, a2) = runOp (f a1) st1
                            in (st2, a2)

with functions like

getState :: (ST -> a) -> Op a
getState g = Op (\st -> (st, g st)

updState :: (ST -> ST) -> Op ()
updState g = Op (\st -> (g st, ()))

and so forth. I want to combine various operations in this monad with IO actions. So I could either write an evaluation loop in which operations in this monad were performed and an IO action is executed with the result, or, I think, I should be able to do something like the following:

newtype Op a = Op {runOp :: ST -> IO (ST, a)}

Printing functions would have type Op () and other functions would have type Op a, e.g., I could read a character from the terminal using a function of type IO Char. However, I’m not sure what such a function would look like, since e.g., the following is not valid.

runOp (do x <- getLine; setMem 10 ... (read x :: Int) ... ) st

since getLine has type IO Char, but this expression would have type Op Char. In outline, how would I do this?

  • 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-16T17:06:05+00:00Added an answer on May 16, 2026 at 5:06 pm

    The basic approach would be to rewrite your Op monad as a monad transformer. This would allow you to use it in a “stack” of monads, the bottom of which might be IO.

    Here’s an example of what that might look like:

    import Data.Array
    import Control.Monad.Trans
    
    data Registers = Reg { foo :: Int }
    
    data ST = ST {registers :: Registers,
                  memory    :: Array Int Int}
    
    newtype Op m a = Op {runOp :: ST -> m (ST, a)}
    
    instance Monad m => Monad (Op m) where
     return a    = Op $ \st -> return (st, a)
     (>>=) stf f = Op $ \st -> do (st1, a1) <- runOp stf st
                                  (st2, a2) <- runOp (f a1) st1
                                  return (st2, a2)
    
    instance MonadTrans Op where
      lift m = Op $ \st -> do a <- m
                              return (st, a)
    
    getState :: Monad m => (ST -> a) -> Op m a
    getState g = Op $ \st -> return (st, g st)
    
    updState :: Monad m => (ST -> ST) -> Op m ()
    updState g = Op $ \st -> return (g st, ())
    
    testOpIO :: Op IO String
    testOpIO = do x <- lift getLine
                  return x
    
    test = runOp testOpIO
    

    The key things to observe:

    • The use of the MonadTrans class
    • The use of the lift function acting on getLine, which is used to bring the getline function from the IO monad and into the Op IO monad.

    Incidentally, if you don’t want the IO monad to always be present, you can replace it with the Identity monad in Control.Monad.Identity. The Op Identity monad behaves exactly the same as your original Op monad.

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

Sidebar

Related Questions

Suppose I have the following class structure: public class State { public int Id
Suppose I have a method which changes the state of an object, and fires
Suppose we have a Invoice that is saved in a draft state in a
Suppose we have the following table data: ID parent stage submitted 1 1 1
Suppose I have a simple model, such as Record: @Model public class Record {
Suppose we have a class called AccountService that manages the state of accounts. AccountService
Suppose I have a table that contains valid data. I would like to modify
Let's suppose I have a struct like this: struct my_struct { int a; int
Suppose you have the following code: int main(int argc, char** argv) { Foo f;
Suppose I have the following struct: struct sampleData { int x; int y; };

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.