The I/O problem
Functional languages are entirely mathematical, so the places where they don't work show where computing is not mathematics and help to illuminate both fields.
- Real Programming in Functional Languages, James H. Morris (page 33 of 36).
One of those places is I/O:
How the initial [lambda term] is input, and how the final one is output, are questions neither asked or answered.
- How to Declare an Imperative, Philip Wadler (page 2 of 33).
The problem is mathematical in origin
The various fields of study collectively referred to as mathematics are abstract in one or more ways:
In a preliminary sense, mathematics is abstract because it is studied using highly general and formal resources.
For a declarative programming language like Haskell, which is based on one of these individual fields of study - higher-order functions - being so abstract then raises the question of I/O and its observable effects:
How must interactions between a program and an external environment (consisting of, e.g., input/output-devices, file systems, ...) be described in a programming language that abstracts from the existence of an outside world?
- Functions, Frames and Interactions, Claus Reinke (page 10 of 210).
It should then be of little surprise that attempts to find a satisfactory (as opposed to inexplicable) mathematical solution for I/O have been less than successful:
During the 1960s, several researchers began work on proving things about programs. [...]
Several difficult problems emerged from this work. One was the problem of specification: before one can prove that a program is correct, one must specify the meaning of "correct", formally and unambiguously. Formal systems for specifying the meaning of a program were developed, and they looked suspiciously like programming languages.
Researchers began to analyze why it is often harder to prove things about programs written in traditional languages than it is to prove theorems about mathematics. Two aspects of traditional languages emerged as sources of trouble because they are very difficult to model in a mathematical system: mutability and sequencing.
- The Anatomy of Programming Languages, Alice E. Fischer and Frances S. Grodzinsky (page 353 of 600).
There is in principle nothing to stop functional programs from passing a single extra parameter into and out of every single function in the entire system. If this extra parameter were a collection (compound value) of some kind then it could be used to simulate an arbitrarily large set of mutable variables. In effect this approach recreates a single pool of global variables - hence, even though referential transparency is maintained, ease of reasoning is lost (we still know that each function is dependent only upon its arguments, but one of them has become so large and contains irrelevant values that the benefit of this knowledge as an aid to understanding is almost nothing).
- Out of the Tar Pit, Ben Moseley and Peter Marks (page 17 of 66).
We have experimented for some time with a means for the programmer to specify a ‘syntactic’ partial order between subexpressions, with the intended effect that the ‘semantic’ order in which the input actions occur during the evaluation is not in conflict with the specified ‘syntactic’ partial order on the subexpressions (which carries over to the input actions evoked by those subexpressions). As a consequence, whenever the evaluator is about to execute an input action, it first has to decide whether there will occur in the future another input action that should be done first according to the specified ‘syntactic’ partial order. That problem, however, is undecidable.
- An alternative approach to I/O, Maarten Fokkinga and Jan Kuper (page 6 of 12).
The technical difficulty
In 1994 researchers showed that mutability and sequencing could be used anonymously in Haskell if external effects were excluded:
The reason for introducing a different monad
ST, rather than just providing these operations over the
IOmonad, is that destructive updates to variables in a program are not externally visible side-effects. We can therefore encapsulate these imperative effects [...] Using
runSTwe can write pure (non-monadic) functions whose implementation uses imperative features internally.
- Monads and Effects, Nick Benton, John Hughes and Eugenio Moggi (pages 33-34 of 82).
But externally visible side-effects often cause other problems if they have no prior sequencing:
By introducing side effects in expressions, the semantics becomes significantly more complex. At the same time, several issues arise, related to the evaluation order, i.e. the order in which the subparts of an expression are evaluated. If the evaluation order is not strictly defined, the presence of side effects in expressions is a source of non-deterministic behaviour.
- Denotational Semantics of Evaluation Order in Expressions with Side Effects, Nikolaos S. Papaspyrou (first page).
When combining effects, their order is often highly important (the reader might want to try different combinations of the effects “Open door”, and “Walk through door”, for example).
- Fudgets – Purely Functional Processes with applications to Graphical User Interfaces, Magnus Carlsson and Thomas Hallgren (page 12 of 263).
...and the machinery which runs our programs relies on mutability:
An “automatic” [Turing] machine becomes a “choice” machine as soon as we allow the machine’s tape to be modified by external entities: the tape itself becomes a means of communication. This is essentially what happens in “real” computers (memory-mapped I/O); for example, we can write to the computer’s screen by modifying one particular area of memory, or find out which key was pressed on the computer’s keyboard by reading another.
It is less obvious how to add interaction to the lambda calculus.
- Making Uniqueness Typing Less Unique, Edsko de Vries (page 23 of 264).
Stream transformers are fragile to use, continuations are powerful but somewhat clutter the syntax of functions. Monads and uniqueness types both present a trade-off, do we accept the over-sequentialisation imposed by monads, or the visual disorder of explicit environment passing? We believe that a compromise is still to be found [...]
- Approaches to Functional I/O, Owen Stephens (final page).
More satisfactory solutions could also be discovered: the research continues. In the meantime, those who work with programming languages will have to contend with I/O as best they can:
By its very nature I/O does not fit in well with programming language design. Every language designer has encountered the problem that the task of including usable I/O facilities invariably seems to make it necessary to break some of the rules or design principles of the language. In many languages with clean and simple concepts I/O is the "odd one out".
- I/O Considered Harmful (At Least for the First Few Weeks), John Rosenberg and Michael Kölling (first page).
[...] I/O calls are side effects and make impure functions. Impure functions are hard to test.
...especially considering the ever-expanding state space in which memory-mapped I/O resides:
Definition 6.1. A state is an element of 2n for some fixed n; that is, it is a finite sequence of 1s and 0s. The space 2n is called the state space.
The number n is the size of the memory in bits, and will usually be rather large. On modern personal computers at the time of this writing, n would be on the order of several billion.
- On Some Aspects of The Theory of Monads, Carsen Berger (page 14 of 16).
An axiomatic approach
The dependently-typed language Agda relies on Haskell for its outside interactions:
4 Compiling Agda programs
This section deals with the topic of getting Agda programs to interact with the real world. Type checking Agda programs requires evaluating arbitrary terms, ans as long as all terms are pure and normalizing this is not a problem, but what happens when we introduce side effects? Clearly, we don't want side effects to happen at compile time. Another question is what primitives the language should provide for constructing side effecing programs. In Agda, these problems are solved by allowing arbitrary Haskell functions to be imported as axioms. At compile time, these imported functions have no reduction behaviour, only at run time is the Haskell function executed.
- Dependently Typed Programming in Agda, Ulf Norell and James Chapman (page 38 of 41).
Haskell's FFI also allows for arbitrary foreign code to be imported, to only be executed at run time:
instance Monad IO where
return = unitIO
(>>=) = bindIO
foreign import ccall "primUnitIO" unitIO :: a -> IO a
foreign import ccall "primBindIO" bindIO :: IO a -> (a -> IO b) -> IO b
However, Haskell 2010 doesn't allow higher-order FFI declarations, or their use of type variables. Visible implementations of monadic I/O based on other interfaces are another option, but Haskell 2010 also imposes restrictions on its I/O type:
IOtype serves as a tag for operations (actions) that interact with the outside world. The
IOtype is abstract: no constructors are visible to the user.
- The Haskell 2010 Report (page 95 of 329).
On being denotative
The abilities that declarative languages like Haskell provide has inspired interest in programming declaratively without the conspicuous use of I/O:
If input and output were invisible, students could implement fun simulations, animations, and even interactive and distributed games all while using nothing more than plain mathematics.
- A Functional I/O System, Matthias Felleisen, Robert Bruce Findler, Matthew Flatt and Shriram Krishnamurthi (first page).
Haskell is one example of a programming language where the code aspires to resemble pure mathematical expressions and definitions.
- The end of history for programming, Gabriella Gonzalez.
In [Kowalski's original simple and clear] conception there are no imperative constructs and indeed there is no notion of control or of anything 'happening'. The spirit is very like that of Strachey's static Platonic vision — elaborated by Landin and later by Scott — of programs as purely denotative expressions in which entities are represented as the results of applicative combinations of functions with their arguments. Running a program is, on this view, nothing more than finding out what it denotes. The program merely does the denoting, and should not be taken for a prescription of any sort of activity, such as 'computing'.
- LOGLISP: an alternative to PROLOG, John A. Robinson and Ernest E. Sibert (first page).
Instead of putting I/O into the functional programming model (as in continuation- and stream-based I/O), or adopting a hybrid functional/imperative model (as in monadic I/O), let's explore how to move I/O entirely out of our programming model into the implementation of a denotationally simple model for whole systems.
That seems reasonable, considering the prior experience with such systems:
Underneath the implementation of our current functional abstractions (numbers, strings, trees, functions, etc), there are imperative mechanisms, such as memory allocation & deallocation, stack frame modification, and thunk overwriting (to implement laziness).
Consequently, if I/O is confined to the implementation of the whole system, and away from its denotationally-simple model, then interacting with users (and their devices) would also be managed by the implementation. Therefore the implementation would be extended as users devise more ways to interact with the whole system.
But if the implementation's complexity approaches that of an operating system, some of its implementors may very well ask:
How do we get from here (still stuck in the quagmire of imperative programming models) to there (simple semantics and consistently excellent composability)?
...considering the prior experience with these systems:
1. It demands far more implementation effort "beneath" the level of functional programming. It is the low level parts of operating systems whose semantics are most in doubt and where hidden bugs are most harmful: this is where functional programming is needed most!
2. It seems more liable to disaster from some hidden bug in the operating system. Merely discarding a pointer might discard the filing system, requiring recovery in some manner "outside" the operating system.
3. It merely delays the issue of how one is to communicate with a persistent outside world. Sooner or later the question of interfacing to [networks, devices], remote filing systems and other computers must be faced.
- A New Scheme for Writing Functional Operating Systems, William Stoye (page 18 of 31).
Make no mistake, I don’t want to write systems software in a language like C++.
- The Night Watch, James Mickens (page 3 of 4).
For those reasons:
If functional languages are the best tool for writing programs, then this should be regarded as a defeat (if they're not, none of this is important). Thus, we must experiment with extending functional programming techniques to cover interaction and control operations as much as possible.
- The implementation of functional languages using custom hardware, William Stoye (page 80 of 142).
The special claim of ISWIM is that it grafts procedural notions onto a purely functional base without disturbing many of the desirable properties.
- The Next 700 Programming Languages, Peter J. Landin (page 8 of 10).
...which is achieved in Haskell by using types.
A historical note
The stream-based (or dialogue) I/O model provided in Haskell prior to version 1.3 required substantial support from implementations e.g. by using an imperative “wrapper”:
It repeatedly takes a request off the result list, acts on the request, and attaches an appropriate response to the argument list. There has be some clever footwork to deal with the fact that the function has to be applied to a list of responses before there are any responses in the list, but that isn’t a problem in a lazy setting.
- Tackling the Awkward Squad: monadic input/output, concurrency, exceptions, and foreign-language calls in Haskell, Simon Peyton Jones (pages 4-5 of 60).
So why was it replaced?
[...] it has several defects:
- It is hard to extend. New input or output facilities can be added only by extending the
Responsetypes, and by changing the “wrapper” program. Ordinary users are unlikely to be able to do this.
- There is no very close connection between a request and its corresponding response. It is extremely easy to write a program that gets one or more “out of step”.
- Even if the program remains in step, it is easy to accidentally evaluate the response stream too eagerly, and thereby block emitting a request until the response to that request has arrived – which it won’t.
The monadic interface helped to shift the first problem - the difficulty of extending - out of those early Haskell implementations:
- It is easily extensible. The key to our implementation is to extend Haskell with a single form that allows one to call an any procedure written in the programming language C , without losing referential transparency (Section 2.3). Using it programmers can readily extend the power of the I/O system, by writing Haskell functions which call operating system procedures.
- Imperative functional programming Simon Peyton Jones and Philip Wadler (first page).
This made it possible to define dialogue I/O, “wrapper” and all, in Haskell:
The entire I/O system provided by our compiler is written in Haskell, using the non-standard extensions we describe below. The language’s standard
Dialogueinterface for I/O is supported by providing a function to convert a
- (second page).
Any proposal which involves shifting I/O back into modern Haskell implementations must therefore have a practical solution to that original problem - the difficulty of extending implementations.
- Is there an alternate standalone model of I/O with less problems than each of the current models?
- If not, can I/O be moved away from the language (as a model) and into the implementation (thus making the language denotative), while keeping the resulting language relatively practical to use?
- Is the C language "purely functional"?
- Is the Haskell language "purely functional"?
- Haskell is not a purely functional language, but is often described as being referentially transparent.
- Can functional programming be liberated from the von Neumann paradigm?
- Only if no-one proves it to be impossible (or someone proves it is possible).
- Why do our programs need to read input and write output?
- Functional Programming with Side Effects, Mark B. Josephs.
- Functional programming and the language TALE, Henk Barendregt and Marc van Leeuwen.
- The Semantics of an FP Language with Infinite Objects, Teresa Thomas.
- A Mathematical Approach to Nondeterminism in Data Types, Wim H. Hesselink.
- Assignments for Applicative Languages, Vipin Swarup, Uday S. Reddy and Evan Ireland.
- TIP in Haskell – Another Exercise in Functional Programming, Colin Runciman.
- A Functional Approach to Graphics Programming and Modeling, S. Ramakrishnan.
- Imperative Functional Programming, Uday S. Reddy.
- A representable approach to finite nondeterminism, S. O. Anderson and A. J. Power.
- The impact of the lambda calculus in logic and computer science, Henk Barendregt.
- Directions in Functional Programming for Real(-Time) Applications, Walid Taha, Paul Hudak and Zhanyong Wan.
- Reactive Objects, Johan Nordlander, Mark P. Jones, Magnus Carlsson, Richard B. Kieburtz, and Andrew Black.
- Can GUI Programming Be Liberated From The IO Monad, John Peterson, Antony Courtney and Henrik Nilsson.
- Witnessing Side Effects, Tachio Terauchi and Alex Aiken.
- Programming Languages For Interactive Computing, Roly Perera.
- Heap Recycling for Lazy Languages, Jurriaan Hage and Stefan Holdermans.
- Single Assignment C (SAC): High Productivity Meets High Performance, Clemens Grelck.
- Combining Effects and Coeffects via Grading, Marco Gaboardi, Flavien Breuvart, Shin-ya Katsumata, Dominic Orchard and Tarmo Uustalu.
- Mathematical mysteries, Plus magazine.
The primary limitation of FP systems is that they are not history sensitive. Therefore they must be extended somehow before they can become practically useful.
- Can Programming Be Liberated from the von Neumann Style? A Functional Style and Its Algebra of Programs, John Backus (page 11 of 29).
An other important point is that FP systems [...] have no means to save definitions or results produced by a computation and to recall them later. Therefore, they must be imbedded into an environment capable of performing these tasks for them.
- FAD, a Functional Programming Language that Supports Abstract Data Types, Johannes J. Martin (page 2 of 16).
[...‘stream-processing’ solutions based on] systems of communicating processes can be described in a purely functional language by recursion over infinite lists; this is important because it open up the way for problems involving input–output and communication to be dealt with cleanly within the functional style .
- Functional Programs as Executable Specifications: Discussion, David A. Turner (page 17 of 27).
Functional languages are extremely powerful and succinct tools for expressing algorithms, but the way in which the results of such calculations should be communicated to the outside world is not obvious.
- Message-based functional operating systems, William Stoye (first page).
Input and output are perhaps the most systematically neglected features of programming languages. They are usually ad hoc, and they are usually poorly integrated with the other facilities of their hosts – the languages in which they are embedded.
- An Input-Output Model for Interactive Systems, Mary Shaw (first page).
The utility of the different programming style imposed by restricting side-effects is debatable, but it is fair to say that operations such as input and output (since they are history sensitive — i.e. they are inherently side-effects) are particularly awkward to express.
- The Refined-Language Approach To Compiling For Parallel Supercomputers, Henry G. Dietz (page 34 of 188).
Is there any hope of achieving purely functional, yet universal, and of course efficient I/O?
- On the Expressiveness of Purely Functional I/O Systems, Paul Hudak and Raman S. Sundaresh (page 2 of 29).
It is not clear how one might express an interactive process as a function — this is still a research topic, see for example Interactive functional programs: a method and a formal semantics.
- The design and implementation of an operating system in a functional language, John R. G. Cupitt (page 10 of 150).
Starting with Landin streams many I/O schemes have been proposed for purely functional languages, but they tend to be ad hoc in the sense of being designed to express particular kinds of I/O, without aiming for universal power.
- PFL+: a kernel scheme for functional I/O, Andrew Gordon (page 4 of 28).
A straightforward example of such a problematical application is that of a function which could allow us to interactively query and modify a database; this is clearly an interactive problem, since we may only be able to formulate queries once we have seen the responses to earlier ones.
- Functional Programming and Operating Systems, Simon B. Jones and A. F. Sinclair (first page).
At present most declarative languages are guest languages on [...] procedural machines, and able to preserve their data in the file system provided by the host machine. However, the interface to the file system that is provided by the guest languages is primitive and often not referentially transparent.
- A Functional Database, Phil Trinder (page 14 of 210).
A functional program transforms data into other data according to a certain algorithm. Functional programs cannot deal with input/output, cannot switch on and off lamps depending on the value of a certain parameter; in general they cannot deal with process control.
- Functional Programming and Lambda Calculus, Henk Barendregt (page 324).
To maintain correct I/O behaviour the relative temporal ordering of individual I/O operations must be controlled. [...] However, in pure functional languages, as they are referentially transparent, there is usually no mechanism to control the temporal ordering of the evaluation, hence maintaining correct I/O behaviour is non-trivial3. [...]
3It is non-trival as a programmer needs always to be considering how something will be executed – which is only possible with a thorough understanding of the evaluation strategy/denotational semantics. It is also a task from which functional programming is usually claimed to relieve the programmer.
- The implementation of practical functional programming languages, Nigel Perry (page 18 of 144).
Input and output do not require justification: a program without communication with the outside world is of little use.
- Introduction to the Theory of Programming Languages, Bertrand Meyer (page 214 of 459).
One consequence of referential transparency is that it is difficult to incorporate I/O into a pure functional language. [...] Alternative models have been proposed which do not suffer from this difficulty; see for example the Haskell [version 1.1] report, which attempts to represent the current consensus in the field. Nonetheless, these other models are notationally complex, and I/O modeling remains a problem.
- Using a Lazy Functional Language for Textual Information Retrieval, Donald Ziff, Keith Waclena, and Stephen Spackman (page 3 of 13).
A lot has been said in favour of functional programming but the fact is that functional programming suffers from a paramount lack of appreciation in the established programming society. For one reason this is caused by the fact that only recently functional programs have gained execution speeds comparable to their imperative rivals . Another important reason is that functional programming defected on performing input output (I/O hereafter) with the outer world.
I/O and functional programming doesn’t seem to be unifiable: functional languages don’t have the notion of side-effects and lack a precise control of the evaluation order. These are precisely the important aspects for any I/O model. There have been many proposals how to deal with I/O (see section 8) but none of them are completely satisfactory. In particular it is very hard to deal with the side-effect problem.
- High Level Specification of I/O in Functional Languages, Peter Achten, John van Groningen, Rinus Plasmeijer (pages 1-2 of 17).
In the case of input/output, which typically takes the form of an ordered sequence of events, it has always been hard to deny convincingly that the imperative model is more intuitive. Functional models using streams or continuations have always been difficult to grasp at first, whereas practically anyone can quickly come to terms with the ‘input a; print a’ nature of basic (or BASIC) imperative languages.
- Animating Z specifications in Haskell using a monad, Howard S. Goodman (page 25 of 31).
A disadvantage of the [stream] method for writing interactive functional programs is that every part of the output has to be part of the result of the whole program on the top level. As a consequence it is not a trivial task to change a program in such a way that for a specific function that is used somewhere in an expression the value of its arguments is printed every time the function is called. A change like that, which can be of interest for tracing or debugging, requires a change of the structure of the whole program.
- Functional Programming and Parallel Graph Rewriting, Rinus Plasmeijer and Marko van Eekelen (page 82 of 623).
Although the advantages of functional programming are fairly well known, a few areas have proved troublesome, I/O being one of these.
- Functional Languages and Graphical User Interfaces - a review and a case study, Rob Noble and Colin Runciman (page 6 of 51).
There are many attempts to integrate I/O into functional languages, such as linear types , and monads in Haskell, or uniqueness typing in Clean . These approaches have in common that the programmer explicitly has to create data dependencies among consecutive I/O operations which lead to very awkward notations.
- Single Assignment C - Functional Programming Using Imperative Style, Sven-Bodo Scholz (page 2 of 13).
In pure functional languages, several proposals have been made for clean I/O models. We mention Landin’s streams, Monads, and Uniqueness Types. Compared to our approach, these models are more deterministic [when less is required], especially with respect to output. More generally, these models sequentialize I/O actions by imposing static constraints (usually expressed as types) on the program.
- A model for I/O in equational languages with don’t care non-determinism, H. R. Walters and J. F. Th. Kamperman (page 14 of 15).
If, however, we wish to classify programs that have potentially infinite behaviors, such as a program that echoes its input, then a more direct model of input-output seems necessary.
- Untyped Lambda-Calculus with Input-Output, Jerzy Tiuryn and Mitchell Wand (page 2 of 11)
The programming style in a lazy functional language is heavily influenced by the supported I/O-mechanism. Modifying the I/O-behaviour or debugging some lazy functional program that uses I/O is a black art. It is interesting that novices in lazy functional programming in general expect that there is some direct (side-effecting) I/O using a function call.
- A Partial Rehabilitation of Side-Effecting I/O:, Manfred Schmidt-Schauß (first page).
For other models of computation like functional or logic programming, the typical approach has been to add extra features implementing I/O. But these features do not fit in the nice and simple underlying model: in order to be able to understand or reason about programs involving I/O, the basic computational model has to be extended in non trivial ways, making it not so nice and simple anymore [...]
- Input/Output for ELAN, Patrick Viry (first page).
While it is fine that monadic I/O has good theoretical under-pinnings, did anyone stop to think if it helped in user interface programming? If all that it is is a means somehow to construct interactive programs, then it succeeds, but that is not enough. A programmer chooses a language based not on only on ability, but also usability.
- Interacting with Functional Languages, Duncan Sinclair (page 15 of 159).
[...] The meaning of a conventional computer program, as far as a user is concerned, is just the mathematical function it evaluates. But we users are inside our interactive systems; we care about what continually goes on. The meaning surely lies in the whole conversation, not just its end-result. (Indeed there may be no end-result, since there may have been no goal.)
- Turing, Computing and Communication, Robin Milner (page 8 of 12).
Uniqueness typing addresses the more general problem of statically controlled use of resources in functional programs and, even if combined with passing unique representations of environment objects as arguments to these programs, it does not suffice to solve the input/output-problem. The reason is that the environment is not updated in one conceptual step after the evaluation of a program has computed a new environment representation but rather in small steps whenever the environment representation is modified during program evaluation. The primitive interactions are thus implemented as side-effecting operations, the use of which is rendered safe in the uniqueness-typed environment passing framework.
Similarly, monads are used to address the more general problem of computations (involving state, input/output, backtracking, ...) returning values: they do not solve any input/output-problems directly but rather provide an elegant and flexible abstraction of many solutions to related problems [...]. For instance, no less than three different input/output-schemes are used to solve these basic problems in Imperative functional programming, the paper which originally proposed ‘a new model, based on monads, for performing input/output in a non-strict, purely functional language’. [...]
So, both input/output-schemes merely provide frameworks in which side-effecting operations can safely be used with a guaranteed order of execution and without affecting the properties of the purely functional parts of the language.
- Functions, Frames and Interactions, Claus Reinke (pages 96-97 of 210).
Since I/O is by nature side effects, Haskell handles it differently. Monadic I/O is used to overcome this problem.
After overcoming Haskell's I/O, there were no more setbacks in writing the program.
- Functional Programming Using Haskell, Wade Estabrooks, Michael Goit and Mark Steeves.
- Relating Operational and Denotational Semantics for Input/Output Effects, Roy L. Crole and Andrew D. Gordon (page 2 of 38).
Something fundamental and innocent looking as simple I/O has been a hard problem for functional languages. Many models, such lazy streams, continuation semantics, and systems model have been proposed to overcome the discrepancy between a referential transparent language and I/O side effects. Hudak gives examples of the above I/O styles and although imperative syntax is mimicked they look very un-intuitive and cumbersome to someone used to plain I/O statements. Recently, a step forward has been made by employing so-called monads for I/O . This model has also been adopted for the latest revision for HASKELL [version 98]. It remains a matter of taste whether such a treatment of I/O state is conceived as natural or unacceptable.
- A Functional Pattern System for Object-Oriented Design, Thomas Kühne (page 40 of 346).
Now that we have discussed the monadic approach in some detail, you may well be asking the following question: once we have added imperative-looking input/output, concurrency, shared variables, and exceptions, have we not simply re-invented good old procedural programming? Have we “lost the plot” [...]
- Tackling the Awkward Squad: monadic input/output, concurrency, exceptions, and foreign-language calls in Haskell, Simon Peyton Jones (page 55 of 60).
In our approach, we would suggest to dismantle the
IOmonad into its constituting monads, and combine them with the coproduct as needed. This has the advantage that the effects of most monads can be localised, so using e.g. state threads or exceptions in one part of the program will not make the type of every function using this part monadic, as is currently the case with the
- Composing Monads Using Coproducts, Christoph Lüth and Neil Ghani (page 11 of 12).
It is possible that a declarative programming style for the IO part of a program can be integrated into Haskell.
- Realising nondeterministic I/O in the Glasgow Haskell Compiler, David Sabel (page 37 of 39).
The common method to relieve the programming language designer from the inherent IO-problems is to shift responsibility to the programmer who has to sequentialize all IO-requests. This is also true for the monadic approach implemented in Haskell [version 98] .
- FUNDIO: A Lambda-Calculus With letrec, case, Constructors, and an IO-Interface:, Manfred Schmidt-Schauß (page 3 of 84).
As for intrinsic advantage, I think Haskell has at least one obvious handicap -- monadic I/O. Every minute I spend trying to understand how to program with it is a minute I am solving a problem I don't care about.
- New Paul Graham thing,sean.
[...] doing IO in Haskell means using a polymorphic type as well as a mathematical theory that is rather obtuse. That's why IO is so far down in this tutorial.
- Haskell Tutorial for C Programmers, Eric Etheridge.
HASKELL uses monads for destructive updates and I/O; they add a restricted form of stateful computation to a pure language, retaining referential transparency . The disadvantage is that programs become more complicated. Also, if imperative elements of a given application were not taken into account during its design but turn out to be necessary later on, often major parts have to be redesigned or (at least) reimplemented, especially because types change significantly. [...]
The TRUTH team considers this point as one of the biggest drawbacks of the purely functional paradigm as followed by HASKELL.
- Functional programming languages for verification tools: A comparison of Standard ML and Haskell, Martin Leucker, Thomas Noll, Perdita Stevens and Michael Weber (pages 12-13 of 22).
Booch: What would be your advice to somebody that would want to take up the banner of functional programming? Where would you suggest they begin, and what hard problems would you like them to pursue?
Backus: Well, trying to functionalize all the input/output stuff.
Booch: Helping it talk to the real world.
- Oral History of John Backus, Grady Booch (pages 36-37 of 42).
A recognised difficulty with using monads to enforce single-threaded access to the I/O world is that the programmer is forced to over-sequentialize their I/O code: I/O operations that are independent and which could be performed in any order still need to be given an explicit ordering in the program.
- Modelling Deterministic Concurrent I/O, Malcolm Dowse and Andrew Butterfield (page 2 of 12).
Monadic I/O , the standard approach to I/O in pure languages, forces the programmer to explicitly sequence all I/O actions. This is not abstract at all, and is hardly a better specification of I/O than any C or Pascal program. This opinion has been echoed by the functional language community.
- A Semantic Framework for Deterministic Functional Input/Output, Malcolm Dowse (page 17 of 224).
The downside of I/O using monads is the need for a monad that can not be unwrapped. So, when using monadic I/O there is no way to get rid of the I/O monad. Furthermore, it is not as intuitive as one would like it to be. A prerequisite to good software design is a thorough understanding of the structures and glues of the implementation language. Monads are an interesting addition to both of these sets, and therefore a design implication is that the designer needs to be aware of the potential benefits of using monads. Yet the understanding of monads is not trivial. The extensive amount of tutorials and questions on the Internet strengthen this thought. Another disadvantage is that monads tend to be an all-or-nothing solution. The transition from no interaction at all to a single form of interaction is not very smooth. Parts of the program need to be rewritten.
- Input/Output in Functional Languages (Using Algebraic Union Types), R.J. Rorije (page 30 of 130).
The common practice of mixing IO with functionality inhibits composability whether in C or in Haskell.
[...] the next question that always comes up is how to do IO down in an function (in an expression) down a ways in the code. This often involves rewriting those "expressions" into "commands".
[...] for a long time [interaction] was seen as one of the major obstacles for functional programming. The issues are now better understood, and two workable solutions have emerged (uniqueness typing and monadic I/O). Nevertheless, the last word on this issue has not yet been said.
- Making Uniqueness Typing Less Unique, Edsko de Vries (page 23 of 264).
Support for [external] state in Haskell exists in the form of the I/O monad, but in our opinion the monadic idiom does not scale well to large, complexly stateful programs, and imposes constraints that are unnatural in the eyes of systems programmers.
- The Origins of the BitC Programming Language, Jonathan Shapiro, Swaroop Sridhar and M. Scott Doerrie.
Supporting IO would require us to extend our heap model to include relevant aspects of the outside system, plus some kind of nondeterminism for IO actions to take into account that we can never model the world in its entirety.
- Imperative Functional Programming with Isabelle/HOL, Lukas Bulwahn, Alexander Krauss, Florian Haftmann, Levent Erkök and John Matthews (page 15 of 16).
Writing solutions that are both elegant and efficient for applications that perform substantial I/O or transform graph-structured data is still a challenge.
- Functional Programming, Olaf Chitil (page 20 of 23).
As Amdahl’s law states , the overall speedup that can be achieved is determined not by the parallel parts of a program but by its sequential segments. This becomes even more true with increasing numbers of processing units. Therefore, we believe that even partly lifting the requirement of strictly sequential I/O can be hugely beneficial.
- Controlling Chaos: On Safe Side-Effects in Data-Parallel Operations, Stephan Herhut, Sven-Bodo Scholz and Clemens Grelck (page 2 of 9).
Input/output is awkward in declarative languages . Some functional languages like LISP have procedural read and write operations. Prolog has ugly read and write "predicates" that execute in sequence. Haskell monads provide pure functional I/O but still involve a sequence of actions.
- Specifying Input/Output by Enumeration, Walter W. Wilson and Yu Lei (first page).
[...] where would I read up more on how we'd get rid of the imperative stuff entirely, [going] purely functional, and still have terminal interaction?
- The C language is purely functional, Raoul Duke.
Anyone expecting to use Haskell for primarily I/O related stuff is going to die from frustration.
- Part II: How not to Start Your Haskell Program, Ryan Deschamps.
A potential drawback of monads, is that they force the use of a single environment, to be passed between all I/O operations within a program, effectively sequentialising all I/O operations.
- Approaches to Functional I/O, Owen Stephens (page 5 of 8).
Once you’re in the IO monad, you’re stuck there forever, and are reduced to Algol-style imperative programming. You cannot easily convert between functional and monadic style without a radical restructuring of code.
- Of Course ML Has Monads!, Robert Harper.
Functional languages have many advantages— being mathematical in nature, it is relatively easy to prove correctness of their programs—so we would like a way to mimic, in a functional manner, non-functional side-effects such as input/output and mutable state.
- On Some Aspects of The Theory of Monads, Carsen Berger (page 14 of 16).
IO in Haskell [is] for me a source of great displeasure and it just defeated every try I have given to learn the language.
- Introduction to Haskell
IO, Ludovic Kuty.
- Trace-based just-in-time compiler for Haskell with RPython, Even Wiik Thomassen (page 15 of 96).
Understanding I/O in Haskell, which implies understanding Monads (at least, the
IOMonad) is actually one of the major difficulties I’ve came across while learning Haskell [...]
- Is it possible to solve TSORT with Haskell?, Bruno Oliveira.
Here you’re equating functional languages with the inability to make use of benign effects, such as instrumentation of code for profiling purposes, which is indeed very clumsy in Haskell (you either have to rewrite your code to be imperative so that it conforms to the
IOMonad, or else you have to admit that you really want ML [or perhaps HasFuse] by using
- Who teaches functional programming?, Robert Harper.
Although the functional programming community has solved many of the difficulties in implementation and use of functional languages, more research is needed on several issues of importance to the real world: on facilities for input/output, nondeterministic, realtime, parallel, and database programming.
- Notes on Functional Programming with Haskell, H. Conrad Cunningham (page 21 of 192).
[...] you shouldn't spend much time writing
IOstuff, because it's a bad language embedded in a good one.
- On the unsafety of interleaved I/O, Dan Doel.
IOis indeed a monad instance, but not a very nice one - the compiler treats it specially, and it is not very nice to reason about it - instead, we shall explore some monads which do not put into question Haskell’s purity.
- Understanding Monads, Nick Hu (first page).
As an example, consider some code that needs access to the current time. This is something that would normally require
IO, but we likely want to be able to use the value in a pure context without “infecting” the entire program with
- Four months with Haskell, Alexis King.
The illusion is so good that programmers are fooled into thinking I/O is pure in Haskell. And now that we can write mostly pure code with occasional impure wrappers, researchers have mostly stopped seeking superior alternatives.
- A Problem With I/O, Ben Lynn.
I like the concise way of coding pure functions in Haskell using mathematical ideas like recursion and list comprehensions. However, user interactions are quite painful to implement using the I/O monad.
- The Definition of Functional Programming, Dirk Verlinden.