views:

377

answers:

3

Is it legal to call a C compiler written in C or a PHP interpreter written in PHP metacircular? Is this definition valid only for languages of a specific type, like Lisp? In short, what are the conditions that an interpreter should satisfy for being called Metacircular?

+3  A: 

Here is a definition from the wikipedia page for metacircular:

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.

So the answer is no in both cases:

  • A C compiler is not an interpreter (evaluator). It translates a program from one form to another without executing it.
  • A (hypothetical) PHP interpreter written in PHP would be a self interpreter, but not necessarily metacircular.
Stephen C
+9  A: 

A metacircular interpreter is an interpreter written in a (possibly more basic) implementation of the same language. This is usually done to experiment with adding new features to a language, or creating a different dialect.

The reason this process is associated with Lisp is because of the highly lucid paper "The Art of the Interpreter", which shows several metacircular interpreters based on Scheme. (The paper is the kernel for the book SICP, and its fourth chapter works through others that create e.g. a lazily-evaluated Scheme.)

This is also vastly easier to do in a "homoiconic" language (a language whose code can be manipulated as data at runtime), such as Lisp, Prolog, and Forth.

As to your direct question - the C compiler wouldn't be an interpreter at all. A compiler written in its own language is 'self-hosting', which is a similar property, but more related to bootstrapping. A PHP interpreter in PHP probably wouldn't count, since you would likely be re-implementing a nontrivial amount of the language in the process. The major benefit of a conventional metacircular interpreter is that doing so isn't necessary - you can plug in the existing parser, garbage collection (if any), etc., and just write a top-level evaluator with different semantics. In Scheme or Prolog, it's often less than a page of code.

silentbicycle
the reason this is associated with Lisp is that McCarthy (you may have heard of him) gave a description of Lisp evaluation in Lisp.
Rainer Joswig
While the original Lisp was defined in terms of itself on paper (before it was ever implemented, which is an interesting story in its own right), AotI / SICP have probably done more to directly popularize the idea and (in particular) the term, which is what the question is about.Also, I have in fact heard of him. Was that really necessary?
silentbicycle
say what you want, the term is associated with Lisp via McCarthy's famous paper. Nice that a Scheme paper and some books pick up the idea 17 years later.
Rainer Joswig
Alan Kay says: '... that was the big revelation to me when I was in graduate school - when I finally understood that the half page of code on the bottom of page 13 of the Lisp 1.5 manual was Lisp in itself. These were "Maxwell's Equations of Software!" This is the whole world of programming in a few lines that I can put my hand over.'
Rainer Joswig
+1  A: 

To complement the above answers: http://www.c2.com/cgi/wiki?MetaCircularEvaluator

Vijay Mathew