Lambda Calculus
Check-in [631c62a7e4]
Not logged in

Many hyperlinks are disabled.
Use anonymous login to enable hyperlinks.

Overview
Comment:fixed compilation bugs, added memoization, started working on parser
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA1:631c62a7e4cabbf0a69f68a50c049a7440fe3baa
User & Date: bill 2012-03-20 04:05:52
Context
2012-03-20
04:28
changed lambdas back to simple var-body pairs, instead of taking a function; escaped var names check-in: 9d67014f10 user: bill tags: trunk
04:05
fixed compilation bugs, added memoization, started working on parser check-in: 631c62a7e4 user: bill tags: trunk
2012-03-19
14:57
printing and compiling with new LC-based structure check-in: 2123c15f8a user: bill tags: trunk
Changes
Hide Diffs Unified Diffs Ignore Whitespace Patch

Changes to hl.js.

47
48
49
50
51
52
53


54
55
56
57
58
59
60
..
61
62
63
64
65
66
67

































68





69
70
71
72
73
74
75
..
80
81
82
83
84
85
86
87









88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128

129
130
131
132
133
134
135
...
145
146
147
148
149
150
151





152
153
154
155
156
157
158
...
163
164
165
166
167
168
169
170



171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188



















189















190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224

225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249


250

251
252
253


254

255

256
257

258
259
260
261
262
263
264
...
337
338
339
340
341
342
343








































344

345


346

























































































































































































347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
...
403
404
405
406
407
408
409
410
411
412
413
414
415
416

417
418















419
420
421
422
t0 = _lit bubba
t1 = _ref bubba
t2 = _lambda \x ._lit hello
t3 = _lambda \x . _lit true
t4 = _lambda \x . _ref x
t5 = _lambda \x . _lambda \y . _ref x
t6 = _lambda \x . _lambda \y . _ref y


tlit = _lambda \x . _name lit (_lambda \f . _apply (_ref f) (_ref x))
tref = _lambda \x . _name ref (_lambda \f . _apply (_ref f) (_ref x))
tlambda = _lambda \f . _name lambda (_lambda \g . _lambda \v . _apply (_ref g) (_apply (_ref f) (_ref v)))
tapply = _lambda \func . _lambda \arg . _name apply (_lambda \f . _apply (_apply (_ref f) (_ref func)) (_ref arg))
tprim =  _lambda \arg . _lambda \rest . _name prim (_lambda \f . _apply (_apply (_ref f) (_ref arg)) (_ref rest))
tname = _lambda \nm . _lambda \ast . _name name (_lambda \f . _apply (_apply (_ref f) (_ref nm)) (_ref ast))
*/})
................................................................................

    var _refId
    var _litId
    var _lambdaId
    var _applyId
    var _primId
    var _nameId







































    function isRef(f) {return f.lambda.id == _refId}
    function isLit(f) {return f.lambda.id == _litId}
    function isLambda(f) {return f.lambda.id == _lambdaId}
    function isApply(f) {return f.lambda.id == _applyId}
    function isPrim(f) {return f.lambda.id == _primId}
    function isName(f) {return f.lambda.id == _nameId}
    function astType(f) {
................................................................................
	case _applyId: return 'apply'
	case _primId: return 'prim'
	case _nameId: return 'name'
	}
    }
    function resolve(v){return typeof v == 'function' ? v() : v}
    function first(){return function(a) {return a}}
    function second(){return function(a) {return function(b){return b}}}









    function astPrint(ast, res) {
	var isFirst = !res

	res = res || []
	if (isFirst) res.gen = 0
	switch (ast.lambda && ast.lambda.id) {
	case _nameId:
	    res.push('name ', ast(first), ' (')
	    astPrint(ast(second)(), res)
	    res.push(')')
	    break
	case _refId:
	    res.push('ref ')
	    var val = ast(first)()
	    res.push(val.lambda ? "WHA?" : val)
	    break
	case _litId:
	    res.push('lit ')
	    var val = ast(first)()

	    res.push(val.lambda ? "{" + val.lambda.toString() + "}" : val)
	    break
	case _lambdaId:
	    var v = "VAR" + res.gen++
	    var vf = function(){return v}

	    res.push('lambda ')
	    res.push(v)
	    res.push(' . ')
	    astPrint(resolve(ast(first)(vf)), res)
	    break
	case _applyId:
	    res.push('apply (')
	    astPrint(ast(first), res)
	    res.push(') (')
	    astPrint(ast(second)(), res)
	    res.push(')')
	    break
	case _primId:
	    res.push('prim ')
	    astPrint(ast(first), res)

	    break
	default:
	    res.push("WHA???")
	    break
	}
	return isFirst && res.join('')
    }
