In computer science and mathematical logic, a function type (or arrow type or exponential) is the type of a variable or parameter to which a function has or can be assigned, or an argument or result type of a higher-order function 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 {{nowrap|· → ·}}
, is a higher-kinded type). In theoretical settings and programming languages where functions are defined in curried form, such as the simply typed lambda calculus, 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. The class of such maps or functions is called the exponential object. The act of currying 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.
The syntax used for function types in several programming languages can be summarized, including an example type signature for the higher-order function composition function:
Language | Notation | Example type signature | |
---|---|---|---|
With first-class functions, parametric polymorphism | C# | Func<''α''<sub>1</sub>,''α''<sub>2</sub>,...,''α''<sub>''n''</sub>,''ρ''> | |
Haskell | ''α'' -> ''ρ'' | ||
OCaml | ''α'' -> ''ρ'' | ||
Scala | (''α''<sub>1</sub>,''α''<sub>2</sub>,...,''α''<sub>''n''</sub>) => ''ρ'' | ||
Standard ML | ''α'' -> ''ρ'' | ||
Swift | ''α'' -> ''ρ'' | ||
Rust | fn(''α''<sub>1</sub>,''α''<sub>2</sub>,...,''α''<sub>''n''</sub>) -> ''ρ'' | ||
With first-class functions, without parametric polymorphism | Go | func(''α''<sub>1</sub>,''α''<sub>2</sub>,...,''α''<sub>''n''</sub>) ''ρ'' | |
C++, Objective-C, with blocks | ''ρ'' (^)(''α''<sub>1</sub>,''α''<sub>2</sub>,...,''α''<sub>''n''</sub>) | ||
Without first-class functions, parametric polymorphism | C | ''ρ'' (*)(''α''<sub>1</sub>,''α''<sub>2</sub>,...,''α''<sub>''n''</sub>) | |
C++11 | Not unique.std::function<''ρ'' (''α''<sub>1</sub>,''α''<sub>2</sub>,...,''α''<sub>''n''</sub>)> is the more general type (see below). |
When looking at the example type signature of, for example C#, the type of the function is actually Func<Func<A,B>,Func<B,C>,Func<A,C>>
.
Due to type erasure in C++11's std::function
, it is more common to use templates for higher order function parameters and type inference (auto
) for closures.
The function type in programming languages does not correspond to the space of all set-theoretic functions. Given the countably infinite type of natural numbers 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.
Denotational semantics 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 functions is not sufficient either if the programming language allows writing non-terminating computations (which is the case if the programming language is Turing complete). Expression must be restricted to the so-called continuous functions (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.
. Benjamin C. Pierce. Types and Programming Languages. 2002. limited. The MIT Press. 99–100. 9780262162098 .
. John C. Mitchell. Foundations for Programming Languages. The MIT Press.