{{Short description|Evaluation of a function on its argument}}{{More citations needed|date=February 2024}} In mathematics, '''function application''' (or '''evaluation''') is the act of taking a function and an input from its domain to obtain the corresponding value from its range. In this sense, function application can be thought of as the opposite of function abstraction.<ref>{{Citation |last1=Alama |first1=Jesse |title=The Lambda Calculus |date=2023 |encyclopedia=The Stanford Encyclopedia of Philosophy |editor-last=Zalta |editor-first=Edward N. |url=https://plato.stanford.edu/archives/win2023/entries/lambda-calculus/ |access-date=2024-02-29 |edition=Winter 2023 |publisher=Metaphysics Research Lab, Stanford University |last2=Korbmacher |first2=Johannes |editor2-last=Nodelman |editor2-first=Uri}}</ref>
It is central to programming languages derived from lambda calculus, such as LISP and Scheme, and also in functional languages. It has a role in the study of the denotational semantics of computer programs, because it is a continuous function on complete partial orders. Function application is also a continuous function in homotopy theory, and, indeed underpins the entire theory: it allows a homotopy deformation to be viewed as a continuous path in the space of functions. Likewise, valid mutations (refactorings) of computer programs can be seen as those that are "continuous" in the Scott topology.
The most general setting for function application is in category theory, where it is right adjoint to currying in closed monoidal categories. A special case of this are the Cartesian closed categories, whose internal language is simply typed lambda calculus.
==Representation== Function application is usually depicted by juxtaposing the variable representing the function with its argument encompassed in parentheses. For example, the following expression represents the application of the function ''ƒ'' to its argument ''x''.
:<math>f(x) </math>
In some instances, a different notation is used where the parentheses aren't required, and function application can be expressed just by juxtaposition. For example, the following expression can be considered the same as the previous one:
:<math>f\; x</math>
The latter notation is especially useful in combination with the currying isomorphism. Given a function <math>f : (X \times Y) \to Z</math>, its application is represented as <math>f(x, y)</math> by the former notation and <math>f\;(x,y)</math> (or <math>f \; \langle x, y \rangle</math> with the argument <math>\langle x, y \rangle \in X \times Y</math> written with the less common angle brackets) by the latter. However, functions in curried form <math>f : X \to (Y \to Z)</math> can be represented by juxtaposing their arguments: <math>f\; x \; y</math>, rather than <math>f(x)(y)</math>. This relies on function application being left-associative.
When mathematical notation is represented in a digital document, the invisible zero-width Unicode characters {{unichar|2061}} and {{unichar|2062}} can be used to distinguish concatenation meaning function application from concatenation meaning multiplication.
==As an operator==
Function application can be defined as an operator, called apply or <math>\$</math>, by the following definition:
:<math>f \mathop{\,\$\,} x = f(x)</math>
The operator may also be denoted by a backtick (`).
If the operator is understood to be of low precedence and right-associative, the application operator can be used to cut down on the number of parentheses needed in an expression. For example;
:<math>f(g(h(j(x)))) </math>
can be rewritten as:
:<math>f \mathop{\,\$\,} g \mathop{\,\$\,} h \mathop{\,\$\,} j \mathop{\,\$\,} x</math>
This can be equivalently expressed using function composition as:
:<math>(f \circ g \circ h \circ j)(x)</math>
or even:
:<math>(f \circ g \circ h \circ j \circ x)()</math>
if one considers <math>x</math> to be a constant function returning <math>x</math>.
== Set theory == In axiomatic set theory, especially Zermelo–Fraenkel set theory, a function <math>f: D \mapsto R</math> is often defined as a relation (<math>f \subseteq D \times R</math>) having the property that, for any <math>x \in D</math> there is a unique <math>y \in R</math> such that <math>(x,y) \in f</math>.
One is usually not content to write "<math>(x,y) \in f</math>" to specify that <math>y</math>, and usually wishes for the more common function notation "<math>f(x) = y </math>" . Function application, or more specifically, the notation "<math>f(x)</math>", is not present in the usual signature of set theory, but may be added to the theory as a binary function symbol <math>\bullet ( \bullet )</math> if desired without any loss of expressiveness by defining:<ref>{{Cite book |last=Mendelson |author-link=Elliott Mendelson |first=Elliott |url=https://www.routledge.com/Introduction-to-Mathematical-Logic/Mendelson/p/book/9781032919140 |title=Introduction to Mathematical Logic |date=2015 |publisher=CRC Press |isbn=978-1-4822-3778-8 |edition=6th |language=en |pp=102-103, 231, 235, 245-246 }}</ref>
<math display="block">X(Y) = \left\{ \begin{array}{lll} z & \text{if } X \text{ is a function, and } (Y,z) \in X \\ \varnothing & \text{otherwise} \\ \end{array} \right.</math>
Or, more formally:<ref>{{Cite book |last=Suppes |first=Patrick |author-link=Patrick Suppes |url=https://archive.org/details/axiomaticsettheo00supp_0/ |title=Axiomatic set theory |date=1972 |publisher=New York, Dover Publications |others=Internet Archive |isbn=978-0-486-61630-8 |pages=87}}</ref><ref>{{Cite book |last=Lévy |first=Azriel |url=https://archive.org/details/basicsettheory00levy_0/mode/2up |title=Basic set theory |date=1979 |publisher=Springer-Verlag |isbn=978-0-387-08417-6 |location=Berlin; New York |pages=27}}</ref>
<math display="block">X(Y) = z \iff (\exists D,R ( X \in R^D \and ((Y,z) \in X))) \or (\forall D,R(X \notin R^D \or (Y,z) \notin X) \and z = \varnothing),</math>
where <math>R^D</math> denotes set exponentiation: the set of all functions from <math>D</math> to <math>R</math>.
In prose: <math>X(Y) = z</math> if there exists a domain <math>D</math> and range <math>R</math> such that <math>X</math> is a function from <math>D</math> to <math>R</math> and <math>(Y,z) \in X</math>; or (the negation of former) and <math>z = \varnothing.</math> The choice of using the empty set <math>\varnothing</math> when <math>X(Y)</math> is undefined is arbitrary to ensure that the notation defined for the entire domain of discourse.<ref>{{Cite book |last=Lévy |first=Azriel |url=https://archive.org/details/basicsettheory00levy_0/mode/2up |title=Basic set theory |date=1979 |publisher=Springer-Verlag |isbn=978-0-387-08417-6 |location=Berlin; New York |pages=15}}</ref>
If <math>\Psi(X,Y,z)</math> denotes the formula on the right side of the biconditional above, for any two sets, <math>X,Y</math> the formula <math>\Psi </math> associates a unique object <math>z</math>: <math>\forall X,Y \, \exists! z \, \Psi(X,Y,z)</math>. Therefore the language of set theory can use an extension by definition to include the function application operation <math>\bullet ( \bullet )</math> conservatively.
== Programming ==
In computer programming, function application often refers to calling or executing a procedure, rather than a true mathematical function (see functions in computer programing), with similar rules for its behavior.
Function application corresponds to beta reduction in lambda calculus.
=== Apply function ===
Related to the apply operator, the function '''apply''' applies a function to a variable ''list'' of arguments. ''Eval'' and apply are the two interdependent components of the ''eval-apply cycle'' in Lisp, described in SICP.<ref>Harold Abelson, Gerald Jay Sussman, Julie Sussman, ''Structure and Interpretation of Computer Programs'', (1996) MIT Press, {{ISBN|0-262-01153-0}}. ''See [http://mitpress.mit.edu/sicp/full-text/book/book-Z-H-26.html Section 4.1, The Metacircular Evaluator]''</ref> This is suported in languages with variadic functions, because this is the only way to call a function with an indeterminate (at compile time) number of arguments.
==== Common Lisp and Scheme ==== In Common Lisp '''apply''' is a function that applies a function to a list of arguments (note here that "+" is a variadic function that takes any number of arguments):<syntaxhighlight lang="lisp">(apply #'+ (list 1 2))</syntaxhighlight>Similarly in Scheme:<syntaxhighlight lang="scheme">(apply + (list 1 2))</syntaxhighlight>
==== C++ ==== In C++, Bind <ref>{{Cite web |title=Boost: Bind.HPP documentation - 1.49.0 |url=http://www.boost.org/doc/libs/1_49_0/libs/bind/bind.html#with_functions}}</ref> is used either via the std namespace or via the boost namespace.
==== C# and Java ==== In C# and Java, variadic arguments are simply collected in an array. Caller can explicitly pass in an array in place of the variadic arguments. This can only be done for a variadic parameter. It is not possible to apply an array of arguments to non-variadic parameter without using reflection. An ambiguous case arises should the caller want to pass an array itself as one of the arguments rather than using the array as a ''list'' of arguments. In this case, the caller should cast the array to <code>Object</code> to prevent the compiler from using the ''apply'' interpretation.<syntaxhighlight lang="java">variadicFunc(arrayOfArgs);</syntaxhighlight>With version 8 lambda expressions were introduced. Functions are implemented as objects with a functional interface, an interface with only one non-static method. The standard interface<syntaxhighlight lang="java">Function<T,R></syntaxhighlight>consist of the method (plus some static utility functions):<syntaxhighlight lang="java">R apply(T para)</syntaxhighlight>
==== Go ==== In Go, typed variadic arguments are simply collected in a slice. The caller can explicitly pass in a slice in place of the variadic arguments, by appending a <code>...</code> to the slice argument. This can only be done for a variadic parameter. The caller can not apply an array of arguments to non-variadic parameters, without using reflection..{{sxhl|1=s := []string{"foo", "bar"} variadicFunc(s...)|2=go}}
==== JavaScript ==== In JavaScript, function objects have an <code>apply</code> method, the first argument is the value of the <code>this</code> keyword inside the function; the second is the list of arguments:<syntaxhighlight lang="javascript">func.apply(null, args);</syntaxhighlight>ES6 adds the spread operator <code>func(...args)</code><ref>{{cite web |title=Spread syntax - JavaScript {{Pipe}} MDN |url=https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Operators/Spread_operator |access-date=2017-04-20}}</ref> which may be used instead of <code>apply</code>.
==== Lua ==== In Lua, apply can be written this way:<syntaxhighlight lang="lua">function apply(f,...) return f(...) end </syntaxhighlight>
==== Perl ==== In Perl, arrays, hashes and expressions are automatically "flattened" into a single list when evaluated in a list context, such as in the argument list of a function<syntaxhighlight lang="perl"> # Equivalent subroutine calls: @args = (@some_args, @more_args); func(@args);
func(@some_args, @more_args); </syntaxhighlight>
==== PHP ==== In PHP, <code>apply</code> is called <code>call_user_func_array</code>:<syntaxhighlight lang="php">call_user_func_array('func_name', $args);</syntaxhighlight>
==== Python and Ruby ==== In Python and Ruby, the same asterisk notation used in defining variadic functions is used for calling a function on a sequence and array respectively:<syntaxhighlight lang="python">func(*args)</syntaxhighlight>Python originally had an apply function, but this was deprecated in favour of the asterisk in 2.3 and removed in 3.0.<ref>{{cite web |date=8 February 2005 |title=Non-essential built-in functions |url=https://docs.python.org/release/2.3.5/lib/non-essential-built-in-funcs.html |accessdate=19 May 2013 |work=Python Library Reference}}</ref>
==== R ==== In R, <code lang="rsplus">do.call</code> constructs and executes a function call from a name or a function and a list of arguments to be passed to it:<syntaxhighlight lang="r"> f(x1, x2) # can also be performed via do.call(what = f, args = list(x1, x2)) </syntaxhighlight>
==== Smalltalk ==== In Smalltalk, block (function) objects have a <code>valueWithArguments:</code> method which takes an array of arguments:<syntaxhighlight lang="smalltalk">aBlock valueWithArguments: args</syntaxhighlight>
==== Tcl ==== Since Tcl 8.5,<ref>{{cite web |date=2006 |title=apply |url=https://tmml.sourceforge.net/doc/tcl/apply.html |accessdate=23 June 2014 |work=Tcl documentation}}</ref> a function can be applied to arguments with the <code>apply</code> command<syntaxhighlight lang="tcl">apply func ?arg1 arg2 ...?</syntaxhighlight>where the function is a two element list {args body} or a three element list {args body namespace}.
== Universal property == Consider a function <math>g:(X\times Y)\to Z</math>, that is, <math>g\isin [(X\times Y)\to Z]</math> where the bracket notation <math>[A\to B]</math> denotes the space of functions from ''A'' to ''B''. By means of currying, there is a unique function <math>\mbox{curry}(g) :X\to [Y\to Z]</math><!--something like this is needed, because not everybody knows what does currying means: , where <math>curry (f)\stackrel{def}{=}\lambda x.(\lambda y.(g(x,y))</math> with type <math>curry:((\alpha\times\beta)\to\gamma)\to(\alpha\to(\beta\to\gamma))</math> here <math>\lambda</math> is the abstractor of the <math>\lambda</math>-calculus The lambda may be confuse the reader not familiar with lambda calculus and its use in category theory as told below-->. Then '''Apply''' provides the universal morphism
: <math>\mbox{Apply}:([Y\to Z]\times Y) \to Z</math>,
so that
: <math>\mbox {Apply}(f,y)=f(y)</math>
or, equivalently one has the commuting diagram<!--it is not clear what does this mean. can a pair of functions be applied to an object?, this needs an explanation-->
: <math>\mbox{Apply} \circ \left( \mbox{curry}(g) \times \mbox{id}_Y \right) = g</math>
More precisely, curry and apply are adjoint functors.<!--Maybe something like this can improve reading :<math>f(x)=g(f,x)</math> :<math>= (\mbox{Apply} \circ (\mbox{curry}(g),\mbox{id}_Y))(f,x)</math> :<math>= (\mbox{Apply} ((\mbox{curry}(g),\mbox{id}_Y ))(f,x))</math> :<math>= (\mbox{Apply} ((\mbox{curry}(g))(f),\mbox{id}_Y x)) </math>-->
The notation <math>[A\to B]</math> for the space of functions from ''A'' to ''B'' occurs more commonly in computer science. In category theory, however, <math>[A\to B]</math> is known as the exponential object, and is written as <math>B^A</math>. There are other common notational differences as well; for example ''Apply'' is often called ''Eval'',<ref>Saunders Mac Lane, ''Category Theory''</ref> even though in computer science, these are not the same thing, with eval distinguished from ''Apply'', as being the evaluation of the quoted string form of a function with its arguments, rather than the application of a function to some arguments.
Also, in category theory, ''curry'' is commonly denoted by <math>\lambda</math>, so that <math>\lambda g</math> is written for ''curry''(''g''). This notation is in conflict with the use of <math>\lambda</math> in lambda calculus, where lambda is used to denote bound variables. With all of these notational changes accounted for, the adjointness of ''Apply'' and ''curry'' is then expressed in the commuting diagram center|Universal property of the exponential object The articles on exponential object and Cartesian closed category provide a more precise discussion of the category-theoretic formulation of this idea. Thus the use of lambda here is not accidental; the internal language of Cartesian closed categories is simply typed lambda calculus. The most general possible setting for ''Apply'' are the closed monoidal categories, of which the cartesian closed categories are an example. In homological algebra, the adjointness of curry and apply is known as tensor-hom adjunction.
== Topological properties == In order theory, in the category of complete partial orders endowed with the Scott topology, both ''curry'' and ''apply'' are continuous functions (that is, they are Scott continuous).<ref>H.P. Barendregt, ''The Lambda Calculus'', (1984) North-Holland {{ISBN|0-444-87508-5}}</ref> This property helps establish the foundational validity of the study of the denotational semantics of computer programs.
In algebraic geometry and homotopy theory, ''curry'' and ''apply'' are both continuous functions when the space <math>Y^X</math> of continuous functions from <math>X</math> to <math>Y</math> is given the compact open topology, and <math>X</math> is locally compact Hausdorff. This result is very important, in that it underpins homotopy theory, allowing homotopic deformations to be understood as continuous paths in the space of functions.
==Other instances== The Curry–Howard correspondence relates function application to the logical rule of modus ponens.
== See also == {{Wiktionary|apply}} * Evaluation strategy * Execution (computing) * Function composition * Polish notation
==References== {{Reflist}}
Category:Functions and mappings Category:Adjoint functors Category:Lambda calculus Category:Functional programming Category:Higher-order functions