refactor: extract abstract Expression interface (#30)
## Description The codebase currently couples the engine and plugins directly to `lambda.Expression`. This PR introduces an abstract `expr.Expression` interface to enable future support for multiple evaluation modes. - Add `pkg/expr/expr.go` with an `Expression` interface requiring a `String()` method. - Update `lambda.Expression` to embed `expr.Expression`. - Add `String()` method to `Abstraction`, `Application`, and `Variable` types. - Update plugins to use `String()` instead of `lambda.Stringify()`. ### Decisions - The `expr.Expression` interface is minimal (only `String()`) to avoid over-constraining future expression types. - The engine still stores `*lambda.Expression` directly rather than `expr.Expression`, because Go's interface semantics require pointer indirection for in-place mutation during reduction. - Future evaluation modes will implement their own concrete types satisfying `expr.Expression`. ## Benefits - Establishes a foundation for supporting multiple evaluation modes (SKI combinators, typed lambda calculus, etc.). - Plugins now use the abstract `String()` method, making them more decoupled from the lambda-specific implementation. - Prepares the codebase for a Reducer interface abstraction in a future PR. ## Checklist - [x] Code follows conventional commit format. - [x] Branch follows naming convention (`<type>/<description>`). - [x] Tests pass (if applicable). - [ ] Documentation updated (if applicable). Reviewed-on: #30 Co-authored-by: M.V. Hutz <git@maximhutz.me> Co-committed-by: M.V. Hutz <git@maximhutz.me>
This commit was merged in pull request #30.
This commit is contained in:
@@ -1,5 +1,5 @@
|
||||
// Package "engine" provides an extensible interface for users to interfact with
|
||||
// λ-calculus.
|
||||
// Package "engine" provides an extensible interface for users to interact with
|
||||
// λ-calculus and other expression evaluation modes.
|
||||
package engine
|
||||
|
||||
import (
|
||||
@@ -8,7 +8,7 @@ import (
|
||||
"git.maximhutz.com/max/lambda/pkg/lambda"
|
||||
)
|
||||
|
||||
// A process for reducing one λ-expression.
|
||||
// A process for reducing one expression.
|
||||
type Engine struct {
|
||||
Config *config.Config
|
||||
Expression *lambda.Expression
|
||||
@@ -25,7 +25,7 @@ func New(config *config.Config, expression *lambda.Expression) *Engine {
|
||||
}
|
||||
|
||||
// Begin the reduction process.
|
||||
func (e Engine) Run() {
|
||||
func (e *Engine) Run() {
|
||||
e.Emit(StartEvent)
|
||||
|
||||
lambda.ReduceAll(e.Expression, func() {
|
||||
|
||||
@@ -4,7 +4,6 @@ import (
|
||||
"log/slog"
|
||||
|
||||
"git.maximhutz.com/max/lambda/internal/engine"
|
||||
"git.maximhutz.com/max/lambda/pkg/lambda"
|
||||
)
|
||||
|
||||
type Logs struct {
|
||||
@@ -20,6 +19,5 @@ func NewLogs(logger *slog.Logger, process *engine.Engine) *Logs {
|
||||
}
|
||||
|
||||
func (t *Logs) Step() {
|
||||
stringified := lambda.Stringify(*t.process.Expression)
|
||||
t.logger.Info("reduction", "tree", stringified)
|
||||
t.logger.Info("reduction", "tree", (*t.process.Expression).String())
|
||||
}
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
// Package "explanation" provides a observer to gather the reasoning during the
|
||||
// Package "explanation" provides an observer to gather the reasoning during the
|
||||
// reduction, and present a thorough explanation to the user for each step.
|
||||
package plugins
|
||||
|
||||
@@ -6,10 +6,9 @@ import (
|
||||
"fmt"
|
||||
|
||||
"git.maximhutz.com/max/lambda/internal/engine"
|
||||
"git.maximhutz.com/max/lambda/pkg/lambda"
|
||||
)
|
||||
|
||||
// Track the reductions made by a reduction proess.
|
||||
// Track the reductions made by a reduction process.
|
||||
type Explanation struct {
|
||||
process *engine.Engine
|
||||
}
|
||||
@@ -24,9 +23,9 @@ func NewExplanation(process *engine.Engine) *Explanation {
|
||||
}
|
||||
|
||||
func (t *Explanation) Start() {
|
||||
fmt.Println(lambda.Stringify(*t.process.Expression))
|
||||
fmt.Println((*t.process.Expression).String())
|
||||
}
|
||||
|
||||
func (t *Explanation) Step() {
|
||||
fmt.Println(" =", lambda.Stringify(*t.process.Expression))
|
||||
fmt.Println(" =", (*t.process.Expression).String())
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user