b52a7596efad0bba49b679bee17d28ed9d162b61
Replace mutable in-place expression modification with immutable expressions that use structural sharing. This eliminates unnecessary copying during variable substitution and beta reduction. ## Changes - Make expression fields unexported (immutable by design) - Convert Substitute() and Rename() to return new expressions - Implement structural sharing: return self when unchanged - Remove Copy() method entirely - Add getter methods for expression fields ## Performance Impact Benchmarked across all samples: | Sample | Before | After | Improvement | |-------------|--------|-------|-------------| | church | 230ms | 170ms | 26% faster | | saccharine | 320ms | 160ms | 50% faster | | simple | 30ms | 20ms | 33% faster | ## Key Optimization Previously, variable substitution created deep copies: ```go *e = replacement.Copy() // Deep copy entire tree ``` Now uses structural sharing: ```go return replacement // Share pointer directly ``` This eliminates 100% of Copy() allocation overhead (10-50ms per sample). ## Files Modified - pkg/lambda/expression.go: Unexport fields, remove Copy(), add methods - pkg/lambda/substitute.go: Functional API with structural sharing - pkg/lambda/rename.go: Functional API with structural sharing - pkg/lambda/reduce.go: Use new functional API - pkg/lambda/get_free_variables.go: Access unexported fields - pkg/lambda/is_free_variable.go: Access unexported fields - pkg/lambda/stringify.go: Access unexported fields
lambda
Making a lambda calculus interpreter in Go.
Things to talk about
- Exhaustive sum types.
- Recursive descent and left-recursion.
- Observer pattern, event emission.
Links
https://zicklag.katharos.group/blog/interaction-nets-combinators-calculus/ https://arxiv.org/pdf/2505.20314
Languages
Go
96.6%
Makefile
3.4%