Lambda Calculus
Check-in [183f4ee657]
Not logged in

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

Overview
Comment:parser looks promising, updated TODO and some docs
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA1:183f4ee65749929ebfb7ae7c9643ac7552d05b94
User & Date: bill 2012-03-20 23:14:58
Context
2012-03-20
23:18
minor changes check-in: 331945630b user: bill tags: trunk
23:14
parser looks promising, updated TODO and some docs check-in: 183f4ee657 user: bill tags: trunk
15:53
doc changes check-in: c417d12f49 user: bill tags: trunk
Changes
Hide Diffs Unified Diffs Ignore Whitespace Patch

Changes to TODO.

1

2
3
4
5
TODO

* debug VM

DONE
* print VM contexts

>




1
2
3
4
5
6
TODO
* allow args in definitions: func arg1 arg2 = body => func = \arg1 arg2 . body
* debug VM

DONE
* print VM contexts

Changes to hl.js.

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
...
749
750
751
752
753
754
755


756
757
758
759
	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()))
................................................................................
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)

    console.log("LIT: " + dgen(wlit('x'), true)[0])



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







<
<
|
<
>
>
|
<
<
<
<
>




|


|

>

|
|
<
|
|

|
|
<
|


<
>







 







>
>




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
...
744
745
746
747
748
749
750
751
752
753
754
755
756
	for (i in oldVars) {
	    vars[i] = oldVars[i]
	}
	return expr
    }
    
    function tparseVariable(tok, vars, oldVars) {


	if (vars[tok] || CTX.astsByName[tok]) {

	    vars[tok] = tok
	    cur = wref(tok)
	} else {




	    cur = wlit(tok)
	}
	return cur
    }
    function tparseLambda(toks, vars) {
	var name, old, body
	
	if (toks.length < 3 || toks[toks.length - 1] == '.') {
	    throw new Error('imcomplete lambda definition: ' + toks.reverse().join(' '))
	}
	old = vars[name]
	if (toks[toks.length - 2] == '.') {
	    name = toks.pop()
	    vars[name] = name

	    toks.pop()
	    body = tparse(toks, vars)
	} else {
	    name = toks.pop()
	    vars[name] = name

	    body = tparseLambda(toks, vars)
	}
	vars[name] = old

	return wlambda(name, 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()))
................................................................................
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)

    console.log("LIT: " + dgen(wlit('x'), true)[0])

    console.log("\\x.x: " + astPrint(parse("\\x.x x")))

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

Changes to lazp.wiki.

19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
_lambda var body -- lambda binding: var should be a string or number, body is an AST  
_apply func arg -- function application: func can be any AST function except a _prim  
_prim arg rest -- primitive call: rest is either a _lit, a _ref, or another _prim (which allows more args)  

The AST functions serve as an embedded DSL.
## Examples

eval x = 𝛍x . x -- evaluates x  
apply func arg = 𝛍 func arg . _apply (_lit func) (_lit arg)  
compile code-string -- function returning AST  

## Parser macros  

How these work depends on the parser you use, but they run Lazp code at parse-time.  Parser macros can implement things like splicing primitive values into the AST and importing libraries, but the most important thing is that they allow developers to extend the parser.  Parser macros can be activated using a standard parser macro (of course :) ).

## Implementation








|
|
|







19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
_lambda var body -- lambda binding: var should be a string or number, body is an AST  
_apply func arg -- function application: func can be any AST function except a _prim  
_prim arg rest -- primitive call: rest is either a _lit, a _ref, or another _prim (which allows more args)  

The AST functions serve as an embedded DSL.
## Examples

eval x -- evaluates x  
apply func arg = eval (_apply (_lit func) (_lit arg))  
compile code-string -- returns an AST for a string of Lazp code  

## Parser macros  

How these work depends on the parser you use, but they run Lazp code at parse-time.  Parser macros can implement things like splicing primitive values into the AST and importing libraries, but the most important thing is that they allow developers to extend the parser.  Parser macros can be activated using a standard parser macro (of course :) ).

## Implementation