<p>I'm writing a backend for lazy lambda calculus. Now I'm curious about how it could interface with the system. As an experiment, I managed to write READ and WRITE primitives, each of which does the following:</p>
<ul>
<li>READ: expands into a lazy list of bits read from stdin</li>
<li>WRITE: takes a lazy list of bits and writes to stdout as soon as each bit is available</li>
</ul>
<p>However, these don't generalize well with other system interfaces (e.g. system timer, filesystem, etc), especially as each action needs to be done sequentially, and different types of actions can be interleaved. What are some possible approaches to such a system interface, built into a lazy LC runtime?</p>

<functional-programming><runtime><lambda-calculus>
Possible ways for a system interface in a lazy LC language?
CC BY-SA 4.0
2023-07-21T17:56:24.637Z
You need to introduce a dependency between actions? you can only call a write after a aread, so you add a state variable to your system functions and let them return a new state, something like this ? (e.g. a bit like an IO monad) Do you have an example of something you would like to express that is hard to do?
coredump
<h1>The standard approach: monadic I/O</h1>
<p>Modern pure, functional programming languages have overwhelmingly standardized on monadic approaches for embedding sequential programs into a language that otherwise may not precisely specify an evaluation order. The essential idea is to include the following definitions:</p>
<pre class="lang-hs prettyprint-override"><code>type IO : Type -&gt; Type
pure : a -&gt; IO a
bind : IO a -&gt; (a -&gt; IO b) -&gt; IO b
</code></pre>
<p>The <code>pure</code> operation is sometimes called <code>return</code>, and the <code>bind</code> operation is often named <code>&gt;&gt;=</code> (which is an infix operator) or, less commonly, <code>flatMap</code>. Additionally, most languages with monadic I/O include some version of <a href="https://www.haskell.org/onlinereport/haskell2010/haskellch3.html#x8-470003.14" rel="noreferrer">Haskell’s <code>do</code> notation</a> to provide some syntactic sugar for monadic programs.</p>
<p>The best way to think about the meaning of a value of type <code>IO a</code> is as a sequential “recipe” (or program) for producing a value of type <code>a</code>. These recipes can include side-effectful steps like the ones you mention in your question. These can be exposed as primitives with types like the following:</p>
<pre class="lang-hs prettyprint-override"><code>readByte : IO (Maybe Byte)
writeByte : Byte -&gt; IO Unit
</code></pre>
<p>The language’s runtime knows how to interpret these recipes, but there is no safe way to convert a value of type <code>IO a</code> to a value of type <code>a</code> within the language. Instead, programs define a <code>main</code> binding that serves as the program’s entry point:</p>
<pre class="lang-hs prettyprint-override"><code>main : IO Unit
</code></pre>
<p>The compiler arranges so that this value is evaluated when the program is invoked, and the resulting recipe is then sequentially <em>executed</em>.</p>
<h2>Implementation</h2>
<p>There are various possible ways to implement the above interface. One way would be to actually reify an action of type <code>IO a</code> as a big <a href="https://en.wikipedia.org/wiki/Generalized_algebraic_data_type" rel="noreferrer">generalized algebraic datatype</a> representing all possible actions:</p>
<pre class="lang-hs prettyprint-override"><code>data IO : Type -&gt; Type where
  Pure : a -&gt; IO a
  Bind : IO a -&gt; (a -&gt; IO b) -&gt; IO b
  ReadByte : IO (Maybe Byte)
  WriteByte : Byte -&gt; IO Unit
  ...
