Files
lambda/pkg/lambda/expression.go
2026-01-30 17:54:47 -05:00

101 lines
2.4 KiB
Go

package lambda
import (
"fmt"
"git.maximhutz.com/max/lambda/pkg/set"
)
// Expression is the interface for all lambda calculus expression types.
// It embeds the general expr.Expression interface for cross-mode compatibility.
type Expression interface {
fmt.Stringer
// Substitute replaces all free occurrences of the target variable with the
// replacement expression. Alpha-renaming is performed automatically to
// avoid variable capture.
Substitute(target string, replacement Expression) Expression
// 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.
GetFree() set.Set[string]
// Rename replaces all occurrences of the target variable name with the new name.
Rename(target string, newName string) Expression
// IsFree returns true if the variable name n occurs free in the expression.
// This function does not mutate the input expression.
IsFree(n string) bool
}
/** ------------------------------------------------------------------------- */
type Abstraction struct {
parameter string
body Expression
}
var _ Expression = Abstraction{}
func (a Abstraction) Parameter() string {
return a.parameter
}
func (a Abstraction) Body() Expression {
return a.body
}
func (a Abstraction) String() string {
return "\\" + a.parameter + "." + a.body.String()
}
func NewAbstraction(parameter string, body Expression) Abstraction {
return Abstraction{parameter, body}
}
/** ------------------------------------------------------------------------- */
type Application struct {
abstraction Expression
argument Expression
}
var _ Expression = Application{}
func (a Application) Abstraction() Expression {
return a.abstraction
}
func (a Application) Argument() Expression {
return a.argument
}
func (a Application) String() string {
return "(" + a.abstraction.String() + " " + a.argument.String() + ")"
}
func NewApplication(abstraction Expression, argument Expression) Application {
return Application{abstraction, argument}
}
/** ------------------------------------------------------------------------- */
type Variable struct {
name string
}
var _ Expression = Variable{}
func (v Variable) Name() string {
return v.name
}
func (v Variable) String() string {
return v.name
}
func NewVariable(name string) Variable {
return Variable{name}
}