On Sat, Feb 28, 2015 at 6:40 PM, Zongzhe Yuan <psyzy3@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
allbery.b@gmail.com                                  ballbery@sinenomine.net
unix, openafs, kerberos, infrastructure, xmonad        http://sinenomine.net