fbpx
Wikipedia

Currying

In mathematics and computer science, currying 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 that takes two arguments, one from and one from and produces objects in The curried form of this function treats the first argument as a parameter, so as to create a family of functions The family is arranged so that for each object in there is exactly one function

In this example, itself becomes a function, that takes as an argument, and returns a function that maps each to The proper notation for expressing this is verbose. The function belongs to the set of functions Meanwhile, belongs to the set of functions Thus, something that maps to will be of the type With this notation, is a function that takes objects from the first set, and returns objects in the second set, and so one writes 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.[1][2] The example above can be used to illustrate partial application; it is quite similar. Partial application is the function that takes the pair and together as arguments, and returns Using the same notation as above, partial application has the signature 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.[3]

The concept of currying was introduced by Gottlob Frege,[4][5] developed by Moses Schönfinkel,[6][5][7][8][9][10][11] and further developed by Haskell Curry.[8][10][12][13]

Uncurrying is the dual transformation to currying, and can be seen as a form of defunctionalization. It takes a function whose return value is another function , and yields a new function that takes as parameters the arguments for both and , and returns, as a result, the application of and subsequently, , to those arguments. The process can be iterated.

Motivation edit

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 functions with a single argument. Practical functions frequently take more arguments than this. Frege 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.[14] All "ordinary" functions that might typically be encountered in mathematical analysis or in computer programming 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 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. This property is inherited from lambda calculus, where multi-argument functions are usually represented in curried form.

Currying is related to, but not the same as partial application.[1][2] 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 edit

The "Curry" in "Currying" is a reference to logician Haskell Curry, who used the concept extensively, but Moses Schönfinkel had the idea six years before Curry.[10] The alternative name "Schönfinkelisation" has been proposed.[15] In the mathematical context, the principle can be traced back to work in 1893 by Frege.[4][5]

The originator of the word "currying" is not clear. David Turner says the word was coined by Christopher Strachey in his 1967 lecture notes Fundamental Concepts in Programming Languages,[16] but although the concept is mentioned and Curry is mentioned in the context of higher-order functions, the word "currying" does not appear in the notes and Curry is not associated with the concept.[7] John C. Reynolds defined "currying" in a 1972 paper, but did not claim to have coined the term.[8]

Definition edit

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   denotes all functions from   to  . If   is such a function, we write  . Let   denote the ordered pairs of the elements of   and   respectively, that is, the Cartesian product of   and  . Here,   and   may be sets, or they may be types, or they may be other kinds of objects, as explored below.

Given a function

 ,

currying constructs a new function

 .

That is,   takes an argument of type   and returns a function of type  . It is defined by

 

for   of type   and   of type  . We then also write

 

Uncurrying is the reverse transformation, and is most easily understood in terms of its right adjoint, the function  

Set theory edit

In set theory, the notation   is used to denote the set of functions from the set   to the set  . Currying is the natural bijection between the set   of functions from   to  , and the set   of functions from   to the set of functions from   to  . In symbols:

 

Indeed, it is this natural bijection that justifies the exponential notation 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  , the functor   is left adjoint to the functor  .

In the category of sets, the object   is called the exponential object.

Function spaces edit

In the theory of function spaces, such as in functional analysis or homotopy theory, one is commonly interested in continuous functions between topological spaces. One writes   (the Hom functor) for the set of all functions from   to  , and uses the notation   to denote the subset of continuous functions. Here,   is the bijection

 

while uncurrying is the inverse map. If the set   of continuous functions from   to   is given the compact-open topology, and if the space   is locally compact Hausdorff, then

 

is a homeomorphism. This is also the case when  ,   and   are compactly generated,[17]: chapter 5 [18] although there are more cases.[19][20]

One useful corollary is that a function is continuous if and only if its curried form is continuous. Another important result is that the application map, usually called "evaluation" in this context, is continuous (note that eval is a strictly different concept in computer science.) That is,

 

is continuous when   is compact-open and   locally compact Hausdorff.[21] These two results are central for establishing the continuity of homotopy, i.e. when   is the unit interval  , so that   can be thought of as either a homotopy of two functions from   to  , or, equivalently, a single (continuous) path in  .

Algebraic topology edit

In algebraic topology, currying serves as an example of Eckmann–Hilton duality, and, as such, plays an important role in a variety of different settings. For example, loop space is adjoint to reduced suspensions; this is commonly written as

 

where   is the set of homotopy classes of maps  , and   is the suspension of A, and   is the loop space of A. In essence, the suspension   can be seen as the cartesian product of   with the unit interval, modulo an equivalence relation to turn the interval into a loop. The curried form then maps the space   to the space of functions from loops into  , that is, from   into  .[21] Then   is the adjoint functor that maps suspensions to loop spaces, and uncurrying is the dual.[21]

