28 lines
520 B
Go
28 lines
520 B
Go
package lambda
|
|
|
|
import "git.maximhutz.com/max/lambda/pkg/fifo"
|
|
|
|
func ReduceOnce(e *Expression) bool {
|
|
stack := fifo.New(e)
|
|
|
|
for !stack.Empty() {
|
|
top := stack.MustPop()
|
|
|
|
switch typed := (*top).(type) {
|
|
case *Abstraction:
|
|
stack.Push(&typed.Body)
|
|
case *Application:
|
|
if fn, fnOk := typed.Abstraction.(*Abstraction); fnOk {
|
|
Substitute(&fn.Body, fn.Parameter, typed.Argument)
|
|
*top = fn.Body
|
|
return true
|
|
}
|
|
|
|
stack.Push(&typed.Argument)
|
|
stack.Push(&typed.Abstraction)
|
|
}
|
|
}
|
|
|
|
return false
|
|
}
|