If we have a file named Some.purs
which contains the following code:
module Some where
class Foo a where
bar :: a -> a
data Baz = Baz
instance fooBaz :: Foo Baz where
bar _ = Baz
bat = bar Baz
Running this file through purs compile
produces the following JavaScript:
// Generated by purs version 0.13.6
"use strict";
var Baz = (function () {
function Baz() {
};
Baz.value = new Baz();
return Baz;
})();
var Foo = function (bar) {
this.bar = bar;
};
var fooBaz = new Foo(function (v) {
return Baz.value;
});
var bar = function (dict) {
return dict.bar;
};
var bat = bar(fooBaz)(Baz.value);
module.exports = {
bar: bar,
Foo: Foo,
Baz: Baz,
bat: bat,
fooBaz: fooBaz
};
Why is var bat = bar(fooBaz)(Baz.value)
used instead of var bat = fooBaz.bar(Baz.value)
? The only explanation that I can come with is that instances of typeclasses are internally represented as parameters to functions.
Note: couldn’t transforming instances of typeclasses this way be a good optimization?