The duality between the mapping cone and the mapping fiber (cofibration and fibration)[17]: chapters 6,7  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, the relationship between currying and uncurrying is known as tensor-hom adjunction. Here, an interesting twist arises: the Hom functor and the tensor product functor might not lift to an exact sequence; this leads to the definition of the Ext functor and the Tor functor.

Domain theory edit

In order theory, that is, the theory of lattices of partially ordered sets,   is a continuous function when the lattice is given the Scott topology.[22] Scott-continuous functions were first investigated in the attempt to provide a semantics for lambda calculus (as ordinary set theory is inadequate to do this). More generally, Scott-continuous functions are now studied in domain theory, which encompasses the study of denotational semantics of computer algorithms. Note that the Scott topology is quite different than many common topologies one might encounter in the category of topological spaces; the Scott topology is typically finer, and is not sober.

The notion of continuity makes its appearance in homotopy type theory, where, roughly speaking, two computer programs can be considered to be homotopic, i.e. compute the same results, if they can be "continuously" refactored from one to the other.

Lambda calculi edit

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. Consider a function   taking two arguments, and having the type  , which should be understood to mean that x must have the type  , y must have the type  , and the function itself returns the type  . The curried form of f is defined as

 

where   is the abstractor of lambda calculus. Since curry takes, as input, functions with the type  , one concludes that the type of curry itself is

 

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

 .

That is, the parenthesis are not required to disambiguate the order of the application.

Curried functions may be used in any programming 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.

Type theory edit

In type theory, the general idea of a type system in computer science is formalized into a specific algebra of types. For example, when writing  , the intent is that   and   are types, while the arrow   is a type constructor, specifically, the function type or arrow type. Similarly, the Cartesian product   of types is constructed by the product type constructor  .

The type-theoretical approach is expressed in programming languages such as ML and the languages derived from and inspired by it: CaML, Haskell and F#.

The type-theoretical approach provides a natural complement to the language of category theory, as discussed below. This is because categories, and specifically, monoidal categories, have an internal language, with simply-typed lambda calculus 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), and as other types of mathematical systems, as explored further, below.

Logic edit

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.

The exponential object   in the category of Heyting algebras is normally written as material implication  . Distributive Heyting algebras are Boolean algebras, and the exponential object has the explicit form  , thus making it clear that the exponential object really is material implication.[23]

Category theory edit

The above notions of currying and uncurrying find their most general, abstract statement in category theory. Currying is a universal property of an exponential object, and gives rise to an adjunction in cartesian closed categories. That is, there is a natural isomorphism between the morphisms from a binary product   and the morphisms to an exponential object  .

This generalizes to a broader result in closed monoidal categories: Currying is the statement that the tensor product and the internal Hom are adjoint functors; that is, for every object   there is a natural isomorphism:

 

Here, Hom denotes the (external) Hom-functor of all morphisms in the category, while   denotes the internal hom functor in the closed monoidal category. For the category of sets, the two are the same. When the product is the cartesian product, then the internal hom   becomes the exponential object  .

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; the more general setting of closed monoidal categories is suitable for quantum computation.[24]

The difference between these two is that the product for cartesian categories (such as the category of sets, complete partial orders or Heyting algebras) is just the Cartesian product; it is interpreted as an ordered pair of items (or a list). Simply typed lambda calculus is the internal language of cartesian closed categories; and it is for this reason that pairs and lists are the primary types in the type theory of LISP, Scheme and many functional programming languages.

By contrast, the product for monoidal categories (such as Hilbert space and the vector spaces of functional analysis) is the tensor product. The internal language of such categories is linear logic, a form of quantum logic; the corresponding type system is the linear type system. Such categories are suitable for describing entangled quantum states, and, more generally, allow a vast generalization of the Curry–Howard correspondence to quantum mechanics, to cobordisms in algebraic topology, and to string theory.[3] The linear type system, and linear logic are useful for describing synchronization primitives, such as mutual exclusion locks, and the operation of vending machines.

Contrast with partial function application edit

Currying and partial function application are often conflated.[1][2] 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 is greater than two.[25]

Given a function of type  , currying produces  . That is, while an evaluation of the first function might be represented as  , evaluation of the curried function would be represented as  , applying each argument in turn to a single-argument function returned by the previous invocation. Note that after calling  , 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   above, 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.

Intuitively, partial function application says "if you fix the first argument 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   and   then   or simply   where   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  , a function   may be defined such that  . 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 edit

