Hello Internet. In this blog post we are going to learn some type theory, which is a field that lies in the intersection of mathematics and computer science. We are eventually going to learn the variant called Martin-Loef Type Theory, or MLTT for short. To motivate it, we are going to try answering the question, “What is a valid program?”

Not every program makes sense, for example what would a program consisting of the single expression `x + 2`

mean? If we don’t know what `x`

is, then we don’t know what the meaning of that expression is. More importantly, a program can combine expressions that are incompatible with each other. For example, if `+`

is the usual operation that adds two numbers then an expression like `"abc" + 5`

can’t be given a meaning. To avoid such meaningless programs, we should talk about the different kinds of data and the valid operations on these pieces of data. In Type Theory, we call the different kinds of data “types.” Type theory gives each piece of data a type, and the type tells you how the data is constructed and how to write a meaningful program that processes that data. We will be working at an informal level of detail, much more detail will be required if for example, we want to implement a programming language based on type theory. Also MLTT is quite complex at first glance, so we’ll build our way there in small steps.

## System T

System T has two types. The first one is natural numbers, whose type we’ll call . The second one is functions , which take a value of type and return a a value of type . So an example function type will be , any value of this type will be a function that maps natural numbers to natural numbers. Before we jump in, it’ll be useful to introduce some notation. An obviously useful piece of notation will be , which means has the type , for example we expect to prove that , meaning is a natural number. Occasionally, I will say 5 is an element of the type of natural numbers, to mean .

Let’s start defining the function type.

What is a function ? It is a machine, that allows you to put in a value of type in and get out a value of type . For example, the statement means that assuming you have a natural number , you can get another one . To express this idea more formally, we need a way of asserting that “Given certain assumptions, some value has the type “. We write this as , which means given the assumptions , the value has the type . This assertion is pronounced “ entails that has the type . The assumptions will be of the form , which simply means that we are assuming that has the type , has the type and so on. In Type Theory, we call the rule that tells you how to use a value of a given type the *elimination* rule. So for functions we get the following:

**Elimination Rule for Functions** If , and then . In other words, if certain assumptions allow you to make a function then you can plug in a value of the appropriate type and get a particular type of output. Technically speaking we need one such axiom for every expression , and , and we need to write down a grammar that defines all the valid expressions, I’ll leave that to you it’s not that difficult.

Okay, but how do you *make* a function? Well, let’s say you are making a function with the type , for example. You have to create a program with a variable , which will be a placeholder for the input to the function. But, in order for your function to really have the type , you can’t just use any old expression involving , you have to make sure that whenever is a natural number then that expression is also a natural number. So, in conclusion if you have an expression such that given the assumption , we have , then you can construct a function → . The notation for this function is which is shorthand for “The function which takes as input and returns as output. If we generalise to arbitrary input and output types, we get this rule:

**Introduction Rule for Functions** If , then .

Let me give an example of using the above two rules. Let’s assume that we have a function that adds one to a natural number . How do we define a function that adds two to a natural number? Well, by the elimination rule for functions. Applying the elimination rule again, we have . The introduction rule then allows us to form the function , that takes in and adds one to it twice. I’ll introduce some shorthand for this definition. Let us write to mean that the function is defined to be equal to , so for example we would write

The elimination rule tells us that we use a function by applying it to input. The natural question is, “What is the resulting value of the expression?” Since, we hope, every function is made using the introduction rule, we just have to specify what means when . Intuitively, what should happen is that we substitute for in , however defining this is quite difficult. Let’s say that, after a lot of work you managed to define integration, and a type of real numbers . Consider the function . Assuming that , how do we calculate ? The wrong thing to do would be to simply replace the symbol for the symbol and get , because the two uses of are different, one is the variable we are integrating with respect to and the other is the input to the function. Making sure that different variables which have the same name don’t get confused with each other, so that instead of the wrong answer we get something like is the primary difficulty with actually defining substitution. For now, we’ll just sweep this detail under the rug. So what we want to say is that the value of is defined to be but with substituted to be . This motivates the idea of definitional equality. We say that two values of the same type, for example, are definitionally equal if is equal to by definition. This means that whenever we have we can always swap it with and get the same program. To give an example, given an appropriate definition of addition will be definitionally equal to , but if we assume that then won’t be definitionally equal to . The definition of addition allows one to *simplify* as being equal to , but can’t be further simplified. Indeed, one actually has to prove that , it isn’t a simple matter of using the definition of addition to write as . The notation is , which means and are definitionally equal to each other as elements of the type . So, without further ado:

**Computation rule for functions** If , and then , where just means “ with substituted with .”

Let me give an example of the above rule. Given our good old function, we derived that , and so by the computation rule for functions . From now I will be lazy and write instead of for example.

The last thing we want to say, is that every function can be written uniquely using . This results in the totally obvious:

**Uniqueness principle for functions** If , then .

