1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 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 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136
|
program lazylam; // -*-C-*-ish
/* [Unfinished] evaluator for the untyped lambda calculus with integers,
lazy version. */
// Raw values
data Expr = Var(Int x) // de Bruijn indexed variable
| Lam(Expr scope) // Binding
| App(Expr f, Expr s) // Function application
| Const(Int val)
| Inc(Expr ni)
| Dec(Expr nd)
| PrimRec(Expr target,Expr mzero,Expr msuc)
| If(Expr i, Expr t, Expr e);
// Semantic representation of values. Difference from eager version is
// that each recursive value is a suspension - Sem() rather than Sem.
// We only evaluate these when we need them
data Sem = SemLam(Sem(Sem()) scope)
| SemConst(Int val)
| SemPrimRec(Sem() target, Sem() mzero, Sem() msuc)
| SemInc(Sem() ni)
| SemDec(Sem() nd)
| Blocked(Blocked f, [Sem()] args);
// Irreducible terms
data Blocked = BVar(Int x);
data Spine<a> = Lin | Snoc(Spine<a> init, a last);
type Ctxt = Spine<Sem()>;
Exception FellOffEndOfContext;
Sem lookup(Int v, Ctxt ctxt)
{
if (v==0) {
return force(ctxt.last); // Evaluate it
} else if (v>0) {
return lookup(v-1,ctxt.init);
} else {
throw(FellOffEndOfContext);
}
}
Sem eval(Ctxt ctxt, Expr e)
{
case e of {
Var(v) -> return lookup(v,ctxt);
| Lam(sc) -> return SemLam(lambda(arg) -> { eval(Snoc(ctxt,@arg),sc) });
| App(f,a) -> return app(ctxt,eval@(ctxt,f),eval@(ctxt,a));
| Const(c) -> return SemConst(c);
| Inc(n) -> return increment(eval(ctxt,n));
| Dec(n) -> return decrement(eval(ctxt,n));
| PrimRec(t,z,s) ->
return primrec(ctxt, eval@(ctxt,t),eval@(ctxt,z),eval@(ctxt,s));
| If(i,t,e) ->
return runIf(ctxt, eval@(ctxt,i),eval@(ctxt,t),eval@(ctxt,e));
}
}
Sem app(Ctxt ctxt, Sem() f, Sem() a)
{
case f of { // Evaluate f
SemLam(scfun) -> return scfun(@a);
}
}
Sem increment(Sem n)
{
case n of {
SemConst(c) -> return (SemConst(c+1));
}
}
Sem decrement(Sem n)
{
case n of {
SemConst(c) -> return (SemConst(c-1));
}
}
Sem primrec(Ctxt ctxt, Sem() t, Sem() z, Sem() s)
{
case t of { // Evaluate t
SemConst(x) ->
if (x==0) {
return z; // Evaluate z
}
else
{
dec = SemConst@(x-1);
rec = primrec@(ctxt,dec,@z,@s);
return app(ctxt,app@(ctxt,@s,@dec),@rec);
}
}
}
Sem runIf(Ctxt ctxt, Sem() i, Sem() t, Sem() e)
{
case i of { // Evaluate i, then t or e, but not both.
SemConst(x) -> if (x!=0) { return t; } else { return e; }
}
}
Void showSem(Sem v)
{
case v of {
SemConst(x) -> putStrLn(String(x));
}
}
Void main()
{
// plus = \m n. primrec n m (\k ih. inc(ih))
plus = Lam(Lam(PrimRec(Var(0),Var(1),
Lam(Lam(Inc(Var(0)))))));
// mult = \m n. primrec n 0 (\k ih. plus m ih)
mult = Lam(Lam(PrimRec(Var(0),Const(0),
Lam(Lam(App(App(plus,Var(3)),Var(0)))))));
// Wow, this works when we evaluate lazily!
// y = \f . (\x. f (x x)) (\x. f (x x))
y = Lam(App(Lam(App(Var(1),App(Var(0),Var(0)))),
Lam(App(Var(1),App(Var(0),Var(0))))));
// add4 = \x. y (\add4 x. if x==0 then 4 else 1+(add4 (x-1))) x
addbody = Lam(Lam(If(Var(0),Inc(App(Var(1),(Dec(Var(0))))),Const(4))));
add4 = Lam(App(App(y,addbody),Var(0)));
showSem(eval(Lin,App(App(mult, Const(6)),Const(7))));
// Wow, it works!
showSem(eval(Lin,App(add4,Const(3))));
}
|