................................................................................
    }

    /**
       A context is program scope
     */
    function createContext() {
	var ctx = (function(){





	    function id(func, id) {
		func.context = C
		func.id = id
		return func
	    }

	    function nameAst(nm, ast) {
................................................................................
		if (!ast.id) {
		    C.astsById.push(ast)
		    ast.id = C.astsById.length
		}
		return ast
	    }

	    var C = {



		astsById: [],
		astsByName: {},
		addAst: addAst,
		id: id,
		nameAst: nameAst,
		eval: function(str){return eval(str)},
		subcontext: function(){return function(str){return eval(str)}},
	    }

	    return C
	})()

	ctx.funcId = 0
	return ctx
    }

    var CTX = createContext()




















    function dgen(ast) {return gen(ast, null, null, true)}















    function gen(ast, res, ctx, deref, prim, cont) {
	var isFirst = !res

	res = res || []
	ctx = ctx || CTX.subcontext()
	ctx.curLit = 0
	if (isFirst) ctx.gen = 0
	CTX.addAst(ast)
	switch (ast.lambda && ast.lambda.id) {
	case _nameId:
	    var nm = ast(first)
	    var a = ast(second)

	    CTX.nameAst(nm, a)
	    gen(a(), res, ctx)
	    break
	case _refId:
	    var val = ast(first)()

	    if (val.lambda) throw new Error("attempt to use lambda as a variable")
	    res.push(val)
	    if (deref) res.push("()")
	    break
	case _litId:
	    var lit = "lit" + ctx.curLit++

	    ctx("var " + lit)
	    ctx("(function(v){" + lit + " = v})")(ast(first)())
	    res.push(lit)
	    if (deref) res.push("()")
	    break
	case _lambdaId:
	    var v = "VAR" + ctx.gen++
	    var vf = function(){return v}


	    if (!deref) res.push("(function(){return ")
	    res.push("id(function(" + v + "){return ")
	    gen(resolve(ast(first)(vf)), res, ctx)
	    res.push("}, " + ast.id + ")")
	    if (!deref)	res.push("})")
	    break
	case _applyId:
	    var func = ast(first)
	    var arg = ast(second)()

	    if (!deref) res.push("(function(){return ")
	    gen(func, res, ctx, true)
	    res.push("(")
	    gen(arg, res, ctx)
	    res.push(")")
	    if (!deref)	res.push("})")
	    break
	case _primId:
	    var arg = ast(first)
	    var rest = ast(rest)

	    if (prim) {
		if (cont) {
		    res.push(", ")
		}


		gen(arg, res, ctx, false, true, true, true)

	    } else {
		if (!deref) res.push("(function(){return ")
		gen(arg, res, ctx, true, false)


		res.push("(")

		gen(rest, res, ctx, true, false, true)

		res.push(")")
		if (!deref)	res.push("})")

	    }
	    break
	default:
	    res.push("'WHA???'")
	    break
	}
	return isFirst && [res.join(''), ctx]
................................................................................
	return id(function(_ast) {
	    return id(function(_f) {
		return _f()(_nm)(_ast)
	    }, _nameId)
	}, -4)
    }









































    function compile(str) {

    }




























































































































































































/*
    console.log("_lambdaId: " + _lambdaId)
    console.log("t1: " + LC.L._t1())
    console.log("t1.id: " + LC.L._t1().lambda.id)
    console.log("t1: " + astPrint(LC.L._t1()))
    console.log("t2: " + LC.L._t2())
    console.log("t2 lambda: " + LC.L._t2().lambda)
    console.log("t2 id: " + LC.L._t2().lambda.id)
    console.log("t2 type: " + astType(LC.L._t2()))
    console.log("t2 var: " + LC.L._t2()(first))
    console.log("t2 body: " + LC.L._t2()(second)().lambda)
    console.log("t2: " + astPrint(LC.L._t2()))
    console.log("t3: " + astPrint(LC.L._t3()))
*/
    console.log("t4: " + astPrint(LC.L._t4()))
