PuzzleReels
Sections

The Deep

The maths · Episode 15 · 4:43 · gives the answer

The puzzle

Episode 8's submarine moved by the same whole number every turn. Now let its position on turn t be given by any program at all, one charge a turn, and a miss still tells you nothing.

Can you still guarantee to sink it?

The maths, properly

Call a submarine describable if there is a program that, given a turn t, always finishes and prints where the submarine is. A program that runs for ever on some turn does not say where anything is, so it does not describe a submarine.

Both halves are the same move, aimed in opposite directions. Neither can be checked by running every program. What a script can check is a real language small enough to run exhaustively, and that is the worked case below.

Half one: list them, and take the diagonal

This is the film's first route. Write out every string of characters, shortest first and then alphabetically, and cross out the ones that do not describe a submarine. Number what is left: f1,f2,f3,…. Make a table with one row per program and one column per turn, and drop on its diagonal:

h(t)=ft(t)

If the submarine follows fk, then on turn k the charge is at fk(k), and so is the submarine. This is proof three of episode 8 again: any family that can be put in a list is caught by its diagonal.

The catch is in the crossing out. It has two parts. Strings that are not programs at all can be crossed out by a parser, mechanically. Programs that run for ever on some turn cannot. In general, no program can tell whether another will finish: that is Turing's halting problem, from 1936. So the list the hunter needs exists, since every string either describes a submarine or does not, but nobody has been shown a way to write it out. Half two says nobody ever will.

Half two: add one

This is the film's second route, stated for any hunter. Suppose some program H, given t, prints the turn-t drop. Then

e(t)=H(t)+1

is a program too: run H, and add one. It always finishes, because H does, so e is a describable submarine. On every turn it is one place above the charge, so H never sinks it. No program is a winning hunter.

The film runs the same move on its own list. If e=h+1 were program number k, then on turn k it would have to satisfy fk(k)=h(k)+1=fk(k)+1. So e is on no list, and the diagonal hunter is not a program. The narration calls e "three lines of code". That is true only if the hunter is code, and that is exactly what the argument rules out. The hunter exists, but you cannot write it down.

A third consequence: the list cannot be written out

Put the two halves together. If some program could write out the list of programs that always finish, the diagonal would be a program too: to find h(t), list as far as the t-th and run it on t. Half one makes that a winning hunter, and half two says no program is one. So no program can list exactly the programs that always finish. This cousin of the halting problem falls straight out of the submarine, and it is why the crossing out above cannot be done mechanically.

A worked case: arithmetic in t

The film makes the list for real in a toy language. A program is an arithmetic expression in t, built from digits (with no leading zeros), t, +, −, × and brackets, with unary minus: 16 characters in all. Every expression in it finishes, so here the crossing out is mechanical, and nothing about halting is being dodged.

Up to 4 characters there are 69,904 strings. 18,266 of them are programs and 51,638 are crossed out. The list starts with the ten constant submarines, and t itself is program 11. Many programs describe the same submarine, such as t, (t), −−t and t×1. The 18,266 programs describe 11,415 different paths.

123456000000011111112222222333333344444445555555h(t)h(t)+1012345123456
The top of the list: every row a constant, and the diagonal
1,3721,3731,3741,375t×79,6049,6119,6189,625t×810,97610,98410,99211,000t×912,34812,35712,36612,375t×t1,882,3841,885,1291,887,8761,890,625h(t)h(t)+19,60410,98412,3661,890,6259,60510,98512,3671,890,626
Further down: t×t is row 1,375, sunk on turn 1,375 at 1,890,625

The film's submarine moves t×t. On the list it is program 1,375, so on turn 1,375 the charge is where it is, at 1,890,625. That position has 7 digits, and no earlier charge lands on it. The film's second submarine, 9−t×t, has five characters and is program 204,999, sunk at −42,024,589,992. Guaranteed is not the same as soon.

