6.62 KB
Newer Older
1 2 3 4 5 6
# lambda

#### A toy interpreter and compiler of the untyped λ calculus, written in common lisp.

A toy compiler of the untyped lambda calculus into common lisp (so far), written in common lisp, with an interpreter and a user interface (UI).

7 8
The UI includes a viewer of all available transformations of a user-input λ term, as well as two simple REPLs.
The viewer can also evaluate terms before listing their different representations, and therefore is the recommended part of the UI (Church arithmetic support TBA).

Ioanna M. Dimitriou H.'s avatar
Ioanna M. Dimitriou H. committed
10 11
##### Goal

The goal of this project is to compile the untyped λ calculus and extensions thereof into several targets, such as a stack machine (WebAssembly in mind), and some assembly languages. 
Ioanna M. Dimitriou H.'s avatar
Ioanna M. Dimitriou H. committed
13 14 15

For learning and understanding compilers, therefore I write most of it by hand. Written in common lisp because I am most comfortable with this language.

16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66
## Try it!


- Common Lisp REPL (SBCL and CCL tested) with Quicklisp installed

Clone this repository to a directory where Quicklisp can find it, for example in `quicklisp/local-projects/` or in whatever `(:tree ...)` you have in `~/.config/common-lisp/repos.conf`.

In your Common Lisp REPL evaluate:
(ql:quickload "lambda")
(in-package :lambda.ui)
You'll be prompted: 

Input which UI to use:
  3  for the transformation viewer
  2  for the church REPL
  1  for the base REPL
  0  to exit
Anything else will return help instructions.

The UI help instructions include the BNF of the standard syntax of this λ calculus, and a short description of each menu options. Each UI mode starts with help instructions specific to the UI mode, examples of usage, and the set of self encoded primitives, such as `:ID :TRUE :FALSE :S :AND :OR :IF :Y :OMEGA`, which you can use when entering a λ term.

#### Examples of λ terms, without and with primitives, and of reductions.

Note that only single variable abstractions are allowed.

Without primitives:

(x y)
(λ a (λ b (a b))

With primitives:

((:or :true) a)
(((:if :false) a ) b)

In the UI's "base REPL", the above evaluate to `:true` and `b`, respectively.

## Implementation features
67 68 69 70 71
  - A User Interface (UI) in the package `:lambda.ui` (call `(main)`) using self-encoded primitives (encoded as λ terms). 
    The UI includes:
	+ A transformation viewer: Input a λ TERM and see its available representations (see next list item). Evaluate the TERM before returning the different representations with =:eval TERM=. In the TERM you can use the same primitives as the `λ.base` REPL below.
	+ Two simple Read Eval Print Loops (REPLs) for this λ calculus:
          * `λ.base` allows you to use the following symbols as self-encoded primitives: `:ID :TRUE :FALSE :S :AND :OR :IF :Y :OMEGA`
72 73
          * `λ.church` allows you to use base, as well as numbers in your statements, and the following symbols as primitives for numeric functions:
            `:A+ :A* :Aexp :Azero? :A+1 :A-1 :A-factorial`, for plus, times, exponent, zero test, successor, predecessor, and factorial.

75 76 77 78 79
  - Showcases representations of lambda terms in my quest to understand compilers.
    So far, showcased representations of lambda terms are the following.
      + The standard, single variable binding, parenthesised lambda terms as quoted common-lisp lists, for example, `'(^ x (x y))`.
      + De Bruijn representation (locally nameless for open terms), the example becomes `'(^ (0 y))`.
      + A common lisp representation, the example becomes `'(lambda (x) (funcall x y))`.
80 81
      + An implementation of Tromp's
        * combinatorial logic representation in the form of SKI-calculus (towards a stack machine - WebAssembly),
82 83 84 85 86 87 88 89 90 91 92 93 94 95
        * and his binary representation.
      + TBA: Implementation of Mogensen's continuation passing style self encoding of lambda calculus (Mogensen-Scott encoding).
      + I provide transformations between defined representations.
  - Lazy, normal order, and applicative order reducers, with respective steppers and self-evaluation loop catchers.
  - A λ calculus interpreter in common-lisp and a compiler to common lisp lambda abstractions.
  - Compiler transformations and simplifications are currently only β-reducing in normal order with loop-catching and maximum allowed steps, closing possibly open terms, full renaming of bound variables, then switching to common-lisp syntax and returning the resulting common-lisp closure.
  For the base of this implementation I just followed Barendrecht and Barendsen's 1994 book "Introduction to Lambda Calculus" [BarBar84]. TBA representations come with respective papers (see references below), which I am currently implementing.

#### Tested
   By adding a catch-loop stepper-based reducer of the standard representation, I have been able to use cl-quickcheck on arbitrary generated terms, and
   quickcheck also examples, theorems, and lemmas from [BarBar84].
   Extensive manually written tests are performed as well.
   Run all tests for CCL and SBCL by loading `run-all-tests.lisp`, or for a particular common lisp implementation from the command line for example as `ccl -b -l run-all-tests.lisp`.
99 100 101 102 103 104 105 106 107
#### Documented

I used the principle of literal programming, with comments in plain text format. The beginning of each file should be informative of the file contents.


Immediate TODO goals, in my priority order, starting from highest priority.
108 109 110
-  Implement Tromp's cross interpreter for SKI-terms.
-  Add transformation of SKI-calculus terms to WebAssembly.
-  Implement more encodings or/and replace them with primitives of a target language (e.g. WebAssembly).
111 112 113 114
-  Find a nicer way around printing keywords with their dots (print related functions in ui.lisp are messy!)
-  Implement UI for steppers.
-  Find a faster encoding for natural numbers or integers, or/and get common lisp (or other target languages, such as the above mentioned stack machine)
-  Compile to my computer's assembly (with the help of disassembly).
-  Unify the 3 UI "modes" into one big REPL. Low priority because it's probably not that relevant to the main goals of this project.
116 117 118 119 120 121 122 123 124 125

# References
In several places in documentation and in comments, I refer to the following publications, in LaTeX bibtex-style alpha.

- [Bar84] **The Lambda Calculus: Its Syntax and Semantics**, H.P. Barendregt, *Elsevier (1984)*
- [BarBar84] **Introduction to lambda calculus**, H.P. Barendregt and E.Barendsen, *Nieuw archief voor wisenkunde 4, 337-372 (1984)*
- [Tro18] **Functional Bits : Lambda Calculus based Algorithmic Information Theory**, J. Tromp, *<> (2018)*
- [Mog94] **Efficient Self-Interpretation in Lambda Calculus**, T. Mogensen, *Journal of Functional Programming 2, (1994)*