[plt-scheme] Re: define-syntax help

From: Jacob Matthews (jacobm at cs.uchicago.edu)
Date: Sat Mar 24 14:48:10 EDT 2007

On 3/24/07, Kyle Smith <airfoil at bellsouth.net> wrote:

> (require implicit-object-static)
> (begin
>       (register test 2 main)
>       (register value 7 other)
>       (let* ([v1 (lookup main test)]
>              [v2 (lookup other value)]
>              [v3 (begin
>                    (register amount (* 2 (lookup value)) other)
>                    (lookup other amount))])
>         (list v1 v2 v3)))
> ------------------------End Definitions----------------------------
> ------------------------Begin Results------------------------------
> Welcome to DrScheme, version 369.8-svn2mar2007 [3m].
> Language: Textual (MzScheme, includes R5RS).
> compile: access from an uncertified context to unexported variable from
> module: implicit-object-static in: *field-hash*
> compile: access from an uncertified context to unexported variable from
> module: implicit-object-static in: *field-hash*
> . hash-table-get: no value found for key: main.test
> >
> ------------------------End Results--------------------------------

Hm, I had originally tested the code in module language, where this
problem doesn't crop up. The complaint appears to be that
syntax-parameterize is somehow ending up causing the reference to
*field-hash* to end up losing its certification (see mzscheme manual
12.6.3). I don't know why that is --- maybe a better macrologist than
I can explain why that's happening? --- but fortunately there's an
easy fix. Replace the definition of register with this:

(define-syntax (register stx)
    (syntax-case stx ()
      [(_ field expr obj)
       (with-syntax ([object-symbol (syntaxes->field-symbol #'obj #'field)])
         (syntax/loc stx
           (let ([ht *field-hash*])
             (syntax-parameterize ((lookup-context #'obj))
               (hash-table-put! ht 'object-symbol expr)))))]))

Now the body of the syntax-parameterize only includes lexical bindings
--- not the troublesome reference to *field-hash* --- so there's no
problem with certificates. But again, I'm not sure why this little
hack ought to be necessary.

-jacob


Posted on the users mailing list.