Search code examples
functional-programmingiomonadsside-effects

What are the alternative of monads to use IO in pure functional programming?


monads are described as the haskell solution to deal with IO. I was wondering if there were other ways to deal with IO in pure functional language.


Solution

  • What alternatives are there to monads for I/O in a pure functional language?

    I'm aware of two alternatives in the literature:

    • One is a so-called linear type system. The idea is that a value of linear type must be used exactly one time: you can't ignore it, and you can't use it twice. With this idea in mind, you give the state of the world an abstract type (e.g., World), and you make it linear. If I mark linear types with a star, then here are the types of some I/O operations:

      getChar :: World* -> (Char, World*)
      putChar :: Char -> World* -> World*
      

      and so on. The compiler arranges to make sure you never copy the world, and then it can arrange to compile code that updates the world in place, which is safe because there is only one copy.

      The uniqueness typing in the language Clean is based on linearity.

      This system has a couple of advantages; in particular, it doesn't enforce the total ordering on events that monads do. It also tends to avoid the "IO sin bin" you see in Haskell where all effectful computations are tossed into the IO monad and they all get totally ordered whether you want total order or not.

    • The other system I'm aware of predates monads and Clean and is based on the idea that an interactive program is a function from a (possibly infinite) sequence of requests to a (possibly infinite) sequence of responses. This system, which was called "dialogs", was pure hell to program. Nobody misses it, and it had nothing in particular to recommend it. Its faults are enumerated nicely in the paper that introduced monadic I/O (Imperative Functional Programming) by Wadler and Peyton Jones. This paper also mentions an I/O system based on continuations which was introduced by the Yale Haskell group but which was short-lived.