feat: only compute all free variables during a-conversion
This commit is contained in:
BIN
pkg/.DS_Store
vendored
Normal file
BIN
pkg/.DS_Store
vendored
Normal file
Binary file not shown.
14
pkg/lambda/is_free_variable.go
Normal file
14
pkg/lambda/is_free_variable.go
Normal file
@@ -0,0 +1,14 @@
|
||||
package lambda
|
||||
|
||||
func IsFreeVariable(n string, e Expression) bool {
|
||||
switch e := e.(type) {
|
||||
case *Variable:
|
||||
return e.Value == n
|
||||
case *Abstraction:
|
||||
return e.Parameter != n && IsFreeVariable(n, e.Body)
|
||||
case *Application:
|
||||
return IsFreeVariable(n, e.Abstraction) || IsFreeVariable(n, e.Argument)
|
||||
default:
|
||||
return false
|
||||
}
|
||||
}
|
||||
@@ -11,12 +11,12 @@ func Substitute(e *Expression, target string, replacement Expression) {
|
||||
return
|
||||
}
|
||||
|
||||
replacementFreeVars := GetFreeVariables(replacement)
|
||||
if !replacementFreeVars.Has(typed.Parameter) {
|
||||
if !IsFreeVariable(typed.Parameter, replacement) {
|
||||
Substitute(&typed.Body, target, replacement)
|
||||
return
|
||||
}
|
||||
|
||||
replacementFreeVars := GetFreeVariables(replacement)
|
||||
used := GetFreeVariables(typed.Body)
|
||||
used.Merge(replacementFreeVars)
|
||||
freshVar := GenerateFreshName(used)
|
||||
|
||||
BIN
pkg/saccharine/.DS_Store
vendored
Normal file
BIN
pkg/saccharine/.DS_Store
vendored
Normal file
Binary file not shown.
Reference in New Issue
Block a user