You need to make Animate
independent of f
. How do you do that?
Quick and dirty
A non-robust way, which works in this case, would be:
Module[{ff, xx},
ff = f[xx];
Animate[Plot[ff /. xx -> a - x, {x, -1, 1}], {a, 0, 2}]
]
This is unaffected by future redefinitions of f
, and works in this this case. Note, however, that this relies upon f[xx]
evaluating to something that is both (1) independent of future redefinitions* and (2) the same, upon replacement, as supplying f[a-x]
directly. In particular, this would fail if f
were initially defined as follows:
ClearAll[f]
(* f[xx] won't evaluate until xx is replaced with a real number *)
f[x_] /; x > 1 := x^2
f[x_] /; x < 1 := x
or
ClearAll[f]
(* f[xx] will evaluate differently than f[aRealNumber] *)
f[x_Real] := x^2
f[x_] := x
*Even here you could later do something silly like Unprotect[Power]; Power[x_,y__]:=x
to mess things up, though I think it's reasonable to assume you're not worried about that.
More robust
Module[{ff},
DownValues[ff] = (DownValues[f] /. f -> ff);
Animate[Plot[ff[a - x], {x, -1, 1}], {a, 0, 2}]
]
Here you've effectively copied the definition of f
into ff
.