1 /*jslint bitwise: true,
23 // substitution and translation algorithms from Chris Hankin, An Introduction to Lambda Calculi for Comptuer Scientists
25 function Db_free(variable) {
26 this.variable = variable;
27 this.subst = function (m, new_term) {
30 this.renumber = function (m, i) {
33 // we assume that other will have variable iff it's a Db_free
34 this.equal = function (other) {
35 return other.variable && this.variable.equal(other.variable);
37 this.contains = this.equal;
40 function Db_index(i) {
42 this.subst = function (m, new_term) {
45 } else if (this.index > m) {
46 return new Db_index(this.index - 1);
48 return new_term.renumber(this.index, 1);
51 this.renumber = function (m, i) {
55 return new Db_index(this.index + m - 1);
58 // we assume that other will have index iff it's a Db_index
59 this.equal = function (other) {
60 return this.index === other.index;
62 this.contains = this.equal;
65 function Db_app(left, right) {
68 this.subst = function (m, new_term) {
69 return new Db_app(this.left.subst(m, new_term), this.right.subst(m, new_term));
71 this.renumber = function (m, i) {
72 return new Db_app(this.left.renumber(m, i), this.right.renumber(m, i));
74 // we assume that other will have left iff it's a Db_app
75 this.equal = function (other) {
76 return other.left && this.left.equal(other.left) && this.right.equal(other.right);
78 this.contains = function (other) {
79 if (other.left && this.left.equal(other.left) && this.right.equal(other.right)) {
82 return this.left.contains(other) || this.right.contains(other);
87 function Db_lam(body) {
89 this.subst = function (m, new_term) {
90 return new Db_lam(this.body.subst(m + 1, new_term));
92 this.renumber = function (m, i) {
93 return new Db_lam(this.body.renumber(m, i + 1));
95 // we assume that other will have body iff it's a Db_lam
96 this.equal = function (other) {
97 return other.body && this.body.equal(other.body);
99 this.contains = function (other) {
100 if (other.body && this.body.equal(other.body)) {
103 return this.body.contains(other);
110 // substitution and normal-order evaluator based on Haskell version by Oleg Kisleyov
111 // http://okmij.org/ftp/Computation/lambda-calc.html#lambda-calculator-haskell
113 function Variable(name, tag) {
116 this.to_string = function () {
117 // append count copies of str to accum
118 // function markup(accum, count) {
119 // if (count === 0) {
122 // return markup(accum + "'", count - 1);
125 // return markup(this.name, this.tag);
127 for (var count = 0; count < this.tag; count += 1) {
132 this.equal = function (other) {
133 return (this.tag === other.tag) && (this.name === other.name);
135 // position of this in seq
136 this.position = function (seq) {
137 for (var i = 0; i < seq.length; i += 1) {
138 if (this.equal(seq[i])) {
139 return new Db_index(i + 1);
142 return new Db_free(this);
146 // if v occurs free_in term, returns Some v' where v' is the highest-tagged
147 // variable with the same name as v occurring (free or bound) in term
149 function free_in(v, term) {
150 var res = term.has_free(v);
151 return res[0] && res[1];
154 function subst(v, new_term, expr) {
155 if (new_term.variable && new_term.variable.equal(v)) {
158 return expr.subst(v, new_term);
162 function equal(expr1, expr2) {
163 return expr1.debruijn([]).equal(expr2.debruijn([]));
166 function contains(expr1, expr2) {
167 return expr1.debruijn([]).contains(expr2.debruijn([]));
171 function Lambda_var(variable) {
172 this.variable = variable;
173 this.debruijn = function (seq) {
174 return this.variable.position(seq);
176 this.to_string = function (as_atom) {
177 return this.variable.to_string();
179 this.has_free = function (v) {
180 if (v.name !== this.variable.name) {
182 } else if (v.tag === this.variable.tag) {
185 return [false, this.variable];
188 this.subst = function (v, new_term) {
189 if (this.variable.equal(v)) {
195 this.check_eta = function () {
198 this.eval_loop = function (stack, eta) {
199 function unwind(left, stack) {
200 // if (stack.length === 0) {
204 // var xs = stack.slice(1);
205 // return unwind(new Lambda_app(left, x.eval_loop([], eta)), xs);
208 while (stack.length) {
210 // res = new Lambda_app(res, x.eval_loop([], eta));
211 res = new Lambda_app(res, reduce(x, eta, false));
215 // return unwind(this, stack);
216 // trampoline to, args
217 return [null, unwind(this, stack)];
219 this.eval_cbv = function (aggressive) {
224 function Lambda_app(left, right) {
227 this.debruijn = function (seq) {
228 return new Db_app(this.left.debruijn(seq), this.right.debruijn(seq));
230 this.to_string = function (as_atom) {
232 if (this.left.left) {
233 base = this.left.to_string() + " " + this.right.to_string(true);
235 base = this.left.to_string(true) + " " + this.right.to_string(true);
238 return "(" + base + ")";
243 this.has_free = function (v) {
244 var left_res = this.left.has_free(v);
245 var right_res = this.right.has_free(v);
246 var left_bool = left_res[0];
247 var right_bool = right_res[0];
248 var left_tag = left_res[1].tag;
249 var right_tag = right_res[1].tag;
251 if (left_tag > right_tag) {
256 return [left_bool || right_bool, res];
258 this.subst = function (v, new_term) {
259 return new Lambda_app(subst(v, new_term, this.left), subst(v, new_term, this.right));
261 this.check_eta = function () {
264 this.eval_loop = function (stack, eta) {
265 var new_stack = stack.slice(0);
266 new_stack.unshift(this.right);
267 // return this.left.eval_loop(new_stack, eta);
268 // trampoline to, args
269 return [this.left, new_stack, eta];
271 this.eval_cbv = function (aggressive) {
272 var left = this.left.eval_cbv(aggressive);
273 var right = this.right.eval_cbv(aggressive);
275 return subst(left.bound, right, left.body).eval_cbv(aggressive);
277 return new Lambda_app(left, right);
283 // (* if v occurs free_in term, returns Some v' where v' is the highest-tagged
284 // * variable with the same name as v occurring (free or bound) in term *)
287 function Lambda_lam(variable, body) {
288 this.bound = variable;
290 this.debruijn = function (seq) {
291 var new_seq = seq.slice(0);
292 new_seq.unshift(this.bound);
293 return new Db_lam(this.body.debruijn(new_seq));
295 this.to_string = function (as_atom) {
296 var base = "\\" + this.to_string_funct();
298 return "(" + base + ")";
303 this.to_string_funct = function () {
304 if (this.body.to_string_funct) {
305 return this.bound.to_string() + " " + this.body.to_string_funct();
307 return this.bound.to_string() + ". " + this.body.to_string();
310 this.has_free = function (v) {
311 if (this.bound.equal(v)) {
314 return this.body.has_free(v);
317 this.subst = function (v, new_term) {
318 function bump_tag(v1, v2) {
320 if (v1.tag > v2.tag) {
325 return new Variable(v1.name, max + 1);
327 function bump_tag2(v1, v2) {
328 if (v1.name !== v2.name) {
331 return bump_tag(v1, v2);
334 if (this.bound.equal(v)) {
337 var res = free_in(this.bound, new_term);
338 // if x is free in the inserted term new_term, a capture is possible
340 // this.bound is free in new_term, need to alpha-convert
341 var uniq_x = bump_tag2(bump_tag(this.bound, res), v);
342 var res2 = free_in(uniq_x, this.body);
344 uniq_x = bump_tag(uniq_x, res2);
346 var body2 = subst(this.bound, new Lambda_var(uniq_x), this.body);
347 return new Lambda_lam(uniq_x, subst(v, new_term, body2));
349 // this.bound not free in new_term, can substitute new_term for v without any captures
350 return new Lambda_lam(this.bound, subst(v, new_term, this.body));
354 this.check_eta = function () {
355 if (this.body.right && this.body.right.variable && this.bound.equal(this.body.right.variable) && !free_in(this.bound, this.body.left)) {
356 return this.body.left;
361 this.eval_loop = function (stack, eta) {
362 if (stack.length === 0) {
363 // var term = new Lambda_lam(this.bound, this.body.eval_loop([], eta));
364 var term = new Lambda_lam(this.bound, reduce(this.body, eta, false));
366 return [null, term.check_eta()];
372 var xs = stack.slice(1);
373 // return subst(this.bound, x, this.body).eval_loop(xs, eta);
374 // trampoline to, args
375 return [subst(this.bound, x, this.body), xs, eta];
378 this.eval_cbv = function (aggressive) {
380 return new Lambda_lam(this.bound, this.body.eval_cbv(aggressive));
385 this.to_int = function (sofar) {
386 // if (this.body.body && this.body.body.variable && this.body.bound.equal(this.body.body.variable)) {
388 // } else if (this.body.variable && this.bound.equal(this.body.variable)) {
390 // } else if (this.body.body && this.body.body.left && this.body.body.left.variable && this.bound.equal(this.body.body.left.variable)) {
391 // var new_int = new Lambda_lam(this.bound, new Lambda_lam(this.body.bound, this.body.body.right));
392 // return new_int.to_int(1 + sofar);
394 // return "not a church numeral";
396 var res = 0, s = this.bound, z, cursor;
397 if (this.body.variable && s.equal(this.body.variable)) {
399 } else if (this.body.bound) {
401 cursor = this.body.body;
402 while (cursor.left && cursor.left.variable && s.equal(cursor.left.variable)) {
404 cursor = cursor.right;
406 if (cursor.variable && z.equal(cursor.variable)) {
410 return "not a church numeral";
416 ///////////////////////////////////////////////////////////////////////////////////
418 // cbv is false: use call-by-name
419 // cbv is 1: use call-by-value, don't descend inside lambda
420 // cbv is 2: applicative order
421 function reduce(expr, eta, cbv) {
423 return expr.eval_cbv(cbv > 1);
425 // return expr.eval_loop([], eta);
426 var to_eval = expr, res = [[], eta];
427 while (to_eval !== null) {
428 res = to_eval.eval_loop.apply(to_eval, res);
429 to_eval = res.shift();
435 function make_var(name) {
436 return new Variable(name);
438 function make_app(aa, bb) {
439 return new Lambda_app(aa, bb);
441 function make_lam(a, aa) {
442 return new Lambda_lam(a, aa);
446 if (console && console.debug) {
448 console.debug.apply(this, arguments);
458 // // Basic data structure, essentially a LISP/Scheme-like cons
459 // // pre-terminal nodes are expected to be of the form new cons(null, "string")
460 // function cons(car, cdr) {
465 // // takes a stack of symbols, returns a pair: a tree and the remaining symbols
466 // function parse(split) {
467 // if (split == null) return (new cons (null, null));
468 // if (split.length == 0) return (new cons (null, null));
469 // var token = split.shift();
470 // if (token == ")") return (new cons (null, split));
471 // var next = parse(split);
472 // if (token == "(") {
473 // var nextnext = parse(next.cdr);
474 // return (new cons ((new cons (next.car, nextnext.car)),
477 // return (new cons ((new cons ((new cons (null, token)),
482 // // substitute arg in for v in tree
483 // function sub(tree, v, arg) {
484 // if (tree == null) return (null);
485 // if (tree.car == null) if (tree.cdr == v) return (arg);
486 // if (tree.car == null) return (tree);
488 // // perform alpha reduction to prevent variable collision
489 // if (isBindingForm(tree))
490 // return (new cons (tree.car,
491 // sub(sub(tree.cdr, // inner sub = alpha reduc.
493 // fresh(tree.cdr.car.cdr)),
497 // return (new cons ((sub (tree.car, v, arg)),
498 // (sub (tree.cdr, v, arg))))
501 // // Guaranteed unique for each call as long as string is not empty.
503 // function fresh(string) {
505 // if (typeof(string) != "string") return (string);
506 // return (new cons (null,
507 // string.substring(0,1) + (i).toString()));
510 // // Keep reducing until there is no more change
511 // function fixedPoint (tree) {
512 // var t2 = reduce(tree);
513 // if (treeToString(tree) == treeToString(t2)) return (tree);
514 // return (fixedPoint (t2));
517 // // Reduce all the arguments, then try to do beta conversion on the whole
518 // function reduce(tree) {
519 // if (tree == null) return (tree);
520 // if (typeof(tree) == "string") return (tree);
521 // return (convert (new cons (reduce (tree.car), mapReduce (tree.cdr))));
524 // // Reduce all the arguments in a list
525 // function mapReduce(tree) {
526 // if (tree == null) return (tree);
527 // if (tree.car == null) return (tree);
528 // return (new cons (reduce (tree.car), mapReduce(tree.cdr )));
531 // // If the list is of the form ((lambda var body) arg), do beta reduc.
532 // function convert(tree) {
533 // if (isLet(tree)) {
534 // return (sub(tree.cdr.car, tree.car.cdr.car.cdr, tree.car.cdr.cdr.car));}
536 // if (isConvertable(tree)) {
537 // return (sub(tree.car.cdr.cdr.car, tree.car.cdr.car.cdr, tree.cdr.car));}
538 // else return(tree);
541 // // Is of form ((let var arg) body)?
542 // function isLet(tree) {
543 // if (tree == null) return (false);
544 // if (!(isBindingForm(tree.car))) return (false);
545 // if (tree.car.car.cdr != "let") return (false);
546 // if (tree.cdr == null) return (false);
547 // if (tree.cdr.car == null) return (false);
551 // // Is of form ((lambda var body) arg)?
552 // function isConvertable(tree) {
553 // if (tree == null) return (false);
554 // if (!(isBindingForm(tree.car))) return (false);
555 // if (tree.car.car.cdr != "lambda") return (false);
556 // if (tree.cdr == null) return (false);
557 // if (tree.cdr.car == null) return (false);
561 // // Is of form (lambda var body)?
562 // function isBindingForm(tree) {
563 // if (tree == null) return (false);
564 // if (tree.car == null) return (false);
565 // if (tree.car.car != null) return (false);
566 // if ((tree.car.cdr != "lambda")
567 // && (tree.car.cdr != "let")
568 // && (tree.car.cdr != "exists")
569 // && (tree.car.cdr != "forall")
570 // && (tree.car.cdr != "\u03BB")
571 // && (tree.car.cdr != "\u2200")
572 // && (tree.car.cdr != "\u2203")
575 // if (tree.car.cdr == null) return (false);
576 // if (tree.cdr.car == null) return (false);
577 // if (tree.cdr.car.car != null) return (false);
578 // if (tree.cdr.cdr == null) return (false);
582 // function treeToString(tree) {
583 // if (tree == null) return ("")
584 // if (tree.car == null) return (tree.cdr)
585 // if ((tree.car).car == null)
586 // return (treeToString(tree.car) + " " + treeToString(tree.cdr))
587 // return ("(" + treeToString(tree.car) + ")" + treeToString(tree.cdr))
590 // // use this instead of treeToString if you want to see the full structure
591 // function treeToStringRaw(tree) {
592 // if (tree == null) return ("@")
593 // if (typeof(tree) == "string") return (tree);
594 // return ("(" + treeToStringRaw(tree.car) + "." +
595 // treeToStringRaw(tree.cdr) + ")")
598 // // Make sure each paren will count as a separate token
599 // function stringToTree(input) {
600 // input = input.replace(/let/g, " ( ( let ");
601 // input = input.replace(/=/g, " ");
602 // input = input.replace(/in/g, " ) ");
603 // input = input.replace(/\(/g, " ( ");
604 // input = input.replace(/\)/g, " ) ");
605 // input = input.replace(/;.*\n/g," ");
606 // input = input.replace(/\^/g, " ^ ");
607 // input = input.replace(/[\\]/g, " lambda ");
608 // input = input.replace(/\u03BB/g, " lambda ");
609 // return ((parse(input.split(/[ \f\n\r\t\v]+/))).car)
612 // // Adjust spaces to print pretty
613 // function formatTree(tree) {
614 // output = treeToStringRaw (tree);
615 // output = output.replace(/^[ \f\n\r\t\v]+/, "");
616 // output = output.replace(/[ \f\n\r\t\v]+$/, "");
617 // output = output.replace(/[ \f\n\r\t\v]+\)/g, ")");
618 // output = output.replace(/\)([^)(])/g, ") $1");
619 // output = output.replace(/lambda/g, "\\");
620 // // output = output.replace(/lambda/g, "\u03BB");
621 // // output = output.replace(/exists/g, "\u2203");
622 // // output = output.replace(/forall/g, "\u2200");
626 // function mytry(form) {
628 // form.result.value = formatTree((stringToTree(form.input.value)));
629 // // form.result.value = formatTree(fixedPoint(stringToTree(form.input.value)));