esolang

///

Find-and-replace as a whole language. Each substitution rewrites the rest of the program, so the program edits itself as it runs.

Sophie Swett, 2006 · Turing complete: yes · ●●○ moderate · esolangs.org

Tested with 6 examples and 4 known programs · Hello World on Try It Online: identical · Test method

Run it

Hello World

Expected output

Hello, world!

Output


all 27 steps

The idea

/// (pronounced "slashes") was made by Sophie Swett (Ihope127 on esolangs.org) in 2006, after the s/old/new/ notation people type on IRC to correct themselves. It has exactly one command: substitute.

Read the story

What makes it strange is what it substitutes in. There is no separate data. A substitution /old/new/ is applied to the rest of the program text, over and over, until old no longer appears. Then the interpreter carries on reading the (now changed) program. Everything that is not a command is printed as it is read.

So the data and the code are the same string, and the only way to compute is to have the program rewrite its own future. There is no obvious way to loop, and for a while it was unclear whether /// could compute anything interesting. In 2009 Ørjan Johansen showed it is Turing complete by writing an interpreter for Bitwise Cyclic Tag in it.

The machine

The program is a string, read from the left.

Next charactersDoes
\cPrint c, remove both
/pattern/replacement/Remove this command, then replace the first pattern in the rest of the program with replacement, again and again until there is none
any other characterPrint it and remove it

Inside the pattern and replacement, \ escapes the next character (so \/ is a literal slash). If the program ends in the middle of a command, it stops. If the replacement contains the pattern, the substitution never ends.

Example

The simplest program prints itself: Hello, world! has no slashes.

The simplest program prints itself: Hello, world! has no slashes.

Chained substitutions double and double again:

/a/bb//b/cc//c/dd/a
  1. /a/bb/ turns the rest, /b/cc//c/dd/a, into /b/cc//c/dd/bb.
  2. /b/cc/ turns /c/dd/bb into /c/dd/cccc.
  3. /c/dd/ turns cccc into dddddddd, which is printed.

Code that writes code. The substitution creates a new substitution, which then runs.

/!/\/a\/b\//!aaa

The first command replaces ! with /a/b/ (the slashes are escaped). The rest becomes /a/b/aaa, which turns aaa into bbb.

Expected: dddddddd
Expected: bbb
Expected: never ends; stopped by a size limit

On Windows Git Bash, an argument that starts with / is turned into a path. Run export MSYS_NO_PATHCONV=1 first, or put the program in a file.

Checked examples

The rule replaces world! world! by Hello, in the rest of the program, world! world! world!.

Hello World

Expected output

Hello, world!

Output


Hello World

Expected output

Hello, world!

Output


Hello World

Expected output

Hello, world!

Output


Hello

The rule replaces world! world! by Hello, in the rest of the program, world! world! world!. That leaves Hello, world!, which prints.

Expected output

Hello, world!

Output


Escapes

\/ is an escaped slash. The first rule turns every a into /, which builds the next rule out of the text.

Expected output

Hello, world!

Output


Quirks

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

Try this

  1. Print Hello, world! using a substitution, so that the word world appears only once in the program.
  2. Write a program that prints 27 xs using three substitutions.
  3. Hard: make a substitution that creates a substitution that creates a substitution. Escape carefully.

Patterns it teaches

Further

String-rewriting paradigm

Self-modifying

Write your own in Run Convert a program from ///

Implementation

JavaScript interpreter, source and command line

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

Command line

shib run slashes -e 'Hello, world!'
shib run shib:slashes -e 'Hello, world!'
shib manual slashes

Kernel translation

Each command and its kernel instructions
CommandKernel instructions
\REST DUP SIZE IF UNCONS EMIT THEN REWRITE
/REST part 1 EQ IF 1000 LOAD 1002 STORE 1001 LOAD part 1 EQ IF 1000 LOAD 1003 STORE 1001 LOAD rewrite REWRITE ELSE HALT THEN ELSE HALT THEN

Kernel definition

#! shibboleth 3 seed=slashes syllables= plain=off reader=rewrite other=print
/// as a front end: the program text rewrites itself. A character no rule reads goes to print; \ prints the next one;
/pattern/replacement/ replaces the first occurrence in the rest of the program until there is none (REST, REWRITE).
part reads up to an unescaped / into cell 1000 (the rest goes to 1001) and says 1 if it found one, 2 if the text ended
"\\" => REST DUP SIZE IF UNCONS EMIT THEN REWRITE
"/" => REST part 1 EQ IF 1000 LOAD 1002 STORE 1001 LOAD part 1 EQ IF 1000 LOAD 1003 STORE 1001 LOAD rewrite REWRITE ELSE HALT THEN ELSE HALT THEN
print => EMIT
keep => DUP -1 EQ IF DROP 2 ELSE 1000 LOAD SWAP CAT 1000 STORE 0 THEN
step => DUP 47 EQ IF DROP 1 ELSE DUP 92 EQ IF DROP 1001 LOAD UNCONS SWAP 1001 STORE THEN keep THEN
part => 1001 STORE [ ] 1000 STORE 0 BEGIN DROP 1001 LOAD UNCONS SWAP 1001 STORE step DUP UNTIL
rewrite => BEGIN DUP 1002 LOAD FIND DUP 0 LT NOT IF SPLIT 1002 LOAD SIZE SPLIT SWAP DROP 1003 LOAD SWAP CAT CAT 1 ELSE DROP 0 THEN NOT UNTIL

Source

Module
langs/classic.js
Kernel definition
shibboleth/witness/slashes.shib on shibboleth/kernel.js

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

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