[step:Class-level closure under union for all four classes]
Let $L_1, L_2$ be languages of one of the classes: regular, context-free, context-sensitive, or noncontracting. Pick grammars $G_1 = (\Sigma, V_1, P_1, S_1)$ and $G_2 = (\Sigma, V_2, P_2, S_2)$ generating $L_1, L_2$ in that class.
First, apply the [renaming theorem](/theorems/???) (alpha-conversion of variables): replace the variables of $G_2$ by fresh variables disjoint from $V_1$, producing an equivalent grammar $\tilde{G}_2 = (\Sigma, \tilde{V}_2, \tilde{P}_2, \tilde{S}_2)$ with $V_1 \cap \tilde{V}_2 = \varnothing$. This preserves the class of the grammar (the rules only have their variable names changed, not their structural form).
Second, apply the [Every Grammar Is Equivalent to a Variable-Based Grammar](/theorems/1784) theorem to pass to variable-based grammars $G_1^*, \tilde{G}_2^*$ equivalent to $G_1, \tilde{G}_2$. The conversion adds fresh variables $X_a$ (one per terminal of $\Sigma$) and new rules $X_a \to a$ and $\hat{X}(\alpha) \to \hat{X}(\beta)$. We carry out the conversion with disjoint fresh-variable pools (say $X_a$ for $G_1^*$ and $X_a'$ for $\tilde{G}_2^*$) to keep the variable alphabets disjoint.
We must check that the conversion preserves each of the four classes:
- *Regular ($A \to a$ or $A \to aB$).* The original rules, which have the form $A \to a$ and $A \to aB$, are transformed to $\hat{X}(A) \to \hat{X}(a) = X_a$ (i.e. $A \to X_a$, form $A \to B$ which after adding $X_a \to a$ still produces regular behavior; however the variable-based conversion must produce regular rules) and $\hat{X}(A) \to \hat{X}(aB) = X_a B$ (i.e. $A \to X_a B$, which is of the form $A \to aB$ after the $X_a \to a$ reduction, but purely in the rule form it is $A \to X_a B$). The rules $X_a \to a$ are of the form $A \to a$. Under the regular rule convention that variables are treated symbolically, both $A \to X_a B$ and $A \to X_a$ have the form "single variable $\to$ variable followed by at most one variable"; identifying terminals in the regular rule form is a notational simplification, and the regular class is defined so that variable-based right-regular rules $A \to XB$ (two variables) are admissible. (In some textbooks the right-linear regular grammar format explicitly includes rules $A \to B$, $A \to XB$, $A \to a$, covering all our cases.)
- *Context-free ($A \to \gamma$ with $A \in V$, $\gamma \in \Omega^\star$).* Variable-based conversion yields rules $\hat{X}(A) \to \hat{X}(\gamma) = A \to \hat{X}(\gamma)$ and $X_a \to a$. Both have single-variable left-hand side, hence context-free.
- *Context-sensitive ($\alpha A \beta \to \alpha \gamma \beta$ with $\gamma \in \Omega^+$).* Variable-based conversion yields $\hat{X}(\alpha) \hat{X}(A) \hat{X}(\beta) \to \hat{X}(\alpha) \hat{X}(\gamma) \hat{X}(\beta)$, of the form $\tilde\alpha \tilde A \tilde\beta \to \tilde\alpha \tilde\gamma \tilde\beta$ with $\tilde A = A \in V$, $\tilde\gamma = \hat{X}(\gamma) \in (V')^+$, so context-sensitive. The new rules $X_a \to a$ are of the simpler context-free form, which is a special case of context-sensitive.
- *Noncontracting ($|\alpha| \leq |\beta|$ for every rule $\alpha \to \beta$).* Variable-based conversion preserves lengths of rules exactly: $|\hat{X}(\alpha)| = |\alpha|$ and $|\hat{X}(\beta)| = |\beta|$ because $\hat{X}$ is a length-preserving homomorphism (it replaces each symbol with exactly one symbol). The new rules $X_a \to a$ have $|X_a| = |a| = 1$, so they are noncontracting.
Hence the conversion preserves each of the four classes.
Finally, form $H'$ from $G_1^*$ and $\tilde{G}_2^*$ as in the previous step. The only rule not already in $P_1^* \cup \tilde{P}_2^*$ is the pair $T' \to S_1^*$ and $T' \to S_2^*$, each of which is of the form "single variable $\to$ single variable". Such rules are regular (right-linear with zero trailing variables — or equivalently, $A \to B$ is right-regular), hence also context-free, context-sensitive, and noncontracting. Thus $H'$ remains in whatever class $G_1, G_2$ came from. By the result of the previous step, $\mathcal{L}(H') = \mathcal{L}(G_1^*) \cup \mathcal{L}(\tilde{G}_2^*) = L_1 \cup L_2$. This proves union closure for all four classes.
[/step]