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

  • Home
  • SEARCH
  • 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 8619895
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T06:27:02+00:00 2026-06-12T06:27:02+00:00

I’ve seen mentioned that IO doesn’t satisfy the monad laws, but I didn’t find

  • 0

I’ve seen mentioned that IO doesn’t satisfy the monad laws, but I didn’t find a simple example showing that. Anybody knows an example? Thanks.

Edit: As ertes and n.m. pointed out, using seq is a bit illegal as it can make any monad fail the laws (combined with undefined). Since undefined may be viewed as a non-terminating computation, it’s perfectly fine to use it.

So the revised question is: Anybody knows an example showing that IO fails to satisfy the monad laws, without using seq? (Or perhaps a proof that IO does satisfy the laws if seq is not allowed?)

  • 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-12T06:27:03+00:00Added an answer on June 12, 2026 at 6:27 am

    tl;dr upfront: seq is the only way.

    Since the implementation of IO is not prescribed by the standard, we can only look at specific implementations. If we look at GHC’s implementation, as it is available from the source (it might be that some of the behind-the-scenes special treatment of IO introduces violations of the monad laws, but I’m not aware of any such occurrence),

    -- in GHC.Types (ghc-prim)
    newtype IO a = IO (State# RealWorld -> (# State# RealWorld, a #))
    
    -- in GHC.Base (base)
    instance  Monad IO  where
        {-# INLINE return #-}
        {-# INLINE (>>)   #-}
        {-# INLINE (>>=)  #-}
        m >> k    = m >>= \ _ -> k
        return    = returnIO
        (>>=)     = bindIO
        fail s    = failIO s
    
    returnIO :: a -> IO a
    returnIO x = IO $ \ s -> (# s, x #)
    
    bindIO :: IO a -> (a -> IO b) -> IO b
    bindIO (IO m) k = IO $ \ s -> case m s of (# new_s, a #) -> unIO (k a) new_s
    
    thenIO :: IO a -> IO b -> IO b
    thenIO (IO m) k = IO $ \ s -> case m s of (# new_s, _ #) -> unIO k new_s
    
    unIO :: IO a -> (State# RealWorld -> (# State# RealWorld, a #))
    unIO (IO a) = a
    

    it’s implemented as a (strict) state monad. So any violation of the monad laws IO makes, is also made by Control.Monad.State[.Strict].

    Let’s look at the monad laws and see what happens in IO:

    return x >>= f ≡ f x:
    return x >>= f = IO $ \s -> case (\t -> (# t, x #)) s of
                                  (# new_s, a #) -> unIO (f a) new_s
                   = IO $ \s -> case (# s, x #) of
                                  (# new_s, a #) -> unIO (f a) new_s
                   = IO $ \s -> unIO (f x) s
    

    Ignoring the newtype wrapper, that means return x >>= f becomes \s -> (f x) s. The only way to (possibly) distinguish that from f x is seq. (And seq can only distinguish it if f x ≡ undefined.)

    m >>= return ≡ m:
    (IO k) >>= return = IO $ \s -> case k s of
                                     (# new_s, a #) -> unIO (return a) new_s
                      = IO $ \s -> case k s of
                                     (# new_s, a #) -> (\t -> (# t, a #)) new_s
                      = IO $ \s -> case k s of
                                     (# new_s, a #) -> (# new_s, a #)
                      = IO $ \s -> k s
    

    ignoring the newtype wrapper again, k is replaced by \s -> k s, which again is only distinguishable by seq, and only if k ≡ undefined.

    m >>= (\x -> g x >>= h) ≡ (m >>= g) >>= h:
    (IO k) >>= (\x -> g x >>= h) = IO $ \s -> case k s of
                                                (# new_s, a #) -> unIO ((\x -> g x >>= h) a) new_s
                                 = IO $ \s -> case k s of
                                                (# new_s, a #) -> unIO (g a >>= h) new_s
                                 = IO $ \s -> case k s of
                                                (# new_s, a #) -> (\t -> case unIO (g a) t of
                                                                           (# new_t, b #) -> unIO (h b) new_t) new_s
                                 = IO $ \s -> case k s of
                                                (# new_s, a #) -> case unIO (g a) new_s of
                                                                    (# new_t, b #) -> unIO (h b) new_t
    ((IO k) >>= g) >>= h = IO $ \s -> case (\t -> case k t of
                                                    (# new_s, a #) -> unIO (g a) new_s) s of
                                        (# new_t, b #) -> unIO (h b) new_t
                         = IO $ \s -> case (case k s of
                                              (# new_s, a #) -> unIO (g a) new_s) of
                                        (# new_t, b #) -> unIO (h b) new_t
    

    Now, we generally have

    case (case e of                    case e of
            pat1 -> ex1) of       ≡      pat1 -> case ex1 of
      pat2 -> ex2                                  pat2 -> ex2
    

    per equation 3.17.3.(a) of the language report, so this law holds not only modulo seq.

    Summarising, IO satisfies the monad laws, except for the fact that seq can distinguish undefined and \s -> undefined s. The same holds for State[T], Reader[T], (->) a, and any other monads wrapping a function type.

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

Sidebar

Related Questions

Seemingly simple, but I cannot find anything relevant on the web. What is the
I have a French site that I want to parse, but am running into
I am doing a simple coin flipping experiment for class that involves flipping a
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I have a .ini file as follows: [playlist] numberofentries=2 File1=http://87.230.82.17:80 Title1=(#1 - 365/1400) Example
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have just tried to save a simple *.rtf file with some websites and
I want to count how many characters a certain string has in PHP, but
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function

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.