||It has been suggested that this article be merged into Interpreter (computing). (Discuss) Proposed since February 2014.|
A self-interpreter is a programming language interpreter, written in a programming language which can interpret itself. An example would be a BASIC interpreter written in BASIC. Conceptually, self-interpreters are closely related to self-hosting compilers.
If no compiler exists for the language to be interpreted, creating a self-interpreter requires the implementation of the language in a host language. The host language can be another programming language or assembler. By having a first interpreter such as this, the system is bootstrapped and new versions of the interpreter can be developed in the language itself. It was in this way, for example, that Donald Knuth developed the TANGLE interpreter for the language WEB of the industrial standard TeX typesetting system.
Defining a computer language is usually done in relation to an abstract machine (so-called operational semantics), or as a mathematical function (denotational semantics). A language can also be defined by an interpreter, whereby the semantics of the host language in which the interpreter is defined is usually considered as given. The definition of a language by a self-interpreter is not well-founded (i.e., cannot be used to define a language), but a self-interpreter tells a reader a lot about the expressiveness and elegance of a language. It also enables the interpreter to interpret its own source code, which is the first step towards a reflective interpreter.
There is an important design dimension in the implementation of a self-interpreter, namely whether a language feature in the interpreted language is implemented by using the same feature in the host language of the interpreter. A typical example is whether a closure in a Lisp-like language is implemented using closures in the interpreter language, or implemented "manually" using a data structure that stores the environment explicitly. The more features that are implemented by the same feature in the host language, the less control the programmer of the interpreter has. For example, a different behavior for dealing with number overflows cannot be realized if the arithmetic operations are just delegated to the corresponding operations in the host language.
There are some languages that have a particularly nice and elegant self-interpreter, such as Lisp or Prolog. Much research on self-interpreters, in particular reflective interpreters, has been conducted in the context of the Scheme programming language, a dialect of Lisp. In general, however, any Turing-complete language allows the writing of its own interpreter. Lisp is such a language because Lisp programs are lists of symbols and other lists. XSLT is such a language because XSLT programs are written in XML. An important and popular sub-domain of meta-programming is the practice of writing Domain-Specific Languages or DSLs. DSLs are solution languages tailored to resemble as closely as possible the human jargon of “domain experts”.
Clive Gifford introduced a measure quality of self-interpreter, the eigenratio, which is the limit of the ratio between computer time spent to run a stack of N self-interpreters and time spent to run a stack of N−1 self-interpreters as N goes to infinity. This value does not depend on a top-level program being run.
Examples of languages with a self-interpreter
- Meta-circular evaluator
- Indirect self-reference
- Quine (computing)
- Self reference
- "The Roots of Lisp", by Paul Graham
- The book Structure and Interpretation of Computer Programs contains a chapter dedicated to meta-circular evaluators
- The paper "A Simple Reflective Interpreter" (1992) by Stanley Jefferson and Daniel Friedman contains an implementation of a minimal Scheme interpreter in Scheme such that every instance of the interpreter can be used to run another interpreter while providing access to the next higher level.
- The paper "A Very Short Self-Interpreter" http://arxiv.org/html/cs.PL/0311032 by Oleg Mazonka and Daniel B. Cristofani
- See the paper "A Tutorial on Behavioral Reflection and its Implementation" http://www2.parc.com/csl/groups/sda/projects/reflection96/docs/malenfant/malenfant.pdf for an overview of how to implement full reflective capabilities more efficiently
- Another simple self-interpreter at http://mazonka.com/subleq/