{{Short description|In functional programming}} {{Distinguish|Partial evaluation|Partial function}}

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 <math> f \colon (X \times Y \times Z) \to N </math>, we might fix (or 'bind') the first argument, producing a function of type <math> \text{partial}(f) \colon (Y \times Z) \to N </math>. Evaluation of this function might be represented as <math>f_\text{partial}(2, 3)</math>. 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, which is a related, but distinct concept.

== Motivation == 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: ''div''<sub>1</sub>(''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 <code>plus_one</code>. 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 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 <code>curry</code>, <code>uncurry</code> and <code>papply</code> 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.<ref>{{harvnb|Marlow|Peyton Jones|2004}}</ref>

Scala implements optional partial application with placeholder, e.g. {{code|2=scala|1=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. {{code|2=scala|1=def add(x: Int)(y: Int) = {x+y}; add(1) _}}.

Clojure implements partial application using the <code>partial</code> function defined in its core library.<ref>{{Cite web|last=|first=|date=|title=clojure/clojure, partial function|url=https://github.com/clojure/clojure/blob/653b8465845a78ef7543e0a250078eea2d56b659/src/clj/clojure/core.clj#L2614|archive-url=|archive-date=|access-date=2020-07-18|website=GitHub|language=en}}</ref>

The C++ standard library provides <code>bind(function, args..)</code> 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 <code>bind_front(function, args...)</code> is also provided which binds the first <code>sizeof...(args)</code> arguments of the function to the args. In contrast, <code>bind</code> allows binding any of the arguments of the function passed to it, not just the first ones.<!-- C++23 will also add a bind_back function which binds the last arguments of the function --> Alternatively, lambda expressions can be used: <syntaxhighlight lang="cpp"> int f(int a, int b); auto f_partial = [](int a) { return f(a, 123); }; assert(f_partial(456) == f(456, 123) );</syntaxhighlight>

In Java, <code>MethodHandle.bindTo</code> partially applies a function to its first argument.<ref>{{Cite web|url=https://docs.oracle.com/javase/7/docs/api/java/lang/invoke/MethodHandle.html#bindTo(java.lang.Object)|title=MethodHandle (Java Platform SE 7)|website=docs.oracle.com|language=en|access-date=2018-09-12}}</ref> Alternatively, since Java 8, lambdas can be used: <syntaxhighlight lang="java"> public static <A, B, R> Function<B, R> partialApply(BiFunction<A, B, R> biFunc, A value) { return b -> biFunc.apply(value, b); } </syntaxhighlight>

In Raku, the [https://docs.perl6.org/routine/assuming <code>assuming</code>] method creates a new function with fewer parameters.<ref>{{cite web|url=https://docs.perl6.org/routine/assuming|title=Method assuming|website=docs.perl6.org|access-date=2018-09-12}}</ref>

The Python standard library module <code>functools</code> includes the <code>partial</code> function, allowing positional and named argument bindings, returning a new function.<ref>{{Cite web|url=https://docs.python.org/3/library/functools.html#functools.partial|title=10.2. functools — Higher-order functions and operations on callable objects — Python 3.7.0 documentation|website=docs.python.org|access-date=2018-09-12}}</ref>

In XQuery, an argument placeholder (<code>?</code>) is used for each non-fixed argument in a partial function application.<ref>{{Cite web|url=https://www.w3.org/TR/2017/REC-xquery-31-20170321/#dt-partial-function-application|title=XQuery 3.1: An XML Query Language|website=www.w3.org|language=EN|access-date=2018-09-12}}</ref>

== Definitions == In the simply typed lambda calculus with function and product types (''λ''<sup>→,&times;</sup>) partial application, currying and uncurrying can be defined as ; <code>papply</code> : (((''a'' &times; ''b'') → ''c'') &times; ''a'') → (''b'' → ''c'') = ''λ''(''f'', ''x''). ''λy''. ''f'' (''x'', ''y'') ; <code>curry</code> : ((''a'' &times; ''b'') → ''c'') → (''a'' → (''b'' → ''c'')) = ''λf''. ''λx''. ''λy''. ''f'' (''x'', ''y'') ; <code>uncurry</code> : (''a'' → (''b'' → ''c'')) → ((''a'' &times; ''b'') → ''c'') = ''λf''. ''λ''(''x'', ''y''). ''f x y''

Note that <code>curry</code> <code>papply</code> = <code>curry</code>.

== Mathematical formulation and examples ==

Partial application can be a useful way to define several useful notions in mathematics.

Given sets <math>X, Y</math> and <math>Z</math>, and a function <math>f: X\times Y \rightarrow Z</math>, one can define the function :<math> f(\,\cdot\, , -): X\rightarrow (Y\rightarrow Z),</math> where <math>(Y\rightarrow Z)</math> is the set of functions <math>Y\rightarrow Z</math>. The image of <math>x\in X</math> under this map is <math>f(x, \,\cdot\,):Y\rightarrow Z</math>. This is the function which sends <math> y \in Y</math> to <math>f(x,y)</math>. There are often structures on <math>X, Y, Z</math> which mean that the image of <math>f(\,\cdot\, ,-)</math> restricts to some subset of functions <math>Y\rightarrow Z</math>, as illustrated in the following examples.

=== Group actions === A group action can be understood as a function <math>* : G\times X \rightarrow X</math>. The partial evaluation <math>\rho: G \rightarrow \text{Sym}(X)\subset (X\rightarrow X)</math> restricts to the group of bijections from <math>X</math> to itself. The group action axioms further ensure <math>\rho</math> is a group homomorphism.

=== Inner-products and canonical map to the dual === An inner-product on a vector space <math>V</math> over a field <math>K</math> is a map <math>\phi:V\times V\rightarrow K</math>. The partial evaluation provides a canonical map to the dual vector space, <math>\phi(\, \cdot \, ,-):V\rightarrow V^*\subset (V\rightarrow K)</math>. If this is the inner-product of a Hilbert space, the Riesz representation theorem ensures this is an isomorphism.

=== Cross-products and the adjoint map for Lie algebras === The partial application of the cross product <math>\times</math> on <math>\mathbb{R}^3</math> is <math>\times(\, \cdot \, , -): \mathbb{R}^3 \mapsto \text{End}(\mathbb{R}^3)</math>. The image of the vector <math>\mathbf{u}</math> is a linear map <math>T_\mathbf{u}</math> such that <math>T_\mathbf{u}(\mathbf{v}) = \mathbf{u}\times\mathbf{v}</math>. The components of <math>T_\mathbf{u}</math> can be found to be <math>(T_\mathbf{u})_{ij} = \epsilon_{ijk}u_k</math>.

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

== See also == * η-conversion * POP-2 * Restriction (mathematics), the more general phenomenon of restricting a function to a subset of its domain

== References == {{Reflist}}

== Further reading == * {{citation |author-link1=Simon Marlow |last1=Marlow |first1=Simon |author-link2=Simon Peyton Jones |first2=Simon |last2=Peyton Jones |date=2004 |url=http://research.microsoft.com/en-us/um/people/simonpj/papers/eval-apply/eval-apply-icfp.ps |title=Making a Fast Curry: Push/Enter vs. Eval/Apply for Higher-order Languages |work=ICFP '04 Proceedings of the ninth ACM SIGPLAN international conference on Functional programming}} * Benjamin C. Pierce et al. [https://www.cis.upenn.edu/~bcpierce/sf/current/Poly.html#lab126 "Partial Application"], {{Webarchive|url=https://web.archive.org/web/20160521182324/http://www.cis.upenn.edu/~bcpierce/sf/current/Poly.html#lab126 |date=2016-05-21 }} [https://www.cis.upenn.edu/~bcpierce/sf/current/Poly.html#lab127 "Digression: Currying"]. {{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 == * [http://rosettacode.org/wiki/Partial_function_application Partial function application] on Rosetta code. * [http://www.haskell.org/haskellwiki/Partial_application Partial application] at Haskell Wiki * [http://www.haskell.org/haskellwiki/Constant_applicative_form Constant applicative form] at Haskell Wiki * [https://blogs.janestreet.com/the-dangers-of-being-too-partial/ The dangers of being too partial]

{{Programming paradigms navbox}}

Category:Functional programming Category:Implementation of functional programming languages Category:Articles with example Java code Category:Articles with example C++ code