This is a Haskell implementation of a Lambda Calculus interpreter, developed as part of the "Paradigms of Programming" course (Tema 3). It supports evaluation strategies, macro definitions, and includes a standard library of Church encodings.
Lambda.hs: Core logic for the lambda calculus, including:Lambdadata type definition.- Functions for handling variables (
vars,freeVars,newVar). - Substitution and Beta-reduction logic (
reduce). - Evaluation strategies: Normal (
normalStep) and Applicative (applicativeStep) order.
Parser.hs: A monadic parser for parsing lambda expressions and line definitions from strings.Binding.hs: Manages the evaluation context and macro expansion (expandMacros,simplifyCtx).Default.hs: Contains a "standard library" of pre-defined macros (Church booleans, numerals, and pairs) and the default context.main.hs: The main entry point, implementing a Read-Eval-Print Loop (REPL) for interactive use.test.hs: A comprehensive test suite to verify the correctness of the implementation.
- Evaluation Strategies: Supports both Normal and Applicative order reduction to normal form.
- Parsing: Parses standard lambda calculus syntax (e.g.,
\x.x,\x y.x,lambda x.x). - Macros & Context: Allows defining and using macros (e.g.,
TRUE,ADD). - Standard Library: Includes Church encodings for:
- Booleans:
TRUE,FALSE,AND,OR,NOT,XOR. - Pairs:
PAIR,FST,SND. - Numerals:
N0,N1...N2,SUCC,PRED,ADD,SUB,MULT.
- Booleans:
- Interactive REPL: rigorous testing environment.
To start the interactive interpreter, run:
runhaskell main.hs<expression>: Evaluates a lambda expression.<NAME> = <expression>: Defines a new macro in the current context.:ctx: Displays the current context (defined macros).:r: Resets the context to the default state (reloadsDefault.hs).:q: Quits the REPL.
λ> \x.x
λx.x
λ> (\x.x) y
y
λ> ADD N1 N1
λf.λx.f (f x)
λ> MYID = \x.x
λx.x
λ> MYID a
a
To run the full test suite:
runhaskell test.hsTo run tests for specific components:
runhaskell test.hs lambda # Test evaluation logic
runhaskell test.hs parser # Test parser
runhaskell test.hs binding # Test context/macros
runhaskell test.hs default # Test standard library