As you may be figuring out, there’s a reason why I resisted walking through Gödel’s proof of incompleteness for so long. Incompeteness isn’t a simple proof!

To refresh your memory, here’s a sketch of the proof:

- Take a simple logic. We’ve been using a variant of the Principia Mathematica’s logic, because that’s what Gödel used.
- Show that any statement in the logic can be encoded as a number using an arithmetic process based on the syntax of the logic. The process of encoding statements numerically is called Gödel numbering.
- Show that you can express meta-mathematical properties of logical statements in terms of arithemetic properties of their Gödel numbers. In particular, we need to build up the logical infrastructure that we need to talk about whether or not a statement is
*provable*. - Using meta-mathematical properties, show how you can create an unprovable statement encoded as a Gödel number.

What we’ve done so far is the first two steps, and part of the third. In this post, we saw the form of the Principia’s logic that we’re using, and how to numerically encode it as a Gödel numbering. We’ve start started on the third point in this post, by figuring out just what it means to say that things are encoded arithmetically. Now we can get to the part where we see how to encode meta-mathematical properties in terms of arithmetic properties of the Gödel numbering. In this post, we’re going to build up everything we need to express syntactic correctness, logical validity, and provability in terms of arithmetical properties of Gödel numbers. (And, as a reminder, I’ve been using this translation on Gödel’s original paper on incompleteness.)

This is the most complex part of the incompleteness proof. The basic concept of what we’re doing is simple, but the mechanics are very difficult. What we want to do is define a set of predicates about logical statements, but we want those predicates to be expressed as arithmetic properties of the numerical representations of the logical statements.

The point of this is that we’re showing that done in the right way, arithmetic *is* logic – that doing arithmetic on the Gödel numbers is doing logical inference. So what we need to do is build up a toolkit that shows us how to understand and manipulate logic-as-numbers using arithmetic. As we saw in the last post, primitive recursion is equivalent to arithmetic – so if we can show how all of the properties/predicates that we define are primitive recursive, then they’re arithmetic.

This process involves a *lot* of steps, each of which is building the platform for the steps that follow it. I struggled quite a bit figuring out how to present these things in a comprehensible way. What I ended up with is writing them out as code in a pseudo-computer language. Before inventing this language, I tried writing actual executable code, first in Python and then in Haskell, but I wasn’t happy with the clarity of either one.

Doing it in an unimplemented language isn’t as big a problem as you might think. Even if this was all executable, you’re not going to be able to actually run any of it on anything real – at least not before you hair turns good and gray. The way that this stuff is put together is not what any sane person would call efficient. But the point isn’t to be efficient: it’s to show that this is *possible*. This code is really all about searching; if we wanted to be efficient, this could all be done in a different representation, with a different search method that was a lot faster – but that wolud be harder to understand.

So, in the end, I threw together a simple language that’s easy to read. This language, if it were implemented, wouldn’t really even be Turing complete – it’s a primitive recursive language.

### Basics

We’ll start off with simple numeric properties that have no obvious connection to the kinds of meta-mathematical statements that we want to talk about, but we’ll use those to define progressively more and more complex and profound properties, until we finally get to our goal.

# divides n x == True if n divides x without remainder. pred divides(n, x) = x mod n == 0 pred isPrime(0) = False pred isPrime(1) = False pred isPrime(2) = True pred isPrime(n) = { all i in 2 to n { not divides(i, n) } } fun fact(0) = 1 fun fact(n) = n * fact(n - 1)

Almost everything we’re going to do here is built on a common idiom. For anything we want to do arithmetically, we’re going to find a bound – a maximum numeric value for it. Then we’re going to iterate over all of the values smaller than that bound, searching for our target.

For example, what’s the *n*th prime factor of *x*? Obviously, it’s got to be smaller than *x*, so we’ll use *x* as our bound. (A better bound would be the square root of x, but it doesn’t matter. We don’t care about efficiency!) To find the *n*th prime factor, we’ll iterate over all of the numbers smaller than our bound *x*, and search for the smallest number which is prime, which divides *x*, and which is larger than the *n-1*th prime factor of *x*. We’ll translate that into pseudo-code:

