Total language
   HOME

TheInfoList



OR:

Total functional programming (also known as strong functional programming, to be contrasted with ordinary, or ''weak''
functional programming In computer science, functional programming is a programming paradigm where programs are constructed by applying and composing functions. It is a declarative programming paradigm in which function definitions are trees of expressions that ...
) is a programming paradigm that restricts the range of programs to those that are provably terminating.


Restrictions

Termination is guaranteed by the following restrictions: # A restricted form of
recursion Recursion (adjective: ''recursive'') occurs when a thing is defined in terms of itself or of its type. Recursion is used in a variety of disciplines ranging from linguistics to logic. The most common application of recursion is in mathemati ...
, which operates only upon 'reduced' forms of its arguments, such as Walther recursion, substructural recursion, or "strongly normalizing" as proven by abstract interpretation of code. # Every function must be a total (as opposed to partial) function. That is, it must have a definition for everything inside its domain. #* There are several possible ways to extend commonly used partial functions such as division to be total: choosing an arbitrary result for inputs on which the function is normally undefined (such as \forall x \in \mathbb. x \div 0 = 0 for division); adding another argument to specify the result for those inputs; or excluding them by use of type system features such as refinement types. These restrictions mean that total functional programming is not
Turing-complete In computability theory, a system of data-manipulation rules (such as a computer's instruction set, a programming language, or a cellular automaton) is said to be Turing-complete or computationally universal if it can be used to simulate any ...
. However, the set of algorithms that can be used is still huge. For example, any algorithm for which an asymptotic upper bound can be calculated (by a program that itself only uses Walther recursion) can be trivially transformed into a provably-terminating function by using the upper bound as an extra argument decremented on each iteration or recursion. For example,
quicksort Quicksort is an efficient, general-purpose sorting algorithm. Quicksort was developed by British computer scientist Tony Hoare in 1959 and published in 1961, it is still a commonly used algorithm for sorting. Overall, it is slightly faster than ...
is not trivially shown to be substructural recursive, but it only recurs to a maximum depth of the length of the vector (worst-case time complexity O(''n''2)). A quicksort implementation on lists (which would be rejected by a substructural recursive checker) is, using Haskell: import Data.List (partition) qsort [] = [] qsort = [a] qsort (a:as) = let (lesser, greater) = partition (++ qsort greater To make it substructural recursive using the length of the vector as a limit, we could do: import Data.List (partition) qsort x = qsortSub x x -- minimum case qsortSub [] as = as -- shows termination -- standard qsort cases qsortSub (l:ls) [] = [] -- nonrecursive, so accepted qsortSub (l:ls) = -- nonrecursive, so accepted qsortSub (l:ls) (a:as) = let (lesser, greater) = partition (++ qsortSub ls greater Some classes of algorithms have no theoretical upper bound but do have a practical upper bound (for example, some heuristic-based algorithms can be programmed to "give up" after so many recursions, also ensuring termination). Another outcome of total functional programming is that both strict evaluation and
lazy evaluation In programming language theory, lazy evaluation, or call-by-need, is an evaluation strategy which delays the evaluation of an expression until its value is needed ( non-strict evaluation) and which also avoids repeated evaluations (sharing). The ...
result in the same behaviour, in principle; however, one or the other may still be preferable (or even required) for performance reasons. In total functional programming, a distinction is made between
data In the pursuit of knowledge, data (; ) is a collection of discrete Value_(semiotics), values that convey information, describing quantity, qualitative property, quality, fact, statistics, other basic units of meaning, or simply sequences of sy ...
and codata—the former is
finitary In mathematics and logic, an operation is finitary if it has finite arity, i.e. if it has a finite number of input values. Similarly, an infinitary operation is one with an infinite number of input values. In standard mathematics, an operation ...
, while the latter is potentially infinite. Such potentially infinite data structures are used for applications such as I/O. Using codata entails the usage of such operations as
corecursion In computer science, corecursion is a type of operation that is dual to recursion. Whereas recursion works analytically, starting on data further from a base case and breaking it down into smaller data and repeating until one reaches a base case, ...
. However, it is possible to do I/O in a total functional programming language (with
dependent types In computer science and logic, a dependent type is a type whose definition depends on a value. It is an overlapping feature of type theory and type systems. In intuitionistic type theory, dependent types are used to encode logic's quantifiers lik ...
) also without codata.Archived copy
/ref> Both Epigram and
Charity Charity may refer to: Giving * Charitable organization or charity, a non-profit organization whose primary objectives are philanthropy and social well-being of persons * Charity (practice), the practice of being benevolent, giving and sharing * C ...
could be considered total functional programming languages, even though they do not work in the way
Turner Turner may refer to: People and fictional characters *Turner (surname), a common surname, including a list of people and fictional characters with the name * Turner (given name), a list of people with the given name *One who uses a lathe for turni ...
specifies in his paper. So could programming directly in plain
System F System F (also polymorphic lambda calculus or second-order lambda calculus) is a typed lambda calculus that introduces, to simply typed lambda calculus, a mechanism of universal quantification over types. System F formalizes parametric polymorph ...
, in Martin-Löf type theory or the
Calculus of Constructions In mathematical logic and computer science, the calculus of constructions (CoC) is a type theory created by Thierry Coquand. It can serve as both a typed programming language and as constructive foundation for mathematics. For this second reason, ...
.


See also

*
Termination analysis In computer science, termination analysis is program analysis which attempts to determine whether the evaluation of a given program halts for ''each'' input. This means to determine whether the input program computes a ''total'' function. It is c ...


References

{{DEFAULTSORT:Total Functional Programming Programming paradigms Functional programming Proof assistants