Difference between revisions of "Output/Input"

From HaskellWiki
Jump to: navigation, search
m (Extra article added)
(Article rewritten)
 
(23 intermediate revisions by the same user not shown)
Line 1: Line 1:
 
[[Category:Theoretical foundations]]
 
[[Category:Theoretical foundations]]
  
=== <u>Clearing away the smoke and mirrors</u> ===
+
<div style="border-left:1px solid lightgray; padding: 1em" alt="blockquote">
 +
There is a world of difference between the value <code>echoML</code> which has no side effects when evaluated, and the computation <code>echoML ()</code>, which does.
 +
 
 +
<tt>[https://citeseerx.ist.psu.edu/viewdoc/download?doi=10.1.1.91.3579&rep=rep1&type=pdf How to Declare an Imperative], Philip Wadler (page 25 of 33).</tt>
 +
</div>
  
<div style="border-left:1px solid lightgray; padding: 1em" alt="blockquote">
+
Of course, that assumes everything in the program's <i>“world”</i> required by <code>echoML</code> actually <b>works as intended</b>: reality isn't always so obliging! To then define Haskell's I/O type as:
The implementation in GHC uses the following one:
 
  
 +
<div style="border-left:1px solid lightgray; padding: 1em" alt="blockquote">                                                                         
 
<haskell>
 
<haskell>
 
type IO a  =  World -> (a, World)
 
type IO a  =  World -> (a, World)
 
</haskell>
 
</haskell>
  
An <code>IO</code> computation is a function that (logically) takes the state of the world, and returns a modified world as well as the return value. Of course, GHC does not actually pass the world around; instead, it passes a dummy “token,” to ensure proper sequencing of actions in the presence of lazy evaluation, and performs input and output as actual side effects!
+
[where an] <code>IO</code> computation is a function that (logically) takes the state of the world, and returns a modified world as well as the return value.
  
 
<tt>[https://citeseerx.ist.psu.edu/viewdoc/download?doi=10.1.1.168.4008&rep=rep1&type=pdf A History of Haskell: Being Lazy With Class], Paul Hudak, John Hughes, Simon Peyton Jones and Philip Wadler.</tt>
 
<tt>[https://citeseerx.ist.psu.edu/viewdoc/download?doi=10.1.1.168.4008&rep=rep1&type=pdf A History of Haskell: Being Lazy With Class], Paul Hudak, John Hughes, Simon Peyton Jones and Philip Wadler.</tt>
 
</div>
 
</div>
  
...so what starts out as an I/O action of type:
+
...seems rather optimistic.
 +
<sup> </sup>
  
<haskell>
+
----
World -> (a, World)
+
=== <u>The determining of <code>IO</code></u> ===
</haskell>
 
  
is changed by GHC to approximately:
+
Since reality isn't always so deterministic in behaviour, perhaps the study of <i>nondeterminism</i> can provide a more plausible implementation:
  
<haskell>
+
<div style="border-left:1px solid lightgray; padding: 1em" alt="blockquote">
() -> (a, ())
+
We propose to solve this problem [existing constructs for nondeterminism not being compatible with the mathematical foundations of functional programming] by placing the nondeterminism in pseudo-data. A program is passed an infinite tree of two-valued <code>decisions</code>, along with its input. These <code>decisions</code> may be fixed at runtime, thereby permitting nondeterminism. Once fixed, a <code>decision</code> remains unchanged so equivalent expression must always have the same value.
</haskell>
 
  
As the returned unit-value <code>()</code> contains no useful information, that type can be simplified further:
+
<tt>[https://academic.oup.com/comjnl/article-pdf/31/3/243/1157325/310243.pdf Nondeterminism with Referential Transparency in Functional Programming Languages], F. Warren Burton (front page).</tt>
 +
</div>
  
<haskell>
+
There's more:
() -> a
 
</haskell>
 
  
<sub>Why "approximately"? Because "logically" a function in Haskell has no observable effects.</sub>
+
<div style="border-left:1px solid lightgray; padding: 1em" alt="blockquote">
 +
The approach generalizes so that a program can make use of other run-time information such as the current time or current amount of available storage.
 +
</div>
  
----
+
Using this <i>pseudo-data</i> approach:
=== <u>Previously seen</u> ===
 
  
The type <code>() -> a</code> (or variations of it) have appeared elsewhere - examples include:
+
<haskell>
 +
-- abstract; single-use I/O-access mediator
 +
data Exterior
 +
getchar :: Exterior -> Char
 +
putchar :: Char -> Exterior -> ()
  
* page 2 of 13 in [https://fi.ort.edu.uy/innovaportal/file/20124/1/22-landin_correspondence-between-algol-60-and-churchs-lambda-notation.pdf A Correspondence Between ALGOL 60 and Church's Lambda-Notation: Part I] by Peter Landin:
+
-- from section 2 of Burton's paper
:{|
+
data Tree a = Node { contents :: a,
|<div style="border-left:1px solid lightgray; padding: 1em" alt="blockquote">
+
                    left    :: Tree a,
 +
                    right    :: Tree a }
  
The use of <code>λ</code>, and in particular (to avoid an irrelevant bound variable) of <code>λ()</code> , to delay and possibly avoid evaluation is exploited repeatedly in our model of ALGOL 60. A function that requires an argument-list of length zero is called a ''none-adic'' function.
+
-- utility definitions
</div>
+
type OI  =  Tree Exterior
<sup> </sup>
 
<haskell>
 
(\ () -> …) :: () -> a
 
</haskell>
 
|}
 
  
* page 148 of 168 in [[IO Semantics|Functional programming and Input/Output]] by Andrew Gordon:
+
main'    :: OI -> ()
:{|
+
main'   = ...
|<div style="border-left:1px solid lightgray; padding: 1em" alt="blockquote">
 
<pre>
 
abstype 'a Job = JOB of unit -> 'a
 
</pre>
 
</div>
 
<sup> </sup>
 
<haskell>
 
data Job a = JOB (() -> a)
 
</haskell>
 
|}
 
  
* page 3 of [https://www.cs.bham.ac.uk/~udr/papers/assign.pdf Assignments for Applicative Languages] by Vipin Swarup, Uday S. Reddy and Evan Ireland:
+
getChar' :: OI -> Char
:{|
+
getChar' = getchar . contents
|<div style="border-left:1px solid lightgray; padding: 1em" alt="blockquote">
 
A value of type <code>Obs 𝜏</code> is called an ''observer''. Such a value observes (i.e. views or inspects) a state and returns a value of type <code>𝜏</code>. [...] An observer type <code>Obs 𝜏</code> may be viewed as an implicit function space from the set of states to the type <code>𝜏</code>.
 
</div>
 
<sup> </sup>
 
<haskell>
 
type Obs tau = State -> tau
 
</haskell>
 
|}
 
  
* [https://image.slidesharecdn.com/lazyio-120422092926-phpapp01/95/lazy-io-15-728.jpg page 15] of ''Non-Imperative Functional Programming'' by Nobuo Yamashita:
+
putChar' :: Char -> OI -> ()
 +
putChar' c = putchar c . contents
  
:{|
+
part    :: OI -> (OI, OI)
<haskell>
+
parts    :: OI -> [OI]
type a :-> b = OI a -> b
 
</haskell>
 
|}
 
  
* [http://h2.jaguarpaw.co.uk/posts/mtl-style-for-free MTL style for free] by Tom Ellis:
+
part t  =  (left t, right t)
 
+
parts t  = let !(t1, t2) = part t in
:{|
+
            t1 : parts t2
<haskell>
 
data Time_ a = GetCurrentTime (UTCTime -> a)
 
 
</haskell>
 
</haskell>
|}
 
  
* [http://h2.jaguarpaw.co.uk/posts/impure-lazy-language An impure lazy programming language], also by Tom Ellis:
+
To avoid the visible use of trees, the single-use property can instead be applied directly to <code>OI</code> values. This permits an abstract definition of the <code>OI</code> type:
  
:{|
 
 
<haskell>
 
<haskell>
data IO a = IO (() -> a)
+
data OI
 +
partOI  :: OI -> (OI, OI)
 +
getChar :: OI -> Char
 +
putChar :: Char -> OI -> ()
 
</haskell>
 
</haskell>
|}
 
  
* page 2 of [https://citeseerx.ist.psu.edu/viewdoc/download?doi=10.1.1.128.9269&rep=rep1&type=pdf Unique Identifiers in Pure Functional Languages] by Péter Diviánszky:
+
The choice to use (theoretically) infinite structured values (binary trees or otherwise) is then an implementation matter.
:{|
 
|<div style="border-left:1px solid lightgray; padding: 1em" alt="blockquote">
 
[...] The type <code>Id</code> can be hidden by the synonym data type
 
<pre>
 
:: Create a  :==  Id -> a
 
</pre>
 
</div>
 
 
<sup> </sup>
 
<sup> </sup>
<haskell>
 
type Create a = Id -> a
 
</haskell>
 
|}
 
  
* page 7 of [https://citeseerx.ist.psu.edu/viewdoc/download?doi=10.1.1.701.930&rep=rep1&type=pdf Functional Reactive Animation] by Conal Elliott and Paul Hudak:
+
----
:{|
+
=== <u>Other interfaces</u> ===
|<div style="border-left:1px solid lightgray; padding: 1em" alt="blockquote">
+
 
An early implementation of Fran represented behaviors as implied in the formal semantics:
+
In addition to the [[Monad|current]] one:
<haskell>
 
data Behavior a = Behavior (Time -> a)
 
</haskell>
 
</div>
 
|}
 
  
* page 26 of [https://citeseerx.ist.psu.edu/viewdoc/download?doi=10.1.1.91.3579&rep=rep1&type=pdf How to Declare an Imperative] by Philip Wadler:
 
:{|
 
|<div style="border-left:1px solid lightgray; padding: 1em" alt="blockquote">
 
The type <code>'a io</code> is represented by a function expecting a dummy argument of type <code>unit</code> and returning a value of type <code>'a</code>.
 
<pre>
 
type 'a io = unit -> a
 
</pre>
 
</div>
 
<sup> </sup>
 
 
<haskell>
 
<haskell>
type Io a = () -> a
+
type M a   = OI -> a
</haskell>
 
|}
 
  
* The [https://www.vex.net/~trebla/haskell/IO.xhtml Haskell I/O Tutorial] by Albert Lai:
+
unit      :: a -> M a
:{|
+
unit x    =  \ u -> let !_ = partOI u in x
|<div style="border-left:1px solid lightgray; padding: 1em" alt="blockquote">
 
But I can already tell you why we cannot follow other languages and use simply <code>X</code> or <code>() -> X</code>.
 
</div>
 
|}
 
  
* [http://comonad.com/reader/2011/free-monads-for-less-3 Free Monads for Less (Part 3 of 3): Yielding IO] by Edward Kmett:
+
bind      :: M a -> (a -> M b) -> M b
:{|
+
bind m k  = \ u -> let !(u1, u2) = partOI u in
|<div style="border-left:1px solid lightgray; padding: 1em" alt="blockquote">
+
                    let !x = m u1 in
<haskell>
+
                    let !y = k x u2 in
newtype OI a = forall o i. OI (FFI o i) o (i -> a) deriving Functor
+
                    y
</haskell>
 
</div>
 
<sup> </sup>
 
<haskell>
 
type Oi a = forall i . i -> a
 
</haskell>
 
|}
 
  
* page 27 of [https://blog.higher-order.com/assets/scalaio.pdf Purely Functional I/O in Scala] by Rúnar Bjarnason:
+
getcharM  :: M Char
:{|
+
getcharM  = getChar
|<div style="border-left:1px solid lightgray; padding: 1em" alt="blockquote">
 
<pre>
 
class IO[A](run: () => A)
 
</pre>
 
</div>
 
<sup> </sup>
 
<haskell>
 
class Io a where run :: () -> a
 
</haskell>
 
|}
 
  
* [https://stackoverflow.com/questions/6647852/haskell-actual-io-monad-implementation-in-different-language/6706442#6706442 ysdx's answer] to [https://stackoverflow.com/questions/6647852/haskell-actual-io-monad-implementation-in-different-language this SO question]:
+
putcharM  :: Char -> M ()
:{|
+
putcharM   = putChar
|<div style="border-left:1px solid lightgray; padding: 1em" alt="blockquote">
 
Let's say you want to implement <code>IO</code> in SML :
 
<pre>
 
structure Io : MONAD =
 
struct
 
   type 'a t = unit -> 'a
 
        ⋮
 
end
 
</pre>
 
</div>
 
<sup> </sup>
 
<haskell>
 
type T a = () -> a
 
 
</haskell>
 
</haskell>
|}
 
  
* [https://stackoverflow.com/questions/45136398/is-the-monadic-io-construct-in-haskell-just-a-convention/45141523#45141523 luqui's answer] to [https://stackoverflow.com/questions/45136398/is-the-monadic-io-construct-in-haskell-just-a-convention this SO question]:
+
the <code>OI</code> interface can be used to implement other models of I/O:
:{|
 
|<haskell>
 
newtype IO a = IO { runIO :: () -> a }
 
</haskell>
 
|}
 
  
* [https://stackoverflow.com/questions/15418075/the-reader-monad/15419592#15419592 luqui's answer] to [https://stackoverflow.com/questions/15418075/the-reader-monad this SO question]:
+
* [[Comonad|comonad]]:
:{|
 
|<haskell>
 
newtype Supply r a = Supply { runSupply :: r -> a }
 
</haskell>
 
|}
 
  
* [https://stackoverflow.com/questions/51770808/how-exactly-does-ios-work-under-the-hood/51772273#51772273 chi's answer] to [https://stackoverflow.com/questions/51770808/how-exactly-does-ios-work-under-the-hood this SO question]:
+
:<haskell>
:{|
+
type C a        = (OI, a)
|<div style="border-left:1px solid lightgray; padding: 1em" alt="blockquote">
 
As long as we have its special case <code>IO c = () ~> c</code>, we can represent (up to isomorphism) […] <code>a ~> c</code> […]
 
</div>
 
|}
 
  
Of these, it is the [https://hackage.haskell.org/package/oi/docs/src/Data-OI-Internal.html#OI implementation of <code>OI a</code>] in Yamashita's [https://hackage.haskell.org/package/oi oi] package which is most interesting as its values are ''monousal'' - once used, their contents remain constant. This single-use property also appears in the implementation of the abstract <code>decision</code> type described by F. Warren Burton in [https://academic.oup.com/comjnl/article-pdf/31/3/243/1157325/310243.pdf Nondeterminism with Referential Transparency in Functional Programming Languages].
+
extract          :: C a -> a
 +
extract (u, x)  =  let !_ = partOI u in x
  
----
+
duplicate        :: C a -> C (C a)
=== <code>IO</code><u>, redefined</u> ===
+
duplicate (u, x) = let !(u1, u2) = partOI u in
 +
                    (u2, (u1, x))
  
Based on these and other observations, a reasonable distillment of these examples would be <code>OI -> a</code>, which then implies:
+
extend          :: (C a -> b) -> C a -> C b
 +
extend h (u, x)  =  let !(u1, u2) = partOI u in
 +
                    let !y        = h (u1, x) in
 +
                    (u2, y)
  
<haskell>
+
getcharC        :: C () -> Char
type IO a = OI -> a
+
getcharC (u, ()) = getChar u
</haskell>
 
  
Using Burton's ''pseudodata'' approach:
+
putcharC        :: C Char -> ()
 +
putcharC (u, c)  =  putChar c u
  
<haskell>
+
</haskell>
-- abstract; single-use I/O-access mediator
 
data Exterior
 
getchar :: Exterior -> Char
 
putchar :: Char -> Exterior -> ()
 
  
-- from section 2 of Burton's paper
+
* [[Arrow|arrow]]:
data Tree a = Node { contents :: a,
 
                    left    :: Tree a,
 
                    right    :: Tree a }
 
  
  -- utility definitions
+
:<haskell>
type OI =  Tree Exterior
+
type A b c  = (OI -> b) -> (OI -> c)
  
getChar' :: OI -> Char
+
arr          :: (b -> c) -> A b c
getChar' = getchar . contents
+
arr f        =  \ c' u -> let !x = c' u in f x
  
putChar' :: Char -> OI -> ()
+
both        :: A b c -> A b' c' -> A (b, b') (c, c')
putChar' c = putchar c . contents
+
f' `both` g' =  \ c' u -> let !(u1:u2:u3:_) = partsOI u in
 +
                          let !(x, x')      = c' u1 in
 +
                          let !y            = f' (unit x) u2 in
 +
                          let !y'          = g' (unit x') u3 in
 +
                          (y, y')
 +
                where
 +
                  unit x u = let !_ = partOI u in x
  
part     :: OI -> (OI, OI)
+
getcharA     :: A () Char
parts    :: OI -> [OI]
+
getcharA    =  \ c' u -> let !(u1, u2) = partOI u in
 +
                          let !_        = c' u1 in
 +
                          let !ch      = getChar u2 in
 +
                          ch   
  
part t  =  (left t, right t)
+
putcharA    :: A Char ()
parts t  =  let !(t1, t2) = part t in
+
putcharA    \ c' u -> let !(u1, u2) = partOI u in
            t1 : parts t2
+
                          let !ch      = c' u1 in
 +
                          let !z        = putChar ch u2 in
 +
                          z
 
</haskell>
 
</haskell>
  
Of course, in an actual implementation <code>OI</code> would be abstract like <code>World</code>, and for similar reasons. This allows for a simpler implementation for <code>OI</code> and its values, instead of being based on (theoretically) infinite structured values like binary trees. That simplicity has benefits for the <code>OI</code> interface, in this case:
+
including [https://citeseerx.ist.psu.edu/viewdoc/download?doi=10.1.1.91.3579&rep=rep1&type=pdf those used in earlier versions] of Haskell:
 +
 
 +
* dialogues:
  
<haskell>
+
:<haskell>
data OI
+
runD :: ([Response] -> [Request]) -> OI -> ()
part :: OI -> (OI, OI)
+
runD d u = foldr (\ (!_) -> id) () $ yet $ \ l -> zipWith respond (d l) (partsOI u)
getChar' :: OI -> Char
 
putChar' :: Char -> OI -> ()
 
</haskell>
 
<sup> </sup>
 
  
----
+
yet :: (a -> a) -> a
 +
yet f = f (yet f)
  
=== <u>Various questions</u> ===
+
respond :: Request -> OI -> Response
 +
respond Getq    u = let !c = getChar u in Getp c
 +
respond (Putq c) u = let !_ = putChar c u in Putp
  
* Is the C language "purely functional"?
+
data Request  = Getq | Putq Char
 +
data Response = Getp Char | Putp
 +
</haskell>
  
::No:
+
* continuations:
::* C isn't "pure" - it allows unrestricted access to observable effects, including those of I/O.
 
::* C isn't "functional" - it was never intended to be [[Referential transparency|referentially transparent]], which severely restricts the ability to use [[Equational reasoning examples|equational reasoning]].
 
  
* Is the Haskell language "purely functional"?
+
:<haskell>
 +
type Answer = OI -> ()
  
::[https://chadaustin.me/2015/09/haskell-is-not-a-purely-functional-language Haskell is not a purely functional language], but is often described as being referentially transparent.
+
runK :: Answer -> IO -> ()
 +
runK a u = a u
  
* Can functional programming be liberated from the von Neumann paradigm?
+
doneK :: Answer
 +
doneK = \ u -> let !_ = partOI u in ()
  
::That remains an [[Open research problems|open research problem]].
+
getcharK :: (Char -> Answer) -> Answer
 +
getcharK k  = \ u -> let !(u1, u2) = partOI u in
 +
                      let !c        = getChar u1 in
 +
                      let !a        = k c in
 +
                      a u2
  
* Can a language be "purely functional" or "denotative"?
+
putcharK :: Char -> Answer -> Answer
 +
putcharK c a = \ u -> let !(u1, u2) = partOI u in
 +
                      let !_        = putChar c u1 in
 +
                      a u2
 +
</haskell>
  
::Conditionally, yes - the condition being the language is restricted in what domains it can be used in:
+
and even <i>that</i> <s><i>world</i></s> state-passing style, which is also used by [https://citeseerx.ist.psu.edu/viewdoc/download?doi=10.1.1.17.935&rep=rep1&type=pdf Clean], [https://staff.science.uva.nl/c.u.grelck/publications/HerhSchoGrelDAMP09.pdf Single-Assignment C] and as part of the I/O model used for the verification of interactive programs in [https://cakeml.org/vstte18.pdf CakeML], remembering that <code>OI</code> values can only be used once:
  
::* If a language is free of observable effects, including those of I/O, then the only other place where those effects can reside is within its implementation.
+
<haskell>
::* There is no bound on the ways in which observable effects can be usefully combined, leading to a similarly-unlimited variety of imperative computations.
+
newtype World = W OI
::* A finite implementation cannot possibly accommodate all of those computations, so a subset of them must be chosen. This restricts the implementation and language to those domains supported by the chosen computations.
 
  
* Why do our programs need to read input and write output?
+
getcharL :: World -> (Char, World)
 +
getcharL (W u) = let !(u1, u2) = partOI u in
 +
                let !c = getChar u1 in
 +
                (c, W u2)
  
::Because programs are usually written for [https://citeseerx.ist.psu.edu/viewdoc/download?doi=10.1.1.628.7053&rep=rep1&type=pdf practical] purposes, such as implementing domain-specific [https://citeseerx.ist.psu.edu/viewdoc/download?doi=10.1.1.7.2089&rep=rep1&type=pdf little languages] like [https://dhall-lang.org Dhall].
+
putcharL :: Char -> World -> World
 +
putcharL c (W u) = let !(u1, u2) = partOI u in
 +
                  let !_ = putChar u1 in
 +
                  W u2
 +
</haskell>
 +
<sup> </sup>
  
 
----
 
----
 
 
=== <u>See also</u> ===
 
=== <u>See also</u> ===
  
* [https://pqnelson.github.io/2021/07/29/monadic-io-in-ml.html Monadic <code>IO</code> in Standard ML]
+
* [[Plainly partible]]
 
* [[Disposing of dismissives]]
 
* [[Disposing of dismissives]]
* [[IO, partible-style]]
 
 
* [[IO then abstraction]]
 
* [[IO then abstraction]]
* [https://okmij.org/ftp/Computation/IO-monad-history.html The IO monad in 1965]
 

Latest revision as of 12:02, 28 November 2022


There is a world of difference between the value echoML which has no side effects when evaluated, and the computation echoML (), which does.

How to Declare an Imperative, Philip Wadler (page 25 of 33).

Of course, that assumes everything in the program's “world” required by echoML actually works as intended: reality isn't always so obliging! To then define Haskell's I/O type as:

type IO a  =  World -> (a, World)

[where an] IO computation is a function that (logically) takes the state of the world, and returns a modified world as well as the return value.

A History of Haskell: Being Lazy With Class, Paul Hudak, John Hughes, Simon Peyton Jones and Philip Wadler.

...seems rather optimistic.


The determining of IO

Since reality isn't always so deterministic in behaviour, perhaps the study of nondeterminism can provide a more plausible implementation:

We propose to solve this problem [existing constructs for nondeterminism not being compatible with the mathematical foundations of functional programming] by placing the nondeterminism in pseudo-data. A program is passed an infinite tree of two-valued decisions, along with its input. These decisions may be fixed at runtime, thereby permitting nondeterminism. Once fixed, a decision remains unchanged so equivalent expression must always have the same value.

Nondeterminism with Referential Transparency in Functional Programming Languages, F. Warren Burton (front page).

There's more:

The approach generalizes so that a program can make use of other run-time information such as the current time or current amount of available storage.

Using this pseudo-data approach:

 -- abstract; single-use I/O-access mediator
data Exterior
getchar :: Exterior -> Char
putchar :: Char -> Exterior -> ()

 -- from section 2 of Burton's paper
data Tree a = Node { contents :: a,
                     left     :: Tree a,
                     right    :: Tree a }

 -- utility definitions
type OI  =  Tree Exterior

main'    :: OI -> ()
main'    =  ...

getChar' :: OI -> Char
getChar' =  getchar . contents

putChar' :: Char -> OI -> ()
putChar' c = putchar c . contents

part     :: OI -> (OI, OI)
parts    :: OI -> [OI]

part t   =  (left t, right t)
parts t  =  let !(t1, t2) = part t in
            t1 : parts t2

To avoid the visible use of trees, the single-use property can instead be applied directly to OI values. This permits an abstract definition of the OI type:

data OI
partOI  :: OI -> (OI, OI)
getChar :: OI -> Char
putChar :: Char -> OI -> ()

The choice to use (theoretically) infinite structured values (binary trees or otherwise) is then an implementation matter.


Other interfaces

In addition to the current one:

type M a   =  OI -> a

unit       :: a -> M a
unit x     =  \ u -> let !_ = partOI u in x 

bind       :: M a -> (a -> M b) -> M b
bind m k   =  \ u -> let !(u1, u2) = partOI u in
                     let !x = m u1 in
                     let !y = k x u2 in
                     y

getcharM   :: M Char
getcharM   =  getChar

putcharM   :: Char -> M () 
putcharM   =  putChar

the OI interface can be used to implement other models of I/O:

type C a         =  (OI, a)

extract          :: C a -> a
extract (u, x)   =  let !_ = partOI u in x

duplicate        :: C a -> C (C a)
duplicate (u, x) =  let !(u1, u2) = partOI u in
                    (u2, (u1, x))

extend           :: (C a -> b) -> C a -> C b
extend h (u, x)  =  let !(u1, u2) = partOI u in
                    let !y        = h (u1, x) in
                    (u2, y)

getcharC         :: C () -> Char
getcharC (u, ()) =  getChar u

putcharC         :: C Char -> ()
putcharC (u, c)  =  putChar c u
type A b c   =  (OI -> b) -> (OI -> c)

arr          :: (b -> c) -> A b c
arr f        =  \ c' u -> let !x = c' u in f x

both         :: A b c -> A b' c' -> A (b, b') (c, c')
f' `both` g' =  \ c' u -> let !(u1:u2:u3:_) = partsOI u in
                          let !(x, x')      = c' u1 in
                          let !y            = f' (unit x) u2 in
                          let !y'           = g' (unit x') u3 in
                          (y, y')
                where
                  unit x u = let !_ = partOI u in x

getcharA     :: A () Char
getcharA     =  \ c' u -> let !(u1, u2) = partOI u in
                          let !_        = c' u1 in
                          let !ch       = getChar u2 in
                          ch     

putcharA     :: A Char ()
putcharA     =  \ c' u -> let !(u1, u2) = partOI u in
                          let !ch       = c' u1 in
                          let !z        = putChar ch u2 in
                          z

including those used in earlier versions of Haskell:

  • dialogues:
runD :: ([Response] -> [Request]) -> OI -> ()
runD d u = foldr (\ (!_) -> id) () $ yet $ \ l -> zipWith respond (d l) (partsOI u)

yet :: (a -> a) -> a
yet f = f (yet f)

respond :: Request -> OI -> Response
respond Getq     u = let !c = getChar u in Getp c
respond (Putq c) u = let !_ = putChar c u in Putp

data Request  = Getq | Putq Char
data Response = Getp Char | Putp
  • continuations:
type Answer = OI -> ()

runK :: Answer -> IO -> ()
runK a u = a u

doneK :: Answer
doneK = \ u -> let !_ = partOI u in ()

getcharK :: (Char -> Answer) -> Answer
getcharK k   = \ u -> let !(u1, u2) = partOI u in
                      let !c        = getChar u1 in
                      let !a        = k c in
                      a u2

putcharK :: Char -> Answer -> Answer
putcharK c a = \ u -> let !(u1, u2) = partOI u in
                      let !_        = putChar c u1 in
                      a u2

and even that world state-passing style, which is also used by Clean, Single-Assignment C and as part of the I/O model used for the verification of interactive programs in CakeML, remembering that OI values can only be used once:

newtype World = W OI

getcharL :: World -> (Char, World)
getcharL (W u) = let !(u1, u2) = partOI u in
                 let !c = getChar u1 in
                 (c, W u2)

putcharL :: Char -> World -> World
putcharL c (W u) = let !(u1, u2) = partOI u in
                   let !_ = putChar u1 in
                   W u2


See also