pufuwozu/roy — Issue #91: Optimization: lift supercombinator lambdas
Author: eegg
Created: 2011-12-30 00:50:47+00:00
State: open

The Roy code

```haskell
let foo a =
if a then
(\x y -> x)
else
(\x y -> y)
```

contains two lambda expressions, `(\x y -> x)` and `(\x y -> y)`. These expressions have no free variables, and are therefore candidates for lifting. However, under 0.1.3, Roy produces the Javascript

```javascript
var foo = function(a) {
return (function() {
if(a) {
return function(x, y) {
return x;
};
} else {
return function(x, y) {
return y;
};
}
})();
}
```

which creates a new function every time it is called. Better would be to lift the functions to variables in a closure, then refer to them in the body of the function definition:

```javascript
var foo = (function() {
var lam1 = function(x, y) {
return x;
};
var lam2 = function(x, y) {
return y;
};
return function(a) {
return (function() {
if(a) {
return lam1;
} else {
return lam2;
}
})();
};
})();
```