/*
    console.log("t5: " + astPrint(LC.L._t5()))
    console.log("t6: " + astPrint(LC.L._t6()))
................................................................................
    console.log("tname: " + dgen(LC.L._tname())[0])
    console.log("tlit: " + LC.L.__lit())
    console.log("tref: " + LC.L.__ref())
    console.log("tlambda: " + LC.L.__lambda())
    console.log("tapply: " + LC.L.__apply())
    console.log("tprim: " + LC.L.__apply())
    console.log("tname: " + LC.L.__name())
*/
/*
    console.log("REF ID: " + (ref("duh")()).id)
    console.log("REF TEST: " + astPrint(ref("duh")()))
    console.log("REF GEN: " + dgen(ref("duh")())[0])
*/


    console.log("T4 AST: " + astPrint(wlambda(function(x){return ref(x)})))
    console.log("T4 gen: " + dgen(wlambda(function(x){return ref(x)}))[0])
















// TODO: compile AST funcs directly and include the JS source here
// for self hosting
})()







>
>







 







>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
|
>
>
>
>
>







 







|
>
>
>
>
>
>
>
>
>





|

|
|




|




|





<




|



|

|




|
>







 







>
>
>
>
>







 







|
>
>
>








<
<








>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
|
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
|
|








|
|


|


|









|





<

>

|
|




|
|

|
|

|

|









>
>
|
>

<
<
>
>

>
|
>

<
>







 







>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
|
>
|
>
>
|
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>










|







 







<
<





>
|
|
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>




47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
..
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
...
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160

161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
...
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
...
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235


236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311

312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343


344
345
346
347
348
349
350

351
352
353
354
355
356
357
358
...
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
...
725
726
727
728
729
730
731


