Curried Form
   HOME

TheInfoList



OR:

In
mathematics Mathematics is a field of study that discovers and organizes methods, Mathematical theory, theories and theorems that are developed and Mathematical proof, proved for the needs of empirical sciences and mathematics itself. There are many ar ...
and
computer science Computer science is the study of computation, information, and automation. Computer science spans Theoretical computer science, theoretical disciplines (such as algorithms, theory of computation, and information theory) to Applied science, ...
, currying is the technique of translating a
function Function or functionality may refer to: Computing * Function key, a type of key on computer keyboards * Function model, a structured representation of processes in a system * Function object or functor or functionoid, a concept of object-orie ...
that takes multiple
arguments An argument is a series of sentences, statements, or propositions some of which are called premises and one is the conclusion. The purpose of an argument is to give reasons for one's conclusion via justification, explanation, and/or persua ...
into a sequence of families of functions, each taking a single argument. In the prototypical example, one begins with a function f:(X\times Y)\to Z that takes two arguments, one from X and one from Y, and produces objects in 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\to Z. The family is arranged so that for each object x in X, there is exactly one function f_x. In this example, \mbox itself becomes a function that takes f as an argument, and returns a function that maps each x to f_x. The proper notation for expressing this is verbose. The function f belongs to the set of functions (X\times Y)\to Z. Meanwhile, f_x belongs to the set of functions Y\to Z. Thus, something that maps x to f_x will be of the type X\to \to Z With this notation, \mbox is a function that takes objects from the first set, and returns objects in the second set, and so one writes \mbox: X\times Y)\to Zto (X\to \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 Partial may refer to: Mathematics *Partial derivative, derivative with respect to one of several variables of a function, with the other variables held constant ** ∂, a symbol that can denote a partial derivative, sometimes pronounced "partial ...
. The example above can be used to illustrate partial application; it is quite similar. Partial application is the function \mbox that takes the pair f and x together as arguments, and returns f_x. Using the same notation as above, partial application has the signature \mbox:( X\times Y)\to Z\times X) \to \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 In computer science, functional programming is a programming paradigm where programs are constructed by Function application, applying and Function composition (computer science), composing Function (computer science), functions. It is a declarat ...
languages Language is a structured system of communication that consists of grammar and vocabulary. It is the primary means by which humans convey meaning, both in spoken and signed forms, and may also be conveyed through writing. Human language is ch ...
, and many others, it provides a way of automatically managing how arguments are passed to functions and exceptions. In
theoretical computer science Theoretical computer science is a subfield of computer science and mathematics that focuses on the Abstraction, abstract and mathematical foundations of computation. It is difficult to circumscribe the theoretical areas precisely. The Associati ...
, 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 In programming language theory and proof theory, the Curry–Howard correspondence is the direct relationship between computer programs and mathematical proofs. It is also known as the Curry–Howard isomorphism or equivalence, or the proofs-as-p ...
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 Friedrich Ludwig Gottlob Frege (; ; 8 November 1848 – 26 July 1925) was a German philosopher, logician, and mathematician. He was a mathematics professor at the University of Jena, and is understood by many to be the father of analytic philos ...
, developed by
Moses Schönfinkel Moses Ilyich Schönfinkel (; 29 September 1888 – ) was a logician and mathematician, known for the invention of combinatory logic. Life Moses Schönfinkel was born on in Ekaterinoslav, Russian Empire (now Dnipro, Ukraine). He was born to a J ...
,Originally published as Republished as and further developed by
Haskell Curry Haskell Brooks Curry ( ; September 12, 1900 – September 1, 1982) was an American mathematician, logician and computer scientist. Curry is best known for his work in combinatory logic, whose initial concept is based on a paper by Moses Schönfin ...
. Uncurrying is the
dual Dual or Duals may refer to: Paired/two things * Dual (mathematics), a notion of paired concepts that mirror one another ** Dual (category theory), a formalization of mathematical duality *** see more cases in :Duality theories * Dual number, a nu ...
transformation to currying, and can be seen as a form of
defunctionalization In programming languages, defunctionalization is a compile-time transformation which eliminates higher-order functions, replacing them by a single first-order ''apply'' function. The technique was first described by John C. Reynolds in his 1972 pa ...
. It takes a
function Function or functionality may refer to: Computing * Function key, a type of key on computer keyboards * Function model, a structured representation of processes in a system * Function object or functor or functionoid, a concept of object-orie ...
f whose return value is another function g, and yields a new function f' that takes as parameters the arguments for both f and g, and returns, as a result, the application of f and subsequently, g, to those arguments. The process can be iterated.


Motivation

