Reflecting on the lexical environment at macro expansion time

36 views
Skip to first unread message

William J. Bowman

unread,
Jul 17, 2019, 4:35:21 PM7/17/19
to Racket Users
I'm trying to write a macro that reflects on which identifiers are in scope in
the environment when it runs, and on the identifiers in scope for its generated
code.
It should act something like this:

(define-syntax (display-env stx)
(syntax-case stx ()
[(_)
(printf "Env: ~a" (local-environment))]))

> (lambda (x) (display-env) x)
Env: '(x)
#<procedure>
> (lambda (x) (let-syntax ([y (make-rename-transformer #'x)]) (display-env) y))
Env: '(y x)
#<procedure>

Presumably, the expander knows which identifiers are in scope, so I *ought* to
be able to get my hands on that set.

namespaces aren't the thing; they only give me access to the top-level
definitions, as far as I can tell.
I also want access to any lexical identifiers introduced by `λ` or `let` or
`let-syntaxes` or `let-values` etc.
But I want to be able to use them pretty much like namespaces: get the list of
symbols in scope, and get their values and identifiers.

Any one know how to do this?

--
William J. Bowman

gfb

unread,
Jul 17, 2019, 4:46:10 PM7/17/19
to Racket Users

William J. Bowman

unread,
Jul 17, 2019, 5:02:15 PM7/17/19
to gfb, Racket Users
Excellent! Thanks.
> <https://docs.racket-lang.org/reference/stxops.html#(def._((quote._~23~25kernel)._syntax-debug-info))>
> <https://github.com/AlexKnauth/debug/blob/master/debug/repl.rkt>
>
> --
> 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...@googlegroups.com.
> To view this discussion on the web visit https://groups.google.com/d/msgid/racket-users/7b0eb8f1-b9cb-4065-9a90-1708237d5040%40googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.

Reply all
Reply to author
Forward
0 new messages