[Haskell-cafe] A Question about IO

14 views
Skip to first unread message

Zongzhe Yuan

unread,
Feb 28, 2015, 6:40:18 PM2/28/15
to Haskell-Cafe
Hi
i have a question about IO monad.
Sometimes IO do something and return something, i wonder if the return type, for example is IO Int means it will return an int, could i purely fetch the int?
but i fail, i cannot construct a function, i use do notation but i found i must use something to pass the int to another monad.
But i could write a function has the type of Maybe a -> a and it is easily.
Maybe monad and IO monad both instance monad but why i can’t do that?


Zongzhe Yuan

This message and any attachment are intended solely for the addressee
and may contain confidential information. If you have received this
message in error, please send it back to me, and immediately delete it.

Please do not use, copy or disclose the information contained in this
message or in any attachment. Any views or opinions expressed by the
author of this email do not necessarily reflect the views of the
University of Nottingham.

This message has been checked for viruses but the contents of an
attachment may still contain software viruses which could damage your
computer system, you are advised to perform your own checks. Email
communications with the University of Nottingham may be monitored as
permitted by UK legislation.

_______________________________________________
Haskell-Cafe mailing list
Haskel...@haskell.org
http://mail.haskell.org/cgi-bin/mailman/listinfo/haskell-cafe

Brandon Allbery

unread,
Feb 28, 2015, 7:00:23 PM2/28/15
to Zongzhe Yuan, Haskell-Cafe
On Sat, Feb 28, 2015 at 6:40 PM, Zongzhe Yuan <psy...@nottingham.ac.uk> wrote:
Sometimes IO do something and return something, i wonder if the return type, for example is IO Int means it will return an int, could i purely fetch the int?

Conceptually, an IO something is a *program* that can produce, not under your control, a something. You create this program and return it as the value of (main :: IO a); then the runtime executes it.

(Actually, what happens in GHC is different. There are ways to get at the value --- but you have to dig into GHC internals and use them to lie to GHC, and it will punish you for it by (among other things) treating it as a pure value and memoizing, sharing (or not sharing when you expect it to), etc. as it sees fit. In short: don't. This is not some thing to force you to jump through hoops for no good reason, with an easy escape hatch to get the behavior you'd get from a random Perl/Java/whatever program; if you go around its back, things *will* break unless you understand how GHC works.)

There is nothing in Monad that guarantees that you can go from Monad m => m a -> a. Specific Monad-s (lists, Maybe, etc.) may provide functions to do so, or expose their data constructors so that you can pattern match them; other Monad-s (IO, ST, STM) do not, and you cannot get at "the value inside" --- indeed, there may not be a value inside. (This isn't even specific to Monad; you can't pattern match a Data.Map.Map, either, and it's not a Monad. It does provide toList, though, as well as its own access methods.)

--
brandon s allbery kf8nh                               sine nomine associates
allb...@gmail.com                                  ball...@sinenomine.net
unix, openafs, kerberos, infrastructure, xmonad        http://sinenomine.net
Reply all
Reply to author
Forward
0 new messages