A Question about IO monad

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.

Hi Zongzhe,
You can indeed write "Maybe a -> a" function easy, because you have Maybe
constructors exported for you. But please note that this will lead to
non-total function, e.g. it will have to return an error in case of call
with Nothing, so you should avoid writing and using functions like this.
IO case is even less safe, since IO is a special monad (its constructor is
not exported), and you should want to do something like this even less, but
still, you have functions like unsafePerformIO that have type "IO a -> a".
But most probably if you need it -- you're doing something wrong.
If you got to "IO a" value -- you should continue writing functions that
operate in IO, and if you want to apply pure function to inner value, you
can do it like this:
main = do
i <- ioIntVal
let newIntVal = myPureIntFunc i
putStrLn (show i)
1 бер. 2015 01:44, користувач "Zongzhe Yuan"
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 Haskell-Cafe@haskell.org http://mail.haskell.org/cgi-bin/mailman/listinfo/haskell-cafe

You can indeed write "Maybe a -> a" function easy, because you have Maybe constructors exported for you. But please note that this will lead to non-total function, e.g. it will have to return an error in case of call with Nothing, so you should avoid writing and using functions like this.
A tiny nitpick: you can get a total function if you return a default value instead of erroring out.
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?
The IO in IO Int means that instead of just returning an Int, a computation may produce a side-effect (like writing a string to standard output). Since reasoning about code without side-effects is much simpler, you want to separate it from impure code. That's what IO is for. Once you're inside IO, you're dealing with impure code, so you want to keep track of things that rely on it. You can't* and don't want to escape. * As Konstantine points out, there are legitimate cases for using unsafePerformIO, like writing an FFI binding to a pure function. Haskell type system can't see whether the function in question is pure or not, so it's tagged with IO. However, if you know that it's pure, you can explicitly state that by using unsafePerformIO.

On Sun, Mar 1, 2015 at 12:58 PM, Nikita Karetnikov
You can indeed write "Maybe a -> a" function easy, because you have Maybe constructors exported for you. But please note that this will lead to non-total function, e.g. it will have to return an error in case of call with Nothing, so you should avoid writing and using functions like this.
A tiny nitpick: you can get a total function if you return a default value instead of erroring out.
What default value would you return for the function `Maybe a -> a`? - Adam
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?
The IO in IO Int means that instead of just returning an Int, a computation may produce a side-effect (like writing a string to standard output). Since reasoning about code without side-effects is much simpler, you want to separate it from impure code. That's what IO is for. Once you're inside IO, you're dealing with impure code, so you want to keep track of things that rely on it. You can't* and don't want to escape.
* As Konstantine points out, there are legitimate cases for using unsafePerformIO, like writing an FFI binding to a pure function. Haskell type system can't see whether the function in question is pure or not, so it's tagged with IO. However, if you know that it's pure, you can explicitly state that by using unsafePerformIO.
_______________________________________________ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://mail.haskell.org/cgi-bin/mailman/listinfo/haskell-cafe

A tiny nitpick: you can get a total function if you return a default value instead of erroring out.
What default value would you return for the function `Maybe a -> a`?
You can do it with unsafeCoerce, but there are restrictions. And that's not what I meant, sorry. I mentally substituted an a for an Int: f :: Maybe Int -> Int f (Just x) = x f Nothing = 0 And there's also the maybe function.

But this is exactly the point: there can be no total, generic function of type f :: Monad m => m a -> a (though there is for a Comonad ;)) As for Zongzhe Yuan's original question, i thinks it's safe to think of a value of type (IO a) as a program that, when executed, will result in a value of type a. This program can be copied, stored in a variable, passed to another thread, etc. It will only be actually executed when (perhaps after being combined with other IO programs) bound to the main program. Kind regards, Macin Mrotek
participants (5)
-
Adam Bergmark
-
Konstantine Rybnikov
-
Marcin Mrotek
-
Nikita Karetnikov
-
Zongzhe Yuan