On Tue, Mar 24, 2026 at 12:36:56AM +0100, Waldek Hebisch wrote:
> On Mon, Mar 23, 2026 at 10:32:56AM -0700, 'Martin R' via FriCAS - computer algebra system wrote:
> > Hi there!
> >
> > I am trying to find out what Pattern and friends do (in the end, I would
> > like to do pattern matching on InputForm). I found very few examples, one
> > of which is the following.
Maybe I will expand a bit on what I wrote in the previous message.
When one wants to do Rubi-style pattern maching integrator one
of simplest patterns is for product of two linear polynomials:
(a*x + b)*(c*x + d)
Normally FriCAS immediately expands such things, so one really has
a*c*x^2 + b*x + c*x + b*d
Such expanded pattern is unlikely to match expanded user input.
That is, when you look for a product, then expanded expressions
are unlikely to match. However, one can try to factor first.
In other systems naive factoring may work OK. In FriCAS
mere coercion from Factored to orignal domain expands, so it
does not work. If you insist on pattern matching, there is a
workaround, write pattern as
paren(a*x + b)*paren(c*x + d)
You can produce similar thing from FactorList, so matching
should works as in other systems. But once you have
FactorList it is easy to check that factors are linear and
extract coefficients from them. In the process one can
also apply various side conditions, so using pattern gains
only a little (if any).
Given explicit tree form, like InputForm one normally
ignores issues related to expressions. In Boot we have
pattern matching on Lisp lists (which are really rather
general tree forms). One can write:
x is ["where", ["DEF", :.], :.]
to check for specific symbols in given places. One can extract
subtrees using variables in the pattern:
x is [op1, ["DEF", sig, :.], :.]
assigns values to op1 and sig. One can test for presence of
prescribed values contained in variables like:
x is [op1, ["DEF", =sig, :.], :.]
Coding corresponding pattern matcher is easy. Main value is
integration with language syntax. In Spad we have trouble,
namely various candidate syntaxes are already "taken" by
other constructs. In particular 'is' has quite a different
meaning, '==' and '~=' which are next 2 natural choices also
have incompatible meaning. There is another problem, namely
FriCAS objects are typed and both sides of match expression
need to have appriate type. Boot style patterns could work
on SExpression, but building SExpression-s in FriCAS requires
a lot of 'convert'-s, so patterns would look ugly. And
they would not work on tree-like things which are not
SExpression-s. InputForm and OutputForm probably could be
handled by a bunch of trivial coercions (trivial in the
sense that they are implmentd by 'pretend'). But we can
have varied tree-like structures and it would be unnatural
to support only things represented by Lisp lists. Typed
landuages like ML have their form of pattern matching
which can match records and unions and it would be nice
to support something similar. So, it is not so easy to
specify general design for pattern matching. In particular,
it looks that general machinery needs to be part of
the language.
For specific domain like InputForm one can easily implement
special matcher. But then the trouble is that FriCAS
language does not help with syntax and types. Pattern
matching for Expression-s tries to work around the problem
by converting expressions to pattern. For InputForm that
is more trucky: a symbol in InputForm may be just a literal
symbol. But one also needs special things, that is variables
to match subparts (here something like "?(x)" could indicate
that 'x' is a match variable). So there is some work to
design useful notation.
BTW: In some sense patten matching in Boot is trivial. But
it is quite convenient and blends nicely into the language.
--
Waldek Hebisch