From HandWiki - Reading time: 19 min
P′′ (P double prime)[1] is a primitive computer programming language created by Corrado Böhm in 1964 to describe a family of Turing machines.[2][3] It provided one of the earliest formulations of the single-entry single-exit principle central to structured programming.
P′′ is formally defined as a set of words on the four-instruction alphabet , as follows:
Let a finite alphabet , with , be given, together with a Turing Machine equipped with a tape that is infinite to the left and divided into squares, only finitely many of which initially contain non-blank symbols. The remaining squares contain the blank symbol . The machine has a single head that can read and write one square at a time and move along the tape.[4][5]
For formalization, the alphabet symbols are identified with their indices . The integer corresponds to the blank symbol (). Arithmetic on squares is performed modulo , so incrementing produces and decrementing produces . This formulation matches Böhm’s definition and differs only in notation, replacing symbolic alphabet elements with their numerical indices.[n 2]
A program (called: "word") operates on a tape configuration consisting of a given initial tape together with the position of the head. Each tape square contains a value from the set . All but finitely many squares initially contain the value .
Instructions
| Char. | Instruction |
|---|---|
| Shift the tape-head one square to the right (if possible). If the head is already at the right end of the tape, the instruction has no effect. | |
| Increment the current square modulo ,[n 3] then shift the head one square to the left. | |
| Jump forward past if the current square = [n 4] | |
| Jump backward past if the current square [n 4] |
Define as repeated application of an instruction sequence.[n 5] For example, .
Böhm defines three macro's r, r' and L:[6]
Based on these macro's, Böhm[2] gives the following word to compute the predecessor of an integer :
When P′′ is defined using square values in , the number is represented in bijective base-n notation , most significant digit first. These tuples appear in consecutive squares, flanked by s, with the tape head on the leftmost .
The expansion of the macro's depends on the modulus.
Modulus = 3, when tape squares take values in
When square values are in , the word expands to that is
In bijective base‑2 notation the number (for example) is encoded as .[n 7]
So the initial tape will be (with head position underlined)
After execution of the word the tape configuration is
, which corresponds to the bijective base‑2 encoding for 7.[n 8]
Modulus = 256, when tape squares take values in
When P′′ is defined with an alphabet of size , the word expands to Its full length is characters.[n 9]
In bijective base‑255 notation the number (for example) is encoded as .[n 10]
The initial tape will be (with head position underlined):
After execution of the word the tape configuration is
, which corresponds to the bijective base‑255 encoding for .[n 11]
Böhm proved that P′′ is Turing-complete.[7] The language demonstrates that programs can be written without using selection (if-then-else) constructs, only sequence and iteration are necessary. P′′ words are evaluated from left to right with backward control eliminated recursively. Böhm’s paper on the language P′′ is one of the earliest explicit statements of what we now call the single-entry single-exit (SESE) property:
a) It is only possible to enter a cycle from its first instruction, ...
b) It is only possible to exit a cycle from its last instruction.
— Böhm (1964)[8]
This design anticipated Dijkstra's 1968 advocacy of structured programming.[9] While Dijkstra argued programmers should avoid goto statements,[n 12] Böhm's P′′ prevented unstructured control flow through language design—enforcing structure at the source rather than requiring later transformation or programmer discipline.
His results were restated in the 1966 Böhm-Jacopini structured program theorem,[3] which combined Böhm's selection elimination with Jacopini's flowchart transformation method.
In conventional high-level languages, a while loop executes a block of code based on a fixed boolean expression that is evaluated at the start of each iteration. The same condition is tested each time the loop executes:
while condition_expression:
# loop body
In P′′, loops are written as , where is a sequence of instructions. Loop continuation is determined dynamically by the tape head: after executing , the program examines the value of the current tape square, which may change during execution because instructions in can move the head. This makes P′′ loops more general than conventional while loops, as the loop condition is embedded in the data rather than specified separately. A conventional while loop is equivalent to a P′′ loop only when at and the head points to the same square.
Not every P′′ loop can be trivially transcribed to a standard while loop. For example, the word
This word cannot be translated to a while loop without rethinking the logic.
In formal language theory, the language Brainfuck (hereafter: BF) may be regarded as a mirrored instantiation of P′′, where P′′ is defined with an alphabet of size .
The two languages differ only in the orientation of their tapes: P′′ operates on a left-infinite tape, whereas BF operates on a right-infinite tape. As is standard for Turing-machine–like models, this difference can be eliminated by mirroring the tape and reversing the direction of head movement.
Under this convention, there exists a literal one-to-one correspondence between the instructions of P′′ and BF. Arithmetic operations and loop constructs are preserved directly, while head movements are reversed to account for the mirrored tape orientation.[n 13] In other words, P′′ and BF are bisimilar under macro expansion with mirrored tapes/heads and disregarded input.
| # | P′′ pattern | BF pattern | Meaning |
|---|---|---|---|
| 1 | [n 5] | Composite pattern for head move | |
| 2 | [n 5] | Arithmetic decrement () | |
| 3 | Arithmetic increment () | ||
| 4 | Arithmetic increment plus head move | ||
| 5 | Head move | ||
| 6 | Loop start | ||
| 7 | Loop end |
The correspondences between P′′ and BF can be interpreted as (nondeterministic) bidirectional term-rewrite rules.
For example, the BF program can be translated to P′′ in two ways:
| (i) | , by applying rule 4, | |
| (ii) | , by applying rules 3 and 1. |
Note that the tape heads in P′′ and BF move in opposite directions.
Execution of the translated program requires a mirrored tape configuration. The left-infinite tape on which P′′ operates
must be mirrored to a right-infinite tape on which BF operates
The BF head is initialized at the position corresponding to the P′′ head under mirroring. With this arrangement, execution of a BF program related by the instruction correspondence mimics the behavior of the corresponding P′′ program.
Loosely speaking, P′′ — when defined with an alphabet of size 256 — can be translated into BF by
with , and then, in the resulting program, and .Consider the P′′ program from example 2 above: that is
The tape
So the translated BF program first has to up its tape to the mirrored copy of the tape on which the P′′ program started.
In bijective base‑255 notation the number is encoded as .[n 10]
In P′′, an initial tape can be (with head position underlined):
In BF, the mirrored tape
can be input by the code
// Cell 0 = 0
>+ // Cell 1 = 1
>+ // Cell 2 = 1
>+++++++++++++++++++++++++++++ // Cell 3 = 29
>++ // Cell 4 = 2
> // Head initially on cell 5
The execution of a program translated to BF will transform the mirrored tape to the final state
which corresponds to the bijective base‑255 encoding for .[n 11]
The program
The correspondences 1, 2, 3 in the table above are optimizing rules that correspond to the macro's L, r' and r given by Böhm.[6]
Using these shortcuts the shortest translation to BF has 18 instructions (+ the initialization of the tape):
<[<]>[-[>[>]]->]<+
When no shortcut is applied, the shortest translation by the rules has 4612 instructions, not including instructions required to initialize the tape.
<[<]+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+>[+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><[+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+>[+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+>]]+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+>]<+><