Potential new operator

11 views
Skip to first unread message

Waldek Hebisch

unread,
Jul 26, 2026, 2:10:29 PM (10 days ago) Jul 26
to fricas...@googlegroups.com
I am thinking about adding a new operator, that is '=?' to Spad.
The intent is to have available operator for use as a pattern
match operator. Currently Boot uses 'is' for pattern matching.
To replace Boot code by Spad it would be handy to have similar
operator ate Spad level. But in Spad 'is' is used for types
and it would be awkward to overload it. So we need a new
operator and '=?' seems like reasonable choice.

Opinions?

--
Waldek Hebisch

Ralf Hemmecke

unread,
Jul 26, 2026, 2:59:26 PM (10 days ago) Jul 26
to fricas...@googlegroups.com, Peter Broadbery
I don't know where exactly you need =?, but wouldn't a function like

match?(foo, pattern)

do the same thing? And match? should be implemented in a domain/package
and pattern should be an element of a domain of patterns.

But maybe you want something else.

Furthermore, Peter Broadbery recently had some ideas of implementing a
pattern matching mechanism in Aldor. Maybe that ideas could be taken for
FriCAS?

As far as I understand, he uses

foo case pattern

https://groups.google.com/g/fricas-devel/c/HoJ8uqrh33s/m/OT_iUk_yAAAJ
https://groups.google.com/g/fricas-devel/c/HoJ8uqrh33s/m/8vCYTj0bDwAJ
https://github.com/aldorlang/aldor/issues/175

Ralf

Waldek Hebisch

unread,
Jul 26, 2026, 3:32:56 PM (10 days ago) Jul 26
to 'Ralf Hemmecke' via FriCAS - computer algebra system
On Sun, Jul 26, 2026 at 08:59:21PM +0200, 'Ralf Hemmecke' via FriCAS - computer algebra system wrote:
> I don't know where exactly you need =?, but wouldn't a function like
>
> match?(foo, pattern)
>
> do the same thing? And match? should be implemented in a domain/package and
> pattern should be an element of a domain of patterns.

In Boot

i is [a, ., b]

not only matches list of 3 elements, but also assign value to a
and b. This can not be done by library function. In other
words new operator must be build into compiler.

> But maybe you want something else.
>
> Furthermore, Peter Broadbery recently had some ideas of implementing a
> pattern matching mechanism in Aldor. Maybe that ideas could be taken for
> FriCAS?
>
> As far as I understand, he uses
>
> foo case pattern
>
> https://groups.google.com/g/fricas-devel/c/HoJ8uqrh33s/m/OT_iUk_yAAAJ
> https://groups.google.com/g/fricas-devel/c/HoJ8uqrh33s/m/8vCYTj0bDwAJ
> https://github.com/aldorlang/aldor/issues/175

As I wrote in that thread, using 'case' is complicated for the
compiler (and possibly for users too as this is rather strange
overloading), that is why I look for new operator.

In principle we could use some longish keyword to avoid conflict
with existing things, but important property of pattern matching
is that it provide compact notation. Using long keryword would
work agaist this. And many short things are already used, so
'=?' look reasonable to me: it is among shortest currently
unised operators, and it is somewhat natural.

--
Waldek Hebisch

Peter Broadbery

unread,
Jul 26, 2026, 4:40:55 PM (10 days ago) Jul 26
to fricas...@googlegroups.com
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

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? What types can be
used on the right of the operator? Is [2, a, .] a good pattern?

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.

Peter
> --
> You received this message because you are subscribed to the Google Groups "FriCAS - computer algebra system" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to fricas-devel...@googlegroups.com.
> To view this discussion visit https://groups.google.com/d/msgid/fricas-devel/amZg5FLhYaZ3yAY7%40fricas.org.

Waldek Hebisch

unread,
Jul 26, 2026, 6:15:28 PM (10 days ago) Jul 26
to fricas...@googlegroups.com
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
Reply all
Reply to author
Forward
0 new messages