Explanation of macros; Haskell macros

Pascal Costanza costanza at web.de
Thu Oct 30 20:04:42 EST 2003


Peter Seibel wrote:

> "Anton van Straaten" <anton at appsolutions.com> writes:

>>Yes, but the point is that with a concise syntax for lambda, entire
>>classes of macros can become unnecessary. That's how Smalltalk
>>handles 'if', for example - no macros or special forms needed.
> 
> 
> Okay, so I picked an unfortunate example in that it also falls in the
> class of macros that become unecessary when other bits of syntactic
> sugar are provided.

No, I don't think so. Here is what I needed in a real program.

I had several cond statements at various places of my code. They all 
didn't have a default case. At a certain stage, I needed to be sure to 
get an exceptional return value for the default case instead of NIL. So 
I added a macro:

(defconst *error-form*
   '(mv (trans-pstate pstate :exception 'program-error) *void*))

(defmacro ev-cond* (pstate &body body)
   `(cond ,@(append body `((t (let ((pstate ,pstate)) ,*error-form*))))))

*ERROR-FORM* here is the standard code that changes a program state to a 
state with an exception and a *void* return type. EV-COND* is my 
modified COND that simply adds a default case in which PSTATE is 
correctly bound so that *ERROR-FORM* can do the right thing.

(This isn't "pure" Common Lisp, but ACL2, and the code is somewhat more 
complicated than necessary because ACL2 places some severe restrictions 
on the code that one can write.)

The essence of this is as follows:

(defmacro my-cond (&body body)
   `(cond ,@(append body '(t (handle-the-default-case))))

With simple shadowing imports one can make this completely transparent 
to client code.

How would you do this with a Smalltalk-like if expression? (This is not 
a rhetoric question - I would be really interested to hear how one could 
make this work.)


Pascal





More information about the Python-list mailing list