esolang

Emmental

A language with no loops and no conditionals, only an instruction that rewrites the interpreter. Every loop you write is the interpreter changing itself.

Chris Pressey, 2007 · Turing complete: yes · ●●● hard · esolangs.org

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

Run it

Hello World

Expected output

Hello, world!
\x00

Output


The idea

Most self-modifying languages change the program. Emmental changes the language. It is defined by a meta-circular interpreter (an Emmental interpreter described in Emmental terms), and one instruction, !, lets a program give any symbol a new meaning in that interpreter.

Read the story

Chris Pressey's point was that this is a real source of power. Emmental has no while, no if and no recursion. But once you can define a symbol as a program that runs that same symbol again, you have a loop. And with the queue and the odd ~ (a base-2 logarithm), you can build choices.

Is it universal? Pressey believed so but did not prove it. Ørjan Johansen later argued that it is, with an Underload interpreter written in Emmental. (The two have disagreed about whether that construction fits the spirit of the question.)

The machine

A stack of symbols (bytes), a queue of symbols, and the interpreter: a table from each symbol to its meaning.

SymbolDoes
#Push 0
0…9Pop x, push x × 10 + digit. So #72 pushes 72 (H)
+ -Add, subtract
~Pop x, push its base-2 log, rounded down (log of 0 is 8)
. ,Print a symbol; read one
^ vCopy the top into the queue; move the queue's front to the stack
:Duplicate
;Push the symbol ;
!Pop a symbol s, then pop a program down to a ;. From now on, s means that program
?Pop a symbol and run it, with the interpreter as it is now

One subtle rule: a new definition is early bound. The symbols inside it keep the meanings they had when ! ran. Only ? looks things up in the current interpreter, and that is how loops happen.

Example

Define h as "print H, print i", then use it twice:

Define h as "print H, print i", then use it twice:

;#35#55#50#46#35#49#48#53#46#104!hh       prints HiHi

Step by step: ; marks the start of the program. #35#55#50#46 push the symbols #, 7, 2, .; #35#49#48#53#46 push #, 1, 0, 5, .. #104 pushes h. ! pops h, pops the program #72.#105. down to the ;, and makes h mean it. Then hh runs it twice.

A loop: make * mean "read, print, push *, run it".

;#44#46#35#52#50#63#42!*                  cat, forever (it never notices end of input)
Expected: abc, then NULs until the step limit

Checked examples

Hello World

Expected output

Hello, world!

Output


Example

Expected output

0

Output


Example

Expected output

O

Output


Quirks

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

Try this

  1. Define x to print x (symbol 120), and run it three times.
  2. Redefine + to mean -, then run #100#1+.. Which letter comes out?
  3. Hard: build an if: a symbol that prints y if the top of the stack is 0 and n otherwise. (Use ~, ? and a few definitions indexed by the result.)

Patterns it teaches

Further

Self-modifying

Stack-based

Write your own in Run Convert a program from Emmental

Implementation

JavaScript interpreter, source and command line

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

Command line

shib run emmental -e ';#35#52#56#63#48!0'
shib manual emmental

Source

Module
langs/dialects/ports.js
Interpreter
langs/dialects/stack.js

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

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