MathGroup Archive 2003

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

Search the Archive

Re: Re: New version, new bugs

  • To: mathgroup at smc.vnet.net
  • Subject: [mg42952] Re: [mg42930] Re: New version, new bugs
  • From: Andrzej Kozlowski <andrzej at platon.c.u-tokyo.ac.jp>
  • Date: Wed, 6 Aug 2003 03:16:27 -0400 (EDT)
  • Sender: owner-wri-mathgroup at wolfram.com

Here is another, calmer, and I hope my last attempt to deal with some 
of the issues raised.
First of all, I think some of the confusion has been caused by the use 
of the word "evaluate", where something else, perhaps "parse" ought to 
be used. For example:


HoldPattern[Plus[x_]]

"evaluates" to

HoldPattern[Plus[x_]]

but


a+b/.HoldPattern[Plus[x_]]:>0


0

so in some sense the pattern matcher "evaluates" HoldPattern[Plus[x_]] 
to Plus[x_]. As I said above, perhaps one should use a different word 
(it seems to me that some people call this sort of thing parsing).

This is, of course, the key to the difference between Condition and 
RuleCondition: they both hold their first argument but the pattern 
matcher parses it quite differently.

I can't understand Maxim's question about how to re-write

> 2/.x_:>x^2/;x>0

using RuleCondition. The answer is of course


2 /. x_ :> RuleCondition[x^2, x > 0]

but I can't imagine that he does not know this? Perhaps it is yet 
another one of his trick questions designed to show his superior 
understanding.

I made a bad error with my explanation of why


f[a] + f[b] + 1 /. HoldPattern[Plus][f[_] ..] :> 0 /; True

does not work. I still think that my argument was reasonable, but I 
should have checked it in practice. The argument was as follows. The 
whole pattern on the left of the rule is not enclosed in HoldPattern, 
therefore the pattern matcher evaluates it (here evaluates means really 
evaluates). As always it starts with the head. It sees 
HoldPattern[Plus]. As far as the pattern matcher is concerned 
HoldPattern[Plus] is just Plus (this is parsing not evaluation), so we 
get Plus[f[_] ..]. Proceeding with the evaluation we arrive at f[_] ..
(The process involves both parsing and evaluation.)
That is what I thought, but clearly it is now what actually happens. It 
seems that the Plus is not in fact stripped off, but the flat attribute 
fails to be applied. Exactly what goes wrong I am not sure. I naturally 
feel a little disappointed with not being able to work this out, but I 
do not think it important. Of all of Maxim's original examples  only:

f[a] + f[b] + 1 /. HoldPattern[Plus[f[_] ..]] :> 0 /; True

is, in my opinion, properly formed, and the only one I would ever 
attempt. The ones involving HoldPattern[Plus] would not be tried by any 
sane person who was not deliberately trying to score some debating 
points about the way the pattern matcher works or fails to work.
That seems to me also to be what all this thread has been about, which, 
as I wrote originally, is hardly constructive.

Andrzej Kozlowski
Yokohama, Japan
http://www.mimuw.edu.pl/~akoz/
http://platon.c.u-tokyo.ac.jp/andrzej/




On Monday, August 4, 2003, at 06:45 AM, Maxim wrote:

>
>
> Andrzej Kozlowski wrote:
>
>> Maxim has pointed out that RuleCondition has the attribute HoldFirst. 
>> I
>> was too careless giving my explanation: what I meant to refer to was
>> the following difference between Condition and RuleCondition (which I
>> learned form Allan Hayes)
>>
>> Hold[a] /. a :> Condition[1 + 1,True]
>>
>>          Hold[1+1]
>>
>> Hold[a] /. a :> RuleCondition[1 + 1, True]
>>
>>          Hold[2]
>>
>>
>
> I have no idea what RuleCondition is for and how you can rewrite, say,
>
> 2/.x_:>x^2/;x>0
>
> using RuleCondition, but it's totally irrelevant; you say that 
> something
> somewhere doesn't get evaluated -- then put Evaluate in the right 
> place and
> demonstrate the difference. I have a feeling you won't succeed. And 
> before
> you give yet another "conclusive demonstration", try also explaing how 
> these
> four examples work:
>
> f[a] + f[b] + 1 /. HoldPattern[Plus][_, _] /; True :> 1
> f[a] + f[b] + 1 /. HoldPattern[Plus[_, _]] /; True :> 1
> f[a] + f[b] + 1 /. HoldPattern[Plus][_, _] :> 1 /; True
> f[a] + f[b] + 1 /. HoldPattern[Plus[_, _]] :> 1 /; True
>
> I'll try giving a suggestion why the four examples in my original 
> posting
> (with f[_]..) work that way; it is probably related to the following:
>
> In[1]:=
> FreeQ[x+y+z,x+y]
> Cases[x+y+z,x+y,{0,-1}]
>
> Out[1]=
> False
>
> Out[2]=
> {}
>
> Even though Cases does pattern matching (the second argument can be a
> pattern), in this case it doesn't break the expression into 
> Plus[Plus[x,y],z]
> (yes, I know that it is a documented feature). So my guess is that 
> somewhere
> in the dark recesses of Mathematica pattern matcher this confusion 
> between
> two approaches arises.
>
> Of course, maybe Jens-Peer Kuska will explain to me again how I don't 
> know
> what I'm doing, but he did seem to be rather quiet lately...
>
> The real problem is that there isn't any complete specifications 
> describing
> the behaviour of pattern matching. So it's not even possible to say if 
> an
> example is correct/incorrect, one has to resort to simply pointing out
> inconsistencies...only it seems that developers sometimes have to 
> resort to
> the same means.
>
> Exactly the same goes for the example with Module and variables 
> renaming.
>
> Maxim Rytin
> m.r at prontomail.com
>
>
>
>


  • Prev by Date: Re: New options in 5.0
  • Next by Date: optimizeExpression package in Mathematica version 5?
  • Previous by thread: Re: Re: New version, new bugs
  • Next by thread: Re: New version, new bugs