feat: add De Bruijn index reduction engine
Closes #26 - Added -i flag to select interpreter (lambda or debruijn) - Created debruijn package with Expression interface - Variable contains index and optional label - Abstraction contains only body (no parameter) - Application structure remains similar - Implemented De Bruijn reduction without variable renaming - Shift operation handles index adjustments - Substitute replaces by index instead of name - Abstracted Engine into interface with two implementations - LambdaEngine: original named variable engine - DeBruijnEngine: new index-based engine - Added conversion functions between representations - LambdaToDeBruijn: converts named to indexed - DeBruijnToLambda: converts indexed back to named - SaccharineToDeBruijn: direct saccharine to De Bruijn - Updated main to switch engines based on -i flag - All test samples pass with both engines Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
This commit is contained in:
36
internal/engine/debruijn_engine.go
Normal file
36
internal/engine/debruijn_engine.go
Normal file
@@ -0,0 +1,36 @@
|
||||
package engine
|
||||
|
||||
import (
|
||||
"git.maximhutz.com/max/lambda/internal/config"
|
||||
"git.maximhutz.com/max/lambda/pkg/debruijn"
|
||||
"git.maximhutz.com/max/lambda/pkg/emitter"
|
||||
)
|
||||
|
||||
// A process for reducing one λ-expression using De Bruijn indices.
|
||||
type DeBruijnEngine struct {
|
||||
Config *config.Config
|
||||
Expression *debruijn.Expression
|
||||
emitter.Emitter
|
||||
}
|
||||
|
||||
// NewDeBruijnEngine creates a new De Bruijn engine.
|
||||
func NewDeBruijnEngine(config *config.Config, expression interface{}) *DeBruijnEngine {
|
||||
expr := expression.(*debruijn.Expression)
|
||||
return &DeBruijnEngine{Config: config, Expression: expr}
|
||||
}
|
||||
|
||||
// Run begins the reduction process.
|
||||
func (e *DeBruijnEngine) Run() {
|
||||
e.Emit("start")
|
||||
|
||||
debruijn.ReduceAll(e.Expression, func() {
|
||||
e.Emit("step")
|
||||
})
|
||||
|
||||
e.Emit("end")
|
||||
}
|
||||
|
||||
// GetResult returns the stringified result.
|
||||
func (e *DeBruijnEngine) GetResult() string {
|
||||
return debruijn.Stringify(*e.Expression)
|
||||
}
|
||||
Reference in New Issue
Block a user