esolang

Retina

A golfing language where every line is a regular expression. A program is a pipeline of search-and-replace stages over one string.

Martin Ender, 2015 · Turing complete: yes · ●●○ moderate · esolangs.org

Tested with 3 known programs · Hello World on Try It Online: identical except the line end · Test method

Run it

Expected: 5

The idea

Martin Ender built Retina on the .NET regular expression engine, which is far more powerful than textbook regular expressions: it has lookarounds, backreferences and balancing groups. Retina wraps that engine in the thinnest possible language. Lines pair up into stages; each stage transforms the string; the string at the end is the output.

Read the story

That turns out to be surprisingly good for code golf. Arithmetic is done in unary (111 is 3), where regexes are at home. Add a loop option and you can repeat a stage until the string stops changing. With that, Retina is Turing complete: a loop of replace stages is a Thue-style rewriting system, but deterministic.

The machine

One string, which starts as the input.

Program shapeStage
Two lines: a regex, then a replacementReplace every match
One line left over at the endCount: the string becomes the number of matches
+ before a backtick, as in +`regex | Repeat the stage until the string no longer changes

Real Retina has many more stage types (match, split, transliterate, sort, …) and options. In the source, stands for a newline.

This implementation is a sliver, with only Replace, Count and the + loop, on JavaScript regexes. For simple patterns they behave like .NET's; for anything clever, they do not.

Example

Unary addition: delete the plus sign, then count the ones.

Unary addition: delete the plus sign, then count the ones.

\+

1

On input 111+11 the first stage gives 11111, and the Count stage turns that into 5.

Sorting, the Thue way but deterministic: swap every ba to ab, and repeat until nothing changes.

+`ba
ab

On input babba this gives aabbb. Without the +, one pass gives abbab: it replaces the matches that are there, and the new bas it creates wait for a pass that never comes.

Expected: aabbb
Expected: abbab

Patterns it teaches

  • String rewriting: a pipeline of rewrites, the most practical member of the family.
  • Code golf: built for golfing on the Programming Puzzles & Code Golf site.
  • Fixed points: the + loop runs a stage until the string is a fixed point of it.

Try this

  1. Unary multiplication by 2: turn 111 into 111111.
  2. Convert unary to decimal and back for small numbers. (The Count stage does one direction.)
  3. Hard: in real Retina, look up the transliteration stage and write ROT13 in one stage.

Further

Also in Rewriting

Write your own in Run Convert a program from Retina

Implementation

JavaScript interpreter, source and command line

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

Command line

shib run retina -e '\+

1' --input '111+11'
shib manual retina

Source

Module
langs/museum/ports.js
Interpreter
langs/museum/retina.js, langs/common.js

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

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