[plt-scheme] Re: Macros and Matching

From: Paulo J. Matos (pocm at soton.ac.uk)
Date: Fri Jul 13 07:41:24 EDT 2007

On 13/07/07, Paulo J. Matos <pocm at soton.ac.uk> wrote:
> Just found why doesn't work. From R5RS:
> "The keyword at the beginning of the pattern in a <syntax rule> is not
> involved in the matching and is not considered a pattern variable or
> literal identifier."
>
> Any ideas on the best way to solve this?
>

The idea that followed my last email was to use the fender field of
syntax case so I did this:
(module test mzscheme

  (define-syntax (machine stx)
    (syntax-case stx ()
      [(_ mname sexps ...)
       (identifier? #'mname) ; fender
       (let* ([sections (syntax->list #'(sexps ...))]
              [parts (map (λ (stx-expr)
                            (syntax-case stx-expr (variables properties)
                              ((_ (name type value) ...)
                               (and (identifier? (car
(syntax-object->datum stx-expr)))
                                    (eqv? 'variables (car
(syntax-object->datum stx-expr))))
                               (syntax (begin (printf "Var ~a : ~a ->
~a~n" 'name 'type 'value) ...)))
                              ((_ formula ...)
                               (and (identifier? (car
(syntax-object->datum stx-expr)))
                                    (eqv? 'properties (car
(syntax-object->datum stx-expr))))
                               (syntax (begin (printf "Prop ~a~n"
'formula) ...)))))
                          sections)])
         #`(begin
             #, at parts))]))

  (machine test
   (variables
    (a integer 0)
    (b boolean false))
   (properties
    (=> a b))))

But now I get:
variables: bad syntax in: (variables (a integer 0) (b boolean false))

Any ideas?

> Cheers,
>
> Paulo Matos
>
> On 13/07/07, Paulo J. Matos <pocm at soton.ac.uk> wrote:
> > Hi all,
> >
> > I have a macro which defines a specification language on top of scheme
> > and sometimes it is behaving in unexpected ways (to me). Here's a
> > sample:
> >
> > (module test mzscheme
> >
> >   (define-syntax (machine stx)
> >     (syntax-case stx ()
> >       [(_ mname sexps ...)
> >        (identifier? #'mname) ; fender
> >        (let* ([sections (syntax->list #'(sexps ...))]
> >               [parts (map (syntax-rules (variables properties)
> >                             ((variables (name type value) ...)
> >                              (begin (printf "Var ~a : ~a -> ~a~n"
> > 'name 'type 'value) ...))
> >                             ((properties formula ...)
> >                              (begin (printf "Prop ~a~n" 'formula) ...)))
> >                     sections)])
> >          #`(begin
> >              #, at parts))]))
> >
> >   (machine test
> >    (variables
> >     (a integer 0)
> >     (b boolean false))
> >    (properties
> >     (=> a b))))
> >
> > The output is:
> > Var a : integer -> 0
> > Var b : boolean -> false
> > Var => : a -> b
> >
> > Obviously the last line is wrong. Why is (=> a b) being matched to a
> > variable? I understand that it is matching => to name, a to type and b
> > to value but it is inside the properties section so should definitely
> > not be matched to a variable. What's the problem with this?
> >
> > Cheers,
> > --
> > Paulo Jorge Matos - pocm at soton.ac.uk
> > http://www.personal.soton.ac.uk/pocm
> > PhD Student @ ECS
> > University of Southampton, UK
> >
>
>
> --
> Paulo Jorge Matos - pocm at soton.ac.uk
> http://www.personal.soton.ac.uk/pocm
> PhD Student @ ECS
> University of Southampton, UK
>


-- 
Paulo Jorge Matos - pocm at soton.ac.uk
http://www.personal.soton.ac.uk/pocm
PhD Student @ ECS
University of Southampton, UK

Posted on the users mailing list.