Mills ratio: Difference between revisions
en>Michael Hardy em-dash |
Fixed the definition of the hazard rate |
||
Line 1: | Line 1: | ||
< | : ''Not to be confused with [[partial evaluation]]''. | ||
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]]. Given a function <math>\scriptstyle f \colon (X \times Y \times Z) \to N </math>, we might fix (or 'bind') the first argument, producing a function of type <math> \scriptstyle\text{partial}(f) \colon (Y \times Z) \to N </math>. Evaluation of this function might be represented as <math>f_{partial}(2, 3)</math>. Note that the result of partial function application in this case is a function that takes two arguments. | |||
== Motivation == | |||
Intuitively, partial function application says "if you fix the first [[parameter (computer science)|argument]]s of the function, you get a function of the remaining arguments". For example, if function ''div'' stands for the division operation ''x'' / ''y'', then ''div'' with the parameter ''x'' fixed at 1 (i.e. ''div'' 1) is another function: 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 (programming language)|ML]] and [[Haskell (programming language)|Haskell]] functions are defined in [[currying|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 [[closure (computer science)|closure]]s, while Haskell can use more efficient techniques.<ref>{{harvnb|Marlow|Peyton Jones|2004}}</ref> | |||
[[Scala (programming language)|Scala]] implements optional partial application with multiple parameter lists, e.g. def add(x)(y) { x+y}; add(1) returns an incrementing function. | |||
The [[C++]] standard library provides bind(function, args..) to return a [[functor]] that is the result of partial application of the given arguments to the given function. | |||
== Definitions == | |||
In the [[simply-typed lambda calculus]] with [[function type|function]] and [[product type]]s (λ<sup>→,×</sup>) partial application, currying and uncurrying can be defined as: | |||
* <code>papply </code> : (((''a'' × ''b'') → ''c'') × ''a'') → (''b'' → ''c'') = λ(''f'', x). λy. f (x, y) | |||
* <code>curry </code> : ((''a'' × ''b'') → ''c'') → (''a'' → (''b'' → ''c'')) = λf. λx. λy. f (x, y) | |||
* <code>uncurry</code> : (''a'' → (''b'' → ''c'')) → ((''a'' × ''b'') → ''c'') = λf. λ(x, y). f x y | |||
Note that <code>curry</code> <code>papply</code> = <code>curry</code>. | |||
== See also == | |||
* [[Currying]] | |||
* [[η-conversion]] | |||
* [[POP-2]] | |||
== Notes == | |||
<references /> | |||
== Further reading == | |||
* [[Simon Marlow]] and [[Simon Peyton Jones]] (2004, 2006). [http://research.microsoft.com/en-us/um/people/simonpj/papers/eval-apply/eval-apply-icfp.ps "Making a Fast Curry: Push/Enter vs. Eval/Apply for Higher-order Languages"]. ''ICFP '04 Proceedings of the ninth ACM SIGPLAN international conference on Functional programming''. | |||
* [[Benjamin C. Pierce]] et al. [http://www.cis.upenn.edu/~bcpierce/sf/Poly.html#lab97 "Partial Application"], [http://www.cis.upenn.edu/~bcpierce/sf/Poly.html#lab98 "Digression: Currying"]. ''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 | |||
* [http://ocaml.janestreet.com/?q=node/30 The dangers of being too partial] | |||
[[Category:Functional programming]] | |||
[[Category:Implementation of functional programming languages]] |
Latest revision as of 17:57, 28 February 2013
- Not to be confused with partial evaluation.
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. 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.
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 stands for the division operation x / y, then div with the parameter x fixed at 1 (i.e. div 1) is another function: 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 and Haskell 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.[1]
Scala implements optional partial application with multiple parameter lists, e.g. def add(x)(y) { x+y}; add(1) returns an incrementing function.
The C++ standard library provides bind(function, args..) to return a functor that is the result of partial application of the given arguments to the given function.
Definitions
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
.
See also
Notes
Further reading
- Simon Marlow and Simon Peyton Jones (2004, 2006). "Making a Fast Curry: Push/Enter vs. Eval/Apply for Higher-order Languages". ICFP '04 Proceedings of the ninth ACM SIGPLAN international conference on Functional programming.
- Benjamin C. Pierce et al. "Partial Application", "Digression: Currying". Software Foundations.
External links
- Partial function application on Rosetta code.
- Partial application at Haskell Wiki
- Constant applicative form at Haskell Wiki
- The dangers of being too partial