Personal tools

How to get rid of IO

From HaskellWiki

(Difference between revisions)
Jump to: navigation, search
(awkward squad)
m (Answer: copy editing: fix whitespace, remove acronyms, remove value judgments, correct grammar)
 
Line 6: Line 6:
 
== Answer ==
 
== Answer ==
   
You can get rid of it, but we don't tell you how, since it is certainly not what need.
+
You can get rid of it, but you almost certainly don't need to. The special safety belt of Haskell is that you cannot get rid of IO! Nonetheless, the biggest parts of Haskell programs are and should be non-IO functions. Applications using both IO and non-IO functions are written by plugging together these two flavors of functions using functions like <hask>(>>=)</hask>. These functions allow useful IO while having all the safety properties of a pure [[functional programming]] language.
It is the special safety belt of Haskell, that you cannot get rid of IO!
+
Nonetheless, the biggest parts of Haskell programs are and should be non-IO functions.
+
You can hide some functions using [[do notation]], which looks like this:
Applications using both IO and non-IO functions are written
 
by plugging together these two flavors of functions
 
using atomic combinator functions like <hask>(>>=)</hask>.
 
These combinators are the great and elegant trick
 
that allow to do something useful with IO functions
 
while having all safety properties of a pure [[functional programming]] language.
 
If that scares you, you can hide the combinator using the [[do notation]],
 
which will looks quite conveniently like:
 
 
<haskell>
 
<haskell>
 
do text <- readFile "foo"
 
do text <- readFile "foo"
 
writeFile "bar" (someComplicatedNonIOOperation text)
 
writeFile "bar" (someComplicatedNonIOOperation text)
 
</haskell>
 
</haskell>
Btw. using the combinators this would look like
+
Without hiding the functions, this would look like:
 
<haskell>
 
<haskell>
 
writeFile "bar" . someComplicatedNonIOOperation =<< readFile "foo"
 
writeFile "bar" . someComplicatedNonIOOperation =<< readFile "foo"
Line 20: Line 20:
 
=== What we didn't tell you at the beginning ===
 
=== What we didn't tell you at the beginning ===
   
Btw. The function that answers your initial question is <hask>unsafePerformIO</hask>.
+
There is a function which directly answers the initial question, namely, <hask>unsafePerformIO</hask>.
 
It is however not intended for conveniently getting rid of the <hask>IO</hask> constructor.
 
It is however not intended for conveniently getting rid of the <hask>IO</hask> constructor.
 
It must only be used to wrap IO functions that behave like non-IO functions,
 
It must only be used to wrap IO functions that behave like non-IO functions,

Latest revision as of 15:17, 8 March 2013

Contents

[edit] 1 Question

I have something of type
IO a
, but I need something of type
a

How can I get that?

[edit] 2 Answer

You can get rid of it, but you almost certainly don't need to. The special safety belt of Haskell is that you cannot get rid of IO! Nonetheless, the biggest parts of Haskell programs are and should be non-IO functions. Applications using both IO and non-IO functions are written by plugging together these two flavors of functions using functions like
(>>=)
. These functions allow useful IO while having all the safety properties of a pure functional programming language.

You can hide some functions using do notation, which looks like this:

do text <- readFile "foo"
   writeFile "bar" (someComplicatedNonIOOperation text)

Without hiding the functions, this would look like:

writeFile "bar" . someComplicatedNonIOOperation =<< readFile "foo"

[edit] 2.1 What we didn't tell you at the beginning

There is a function which directly answers the initial question, namely,
unsafePerformIO
. It is however not intended for conveniently getting rid of the
IO
constructor.

It must only be used to wrap IO functions that behave like non-IO functions,

Since this property cannot be checked by the compiler, it is your task and thus the
unsafe
part of the name.

(Some library writers have abused that name component for partial functions. Don't get confused!) You will only need this in rare cases and only experienced programmers shall do this.

[edit] 3 See also