[Python-Dev] A Hygienic Macro System in Python?

Tom Emerson tree@basistech.com
Mon, 18 Mar 2002 16:10:40 -0500


Jeremy Hylton writes:
> There has not been much interest in macros of any sort,
> although there have been a few threads on comp.lang.python
> recently.  It may be that a few motivating examples would
> make clear why we should be interested :-).  

Sure, here's one from Dylan, that provides a useful bit of syntactic
sugar that makes some code easier to read:

  define macro unless
    { unless (?test:expression) ?:body end }
     => { if (~ ?test) ?body end }

which allows me to write something like

  unless ( has_foo (bar) )
    do_something
  end

Which is equivalent to

  if (~ has_foo(bar))
    do_something
  end

I find using 'unless' makes some things more readable. To learn the
internals of the compiler I've been adding an unless statement to
Python, but one shouldn't need to do this in order to add useful
constructions like this.

Guido van Rossum writes:
> I've considered it, and rejected it.  That doesn't mean you shouldn't
> bring it up, but I expect it would turn Python into an entirely
> different language.

Perhaps, perhaps not. Did the addition of hygienic macros to Scheme
make it an entirely different language?

> I recently had a thought about how Python and C++ represent extreme
> ends of the spectrum of how much happens in the parser/compiler
> vs. how much happens at run time (although Lisp would be even more
> extreme than Python on this scale).
[...]

This is comparing apples to oranges though? C++ is a monstrosity, as
anyone who has worked on a C++ compiler (especially the front-end!)
can tell you. The complications that are introduced into the language
runtime by features like parameterized types far outweight their
usefulness IMHO. I agree with all of your points about C++, which is
why I avoid many of the bletcherous features you describe.

Even with parameterized types aside, trying to debug any non-trivial
amount of C or C++ that makes use of a large number of function macros
is a royal PITA. Indeed, Stroustrup recommends replacing macros with
inline functions for this very reason: at least then you can step into
the functions.

I don't think it would be possible to add hygienic macros to C/C++,
and I think it would be a bad idea adding pre-processor-based macros
to Python. However, Scheme and Dylan both provide high-level
environments where macro systems have been instituted and tools
created to successfully debug code using them. The really issue is
comparing Python to Lisp/Scheme/Dylan, and seeing where it's dichotomy
between runtime and compile time leave the programmer.

> I guess I'm wary of anything that adds compile time complexity,
> especially if the user will be aware of it -- I'm not set against
> adding optimizations, as long as they don't change the semantics and
> don't add new error messages or warnings.

Well, adding any type of macro system will add compile time
complexity. The question is whether or not the benefits make that
extra complexity worth-while.

Anyway, just some random thoughts.

    -tree

-- 
Tom Emerson                                          Basis Technology Corp.
Sr. Computational Linguist                         http://www.basistech.com
  "Beware the lollipop of mediocrity: lick it once and you suck forever"