Personal tools

How to get rid of IO

From HaskellWiki

(Difference between revisions)
Jump to: navigation, search
(Small text about that really FAQ)
(awkward squad)
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 we don't tell you how, since it is certainly not what need.
 
It is the special safety belt of Haskell, that you cannot get rid of IO!
 
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.
 
Nonetheless, the biggest parts of Haskell programs are and should be non-IO functions.
This is achieved by plugging together IO functions and non-IO functions
+
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>.
 
using atomic combinator functions like <hask>(>>=)</hask>.
If that scares you, you can hide that in the [[do notation]],
+
These combinators are the great and elegant trick
which will then look quite conveniently as:
+
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"
Line 33: Line 33:
 
== See also ==
 
== See also ==
   
  +
* [[Introduction to IO]]
 
* [[Avoiding IO]] - Avoiding IO in the first place is a good thing, and we tell you how to achieve that
 
* [[Avoiding IO]] - Avoiding IO in the first place is a good thing, and we tell you how to achieve that
  +
* [[Tutorials#Practical_Haskell|Tackling the awkward squad]]
 
* http://www.haskell.org/wikisnapshot/ThatAnnoyingIoType.html
 
* http://www.haskell.org/wikisnapshot/ThatAnnoyingIoType.html
 
* http://www.haskell.org/wikisnapshot/UsingIo.html
 
* http://www.haskell.org/wikisnapshot/UsingIo.html

Revision as of 09:19, 28 April 2009

Contents

1 Question

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

How can I get that?

2 Answer

You can get rid of it, but we don't tell you how, since it is certainly not what need. 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. Applications using both IO and non-IO functions are written by plugging together these two flavors of functions

using atomic combinator functions like
(>>=)
.

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:

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

Btw. using the combinators this would look like

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

2.1 What we didn't tell you at the beginning

Btw. The function that answers your initial question is
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.

3 See also