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
9 let make_pair = \f s g. g f s in
11 let get_snd = false in
12 let empty = make_pair true junk in
13 let isempty = \x. x get_fst in
14 let make_list = \h t. make_pair false (make_pair h t) in
15 let head = \l. isempty l err (l get_snd get_fst) in
16 let tail = \l. isempty l err (l get_snd get_snd) in
18 ; a list of numbers to experiment on
19 let mylist = make_list 1 (make_list 2 (make_list 3 empty)) in
22 let iszero = \n. n (\x. false) true in
23 let succ = \n s z. s (n s z) in
24 let mul = \m n s. m (n s) in
25 let pred = (\shift n. n shift (make_pair 0 0) get_snd) (\p. p (\x y. make_pair (succ x) x)) in
26 let leq = \m n. iszero(n pred m) in
27 let eq = \m n. and (leq m n)(leq n m) in
29 ; a fixed-point combinator for defining recursive functions
30 let Y = \f. (\h. f (h h)) (\h. f (h h)) in
32 let length = Y (\length l. isempty l 0 (succ (length (tail l)))) in
35 let makePair = make_pair in
39 let isNil = isempty in
40 let makeList = make_list in
41 let isZero = iszero in
44 let t1 = (make_list 1 empty) in
45 let t2 = (make_list 2 empty) in
46 let t3 = (make_list 3 empty) in
47 let t12 = (make_list t1 (make_list t2 empty)) in
48 let t23 = (make_list t2 (make_list t3 empty)) in
49 let ta = (make_list t1 t23) in
50 let tb = (make_list t12 t3) in
51 let tc = (make_list t1 (make_list t23 empty)) in
63 <input id="PARSE" value="Normalize" type="button">
64 <input id="ETA" type="checkbox">do eta-reductions too
65 <noscript><p>You may not see it because you have JavaScript turned off. Uffff!</p></noscript>
66 <script src="/code/lambda.js"></script>
67 <script src="/code/tokens.js"></script>
68 <script src="/code/parse.js"></script>
69 <script src="/code/json2.js"></script>
73 /*jslint evil: true */
75 /*members create, error, message, name, prototype, stringify, toSource,
79 /*global JSON, make_parse, parse, source, tree */
81 // Make a new object that inherits members from an existing object.
83 if (typeof Object.create !== 'function') {
84 Object.create = function (o) {
91 // Transform a token object into an exception object and throw it.
93 Object.prototype.error = function (message, t) {
95 t.name = "SyntaxError";
102 var parse = make_parse();
104 function go(source) {
105 var string, tree, expr, eta;
107 tree = parse(source);
108 // string = JSON.stringify(tree, ['key', 'name', 'message', 'value', 'arity', 'first', 'second', 'third', 'fourth'], 4);
109 expr = tree.handler();
110 // string = JSON.stringify(expr, ['key', 'name', 'message', 'value', 'arity', 'first', 'second', 'tag', 'variable', 'left', 'right', 'bound', 'body' ], 4);
111 // string = expr.to_string() + "\n\n~~>\n\n";
113 eta = document.getElementById('ETA').checked;
114 string = string + reduce(expr, eta, false).to_string();
116 string = JSON.stringify(e, ['name', 'message', 'from', 'to', 'key',
117 'value', 'arity', 'first', 'second', 'third', 'fourth'], 4);
119 document.getElementById('OUTPUT').innerHTML = string
120 .replace(/&/g, '&')
121 .replace(/[<]/g, '<');
124 document.getElementById('PARSE').onclick = function (e) {
125 go(document.getElementById('INPUT').value);