Uncurrying
See original GitHub issueOne thing that functional languages like PureScript do is generate curried functions. This appears to be a blocker for Closure. For example,
(function(){
var Show_string = { show: function(s){ return s; } };
function print(d,v){
console.log(d.show(v));
}
function greet(d,v){
return print(d,v + "!");
}
return greet(Show_string, "Mary");
})();
Closure outputs,
console.log("Mary!");
Excellent result. It did pretty straight-forward transformations here. Including removing the overhead of generic functions, which are achieved via dictionary (argument) passing in Haskell-like languages. However, in languages like Haskell/PureScript, functions are all single-argument chains of functions, like this:
(function(){
var Show_string = { show: function(s){ return s; } };
function print(d) {
return function(v){
console.log(d.show(v));
}
}
function greet(d){
return function(v){
return print(d)(v + "!");
}
}
return greet(Show_string)("Mary");
})();
Unfortunately, Closure outputs the following:
(function() {
function c(a) {
return function(b) {
console.log(a.show(b));
};
}
return function(a) {
return function(b) {
return c(a)(b + "!");
};
}({show:function(a) {
return a;
}})("Mary");
})();
This means that PureScript’s output is both large and slow – this example is small, but imagine large codebases with nested levels of this.
So it seems that “uncurrying” is not a process that Closure currently does. I’m not sure whether this was decided against, simply because it’s not normal in JS to have functions like this, or whether it’s an oversight, or whether there’s a good reason that makes reducing these in the general case difficult.
Could someone fill me in?
If it’s simply not something considered, how difficult would it be to add? Is it something I could implement myself in Closure? I’m discussing with the PureScript community and considering whether to add it to the compiler output, but on the other hand, this kind of thing feels like Closure’s bread and butter, so I’m attempting to do it the Right Way before going off and patching something further up the chain.
Issue Analytics
- State:
- Created 3 years ago
- Reactions:15
- Comments:13 (2 by maintainers)
Top GitHub Comments
Tried setting
assumeClosuresOnlyCaptureReferences = true
and it worked:On commit 2809700cd70d76b6212cd45540151b90c8d003ba, diff:
Command:
Input:
Output:
Thanks a bunch @krk! I’ve eyeballed the Dockerfile and can’t see where my attempt diverged from yours, but no matter, I’m sure it was something subtle and trivial.
Happy new year! 🎊
EDIT: I was able to reproduce the result. ✔️