In computing, a meta-circular evaluator (MCE) or meta-circular interpreter (MCI) is an interpreter which defines each feature of the interpreted language using a similar facility of the interpreter's host language. For example, interpreting a lambda application may be implemented using function application.[1] Meta-circular evaluation is most prominent in the context of Lisp.[1][2] A self-interpreter is a meta-circular interpreter where the interpreted language is nearly identical to the host language; the two terms are often used synonymously.[3]
A self-interpreter is a meta-circular interpreter where the host language is also the language being interpreted.[8] A self-interpreter displays a universal function for the language in question, and can be helpful in learning certain aspects of the language.[2] A self-interpreter will provide a circular, vacuous definition of most language constructs and thus provides little insight into the interpreted language's semantics, for example evaluation strategy. Addressing these issues produces the more general notion of a "definitional interpreter".[1]
From self-interpreter to abstract machine
This part is based on Section 3.2.4 of Danvy's thesis.
[9]
Here is the core of a self-evaluator for the
calculus. The abstract syntax of the calculus is
implemented as follows in OCaml, representing variables with their
de Bruijn index, i.e., with their lexical offset
(starting from 0):
typeterm=INDofint(* de Bruijn index *)|ABSofterm|APPofterm*term
Values (of type value) conflate expressible values (the
result of evaluating an expression in an environment) and denotable
values (the values denoted by variables in the environment), a
terminology that is due to Christopher Strachey.
[10][11]
Environments are represented as lists of denotable values.
The core evaluator has three clauses:
It maps a variable (represented with a de Bruijn index) into the value in the current environment at this index.
It maps a syntactic function into a semantic function. (Applying a semantic function to an argument reduces to evaluating the body of the corresponding syntactic function in its lexical environment, extended with the argument.)
It maps a syntactic application into a semantic application.
This evaluator is compositional in that
each of its recursive calls is made over a proper sub-part of the given
term. It is also higher order since the domain
of values is a function space.
In "Definitional Interpreters", Reynolds answered the question as to whether such a self-interpreter is well defined.
He answered in the negative because the evaluation strategy of the defined language (the source language) is determined by the evaluation strategy of the defining language (the meta-language).
If the meta-language follows call by value (as OCaml does), the source language follows call by value.
If the meta-language follows call by name (as Algol 60 does), the source language follows call by name.
And if the meta-language follows call by need (as Haskell does), the source language follows call by need.
In "Definitional Interpreters", Reynolds made a self-interpreter well defined by making it independent of the evaluation strategy of its defining language.
He fixed the evaluation strategy by transforming the self-interpreter into Continuation-Passing Style, which is evaluation-strategy independent, as later captured in Gordon Plotkin's Independence Theorems.
[12]
Furthermore, because logical relations had yet to be discovered, Reynolds made the resulting continuation-passing evaluator first order by (1) closure-converting it and (2) defunctionalizing the continuation.
He pointed out the "machine-like quality" of the resulting interpreter, which is the origin of the CEK machine since Reynolds's CPS transformation was for call by value.
[13]
For call by name, these transformations map the self-interpreter to an early instance of the Krivine machine.
[14]
The SECD machine and many other abstract machines can be inter-derived this way.
[15][16]
It is remarkable that the three most famous abstract machines for the calculus functionally correspond to the same self-interpreter.
Self-interpretation in total programming languages
Total functional programming languages that are strongly normalizing cannot be Turing complete, otherwise one could solve the halting problem by seeing if the program type-checks. That means that there are computable functions that cannot be defined in the total language.[17] In particular it is impossible to define a self-interpreter in a total programming language, for example in any of the typed lambda calculi such as the simply typed lambda calculus, Jean-Yves Girard's System F, or Thierry Coquand's calculus of constructions.[18][19] Here, by "self-interpreter" we mean a program that takes a source term representation in some plain format (such as a string of characters) and returns a representation of the corresponding normalized term. This impossibility result does not hold for other definitions of "self-interpreter".
For example, some authors have referred to functions of type as self-interpreters, where is the type of representations of -typed terms. To avoid confusion, we will refer to these functions as self-recognizers.
Brown and Palsberg showed that self-recognizers could be defined in several strongly-normalizing languages, including System F and System Fω.[20]
This turned out to be possible because the types of encoded terms being reflected in the types of their representations prevents constructing a diagonal argument.
In their paper, Brown and Palsberg claim to disprove the "conventional wisdom" that self-interpretation is impossible (and they refer to Wikipedia as an example of the conventional wisdom), but what they actually disprove is the impossibility of self-recognizers, a distinct concept. In their follow-up work, they switch to the more specific "self-recognizer" terminology used here, notably distinguishing these from "self-evaluators", of type .[21]
They also recognize that implementing self-evaluation seems harder than self-recognition, and leave the implementation of the former in a strongly-normalizing language as an open problem.
Uses
In combination with an existing language implementation, meta-circular interpreters provide a baseline system from which to extend a language, either upwards by adding more features or downwards by compiling away features rather than interpreting them.[22] They are also useful for writing tools that are tightly integrated with the programming language, such as sophisticated debuggers.[citation needed] A language designed with a meta-circular implementation in mind is often more suited for building languages in general, even ones completely different from the host language.[citation needed]
^Böhm, Corrado (1954). "Calculatrices digitales. Du déchiffrage des formules logico-mathématiques par la machine même dans la conception du programme". Ann. Mat. Pura Appl. 4 (37): 1–51.
^
Schmidt, David A. (1980). "State transition machines for lambda-calculus expressions". State transition machines for lambda calculus expressions. Lecture Notes in Computer Science. Vol. 94. Semantics-Directed Compiler Generation, LNCS 94. pp. 415–440. doi:10.1007/3-540-10250-7_32. ISBN978-3-540-10250-2.
^Danvy, Olivier (2004). A Rational Deconstruction of Landin's SECD Machine(PDF). Implementation and Application of Functional Languages, 16th International Workshop, IFL 2004, Revised Selected Papers, Lecture Notes in Computer Science 3474, Springer. pp. 52–71. ISSN0909-0878.
^Brown, Matt; Palsberg, Jens (January 2017). "Typed self-evaluation via intensional type functions". Proceedings of the 44th ACM SIGPLAN Symposium on Principles of Programming Languages. pp. 415–428. doi:10.1145/3009837.3009853. ISBN9781450346603.