In particular, they abstract away from the presence of an outside environment. For a declarative programming language like Haskell, which is based on one of these individual fields of study - higher-order functions - this raises the question of I/O and its observable effects:
The dependently-typed language Agda relies on Haskell for its outside interactions:
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:
IO type serves as a tag for operations (actions) that interact with the outside world. The
IO type is abstract: no constructors are visible to the user.
The Haskell 2010 Report (page 95 of 329).
On being denotative
Instead of just being declarative, there have been calls for languages like Haskell to be denotative:
My vision of “solving the I/O problem” for functional programming is nothing
like Haskell’s semantically imperative (non-)solution (called
in Haskell), but rather to continue shifting semantically imperative mechanisms
out of the programming model and into the implementation of (semantically simple)
Can functional programming be liberated from the von Neumann paradigm?, Conal Elliott.
...it's been done before:
Because TALE is designed to describe computational algorithms rather than to
control computers, you will find no description of input/output operations. This is
a deliberate choice, since we consider these operations to be imperative in nature,
and not fit for expression by reduction semantics. [...] We consider this interface
to the real world as part of the programming environment, rather than as belonging
to the programming language proper, as much as is the case with text-editors,
compilers, operating systems, and the like.
Functional programming and the language TALE, Henk Barendregt and Marc van Leeuwen (pages 154-155).
In the absence of I/O:
The result of a program is simply the value it reduces to (if any).
In an actual implementation, there should of course be some way to enter input data,
form a function application of a precompiled function to it, and process in some way
the result yielded. These actions could be performed by an interactive
expression-evaluator, but might just as well be realised by making the
compiled functions (together with the reducer) accessible as a
function-library to other (possibly imperative) languages.
...but which other languages?
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).
Using more than one language
One suggestion is to ease the relocation of effects into the implementation by using a suitable imperative language:
My view is that the next logical step for programming is to split into two non-overlapping programming domains:
- runtime building for ...
- ... mathematical programming languages
[...] 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.
So instead of having the denotative/imperative division within Haskell by way of types, it would be at the language level in the forms of differing syntax and semantics, foreign calls, and so forth - an approach to be wary of in any major project, be it an implementation for some new programming language or an operating system:
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.
A New Scheme for Writing Functional Operating Systems, William Stoye (page 18 of 31).
This too could be alleviated by keeping the languages as similar as possible, but the logical conclusion of that endeavour is the two languages having only one point of difference - the imperative features - which would make having both languages effectively redundant:
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.
- 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"?
- 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 referentially transparent, which severely restricts the ability to use equational reasoning.
- 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 (e.g. like trying to solve the halting problem).
- Why do our programs need to read input and write output?
- Because programs are usually written for practical purposes, such as implementing domain-specific little languages like Dhall.
- Reactive Objects, Johan Nordlander, Mark P. Jones, Magnus Carlsson, Richard B. Kieburtz, and Andrew Black.
- Learn You A Haskell introduces I/O very late because it's a minor, ugly cousin feature of the language.
Learning Haskell, Heath Raftery.
Haskell uses the seriously complex machinery of monads to do I/O, supposedly without side effects (I don't accept this). And you end up writing stuff [...] which to me looks like C. There must be a more functional approach.
Monads Schmonads: Functional Input without tears (PyFL), Bill Wadge.
In the pure functional languages, Haskell and Clean, at present the most developed ones, the I/O problem is solved by respectively monads and uniqueness typing. But using these features, in both cases it is still possible to write incomprehensible code when dealing with I/O.
Gems of Corrado Böhm, Henk Barendregt (page 15 of 28, in footnote).
Fundamentally, all functional languages are translated to an imperative language, and it leaks.
It leaks when you need to read and write files, when you need to respond to real-time user events, when you write to the screen or interact with the GPU, or when you communicate with an external process or API.
Functional Programming Is a Leaky Abstraction, Owen Merkling.
Shallow embeddings rely on an analogy between mathematical functions and procedures in a pure functional programming language. Effects, however, like state, I/O, and exceptions, can stretch this analogy too far.
Proof-Producing Synthesis of CakeML with I/O and Local State from Monadic HOL Functions, Son Ho, Oskar Abrahamsson, Ramana Kumar, Magnus O. Myreen, Yong Kiam Tan, and Michael Norrish (page 2 of 16).
Essentially, the Haskell type
IO t combines both the features of [Idealised Algol's]
exp. This makes the [Haskell] framework slightly simpler, but it can be cumbersome if imperative computations are performed extensively.
Handout 9: Imperative programs and the Lambda Calculus, Uday Reddy (first page).
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.
If you find doing this separation [of code making I/O calls] annoying, you really wouldn’t like the machinations you have to do to mix pure and impure code in some functional languages (like Haskell).
IO is indeed a monad instance, but not a very nice one - the compiler treats it specially, and it is not very nice to reason about [...]
Understanding Monads, Nick Hu (first page).
Haskell compromises brilliantly, by fencing off pure and impure functions from one another [...] 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.
Understanding I/O in Haskell, which implies understanding Monads (at least, the
IO Monad) 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
IO Monad, 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 facilities for input/output [...]
Notes on Functional Programming with Haskell, H. Conrad Cunningham (page 21 of 192).
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.
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).
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.
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).
Writing solutions that are both elegant and efficient for applications that perform substantial I/O [...] is still a challenge.
Functional Programming, Olaf Chitil (page 20 of 23).
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 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).
Unfortunately, a monadic programming style typically differs fundamentally from an ordinary functional style. While this is not as much of a problem if the programming task at hand is side-effecting by nature, it becomes an issue when side effects are only involved peripherally: then, a programmer will be keen to maintain a functional look and feel to the program and not have encapsulation of side effects dominate the overall style of the program.
Heap Recycling for Lazy Languages, Jurriaan Hage and Stefan Holdermans (first page).
[...] 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".
The IO Monad for People who Simply Don't Care, "jefu".
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).
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. [...] Yet the understanding of monads is not trivial. The extensive amount of tutorials and questions on the Internet strengthen this thought.
Input/Output in Functional Languages (Using Algebraic Union Types), R.J. Rorije (page 30 of 130).
[...] 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).
In our approach, we would suggest to dismantle the
IO monad into its constituting monads, and combine them [...] 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).
Since I/O is by nature [reliant on] side effects, Haskell handles it differently. Monadic I/O is used to overcome this problem.
After overcoming [the problem of] Haskell's I/O, there were no more setbacks in writing the program.
Functional Programming Using Haskell, Wade Estabrooks, Michael Goit and Mark Steeves.
The notation for interactive programs written in the monadic style is irritatingly close to the notation used in imperative languages.
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 [...] 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).
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 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).
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 the case of input/output, which typically takes the form of an ordered sequence of events, it [had] always been hard to deny convincingly that the imperative model is more intuitive.
Animating Z specifications in Haskell using a monad, Howard S. Goodman (page 25 of 31).
Although we all love the beautiful aspects of functional languages we must admit that it is difficult to deal with a beast called Input-Output (I/O).
The Beauty and the Beast, Peter Achten and Rinus Plasmeijer (first page).
One consequence of referential transparency is that it is difficult to incorporate I/O into a pure functional language. At least, the mechanism familiar to imperative programmers, say, a function of no arguments [...] which returns the next data item of a stream, would be impossible, since successive calls [...] ought to return different values. Alternative models have been proposed which do not suffer from this difficulty [...] Nonetheless, these other models are notationally complex, and I/O [modelling] remains a problem.
Using a Lazy Functional Language for Textual Information Retrieval, Donald Ziff, Keith Waclena, and Stephen Spackman (page 3 0f 13).
To maintain correct I/O behaviour the relative temporal ordering of individual I/O operations must be controlled. [...] However, in pure functional languages [...] maintaining correct I/O behaviour is non-trivial [...] as a programmer needs always to be considering how something will be executed [...] a task from which functional programming is usually claimed to relieve the programmer.
The implementation of practical functional programming languages, Nigel Perry (page 18 0f 144, in footnote).
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).
[...] 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).
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, Willian Stoye (first page).