fun prFactor(0, x) = 0 fun prFactor(n, x) = { first y in 1 to x { isPrime(y) and divides(y, x) and prFactor(n - 1, x) < y } }

Similarly, for extracting values from strings, we need to be able to ask, in general, what's the nth prime number? This is nearly identical to `prFactor`

above. The only difference is that we need a different bound. Fortunately, we know that the *n*th prime number can't be larger than the factorial of the previous prime plus 1.

fun nthPrime(0) = 0 fun nthPrime(n) = { first y in 1 to fact(nthPrime(n - 1)) + 1 { isPrime(y) and y > nthPrime(n - 1)) } }

In composing strings of Gödel numbers, we use exponentiation. Given integers *x* and *n*, *x ^{n}*, we can obviously compute them via primitive recursion. I'll define them below, but in the rest of this post, I'll write them as an operator in the language:

fun pow(n, 0) = 1 fun pow(n, i) = n * pow(n, i - 1)

### String Composition and Decomposition

With those preliminaries out of the way, we can get to the point of defining something that's actually about one of the strings encoded in these Gödel numbers. Given a number *n* encoding a string, `item(n, x)`

is the value of the *n*th character of x. (This is slow. This is *really* slow! We're getting to the limit of what a very powerful computer can do in a reasonable amount of time. But this doesn't matter. The point isn't that this is a good way of doing these things: it's that these things are *possible*. To give you an idea of just how slow this is, I started off writing the stuff in this post in Haskell. Compiled with GHC, which is a very good compiler, `item`

to extract the 6th character of an 8 character string took around 10 minutes on a 2.4Ghz laptop.)

fun item(n, x) = { first y in 1 to x { divides(prFactor(n, x) ** y, y) and not divides(prFactor(n, x)**(y+1), x) } }

Given a string, we want to be able to ask how long it is; and given two strings, we want to be able to concatenate them.

fun length(x) = { first y in 1 to x { prFactor(y, x) > 0 and prFactor(y + 1, x) == 0 } } fun concat(x, y) = { val lx = length(x) val ly = length(y) first z in 1 to nthprime(lx + ly)**(x + y) { (all n in 1 to lx { item(n, z) == item(n, x) }) and (all n in 1 to ly { item(n + lx, z) == item(n, y) }) } } fun concatl([]) = 0 fun concatl(xs) = { concat(head(xs), concatl(tail(xs))) } fun seq(x) = 2**x

We want to be able to build statements represented as numbers from other statements represented as numbers. We'll define a set of functions that either compose new strings from other strings, and to check if a particular string is a particular kind of syntactic element.

# x is a variable of type n. pred vtype(n, x) = { some z in 17 to x { isPrime(z) and x == n**z } } # x is a variable pred isVar(x) = { some n in 1 to x { vtype(n, x) } } fun paren(x) = concatl([gseq(11), x, gseq(13)]) # given the Gödel number for a statement x, find # the Gödel number for not x. fun gnot(x) = concat(gseq(5), paren(x)) # Create the number for x or y. fun gor(x, y) = concatl([paren(x), seq(7), paren(y)]) # Create the number for 'forall x(y)'. fun gforall(x, y) = concatl([seq(9), seq(x), paren(y)]) # Create the number for x with n invocations of the primitive # successor function. fun succn(0, x) = x fun succn(n, x) = concat(seq(3), succn(n - 1, x)) # Create the number n using successor and 0. fun gnumber(n) = succn(n, seq(1)) # Check if a statement is type-1. pred stype_one(x) = { some m in 1 to x { m == 1 or (vtype(1, m) and x == succn(n, seq(m)) } } # Check if a statement is type n. pred fstype(1, x) = stype_one(x) pred fstype(n, x) = some v in 1 to x { vtype(n, v) and R(v) } }

That last function contains an error: the translation of Gödel that I'm using says `R(v)`

without defining `R`

. Either I'm missing something, or the translator made an error.

### Formulae

Using what we've defined so far, we're now ready to start defining formulae in the basic Principia logic. Forumlae are strings, but they're strings with a constrained syntax.

