Currying
In mathematics and computer science, partial application, invented by Moses Schönfinkel and later re-invented by Haskell Curry,[1][not specific enough to verify] is the technique of transforming a function that takes multiple arguments (or an n-tuple of arguments) in such a way that it can be called as a chain of functions each with a single argument.
Uncurrying is the dual transformation to currying, and can be seen as a form of defunctionalization. It takes a function f(x) which returns another function g(y) as a result, and yields a new function f'(x, y) which takes a number of additional parameters and applies them to the function returned by f. The process can be iterated if necessary.
Contents |
[edit] Motivation
Currying is similar to the process of calculating a function of multiple variables for some given values on paper. For example, given the function f(x,y) = y / x:
- To evaluate f(2,y), first replace x with 2.
- Since the result is a function of y, this function g(y) can be defined as g(y) = f(2,y) = y / 2.
- Next, replace the y argument with 3, producing g(3) = f(2,3) = 3 / 2.
On paper, using classical notation, this is usually done all in one step. However, each argument can be replaced sequentially as well. Each replacement results in a simpler function taking one fewer argument. This produces a chain of functions as in lambda calculus, where each function takes only one argument, and multi-argument functions are usually represented in curried form.
The practical motivation for currying is that very often the functions obtained by supplying some but not all of the arguments to a curried function (often called partial application) are useful; for example, many languages have a function or operator similar to plus_one. Currying makes it easy to define these functions, for example by currying the addition operator with 1 as its first argument.
This is similar in computer code: If we let f be a function f(x,y) = y / x, then the function
is a curried version of f. In particular,
is the curried equivalent of the example above.
[edit] Definition
Given a function f of type
, then currying it makes a function
. That is, curry(f) takes an argument of type X and returns a function of type
. Uncurrying is the reverse transformation, and is most easily understood in terms of its right adjoint, apply.
The → operator is often considered right-associative, so the curried function type
is often written as
. Conversely, function application is considered to be left-associative, so that
is equivalent to
.
Intuitively, currying 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 curried form of 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.
Some programming languages almost always use curried functions to achieve multiple arguments; notable examples are ML and Haskell, where in both cases all functions have exactly one argument.
Curried functions may be used in any language that supports closures; however, uncurried functions are generally preferred for efficiency reasons, since the overhead of partial application and closure creation can then be avoided for most function calls.
[edit] Mathematical view
In theoretical computer science, currying provides a way to study functions with multiple arguments in very simple theoretical models such as the lambda calculus in which functions only take a single argument.
When viewed in a set-theoretic light, currying is the natural code between the set
of functions from
to A, and the set (AB)C of functions from C to the set of functions from B to A. In category theory, currying can be found in the universal property of an exponential object, which gives rise to the following adjunction in cartesian closed categories: There is a natural isomorphism between the morphisms from a binary product
and the morphisms to an exponential object
. In other words, currying is the statement that product and Hom are adjoint functors; that is there is a natural transformation:
This is the key property of being a Cartesian closed category.
Under the Curry-Howard correspondence, the existence of currying and uncurrying is equivalent to the logical theorem
, as tuples (product type) corresponds to conjunction in logic, and function type corresponds to implication.
Curry is a continuous function in the Scott topology.
[edit] Naming
The name "currying", coined by Christopher Strachey in 1967, is a reference to logician Haskell Curry. The alternative name "Schönfinkelisation", has been proposed as a reference to Moses Schönfinkel.[2]
[edit] See also
[edit] Notes
- ^ (Barendregt 2000, p. 8)
- ^ I. Heim and A. Kratzer (1998). Semantics in Generative Grammar. Blackwell.
[edit] References
- Heim, Irene; Kratzer, Angelika (1998), Semantics in a Generative Grammar, Malden: Blackwall Publishers
[edit] External links
| Look up currying in Wiktionary, the free dictionary. |
- Currying != Generalized Partial Application! - post at Lambda-the-Ultimate.org
[edit] Language-Specific Implementations
- Currying in Algol68G
- Currying in C
- Currying in CSharp, Alternative (faster) method
- Currying in Delphi 2009
- Currying in Java
- Currying in Javascript
- Currying in Lua
- Currying in Perl
- Currying in PHP
- Partial Application in PowerShell
- Currying in Ruby
- Currying in Scala
- Implicit currying in Scheme
- Currying in Smalltalk
