On Sun, Jul 26, 2026 at 09:40:23PM +0100, Peter Broadbery wrote:
> Hi Waldek,
>
> As Ralf says, I am also interested in getting a 'nice' pattern
> decomposition operator.. Is =? intended to be used as
> firstPlusLastOf3(somelist: List X): .. ==
> someList =? [a, ., b] => a + b
> 0
Modulo wrongly indented 0 above this is good example.
> It's a reasonable syntax. Possibly, the harder part is how the '[a, .,
> b]' works.. What are the restrictions there?
> Can something like 'x =? [., [b, ., x], c] work?
Yes.
> What types can be
> used on the right of the operator? Is [2, a, .] a good pattern?
Yes.
> Of course, the above questions might be over-analysing a good idea.
> Instead making it work for lists and then maybe unions, then
> generalising
> is completely valid.
Well, the questions are good. The idea is the on left hand side
we have variable having a value, hence also having a type. What
can be on to right hand side and accepted types on the left hand
side are to be determined later. First, lists and records.
For them we have constructor syntax using list notation.
Pattern can use the same constructor syntax. Each position
is eiter a value (to be matched using equality), subpattern
(to be matched with corresponding element) or a variable.
In principle such syntax is capable of matching anything
which use can be constructed using list style syntax. Of
course, the trouble is how to provide matching function.
In FriCAS records are built-in into compiler, so natural
solution is to have built-in matcher. Also unions are
built-in and matching can be done based on types (for
old style unions) or tags. Built-in matcher is less
natural for lists, but lists are special enough that it
is IMO reasonable to have built-in matcher for lists.
ATM I do not know how to handle more general types on
left hand side. One potential possiblity is to allow
domains to export RecordCategory or UnionCategory.
Then corresponding domain for the purpose of matching
would behave exactly as record or union.
--
Waldek Hebisch