* Re: Extensible Macros and define-library
2021-04-17 10:32 Extensible Macros and define-library J. Vincent Toups
@ 2021-05-11 21:41 ` Per Bothner
0 siblings, 0 replies; 2+ messages in thread
From: Per Bothner @ 2021-05-11 21:41 UTC (permalink / raw)
To: J. Vincent Toups, kawa
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
http://www.gnu.org/software/kawa/Variables-and-Patterns.html
Understand that the (let ...) family and lambda formal parameters have been extended to
take patterns: http://www.gnu.org/software/kawa/Extended-formals.html
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: www.gnu.org/software/kawa/Conditionals.html .
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/BindDecls.java.
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: www.gnu.org/software/kawa/Symbols-and-namespaces.html
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
per@bothner.com http://per.bothner.com/
^ permalink raw reply [flat|nested] 2+ messages in thread