Not every submarine waits for its own turn. A charge aimed at an earlier program can land on it by luck, and 7,008 of the 18,266 (38%) are sunk before their own turn that way.

Here the hunter is a program, just not one in the language. The script that checks this page computes the toy hunter, so it is plainly a program. It is not a toy program, though, and the reason is half two in miniature. The toy language is closed under adding one: if E is a toy program, so is (E)+1. A toy hunter would therefore be dodged by a toy submarine. The hunter has to sit outside what it hunts. Here it sits one step outside, in a language that can read a string and run it.

The jumping submarine is not in the toy language. The film's third opening submarine is at t on most turns but at −2t on every third one. Every toy program is a polynomial in t, and one that agrees with t on infinitely many turns is t, which is not −6 on turn 3. The film keeps it in so that "any program" is never quietly narrowed to "any arithmetic expression".

Find your submarine

Type a submarine of up to 5 characters, in the toy language. The lab writes out the list exactly as above, finds the submarine's place on it, and runs the diagonal hunter against it.

Some submarines, their places on the list, and where the diagonal sinks them
SubmarinePlaceAtSunk on
010its own turn
t1111its own turn
-t112−112its own turn
t*t1,3751,890,625its own turn
9-t*t204,999−42,024,589,992its own turn
t*t*t210,4999,327,174,694,881,499its own turn
2*t+163,158126,317turn 39,020, by luck

How long the queue is

Counting by hand stops at five characters, but the grammar can be counted without listing. Each string parses in only one way, so counting parse trees counts programs. The count agrees with the brute-force list at every length up to 5:

How long the queue is: strings of each length, and how many are programs
LengthStringsProgramsShare
11611168.8%
216210139.5%
31631,37533.6%
416416,77925.6%
5165210,86220.1%
61662,628,96415.7%
716732,859,45512.2%
8168410,515,9359.6%
91695,130,624,9267.5%
10161064,131,247,1125.8%

Programs become a smaller share of the strings as the length grows, but there is no last one. The list never ends, and every program is somewhere on it.

Generalising: the ladder

The film's ladder has four rungs, and two facts decide all of them.

  • Any family you can list is caught, by its diagonal. Whole-number starts and speeds, polynomials, programs that always finish: each can be listed, so each is caught.
  • A family closed under adding one cannot contain its own hunter. If the hunter were in the family, the hunter plus one would be too, and it would never be hit. Straight lines are closed under adding one, so episode 8's spiral hunter was never a straight line: its first three drops already do not lie on one. The same goes for polynomials, for the toy language, and for programs.

The last rung, any path at all, needs neither. No hunter of any kind, program or not, catches every path: whatever drops d1,d2,… a hunter makes, the path dt+1 dodges them all. The film says that there the list runs out. That is true, as the last section shows, but the reason no hunter works is this one line.

What plays the part of an irrational number

The film's table matches families of numbers with families of submarines:

  • Ratios of whole numbers go with whole-number starts and speeds.
  • 2, a root of a polynomial, goes with polynomial paths.
  • π and e, whose digits a program can print, go with program paths.
  • A number on no list goes with a path on no list.

Each family of numbers on the left of the first three rows can be listed: the ratios, the roots of whole-number polynomials, and Turing's computable numbers from 1936, which include π and e. The real numbers cannot be listed, so almost every real number is on none of these lists. Being irrational was never a property of a number on its own. It means not being a ratio of whole numbers, and being uncomputable means not being on the list of programs. A speed of 2 escapes episode 8's family and is caught by a longer list, as episode 8's page shows.

What changes if the puzzle changes

More charges. With two charges a turn, or a hundred, the hunter is still beaten if it is a program: put the evader one above the highest charge.

Tossing coins. A hunter that chooses its drops at random cannot be sure of every path either. Its turn-t drop is a random whole number, and some position has a chance of at most ε/2t of being chosen. A path through such positions is hit with chance at most ε in total, and ε can be as small as you like. Whether a coin-tossing program can be sure, with probability one, of every describable submarine is a subtler question that this page does not take up.

