Re: [racket-users] Odd behavior of syntax-case

2016-01-28 Thread Matthew Flatt
The pattern matcher in `syntax-case` is less powerful than the one in
`syntax-parse`. The pattern

  (_ trash-left ... save-the-world . trash-right)

can match `save-the-world` only against the last item in a list --- or
against the first of the last pair for a non-list.


With `syntax-parse`, as you've probably noticed, you can write

  (_ trash-left ... save-the-world trash-right ...)

and it will find a match in all of your examples. With `syntax-case`,
that pattern is not even allowed.


At Wed, 27 Jan 2016 23:11:25 -0800 (PST), reilithion wrote:
> I was trying to write a macro today (I'm not too good at writing macros yet) 
> and I ran up against a wall when I realized that syntax-case wasn't behaving 
> the way I expected. I boiled down the behavior to the following test case:
> 
> #lang racket/base
> 
> (require (for-syntax racket/base))
> 
> ;; Intended behavior:
> ;; Break the world unless save-the-world is
> ;; ANYWHERE in the list of arguments
> (define-syntax (break-the-world stx)
>   (syntax-case stx (save-the-world)
> [(_ trash-left ... save-the-world . trash-right)
>  #'(begin (display 'hooray) (newline))]
> [_ #'(error "The world is broken!")]))
> 
> (break-the-world save-the-world)
> (break-the-world 'trash1 save-the-world)
> (break-the-world save-the-world 'trash2)
> (break-the-world 'trash1 save-the-world 'trash2)
> 
> The first two expansions of break-the-world result in "hooray". The last two, 
> on the other hand, break the world. I gather that 'trash2 is not being 
> matched 
> by the ". trash-right" part of my pattern. What I don't understand is why, 
> especially when this kind of thing seems to work if I use syntax-parse 
> instead.
> 
> Is there a more idiomatic way of matching a pattern anywhere in a list?
> 
> Thanks!
> Lucas Paul
> 
> -- 
> You received this message because you are subscribed to the Google Groups 
> "Racket Users" group.
> To unsubscribe from this group and stop receiving emails from it, send an 
> email to racket-users+unsubscr...@googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.

-- 
You received this message because you are subscribed to the Google Groups 
"Racket Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to racket-users+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


Re: [racket-users] Odd behavior of syntax-case

2016-01-28 Thread reilithion
Thanks for the explanation! Does this mean that syntax-case simply doesn't have 
a way to write a pattern that matches save-the-world anywhere in the list? I'm 
also curious why these two constructs have such different pattern matchers. Is 
it just that syntax-parse is newer and will one day supplant syntax-case?

On Thursday, January 28, 2016 at 6:06:31 AM UTC-7, Matthew Flatt wrote:
> The pattern matcher in `syntax-case` is less powerful than the one in
> `syntax-parse`. The pattern
> 
>   (_ trash-left ... save-the-world . trash-right)
> 
> can match `save-the-world` only against the last item in a list --- or
> against the first of the last pair for a non-list.
> 
> 
> With `syntax-parse`, as you've probably noticed, you can write
> 
>   (_ trash-left ... save-the-world trash-right ...)
> 
> and it will find a match in all of your examples. With `syntax-case`,
> that pattern is not even allowed.
> 
> 
> At Wed, 27 Jan 2016 23:11:25 -0800 (PST), reilithion wrote:
> > I was trying to write a macro today (I'm not too good at writing macros 
> > yet) 
> > and I ran up against a wall when I realized that syntax-case wasn't 
> > behaving 
> > the way I expected. I boiled down the behavior to the following test case:
> > 
> > #lang racket/base
> > 
> > (require (for-syntax racket/base))
> > 
> > ;; Intended behavior:
> > ;; Break the world unless save-the-world is
> > ;; ANYWHERE in the list of arguments
> > (define-syntax (break-the-world stx)
> >   (syntax-case stx (save-the-world)
> > [(_ trash-left ... save-the-world . trash-right)
> >  #'(begin (display 'hooray) (newline))]
> > [_ #'(error "The world is broken!")]))
> > 
> > (break-the-world save-the-world)
> > (break-the-world 'trash1 save-the-world)
> > (break-the-world save-the-world 'trash2)
> > (break-the-world 'trash1 save-the-world 'trash2)
> > 
> > The first two expansions of break-the-world result in "hooray". The last 
> > two, 
> > on the other hand, break the world. I gather that 'trash2 is not being 
> > matched 
> > by the ". trash-right" part of my pattern. What I don't understand is why, 
> > especially when this kind of thing seems to work if I use syntax-parse 
> > instead.
> > 
> > Is there a more idiomatic way of matching a pattern anywhere in a list?
> > 
> > Thanks!
> > Lucas Paul

-- 
You received this message because you are subscribed to the Google Groups 
"Racket Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to racket-users+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


Re: [racket-users] Odd behavior of syntax-case

2016-01-28 Thread Matthew Flatt
That's right: `syntax-case` doesn't have a way to write that as a
pattern.

And yes: `syntax-parse` is newer and will one day supplant `syntax-case`
(more completely than it has already).

At Thu, 28 Jan 2016 06:03:46 -0800 (PST), reilithion wrote:
> Thanks for the explanation! Does this mean that syntax-case simply doesn't 
> have a way to write a pattern that matches save-the-world anywhere in the 
> list? I'm also curious why these two constructs have such different pattern 
> matchers. Is it just that syntax-parse is newer and will one day supplant 
> syntax-case?
> 
> On Thursday, January 28, 2016 at 6:06:31 AM UTC-7, Matthew Flatt wrote:
> > The pattern matcher in `syntax-case` is less powerful than the one in
> > `syntax-parse`. The pattern
> > 
> >   (_ trash-left ... save-the-world . trash-right)
> > 
> > can match `save-the-world` only against the last item in a list --- or
> > against the first of the last pair for a non-list.
> > 
> > 
> > With `syntax-parse`, as you've probably noticed, you can write
> > 
> >   (_ trash-left ... save-the-world trash-right ...)
> > 
> > and it will find a match in all of your examples. With `syntax-case`,
> > that pattern is not even allowed.
> > 
> > 
> > At Wed, 27 Jan 2016 23:11:25 -0800 (PST), reilithion wrote:
> > > I was trying to write a macro today (I'm not too good at writing macros 
> yet) 
> > > and I ran up against a wall when I realized that syntax-case wasn't 
> behaving 
> > > the way I expected. I boiled down the behavior to the following test case:
> > > 
> > > #lang racket/base
> > > 
> > > (require (for-syntax racket/base))
> > > 
> > > ;; Intended behavior:
> > > ;; Break the world unless save-the-world is
> > > ;; ANYWHERE in the list of arguments
> > > (define-syntax (break-the-world stx)
> > >   (syntax-case stx (save-the-world)
> > > [(_ trash-left ... save-the-world . trash-right)
> > >  #'(begin (display 'hooray) (newline))]
> > > [_ #'(error "The world is broken!")]))
> > > 
> > > (break-the-world save-the-world)
> > > (break-the-world 'trash1 save-the-world)
> > > (break-the-world save-the-world 'trash2)
> > > (break-the-world 'trash1 save-the-world 'trash2)
> > > 
> > > The first two expansions of break-the-world result in "hooray". The last 
> two, 
> > > on the other hand, break the world. I gather that 'trash2 is not being 
> matched 
> > > by the ". trash-right" part of my pattern. What I don't understand is 
> > > why, 
> > > especially when this kind of thing seems to work if I use syntax-parse 
> instead.
> > > 
> > > Is there a more idiomatic way of matching a pattern anywhere in a list?
> > > 
> > > Thanks!
> > > Lucas Paul
> 
> -- 
> You received this message because you are subscribed to the Google Groups 
> "Racket Users" group.
> To unsubscribe from this group and stop receiving emails from it, send an 
> email to racket-users+unsubscr...@googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.

-- 
You received this message because you are subscribed to the Google Groups 
"Racket Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to racket-users+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


[racket-users] Odd behavior of syntax-case

2016-01-27 Thread reilithion
I was trying to write a macro today (I'm not too good at writing macros yet) 
and I ran up against a wall when I realized that syntax-case wasn't behaving 
the way I expected. I boiled down the behavior to the following test case:

#lang racket/base

(require (for-syntax racket/base))

;; Intended behavior:
;; Break the world unless save-the-world is
;; ANYWHERE in the list of arguments
(define-syntax (break-the-world stx)
  (syntax-case stx (save-the-world)
[(_ trash-left ... save-the-world . trash-right)
 #'(begin (display 'hooray) (newline))]
[_ #'(error "The world is broken!")]))

(break-the-world save-the-world)
(break-the-world 'trash1 save-the-world)
(break-the-world save-the-world 'trash2)
(break-the-world 'trash1 save-the-world 'trash2)

The first two expansions of break-the-world result in "hooray". The last two, 
on the other hand, break the world. I gather that 'trash2 is not being matched 
by the ". trash-right" part of my pattern. What I don't understand is why, 
especially when this kind of thing seems to work if I use syntax-parse instead.

Is there a more idiomatic way of matching a pattern anywhere in a list?

Thanks!
Lucas Paul

-- 
You received this message because you are subscribed to the Google Groups 
"Racket Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to racket-users+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.