[plt-scheme] Re (modified): macros that expand into LISTS OF top-level definitions with computed names

From: Lee Spector (lspector at hampshire.edu)
Date: Fri Aug 14 11:15:24 EDT 2009

On Aug 12, 2009, at 12:25 PM, Matthias Felleisen wrote:
> Here is a macro that computes with partial name? Is this good enough?

This does indeed do what I asked, and has taught me something about  
PLT macros, but it raises a new issue relative to my actual problem:  
Is there a straightforward way to iteratively construct a PLT macro  

As I mentioned in one of my replies my goal here is to be able to loop  
over lists of items and perform definitions for each (defining names  
that are computed from each). To be more explicit, my code currently  
includes things like this, where pusher is a procedure that returns a  
procedure and exec, integer, float, etc. are types in my embedded  

(define exec.push (pusher 'exec))
(register-instruction 'exec.push)

(define integer.push (pusher 'integer))
(register-instruction 'integer.push)

(define float.push (pusher 'float))
(register-instruction 'float.push)

(define code.push (pusher 'code))
(register-instruction 'code.push)

(define boolean.push (pusher 'boolean))
(register-instruction 'boolean.push)

Since I have to do this for lots of instructions (not just push), and  
potentially many more types, and since I'll sometimes want to exclude  
some of the instructions and/or types from particular runs, it'd be  
nicer to define a single a list of all the types (exec, integer,  
float, ...) and then define a particular instruction for all of those  
types by saying something like:

(define-all-types 'push '(pusher type))

The code you sent me shows me how to produce a macro that expands into  
something like "(define exec.push ..." -- which was not obvious and  
I'm glad to know how to do that now -- but not how to perform an  
iteration in producing the expansion, which in this case should end up  
being something like "(begin (define exec.push (pusher 'exec))  
(register-instruction 'exec.push) (define integer.push... "

I made a couple of stabs at including iteration forms in the define- 
syntax call but all failed for one reason or another. Is there a way  
to do this that will work?

I can easily construct the appropriate expansion as a list and pass it  
to eval, but then I still have the problem of how to "provide" the  
resulting computed names to another module.

Of course I can also generate a source code file to be used in a  
manual second compilation, but that's not so nice.

And of course I can also just type in the definitions manually for  
each type/instruction pair, which is probably what I'll do for now.



On Aug 12, 2009, at 12:25 PM, Matthias Felleisen wrote:

> On Aug 12, 2009, at 11:26 AM, Lee Spector wrote:
>> In fact I DO know partial-name at compile time, or at least at what  
>> should be compile time for the code that will call the resulting  
>> functions. That is, I'm not going to be doing things like taking  
>> input from the user that gets turned into function names. But I  
>> want to do some computation to produce the function names and  
>> definition bodies since these will be combinatorial products of  
>> lists of symbols. Is there a way to do this either using PLT macros  
>> or module instantiation phases?
> Here is a macro that computes with partial name? Is this good enough?
> #lang scheme/load
> (module c scheme
>  (define partial-name "yours")
>  (provide partial-name))
> (module a scheme
>  (require (for-template 'c) (for-syntax 'c))
>  (define-syntax (def stx)
>    (syntax-case stx ()
>      [(_ f x body ...)
>       (let* ([f:sym (syntax-e (syntax f))]
>              [my-f:str (string-append "my-" partial-name (number- 
> >string (string-length partial-name)) (symbol->string f:sym))]
>              [my-f:sym (string->symbol my-f:str)]
>              [my-f:id (datum->syntax stx my-f:sym)])
>         (printf "~a\n" my-f:sym)
>         #`(define #,my-f:id (lambda (x) body ...)))]))
>  (provide def))
> (module b scheme
>  (require 'a)
>  (def f x (sqrt x))
>  (printf "~a\n" (my-yours5f 4))
>  (def yours x (sqrt x))
>  (printf "~a\n" (my-yours5yours 4)))
> (require 'b)

Lee Spector, Professor of Computer Science
School of Cognitive Science, Hampshire College
893 West Street, Amherst, MA 01002-3359
lspector at hampshire.edu, http://hampshire.edu/lspector/
Phone: 413-559-5352, Fax: 413-559-5438

Check out Genetic Programming and Evolvable Machines:
http://www.springer.com/10710 - http://gpemjournal.blogspot.com/

Posted on the users mailing list.