[plt-scheme] Attaching compile time information to identifiers

From: Sam Tobin-Hochstadt (samth at ccs.neu.edu)
Date: Sun Oct 29 13:43:02 EST 2006

On Sun, 2006-10-29 at 13:21 -0500, David Van Horn wrote:
> I'd like to write two macros, f & g, with the following usage:
> 
> (f id e ...)
> (g id)
> 
> Such that for the same identifier id, g knows (during expansion) how 
> many e's were given in (f id e ...), ie. I'd like f to attach some 
> information to id that g can subsequently look up.  How can I do this?

What you probably want here is a `module-identifier-mapping', which is a
hashtable keyed on identifiers, with the equality being
`module-identifier=?'.  The transformer for `f' can add `id' to the map,
and then `g' can recognize id by looking it up in the map.  

The following code demonstrates the idea:

(module m mzscheme
  (require-for-syntax (lib "boundmap.ss" "syntax"))
  (define-for-syntax mapping (make-module-identifier-mapping))
  
  (provide f g)
  
  (define-syntax (f stx)
    (syntax-case stx ()
      [(f id args ...)
       (begin
         (module-identifier-mapping-put! mapping #'id #'(args ...))
         #'(void))]))
  
  (define-syntax (g stx)
    (syntax-case stx ()
      [(g id)
       (module-identifier-mapping-get mapping #'id)])))

(module n mzscheme
  (require m)
  
  (f one + 3 4)
  (f two * 9 10)
  
  (display (g one))
  (newline)
  (display (g two)))

sam th



Posted on the users mailing list.