public inbox for kawa@sourceware.org
 help / color / mirror / Atom feed
* Extensible Macros and define-library
@ 2021-04-17 10:32 J. Vincent Toups
  2021-05-11 21:41 ` Per Bothner
  0 siblings, 1 reply; 2+ messages in thread
From: J. Vincent Toups @ 2021-04-17 10:32 UTC (permalink / raw)
  To: kawa

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

You can see the code as it currently is here:

https://github.com/VincentToups/kawa-lib/blob/main/lib/shadchen.scm

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.

eg:

(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!

^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2021-05-11 21:41 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-04-17 10:32 Extensible Macros and define-library J. Vincent Toups
2021-05-11 21:41 ` Per Bothner

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).