Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Partial application and closures

I was asked what's the relationship between partial function application and closures. I would say there isn't any, unless I'm missing the point. Let's say I'm writing in python and I have a very simple function MySum defined as follows:

MySum = lambda x, y : x + y;

Now I'm fixing one parameter to obtain a function with smaller arity which returns the same value that MySum would return if I called it with the same parameters (partial application):

MyPartialSum = lambda x : MySum(x, 0);

I could do the the very same thing with C:

int MySum(int x, int y) { return x + y; }
int MyPartialSum(int x) { return MySum(x, 0); }

So, the dumb question is: what's the difference? Why should I need closures for partial applications? These codes are equivalent, I don't see what's the bound with closures and partial application.

like image 550
Eigendrea Avatar asked Jul 21 '12 08:07

Eigendrea


4 Answers

Partial function application is about fixing some arguments of a given function to yield another function with fewer arguments, like

sum = lambda x, y: x + y
inc = lambda x: sum(x, 1)

Notice that 'inc' is 'sum' partially applied, without capturing anything from the context (as you mentioned closure).

But, such hand-written (usually anonymous) functions are kind of tedious. One can use a function factory, which returns an inner function. The inner function can be parameterized by capturing some variable from its context, like

# sum = lambda x, y: x + y
def makePartialSumF(n):
    def partialSumF(x):
        return sum(x, n)
    return partialSumF

inc = makePartialSumF(1)
plusTwo = makePartialSumF(2)

Here the factory makePartialSumF is invoked twice. Each call results in a partialSumF function (capturing different values as n). Using closure makes the implementation of partial application convenient. So you can say partial application can be implemented by means of closure. Of course, closures can do many other things! (As a side node, python does not have proper closure.)

Currying is about turning a function of N arguments into a unary function which returns a unary function... for example we have a function which takes three arguments and returns a value:

sum = lambda x, y, z: x + y + z

The curried version is

curriedSum = lambda x: lambda y: lambda z: x + y + z

I bet you wouldn't write python code like that. IMO the motivation of Currying is mostly of theoretical interest. (A framework of expressing computations using only unary functions: every function is unary!) The practical byproduct is that, in languages where functions are curried, some partial applications (when you 'fix' arguments from the left) are as trivial as supplying arguments to curried function. (But not all partial applications are as such. Example: given f(x,y,z) = x+2*y+3*z, when you binds y to a constant to yield a function of two variables.) So you can say, Currying is a technique which, in practice and as a byproduct, can make many useful partial functional applications trivial, but that's not the point of Currying.

like image 168
Ji Han Avatar answered Oct 18 '22 00:10

Ji Han


Partial application is a technique whereby you take an existing function and a subset of it's arguments, and produce a new function that accepts the remaining arguments.

In other words, if you have function F(a, b), a function that applies partial application of a would look like B(fn, a) where F(a, b) = B(F, a)(b).

In your example you're simply creating new functions, rather than applying partial application to the existing one.

Here's an example in python:

def curry_first(fn, arg):
    def foo(*args):
       return fn(arg, *args)
    return foo

This creates a closure over the supplied function and argument. A new function is returned that calls the first function with new argument signature. The closure is important - it allows fn access to arg. Now you can do this sort of thing:

add = lambda x, y : x + y;
add2 = curry_first(add, 2)
add2(4) # returns 6

I've usually heard this referred to as currying.

like image 33
Hamish Avatar answered Oct 17 '22 23:10

Hamish


Simply, the result of a partial application is normally implemented as a closure.

like image 3
Kai Avatar answered Oct 17 '22 23:10

Kai


Closures are not a required functionality in a language. I'm experimenting a homemade language, lambdatalk, in which lambdas don't create closures but accept partial application. For instance this is how the set [cons, car, cdr] could be defined in SCHEME:

(def cons (lambda (x y) (lambda (m) (m x y))))
(def car (lambda (z) (z (lambda (p q) p))))
(def cdr (lambda (z) (z (lambda (p q) q))))

(car (cons 12 34)) -> 12
(cdr (cons 12 34)) -> 34

and in lambdatalk:

{def cons {lambda {:x :y :m} {:m :x :y}}} 
{def car {lambda {:z} {:z {lambda {:x :y} :x}}}}
{def cdr {lambda {:z} {:z {lambda {:x :y} :y}}}}

{car {cons 12 34}} -> 12
{cdr {cons 12 34}} -> 34

In SCHEME the outer lambda saves x and y in a closure the inner lambda can access given m. In lambdatalk the lambda saves :x and :y and returns a new lambda waiting for :m. So, even if closure (and lexical scope) are usefull functionalities, there are not a necessity. Without any free variables, out of any lexical scope, functions are true black boxes without any side effect, in a total independance, following a true functional paradigm. Don't you think so?

like image 2
Alain Marty Avatar answered Oct 17 '22 22:10

Alain Marty