X-Git-Url: http://lambda.jimpryor.net/git/gitweb.cgi?p=lambda.git;a=blobdiff_plain;f=code%2Flambda.js;h=955254bfc21743a880bf4338e2c6913a2d81a835;hp=83ae7c4aea7d65d6250e5be5449a69ac0da4184f;hb=c51047cd5851147777c81822d00b1dd6e35988d5;hpb=f80471bc9bb0900caa27e86d06b4b93f17dd6469 diff --git a/code/lambda.js b/code/lambda.js index 83ae7c4a..955254bf 100644 --- a/code/lambda.js +++ b/code/lambda.js @@ -487,10 +487,10 @@ function sub(tree, v, arg) { if (tree.car == null) return (tree); // perform alpha reduction to prevent variable collision - if (isBindingForm(tree)) - return (new cons (tree.car, + if (isBindingForm(tree)) + return (new cons (tree.car, sub(sub(tree.cdr, // inner sub = alpha reduc. - tree.cdr.car.cdr, + tree.cdr.car.cdr, fresh(tree.cdr.car.cdr)), v, arg))); @@ -504,7 +504,7 @@ var i = 0; function fresh(string) { i = i+1; if (typeof(string) != "string") return (string); - return (new cons (null, + return (new cons (null, string.substring(0,1) + (i).toString())); } @@ -533,11 +533,11 @@ function mapReduce(tree) { function convert(tree) { if (isLet(tree)) { return (sub(tree.cdr.car, tree.car.cdr.car.cdr, tree.car.cdr.cdr.car));} - else + else if (isConvertable(tree)) { return (sub(tree.car.cdr.cdr.car, tree.car.cdr.car.cdr, tree.cdr.car));} else return(tree); -} +} // Is of form ((let var arg) body)? function isLet(tree) { @@ -547,7 +547,7 @@ function isLet(tree) { if (tree.cdr == null) return (false); if (tree.cdr.car == null) return (false); return(true); -} +} // Is of form ((lambda var body) arg)? function isConvertable(tree) { @@ -557,14 +557,14 @@ function isConvertable(tree) { if (tree.cdr == null) return (false); if (tree.cdr.car == null) return (false); return(true); -} +} // Is of form (lambda var body)? function isBindingForm(tree) { if (tree == null) return (false); if (tree.car == null) return (false); if (tree.car.car != null) return (false); - if ((tree.car.cdr != "lambda") + if ((tree.car.cdr != "lambda") && (tree.car.cdr != "let") && (tree.car.cdr != "exists") && (tree.car.cdr != "forall") @@ -583,7 +583,7 @@ function isBindingForm(tree) { function treeToString(tree) { if (tree == null) return ("") if (tree.car == null) return (tree.cdr) - if ((tree.car).car == null) + if ((tree.car).car == null) return (treeToString(tree.car) + " " + treeToString(tree.cdr)) return ("(" + treeToString(tree.car) + ")" + treeToString(tree.cdr)) } @@ -592,7 +592,7 @@ function treeToString(tree) { function treeToStringRaw(tree) { if (tree == null) return ("@") if (typeof(tree) == "string") return (tree); - return ("(" + treeToStringRaw(tree.car) + "." + + return ("(" + treeToStringRaw(tree.car) + "." + treeToStringRaw(tree.cdr) + ")") } @@ -624,7 +624,7 @@ function formatTree(tree) { return (output) } -function mytry(form) { +function mytry(form) { i = 0; form.result.value = formatTree((stringToTree(form.input.value))); // form.result.value = formatTree(fixedPoint(stringToTree(form.input.value)));