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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T05:12:51+00:00 2026-06-02T05:12:51+00:00

Monads can do many amazing, crazy things. They can create variables which hold a

  • 0

Monads can do many amazing, crazy things. They can create variables which hold a superposition of values. They can allow you to access data from the future before you compute it. They can allow you to write destructive updates, but not really. And then the continuation monad allows you to break people’s minds! Ususally your own. 😉

But here’s a challenge: Can you make a monad which can be paused?

data Pause s x
instance Monad (Pause s)
mutate :: (s -> s) -> Pause s ()
yield :: Pause s ()
step :: s -> Pause s () -> (s, Maybe (Pause s ()))

The Pause monad is a kind of state monad (hence mutate, with the obvious semantics). Normally a monad like this has some sort of “run” function, which runs the computation and hands you back the final state. But Pause is different: It provides a step function, which runs the computation until it calls the magical yield function. Here the computation is paused, returning to the caller enough information to resume the computation later.

For extra awesomness: Allow the caller to modify the state between step calls. (The type signatures above ought to allow this, for example.)


Use case: It’s often easy to write code that does something complex, but a total PITA to transform it to also output the intermediate states in its operation. If you want the user to be able to change something mid-way through execution, things get complex really fast.

Implementation ideas:

  • Obviously it can be done with threads, locks and IO. But can we do better? 😉

  • Something insane with a continuation monad?

  • Maybe some kind of writer monad, where yield just logs the current state, and then we can “pretend” to step it by iterating over the states in the log. (Obviously this precludes altering the state between steps, since we’re not really “pausing” anything now.)

  • 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-06-02T05:12:53+00:00Added an answer on June 2, 2026 at 5:12 am

    Sure; you just let any computation either finish with a result, or suspend itself, giving an action to be used on resume, along with the state at the time:

    data Pause s a = Pause { runPause :: s -> (PauseResult s a, s) }
    
    data PauseResult s a
        = Done a
        | Suspend (Pause s a)
    
    instance Monad (Pause s) where
        return a = Pause (\s -> (Done a, s))
        m >>= k = Pause $ \s ->
            case runPause m s of
                (Done a, s') -> runPause (k a) s'
                (Suspend m', s') -> (Suspend (m' >>= k), s')
    
    get :: Pause s s
    get = Pause (\s -> (Done s, s))
    
    put :: s -> Pause s ()
    put s = Pause (\_ -> (Done (), s))
    
    yield :: Pause s ()
    yield = Pause (\s -> (Suspend (return ()), s))
    
    step :: Pause s () -> s -> (Maybe (Pause s ()), s)
    step m s =
        case runPause m s of
            (Done _, s') -> (Nothing, s')
            (Suspend m', s') -> (Just m', s')
    

    The Monad instance just sequences things in the normal way, passing the final result to the k continuation, or adding the rest of the computation to be done on suspension.

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

Sidebar

Related Questions

While monads are represented in Haskell using the bind and return functions, they can
Can continuations be said to be monads? Are they a subset of monads or
There are too many tutorials out there on monads that say... Look! here is
I finally got a hold on how to use monads (don't know if I
I like haskell and many things connected with it as its type-engine, lot of
Can the following function be simplified with higher order functions, Monads or what have
how can you access the inner monad of a ReaderT. In my case I
In my project I have created a data type, that can hold one of
I'm reading the Monads chapter in Real World Haskell (chapter 14). A function is
I've just wrapped my head around monads (at least I'd like to think I

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.