perf: implement structural sharing for expression trees #10
@@ -30,12 +30,12 @@ func NewAbstraction(parameter string, body Expression) *Abstraction {
|
||||
/** ------------------------------------------------------------------------- */
|
||||
|
||||
type Application struct {
|
||||
function Expression
|
||||
argument Expression
|
||||
abstraction Expression
|
||||
argument Expression
|
||||
}
|
||||
|
||||
func (a *Application) Function() Expression {
|
||||
return a.function
|
||||
func (a *Application) Abstraction() Expression {
|
||||
return a.abstraction
|
||||
}
|
||||
|
||||
func (a *Application) Argument() Expression {
|
||||
@@ -46,8 +46,8 @@ func (a *Application) Accept(v Visitor) {
|
||||
v.VisitApplication(a)
|
||||
}
|
||||
|
||||
func NewApplication(function Expression, argument Expression) *Application {
|
||||
return &Application{function: function, argument: argument}
|
||||
func NewApplication(abstraction Expression, argument Expression) *Application {
|
||||
return &Application{abstraction: abstraction, argument: argument}
|
||||
}
|
||||
|
||||
/** ------------------------------------------------------------------------- */
|
||||
|
||||
@@ -11,7 +11,7 @@ func GetFreeVariables(e Expression) *set.Set[string] {
|
||||
vars.Remove(e.parameter)
|
||||
return vars
|
||||
case *Application:
|
||||
vars := GetFreeVariables(e.function)
|
||||
vars := GetFreeVariables(e.abstraction)
|
||||
vars.Merge(GetFreeVariables(e.argument))
|
||||
return vars
|
||||
default:
|
||||
|
||||
@@ -7,7 +7,7 @@ func IsFreeVariable(n string, e Expression) bool {
|
||||
case *Abstraction:
|
||||
return e.parameter != n && IsFreeVariable(n, e.body)
|
||||
case *Application:
|
||||
return IsFreeVariable(n, e.function) || IsFreeVariable(n, e.argument)
|
||||
return IsFreeVariable(n, e.abstraction) || IsFreeVariable(n, e.argument)
|
||||
default:
|
||||
return false
|
||||
}
|
||||
|
||||
@@ -12,14 +12,14 @@ func ReduceOnce(e *Expression) bool {
|
||||
case *Abstraction:
|
||||
stack.Push(&typed.body)
|
||||
case *Application:
|
||||
if fn, fnOk := typed.function.(*Abstraction); fnOk {
|
||||
if fn, fnOk := typed.abstraction.(*Abstraction); fnOk {
|
||||
reduced := Substitute(fn.body, fn.parameter, typed.argument)
|
||||
*top = reduced
|
||||
return true
|
||||
}
|
||||
|
||||
stack.Push(&typed.argument)
|
||||
stack.Push(&typed.function)
|
||||
stack.Push(&typed.abstraction)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -23,14 +23,14 @@ func Rename(expr Expression, target string, newName string) Expression {
|
||||
return NewAbstraction(newParam, newBody)
|
||||
|
||||
case *Application:
|
||||
newFunc := Rename(e.function, target, newName)
|
||||
newAbs := Rename(e.abstraction, target, newName)
|
||||
newArg := Rename(e.argument, target, newName)
|
||||
|
||||
if newFunc == e.function && newArg == e.argument {
|
||||
if newAbs == e.abstraction && newArg == e.argument {
|
||||
return e
|
||||
}
|
||||
|
||||
return NewApplication(newFunc, newArg)
|
||||
return NewApplication(newAbs, newArg)
|
||||
|
||||
default:
|
||||
return expr
|
||||
|
||||
@@ -19,7 +19,7 @@ func (v *stringifyVisitor) VisitAbstraction(f *Abstraction) {
|
||||
|
||||
func (v *stringifyVisitor) VisitApplication(c *Application) {
|
||||
v.builder.WriteRune('(')
|
||||
c.function.Accept(v)
|
||||
c.abstraction.Accept(v)
|
||||
v.builder.WriteRune(' ')
|
||||
c.argument.Accept(v)
|
||||
v.builder.WriteRune(')')
|
||||
|
||||
@@ -31,14 +31,14 @@ func Substitute(expr Expression, target string, replacement Expression) Expressi
|
||||
return NewAbstraction(param, newBody)
|
||||
|
||||
case *Application:
|
||||
newFunc := Substitute(e.function, target, replacement)
|
||||
newAbs := Substitute(e.abstraction, target, replacement)
|
||||
newArg := Substitute(e.argument, target, replacement)
|
||||
|
||||
if newFunc == e.function && newArg == e.argument {
|
||||
if newAbs == e.abstraction && newArg == e.argument {
|
||||
return e
|
||||
}
|
||||
|
||||
return NewApplication(newFunc, newArg)
|
||||
return NewApplication(newAbs, newArg)
|
||||
|
||||
default:
|
||||
return expr
|
||||
|
||||
Reference in New Issue
Block a user