732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
t0 = _lit bubba
t1 = _ref bubba
t2 = _lambda \x ._lit hello
t3 = _lambda \x . _lit true
t4 = _lambda \x . _ref x
t5 = _lambda \x . _lambda \y . _ref x
t6 = _lambda \x . _lambda \y . _ref y
t7 = _lambda \x . _lambda \y . _lambda \z . _apply (_ref x) (_apply (_ref y) (_ref z))
t77 = \x y z. x (y z)
tlit = _lambda \x . _name lit (_lambda \f . _apply (_ref f) (_ref x))
tref = _lambda \x . _name ref (_lambda \f . _apply (_ref f) (_ref x))
tlambda = _lambda \f . _name lambda (_lambda \g . _lambda \v . _apply (_ref g) (_apply (_ref f) (_ref v)))
tapply = _lambda \func . _lambda \arg . _name apply (_lambda \f . _apply (_apply (_ref f) (_ref func)) (_ref arg))
tprim =  _lambda \arg . _lambda \rest . _name prim (_lambda \f . _apply (_apply (_ref f) (_ref arg)) (_ref rest))
tname = _lambda \nm . _lambda \ast . _name name (_lambda \f . _apply (_apply (_ref f) (_ref nm)) (_ref ast))
*/})
................................................................................

    var _refId
    var _litId
    var _lambdaId
    var _applyId
    var _primId
    var _nameId
    var tokenPat = null
    var specials = '[]().*+?|'
    var tokenDefPat = /^ *([^ ]+) *(=[.)]=|=\([^=]+=|=)(?:[^=])/
    var numberPat = /^[0-9]+(\.[0-9]+)?|([0-9]+)?\.[0-9]+/
    var order = []
    var warnFreeVariable = []
    var charCodes = {
	"'": '$a',
	',': '$b',
	'$': '$c',
	'@': '$d',
	'?': '$e',
	'/': '$f',
	'*': '$g',
	'&': '$h',
	'^': '$i',
	'!': '$k',
	'`': '$l',
	'~': '$m',
	'-': '$n',
	'+': '$o',
	'=': '$p',
	'|': '$q',
	'[': '$r',
	']': '$s',
	'{': '$t',
	'}': '$u',
	'"': '$v',
	':': '$w',
	';': '$x',
	'<': '$y',
	'>': '$z',
	'%': '$A',
    }
    var codeChars = {}
    for (i in charCodes) {
	codeChars[charCodes[i].substring(1)] = i
    }
    
    function isRef(f) {return f.lambda.id == _refId}
    function isLit(f) {return f.lambda.id == _litId}
    function isLambda(f) {return f.lambda.id == _lambdaId}
    function isApply(f) {return f.lambda.id == _applyId}
    function isPrim(f) {return f.lambda.id == _primId}
    function isName(f) {return f.lambda.id == _nameId}
    function astType(f) {
................................................................................
	case _applyId: return 'apply'
	case _primId: return 'prim'
	case _nameId: return 'name'
	}
    }
    function resolve(v){return typeof v == 'function' ? v() : v}
    function first(){return function(a) {return a}}
    function second(){return function(a) {return function(b){return b()}}}
    function getNameNm(n) {return n(first)}
    function getNameAst(n) {return n(second)}
    function getRefVar(r) {return r(first)()}
    function getLitVal(l) {return l(first)()}
    function getLambdaBody(l, varName) {return resolve(l(first)(laz(varName)))}
    function getApplyFunc(a) {return a(first)}
    function getApplyArg(a) {return a(second)}
    function getPrimArg(p) {return p(first)}
    function getPrimRest(p) {return p(second)}
    function astPrint(ast, res) {
	var isFirst = !res

	res = res || []
	if (isFirst) res.gen = 0
	switch (ast.id || (ast.lambda && ast.lambda.id)) {
	case _nameId:
	    res.push('name ', getNameNm(ast), ' (')
	    astPrint(getNameAst(ast), res)
	    res.push(')')
	    break
	case _refId:
	    res.push('ref ')
	    var val = getRefVar(ast)
	    res.push(val.lambda ? "WHA?" : val)
	    break
	case _litId:
	    res.push('lit ')
	    var val = getLitVal(ast)

	    res.push(val.lambda ? "{" + val.lambda.toString() + "}" : val)
	    break
	case _lambdaId:
	    var v = "VAR" + res.gen++


	    res.push('lambda ')
	    res.push(v)
	    res.push(' . ')
	    astPrint(getLambdaBody(ast, v), res)
	    break
	case _applyId:
	    res.push('apply (')
	    astPrint(getApplyFunc(ast), res)
	    res.push(') (')
	    astPrint(getApplyArg(ast), res)
	    res.push(')')
	    break
	case _primId:
	    res.push('prim ')
	    astPrint(getPrimArg(ast), res)
	    astPrint(getPrimRest(ast), res)
	    break
	default:
	    res.push("WHA???")
	    break
	}
	return isFirst && res.join('')
    }
