refactor: remove unnecessary comments from structural sharing implementation
Remove verbose inline and doc comments added in the structural sharing PR. The code is self-explanatory and the comments were redundant. Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
This commit is contained in:
@@ -1,8 +1,6 @@
|
|||||||
package lambda
|
package lambda
|
||||||
|
|
||||||
// Expression represents an immutable lambda calculus expression.
|
// Expression represents an immutable lambda calculus expression.
|
||||||
// All expression types use structural sharing: operations return
|
|
||||||
// the same expression pointer when unchanged, reducing allocations.
|
|
||||||
type Expression interface {
|
type Expression interface {
|
||||||
Accept(Visitor)
|
Accept(Visitor)
|
||||||
Substitute(target string, replacement Expression) Expression
|
Substitute(target string, replacement Expression) Expression
|
||||||
@@ -12,7 +10,6 @@ type Expression interface {
|
|||||||
/** ------------------------------------------------------------------------- */
|
/** ------------------------------------------------------------------------- */
|
||||||
|
|
||||||
// Abstraction represents a lambda abstraction (λx. body).
|
// Abstraction represents a lambda abstraction (λx. body).
|
||||||
// Fields are unexported to enforce immutability.
|
|
||||||
type Abstraction struct {
|
type Abstraction struct {
|
||||||
parameter string
|
parameter string
|
||||||
body Expression
|
body Expression
|
||||||
@@ -37,7 +34,6 @@ func NewAbstraction(parameter string, body Expression) *Abstraction {
|
|||||||
/** ------------------------------------------------------------------------- */
|
/** ------------------------------------------------------------------------- */
|
||||||
|
|
||||||
// Application represents function application (f arg).
|
// Application represents function application (f arg).
|
||||||
// Fields are unexported to enforce immutability.
|
|
||||||
type Application struct {
|
type Application struct {
|
||||||
function Expression
|
function Expression
|
||||||
argument Expression
|
argument Expression
|
||||||
@@ -62,7 +58,6 @@ func NewApplication(function Expression, argument Expression) *Application {
|
|||||||
/** ------------------------------------------------------------------------- */
|
/** ------------------------------------------------------------------------- */
|
||||||
|
|
||||||
// Variable represents a variable reference.
|
// Variable represents a variable reference.
|
||||||
// Fields are unexported to enforce immutability.
|
|
||||||
type Variable struct {
|
type Variable struct {
|
||||||
value string
|
value string
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -13,7 +13,6 @@ func ReduceOnce(e *Expression) bool {
|
|||||||
stack.Push(&typed.body)
|
stack.Push(&typed.body)
|
||||||
case *Application:
|
case *Application:
|
||||||
if fn, fnOk := typed.function.(*Abstraction); fnOk {
|
if fn, fnOk := typed.function.(*Abstraction); fnOk {
|
||||||
// Perform beta reduction with structural sharing
|
|
||||||
reduced := fn.body.Substitute(fn.parameter, typed.argument)
|
reduced := fn.body.Substitute(fn.parameter, typed.argument)
|
||||||
*top = reduced
|
*top = reduced
|
||||||
return true
|
return true
|
||||||
|
|||||||
@@ -1,16 +1,14 @@
|
|||||||
package lambda
|
package lambda
|
||||||
|
|
||||||
// Rename replaces all occurrences of target variable with newName.
|
// Rename replaces all occurrences of target variable with newName.
|
||||||
// Uses structural sharing: returns the same expression pointer when unchanged.
|
|
||||||
func (v *Variable) Rename(target string, newName string) Expression {
|
func (v *Variable) Rename(target string, newName string) Expression {
|
||||||
if v.value == target {
|
if v.value == target {
|
||||||
return NewVariable(newName)
|
return NewVariable(newName)
|
||||||
}
|
}
|
||||||
return v // unchanged
|
return v
|
||||||
}
|
}
|
||||||
|
|
||||||
// Rename replaces all occurrences of target variable with newName.
|
// Rename replaces all occurrences of target variable with newName.
|
||||||
// Uses structural sharing: only allocates when something changes.
|
|
||||||
func (a *Abstraction) Rename(target string, newName string) Expression {
|
func (a *Abstraction) Rename(target string, newName string) Expression {
|
||||||
newParam := a.parameter
|
newParam := a.parameter
|
||||||
if a.parameter == target {
|
if a.parameter == target {
|
||||||
@@ -20,20 +18,19 @@ func (a *Abstraction) Rename(target string, newName string) Expression {
|
|||||||
newBody := a.body.Rename(target, newName)
|
newBody := a.body.Rename(target, newName)
|
||||||
|
|
||||||
if newParam == a.parameter && newBody == a.body {
|
if newParam == a.parameter && newBody == a.body {
|
||||||
return a // unchanged
|
return a
|
||||||
}
|
}
|
||||||
|
|
||||||
return NewAbstraction(newParam, newBody)
|
return NewAbstraction(newParam, newBody)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Rename replaces all occurrences of target variable with newName.
|
// Rename replaces all occurrences of target variable with newName.
|
||||||
// Uses structural sharing: only allocates when something changes.
|
|
||||||
func (a *Application) Rename(target string, newName string) Expression {
|
func (a *Application) Rename(target string, newName string) Expression {
|
||||||
newFunc := a.function.Rename(target, newName)
|
newFunc := a.function.Rename(target, newName)
|
||||||
newArg := a.argument.Rename(target, newName)
|
newArg := a.argument.Rename(target, newName)
|
||||||
|
|
||||||
if newFunc == a.function && newArg == a.argument {
|
if newFunc == a.function && newArg == a.argument {
|
||||||
return a // unchanged
|
return a
|
||||||
}
|
}
|
||||||
|
|
||||||
return NewApplication(newFunc, newArg)
|
return NewApplication(newFunc, newArg)
|
||||||
|
|||||||
@@ -1,26 +1,22 @@
|
|||||||
package lambda
|
package lambda
|
||||||
|
|
||||||
// Substitute replaces all free occurrences of target with replacement.
|
// Substitute replaces all free occurrences of target with replacement.
|
||||||
// Uses structural sharing: returns the same expression pointer when unchanged.
|
|
||||||
func (v *Variable) Substitute(target string, replacement Expression) Expression {
|
func (v *Variable) Substitute(target string, replacement Expression) Expression {
|
||||||
if v.value == target {
|
if v.value == target {
|
||||||
return replacement // NO COPY - share directly!
|
return replacement
|
||||||
}
|
}
|
||||||
return v // unchanged, return self
|
return v
|
||||||
}
|
}
|
||||||
|
|
||||||
// Substitute replaces all free occurrences of target with replacement.
|
// Substitute replaces all free occurrences of target with replacement.
|
||||||
// Uses structural sharing and performs alpha conversion to avoid variable capture.
|
|
||||||
func (a *Abstraction) Substitute(target string, replacement Expression) Expression {
|
func (a *Abstraction) Substitute(target string, replacement Expression) Expression {
|
||||||
if a.parameter == target {
|
if a.parameter == target {
|
||||||
return a // shadowed, return unchanged
|
return a
|
||||||
}
|
}
|
||||||
|
|
||||||
// Handle alpha conversion if needed to avoid variable capture
|
|
||||||
body := a.body
|
body := a.body
|
||||||
param := a.parameter
|
param := a.parameter
|
||||||
if IsFreeVariable(param, replacement) {
|
if IsFreeVariable(param, replacement) {
|
||||||
// Need to rename to avoid capture
|
|
||||||
freeVars := GetFreeVariables(replacement)
|
freeVars := GetFreeVariables(replacement)
|
||||||
freeVars.Merge(GetFreeVariables(body))
|
freeVars.Merge(GetFreeVariables(body))
|
||||||
freshVar := GenerateFreshName(freeVars)
|
freshVar := GenerateFreshName(freeVars)
|
||||||
@@ -30,20 +26,19 @@ func (a *Abstraction) Substitute(target string, replacement Expression) Expressi
|
|||||||
|
|
||||||
newBody := body.Substitute(target, replacement)
|
newBody := body.Substitute(target, replacement)
|
||||||
if newBody == body && param == a.parameter {
|
if newBody == body && param == a.parameter {
|
||||||
return a // STRUCTURAL SHARING: nothing changed
|
return a
|
||||||
}
|
}
|
||||||
|
|
||||||
return NewAbstraction(param, newBody)
|
return NewAbstraction(param, newBody)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Substitute replaces all free occurrences of target with replacement.
|
// Substitute replaces all free occurrences of target with replacement.
|
||||||
// Uses structural sharing: only allocates when something actually changes.
|
|
||||||
func (a *Application) Substitute(target string, replacement Expression) Expression {
|
func (a *Application) Substitute(target string, replacement Expression) Expression {
|
||||||
newFunc := a.function.Substitute(target, replacement)
|
newFunc := a.function.Substitute(target, replacement)
|
||||||
newArg := a.argument.Substitute(target, replacement)
|
newArg := a.argument.Substitute(target, replacement)
|
||||||
|
|
||||||
if newFunc == a.function && newArg == a.argument {
|
if newFunc == a.function && newArg == a.argument {
|
||||||
return a // STRUCTURAL SHARING: nothing changed
|
return a
|
||||||
}
|
}
|
||||||
|
|
||||||
return NewApplication(newFunc, newArg)
|
return NewApplication(newFunc, newArg)
|
||||||
|
|||||||
Reference in New Issue
Block a user