Commit Graph

26 Commits

Author SHA1 Message Date
0ec52008bb feat: repr, codec 2026-01-30 16:24:17 -05:00
9c7fb8ceba refactor: rename interpreter to runtime and use receiver methods (#39)
## Description

The codebase previously used "interpreter" terminology and standalone functions for expression operations.
This PR modernizes the architecture by renaming to "runtime" and converting operations to receiver methods.

- Rename `pkg/interpreter` to `pkg/runtime`.
- Move `ReduceOnce` to new `pkg/normalorder` package for reduction strategy isolation.
- Convert standalone functions (`Substitute`, `Rename`, `GetFree`, `IsFree`) to receiver methods on concrete expression types.
- Change `Set` from pointer receivers to value receivers for simpler usage.
- Update all references from "interpreter" to "runtime" terminology throughout the codebase.

### Decisions

- Operations like `Substitute`, `Rename`, `GetFree`, and `IsFree` are now methods on the `Expression` interface, implemented by each concrete type (`Variable`, `Abstraction`, `Application`).
- The `normalorder` package isolates the normal-order reduction strategy, allowing future reduction strategies to be added in separate packages.
- `Set` uses value receivers since Go maps are reference types and don't require pointer semantics.

## Benefits

- Cleaner API: `expr.Substitute(target, replacement)` instead of `Substitute(expr, target, replacement)`.
- Better separation of concerns: reduction strategies are isolated from expression types.
- Consistent terminology: "runtime" better reflects the execution model.
- Simpler `Set` usage without needing to manage pointers.

## Checklist

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

Reviewed-on: #39
Co-authored-by: M.V. Hutz <git@maximhutz.me>
Co-committed-by: M.V. Hutz <git@maximhutz.me>
2026-01-18 20:52:34 +00:00
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
609fe05250 feat: add benchmark target to Makefile (#14)
## Description

This PR adds benchmarking capabilities to the lambda interpreter.
The benchmarks measure performance across all sample files in the samples folder.
This enables consistent performance testing and helps track optimization improvements over time.

Changes in this PR:
- Added new `bench` target to Makefile for running Go benchmarks.
- Created `benchmark_test.go` with sub-benchmarks for each sample file (Church, Fast, Saccharine, Simple, Thunk).
- Used `b.Run` for organizing sample-specific sub-benchmarks and `b.Loop` for efficient iteration.
- Configured benchmarks to use fixed iterations (10x) and 4 CPU cores for reproducible results.

### Decisions

Used `b.Loop()` instead of traditional `for i := 0; i < b.N; i++` pattern.
This is the modern Go benchmarking idiom that provides better performance measurement.

Benchmarks run the full pipeline (parse, compile, execute, stringify) to measure end-to-end performance for each sample.

## Benefits

Provides quantitative performance metrics for the lambda interpreter.
Enables tracking performance improvements or regressions across different sample complexities.
Consistent benchmark configuration (fixed iterations, CPU cores) ensures reproducible results for comparison.

## Checklist

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

Reviewed-on: #14
Co-authored-by: M.V. Hutz <git@maximhutz.me>
Co-committed-by: M.V. Hutz <git@maximhutz.me>
2026-01-11 22:48:26 +00:00
0eff85f8fa feat: add output flag (#13)
## Description

The lambda CLI previously only wrote output to stdout using shell redirection.
This PR adds support for writing results to files using the `-o` flag.
This is implemented using a new `Destination` interface that mirrors the existing `Source` pattern.

Changes:
- Added `Destination` interface with `StdoutDestination` and `FileDestination` implementations.
- Added `-o` flag to CLI argument parser for output file specification.
- Updated `Config` to use `Destination` instead of direct output handling.
- Refactored main to use `Destination.Write()` for result output.
- Updated Makefile targets (`run`, `profile`, `explain`) to use `-o` flag instead of shell redirection.

### Decisions

The `-o` flag defaults to stdout when not specified or when set to `-`.
This maintains backward compatibility while providing explicit file output capability.

## Benefits

- Cleaner command-line interface without shell redirection.
- Symmetric design with `Source` interface for input.
- More portable across different shells and environments.
- Explicit output handling improves code clarity.

## Checklist

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

Reviewed-on: #13
Co-authored-by: M.V. Hutz <git@maximhutz.me>
Co-committed-by: M.V. Hutz <git@maximhutz.me>
2026-01-11 22:14:48 +00:00
62699a0e37 feat: add file input flag (#12)
## Description

The lambda CLI previously only supported inline string expressions and stdin input.
This PR adds support for reading lambda expressions from files using the `-f` flag.
This makes it easier to work with larger programs stored in files.

Changes:
- Added `FileSource` type to `internal/config/source.go` for reading from file paths.
- Added `-f` flag to CLI argument parser with validation to prevent conflicting inputs.
- Updated Makefile targets (`run`, `profile`, `explain`) to use `-f` flag instead of stdin redirection.

### Decisions

The `-f` flag takes precedence over positional arguments.
If both are specified, an error is returned to avoid ambiguity.

## Benefits

- More intuitive workflow for file-based lambda programs.
- Cleaner Makefile targets without stdin redirection.
- Consistent with common CLI conventions (e.g., `grep -f`, `awk -f`).

## Checklist

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

Reviewed-on: #12
Co-authored-by: M.V. Hutz <git@maximhutz.me>
Co-committed-by: M.V. Hutz <git@maximhutz.me>
2026-01-11 21:43:11 +00:00
90c205db2e refactor: improve Makefile structure and usability (#11)
## Description

This PR refactors the Makefile to improve usability, maintainability, and cross-platform compatibility.
The changes modernize the Makefile structure and make it more user-friendly.

Changes made:
- Renamed the `it` target to `build` for better clarity and conventional naming.
- Added a `help` target as the default goal to display available targets and their descriptions.
- Moved the TEST variable to the top with other configuration variables for better organization.
- Updated binary name from `lambda.exe` to `lambda` and used `${BINARY_NAME}` variable consistently throughout.
- Replaced all `@` prefixes with the `.SILENT:` directive for cleaner syntax.
- Added a `clean` target to remove all build artifacts (binary, program.out, profile directory).
- Made the `graph` target cross-platform by replacing macOS-specific `open` command with file:// URL echo.
- Updated .gitignore to include the `lambda` binary.

### Decisions

- Used `.SILENT:` directive instead of individual `@` prefixes for a cleaner, more maintainable Makefile.
- Made `help` the default target so users can run `make` without arguments to see available commands.
- Removed platform-specific commands (like `open`) in favor of cross-platform alternatives.

## Benefits

- Improved discoverability: Users can run `make` to see all available targets.
- Better maintainability: Using `${BINARY_NAME}` variable consistently makes future changes easier.
- Cross-platform compatibility: Removed macOS-specific commands.
- Cleaner syntax: `.SILENT:` directive eliminates repetitive `@` prefixes.
- More conventional: Renamed `it` to `build` follows standard Makefile conventions.

## Checklist

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

Reviewed-on: #11
Co-authored-by: M.V. Hutz <git@maximhutz.me>
Co-committed-by: M.V. Hutz <git@maximhutz.me>
2026-01-11 21:13:41 +00:00
22019acbb1 feat: add PHONY declarations and docs target to Makefile (#9)
---
name: "Default Template"
about: "The default template for `lambda`."
title: "feat: add PHONY declarations and docs target to Makefile"
ref: "main"
assignees: []
labels: []
---

## Description

This PR improves the Makefile by adding proper `.PHONY` declarations and a new `docs` target.
The `.PHONY` declarations ensure make properly handles targets that don't create files.
The `docs` target provides an easy way to view Go package documentation locally using godoc.

Changes:
- Add `.PHONY` declaration for all non-file targets (`it`, `run`, `profile`, `explain`, `graph`, `docs`).
- Add `docs` target that runs godoc server on port 6060 with helpful usage message.

### Decisions

None.

## Benefits

- Prevents make from getting confused if files with target names exist.
- Provides convenient documentation viewing with `make docs`.
- Improves Makefile maintainability following best practices.

## Checklist

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

Reviewed-on: #9
Co-authored-by: M.V. Hutz <git@maximhutz.me>
Co-committed-by: M.V. Hutz <git@maximhutz.me>
2026-01-10 23:29:30 +00:00
Max
1d981ecce3 feat: compiled solution 2025-12-29 02:40:42 -05:00
Max
17cf8f86f8 feat: explanation as observer too 2025-12-29 01:31:09 -05:00
Max
c2b397a9f6 feat: observer pattern for statistics 2025-12-29 00:51:50 -05:00
Max
e9dc3fe171 feat: added optional profiling 2025-12-28 22:52:10 -05:00
Max
a4c049c0ff feat: push outputs to file 2025-12-28 03:39:24 -05:00
Max
633d4a4d3b fix: no stringify in hot loop 2025-12-28 02:19:48 -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
0e185fbf41 feat: expression 2025-12-27 03:43:19 -05:00
Max
1896cd652d feat: better error messages 2025-12-27 02:39:56 -05:00
Max
df53409887 fix: parameters converted in opposite order 2025-12-27 01:41:00 -05:00
Max
a05a63627e feat: better recursive descent 2025-12-27 01:18:06 -05:00
Max
a56ec808ec feat: read from std in 2025-12-25 00:46:48 -05:00
Max
d5999e8e1c feat: reducer, but doesn`t work 2025-12-25 00:30:15 -05:00
Max
61bb622dcd feat: tokenizer 2025-12-23 14:17:43 -05:00