Lambda calculus is the DNA of all computation
In lambda calculus the entire grammar of the language comprises of just three kinds of expressions
- variables
- lambda abstraction
- application
The grammar can be formulated recursively as
In lambda calculus functions accept only a single argument, but they can be nested, this is referred to as currying in the literature. For example, we can have a simple function that just returns its argument which is generally known as the identity function.
Any language that has first class functions with closures can be used to simulate lambda calculus. As an example I will use my own custom toy language.
The result of evaluating f g 1
is 1
, because f g
→ g
and g 1
→ 1
.
As we can see the language can be used to express every single term we have in lambda calculus. Can these terms be used to express any computation? As it turns out yes, in fact we can encode data type using just functions. These encodings are called Church encodings in the literature.
Let’s start with booleans, they can be defined as follows:
And then we can defined a function that will work just like if ... then ... else ...
in general purpose programming languages.
Let’s also defined the and
combinator which checks if two values are true.
Let’s see if this works! Feel free to play around with the code…
let tru t f = t; let fls t f = f; let test l m n = l m n; let and_ b c = b c fls; test (and_ tru tru) "both true!" "nope"
Okay, so we have booleans. Can we create data structures? Let’s try to define a simple two element tuple.
Here we have a function pair
which can be used to create a pair, and two functions fo retrieving the elements of the pair. We can define these functions in the programming language.
We can also use many sequentially nested tuples to simulate lists, this is in fact a mechanism that served as the backbone for the lisp family of languages. The example below is interactive.
let tru t f = t; let fls t f = f; let pair f s b = b f s; let fst p = p tru; let snd p = p fls; let list = pair 1 (pair 2 (pair 3 (pair 4 (pair 5 fls)))); list |> snd |> snd |> snd |> fst
Bibliography
- B. C. Pierce, Types and programming languages. MIT Press, 2002.