## 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>
95 lines
1.9 KiB
Go
95 lines
1.9 KiB
Go
package lambda
|
|
|
|
import "git.maximhutz.com/max/lambda/pkg/expr"
|
|
|
|
// Expression is the interface for all lambda calculus expression types.
|
|
// It embeds the general expr.Expression interface for cross-mode compatibility.
|
|
type Expression interface {
|
|
expr.Expression
|
|
Accept(Visitor)
|
|
}
|
|
|
|
/** ------------------------------------------------------------------------- */
|
|
|
|
type Abstraction struct {
|
|
parameter string
|
|
body Expression
|
|
}
|
|
|
|
func (a *Abstraction) Parameter() string {
|
|
return a.parameter
|
|
}
|
|
|
|
func (a *Abstraction) Body() Expression {
|
|
return a.body
|
|
}
|
|
|
|
func (a *Abstraction) Accept(v Visitor) {
|
|
v.VisitAbstraction(a)
|
|
}
|
|
|
|
func (a *Abstraction) String() string {
|
|
return Stringify(a)
|
|
}
|
|
|
|
func NewAbstraction(parameter string, body Expression) *Abstraction {
|
|
return &Abstraction{parameter: parameter, body: body}
|
|
}
|
|
|
|
/** ------------------------------------------------------------------------- */
|
|
|
|
type Application struct {
|
|
abstraction Expression
|
|
argument Expression
|
|
}
|
|
|
|
func (a *Application) Abstraction() Expression {
|
|
return a.abstraction
|
|
}
|
|
|
|
func (a *Application) Argument() Expression {
|
|
return a.argument
|
|
}
|
|
|
|
func (a *Application) Accept(v Visitor) {
|
|
v.VisitApplication(a)
|
|
}
|
|
|
|
func (a *Application) String() string {
|
|
return Stringify(a)
|
|
}
|
|
|
|
func NewApplication(abstraction Expression, argument Expression) *Application {
|
|
return &Application{abstraction: abstraction, argument: argument}
|
|
}
|
|
|
|
/** ------------------------------------------------------------------------- */
|
|
|
|
type Variable struct {
|
|
value string
|
|
}
|
|
|
|
func (v *Variable) Value() string {
|
|
return v.value
|
|
}
|
|
|
|
func (v *Variable) Accept(visitor Visitor) {
|
|
visitor.VisitVariable(v)
|
|
}
|
|
|
|
func (v *Variable) String() string {
|
|
return Stringify(v)
|
|
}
|
|
|
|
func NewVariable(name string) *Variable {
|
|
return &Variable{value: name}
|
|
}
|
|
|
|
/** ------------------------------------------------------------------------- */
|
|
|
|
type Visitor interface {
|
|
VisitAbstraction(*Abstraction)
|
|
VisitApplication(*Application)
|
|
VisitVariable(*Variable)
|
|
}
|