From: netytan
Subject: Custom macro systems.
Date: 
Message-ID: <1164938530.816214.310200@16g2000cwy.googlegroups.com>
Hi all,

I am interested in implementing my own vision of what macro systems
should be, primarily for educational reasons.

I don't want to start a flame war but on a more personal note: after
using both for quite a while I generally feel that Scheme is a nicer
language, but the macros are a pain in the ass and that's a very
important part of the lisping experience.

After comparing CL and Scheme I'm undecided on which one to attempt the
project in. Which language is more amenable to this type of work?

Is there any particular reason that Scheme moved away from the more
programmatic view of macros in CL? (Ignoring hygiene which I feel could
be implemented just as effectively in other styles.)

Ideally I don't want to implement my own interpreter just to diddle the
macro system. Being quite new to the subject I'm looking for someone to
assert that I don't have too. I've seen implementations of defmacro and
syntax-rules as libraries, but they seemed to rely on macros already
being present.

Lastly, could you implement something like quasi quote in Scheme
without writing an interpreter for it. I understand that Scheme doesn't
have a standard way to create read-macros.

Thanks in advance for any advice, comments or conversation,

Take care,

Mark.

From: Steven Haflich
Subject: Re: Custom macro systems.
Date: 
Message-ID: <v6Nbh.1546$Py2.347@newssvr27.news.prodigy.net>
Pillsy wrote:
  > Yes. I was wondering whether
 >
 > (shadow 'defun)
 > (defmacro defun ...)
 >
 > and the like were seriously frowned upon.

Well, it makes your code a little less readable to someone who knows the
standard language.  I think it's better to use fresh names.

By the way, do you intend to add types to method lambda lists as well.
This is a serious question.  Consider:

(defmethod foo ((x integer))
   (declare (fixnum x))   ; Because I know this will be the case.
   ...)

This sort of thing is rarely needed, of course, much less often than
it is needed for defuns.  But you could for consistency permit:

(defmethodt foo ((x integer fixnum))
   ...)
From: Pillsy
Subject: Re: Custom macro systems.
Date: 
Message-ID: <1164944772.241396.292370@16g2000cwy.googlegroups.com>
Steven Haflich wrote:
[...]
> By the way, do you intend to add types to method lambda lists as well.
> This is a serious question.  Consider:

> (defmethod foo ((x integer))
>    (declare (fixnum x))   ; Because I know this will be the case.
>    ...)

> This sort of thing is rarely needed, of course, much less often than
> it is needed for defuns.  But you could for consistency permit:

> (defmethodt foo ((x integer fixnum))
>    ...)

Hmm. That's not a bad idea, and it shouldn't be super-hard to do once I
have it working for deftfun.

Thanks for the suggestion, 
Matt
From: Pascal Bourguignon
Subject: Re: Custom macro systems.
Date: 
Message-ID: <87fyc05lgv.fsf@thalassa.informatimago.com>
"netytan" <·······@gmail.com> writes:

> Hi all,
>
> I am interested in implementing my own vision of what macro systems
> should be, primarily for educational reasons.
>
> I don't want to start a flame war but on a more personal note: after
> using both for quite a while I generally feel that Scheme is a nicer
> language, but the macros are a pain in the ass and that's a very
> important part of the lisping experience.
>
> After comparing CL and Scheme I'm undecided on which one to attempt the
> project in. Which language is more amenable to this type of work?
>
> Is there any particular reason that Scheme moved away from the more
> programmatic view of macros in CL? (Ignoring hygiene which I feel could
> be implemented just as effectively in other styles.)
>
> Ideally I don't want to implement my own interpreter just to diddle the
> macro system. Being quite new to the subject I'm looking for someone to
> assert that I don't have too. I've seen implementations of defmacro and
> syntax-rules as libraries, but they seemed to rely on macros already
> being present.
>
> Lastly, could you implement something like quasi quote in Scheme
> without writing an interpreter for it. I understand that Scheme doesn't
> have a standard way to create read-macros.

It doesn't really matter, for both questions.  
You can implement then as a pre-processor.


For example, using CL formalism, instead of using the normal REPL, you
could implement your own REPL:

     (loop (print (eval (read))))

and then, replace the standard READ by your own reader-macro-enabled
READ:

     (loop (print (eval (read/reader-macros))))

And then, you can add your own macroexpansion phase:

     (loop (print (eval (expand-macros (read/reader-macros)))))


The only difference, is that you must first call this your own REPL,
and implement your own LOAD using these expand-macros and
read/reader-macros too, plus perhaps a couple of other functions such
as READ-FROM-STRING.


-- 
__Pascal Bourguignon__                     http://www.informatimago.com/
You're always typing.
Well, let's see you ignore my
sitting on your hands.
From: Pascal Costanza
Subject: Re: Custom macro systems.
Date: 
Message-ID: <4ta3kvF12ouldU1@mid.individual.net>
netytan wrote:
> Hi all,
> 
> I am interested in implementing my own vision of what macro systems
> should be, primarily for educational reasons.
> 
> I don't want to start a flame war but on a more personal note: after
> using both for quite a while I generally feel that Scheme is a nicer
> language, but the macros are a pain in the ass and that's a very
> important part of the lisping experience.

Depending on what it exactly is that you prefer in Scheme over CL, it 
might still be a good choice to stick to CL. Some things are relatively 
straightforward to embed into CL, like Lisp-1-style variables.

> After comparing CL and Scheme I'm undecided on which one to attempt the
> project in. Which language is more amenable to this type of work?

Hard to tell without more background knowledge about what you actually 
want to do. In general, CL has more standard functions for processing 
lists, etc., so it's easier to write portable code that can be run on 
different CL implementations. But again, it's hard to tell whether 
that's important to you or not.

> Is there any particular reason that Scheme moved away from the more
> programmatic view of macros in CL? (Ignoring hygiene which I feel could
> be implemented just as effectively in other styles.)

Hygiene is straightforward to achieve in CL-style macro systems. What's 
not so straightforward is "automatic" hygiene. Especially the 
referential transparency bit is hard to implement when it's not already 
part of the macro system.

For both pragmatic and aesthetic reasons, macro hygiene is more 
important in Scheme than in CL.

On top of that, what syntax-rules and syntax-case in Scheme provide are 
more means to destructure parts of your macro expressions, while keeping 
track of macro hygiene issues. In Common Lisp, by default only the macro 
lambda list supports destructuring, inside a macro definition, you have 
to destructure manually.

Of course, in Common Lisp, we have destructuring-bind and loop, with 
which we can get quite far as well. Interestingly, there are quite a few 
macro examples using syntax-rules and syntax-case that I have seen that 
can be expressed in a more compact way in CL. So syntax-rules and 
syntax-case doesn't seem to buy that much (but I am biased in this regard).

> Ideally I don't want to implement my own interpreter just to diddle the
> macro system. Being quite new to the subject I'm looking for someone to
> assert that I don't have too. I've seen implementations of defmacro and
> syntax-rules as libraries, but they seemed to rely on macros already
> being present.

That's important when you want a direct integration in the Lisp/Scheme 
system of your choice. Macros are a hook into the existing compiler to 
transform code before it is being compiled into its target 
representation (like machine code). If you want to make sure that your 
own macro system can be used in a convenient way, you don't have another 
choice than to use the existing hooks.

> Lastly, could you implement something like quasi quote in Scheme
> without writing an interpreter for it. I understand that Scheme doesn't
> have a standard way to create read-macros.

I vaguely recall that MIT Scheme supports read macros, but I could be 
wrong. Apart from that, it seems to me that read macros are non-existent 
in the Scheme world. (But maybe PLT Scheme...)



Pascal

-- 
My website: http://p-cos.net
Common Lisp Document Repository: http://cdr.eurolisp.org
Closer to MOP & ContextL: http://common-lisp.net/project/closer/
From: William D Clinger
Subject: Re: Custom macro systems.
Date: 
Message-ID: <1164981855.336872.25810@n67g2000cwd.googlegroups.com>
Pascal Costanza quoting netytan:

> > Lastly, could you implement something like quasi quote in Scheme
> > without writing an interpreter for it. I understand that Scheme doesn't
> > have a standard way to create read-macros.
>
> I vaguely recall that MIT Scheme supports read macros, but I could be
> wrong. Apart from that, it seems to me that read macros are non-existent
> in the Scheme world. (But maybe PLT Scheme...)

PLT Scheme, MIT Scheme, Larceny, and no doubt many other
implementations of Scheme provide mechanisms for defining
reader macros.  As netytan said, however, there is no standard
way to create them.

As for netytan's question about quasiquote, the R5RS requires
implementations to expand `E into (quasiquote E), so reader
macros aren't a problem for that.  In Scheme, the quasiquote
macro is hygienic, and can be defined using syntax-rules,
although doing so is generally considered perverse; for the
quasiquote macro in Larceny v0.93, "Deviated Prevert", see
http://larceny.ccs.neu.edu/larceny-trac/browser/trunk/larceny_src/src/Compiler/usual.sch

Will
From: Pascal Costanza
Subject: Re: Custom macro systems.
Date: 
Message-ID: <4td37bF11jic1U1@mid.individual.net>
William D Clinger wrote:
> Pascal Costanza quoting netytan:
> 
>>> Lastly, could you implement something like quasi quote in Scheme
>>> without writing an interpreter for it. I understand that Scheme doesn't
>>> have a standard way to create read-macros.
>> I vaguely recall that MIT Scheme supports read macros, but I could be
>> wrong. Apart from that, it seems to me that read macros are non-existent
>> in the Scheme world. (But maybe PLT Scheme...)
> 
> PLT Scheme, MIT Scheme, Larceny, and no doubt many other
> implementations of Scheme provide mechanisms for defining
> reader macros.  As netytan said, however, there is no standard
> way to create them.

OK, thanks for the correction.

Pascal

-- 
My website: http://p-cos.net
Common Lisp Document Repository: http://cdr.eurolisp.org
Closer to MOP & ContextL: http://common-lisp.net/project/closer/
From: netytan
Subject: Re: Custom macro systems.
Date: 
Message-ID: <1165271345.285950.12750@n67g2000cwd.googlegroups.com>
Hey everyone,

Sorry if I've been slow to respond, it's been a busy week (lots of
coursework etc).

Anyway I think that I understand it now. I've started my quasi-quote
language which I hope to expand into a macro system when I have some
more time. Either way it's an interesting exercise.

I know that read-macros get a bit of bad press from some people, but
really what's wrong with them. They provide a nice touch for some
things. I can't say that I don't understand the reasons that Scheme
left them out, but it's not the choice I would have made.

I'm thinking about how read macros would be implemented, but it seems
that they're tied into the parser for the language itself. Maybe I'll
have to make my own lisp before I really get it.

I'll look at the implementations that were mentioned as supporting them
:).

Thank you all so much for your input,

Mark.
From: Pascal Costanza
Subject: Re: Custom macro systems.
Date: 
Message-ID: <4tjml7F145u6qU1@mid.individual.net>
netytan wrote:

> I'm thinking about how read macros would be implemented, but it seems
> that they're tied into the parser for the language itself. Maybe I'll
> have to make my own lisp before I really get it.

This topic is actually quite well described in "Common Lisp, the 
Language, 2nd Edition." Fortunately, you can get that back for free from 
the web.

See for example http://oopweb.com/LISP/Documents/cltl/VolumeFrames.html

Read Chapter 22, especially the first part until and including 
readtables. Also see Chapter 32 for an implementation of backquote.


Pascal

-- 
My website: http://p-cos.net
Common Lisp Document Repository: http://cdr.eurolisp.org
Closer to MOP & ContextL: http://common-lisp.net/project/closer/
From: Alexander
Subject: Re: Custom macro systems.
Date: 
Message-ID: <1164994011.041136.150930@n67g2000cwd.googlegroups.com>
"""netytan �����(�):
"""
> Hi all,
>
> I am interested in implementing my own vision of what macro systems
> should be, primarily for educational reasons.

> I don't want to start a flame war but on a more personal note: after
> using both for quite a while I generally feel that Scheme is a nicer
> language, but the macros are a pain in the ass and that's a very
> important part of the lisping experience.

What are talking about? Syntax-case or syntax-rules ?
Anyway the best way to compare different macro systems is to write
sufficiently advanced macro (iterate (http://www.cliki.net/iterate)
for example  or something like it) in different systems.