Logic

A machine that carries one bit

Write three numbers in base two, one above another, and read the columns from the right. Whether the bottom number is the sum of the top two can be checked with one bit of memory — the carry. That two-state machine is the whole of addition, and machines can be combined, negated and made to guess a missing number. So every sentence about whole numbers built from addition can be decided by building a machine for it, and the same machines can also handle 'x is a power of two', which addition alone cannot say.

Worth reading first: Arithmetic with addition alone · A language that can name a set.

Arithmetic with addition alone found that every question about whole numbers that can be phrased with addition, equality and the quantifiers “for all” and “there exists” has an answer that can be computed. Mojżesz Presburger proved it in 1929 by eliminating quantifiers: each “there exists” can be traded for a condition on remainders, until nothing is left but a check a computer can do. That essay mentioned, in its last pages, that the same sets of numbers are exactly those a finite machine can read. This essay takes that second route seriously, because it is a different method of proof and it reaches further.

The method rests on one observation about addition: checking a sum needs almost no memory.

13 + 11 = 24, checked column by column with one bit of memory. Binary addition of 13 and 11: columns read from the least significant, carries 011110 from the right, sum 24.
Fig. 1 13 + 11 = 24 written in base two, one number to a row, most significant digit on the left. The machine reads the columns from the right, one column a step, and all it remembers from one column to the next is the carry, 0 or 1, shown above each column. A sum is right exactly when every column is allowed and no carry remains at the end.

Write x=13x = 13, y=11y = 11 and z=24z = 24 in base two, one above another, padded with zeros to the same length. Read the columns from the right. Each column is three bits, one from each number. The column is consistent with z=x+yz = x + y exactly when the top two bits plus the carry from the previous column leave the bottom bit when halved, and the new carry is what is left over. Nothing else about the earlier columns matters — not how many there were, not what digits they held, only whether they left a one to carry. A machine that remembers only the carry, one bit, can check any sum of any size.

Addition as a machine with two states

That machine can be drawn in full.

Addition as a machine with two states. Two-state carry automaton over columns (x, y, z): from carry 0 staying on 000 011 101, to carry 1 on 110; from carry 1 staying on 010 100 111, back on 001.
Fig. 2 The whole of addition as a machine: two states, remembering the carry. Each arrow is labelled with the columns (x y z) it accepts; a column not on any arrow from the current state stops the machine and rejects. It starts and must finish in “carry 0”. Checked on every x and y below 64 against the right sum and two wrong ones: it accepts exactly the true sums.

It has two states, “carry 0” and “carry 1”. From “carry 0”, the columns 000000, 011011 and 101101 keep the carry at nought — nothing, or a single one that lands in zz — and the column 110110 moves to “carry 1”, since 1+11 + 1 is 00 with one carried. From “carry 1” the columns 010010, 100100 and 111111 keep it, and 001001 returns to “carry 0”. Any other column contradicts the sum and the machine stops. It must start and finish with no carry.

The figure checks it on every pair of numbers below 6464, offering the true sum and two false ones, and it accepts exactly the true sums. A finite automaton — a machine with a fixed, finite number of states, reading its input one symbol at a time — recognises the relation x+y=zx + y = z, and it needs only two states to do it.

Machines combine, and quantifiers become guesses

Having a machine for addition is the starting point. The power of the method is that machines can be combined in exactly the ways sentences are built.

“And” is running two machines side by side, each in its own state, and accepting when both accept: the product of the machines. “Not” is swapping the accepting and rejecting states of a machine that never stops early. “There exists a yy” is the interesting one. A machine for a formula about xx and yy reads two tracks of bits. To decide “there exists yy such that…”, erase the yy track and let the machine guess each bit of yy as it goes: it accepts if some sequence of guesses leads to acceptance. That is a nondeterministic machine, and a standard construction, which a language that can name a set used for sets of positions in words, turns it back into an ordinary one by tracking every set of states the guesses could have reached.

“For all” is “not there exists not”. So every sentence built from addition, equality, “and”, “not” and the quantifiers turns into a machine, step by step, and a sentence with no free variables turns into a machine that reads nothing and either accepts or does not. That decides the sentence. J. Richard Büchi introduced the method in 1960, and it gives a proof of Presburger’s theorem that never mentions remainders at all: a quantifier is a shadow described “there exists” as projection, and in this setting the projection is erasing a track.

One sentence, decided

It helps to follow the method through one sentence. Take “every number is even or one more than an even number”:

∀x ∃y (x=y+y ∨ x=y+y+1).\forall x\ \exists y\ \big(x = y + y \ \lor\ x = y + y + 1\big).

Start inside. The formula x=y+yx = y + y is the addition machine with the zz track set to xx and both summand tracks set to yy — equivalently, the doubling machine drawn below, which checks that xx’s bits are yy’s bits shifted up one place. The formula x=y+y+1x = y + y + 1 is the same with a one added, which the machine handles by starting in “carry 1” instead of “carry 0”. “Or” is the product machine accepting when either part does.

