Self-interpreter
Encyclopedia
A self-interpreter, or metainterpreter, is a programming language
Programming language
A programming language is an artificial language designed to communicate instructions to a machine, particularly a computer. Programming languages can be used to create programs that control the behavior of a machine and/or to express algorithms precisely....

 interpreter
Interpreter (computing)
In computer science, an interpreter normally means a computer program that executes, i.e. performs, instructions written in a programming language...

 written in the language it interprets. An example would be a BASIC interpreter written in BASIC. Conceptually, self-interpreters are closely related to self-hosting
Self-hosting
The term self-hosting was coined to refer to the use of a computer program as part of the toolchain or operating system that produces new versions of that same program—for example, a compiler that can compile its own source code. Self-hosting software is commonplace on personal computers and larger...

 compilers.

This may sound paradoxical; how can one implement a language in that language if it doesn't yet exist? However there is little mystery here. Self-interpreters are always "mocked up" in some other existing language and then later converted to the language they interpret. In these cases the early mockups can be used to develop the source code of the interpreter.
Once the system is bootstrapped
Bootstrapping (compilers)
In computer science, bootstrapping is the process of writing a compiler in the target programming language which it is intended to compile...

 new versions of the interpreter can be developed in the language itself.

A definition of a computer language is usually made in relation to an abstract machine (so-called operational semantics
Operational semantics
In computer science, operational semantics is a way to give meaning to computer programs in a mathematically rigorous way. Operational semantics are classified into two categories: structural operational semantics formally describe how the individual steps of a computation take place in a...

) or as a mathematical function (denotational semantics
Denotational semantics
In computer science, denotational semantics is an approach to formalizing the meanings of programming languages by constructing mathematical objects which describe the meanings of expressions from the languages...

). A language can also be defined by an interpreter, whereby the semantics of the 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, 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 implementation language of the interpreter (the host language). A typical example is whether a closure
Closure (computer science)
In computer science, a closure is a function together with a referencing environment for the non-local variables of that function. A closure allows a function to access variables outside its typical scope. Such a function is said to be "closed over" its free variables...

 in a Lisp
Lisp programming language
Lisp is a family of computer programming languages with a long history and a distinctive, fully parenthesized syntax. Originally specified in 1958, Lisp is the second-oldest high-level programming language in widespread use today; only Fortran is older...

-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 are implemented by the same feature in the interpreter 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
Lisp programming language
Lisp is a family of computer programming languages with a long history and a distinctive, fully parenthesized syntax. Originally specified in 1958, Lisp is the second-oldest high-level programming language in widespread use today; only Fortran is older...

 or Prolog
Prolog
Prolog is a general purpose logic programming language associated with artificial intelligence and computational linguistics.Prolog has its roots in first-order logic, a formal logic, and unlike many other programming languages, Prolog is declarative: the program logic is expressed in terms of...

. 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
Turing completeness
In computability theory, a system of data-manipulation rules is said to be Turing complete or computationally universal if and only if it can be used to simulate any single-taped Turing machine and thus in principle any computer. A classic example is the lambda calculus...

 language allows the writing of its own interpreter.

Clive Gifford introduced a measure quality of self-interpreter - eigenratio.
Eigenratio is a 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, when N goes to infinity. This value does not depend on top-level program being run.

The book Structure and Interpretation of Computer Programs
Structure and Interpretation of Computer Programs
Structure and Interpretation of Computer Programs is a textbook published in 1984 about general computer programming concepts from MIT Press written by Massachusetts Institute of Technology professors Harold Abelson and Gerald Jay Sussman, with Julie Sussman...

presents a set of interesting examples of meta-circular interpretation for the Scheme programming language and variants thereof.

See also

  • meta-circular evaluator
    Meta-circular evaluator
    A meta-circular evaluator is a special case of a self-interpreter in which the existing facilities of the parent interpreter are directly applied to the source code being interpreted, without any need for additional implementation...

  • Indirect self-reference
    Indirect self-reference
    Indirect self-reference describes an object referring to itself indirectly.For example, define the function f such that f = "x". Any function passed as an argument to f is invoked with itself as an argument, and thus in any use of that argument is indirectly referring to itself.This example is...

  • Quine (computing)
  • Forth
  • Self-hosting
    Self-hosting
    The term self-hosting was coined to refer to the use of a computer program as part of the toolchain or operating system that produces new versions of that same program—for example, a compiler that can compile its own source code. Self-hosting software is commonplace on personal computers and larger...

  • Self reference
  • Bootstrapping
    Bootstrapping (compilers)
    In computer science, bootstrapping is the process of writing a compiler in the target programming language which it is intended to compile...

  • eval
    Eval
    In some programming languages, eval is a function which evaluates a string as though it were an expression and returns a result; in others, it executes multiple lines of code as though they had been included instead of the line including the eval...

  • PyPy
    PyPy
    PyPy is a Python interpreter and JIT compiler. PyPy focuses on speed, efficiency and 100% compatibility with the original CPython interpreter.- Details and motivation :...

  • Rubinius
    Rubinius
    Rubinius is an alternative Ruby programming language implementation created by Evan Phoenix. Based loosely on the Smalltalk-80 Blue Book design, Rubinius seeks to"provide a rich, high-performance environment for running Ruby code."-Goals:...


External links

  • "The Roots of Lisp", by Paul Graham
  • The book Structure and Interpretation of Computer Programs contains a chapter dedicated to meta-circular evaluator
    Meta-circular evaluator
    A meta-circular evaluator is a special case of a self-interpreter in which the existing facilities of the parent interpreter are directly applied to the source code being interpreted, without any need for additional implementation...

    s
  • 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" for an overview of how to implement full reflective capabilities more efficiently at http://www2.parc.com/csl/groups/sda/projects/reflection96/docs/malenfant/ref96/ref96.html
  • Another simple self-interpreter at http://mazonka.com/subleq/
  • A self-interpreter for Javascript at http://lxr.mozilla.org/mozilla/source/js/narcissus/
The source of this article is wikipedia, the free encyclopedia.  The text of this article is licensed under the GFDL.
 
x
OK