Re: Problem with evaluation of delayed rules
- To: mathgroup at smc.vnet.net
- Subject: [mg21579] Re: Problem with evaluation of delayed rules
- From: Bojan Bistrovic <bojanb at physics.odu.edu>
- Date: Sat, 15 Jan 2000 02:04:22 -0500 (EST)
- Organization: Old Dominion Universityaruba
- References: <85mkef$1og@smc.vnet.net>
- Sender: owner-wri-mathgroup at wolfram.com
Eckhard Hennig wrote:
>
> Hi, I would greatly appreciate any elegant solutions to the following type
> of problem.
>
> Assume that we have a list of symbols, e.g.
>
> In[1]:= symbols = {a, b, c, d};
>
> and that we have the following expression that involves a function (here
> "If") with HoldXXX attribute (HoldAll, HoldRest, ...).
>
> In[2]:= expr = If[x > 0, Difference[1, 2], Difference[3, 4]];
>
> In the above expression, I want to replace all objects of the form
> Difference[i, j] by differences of the two entries i and j from the list
> "symbols". If I use a delayed rule as follows
>
> In[3]:= expr /. Difference[x_, y_] :> symbols[[x]] - symbols[[y]]
>
> then this is what I get:
>
> Out[3]= If[x > 0, symbols[[1]] - symbols[[2]], symbols[[3]] - symbols[[4]]]
>
> Due to the HoldXXX attribute of the "If" function, the right-hand side of
> the delayed rule remains unevaluated. However, I need the result of the rule
> to be evaluated BEFORE it is inserted into expr, i.e. the result I want for
> In[3] is:
>
> Out[3new]= If[x > 0, a - b, c - d]
>
> Does there exist any (simple) approach to forcing Mathematica 3.0/4.0 to
> simplify the result of a delayed rule as soon as the rule applies to a
> subexpression of a held expression (please note the conditions below)? One
> may argue that it doesn't make a difference in the end whether result of
> rewriting expr is given in the form of Out[3] or Out[3new]. Well, the
> difference comes in as soon as you clear the value of "symbols" with
> Clear[symbols] and then evaluate Out[3] for some x.
>
> Condition 1: Temporarily removing any HoldXXX attributes from expr or any of
> its subexpressions is not a solution because I only want the results of
> rules to be simplified. No further simplification of held subexpressions
> must be performed after rule application. For example,
>
> Out[3] /. If -> MyIf /. MyIf -> If
>
> yields Out[3new], but this approach does not count as a solution.
>
> Condition 2: The solution must not be limited to a predefined set of
> functions with HoldXXX attribute, say If and Which.
>
> Best regards,
>
> Eckhard
>
You could use the "/. If -> MyIf /. MyIf -> If" trick and map a "Hold"
function before the transformation to the places yuo don't want
evaluated:
MapAt[ReleaseHold,MapAt[Hold,Out[3],{1}]/.If->MyIf/.MyIf->If,{1}]
If "Hold" doesn't do the trick, the, you can try with HoldForm or
Unevaluated or something else. There was a talk about working with
unevaluated expressions on 1999 developers conference; here's the link:
http://library.wolfram.com/conferences/devconf99/withoff/index2.html
http://library.wolfram.com/conferences/devconf99/withoff/index.html
The first one is HTML and the secon one is .nb format
Bye, Bojan
--
-------------------------------------------------------------
Bojan Bistrovic, bojanb at physics.odu.edu
Old Dominion University, Physics Department, Norfolk, VA
-------------------------------------------------------------