On Friday, August 18, 2017 at 10:14:10 AM UTC-4, Matthias Felleisen wrote:
> On Aug 18, 2017, at 9:44 AM, Sam Waxman <samw...@gmail.com> wrote:
> 
> On Friday, August 18, 2017 at 9:31:33 AM UTC-4, Matthias Felleisen wrote:
> On Aug 18, 2017, at 5:28 AM, Sam Waxman <samw...@gmail.com> wrote:
> 
> If I have code like this,
> 
> (define-syntax-rule (identity x)
>  x)
> 
> ((identity identity) 1)
> 
> At phase 1, the (identity identity) will get transformed into identity. As 
> macros expand outside in, however, this won't turn into (identity 1) like a 
> function would, it will try expanding the identity macro all by itself 
> without any "arguments" and give a bad syntax error.
> 
> Is there a simple way to change this so that ((identity identity) 1) actually 
> does expand into (identity 1) which expands into 1? I'm guessing not, but it 
> would certainly be a nice trick if there were.
> 
> 
> The explanation of the error is wrong. Run it in DrRacket and see which 
> identity it highlights for the error message. [I’d rather have you come up 
> with a proper explanation yourself. That way you will remember.]
> 
> It highlights the second. I don't think I misspoke above, but please correct 
> me if I'm wrong.
> 
> ((identity identity) 1) first starts by receiving the entire s-expression. 
> Then it tries expanding the first thing in that s-expression which is 
> (identity identity)
> 
> To do this, it goes to the identity rule, which states that identity is 
> allowed to expand if it has exactly one argument. In this case it does, so 
> #'(identity identity) becomes #'identity. Then instead of moving back to the 
> first s-expression and moving onto 1, because macro-expansion goes outside-in 
> (and more importantly, I suppose, depth first), the macro expander then tries 
> to expand #'identity. This is where the bad syntax error comes from because 
> the rule for identity says it must have an argument, but this identity is 
> getting expanded with no arguments.
> 
> 
> Sorry I misread your words. Yes, the expander re-expands the output of a 
> transformer (see below) and thus id is mentioned w/o arguments so the 
> compile-time call fails (or in old expanders produces a free var). 
> 
> 
> 
> 
> ;; Expression  = S-expression over Symbols only 
> ;; Table       = [Listof [List Symbol Transformer]]
> ;; Transformer = [Expression -> Expression]
> 
> ;; Expression Table -> Expression 
> (define (expander exp syntax-table)
>   (displayln exp)
>   (match exp
>     [(? number? n) n]
>     [(? symbol? s) s]
>     [`(,(? symbol? token) ,t)
>      (cond
>        [(in-domain? token syntax-table)
>         (expander ((retrieve token syntax-table) exp) syntax-table)]
>        [else (error 'expander "not a macro: ~e" token)])]
>     [`(lambda (,x) exp-body) `(lamnbda (,x) (expander exp-body))]
>     [`(,f ,a) `(,(expander f syntax-table) ,(expander a syntax-table))]
>     [else (error 'expander "bad syntax: ~e" exp)]))

Thanks, it's nice to see it written out like that!

-- 
You received this message because you are subscribed to the Google Groups 
"Racket Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to racket-users+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to