pred elFm(x) = { some y in 1 to x { some z in 1 to x { some n in 1 to x { stype(n, y) and stype(n+1, z) and x == concat(z, paren(y)) } } } }

All this is doing is expressing the grammar rule in arithmetic form: an elementary formula is a predicate: *P(x)*, where *x* is a variable on level *n*, and *P* is a variable of level *x + 1*.

The next grammar rule that we encode this way says how we can combine elementary formulae using operators. There are three operators: negation, conjunction, and universal quantification.

pred op(x, y, z) = { x == gnot(y) or x == gor(y, z) or (some v in 1 to x { isVar(v) and x == gforall(v, y) }) }

And now we can start getting complex. We're going to define the idea of a *valid sequence* of formulae. *x* is a valid sequence of formulae when it's formed from a collection of formulae, each of which is either an elementary formula, or is produced from the formulae which occured before it in the sequence using either negation, logical-or, or universal quantification.

In terms of a more modern way of talking about it, the syntax of the logic is a grammar. A formula sequence, in this system, is another way of writing the parse-tree of a statement: the sequence is the parse-tree of the last statement in the sequence.

pred fmSeq(x) = { all p in 0 to length(x) { elFm(item(n, x)) or some p in 0 to (n - 1) { some q in 0 to (n - 1) { op(item(n,x), item(p, x), item(q, x)) } } } }

The next one bugs me, because it seems wrong, but it isn't really! It's a way of encoding the fact that a formula is the result of a well-defined sequence of formulae. In order to ensure that we're doing primitive recursive formulae, we're always thinking about sequences of formulae, where the later formulae are produced from the earlier ones. The goal of the sequence of formula is to produce the last formula in the sequence. What this predicate is really saying is that a formula is a valid formula if there is some sequence of formulae where this is the last one in the sequence.

Rephrasing that in grammatical terms, a string is a formula if there is valid parse tree for the grammar that produces the string.

pred isFm(x) = { some n in 1 to nthPrime(length(x)**2)**(x*length(x)**2) { fmSeq(n) } }

So, now, can we say that a statement is valid because it's parsed according to the grammar? Not quite. It's actually a familiar problem for people who write compilers. When you parse a program in some language, the grammar doesn't usually specify variables must be declared before they're used. It's too hard to get that into the grammar. In this logic, we've got almost the same problem: the grammar hasn't restricted us to only use bound variables. So we need to have ways to check whether a variable is bound in a Gödel-encoded formula, and then use that to check the validity of the formula.

# The variable v is bound in formula x at position n. pred bound(v, n, x) = { isVar(v) and isFm(x) and (some a in 1 to x { some b in 1 to x { some c in 1 to x { x == concatl([a, gforall(v, b), c]) and isFm(b) and length(a) + 1 ≤ n ≤ length(a) + length(forall(v, b)) } } }) } # The variable v in free in formula x at position n pred free(v, n, x) = { isVar(v) and isFm(x) and (some a in 1 to x { some b in 1 to x { some c in 1 to x { v == item(n, x) and n ≤ length(x) and not bound(v, n, x) } } }) } pred free(v, x) = { some n in 1 to length(x) { free(v, n, x) } }

To do logical inference, we need to be able to do things like replace a variable with a specific infered value. We'll define how to do that:

# replace the item at position n in x with y. fun insert(x, n, y) = { first z in 1 to nthPrime(length(x) + length(y))**(x+y) { some u in 1 to x { some v in 1 to x { x == concatl([u, seq(item(n, x)), v]) and z == concatl([u, y, v]) and n == length(u) + 1 } } } }

There are inference operations and validity checks that we can only do if we know whether a particular variable is free at a particular position.

# freePlace(k, v, k) is the k+1st place in x (counting from the end) # where v is free. fun freePlace(0, v, x) = { first n in 1 to length(x) { free(v, n, x) and not some p in n to length(x) { free(v, p, x) } } } fun freePlace(k, v, x) = { first n in 1 to freePlace(n, k - 1, v) { free(v, n, x) and not some p in n to freePlace(n, k - 1, v) { free(v, p, x) } } } # number of places where v is free in x fun nFreePlaces(v, x) = { first n in 1 to length(x) { freeplace(n, v, x) == 0 } }

