With vs. Block scoping of parameters for Plot



A very basic question..

Why does Block work, but not With, to specify value of a parameter for a function to be plotted?


    f[x_] := c Cos[x]

    Block[{c = 2}, Plot[f[x], {x, 0, \[Pi]}]]

Function graph OK using "Block".[1]

      With[{c = 2}, Plot[Evaluate[f[x]], {x, 0, \[Pi]}]]
 (* no graph plotted *)

No function graph using "With".


Posted 2018-09-30T20:50:08.153

Reputation: 10 497

I do realize that I could make the parameter an additional argument to the function, that is, use f[x_, c_] := c Cos[x], but in the actual, much more complicated situation I face (namely, using ParametricPlot3D), that seems awkward. – murray – 2018-09-30T20:59:11.823


Have you seen this? Pay attention to what is called lexical scoping in Leonid's answer.

– Michael E2 – 2018-09-30T21:01:23.613

You may be interested in this great answer (also by Leonid )

– user1066 – 2018-09-30T21:03:23.170


By the time when With operates, Plot is still left unevaluated, which means that With only sees f[x] - it doesn't see c, which is why this doesn't work. Actually, yes, as the others pointed out, this is exact dupliate of this.

– Leonid Shifrin – 2018-09-30T21:04:36.763

Whereas With[{c = 2}, Plot[c Cos[x], {x, 0, Pi}]] does work. – Chris Degnen – 2018-09-30T21:06:31.053

Thanks for the references. I did realize that it was an issue of scoping, and I've never appreciated the distinction between lexical and dynamic scoping. The especially confusing thing to me is, as @ChrisDegnen notes, that With[{c = 2}, Plot[c Cos[x], {x, 0, Pi}]] does work. – murray – 2018-09-30T21:16:03.397

@Leonid Would you like to copy your response from SO so that we can have a canonical answer here on MSE as well? – WReach – 2018-10-01T14:27:09.257

@WReach Done as requested ) – Leonid Shifrin – 2018-10-01T17:37:00.987



Reproducing my old SO answer to a nearly identical question here, per request of WReach

Your question is not so much about Plot as it is about how the scoping constructs work. The main confusion here is due to the differences between lexical and dynamic scoping. And the main culprit is this definition:

f[x_] := c Cos[x]

The problem with it is that it makes f implicitly depend on the global symbol (variable) c. I am very much against this sort of constructs, since they lead to infinite confusion. Now, what happens can be illustrated with the following example:

In[55]:= With[{c = 2}, f[x]]

Out[55]= c Cos[x]

To understand why this happens, one must understand what the lexical scoping means. We know that With has a HoldAll attribute. The way it works is that it looks are what is literally inside it, and substitutes variables found literally in the body with their values from the declaration list. This happens during the variable-binding stage, and only then it lets the body to evaluate. From this, it is clear that the following will work:

In[56]:= With[{c = 2}, Evaluate[f[x]]]

Out[56]= 2 Cos[x]

This worked because Evaluate overrides the "part" of HoldAll attribute of With, forcing the body to evaluate before anything else (variable binding, and subsequent body evaluation). Therefore, it would be completely equivalent to use just With[{c = 2}, c Cos[x]] above, as you can see with Trace. The next part of the puzzle is why

With[{c = 2}, Plot[Evaluate @ f[x], {x, 0, Pi}, AxesOrigin -> {0, 0}]]

does not work. This is because this time we do not evaluate the body first. The presence of Evaluate affects only f[x] inside Plot, but not the evaluation of Plot itself inside With. This is illustrated by

In[59]:= With[{c = 2}, q[Evaluate @ f[x]]]

Out[59]= q[c Cos[x]]

Moreover, we do not want Plot to evaluate first, since then the value of c will not be defined. However, all that With sees is f[x], and since the parameter c is not literally present in there (lexical scoping, remember), no substitution will be made. Should we use Block here, and things will work, because Block uses dynamic scoping, meaning that it redefines values in time (part of the execution stack if you wish), rather than in place. Therefore, using Block[{c = 2}, ff[x]] where ff implicitly depends on c is (roughly) equivalent to c = 2; ff[x] (with the difference that c resumes its global value after the Block scope is left). So,

In[60]:= Block[{c = 2}, q[Evaluate @ f[x]]]

Out[60]= q[2 Cos[x]]

To make the With version work, you'd have to inject the expression for f[x] (r.h.s), for example like so:

In[63]:= Unevaluated[With[{c = 2}, q[f[x]]]] /. DownValues[f]

Out[63]= q[2 Cos[x]]

Note that this won't work:

In[62]:= With[{fx = f[x]}, With[{c = 2}, q[fx]]]

Out[62]= q[c Cos[x]]

But the reason here is quite subtle: while outer With evaluates before the inner one, it spots the variable name conflicts and renames its variables. Rules are much more disruptive, they don't respect inner scoping constructs.


If one insists on nested With-s, here is how one can fool the name conflict resolution mechanism of With and make it work:

In[69]:= With[{fx = f[x]}, With @@ Hold[{c = 2}, q[fx]]]

Out[69]= q[2 Cos[x]]

Since outer With can no longer detect the presence of inner With (using Apply[With, Hold[...]] makes the inner With effectively dynamically generated), it does not make any renamings, and then it works. This is a general trick to fool lexical scoping name resolution mechanism when you don't want renaming, although the necessity to use it usually indicates a bad design.


But I digressed. To summarize, making your second method work is quite hard and requires really weird constructs like

Unevaluated[With[{c = 2}, Plot[Evaluate@f[x], {x, 0, Pi},
            AxesOrigin -> {0, 0}]]] /. DownValues[f]


With[{fx = f[x]}, 
     With @@ Hold[{c = 2}, 
                  Plot[Evaluate @ fx, {x, 0, Pi}, AxesOrigin -> {0, 0}]]]

Once again: all this is because With must "see" the variables explicitly in the code, to make replacements. In contrast, Block does not need that, it replaces values dynamically at the moment of evaluation, based on their modified global values, as if you made assignments, this is why it works.

Now, the real culprit is your definition of f. You could have avoided all these troubles should you have defined your f with explicit parameter-passing:

ff[x_, c_] := c Cos[x]

Now, this works out of the box:

With[{c = 2}, 
   Plot[Evaluate @ ff[x, c], {x, 0, Pi}, AxesOrigin -> {0, 0}]]

because the parameters are explicitly present in the function call signature, and so are visible to With.

To summarize: what you observed is a consequence of the interplay between lexical and dynamic scoping. Lexical scoping constructs must "see" their variables explicitly in the code at the variable-binding stage (before evaluation), or they won't be effective. Dynamic scoping effectively modifies values of symbols, and is in this sense less demanding (the price you pay is that the code using lots of dynamic scoping is harder to understand, since it mixes state and behavior). The main reason for trouble is the function definition that makes implicit dependencies on global symbols (that are not in the formal parameter list of the function). It is best to avoid such constructs. It is still possible to make things work, but this is considerably more complicated (as was demonstrated above), and, at least for the case at hand, for no good reason.

Leonid Shifrin

Posted 2018-09-30T20:50:08.153

Reputation: 108 027

Leonid, I edited it a bit so that the example matches the OP. I hope you do not mind. – J. M.'s ennui – 2018-10-01T17:49:24.393

@J.M.issomewhatokay. Thanks. Surely I don't mind. – Leonid Shifrin – 2018-10-01T18:37:52.090