Skip to content

Sagoots/LambdaInterpret

Folders and files

NameName
Last commit message
Last commit date

Latest commit

ย 

History

4 Commits
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 

Repository files navigation

๐Ÿš€ Lambda Calculus REPL

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

โœจ Features

๐Ÿ”ง Core Functionality

  • ๐Ÿ”ค 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

๐Ÿงฎ Mathematical Operations

  • ๐Ÿ”ข Church Numerals - Full support for Church numeral arithmetic
    • SUCC, PLUS, MULT, PRED, MINUS
    • ISZERO, LEQ (less-than-or-equal)
  • ๐Ÿ”€ Church Booleans - Complete boolean logic system
    • TRUE, FALSE, IF, NOT
    • AND, OR, XOR

๐Ÿ”„ Advanced Features

  • ๐Ÿ“ Macro Definitions - Create custom shortcuts with #define syntax
  • โ™ป๏ธ 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

๐Ÿš€ Getting Started

Prerequisites

Wolfram Mathematica (Version 10.0+)

๐Ÿ“ฆ Installation

  1. Save the Package

    MyLambdaREPL.wl
    
  2. Add to Mathematica Path

    (* Option 1: Applications directory *)
    FileNameJoin[{$UserBaseDirectory, "Applications"}]
    
    (* Option 2: Custom directory *)
    AppendTo[$Path, "path/to/your/directory"]
  3. Load the Package

    Needs["MyLambdaREPL`"]

    Alternatively, just copy and paste MyLambdaREPL into yout mahematica notebook.

๐Ÿ’ก Usage

๐ŸŽฎ Starting the REPL

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

โš™๏ธ Configuration

Adjust the maximum reduction steps:

MyLambdaREPL`LambdaStepNum = 500;  (* Default: 100 *)
LambdaREPL[]

๐Ÿ“– Examples

๐Ÿ”ค Basic Lambda Expressions

ฮป> (\x.x) y
Reduction Chain:
(\x.x) y
y
Out: y

๐Ÿ“š Using Let Expressions

ฮป> let id = (\x.x) in id A
Reduction Chain:
let id = (\x.x) in id A
(\x.x) A
A
Out: A

๐Ÿงฎ Church Numeral Arithmetic

ฮป> PLUS 2 3
PLUS 2 3 ->
... (reduction steps) ... ->
ฮปf x.f (f (f (f (f x)))) -> (* Church numeral for 5 *)

๐Ÿ“ Macro Definitions

ฮป> #define ID = \x.x
Defined macro: ID = ฮปx.x

ฮป> ID A
ID A ->
(\x.x) A ->
A

๐Ÿ”„ Recursive Functions with Y Combinator

ฮป> #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 *)

๐Ÿ“‹ Language Reference

๐Ÿ”ค Syntax Elements

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

๐Ÿงฎ Built-in Functions

Arithmetic Operations

  • SUCC - Successor function
  • PLUS - Addition
  • MULT - Multiplication
  • PRED - Predecessor
  • MINUS - Subtraction
  • ISZERO - Zero predicate
  • LEQ - Less than or equal

Boolean Operations

  • TRUE / FALSE - Boolean constants
  • IF - Conditional expression
  • NOT - Logical negation
  • AND / OR / XOR - Logical operations

Special Combinators

  • Y - Y combinator (fixed-point)
  • I - Identity combinator
  • K - Constant combinator
  • S - Substitution combinator
  • Z - Zero combinator

๐Ÿ—๏ธ Project Structure

MyLambdaREPL.wl
โ”œโ”€โ”€ Tokenizer      # String โ†’ Tokens
โ”œโ”€โ”€ Parser         # Tokens โ†’ AST  
โ”œโ”€โ”€ Reducer        # AST โ†’ Reduced AST
โ”œโ”€โ”€ Printer        # AST โ†’ Pretty String
โ”œโ”€โ”€ Macro System   # #define handling
โ””โ”€โ”€ REPL Interface # User interaction

๐Ÿšช Exiting

ฮป> exit
Exiting REPL. Goodbye!

ฮป> quit
Exiting REPL. Goodbye!

๐Ÿค Contributing

We welcome contributions! Feel free to:

  • ๐Ÿ› Report bugs
  • ๐Ÿ’ก Suggest new features
  • ๐Ÿ”ง Submit pull requests
  • ๐Ÿ“– Improve documentation

๐Ÿ“„ License

This project was developed for academic purposes at Technion - Israel Institute of Technology.


About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors