IO behaves oddly if used nested

Alastair Reid alastair at
Thu Oct 2 12:20:22 EDT 2003

> What about this program:
>   main :: IO ()
>   main = putStr (show (putStr "Hello World!"))
> Am I the only one who feels that there is some conceptual _wrongness_ about
> Hugs responding with <<IO action>>?

I think it is exactly right.

Having it print "Hello World" would clearly be wrong since
it confuses values (like strings) with computations that 
produce strings. 

One way to understand the difference is to think of the difference between you 
sending an order to a shop for them to send you a pair of socks and them 
actually sending you the socks.  The order is a computation which you can 
photocopy, file in your in tray, lose, etc.  The sending of the socks is an 
action described by the order.

What would you like to see in this case?

In the sock example, I might like to see:

  <<Order for 1 pair red socks from blaat blaat>>

But monads are often opaque datatypes so the best I might be able to get is:

  <<Order for 0 or more things>>

(Monads are often opaque datatypes because they often involve function types 
and function types are opaque.)

> Another question with a trivial answer, what is the result of:
>   main :: IO (IO ())
>   main = return (putStr "Hello World!")

It is a computation which, if executed, will print "Hello World"

> Clearly it also shows the relation between IO and chosen evaluation
> strategy.

This isn't clear to me at all - can you explain further?


More information about the Haskell mailing list