Why IOString != String In Haskell

In GHCi, I want to test my headRev function, while loading Mercurial log from a file and pass the content to the headRev function:

log = readFile "hg.log"
headRev log

I get a error message complaining "IO String is not [Char]":

Couldn't match expected type [Char] with actual type IOString In the first argument of `headRev`, namely `log` In the expression: headRev log In an equation for it: it = headRev log

Why IOString is not compatible with String? Why is it designed like this? After googled the error, here is a insightful reason to this question.

There is a very good reason why there is no such function.

Haskell has the notion of functional purity. This means that a function will always return the same result when called with the same parameters. The only place where IO is allowed is inside the IO Monad. 1

And BTW how to "walk" around it while you want to test in GHCi? I'm doing it this way:

Define interactWith function in source code, compile, and load via :l *hgrev

interactWith function [inputFile] = do input <- readFile inputFile putStrLn (function input)

Then in GHCi:

let f log = headRev (lines log) interactWith f "hg.log"

Footnotes:

HomeTop