# Partial application

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

In functional programming

Not to be confused with [Partial evaluation](/source/Partial_evaluation) or [Partial function](/source/Partial_function).

In [computer science](/source/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](/source/Arity). Given a function f : ( X × Y × Z ) → N {\displaystyle f\colon (X\times Y\times Z)\to N} , we might fix (or 'bind') the first argument, producing a function of type partial ( f ) : ( Y × Z ) → N {\displaystyle {\text{partial}}(f)\colon (Y\times Z)\to N} . Evaluation of this function might be represented as f partial ( 2 , 3 ) {\displaystyle f_{\text{partial}}(2,3)} . 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](/source/Currying), which is a related, but distinct concept.

## Motivation

Intuitively, partial function application says "if you fix the first [arguments](/source/Parameter_(computer_science)) 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](/source/ML_(programming_language)), [Haskell](/source/Haskell_(programming_language)) and [F#](/source/F_Sharp_(programming_language)), functions are defined in [curried](/source/Currying) form by default. Supplying fewer than the total number of arguments is referred to as partial application.

In languages with [first-class functions](/source/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](/source/Closure_(computer_science)), while Haskell can use more efficient techniques.[1]

[Scala](/source/Scala_(programming_language)) implements optional partial application with placeholder, e.g. def add(x: Int, y: Int) = {x+y}; add(1, _: Int) returns an incrementing function. Scala also supports multiple parameter lists as currying, e.g. def add(x: Int)(y: Int) = {x+y}; add(1) _.

[Clojure](/source/Clojure) implements partial application using the partial function defined in its core library.[2]

