[racket] Using ... in a syntax pattern destroys location information?

From: Jay McCarthy (jay.mccarthy at gmail.com)
Date: Tue Jun 28 20:01:11 EDT 2011

I think this program is instructive:

#lang racket

(define-syntax (see stx)
  (printf "see\n")
  (printf "~v\n" (syntax-line stx))
  (syntax-case stx ()
    [es
     (printf "~v\n" (syntax-line #'es))])
  (syntax-case stx ()
    [(e ...)
     (printf "~v\n" (syntax-line #'(e ...)))])
  (syntax-case stx ()
    [(e ...)
     (printf "~v\n" (syntax-line (syntax/loc stx (e ...))))])
  #'(void))

(see 1 2 3 4)

(define-syntax (see2 stx)
  (printf "see2\n")
  (printf "~v\n" (syntax-line stx))
  (syntax-case stx ()
    [(_ es)
     (printf "~v\n" (syntax-line #'es))])
  (syntax-case stx ()
    [(_ (e ...))
     (printf "~v\n" (syntax-line #'(e ...)))])
  (syntax-case stx ()
    [(_ (e ...))
     (printf "~v\n" (syntax-line (syntax/loc stx (e ...))))])
  #'(void))

(see2 (1 2 3 4))

Output is:

see
17
17
11
17
see2
33
33
27
33

2011年6月28日15:23 Jay McCarthy <jay.mccarthy at gmail.com>:
> Syntax info is stored in the cons.   ... constructs a new cons. The elements are the same so they have the same syntax locations. But the cons is new, so it has no syntax location information.
>
> Your method is one way to avoid this, but there are others.  But I'm dictating to my wife (I'm driving) so I can't go into them right now.
>
> xoxo
> Jay's personal secretary
>
> iPhoneから送信
>
> On 2011/06/28, at 14:51, Danny Yoo <dyoo at cs.wpi.edu> wrote:
>
>> I was working with a few friends, and we came across the following
>> behavior: simple usage of the syntax pattern matching system doesn't
>> seem to preserve the syntax location of syntax objects under certain
>> conditions.
>>
>>
>> Here's an example to demonstrate:
>>
>> ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
>> #lang racket
>>
>> (define x (datum->syntax #f 'hello (list 'my-source 3 3 18 5)))
>> (define y (datum->syntax #f 'world (list 'my-source 3 9 24 5)))
>>
>> (define a-bigger-syntax #`(module foo racket/base (#%module-begin #,x #,y)))
>>
>> (define a-reconstructed-syntax
>>  (syntax-case a-bigger-syntax ()
>>    [(module name language (a-modbeg body ...))
>>     #'(module name language (a-modbeg body ...))]))
>> ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
>>
>>
>> When we inspect the syntax locations in a-reconstructed-syntax, we see
>> funkiness associated with the locations of the body elements.
>>
>>
>> We've isolated the issue down to the use of ...; if we do the
>> following, things look slightly better, but the location of the
>> 'world' is still messed up.
>>
>> ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
>> (define another-reconstructed-syntax
>>  (syntax-case a-bigger-syntax ()
>>    [(module name language (a-modbeg . body))
>>     #'(module name language (a-modbeg . body))]))
>> ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
>>
>>
>> I guess I'm trying to say: I don't understand the behavior of syntax
>> and syntax-case, with regards to how they preserve the source
>> locations of the matched sub-elements.
>> _________________________________________________
>>  For list-related administrative tasks:
>>  http://lists.racket-lang.org/listinfo/users
>



-- 
Jay McCarthy <jay at cs.byu.edu>
Assistant Professor / Brigham Young University
http://faculty.cs.byu.edu/~jay

"The glory of God is Intelligence" - D&C 93



Posted on the users mailing list.