AQA 7517, section 4.12.1.4
The idea
In 4.12.1.3, a function like add took a pair of arguments. There is another way to look at it, called partial function application: a function that "takes two arguments" is treated as a function that takes one argument at a time. (Writing a function this way is called currying. Partial application is then giving it fewer arguments than it needs.)
A two-argument function
add takes two integers and gives an integer:
If we give add only its first argument, say 5, then is fixed at 5 and we get a new function of the one remaining argument:
So and . Creating add5 from add like this is partial function application.
In the partial application scheme the type of add is written
This says: add takes one integer, and returns a function. That returned function takes another integer and returns an integer.
So add 5, which supplies only the first argument, is not a number: it is a new function (add5 above), and whatever integer you give it next, it adds 5 to that integer.
Giving add5 its second argument, 3 or 10, completes the application.
Dropping the brackets
The arrow associates to the right, so the brackets can be dropped:
Read this as: add takes an integer, then another integer, and only then produces its integer result.
With the set symbols from 4.12.1.1, the same type is .
One, two and three argument functions
| Number of arguments | Type (partial application scheme) | Application |
|---|---|---|
| One | f x gives a result of type |
|
| Two | f x gives a function . f x y gives type |
|
| Three | f x gives . f x y gives . f x y z gives |
Reading a type from left to right: each arrow means "give me another argument". The type after the last arrow is the final result.
Three-argument example
The function volume multiplies a length, a width and a height. Applying volume to 2, then 3, then 4 gives . At each stage one more argument is supplied, and until the last one the result is a function expecting the rest. The type of volume is .
| Expression | What it is | Type |
|---|---|---|
volume |
The function | |
volume 2 |
Partial: one argument | |
volume 2 3 |
Partial: two arguments | |
volume 2 3 4 |
Complete | (the value 24) |
Why is it useful?
-
You can make specialised functions from general ones:
add 5is "add five", andmultiply 2is "double". -
The new functions are easy to pass to
mapandfilter(4.12.2.1). For example,map (add 10) [1, 2, 3]gives[11, 12, 13]. -
It helps with composition (4.12.1.5), since every function takes one argument.
Worked example
Task: a function multiplies two integers:
Its type is . Partially apply multiply to 3 to make a new function triple. Write triple out, state its type, and work out triple(7).
- Fixing at 3 leaves one argument: .
- One of the two integer arguments has been supplied, so . It is a function that multiplies by 3.
- , and .
Exam tip: the key idea is that applying a function to fewer arguments than it needs gives a new function. Be able to write the type of the result (remove the first type and the first arrow), such as add 5 having type .
Check yourself
What is partial function application?
Applying a function to fewer arguments than it needs, which returns a new function that takes the remaining arguments.
What is the type of add 5 if add has type ?
: a function that adds 5 to its argument.
A function f has type . What is the type of f x y?
.