esolang

Smallfuck

Brainfuck shrunk to bits: five instructions, no input, no output, and a tape that ends. Walk off the edge and the program is over.

Nikita Ayzikovsky, 2002 · Turing complete: with unbounded memory · ●○○ easy · esolangs.org

Tested with 3 examples and 3 known programs · Test method

Run it

Example

Expected output

1110000000000000

Output


The idea

Nikita Ayzikovsky made Smallfuck around 2002 as a language to compile into SMETANA, another of his minimal machines. He took Brainfuck and removed everything he could.

Read the story

Cells hold one bit, so + and - collapse into a single "flip". Input and output go too: you set up the tape before the run and read it afterwards. What is left is almost exactly P′′ with a two-symbol alphabet, and much easier to read.

The tape is finite. That makes Smallfuck a finite-state machine in principle, not a Turing machine. Give it an unbounded tape and it becomes universal again, since Boolfuck (its sibling with I/O) reduces to it.

The machine

A finite tape of bits, all set by the caller, and a pointer at cell 0. The size is up to the implementation.

CharDoes
*Flip the current bit
> <Move the pointer right or left. Moving off either end ends the program normally
[If the bit is 0, jump past the matching ]
]Jump back to the matching [ (which tests again)

Everything else is a comment.

Example

Add 1 to a binary number stored lowest bit first.

Add 1 to a binary number stored lowest bit first. The carry ripples right until it finds a 0.

[*>]    while the bit is 1: flip it to 0, step right
*       the bit is now 0: flip it to 1

On 11010 (1 + 2 + 8 = eleven): the first two 1s become 0s, the pointer lands on the 0 in cell 2, and * sets it. Result 00110 (4 + 8 = twelve). This is the same program as the P′′ example, mirrored: the carry runs right here and left there.

shib run smallfuck -e '[*>]*' --input 11010   # 00110
shib run smallfuck -e '[*>]*' --input 1111    # 0000: the carry runs off the end and the program stops

This implementation keeps a fixed tape of 16 bits, so it prints 0011000000000000 and 0000100000000000; the comments above are for a tape exactly as long as the input.

The second run shows the edge rule at work. Overflow is not an error. The pointer leaves the tape, the program ends, and you get 0.

Checked examples

Example

Expected output

1111111111111111

Output


Example

Expected output

0000000000000000

Output


Quirks

  • A run stops after 3,000,000 steps.

Try this

  1. Write a program that clears the whole tape. Hint: you cannot test "end of tape", but you do not need to; walking off it is how you stop.
  2. Subtract 1 from a lowest-bit-first number. Loops only run "while 1", so flip first and think in inverted bits. (One answer is five characters.)
  3. Hard: shift every bit one cell to the right (doubling the number). Hint: remember the carried bit by which branch of the program you are in.

Patterns it teaches

  • Turing tarpit: five symbols are enough for any bit-level algorithm that fits the tape.
  • Turing completeness: the clearest case of "bounded". The instructions are universal; only the finite tape keeps the language from being Turing complete.

Further

Smallfuck builds on Brainfuck.

Brainfuck derivatives

Cell-based

Write your own in Run Convert a program from Smallfuck

Implementation

JavaScript interpreter, source and command line

A JavaScript interpreter, used in the browser and on the command line. Id: smallfuck.

Command line

shib run smallfuck -e '[*>]' --input '1100000000000000'
shib manual smallfuck

Source

Module
langs/classic.js

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

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