esolang
Learn · Ideas13 languages show it

Metacircular interpreter

An interpreter for a language, written in that same language. The snake that reads its own rulebook.

●●● hard · where to see it

The idea

An interpreter is a program that runs other programs. Usually it is written in a different, "real" language, such as a Brainfuck interpreter in JavaScript or a Python interpreter in C. A self-interpreter is written in the language it interprets. When it leans on the host to do the hard parts (Lisp's eval using Lisp's own lists and functions), it is called metacircular.

Read the story

There are three reasons to build one.

  • It defines the language. McCarthy's 1960 paper described Lisp by writing eval in Lisp. Steve Russell then noticed it could be hand-compiled into machine code, and Lisp had its first interpreter.
  • It proves the language is strong enough. A language that can run itself is at least as powerful as itself, and in practice that usually means universal.
  • It opens the language up. Once the interpreter is a program in the language, you can change it from inside to add a feature, trace a run, or let the program redefine what its own words mean.

The cost is a tower. An interpreter running an interpreter running a program is slow, and every level multiplies the cost.

How it works

A self-interpreter needs three things from the language.

A self-interpreter needs three things from the language.

  1. A way to hold a program as data (see code as data).
  2. A loop that reads one instruction from that data and dispatches on it.
  3. A place to keep the interpreted program's state (its tape, stack or variables) without tangling it with its own.

In Lisp the shape is famous. A sketch, not a full evaluator.

(define (eval expr env)
  (cond ((symbol? expr) (lookup expr env))          ; a name: look it up
        ((eq? (car expr) 'quote) (cadr expr))      ; (quote x): x itself, unrun
        ((eq? (car expr) 'lambda) (make-closure expr env))
        (else (apply (eval (car expr) env)          ; a call: evaluate the function,
                     (map (lambda (e) (eval e env)) ; evaluate the arguments,
                          (cdr expr))))))           ; and apply one to the other

eval and apply call each other, and every Lisp feature the evaluator needs it borrows from the Lisp it runs on. That borrowing is the "circular" part.

In Brainfuck nothing can be borrowed. Daniel B. Cristofani's dbfi is a Brainfuck interpreter in 423 Brainfuck commands. It reads the program to run from its input, then a !, then that program's input. It lays the program out on its tape as cells, and walks a marker over it to find the current instruction and matching brackets. Save its code from brainfuck.org as dbfi.b and run it with any brainfuck interpreter, for example the shib command line.

shib run brainfuck dbfi.b --input ',[.,]!cat'                  # prints: cat
shib run brainfuck dbfi.b --input ',[>+<-]>[<++>-]<.!*'        # doubles '*' (42): prints T (84)

The second line is the doubling program from the Brainfuck page, now running two levels deep; the host interpreter runs dbfi, and dbfi runs the doubler.

Seen in

  • Brainfuck. dbfi and others, a few hundred characters. A favourite proof that the tarpit is universal.
  • Emmental. Chris Pressey (2007) made the metacircular interpreter part of the language. The ! instruction redefines a symbol as an Emmental program, so programs rewrite the interpreter that runs them.
  • Forth. The outer interpreter that reads words is itself made of Forth words, and programs can replace parts of it.

Try this

  1. Run the two dbfi lines above. Then run dbfi on itself by feeding it dbfi's commands, !, then a small program, ! and its input. How much slower is it?
  2. Write a self-interpreter for Deadfish in a language of your choice, then explain why Deadfish cannot have one written in Deadfish.
  3. Hard: dbfi still leans on its host for the tape, input and output. What is the smallest machine you could not move into the text?

Further

Where to see it