[plt-scheme] "identifier used out of context" after two local-expands

From: Kimberley Burchett (kim.burchett at gmail.com)
Date: Mon May 29 14:27:05 EDT 2006

I'm trying to write a macro that takes a single subform and does this:
  1) expands all the macros in that subform except for one particular macro
  2) recursively walks the expanded result
  3) when the walk encounters the unexpanded macro, expands the
unexpanded subforms.

I'm using local-expand for this, because it has a stop-list.  Steps 1
and 2 seem to work just fine, but I have a problem in step #3.  When I
try to expand the subforms via a second call to local-expand, I get
"identifier used out of context" errors.  This only happens for
identifiers that were lexically bound in the original subform.  I've
whittled the problem down to the following small example, where the
macro I don't want to be expanded is named "stop".

(define-syntax (phase-one stx)
  (syntax-case stx ()
    ((_ e)
     (let* ((stop-id (datum->syntax-object stx 'stop))
            (partially-expanded (local-expand #'e
                                              (list stop-id))))
       (phase-two partially-expanded)))))

(define-for-syntax (phase-two stx)
  (syntax-case stx ()
    ((lambda formals e)
     #`(lambda formals #,(phase-two #'e)))
    ((i x1 x2 x3)
     (local-expand #'(i x1 x2 x3) (syntax-local-context) ()))))

(phase-one (lambda (x) (stop x x x)))

>>> compile: identifier used out of context in: x

Can anyone explain to me what I'm doing wrong?

Posted on the users mailing list.