</code></pre>
<p>The runtime could then include an interpreter for this datatype. However, this is not usually how things are done.</p>
<p>The more common approach is to represent values of type <code>IO</code> as <em>functions</em>:</p>
<pre class="lang-hs prettyprint-override"><code>data IO a = IO (RealWorld -&gt; (a, RealWorld))
</code></pre>
<p>This is equivalent to a state monad where the state is <code>RealWorld</code>. This is a somewhat amusing idea, as it suggests that each <code>IO</code> action really is pure, and it simply functionally updates the <code>RealWorld</code> as necessary and returns a new one. Of course, this is not really possible—we cannot split the <code>RealWorld</code>!—so the <code>RealWorld</code> value must be used <em>linearly</em>; that is, it must always be consumed exactly once. This could be guaranteed using <a href="https://en.wikipedia.org/wiki/Substructural_type_system" rel="noreferrer">linear types</a>, but in most implementations, the internals of <code>IO</code> are simply not exposed, and the monadic interface maintains the linearity invariant.</p>
<p>But what actually <em>is</em> a value of type <code>RealWorld</code>? Semantically, it is simply a unique token representing the state of the real world. Operationally, it is nothing at all: in GHC, a value of type <code>RealWorld</code> is a <a href="https://doc.rust-lang.org/nomicon/exotic-sizes.html#zero-sized-types-zsts" rel="noreferrer">zero-sized type</a> that takes up no bytes in memory. However, somewhat counterintuitively, it can sometimes still be necessary to consider these values as “real” function arguments even at runtime, as applying a function that accepts a <code>RealWorld</code> token actually executes its side-effects, while simply evaluating the function does not.</p>
<p>For an in-depth guide on how all these pieces really fit together, see <a href="https://doi.org/10.1017/S0956796800000319" rel="noreferrer">Implementing lazy functional languages on stock hardware: the Spineless Tagless G-machine</a> (<a href="https://www.microsoft.com/en-us/research/wp-content/uploads/1992/04/spineless-tagless-gmachine.pdf" rel="noreferrer">pdf</a>), which is written from an implementor’s point of view and covers all the low-level details in truly exquisite detail.</p>
<h2>Adding lazy I/O</h2>
<p>The above approach works when you’d like to simply embed a sequential program into a lazy language, but your questions actually asks for something slightly more than that. Specifically, consider your description of your <code>READ</code> operation:</p>
<blockquote>
<p>READ: expands into a <strong>lazy list of bits</strong> read from stdin</p>
</blockquote>
<p>Emphasis mine. What I’ve described above is not quite enough to provide this functionality, as an implementation in terms of <code>readByte</code> would require reading the entire input stream before proceeding to the next monadic action. What you want is <em>lazy I/O</em>, which allows an <code>IO</code> action to return a value with unevaluated subcomponents that cause further <code>IO</code> actions to be executed when they are forced.</p>
<p>It’s worth stating that, in Haskell, <strong>many people consider lazy I/O to have been a bad idea.</strong> The problem is that it can make it possible to observe the order of evaluation of pure code! For example, consider <a href="https://stackoverflow.com/a/13129263">this answer on Stack Overflow</a>, which provides the following program:</p>
<pre class="lang-hs prettyprint-override"><code>wrong = do
  fileData &lt;- withFile &quot;test.txt&quot; ReadMode hGetContents
  putStr fileData
</code></pre>
<p>In Haskell, <code>hGetContents</code> uses lazy I/O, so it returns a <code>String</code> that will read the file when the value is forced. The problem is that <code>withFile</code> closes the file handle when it returns, and <code>fileData</code> has not yet been forced, so this program will attempt to read data from a closed handle, which fails. Even worse examples can involve interleaved reads from a single handle that depend on order of evaluation.</p>
<p>All that said, lazy I/O is simply so <em>convenient</em> that it does get used, anyway, so it can be useful to provide a way to express it. In Haskell, this is implemented via the following primitive:</p>
<pre class="lang-hs prettyprint-override"><code>unsafeInterleaveIO : IO a -&gt; IO a
</code></pre>
<p>This primitive accepts an <code>IO</code> action and returns an action that, when executed, returns an unevaluated thunk. When that thunk is forced, the provided action will be executed to produce its value. Using this primitive, we can implement your <code>READ</code> operation in terms of <code>readByte</code>:</p>
<pre class="lang-hs prettyprint-override"><code>getAllBytes : IO [Byte]
getAllBytes = unsafeInterleaveIO (do
  maybe_b &lt;- readByte
  case maybe_b of
    Nothing -&gt; pure []
    Just b -&gt; do
      bs &lt;- getAllBytes
      pure (Cons b bs))
