Jump to content

Function application

From Wikipedia, the free encyclopedia

In mathematics, function application 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.[1]

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

[edit]

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.

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:

The latter notation is especially useful in combination with the currying isomorphism. Given a function , its application is represented as by the former notation and (or with the argument written with the less common angle brackets) by the latter. However, functions in curried form can be represented by juxtaposing their arguments: , rather than . This relies on function application being left-associative.

When mathematical notation is represented in a digital document, the invisible zero-width Unicode characters U+2061 FUNCTION APPLICATION and U+2062 INVISIBLE TIMES can be used to distinguish concatenation meaning function application from concatenation meaning multiplication.

As an operator

[edit]

Function application can be defined as an operator, called apply or , by the following definition:

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;

can be rewritten as:

This can be equivalently expressed using function composition as:

or even:

if one considers to be a constant function returning .

Set theory

[edit]

In axiomatic set theory, especially Zermelo–Fraenkel set theory, a function is often defined as a relation () having the property that, for any there is a unique such that .

One is usually not content to write "" to specify that , and usually wishes for the more common function notation "" . Function application, or more specifically, the notation "", is not present in the usual signature of set theory, but may be added to the theory as a binary function symbol if desired without any loss of expressiveness by defining:[2]

Or, more formally:[3][4]

where denotes set exponentiation: the set of all functions from to .

In prose: if there exists a domain and range such that is a function from to and ; or (the negation of former) and The choice of using the empty set when is undefined is arbitrary to ensure that the notation defined for the entire domain of discourse.[5]

If denotes the formula on the right side of the biconditional above, for any two sets, the formula associates a unique object : . Therefore the language of set theory can use an extension by definition to include the function application operation conservatively.

Programming

[edit]

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

[edit]

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.[6] 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

[edit]

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):

(apply #'+ (list 1 2))

Similarly in Scheme:

(apply + (list 1 2))

C++

[edit]

In C++, Bind [7] is used either via the std namespace or via the boost namespace.

C# and Java

[edit]

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 Object to prevent the compiler from using the apply interpretation.

variadicFunc(arrayOfArgs);

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

Function<T,R>

consist of the method (plus some static utility functions):

R apply(T para)

Go

[edit]

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 ... 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..

s := []string{"foo", "bar"}
variadicFunc(s...)

JavaScript

[edit]

In JavaScript, function objects have an apply method, the first argument is the value of the this keyword inside the function; the second is the list of arguments:

func.apply(null, args);

ES6 adds the spread operator func(...args)[8] which may be used instead of apply.

Lua

[edit]

In Lua, apply can be written this way:

function apply(f,...)
  return f(...)
end

Perl

[edit]

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

# Equivalent subroutine calls:
@args = (@some_args, @more_args);
func(@args);

func(@some_args, @more_args);

PHP

[edit]

In PHP, apply is called call_user_func_array:

call_user_func_array('func_name', $args);

Python and Ruby

[edit]

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:

func(*args)

Python originally had an apply function, but this was deprecated in favour of the asterisk in 2.3 and removed in 3.0.[9]

R

[edit]

In R, do.call constructs and executes a function call from a name or a function and a list of arguments to be passed to it:

f(x1, x2)
# can also be performed via
do.call(what = f, args = list(x1, x2))

Smalltalk

[edit]

In Smalltalk, block (function) objects have a valueWithArguments: method which takes an array of arguments:

aBlock valueWithArguments: args

Tcl

[edit]

Since Tcl 8.5,[10] a function can be applied to arguments with the apply command

apply func ?arg1 arg2 ...?

where the function is a two element list {args body} or a three element list {args body namespace}.

Universal property

[edit]

Consider a function , that is, where the bracket notation denotes the space of functions from A to B. By means of currying, there is a unique function . Then Apply provides the universal morphism

,

so that

or, equivalently one has the commuting diagram

More precisely, curry and apply are adjoint functors.

The notation for the space of functions from A to B occurs more commonly in computer science. In category theory, however, is known as the exponential object, and is written as . There are other common notational differences as well; for example Apply is often called Eval,[11] 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 , so that is written for curry(g). This notation is in conflict with the use of 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

Universal property of the exponential object
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

[edit]

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).[12] 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 of continuous functions from to is given the compact open topology, and 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

[edit]

The Curry–Howard correspondence relates function application to the logical rule of modus ponens.

See also

[edit]

References

[edit]
  1. ^ Alama, Jesse; Korbmacher, Johannes (2023), "The Lambda Calculus", in Zalta, Edward N.; Nodelman, Uri (eds.), The Stanford Encyclopedia of Philosophy (Winter 2023 ed.), Metaphysics Research Lab, Stanford University, retrieved 2024-02-29
  2. ^ Mendelson, Elliott (2015). Introduction to Mathematical Logic (6th ed.). CRC Press. pp. 102–103, 231, 235, 245–246. ISBN 978-1-4822-3778-8.
  3. ^ Suppes, Patrick (1972). Axiomatic set theory. Internet Archive. New York, Dover Publications. p. 87. ISBN 978-0-486-61630-8.
  4. ^ Lévy, Azriel (1979). Basic set theory. Berlin; New York: Springer-Verlag. p. 27. ISBN 978-0-387-08417-6.
  5. ^ Lévy, Azriel (1979). Basic set theory. Berlin; New York: Springer-Verlag. p. 15. ISBN 978-0-387-08417-6.
  6. ^ Harold Abelson, Gerald Jay Sussman, Julie Sussman, Structure and Interpretation of Computer Programs, (1996) MIT Press, ISBN 0-262-01153-0. See Section 4.1, The Metacircular Evaluator
  7. ^ "Boost: Bind.HPP documentation - 1.49.0".
  8. ^ "Spread syntax - JavaScript | MDN". Retrieved 2017-04-20.
  9. ^ "Non-essential built-in functions". Python Library Reference. 8 February 2005. Retrieved 19 May 2013.
  10. ^ "apply". Tcl documentation. 2006. Retrieved 23 June 2014.
  11. ^ Saunders Mac Lane, Category Theory
  12. ^ H.P. Barendregt, The Lambda Calculus, (1984) North-Holland ISBN 0-444-87508-5