One thing hit me as I went to bed last night about this problem:

Writing a macro to optimize an s-exp *is* writing a compiler.

The good news is that you *don't* have to write a parser.  There is
some low hanging fruit here (like the + macro described above), but I
imagine there will be a lot of subtle cases that make it very
difficult do to this job.  The biggest issue I see is that you could
accidently apply a macro that creates MORE work for the JVM.

So I'd recommend doing something for the easy cases, and let the JVM
do its job for everything else.

Sean

On May 12, 7:12 am, Paul Stadig <p...@stadig.name> wrote:
> You could write a Clojure program that took in a Clojure program and spit
> out an optimized Clojure program. You could do something similar to the
> meta-circular evaluator, where you step through the program (which is just a
> data structure) recursively matching the head each time against different
> symbols. In your case you could match against '+ and do something special,
> but as Konrad mentioned there are some difficulties with that.
>
> Another possibility would be to write a macro called my-+ or ++ or
> something, and use that instead. This would be a simple solution, because
> you wouldn't have to rewrite all of the matching for all of the different
> forms. The only problem would be that you'd have to change any existing
> code, but at least that way you'd be optimizing the parts of your code that
> need it and it would be clear what you are doing. You could also exclude
> clojure.core/+ from your namespace and write a macro called + that would
> then call clojure.core/+. I'm not sure if the following code works and is
> bug-free, but it'd be something like:
>
> (ns test
>   (:refer-clojure :exclude [+]))
>
> (defmacro +
>   ([] 0)
>   ([a] `(clojure.core/+ ~a))
>   ([a b] `(clojure.core/+ ~a ~b))
>   ([a b & c] `(clojure.core/+ ~a (+ ~b ~...@c))))
>
> There are some "challenges" to having '+ or my-+ or ++ be a macro, it means
> that you would have to wrap it with a function literal before you could pass
> it to functions like 'map.
>
> It is true that '+ gets inlined with two arguments, so it is faster than
> calling it with three or more arguments. Another possibility would be to
> just discipline yourself to use the two argument form instead of the three
> or more argument form.
>
> Paul
>
> On Mon, May 11, 2009 at 11:42 PM, Mark Reid <mark.r...@gmail.com> wrote:
>
> > Hi,
>
> > I'm quite new to macros so forgive me if this is a naïve question, but
> > is it possible to write macros that are applied to an entire Clojure
> > program?
>
> > The reason I ask is that, in other threads in this group, some simple
> > transformations to improve efficiency of Clojure programs were
> > mentioned. In particular, it seems converting `(+ 1 2 3)` to `(+ 1 (+
> > 2 3))` can speed things up. Applying such a transformation to my own
> > code would be a mindless pattern matching job, and thus perfect for
> > automation.
>
> > Any suggestions how I might write a Clojure program that takes as
> > input another Clojure program and outputs a third one that is
> > optimised but semantically equivalent to the input? Are macros the
> > right tool for the job in this case?
>
> > Thanks,
>
> > Mark.
> > --
> >http://mark.reid.name
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"Clojure" group.
To post to this group, send email to clojure@googlegroups.com
To unsubscribe from this group, send email to 
clojure+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/clojure?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to