# Difference between revisions of "Avoiding IO"

m |
(randomIO) |
||

Line 44: | Line 44: | ||

== State monad == |
== State monad == |
||

− | randomIO |
||

+ | If you want to maintain a running state, it is tempting to use <hask>IORef</hask>. |
||

+ | But this is not necessary, since there is the comfortable <hask>State</hask> monad and its transformer counterpart. |
||

+ | |||

+ | Another example is random number generation. |
||

+ | In cases where no real random numbers are required, but only arbitrary numbers, |
||

+ | you do not need access to the outside world. |
||

+ | You can simply use a pseudo random number generator with an explicit state. |
||

+ | This state can be hidden in a State monad. |
||

+ | |||

+ | Example: A function which computes a random value |
||

+ | with respect to a custom distribution |
||

+ | (<hask>distInv</hask> is the inverse of the distribution function) |
||

+ | can be defined via IO |
||

+ | <haskell> |
||

+ | randomDist :: (Random a, Num a) => (a -> a) -> IO a |
||

+ | randomDist distInv = liftM distInv (randomRIO (0,1)) |
||

+ | </haskell> |
||

+ | but [[Humor/Erlkönig|there is no need to do so]]. |
||

+ | You don't need the state of the whole world |
||

+ | just for remembering the state of a random number generator. |
||

+ | What about |
||

+ | <haskell> |
||

+ | randomDist :: (RandomGen g, Random a, Num a) => (a -> a) -> State g a |
||

+ | randomDist distInv = liftM distInv (State (randomR (0,1))) |
||

+ | </haskell> |
||

+ | ? You can get actual values by running the <hask>State</hask> as follows: |
||

+ | <haskell> |
||

+ | evalState (randomDist distInv) (mkStdGen an_arbitrary_seed) |
||

+ | </haskell> |
||

+ | |||

== ST monad == |
== ST monad == |

## Revision as of 16:31, 25 December 2008

Haskell requires an explicit type for operations involving input and output.
This way it makes a problem explicit, that exists in every language:
Input and output functions can have so many effects, that the type signature says more or less that almost everything must be expected.
It is hard to test them, because they can in principle depend on every state of the real world.
Thus in order to maintain modularity you should avoid IO whereever possible.
It is too tempting to get rid of IO by `unsafePerformIO`

,
but we want to present some clean techniques to avoid IO.

## Lazy construction

You can avoid a series of output functions by constructing a complex data structure with non-IO code and output it with one output function.

Instead of

```
-- import Control.Monad (replicateM_)
replicateM_ 10 (putStr "foo")
```

you can also create the complete string and output it with one call of `putStr`

:

```
putStr (concat $ replicate 10 "foo")
```

Similarly,

```
do
h <- openFile "foo" WriteMode
replicateM_ 10 (hPutStr h "bar")
hClose h
```

can be shortened to

```
writeFile "foo" (concat $ replicate 10 "bar")
```

which also ensures proper closing of the handle `h`

in case of failure.

Since you have now an expression for the complete result as string,
you have a simple object that can be re-used in other contexts.
E.g. you can also easily compute the length of the written string using `length`

without bothering the file system, again.

## State monad

If you want to maintain a running state, it is tempting to use `IORef`

.
But this is not necessary, since there is the comfortable `State`

monad and its transformer counterpart.

Another example is random number generation. In cases where no real random numbers are required, but only arbitrary numbers, you do not need access to the outside world. You can simply use a pseudo random number generator with an explicit state. This state can be hidden in a State monad.

Example: A function which computes a random value
with respect to a custom distribution
(`distInv`

is the inverse of the distribution function)
can be defined via IO

```
randomDist :: (Random a, Num a) => (a -> a) -> IO a
randomDist distInv = liftM distInv (randomRIO (0,1))
```

but there is no need to do so. You don't need the state of the whole world just for remembering the state of a random number generator. What about

```
randomDist :: (RandomGen g, Random a, Num a) => (a -> a) -> State g a
randomDist distInv = liftM distInv (State (randomR (0,1)))
```

? You can get actual values by running the `State`

as follows:

```
evalState (randomDist distInv) (mkStdGen an_arbitrary_seed)
```

## ST monad

STRef instead of IORef, STArray instead of IOArray

## Custom type class

example getText</hask>