Hi again,<br><br><div class="gmail_quote">On Thu, Jul 14, 2011 at 6:23 PM, Eli Barzilay <span dir="ltr"><<a href="mailto:eli@barzilay.org">eli@barzilay.org</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin: 0pt 0pt 0pt 0.8ex; border-left: 1px solid rgb(204, 204, 204); padding-left: 1ex;">
<div class="im">Two hours ago, Markku Rontu wrote:<br>
> > this is not really true. The domain specific languages of<br>
> > syntax-rules and syntax-case macro transformers exist purely for<br>
> > your convenience. You can always manipulate syntax objects<br>
> > directly, all the necessary procedures are available and<br>
> > documented in section 11 of the Racket reference manual.<br>
> ><br>
> I'm sure the section 11 does go through the relevant issues, but<br>
> doesn't feel like a good introduction to the matter.<br>
<br>
</div>It's a reference, it's not supposed to be an introduction. For a more<br>
in-depth introduction than my blog post, see the chapter in the racket<br>
guide:<br>
<br>
<a href="http://docs.racket-lang.org/guide/macros.html" target="_blank">http://docs.racket-lang.org/guide/macros.html</a><br>
<div class="im"><br></div></blockquote><div><br>I feel reference material tends to require some other material as support. Your blog posting was a good start. Maybe it's wishful thinking, but many other languages have a lot of screencasts and talks online that I can watch. And also articles where I can see how people build stuff from scratch using their favourite environment. That's really cool stuff and I wish there was more on Racket as well.<br>
<br></div><blockquote class="gmail_quote" style="margin: 0pt 0pt 0pt 0.8ex; border-left: 1px solid rgb(204, 204, 204); padding-left: 1ex;"><div class="im">
<br>
> I can theorise that I can manipulate these syntax objects with plain<br>
> old functions but nowhere does it seem to show a complete useful<br>
> case. What I'm missing is an example that contains all the bits but<br>
> that is not as complicated like the Racket sources themselves. Maybe<br>
> I just didn't find it?<br>
<br>
</div>Note that usually you start with `syntax-rules' because it's simple<br>
and covers many cases. But running straight to the macro system<br>
bowels can be confusing since it's not just "lists and symbols". So<br>
the main tool that is used is `syntax-case', which can be used in a<br>
very similar way to `syntax-rules':<br>
<br>
(define def<br>
(syntax-rules (=)<br>
[(def x = y) (define x y)]))<br>
<br>
gets translated to<br>
<br>
(define (def stx)<br>
(syntax-case stx (=)<br>
[(def x = y) #'(define x y)]))<br>
<br>
It looks similar, but there are important differences:<br>
<br>
* `syntax-rules' is creating a macro function (=> it is itself a<br>
macro) for you, abstracting all of the details out. The translation<br>
defines `def' as a function directly -- se we have a name for the<br>
input syntax, `stx' is often used by convention.<br>
<br>
* We then use `syntax-case' which has the same kind of pattern<br>
language you know from `syntax-rules', but it needs to have its<br>
input specified directly. It's common to pass `stx', the input to<br>
the macro, but you can really give it anything you want.<br>
<br>
* And the most important change is that #' in the result. With<br>
`syntax-rules', the results are actually templates for plugging<br>
pattern variables in. But since we're writing plain racket code<br>
here, the results can be any racket expression. In this case, I use<br>
#'(define x y) which is actually short for (syntax (define x y)).<br>
This `syntax' thing is very similar to `quote', or actually more<br>
like a kind of a quasiquote since pattern matched variables are<br>
plugged into it -- so it's not really `x' and `y' that will be in<br>
the result, it will instead be what the `x' and `y' variables<br>
matched over when `stx' was fed into `syntax-case'.<br>
<br>
Again, see that blog post for an introduction that explains more from<br>
this POV.<br>
<br>
(This kind of explanations were also made countless times; it's easy<br>
to fall into the syntax-rules-only trap though.)<br>
<div class="im"><br></div></blockquote><div><br>Fortunately, I didn't fall for that one. I've been using syntax-case successfully for a long time because I quite soon needed to start breaking hygiene. I mostly write macros as sugar for various DSLs. There I need to often introduce new variables and avoid repetition.<br>
</div><blockquote class="gmail_quote" style="margin: 0pt 0pt 0pt 0.8ex; border-left: 1px solid rgb(204, 204, 204); padding-left: 1ex;"><div class="im">
<br>
> It's simple, it's been explained countless times, it works in many<br>
> cases.<br>
<br>
</div>No, it's broken in pretty much all cases. Give me a single symbolic<br>
macro and I'll show you how it's broken. (And point out how CL<br>
bypasses the problem...)<br>
<div><div></div><div class="h5"><br></div></div></blockquote><div><br>I think it's a bit disingenuous to say they are broken. People made successful systems using their simple but broken tools, yes? People are making successful systems in crippled languages such as Java and CL. And some people even like it :-) Sure they can only build a limited thing with the broken tool, but if you stay within its limits it doesn't matter, does it? Nobody is trying to implement Typed Clojure on top of the macro system, I hope :-) But as syntactic sugar for a DSL, where's the big problem that makes it entirely broken?<br>
<br>From a theoretical point of view, I think you are absolutely correct that the symbolic macros are broken. From a practical point of view, you can prove Racket macro superiority only by implementing superior systems and showing in practice that the choices you made were superior.<br>
<br>-Markku<br> </div><blockquote class="gmail_quote" style="margin: 0pt 0pt 0pt 0.8ex; border-left: 1px solid rgb(204, 204, 204); padding-left: 1ex;"><div><div class="h5">
--<br>
((lambda (x) (x x)) (lambda (x) (x x))) Eli Barzilay:<br>
<a href="http://barzilay.org/" target="_blank">http://barzilay.org/</a> Maze is Life!<br>
</div></div></blockquote></div><br>