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 tha ...
) 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 mathematic ...
, which operates only upon 'reduced' forms of its arguments, such as Walther recursion, substructural recursion, or "strongly normalizing" as proven by
abstract interpretation In computer science, abstract interpretation is a theory of sound approximation of the semantics of computer programs, based on monotonic functions over ordered sets, especially lattices. It can be viewed as a partial execution of a computer ...
of code. # Every function must be a total (as opposed to
partial Partial may refer to: Mathematics *Partial derivative, derivative with respect to one of several variables of a function, with the other variables held constant ** ∂, a symbol that can denote a partial derivative, sometimes pronounced "partial d ...
) 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 In a programming language, an evaluation strategy is a set of rules for evaluating expressions. The term is often used to refer to the more specific notion of a ''parameter-passing strategy'' that defines the kind of value that is passed to the f ...
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 values that convey information, describing quantity, quality, fact, statistics, other basic units of meaning, or simply sequences of symbols that may be further interpret ...
and
codata The Committee on Data of the International Science Council (CODATA) was established in 1966 as the Committee on Data for Science and Technology, originally part of the International Council of Scientific Unions, now part of the International ...
—the former is finitary, 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. However, it is possible to do I/O in a total functional programming language (with dependent types) also without codata.Archived copy
/ref> Both
Epigram An epigram is a brief, interesting, memorable, and sometimes surprising or satirical statement. The word is derived from the Greek "inscription" from "to write on, to inscribe", and the literary device has been employed for over two mill ...
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 Intuitionistic type theory (also known as constructive type theory, or Martin-Löf type theory) is a type theory and an alternative foundation of mathematics. Intuitionistic type theory was created by Per Martin-Löf, a Swedish mathematician and p ...
or the Calculus of Constructions.


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