[plt-scheme] Re: define-syntax help

From: Kyle Smith (airfoil at bellsouth.net)
Date: Sat Mar 24 10:29:15 EDT 2007

Jacob Matthews <jacobm at ...> writes:

> 
> In this version, lookup-context is a parameter that only exists at
> compile-time, not at runtime. At compile time, register does the same
> parameterization, binding lookup-context to the name of the object
> currently being registered, but now, rather than being bound during
> the _evaluation_ of register's expression it's being bound during the
> _expansion_ of that expression. The consequence of this is that
> lookups that don't specify an object must be syntactically nested
> inside   registers, or they will be syntax errors. So myfun, defined
> above, is no longer syntactically legal, and the if statement will be
> a statically-detected syntax error, but the working example will still
> produce (2 7 14) as we wanted originally.
> 
> -jacob

Hi Jacob,

Forgive me for cutting in on this thread, but it interested me.  So, I tried 
out the implicit-object-static module with the `working example', if I'm 
correct, with the following results:

-------------------------Begin Definitions---------------------
(module implicit-object-static mzscheme

  (require (lib "stxparam.ss"))
  (require-for-syntax (lib "stxparam.ss"))
  (provide lookup register)

  (define *field-hash* (make-hash-table))
  (define-syntax-parameter lookup-context #f)

  ;; syntaxes->field-symbol : syntax[id] syntax[id] -> symbol
  ;; given identifiers a and b, produces 'a.b
  (define-for-syntax (syntaxes->field-symbol o f)
    (string->symbol (format "~a.~a" (syntax-object->datum o)
(syntax-object->datum f))))

  (define-syntax (lookup stx)
    (syntax-case stx ()
      [(_ field)
       (unless (syntax-parameter-value #'lookup-context)
         (raise-syntax-error #f "cannot use lookup shorthand outside
the lexical context of a register statement" stx))
       (with-syntax ([implicit-object-name (syntax-parameter-value
#'lookup-context)])
         (syntax/loc stx (lookup implicit-object-name field)))]
      [(_ obj field)
       (with-syntax ([object-symbol (syntaxes->field-symbol #'obj #'field)])
         (syntax/loc stx (hash-table-get *field-hash* 'object-symbol)))]))

  (define-syntax (register stx)
    (syntax-case stx ()
      [(_ field expr obj)
       (with-syntax ([object-symbol (syntaxes->field-symbol #'obj #'field)])
          (syntax/loc stx
            (syntax-parameterize ((lookup-context #'obj))
              (hash-table-put! *field-hash* 'object-symbol expr))))]))
  )
(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--------------------------------

I'm not sure what the problem is.  I tried placing the definition of
*field-hash* in a separate module and the requiring-for-syntax that 
module with the same results.  Oddly, with the exact same version of
DrScheme on my Mac Pro gives a slightly different error message,
just complaining about an unbound reference to *field-hash*.

If you know what the problem is I would greatly appreciate it; or if
I'm using the wrong code to test with I'd likewise be grateful for 
correction.

Thanks,

--kyle
airfoil at bellsouth dot com
schemekeys.blogspot.com




Posted on the users mailing list.