Add a new interpreter option (-i debruijn) that uses De Bruijn indices for variable representation, eliminating the need for variable renaming during substitution. - Add -i flag to select interpreter (lambda or debruijn) - Create debruijn package with Expression types (Variable with index, Abstraction without parameter, Application) - Implement shift and substitute operations for De Bruijn indices - Add conversion functions between lambda and De Bruijn representations - Update CLI to support switching between interpreters - Add De Bruijn tests to verify all samples pass Closes #26
36 lines
749 B
Go
36 lines
749 B
Go
package debruijn
|
|
|
|
import (
|
|
"strconv"
|
|
"strings"
|
|
)
|
|
|
|
type stringifyVisitor struct {
|
|
builder strings.Builder
|
|
}
|
|
|
|
func (v *stringifyVisitor) VisitVariable(a *Variable) {
|
|
v.builder.WriteString(strconv.Itoa(a.index))
|
|
}
|
|
|
|
func (v *stringifyVisitor) VisitAbstraction(f *Abstraction) {
|
|
v.builder.WriteRune('\\')
|
|
v.builder.WriteRune('.')
|
|
f.body.Accept(v)
|
|
}
|
|
|
|
func (v *stringifyVisitor) VisitApplication(c *Application) {
|
|
v.builder.WriteRune('(')
|
|
c.abstraction.Accept(v)
|
|
v.builder.WriteRune(' ')
|
|
c.argument.Accept(v)
|
|
v.builder.WriteRune(')')
|
|
}
|
|
|
|
// Stringify converts a De Bruijn expression to its string representation.
|
|
func Stringify(e Expression) string {
|
|
b := &stringifyVisitor{builder: strings.Builder{}}
|
|
e.Accept(b)
|
|
return b.builder.String()
|
|
}
|