index: move eval order
[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 ; booleans
5 let true = \x y. x in
6 let false = \x y. y in
7 let and = \l r. l (r true false) false in
8 let make\_pair = \f s g. g f s in
9 let get\_fst = true in
10 let get\_snd = false in
11 let empty = make\_pair true junk in
12 let isempty = \x. x get\_fst in
13 let make\_list = \h t. make\_pair false (make\_pair h t) in
14 let head = \l. isempty l err (l get\_snd get\_fst) in
15 let tail = \l. isempty l err (l get\_snd get\_snd) in
16 ;
17 ; a list of numbers to experiment on
18 let mylist = make\_list 1 (make\_list 2 (make\_list 3 empty)) in
19 ;
20 ; church numerals
21 let iszero = \n. n (\x. false) true in
22 let succ = \n s z. s (n s z) in
23 let mul = \m n s. m (n s) in
24 let pred = (\shift n. n shift (make\_pair 0 0) get\_snd) (\p. p (\x y. make\_pair (succ x) x))  in
25 let leq = \m n. iszero(n pred m) in
26 let eq = \m n. and (leq m n)(leq n m) in
27 ;
28 ; a fixed-point combinator for defining recursive functions
29 let Y = \f. (\h. f (h h)) (\h. f (h h)) in
30 ;
31 let length = Y (\length l. isempty l 0 (succ (length (tail l)))) in
32 ;
33 ; synonyms
34 let makePair = make\_pair in
35 let fst = get\_fst in
36 let snd = get\_snd in
37 let nil = empty in
38 let isNil = isempty in
39 let makeList = make\_list in
40 let isZero = iszero in
41 let mult = mul in
42 ;
43 length (tail mylist)
44 </textarea>
45
46 <input id="PARSE" value="Normalize" type="button">
47 <input id="ETA" type="checkbox">do eta-reductions too
48 <noscript><p>You may not see it because you have JavaScript turned off. Uffff!</p></noscript>
49 <script src="/code/lambda.js"></script>
50 <script src="/code/tokens.js"></script>
51 <script src="/code/parse.js"></script>
52 <script src="/code/json2.js"></script>
53 <pre id="OUTPUT">
54 </pre>
55 <script>
56 /*jslint evil: true */
57
58 /*members create, error, message, name, prototype, stringify, toSource,
59     toString, write
60 */
61
62 /*global JSON, make_parse, parse, source, tree */
63
64 // Make a new object that inherits members from an existing object.
65
66 if (typeof Object.create !== 'function') {
67     Object.create = function (o) {
68         function F() {}
69         F.prototype = o;
70         return new F();
71     };
72 }
73
74 // Transform a token object into an exception object and throw it.
75
76 Object.prototype.error = function (message, t) {
77     t = t || this;
78     t.name = "SyntaxError";
79     t.message = message;
80     throw t;
81 };
82
83
84 (function () {
85     var parse = make_parse();
86
87     function go(source) {
88         var string, tree, expr, eta;
89         try {
90             tree = parse(source);
91  //           string = JSON.stringify(tree, ['key', 'name', 'message', 'value', 'arity', 'first', 'second', 'third', 'fourth'], 4);
92                         expr = tree.handler();
93             // string = JSON.stringify(expr, ['key', 'name', 'message', 'value', 'arity', 'first', 'second', 'tag', 'variable', 'left', 'right', 'bound', 'body' ], 4);
94 //                      string = expr.to_string() + "\n\n~~>\n\n";
95                         string = '';
96                         eta = document.getElementById('ETA').checked;
97                         string = string + reduce(expr, eta, false).to_string();
98         } catch (e) {
99             string = JSON.stringify(e, ['name', 'message', 'from', 'to', 'key',
100                     'value', 'arity', 'first', 'second', 'third', 'fourth'], 4);
101         }
102         document.getElementById('OUTPUT').innerHTML = string
103             .replace(/&/g, '&amp;')
104             .replace(/[<]/g, '&lt;');
105     }
106
107     document.getElementById('PARSE').onclick = function (e) {
108         go(document.getElementById('INPUT').value);
109     };
110 }());
111
112 </script>