       Re: Metastable expressions in Mathematica

• To: mathgroup at smc.vnet.net
• Subject: [mg119637] Re: Metastable expressions in Mathematica
• From: Leonid Shifrin <lshifr at gmail.com>
• Date: Wed, 15 Jun 2011 07:21:18 -0400 (EDT)
• References: <201106141014.GAA06248@smc.vnet.net>

```Hi David,

This is a known effect. There is a special command Update, used in such
cases to force Mathematica to propagate changes like the one you discuss.
For the case at hand:

In:= ClearAll[f];
test = g[f[a]]

Out= g[f[a]]

In:= switch = False;
f[x_] := x^2 /; switch

In:= test

Out= g[f[a]]

In:= switch = True;

In:= test

Out= g[f[a]]

In:= Update[f];
test

Out= g[a^2]

I have an impression that switches in conditions are a major source of such
situations (at least, I was bitten by this more than once and every time in
this setting). My guess is that the reason may be that patterns and
definitions are optimized internally for a faster dispatch, and not
re-evaluating such conditional switches may be a part of the optimization,
as you suggested.

Regards,
Leonid

On Tue, Jun 14, 2011 at 2:14 PM, David Bailey <dave at removedbailey.co.uk>wrote:

> Everyone knows that expressions in Mathematica get automatically
> evaluated as far as is possible. Furthermore, most people realise that
> Mathematica must have a way to track changes to the definitions of
> symbols, so as to avoid excessive evaluation.
>
> Here is the typical case:
>
> In:= Clear[f]
>
> In:= test = g[f[a]]
>
> Out= g[f[a]]
>
> 'test' contains an expression containing an unevaluated function call to f
>
> In:= test
>
> Out= g[f[a]]
>
> Now we give f a definition, and confirm that Mathematica 'knows' the
> expression held by 'test' needs to be reevaluated:
>
> In:= f[x_] := x^2;
>
> In:= test
>
> Out= g[a^2]
>
> So far as expected, but now we define f in a way that allows it to be
> switched on and off without actually changing its definition:
>
> In:= Clear[f]
>
> In:= switch = False;
>
> In:= f[x_] := x^2 /; switch
>
> In:= test = g[f[a]]
>
> Out= g[f[a]]
>
> In:= switch = True;
>
> Here we see that Mathematica fails to notice that the expression can be
> further evaluated - the expression in 'test' is metastable.
>
> In:= test
>
> Out= g[f[a]]
>
> Some processes obviously cause complete evaluation:
>
> In:= Uncompress[Compress[test]]
>
> Out= g[a^2]
>
> Others, rather surprisingly, do not!
>
> (Dialog) In:= test /. f -> f
>
> (Dialog) Out= g[f[a]]
>
> I don't really consider metastable expression to be a bug - more a
> curiosity - and it would be undesirable to slow Mathematica processing
> to try to fix this.
>
> However, metastable expressions, do give a window into the methods
> Mathematica uses to minimise the reevaluation of expressions. For
> example, it might be interesting to repeat the above 10000 times with
> different symbols, to determine if Mathematica is always consistent in
> this regard.
>
> I have never seen this effect discussed, so I would be interested in
> links to previous discussions, if any.
>
> David Bailey
> http://www.dbaileyconsultancy.co.uk
>
>

```

• Prev by Date: Re: Seaching in Pi a sequence. Looking for a faster method