Tree Patterns Felix Thibault 16 Oct 2020 00:56 UTC

On Wed, Oct 14, 2020 at 1:32 AM Marc Nieper-Wißkirchen
<xxxxxx@nieper-wisskirchen.de> wrote:
>As I commented earlier on this list, I think it makes more sense
>(unless one has several independent implementations to test) to
>document the intended behavior first and then to see whether the
>existing implementation(s) behave accordingly.

Hmm, the tree behavior is clear enough on lists, like the first JSON
expression (except GlossSeeAlso)
from https://json.org/example.html - loading that in with (chibi json)
and matching

(match example-1 (((keys *** `(SortAs . ,rest))) (list keys rest)))
=> ((glossary GlossDiv GlossList GlossEntry) "SGML")

(and since (chibi json) uses dotted pair alists, we see how it works with them)
and we can use:

(fold (lambda (key alist) (cdr (assv key alist))) example-1 keys)

to get back the list that has '(SortAs . "SGML") in it.

but what are we expecting with vectors and other data types? ((keys
*** `(SortAs . ,rest))
is in the form of a list. If we took an arithmetic sexpr and converted
it to vector form with

(define (list-tree->vector-tree tree)
  (if (list? tree)
      (list->vector (map list-tree->vector-tree tree))
      tree))

we could write
(match (list-tree->vector-tree '(+ (* (+ 7 2) (- 7 5))) 4))
 and say that since (k *** 7) matches the list tree, #(k *** 7) should
match the vector (it doesn't) but if are talking about sexpr with vectors
and records in it, that fall inside the *** it's less clear. It seems like if
(k *** 7) would a tree with an arbitrary number of vectors inside it
in particular, it would match the all-vector tree #(k *** 7) matches.

If we were to try to match records an additional complication would be that
all of the macros for record matching we have assume the rtd is known, so
it would require new record introspection macros.

These are my first impressions, anyway. I will look at/ think about
this some more
tomorrow.

As for cycles, (match '#0=(a . #0#) ((a *** 'b) a)) never returns.