Argument
Currying
Overview
In mathematics and computer science, currying (named after Haskell Curry) is the technique of translating a function that takes multiple arguments into a sequence of families of functions, each taking a single argument.
In the prototypical example, one begins with a function
f
:
(
X
×
Y
)
→
Z
{\displaystyle f:(X\times Y)\to Z}
that takes two arguments, one from
X
{\displaystyle X}
and one from
Y
,
{\displaystyle Y,}
and produces objects in
Z
.
{\displaystyle Z.}
The curried form of this function treats the first argument as a parameter, so as to create a family of functions
f
x
:
Y
→
Z
.
{\displaystyle f_{x}:Y\to Z.}
The family is arranged so that for each object
x
{\displaystyle x}
in
X
,
{\displaystyle X,}
there is exactly one function
f
x
{\displaystyle f_{x}}
, such that for any
y
{\displaystyle y}
in
Y
{\displaystyle Y}
,
f
x
(
y
)
=
f
(
x
,
y
)
{\displaystyle f_{x}(y)=f(x,y)}
.
In this example,
curry
{\displaystyle {\mbox{curry}}}
itself becomes a function that takes
f
{\displaystyle f}
as an argument, and returns a function that maps each
x
{\displaystyle x}
to
f
x
.
{\displaystyle f_{x}.}
The proper notation for expressing this is verbose. The function
f
{\displaystyle f}
belongs to the set of functions
(
X
×
Y
)
→
Z
.
{\displaystyle (X\times Y)\to Z.}
Meanwhile,
f
x
{\displaystyle f_{x}}
belongs to the set of functions
Y
→
Z
.
{\displaystyle Y\to Z.}
Thus, something that maps
x
{\displaystyle x}
to
f
x
{\displaystyle f_{x}}
will be of the type
X
→
[
Y
→
Z
]
.
{\displaystyle X\to [Y\to Z].}
With this notation,
curry
{\displaystyle {\mbox{curry}}}
is a function that takes objects from the first set, and returns objects in the second set, and so one writes
curry
:
[
(
X
×
Y
)
→
Z
]
→
(
X
→
[
Y
→
Z
]
)
.
{\displaystyle {\mbox{curry}}:[(X\times Y)\to Z]\to (X\to [Y\to Z]).}
This is a somewhat informal example; more precise definitions of what is meant by "object" and "function" are given below. These definitions vary from context to context, and take different forms, depending on the theory that one is working in.
Currying is related to, but not the same as, partial application. The example above can be used to illustrate partial application; it is quite similar. Partial application is the function
apply
{\displaystyle {\mbox{apply}}}
that takes the pair
f
{\displaystyle f}
and
x
{\displaystyle x}
together as arguments, and returns
f
x
.
{\displaystyle f_{x}.}
Using the same notation as above, partial application has the signature
apply
:
(
[
(
X
×
Y
)
→
Z
]
×
X
)
→
[
Y
→
Z
]
.
{\displaystyle {\mbox{apply}}:([(X\times Y)\to Z]\times X)\to [Y\to Z].}
Written this way, application can be seen to be adjoint to currying.
The currying of a function with more than two arguments can be defined by induction.
Currying is useful in both practical and theoretical settings. In functional programming languages, and many others, it provides a way of automatically managing how arguments are passed to functions and exceptions. In theoretical computer science, it provides a way to study functions with multiple arguments in simpler theoretical models which provide only one argument. The most general setting for the strict notion of currying and uncurrying is in the closed monoidal categories, which underpins a vast generalization of the Curry–Howard correspondence of proofs and programs to a correspondence with many other structures, including quantum mechanics, cobordisms, and string theory.
The concept of currying was introduced by Gottlob Frege, developed by Moses Schönfinkel,
and further developed by Haskell Curry.
Uncurrying is the dual transformation to currying, and can be seen as a form of defunctionalization. It takes a function
f
{\displaystyle f}
whose return value is another function
g
{\displaystyle g}
, and yields a new function
f
′
{\displaystyle f'}
that takes as parameters the arguments for both
f
{\displaystyle f}
and
g
{\displaystyle g}
, and returns, as a result, the application of
f
{\displaystyle f}
and subsequently,
g
{\displaystyle g}
, to those arguments. The process can be iterated.
Overview adapted from Wikipedia (CC BY-SA 4.0; see the article history for authors).
Possible passages
Found by search and not yet checked by a person; they may only mention the idea in passing.
of luncheon, a well-spiced aleberry, to drive away the ague-fit that is quaking through my nerves; then I shall come to heart, in some degree." "Good!" replied the Knight; "demand what you want; you are at home here." Franz made himself be served like a bashaw; and having nothing else but currying to expect, he determined to deserve it; he bantered and bullied, in his most imperious style, the servants that were…
Thomas Carlyle, Translations from the German
Names Currying.
Thus there arises a classification of modes of formation, or in the language commonly used by mathematicians, a classification of Functions. Any number, considered as formed from any other number, is called a function of it; and there are as many kinds of functions as there are modes of formation. The simple functions are by no means numerous, most functions being formed by the combination of several of the…
John Stuart Mill, A System of Logic, Ratiocinative and Inductive
States what Currying is.
Then two I mark’d, that sat Propp’d ’gainst each other, as two brazen pans Set to retain the heat. From head to foot, A tetter bark’d them round. Nor saw I e’er Groom currying so fast, for whom his lord Impatient waited, or himself perchance Tir’d with long watching, as of these each one Plied quickly his keen nails, through furiousness Of ne’er abated pruriency. The crust Came drawn from underneath in flakes, like…
Dante Alighieri, Divine Comedy, Cary's Translation, Hell
Names Currying.
Philosophers who discussed Currying
Related ideas
- Tuple
- If and only if
- Function (mathematics)
- Mathematics
- Ordered pair
- Quantum mechanics
- Curry–Howard correspondence
- Set (mathematics)
- Set theory
- String theory
- Topological space
- Theoretical computer science
- Bijection
- Type theory
- Arity
- Mathematical analysis
- Gottlob Frege
- Computer programming
- Computer science
- Boolean algebra
- Classical logic
- Linear logic
- Category theory
- Continuous function
Place in the tradition
Part of First-order logic.