Skip to content

paul-talma/lambdapy

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

63 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Lambda_py

An implementation of the lambda calculus in python.

Project structure

Working project tree:

.
├── LICENSE
├── README.md
├── app.py
├── main
│   ├── __init__.py
│   ├── grammar.txt
│   ├── interpreter.py
│   ├── lexer.py
│   ├── parser.py
│   ├── run.py
│   ├── terms.py
│   └── transpiler.py
├── templates
│   └── index.html
└── testing
    ├── __init__.py
    ├── input.txt
    └── test.py

Implementation details

Our implementation provides an interface for the user to enter a lambda expression as a string. The string is then parsed into a lambda expression and evaluated. The result is returned to the user.

The terms of the lambda calculus ("lambda terms") are recursively defined by the following Backus-Naur grammar:

M :: x | λ x . M | M N

Terms of the first kind are variables, terms of the second kind are (function) abstractions, and terms of the third kind are (function) applications.

To represent lambda expressions, we define a Term class, which the Variable, Abstraction, and Application classes subclass.

These term classes form the nodes of an abstract syntax tree constructed by the Parser class.

Lambda terms are read from a text file by the Lexer class, which the parser uses to build the AST.

Evaluating lambda terms turned out to be non-trivial. The naive solution is to traverse the AST of a lambda term, recursively evaluating expressions. However, this strategy does not yield the desired behavior on lambda terms with no normal form. A lambda term is in normal form if it cannot be β -reduced any further. β -reduction allows one to repace expressions of the form ( λ x . M ) N by ones of the form M [ x := N ] (this denotes the substitution of N for x in M ). For example, the term ( λ x . x y ) z β -reduces to z y , which is in normal form. By contrast, Ω := ( λ x . x x ) ( λ x . x x ) does not have a normal form, as can be seen by the fact that applying β -reduction to it yields Ω again. Evaluating Ω should fail to terminate. However, the standard strategy simply terminates after one β -reduction and returns Ω . In order to get the desired behavior, we must perform substitutions in λ -expressions before evaluating applications. This maneuver is implemented in the visit_Application function.

TODOs

Lexer

  • allow both "lambda" and "\" for variable binding

Parser

  • allow syntactic sugar wrt. parentheses

CLI

  • build CLI

Extensions

  • add primitives (numerical expressions, arithmetic functions)
  • add type structure

About

No description, website, or topics provided.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published