You might have the question, “How do we talk about functions like , which take two arguments?” The answer is quite elegant actually, a function that takes two arguments, one of type and the other of type and returns something of type , is the same thing as a function with the type . Such a function , takes the first argument as input, and returns a function that takes the second argument and outputs the result. For example, we will soon be able to define , such that , is the function that adds three to its input. In a similar way a function with three inputs will have a type like , which is quite cumbersome to write. So I’ll be lazy and write , and while I’m at it I’ll be lazier and write instead of , for example. The idea of defining multi argument functions like this is called *currying*.

Let’s move on from functions and think about how to define the natural numbers. The pattern will be mostly the same, we will give introduction, elimination and computation rules.

**Introduction Rule for Natural Numbers** We always have , i.e. zero is a natural number. Further if, then . The function allows you to construct new natural numbers from old ones, by adding one to them. So, given no assumptions , and so as well. The name humans give to is , and similarly which gives us the number and so on. We call and the *constructors* for , a constructor is a *fundamental* way of making an element of a type, these constructors are part of the definition of the type.

**Elimination rule for Natural Numbers** We want to make a function that takes a natural number as input. Let’s think about one of the simplest such functions, the factorial. The factorial is *defined* like this, assuming a proper definition of multiplication:

At first glance, it does seem kind of circular to define any function in terms of itself, like what the second equality tries to do. However, this definition makes sense, and always produces a natural number for any , here is some intuition about how this works. Say we want to simplify , we can use the definition like this:

In the above calculation we use the definition , and apply the second equation above. We repeat this process until we get to , after which we may apply the first equation. So the reason such “circular” definition works, is because we reduce the number step by step from to repeatedly until we get , and we know that this will eventually reach since and are the only ways we gave to *make* a natural number given in the introduction rule. Computer scientists may \mathrm{rec}ognise this idea as *recursion*. So, we should define functions by their values and , and we should be able to define in terms of and . This elimination rule is kind of intuitive given the introduction rules above. Now, some definitions may look valid, but may run forever for example if we define:

Then, we are simply defining the value of the function as itself, which doesn’t help one to compute the value of the function. Let’s say I try to work out , we’ll the second rule allows me to write this as , and I can apply the second rule again and get , and so on, we’re just going in circles. To prevent this, the elimination rule is carefully designed to make sure that when you define you may only use the values of and , and in particular you can’t use say . So here is the elimination rule:

Given the following data

We have:

Here I’m being lazy again, and I’m leaving out the assumptions since obviously these rules will apply whatever assumptions you have. The first piece of data you need to make a function , is the value of , this is given by . The next piece of data you need, is the value of given and the value of , this is provided by which is a function. The function takes as input , the value of and returns the value of , phrasing the elimination rule this way makes sure that we can only define sensible functions by recursion. We call the recursor for the type of natural numbers.

Let us do an example. We will define : We will have to write where is a function , whose definition must depend on . We will define this function by recursion, since we want , and , so here we go:

Now strictly speaking, we have to write the output type of the recursor but it turns out that there is an automatic algorithm for figuring output types, that even works in languages much more general than System T. So I’ll usually leave them out.

The only thing we need to make sure is that we really have and , and for this we need a computation rule.

**Computation rule for Natural Numbers** Given , we have:

The equalities for follow as a special case of this. Now it’s your turn:

**Exercise** Write the definitions for addition, multiplication and exponentiation using . After that, just so you can see how powerful can be when combined with other constructs we’ve been talking about define the Ackermann function, , which satisfies the following equalities:

So that’s the definition of System T.

One of the things we would want to add to System T are pairs, so that we can talk about multiple pieces of data at once. For this we define the product type of pairs where and , the rules are intuitive enough that I would encourage you to figure them out yourself before reading on.

**Introduction Rule** Given and , we have

**Elimination Rule** Given , we have

**Computation Rule** Given , , and we have:

Finally, often in programming we have to talk about either having one kind of data or a different kind. For example, we might want to say “When I look up a value in a table, I either get a result or I get nothing.” For this we will introduce the disjoint union which either contains a value of type or a value of type .

**Introduction Rule** Given , we have , and similarly given we have .

**Elimination Rule** Given and we have , or in other words if you can get an from either an or a then you can get an from a value with the type .

**Computation Rule** Given , , we have:

- Assuming , we have .
- Assuming , we have .

Now, we’ll introduce some more abbreviations. It is quite cumbersome to write out recursors like all the time, so I’ll use what’s called pattern matching notation. Here is an example, we can define using pattern matching like this:

So first of all I wrote for example, instead of , also I just wrote the definitional equalities I want satisfied instead of defining the function with the recursor. Of course, if one is too sloppy with pattern matching notation one can get functions that aren’t definable with the recursor, but once you’ve defined a few functions with the recursor you get a lot of intuition about what how to translate between pattern matching and recursors. In fact, many programming languages based on type theory can automatically translate between recursors and pattern matching.

