Google
Last weekend I hacked partial application into my parse transform for erlang, unraverl. The rules are very simple but the implications are interesting if nothing else. In other functional languages, haskell as an example, there's nothing inherently wrong with defining a function that requires two arguments but giving it one.
add x y = x + y
example = add 1
{- => \y -> 1 + y -}
The result is a lambda where the supplied argument is applied across the code in the function.

Unraverl's implementation?

Using the initial example in Erlang
add(x, y) -> x + y.

example() -> add(1).
the parse transform sees that there are no local (only) functions named 'add' with an arity of 1. It then finds the next highest arity definition and changes the call to accommodate
%% roughly
example() -> fun(y) -> add(1, y) end.
So while haskell applies whatever arguments are available to the actual function (internally I'm not sure how its implemented), unraverl simply wraps a call to the actual function in a new fun requiring the remaining arguments. To make use of this feature in your own applications simply add the compiler directive
-compile([{parse_transform, unraverl}]). 
and make sure the library is in the code path. As yet, it doesn't have a makefile or unit tests so use at your own peril.

Published

14 Aug 2009

Tags