MathGroup Archive 2011

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

Search the Archive

Re: Unprotect

  • To: mathgroup at smc.vnet.net
  • Subject: [mg120897] Re: Unprotect
  • From: Leonid Shifrin <lshifr at gmail.com>
  • Date: Sun, 14 Aug 2011 20:18:39 -0400 (EDT)
  • Delivered-to: l-mathgroup@mail-archive0.wolfram.com
  • References: <201108141215.IAA03440@smc.vnet.net>

Hi Themis,

In code,  one possible answer to both questions is this:

Entropy;
Unprotect[Entropy];
Entropy[x_] := 1/x;
DownValues[Entropy] = RotateRight[DownValues[Entropy]];
Entropy[10.]

To understand why you need to call Entropy first, and why this solves the
first problem, please read this post:

http://stackoverflow.com/questions/5649379/why-do-i-have-to-evaluate-this-twice/

Note that I don't justify this behavior - IMO it is a borderline bug, or at
least this contradicts the semantics of Protect/Unprotect, as exposed to the
user.

The reason why manipulations with DownValues solve the second problem (error
message) can be seen upon removing the ReadProtected attribute of Entropy
and inspecting the code. It uses the construct like

f[args___]:=Block[{someValue = ...}, someCode/;someValue =!=$Failed]

This looks like a general definition (since triple underscore is used), but
it is not, since the condition inside Block is a part of the pattern. When
you add your definition, it is therefore not considered more specific by
Mathematica, and is therefore added after the built-in one. It is the
built-in definition that is responsible for the error message. By
rearranging DownValues, we put your definition on top, so the built-in never
fires. I would consider  it a possible implementation defect - one could
instead add another built-in definition  like

f[args___]:="never happens"/;Message[...]

and issue a message here - and this problem would then be absent.

Regards,
Leonid


On Sun, Aug 14, 2011 at 5:15 AM, Themis Matsoukas <tmatsoukas at me.com> wrote:

> I want to redefine a build in command. I use the following example:
>
> Unprotect[Entropy];
> Entropy[x_] := 1/x
> Entropy[10.]
>
> When I execute these commands on a fresh kernel I get:
>
> SetDelayed::write: Tag Entropy in Entropy[x_] is Protected. >>
>
> $Failed
>
> Entropy::targ: Argument 10.` at position 1 should be a List, a SparseArray,
> or a String. >>
>
> Entropy[10.]
>
> If I execute a second time I get my definition to work but I still get a
> warning about the arguments:
>
> Entropy::targ: Argument 10.` at position 1 should be a List, a SparseArray,
> or a String. >>
>
> 0.1
>
> So, two questions:
> 1. Do I have to execute twice to cause a custom definition to stick?
> 2. How do I get rid of the warning once the custom definition works?
>
> Running 8.01 on Lion.
>
> {"8.0 for Mac OS X x86 (64-bit) (February 24, 2011)", 1}
>
> Themis
>
>



  • References:
    • Unprotect
      • From: Themis Matsoukas <tmatsoukas@me.com>
  • Prev by Date: Cataloging apps for Finder files? (Mathematica notebooks especially)
  • Next by Date: Re: Getting the Date and Time From a Time Server
  • Previous by thread: Unprotect
  • Next by thread: Re: Unprotect