esolang

P′′

Two instructions and a loop. The 1964 grandparent of Brainfuck, and the first language without goto proven universal.

Corrado Böhm, 1964 · Turing complete: yes · ●●○ moderate · esolangs.org

Tested with 2 known programs · Test method

Run it

Expected: prints 1100, with ^ under the head

The idea

Corrado Böhm wanted to know how little a programming language needs. His answer, P′′ ("P double prime"), has no goto, no variables and no numbers. It has a tape, a head, two moves and a loop.

Read the story

He then proved that this is enough to compute anything a Turing machine can. That made P′′ the first structured language (loops, but no jumps) shown to be universal. Two years later Böhm and Giuseppe Jacopini generalised the idea into the structured program theorem: any flowchart can be rewritten with only sequence, choice and loop.

Nobody wrote P′′ for fun. It was mathematics. Nearly thirty years later Urban Müller added input and output, gave it eight punctuation marks, and called it Brainfuck.

The machine

A tape that is infinite to the left, a head, and an alphabet of symbols a₀, a₁, …, aₙ (at least two). a₀ is the blank. Every cell starts blank.

WordDoes
RMove the head one cell right (if there is one)
λChange the symbol under the head to the next one (aₙ wraps to a₀), then move one cell left
(q)Repeat q while the symbol under the head is not the blank

Only λ writes, and it always moves left after writing. That one quirk makes P′′ feel strange. To change a cell without moving, you write λR: bump and step left, then step back.

From these, Böhm built the rest as abbreviations. With n + 1 symbols:

BrainfuckP′′
+λR
-λR written n times (n bumps go all the way round)
<n times λR, then λ (lower, then raise and step left: a pure move)
>R
[ ]( )

Example

Add 1 to a binary number.

Add 1 to a binary number. Use two symbols, 0 (blank) and 1, put the lowest bit on the right, and start the head there.

(λ)     while the cell is 1: λ turns it into 0 and steps left (the carry ripples left)
λR      the cell is now 0: λ turns it into 1 and steps left, R steps back

With n = 1, λ on a 1 gives 0, so the loop is exactly "clear and carry". Trace on 1011 (eleven): the two rightmost 1s become 0, the head stops on the 0, and λR turns it into 1. Result 1100 (twelve).

Expected: the tape grows a cell to the left: 01000

A number after the tape sets n (default 1). The player accepts l as an ASCII stand-in for λ.

Patterns it teaches

  • Turing tarpit: the original one. Universal, and nearly impossible to write by hand without building abbreviations first.
  • Turing completeness: Böhm's proof shows that a loop that tests one cell, plus local moves, is enough. No jumps, no arithmetic.

Try this

  1. Run the example on 111 and explain where the extra cell comes from.
  2. With n = 2 (symbols 0, 1, 2), the < macro is λRλRλ. Check with the player that it leaves the tape 012 unchanged and only moves the head.
  3. Hard: subtract 1 from a binary number. The loop can only test "not blank", so you cannot loop "while 0". Hint: x − 1 is NOT(NOT x + 1).

Further

  • esolangs.org/wiki/P′′
  • Corrado Böhm, "On a family of Turing machines and the related programming language", ICC Bulletin 3 (1964).
  • Böhm and Jacopini, "Flow diagrams, Turing machines and languages with only two formation rules", Communications of the ACM (1966).
  • Its child: Brainfuck. A bit-sized cousin: Smallfuck.

Brainfuck

Cell-based

Write your own in Run Convert a program from P′′

Implementation

JavaScript interpreter, source and command line

A JavaScript interpreter, used in the browser and on the command line. Id: p-prime-prime.

Command line

shib run p-prime-prime -e '(λ)λR' --input '1011'
shib manual p-prime-prime

Source

Module
langs/museum/ports.js
Interpreter
langs/museum/p-prime-prime.js, langs/common.js

Docs: Kernel instructions · The .shib format · Command line · Adding a language

Source esolangs.org (CC0) · Edit on esolangs.org