feat: improve reduction algorithm with LIFO-based iterator #15
2
.gitignore
vendored
2
.gitignore
vendored
@@ -3,7 +3,7 @@
|
|||||||
# https://github.com/github/gitignore/blob/main/community/Golang/Go.AllowList.gitignore
|
# https://github.com/github/gitignore/blob/main/community/Golang/Go.AllowList.gitignore
|
||||||
#
|
#
|
||||||
# Binaries for programs and plugins
|
# Binaries for programs and plugins
|
||||||
lambda
|
/lambda
|
||||||
*.exe
|
*.exe
|
||||||
*.exe~
|
*.exe~
|
||||||
*.dll
|
*.dll
|
||||||
|
|||||||
59
pkg/lambda/iterator.go
Normal file
59
pkg/lambda/iterator.go
Normal 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
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user