MathGroup Archive 2007

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

Search the Archive

Re: Controlling evaluation

  • To: mathgroup at smc.vnet.net
  • Subject: [mg83956] Re: Controlling evaluation
  • From: Yaroslav Bulatov <yaroslavvb at gmail.com>
  • Date: Wed, 5 Dec 2007 07:16:32 -0500 (EST)
  • References: <fiji6s$ikm$1@smc.vnet.net> <fim998$run$1@smc.vnet.net>

Using dummy symbol, then replacing it, like you did, is one of the
techniques that Robby Villegas' suggested for dealing with the
"overarching evaluator".
http://library.wolfram.com/conferences/devconf99/villegas/UnevaluatedExpressions/index.html

He gives a few other approaches, I think the most general one is to
use Replace* to manipulate expressions into the form you want.
For instance, the function below uses Extract/Position/ReplacePart to
extract expressions matching pattern, evaluate them, then stuff the
results back in

(*evaluates subexpressions of expr matching pattern,expr's head \
should have HoldAll attribute*)
pointEvaluate[expr_, patt_] :=
 Module[{poses, parts}, poses = Position[expr, patt];
  (*ReplacePart doesn't work for empty position lists*)
  If[Length[poses] > 0,(*extract the parts (it also evaluates them),
   put them back in*)parts = Extract[expr, poses];
   ReplacePart[expr, parts, poses, MapIndexed[#2 &, parts]],
   (*no pattern fits,return expr unchanged*)
   expr
   ]
  ]
arg1 = {a, b, c}; arg2 = {d, e, f};
pointEvaluate[Condition[ex, samelistQ[arg1, arg2]],
 HoldPattern[arg1 | arg2]]
pointEvaluate[Hold[{1 + 2, 3*4, 5 + 6}], HoldPattern[_ + _]]

My own question for the group -- what is the recommended way to define
a function only for arguments with HoldAll head (like Hold, Condition,
etc)?

Yaroslav


On Nov 29, 3:54 am, dh <d... at metrohm.ch> wrote:
> Hi,
>
> Evaluate will evaluate its arguments if it appears as argument in a
>
> function with one of the Hold.. attributes. Consider:
>
> Condition[expr, samelistQ[arg1 // Evaluate, arg2 // Evaluate]]
>
> Condition has the attribute HoldAll. If samelistQ has the asme
>
> attribute, we therefore need to write Evaluate twice:
>
> Condition[expr, Evaluate[samelist[Evaluate[arg1],Evaluate[arg1]]]
>
> hope this helps, Daniel
>
> magma wrote:
> > I am building a package and I need to programmatically construct
> > expressions .
> > At a certain point I have
>
> > arg1 = {a, b, c}; (* some list *)
> > arg2 = {d, e, f}; (* some other list *)
> > samelistQ[list1_List, list2_List] :=
> >  list1 === list2  (* some logic function *)
>
> > Now I want to construct an expression like
>
> > Condition[expr, samelistQ[arg1, arg2]]
>
> > with arg1 and arg2 evaluated, but with samelist[...] not evaluated.
> > In other words, I want to obtain exactly this
>
> > Condition[expr, samelistQ[{a, b, c}, {d, e, f}]]
>
> > where the 2 args have been evaluated, but samelistQ[...] appears
> > always unevaluated
> > What is expr is not important, just leave it unassigned.
> > The problem is that Condition blocks the evaluation of arg1 and arg2
> > and of samelistQ.
>
> > Now, doing
>
> > Condition[expr, samelistQ[arg1 // Evaluate, arg2 // Evaluate]]
>
> > does not work, because Evaluate does not disappear.
> > So what to do? I tried Hold, ReleaseHold, ect, but it didn't work.
>
> > This is how I solved it
>
> > Condition @@ List[expr, pp[arg1, arg2]] /. pp -> samelistQ
>
> > where pp is an unassigned symbol.
> > My question: isn't there a better way, using the functions which are
> > supposed to control evaluation (Hold, Evaluate, ect) to achieve this
> > result?
>
> > Perhaps a related more general question:
> > how do we block
> > f[x,y]
> > while allowing evaluation of x, and y ?
> > Thank you in advance for any comment



  • Prev by Date: Re: slot argument weirdness
  • Next by Date: Re: slot argument weirdness
  • Previous by thread: Re: File reading and writing within for loop
  • Next by thread: Re: Controlling evaluation