In the original logic, some inference rules are defined in terms of a primitive substitution operator, which we wrote as *subst[v/c](a)* to mean substitute the value *c* for the variable *c* in the statement *a*. We'll build that up on a couple of steps, using the `freePlaces`

function that we just defined.

# Subst1 replaces a single instance of v with y. fun subst'(0, x, v, y) = x fun subst1(0k, x, v, y) = insert(subst1(k, x, v, y), freePlace(k, v, x), y) # subst replaces all instances of v with y fun subst(x, v, y) = subst'(nFreePlaces(v, x), x, v, y)

The next thing we're going to do isn't, strictly speaking, absolutely necessary. Some of the harder stuff we want to do will be easier to write using things like implication, which aren't built in primitive of the Principia logic. To write those as clearly as possible, we'll define the full suite of usual logical operators in terms of the primitives.

# implication fun gimp(x, y) = gor(gnot(x), y) # logical and fun gand(x, y) = gnot(gor(gnot(x), gnot(y))) # if/f fun gequiv(x, y) = gand(gimp(x, y), gimp(y, x)) # existential quantification fun gexists(v, y) = not(gforall(v, not(y)))

### Axioms

The Peano axioms are valid logical statements, so they have Gödel numbers in this system. We could compute their value, but why bother? We know that they exist, so we'll just give them names, and define a predicate to check if a value matches them.

The form of the Peano axioms used in incompleteness are:

- Zero:
*¬(succ(x*_{1}) = 0) - Uniqueness:
*succ(x*_{1}) = succ(y_{1}) Rightarrow x = y - Induction:
*x*_{2}(0) ∧ ∀x_{1}(x_{2}(x_{1})⇒ x_{2}(succ(x_{1}))) ⇒ ∀x_{1}(x_{2}(x_{1}))

const pa1 = ... const pa2 = ... const pa3 = ... pred peanoAxiom(x) = (x == pa1) or (x == pa2) or (x == pa3)

Similarly, we know that the propositional axioms must have numbers. The propositional

axioms are:

I'll show the translation of the first - the rest follow the same pattern.

# Check if x is a statement that is a form of propositional # axiom 1: y or y => y pred prop1Axiom(x) = some y in 1 to x { isFm(x) and x == imp(or(y, y), y) } } pred prop2Axiom(x) = ... pred prop3Axiom(x) = ... pred prop4Axiom(x) = ... pred propAxiom(x) = prop2Axiom(x) or prop2Axiom(x) or prop3Axiom(x) or prop4Axiom(x)

Similarly, all of the other axioms are written out in the same way, and we add a predicate `isAxiom`

to check if something is an axiom. Next is quantifier axioms, which are complicated, so I'll only write out one of them - the other follows the same basic scheme.

The two quantifier axioms are:

quantifier_axiom1_condition(z, y, v) = { not some n in 1 to length(y) { some m in 1 to length(z) { some w in 1 to z { w == item(m, z) and bound(w, n, y) and free(v, n, y) } } } } pred quantifier1Axiom(x) = { some v in 1 to x { some y in 1 to x { some z in 1 to x { some n in 1 to x { vtype(n, v) and stype(n, z) and isFm(y) and quantifier_axiom1_condition(z, y, v) and x = gimp(gforall(v, y), subst(y, v, z)) } } } } } quanitifier_axiom2 = ... isQuantifierAxiom = quantifier1Axiom(x) or quantifier2Axiom(x)

We need to define a predicate for the reducibility axiom (basically, the Principia's version of the ZFC axiom of comprehension). The reducibility axiom is a schema: for any predicate , . In our primitive recursive system, we can check if something is an instance of the reducibility axiom schema with:

pred reduAxiom(x) = some u in 1 to x { some v in 1 to x { some y in 1 to x { some n in 1 to x { vtype(n, v) and vtype(n+1, u) and not free(u, y) and isFm(y) and x = gexists(u, gforall(v, gequiv(concat(seq(u), paren(seq(v))), y))) } } } } }

