MathGroup Archive 2005

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

Search the Archive

Re: Warning from Piecewise

  • To: mathgroup at smc.vnet.net
  • Subject: [mg61569] Re: [mg61523] Warning from Piecewise
  • From: Andrzej Kozlowski <akoz at mimuw.edu.pl>
  • Date: Sat, 22 Oct 2005 03:24:08 -0400 (EDT)
  • References: <FF69AAF8-0118-46DB-B123-C2DC1A80204A@mimuw.edu.pl>
  • Sender: owner-wri-mathgroup at wolfram.com

Having thought more about this, I think I should add a few comments  
about why I described below happens, that si why parts of the left  
hand side of a SetDelayed (and Set) function are evaluated. The  
reason has to do with pattern matching: pattern matching is done  
after parts of an expression have been evaluated.

I also wrote somewhat vaguely:

> SetDelayed does not evaluate the head of an expression on the LHS

what I mean was that the head is actually evaluated but any rules  
associated with the head are not applied.

Andrzej Kozlowski



On 21 Oct 2005, at 20:57, Andrzej Kozlowski wrote:

> On 21 Oct 2005, at 13:38, Chris Rodgers wrote:
>
>
>
>> Hi,
>>
>> When I try to set a delayed expression like this:
>>
>> Comm[Piecewise[A:{{_, _}..}], Piecewise[B:{{_, _}..}]] :=
>>    Comm[Testy[A], Testy2[B]]
>>
>> I get a warning from Piecewise that says:
>>
>> Piecewise::pairs: The first argument A : {{_, _} ..} of Piecewise  
>> is not
>> a list of pairs.
>>
>> I thought that the HoldAll attribute on SetDelayed should prevent
>> evaluation of the LHS argument, so where is this warning coming from?
>>
>> Can I ignore a warning like this, or should I instead use a different
>> sort of pattern?
>>
>> Many thanks,
>>
>> Chris Rodgers.
>>
>>
>>
>>
>
> Actually Set and SetDelayed are special functions and their  
> behaviour in this sort of situation is not determined by their  
> attributes. You can see the difference as follows. First, let us  
> create a function H, to which we give the Attribute HoldAll.
>
> SetAttributes[H, HoldAll]
>
> Let's also define a function f to be:
>
> f[x_] := Print[x]
>
> (There is a reason why I need another function that does the same  
> thing as Print).
> Let's first try the following using a function G which evaluates  
> its arguments:
>
>
> G[f[x=1+1],p]
>
> From In[3]:=
> 2
>
> Out[3]=
> G(Null,p)
>
> we can check that x has the value 2:
>
> In[4]:=
> x
>
> Out[4]=
> 2
>
> Now let's clear x and do the same thing with H:
>
>
> In[5]:=
> Clear[x]
>
> In[6]:=
> H[f[x=1+1],p]
>
> Out[6]=
> H(f(x=1+1),p)
>
> In[7]:=
> x
>
> Out[7]=
> x
>
> No evaluation at all, as expected. Now let's again clear x and set  
> H to be SetDelayed:
>
> In[8]:=
> Clear[x]
>
> In[9]:=
> H=SetDelayed;
>
> In[10]:=
> H[f[x=1+1],p]
>
> We get no output, and nothing gets printed, which means f was not  
> evaluated. However:
>
> In[11]:=
> x
>
> Out[11]=
> 2
>
> In other words, although SetDelayed does not evaluate the head of  
> an expression on the LHS it does evaluate its other parts.
> Why does it behave in this way? I think the reason is related to  
> the way SetDelayed defines DownValues for symbols. Finally, you can  
> avoid your message by defining Comm as follows:
>
> Comm[Unevaluated[Piecewise[A : {{_, _} ..}]], Unevaluated[
>           Piecewise[B : {{_, _} ..}]]] :=
>    Comm[Testy[A], Testy2[B]]
>
> In fact however it does not really matter. If you evaluate the  
> definition with Unevaluated and one without and then check what  
> Mathematica knows about Comm  with
>
> ?Comm
>
> you will see exactly the same Output. So I think you are safe using  
> your function, provided of course the rest of your code that you  
> did not include is O.K.
>
> Andrzej Kozlowski
> Tokyo, Japan
>
>
>
>
>


  • Prev by Date: Re: How smooth graphs?
  • Next by Date: Re: Re: Pure Function for String Selection
  • Previous by thread: Re: Warning from Piecewise
  • Next by thread: Re: Re: Warning from Piecewise