- Rename pkg/interpreter to pkg/runtime - Move ReduceOnce to new pkg/normalorder package - Convert standalone functions (Substitute, Rename, GetFree, IsFree) to receiver methods on concrete expression types - Change Set from pointer receivers to value receivers - Update all references from interpreter to runtime terminology Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
36 lines
855 B
Go
36 lines
855 B
Go
package lambda
|
|
|
|
func (e Variable) Substitute(target string, replacement Expression) Expression {
|
|
if e.Name() == target {
|
|
return replacement
|
|
}
|
|
|
|
return e
|
|
}
|
|
|
|
func (e Abstraction) Substitute(target string, replacement Expression) Expression {
|
|
if e.Parameter() == target {
|
|
return e
|
|
}
|
|
|
|
body := e.Body()
|
|
param := e.Parameter()
|
|
if replacement.IsFree(param) {
|
|
freeVars := replacement.GetFree()
|
|
freeVars.Merge(body.GetFree())
|
|
freshVar := GenerateFreshName(freeVars)
|
|
body = body.Rename(param, freshVar)
|
|
param = freshVar
|
|
}
|
|
|
|
newBody := body.Substitute(target, replacement)
|
|
return NewAbstraction(param, newBody)
|
|
}
|
|
|
|
func (e Application) Substitute(target string, replacement Expression) Expression {
|
|
abs := e.Abstraction().Substitute(target, replacement)
|
|
arg := e.Argument().Substitute(target, replacement)
|
|
|
|
return NewApplication(abs, arg)
|
|
}
|