Here's another tempting one: if a module imports a library but doesn't use some of the library's exported bindings, it seems like it should be safe not to load those bindings, right? Nope! Because the macro system has complete access to the syntactic environment, a macro exported by the module might actually compute a reference to the binding:
The syntax object #'here encapsulates the entire syntactic environment, and if the imported library exports mumbly-foo, looking up 'mumbly-foo in the syntactic environment will find it. Similarly, run-time code in the module might perform(define-syntax (foo stx)
(syntax-case stx
... (datum->syntax-object
#'here
(string->symbol "mumbly-foo")) ...
and it would again find the binding. So it's not generally possible to prune any library imports, as long as macros and eval have complete access to the syntactic environment through syntax objects.(eval (datum->syntax-object #'here 'mumbly-foo))
Well, eval is scary... does this mean the first optimization is unsafe, say, if eval tries to mutate it? I don't think so. Module bindings are stored in the lexical (i.e., local) environment, which I believe is not available to eval. The bindings available to eval are the ones that come from PLT Scheme's "namespace" objects. As I understand it, these namespaces do not contain mappings for local variables, only for globals and module imports.
2 comments:
It is possible to get a namespace corresponding to the bindings visible within a module (using 'module->namespace'). That's how DrScheme's "(module ...)" language works. By default, though, the compiler does not allow mutation of variables through that namespace (but that's customizable via the 'compile-enforce-module-constants' parameter).
So yes, the first optimization is safe.
Dave,
Quick question that has nothing to do with this blog: The logo of the word "timshel" on your homepage....is that how it is written in Hebrew? I'm having a hard time finding anywhere it is written in Hebrew (including a bunch of online dictionaries). The story of timshel is by far my favorite part of East of Eden.
Can you please let me know the answer by emailing me? It'd be much appreciated. (bschott40@msn.com)
Post a Comment