In
computer science
Computer science is the study of computation, information, and automation. Computer science spans Theoretical computer science, theoretical disciplines (such as algorithms, theory of computation, and information theory) to Applied science, ...
and
mathematical logic
Mathematical logic is the study of Logic#Formal logic, formal logic within mathematics. Major subareas include model theory, proof theory, set theory, and recursion theory (also known as computability theory). Research in mathematical logic com ...
, a function type (or arrow type or exponential) is the type of a
variable or
parameter
A parameter (), generally, is any characteristic that can help in defining or classifying a particular system (meaning an event, project, object, situation, etc.). That is, a parameter is an element of a system that is useful, or critical, when ...
to which a
function has or can be assigned, or an argument or result type of a
higher-order function In mathematics and computer science, a higher-order function (HOF) is a function that does at least one of the following:
* takes one or more functions as arguments (i.e. a procedural parameter, which is a parameter of a procedure that is itself ...
taking or returning a function.
A function type depends on the type of the parameters and the result type of the function (it, or more accurately the unapplied
type constructor
, is a
higher-kinded type). In theoretical settings and
programming language
A programming language is a system of notation for writing computer programs.
Programming languages are described in terms of their Syntax (programming languages), syntax (form) and semantics (computer science), semantics (meaning), usually def ...
s where functions are defined in
curried form, such as the
simply typed lambda calculus
The simply typed lambda calculus (), a form
of type theory, is a typed interpretation of the lambda calculus with only one type constructor () that builds function types. It is the canonical and simplest example of a typed lambda calculus. The ...
, a function type depends on exactly two types, the
domain ''A'' and the
range ''B''. Here a function type is often denoted , following mathematical convention, or , based on there existing exactly (exponentially many)
set-theoretic functions mappings ''A'' to ''B'' in the
category of sets
In the mathematical field of category theory, the category of sets, denoted by Set, is the category whose objects are sets. The arrows or morphisms between sets ''A'' and ''B'' are the functions from ''A'' to ''B'', and the composition of mor ...
. The class of such maps or functions is called the
exponential object
In mathematics, specifically in category theory, an exponential object or map object is the category theory, categorical generalization of a function space in set theory. Category (mathematics), Categories with all Product (category theory), fini ...
. The act of
currying
In mathematics and computer science, currying is the technique of translating a function that takes multiple arguments into a sequence of families of functions, each taking a single argument.
In the prototypical example, one begins with a functi ...
makes the function type
adjoint to the
product type; this is explored in detail in the article on currying.
The function type can be considered to be a special case of the
dependent product type, which among other properties, encompasses the idea of a
polymorphic function.
Programming languages
The syntax used for function types in several programming languages can be summarized, including an example type signature for the higher-order
function composition
In mathematics, the composition operator \circ takes two function (mathematics), functions, f and g, and returns a new function h(x) := (g \circ f) (x) = g(f(x)). Thus, the function is function application, applied after applying to . (g \c ...
function:
When looking at the example type signature of, for example C#, the type of the function is actually
Func,Func<B,C>,Func>
.
Due to
type erasure
In programming languages, type erasure is the load-time process by which explicit type annotations are removed from a program, before it is executed at run-time. Operational semantics not requiring programs to be accompanied by types are named ...
in C++11's
std::function
, it is more common to use
templates for
higher order function parameters and
type inference
Type inference, sometimes called type reconstruction, refers to the automatic detection of the type of an expression in a formal language. These include programming languages and mathematical type systems, but also natural languages in some bran ...
(
auto
) for
closures.
Denotational semantics
The function type in programming languages does not correspond to the space of all set-theoretic functions. Given the
countably infinite
In mathematics, a set is countable if either it is finite or it can be made in one to one correspondence with the set of natural numbers. Equivalently, a set is ''countable'' if there exists an injective function from it into the natural numbe ...
type of
natural number
In mathematics, the natural numbers are the numbers 0, 1, 2, 3, and so on, possibly excluding 0. Some start counting with 0, defining the natural numbers as the non-negative integers , while others start with 1, defining them as the positive in ...
s as the domain and the booleans as range, then there are an
uncountably infinite number (2
ℵ0 =
c) of set-theoretic functions between them. Clearly this space of functions is larger than the number of functions that can be defined in any programming language, as there exist only countably many programs (a program being a finite sequence of a finite number of symbols) and one of the set-theoretic functions effectively solves the
halting problem
In computability theory (computer science), computability theory, the halting problem is the problem of determining, from a description of an arbitrary computer program and an input, whether the program will finish running, or continue to run for ...
.
Denotational semantics
In computer science, denotational semantics (initially known as mathematical semantics or Scott–Strachey semantics) is an approach of formalizing the meanings of programming languages by constructing mathematical objects (called ''denotations'' ...
concerns itself with finding more appropriate models (called
domains) to model programming language concepts such as function types. It turns out that restricting expression to the set of
computable function
Computable functions are the basic objects of study in computability theory. Informally, a function is ''computable'' if there is an algorithm that computes the value of the function for every value of its argument. Because of the lack of a precis ...
s is not sufficient either if the programming language allows writing
non-terminating computations (which is the case if the programming language is
Turing complete
Alan Mathison Turing (; 23 June 1912 – 7 June 1954) was an English mathematician, computer scientist, logician, cryptanalyst, philosopher and theoretical biologist. He was highly influential in the development of theoretical comput ...
). Expression must be restricted to the so-called ''
continuous functions
In mathematics, a continuous function is a function such that a small variation of the argument induces a small variation of the value of the function. This implies there are no abrupt changes in value, known as '' discontinuities''. More preci ...
'' (corresponding to continuity in the
Scott topology, not continuity in the real analytical sense). Even then, the set of continuous function contains the ''parallel-or'' function, which cannot be correctly defined in all programming languages.
See also
*
Cartesian closed category
In category theory, a Category (mathematics), category is Cartesian closed if, roughly speaking, any morphism defined on a product (category theory), product of two Object (category theory), objects can be naturally identified with a morphism defin ...
*
Currying
In mathematics and computer science, currying is the technique of translating a function that takes multiple arguments into a sequence of families of functions, each taking a single argument.
In the prototypical example, one begins with a functi ...
*
Exponential object
In mathematics, specifically in category theory, an exponential object or map object is the category theory, categorical generalization of a function space in set theory. Category (mathematics), Categories with all Product (category theory), fini ...
, category-theoretic equivalent
*
First-class function
In computer science, a programming language is said to have first-class functions if it treats function (programming), functions as first-class citizens. This means the language supports passing functions as arguments to other functions, returning ...
*
Function space
In mathematics, a function space is a set of functions between two fixed sets. Often, the domain and/or codomain will have additional structure which is inherited by the function space. For example, the set of functions from any set into a ve ...
, set-theoretic equivalent
References
*
*
*
''Homotopy Type Theory: Univalent Foundations of Mathematics'', The Univalent Foundations Program, Institute for Advanced Study ''See section 1.2''.
{{Data types
Data types
Subroutines
Type theory