Revision as of 04:42, 13 January 2009

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 wherever possible.

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.

2 Writer monad

If the only reason that you need IO is to output information (e.g. logging, collecting statistics), a Writer monad might do the job.
This technique works just fine with lazy construction, especially if the lazy object that you need to create is a Monoid.

3 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

and you can define new operations in ST, but then you need to resort to unsafe operations.
You can escape from ST to non-monadic code in a safe, and in many cases efficient, way.

5 Custom monad type class

If you only use a small set of IO operations in otherwise non-IO code
you may define a custom monad type class which implements just these functions.
You can then implement these functions based on IO for the application and without IO for the test suite.

As an example consider the function

localeTextIO ::String->IOString

which converts an English phrase to the currently configured user language of the system.