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