ashinn / chibi-scheme

Official chibi-scheme repository
Other
1.22k stars 141 forks source link

`(chibi match)`: purpose of `***` patterns #941

Closed dpk closed 4 months ago

dpk commented 12 months ago

What’s the idea of *** tree patterns in (chibi match)? (Apologies for this slight abuse of the Chibi issue tracker to ask this question.)

As far as I can tell, the left-hand side of the *** operator is of limited usefulness because it can’t disambiguate between what one might call alternative paths to a matching value: matching both (x *** 'c) and (x *** 'd) against (a (b c) (b d)) will bind x to (a b). Also, it never matches the cars of any lists: asked to find d in (a (b c) (d c)) it will fail.

ashinn commented 4 months ago

A large part of the motivation is SXPATH-style usage - finding nested patterns in SXML - and I actually use it for this. For example, from the test suite:

(test "sxml tree search"
          '(((href . "http://synthcode.com/")) ("synthcode"))
        (match '(p (ul (li a (b c) (a (@ (href . "http://synthcode.com/"))
                                      "synthcode") d e f)))
          (((or 'p 'ul 'li 'b) *** ('a ('@ attrs ...) text ...))
           (list attrs text))
          (else #f)))

You would often want _ for the left-hand pattern, but in cases like this can use it to restrict to nesting within certain know tags.

It doesn't match the cars with the right-hand pattern because they are matched by the left-hand pattern, and are accumulated in that binding list. But this is documented as experimental. If you have suggestions for improvement we could consider that. For example, the pattern (*** y) could allow any path and also match y against the cars.

dpk commented 4 months ago

Thanks!