The [C++](/source/C%2B%2B) standard library provides bind(function, args..) to return a [function object](/source/Function_object) that is the result of partial application of the given arguments to the given function. Since [C++20](/source/C%2B%2B20) 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](/source/Anonymous_function#C++_(since_C++11)) can be used:

int f(int a, int b);
auto f_partial = [](int a) { return f(a, 123); };
assert(f_partial(456) == f(456, 123) );

In [Java](/source/Java_(programming_language)), MethodHandle.bindTo partially applies a function to its first argument.[3] Alternatively, since Java 8, lambdas can be used:

public static <A, B, R> Function<B, R> partialApply(BiFunction<A, B, R> biFunc, A value) {
    return b -> biFunc.apply(value, b);
}

In [Raku](/source/Raku_(programming_language)), the [assuming](https://docs.perl6.org/routine/assuming) method creates a new function with fewer parameters.[4]

The [Python](/source/Python_(programming_language)) standard library module functools includes the partial function, allowing positional and named argument bindings, returning a new function.[5]

In [XQuery](/source/XQuery), an argument placeholder (?) is used for each non-fixed argument in a partial function application.[6]

## Definitions

In the [simply typed lambda calculus](/source/Simply_typed_lambda_calculus) with [function](/source/Function_type) and [product types](/source/Product_type) (*λ*→,×) 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 X , Y {\displaystyle X,Y} and Z {\displaystyle Z} , and a function f : X × Y → Z {\displaystyle f:X\times Y\rightarrow Z} , one can define the function

- f ( ⋅ , − ) : X → ( Y → Z ) , {\displaystyle f(\,\cdot \,,-):X\rightarrow (Y\rightarrow Z),}

where ( Y → Z ) {\displaystyle (Y\rightarrow Z)} is the set of functions Y → Z {\displaystyle Y\rightarrow Z} . The image of x ∈ X {\displaystyle x\in X} under this map is f ( x , ⋅ ) : Y → Z {\displaystyle f(x,\,\cdot \,):Y\rightarrow Z} . This is the function which sends y ∈ Y {\displaystyle y\in Y} to f ( x , y ) {\displaystyle f(x,y)} . There are often structures on X , Y , Z {\displaystyle X,Y,Z} which mean that the image of f ( ⋅ , − ) {\displaystyle f(\,\cdot \,,-)} restricts to some subset of functions Y → Z {\displaystyle Y\rightarrow Z} , as illustrated in the following examples.

### Group actions

A [group action](/source/Group_action) can be understood as a function ∗ : G × X → X {\displaystyle *:G\times X\rightarrow X} . The partial evaluation ρ : G → Sym ( X ) ⊂ ( X → X ) {\displaystyle \rho :G\rightarrow {\text{Sym}}(X)\subset (X\rightarrow X)} restricts to the group of bijections from X {\displaystyle X} to itself. The group action axioms further ensure ρ {\displaystyle \rho } is a [group homomorphism](/source/Group_homomorphism).

### Inner-products and canonical map to the dual

An [inner-product](/source/Inner-product) on a vector space V {\displaystyle V} over a field K {\displaystyle K} is a map ϕ : V × V → K {\displaystyle \phi :V\times V\rightarrow K} . The partial evaluation provides a canonical map to the [dual vector space](/source/Dual_vector_space), ϕ ( ⋅ , − ) : V → V ∗ ⊂ ( V → K ) {\displaystyle \phi (\,\cdot \,,-):V\rightarrow V^{*}\subset (V\rightarrow K)} . If this is the inner-product of a [Hilbert space](/source/Hilbert_space), the [Riesz representation theorem](/source/Riesz_representation_theorem) ensures this is an [isomorphism](/source/Isomorphism).

### Cross-products and the adjoint map for Lie algebras

The partial application of the cross product × {\displaystyle \times } on R 3 {\displaystyle \mathbb {R} ^{3}} is × ( ⋅ , − ) : R 3 ↦ End ( R 3 ) {\displaystyle \times (\,\cdot \,,-):\mathbb {R} ^{3}\mapsto {\text{End}}(\mathbb {R} ^{3})} . The image of the vector u {\displaystyle \mathbf {u} } is a linear map T u {\displaystyle T_{\mathbf {u} }} such that T u ( v ) = u × v {\displaystyle T_{\mathbf {u} }(\mathbf {v} )=\mathbf {u} \times \mathbf {v} } . The components of T u {\displaystyle T_{\mathbf {u} }} can be found to be ( T u ) i j = ϵ i j k u k {\displaystyle (T_{\mathbf {u} })_{ij}=\epsilon _{ijk}u_{k}} .

This is closely related to the [adjoint map](/source/Adjoint_map) for [Lie algebras](/source/Lie_algebras). Lie algebras are equipped with a bracket [ ⋅ , ⋅ ] : g × g → g {\displaystyle [\,\cdot \,,\,\cdot \,]:{\mathfrak {g}}\times {\mathfrak {g}}\rightarrow {\mathfrak {g}}} . The partial application gives a map ad : g → End ( g ) {\displaystyle {\text{ad}}:{\mathfrak {g}}\rightarrow {\text{End}}({\mathfrak {g}})} . The axioms for the bracket ensure this map is a homomorphism of Lie algebras.

## See also

- [η-conversion](/source/%CE%97-conversion)

- [POP-2](/source/POP-2)

- [Restriction (mathematics)](/source/Restriction_(mathematics)), the more general phenomenon of restricting a function to a subset of its domain

## References

1. **[^](#cite_ref-1)** [Marlow & Peyton Jones 2004](#CITEREFMarlowPeyton_Jones2004)

1. **[^](#cite_ref-2)** ["clojure/clojure, partial function"](https://github.com/clojure/clojure/blob/653b8465845a78ef7543e0a250078eea2d56b659/src/clj/clojure/core.clj#L2614). *GitHub*. Retrieved 2020-07-18.

1. **[^](#cite_ref-3)** ["MethodHandle (Java Platform SE 7)"](https://docs.oracle.com/javase/7/docs/api/java/lang/invoke/MethodHandle.html#bindTo(java.lang.Object)). *docs.oracle.com*. Retrieved 2018-09-12.

1. **[^](#cite_ref-4)** ["Method assuming"](https://docs.perl6.org/routine/assuming). *docs.perl6.org*. Retrieved 2018-09-12.

1. **[^](#cite_ref-5)** ["10.2. functools — Higher-order functions and operations on callable objects — Python 3.7.0 documentation"](https://docs.python.org/3/library/functools.html#functools.partial). *docs.python.org*. Retrieved 2018-09-12.

1. **[^](#cite_ref-6)** ["XQuery 3.1: An XML Query Language"](https://www.w3.org/TR/2017/REC-xquery-31-20170321/#dt-partial-function-application). *www.w3.org*. Retrieved 2018-09-12.

## Further reading

- [Marlow, Simon](/source/Simon_Marlow); [Peyton Jones, Simon](/source/Simon_Peyton_Jones) (2004), ["Making a Fast Curry: Push/Enter vs. Eval/Apply for Higher-order Languages"](http://research.microsoft.com/en-us/um/people/simonpj/papers/eval-apply/eval-apply-icfp.ps), *ICFP '04 Proceedings of the ninth ACM SIGPLAN international conference on Functional programming*

- [Benjamin C. Pierce](/source/Benjamin_C._Pierce) et al. ["Partial Application"](https://www.cis.upenn.edu/~bcpierce/sf/current/Poly.html#lab126), [Archived](https://web.archive.org/web/20160521182324/http://www.cis.upenn.edu/~bcpierce/sf/current/Poly.html#lab126) 2016-05-21 at the [Wayback Machine](/source/Wayback_Machine) ["Digression: Currying"](https://www.cis.upenn.edu/~bcpierce/sf/current/Poly.html#lab127). [Archived](https://web.archive.org/web/20160521182324/http://www.cis.upenn.edu/~bcpierce/sf/current/Poly.html#lab127) 2016-05-21 at the [Wayback Machine](/source/Wayback_Machine) *Software Foundations*.

## External links

- [Partial function application](http://rosettacode.org/wiki/Partial_function_application) on Rosetta code.

- [Partial application](http://www.haskell.org/haskellwiki/Partial_application) at Haskell Wiki

- [Constant applicative form](http://www.haskell.org/haskellwiki/Constant_applicative_form) at Haskell Wiki

- [The dangers of being too partial](https://blogs.janestreet.com/the-dangers-of-being-too-partial/)

v t e Programming paradigms Imperative Structured Jackson structures Block-structured Modular Non-structured Procedural Programming in the large and in the small Design by contract Invariant-based Nested function Object-oriented Class-based, Prototype-based, Object-based Agent Immutable object Persistent Uniform function call syntax Declarative Functional Recursive Anonymous function (Partial application) Higher-order Purely functional Total Strict GADTs Dependent types Functional logic Point-free style Expression-oriented Applicative, Concatenative Function-level, Value-level Monad Dataflow Flow-based Reactive (Functional reactive) Signals Streams Synchronous Logic Abductive logic Answer set Constraint (Constraint logic) Inductive logic Nondeterministic Ontology Probabilistic logic Query Domain- specific language (DSL) Algebraic modeling Array Automata-based (Action) Command (Spacecraft) Differentiable End-user Grammar-oriented Interface description Language-oriented List comprehension Low-code Modeling Natural language Non-English-based Page description Pipes and filters Probabilistic Quantum Scientific Scripting Set-theoretic Simulation Stack-based System Tactile Templating Transformation (Graph rewriting, Production, Pattern) Visual Concurrent, parallel Actor-based Automatic mutual exclusion Choreographic programming Concurrent logic (Concurrent constraint logic) Concurrent OO Macroprogramming Multitier programming Organic computing Parallel programming models Partitioned global address space Process-oriented Relativistic programming Service-oriented Structured concurrency Metaprogramming Attribute-oriented Automatic (Inductive) Dynamic Extensible Generic Homoiconicity Interactive Macro (Hygienic) Metalinguistic abstraction Multi-stage Program synthesis (Bayesian, by demonstration, by example, vibe coding) Reflective Self-modifying code Symbolic Template Separation of concerns Aspects Components Data-driven Data-oriented Event-driven Features Literate Roles Subjects Comparisons/Lists Comparison (multi-paradigm, object-oriented, functional), List (OO, by type)

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