[plt-scheme] how to write match expander?

From: Richard Cobbe (cobbe at ccs.neu.edu)
Date: Wed May 3 23:23:12 EDT 2006

How do you write match expanders?

First, the docs in ch 25 of the mzlib manual aren't entirely clear.  If
I say (define-match-expander x y), then it appears that y has to be a
function from syntax (representing the pattern that the user writes) to
syntax (representing the pattern that plt-match.ss can understand
natively).  Is that right?

If that is correct, then I'm missing something else.

In DrScheme 301.13-svn28apr2006:

(module types mzscheme

  (require (lib "plt-match.ss"))

  (define-struct field-struct (type name))
  ;; I don't want to export the struct, but I want to export a match
  ;; expander that lets people pattern-match on field-struct values as
  ;; though I had exported the struct.

  (define-match-expander field
    (lambda (orig-pattern)
      (syntax-case orig-pattern ()
        [(_ type-pattern name-pattern)
         #'(struct field-struct (type-pattern name-pattern))])))

  (provide make-field-struct field))

> (require types)
> (require (lib "plt-match.ss"))
> (match (make-field-struct 'int 'x)
    [(field type name) (list type name)])
compile: access from an uncertified context to unexported syntax from module: types in: field-struct

and it highlights the occurrence of field-struct within the definition
of the field match expander.

How do I do this?

Richard


Posted on the users mailing list.