[racket-dev] some surprising behavior with syntax-parameterize and lexical info

From: Michael W (mwilber at uccs.edu)
Date: Fri Apr 6 16:13:57 EDT 2012

This seems to work for me, printing 84, just like you expect:

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

(define-syntax (def stx)
 (syntax-case stx ()
   [(_ (name args ...) body ...)
    (with-syntax ([function-stx stx])
      (syntax/loc stx
        (define (name args ...)
          (splicing-syntax-parameterize ([current-def
                                          (quote-syntax fuunction-stx)])
            body ...))))]))

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

....Wait, what?


One hour ago, Danny Yoo wrote:
> >> I suspect that I should be using quote-syntax at this specific point,
> >> but I am not completely sure.
> >
> > Right. Try replacing ??? with (quote-syntax #,a-placeholder).
> 
> 
> I have to admit that I'm still confused.  Here's my example:
> 
> ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
> #lang racket
> 
> (require racket/stxparam racket/splicing)
> 
> (define-syntax-parameter current-def #f)
> 
> (define-syntax (def stx)
>  (syntax-case stx ()
>    [(_ (name args ...) body ...)
>     (with-syntax ([function-stx stx])
>       (syntax/loc stx
>         (define (name args ...)
>           (splicing-syntax-parameterize ([current-def
>                                           (quote-syntax
> function-stx)])
>             body ...))))]))
> 
> (define-syntax (outer stx)
>  (syntax-case stx ()
>    [(_ id)
>     (datum->syntax (syntax-parameter-value #'current-def)
>                    (syntax-e #'id))]))
> 
> (define x 42)
> (def (f x) (* (outer x) x))
> (f 2)
> ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
> 
> From what I understand of quote-syntax, it should create a syntax
> object that preserves the lexical information in function-stx.
> function-stx should be a syntax object whose lexical information does
> not include the function argument's variables.  Yet, when I run this,
> the program prints 4, rather than 84 as I expected.

-- 
Take it easy,
    _mike

Posted on the dev mailing list.