</code></pre>
<p>This is an iteratively recursive function that uses <code>unsafeInterleaveIO</code> on each iteration, so the recursive call will immediately return a thunk, and the result is a lazy stream of bytes.</p>
<h1>The ancient approach: dialogue I/O</h1>
<p>Before the development of monadic I/O, Haskell used a different approach to I/O known as <em>dialogue I/O</em>. This approach is now generally regarded as drastically inferior to monadic I/O, so I do not recommend actually implementing it. However, it can be useful to know about if simply to illustrate a plausible approach that we now know doesn’t work out well in practice.</p>
<p>A good overview of dialogue I/O is discussed in <a href="https://stackoverflow.com/a/17004448">this Stack Overflow answer</a>, so I won’t go into depth here. The essential idea is that, instead of having an explicit <code>IO</code> type, the <code>main</code> entry point is given the following (somewhat curious) type:</p>
<pre class="lang-hs prettyprint-override"><code>main : [Response] -&gt; [Request]
</code></pre>
<p>The idea is that <code>main</code> should lazily evaluate to a list of actions to perform, and its argument is a lazy list of results of those actions. The obvious problem to this approach is that if a program ever forces the list of responses too far, before producing the necessary requests, the program can only abort or hang. The monadic approach is much preferred because it <em>locally</em> connects each request to the code that will receive the response, which both avoids this problem and makes programs much easier to understand.</p>

CC BY-SA 4.0
2023-07-22T18:35:07.580Z
2023-07-22T18:35:07.580Z
Alexis King
CC BY-SA 4.0
2023-07-24T23:50:23.400Z
For future reference: I couldn't find the implementation details of I/O primitives in the STG paper, but instead found it in [Imperative Functional Programming (pdf)](https://www.microsoft.com/en-us/research/wp-content/uploads/1993/01/imperative.pdf).
Bubbler
# Possible ways for a system interface in a lazy LC language?
Posted by: username_0 on 2023-07-21
I'm writing a backend for lazy lambda calculus. Now I'm curious about how it could interface with the system. As an experiment, I managed to write READ and WRITE primitives, each of which does the following:

* READ: expands into a lazy list of bits read from stdin
* WRITE: takes a lazy list of bits and writes to stdout as soon as each bit is available

However, these don't generalize well with other system interfaces (e.g. system timer, filesystem, etc), especially as each action needs to be done sequentially, and different types of actions can be interleaved. What are some possible approaches to such a system interface, built into a lazy LC runtime?
**Tags:** functional-programming runtime lambda-calculus
> **Comments:**
> **username_1**: You need to introduce a dependency between actions? you can only call a write after a aread, so you add a state variable to your system functions and let them return a new state, something like this ? (e.g. a bit like an IO monad) Do you have an example of something you would like to express that is hard to do?  
---
## 1 answers
### Answer by username_2 on 2023-07-22. Score: 8 | Accepted answer
The standard approach: monadic I/O
Modern pure, functional programming languages have overwhelmingly standardized on monadic approaches for embedding sequential programs into a language that otherwise may not precisely specify an evaluation order. The essential idea is to include the following definitions:
type IO : Type -> Type
pure : a -> IO a
bind : IO a -> (a -> IO b) -> IO b

The pure operation is sometimes called return, and the bind operation is often named >>= (which is an infix operator) or, less commonly, flatMap. Additionally, most languages with monadic I/O include some version of Haskell’s do notation to provide some syntactic sugar for monadic programs.
The best way to think about the meaning of a value of type IO a is as a sequential “recipe” (or program) for producing a value of type a. These recipes can include side-effectful steps like the ones you mention in your question. These can be exposed as primitives with types like the following:
readByte : IO (Maybe Byte)
writeByte : Byte -> IO Unit

The language’s runtime knows how to interpret these recipes, but there is no safe way to convert a value of type IO a to a value of type a within the language. Instead, programs define a main binding that serves as the program’s entry point:
main : IO Unit

The compiler arranges so that this value is evaluated when the program is invoked, and the resulting recipe is then sequentially executed.
Implementation
There are various possible ways to implement the above interface. One way would be to actually reify an action of type IO a as a big generalized algebraic datatype representing all possible actions:
data IO : Type -> Type where
  Pure : a -> IO a
  Bind : IO a -> (a -> IO b) -> IO b
  ReadByte : IO (Maybe Byte)
  WriteByte : Byte -> IO Unit
  ...

The runtime could then include an interpreter for this datatype. However, this is not usually how things are done.
The more common approach is to represent values of type IO as functions:
data IO a = IO (RealWorld -> (a, RealWorld))

