Mathematica 9 is now available
Services & Resources / Wolfram Forums
-----
 /
MathGroup Archive
2002
*January
*February
*March
*April
*May
*June
*July
*August
*September
*October
*November
*December
*Archive Index
*Ask about this page
*Print this page
*Give us feedback
*Sign up for the Wolfram Insider

MathGroup Archive 2002

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

Search the Archive

FW: Evaluation of Conditional Definitions

  • To: mathgroup at smc.vnet.net
  • Subject: [mg33810] FW: Evaluation of Conditional Definitions
  • From: "Wolf, Hartmut" <Hartmut.Wolf at t-systems.com>
  • Date: Fri, 19 Apr 2002 02:27:24 -0400 (EDT)
  • Sender: owner-wri-mathgroup at wolfram.com

> -----Original Message-----
> From: David Park [mailto:djmp at earthlink.net]
To: mathgroup at smc.vnet.net
> Sent: Tuesday, April 16, 2002 1:57 AM
> Cc: Ted R. Ersek; Hartmut Wolf; Allan Hayes
> Subject: [mg33810] Evaluation of Conditional Definitions
> 
> 
> Dear MathGroup,
> 
> I want to make a definition that will apply only if a global 
> variable is set
> to True. The following works, but only with an original input 
> expression and
> not on reevaluation of an expression.
> 
> Clear[g, a, UseDefinition];
> g[a] /; UseDefinition == True := 2
> 
> UseDefinition = False;
> expr = g[a]
> g[a]
> 
> UseDefinition = True;
> g[a]
> 2
> 
> But...
> 
> expr
> g[a]
> 
> Why does Mathematica stop before applying the definition to g[a]?
> 
> I actually want to use this with an UpValue definition but 
> run into the same
> problem.
> 
> Clear[f, a, UseDefinition];
> a /: f[a] /; UseDefinition == True := 2
> 
> UseDefinition = False;
> expr = f[a]
> f[a]
> 
> UseDefinition = True;
> f[a]
> 2
> 
> expr
> f[a]
> 
> David Park
> djmp at earthlink.net
> http://home.earthlink.net/~djmp/
> 

Dear David,

just let me add a few observations (look into the attached notebook). Right
now I haven't got the time to check against the details of the (specified,
published?) evaluation sequence.

Anyway, this seems to be a contradiction to the saying "Mathematica is an
infinite evaluation system". My guess is that some optimization of the
evaluation (i.e. reusing stored values) here comes across correctness. Most
interesting is the case where I assigned the unevaluated f[a] and the
toggled UseDefinition twice. At first things looked to be fine, but...

Even SetDelayed goes wrong!

A work-around might be to force re-evaluation of f[a] with ReplaceAll[expr,
HoldPattern[f[a]]:> f[a]].
To set $HistoryLength = 0 does not help.

--
Hartmut

_______________
Here an interesting sequence of my notebook:

 Set Unevaluated f[a] seemingly does work (when UseDefinition was True), but
...

In[2]:= Clear[f, a, UseDefinition]
In[3]:= f[a] /; UseDefinition == True := 2

In[4]:= UseDefinition = True; 

In[5]:= Trace[expr = Unevaluated[f[a]]]
Out[5]=
{HoldForm[expr = f[a]], HoldForm[f[a]], 
  {{HoldForm[UseDefinition], HoldForm[True]}, 
   HoldForm[True == True], HoldForm[True]}, HoldForm[2]}

In[6]:= f[a]
Out[6]= 2

In[7]:= Trace[expr]
Out[7]=
{HoldForm[expr], HoldForm[f[a]], 
  {{HoldForm[UseDefinition], HoldForm[True]}, 
   HoldForm[True == True], HoldForm[True]}, HoldForm[2]}

does work!

In[8]:= UseDefinition = False; 

In[9]:= Trace[expr]
Out[9]=
{HoldForm[expr], HoldForm[f[a]], 
  {{HoldForm[UseDefinition], HoldForm[False]}, 
   HoldForm[False == True], HoldForm[False]}, 
  HoldForm[f[a]]}

does work also, great!

In[10]:= UseDefinition = True; 

In[11]:= Trace[expr]
Out[11]=
{HoldForm[expr], HoldForm[f[a]]}

*but* *what's* *that* !?

In[12]:= Information["expr", LongForm -> False]
expr 
expr = f[a]

In[13]:= Information["f", LongForm -> False]
f
f[a] /; UseDefinition == True := 2



a work-around

In[14]:= Trace[expr /. {HoldPattern[f[a]] :> f[a]}]
Out[14]=
{{HoldForm[expr], HoldForm[f[a]]}, 
  {{HoldForm[HoldPattern[f[a]] :> f[a]], 
    HoldForm[HoldPattern[f[a]] :> f[a]]}, 
   HoldForm[{HoldPattern[f[a]] :> f[a]}]}, 
  HoldForm[f[a] /. {HoldPattern[f[a]] :> f[a]}], 
  HoldForm[f[a]], {{HoldForm[UseDefinition], 
    HoldForm[True]}, HoldForm[True == True], 
   HoldForm[True]}, HoldForm[2]}

In[15]:= UseDefinition = False; 

In[16]:= Trace[expr /. {HoldPattern[f[a]] :> f[a]}]
Out[16]=
{{HoldForm[expr], HoldForm[f[a]]}, 
  {{HoldForm[HoldPattern[f[a]] :> f[a]], 
    HoldForm[HoldPattern[f[a]] :> f[a]]}, 
   HoldForm[{HoldPattern[f[a]] :> f[a]}]}, 
  HoldForm[f[a] /. {HoldPattern[f[a]] :> f[a]}], 
  HoldForm[f[a]], {{HoldForm[UseDefinition], 
    HoldForm[False]}, HoldForm[False == True], 
   HoldForm[False]}, HoldForm[f[a]]}

In[17]:= UseDefinition = True; 

In[18]:= Trace[expr /. {HoldPattern[f[a]] :> f[a]}]
Out[18]=
{{HoldForm[expr], HoldForm[f[a]]}, 
  {{HoldForm[HoldPattern[f[a]] :> f[a]], 
    HoldForm[HoldPattern[f[a]] :> f[a]]}, 
   HoldForm[{HoldPattern[f[a]] :> f[a]}]}, 
  HoldForm[f[a] /. {HoldPattern[f[a]] :> f[a]}], 
  HoldForm[f[a]], {{HoldForm[UseDefinition], 
    HoldForm[True]}, HoldForm[True == True], 
   HoldForm[True]}, HoldForm[2]}



  • Prev by Date: Re: Evaluation of Conditional Definitions
  • Next by Date: Re: numerical compuation of integral with unbounded limits
  • Previous by thread: Re: Evaluation of Conditional Definitions
  • Next by thread: What features do you think ae missing?