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