public inbox for
 help / color / mirror / Atom feed
From: "J. Vincent Toups" <>
Subject: Extensible Macros and define-library
Date: Sat, 17 Apr 2021 06:32:58 -0400	[thread overview]
Message-ID: <> (raw)

I'm seeking ideas on how to implement an extensible pattern matcher.

You can see the code as it currently is here:

Usage look like this

(define (example arg)
 (match arg
  ((string? x) x)
  ((number? x) x)
  ((list a b c) (list c b a))
  (anything-else (error "Fail"))))

(example 10) -> 10
(example '(1 2 3)) -> (3 2 1)

When matching against a pattern which is a list, the matcher uses the
head of the list to determine what behavior to implement. The list
head is interpreted as being in its own namespace (the pattern
namespace, if you'd like). If you examine the source code you can see
that we dispatch presently on the symbolic value of the head rather
than its syntax. The "pattern namespace" is totally static and flat.

I'm trying to figure out how to let users extend this pattern matcher.
A user defined pattern is naturally thought of as a special kind of
syntax-transformer. It takes some syntax in and returns a new pattern
using primitive patterns as an implementation language.


(define-pattern list-of-two
 (lambda (expr)
  (syntax-case expr ()
   ((_ a b) #'(list a b)))))

How can I set this system up so that

1. patterns can be exported/imported/etc using define-library
2. they are neither regular functions nor syntax-transformers
    a. they don't have run time values
    b. they also aren't interpreted as regular syntax-transformers?

I'm not sure such a thing can be defined in pure R7RS/R6RS scheme?

I _think_ the essence of this question is whether there is any clever
way to set up a separate namespace that I can interact with at
syntax-transformation time. I think I could hack something together if
there was a way to do a macro-expand-once but in that case I think I'd
still have to define patterns as regular syntax-transformations. And
it doesn't seem like there is such a thing as "expand-once" in
R(6,7)RS or in Kawa.

A cute use of such a thing would be to implement something like
scala's "unapply" idiom where a specific static method of a class
describes how to perform a pattern match for instances.

Any advice would be appreciated!

             reply	other threads:[~2021-04-17 10:33 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-04-17 10:32 J. Vincent Toups [this message]
2021-05-11 21:41 ` Per Bothner

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to='' \ \ \

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).