IO
monadicon.icon
code:io.hs
data IO a --abstract
The type of plans to perform effects that ultimately yield an a .
Evaluation does not trigger the actual effects. It will at most evaluate the plan.
Execution triggers the actual effects. Executing a plan is not possible from within a Haskell program.
The unit type
data () = ()
A type with a single value (nullary tuple).
Often used to parameterize other types.
A plan for actions with no interesting result: IO ()
Optioning out of doing IO
return :: a -> IO a
An plan that when executed, perform no effects and returns
the given result.
Intuitively, IO a says that we may use effects to obtainan a . We are not required to.
On the other hand, a says that we must not use effects to obtain an a .
No escape from IO
There's no function such as
code:runIO.hs
runIO :: IO a -> a
There actually is one, called unsafePerformIO , but its use is generally not justified.
Summary
IO is a primitive type, and (>>=) and return for IO are primitive functions,
There is no (politically correct) function runIO :: IO a -> a , whereas for most other monads
There is a corresponding function, or at least some way to get an a out of the monad;
Values of IO a denote side-effecting programs that can be executed by the run-time system.
IO is about allowing real side effects to occur; the other types we have seen are entirely pure as far as Haskell is concerned, even though they capture a form of effects.