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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T17:08:51+00:00 2026-05-31T17:08:51+00:00

I thought that in principle Haskell’s type system would forbid calls to impure functions

  • 0

I thought that in principle Haskell’s type system would forbid calls to impure functions (i.e. f :: a -> IO b) from pure ones, but today I realized that by calling them with return they compile just fine. In this example:

h :: Maybe ()
h = do
    return $ putStrLn "???"
    return ()

h works in the Maybe monad, but it’s a pure function nevertheless. Compiling and running it simply returns Just () as one would expect, without actually doing any I/O. I think Haskell’s laziness puts the things together (i.e. putStrLn‘s return value is not used – and can’t since its value constructors are hidden and I can’t pattern match against it), but why is this code legal? Are there any other reasons that makes this allowed?

As a bonus, related question: in general, is it possible to forbid at all the execution of actions of a monad from within other ones, and how?

  • 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-31T17:08:53+00:00Added an answer on May 31, 2026 at 5:08 pm

    IO actions are first-class values like any other; that’s what makes Haskell’s IO so expressive, allowing you to build higher-order control structures (like mapM_) from scratch. Laziness isn’t relevant here,1 it’s just that you’re not actually executing the action. You’re just constructing the value Just (putStrLn "???"), then throwing it away.

    putStrLn "???" existing doesn’t cause a line to be printed to the screen. By itself, putStrLn "???" is just a description of some IO that could be done to cause a line to be printed to the screen. The only execution that happens is executing main, which you constructed from other IO actions, or whatever actions you type into GHCi. For more information, see the introduction to IO.

    Indeed, it’s perfectly conceivable that you might want to juggle about IO actions inside Maybe; imagine a function String -> Maybe (IO ()), which checks the string for validity, and if it’s valid, returns an IO action to print some information derived from the string. This is possible precisely because of Haskell’s first-class IO actions.

    But a monad has no ability to execute the actions of another monad unless you give it that ability.

    1 Indeed, h = putStrLn "???" `seq` return () doesn’t cause any IO to be performed either, even though it forces the evaluation of putStrLn "???".

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

Sidebar

Related Questions

I thought that by default my Regex would exhibit the greedy behavior that I
I thought that calling BeginInvoke more than once on the same delegate instance would
I thought that a delegate instance was interchangeable with a function instance. Take the
I thought that this was easier… I have a asp:hyperlink control, with target=_blank ,
I thought that layout is just a widget that keeps more widgets inside. But
I thought that I improve performance when I replace this code: def f(a, b):
I thought that the purpose of Ruby's BigDecimal class is that it is infinitely
I have always thought that in order to connect to SQL server using windows
After I thought that I've understood how they work, I tried this: NSString *str1
I saw this in someone's code and thought wow, that's an elegant way to

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.