................................................................................
    }

    /**
       A context is program scope
     */
    function createContext() {
	var ctx = (function(){
	    var tokens = {}
	    var groupOpens = {'(': ')'}
	    var groupCloses = {')': 1}
	    var C

	    function id(func, id) {
		func.context = C
		func.id = id
		return func
	    }

	    function nameAst(nm, ast) {
................................................................................
		if (!ast.id) {
		    C.astsById.push(ast)
		    ast.id = C.astsById.length
		}
		return ast
	    }

	    return C = {
		tokens: tokens,
		groupOpens: groupOpens,
		groupCloses: groupCloses,
		astsById: [],
		astsByName: {},
		addAst: addAst,
		id: id,
		nameAst: nameAst,
		eval: function(str){return eval(str)},
		subcontext: function(){return function(str){return eval(str)}},
	    }


	})()

	ctx.funcId = 0
	return ctx
    }

    var CTX = createContext()

    function Memo() {
	this.count = 0
    }
    Memo.prototype = {
	add: function() {return "memo" + this.count++},
	toString: function() {
	    if (this.count) {
		var ret = ''

		for (var i = 0; i < this.count; i++) {
		    ret += "var memo" + i + "; "
		}
		return ret
	    } else {
		return ''
	    }
	}
    }

    function dgen(ast) {
	var mem = new Memo()
	var ret = gen(ast, [], null, mem, true)

	return mem.count ? ["(function(){", mem, "return " + ret[0] + "})()", ret[1]] : ret
    }
    function memoStart(res, memo, deref) {
	if (!deref) {
	    var mem = memo.add()

	    res.push("function(){return ", mem, " || (", mem, " = ")
	}
    }
    function memoEnd(res, memo, deref) {
	if (!deref) res.push(")}")
    }
    function gen(ast, res, ctx, memo, deref, prim, cont) {
	var isFirst = !ctx

	res = res || []
	ctx = ctx || CTX.subcontext()
	ctx.curLit = 0
	if (isFirst) ctx.gen = 0
	CTX.addAst(ast)
	switch (ast.lambda && ast.lambda.id) {
	case _nameId:
	    var nm = getNameNm(ast)
	    var a = getNameAst(ast)

	    CTX.nameAst(nm, a)
	    gen(a, res, ctx, memo)
	    break
	case _refId:
	    var val = getRefVar(ast)

	    if (val.lambda) throw new Error("attempt to use lambda as a variable")
	    res.push(val)
	    if (deref) res.push("()")
	    break
	case _litId:
	    var lit = "lit" + ctx.curLit++

	    ctx("var " + lit)
	    ctx("(function(v){" + lit + " = v})")(getLitVal(ast))
	    res.push(lit)
	    if (deref) res.push("()")
	    break
	case _lambdaId:
	    var v = "VAR" + ctx.gen++


	    memo = new Memo()
	    if (!deref) res.push("(function(){return ")
	    res.push("id(function(" + v + "){", memo, "return ")
	    gen(getLambdaBody(ast, v), res, ctx, memo, true)
	    res.push("}, " + ast.id + ")")
	    if (!deref)	res.push("})")
	    break
	case _applyId:
	    var func = getApplyFunc(ast)
	    var arg = getApplyArg(ast)

	    memoStart(res, memo, deref)
	    gen(func, res, ctx, memo, true)
	    res.push("(")
	    gen(arg, res, ctx, memo)
	    res.push(")")
	    memoEnd(res, memo, deref)
	    break
	case _primId:
	    var arg = ast(first)
	    var rest = ast(rest)

	    if (prim) {
		if (cont) {
		    res.push(", ")
		}
		res.push(arg)
		if (isPrim(rest)) {
		    gen(rest, res, ctx, memo, false, true, true)
		}
	    } else {


		memoStart(res, memo, deref)
		res.push(arg)
		res.push("(")
		if (isPrim(rest)) {
		    gen(rest, res, ctx, memo, true, true, false)
		}
		res.push(")")

		memoEnd(res, memo, deref)
	    }
	    break
	default:
	    res.push("'WHA???'")
	    break
	}
	return isFirst && [res.join(''), ctx]
................................................................................
	return id(function(_ast) {
	    return id(function(_f) {
		return _f()(_nm)(_ast)
	    }, _nameId)
	}, -4)
    }

    function defineToken(name, def) {
	if (def != '=') {
	    CTX.tokens[name] = 1
	    tokenPat = null
	    if (def[1] == '(') {
		CTX.groupOpens[name] = def.substring(2, def.length - 1)
	    } else if (def[1] == ')') {
		CTX.groupCloses[name] = 1
	    }
	}
    }
    function evalLine(line, noRebuild) {
	if (line != "" && (line.match(/^#(define|strict|lazy)/) || line[0] != '#')) {
	    var def = line.match(tokenDefPat)
	    var name = def ? def[1].trim() : null
	    
	    if (def) {
		defineToken(name, def[2])
		line = line.substring(def[0].length).trim()
	    }
	    return addExpr(name, line, noRebuild)
	}
	return false
    }
    function newEntry(name, ast) {
	ast.name = name
	ast.cname = nameSub(name)
	try {
	    var cmp = compile(ast)
	    ast.func = cmp[0]
	    ast.env = cmp[1]
	    ast.src = 'function() {\nreturn ' + ast.func + '\n}'
	    if (warnFreeVariable.length) {
		ast.usesFree = warnFreeVariable.join(', ')
		warnFreeVariable = []
	    }
	} catch (err) {
	    ast.src = function() {return "Error compiling: " + expr}
	}
    }
    function nameSub(name) {
	var s = '_'

	for (var i = 0; i < name.length; i++) {
	    var code = charCodes[name[i]]
	    
	    if (code) {
		if (!s) s = name.substring(0, i)
		s += code
	    } else if (s) {
		s += name[i]
	    }
	}
	return s || name
    }
    function nameUnsub(name) {
	var s = ''

	for (var i = 1; i < name.length; i++) {
	    if (name[i] == '$') {
		if (!s) s = name.substring(0, i)
		s += codeChars[name[++i]]
	    } else {
		if (s) s += name[i]
	    }
	}
	return s
    }
    function addExpr(name, txt, noRebuild) {
	if (name) {
	    var expr = newEntry(name, parse(txt))
	    var newOutput = ''
	    
	    expr.expr.name = name
	    if (!noRebuild) {
		for (var i = 0; i < order.length; i++) {
		    if (order[i].name == name) {
			order.splice(i, 1)
		    }
		}
	    }
	    exp("L", LC.L = L = null)
	    order.push(expr)
	    var hk = expr.expr.hashKey()
	    if (!hashed[hk]) hashed[hk] = expr
	    exprs[name] = expr
	    return true
	} else {
	    runExpr(txt.trim())
	    return false
	}
    }
    function addToken(tok, group) {
	var pat = ''
	
	CTX.tokens[tok] = group
	tokenPat = null
    }
    function createTokenPat() {
	if (!tokenPat) {
	    var types = []
	    
	    for (var i in CTX.tokens) {
		types.push(i)
	    }
	    // sort them by length, longest first
	    types.sort(function(a, b) {b.length - a.length})
	    for (var i = 0; i < types.length; i++) {
		var s = types[i]
		var o = ''
		
		for (var p = 0; p < s.length; p++) {
		    if (specials.indexOf(s[p]) > -1) {
			o += '\\'
		    }
		    o += s[p]
		}
		types[i] = o
	    }
	    types.push('((#define|#strict|#lazy)(?=[ \t]))|[()#.\\\\]| +')
	    tokenPat = new RegExp(/'(\\'|[^'])*'|"(\\"|[^"])*"/.source + '|' + types.join('|'))
	}
    }
    function tokenize(str) {
	var pos = 0
	var toks = []
	
	str = str.replace(/\u03BB/g, '\\')
	createTokenPat()
	while (str.length && (pos = str.search(tokenPat)) > -1) {
	    if (pos > 0) {
		toks.push(str.substring(0, pos))
	    }
	    var tok = tokenPat.exec(str.substring(pos))[0]
	    if (tok.trim()) {
		if (tok[0] == '#' && !tok.match(/^#(define|strict|lazy)/)) break
		toks.push(tok)
	    }
	    str = str.substring(pos + tok.length)
	}
	if (str.length) {
	    toks.push(str)
	}
	return toks
    }
    function parse(str) {return tparse(tokenize(str).reverse(), {})}
    function addDef(toks) {
	var t = toks.reverse()
	
	defs[t[0]] = t.join(' ')
    }
    function tparse(toks, vars, expr) {
	var cur
	var oldVars = {}

	while (toks.length) {
	    var tok = toks.pop()

	    if (tok == ')') {
		toks.push(tok)
		return expr
	    }
	    if (tok == '\\') {
		cur = tparseLambda(toks, vars)
	    } else if (tok == '#define') {
		addDef(toks)
		toks = []
	    } else if (tok == '#lazy' || tok == '#strict') {
		cur = tparseVariable(tok, vars, oldVars)
	    } else {
		var expectedClose = CTX.groupOpens[tok]
		var skip = false

		if (expectedClose) {
		    cur = tparse(toks, vars, tok != '(' ? tparseVariable(tok, vars, oldVars) : null)
		    if (toks.length && toks[toks.length - 1] == expectedClose) {
			toks.pop()
		    }
		    skip = true
		}
		if (!skip) {
		    cur = tparseVariable(tok, vars, oldVars)
		}
	    }
	    expr = expr ? wapply(expr, cur) : cur
	    if (CTX.groupCloses[tok]) {
		toks.push(tok)
		return expr
	    }
	}
	for (i in oldVars) {
	    vars[i] = oldVars[i]
	}
	return expr
    }
    
    function tparseVariable(tok, vars, oldVars) {
	var cur = vars[tok]
	if (!cur) {
	    if (CTX.astsByName[tok]) {
		cur = wlit(CTX.astsByName[tok])
	    } else {
//****		cur = wref(tok) -- need to link in a ref to the lambda's var
	    }
	    if (vars[tok]) oldVars[tok] = vars[tok]
	    vars[tok] = cur
	}
	return cur
    }
    function tparseLambda(toks, vars) {
	var name, old, body, lvar
	
	if (toks.length < 3 || toks[toks.length - 1] == '.') {
		throw new Error('imcomplete lambda definition: ' + toks.reverse().join(' '))
	}
	if (toks[toks.length - 2] == '.') {
		name = toks.pop()
		old = vars[name]
		lvar = vars[name] = new Variable(name, false)
		toks.pop()
		body = tparse(toks, vars)
	} else {
		name = toks.pop()
		old = vars[name]
		lvar = vars[name] = new Variable(name, false)
		body = tparseLambda(toks, vars)
	}
	vars[name] = old
	return new Lambda(lvar, body)
    }

/*
    console.log("_lambdaId: " + _lambdaId)
    console.log("t1: " + LC.L._t1())
    console.log("t1.id: " + LC.L._t1().lambda.id)
    console.log("t1: " + astPrint(LC.L._t1()))
    console.log("t2: " + LC.L._t2())
    console.log("t2 lambda: " + LC.L._t2().lambda)
    console.log("t2 id: " + LC.L._t2().lambda.id)
    console.log("t2 type: " + astType(LC.L._t2()))
    console.log("t2 var: " + LC.L._t2()(first))
    console.log("t2 body: " + LC.L._t2()(second).lambda)
    console.log("t2: " + astPrint(LC.L._t2()))
    console.log("t3: " + astPrint(LC.L._t3()))
*/
    console.log("t4: " + astPrint(LC.L._t4()))
/*
    console.log("t5: " + astPrint(LC.L._t5()))
    console.log("t6: " + astPrint(LC.L._t6()))
................................................................................
    console.log("tname: " + dgen(LC.L._tname())[0])
    console.log("tlit: " + LC.L.__lit())
    console.log("tref: " + LC.L.__ref())
    console.log("tlambda: " + LC.L.__lambda())
    console.log("tapply: " + LC.L.__apply())
    console.log("tprim: " + LC.L.__apply())
    console.log("tname: " + LC.L.__name())


    console.log("REF ID: " + (ref("duh")()).id)
    console.log("REF TEST: " + astPrint(ref("duh")()))
    console.log("REF GEN: " + dgen(ref("duh")())[0])
*/

    var t4 = wlambda(function(x){return ref(x)})
    console.log("T4 AST: " + astPrint(t4))
    console.log("T4 gen: " + dgen(t4)[0])
    console.log("T4 src: " + t4)

    console.log("T7: " + astPrint(LC.L._t7()))
    var t7 = wlambda(function(x){return wlambda(function(y){return wlambda(function(z){return wapply(ref(x), wapply(ref(y), ref(z)))})})})
    console.log("T7 AST: " + astPrint(t7))
    console.log("T7 gen: " + dgen(t7)[0])
    console.log("T7 run: " + eval(dgen(t7)[0]))
    console.log("T7 1: " + eval(dgen(t7)[0])(laz(function(x){return x()+"1"})))
    console.log("T7 2: " + eval(dgen(t7)[0])(laz(function(x){return x()+"1"}))(laz(function(y){return y()+"2"})))

var log=console.log

    console.log("T7 3: " + eval(dgen(t7)[0])(laz(function(x){log("x: " + x()); return String(x())+"1"}))(laz(function(y){log("y: " + y()); return y()+"2"}))(laz("FLOOP ")))

    console.log("T77: " + LC.L._t77)

// TODO: compile AST funcs directly and include the JS source here
// for self hosting
})()