In computer science, partial application (or partial function application) refers to the process of fixing a number of arguments of a function, producing another function of smaller arity. Given a function
f\colon(X x Y x Z)\toN
partial(f)\colon(Y x Z)\toN
fpartial(2,3)
Intuitively, partial function application says "if you fix the first arguments 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: div1(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.
In languages such as ML, Haskell 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 implements partial application using the partial
function defined in its core library.[1]
The C++ standard library provides bind(function, args..)
to return a function object that is the result of partial application of the given arguments to the given function. Since C++20 the function bind_front(function, args...)
is also provided which binds the first sizeof...(args)
arguments of the function to the args. In contrast, bind
allows binding any of the arguments of the function passed to it, not just the first ones. Alternatively, lambda expressions can be used:
In Java, MethodHandle.bindTo
partially applies a function to its first argument.[2] Alternatively, since Java 8, lambdas can be used:
In Raku, the assuming
method creates a new function with fewer parameters.[3]
The Python standard library module functools
includes the partial
function, allowing positional and named argument bindings, returning a new function.[4]
In XQuery, an argument placeholder (?
) is used for each non-fixed argument in a partial function application.[5]
In the simply-typed lambda calculus with function and product types (λ→,×) 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
.
Partial application can be a useful way to define several useful notions in mathematics.
Given sets
X,Y
Z
f:X x Y → Z
f( ⋅ ,-):X → (Y → Z),
(Y → Z)
Y → Z
x\inX
f(x, ⋅ ):Y → Z
y\inY
f(x,y)
X,Y,Z
f( ⋅ ,-)
Y → Z
A group action can be understood as a function
*:G x X → X
\rho:G → Sym(X)\subset(X → X)
X
\rho
An inner-product on a vector space
V
K
\phi:V x V → K
\phi( ⋅ ,-):V → V*\subset(V → K)
The partial application of the cross product
x
R3
x ( ⋅ ,-):R3\mapstoEnd(R3)
u
Tu
Tu(v)=u x v
Tu
(Tu)ij=\epsilonijkuk
This is closely related to the adjoint map for Lie algebras. Lie algebras are equipped with a bracket
[ ⋅ , ⋅ ]:ak{g} x ak{g} → ak{g}
ad:ak{g} → End(ak{g})