This is equivalent to a state monad where the state is RealWorld. This is a somewhat amusing idea, as it suggests that each IO action really is pure, and it simply functionally updates the RealWorld as necessary and returns a new one. Of course, this is not really possible—we cannot split the RealWorld!—so the RealWorld value must be used linearly; that is, it must always be consumed exactly once. This could be guaranteed using linear types, but in most implementations, the internals of IO are simply not exposed, and the monadic interface maintains the linearity invariant.
But what actually is a value of type RealWorld? Semantically, it is simply a unique token representing the state of the real world. Operationally, it is nothing at all: in GHC, a value of type RealWorld is a zero-sized type that takes up no bytes in memory. However, somewhat counterintuitively, it can sometimes still be necessary to consider these values as “real” function arguments even at runtime, as applying a function that accepts a RealWorld token actually executes its side-effects, while simply evaluating the function does not.
For an in-depth guide on how all these pieces really fit together, see Implementing lazy functional languages on stock hardware: the Spineless Tagless G-machine (pdf), which is written from an implementor’s point of view and covers all the low-level details in truly exquisite detail.
Adding lazy I/O
The above approach works when you’d like to simply embed a sequential program into a lazy language, but your questions actually asks for something slightly more than that. Specifically, consider your description of your READ operation:

READ: expands into a lazy list of bits read from stdin

Emphasis mine. What I’ve described above is not quite enough to provide this functionality, as an implementation in terms of readByte would require reading the entire input stream before proceeding to the next monadic action. What you want is lazy I/O, which allows an IO action to return a value with unevaluated subcomponents that cause further IO actions to be executed when they are forced.
It’s worth stating that, in Haskell, many people consider lazy I/O to have been a bad idea. The problem is that it can make it possible to observe the order of evaluation of pure code! For example, consider this answer on Stack Overflow, which provides the following program:
wrong = do
  fileData <- withFile "test.txt" ReadMode hGetContents
  putStr fileData

In Haskell, hGetContents uses lazy I/O, so it returns a String that will read the file when the value is forced. The problem is that withFile closes the file handle when it returns, and fileData has not yet been forced, so this program will attempt to read data from a closed handle, which fails. Even worse examples can involve interleaved reads from a single handle that depend on order of evaluation.
All that said, lazy I/O is simply so convenient that it does get used, anyway, so it can be useful to provide a way to express it. In Haskell, this is implemented via the following primitive:
unsafeInterleaveIO : IO a -> IO a

This primitive accepts an IO action and returns an action that, when executed, returns an unevaluated thunk. When that thunk is forced, the provided action will be executed to produce its value. Using this primitive, we can implement your READ operation in terms of readByte:
getAllBytes : IO [Byte]
getAllBytes = unsafeInterleaveIO (do
  maybe_b <- readByte
  case maybe_b of
    Nothing -> pure []
    Just b -> do
      bs <- getAllBytes
      pure (Cons b bs))

This is an iteratively recursive function that uses unsafeInterleaveIO on each iteration, so the recursive call will immediately return a thunk, and the result is a lazy stream of bytes.
The ancient approach: dialogue I/O
Before the development of monadic I/O, Haskell used a different approach to I/O known as dialogue I/O. This approach is now generally regarded as drastically inferior to monadic I/O, so I do not recommend actually implementing it. However, it can be useful to know about if simply to illustrate a plausible approach that we now know doesn’t work out well in practice.
A good overview of dialogue I/O is discussed in this Stack Overflow answer, so I won’t go into depth here. The essential idea is that, instead of having an explicit IO type, the main entry point is given the following (somewhat curious) type:
main : [Response] -> [Request]

The idea is that main should lazily evaluate to a list of actions to perform, and its argument is a lazy list of results of those actions. The obvious problem to this approach is that if a program ever forces the list of responses too far, before producing the necessary requests, the program can only abort or hang. The monadic approach is much preferred because it locally connects each request to the code that will receive the response, which both avoids this problem and makes programs much easier to understand.
> **Comments:**
> **username_0** (2023-07-24): For future reference: I couldn't find the implementation details of I/O primitives in the STG paper, but instead found it in [Imperative Functional Programming (pdf)](https://www.microsoft.com/en-us/research/wp-content/uploads/1993/01/imperative.pdf).  
---
