Switch more posts to work with KaTeX and the latex macro
All checks were successful
continuous-integration/drone/push Build is passing

This commit is contained in:
2020-03-04 14:07:05 -08:00
parent 80d722568e
commit 67ecc741d0
5 changed files with 174 additions and 123 deletions

View File

@@ -29,50 +29,69 @@ add l r = l + r
```
Lambda calculus is all about abstraction into functions, and the application of these functions. In lambda calculus, abstraction looks like this:
$$
{{< latex >}}
\lambda \ x \ . \ t
$$
{{< /latex >}}
This reads: a function that, when given a variable `x`, evaluates to `t`. Usually, `t` is an expression using `x`. For example, suppose that there exists a function `double`. If we wanted to create a function that multiplies a number by 4, we can write it as follows:
$$
{{< latex >}}
\lambda \ x \ . \ \text{double} \ (\text{double} \ x)
$$
{{< /latex >}}
Here, we see function application: the function `double`, which takes a number and returns a number, is given the parameter `x`. The result of evaluating `double x` is then
given as a parameter to `double` again, thereby multiplying `x` by 4. Let's use this new fuction to multiply some number y by 4:
$$
{{< latex >}}
(\lambda \ x . \ \text{double} \ (\text{double} \ x)) \ y
$$
{{< /latex >}}
Here, we see both abstraction and application: we abstract the process of multiplying a number by 4, and then apply that process to a variable y. Application works by simply replacing the variable before the dot in the abstraction with the value that's being passed in. This is called __binding__. In our case, we're binding the variable `x` to the value of `y`. This results in the following:
$$
{{< latex >}}
\text{double} \ (\text{double} \ y)
$$
{{< /latex >}}
Since we have no idea what the body (the expression after the dot) of `double` looks like, we cannot simplify this any further.
### Currying
This is nice and all, but what about functions of more than one variable? What if we want to add numbers, like in our original example of abstraction? If lambda abstraction has only one variable on the left of the dot, how can we represent such functions? Surely there has to be a way, since we claim this little language can represent any computation that can be done on a computer. The answer is fairly simple. We use abstraction twice:
$$
{{< latex >}}
\lambda \ x \ . \ \lambda \ y \ . \ x + y
$$
{{< /latex >}}
The second abstraction is inside the first. It can be equivalently written:
$$
{{< latex >}}
\lambda \ x \ . \ (\lambda \ y \ . \ x + y)
$$
{{< /latex >}}
I've done something here that you might've accepted without questioning, but that I can't ignore without feeling guilty. I've assumed that our lambda calculus has numbers and addition. This __isn't needed__ for the language to be Turing complete. However, it makes for much nicer examples, so we'll stick with it here. Let's move on to an example of using our function to add two numbers:
$$
{{< latex >}}
(\lambda \ x \ . \ \lambda \ y \ . \ x + y) \ 1 \ 2
$$
{{< /latex >}}
Function application is left associative. This is equivalently written as:
$$
{{< latex >}}
((\lambda \ x \ . \ \lambda \ y \ . \ x + y) \ 1) \ 2
$$
{{< /latex >}}
First, we bind `x` to 1:
$$
{{< latex >}}
(\lambda \ y \ . \ 1 + y) \ 2
$$
{{< /latex >}}
Then, we bind `y` in the resulting expression to 2:
$$
{{< latex >}}
1 + 2
$$
{{< /latex >}}
Well, there it is. We can extend this logic to functions of 3, 4, and more variables.
### Partial Function Application
@@ -81,30 +100,38 @@ Our way of defining functions of more than one variable leads to a curious effec
add(1);
```
Oh no! The compiler won't like this; `add` takes two parameters, and we've only given it one. However, when we try to do the same thing in lambda calculus:
$$
{{< latex >}}
(\lambda \ x \ . \ \lambda \ y \ . \ x + y) 1
$$
{{< /latex >}}
As before, we can bind `x` to 1:
$$
{{< latex >}}
\lambda \ y \ . \ 1 + y
$$
{{< /latex >}}
This is not an error! All we've done is produce a function that takes the remaining parameter, `y`, and adds it to the parameter we've already passed in. This is an example of a partially applied function. Effectively, this new function we've produce is waiting for the remaining parameters we haven't yet given it. Once we do, it'll behave just like it would've if we passed all the parameters in together.
### First Class Functions
So far, we've only been applying our functions to numbers. But it doesn't have to be this way! Functions can also serve as parameters to other functions. For example, we can imagine a function that takes another function `f`, which transforms a number somehow, and then applies it to a number two times. The first parameter of such a function would be `f`, and the other would be some number `x`. In the body, we will apply `f` to the result of applying `f` to `x`:
$$
{{< latex >}}
\lambda \ f \ . \ \lambda \ x \ . \ f (f \ x)
$$
{{< /latex >}}
This might look kind of familiar! Let's apply this function to our previously mentioned `double` function:
$$
{{< latex >}}
(\lambda \ f . \ \lambda \ x \ . \ f (f \ x)) \ double
$$
{{< /latex >}}
Just like in our previous examples, we simply replace `f` with `double` during application:
$$
{{< latex >}}
\lambda \ x \ . double \ (double \ x)
$$
{{< /latex >}}
We've now created our multiply-by-four function! We can create other functions the same way. For instance, if we had a function `halve`, we could create a function to divide a number by 4 by applying our "apply-twice" function to it.
### Church Encoded Integers
@@ -112,26 +139,34 @@ We can represent numbers using just abstraction and application. This is called
Now, let's try represent addition. Addition of two numbers `a` and `b` would be done by taking a function `f` and applying it the first number of times, and then applying it the second number more times. Since addition must take in numbers `a` and `b`, which are functions of two variables, and return a number, we will end up with
something like this:
$$
{{< latex >}}
\lambda \ a \ . \ \lambda \ b \ . \ ??
$$
{{< /latex >}}
What goes in the body of the second lambda? We know that a number is a function which takes a function and a variable to apply the function to:
$$
{{< latex >}}
\lambda \ f \ . \ \lambda \ v \ . \ ??
$$
{{< /latex >}}
Since the addition of a and b must return a number, we then have the following:
$$
{{< latex >}}
\lambda \ a \ . \ \lambda \ b \ . \ \lambda \ f \ . \ \lambda \ v \ . \ ??
$$
{{< /latex >}}
So how do we apply `f` the first number of times, and then apply it the second number more times? Well, `a f` is a partially applied function that, when given a variable, will apply `f` to that variable the first number of times. Similarly, `b f` is a partially applied function that will apply `f` the second number of times. We feed `v` into `a f`, which applies `f` the first number of times and returns the result. We then feed that into `b f`. This results in:
$$
{{< latex >}}
\lambda \ a \ . \ \lambda \ b \ . \ \lambda \ f \ . \ \lambda \ v \ . \ b \ f \ (a \ f \ v)
$$
{{< /latex >}}
Phew! Now let's try multiplication. The product of two numbers would apply the function the first number of times, the second number of times. As we already know, `a f` will get us a function that fulfills the first part of this requirement. Then we can apply _that function_ the second number of times, which would give us what we want:
$$
{{< latex >}}
\lambda \ a \ . \ \lambda \ b \ . \ \lambda \ f \ . \ \lambda \ v \ . \ b \ (a \ f) \ v
$$
{{< /latex >}}
Neat!