Partial application
Encyclopedia
In computer science
, partial application (or partial function application) refers to the process of fixing a number of arguments to a function, producing another function of smaller arity
. Given a function , we might fix (or 'bind') the first argument, producing a function of type . Evaluation of this function might be represented as . Note that the result of partial function application in this case is a function that takes two arguments.
s of the function, you get a function of the remaining arguments". For example, if function div stands for the division operation x / y, then div with the parameter x fixed at 1 (i.e. div 1) is another function: the same as the function inv that returns the multiplicative inverse of its argument, defined by inv(y) = 1 / y.
The practical motivation for partial application is that very often the functions obtained by supplying some but not all of the arguments to a function are useful; for example, many languages have a function or operator similar to
In languages such as ML and Haskell
functions are defined in curried
form by default. Supplying fewer than the total number of arguments is referred to as partial application. In languages with first-class functions one can define
s, while Haskell can use more efficient techniques.
and product type
s (λ→,×) partial application, currying and uncurrying can be defined as:
Note that
Computer science
Computer science or computing science is the study of the theoretical foundations of information and computation and of practical techniques for their implementation and application in computer systems...
, partial application (or partial function application) refers to the process of fixing a number of arguments to a function, producing another function of smaller arity
Arity
In logic, mathematics, and computer science, the arity of a function or operation is the number of arguments or operands that the function takes. The arity of a relation is the dimension of the domain in the corresponding Cartesian product...
. Given a function , we might fix (or 'bind') the first argument, producing a function of type . Evaluation of this function might be represented as . Note that the result of partial function application in this case is a function that takes two arguments.
Motivation
Intuitively, partial function application says "if you fix the first argumentParameter (computer science)
In computer programming, a parameter is a special kind of variable, used in a subroutine to refer to one of the pieces of data provided as input to the subroutine. These pieces of data are called arguments...
s of the function, you get a function of the remaining arguments". For example, if function div stands for the division operation x / y, then div with the parameter x fixed at 1 (i.e. div 1) is another function: the same as the function inv that returns the multiplicative inverse of its argument, defined by inv(y) = 1 / y.
The practical motivation for partial application is that very often the functions obtained by supplying some but not all of the arguments to a function are useful; for example, many languages have a function or operator similar to
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.In languages such as ML and Haskell
Haskell (programming language)
Haskell is a standardized, general-purpose purely functional programming language, with non-strict semantics and strong static typing. It is named after logician Haskell Curry. In Haskell, "a function is a first-class citizen" of the programming language. As a functional programming language, the...
functions are defined in curried
Currying
In mathematics and computer science, currying is the technique of transforming a function that takes multiple arguments in such a way that it can be called as a chain of functions each with a single argument...
form by default. Supplying fewer than the total number of arguments is referred to as partial application. In languages with first-class functions one can define
curry
, uncurry
and papply
to perform currying and partial application explicitly. This might incur a greater run-time overhead due to the creation of additional closureClosure (computer science)
In computer science, a closure is a function together with a referencing environment for the non-local variables of that function. A closure allows a function to access variables outside its typical scope. Such a function is said to be "closed over" its free variables...
s, while Haskell can use more efficient techniques.
Definitions
In the simply-typed lambda calculus with functionFunction type
In computer science, a function type is the type of a variable or parameter to which a function has or can be assigned or the result type of a higher-order function returning a function....
and product type
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 product. An instance of a product type retains the fixed...
s (λ→,×) partial application, currying and uncurrying can be defined as:
-
papply
: (((a × b) → c) × a) → (b → c) = λ(f, x). λy. f (x, y) -
curry
: ((a × b) → c) → (a → (b → c)) = λf. λx. λy. f (x, y) -
uncurry
: (a → (b → c)) → ((a × b) → c) = λf. λ(x, y). f x y
Note that
curry
papply
= curry
.Further reading
- Simon Marlow and Simon Peyton JonesSimon Peyton JonesSimon Peyton Jones is a British computer scientist who researches the implementation and applications of functional programming languages, particularly lazy functional languages...
(2004, 2006). "Making a Fast Curry: Push/Enter vs. Eval/Apply for Higher-order Languages". ICFP '04 Proceedings of the ninth ACM SIGPLAN international conference on Functional programming. - Benjamin C. PierceBenjamin C. PierceBenjamin Crawford Pierce is an American professor of computer science at the University of Pennsylvania. Pierce joined Penn in 1998 from Indiana University and held research positions at the University of Cambridge and the University of Edinburgh. He received his Ph.D. from Carnegie Mellon...
et al. "Partial Application", "Digression: Currying". Software Foundations.
External links
- Partial function application on Rosetta code.
- Partial application at Haskell Wiki
- Constant applicative form at Haskell Wiki
- The dangers of being too partial