public inbox for
 help / color / mirror / Atom feed
From: Per Bothner <>
To: "J. Vincent Toups" <>,
Subject: Re: Extensible Macros and define-library
Date: Tue, 11 May 2021 14:41:05 -0700	[thread overview]
Message-ID: <> (raw)
In-Reply-To: <>

Sorry for the late response - I missed your message.

On 4/17/21 3:32 AM, J. Vincent Toups via Kawa wrote:
> I'm seeking ideas on how to implement an extensible pattern matcher.

If you want to define your own pattern-matching library, you can of course do what.
Likewise, if you're trying to implement a library following some other existing API.
However, if you want pattern-matching to be part of Kawa itself and available
as part of the Kawa "standard library" then note that Kawa has its own pattern
"language" and "vision".  If you're not already familar with it, check out
Understand that the (let ...) family and lambda formal parameters have been extended to
take patterns:
Note the (! patterm value) syntax is an extension of plain define.
Note how if-expressions and cond have been extended to be pattern-aware,
using the (? pattern value) form: .

Now the Kawa pattern "syntax" is rather limited.  My goal was to integrate
patterns into the language, so I stuck to pattern forms that wouldn't constrain
extending the pattern language.  Unfortunately, syntax-rules is based on
pattern-match using an elegant but limited syntax - which is incompatible
with most other designs: In syntax-rules the pattern (list x y z) matches
a *four*-element list, binding the name 'list to the first element of the list.
I.e. the first element of a list in a syntax-rules pattern is not a
pattern operator, but instead matches just like the remaining elements.

Now if we *do* want to allow "pattern operators", breaking with syntax-rules compatibility
(and I think we do) then it would be desirable to allow for some kind of extensibility,
as you're talking about.

If you want to extend or hook into the Kawa pattern framework, the place where
lists are handled is starting near line 143 in kawa/lang/
Specifically, search for the error message "unrecognized pattern operator "
and erplace that to search for a defined pattern operator.
Unfortunately, the code is rather convoluted.

The hard part is defining a Scheme API that can hook into the BindDecls class.
Specifically, what kind of transformer is it: What are the inputs (patterns,
presumably) and what are the outputs (declarations/bindings, initialized from
the match form) in a clean way that can be mapped to Kawa internals.

How to deal with namespaces and hygiene can probably best be done with
compound names:
For example when the compiler sees a pattern (OP arg1 arg2 args3)
it could look for a transfomer with the name $PATTERN-BIND$:OP
- i.e. the symbol 'OP in the '$PATTERN-BIND$ namespace.
	--Per Bothner

      reply	other threads:[~2021-05-11 21:41 UTC|newest]

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

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 \ \ \ \ \

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