Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<blockquote> <p>What alternatives are there to monads for I/O in a pure functional language?</p> </blockquote> <p>I'm aware of two alternatives in the literature:</p> <ul> <li><p>One is a so-called <em>linear type system</em>. The idea is that a value of linear type must be used <em>exactly</em> 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., <code>World</code>), and you make it linear. If I mark linear types with a star, then here are the types of some I/O operations:</p> <pre><code>getChar :: World* -&gt; (Char, World*) putChar :: Char -&gt; World* -&gt; World* </code></pre> <p>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.</p> <p>The uniqueness typing in the language <a href="http://clean.cs.ru.nl/" rel="noreferrer">Clean</a> is based on linearity.</p> <p>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 "<code>IO</code> sin bin" you see in Haskell where <em>all</em> effectful computations are tossed into the <code>IO</code> monad and they all get totally ordered whether you want total order or not.</p></li> <li><p>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 <em>requests</em> to a (possibly infinite) sequence of <em>responses</em>. 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 <a href="http://homepages.inf.ed.ac.uk/wadler/papers/imperative/imperative.ps" rel="noreferrer">the paper that introduced monadic I/O (<em>Imperative Functional Programming</em>)</a> 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.</p></li> </ul>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload