Files
lambda/pkg/lambda/get_free_variables.go
2026-02-09 20:10:40 -05:00

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))
}
}