Now, the set axiom. In the logic we're using, this is the axiom that defines set equality. It's written as . Set equality is defined for all types of sets, so we need to have one version of axiom for each level. We do that using *type-lifting*: we say that the axiom is true for type-1 sets, and that any type-lift of the level-1 set axiom is also a version of the set axiom.

fun typeLift(n, x) = { first y in 1 to x**(x**n) { all k in 1 to length(x) { item(k, x) ≤ 13 and item(k, y) == item(k, v) or item(k, x) > 13 and item(k, y) = item(k, x) * prFactor(1, item(k, x))**n } } }

We haven't defined the type-1 set axiom. But we just saw the axiom above, and it's obviously a simple logical statement. That mean that it's got a Gödel number. Instead of computing it, we'll just say that that number is called `sa1`

. Now we can define a predicate to check if something is a set axiom:

val sa1 = ... pred setAxiom(x) = some n in 1 to x { x = typeLift(n, sa) } }

We've now defined all of the axioms of the logic, so we can now create a general predicate to see if a statement fits into any of the axiom categories:

pred isAxiom(x) = peanoAxiom(x) or propAxiom(x) or quantifierAxom(x) or reduAxiom(x) or setAxiom(x)

### Proofs and Provability!

With all of the axioms expressible in primitive recursive terms, we can start on what it means for something to be provable. First, we'll define what it means for some statement *x* to be an *immediate consequence* of some statements *y* and *z*. (Back when we talked about the Principia's logic, we said that *x* is an immediate consequence of *y* and *z* if either: y is the formula *z ⇒ x*, or if *c* is the formula ∀v.x).

pred immConseq(x, y, z) = { y = imp(z, x) or some v in 1 to x { isVar(v) and x = forall(v, y) } }

Now, we can use our definition of an immediate consequence to specify when a sequence of formula is a *proof figure*. A proof figure is a sequence of statements where each statement in it is either an axiom, or an immediate consequence of two of the statements that preceeded it.

pred isProofFigure(x) = { (all n in 0 to length(x) { isAxiom(item(n, x)) or some p in 0 to n { some q in 0 to n { immConseq(item(n, x), item(p, x), item(q, x)) } } }) and length(x) > 0 }

We can say that *x* is a proof of *y* if *x* is proof figure, and the last statement in *x* is *y*.

pred proofFor(x, y) = isProofFigure(x) and item(length(x), x) == y

Finally, we can get to the most important thing! We can define what it means for something to be *provable*! It's provable if there's a proof for it!

pre provable(x) = some y { proofFor(y, x) } }

Note that this last one is *not* primitive recursive! There's no way that we can create a bound for this: a proof can be any length.

At last, we're done with these definition. What we've done here is really amazing: now, every logical statement can be encoded as a number. Every proof in the logic can be encoded as a sequence of numbers: if something is provable in the Principia logic, we can encode that proof as a string of numbers, and check the proof for correctness using nothing but (a whole heck of a lot of) arithmetic!

Next post, we'll finally get to the most important part of what Gödel did. We've been able to define what it means for a statement to be provable - we'll use that to show that there's a way of creating a number encoding the statement that something is *not* provable. And we'll show how that means that there is a true statement in the Principia's logic which isn't provable using the Principia's logic, which means that the logic isn't complete.

In fact, the proof that we'll do shows a bit more than that. It doesn't just show that the Principia's logic is incomplete. It shows that *any* consistent formal system like the Principia, any system which is powerful enough to encode Peano arithmetic, *must* be incomplete.

Brandon WilsonThink there might be a typo in

item(n, x):

divides(prFactor(n, x) ** y, y)

should be

divides(prFactor(n, x) ** y, x)

mconcerning ‘fstype(n, x)’: It’s in deed an error in the translation, or rather two: Gödel had ‘R’ where Hitzel elsewhere uses ‘seq’, and it’s …vtype(n,v) ∧

x =R(v)….TimThere’s a bug in the implementation of stype_one: the parentheses are placed the wrong way.

Pingback: Gödel’s Take on Automated Testing Tools | Automation Anywhere | Why You'll Never Have Full Test Coverage Automation Anywhere Blog