[racket] define-match-expander and the second proc-expr for when it's not a match pattern

From: Alexander D. Knauth (alexander at knauth.org)
Date: Sun Jul 20 17:32:34 EDT 2014

Thanks I didn’t know about prop:match-expander.  
Now the rename transformer seems to work but it’s not working as a match-expander:

(define-match-expander+rename-transformer my-list
  #:match-expander
  (lambda (stx)
    (syntax-parse stx
      [(my-list expr:expr ...) ; doesn't work
       #'(list expr ...)]))
  #:rename-transformer #'list ; works
  )

(match '(1 2 3)
  [(my-list one two three) ; match: syntax error in pattern
   (list one two three)])

(my-list 1 2 3) ; works

Am I doing something wrong, or are match-expanders just not allowed to also be rename-transformers?

#lang racket

(require (for-syntax syntax/parse))

(begin-for-syntax
  (struct match-expander+rename-transformer (match-expander rename-transformer) #:transparent
    #:property prop:match-expander (struct-field-index match-expander)
    #:property prop:rename-transformer (struct-field-index rename-transformer)
    ))

(define-syntax define-match-expander+rename-transformer
  (lambda (stx)
    (syntax-parse stx
      [(def id:id
         (~seq #:match-expander match-expander)
         (~seq #:rename-transformer rename-transformer))
       #'(define-syntax id
           (match-expander+rename-transformer
            match-expander
            rename-transformer))])))

(define-match-expander+rename-transformer my-list
  #:match-expander
  (lambda (stx)
    (syntax-parse stx
      [(my-list expr:expr ...) ; doesn't work
       #'(list expr ...)]))
  #:rename-transformer #'list ; works
  )

(match '(1 2 3)
  [(my-list one two three) ; match: syntax error in pattern
   (list one two three)])

(my-list 1 2 3) ; works


On Jul 20, 2014, at 5:05 PM, J. Ian Johnson <ianj at ccs.neu.edu> wrote:

> You want to use the prop:match-expander and prop:procedure properties for a struct you bind for syntax. That way the match-expander has its piece and the procedure provides the syntax transformer.
> -Ian
> ----- Original Message -----
> From: Alexander D. Knauth <alexander at knauth.org>
> To: racket users list <users at racket-lang.org>
> Sent: Sun, 20 Jul 2014 13:10:01 -0400 (EDT)
> Subject: [racket] define-match-expander and the second proc-expr for when it's not a match pattern
> 
> Is there a way to supply a rename transformer instead of a procedure for it to use when it’s not used as a match pattern?
> 
> If I do something like this:
> 
> #lang racket
> 
> (require (for-syntax syntax/parse))
> 
> (define-match-expander my-list
>  (lambda (stx) ; to use when it’s a match pattern, works
>    (syntax-parse stx
>      [(my-list stuff:expr ...)
>       #'(list stuff ...)]))
>  (make-rename-transformer #'list)) ; to use when it’s not a match pattern, doesn’t work
> 
> (match '(1 2 3)
>  [(my-list one two three) ; works
>   (list one two three)])
> 
> (my-list 1 2 3) ; doesn’t work
> 
> It gives me this error:
> . . ../../Applications/Racket v6.0.1.8/collects/syntax/wrap-modbeg.rkt:46:4: application: not a procedure;
> expected a procedure that can be applied to arguments
>  given: #<rename-transformer>
>  arguments...:
>   #<syntax:16:0 (my-list 1 2 3)>
> 
> Is there a way to supply a rename transformer instead of a procedure for it to use when it’s not used as a match pattern?
> 
> 
> ____________________
>  Racket Users list:
>  http://lists.racket-lang.org/users
> 



Posted on the users mailing list.