M.V. Hutz b52a7596ef perf: implement structural sharing for expression trees
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
2026-01-10 20:16:57 -05:00
2025-12-30 15:58:14 -05:00
2026-01-10 11:37:18 -05:00
2025-12-30 15:58:14 -05:00
2025-12-29 02:40:42 -05:00
2025-12-28 02:07:46 -05:00
2025-12-23 14:17:43 -05:00
2025-12-22 21:09:00 +00:00
2025-12-29 19:28:45 -05:00

lambda

Making a lambda calculus interpreter in Go.

Things to talk about

  • Exhaustive sum types.
  • Recursive descent and left-recursion.
  • Observer pattern, event emission.

https://zicklag.katharos.group/blog/interaction-nets-combinators-calculus/ https://arxiv.org/pdf/2505.20314

Description
Making a lambda calculus interpreter in Go.
Readme AGPL-3.0 863 KiB
Languages
Go 96.6%
Makefile 3.4%