## Logic

Now you may be wondering, “How does this have anything to do with mathematics?” Well, after very little more we can already do logic.

The main idea is that you can define any statement, by describing what it would take to prove that statement. For example, you prove the statement “ and are true,” by giving two proofs, you first prove and then you prove , this constitutes a *definition* of the the statement “ and are true.” So we will model *propositions as types*, where a proposition is viewed as the same thing as the type of its proofs.

So let’s say you have a proposition , and another one . We will call the type whose elements are proofs of , by the same name and we will do the same thing with , since there a statements meaning is given by the data one needs to prove it there is no need to distinguish between the two.

So what is a proof of “ and “? Well, it is just a pair of proofs, one for and another for , which just an element of the product type . Similarly, what is a proof of “ or “? It’s just either a proof of , or a proof of or in other terms an element of the disjoint union . In the same vein, the statment “ implies ,” is proven by giving a process that transforms proofs of into proofs of , in other words a function . The only thing we are missing to do propositional logic is the propositions true () and false (). Let’s define them using propositions as types, starting with true.

How do you prove the proposition “true.” Well, the proposition “true,” is trivially true, and in particular you don’t need to do anything to prove it true. So the corresponding type, which we call , has a constructor that is trivial in the sense that it doesn’t require any arguments. Let’s define , but again I would advise you to come up with its introduction, elimination and computation rules yourself

**Introduction Rule** We always have .

**Elimination Rule** Given , we have a function .

**Computation Rule** Assuming , we have .

Okay, how about “false.” How do you prove “false”? The short answer is that you can’t, “false” is not trivially provable. If our logic is inconsistent, then maybe you can make a proof of false but it won’t be a simple matter of invoking one of the constructors. However, one can prove in the standard foundation of mathematics that this logic is consistent, and so it’s just as reliable as any result in mathematics. Now on to the (very simple definition) of the type which corresponds to the proposition “false.”

There is no introduction rule, since there shouldn’t be a way of proving false.

**Elimination Rule** Many people find this elimination rule tricky, so let me phrase it this way. The elimination rules for and have two cases, since these types have two constructors. The elimination rule for say, has one case since it has one constructor. Since has no constructors, it’s elimination rule should have no cases, we can always just claim since there are no constructors to deal with. Logicians call this principle *ex falso quodlibet*, which means “from the false anything follows,” if you assume that false things are true (or equivalently that has an element), then you can prove whatever you want from that.

The last logical notion we need to define, is the proposition “not A”. We will simply define this as the type , since the statement “A is not true” means the same thing as “A implies a falsehood.” Thinking in terms of proofs, you can prove the statement “not A”, by assuming it to be true and showing something that can’t be true. However, such a proof is just a function which takes a proof of as input and returns a proof of , a false statement, as output.

Let’s prove some logical theorems by writing them out as computer programs in System T. Let’s first try to prove , or in English “not A or not B implies, not (A and B).” I’ll write the proof using pattern matching notation, it’s quite a good exercise to translate this into recursors. So, there are two possible cases for the first input:

After pattern matching on the pair, assuming it is of the form we get this:

Now each of the has to have the type , seeing as we can’t explicitly construct we have to use some of our input data somehow. However, in the first case, we have and so , and we can apply a similar thing to the second case. Here is the finished proof

Indeed the proof reads almost naturally, once you get used to type theory. The two lines of the type theoretic proof, are exactly the two cases of an informal proof, so you could read the proof like this: “Suppose (not A) or (not B), then there are two cases. Firstly, we may have (not A) in which case if we also have A and B, then we have A and so (not A) and A is true which is a contradiction. Secondly, we may have (not B), and assuming (A and B) we have B and in particular notB and B is true which is a contradiction.” This is one of the things that makes type theory an elegant foundation of mathematics, whereas traditionally you would have data encoded as sets and you would reason about that data separately using logic, both of these mathematical activities type theoretically speaking are just programming, and the idea of a type unifies them both.

Anyways, that’s quite enough for today. Next time, we’ll generalise type theory by making every type a value, i.e. an element of another type. This will allow us to use propositions as types to reason about our programs, so that we can prove that, for example a certain algorithm sorts a list, or more simply that addition is commutative. We will introduce new types which exploit this, which will lead to a logic with quantifiers and equality. But for now, here are some exercises

**Exercises**

Prove the following statements using propositions as types:

- If , then (if then )
- If , then (not (not .
- If or , and implies , and implies then .

Let’s say that instead of the recursor , I gave you the following function, which basically is the recursor but it doesn’t keep track of the current number while recursing:

Can you define such that it satisfies the definitional equalities we listed before?

Keenan NaidooJuly 27, 2016 at 10:45 pmInteresting read!

Dependent Types – MathemafricaOctober 11, 2016 at 10:54 am[…] blog post will carry on from the previous one, and introduce dependent types. So what is a dependent type? To motivate the idea let’s talk […]