Using `With` with a list of `Rules` - but without affecting the normal behaviour of `With`

和自甴很熟 提交于 2019-12-19 07:53:19

问题


Say I have a list of Rules

rules = {a -> b, c -> d};

which I use throughout a notebook. Then, at one point, it makes sense to want the rules to apply before any other evaluations take place in an expression. Normally if you want something like this you would use

In[2]:= With[{a=b,c=d}, expr[a,b,c,d]]
Out[2]= expr[b, b, d, d]

How can I take rules and insert it into the first argument of With?


Edit

BothSome solutions fail do all that I was looking for - but I should have emphasised this point a little more. See the bold part above.

For example, let's look at

rules = {a -> {1, 2}, c -> 1};

If I use these vaules in With, I get

In[10]:= With[{a={1,2},c=1}, Head/@{a,c}]
Out[10]= {List,Integer}

Some versions of WithRules yield

In[11]:= WithRules[rules, Head/@{a,c}]
Out[11]= {Symbol, Symbol}

(Actually, I didn't notice that Andrew's answer had the Attribute HoldRest - so it works just like I wanted.)


回答1:


You want to use Hold to build up your With statement. Here is one way; there may be a simpler:

In[1]:= SetAttributes[WithRules, HoldRest]

In[2]:= WithRules[rules_, expr_] := 
 With @@ Append[Apply[Set, Hold@rules, {2}], Unevaluated[expr]]

Test it out:

In[3]:= f[args___] := Print[{args}]

In[4]:= rules = {a -> b, c -> d};

In[5]:= WithRules[rules, f[a, c]]

During evaluation of In[5]:= {b,d}

(I used Print so that any bug involving me accidentally evaluating expr too early would be made obvious.)




回答2:


I have been using the following form of WithRules for a long time. Compared to the one posted by Andrew Moylan, it binds sequentially so that you can say e.g. WithRules[{a->b+1, b->2},expr] and get a expanded to 3:

SetAttributes[WithRules, HoldRest]
WithRules[rules_, expr_] := ReleaseHold@Module[{notSet}, Quiet[
     With[{args = Reverse[rules /. Rule[a_, b_] -> notSet[a, b]]},
       Fold[With[{#2}, #1] &, Hold@expr, args]] /. notSet -> Set, 
   With::lvw]]

This was also posted as an answer to an unrelated question, and as noted there, it has been discussed (at least) a couple of times on usenet:

  • A version of With that binds variables sequentially
  • Add syntax highlighting to own command

HTH

EDIT: Added a ReleaseHold, Hold pair to keep expr unevaluated until the rules have been applied.




回答3:


One problem with Andrew's solution is that it maps the problem back to With, and that does not accept subscripted variables. So the following generates messages.

WithRules[{Subscript[x, 1] -> 2, Subscript[x, 2] -> 3}, 
 Power[Subscript[x, 1], Subscript[x, 2]]]

Given that With performs syntactic replacement on its body, we can set WithRules alternatively as follows:

ClearAll[WithRules]; SetAttributes[WithRules, HoldRest];
WithRules[r : {(_Rule | _RuleDelayed) ..}, body_] := 
 ReleaseHold[Hold[body] /. r]

Then

In[113]:= WithRules[{Subscript[x, 1] -> 2, 
  Subscript[x, 2] -> 3}, Subscript[x, 1]^Subscript[x, 2]]

Out[113]= 8

Edit: Addressing valid concerns raised by Leonid, the following version would be safe:

ClearAll[WithRules3]; SetAttributes[WithRules3, HoldRest];
WithRules3[r : {(_Rule | _RuleDelayed) ..}, body_] := 
 Developer`ReplaceAllUnheld[Unevaluated[body], r]

Then

In[194]:= WithRules3[{Subscript[x, 1] -> 2, Subscript[x, 2] -> 3}, 
 Subscript[x, 1]^Subscript[x, 2]]

Out[194]= 8

In[195]:= WithRules3[{x -> y}, f[y_] :> Function[x, x + y]]

Out[195]= f[y_] :> Function[x, x + y]

Edit 2: Even WithRules3 is not completely equivalent to Andrew's version:

In[206]:= WithRules3[{z -> 2}, f[y_] :> Function[x, x + y + z]]

Out[206]= f[y_] :> Function[x, x + y + z]

In[207]:= WithRules[{z -> 2}, f[y_] :> Function[x, x + y + z]]

Out[207]= f[y$_] :> Function[x$, x$ + y$ + 2]


来源:https://stackoverflow.com/questions/5188875/using-with-with-a-list-of-rules-but-without-affecting-the-normal-behaviour

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!