pufuwozu/roy — Issue #75: Optimize type with single nullary constructor to nullable value
Author: eegg
Created: 2011-12-13 01:00:20+00:00
State: open

The Roy code

```haskell
data Maybe a = Nothing | Just a

let maybe b f m = match m
case Nothing = b
case (Just x) = f x
```

generates (0.1.3) the JS

```javascript
var Nothing = function(){};
var Just = function(a){this._0 = a};
var maybe = function(b, f, m) {
return (function() {
if(m instanceof Nothing) {
return b;
} else if(m instanceof Just) {
var x = m._0;
return f(x);
}
})();
}
```

However, the natural representation of `Nothing` in JS would be the `null` value. This applies to all types with a single nullary data constructor. The above would ideally compile to something like

```javascript
var Just = function(a){this._0 = a};
var maybe = function(b, f, m) {
return (function() {
if(m === null) {
return b;
} else if(m instanceof Just) {
var x = m._0;
return f(x);
}
})();
}
```