(no commit message)
[lambda.git] / assignment_3_evaluator.mdwn
1 Here are the definitions pre-loaded for working on assignment 3:
2
3 <textarea id="INPUT" style="border: 2px solid black; color: black; font-family: monospace; height: 3in; overflow: auto; padding: 0.5em; width: 100%;">
4 let true = \x y. x in
5 let false = \x y. y in
6 let and = \l r. l (r true false) false in
7 let makePair = \f s g. g f s in
8 let fst = true in
9 let snd = false in
10 let nil = makePair true meh in
11 let isNil = \x. x fst in
12 let makeList = \h t. makePair false (makePair h t) in
13 let head = \l. isNil l err (l snd fst) in
14 let tail = \l. isNil l err (l snd snd) in
15 let mylist = makeList 1 (makeList 2 (makeList 3 nil)) in
16 let Y = \f. (\h. f (h h)) (\h. f (h h)) in
17 let isZero = \n. n (\x. false) true in
18 let succ = \n s z. s (n s z) in
19 let mult = \m n s. m (n s) in
20 let length = Y (\length l. isNil l 0 (succ (length (tail l)))) in
21 let pred = \n. isZero n 0 (length (tail (n (\p. makeList meh p) nil))) in
22 let leq = \m n. isZero(n pred m) in
23 let eq = \m n. and (leq m n)(leq n m) in
24 ;
25 length (tail mylist)
26 </textarea>
27 <input id="PARSE" value="Normalize" type="button">
28 <input id="ETA" type="checkbox">do eta-reductions too
29 <noscript><p>You may not see it because you have JavaScript turned off. Uffff!</p></noscript>
30 <script src="/code/lambda.js"></script>
31 <script src="/code/tokens.js"></script>
32 <script src="/code/parse.js"></script>
33 <script src="/code/json2.js"></script>
34 <pre id="OUTPUT">
35 </pre>
36 <script>
37 /*jslint evil: true */
38
39 /*members create, error, message, name, prototype, stringify, toSource,
40     toString, write
41 */
42
43 /*global JSON, make_parse, parse, source, tree */
44
45 // Make a new object that inherits members from an existing object.
46
47 if (typeof Object.create !== 'function') {
48     Object.create = function (o) {
49         function F() {}
50         F.prototype = o;
51         return new F();
52     };
53 }
54
55 // Transform a token object into an exception object and throw it.
56
57 Object.prototype.error = function (message, t) {
58     t = t || this;
59     t.name = "SyntaxError";
60     t.message = message;
61     throw t;
62 };
63
64
65 (function () {
66     var parse = make_parse();
67
68     function go(source) {
69         var string, tree, expr, eta;
70         try {
71             tree = parse(source);
72  //           string = JSON.stringify(tree, ['key', 'name', 'message', 'value', 'arity', 'first', 'second', 'third', 'fourth'], 4);
73                         expr = tree.handler();
74             // string = JSON.stringify(expr, ['key', 'name', 'message', 'value', 'arity', 'first', 'second', 'tag', 'variable', 'left', 'right', 'bound', 'body' ], 4);
75 //                      string = expr.to_string() + "\n\n~~>\n\n";
76                         string = '';
77                         eta = document.getElementById('ETA').checked;
78                         string = string + reduce(expr, eta, false).to_string();
79         } catch (e) {
80             string = JSON.stringify(e, ['name', 'message', 'from', 'to', 'key',
81                     'value', 'arity', 'first', 'second', 'third', 'fourth'], 4);
82         }
83         document.getElementById('OUTPUT').innerHTML = string
84             .replace(/&/g, '&amp;')
85             .replace(/[<]/g, '&lt;');
86     }
87
88     document.getElementById('PARSE').onclick = function (e) {
89         go(document.getElementById('INPUT').value);
90     };
91 }());
92
93 </script>