In
computer science, partial application (or partial function application) refers to the process of fixing a number of arguments to a function, producing another function of smaller
arity
Arity () is the number of arguments or operands taken by a function, operation or relation in logic, mathematics, and computer science. In mathematics, arity may also be named ''rank'', but this word can have many other meanings in mathemati ...
. Given a function
, we might fix (or 'bind') the first argument, producing a function of type
. Evaluation of this function might be represented as
. Note that the result of partial function application in this case is a function that takes two arguments. Partial application is sometimes incorrectly called
currying
In mathematics and computer science, currying is the technique of translating the evaluation of a function that takes multiple arguments into evaluating a sequence of functions, each with a single argument. For example, currying a function f th ...
, which is a related, but distinct concept.
Motivation
Intuitively, partial function application says "if you fix the first
argument
An argument is a statement or group of statements called premises intended to determine the degree of truth or acceptability of another statement called conclusion. Arguments can be studied from three main perspectives: the logical, the dialectic ...
s of the function, you get a function of the remaining arguments". For example, if function ''div''(''x'',''y'') = ''x''/''y'', then ''div'' with the parameter ''x'' fixed at 1 is another function: ''div''
1(''y'') = ''div''(1,''y'') = 1/''y''. This is the same as the function ''inv'' that returns the multiplicative inverse of its argument, defined by ''inv''(''y'') = 1/''y''.
The practical motivation for partial application is that very often the functions obtained by supplying some but not all of the arguments to a function are useful; for example, many languages have a function or operator similar to
plus_one
. Partial application makes it easy to define these functions, for example by creating a function that represents the addition operator with 1 bound as its first argument.
Implementations
In languages such as
ML,
Haskell
Haskell () is a general-purpose, statically-typed, purely functional programming language with type inference and lazy evaluation. Designed for teaching, research and industrial applications, Haskell has pioneered a number of programming lang ...
and
F#, functions are defined in
curried form by default. Supplying fewer than the total number of arguments is referred to as partial application.
In languages with
first-class functions, one can define
curry
,
uncurry
and
papply
to perform currying and partial application explicitly. This might incur a greater run-time overhead due to the creation of additional
closures, while Haskell can use more efficient techniques.
Scala implements optional partial application with placeholder, e.g. returns an incrementing function. Scala also supports multiple parameter lists as currying, e.g. .
Clojure
Clojure (, like ''closure'') is a dynamic and functional dialect of the Lisp programming language on the Java platform. Like other Lisp dialects, Clojure treats code as data and has a Lisp macro system. The current development process is com ...
implements partial application using the
partial
function defined in its core library.
The
C++
C, or c, is the third letter in the Latin alphabet, used in the modern English alphabet, the alphabets of other western European languages and others worldwide. Its name in English is ''cee'' (pronounced ), plural ''cees''.
History
"C" ...
standard library provides
bind(function, args..)
to return a
function object
In computer programming, a function object is a construct allowing an object to be invoked or called as if it were an ordinary function, usually with the same syntax (a function parameter that can also be a function). Function objects are often ...
that is the result of partial application of the given arguments to the given function. Alternatively,
lambda expressions Lambda expression may refer to:
*Lambda expression in computer programming, also called an anonymous function
In computer programming, an anonymous function (function literal, lambda abstraction, lambda function, lambda expression or block) is a f ...
can be used:
int f(int a, int b);
auto f_partial = [](int a) ;
assert(f_partial(456) f(456, 123) );
In Java (programming language), Java,
MethodHandle.bindTo
partially applies a function to its first argument.
Alternatively, since Java 8, lambdas can be used:
public static Function partialApply(BiFunction biFunc, A value)
In
Raku, th
assuming
method creates a new function with fewer parameters.
The
Python
Python may refer to:
Snakes
* Pythonidae, a family of nonvenomous snakes found in Africa, Asia, and Australia
** ''Python'' (genus), a genus of Pythonidae found in Africa and Asia
* Python (mythology), a mythical serpent
Computing
* Python (pr ...
standard library module
functools
includes the
partial
function, allowing positional and named argument bindings, returning a new function.
In
XQuery
XQuery (XML Query) is a query and functional programming language that queries and transforms collections of structured and unstructured data, usually in the form of XML, text and with vendor-specific extensions for other data formats (JSON, b ...
, an argument placeholder (
?
) is used for each non-fixed argument in a partial function application.
Definitions
In the
simply-typed lambda calculus
The simply typed lambda calculus (\lambda^\to), a form
of type theory, is a typed interpretation of the lambda calculus with only one type constructor (\to) that builds function types. It is the canonical and simplest example of a typed lambda cal ...
with
function and
product type
In programming languages and type theory, a product of ''types'' is another, compounded, type in a structure. The "operands" of the product are types, and the structure of a product type is determined by the fixed order of the operands in the prod ...
s (''λ''
→,×) partial application, currying and uncurrying can be defined as
;
papply
: (((''a'' × ''b'') → ''c'') × ''a'') → (''b'' → ''c'') = ''λ''(''f'', ''x''). ''λy''. ''f'' (''x'', ''y'')
;
curry
: ((''a'' × ''b'') → ''c'') → (''a'' → (''b'' → ''c'')) = ''λf''. ''λx''. ''λy''. ''f'' (''x'', ''y'')
;
uncurry
: (''a'' → (''b'' → ''c'')) → ((''a'' × ''b'') → ''c'') = ''λf''. ''λ''(''x'', ''y''). ''f x y''
Note that
curry
papply
=
curry
.
Mathematical formulation and examples
Partial application can be a useful way to define several useful notions in mathematics.
Given sets
and
, and a function
, one can define the function
:
where
is the set of functions
. The image of
under this map is
. This is the function which sends
to
. There are often structures on
which mean that the image of
restricts to some subset of functions
, as illustrated in the following examples.
Group actions
A
group action
In mathematics, a group action on a space is a group homomorphism of a given group into the group of transformations of the space. Similarly, a group action on a mathematical structure is a group homomorphism of a group into the automorphism ...
can be understood as a function
. The partial evaluation
restricts to the group of bijections from
to itself. The group action axioms further ensure
is a
group homomorphism
In mathematics, given two groups, (''G'', ∗) and (''H'', ·), a group homomorphism from (''G'', ∗) to (''H'', ·) is a function ''h'' : ''G'' → ''H'' such that for all ''u'' and ''v'' in ''G'' it holds that
: h(u*v) = h(u) \cdot h(v)
...
.
Inner-products and canonical map to the dual
An
inner-product
In mathematics, an inner product space (or, rarely, a Hausdorff pre-Hilbert space) is a real vector space or a complex vector space with an operation called an inner product. The inner product of two vectors in the space is a scalar, often de ...
on a vector space
over a field
is a map
. The partial evaluation provides a canonical map to the
dual vector space
In mathematics, any vector space ''V'' has a corresponding dual vector space (or just dual space for short) consisting of all linear forms on ''V'', together with the vector space structure of pointwise addition and scalar multiplication by const ...
,
. If this is the inner-product of a
Hilbert space
In mathematics, Hilbert spaces (named after David Hilbert) allow generalizing the methods of linear algebra and calculus from (finite-dimensional) Euclidean vector spaces to spaces that may be infinite-dimensional. Hilbert spaces arise natural ...
, the
Riesz representation theorem
:''This article describes a theorem concerning the dual of a Hilbert space. For the theorems relating linear functionals to measures, see Riesz–Markov–Kakutani representation theorem.''
The Riesz representation theorem, sometimes called th ...
ensures this is an
isomorphism
In mathematics, an isomorphism is a structure-preserving mapping between two structures of the same type that can be reversed by an inverse mapping. Two mathematical structures are isomorphic if an isomorphism exists between them. The word is ...
.
Cross-products and the adjoint map for Lie algebras
The partial application of the cross product
on
is
. The image of the vector
is a linear map
such that
. The components of
can be found to be
.
This is closely related to the
adjoint map
In mathematics, the adjoint representation (or adjoint action) of a Lie group ''G'' is a way of representing the elements of the group as linear transformations of the group's Lie algebra, considered as a vector space. For example, if ''G'' is GL(n ...
for
Lie algebras
In mathematics, a Lie algebra (pronounced ) is a vector space \mathfrak g together with an operation called the Lie bracket, an alternating bilinear map \mathfrak g \times \mathfrak g \rightarrow \mathfrak g, that satisfies the Jacobi identit ...
. Lie algebras are equipped with a bracket
. The partial application gives a map
. The axioms for the bracket ensure this map is a homomorphism of Lie algebras.
See also
*
η-conversion
Lambda calculus (also written as ''λ''-calculus) is a formal system in mathematical logic for expressing computation based on function abstraction and application using variable binding and substitution. It is a universal model of computation t ...
*
POP-2
POP-2 (also referred to as POP2) is a programming language developed around 1970 from the earlier language POP-1 (developed by Robin Popplestone in 1968, originally named COWSEL) by Robin Popplestone and Rod Burstall at the University of Edin ...
*
Restriction (mathematics)
In mathematics, the restriction of a function f is a new function, denoted f\vert_A or f , obtained by choosing a smaller domain A for the original function f.
The function f is then said to extend f\vert_A.
Formal definition
Let f : E \to ...
, the more general phenomenon of restricting a function to a subset of its domain
References
Further reading
*
*
Benjamin C. Pierce et al
"Partial Application" {{Webarchive, url=https://web.archive.org/web/20160521182324/http://www.cis.upenn.edu/~bcpierce/sf/current/Poly.html#lab127 , date=2016-05-21 ''Software Foundations''.
External links
Partial function applicationon Rosetta code.
Partial applicationat Haskell Wiki
Constant applicative format Haskell Wiki
The dangers of being too partial
Functional programming
Implementation of functional programming languages
Articles with example Java code
Articles with example C++ code