esolang
Learn · Ideas67 languages show it

String rewriting

No variables, no loops. Only "find this, replace it with that", until nothing matches.

●○○ easy · where to see it

The idea

A rewriting system is a string and a list of rules. Each rule says to replace the pattern L with R. You keep applying rules until none applies. The final string is the answer.

Read the story

The idea goes back to the Norwegian mathematician Axel Thue, who studied such systems in 1914; they are still called semi-Thue systems. Andrey Markov Jr. later made them deterministic by fixing the order in which rules are tried. Both are universal; anything a Turing machine can do, a well-chosen rule list can do too.

Rewriting is attractive for esolangs because the whole program state is visible. There is no hidden memory, only one string you can print at every step.

How it works

Rules are tried top to bottom.

A Markov algorithm

Rules are tried top to bottom. The first rule whose pattern occurs is applied, at its leftmost occurrence, and then you start again from the top.

This classic example (from the Wikipedia article on Markov algorithms) turns binary into unary.

|0 → 0||
1  → 0|
0  → (nothing)

On 101.

101
0|01        rule 2: the first 1 becomes 0|
00||1       rule 1: |0 becomes 0||  (a bar moving right doubles)
00||0|      rule 2
00|0|||     rule 1
000|||||    rule 1
00|||||     rule 3, three times: zeros disappear
0|||||
|||||       5 bars: 101 in binary is 5

Each bar that moves right past a 0 doubles. That is exactly "shift left = times two", done by hand, one bar at a time.

Thue: nondeterministic order

Thue drops the ordering. At each step any rule that matches may be applied, at any place. A correct Thue program has to give the same answer whatever choice is made, or it has to make the randomness useful.

///: the program rewrites itself

/// has one command, /pattern/replacement/. It replaces the pattern, over and over, in the rest of the program, then continues. Text outside a command is printed.

/a/b/aaa      →  prints bbb
/a/aa/a       →  never stops: every replacement makes another a

So in /// the rules and the data are the same string. A rule can even create new rules.

Step by step in ///

a becomes bb, then each b becomes cc.

One rule

Expected output

Hello, world!

Output


  • /foo/Hello, world!/ replaces foo in the rest of the program by the greeting. The rest is /bar/foo/bar.
  • That becomes /bar/Hello, world!/bar, a rule that replaces bar by the greeting in the rest, bar.
  • What is left, Hello, world!, is plain text and prints.

A rule that grows the text

a becomes bb, then each b becomes cc. Rewriting can make text longer, which is how /// computes.

Expected output

cccc

Output


The kernel runs /// as a dictionary with reader=rewrite. It reads one rule at a time from the rest of the text, and the words REST, FIND, SPLIT and REWRITE do the replacing.

Side by side

The same in a standalone /// interpreter

The kernel dictionary agrees with this interpreter on 1,000 random programs.

Expected output

Hello, world!

Output


Thue, rules on lines

In Thue the rules come first, one per line as left::=right. A right side starting with ~ prints instead.

Expected output

Hello World!

Output


A Markov algorithm

A Markov algorithm applies the first rule that matches, at its first place. These rules turn a binary number into a row of ones.

Expected output

|||||

Output


Try this

  1. Write a Markov algorithm that sorts a string of as and bs (all as first). One rule is enough.
  2. Write a /// program that prints hello with one substitution and a shorter body.
  3. Adding unary numbers like |||+|| takes one rule (+ → nothing). Hard: write a Markov algorithm that doubles one (||| → ||||||). You will need a marker letter.

Seen in

  • Thue. A semi-Thue system plus input and output, with random rule choice.
  • ///. Rewriting where the program text is the data; proven universal by Ørjan Johansen in 2009.
  • Retina. Regex replacement stages, a practical rewriting language built for golf.
  • FRACTRAN. Rewriting a number instead of a string; each fraction is a rule "trade these prime factors for those".

Further

Where to see it