MathGroup Archive 2007

[Date Index] [Thread Index] [Author Index]

Search the Archive

Re: Matching two patterns?

On 10 Mar 2007, at 12:52, Neri wrote:

> Can two patterns be matched with each other, to find if they
> intersect?
> What I mean is that  { _, 1} matches {1, _ } in the sense that there
> are lists that fit both, in this case {1,1}.
> However, { _, 1} doesn't match { _, 2} in the sense that no list can
> fit both.
> For lists it seems easy to write a function that does that, but I
> wondered if such a function already exists and can it be more general?
> Thanks,
> Neri

What you asking for is impossible, even in the case of lists, because  
it is based on a confusion of semantics (the question of existence)  
and syntax (pattern matching). For that reason is no way for  
Mathematica to decide if there exists anything that matches any given  
pattern. Even in the simplest case, for example that of a "self- 
contradictory pattern" such as:

_?(# == 1 && # == 2&)

we can tell from our knowledge of elementary arithmetic that this  
will never match anything but Mathematica has no no way to decide  
such semantic questions by pattern matching, since pattern matching  
involves only syntax (and there is nothing wrong with the syntax of  
the above pattern). Of course the question of whether there exists a  
real number which is equal to 1 and equal to 2 can be decided by  



but this has nothing to do with pattern matching.

It seems to me that only thing that might be related to your question  
that can actually be done is something like this:



which tells you that anything that matches {_,1} will also match  
{_,_Integer}. Note however that although



it does not actually mean that Mathematica somehow made use of the  
fact that not all integers are equal to 1. That is a semantic  
question and can't be decided by pattern matching.

Andrzej Kozlowski

  • Prev by Date: Multiple Kernels
  • Next by Date: Re: Transformation rules - explain please
  • Previous by thread: Matching two patterns?
  • Next by thread: Re: Matching two patterns?