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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T01:32:35+00:00 2026-05-25T01:32:35+00:00

Consider the fragment – getLine >>= \_ -> getLine >>= putStr It does the

  • 0

Consider the fragment –

getLine >>= \_ -> getLine >>= putStr

It does the reasonable thing, asking for a string twice, and then printing the last input. Because the compiler has no way of knowing what outside effects getLine has, it has to execute both of them, even though we throw away the result of the first one.

What I need is to wrap the IO Monad into another Monad (M) that allows IO computations to be effectively NOPs unless their return values are used. So that the program above could be rewritten as something like –

runM $ lift getLine >>= \_ -> lift getLine >>= lift putStr

Where

runM :: M a -> IO a
lift :: IO a -> M a

And the user is asked for input only once.

However, I cannot figure out how to write this Monad to achieve the effect I want. I’m not sure if it’s even possible. Could someone please help?

  • 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-25T01:32:36+00:00Added an answer on May 25, 2026 at 1:32 am

    Lazy IO is usually implemented using unsafeInterleaveIO :: IO a -> IO a, which delays the side effects of an IO action until its result is demanded, so we’ll probably have to use that, but let’s get some minor problems out of the way first.

    First of all, lift putStr would not type check, as putStr has type String -> IO (), and lift has type IO a -> M a. We’ll have to use something like lift . putStr instead.

    Secondly, we’re going to have to differentiate between IO actions that should be lazy and those who should not. Otherwise the putStr will never be executed, as we’re not using it’s return value () anywhere.

    Taking that into account, this seems to work for your simple example, at least.

    {-# LANGUAGE GeneralizedNewtypeDeriving #-}
    
    import System.IO.Unsafe
    
    newtype M a = M { runM :: IO a }
        deriving (Monad)
    
    lazy :: IO a -> M a
    lazy = M . unsafeInterleaveIO
    
    lift :: IO a -> M a
    lift = M
    
    main = runM $ lazy getLine >> lazy getLine >>= lift . putStr
    

    However, as C. A. McCann points out, you should probably not use this for anything serious. Lazy IO is frowned upon already, as it makes it difficult to reason about the actual order of the side effects. This would make it even harder.

    Consider this example

    main = runM $ do
        foo <- lazy readLn
        bar <- lazy readLn
        return $ foo / bar
    

    The order of the two numbers are read in will be completely undefined, and may change depending on compiler version, optimizations or the alignment of the stars. The name unsafeInterleaveIO is long and ugly for a good reason: to remind you of the dangers of using it. It’s a good idea to let people know when it’s being used and not hide it in a monad.

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

Sidebar

Related Questions

Consider this C# snippet: static string input = null; static string output = null;
Consider the following string which is a C fragment in a file: strcat(errbuf,errbuftemp); I
Consider the following code fragment in VS2010 Beta 1: let array = Array2D.zeroCreate 1000
Consider the following code fragment: let t1 = async { return process1() } let
Consider: List<String> someList = new ArrayList<>(); // add "monkey", "donkey", "skeleton key" to someList
consider these POCOs: class Foo { int Id {get;set;} string Name {get;set;} } class
Consider the following code: class Program { static void Main(string[] args) { try {
Consider the following code: [Serializable] public class Human { public string Name { get;
Consider the following code: class Foo(var name: String = bar) Now i try to
Consider this scenario. I have some business logic that now and then will be

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.