Re: Matching two patterns?

*To*: mathgroup at smc.vnet.net*Subject*: [mg74146] Re: [mg74105] Matching two patterns?*From*: Andrzej Kozlowski <akoz at mimuw.edu.pl>*Date*: Mon, 12 Mar 2007 22:09:13 -0500 (EST)

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 Mathematica: Resolve[Exists[a,Element[a,Reals],a==1&&a==2]] False 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: MatchQ[{_,1},{_,_Integer}] True which tells you that anything that matches {_,1} will also match {_,_Integer}. Note however that although MatchQ[{_,_Integer},{_,1}] False 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