# What is the correct usage of HoldFirst?

I encountered a situation where using HoldFirst seems to do the trick for the specific task, but I am uncertain about the proper use of this attribute.

Consider the following assignment:

f[x[t]] ^= D[x[t]^3, x[t]]


namely, the derivative of some function of x[t] (here, a simple polynomial) is associated with the up-values of symbol x.

Where would such a construct be useful?

One instance I can think of is when the expression on the rhs of ^= is dependent upon the quantity x[t] but we do not intend to treat f[x[t]] as a function but as a placeholder for future use of the expression involving x[t] - for lack of a better term, I'd call f[] a container for 'things' associated with x[t].

Now, consider the following line of code:

g[f[x[t]]] ^= Integrate[2 x[t], x[t]]


If you evaluate this expression above, the front-end will let you know that UpSet: Tag Times in g[3 x[t]^2] is Protected.

What this means is that you are not allowed to associate a value with the up-values of Times[].

(In case you are having trouble to understand that message, consider evaluating FullForm[f[x[t]]] and look at the head of that expression, hint: it's Times[]).

This time, Mathematica didn't allow us to use the container g[] in a similar way we used f[] above because associating the expressions our way would have disrupted the up-values of the system-defined symbol Times[].

There is a work-around this (there are probably more workarounds, but this is the one I could think of), namely the use of attribute HoldFirst[].

Evaluating something like

SetAttributes[g, HoldFirst]

g[f[x[t]]] ^= Integrate[2 x[t], x[t]]


allows us to achieve the intended behavior ie assign an up-value for f (I am not sure if I should call it a symbol or an expression since the context is f[x[t]]) and use g as a container for f[x[t]].

Having said all that, the question is if this is an appropriate use of HoldFirst.

---Edit (see below)---

Perhaps it would be helpful to understand better where this post is coming from if I explained the context in which the issue came up.

Consider two random variables $x(t)$ and $y(t)$ each with mean $μ_x$ and $μ_y$ and variance $σ_x^2$ and $σ_y^2$, respectively. Also, assume their co-variance is non-zero (or their correlation coefficient is different from zero-remember how $Cov(x,y)=σ_x σ_y ρ_{x,y}$).

It is straightforward to verify that a new random variable $z(t)$ defined as $z(t) = α x(t) - β (y_0 + y(t))$ where $α,β$ are positive scalars and $y_0$ a positive constant, would have a mean equal to $μ_z=α μ_x-β (y_0+μ_y)$ and a variance equal to $σ_z=α^2 σ_x^2+βσ_y^2-2αβσ_x σ_y ρ_{x,y}$ (obviously, $ρ_{x,y}$ is the correlation coefficient).

Consider now the optimization problem of minimizing the variance of variable $z(t)$ under the constraint that $μ_z\geqξ$, where $ξ$ is some positive constant:

$min_{_{\{α,β\}}}$ $σ_z^2$

s.t.: $μ_z\geqξ$

(non-negativity constraints are left out for brevity).

Now, in this context the Lagrangian, $\mathfrak L$, is a functional of the variance of $z(t)$ or $\mathfrak L\equiv\mathfrak L(σ^2_z)$ which in turn is functionally dependent on the variances and co-variance etc of variables $x(t),y(t)$.

What I was trying to achieve with the container analogy in the body of the question was to replicate transparently the functional dependence of the Lagrangian on the variance on the one hand, and be able to perform operations on the 'container' in order to solve the optimization problem, on the other.

• This is definitely a non-standard way to use HoldFirst. Generally you set it on a function where the first argument should be held so you can manipulate it itself, rather than its value. Think pass-by-reference. The more common thing would be to wrap g[f[x[t]]] in HoldPattern. This is also less-likely to produce down-stream oddities. – b3m2a1 Aug 4 '17 at 0:59