[racket] Dynamic Require of Syntax

From: Ryan Culpepper (ryanc at ccs.neu.edu)
Date: Fri Feb 1 10:29:42 EST 2013

The answer is roughly "that is not possible". In general, the meaning of 
a macro cannot be represented by a run-time value (but see below), so if 
you want to do something reflection-based with macros, you probably need 
to change your design to deal with terms (represented by syntax objects) 
instead of values.

Actually, it does make sense to talk about the run-time value that 
corresponds to a macro in the case of identifier macros---macros that 
can be used like variables. For example, if you use 'contract-out' to 
provide a variable 'f' with a contract, it actually creates an auxiliary 
identifier macro and provides that as 'f'. The expansion of the provided 
'f' macro fills in the negative blame part of the contract with the 
module where the expansion occurred and applies the contract to the 
original 'f' variable.

That's the situation handled by the extension of 'dynamic-require' to 
macros. So doing

   (dynamic-require mod macro-name)

is approximately the same as

   (parameterize ((current-namespace (make-empty-base-namespace)))
     (namespace-require mod)
     (eval macro-name))

(except that it uses 'namespace-attach-module' instead of just 
'namespace-require'). If 'macro-name' happens to be a macro that acts 
like a variable, then 'eval' produces a value. Otherwise, if it's 
something like 'define' or 'match' or 'for/list', you get an error, and 
it's exactly the same error you would get if you wrote something like

   #lang racket
   for/list

What to do next depends on what you want to do with the macros.

Ryan


On 02/01/2013 02:57 AM, Dan Grossman wrote:
>
> Cody and I would love an answer to this question from 1.5 months ago if
> anybody can point us in the right direction and/or ask us to clarify the
> question.  Or if the answer is "that is not possible" then we'll do
> something else.
>
> Much thanks!
>
> --Dan
>
> On Mon, Dec 17, 2012 at 9:20 PM, Cody Schroeder <codeblack08 at gmail.com
> <mailto:codeblack08 at gmail.com>> wrote:
>
>        I'm in a position that I would like to use dynamic-require
>     <http://docs.racket-lang.org/reference/Module_Names_and_Loading.html#(def._((quote._~23~25kernel)._dynamic-require))> to
>     gain access to all of the provides in a module.  This is quite
>     straightforward for normal procedures, but I also want this to
>     include syntax.  However, I get this error when trying to
>     dynamically require the blah macro from my test module:
>
>     (dynamic-require "test.rkt" 'blah)
>     blah: use does not match pattern: (blah body) in: blah
>
>        From the spec, it says that "If the module exports provided as
>     syntax, then a use of the binding is expanded and evaluated in a
>     fresh namespace to which the module is attached" when using
>     dynamic-require.  I don't understand how it's being used, though.
>
>        Is there a way to use dynamic-require how I want?  Is there a
>     better way?
>     Cody
>
>     ____________________
>        Racket Users list:
>     http://lists.racket-lang.org/users
>
>
>
>
> ____________________
>    Racket Users list:
>    http://lists.racket-lang.org/users
>


Posted on the users mailing list.