Clean (in terms of programming language) - no side effects . A programming language is pure if all functions in the programs of this language are pure .
Programs written in pure programming languages are easier to debug , verify , easier to detect errors that could not be detected with the help of testing . Programs in a pure programming language are easier to rewrite, avoiding the introduction of errors. At the same time, the process of planning the program itself with the expectation of cleanliness is more complicated.
Another important advantage of pure functional languages is concurrency . Since all functions for calculations use only their parameters, it is possible to organize the calculation of independent functions in an arbitrary order or in parallel, this will not affect the result of calculations. Parallelism can be organized not only at the level of the language compiler, but also at the level of hardware architecture. There are experimental computers based on similar architectures, such as a Lisp machine .
Pure functional languages are sometimes called “deterministic” in the sense that for every function, each call always gives the same effect (in imperative languages this, generally speaking, is not true). At the same time, such languages are called “non-deterministic” in the sense that the order of actual program execution can vary greatly depending on the specific implementation of the language: algorithms can implicitly be parallelized, intermediate data can be excluded from the chain of transformations, the representation of the same types can vary even within the framework of one program, etc. (for imperative languages this is simply impossible). Simply put, pure languages are deterministic at the source code level and non-deterministic at the implementation level (imperative ones, on the contrary).
I / O and clean
The most serious area of application of programming languages in which side effects are constantly present in functions is input-output . It can be assumed that any data entry operation from the user is an action with a side effect, since it is impossible to say in advance what the user will enter as the values of the parameters used in the computational process. Although some researchers and theoretical scholars argue that I / O cannot be considered as an example of side effects, since in fact I / O is a change in the program environment, but in any case I / O makes the functions using it non-deterministic.
In pure functional programming, the assignment operator is absent, objects cannot be changed and destroyed, you can only create new ones by decomposing and synthesizing existing ones. The garbage collector built into any functional language translator will take care of unnecessary objects. Due to this, in pure functional languages all functions are free from side effects. However, this does not prevent these languages from simulating some useful imperative properties, such as exception handling and mutable (destructive) arrays . For this there are special methods.
However, some reasons for the presence of functions with side effects cannot be completely removed from functional programming languages, since in such a case such languages would be too limited in practical application. In the first place, this refers specifically to I / O. It is difficult to imagine a full-fledged programming language, where there is no possibility to perform data input from the user in an interactive mode, as well as to output data for the user.
Monads
To enable the use of technologies such as input-output, without detracting from the property of purity, in many functional programming languages, including Haskell , a special mechanism called the " monad " is used. Monads, as it were, wrap the necessary imperative properties, preventing them from mixing with the pure syntax of a functional language. The use of monads allowed realizing all those bottlenecks that regulated the presence of side effects in functions.
For example, to provide I / O in Haskell, a standard IO
monad is implemented, outside of which no I / O operation can be performed. All other standard monads implemented for Haskell have the same properties.
Examples of pure programming languages
- Haskell
- Clean