feat: improve reduction algorithm with LIFO-based iterator #15

Merged
mvhutz merged 7 commits from feat/lifo-improvements into main 2026-01-12 02:16:07 +00:00
2 changed files with 60 additions and 1 deletions
Showing only changes of commit da9ee0bcb0 - Show all commits

2
.gitignore vendored
View File

@@ -3,7 +3,7 @@
# https://github.com/github/gitignore/blob/main/community/Golang/Go.AllowList.gitignore
#
# Binaries for programs and plugins
lambda
/lambda
*.exe
*.exe~
*.dll

59
pkg/lambda/iterator.go Normal file
View File

@@ -0,0 +1,59 @@
package lambda
type Iterator struct {
trace []*Expression
}
func NewIterator(expr *Expression) *Iterator {
return &Iterator{
trace: []*Expression{expr},
}
}
func (i *Iterator) Current() *Expression {
if len(i.trace) < 1 {
return nil
}
return i.trace[len(i.trace)-1]
}
func (i *Iterator) Parent() *Expression {
if len(i.trace) < 2 {
return nil
}
return i.trace[len(i.trace)-2]
}
func (i *Iterator) Next() {
switch typed := (*i.Current()).(type) {
case *Abstraction:
i.trace = append(i.trace, &typed.body)
case *Application:
i.trace = append(i.trace, &typed.abstraction)
case *Variable:
for len(i.trace) > 1 {
if app, ok := (*i.Parent()).(*Application); ok {
if app.abstraction == *i.Current() {
i.Back()
i.trace = append(i.trace, &app.argument)
return
}
}
i.Back()
}
i.trace = []*Expression{}
}
}
func (i *Iterator) Back() bool {
if len(i.trace) == 0 {
return false
}
i.trace = i.trace[:len(i.trace)-1]
return true
}