From d9639ecc2b6b01ed11387e564bd16a641162ea9a Mon Sep 17 00:00:00 2001 From: "M.V. Hutz" Date: Fri, 16 Jan 2026 19:09:27 -0500 Subject: [PATCH] style: moved isViable to reducer --- pkg/lambda/reduce.go | 30 ------------------------------ pkg/lambda/reducer.go | 16 ++++++++++++++-- 2 files changed, 14 insertions(+), 32 deletions(-) delete mode 100644 pkg/lambda/reduce.go diff --git a/pkg/lambda/reduce.go b/pkg/lambda/reduce.go deleted file mode 100644 index 7bcf50c..0000000 --- a/pkg/lambda/reduce.go +++ /dev/null @@ -1,30 +0,0 @@ -package lambda - -func IsViable(e *Expression) (*Abstraction, Expression, bool) { - if e == nil { - return nil, nil, false - } else if app, appOk := (*e).(*Application); !appOk { - return nil, nil, false - } else if fn, fnOk := app.abstraction.(*Abstraction); !fnOk { - return nil, nil, false - } else { - return fn, app.argument, true - } -} - -func ReduceAll(e *Expression, step func()) { - it := NewIterator(e) - - for !it.Done() { - if fn, arg, ok := IsViable(it.Current()); !ok { - it.Next() - } else { - it.Swap(Substitute(fn.body, fn.parameter, arg)) - step() - - if _, _, ok := IsViable(it.Parent()); ok { - it.Back() - } - } - } -} diff --git a/pkg/lambda/reducer.go b/pkg/lambda/reducer.go index 4c5ea19..4117096 100644 --- a/pkg/lambda/reducer.go +++ b/pkg/lambda/reducer.go @@ -26,6 +26,18 @@ func (r *NormalOrderReducer) Expression() expr.Expression { return r.expression } +func isViable(e *Expression) (*Abstraction, Expression, bool) { + if e == nil { + return nil, nil, false + } else if app, appOk := (*e).(*Application); !appOk { + return nil, nil, false + } else if fn, fnOk := app.abstraction.(*Abstraction); !fnOk { + return nil, nil, false + } else { + return fn, app.argument, true + } +} + // 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() { @@ -33,13 +45,13 @@ func (r *NormalOrderReducer) Reduce() { it := NewIterator(&r.expression) for !it.Done() { - if fn, arg, ok := IsViable(it.Current()); !ok { + if fn, arg, ok := isViable(it.Current()); !ok { it.Next() } else { it.Swap(Substitute(fn.body, fn.parameter, arg)) r.Emit(reducer.StepEvent) - if _, _, ok := IsViable(it.Parent()); ok { + if _, _, ok := isViable(it.Parent()); ok { it.Back() } }