Next, “there exists yy”: erase the yy track and let the machine guess it. The guessing machine accepts a string of xx’s bits exactly when some yy makes the formula true, and after the subset construction it turns out to be the machine that accepts every string: whatever xx is, halving it and rounding down gives a yy that works. Finally, “for all xx” is “not there exists xx not”, and since the machine accepts everything, its complement accepts nothing, the existential over that accepts nothing, and its complement accepts everything. The sentence is true.

For a sentence like this the answer is obvious. The method does not care: the same four operations, applied to a sentence nobody can see through, still end with a machine that accepts or does not, and the answer is read off without insight.

Machines for more than addition

The method is not tied to addition. Any relation that a machine can recognise, read in base two, can be added to the language, and the sentences built from it remain decidable.

A machine for the powers of two, and one for doubling. Automaton accepting binary numerals of powers of two (read least significant first), and a two-track automaton accepting x = 2y.
Fig. 3 Left: a machine that reads a number’s binary digits from the least significant and accepts exactly the powers of two — any number of 0s, then one 1, then only 0s. Right: a machine reading pairs (x, y) that accepts exactly when x = 2y, by remembering y’s previous digit and demanding that x’s digit equal it. Both are checked on every input below 512 and 128 respectively.

“xx is a power of two” is easy for a machine in base two: a power of two is a single one followed by zeros, and a machine with two states, plus a dead state for a second one, recognises it. But it cannot be said with addition alone. Arithmetic with addition alone showed that every set addition can define is eventually periodic — from some point on it repeats with a fixed period — and the powers of two are not. So a machine can see something that Presburger’s formulas cannot.

The largest power of two dividing each number, as a grid. Grid of the relation v2(x) = y for x up to 64: a ruler pattern.
Fig. 4 The relation “y is the largest power of two dividing x”, drawn as a grid: x from 1 to 64 across, the powers of two up the side, and a dot where the relation holds — a ruler, half the marks at 1, a quarter at 2, an eighth at 4. A machine with two states recognises it, reading x and y together.

A little more is possible. The relation “yy is the largest power of two dividing xx” — which picks out the lowest one in xx’s binary expansion — draws a ruler: every odd number is divisible only by 11, every second even number by 22 and no more, and so on. A two-state machine recognises it by reading xx and yy together and demanding that yy’s single one sit exactly at xx’s first one. Adding this relation to addition gives Büchi arithmetic, and every sentence in it is decidable by the same method. Alexei Semenov showed in 1977 that adding just “xx is a power of two” keeps arithmetic decidable, the fact arithmetic with addition alone closed on; the machines explain why.

How small the machines are

The machines for these basic relations are tiny, and that is what makes the method work.

Properties of numbers and the fewest states that recognise them. x is even: 3 states; x is a power of two: 3 states; x is a multiple of 3: 3 states; x = 2y: 3 states; x + y = z: 3 states.
Fig. 5 Five properties of whole numbers written in base two, each with the fewest states a machine needs to recognise it when it reads the digits from the least significant, counting a dead state that rejects for good. Every machine was checked against its property on every input up to 255, and every one needs only three states.

Each machine in the table is built directly, checked against its property on every small input, and then shrunk to the fewest states that do the same job by merging states that behave identically on every future input. “xx is even”, “xx is a power of two”, “xx is a multiple of 3”, “x=2yx = 2y” and “x+y=zx + y = z” each need three states, one of them a dead state. None of these counts depends on how large the numbers are. That is the whole point: a fixed, finite memory handles every number at once, and so a machine can answer a question about all whole numbers.

The price is paid elsewhere. Each “there exists” can multiply the number of states exponentially when the guessing machine is turned back into an ordinary one, and a sentence with several alternating quantifiers can need a machine of staggering size. Michael Fischer and Michael Rabin proved in 1974 that no method at all can decide Presburger arithmetic in less than doubly exponential time in the worst case. Decidable is not the same as fast. In practice the sentences people want to decide are usually far from the worst case, and programs that build these machines routinely handle sentences whose machines have millions of states, because the blow-up that the lower bound guarantees for some sentences rarely appears in the sentences that arise from real questions.

Reading from the other end

The machines here read digits from the least significant, the order in which carries flow. Reading the other way, from the most significant digit, addition still needs only finite memory, but the machine must work differently: at each column it does not yet know whether a carry will arrive from the columns to its right. So it guesses — keeps track of both possibilities, “a carry is coming” and “no carry is coming” — and checks each guess against the next column. The guesses cost nothing extra, because there are only two of them, and the subset construction turns the guessing machine back into an ordinary one of about the same size.

That symmetry is a general fact. A set of binary strings recognised by a finite machine reading left to right is also recognised by one reading right to left — the reversal of a regular language is regular — though the second machine can be larger. For the relations of arithmetic the two directions are equally good, and the choice is only a matter of which machine is easier to draw.

