[plt-scheme] how to write match expander?

From: Matthew Flatt (mflatt at cs.utah.edu)
Date: Thu May 4 01:40:25 EDT 2006

The `match' macro implements its own macro expander to handle bindings
introduced by `define-match-expander'.

That's fine in itself, but the custom macro expander doesn't do all the
things that the expression macro expander does: use marks to track
introduced names, propagate properties, and attach certificates.

I suspect that the custom macro expander should track introductions
with marks, but maybe it doesn't matter. Properties probably don't
matter. Certificates do matter, as your example shows.

MzScheme provides the machinery for doing all three in a custom
expander. I'll adjust the implementation of match-expander to support
certificates, and then I'll report back here to explain how it works.


At Wed, 3 May 2006 23:23:12 -0400, Richard Cobbe wrote:
> 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.