# Lambda Calculus In Clojure - Part 1

Lambda Calculus is the smallest programming language. As we saw on my previous post, the only building blocks available are functions, variables and expressions. There are no built-in primitive values or operations. How can we then solve real-world problems using Lambda Calculus?

In this post, we are going to create a set of building blocks, using lambda expressions, to calculate boolean expressions. For this, we are going to use Clojure, which is a dynamic functional language based on Lambda Calculus.

## Church Encoding

A mathematician called Alonzo Church was able to encode data and operators in Lambda Calculus. **Church encoding** is the name of this way of building primitive terms and operations, converting them into higher-order functions that can be combined to create more complex expressions.

There are a few different primitives that can be constructed in Church Encoding:

- Booleans
- Integers
- Pairs
- Lists
- Tagged unions

Each of these primitives have their own operators, that can be built in a similar way to the values of the primitives. This gives us the basic building blocks for writing programs in Lambda Calculus. For the rest of this post, we are going to focus on Church booleans.

### Church booleans

In boolean logic, there are only two possible values, `true`

and `false`

. As we saw previously, Lambda Calculus doesnâ€™t provide any primitive values, but we can define them in Church Encoding. Given a lambda function with two parameters, we define `true`

and `false`

as following:

```
true = Î»a.Î»b.a
false = Î»a.Î»b.b
```

Looking at this definition, we can see that `true`

is a function of two parameters that returns the first one. Similarly, `false`

returns the second parameter.

Apart from primitive values, we need some operators in order to complete a boolean algebra and write programs. We can define the boolean operators based on the definitions for `true`

and `false`

above:

```
and = Î»p.Î»q.p q p
or = Î»p.Î»q.p p q
```

`and`

returns **q** if **p** is `true`

; otherwise, returns **p**, which equals to `false`

. We can use the definition of `true`

and `false`

above to see the substitution in detail:

When **p** is true:

```
p = true = Î»a.Î»b.a
and = Î»p.Î»q.p q p = Î»p.Î»q.((Î»a.Î»b.a)(q p)) = Î»p.Î»q.q = q
```

When **p** is false:

```
p = false = Î»a.Î»b.b
and = Î»p.Î»q.p q p = Î»p.Î»q.((Î»a.Î»b.b)(q p)) = Î»p.Î»q.p = p = false
```

`or`

works in a similar way.

```
not = Î»p.p false true
```

Applying the same reasoning, `not`

just inverts the value of **p**, returning `false`

if **p** is `true`

, and returning `true`

otherwise.

The last operator is Exclusive OR:

```
xor = Î»a.Î»b.a (not b) b
```

which follows the same rules as the previous operators.

Itâ€™s also possible to define an `if`

predicate, similar to the ones we use in most programming languages:

```
if = Î»p.Î»a.Î»b.p a b
```

The predicate **p** is applied to the arguments **a** and **b**, returning **a** if the predicate evaluates to `true`

, and **b** if the predicate evaluates to `false`

.

## Creating the building blocks

After defining the boolean algebra in Church Encoding, we are ready to start building it in Clojure.

### Lambdas

Clojure has built-in anonymous functions, which can be used to simulate functions in Lambda Calculus. For example, the identity function would look like this:

```
(fn [x] x)
```

`fn`

is the anonymous function definition.`[x]`

is the parameter of the function.`x`

is the body of the function.

Does this look familiar? We could rewrite this expression in Lambda Calculus:

```
(Î»x.x)
```

Both expressions look quite similar. This is because Clojure is based on Lambda Calculus. But, what if we could add some syntactic sugar to the recipe and make Clojureâ€™s anonymous functions even closer to their Lambda Calculus counterparts?

### Defining a Î»-macro

Macros in Clojure are extremely powerful. They are used to extend the language. Letâ€™s redefine the anonymous function as a Î» function:

```
(defmacro Î»
[args & body]
`(fn [~args] ~@body))
```

This macro will take the first element after Î» and make it the single argument of an anonymous function, which will take the rest of the form as the body. This allows us to rewrite the original identity function `(fn [x] x)`

as `(Î» x x)`

, which is very close to the Lambda Calculus syntax `(Î»x.x)`

.

Now, we can start building the boolean algebra using our new shiny syntax.

### Booleans

Letâ€™s start with `true`

and `false`

. As they are reserved words in Clojure, we are going to rename them to `T`

and `F`

:

```
; true = Î»a.Î»b.a
(def T
(Î» a (Î» b a)))
; false = Î»a.Î»b.b
(def F
(Î» a (Î» b b)))
```

Notice that we are using `def`

instead of `defn`

, to define the term, which is a higher-order function (returning a function, in this case). The difference between them is that `defn`

will be evaluated every time itâ€™s called, whereas `def`

will only be evaluated once. This is not strictly necessary for our purposes, but doing so, we can reinforce the idea that lambda expressions always return the same result when evaluated.

The boolean operators implementation follows a similar idea to `true`

and `false`

:

```
; and = Î»p.Î»q.p q p
(def And
(Î» p (Î» q ((p q) p))))
; or = Î»p.Î»q.p p q
(def Or
(Î» p (Î» q ((p p) q))))
; not = Î»p.Î»a.Î»b.p false true
(def Not
(Î» p ((p F) T)))
; xor = Î»a.Î»b.a (not b) b
(def Xor
(Î» a (Î» b ((a (Not b)) b))))
; if = Î»p.Î»a.Î»b.p a b
(def If
(Î» p (Î» a (Î» b ((p a) b)))))
```

## A boolean calculator

Now that we have implemented our boolean algebra, we can start evaluating boolean expressions. In order to help us check the results, we can add a function to convert a Î»-boolean into a Clojure boolean:

```
(def toBoolean
(Î» f ((f true) false)))
```

Letâ€™s try to evaluate a few expressions:

```
; true AND true
=> (toBoolean ((And T) T))
true
```

Similarly,

```
; true AND false
=> (toBoolean ((And T) F))
false
```

To evaluate more complex expressions, we just need to nest boolean expressions:

```
(deftest Î»-booleans
(testing "Boolean expressions"
;T AND T AND F OR T
(is (= (toBoolean ((And T) ((And T) ((Or F) T))))
true))
;T AND F AND F OR T AND T
(is (= (toBoolean ((And T) ((And F) ((Or F) ((And T) T)))))
false))
;T OR (F AND F OR T AND T)
(is (= (toBoolean ((Or T) ((And F) ((Or F) ((And T) T)))))
true))
;F OR (F AND F OR F AND T)
(is (= (toBoolean ((Or F) ((And F) ((Or F) ((And F) T)))))
false))
;F OR F AND F OR T AND T
(is (= (toBoolean ((And ((Or F) F)) ((And T) ((Or F) T))))
false))
;T OR F AND F OR T AND T
(is (= (toBoolean ((And ((Or T) F)) ((And T) ((Or F) T))))
true))))
```

You can find the source code here.

## Conclusion

The Church-Turing Thesis states that any computation that is Turing computable can be represented using Church encoding. Weâ€™ve seen how to do this for boolean expressions. In part 2, we will implement a numerals algebra.

## References

Originally published on srodrigo.me

## Related Jobs

## Related Articles

## Related Issues

- Started
- 0
- 17
- Intermediate

- Submitted
- 1
- 0
- Intermediate

### Get hired!

#### Sign up now and apply for roles at companies that interest you.

Engineers who find a new job through WorksHub average a *15%* increase in salary.