[plt-scheme] Dot-notation for structure field access

From: jos koot (jos.koot at telefonica.net)
Date: Mon Mar 12 16:23:34 EDT 2007

Hi, my code is very wrong! It goes busted in non top level scopes (eg within let) This I experienced when trying to write syntaxes let-struct-type and let-struct-var. The reason is shown below. I dont know any method to find out whether or not two non top level identifiers reference the same var. Is there? I did not find any such tool in PLT-help.

(((((lambda(x)((((((((x x)x)x)x)x)x)x)x))
    (lambda(x)(lambda(y)(x(x y)))))
  (lambda()(printf "Greetings, Jos~n"))))

(module wrong mzscheme
 (define-for-syntax var-table ())
 (define-for-syntax (lookup var)
  (let loop ((table var-table))
   (and (not (null? table))
    (let ((kar (car table)))
     (or (bound-identifier=? var kar) ; <=== WRONG
      (module-identifier=? var kar)
      (loop (cdr table)))))))
 (define-for-syntax (check-table stx tag var)
  (if (lookup var)
   (printf "var found: ~a ~a (table length ~a)~n"
    (syntax-object->datum var)
    (syntax-object->datum tag)
    (length var-table))
    (set! var-table (cons var var-table))
    (printf "var added: ~a ~a (table length ~a)~n"
     (syntax-object->datum var)
     (syntax-object->datum tag)
     (length var-table)))))
 (define-syntax (def stx)
  (syntax-case stx ()
   ((def tag var val)
     (check-table stx #'tag #'var)
     #'(define var val)))))
 (define-syntax (ref stx)
  (syntax-case stx ()
   ((ref tag var)
     (check-table stx #'tag #'var)
 (provide def ref))

(require wrong)

(let ((a -1)) (def 1 a -2) (ref 2 a))
(let ((a -3)) (def 3 a -4) (let () (ref 4 a)))

; Produced output
; Welcome to DrScheme, version 369.8-svn9mar2007 [3m].
; Language: Textual (MzScheme, includes R5RS) custom.
; var added: a 1 (table length 1)
; var found: a 2 (table length 1)
; -2
; var added: a 3 (table length 2)
; var added: a 4 (table length 3) ; added!!!
; -4
; > 
; The reason is that in the context of ref 4 variable 'a' would bind
; differently from the context of ref 3.
; This means that in a local context, the register-lookup fails if a
; reference is made to a struct-type defined outside this context.
  ----- Original Message ----- 
  From: George Herson 
  To: jos koot ; Jens Axel Søgaard 
  Cc: plt-scheme at list.cs.brown.edu 
  Sent: Monday, March 12, 2007 8:16 AM
  Subject: Re: [plt-scheme] Dot-notation for structure field access

  thanks for this work.  I'm using hash tables at
  present but this code will inform my Scheme if not my
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.racket-lang.org/users/archive/attachments/20070312/39b4ca02/attachment.html>

Posted on the users mailing list.