[racket] Why is syntax-e returning a pair and not a list?

From: Robby Findler (robby at eecs.northwestern.edu)
Date: Mon May 27 16:18:16 EDT 2013

I think it isn't really predictable and you should always just use
syntax->list.

But in the first case, when you write a pattern like

  (x y ...)

and you have an earlier syntax binding like you do, then the pattern
matching macros see this as a 'cons'-like operation on syntax and so you
get a pair of syntax objects.

Exploring the source locations of the intermediate "cons"es inside can also
help shed light on what's happening any particular example, too.

Robby



On Mon, May 27, 2013 at 2:52 PM, Tim Nelson <tbnelson at gmail.com> wrote:

> Thanks for the quick reply, and the helpful example!
>
> Can you say a bit more about the circumstances under which syntax-e
> will return a pair, even if its datum is a list? That's the biggest
> cause of confusion for me.
>
> For my purposes, I may just end up switching to syntax->list in my
> program, since that will always return a list.
>
> On Mon, May 27, 2013 at 3:27 PM, Jay McCarthy <jay.mccarthy at gmail.com>
> wrote:
> > A syntax object contains a lot of information in addition to the datum
> > inside it. Sometimes that datum will be a pair or a list. And
> > sometimes the extra data needs to be associated with just the pair and
> > sometimes with all the elements of the list.
> >
> > If you look at this program:
> >
> > #lang racket/base
> > (require (for-syntax racket/base
> >                      racket/match))
> > (begin-for-syntax
> >   (define show-syntax
> >     (match-lambda
> >      [(? syntax? s)
> >       (printf "syntax from line ~a\n"
> >               (syntax-line s))
> >       (show-syntax (syntax-e s))]
> >      [(? list? l)
> >       (printf "list\n")
> >       (for-each show-syntax l)]
> >      [(? pair? p)
> >       (printf "pair\n")
> >       (show-syntax (car p))
> >       (show-syntax (cdr p))]
> >      [(? symbol? s)
> >       (printf "symbol ~a\n" s)])))
> >
> > (define-syntax (ex stx)
> >   (show-syntax stx)
> >   #'(void))
> >
> > (define-syntax (helper1 stx)
> >   #`(ex #,stx))
> >
> > (define-syntax (helper2 stx)
> >   (syntax-case stx ()
> >     [(_ a ...)
> >      (syntax
> >        (ex a ...))]))
> >
> > (define-syntax (helper3 stx)
> >   (syntax-case stx ()
> >     [(_ a ...)
> >      (syntax
> >        (ex a ... z))]))
> >
> > (module+ main
> >   (ex x)
> >   ex
> >   (helper1 a b c)
> >   (helper1 . (a b c))
> >   (helper2 a b c)
> >   (helper3 a b c))
> >
> > You can see how each of the different "helpers" appear to do things
> > that are very close to one another but turn out to have different
> > syntax representations.
> >
> > You normally don't need to worry about this, but sometimes you need to
> > pay attention to it when you want to get correct source code
> > origination information. (Basically, if you have a list in the pattern
> > input and do something like creating a new syntax object like #'(a
> > ...) in a macro and then put that in, then you'll get source code
> > location information from the macro and not from the source of the
> > list, which sometimes gets exposed.)
> >
> > Jay
> >
> >
> > On Mon, May 27, 2013 at 1:06 PM, Tim Nelson <tbnelson at gmail.com> wrote:
> >> Dear All,
> >>
> >> The documentation for syntax-e says that it may return many different
> >> types, including a syntax-pair, but it doesn't really describe why.
> Here's
> >> a concrete piece of code:
> >>
> >> (define stx
> >>   (with-syntax ([x #'5]
> >>                 [(y ...) #'(1 2)])
> >>     (syntax (x y ...))))
> >>
> >> (syntax->datum stx) returns a list.
> >>
> >> (syntax->list stx) (of course) returns a list, too.
> >>
> >> But (syntax-e stx) returns a pair! If I do this:
> >>
> >> (define stx2
> >>   (with-syntax ([x #'5]
> >>                 [y #'(1 2)])
> >>     (syntax (x y))))
> >>
> >> I get a list from syntax-e, but of course the nesting is
> wrong/different.
> >>
> >> So my question is: why a pair?
> >>
> >> Best,
> >> - Tim (Running Racket v. 5.3.3)
> >> ____________________
> >>   Racket Users list:
> >>   http://lists.racket-lang.org/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
> ____________________
>   Racket Users list:
>   http://lists.racket-lang.org/users
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.racket-lang.org/users/archive/attachments/20130527/73ac95f4/attachment.html>

Posted on the users mailing list.