A powerful Read-Eval-Print Loop for the untyped Lambda Calculus
Built with Wolfram Mathematica
Developed as partial fulfillment for course 2360651 - Advanced Topics in Software Engineering
Technion, Spring 2024/25 โข Supervised by Yossi Gil
- ๐ค Tokenization - Intelligent parsing of lambda expressions into token streams
- ๐ณ AST Generation - Converts tokens into Abstract Syntax Trees following lambda calculus grammar
- โก Reduction Engine - Implements normal-order beta-reduction with alpha-conversion and eta-reduction
- ๐จ Pretty Printing - Clean, readable output with optimized parentheses placement
- ๐ข Church Numerals - Full support for Church numeral arithmetic
SUCC,PLUS,MULT,PRED,MINUSISZERO,LEQ(less-than-or-equal)
- ๐ Church Booleans - Complete boolean logic system
TRUE,FALSE,IF,NOTAND,OR,XOR
- ๐ Macro Definitions - Create custom shortcuts with
#definesyntax - โป๏ธ Y Combinator - Built-in support for recursive function definitions
- ๐ File I/O - Read from files and log all interactions
- ๐ก๏ธ Error Handling - Robust error reporting and graceful failure handling
Wolfram Mathematica (Version 10.0+)-
Save the Package
MyLambdaREPL.wl -
Add to Mathematica Path
(* Option 1: Applications directory *) FileNameJoin[{$UserBaseDirectory, "Applications"}] (* Option 2: Custom directory *) AppendTo[$Path, "path/to/your/directory"]
-
Load the Package
Needs["MyLambdaREPL`"]
Alternatively, just copy and paste MyLambdaREPL into yout mahematica notebook.
| Mode | Command | Description |
|---|---|---|
| Interactive | LambdaREPL[] |
Standard notebook interaction |
| File Input | LambdaREPL["input.txt"] |
Read from file, output to notebook |
| File Output | LambdaREPL["", "output.txt"] |
Notebook input, save to file |
| Full File Mode | LambdaREPL["input.txt", "output.txt"] |
Complete file-based operation |
Adjust the maximum reduction steps:
MyLambdaREPL`LambdaStepNum = 500; (* Default: 100 *)
LambdaREPL[]ฮป> (\x.x) y
Reduction Chain:
(\x.x) y
y
Out: yฮป> let id = (\x.x) in id A
Reduction Chain:
let id = (\x.x) in id A
(\x.x) A
A
Out: Aฮป> PLUS 2 3
PLUS 2 3 ->
... (reduction steps) ... ->
ฮปf x.f (f (f (f (f x)))) -> (* Church numeral for 5 *)ฮป> #define ID = \x.x
Defined macro: ID = ฮปx.x
ฮป> ID A
ID A ->
(\x.x) A ->
Aฮป> #define FACT_GEN = \f n. IF (ISZERO n) ONE (MULT n (f (PRED n)))
Defined macro: FACT_GEN = ฮปf n.IF (ISZERO n) ONE (MULT n (f (PRED n)))
ฮป> #define FACT = Y FACT_GEN
Defined macro: FACT = Y FACT_GEN
ฮป> FACT 2
FACT 2 ->
... (reduction steps) ...
ฮปf x.f (f x) ->
Out: ฮปf x.f (f x) (* Church numeral for 2! = 2 *)| Element | Syntax | Example |
|---|---|---|
| Variables | x, y, myVar |
x |
| Abstractions | \x.body or @x.body |
\x.x |
| Applications | f x |
(\x.x) y |
| Let Expressions | let var = val in body |
let id = \x.x in id A |
| Numbers | 0, 1, 2, 3... |
PLUS 2 3 |
SUCC- Successor functionPLUS- AdditionMULT- MultiplicationPRED- PredecessorMINUS- SubtractionISZERO- Zero predicateLEQ- Less than or equal
TRUE/FALSE- Boolean constantsIF- Conditional expressionNOT- Logical negationAND/OR/XOR- Logical operations
Y- Y combinator (fixed-point)I- Identity combinatorK- Constant combinatorS- Substitution combinatorZ- Zero combinator
MyLambdaREPL.wl
โโโ Tokenizer # String โ Tokens
โโโ Parser # Tokens โ AST
โโโ Reducer # AST โ Reduced AST
โโโ Printer # AST โ Pretty String
โโโ Macro System # #define handling
โโโ REPL Interface # User interaction
ฮป> exit
Exiting REPL. Goodbye!
ฮป> quit
Exiting REPL. Goodbye!We welcome contributions! Feel free to:
- ๐ Report bugs
- ๐ก Suggest new features
- ๐ง Submit pull requests
- ๐ Improve documentation
This project was developed for academic purposes at Technion - Israel Institute of Technology.