[plt-dev] identifier-prune-lexical-context

From: Matthew Flatt (mflatt at cs.utah.edu)
Date: Sun May 17 08:33:28 EDT 2009

[File under "obscure macrology" and "macro-expansion performance".]

The `identifier-prune-lexical-context' function added in 4.2.0.2 strips
lexical information from an identifier if the information doesn't
affect the identifier's binding (or, more generally, the binding of
identifiers derived from a given list of symbols).

There's also a derived `quote-syntax/prune' form, which is like
`quote-syntax' on an identifier, but that prunes the lexical
information from the identifier. Note that `(quote-syntax/prune id)' is
different from `(identifier-prune-lexical-context (quote-syntax id))';
the later would wait until the expression is evaluated to prune lexical
information, while the former prunes it as the expression is expanded
(so that the lexical information isn't kept in bytecode, etc.).


You might wonder why irrelevant lexical information would be preserved
in the first place. The reason is `datum->syntax': The macro system
doesn't know whether lexical information will be transferred to some
other datum, where that extra information could become relevant.

Probably `datum->syntax' is too powerful an operation, but I don't know
how to get rid of it. So, we've fought the excessive-information
problem with a lot of encoding, caching, and lazy-initialization
tricks. These tricks, however, fail for nested internal-definition
contexts created by `package', especially when `define*'s are used
within a package to create layers of bindings. I couldn't find new
tricks that work well enough without a pruning hint from the
programmer.


Use `quote-syntax/prune' or `identifier-prune-lexical-context' to
potentially reduce bytecode size when an identifier's context won't be
transferred to other syntax objects --- or perhaps only to '#%top,
which `quote-syntax/prune' excludes from pruning in addition to the
identifier's symbol. Pruning probably is not worthwhile on a macro
template, but it can be useful in the expansion of forms like
`define-struct' and `package' that keep lists of identifiers.



Posted on the dev mailing list.