27cdd9bbcfd839a779723b9ae17421c47e888f77
[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 let t1 = (make_list 1 empty) in
26 let t2 = (make_list 2 empty) in
27 let t3 = (make_list 3 empty) in
28 let t12 = (make_list t1 (make_list t2 empty)) in
29 let t23 = (make_list t2 (make_list t3 empty)) in
30 let ta = (make_list t1 t23) in
31 let tb = (make_list t12 t3) in
32 let tc = (make_list t1 (make_list t23 empty)) in
33 ;
34 ;sum-leaves t1 ~~> 1
35 ;sum-leaves t2 ~~> 2
36 ;sum-leaves t3 ~~> 3
37 ;sum-leaves t12 ~~> 3
38 ;sum-leaves t23 ~~> 5
39 ;sum-leaves ta ~~> 6
40 ;sum-leaves tb ~~> 6
41 ;
42 length (tail mylist)
43 </textarea>
44 <input id="PARSE" value="Normalize" type="button">
45 <input id="ETA" type="checkbox">do eta-reductions too
46 <noscript><p>You may not see it because you have JavaScript turned off. Uffff!</p></noscript>
47 <script src="/code/lambda.js"></script>
48 <script src="/code/tokens.js"></script>
49 <script src="/code/parse.js"></script>
50 <script src="/code/json2.js"></script>
51 <pre id="OUTPUT">
52 </pre>
53 <script>
54 /*jslint evil: true */
55
56 /*members create, error, message, name, prototype, stringify, toSource,
57     toString, write
58 */
59
60 /*global JSON, make_parse, parse, source, tree */
61
62 // Make a new object that inherits members from an existing object.
63
64 if (typeof Object.create !== 'function') {
65     Object.create = function (o) {
66         function F() {}
67         F.prototype = o;
68         return new F();
69     };
70 }
71
72 // Transform a token object into an exception object and throw it.
73
74 Object.prototype.error = function (message, t) {
75     t = t || this;
76     t.name = "SyntaxError";
77     t.message = message;
78     throw t;
79 };
80
81
82 (function () {
83     var parse = make_parse();
84
85     function go(source) {
86         var string, tree, expr, eta;
87         try {
88             tree = parse(source);
89  //           string = JSON.stringify(tree, ['key', 'name', 'message', 'value', 'arity', 'first', 'second', 'third', 'fourth'], 4);
90                         expr = tree.handler();
91             // string = JSON.stringify(expr, ['key', 'name', 'message', 'value', 'arity', 'first', 'second', 'tag', 'variable', 'left', 'right', 'bound', 'body' ], 4);
92 //                      string = expr.to_string() + "\n\n~~>\n\n";
93                         string = '';
94                         eta = document.getElementById('ETA').checked;
95                         string = string + reduce(expr, eta, false).to_string();
96         } catch (e) {
97             string = JSON.stringify(e, ['name', 'message', 'from', 'to', 'key',
98                     'value', 'arity', 'first', 'second', 'third', 'fourth'], 4);
99         }
100         document.getElementById('OUTPUT').innerHTML = string
101             .replace(/&/g, '&amp;')
102             .replace(/[<]/g, '&lt;');
103     }
104
105     document.getElementById('PARSE').onclick = function (e) {
106         go(document.getElementById('INPUT').value);
107     };
108 }());
109
110 </script>