This question is closely related to the answer here.
I'm working in a system of logic which has the usual semantics for building Wffs out of terms and atomic formulas. However, in the interest of 'naturalness' I have defined wffs so as to forbid double quantification, that is Wffs of the form
$$ ((\forall x) (P_x \land ((\forall x)Q_x))) $$ My Wff formation rule for quantificiation reads:
If $\mathcal{A} \equiv ((\forall x) \mathcal{B})$ with $x$ a variable and $\mathcal{B}$ a Wff satisfying $x$ appears in $\mathcal{B}$ and $(\forall x)$ does not appear in $\mathcal{B}$ then $\mathcal{A}$ is a Wff
This definition also forbids redundant quantifciation. I don't think there's anything wrong with this definition on its own.
The trouble comes when I try to define Wff substitution, or substitutability. My rule for substitution of a Wff pretty much says that if $\mathcal{X}$ is an atomic formula, $\mathcal{A}$ is a Wff, and $\mathcal{P}$ is a Wff then $\mathcal{A}[\mathcal{P}/\mathcal{X}]$ just results by replacing any instance of $\mathcal{X}$ and dropping in $\mathcal{P}$. On its own, given my syntax for quantification, it is not gauranteed that $\mathcal{A}[\mathcal{P}/\mathcal{X}]$ is a Wff.
However, I should be able to define a concept of substitutability to cover this. Let $S(\mathcal{A}, \mathcal{P}, \mathcal{X})$ stand for $\mathcal{P}$ is substitable for $\mathcal{X}$ in $\mathcal{A}$. My first condition on substitutability would be that $\mathcal{P}$ has the same free variables as $\mathcal{X}$. This ensures no redundant quantification occurs after substitution and it also avoids any unintential variable capture. However, this condition does not prevent double quantification.
My question is how to define substitutability of a Wff given my rule forbidding double quantificiation? Non-rigorously, it is clear that the rule should be something like "If, within $\mathcal{A}$, $\mathcal{X}$ is within the scope of any quantifier over variable $x$ and $(\forall x)$ appears in $\mathcal{P}$ then $\mathcal{P}$ is not substitutable for $\mathcal{X}$ in $\mathcal{A}$." but I don't know how to make this definition rigorous.
With reference to the linked answer, it is not too difficult, using induction, looking downwards to determine which variables are quantified over within a Wff $\mathcal{P}$, but it seems tricker to determine, looking upwards, which quantifiers of which $\mathcal{X}$ is inside the scope.
Can such a definition of substitability be easily given in this case?