References edit

  1. ^ a b c cdiggins (24 May 2007). "Currying != Generalized Partial Application?!". Lambda the Ultimate: The Programming Languages Weblog.
  2. ^ a b c "Partial Function Application is not Currying". The Uncarved Block. 7 August 2020. from the original on 23 October 2016.
  3. ^ a b Baez, John C.; Stay, Mike (6 June 2009). "Physics, Topology, Logic and Computation: A Rosetta Stone". In Coecke, Bob (ed.). (PDF). Lecture Notes in Physics. Vol. 813: New Structures for Physics. Berlin, Heidelberg: Springer (published 5 July 2010). pp. 95–172. arXiv:0903.0340. doi:10.1007/978-3-642-12821-9_2. ISBN 978-3-642-12821-9. S2CID 115169297. Archived from the original (PDF) on 5 December 2022.
  4. ^ a b Frege, Gottlob (1893). "§ 36". Grundgesetze der arithmetik (in German). Book from the collections of University of Wisconsin - Madison, digitized by Google on 26 August 2008. Jena: Hermann Pohle. pp. 54–55.
  5. ^ a b c Quine, W. V. (1967). "Introduction to Moses Schönfinkel's 1924 "On the building blocks of mathematical logic"". In van Heijenoort, Jean (ed.). From Frege to Gödel: A Source Book in Mathematical Logic, 1879-1931. Harvard University Press. pp. 355–357. ISBN 9780674324497.
  6. ^ Schönfinkel, Moses (September 1924) [Presented at the Mathematischen Gesellschaft (Mathematical Society) in Göttingen on 7 December 1920. Received by Mathematische Annalen on 15 March 1924.]. Written at Moskau. "Über die Bausteine der mathematischen Logik" [On the building blocks of mathematical logic] (PDF). Mathematische Annalen. 92 (3–4). Berlin?: Springer: 305–316. doi:10.1007/BF01448013. S2CID 118507515.
  7. ^ a b Strachey, Christopher (April 2000) [This paper forms the substance of a course of lectures given at the International Summer School in Computer Programming at Copenhagen in August, 1967.]. "Fundamental Concepts in Programming Languages". Higher-Order and Symbolic Computation. 13: 11–49. CiteSeerX 10.1.1.332.3161. doi:10.1023/A:1010000313106. ISSN 1573-0557. S2CID 14124601. There is a device originated by Schönfinkel, for reducing operators with several operands to the successive application of single operand operators.
  8. ^ a b c Originally published as Reynolds, John C. (1 August 1972). "Definitional interpreters for higher-order programming languages". In Rosemary Shields (ed.). Proceedings of the ACM annual conference - ACM '72. Vol. 2. ACM Press. pp. 717–740. doi:10.1145/800194.805852. ISBN 9781450374927. S2CID 163294. In the last line we have used a trick called Currying (after the logician H. Curry) to solve the problem of introducing a binary operation into a language where all functions must accept a single argument. (The referee comments that although "Currying" is tastier, "Schönfinkeling" might be more accurate.) Republished as Reynolds, John C. (1998). "Definitional Interpreters for Higher-Order Programming Languages". Higher-Order and Symbolic Computation. 11 (4). Boston: Kluwer Academic Publishers: 363–397. doi:10.1023/A:1010027404223. 13 – via Syracuse University: College of Engineering and Computer Science - Former Departments, Centers, Institutes and Projects.
  9. ^ Slonneger, Kenneth; Kurtz, Barry L. (1995). "Curried Functions, 5.1: Concepts and Examples, Chapter 5: The Lambda Calculus". Formal Syntax and Semantics of Programming Languages: A Laboratory Based Approach (PDF). Addison-Wesley Publishing Company. p. 144. ISBN 0-201-65697-3.
  10. ^ a b c Curry, Haskell B. (1980). Barwise, Jon; Keisler, H. Jerome; Kunen, Kenneth (eds.). "Some Philosophical Aspects of Combinatory Logic". The Kleene Symposium: Proceedings of the Symposium Held June 18-24, 1978 at Madison, Wisconsin, U.S.A. (Studies in Logic and the Foundations of Mathematics). Studies in Logic and the Foundations of Mathematics. 101. North-Holland Publishing Company, imprint of Elsevier: 85–101. doi:10.1016/S0049-237X(08)71254-0. ISBN 9780444853455. ISSN 0049-237X. S2CID 117179133. Some contemporary logicians call this way of looking at a function "currying", because I made extensive use of it; but Schönfinkel had the idea some 6 years before I did.
  11. ^ "Currying Schonfinkelling". Portland Pattern Repository's Wiki. Cunningham & Cunningham, Inc. 6 May 2012.
  12. ^ Barendregt, Henk; Barendsen, Erik (March 2000) [December 1998]. Introduction to Lambda Calculus (PDF) (Revised ed.). p. 8.
  13. ^ Curry, Haskell; Feys, Robert (1958). Combinatory logic. Vol. I (2 ed.). Amsterdam, Netherlands: North-Holland Publishing Company.
  14. ^ Hutton, Graham; Jones, Mark P., eds. (November 2002). "Frequently Asked Questions for comp.lang.functional, 3. Technical topics, 3.2. Currying". University of Nottingham Computer Science.
  15. ^ Heim, Irene; Kratzer, Angelika (January 2, 1998). Semantics in Generative Grammar (PDF). Malden, Massachusetts: Blackwell Publishers, an imprint of Wiley. ISBN 0-631-19712-5.{{cite book}}: CS1 maint: date and year (link)
  16. ^ Turner, David (1 Jun 1997). "Programming language, Currying, or Schonfinkeling?, #9 / 14". Computer Programming Language Forum. from the original on 3 March 2022. Retrieved 3 March 2022.
  17. ^ a b May, Jon Peter (1999). A concise course in algebraic topology (PDF). Chicago lectures in mathematics. Chicago, Ill.: University of Chicago Press. pp. 39–55. ISBN 0-226-51183-9. OCLC 41266205.
  18. ^ "compactly generated topological space". nLab. 28 May 2023.
  19. ^ Tillotson, J.; Booth, Peter I. (March 1980) [Received 2 October 1978, revised 29 June 1979, published 1 May 1980]. Written at Memorial University of Newfoundland. "Monoidal closed, Cartesian closed and convenient categories of topological spaces" (PDF). Pacific Journal of Mathematics. 88 (1). Berkeley, California: Mathematical Sciences Publishers: 35–53. doi:10.2140/pjm.1980.88.35. eISSN 1945-5844. ISSN 0030-8730.
  20. ^ "convenient category of topological spaces". nLab. 11 August 2023.
  21. ^ a b c Rotman, Joseph Jonah (1988). "Chapter 11". An introduction to algebraic topology. Graduate texts in mathematics; 119. New York: Springer-Verlag. ISBN 978-0-387-96678-6. OCLC 17383909.
  22. ^ Barendregt, Hendrik Pieter (1984). "Theorems 1.2.13, 1.2.14". The lambda calculus: its syntax and semantics. Studies in logic and the foundations of mathematics. Vol. 103 (Rev. ed.). North-Holland, an imprint of Elsevier. ISBN 978-0-444-87508-2.
  23. ^ Mac Lane, Saunders; Moerdijk, Ieke (1992). "Chapter I. Categories of Functors; sections 7. Propositional Calculus, 8. Heyting Algebras, and 9. Quantifiers as Adjoints". Sheaves in Geometry and Logic: A First Introduction to Topos Theory. New York: Springer-Verlag, part of Springer Science & Business Media. pp. 48–57. ISBN 978-0-387-97710-2.
  24. ^ Abramsky, Samson; Coecke, Bob (5 March 2007). "A categorical semantics of quantum protocols". Logic in Computer Science (LICS 2004): Proceedings, 19th Annual IEEE Symposium, Turku, Finland, 2004]. IEEE Computer Society Press. arXiv:quant-ph/0402130. doi:10.1109/LICS.2004.1319636.
  25. ^ Lee, G. Kay (15 May 2013). "Functional Programming in 5 Minutes". Slides.

