refactor: extract abstract Expression interface #30
@@ -8,7 +8,6 @@ import (
|
|||||||
"git.maximhutz.com/max/lambda/internal/engine"
|
"git.maximhutz.com/max/lambda/internal/engine"
|
||||||
"git.maximhutz.com/max/lambda/internal/plugins"
|
"git.maximhutz.com/max/lambda/internal/plugins"
|
||||||
"git.maximhutz.com/max/lambda/pkg/convert"
|
"git.maximhutz.com/max/lambda/pkg/convert"
|
||||||
"git.maximhutz.com/max/lambda/pkg/lambda"
|
|
||||||
"git.maximhutz.com/max/lambda/pkg/saccharine"
|
"git.maximhutz.com/max/lambda/pkg/saccharine"
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -32,7 +31,7 @@ func main() {
|
|||||||
|
|
||||||
// Compile expression to lambda calculus.
|
// Compile expression to lambda calculus.
|
||||||
compiled := convert.SaccharineToLambda(ast)
|
compiled := convert.SaccharineToLambda(ast)
|
||||||
logger.Info("compiled λ expression", "tree", lambda.Stringify(compiled))
|
logger.Info("compiled λ expression", "tree", compiled.String())
|
||||||
|
|
||||||
// Create reduction engine.
|
// Create reduction engine.
|
||||||
process := engine.New(options, &compiled)
|
process := engine.New(options, &compiled)
|
||||||
@@ -62,7 +61,7 @@ func main() {
|
|||||||
process.Run()
|
process.Run()
|
||||||
|
|
||||||
// Return the final reduced result.
|
// Return the final reduced result.
|
||||||
result := lambda.Stringify(compiled)
|
result := (*process.Expression).String()
|
||||||
err = options.Destination.Write(result)
|
err = options.Destination.Write(result)
|
||||||
cli.HandleError(err)
|
cli.HandleError(err)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,5 +1,5 @@
|
|||||||
// Package "engine" provides an extensible interface for users to interfact with
|
// Package "engine" provides an extensible interface for users to interact with
|
||||||
// λ-calculus.
|
// λ-calculus and other expression evaluation modes.
|
||||||
package engine
|
package engine
|
||||||
|
|
||||||
import (
|
import (
|
||||||
@@ -8,7 +8,7 @@ import (
|
|||||||
"git.maximhutz.com/max/lambda/pkg/lambda"
|
"git.maximhutz.com/max/lambda/pkg/lambda"
|
||||||
)
|
)
|
||||||
|
|
||||||
// A process for reducing one λ-expression.
|
// A process for reducing one expression.
|
||||||
type Engine struct {
|
type Engine struct {
|
||||||
Config *config.Config
|
Config *config.Config
|
||||||
Expression *lambda.Expression
|
Expression *lambda.Expression
|
||||||
@@ -25,7 +25,7 @@ func New(config *config.Config, expression *lambda.Expression) *Engine {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Begin the reduction process.
|
// Begin the reduction process.
|
||||||
func (e Engine) Run() {
|
func (e *Engine) Run() {
|
||||||
e.Emit(StartEvent)
|
e.Emit(StartEvent)
|
||||||
|
|
||||||
lambda.ReduceAll(e.Expression, func() {
|
lambda.ReduceAll(e.Expression, func() {
|
||||||
|
|||||||
@@ -4,7 +4,6 @@ import (
|
|||||||
"log/slog"
|
"log/slog"
|
||||||
|
|
||||||
"git.maximhutz.com/max/lambda/internal/engine"
|
"git.maximhutz.com/max/lambda/internal/engine"
|
||||||
"git.maximhutz.com/max/lambda/pkg/lambda"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
type Logs struct {
|
type Logs struct {
|
||||||
@@ -20,6 +19,5 @@ func NewLogs(logger *slog.Logger, process *engine.Engine) *Logs {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (t *Logs) Step() {
|
func (t *Logs) Step() {
|
||||||
stringified := lambda.Stringify(*t.process.Expression)
|
t.logger.Info("reduction", "tree", (*t.process.Expression).String())
|
||||||
t.logger.Info("reduction", "tree", stringified)
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -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.
|
// reduction, and present a thorough explanation to the user for each step.
|
||||||
package plugins
|
package plugins
|
||||||
|
|
||||||
@@ -6,10 +6,9 @@ import (
|
|||||||
"fmt"
|
"fmt"
|
||||||
|
|
||||||
"git.maximhutz.com/max/lambda/internal/engine"
|
"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 {
|
type Explanation struct {
|
||||||
process *engine.Engine
|
process *engine.Engine
|
||||||
}
|
}
|
||||||
@@ -24,9 +23,9 @@ func NewExplanation(process *engine.Engine) *Explanation {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (t *Explanation) Start() {
|
func (t *Explanation) Start() {
|
||||||
fmt.Println(lambda.Stringify(*t.process.Expression))
|
fmt.Println((*t.process.Expression).String())
|
||||||
}
|
}
|
||||||
|
|
||||||
func (t *Explanation) Step() {
|
func (t *Explanation) Step() {
|
||||||
fmt.Println(" =", lambda.Stringify(*t.process.Expression))
|
fmt.Println(" =", (*t.process.Expression).String())
|
||||||
}
|
}
|
||||||
|
|||||||
11
pkg/expr/expr.go
Normal file
11
pkg/expr/expr.go
Normal file
@@ -0,0 +1,11 @@
|
|||||||
|
// Package expr provides the abstract Expression interface for all evaluatable
|
||||||
|
// expression types in the lambda interpreter.
|
||||||
|
package expr
|
||||||
|
|
||||||
|
// Expression is the base interface for all evaluatable expression types.
|
||||||
|
// Different evaluation modes (lambda calculus, SKI combinators, typed lambda
|
||||||
|
// calculus, etc.) implement this interface with their own concrete types.
|
||||||
|
type Expression interface {
|
||||||
|
// String returns a human-readable representation of the expression.
|
||||||
|
String() string
|
||||||
|
}
|
||||||
@@ -1,6 +1,11 @@
|
|||||||
package lambda
|
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 {
|
type Expression interface {
|
||||||
|
expr.Expression
|
||||||
Accept(Visitor)
|
Accept(Visitor)
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -23,6 +28,10 @@ func (a *Abstraction) Accept(v Visitor) {
|
|||||||
v.VisitAbstraction(a)
|
v.VisitAbstraction(a)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (a *Abstraction) String() string {
|
||||||
|
return Stringify(a)
|
||||||
|
}
|
||||||
|
|
||||||
func NewAbstraction(parameter string, body Expression) *Abstraction {
|
func NewAbstraction(parameter string, body Expression) *Abstraction {
|
||||||
return &Abstraction{parameter: parameter, body: body}
|
return &Abstraction{parameter: parameter, body: body}
|
||||||
}
|
}
|
||||||
@@ -46,6 +55,10 @@ func (a *Application) Accept(v Visitor) {
|
|||||||
v.VisitApplication(a)
|
v.VisitApplication(a)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (a *Application) String() string {
|
||||||
|
return Stringify(a)
|
||||||
|
}
|
||||||
|
|
||||||
func NewApplication(abstraction Expression, argument Expression) *Application {
|
func NewApplication(abstraction Expression, argument Expression) *Application {
|
||||||
return &Application{abstraction: abstraction, argument: argument}
|
return &Application{abstraction: abstraction, argument: argument}
|
||||||
}
|
}
|
||||||
@@ -64,6 +77,10 @@ func (v *Variable) Accept(visitor Visitor) {
|
|||||||
visitor.VisitVariable(v)
|
visitor.VisitVariable(v)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (v *Variable) String() string {
|
||||||
|
return Stringify(v)
|
||||||
|
}
|
||||||
|
|
||||||
func NewVariable(name string) *Variable {
|
func NewVariable(name string) *Variable {
|
||||||
return &Variable{value: name}
|
return &Variable{value: name}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user