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

The Archive Base Latest Questions

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

A confusing title for a confusing question! I understand a) monads, b) the IO

  • 0

A confusing title for a confusing question! I understand a) monads, b) the IO monad, c) the Cont monad (Control.Monad.Cont), and d) the ContT continuation transformer monad. (And I vaguely understand monad transformers in general — though not enough to answer this question.) I understand how to write a program where all the functions are in the Cont monad (Cont r a), and I understand how to write a program where all the functions are in the combined Cont/IO monad (ContT r IO a).

But I’m wondering how I might write a program where some functions are in a combined Cont/IO monad (ContT r IO a) and other functions are just in the Cont monad (Cont r a). Basically, I want to write the whole program in continuation style, but only use the IO monad where necessary (much like in “regular” Haskell code, I only use the IO monad where necessary).

For example consider these two functions, in non-continuation style:

foo :: Int -> IO Int
foo n = do
    let x = n + 1
    print x
    return $ bar x

bar :: Int -> Int
bar m = m * 2

Note that foo requires IO but bar is pure. Now I figured out how to write this code fully using the continuation monad, but I needed to thread IO through bar as well:

foo :: Int -> ContT r IO Int
foo n = do
    let x = n + 1
    liftIO $ print x
    bar x

bar :: Int -> ContT r IO Int
bar m = return $ m * 2

I do want all my code in continuation style, but I don’t want to have to use the IO monad on functions that don’t require it. Basically, I would like to define bar like this:

bar :: Int -> Cont r Int
bar m = return $ m * 2

Unfortunately, I can’t find a way to call a Cont r a monad function (bar) from inside a ContT r IO a monad function (foo). Is there any way to “lift” a non-transformed monad into a transformed one? i.e., how can I change the line “bar x” in foo so that it can correctly call bar :: Int -> Cont r Int?

  • 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-23T13:28:15+00:00Added an answer on May 23, 2026 at 1:28 pm

    This is where Control.Monad.Class comes in. Make bar polymorphic in what monad it can work in:

    bar :: MonadCont m => Int -> m Int
    bar m = return $ m * 2
    

    Note that the instances list at the bottom of the page shows that the instances of MonadCont known at the time the docs were generated include both Cont r and Monad m => ContT r m. Additionally, the MonadCont class is what defines the callCC function, which is what is necessary to use the continuation features. This means you can use the full expressiveness of continuations within bar, even though this example does not.

    In this way, you write functions that provably can’t use IO, because they don’t have a MonadIO constraint, nor does their type explicitly mention IO. But they are polymorphic in which monad they work within, such that they can be called trivially from contexts that do include IO.

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

Sidebar

Related Questions

Sorry if the question title is confusing. Let me explain further. I am building
I know the title of this question is a bit confusing, but here it
Ok so my question title may have been a little confusing. Here's my example:
Hello everybody and sorry for the rather confusing question-title. I have a Hibernate Class
I know the title is bit confusing for my question. Let me explain:- There
I know the title is bit confusing for my question. Let me explain:- There
the question title seems pretty confusing, but this is what i want to achieve.
Sorry for the confusing title of the question. I am uncertain about how should
Excuse my title if it's confusing, but I don't know how else to describe
Title might be a bit confusing, so let me explain. I have a website

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.