A submarine that watches. If the submarine sees each charge before it moves, it just moves away, as in episode 8. Everything here assumes the path is fixed before the hunt begins.

Almost every submarine

Every describable submarine is described by some string, so they can be listed. All the paths together cannot be listed. Given any list of paths g1,g2,…, the path gt(t)+1 is on it nowhere. That is the same move a third time, and it is Cantor's diagonal argument from 1891.

So the describable submarines are a sliver of the possible ones. Make "almost every" precise by letting the submarine step +1 or −1 each turn on the toss of a fair coin. Each single path then has probability 0, since its first t steps have probability 2−t. A list of probability-0 events has probability 0 together. So with probability 1 the submarine's path is not describable. That is a theorem, not something a script can check, and it does not mean such a submarine is never caught: any one path is caught by the hunter that follows it. It means no list of descriptions ever holds it.

Checking it

scripts/deep.mjs reads the film's deep.json and rebuilds the toy language's list from scratch: the counts, the film's sieve of the first 48 strings, the first 9 rows of the table, the window that ends on t×t, and t×t's place, catch and path. Any disagreement stops the build. Then it checks the rest of what the page says about the toy language.

  • All 18,266 programs are under the diagonal's charge on their own turn. The film's script checks every seventh of the first 4,000; this one checks all of them, and finds the 7,008 caught early.
  • The counts. The list is made by brute force up to 5 characters, 229,128 programs. The grammar count agrees at every one of those lengths, and gives the rest of the queue table.
  • The lemmas. Every program up to four characters is checked to be a polynomial of degree at most two, and (E)+1 is checked to be a program computing one more than E for each of them. No program up to five characters matches the jumping submarine. None of the three opening submarines, and neither episode 8's drops nor those drops plus one, is a straight line.
  • True by construction. That the diagonal sinks each program on its own turn, that the evader is never hit, and that a flipped diagonal is no row of its table (every 0-and-1 table up to 4 by 4, 66,066 of them) all hold whatever the list is. The script asserts them anyway, as a guard on its own construction rather than as evidence.

What is not checked. Everything about all programs: that the programs which always finish cannot be listed by a program, Turing's halting problem, and that almost every path is undescribable. These are theorems, and the proofs above are complete. The toy language is a stand-in in which the finite cases can be run, and the film is careful to call it that.

Where this shows up

  • Cantor's diagonal argument (1891) shows that the real numbers, or the paths here, cannot be put in a list.
  • Turing's 1936 paper on computable numbers used the same diagonal to show that no program decides whether programs halt. The third consequence above is a close cousin of that result.
  • Hierarchy theorems in complexity theory, such as Hartmanis and Stearns's from 1965, use a diagonal hunter that is slightly stronger than everything it hunts to show that more time lets programs compute strictly more.

Try it yourself

1. Count the toy language's two-character programs by hand.
  1. A two-digit number with no leading zero gives 90, a minus sign before a digit gives 10, and −t gives 1. Nothing else works: two characters cannot hold a pair of brackets around anything, or an operator between two things.
2. Where is t on the list, and on which turn is it sunk?

Program 11, after the ten constants 0 to 9. On turns 1 to 10 the charges go on the constants' positions, 0 to 9, which is one behind t every time. Turn 11 sinks it at 11.

3. A hunter is a program that drops two charges a turn. Show that it still cannot sink every describable submarine.

Let e(t) be one more than the higher of the two turn-t charges. It is a program that always finishes, so it is a describable submarine, and it is above both charges on every turn.

4. Why can a hunter not simply run every program side by side, and use the answers as they come back?

Because whatever it does, it is a program that prints one drop a turn, so half two applies: its drops plus one are a describable submarine it never hits. Running programs side by side, called dovetailing, does let one program eventually see every answer that ever comes back. What it cannot do is know which programs will never answer, and the hunter would need to know that to drop on time.