7

The usage message for Set reminds us that multiple assignments can easily be made across two lists, without having to rip anything apart. For example:

Remove[x1, x2, y1, y2, z1, z2];
{x1, x2} = {a, b}

Performs the assignment and returns:

{a, b}

Thread, commonly used to generate lists of rules, can also be called explicitly to achieve the same outcome:

Thread[{y1, y2} = {a, b}]
Thread[{z1, z2} -> {a, b}]

Gives:

{a, b}
{z1 -> a, z2 -> b}

However, employing this approach to generate localized constants generates an error. Consider this trivial example function:

Remove[f];
f[x_] :=
 With[{{x1, x2} = {a, b}},
  x + x1 + x2
  ]
f[z]

Here the error message:

With::lvset: "Local variable specification {{x1,x2}={a,b}} contains 
{x1,x2}={a,b}, which is an assignment to {x1,x2}; only assignments 
to symbols are allowed."

The error message documentation (ref/message/With/lvw), says in the 'More Information' section that, "This message is generated when the first element in With is not a list of assignments to symbols." Given this explanation, I understand the mechanics of why my assignment failed. Nonetheless, I'm puzzled and wondering if this is necessary restriction by WRI, or a minor design oversight that should be reported.

So here's my question:

Can anyone shed some light on this behavior and/or offer a workaround? I experimented with trying to force Evaluation, without luck, and I'm not sure what else to try.

5
  • 1
    I often ponder such design choices, and I sometimes post such questions here. Unfortunately it is rare that such things are explained. I have contended myself with the fact that With/Block/Module just work differently, and that Set in the first argument of these isn't actually performing the Set operation but instead is syntax.
    – Mr.Wizard
    Commented Dec 4, 2011 at 6:53
  • From my reading of the error message documentation (quoted part toward the end of my Q), it seems to me as if there is some simple pattern matching driving the current behavior---i.e., accepting patterns that have the head List, containing only elements that evaluate to True for something like MatchQ[Hold[Set[x, 1]], Hold[Set[Symbol_, _]]]. If this is the case, it would, at least in my myopic little world, seem possible to tweak the pattern matching/testing to allow for the behavior I seek, and that the documentation on Set seems to imply should work (as a valid form of assignment). Commented Dec 4, 2011 at 8:21
  • I don't disagree, but I know of no way to effect that change.
    – Mr.Wizard
    Commented Dec 4, 2011 at 8:33
  • Call and ask for it? :) Or pester @Daniel Lichtblau? Commented Dec 4, 2011 at 9:00
  • Somewhat related question: stackoverflow.com/questions/5188875/…
    – Mr.Wizard
    Commented Dec 4, 2011 at 10:47

4 Answers 4

11

What you request is tricky. This is a job for macros, as already exposed by the others. I will explore a different possibility - to use the same symbols but put some wrappers around the code you want to write. The advantage of this technique is that the code is transformed "lexically" and at "compile-time", rather than at run-time (as in the other answers). This is generally both faster and easier to debug.

So, here is a function which would transform the With with your proposed syntax:

Clear[expandWith];
expandWith[heldCode_Hold] :=
 Module[{with}, 
   heldCode /. With -> with //. {
       HoldPattern[with[{{} = {}, rest___}, body_]] :> 
              with[{rest}, body],
       HoldPattern[
         with[{
           Set[{var_Symbol, otherVars___Symbol}, {val_, otherVals___}], rest___}, 
           body_]] :>
              with[{{otherVars} = {otherVals}, var = val, rest}, body]
     } /. with -> With]

Note that this operates on held code. This has the advantage that we don't have to worry about possible evaluation o the code neither at the start nor when expandWith is finished. Here is how it works:

In[46]:= expandWith@Hold[With[{{x1,x2,x3}={a,b,c}},x+x1+x2+x3]]
Out[46]= Hold[With[{x3=c,x2=b,x1=a},x+x1+x2+x3]]

This is, however, not very convenient to use. Here is a convenience function to simplify this:

ew = Function[code, ReleaseHold@expandWith@Hold@code, HoldAll]

We can use it now as:

In[47]:= ew@With[{{x1,x2}={a,b}},x+x1+x2]
Out[47]= a+b+x

So, to make the expansion happen in the code, simply wrap ew around it. Here is your case for the function's definition:

Remove[f];
ew[f[x_] := With[{{x1, x2} = {a, b}}, x + x1 + x2]]

We now check and see that what we get is an expanded definition:

?f
Global`f
f[x_]:=With[{x2=b,x1=a},x+x1+x2]

The advantage of this approach is that you can wrap ew around an arbitrarily large chunk of your code. What happens is that first, expanded code is generated from it, as if you would write it yourself, and then that code gets executed. For the case of function's definitions, like f above, we cansay that the code generation happens at "compile-time", so you avoid any run-time overhead when usin the function later, which may be substantial if the function is called often.

Another advantage of this approach is its composability: you can come up with many syntax extensions, and for each of them write a function similar to ew. Then, provided that these custom code-transforming functions don't conlict with each other, you can simply compose (nest) them, to get a cumulative effect. In a sense, in this way you create a custom code generator which generates valid Mathematica code from some Mathematica expressions representing programs in your custom languuage, that you may create within Mathematica using these means.

EDIT

In writing expandWith, I used iterative rule application to avoid dealing with evaluation control, which can be a mess. However, for those interested, here is a version which does some explicit work with unevaluated pieces of code.

Clear[expandWithAlt];
expandWithAlt[heldCode_Hold] :=
 Module[{myHold},
    SetAttributes[myHold, HoldAll];
    heldCode //. HoldPattern[With[{Set[{vars__}, {vals__}]}, body_]] :>
     With[{eval = 
              (Thread[Unevaluated[Hold[vars] = Hold[vals]], Hold] /.
                   Hold[decl___] :> myHold[With[{decl}, body]])},
       eval /; True] //. myHold[x_] :> x]

I find it considerably more complicated than the first one though.

4
  • This comes close to the ideal of using With itself, yet not modifying default functionality. Easy +1.
    – Mr.Wizard
    Commented Dec 4, 2011 at 19:33
  • Do you think that this is possible to combine with a Gayley-Villegas type modification of the built-in With?
    – Simon
    Commented Dec 5, 2011 at 10:40
  • @Simon You don't need Gayley -Villegas technique here, since the new syntax can not be confused with the standard one. So, you just mean adding DownValues for With. Sure, this is possible and easy to combine with the above. The reasons I did not do this are the run-time overhead this would induce plus a general attitude against messing with fundamental language constructs in this manner. Also, say you add this, make a package using it, someone else using your code would try to add other rules to With, they would conflict with yours in subtle ways... lots of opportunities for a big mess:) Commented Dec 5, 2011 at 10:51
  • 2
    @Simon Given Mathematica's metaprogramming capabilities, I think that code generation is generally a way to go. It is both flexible and composable, you do not alter the built-in functionality in any way, and the changes you introduced are local (and, possibly, lexically scoped). This is easy to modify and easy to debug. You can modify the syntax of "built-in"-s as you please, since mma evaluator won't have to evaluate that syntax anyway - it will be first transformed to a standard one by the code - generating functions you write, and only then evaluated. Commented Dec 5, 2011 at 10:55
7

The tricky issue is to keep the first argument of Set unevaluated. Here is my suggestion (open to improvements of course):

  SetAttributes[myWith, HoldAll];
    myWith[{s : Set[a_List, b_List]}, body_] :=
     ReleaseHold@
      Hold[With][
       Table[Hold[Set][Extract[Hold[s], {1, 1, i}, Hold], 
         Extract[Hold[s], {1, 2, i}]], {i, Length@b}], Hold@body]
    x1 = 12;
    Remove[f];
    f[x_] := myWith[{{x1, x2} = {a, b}}, x + x1 + x2]
    f[z]

results in

a+b+z

Inspired by halirutan below I think his solution, made slightly more safely, is equivalent to the above:

SetAttributes[myWith, HoldAll];
myWith[{Set[a : {__Symbol}, b_List]} /; Length[a] == Length[b], 
  body_] := 
 ReleaseHold@
  Hold[With][
   Replace[Thread[Hold[a, b]], Hold[x_, y_] :> Hold[Set[x, y]], 1], 
   Hold@body]
3
  • Nice! Holding With seems to be key thing. The following uses the same trick: f[x_] = ReleaseHold@ Hold[With][ Flatten@{({x1, x2} -> {a, b} // Thread) /. (Rule[q_, r_] -> HoldForm@Set[q, r])}, x + x1 + x2] Using x1=5; f[z] gives a+b+z.
    – kglr
    Commented Dec 4, 2011 at 12:24
  • If you replace the first argument {s : Set[a_List, b_List] with s:(Set|Rule)[a_List,b_List] without changing anything else,then one can use it both as g[x_]:=myWith[{{x1,x2)->{a,b}},x+x1+x2] using rules and as h[x_]:=myWith[{{x1,x2)={a,b}},x+x1+x2] using assignment for initialization of local variables.
    – kglr
    Commented Dec 4, 2011 at 13:02
  • Good stuff Rolf! Sorry I only came back now to comment and vote. +1. Commented Dec 4, 2011 at 19:37
6

The tutorial "LocalConstants" says

The way With[{x=Subscript[x, 0],...},body] works is to take body, and replace every occurrence of x, etc. in it by Subscript[x, 0], etc. You can think of With as a generalization of the /. operator, suitable for application to Mathematica code instead of other expressions.

Referring to this explanation it seems obvious that something like

x + x1 + x2 /. {x1, x2} -> {a, b}

will not work as it might be expected in the With notation.

Let's assume you really want to hack around this. With[] has the attribute HoldAll, therefore everything you give as first parameter is not evaluated. To make such a vector-assignment work you would have to create

With[{x1=a, x2=b}, ...]

from the vector-notation. Unfortunately,

Thread[{a, b} = {1, 2}]

does not work because the argument to Thread is not held and the assignment is evaluated before Thread can do anything.

Lets fix this

SetAttributes[myThread, HoldFirst];
myThread[Set[a_, b_]] := mySet @@@ Transpose[{a, b}]

gives

In[31]:= myThread[{a, b, c} = {1, 2, 3}]
Out[31]= {mySet[a, 1], mySet[b, 2], mySet[c, 3]}

What looks promising at first, just moved the problem a bit away. To use this in With[] you have to replace at some point the mySet with the real Set. Exactly then, With[] does not see the list {a=1, b=2, c=3} but, since it has to be evaluated, the result of all assignments

In[32]:= With[
 Evaluate[myThread[{a, b, c} = {1, 2, 3}] /. mySet :> Set], a + b + c]

During evaluation of In[32]:= With::lvw: Local 
variable specification {1,2,3} contains 1, which is not an assignment to a symbol. >>

Out[32]= With[{1, 2, 3}, a + b + c]

There seems to be not easy way around this and there is a second question here: If there is a way around this restriction, is it as fast as With would be or do we lose the speed advantage compared to Module? And if speed is not so important, why not using Module or Block in the first place?

3
  • Your last paragraph appears to suggest that this problem does not affect Module or Block, but it does. Or, are you saying there is a workaround for Module or Block that does not work on With?
    – Mr.Wizard
    Commented Dec 4, 2011 at 7:39
  • 2
    @Mr.Wizard In Module and Block you can do at least Module[{a, b, c},{a, b, c} = {1, 2, 3}; a + b + c] which does not work in With.
    – halirutan
    Commented Dec 4, 2011 at 10:09
  • 1
    Lots of things thought impossible are actually possible in mma. Instead of showing what does not work and concluding from there, it is always good to assume that there is a way to make it work, and try to find it out. All you have to do to save this long answer and have a happy end, is this: Unevaluated[With[{{x1, x2} = {a, b}}, x + x1 + x2]] /. HoldPattern[With[{Set[{vars__}, {vals__}]}, body_]] :> (Thread[Unevaluated[Hold[vars] = Hold[vals]], Hold] /. Hold[decl___] :> With[{decl}, body]). This is how I'd do this, staying within your ideology, anyways. Commented Dec 4, 2011 at 21:13
3

You could use Transpose to shorten Rolfs solution by 100 characters:

SetAttributes[myWith, HoldAll];
myWith[{Set[a_List, b_List]}, body_] := 
 ReleaseHold[Hold[With][Hold[Set[#1, #2]] & @@@ Transpose[{a, b}],
   Hold@body
   ]]

@Heike, yep the above breaks if either variable has already a value. What about this:

SetAttributes[myWith, HoldAll];
myWith[{Set[a_List, b_List]}, body_] := 
 ReleaseHold@
  Hold[With][Thread[Hold[a, b]] /. Hold[p__] :> Hold[Set[p]], 
   Hold@body]
2
  • 1
    This breaks when one of the variables in a has been set before, e.g. a = 3; myWith[{{a, b, c} = {1, 2, 3}}, a^2 + b] gives an error, but this is easily fixed by puttingBlock[{a}, ... ] around the right-hand side of myWith.
    – Heike
    Commented Dec 4, 2011 at 11:31
  • I saw it after posting the answer.
    – halirutan
    Commented Dec 4, 2011 at 11:38

Not the answer you're looking for? Browse other questions tagged or ask your own question.