28 lines
643 B
Go
28 lines
643 B
Go
package lambda
|
|
|
|
import (
|
|
"fmt"
|
|
|
|
"git.maximhutz.com/max/lambda/pkg/set"
|
|
)
|
|
|
|
// GetFree returns the set of all free variable names in the expression.
|
|
// This function does not mutate the input expression.
|
|
// The returned set is newly allocated and can be modified by the caller.
|
|
func GetFree(e Expression) set.Set[string] {
|
|
switch e := e.(type) {
|
|
case Variable:
|
|
return set.New(e.Name)
|
|
case Abstraction:
|
|
vars := GetFree(e.Body)
|
|
vars.Remove(e.Parameter)
|
|
return vars
|
|
case Application:
|
|
vars := GetFree(e.Abstraction)
|
|
vars.Merge(GetFree(e.Argument))
|
|
return vars
|
|
default:
|
|
panic(fmt.Errorf("unknown expression type: %v", e))
|
|
}
|
|
}
|