IO behaves oddly if used nested
ddarius at hotpop.com
Thu Oct 2 08:08:16 EDT 2003
On Thu, 02 Oct 2003 12:47:15 +0200
Lennart Augustsson <lennart at augustsson.net> wrote:
> Alastair Reid wrote:
> >>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
> > This isn't clear to me at all - can you explain further?
> Is it even type correct with
> main :: IO (IO ())
> If it is, it shouldn't be. It makes no sense.
> The value computed by the top level IO action should
> have some consumer. Sensible types for the consumer
> (which in some sense is the OS) are () or some exit code.
If I'm not mistaken, the Report restricts main's type to be, at least,
IO a. Anyways, it's perfectly sensible to return anything. The RTS
simply discards it. The above example as an entire program is an IO
action that returns an IO action that is discarded by the RTS. I.e. the
program doesn't do anything. Neither example is odd behavior, unless
you consider Hugs providing a perfectly reasonable instance of Show for
IO a odd.
More information about the Haskell