The difference matters for a human doing long addition, who writes the digits from the right because that is where the carries start. A machine that may guess does not need to: nondeterminism lets it read in whatever order the input arrives, which is exactly the freedom the quantifier “there exists” was translated into.

Why the squares would break it

The machines have a limit, and it is exactly where arithmetic with addition alone found the edge of decidability. Add “xx is a square” to the language, and arithmetic becomes undecidable, because multiplication can be defined from squares and addition:

xy=12((x+y)2−x2−y2),xy = \tfrac12\big((x + y)^2 - x^2 - y^2\big),

and with multiplication, Gödel’s incompleteness and Turing’s undecidability apply in full. So no machine can recognise the squares in base two. If one could, the method would decide full arithmetic, which no algorithm can read what a program does shows to be impossible. The impossibility of a machine for squares is proved by its consequences, not by staring at the binary digits of 1,4,9,16,25,…1, 4, 9, 16, 25, \ldots — although the digits do look patternless, and the gaps between squares grow in a way no fixed memory can track.

The boundary is therefore sharp and slightly surprising. Powers of two: yes. The largest power of two dividing a number: yes. Squares: no. What the machines can see is exactly what fits in a finite memory read digit by digit, and a relation as simple-looking as “is a square” does not.

When two formulas say the same thing

Machines give one more thing for free. Two formulas in the language define the same set of numbers exactly when their machines accept the same strings, and whether two finite machines accept the same strings can be decided: shrink each to its fewest states, as the table above did, and a machine with the fewest states is unique up to renaming, so the two either match state for state or they do not.

So not only is every sentence decidable, but every question of the form “do these two formulas mean the same?” is decidable too. The six ways of arranging two quantifiers that six sentences from two quantifiers drew — “for every xx there is a yy” against “there is a yy for every xx”, and the rest — can each be turned into a machine for a given relation, and their agreements and disagreements read off by comparing machines. For the relations of addition, the logic of quantifier order becomes the combinatorics of finite machines.

Theorems proved by machine

The method is not only a theoretical device. Programs that build these machines from sentences are used to prove theorems about sequences that are themselves defined by machines — automatic sequences, like the Thue–Morse sequence, which records whether each number has an even or odd count of ones in binary. Questions such as “does this sequence contain a block that repeats three times in a row?” become sentences in Büchi arithmetic extended by the sequence, and a program builds the machine and reads off the answer.

Jeffrey Shallit and his collaborators have used a program called Walnut to prove, this way, dozens of results about such sequences that had resisted hand proof, some of them conjectures that had stood for years. The proofs are machines with thousands or millions of states, checked by computer; they are rigorous and unreadable, a trade that four colours, and a proof nobody can read described for a different theorem. Sequences like the one the orbit written as a word followed are often of exactly this kind.

What the machines and tables cannot show

Each machine is checked on a finite range. The figures test every drawn machine against its property on every input up to a stated size; that it is right for all inputs follows from its construction, which reads one column at a time and never depends on the length.

The decision method is described, not run on a sentence. Building the machine for a sentence with several quantifiers involves products, projections and the subset construction, and even small sentences produce machines too large to draw; the figures show the building blocks, and the argument shows how they combine.

Base two is a choice. Everything works in any base, and the sets recognisable in two bases that are not powers of a common number are exactly the eventually periodic ones — Cobham’s theorem, which arithmetic with addition alone quoted. The powers of two are machine-readable in base two and not in base three.

Still open: addition and the primes

What happens if “xx is prime” is added to addition, instead of powers of two or squares? No finite machine recognises the primes in any base — they are not eventually periodic and their gaps are irregular — so the method does not apply, but that does not settle whether the resulting arithmetic is decidable.

It is not known. A decision procedure for addition with the primes would settle questions such as the twin prime conjecture and Goldbach’s conjecture, since each is a sentence in that language: “for every xx there is a prime p>xp > x with p+2p + 2 prime”, and “every even number above 22 is a sum of two primes”. Patrick Bateman, Carl Jockusch and Alan Woods showed in 1993 that, assuming Dickson’s conjecture — a statement about many linear expressions being prime together, of which the twin prime conjecture is the simplest case — the arithmetic of addition and primes is undecidable. Unconditionally, whether a machine or any other method can decide it is open, and it cannot be settled without first settling questions about primes that have been open for centuries.

A carry is enough

The habit worth keeping is to ask how much memory a check needs.

Adding two large numbers looks like it needs the whole numbers. Checking an addition needs one bit, carried from column to column. That single observation turns addition into a two-state machine, and machines combine the way sentences do, so every sentence about addition becomes a machine that can be run. Decidability, here, is the finiteness of a carry, and the limits of the method are exactly the relations — squares, primes — that no finite carry can track.

What links here

Computed from the collection, not written here: the essays that point at this one.

Reads more easily once this is understood

Essays that name this one as worth reading first.

Shares its objects with

Essays that name at least two of the same things, and that neither author linked.

Named objects

A dashed tag is an object no other essay names yet.

BinaryDecidabilityDecision procedureFinite automatonPresburger arithmeticQuantifier