fix: update main and tests for new Reducer API
Update call sites to match the new Reducer interface where the expression is passed to the constructor and Reduce() takes no arguments.
This commit is contained in:
@@ -33,8 +33,8 @@ func main() {
|
||||
compiled := convert.SaccharineToLambda(ast)
|
||||
logger.Info("compiled λ expression", "tree", compiled.String())
|
||||
|
||||
// Create reducer.
|
||||
reducer := lambda.NewNormalOrderReducer()
|
||||
// Create reducer with the compiled expression.
|
||||
reducer := lambda.NewNormalOrderReducer(compiled)
|
||||
|
||||
// If the user selected to track CPU performance, attach a profiler.
|
||||
if options.Profile != "" {
|
||||
@@ -58,7 +58,7 @@ func main() {
|
||||
}
|
||||
|
||||
// Run reduction.
|
||||
reducer.Reduce(compiled)
|
||||
reducer.Reduce()
|
||||
|
||||
// Return the final reduced result.
|
||||
result := reducer.Expression().String()
|
||||
|
||||
@@ -30,8 +30,8 @@ func runSample(samplePath string) (string, error) {
|
||||
compiled := convert.SaccharineToLambda(ast)
|
||||
|
||||
// Create and run the reducer.
|
||||
reducer := lambda.NewNormalOrderReducer()
|
||||
reducer.Reduce(compiled)
|
||||
reducer := lambda.NewNormalOrderReducer(compiled)
|
||||
reducer.Reduce()
|
||||
|
||||
return reducer.Expression().String() + "\n", nil
|
||||
}
|
||||
|
||||
@@ -6,20 +6,18 @@ import (
|
||||
"git.maximhutz.com/max/lambda/pkg/reducer"
|
||||
)
|
||||
|
||||
// Ensure NormalOrderReducer implements reducer.Reducer.
|
||||
var _ reducer.Reducer = (*NormalOrderReducer)(nil)
|
||||
|
||||
// NormalOrderReducer implements normal order (leftmost-outermost) reduction
|
||||
// for lambda calculus expressions.
|
||||
type NormalOrderReducer struct {
|
||||
emitter.BaseEmitter[reducer.Event]
|
||||
expression expr.Expression
|
||||
expression Expression
|
||||
}
|
||||
|
||||
// NewNormalOrderReducer creates a new normal order reducer.
|
||||
func NewNormalOrderReducer() *NormalOrderReducer {
|
||||
func NewNormalOrderReducer(expression Expression) *NormalOrderReducer {
|
||||
return &NormalOrderReducer{
|
||||
BaseEmitter: *emitter.New[reducer.Event](),
|
||||
expression: expression,
|
||||
}
|
||||
}
|
||||
|
||||
@@ -30,23 +28,22 @@ func (r *NormalOrderReducer) Expression() expr.Expression {
|
||||
|
||||
// Reduce performs normal order reduction on a lambda expression.
|
||||
// The expression must be a lambda.Expression; other types are returned unchanged.
|
||||
func (r *NormalOrderReducer) Reduce(e expr.Expression) expr.Expression {
|
||||
r.expression = e
|
||||
|
||||
lambdaExpr, ok := e.(Expression)
|
||||
if !ok {
|
||||
return e
|
||||
}
|
||||
|
||||
func (r *NormalOrderReducer) Reduce() {
|
||||
r.Emit(reducer.StartEvent)
|
||||
it := NewIterator(&r.expression)
|
||||
|
||||
ReduceAll(&lambdaExpr, func() {
|
||||
r.expression = lambdaExpr
|
||||
for !it.Done() {
|
||||
if fn, arg, ok := IsViable(it.Current()); !ok {
|
||||
it.Next()
|
||||
} else {
|
||||
it.Swap(Substitute(fn.body, fn.parameter, arg))
|
||||
r.Emit(reducer.StepEvent)
|
||||
})
|
||||
|
||||
r.expression = lambdaExpr
|
||||
r.Emit(reducer.StopEvent)
|
||||
|
||||
return lambdaExpr
|
||||
if _, _, ok := IsViable(it.Parent()); ok {
|
||||
it.Back()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
r.Emit(reducer.StopEvent)
|
||||
}
|
||||
|
||||
@@ -20,7 +20,7 @@ type Reducer interface {
|
||||
// Emits StartEvent before reduction, StepEvent after each step, and
|
||||
// StopEvent after completion.
|
||||
// Returns the final reduced expression.
|
||||
Reduce(e expr.Expression) expr.Expression
|
||||
Reduce()
|
||||
|
||||
// Expression returns the current expression state.
|
||||
Expression() expr.Expression
|
||||
|
||||
Reference in New Issue
Block a user