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