1 Here are the definitions pre-loaded for working on assignment 3:
3 <textarea id="INPUT" style="border: 2px solid black; color: black; font-family: monospace; height: 3in; overflow: auto; padding: 0.5em; width: 100%;">
7 let and = \l r. l (r true false) false in
8 let or = \l r. l true r in
10 let make\_pair = \f s g. g f s in
11 let get\_fst = true in
12 let get\_snd = false in
13 let empty = make\_pair true junk in
14 let isempty = \x. x get\_fst in
15 let make\_list = \h t. make\_pair false (make\_pair h t) in
16 let head = \l. isempty l err (l get\_snd get\_fst) in
17 let tail = \l. isempty l err (l get\_snd get\_snd) in
19 ; a list of numbers to experiment on
20 let mylist = make\_list 1 (make\_list 2 (make\_list 3 empty)) in
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 = (\shift n. n shift (make\_pair 0 0) get\_snd) (\p. p (\x y. make\_pair (succ x) x)) in
27 let leq = \m n. iszero(n pred m) in
28 let eq = \m n. and (leq m n)(leq n m) in
30 ; a fixed-point combinator for defining recursive functions
31 let Y = \f. (\h. f (h h)) (\h. f (h h)) in
33 let length = Y (\length l. isempty l 0 (succ (length (tail l)))) in
36 let makePair = make\_pair in
40 let isNil = isempty in
41 let makeList = make\_list in
42 let isZero = iszero in
45 let t1 = (make_list 1 empty) in
46 let t2 = (make_list 2 empty) in
47 let t3 = (make_list 3 empty) in
48 let t12 = (make_list t1 (make_list t2 empty)) in
49 let t23 = (make_list t2 (make_list t3 empty)) in
50 let ta = (make_list t1 t23) in
51 let tb = (make_list t12 t3) in
52 let tc = (make_list t1 (make_list t23 empty)) in
65 <input id="PARSE" value="Normalize" type="button">
66 <input id="ETA" type="checkbox">do eta-reductions too
67 <noscript><p>You may not see it because you have JavaScript turned off. Uffff!</p></noscript>
68 <script src="/code/lambda.js"></script>
69 <script src="/code/tokens.js"></script>
70 <script src="/code/parse.js"></script>
71 <script src="/code/json2.js"></script>
75 /*jslint evil: true */
77 /*members create, error, message, name, prototype, stringify, toSource,
81 /*global JSON, make_parse, parse, source, tree */
83 // Make a new object that inherits members from an existing object.
85 if (typeof Object.create !== 'function') {
86 Object.create = function (o) {
93 // Transform a token object into an exception object and throw it.
95 Object.prototype.error = function (message, t) {
97 t.name = "SyntaxError";
104 var parse = make_parse();
106 function go(source) {
107 var string, tree, expr, eta;
109 tree = parse(source);
110 // string = JSON.stringify(tree, ['key', 'name', 'message', 'value', 'arity', 'first', 'second', 'third', 'fourth'], 4);
111 expr = tree.handler();
112 // string = JSON.stringify(expr, ['key', 'name', 'message', 'value', 'arity', 'first', 'second', 'tag', 'variable', 'left', 'right', 'bound', 'body' ], 4);
113 // string = expr.to_string() + "\n\n~~>\n\n";
115 eta = document.getElementById('ETA').checked;
116 string = string + reduce(expr, eta, false).to_string();
118 string = JSON.stringify(e, ['name', 'message', 'from', 'to', 'key',
119 'value', 'arity', 'first', 'second', 'third', 'fourth'], 4);
121 document.getElementById('OUTPUT').innerHTML = string
122 .replace(/&/g, '&')
123 .replace(/[<]/g, '<');
126 document.getElementById('PARSE').onclick = function (e) {
127 go(document.getElementById('INPUT').value);