A modular implementation of lambda calculus in Python, inspired by the reference lecture notes. The project includes both a command-line REPL and a web-based interface for interacting with lambda calculus expressions.
- Expression Representation: Classes for variables, lambda abstractions, and applications
- Parser: Convert lambda calculus notation to syntax tree objects
- Evaluation Strategies:
- Normal order evaluation (leftmost, outermost)
- Applicative order evaluation (leftmost, innermost)
- β-reduction
- Church Encodings:
- Booleans (TRUE, FALSE, AND, OR, NOT)
- Natural numbers (Church numerals)
- Pairs
- Lists
- Interactive Interfaces:
- Command-line REPL for experimenting with lambda calculus
- Web-based interface with an interactive REPL
- Dark mode toggle functionality
- Comprehensive help section
- Python 3.6 or higher
- For Windows users:
pyreadline3module (automatically used as fallback if standardreadlineis unavailable)
To start the interactive command-line REPL for lambda calculus:
python main.pyTo start the web-based interface:
-
Navigate to the web directory:
cd web -
Start a local HTTP server:
# Using Python 3's built-in server python -m http.server -
Open your browser and go to:
http://localhost:8000
λ> λx.x
Parsed: λx.x
λ> (λx.x) y
Parsed: (λx.x) y
λ> beta (λx.x) y
Result: y
Steps taken: 1λ> church 3
Church numeral for 3: λf.λx.f (f (f x))
λ> beta (PLUS TWO THREE)
Result: λf.λx.f (f (f (f (f x))))
Steps taken: 8
λ> extract (PLUS TWO THREE)
Extracted value: 5λ> let ID λx.x
Defined ID = λx.x
λ> let IDENTITY_COMBINATOR λx.x
Defined IDENTITY_COMBINATOR = λx.x
λ> vars
Defined variables:
AND = λp.λq.p q p
CONS = λh.λt.λc.c h t
FALSE = λx.λy.y
FIRST = λp.p (λx.λy.x)
...lambda_calculus/syntax.py: Core syntax classeslambda_calculus/parser.py: Lambda expression parserlambda_calculus/semantics.py: Evaluation strategieslambda_calculus/encodings.py: Church encodingslambda_calculus/repl.py: Interactive command-line REPLweb/: Web-based interface filesindex.html: Main HTML structurestyles.css: Styling including dark modeapp.js: JavaScript for web interface functionality
help: Show help informationquit,exit: Exit the REPLvars: List all defined variableslet NAME EXPR: Define a variablestep EXPR: Perform a single beta-reduction stepbeta EXPR: Fully beta-reduce an expressionnormal EXPR: Evaluate using normal orderapp EXPR: Evaluate using applicative orderchurch N: Create a Church numeral for integer Nextract EXPR: Try to extract a number from a Church numeral
The implementation supports the standard lambda calculus syntax:
- Variables:
x,y,z, etc. - Abstractions:
λx.Mor\x.M(where λ can be written as \ for convenience) - Applications:
M N(with left-associativity) - Parentheses:
(M)for explicit grouping
- Interactive REPL: Type lambda calculus expressions and see results immediately
- Multiple Evaluation Strategies: Choose between β-reduction, Normal Order, and Call-by-value
- Dark Mode: Toggle between light and dark themes for comfortable viewing
- Comprehensive Help: Detailed explanations of lambda calculus concepts and syntax
- Automatic Lambda Symbol Conversion: Type backslash (
\) and see it displayed as lambda (λ)