purescript/purescript — pull request #510 by garyb (2014-07-26 12:03:53+00:00)
Title: Newtypes

This isn't ready for merging just yet, it seems there may be a problem with the optimiser that this causes or uncovers relating to TCO.

The `NewtypeTCO` test is identical to the existing `Rank2Data` test, but uses `newtype` rather than `data` definitions, and when run, the generated code contains a reference to an undeclared variable:

```
[stdin]:125
zero = __tco_zero;
^
ReferenceError: __tco_zero is not defined
at [stdin]:125:28
at [stdin]:149:37
at PS.Main.runNat ([stdin]:134:22)
at [stdin]:155:22
at [stdin]:170:3
at Object. ([stdin]-wrapper:6:22)
at Module._compile (module.js:456:26)
at evalScript (node.js:536:25)
at Socket. (node.js:154:11)
at Socket.EventEmitter.emit (events.js:117:20)
```

Which we can see here:

``` js
var succ = function (__copy_n) {
return function (__copy_zero) {
return function (__copy_succ) {
var n = __copy_n;
var zero = __copy_zero;
var succ = __copy_succ;
tco: while (true) {
var __tco_n = n(zero)(succ);
n = __tco_n;
zero = __tco_zero;
succ = __tco_succ;
continue tco;
};
};
};
};
```

So I'm not quire sure where the `var` declaration for `__tco_zero` has gone, perhaps it's an interaction between different optimisation passes.