Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

A simple example showing that IO doesn't satisfy the monad laws?

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?)

like image 750
Petr Avatar asked Sep 27 '12 08:09

Petr


People also ask

Is IO Monad a state Monad?

The IO monad in Haskell is often explained as a state monad where the state is the world. So a value of type IO a monad is viewed as something like worldState -> (a, worldState) .

What problem does Monad solve?

Conclusion. Monad is a simple and powerful design pattern for function composition that helps us to solve very common IT problems such as input/output, exception handling, parsing, concurrency and other.

What is Monad condition?

A monad is essentially just a functor T with two extra methods, join , of type T (T a) -> T a , and unit (sometimes called return , fork , or pure ) of type a -> T a .

How does a Monad work?

What is a Monad? A monad is an algebraic structure in category theory, and in Haskell it is used to describe computations as sequences of steps, and to handle side effects such as state and IO. Monads are abstract, and they have many useful concrete instances. Monads provide a way to structure a program.


1 Answers

All monads in Haskell are only monads if you exclude the weird seq combinator. This is also true for IO. Since seq is not actually a regular function but involves magic, you have to exclude it from checking the monad laws for the same reason you have to exclude unsafePerformIO. Using seq you can prove all monads wrong, as follows.

In the Kleisli category the monad gives rise to, return is the identity morphism and (<=<) is composition. So return must be an identity for (<=<):

return <=< x = x 

Using seq even Identity and Maybe fail to be monads:

seq (return <=< undefined :: a -> Identity b) () = () seq (undefined            :: a -> Identity b) () = undefined  seq (return <=< undefined :: a -> Maybe b) () = () seq (undefined            :: a -> Maybe b) () = undefined 
like image 156
ertes Avatar answered Nov 11 '22 20:11

ertes