Initial values in Manipulate are hijacked by existing global variable values

Is the following behaviour a bug? It's the same in all versions between 9.0-10.2.0.

Let's try this simple Manipulate with the initial value of a set to 1:

Manipulate[Style[a, Large], {{a, 1}, 0, 2}, SaveDefinitions -> True]


Now let us do the same, but set a value to the global a first:

a = 0.5;

Manipulate[Style[a, Large], {{a, 1}, 0, 2}, SaveDefinitions -> True]


The initial value of the local a (set to 1 in the Manipulate) is hijacked by the global value of a. It now shows as 0.5 instead of 1.

This only happens with SaveDefinitions -> True.

Is this a bug or is it expected behaviour?

Analysis: As Michael points out in the comments, the SaveDefintions option causes a=0.5 to be recorded in the Initialization option. The Manipulate evaluates to this InputForm:

Manipulate[Style[a, Large], {{a, 0.5}, 0, 2}, Initialization :> {a = 0.5}]


Normally SaveDefinitions -> True is supposed to record every symbol that's used in the Manipulate, but apparently it neglects to filter out those that are used as parameters.

So is it reasonable to record the global value of a? In some cases it might be. Consider

a = 1;
f[] := a

Manipulate[f[] + a, {a, 0, 1}]


Is this Manipulate going to show a + 1 as a goes from 0..1, or is it going to show a + a?

It shows a + 1, as one would expect based on the fact that the parameter is local to the Manipulate and shouldn't conflict with global variables. But the function f depends on the global a, so if we want to preserve f, we also need to preserve the global a. Thus on first sight it seems reasonable that SaveDefinitions should save the global a (but not that that should influence the local value).

Now let us see what actually happens if we try it:

Manipulate[f[] + a, {a, 0, 1}, SaveDefinitions -> True]


Well, it seems SaveDefinitions changed the behaviour completely. Now it shows a+a instead of a+1 ... I would definitely call this a bug!

• Seems like a bug, in which the symbol a in the code triggers a search for a symbol Globala with a value (perhaps) that is packaged into the Initialization option. Absolutely no reason I see that such behavior is appropriate here. – Michael E2 Jul 25 '15 at 16:22
• Same behavior in V8.0.4. – Michael E2 Jul 25 '15 at 16:24
• @MichaelE2 It seems we found another, related problem: Will a=1; f[] := a; Manipulate[f[] + a, {a,0,1}] show the value of a + a or 1 + a? It depends on whether we use SaveDefinitions -> True! – Szabolcs Jul 25 '15 at 16:40
• Yikes, it redefines f (with SaveDefinitions -> True)! Try pasting several outputs into your notebook. The function f is redefined each time to represent the local a` in the last pasted Manipulate -- for all the other Manipulates! – Michael E2 Jul 25 '15 at 17:01
• We've seen this all before (32004 and my related piece on StackOverflow). Isn't this a duplicate or a corollary? – Sjoerd C. de Vries Jul 25 '15 at 22:38