MathGroup Archive 1994

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

Search the Archive

RE: function definition

  • To: mathgroup at christensen.Cybernetics.NET
  • Subject: [mg215] RE: function definition
  • From: physjfh at phys.canterbury.ac.nz (Jason Harris)
  • Date: Wed, 23 Nov 1994 19:02:39 +1300

Hi,

This is a further response of mine to several posts from
David Kramer, Leendert van Gastel, Richard Mercer, & Dave Withoff

about whether two patterns say f[x_] and f[y_] should be considered
equal and if we could have the new definition for f[y_] override the old
Equivalent definitions.

I have been a little busy and have just now decided to clean up that
answer I gave a little while ago. I still have not looked up that paper
by Girvas & Meader on a unification like system for pattern matching in
Mathematica, but I think it might have helped. In the end I didn't use
it anyway, since the change in was quite simple. I should have got
around to posting this sooner.

The new procedures I have written will handle conditions, repeated
patterns etc but it still has one problem it cannot handle expressions
like

f[x_,x]:=x

Personally thought I don't think such statements should be allowed in the
language in the first place. since the x on the rhs is ambiguous. Other
than this the new code handles all the cases so far considered in the
math-group (that I am aware of). Here is the code:

In[1]:=  (11/23/94 at 18:48:39)
(* the Literal in StanderdisePatterns is to stop the pattern variable a being
evaluated in case it has any previously assigned values *)

RemovePatterns[expr_]:= ReleaseHold[Hold[expr]//.
		{(holdHead:Pattern)[a_,b___]->b}]

tempCreateRule[var_,{num_}]:=
        With[{newvar=ToExpression[StringJoin["temp`symb",ToString[num]]]},
                        var->newvar]

StandardisePatterns[symb_]:=
        Module[{varlist,replacments},
        varlist=Cases[symb,(hh:Pattern)[a_,b___]->Literal[a],Infinity];
        replacments=MapIndexed[tempCreateRule,varlist];
        symb/.replacments
        ]
        
MatchPatternsQ[a_,b_]:=(StandardisePatterns[a]===StandardisePatterns[b])

In[7]:= 
StandardisePatterns[f[x__/;x<3,y_,(z_)..,r]]

Out[7]= 
f[temp`symb1__ /; temp`symb1 < 3, temp`symb2_, (temp`symb3_).., r]

In[8]:= 
MatchPatternsQ[f[x_,t_],f[x_,z_]]

Out[8]= 
True

In[9]:=
MatchPatternsQ[f[x_,x_],f[x_,z_]]

Out[9]=
False

In[10]:=
MatchPatternsQ[f[x__,y_,r],f[t__,z_,r]]

Out[10]=
True

In[11]:=
MatchPatternsQ[f[x__,y_,r],f[t__,t_,r]]

Out[11]=
False

In[12]:=
MatchPatternsQ[f[x__,y_,r],f[t__,z_,u]]

Out[12]=
False

In[13]:=
MatchPatternsQ[f[x__/;x<3,y_,(z_)..,r],f[t__/;t<3,z_,(y_)..,r]]

Out[13]=
True

In[14]:=
MatchPatternsQ[f[x__/;x<3,y_,(z_)..,x],f[t__/;z<3,z_,(y_)..,r]]

Out[14]=
False

So the only problem in using MatchPatternsQ is when it is called with
patterns like f[x_,x] but these in my mind should not be allowed
anyway. Unfortunately this type of pattern can lead to false positives
i.e. MatchPatternsQ can say two patterns match when they don't. e.g.

In[15]:=
MatchPatternsQ[f[x_,x],f[y_,y]]

Out[15]=
True

I hope this is a satisfactory answer and piece of code.

Cheers,

Jason Harris
Physics Department
University of Canterbury
New Zealand





  • Prev by Date: Re: recognition puzzle
  • Next by Date: Re: Negative Area
  • Previous by thread: Re: recognition puzzle
  • Next by thread: [no subject]