# Function type

> Mediated Wiki article. Canonical URL: https://mediated.wiki/source/Function_type
> Markdown URL: https://mediated.wiki/source/Function_type.md
> Source: https://en.wikipedia.org/wiki/Function_type
> Source revision: 1312812604
> License: Creative Commons Attribution-ShareAlike 4.0 International (https://creativecommons.org/licenses/by-sa/4.0/)

In [computer science](/source/Computer_science) and [mathematical logic](/source/Mathematical_logic), a **function type** (or **arrow type** or **exponential**) is the type of a [variable](/source/Variable_(computer_science)) or [parameter](/source/Parameter_(computer_science)) to which a [function](/source/Function_(computer_science)) has or can be assigned, or an argument or result type of a [higher-order function](/source/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](/source/Type_constructor) · → ·, is a [higher-kinded type](/source/Higher-kinded_type)). In theoretical settings and [programming languages](/source/Programming_language) where functions are defined in [curried form](/source/Curried_form), such as the [simply typed lambda calculus](/source/Simply_typed_lambda_calculus), a function type depends on exactly two types, the [domain](/source/Domain_of_a_function) *A* and the [range](/source/Range_of_a_function) *B*. Here a function type is often denoted *A* → *B*, following mathematical convention, or *B**A*, based on there existing exactly *B**A* (exponentially many) [set-theoretic functions](/source/Function_space) mappings *A* to *B* in the [category of sets](/source/Category_of_sets). The class of such maps or functions is called the [exponential object](/source/Exponential_object). The act of [currying](/source/Currying) makes the function type [adjoint](/source/Adjoint_functor) to the [product type](/source/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](/source/Dependent_type#Formal_definition), which among other properties, encompasses the idea of a [polymorphic function](/source/Polymorphism_(computer_science)).

## 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](/source/Function_composition_(computer_science)) function:

Language Notation Example type signature With first-class functions, parametric polymorphism C# Func<α1,α2,...,αn,ρ> Haskell α -> ρ OCaml α -> ρ Scala (α1,α2,...,αn) => ρ def compose[A, B, C](f: B => C, g: A => B): A => C Standard ML α -> ρ Swift α -> ρ Rust fn(α1,α2,...,αn) -> ρ With first-class functions, without parametric polymorphism Go func(α1,α2,...,αn) ρ C++, Objective-C, with blocks ρ (^)(α1,α2,...,αn) Without first-class functions, parametric polymorphism C ρ (*)(α1,α2,...,αn) C++11 Not unique. std::function<ρ (α1,α2,...,αn)> is the more general type (see below).

When looking at the example type signature of, for example C#, the type of the function compose is actually Func<Func<A,B>,Func<B,C>,Func<A,C>>.

Due to [type erasure](/source/Type_erasure) in C++11's std::function, it is more common to use [templates](/source/Template_(C%2B%2B)) for [higher order function](/source/Higher_order_function) parameters and [type inference](/source/Type_inference) (auto) for [closures](/source/Closure_(computer_programming)).

## Denotational semantics

The function type in programming languages does not correspond to the space of all set-theoretic functions. Given the [countably infinite](/source/Countably_infinite) type of [natural numbers](/source/Natural_number) as the domain and the Booleans as range, then there are an [uncountably infinite](/source/Uncountably_infinite) number (2ℵ0 = [c](/source/Cardinality_of_the_continuum)) 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](/source/Halting_problem).

[Denotational semantics](/source/Denotational_semantics) concerns itself with finding more appropriate models (called [domains](/source/Domain_theory)) to model programming language concepts such as function types. It turns out that restricting expression to the set of [computable functions](/source/Computable_function) is not sufficient either if the programming language allows writing [non-terminating computations](/source/Non-terminating_computation) (which is the case if the programming language is [Turing complete](/source/Turing_complete)). Expression must be restricted to the so-called *[continuous functions](/source/Continuous_functions#Continuous_functions_between_partially_ordered_sets)* (corresponding to continuity in the [Scott topology](/source/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. [clarification needed]

## See also

- [Cartesian closed category](/source/Cartesian_closed_category)
- [Currying](/source/Currying)
- [Exponential object](/source/Exponential_object), category-theoretic equivalent
- [First-class function](/source/First-class_function)
- [Function space](/source/Function_space), set-theoretic equivalent

## References

- Pierce, Benjamin C. (2002). [*Types and Programming Languages*](https://archive.org/details/typesprogramming00pier_207). The MIT Press. pp. [99](https://archive.org/details/typesprogramming00pier_207/page/n122)–100. ISBN 9780262162098.
- Mitchell, John C. *Foundations for Programming Languages*. The MIT Press.
- [*Homotopy Type Theory: Univalent Foundations of Mathematics*, The Univalent Foundations Program, Institute for Advanced Study](http://homotopytypetheory.org/2013/06/20/the-hott-book/). *See section 1.2*.

---
Adapted from the Wikipedia article [Function type](https://en.wikipedia.org/wiki/Function_type) by Wikipedia contributors ([contributor history](https://en.wikipedia.org/wiki/Function_type?action=history)). Available under [Creative Commons Attribution-ShareAlike 4.0 International](https://creativecommons.org/licenses/by-sa/4.0/). Changes may have been made.
