Created
August 10, 2017 00:07
-
-
Save marcmartino/fc52727c57f0592f51e1058291d60393 to your computer and use it in GitHub Desktop.
Lonsdorf interpreters examples
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| var Const = function(x) { return new _Const(x); }; | |
| var _Const = function(val) { this.val = val; }; | |
| _Const.prototype.inspect = function(){ return 'Const('+inspect(this.val)+')'; } | |
| var Add = function(x, y) { return new _Add(x, y); }; | |
| var _Add = function(x, y) { | |
| this.x = x; | |
| this.y = y; | |
| }; | |
| _Add.prototype.inspect = function(){ return 'Add('+inspect(this.x)+', '+inspect(this.y)+')'; } | |
| var Mul = function(x, y) { return new _Mul(x, y); }; | |
| var _Mul = function(x, y) { | |
| this.x = x; | |
| this.y = y; | |
| }; | |
| _Mul.prototype.inspect = function(){ return 'Mul('+inspect(this.x)+', '+inspect(this.y)+')'; } | |
| _Const.prototype.map = function(f) { return this } // imporatnat!!! Doesn't do anything | |
| _Add.prototype.map = function(f) { return Add(f(this.x), f(this.y)) } | |
| _Mul.prototype.map = function(f) { return Mul(f(this.x), f(this.y)) } | |
| var interpret = function(a) { | |
| switch(a.constructor) { | |
| case _Mul: return a.x * a.y; | |
| case _Add: return a.x + a.y; | |
| case _Const: return a.val; | |
| } | |
| } | |
| var program = Mul(Add(Const(2), Const(3)), Const(4)) | |
| var res = cata(interpret, program); | |
| console.log("Program", program) | |
| console.log("Program result", res) | |
| //======== Progam 2 ======= | |
| var _Concat = function(v, next) { this.val = v; this.next = next; }; | |
| var Concat = function(v, x){ return new _Concat(v, x); } | |
| _Concat.prototype.inspect = function(){ return 'Concat('+inspect(this.val)+', '+inspect(this.next)+')'; } | |
| var _Replace = function(v, x, next) { this.val = v; this.x = x; this.next = next; }; | |
| var Replace = function(v, x, next){ return new _Replace(v, x, next); } | |
| _Replace.prototype.inspect = function(){ return 'Replace('+inspect(this.val)+', '+inspect(this.x)+', '+inspect(this.next)+')'; } | |
| var _Input = function(v) { this.val = v; }; | |
| var Input = function(v){ return new _Input(v); } | |
| _Input.prototype.inspect = function(){ return 'Input('+inspect(this.val)+')'; } | |
| _Concat.prototype.map = function(f) { return Concat(this.val, f(this.next)); }; | |
| _Replace.prototype.map = function(f) { return Replace(this.val, this.x, f(this.next)); }; | |
| _Input.prototype.map = function(f) { return Input(this.val); }; | |
| var printy = function(t) { | |
| switch (t.constructor) { | |
| case _Concat: | |
| return "concatting "+t.val +" after "+ t.next; | |
| case _Replace: | |
| return "replacing "+t.val+" with "+ t.x + " on " +t.next; | |
| case _Input: | |
| return t.val; | |
| } | |
| } | |
| var inter = function(t) { | |
| switch (t.constructor) { | |
| case _Concat: | |
| return t.next + t.val; | |
| case _Replace: | |
| return t.next.replace(t.val, t.x); | |
| case _Input: | |
| return t.val; | |
| } | |
| } | |
| var prog = Concat("world", Replace("h", "m", Input("hello"))); | |
| console.log("Program 2", prog); | |
| console.log("Program 2 result", cata(inter, prog)); | |
| console.log("Program 2 print result", cata(printy, prog)); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment