MathGroup Archive 2005

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

Search the Archive

Re: Re: Warning from Piecewise

  • To: mathgroup at smc.vnet.net
  • Subject: [mg61608] Re: [mg61569] Re: [mg61523] Warning from Piecewise
  • From: Andrzej Kozlowski <akoz at mimuw.edu.pl>
  • Date: Sun, 23 Oct 2005 05:46:23 -0400 (EDT)
  • References: <FF69AAF8-0118-46DB-B123-C2DC1A80204A@mimuw.edu.pl> <200510220724.DAA12416@smc.vnet.net> <acbec1a40510221104s9e9e07cn668a994a1b3189fb@mail.gmail.com>
  • Sender: owner-wri-mathgroup at wolfram.com

On 23 Oct 2005, at 03:04, Chris Chiasson wrote:

> I thought evaluation was the transformation of an expression using  
> rules?

It seems to me that the Head has to be, in some sense, evaluated if a  
DownValue is going to be assigned to it. Otherwise how would  
Mathematica know which symbol to assign a DownValue to?

You can see this happen here:

SetDelayed[Print[x], p]

SetDelayed::write: Tag Print in Print[x] is Protected

$Failed

Note that x was not printed but still I think we can say the head of  
the expression Print[x] was evaluated.
This was why I sent my correction.

Andrzej Kozlowski




>
> On 10/22/05, Andrzej Kozlowski <akoz at mimuw.edu.pl> wrote:
>
>> 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
>>>
>>>
>>>
>>>
>>>
>>>
>>
>>
>>
>
>
> --
> Chris Chiasson
> http://chrischiasson.com/contact/chris_chiasson
>


  • Prev by Date: Re: aggregation of related elements in a list
  • Next by Date: Re: significance arithmetic and numeric equation solving
  • Previous by thread: Re: Warning from Piecewise
  • Next by thread: Re: Warning from Piecewise