[plt-scheme] Macro problem: struct name coincides with macro keyword

From: Danny Yoo (dyoo at hkn.eecs.berkeley.edu)
Date: Wed May 3 14:18:09 EDT 2006

>>   (require (lib "42.ss" "srfi"))
>>
>>   (list-ec (: j (index i)'(a b c))
>>            (list i j))
>>   (define-struct index (lexicon positions))
>
>>   index: illegal use of syntax in: (index i)
>
> I mentioned a related problem a while ago:
>
> http://list.cs.brown.edu/pipermail/plt-scheme/2006-March/012013.html
>
>> My first thought was to rename the imported "index" from 42.ss,
>> but index is not exported from 42.ss! It is used purely as
>> a keyword.


Hi Lauri,

Perhaps the srfi-42 implementation be fixed by using the same 
syntax-parameter technique that Ryan Culpepper showed me in:

     http://list.cs.brown.edu/pipermail/plt-scheme/2006-April/012463.html

Let me see... perhaps something like:

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(module test-stx-parameterization mzscheme
   (require (lib "stxparam.ss"))

   (define-syntax-parameter index
     (lambda (stx)
       (raise-syntax-error 'index "Not in the context of a repeat" stx)))

   (define-syntax (repeat stx)
     (syntax-case stx ()
       [(_ (index i)
           test
           e1 e2 ...)
        (syntax/loc stx
          (let loop ([i 0])
            (when test
              e1 e2 ...
              (loop (add1 i)))))])))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;


This seems robust to identifier renaming:

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
> (module foo mzscheme
     (require (prefix : test-stx-parameterization))
     (define-struct index (lexicon positions))
     (:repeat (:index i) (< i 5)
        (printf "hello ~a~n" i)))

> (require foo)
hello 0
hello 1
hello 2
hello 3
hello 4
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;


Would it be acceptable to change PLT Scheme's srfi-42's implementation to 
turn index itself into syntax rather than treat it as a keyword 
identifier?


Posted on the users mailing list.