feat: add De Bruijn index reduction engine #27

Closed
mvhutz wants to merge 1 commits from feat/debruijn-engine into main
Owner

Summary

  • Adds -i flag to select interpreter (lambda or debruijn)
  • Implements De Bruijn index-based reduction engine that eliminates variable renaming
  • Abstracts Engine into interface with LambdaEngine and DeBruijnEngine implementations
  • Adds conversion functions between lambda calculus and De Bruijn representations
  • All test samples pass with both engines

Implementation Details

De Bruijn Package

  • Variable: Contains integer index and optional label string
  • Abstraction: Contains only body (no parameter needed)
  • Application: Similar structure to lambda version
  • Substitute: Replaces variables by index with automatic shift operations
  • ReduceAll: Mirrors lambda reduction without variable renaming overhead

Engine Abstraction

  • Created Engine interface with Run() and GetResult() methods
  • LambdaEngine: Original named variable implementation
  • DeBruijnEngine: New index-based implementation
  • Both engines support all existing features (profiling, statistics, explanation, verbose mode)

Conversion Functions

  • LambdaToDeBruijn: Converts named lambda expressions to De Bruijn indices
  • DeBruijnToLambda: Converts De Bruijn indices back to named expressions
  • SaccharineToDeBruijn: Direct conversion from saccharine syntax to De Bruijn

Testing

  • Added comprehensive test suite comparing both engines
  • All 6 test files pass (thunk, comments, church_5^5, church_6^6, list_2^30, fast_list_2^30)
  • De Bruijn engine produces correct reductions without variable conflicts

Test Plan

  • Build succeeds
  • All existing tests pass with lambda engine (default)
  • All existing tests pass with debruijn engine (-i debruijn)
  • Invalid interpreter flag returns error
  • Both engines produce equivalent reduced forms
  • Performance profiling works with both engines
  • Statistics tracking works with both engines
  • Verbose mode works with both engines

Closes #26

## Summary - Adds `-i` flag to select interpreter (lambda or debruijn) - Implements De Bruijn index-based reduction engine that eliminates variable renaming - Abstracts Engine into interface with LambdaEngine and DeBruijnEngine implementations - Adds conversion functions between lambda calculus and De Bruijn representations - All test samples pass with both engines ## Implementation Details ### De Bruijn Package - **Variable**: Contains integer index and optional label string - **Abstraction**: Contains only body (no parameter needed) - **Application**: Similar structure to lambda version - **Substitute**: Replaces variables by index with automatic shift operations - **ReduceAll**: Mirrors lambda reduction without variable renaming overhead ### Engine Abstraction - Created `Engine` interface with `Run()` and `GetResult()` methods - `LambdaEngine`: Original named variable implementation - `DeBruijnEngine`: New index-based implementation - Both engines support all existing features (profiling, statistics, explanation, verbose mode) ### Conversion Functions - `LambdaToDeBruijn`: Converts named lambda expressions to De Bruijn indices - `DeBruijnToLambda`: Converts De Bruijn indices back to named expressions - `SaccharineToDeBruijn`: Direct conversion from saccharine syntax to De Bruijn ### Testing - Added comprehensive test suite comparing both engines - All 6 test files pass (thunk, comments, church_5^5, church_6^6, list_2^30, fast_list_2^30) - De Bruijn engine produces correct reductions without variable conflicts ## Test Plan - [x] Build succeeds - [x] All existing tests pass with lambda engine (default) - [x] All existing tests pass with debruijn engine (`-i debruijn`) - [x] Invalid interpreter flag returns error - [x] Both engines produce equivalent reduced forms - [x] Performance profiling works with both engines - [x] Statistics tracking works with both engines - [x] Verbose mode works with both engines Closes #26
mvhutz added 1 commit 2026-01-13 02:28:00 +00:00
Closes #26

- Added -i flag to select interpreter (lambda or debruijn)
- Created debruijn package with Expression interface
  - Variable contains index and optional label
  - Abstraction contains only body (no parameter)
  - Application structure remains similar
- Implemented De Bruijn reduction without variable renaming
  - Shift operation handles index adjustments
  - Substitute replaces by index instead of name
- Abstracted Engine into interface with two implementations
  - LambdaEngine: original named variable engine
  - DeBruijnEngine: new index-based engine
- Added conversion functions between representations
  - LambdaToDeBruijn: converts named to indexed
  - DeBruijnToLambda: converts indexed back to named
  - SaccharineToDeBruijn: direct saccharine to De Bruijn
- Updated main to switch engines based on -i flag
- All test samples pass with both engines

Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
mvhutz closed this pull request 2026-01-17 00:27:26 +00:00

Pull request closed

Sign in to join this conversation.