Home > OS >  How to Construct and Transform IO actions without using Monads
How to Construct and Transform IO actions without using Monads

Time:11-14

In the book "Haskell programming from first principles" it is said that:

Monads are not, strictly speaking, necessary to Haskell. Although the current standard for Haskell does use monad for constructing and transforming IO actions, older implementations of Haskell did not. Monads are powerful and fun, but they do not define Haskell. Rather, monads are defined in terms of Haskell.

How did older implementations of Haskell constructed and transformed IO actions without using Monads?

CodePudding user response:

According to A History of Haskell: Being Lazy with Class (see Section 7), two main approaches were stream-based IO and continuation-based IO.

In stream-based IO, your main program is a function [Response] -> [Request], to which the run-time system supplies a magic lazy input list. Then the output is evaluated, to see what request the program produces; some system call is made and the response is fed into the input list. You do need to be careful when writing main that it does not look at its inputs before having output a corresponding request.

In continuation-based IO, there is an abstract type of Behaviour (which in particular was defined as the above [Response] -> [Request] type, but the point is it doesn't have to be), and IO primitives take a continuation to be applied to their results.

Behaviour :: Type

getLine :: (String -> Behaviour) -> Behaviour
putStrLn :: String -> (() -> Behaviour) -> Behaviour  -- or equivalently,  String -> Behaviour -> Behaviour
  • Related