Given that Turnstile makes extremely heavy use of local-expand,
it’s easy to imagine that the macro expander could traverse a single
piece of syntax dozens of times when it really only needs to traverse
it once. Looking at the docs, it seems like syntax-local-expand-expression
is precisely designed for this sort of thing, but I have no idea
how to properly use it. I tried writing the simplest possible macro
I could think of, which expands a single expression and produces
it:

  (define-syntax-parser local-expanding-transformer
    [(_ e:expr)
     (define-values [stx opaque]
       (syntax-local-expand-expression #'e))
     opaque])

However, this doesn’t seem to work:

  > (local-expanding-transformer (+ 1 2))
  #%app: expanded syntax not in its original lexical context
         (extra bindings or scopes in the current context)
    in: (#%app + (quote 1) (quote 2))

This seems to happen because the resulting expression is expanded
in an internal definition context rather than an expression context,
because this works, instead:

  > (#%expression (local-expanding-transformer (+ 1 2)))
  3

However, trying to move that use of #%expression into the expansion
of local-expanding-transformer itself does not alleviate the problem:

  (define-syntax-parser local-expanding-transformer
    [(_ e:expr)
     (define-values [stx opaque]
       (syntax-local-expand-expression #'e))
     #`(#%expression #,opaque)])

  > (local-expanding-transformer (+ 1 2))
  #%app: expanded syntax not in its original lexical context
         (extra bindings or scopes in the current context)
    in: (#%app + (quote 1) (quote 2))

Considering this, does this mean that the only situation in which
it’s safe to use syntax-local-expand-expression is when
(syntax-local-context) is 'expression? Is there any way for me to
force the result of the macro to be expanded in an expression
context? Why doesn’t wrapping the result with #%expression work?

-- 
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