feat: new engine format

This commit is contained in:
2026-02-07 00:12:50 -05:00
parent 8b80cea32b
commit 1d94fa70ff
11 changed files with 45 additions and 100 deletions

View File

@@ -0,0 +1,39 @@
package normalorder
import "git.maximhutz.com/max/lambda/pkg/lambda"
// ReduceOnce attempts to apply a single reduction to a lambda expression.
// It returns (1) the final expression (reduced, or not), and (2) whether or not
// a reduction was applied.
//
// If a reduction is not applied, it returns the original expression.
func ReduceOnce(e lambda.Expression) (lambda.Expression, bool) {
switch e := e.(type) {
case lambda.Abstraction:
body, reduced := ReduceOnce(e.Body())
if reduced {
return lambda.NewAbstraction(e.Parameter(), body), true
}
return e, false
case lambda.Application:
if fn, fnOk := e.Abstraction().(lambda.Abstraction); fnOk {
return fn.Body().Substitute(fn.Parameter(), e.Argument()), true
}
abs, reduced := ReduceOnce(e.Abstraction())
if reduced {
return lambda.NewApplication(abs, e.Argument()), true
}
arg, reduced := ReduceOnce(e.Argument())
if reduced {
return lambda.NewApplication(e.Abstraction(), arg), true
}
return e, false
default:
return e, false
}
}