In this paper McCarthy sets out the following primitives: QUOTE, CAR, CDR, CONS, EQUAL, ATOM, COND, LAMBDA and LABEL.

He then sets up the following abbreviations: T, NIL, DEFUN, NULL, CADR, LIST, AND, ALT, and SUBST.

He rounds it up with an incredible definition of the EVAL function used in the reader.

Given the above set of LISP primitives, is it possible to extend the EVAL function to evaluate defmacro?

  • 1
    $\begingroup$ The book: Lisp in Small Pieces by Christian Queinnec presents an implementation of macros in Scheme that might be useful. $\endgroup$ – Dave Clarke Dec 7 '12 at 13:04

Yes, in fact it's quite similar to evaluating DEFUN. Of course, in the paper EVAL has no rule for evaluating DEFUN, since it's treated as an abbreviation. From the definition of DEFMACRO in emacs LISP (link):

Special Form: defmacro name argument-list body-forms...

defmacro defines the symbol name as a macro that looks like this:

(macro lambda argument-list . body-forms)

It's easier if we ignore the name of the macro and treat that as an abbreviation. Then we have simply to create the list (macro lambda argument-list . body-forms) and set name to be shorthand for that list, the same way a (defun name (arguments) (body)) sets name as a shorthand for (lambda (arguments) (body)). As for evaluating a macro, that is also easy --- just pass the function inside the macro your arguments, but WITHOUT evaluating them first.

| cite | improve this answer | |
  • $\begingroup$ Are you saying that defmacro would need to be implemented by both a modification to eval and a new primitve? (Being similar to defun) $\endgroup$ – hawkeye Sep 7 '10 at 11:05
  • $\begingroup$ The name binding part would need a new primitive, or however it's actually handled in eval --- since the paper treats the name-binding part of defun as an abbreviation. AFAIK, an interpreter keeps a lookup table with variable bindings, and defun etc. will add an entry to that table saying "this variable is now bound to this function". So, if you ignore the name-binding part, you need only modify eval. $\endgroup$ – Evgenij Thorstensen Sep 7 '10 at 11:31

This shows macro expansion as a 'code template' to solve this problem.

| cite | improve this answer | |
  • $\begingroup$ link is not a good answer. Could you please expand this? Thanks. $\endgroup$ – Kaveh Dec 6 '12 at 7:51

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.