External links edit

currying, this, article, about, mathematical, technique, cooking, process, this, name, curry, leather, finishing, process, currier, horse, grooming, currycomb, mathematics, computer, science, currying, technique, translating, function, that, takes, multiple, a. This article is about the mathematical technique For the cooking process of this name see Curry For the leather finishing process see Currier For horse grooming see Currycomb In mathematics and computer science currying 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 fx 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 fx displaystyle f x 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 fx 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 fx displaystyle f x belongs to the set of functions Y Z displaystyle Y to Z Thus something that maps x displaystyle x to fx 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 1 2 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 fx 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 3 The concept of currying was introduced by Gottlob Frege 4 5 developed by Moses Schonfinkel 6 5 7 8 9 10 11 and further developed by Haskell Curry 8 10 12 13 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 Contents 1 Motivation 2 History 3 Definition 3 1 Set theory 3 2 Function spaces 3 3 Algebraic topology 3 4 Domain theory 3 5 Lambda calculi 3 6 Type theory 3 7 Logic 3 8 Category theory 4 Contrast with partial function application 5 See also 6 References 7 External linksMotivation editCurrying 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 functions with a single argument Practical functions frequently take more arguments than this Frege 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 14 All ordinary functions that might typically be encountered in mathematical analysis or in computer programming 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 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 This property is inherited from lambda calculus where multi argument functions are usually represented in curried form Currying is related to but not the same as partial application 1 2 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 editThe Curry in Currying is a reference to logician Haskell Curry who used the concept extensively but Moses Schonfinkel had the idea six years before Curry 10 The alternative name Schonfinkelisation has been proposed 15 In the mathematical context the principle can be traced back to work in 1893 by Frege 4 5 The originator of the word currying is not clear David Turner says the word was coined by Christopher Strachey in his 1967 lecture notes Fundamental Concepts in Programming Languages 16 but although the concept is mentioned and Curry is mentioned in the context of higher order functions the word currying does not appear in the notes and Curry is not associated with the concept 7 John C Reynolds defined currying in a 1972 paper but did not claim to have coined the term 8 Definition editCurrying 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 Y displaystyle X to Y nbsp denotes all functions from X displaystyle X nbsp to Y displaystyle Y nbsp If f displaystyle f nbsp is such a function we write f X Y displaystyle f colon X to Y nbsp Let X Y displaystyle X times Y nbsp denote the ordered pairs of the elements of X displaystyle X nbsp and Y displaystyle Y nbsp respectively that is the Cartesian product of X displaystyle X nbsp and Y displaystyle Y nbsp Here X displaystyle X nbsp and Y displaystyle Y nbsp may be sets or they may be types or they may be other kinds of objects as explored below Given a function f X Y Z displaystyle f colon X times Y to Z nbsp currying constructs a new function g X Y Z displaystyle g colon X to Y to Z nbsp That is g displaystyle g nbsp takes an argument of type X displaystyle X nbsp and returns a function of type Y Z displaystyle Y to Z nbsp It is defined by g x y f x y displaystyle g x y f x y nbsp for x displaystyle x nbsp of type X displaystyle X nbsp and y displaystyle y nbsp of type Y displaystyle Y nbsp We then also write curry f g displaystyle text curry f g nbsp Uncurrying is the reverse transformation and is most easily understood in terms of its right adjoint the function apply displaystyle operatorname apply nbsp Set theory edit In set theory the notation YX displaystyle Y X nbsp is used to denote the set of functions from the set X displaystyle X nbsp to the set Y displaystyle Y nbsp Currying is the natural bijection between the set AB C displaystyle A B times C nbsp of functions from B C displaystyle B times C nbsp to A displaystyle A nbsp and the set AC B displaystyle A C B nbsp of functions from B displaystyle B nbsp to the set of functions from C displaystyle C nbsp to A displaystyle A nbsp In symbols AB C AC B displaystyle A B times C cong A C B nbsp Indeed it is this natural bijection that justifies the exponential notation 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 displaystyle C nbsp the functor B B C displaystyle B mapsto B times C nbsp is left adjoint to the functor A AC displaystyle A mapsto A C nbsp In the category of sets the object YX displaystyle Y X nbsp is called the exponential object Function spaces edit In the theory of function spaces such as in functional analysis or homotopy theory one is commonly interested in continuous functions between topological spaces One writes Hom X Y displaystyle text Hom X Y nbsp the Hom functor for the set of all functions from X displaystyle X nbsp to Y displaystyle Y nbsp and uses the notation YX displaystyle Y X nbsp to denote the subset of continuous functions Here curry displaystyle text curry nbsp is the bijection curry Hom X Y Z Hom X Hom Y Z displaystyle text curry text Hom X times Y Z to text Hom X text Hom Y Z nbsp while uncurrying is the inverse map If the set YX displaystyle Y X nbsp of continuous functions from X displaystyle X nbsp to Y displaystyle Y nbsp is given the compact open topology and if the space Y displaystyle Y nbsp is locally compact Hausdorff then curry ZX Y ZY X displaystyle text curry Z X times Y to Z Y X nbsp is a homeomorphism This is also the case when X displaystyle X nbsp Y displaystyle Y nbsp and YX displaystyle Y X nbsp are compactly generated 17 chapter 5 18 although there are more cases 19 20 One useful corollary is that a function is continuous if and only if its curried form is continuous Another important result is that the application map usually called evaluation in this context is continuous note that eval is a strictly different concept in computer science That is eval YX X Y f x f x displaystyle begin aligned amp amp text eval Y X times X to Y amp amp f x mapsto f x end aligned nbsp is continuous when YX displaystyle Y X nbsp is compact open and Y displaystyle Y nbsp locally compact Hausdorff 21 These two results are central for establishing the continuity of homotopy i e when X displaystyle X nbsp is the unit interval I displaystyle I nbsp so that ZI Y ZY I displaystyle Z I times Y cong Z Y I nbsp can be thought of as either a homotopy of two functions from Y displaystyle Y nbsp to Z displaystyle Z nbsp or equivalently a single continuous path in ZY displaystyle Z Y nbsp Algebraic topology edit In algebraic topology currying serves as an example of Eckmann Hilton duality and as such plays an important role in a variety of different settings For example loop space is adjoint to reduced suspensions this is commonly written as SX Z X WZ displaystyle Sigma X Z approxeq X Omega Z nbsp where A B displaystyle A B nbsp is the set of homotopy classes of maps A B displaystyle A rightarrow B nbsp and SA displaystyle Sigma A nbsp is the suspension of A and WA displaystyle Omega A nbsp is the loop space of A In essence the suspension SX displaystyle Sigma X nbsp can be seen as the cartesian product of X displaystyle X nbsp with the unit interval modulo an equivalence relation to turn the interval into a loop The curried form then maps the space X displaystyle X nbsp to the space of functions from loops into Z displaystyle Z nbsp that is from X displaystyle X nbsp into WZ displaystyle Omega Z nbsp 21 Then curry displaystyle text curry nbsp is the adjoint functor that maps suspensions to loop spaces and uncurrying is the dual 21 The duality between the mapping cone and the mapping fiber cofibration and fibration 17 chapters 6 7 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 the relationship between currying and uncurrying is known as tensor hom adjunction Here an interesting twist arises the Hom functor and the tensor product functor might not lift to an exact sequence this leads to the definition of the Ext functor and the Tor functor Domain theory edit In order theory that is the theory of lattices of partially ordered sets curry displaystyle text curry nbsp is a continuous function when the lattice is given the Scott topology 22 Scott continuous functions were first investigated in the attempt to provide a semantics for lambda calculus as ordinary set theory is inadequate to do this More generally Scott continuous functions are now studied in domain theory which encompasses the study of denotational semantics of computer algorithms Note that the Scott topology is quite different than many common topologies one might encounter in the category of topological spaces the Scott topology is typically finer and is not sober The notion of continuity makes its appearance in homotopy type theory where roughly speaking two computer programs can be considered to be homotopic i e compute the same results if they can be continuously refactored from one to the other Lambda calculi edit 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 Consider a function f x y displaystyle f x y nbsp taking two arguments and having the type X Y Z displaystyle X times Y to Z nbsp which should be understood to mean that x must have the type X displaystyle X nbsp y must have the type Y displaystyle Y nbsp and the function itself returns the type Z displaystyle Z nbsp The curried form of f is defined as curry f lx ly f x y displaystyle text curry f lambda x lambda y f x y nbsp where l displaystyle lambda nbsp is the abstractor of lambda calculus Since curry takes as input functions with the type X Y Z displaystyle X times Y to Z nbsp one concludes that the type of curry itself is curry X Y Z X Y Z displaystyle text curry X times Y to Z to X to Y to Z nbsp The operator is often considered right associative so the curried function type X Y Z displaystyle X to Y to Z nbsp is often written as X Y Z displaystyle X to Y to Z nbsp Conversely function application is considered to be left associative so that f x y displaystyle f x y nbsp is equivalent to curry f x y curry f xy displaystyle text curry f x y text curry f x y nbsp That is the parenthesis are not required to disambiguate the order of the application Curried functions may be used in any programming 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 Type theory edit In type theory the general idea of a type system in computer science is formalized into a specific algebra of types For example when writing f X Y displaystyle f colon X to Y nbsp the intent is that X displaystyle X nbsp and Y displaystyle Y nbsp are types while the arrow displaystyle to nbsp is a type constructor specifically the function type or arrow type Similarly the Cartesian product X Y displaystyle X times Y nbsp of types is constructed by the product type constructor displaystyle times nbsp The type theoretical approach is expressed in programming languages such as ML and the languages derived from and inspired by it CaML Haskell and F The type theoretical approach provides a natural complement to the language of category theory as discussed below This is because categories and specifically monoidal categories have an internal language with simply typed lambda calculus 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 and as other types of mathematical systems as explored further below Logic edit Under the Curry Howard correspondence the existence of currying and uncurrying is equivalent to the logical theorem A B C A B C displaystyle A land B to C Leftrightarrow A to B to C nbsp as tuples product type corresponds to conjunction in logic and function type corresponds to implication The exponential object QP displaystyle Q P nbsp in the category of Heyting algebras is normally written as material implication P Q displaystyle P to Q nbsp Distributive Heyting algebras are Boolean algebras and the exponential object has the explicit form P Q displaystyle neg P lor Q nbsp thus making it clear that the exponential object really is material implication 23 Category theory edit The above notions of currying and uncurrying find their most general abstract statement in category theory Currying is a universal property of an exponential object and gives rise to an adjunction in cartesian closed categories That is there is a natural isomorphism between the morphisms from a binary product f X Y Z displaystyle f colon X times Y to Z nbsp and the morphisms to an exponential object g X ZY displaystyle g colon X to Z Y nbsp This generalizes to a broader result in closed monoidal categories Currying is the statement that the tensor product and the internal Hom are adjoint functors that is for every object B displaystyle B nbsp there is a natural isomorphism Hom A B C Hom A B C displaystyle mathrm Hom A otimes B C cong mathrm Hom A B Rightarrow C nbsp Here Hom denotes the external Hom functor of all morphisms in the category while B C displaystyle B Rightarrow C nbsp denotes the internal hom functor in the closed monoidal category For the category of sets the two are the same When the product is the cartesian product then the internal hom B C displaystyle B Rightarrow C nbsp becomes the exponential object CB displaystyle C B nbsp 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 the more general setting of closed monoidal categories is suitable for quantum computation 24 The difference between these two is that the product for cartesian categories such as the category of sets complete partial orders or Heyting algebras is just the Cartesian product it is interpreted as an ordered pair of items or a list Simply typed lambda calculus is the internal language of cartesian closed categories and it is for this reason that pairs and lists are the primary types in the type theory of LISP Scheme and many functional programming languages By contrast the product for monoidal categories such as Hilbert space and the vector spaces of functional analysis is the tensor product The internal language of such categories is linear logic a form of quantum logic the corresponding type system is the linear type system Such categories are suitable for describing entangled quantum states and more generally allow a vast generalization of the Curry Howard correspondence to quantum mechanics to cobordisms in algebraic topology and to string theory 3 The linear type system and linear logic are useful for describing synchronization primitives such as mutual exclusion locks and the operation of vending machines Contrast with partial function application editMain article Partial application Currying and partial function application are often conflated 1 2 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 is greater than two 25 Given a function of type f X Y Z N displaystyle f colon X times Y times Z to N nbsp currying produces curry f X Y Z N displaystyle text curry f colon X to Y to Z to N nbsp That is while an evaluation of the first function might be represented as f 1 2 3 displaystyle f 1 2 3 nbsp evaluation of the curried function would be represented as fcurried 1 2 3 displaystyle f text curried 1 2 3 nbsp applying each argument in turn to a single argument function returned by the previous invocation Note that after calling fcurried 1 displaystyle f text curried 1 nbsp 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 displaystyle f nbsp above we might fix or bind the first argument producing a function of type partial f Y Z N displaystyle text partial f colon Y times Z to N nbsp Evaluation of this function might be represented as fpartial 2 3 displaystyle f text partial 2 3 nbsp 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 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 X Y Z N displaystyle f colon X times Y times Z to N nbsp and a X displaystyle a in X nbsp then curry partial f a y z curry f a y z displaystyle text curry text partial f a y z text curry f a y z nbsp or simply partial f a curry1 f a displaystyle text partial f a text curry 1 f a nbsp where curry1 displaystyle text curry 1 nbsp 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 displaystyle f x y nbsp a function g y x displaystyle g y x nbsp may be defined such that g y x f x y displaystyle g y x f x y nbsp 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 editTensor hom adjunction Lazy evaluation Closure computer science S mn theorem Closed monoidal categoryReferences edit a b c cdiggins 24 May 2007 Currying Generalized Partial Application Lambda the Ultimate The Programming Languages Weblog a b c Partial Function Application is not Currying The Uncarved Block 7 August 2020 Archived from the original on 23 October 2016 a b Baez John C Stay Mike 6 June 2009 Physics Topology Logic and Computation A Rosetta Stone In Coecke Bob ed New Structures for Physics PDF Lecture Notes in Physics Vol 813 New Structures for Physics Berlin Heidelberg Springer published 5 July 2010 pp 95 172 arXiv 0903 0340 doi 10 1007 978 3 642 12821 9 2 ISBN 978 3 642 12821 9 S2CID 115169297 Archived from the original PDF on 5 December 2022 a b Frege Gottlob 1893 36 Grundgesetze der arithmetik in German Book from the collections of University of Wisconsin Madison digitized by Google on 26 August 2008 Jena Hermann Pohle pp 54 55 a b c Quine W V 1967 Introduction to Moses Schonfinkel s 1924 On the building blocks of mathematical logic In van Heijenoort Jean ed From Frege to Godel A Source Book in Mathematical Logic 1879 1931 Harvard University Press pp 355 357 ISBN 9780674324497 Schonfinkel Moses September 1924 Presented at the Mathematischen Gesellschaft Mathematical Society in Gottingen on 7 December 1920 Received by Mathematische Annalen on 15 March 1924 Written at Moskau Uber die Bausteine der mathematischen Logik On the building blocks of mathematical logic PDF Mathematische Annalen 92 3 4 Berlin Springer 305 316 doi 10 1007 BF01448013 S2CID 118507515 a b Strachey Christopher April 2000 This paper forms the substance of a course of lectures given at the International Summer School in Computer Programming at Copenhagen in August 1967 Fundamental Concepts in Programming Languages Higher Order and Symbolic Computation 13 11 49 CiteSeerX 10 1 1 332 3161 doi 10 1023 A 1010000313106 ISSN 1573 0557 S2CID 14124601 There is a device originated by Schonfinkel for reducing operators with several operands to the successive application of single operand operators a b c Originally published as Reynolds John C 1 August 1972 Definitional interpreters for higher order programming languages In Rosemary Shields ed Proceedings of the ACM annual conference ACM 72 Vol 2 ACM Press pp 717 740 doi 10 1145 800194 805852 ISBN 9781450374927 S2CID 163294 In the last line we have used a trick called Currying after the logician H Curry to solve the problem of introducing a binary operation into a language where all functions must accept a single argument The referee comments that although Currying is tastier Schonfinkeling might be more accurate Republished as Reynolds John C 1998 Definitional Interpreters for Higher Order Programming Languages Higher Order and Symbolic Computation 11 4 Boston Kluwer Academic Publishers 363 397 doi 10 1023 A 1010027404223 13 via Syracuse University College of Engineering and Computer Science Former Departments Centers Institutes and Projects Slonneger Kenneth Kurtz Barry L 1995 Curried Functions 5 1 Concepts and Examples Chapter 5 The Lambda Calculus Formal Syntax and Semantics of Programming Languages A Laboratory Based Approach PDF Addison Wesley Publishing Company p 144 ISBN 0 201 65697 3 a b c Curry Haskell B 1980 Barwise Jon Keisler H Jerome Kunen Kenneth eds Some Philosophical Aspects of Combinatory Logic The Kleene Symposium Proceedings of the Symposium Held June 18 24 1978 at Madison Wisconsin U S A Studies in Logic and the Foundations of Mathematics Studies in Logic and the Foundations of Mathematics 101 North Holland Publishing Company imprint of Elsevier 85 101 doi 10 1016 S0049 237X 08 71254 0 ISBN 9780444853455 ISSN 0049 237X S2CID 117179133 Some contemporary logicians call this way of looking at a function currying because I made extensive use of it but Schonfinkel had the idea some 6 years before I did Currying Schonfinkelling Portland Pattern Repository s Wiki Cunningham amp Cunningham Inc 6 May 2012 Barendregt Henk Barendsen Erik March 2000 December 1998 Introduction to Lambda Calculus PDF Revised ed p 8 Curry Haskell Feys Robert 1958 Combinatory logic Vol I 2 ed Amsterdam Netherlands North Holland Publishing Company Hutton Graham Jones Mark P eds November 2002 Frequently Asked Questions for comp lang functional 3 Technical topics 3 2 Currying University of Nottingham Computer Science Heim Irene Kratzer Angelika January 2 1998 Semantics in Generative Grammar PDF Malden Massachusetts Blackwell Publishers an imprint of Wiley ISBN 0 631 19712 5 a href Template Cite book html title Template Cite book cite book a CS1 maint date and year link Turner David 1 Jun 1997 Programming language Currying or Schonfinkeling 9 14 Computer Programming Language Forum Archived from the original on 3 March 2022 Retrieved 3 March 2022 a b May Jon Peter 1999 A concise course in algebraic topology PDF Chicago lectures in mathematics Chicago Ill University of Chicago Press pp 39 55 ISBN 0 226 51183 9 OCLC 41266205 compactly generated topological space nLab 28 May 2023 Tillotson J Booth Peter I March 1980 Received 2 October 1978 revised 29 June 1979 published 1 May 1980 Written at Memorial University of Newfoundland Monoidal closed Cartesian closed and convenient categories of topological spaces PDF Pacific Journal of Mathematics 88 1 Berkeley California Mathematical Sciences Publishers 35 53 doi 10 2140 pjm 1980 88 35 eISSN 1945 5844 ISSN 0030 8730 convenient category of topological spaces nLab 11 August 2023 a b c Rotman Joseph Jonah 1988 Chapter 11 An introduction to algebraic topology Graduate texts in mathematics 119 New York Springer Verlag ISBN 978 0 387 96678 6 OCLC 17383909 Barendregt Hendrik Pieter 1984 Theorems 1 2 13 1 2 14 The lambda calculus its syntax and semantics Studies in logic and the foundations of mathematics Vol 103 Rev ed North Holland an imprint of Elsevier ISBN 978 0 444 87508 2 Mac Lane Saunders Moerdijk Ieke 1992 Chapter I Categories of Functors sections 7 Propositional Calculus 8 Heyting Algebras and 9 Quantifiers as Adjoints Sheaves in Geometry and Logic A First Introduction to Topos Theory New York Springer Verlag part of Springer Science amp Business Media pp 48 57 ISBN 978 0 387 97710 2 Abramsky Samson Coecke Bob 5 March 2007 A categorical semantics of quantum protocols Logic in Computer Science LICS 2004 Proceedings 19th Annual IEEE Symposium Turku Finland 2004 IEEE Computer Society Press arXiv quant ph 0402130 doi 10 1109 LICS 2004 1319636 Lee G Kay 15 May 2013 Functional Programming in 5 Minutes Slides External links edit nbsp Look up currying in Wiktionary the free dictionary Retrieved from https en wikipedia org w index php title Currying amp oldid 1214432439, wikipedia, wiki, book, books, library,

article

, read, download, free, free download, mp3, video, mp4, 3gp, jpg, jpeg, gif, png, picture, music, song, movie, book, game, games.