parent:
0d159351c7131f98bbe83fa0f45b743e0d28b3f9
nmyk <nick@nmyk.io>
2026-11-14T13:22:38-05:00
add tail calls
diff --git a/eval/builtin.go b/eval/builtin.go
index 7fcb411f0432274f883140f7661903c884ce702b..9c4f1e13c5b9aeabebc16f955861e9617e34a51e 100644
--- a/eval/builtin.go
+++ b/eval/builtin.go
@@ -59,19 +59,6 @@ }
return env
}
-func apply(f sexpr.Function, args []sexpr.Value) (sexpr.Value, error) {
- switch f := f.(type) {
- case *Builtin:
- if err := checkArity(f, len(args)); err != nil {
- return nil, err
- }
- return f.fn(args)
- case *Closure:
- return applyClosure(f, args)
- }
- panic(fmt.Sprintf("eval: cannot apply %T", f))
-}
-
func checkArity(b *Builtin, n int) error {
switch {
case b.maxArgs < 0 && n < b.minArgs:
diff --git a/eval/closure.go b/eval/closure.go
index d674df009652cdc2eef2323eabadb2171c61d463..2f029f1426844149f8de2f5355d0240c31ff561e 100644
--- a/eval/closure.go
+++ b/eval/closure.go
@@ -27,22 +27,22 @@ // (lambda (p1 ... pk) body...)
//
// The parameter list and the shape of the body are checked once, here,
// rather than on every call.
-func evalLambda(operands []sexpr.Value, env *Env) (sexpr.Value, error) {
+func evalLambda(operands []sexpr.Value, env *Env) (outcome, error) {
if len(operands) == 0 {
- return nil, errorf(ErrSyntax, "lambda needs a parameter list and a body")
+ return outcome{}, errorf(ErrSyntax, "lambda needs a parameter list and a body")
}
params, err := parseParams(operands[0])
if err != nil {
- return nil, err
+ return outcome{}, err
}
body := operands[1:]
if len(body) == 0 {
- return nil, errorf(ErrSyntax, "lambda needs a body")
+ return outcome{}, errorf(ErrSyntax, "lambda needs a body")
}
if err := checkBodyShape(body); err != nil {
- return nil, err
+ return outcome{}, err
}
- return &Closure{params: params, body: body, env: env}, nil
+ return done(&Closure{params: params, body: body, env: env})
}
// checkBodyShape enforces: zero or more defines, then one or more
@@ -88,34 +88,35 @@ }
return params, nil
}
-// applyClosure binds each parameter to its argument in the closure's own
-// environment, runs the body's defines, then evaluates its expressions in
-// order. The value of the last one is the result.
+// enterClosure binds each parameter to its argument in the closure's own
+// environment, runs the body's defines and every expression but the last,
+// and returns the last expression with the environment to evaluate it in.
+// The caller evaluates that final expression as a tail call.
//
// As in Scheme, the body's defines form their own scope nested inside the
// parameters', so a define may shadow a parameter.
-func applyClosure(c *Closure, args []sexpr.Value) (sexpr.Value, error) {
+func enterClosure(c *Closure, args []sexpr.Value) (sexpr.Value, *Env, error) {
if len(args) != len(c.params) {
- return nil, errorf(ErrArity, "%s takes %s, got %d", c.displayName(), plural(len(c.params), "argument"), len(args))
+ return nil, nil, errorf(ErrArity, "%s takes %s, got %d", c.displayName(), plural(len(c.params), "argument"), len(args))
}
env := c.env
for i, p := range c.params {
env = env.Extend(p, args[i])
}
s := newScope(env)
- var result sexpr.Value
- for _, form := range c.body {
+ last := len(c.body) - 1
+ for _, form := range c.body[:last] {
var err error
if items, ok := defineForm(form); ok {
_, err = s.define(items)
} else {
- result, err = Eval(form, s.env)
+ _, err = Eval(form, s.env)
}
if err != nil {
- return nil, err
+ return nil, nil, err
}
}
- return result, nil
+ return c.body[last], s.env, nil
}
// Reserved names are the special forms and builtins. In a .gov program
diff --git a/eval/define.go b/eval/define.go
index 11ddad94fb4681ab4318e8e912639700d89b9042..710cebf43f8d2ebbd0a851e45f758c9229605524 100644
--- a/eval/define.go
+++ b/eval/define.go
@@ -73,8 +73,8 @@
// define reached through Eval is in expression position, such as inside an
// if. Scope-level code intercepts the defines that are allowed before they
// get here.
-func evalMisplacedDefine(operands []sexpr.Value, env *Env) (sexpr.Value, error) {
- return nil, errorf(ErrSyntax, "define is only allowed at top level or at the start of a lambda body")
+func evalMisplacedDefine(operands []sexpr.Value, env *Env) (outcome, error) {
+ return outcome{}, errorf(ErrSyntax, "define is only allowed at top level or at the start of a lambda body")
}
func defineForm(v sexpr.Value) ([]sexpr.Value, bool) {
diff --git a/eval/eval.go b/eval/eval.go
index 4b9c3f3e21fbbd9e6feff1217c8ed39acfafb7f7..b9f39c62656fd63ce75d5b909c46d9e3aa759c26 100644
--- a/eval/eval.go
+++ b/eval/eval.go
@@ -6,7 +6,24 @@ // specialForm receives its operands unevaluated and decides itself which of
// them to evaluate. That is the whole difference from a function, and the
// reason if cannot be one: a function's arguments are all evaluated before
// it runs, so both branches would run.
-type specialForm func(operands []sexpr.Value, env *Env) (sexpr.Value, error)
+//
+// A form finishes either with a value or with a tail expression: the
+// expression whose value is the form's value, such as the chosen branch of
+// an if. Eval evaluates a tail expression by looping rather than by calling
+// itself, which is what makes tail calls proper.
+type specialForm func(operands []sexpr.Value, env *Env) (outcome, error)
+
+type outcome struct {
+ value sexpr.Value
+ tail sexpr.Value // if non-nil, continue with tail in env
+ env *Env
+}
+
+func done(v sexpr.Value) (outcome, error) { return outcome{value: v}, nil }
+
+func tailIn(expr sexpr.Value, env *Env) (outcome, error) {
+ return outcome{tail: expr, env: env}, nil
+}
var specialForms map[sexpr.Symbol]specialForm
@@ -26,14 +43,61 @@ //
// Atoms other than symbols evaluate to themselves. A symbol evaluates to its
// binding. A non-empty list is either a special form, chosen by its head
// symbol, or a function call.
+//
+// Expressions in tail position (an if branch, the last operand of and/or,
+// the last form of a closure body) replace expr and env and go round the
+// loop again. A tail-recursive function therefore runs in constant Go
+// stack: each call reuses this frame instead of nesting a new one. Only
+// conditions, arguments and non-final body forms are evaluated by nested
+// calls, and those return before the loop continues.
func Eval(expr sexpr.Value, env *Env) (sexpr.Value, error) {
- switch x := expr.(type) {
- case sexpr.Symbol:
- return evalSymbol(x, env)
- case *sexpr.Pair:
- return evalList(x, env)
+ for {
+ var list *sexpr.Pair
+ switch x := expr.(type) {
+ case sexpr.Symbol:
+ return evalSymbol(x, env)
+ case *sexpr.Pair:
+ list = x
+ default:
+ return expr, nil
+ }
+
+ items, ok := properList(list)
+ if !ok {
+ return nil, errorf(ErrSyntax, "cannot evaluate improper list %s", sexpr.Print(list))
+ }
+ if head, ok := items[0].(sexpr.Symbol); ok {
+ if form, ok := specialForms[head]; ok {
+ out, err := form(items[1:], env)
+ if err != nil {
+ return nil, err
+ }
+ if out.tail == nil {
+ return out.value, nil
+ }
+ expr, env = out.tail, out.env
+ continue
+ }
+ }
+
+ f, args, err := evalCall(items, env)
+ if err != nil {
+ return nil, err
+ }
+ switch f := f.(type) {
+ case *Builtin:
+ if err := checkArity(f, len(args)); err != nil {
+ return nil, err
+ }
+ return f.fn(args)
+ case *Closure:
+ if expr, env, err = enterClosure(f, args); err != nil {
+ return nil, err
+ }
+ default:
+ panic("eval: unknown function type")
+ }
}
- return expr, nil
}
func evalSymbol(name sexpr.Symbol, env *Env) (sexpr.Value, error) {
@@ -50,65 +114,53 @@ }
return b.value, nil
}
-func evalList(list *sexpr.Pair, env *Env) (sexpr.Value, error) {
- items, ok := properList(list)
- if !ok {
- return nil, errorf(ErrSyntax, "cannot evaluate improper list %s", sexpr.Print(list))
- }
- if head, ok := items[0].(sexpr.Symbol); ok {
- if form, ok := specialForms[head]; ok {
- return form(items[1:], env)
- }
- }
- return evalCall(items, env)
-}
-
-// evalCall evaluates the head, then the arguments left to right, then
-// applies the function. Every argument is evaluated before the call; special
-// forms exist for the cases where that is wrong.
-func evalCall(items []sexpr.Value, env *Env) (sexpr.Value, error) {
+// evalCall evaluates the head, then the arguments left to right. Every
+// argument is evaluated before the call; special forms exist for the cases
+// where that is wrong.
+func evalCall(items []sexpr.Value, env *Env) (sexpr.Function, []sexpr.Value, error) {
head, err := Eval(items[0], env)
if err != nil {
- return nil, err
+ return nil, nil, err
}
f, ok := head.(sexpr.Function)
if !ok {
- return nil, errorf(ErrNotAFunction, "%s is not a function", sexpr.Print(head))
+ return nil, nil, errorf(ErrNotAFunction, "%s is not a function", sexpr.Print(head))
}
args := make([]sexpr.Value, len(items)-1)
for i, item := range items[1:] {
if args[i], err = Eval(item, env); err != nil {
- return nil, err
+ return nil, nil, err
}
}
- return apply(f, args)
+ return f, args, nil
}
// (quote d) returns d without evaluating it.
-func evalQuote(operands []sexpr.Value, env *Env) (sexpr.Value, error) {
+func evalQuote(operands []sexpr.Value, env *Env) (outcome, error) {
if len(operands) != 1 {
- return nil, errorf(ErrSyntax, "quote takes 1 operand, got %d", len(operands))
+ return outcome{}, errorf(ErrSyntax, "quote takes 1 operand, got %d", len(operands))
}
- return operands[0], nil
+ return done(operands[0])
}
-// (if c a b) evaluates a unless c is F, else b. (if c a) gives () when c is
-// F. Only the chosen branch is evaluated.
-func evalIf(operands []sexpr.Value, env *Env) (sexpr.Value, error) {
+// (if c a b) continues with a unless c is F, else with b. (if c a) gives ()
+// when c is F. Only the chosen branch is evaluated, and it is in tail
+// position.
+func evalIf(operands []sexpr.Value, env *Env) (outcome, error) {
if len(operands) != 2 && len(operands) != 3 {
- return nil, errorf(ErrSyntax, "if takes 2 or 3 operands, got %d", len(operands))
+ return outcome{}, errorf(ErrSyntax, "if takes 2 or 3 operands, got %d", len(operands))
}
c, err := Eval(operands[0], env)
if err != nil {
- return nil, err
+ return outcome{}, err
}
if isTrue(c) {
- return Eval(operands[1], env)
+ return tailIn(operands[1], env)
}
if len(operands) == 3 {
- return Eval(operands[2], env)
+ return tailIn(operands[2], env)
}
- return sexpr.Nil, nil
+ return done(sexpr.Nil)
}
// isTrue implements Scheme truthiness: F is the only false value.
@@ -118,18 +170,27 @@ return !ok || bool(b)
}
// properList returns the elements of a list that ends in (), or false if
-// the list ends in anything else.
+// the list ends in anything else. It runs on every list the evaluator
+// meets, so it measures the list first and allocates once.
func properList(v sexpr.Value) ([]sexpr.Value, bool) {
- var items []sexpr.Value
+ n := 0
+ rest := v
for {
- switch x := v.(type) {
- case sexpr.Empty:
- return items, true
- case *sexpr.Pair:
- items = append(items, x.Car)
- v = x.Cdr
- default:
- return nil, false
+ p, ok := rest.(*sexpr.Pair)
+ if !ok {
+ break
}
+ n++
+ rest = p.Cdr
}
+ if rest != sexpr.Value(sexpr.Nil) {
+ return nil, false
+ }
+ items := make([]sexpr.Value, n)
+ for i := range items {
+ p := v.(*sexpr.Pair)
+ items[i] = p.Car
+ v = p.Cdr
+ }
+ return items, true
}
diff --git a/eval/logic.go b/eval/logic.go
index 9b0c47815c1b8e6b0cdcdbe84340be83402b59b1..44684c3e846a1523dae1668a7638ffe55c06486e 100644
--- a/eval/logic.go
+++ b/eval/logic.go
@@ -6,33 +6,42 @@ // and and or are special forms rather than functions because they must be
// able to skip evaluating their remaining operands. As in Scheme they
// return the deciding value, not just T or F, so (or x default) is the
// idiom for "x, or default if x is F".
+//
+// The last operand is in tail position: once every earlier operand has
+// failed to decide, the last one's value is the form's value as is.
// (and x...) returns F at the first F operand, else the last value.
-func evalAnd(operands []sexpr.Value, env *Env) (sexpr.Value, error) {
- var result sexpr.Value = sexpr.T
- for _, operand := range operands {
+func evalAnd(operands []sexpr.Value, env *Env) (outcome, error) {
+ if len(operands) == 0 {
+ return done(sexpr.T)
+ }
+ last := len(operands) - 1
+ for _, operand := range operands[:last] {
v, err := Eval(operand, env)
if err != nil {
- return nil, err
+ return outcome{}, err
}
if !isTrue(v) {
- return sexpr.F, nil
+ return done(sexpr.F)
}
- result = v
}
- return result, nil
+ return tailIn(operands[last], env)
}
// (or x...) returns the first operand that is not F, else F.
-func evalOr(operands []sexpr.Value, env *Env) (sexpr.Value, error) {
- for _, operand := range operands {
+func evalOr(operands []sexpr.Value, env *Env) (outcome, error) {
+ if len(operands) == 0 {
+ return done(sexpr.F)
+ }
+ last := len(operands) - 1
+ for _, operand := range operands[:last] {
v, err := Eval(operand, env)
if err != nil {
- return nil, err
+ return outcome{}, err
}
if isTrue(v) {
- return v, nil
+ return done(v)
}
}
- return sexpr.F, nil
+ return tailIn(operands[last], env)
}
diff --git a/eval/tail_test.go b/eval/tail_test.go
new file mode 100644
index 0000000000000000000000000000000000000000..18c787c4f115a9587bff0ea65a4409c367c776a3
--- /dev/null
+++ b/eval/tail_test.go
@@ -0,0 +1,56 @@
+package eval
+
+import (
+ "runtime/debug"
+ "testing"
+)
+
+// The first case loops a million times, the rest 100,000. Without proper
+// tail calls every iteration would add several Go frames; at either depth
+// that is far past the 16 MB stack limit set here (non-tail recursion
+// overflows it at 100,000), and the test binary would die with a stack
+// overflow.
+func TestTailCalls(t *testing.T) {
+ defer debug.SetMaxStack(debug.SetMaxStack(16 << 20))
+
+ runEvalCases(t, []evalCase{
+ {"(define count (lambda (n) (if (== n 0) 'done (count (- n 1))))) (count 1000000)", "done"},
+
+ // Tail position in the true branch, and in a nested if.
+ {"(define count (lambda (n) (if (!= n 0) (count (- n 1)) 'done))) (count 100000)", "done"},
+ {"(define count (lambda (n) (if (== n 0) 'done (if (< n 0) 'neg (count (- n 1)))))) (count 100000)", "done"},
+
+ // The last operand of and/or.
+ {"(define count (lambda (n) (or (== n 0) (count (- n 1))))) (count 100000)", "T"},
+ {"(define count (lambda (n) (and (> n -1) (if (== n 0) 'done (count (- n 1)))))) (count 100000)", "done"},
+
+ // The last form of a body, after defines and other expressions.
+ {"(define count (lambda (n) (define m (- n 1)) n (if (== n 0) 'done (count m)))) (count 100000)", "done"},
+
+ // An accumulator turns a computation into a loop.
+ {`(define sum (lambda (n acc) (if (== n 0) acc (sum (- n 1) (+ acc n)))))
+ (sum 100000 0)`, "5000050000"},
+ {`(define fact-iter (lambda (n acc) (if (== n 0) acc (fact-iter (- n 1) (* n acc)))))
+ (fact-iter 20 1)`, "2432902008176640000"},
+
+ // A tail call to a different function, passed in as an argument.
+ {`(define bounce (lambda (f n) (if (== n 0) 'done (f f (- n 1)))))
+ (bounce bounce 100000)`, "done"},
+
+ // Building a long list with an accumulator, then measuring it.
+ {`(define build (lambda (n acc) (if (== n 0) acc (build (- n 1) (cons n acc)))))
+ (define len (lambda (l acc) (if (== l ()) acc (len (cdr l) (+ acc 1)))))
+ (len (build 100000 ()) 0)`, "100000"},
+ })
+}
+
+// Recursion that is not in tail position still nests, but moderate depths
+// work. The pending (+ 1 ...) at each level is real work the program needs
+// the stack for.
+func TestNonTailRecursion(t *testing.T) {
+ runEvalCases(t, []evalCase{
+ {`(define len (lambda (l) (if (== l ()) 0 (+ 1 (len (cdr l))))))
+ (define build (lambda (n acc) (if (== n 0) acc (build (- n 1) (cons n acc)))))
+ (len (build 10000 ()))`, "10000"},
+ })
+}