Commit Graph

45 Commits

Author SHA1 Message Date
dbc3c5a8d4 Improve testing infrastructure with dynamic discovery and validation (#20)
## Summary

This PR enhances the testing infrastructure with dynamic test discovery, automated validation, and improved error handling.

## Changes

### Testing Infrastructure
- Added `TestSamplesValidity` integration test that validates all test files against their expected output.
- Implemented dynamic test discovery using `filepath.Glob` to automatically find all `.test` files.
- Renamed `benchmark_test.go` to `lambda_test.go` for better naming consistency.
- Consolidated helper functions into a single `runSample` function.
- Replaced all error handling with `assert` for consistent and clear test output.
- Required all `.test` files to have corresponding `.expected` files.

### Iterator Improvements
- Added `Swap` method to iterator for better reduction algorithm.
- Improved reduction algorithm with LIFO-based iterator implementation.

### Build System
- Added `make test` target to run tests without benchmarks.
- Updated Makefile help text to include the new test target.

### Test Cases
- Added new test cases with expected outputs: `church_5^5`, `church_6^6`, `fast_list_2^30`, `list_2^30`.
- Added validation files for all test cases.

## Test plan

- Run tests with expected output validation.
- Run benchmarks to ensure performance is maintained.
- Verify make targets work correctly.

Reviewed-on: #20
Co-authored-by: M.V. Hutz <git@maximhutz.me>
Co-committed-by: M.V. Hutz <git@maximhutz.me>
2026-01-13 01:20:47 +00:00
15c904ccc9 feat: improve reduction algorithm with LIFO-based iterator (#15)
## Description

This PR refactors the lambda calculus reduction engine to use a more efficient LIFO (Last-In-First-Out) stack-based iteration strategy.
Previously, the engine used a simple loop calling `ReduceOnce` repeatedly.
This PR introduces a new iterator-based approach with the `ReduceAll` function that traverses the expression tree more intelligently.

Changes include:

- Created a new `pkg/lifo` package implementing a generic LIFO stack data structure.
- Added `pkg/lambda/iterator.go` with an `Iterator` type for traversing lambda expressions.
- Refactored `pkg/lambda/reduce.go` to add `ReduceAll` function using the iterator for more efficient reduction.
- Updated `internal/engine/engine.go` to use `ReduceAll` instead of looping `ReduceOnce`.
- Renamed sample test files from `.txt` to `.test` extension.
- Fixed `.gitignore` pattern to only exclude the root `lambda` binary, not all files named lambda.
- Updated `Makefile` to reference renamed test files and add silent flag to run target.

### Decisions

- Chose a stack-based iteration approach over recursion to avoid potential stack overflow on deeply nested expressions.
- Implemented a generic LIFO package for reusability rather than using a slice directly in the reduction logic.
- Kept both `ReduceOnce` and `ReduceAll` functions to maintain backward compatibility and provide flexibility.

## Performance

Benchmark results comparing main branch vs this PR on Apple M3:

| Test | Before (ms/op) | After (ms/op) | Change |
|------|----------------|---------------|--------|
| Thunk | 0.014 | 0.014 | 0.00% |
| Fast | 1.29 | 1.20 | **-7.04%** |
| Simple | 21.51 | 6.45 | **-70.01%** |
| Church | 157.67 | 43.00 | -76.788% |
| Saccharine | 185.25 | 178.99 | **-3.38%** |

**Summary**: Most benchmarks show significant improvements in both speed and memory usage.
The Church benchmark shows a regression that needs investigation.

## Benefits

- More efficient expression tree traversal with the iterator pattern.
- Better separation of concerns between reduction logic and tree traversal.
- Generic LIFO stack can be reused in other parts of the codebase.
- Cleaner engine implementation with callback-based step emission.

## Checklist

- [x] Code follows conventional commit format.
- [x] Branch follows naming convention (`<type>/<description>`). Always use underscores.
- [ ] Tests pass (if applicable).
- [ ] Documentation updated (if applicable).

Reviewed-on: #15
Co-authored-by: M.V. Hutz <git@maximhutz.me>
Co-committed-by: M.V. Hutz <git@maximhutz.me>
2026-01-12 02:16:07 +00:00
72a0afbbc0 perf: implement structural sharing for expression trees (#10)
## Description

The profiler revealed that 75% of CPU time was spent on memory allocation, with the primary bottleneck being expression copying during variable substitution. Every time a variable was substituted with an expression, `replacement.Copy()` would create a full deep copy of the entire expression tree.

This PR refactors the lambda calculus interpreter from a mutable, pointer-based implementation to an immutable, structurally-shared implementation. Expressions are now immutable value types that share unchanged subtrees instead of copying them.

**Key changes:**
- Made expression fields unexported to enforce immutability.
- Converted `Substitute()` and `Rename()` from in-place mutation to functional methods that return new expressions.
- Implemented structural sharing: methods return the same pointer when nothing changes.
- Removed `Copy()` method entirely - no more deep copying during substitution.
- Added getter methods for accessing expression fields from outside the package.

### Decisions

**Immutability over mutation:** Switched from mutable `*Expression` pointers with in-place updates to immutable expressions that return new trees. This is a fundamental architectural shift but aligns with functional programming principles and enables structural sharing.

**Structural sharing strategy:** When `Substitute()` or `Rename()` encounters an unchanged subtree, it returns the original pointer instead of creating a new object. This is safe because expressions are now immutable.

**Field encapsulation:** Made all expression fields unexported (`Parameter` → `parameter`, `Body` → `body`, etc.) to prevent external mutation. Added getter methods for controlled access.

## Benefits

**Performance improvements** (measured across all samples):

| Sample      | Before CPU | After CPU | Improvement | Copy Overhead Eliminated |
|-------------|-----------|----------|-------------|--------------------------|
| **saccharine** | 320ms | 160ms | **50% faster** | 50ms (15.6% of total) |
| **church** | 230ms | 170ms | **26% faster** | 40ms (17.4% of total) |
| **simple** | 30ms | 20ms | **33% faster** | 10ms (33.3% of total) |

**Wall-clock improvements:**
- saccharine: 503ms → 303ms (40% faster)
- church: 404ms → 302ms (25% faster)

**Memory allocation eliminated:**
- Before: `runtime.mallocgcSmallScanNoHeader` consumed 10-50ms per sample
- After: **Completely eliminated from profile** 
- All `Copy()` method calls removed from hot path

**The optimization in action:**

Before:
```go
func Substitute(e *Expression, target string, replacement Expression) {
    switch typed := (*e).(type) {
    case *Variable:
        if typed.Value == target {
            *e = replacement.Copy()  // Deep copy entire tree!
        }
    }
}
```

After:
```go
func (v *Variable) Substitute(target string, replacement Expression) Expression {
    if v.value == target {
        return replacement  // Share pointer directly, no allocation
    }
    return v  // Unchanged, share self
}
```

**Codebase improvements:**
- More idiomatic functional programming style.
- Immutability prevents entire class of mutation bugs.
- Clearer ownership semantics (expressions are values, not mutable objects).
- Easier to reason about correctness (no action at a distance).

## Checklist

- [x] Code follows conventional commit format.
- [x] Branch follows naming convention (`perf/structural-sharing`).
- [x] Tests pass (no test files exist, but build succeeds and profiling confirms correctness).
- [x] Documentation updated (added comments explaining structural sharing).

Reviewed-on: #10
Co-authored-by: M.V. Hutz <git@maximhutz.me>
Co-committed-by: M.V. Hutz <git@maximhutz.me>
2026-01-11 02:15:38 +00:00
Max
242fda3b4a feat: progress 2026-01-10 11:37:18 -05:00
Max
2499921679 style: moved functions around 2025-12-30 15:58:14 -05:00
Max
412d3924eb docs: document methods 2025-12-29 20:44:55 -05:00
Max
aabe92f2dc fix: cannot omit final newline 2025-12-29 20:17:50 -05:00
Max
a2ce5b6897 feat: rename profiler to performance, typeless event emitter 2025-12-29 01:15:14 -05:00
Max
c2b397a9f6 feat: observer pattern for statistics 2025-12-29 00:51:50 -05:00
Max
6be3b7958a feat: use iterative approach for reduce once 2025-12-28 02:37:22 -05:00
Max
633d4a4d3b fix: no stringify in hot loop 2025-12-28 02:19:48 -05:00
Max
ee9e71d58e fix: no ds store 2025-12-28 02:07:46 -05:00
Max
0945cedf51 feat: only compute all free variables during a-conversion 2025-12-28 02:07:14 -05:00
Max
4d81aca0b2 feat: fun little program 2025-12-28 00:53:43 -05:00
Max
f4897d53a9 feat: it works! 2025-12-27 23:51:04 -05:00
Max
f038d0a685 feat: parse saccharine, conversion incoming 2025-12-27 23:36:44 -05:00
Max
14fc4b30da feat: cleaner parsing functions 2025-12-27 20:46:10 -05:00
Max
c37e96770f feat: tokenizer accepts braces, line terminator, and equal sign 2025-12-27 19:52:18 -05:00
Max
0e185fbf41 feat: expression 2025-12-27 03:43:19 -05:00
Max
bf0edfc593 style: renamed token index to column 2025-12-27 02:43:17 -05:00
Max
1896cd652d feat: better error messages 2025-12-27 02:39:56 -05:00
Max
884180de92 feat: error for when there is more source code than parsed 2025-12-27 02:08:18 -05:00
Max
df53409887 fix: parameters converted in opposite order 2025-12-27 01:41:00 -05:00
Max
5841023dde fix: no log errors 2025-12-27 01:18:52 -05:00
Max
a05a63627e feat: better recursive descent 2025-12-27 01:18:06 -05:00
Max
e3629acb45 feat: stuff 2025-12-26 03:37:05 -05:00
Max
d427703afe wip: new folder structure, overhaul language 2025-12-26 02:39:15 -05:00
Max
11e7f70625 feat: stuff 2025-12-26 01:59:56 -05:00
Max
fa44051dec fix: don`t overlap "close" 2025-12-26 01:03:06 -05:00
Max
c2ec9127e8 style: no capitalized error messages 2025-12-26 00:08:36 -05:00
Max
17fa94113a style: no capitalized error messages 2025-12-26 00:08:15 -05:00
Max
d74894223e fix: does need to compare to true 2025-12-26 00:07:10 -05:00
Max
5ff8892d13 style: no underscores in variable names 2025-12-26 00:04:41 -05:00
Max
44046e6abb fix: drop zero-init of fresh name generator 2025-12-26 00:02:32 -05:00
Max
c80587d522 style: unneeded else 2025-12-26 00:01:15 -05:00
Max
6f3b252819 style: no "this" or "self" as receiver 2025-12-26 00:00:10 -05:00
Max
e6e4a0df6f style: no punctuation or capitalization in errors 2025-12-25 23:52:39 -05:00
Max
32b1ba12f4 feat: golangci lint 2025-12-25 23:48:39 -05:00
Max
d9deee0734 fix: cannot parse just a token 2025-12-25 02:00:04 -05:00
Max
99703c2587 fix: unbound substitutions, explanation tag 2025-12-25 01:55:46 -05:00
Max
88ee4f799e feat: reducer works 2025-12-25 00:40:34 -05:00
Max
d5999e8e1c feat: reducer, but doesn`t work 2025-12-25 00:30:15 -05:00
Max
2c3ce9baf7 feat: wogihrsoiuvjsroirgj 2025-12-24 14:55:33 -05:00
Max
1d8ecba118 feat: parser 2025-12-23 21:54:42 -05:00
Max
61bb622dcd feat: tokenizer 2025-12-23 14:17:43 -05:00