Currying provides a way for working with functions that take multiple arguments, and using them in frameworks where functions might take only one argument. For example, some analytical techniques can only be applied to
function Function or functionality may refer to: Computing * Function key, a type of key on computer keyboards * Function model, a structured representation of processes in a system * Function object or functor or functionoid, a concept of object-orie ...
s with a single argument. Practical functions frequently take more arguments than this.
Frege Friedrich Ludwig Gottlob Frege (; ; 8 November 1848 – 26 July 1925) was a German philosopher, logician, and mathematician. He was a mathematics professor at the University of Jena, and is understood by many to be the father of analytic philos ...
showed that it was sufficient to provide solutions for the single argument case, as it was possible to transform a function with multiple arguments into a chain of single-argument functions instead. This transformation is the process now known as currying. All "ordinary" functions that might typically be encountered in
mathematical analysis Analysis is the branch of mathematics dealing with continuous functions, limit (mathematics), limits, and related theories, such as Derivative, differentiation, Integral, integration, measure (mathematics), measure, infinite sequences, series ( ...
or in
computer programming Computer programming or coding is the composition of sequences of instructions, called computer program, programs, that computers can follow to perform tasks. It involves designing and implementing algorithms, step-by-step specifications of proc ...
can be curried. However, there are categories in which currying is not possible; the most general categories which allow currying are the closed monoidal categories. Some
programming language A programming language is a system of notation for writing computer programs. Programming languages are described in terms of their Syntax (programming languages), syntax (form) and semantics (computer science), semantics (meaning), usually def ...
s almost always use curried functions to achieve multiple arguments; notable examples are ML and
Haskell Haskell () is a general-purpose, statically typed, purely functional programming language with type inference and lazy evaluation. Designed for teaching, research, and industrial applications, Haskell pioneered several programming language ...
, where in both cases all functions have exactly one argument. This property is inherited from
lambda calculus In mathematical logic, the lambda calculus (also written as ''λ''-calculus) is a formal system for expressing computability, computation based on function Abstraction (computer science), abstraction and function application, application using var ...
, where multi-argument functions are usually represented in curried form. Currying is related to, but not the same as
partial application Partial may refer to: Mathematics *Partial derivative, derivative with respect to one of several variables of a function, with the other variables held constant ** ∂, a symbol that can denote a partial derivative, sometimes pronounced "partial ...
. In practice, the programming technique of closures can be used to perform partial application and a kind of currying, by hiding arguments in an environment that travels with the curried function.


History

The "Curry" in "Currying" is a reference to logician
Haskell Curry Haskell Brooks Curry ( ; September 12, 1900 – September 1, 1982) was an American mathematician, logician and computer scientist. Curry is best known for his work in combinatory logic, whose initial concept is based on a paper by Moses Schönfin ...
, who used the concept extensively, but
Moses Schönfinkel Moses Ilyich Schönfinkel (; 29 September 1888 – ) was a logician and mathematician, known for the invention of combinatory logic. Life Moses Schönfinkel was born on in Ekaterinoslav, Russian Empire (now Dnipro, Ukraine). He was born to a J ...
had the idea six years before Curry. The alternative name "Schönfinkelisation" has been proposed. In the mathematical context, the principle can be traced back to work in 1893 by
Frege Friedrich Ludwig Gottlob Frege (; ; 8 November 1848 – 26 July 1925) was a German philosopher, logician, and mathematician. He was a mathematics professor at the University of Jena, and is understood by many to be the father of analytic philos ...
. The originator of the word "currying" is not clear. David Turner says the word was coined by
Christopher Strachey Christopher S. Strachey (; 16 November 1916 – 18 May 1975) was a British computer scientist. He was one of the founders of denotational semantics, and a pioneer in programming language design and computer time-sharing.F. J. Corbató, et al., T ...
in his 1967 lecture notes
Fundamental Concepts in Programming Languages ''Fundamental Concepts in Programming Languages'' were an influential set of lecture notes written by Christopher Strachey for the International Summer School in Computer Programming at Copenhagen in August, 1967. It introduced much programming ...
, but that source introduces the concept as "a device originated by Schönfinkel", and the term "currying" is not used, while Curry is mentioned later in the context of higher-order functions.
John C. Reynolds John Charles Reynolds (June 1, 1935 – April 28, 2013) was an American computer scientist. Education and affiliations John Reynolds studied at Purdue University and then earned a Doctor of Philosophy (Ph.D.) in theoretical physics from Harvard U ...
defined "currying" in a 1972 paper, but did not claim to have coined the term.


Definition

Currying is most easily understood by starting with an informal definition, which can then be molded to fit many different domains. First, there is some notation to be established. The notation X \to Y denotes all functions from X to Y. If f is such a function, we write f \colon X \to Y . Let X \times Y denote the
ordered pair In mathematics, an ordered pair, denoted (''a'', ''b''), is a pair of objects in which their order is significant. The ordered pair (''a'', ''b'') is different from the ordered pair (''b'', ''a''), unless ''a'' = ''b''. In contrast, the '' unord ...
s of the elements of X and Y respectively, that is, the
Cartesian product In mathematics, specifically set theory, the Cartesian product of two sets and , denoted , is the set of all ordered pairs where is an element of and is an element of . In terms of set-builder notation, that is A\times B = \. A table c ...
of X and Y. Here, X and Y may be sets, or they may be types, or they may be other kinds of objects, as explored below. Given a function :f \colon (X \times Y) \to Z , currying constructs a new function :g \colon X \to (Y \to Z) . That is, g takes an argument of type X and returns a function of type Y\to Z. It is defined by :g(x)(y)=f(x,y) for x of type X and y of type Y. We then also write :\text(f)=g. Uncurrying is the reverse transformation, and is most easily understood in terms of its right adjoint, the function \operatorname.


Set theory

In
set theory Set theory is the branch of mathematical logic that studies Set (mathematics), sets, which can be informally described as collections of objects. Although objects of any kind can be collected into a set, set theory – as a branch of mathema ...
, the notation Y^X is used to denote the
set Set, The Set, SET or SETS may refer to: Science, technology, and mathematics Mathematics *Set (mathematics), a collection of elements *Category of sets, the category whose objects and morphisms are sets and total functions, respectively Electro ...
of functions from the set X to the set Y. Currying is the natural bijection between the set A^ of functions from B\times C to A, and the set (A^C)^B of functions from B to the set of functions from C to A. In symbols: :A^\cong (A^C)^B Indeed, it is this natural bijection that justifies the
exponential notation Scientific notation is a way of expressing numbers that are too large or too small to be conveniently written in decimal form, since to do so would require writing out an inconveniently long string of digits. It may be referred to as scientif ...
for the set of functions. As is the case in all instances of currying, the formula above describes an adjoint pair of functors: for every fixed set C, the functor B\mapsto B\times C is left adjoint to the functor A \mapsto A^C. In the
category of sets In the mathematical field of category theory, the category of sets, denoted by Set, is the category whose objects are sets. The arrows or morphisms between sets ''A'' and ''B'' are the functions from ''A'' to ''B'', and the composition of mor ...
, the
object Object may refer to: General meanings * Object (philosophy), a thing, being, or concept ** Object (abstract), an object which does not exist at any particular time or place ** Physical object, an identifiable collection of matter * Goal, an a ...
Y^X is called the
exponential object In mathematics, specifically in category theory, an exponential object or map object is the category theory, categorical generalization of a function space in set theory. Category (mathematics), Categories with all Product (category theory), fini ...
.


Function spaces

In the theory of
function space In mathematics, a function space is a set of functions between two fixed sets. Often, the domain and/or codomain will have additional structure which is inherited by the function space. For example, the set of functions from any set into a ve ...
s, such as in
functional analysis Functional analysis is a branch of mathematical analysis, the core of which is formed by the study of vector spaces endowed with some kind of limit-related structure (for example, Inner product space#Definition, inner product, Norm (mathematics ...
or
homotopy theory In mathematics, homotopy theory is a systematic study of situations in which Map (mathematics), maps can come with homotopy, homotopies between them. It originated as a topic in algebraic topology, but nowadays is learned as an independent discipli ...
, one is commonly interested in
continuous function In mathematics, a continuous function is a function such that a small variation of the argument induces a small variation of the value of the function. This implies there are no abrupt changes in value, known as '' discontinuities''. More preci ...
s between
topological space In mathematics, a topological space is, roughly speaking, a Geometry, geometrical space in which Closeness (mathematics), closeness is defined but cannot necessarily be measured by a numeric Distance (mathematics), distance. More specifically, a to ...
s. One writes \text(X,Y) (the
Hom functor In mathematics, specifically in category theory, hom-sets (i.e. sets of morphisms between object (category theory), objects) give rise to important functors to the category of sets. These functors are called hom-functors and have numerous applicati ...
) for the set of ''all'' functions from X to Y, and uses the notation Y^X to denote the subset of continuous functions. Here, \text is the
bijection In mathematics, a bijection, bijective function, or one-to-one correspondence is a function between two sets such that each element of the second set (the codomain) is the image of exactly one element of the first set (the domain). Equival ...
:\text:\text(X\times Y, Z) \to \text(X, \text(Y,Z)) , while uncurrying is the inverse map. If the set Y^X of continuous functions from X to Y is given the
compact-open topology In mathematics, the compact-open topology is a topology defined on the set of continuous maps between two topological spaces. The compact-open topology is one of the commonly used topologies on function spaces, and is applied in homotopy theory ...
, and if the space Y is
locally compact Hausdorff In topology and related branches of mathematics, a topological space is called locally compact if, roughly speaking, each small portion of the space looks like a small portion of a compact space. More precisely, it is a topological space in which e ...
, then :\text : Z^\to (Z^Y)^X is a
homeomorphism In mathematics and more specifically in topology, a homeomorphism ( from Greek roots meaning "similar shape", named by Henri Poincaré), also called topological isomorphism, or bicontinuous function, is a bijective and continuous function ...
. This is also the case when X, Y and Y^X are
compactly generated In mathematics, compactly generated can refer to: * Compactly generated group, a topological group which is algebraically generated by one of its compact subsets *Compactly generated space In topology, a topological space X is called a compactly ge ...
, although there are more cases. One useful corollary is that a function is continuous
if and only if In logic and related fields such as mathematics and philosophy, "if and only if" (often shortened as "iff") is paraphrased by the biconditional, a logical connective between statements. The biconditional is true in two cases, where either bo ...
its curried form is continuous. Another important result is that the application map, usually called "evaluation" in this context, is continuous (note that
eval In some programming languages, eval , short for evaluate, is a function which evaluates a string as though it were an expression in the language, and returns a result; in others, it executes multiple lines of code as though they had been incl ...
is a strictly different concept in computer science.) That is, \begin &&\text:Y^X \times X \to Y \\ && (f,x) \mapsto f(x) \end is continuous when Y^X is compact-open and Y locally compact Hausdorff. These two results are central for establishing the continuity of
homotopy In topology, two continuous functions from one topological space to another are called homotopic (from and ) if one can be "continuously deformed" into the other, such a deformation being called a homotopy ( ; ) between the two functions. ...
, i.e. when X is the unit interval I, so that Z^ \cong (Z^Y)^I can be thought of as either a homotopy of two functions from Y to Z, or, equivalently, a single (continuous) path in Z^Y.


Algebraic topology

In
algebraic topology Algebraic topology is a branch of mathematics that uses tools from abstract algebra to study topological spaces. The basic goal is to find algebraic invariant (mathematics), invariants that classification theorem, classify topological spaces up t ...
, currying serves as an example of
Eckmann–Hilton duality In the mathematical disciplines of algebraic topology and homotopy theory, Eckmann–Hilton duality in its most basic form, consists of taking a given diagram for a particular concept and reversing the direction of all arrows, much as in cate ...
, and, as such, plays an important role in a variety of different settings. For example,
loop space In topology, a branch of mathematics, the loop space Ω''X'' of a pointed topological space ''X'' is the space of (based) loops in ''X'', i.e. continuous pointed maps from the pointed circle ''S''1 to ''X'', equipped with the compact-open topolog ...
is adjoint to
reduced suspension In topology, a branch of mathematics, the suspension of a topological space ''X'' is intuitively obtained by stretching ''X'' into a cylinder and then collapsing both end faces to points. One views ''X'' as "suspended" between these end points. The ...
s; this is commonly written as : Sigma X,Z\approxeq , \Omega Z/math> where ,B/math> is the set of
homotopy class In topology, two continuous functions from one topological space to another are called homotopic (from and ) if one can be "continuously deformed" into the other, such a deformation being called a homotopy ( ; ) between the two functions. A ...
es of maps A \rightarrow B, and \Sigma A is the
suspension Suspension or suspended may refer to: Science and engineering * Car suspension * Cell suspension or suspension culture, in biology * Guarded suspension, a software design pattern in concurrent programming suspending a method call and the calling ...
of ''A'', and \Omega A is the
loop space In topology, a branch of mathematics, the loop space Ω''X'' of a pointed topological space ''X'' is the space of (based) loops in ''X'', i.e. continuous pointed maps from the pointed circle ''S''1 to ''X'', equipped with the compact-open topolog ...
of ''A''. In essence, the suspension \Sigma X can be seen as the cartesian product of X with the unit interval, modulo an equivalence relation to turn the interval into a loop. The curried form then maps the space X to the space of functions from loops into Z, that is, from X into \Omega Z. Then \text is the
adjoint functor In mathematics, specifically category theory, adjunction is a relationship that two functors may exhibit, intuitively corresponding to a weak form of equivalence between two related categories. Two functors that stand in this relationship are k ...
that maps suspensions to loop spaces, and uncurrying is the dual. The duality between the
mapping cone Mapping cone may refer to one of the following two different but related concepts in mathematics: * Mapping cone (topology) * Mapping cone (homological algebra) {{mathdab ...
and the mapping fiber (
cofibration In mathematics, in particular homotopy theory, a continuous mapping between topological spaces :i: A \to X, is a ''cofibration'' if it has the homotopy extension property with respect to all topological spaces S. That is, i is a cofibration if f ...
and
fibration The notion of a fibration generalizes the notion of a fiber bundle and plays an important role in algebraic topology, a branch of mathematics. Fibrations are used, for example, in Postnikov systems or obstruction theory. In this article, all ma ...
) can be understood as a form of currying, which in turn leads to the duality of the long exact and coexact Puppe sequences. In
homological algebra Homological algebra is the branch of mathematics that studies homology (mathematics), homology in a general algebraic setting. It is a relatively young discipline, whose origins can be traced to investigations in combinatorial topology (a precurs ...
, the relationship between currying and uncurrying is known as tensor-hom adjunction. Here, an interesting twist arises: the
Hom functor In mathematics, specifically in category theory, hom-sets (i.e. sets of morphisms between object (category theory), objects) give rise to important functors to the category of sets. These functors are called hom-functors and have numerous applicati ...
and the
tensor product In mathematics, the tensor product V \otimes W of two vector spaces V and W (over the same field) is a vector space to which is associated a bilinear map V\times W \rightarrow V\otimes W that maps a pair (v,w),\ v\in V, w\in W to an element of ...
functor might not
lift Lift or LIFT may refer to: Physical devices * Elevator, or lift, a device used for raising and lowering people or goods ** Paternoster lift, a type of lift using a continuous chain of cars which do not stop ** Patient lift, or Hoyer lift, mobile ...
to an
exact sequence In mathematics, an exact sequence is a sequence of morphisms between objects (for example, groups, rings, modules, and, more generally, objects of an abelian category) such that the image of one morphism equals the kernel of the next. Definit ...
; this leads to the definition of the
Ext functor In mathematics, the Ext functors are the derived functors of the Hom functor. Along with the Tor functor, Ext is one of the core concepts of homological algebra, in which ideas from algebraic topology are used to define invariants of algebraic stru ...
and the
Tor functor In mathematics, the Tor functors are the derived functors of the tensor product of modules over a ring. Along with the Ext functor, Tor is one of the central concepts of homological algebra, in which ideas from algebraic topology are used to const ...
.


Domain theory

In
order theory Order theory is a branch of mathematics that investigates the intuitive notion of order using binary relations. It provides a formal framework for describing statements such as "this is less than that" or "this precedes that". This article intr ...
, the theory of lattices of
partially ordered set In mathematics, especially order theory, a partial order on a Set (mathematics), set is an arrangement such that, for certain pairs of elements, one precedes the other. The word ''partial'' is used to indicate that not every pair of elements need ...
s, \text is a
continuous function In mathematics, a continuous function is a function such that a small variation of the argument induces a small variation of the value of the function. This implies there are no abrupt changes in value, known as '' discontinuities''. More preci ...
when the lattice is given the
Scott topology In mathematics, given two partially ordered sets ''P'' and ''Q'', a function ''f'': ''P'' → ''Q'' between them is Scott-continuous (named after the mathematician Dana Scott) if it preserves all directed suprema. That is, for every directed sub ...
. Scott-continuous functions were first investigated in the attempt to provide a semantics for
lambda calculus In mathematical logic, the lambda calculus (also written as ''λ''-calculus) is a formal system for expressing computability, computation based on function Abstraction (computer science), abstraction and function application, application using var ...
(as ordinary set theory is inadequate to do this). More generally, Scott-continuous functions are now studied in
domain theory Domain theory is a branch of mathematics that studies special kinds of partially ordered sets (posets) commonly called domains. Consequently, domain theory can be considered as a branch of order theory. The field has major applications in computer ...
, which encompasses the study of
denotational semantics In computer science, denotational semantics (initially known as mathematical semantics or Scott–Strachey semantics) is an approach of formalizing the meanings of programming languages by constructing mathematical objects (called ''denotations'' ...
of computer algorithms. Note that the Scott topology is quite different than many common topologies one might encounter in the
category of topological spaces In mathematics, the category of topological spaces, often denoted Top, is the category whose objects are topological spaces and whose morphisms are continuous maps. This is a category because the composition of two continuous maps is again con ...
; the Scott topology is typically finer, and is not sober. The notion of continuity makes its appearance in
homotopy type theory In mathematical logic and computer science, homotopy type theory (HoTT) refers to various lines of development of intuitionistic type theory, based on the interpretation of types as objects to which the intuition of (abstract) homotopy theory ap ...
, where, roughly speaking, two computer programs can be considered to be homotopic, i.e. compute the same results, if they can be "continuously"
refactored In computer programming and software design, code refactoring is the process of restructuring existing source code—changing the '' factoring''—without changing its external behavior. Refactoring is intended to improve the design, structure, ...
from one to the other.


Lambda calculi

In
theoretical computer science Theoretical computer science is a subfield of computer science and mathematics that focuses on the Abstraction, abstract and mathematical foundations of computation. It is difficult to circumscribe the theoretical areas precisely. The Associati ...
, currying provides a way to study functions with multiple arguments in very simple theoretical models, such as the
lambda calculus In mathematical logic, the lambda calculus (also written as ''λ''-calculus) is a formal system for expressing computability, computation based on function Abstraction (computer science), abstraction and function application, application using var ...
, in which functions only take a single argument. Consider a function f(x,y) taking two arguments, and having the type (X \times Y)\to Z, which should be understood to mean that ''x'' must have the type X, ''y'' must have the type Y, and the function itself returns the type Z. The curried form of ''f'' is defined as :\text(f) = \lambda x.(\lambda y.(f(x,y))) where \lambda is the abstractor of lambda calculus. Since curry takes, as input, functions with the type (X\times Y)\to Z, one concludes that the type of curry itself is :\text:((X \times Y)\to Z) \to (X \to (Y \to Z)) The → operator is often considered
right-associative In programming language theory, the associativity of an operator is a property that determines how operators of the same precedence are grouped in the absence of parentheses. If an operand is both preceded and followed by operators (for examp ...
, so the curried function type X \to (Y \to Z) is often written as X \to Y \to Z. Conversely,
function application In mathematics, function application is the act of applying a function to an argument from its domain so as to obtain the corresponding value from its range. In this sense, function application can be thought of as the opposite of function abs ...
is considered to be
left-associative In programming language theory, the associativity of an operator is a property that determines how operators of the same precedence are grouped in the absence of parentheses. If an operand is both preceded and followed by operators (for exampl ...
, so that f(x, y) is equivalent to :((\text(f) \; x) \;y) = \text(f) \; x \;y. That is, the parenthesis are not required to disambiguate the order of the application. Curried functions may be used in any
programming language A programming language is a system of notation for writing computer programs. Programming languages are described in terms of their Syntax (programming languages), syntax (form) and semantics (computer science), semantics (meaning), usually def ...
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.


Type theory

In
type theory In mathematics and theoretical computer science, a type theory is the formal presentation of a specific type system. Type theory is the academic study of type systems. Some type theories serve as alternatives to set theory as a foundation of ...
, the general idea of a
type system In computer programming, a type system is a logical system comprising a set of rules that assigns a property called a ''type'' (for example, integer, floating point, string) to every '' term'' (a word, phrase, or other set of symbols). Usu ...
in computer science is formalized into a specific algebra of types. For example, when writing f \colon X \to Y , the intent is that X and Y are
types Type may refer to: Science and technology Computing * Typing, producing text via a keyboard, typewriter, etc. * Data type, collection of values used for computations. * File type * TYPE (DOS command), a command to display contents of a file. * Ty ...
, while the arrow \to is a
type constructor In the area of mathematical logic and computer science known as type theory, a type constructor is a feature of a typed formal language that builds new types from old ones. Basic types are considered to be built using nullary type constructors. So ...
, specifically, the
function type In computer science and mathematical logic, a function type (or arrow type or exponential) is the type of a variable or parameter to which a function has or can be assigned, or an argument or result type of a higher-order function taking or return ...
or arrow type. Similarly, the Cartesian product X \times Y of types is constructed by the
product type In programming languages and type theory, a product of ''types'' is another, compounded, type in a structure. The "operands" of the product are types, and the structure of a product type is determined by the fixed order of the operands in the produ ...
constructor \times. The type-theoretical approach is expressed in programming languages such as ML and the languages derived from and inspired by it:
Caml Caml (originally an acronym for Categorical Abstract Machine Language) is a multi-paradigm, general-purpose, high-level, functional programming language which is a dialect of the ML programming language family. Caml was developed in France ...
,
Haskell Haskell () is a general-purpose, statically typed, purely functional programming language with type inference and lazy evaluation. Designed for teaching, research, and industrial applications, Haskell pioneered several programming language ...
, and F#. The type-theoretical approach provides a natural complement to the language of
category theory Category theory is a general theory of mathematical structures and their relations. It was introduced by Samuel Eilenberg and Saunders Mac Lane in the middle of the 20th century in their foundational work on algebraic topology. Category theory ...
, as discussed below. This is because categories, and specifically,
monoidal categories In mathematics, a monoidal category (or tensor category) is a category \mathbf C equipped with a bifunctor :\otimes : \mathbf \times \mathbf \to \mathbf that is associative up to a natural isomorphism, and an object ''I'' that is both a left an ...
, have an
internal language __NOTOC__ Categorical logic is the branch of mathematics in which tools and concepts from category theory are applied to the study of mathematical logic. It is also notable for its connections to theoretical computer science. In broad terms, cate ...
, with
simply typed lambda calculus The simply typed lambda calculus (), a form of type theory, is a typed interpretation of the lambda calculus with only one type constructor () that builds function types. It is the canonical and simplest example of a typed lambda calculus. The ...
being the most prominent example of such a language. It is important in this context, because it can be built from a single type constructor, the arrow type. Currying then endows the language with a natural product type. The correspondence between objects in categories and types then allows programming languages to be re-interpreted as logics (via
Curry–Howard correspondence In programming language theory and proof theory, the Curry–Howard correspondence is the direct relationship between computer programs and mathematical proofs. It is also known as the Curry–Howard isomorphism or equivalence, or the proofs-as-p ...
), and as other types of mathematical systems, as explored further, below.


Logic

Under the
Curry–Howard correspondence In programming language theory and proof theory, the Curry–Howard correspondence is the direct relationship between computer programs and mathematical proofs. It is also known as the Curry–Howard isomorphism or equivalence, or the proofs-as-p ...
, the existence of currying and uncurrying is equivalent to the logical theorem ((A \land B) \to C) \Leftrightarrow (A \to (B \to C)) (also known as
exportation An export in international trade is a good produced in one country that is sold into another country or a service provided in one country for a national or resident of another country. The seller of such goods or the service provider is an ' ...
), as
tuple In mathematics, a tuple is a finite sequence or ''ordered list'' of numbers or, more generally, mathematical objects, which are called the ''elements'' of the tuple. An -tuple is a tuple of elements, where is a non-negative integer. There is o ...
s (
product type In programming languages and type theory, a product of ''types'' is another, compounded, type in a structure. The "operands" of the product are types, and the structure of a product type is determined by the fixed order of the operands in the produ ...
) corresponds to conjunction in logic, and function type corresponds to implication. The
exponential object In mathematics, specifically in category theory, an exponential object or map object is the category theory, categorical generalization of a function space in set theory. Category (mathematics), Categories with all Product (category theory), fini ...
Q^P in the category of
Heyting algebra In mathematics, a Heyting algebra (also known as pseudo-Boolean algebra) is a bounded lattice (with join and meet operations written ∨ and ∧ and with least element 0 and greatest element 1) equipped with a binary operation ''a'' → ''b'' call ...
s is normally written as material implication P\to Q. Distributive Heyting algebras are
Boolean algebra In mathematics and mathematical logic, Boolean algebra is a branch of algebra. It differs from elementary algebra in two ways. First, the values of the variable (mathematics), variables are the truth values ''true'' and ''false'', usually denot ...
s, and the exponential object has the explicit form \neg P \lor Q, thus making it clear that the exponential object really is material implication.


Category theory

The above notions of currying and uncurrying find their most general, abstract statement in
category theory Category theory is a general theory of mathematical structures and their relations. It was introduced by Samuel Eilenberg and Saunders Mac Lane in the middle of the 20th century in their foundational work on algebraic topology. Category theory ...
. Currying is a
universal property In mathematics, more specifically in category theory, a universal property is a property that characterizes up to an isomorphism the result of some constructions. Thus, universal properties can be used for defining some objects independently fro ...
of an
exponential object In mathematics, specifically in category theory, an exponential object or map object is the category theory, categorical generalization of a function space in set theory. Category (mathematics), Categories with all Product (category theory), fini ...
, and gives rise to an adjunction in
cartesian closed categories In category theory, a category is Cartesian closed if, roughly speaking, any morphism defined on a product of two objects can be naturally identified with a morphism defined on one of the factors. These categories are particularly important in ma ...
. That is, there is a
natural Nature is an inherent character or constitution, particularly of the ecosphere or the universe as a whole. In this general sense nature refers to the laws, elements and phenomena of the physical world, including life. Although humans are part ...
isomorphism In mathematics, an isomorphism is a structure-preserving mapping or morphism between two structures of the same type that can be reversed by an inverse mapping. Two mathematical structures are isomorphic if an isomorphism exists between the ...
between the
morphisms In mathematics, a morphism is a concept of category theory that generalizes structure-preserving maps such as homomorphism between algebraic structures, functions from a set to another set, and continuous functions between topological spaces. Al ...
from a binary product f \colon (X \times Y) \to Z and the morphisms to an exponential object g \colon X \to Z^Y . This generalizes to a broader result in closed monoidal categories: Currying is the statement that the
tensor product In mathematics, the tensor product V \otimes W of two vector spaces V and W (over the same field) is a vector space to which is associated a bilinear map V\times W \rightarrow V\otimes W that maps a pair (v,w),\ v\in V, w\in W to an element of ...
and the
internal Hom In mathematics, specifically in category theory, hom-sets (i.e. sets of morphisms between object (category theory), objects) give rise to important functors to the category of sets. These functors are called hom-functors and have numerous applicati ...
are
adjoint functors In mathematics, specifically category theory, adjunction is a relationship that two functors may exhibit, intuitively corresponding to a weak form of equivalence between two related categories. Two functors that stand in this relationship are k ...
; that is, for every object B there is a
natural isomorphism In category theory, a branch of mathematics, a natural transformation provides a way of transforming one functor into another while respecting the internal structure (i.e., the composition of morphisms) of the categories involved. Hence, a natura ...
: : \mathrm(A\otimes B, C) \cong \mathrm(A, B\Rightarrow C) . Here, ''Hom'' denotes the (external) Hom-functor of all morphisms in the category, while B\Rightarrow C denotes the internal hom functor in the closed monoidal category. For the
category of sets In the mathematical field of category theory, the category of sets, denoted by Set, is the category whose objects are sets. The arrows or morphisms between sets ''A'' and ''B'' are the functions from ''A'' to ''B'', and the composition of mor ...
, the two are the same. When the product is the cartesian product, then the internal hom B\Rightarrow C becomes the exponential object C^B. Currying can break down in one of two ways. One is if a category is not closed, and thus lacks an internal hom functor (possibly because there is more than one choice for such a functor). Another way is if it is not monoidal, and thus lacks a product (that is, lacks a way of writing down pairs of objects). Categories that do have both products and internal homs are exactly the closed monoidal categories. The setting of cartesian closed categories is sufficient for the discussion of
classical logic Classical logic (or standard logic) or Frege–Russell logic is the intensively studied and most widely used class of deductive logic. Classical logic has had much influence on analytic philosophy. Characteristics Each logical system in this c ...
; the more general setting of closed monoidal categories is suitable for
quantum computation A quantum computer is a computer that exploits quantum mechanical phenomena. On small scales, physical matter exhibits properties of both particles and waves, and quantum computing takes advantage of this behavior using specialized hardware. C ...
. The difference between these two is that the product for cartesian categories (such as the
category of sets In the mathematical field of category theory, the category of sets, denoted by Set, is the category whose objects are sets. The arrows or morphisms between sets ''A'' and ''B'' are the functions from ''A'' to ''B'', and the composition of mor ...
,
complete partial order In mathematics, the phrase complete partial order is variously used to refer to at least three similar, but distinct, classes of partially ordered sets, characterized by particular completeness properties. Complete partial orders play a central ro ...
s or
Heyting algebra In mathematics, a Heyting algebra (also known as pseudo-Boolean algebra) is a bounded lattice (with join and meet operations written ∨ and ∧ and with least element 0 and greatest element 1) equipped with a binary operation ''a'' → ''b'' call ...
s) is just the
Cartesian product In mathematics, specifically set theory, the Cartesian product of two sets and , denoted , is the set of all ordered pairs where is an element of and is an element of . In terms of set-builder notation, that is A\times B = \. A table c ...
; it is interpreted as an
ordered pair In mathematics, an ordered pair, denoted (''a'', ''b''), is a pair of objects in which their order is significant. The ordered pair (''a'', ''b'') is different from the ordered pair (''b'', ''a''), unless ''a'' = ''b''. In contrast, the '' unord ...
of items (or a list).
Simply typed lambda calculus The simply typed lambda calculus (), a form of type theory, is a typed interpretation of the lambda calculus with only one type constructor () that builds function types. It is the canonical and simplest example of a typed lambda calculus. The ...
is the
internal language __NOTOC__ Categorical logic is the branch of mathematics in which tools and concepts from category theory are applied to the study of mathematical logic. It is also notable for its connections to theoretical computer science. In broad terms, cate ...
of cartesian closed categories; and it is for this reason that pairs and lists are the primary
types Type may refer to: Science and technology Computing * Typing, producing text via a keyboard, typewriter, etc. * Data type, collection of values used for computations. * File type * TYPE (DOS command), a command to display contents of a file. * Ty ...
in the
type theory In mathematics and theoretical computer science, a type theory is the formal presentation of a specific type system. Type theory is the academic study of type systems. Some type theories serve as alternatives to set theory as a foundation of ...
of
LISP Lisp (historically LISP, an abbreviation of "list processing") is a family of programming languages with a long history and a distinctive, fully parenthesized Polish notation#Explanation, prefix notation. Originally specified in the late 1950s, ...
, Scheme and many
functional programming language In computer science, functional programming is a programming paradigm where programs are constructed by applying and composing functions. It is a declarative programming paradigm in which function definitions are trees of expressions that map ...
s. By contrast, the product for
monoidal categories In mathematics, a monoidal category (or tensor category) is a category \mathbf C equipped with a bifunctor :\otimes : \mathbf \times \mathbf \to \mathbf that is associative up to a natural isomorphism, and an object ''I'' that is both a left an ...
(such as
Hilbert space In mathematics, a Hilbert space is a real number, real or complex number, complex inner product space that is also a complete metric space with respect to the metric induced by the inner product. It generalizes the notion of Euclidean space. The ...
and the
vector space In mathematics and physics, a vector space (also called a linear space) is a set (mathematics), set whose elements, often called vector (mathematics and physics), ''vectors'', can be added together and multiplied ("scaled") by numbers called sc ...
s of
functional analysis Functional analysis is a branch of mathematical analysis, the core of which is formed by the study of vector spaces endowed with some kind of limit-related structure (for example, Inner product space#Definition, inner product, Norm (mathematics ...
) is the
tensor product In mathematics, the tensor product V \otimes W of two vector spaces V and W (over the same field) is a vector space to which is associated a bilinear map V\times W \rightarrow V\otimes W that maps a pair (v,w),\ v\in V, w\in W to an element of ...
. The internal language of such categories is
linear logic Linear logic is a substructural logic proposed by French logician Jean-Yves Girard as a refinement of classical and intuitionistic logic, joining the dualities of the former with many of the constructive properties of the latter. Although the ...
, a form of
quantum logic In the mathematical study of logic and the physical analysis of quantum foundations, quantum logic is a set of rules for manip­ulation of propositions inspired by the structure of quantum theory. The formal system takes as its starting p ...
; the corresponding
type system In computer programming, a type system is a logical system comprising a set of rules that assigns a property called a ''type'' (for example, integer, floating point, string) to every '' term'' (a word, phrase, or other set of symbols). Usu ...
is the
linear type system Substructural type systems are a family of type systems analogous to substructural logics where one or more of the structural rules are absent or only allowed under controlled circumstances. Such systems can constrain access to system resource ...
. Such categories are suitable for describing entangled quantum states, and, more generally, allow a vast generalization of the
Curry–Howard correspondence In programming language theory and proof theory, the Curry–Howard correspondence is the direct relationship between computer programs and mathematical proofs. It is also known as the Curry–Howard isomorphism or equivalence, or the proofs-as-p ...
to
quantum mechanics Quantum mechanics is the fundamental physical Scientific theory, theory that describes the behavior of matter and of light; its unusual characteristics typically occur at and below the scale of atoms. Reprinted, Addison-Wesley, 1989, It is ...
, to
cobordism In mathematics, cobordism is a fundamental equivalence relation on the class of compact space, compact manifolds of the same dimension, set up using the concept of the boundary (topology), boundary (French ''wikt:bord#French, bord'', giving ''cob ...
s in
algebraic topology Algebraic topology is a branch of mathematics that uses tools from abstract algebra to study topological spaces. The basic goal is to find algebraic invariant (mathematics), invariants that classification theorem, classify topological spaces up t ...
, and to
string theory In physics, string theory is a theoretical framework in which the point-like particles of particle physics are replaced by one-dimensional objects called strings. String theory describes how these strings propagate through space and intera ...
. The
linear type system Substructural type systems are a family of type systems analogous to substructural logics where one or more of the structural rules are absent or only allowed under controlled circumstances. Such systems can constrain access to system resource ...
, and
linear logic Linear logic is a substructural logic proposed by French logician Jean-Yves Girard as a refinement of classical and intuitionistic logic, joining the dualities of the former with many of the constructive properties of the latter. Although the ...
are useful for describing
synchronization primitive In computer science, synchronization is the task of coordinating multiple processes to join up or handshake at a certain point, in order to reach an agreement or commit to a certain sequence of action. Motivation The need for synchronization ...
s, such as mutual exclusion locks, and the operation of vending machines.


Contrast with partial function application

Currying and partial function application are often conflated. One of the significant differences between the two is that a call to a partially applied function returns the result right away, not another function down the currying chain; this distinction can be illustrated clearly for functions whose
arity In logic, mathematics, and computer science, arity () is the number of arguments or operands taken by a function, operation or relation. In mathematics, arity may also be called rank, but this word can have many other meanings. In logic and ...
is greater than two. Given a function of type f \colon (X \times Y \times Z) \to N , currying produces \text(f) \colon X \to (Y \to (Z \to N)) . That is, while an evaluation of the first function might be represented as f(1, 2, 3), evaluation of the curried function would be represented as f_\text(1)(2)(3), applying each argument in turn to a single-argument function returned by the previous invocation. Note that after calling f_\text(1), we are left with a function that takes a single argument and returns another function, not a function that takes two arguments. In contrast, partial function application refers to the process of fixing a number of arguments to a function, producing another function of smaller arity. Given the definition of f above, we might fix (or 'bind') the first argument, producing a function of type \text(f) \colon (Y \times Z) \to N. Evaluation of this function might be represented as f_\text(2, 3). Note that the result of partial function application in this case is a function that takes two arguments. Intuitively, partial function application says "if you fix the first
argument An argument is a series of sentences, statements, or propositions some of which are called premises and one is the conclusion. The purpose of an argument is to give reasons for one's conclusion via justification, explanation, and/or persu ...
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. Partial application can be seen as evaluating a curried function at a fixed point, e.g. given f \colon (X \times Y \times Z) \to N and a \in X then \text(\text(f)_a)(y)(z) = \text(f)(a)(y)(z) or simply \text(f)_a = \text_1(f)(a) where \text_1 curries f's first parameter. Thus, partial application is reduced to a curried function at a fixed point. Further, a curried function at a fixed point is (trivially), a partial application. For further evidence, note that, given any function f(x,y), a function g(y,x) may be defined such that g(y,x) = f(x,y). Thus, any partial application may be reduced to a single curry operation. As such, curry is more suitably defined as an operation which, in many theoretical cases, is often applied recursively, but which is theoretically indistinguishable (when considered as an operation) from a partial application. So, a partial application can be defined as the objective result of a single application of the curry operator on some ordering of the inputs of some function.


See also

* Tensor-hom adjunction *
Lazy evaluation In programming language theory, lazy evaluation, or call-by-need, is an evaluation strategy which delays the evaluation of an Expression (computer science), expression until its value is needed (non-strict evaluation) and which avoids repeated eva ...
*
Closure (computer science) In programming languages, a closure, also lexical closure or function closure, is a technique for implementing lexically scoped name binding in a language with first-class functions. Operationally, a closure is a record storing a function to ...
* ' theorem *
Closed monoidal category In mathematics, especially in category theory, a closed monoidal category (or a ''monoidal closed category'') is a category that is both a monoidal category and a closed category in such a way that the structures are compatible. A classic examp ...


References


External links

{{Design patterns Higher-order functions Functional programming Lambda calculus Articles with example Java code