[plt-scheme] more stupid macro tricks

From: Doug Orleans (dougo at place.org)
Date: Tue May 18 01:52:08 EDT 2004

Here's a hack to keep track of definition expressions:

(module src mzscheme
  (require (lib "etc.ss"))
  (require-for-syntax (lib "boundmap.ss" "syntax"))
  (provide (all-defined))

  (define-syntax-set (src def)
    (define *srcs* (make-module-identifier-mapping))
    (define (get-src var)
      (module-identifier-mapping-get *srcs* var (lambda () #f)))
    (define (set-src! var expr)
      (module-identifier-mapping-put! *srcs* var expr))

    (define (src/proc stx)
      (syntax-case stx ()
	((_ var) #`(quote #,(get-src #'var)))))

    (define (def/proc stx)
      (syntax-case stx ()
	((_ var expr) (begin (set-src! #'var #'expr) #'(define var expr)))))

Welcome to MzScheme version 206.1, Copyright (c) 2004 PLT Scheme, Inc.
> (require "src.ss")
> (def x (+ 1 2))
> (src x)
(+ 1 2)
> (def x (* 3 4))
> (src x)
(* 3 4)
> (let () (def x 'foo) (src x))
> (src x)
(quote foo)

I think I know why this is going wrong: at the time `set-src!' is
called, `var' is still a free identifier (i.e. refers to the top-level
binding), but when the `define' is expanded (after `def/proc'
returns), `var' becomes bound.  Is this right?  Is there a way to get
this to do the right thing, i.e. call `set-src!' on the identifier
after it becomes bound?

--dougo at place.org

P.S. I used `make-bound-identifier-mapping' at first, but that didn't
work at all.  Shouldn't that have worked the same for identifiers
without module context?  Once again I can't seem to wrap my head
around the difference between `bound-identifier=?' and

Posted on the users mailing list.