hcju/stacksqa huggingface dataset:
Choose a resolution $\ldots \to F_2 \to F_1 \to F_0 \to M \to 0$ with $F_i$ finite free (Lemma \ref{lemma-resolution-by-finite-free}). By Lemma \ref{lemma-what-kind-of-resolutions} we see that $P_d = \Ker(F_{d - 1} \to F_{d - 2})$ is projective at least if $d \geq 2$. Then $P_d$ is a finite $R$-module as $R$ is Noetherian and $P_d \subset F_{d - 1}$ which is finite free. Whence $0 \to P_d \to F_{d - 1} \to \ldots \to F_1 \to F_0 \to M \to 0$ is the desired resolution.
Assume (1). Choose a free resolution $F_\bullet \to M$ of $M$. Denote $d_e : F_e \to F_{e - 1}$. By Lemma \ref{lemma-independent-resolution} we see that $P_e = \Ker(d_e)$ is projective for $e \geq n - 1$. This implies that $F_e \cong P_e \oplus P_{e - 1}$ for $e \geq n$ where $d_e$ maps the summand $P_{e - 1}$ isomorphically to $P_{e - 1}$ in $F_{e - 1}$. Hence, for any $R$-module $N$ the complex $\Hom_R(F_\bullet, N)$ is split exact in degrees $\geq n + 1$. Whence (2) holds. The implication (2) $\Rightarrow$ (3) is trivial. \medskip\noindent Assume (3) holds. If $n = 0$ then $M$ is projective by Lemma \ref{lemma-characterize-projective} and we see that (1) holds. If $n > 0$ choose a free $R$-module $F$ and a surjection $F \to M$ with kernel $K$. By Lemma \ref{lemma-reverse-long-exact-seq-ext} and the vanishing of $\Ext_R^i(F, N)$ for all $i > 0$ by part (1) we see that $\Ext_R^n(K, N) = 0$ for all $R$-modules $N$. Hence by induction we see that $K$ has projective dimension $\leq n - 1$. Then $M$ has projective dimension $\leq n$ as any finite projective resolution of $K$ gives a projective resolution of length one more for $M$ by adding $F$ to the front.
We will prove this by induction on $n$. \medskip\noindent Base case: $n = 0$. Then $P_e = M_e/\bigcup_{e' < e} M_{e'}$ is projective. Thus we may choose a section $P_e \to M_e$ of the projection $M_e \to P_e$. We claim that the induced map $\psi : \bigoplus_{e \in E} P_e \to M$ is an isomorphism. Namely, if $x = \sum x_e \in \bigoplus P_e$ is nonzero, then we let $e_{max}$ be maximal such that $x_{e_{max}}$ is nonzero and we conclude that $y = \psi(x) = \psi(\sum x_e)$ is nonzero because $y \in M_{e_{max}}$ has nonzero image $x_{e_{max}}$ in $P_{e_{max}}$. On the other hand, let $y \in M$. Then $y \in M_e$ for some $e$. We show that $y \in \Im(\psi)$ by transfinite induction on $e$. Let $x_e \in P_e$ be the image of $y$. Then $y - \psi(x_e) \in \bigcup_{e' < e} M_{e'}$. By induction hypothesis we conclude that $y - \psi(x_e) \in \Im(\psi)$ hence $y \in \Im(\psi)$. Thus the claim is true and $\psi$ is an isomorphism. We conclude that $M$ is projective as a direct sum of projectives, see Lemma \ref{lemma-direct-sum-projective}. \medskip\noindent If $n > 0$, then for $e \in E$ we denote $F_e$ the free $R$-module on the set of elements of $M_e$. Then we have a system of short exact sequences $$ 0 \to K_e \to F_e \to M_e \to 0 $$ over the well-ordered set $E$. Note that the transition maps $F_{e'} \to F_e$ and $K_{e'} \to K_e$ are injective too. Set $F = \bigcup F_e$ and $K = \bigcup K_e$. Then $$ 0 \to K_e/\bigcup\nolimits_{e' < e} K_{e'} \to F_e/\bigcup\nolimits_{e' < e} F_{e'} \to M_e/\bigcup\nolimits_{e' < e} M_{e'} \to 0 $$ is a short exact sequence of $R$-modules too and $F_e/\bigcup_{e' < e} F_{e'}$ is the free $R$-module on the set of elements in $M_e$ which are not contained in $\bigcup_{e' < e} M_{e'}$. Hence by Lemma \ref{lemma-exact-sequence-projective-dimension} we see that the projective dimension of $K_e/\bigcup_{e' < e} K_{e'}$ is at most $n - 1$. By induction we conclude that $K$ has projective dimension at most $n - 1$. Whence $M$ has projective dimension at most $n$ and we win.
It is clear that (1) $\Rightarrow$ (2) and (2) $\Rightarrow$ (3). Assume (3). Choose a set $E \subset M$ of generators of $M$. Choose a well ordering on $E$. For $e \in E$ denote $M_e$ the submodule of $M$ generated by the elements $e' \in E$ with $e' \leq e$. Then $M = \bigcup_{e \in E} M_e$. Note that for each $e \in E$ the quotient $$ M_e/\bigcup\nolimits_{e' < e} M_{e'} $$ is either zero or generated by one element, hence has projective dimension $\leq n$ by (3). By Lemma \ref{lemma-colimit-projective-dimension} this means that $M$ has projective dimension $\leq n$.
Let $0 \to P_n \to P_{n - 1} \to \ldots \to P_0 \to M \to 0$ be a projective resolution. As localization is exact, see Proposition \ref{proposition-localization-exact}, and as each $S^{-1}P_i$ is a projective $S^{-1}R$-module, see Lemma \ref{lemma-ascend-properties-modules}, we see that $0 \to S^{-1}P_n \to \ldots \to S^{-1}P_0 \to S^{-1}M \to 0$ is a projective resolution of $S^{-1}M$. This proves (1). Let $M'$ be an $S^{-1}R$-module. Note that $M' = S^{-1}M'$. Hence we see that (2) follows from (1).
We saw, Lemma \ref{lemma-localize-finite-gl-dim} that if $R$ has finite global dimension $n$, then all the localizations $R_{\mathfrak m}$ have finite global dimension at most $n$. Conversely, suppose that all the $R_{\mathfrak m}$ have global dimension $\leq n$. Let $M$ be a finite $R$-module. Let $0 \to K_n \to F_{n-1} \to \ldots \to F_0 \to M \to 0$ be a resolution with $F_i$ finite free. Then $K_n$ is a finite $R$-module. According to Lemma \ref{lemma-independent-resolution} and the assumption all the modules $K_n \otimes_R R_{\mathfrak m}$ are projective. Hence by Lemma \ref{lemma-finite-projective} the module $K_n$ is finite projective.
Let $F_{\bullet}$ be a finite resolution of $\kappa$ by finite free $R$-modules (Lemma \ref{lemma-what-kind-of-resolutions-Noetherian-local}). By Lemma \ref{lemma-add-trivial-complex} we may assume all the maps in the complex $F_{\bullet}$ have to property that $\Im(F_i \to F_{i-1}) \subset \mathfrak m F_{i-1}$, because removing a trivial summand from the resolution can at worst shorten the resolution. Say $F_n \not = 0$ and $F_i = 0$ for $i > n$, so that the projective dimension of $\kappa$ is $n$. By Proposition \ref{proposition-what-exact} we see that $\text{depth}_{I(\varphi_n)}(R) \geq n$ since $I(\varphi_n)$ cannot equal $R$ by our choice of the complex. Thus by Lemma \ref{lemma-bound-depth} also $\dim(R) \geq n$.
By Propositions \ref{proposition-finite-gl-dim-regular} and \ref{proposition-regular-finite-gl-dim} we see that a Noetherian local ring is a regular local ring if and only if it has finite global dimension. Furthermore, any localization $R_{\mathfrak p}$ has finite global dimension, see Lemma \ref{lemma-localize-finite-gl-dim}, and hence is a regular local ring.
Let $\mathfrak m \subset R$ be the maximal ideal and let $\kappa = R/\mathfrak m$ be the residue field. Let $d = \dim S$. Choose any resolution $F_\bullet \to \kappa$ with each $F_i$ a finite free $R$-module. Set $K_d = \Ker(F_{d - 1} \to F_{d - 2})$. By flatness of $R \to S$ the complex $0 \to K_d \otimes_R S \to F_{d - 1} \otimes_R S \to \ldots \to F_0 \otimes_R S \to \kappa \otimes_R S \to 0$ is still exact. Because the global dimension of $S$ is $d$, see Proposition \ref{proposition-regular-finite-gl-dim}, we see that $K_d \otimes_R S$ is a finite free $S$-module (see also Lemma \ref{lemma-independent-resolution}). By Lemma \ref{lemma-finite-projective-descends} we see that $K_d$ is a finite free $R$-module. Hence $\kappa$ has finite projective dimension and $R$ is regular by Proposition \ref{proposition-finite-gl-dim-regular}.
Assume going up. Take any chain $\mathfrak p_0 \subset \mathfrak p_1 \subset \ldots \subset \mathfrak p_e$ of prime ideals in $R$. By surjectivity we may choose a prime $\mathfrak q_0$ mapping to $\mathfrak p_0$. By going up we may extend this to a chain of length $e$ of primes $\mathfrak q_i$ lying over $\mathfrak p_i$. Thus $\dim(S) \geq \dim(R)$. The case of going down is exactly the same. See also Topology, Lemma \ref{topology-lemma-dimension-specializations-lift} for a purely topological version.
We use the characterization of dimension of Proposition \ref{proposition-dimension}. Let $x_1, \ldots, x_d$ be elements of $\mathfrak p$ generating an ideal of definition of $R_{\mathfrak p}$ with $d = \dim(R_{\mathfrak p})$. Let $y_1, \ldots, y_e$ be elements of $\mathfrak q$ generating an ideal of definition of $S_{\mathfrak q}/\mathfrak pS_{\mathfrak q}$ with $e = \dim(S_{\mathfrak q}/\mathfrak pS_{\mathfrak q})$. It is clear that $S_{\mathfrak q}/(x_1, \ldots, x_d, y_1, \ldots, y_e)$ has a nilpotent maximal ideal. Hence $x_1, \ldots, x_d, y_1, \ldots, y_e$ generate an ideal of definition of $S_{\mathfrak q}$.
By Lemma \ref{lemma-dimension-base-fibre-total} we have an inequality $\dim(S_{\mathfrak q}) \leq \dim(R_{\mathfrak p}) + \dim(S_{\mathfrak q}/\mathfrak pS_{\mathfrak q})$. To get equality, choose a chain of primes $\mathfrak pS \subset \mathfrak q_0 \subset \mathfrak q_1 \subset \ldots \subset \mathfrak q_d = \mathfrak q$ with $d = \dim(S_{\mathfrak q}/\mathfrak pS_{\mathfrak q})$. On the other hand, choose a chain of primes $\mathfrak p_0 \subset \mathfrak p_1 \subset \ldots \subset \mathfrak p_e = \mathfrak p$ with $e = \dim(R_{\mathfrak p})$. By the going down theorem we may choose $\mathfrak q_{-1} \subset \mathfrak q_0$ lying over $\mathfrak p_{e-1}$. And then we may choose $\mathfrak q_{-2} \subset \mathfrak q_{e-1}$ lying over $\mathfrak p_{e-2}$. Inductively we keep going until we get a chain $\mathfrak q_{-e} \subset \ldots \subset \mathfrak q_d$ of length $e + d$.
By Lemma \ref{lemma-dimension-base-fibre-equals-total} we have $\dim(S) = \dim(R) + \dim(S/\mathfrak m_RS)$. Pick generators $x_1, \ldots, x_d \in \mathfrak m_R$ with $d = \dim(R)$, and pick $y_1, \ldots, y_e \in \mathfrak m_S$ which generate the maximal ideal of $S/\mathfrak m_RS$ with $e = \dim(S/\mathfrak m_RS)$. Then we see that $x_1, \ldots, x_d, y_1, \ldots, y_e$ are elements which generate the maximal ideal of $S$ and $e + d = \dim(S)$.
Let $x_1, \ldots, x_d \in \mathfrak m_R$ be a regular sequence of length $d = \dim(R)$. By Lemma \ref{lemma-flat-increases-depth} this maps to a regular sequence in $S$. Hence $S$ is Cohen-Macaulay if $\dim(S) \leq d$. This is true if $S$ is finite flat over $R$ by Lemma \ref{lemma-integral-sub-dim-equal}. And in the second case we assumed it.
Suppose that $R \subset S' \subset S$ is a finitely generated $R$-subalgebra of $S$. In this case set $\mathfrak q' = S' \cap \mathfrak q$. The lemma for the ring maps $R \to S'$ and $S' \to S$ implies the lemma for $R \to S$ by additivity of transcendence degree in towers of fields (Fields, Lemma \ref{fields-lemma-transcendence-degree-tower}). Hence we can use induction on the number of generators of $S$ over $R$ and reduce to the case where $S$ is generated by one element over $R$. \medskip\noindent Case I: $S = R[x]$ is a polynomial algebra over $R$. In this case we have $\text{trdeg}_R(S) = 1$. Also $R \to S$ is flat and hence $$ \dim(S_{\mathfrak q}) = \dim(R_{\mathfrak p}) + \dim(S_{\mathfrak q}/\mathfrak pS_{\mathfrak q}) $$ see Lemma \ref{lemma-dimension-base-fibre-equals-total}. Let $\mathfrak r = \mathfrak pS$. Then $\text{trdeg}_{\kappa(\mathfrak p)} \kappa(\mathfrak q) = 1$ is equivalent to $\mathfrak q = \mathfrak r$, and implies that $\dim(S_{\mathfrak q}/\mathfrak pS_{\mathfrak q}) = 0$. In the same vein $\text{trdeg}_{\kappa(\mathfrak p)} \kappa(\mathfrak q) = 0$ is equivalent to having a strict inclusion $\mathfrak r \subset \mathfrak q$, which implies that $\dim(S_{\mathfrak q}/\mathfrak pS_{\mathfrak q}) = 1$. Thus we are done with case I with equality in every instance. \medskip\noindent Case II: $S = R[x]/\mathfrak n$ with $\mathfrak n \not = 0$. In this case we have $\text{trdeg}_R(S) = 0$. Denote $\mathfrak q' \subset R[x]$ the prime corresponding to $\mathfrak q$. Thus we have $$ S_{\mathfrak q} = (R[x])_{\mathfrak q'}/\mathfrak n(R[x])_{\mathfrak q'} $$ By the previous case we have $\dim((R[x])_{\mathfrak q'}) = \dim(R_{\mathfrak p}) + 1 - \text{trdeg}_{\kappa(\mathfrak p)} \kappa(\mathfrak q)$. Since $\mathfrak n \not = 0$ we see that the dimension of $S_{\mathfrak q}$ decreases by at least one, see Lemma \ref{lemma-one-equation}, which proves the inequality of the lemma. To see the equality in case $R$ is universally catenary note that $\mathfrak n \subset R[x]$ is a height one prime as it corresponds to a nonzero prime in $F[x]$ where $F$ is the fraction field of $R$. Hence any maximal chain of primes in $S_\mathfrak q = R[x]_{\mathfrak q'}/\mathfrak nR[x]_{\mathfrak q'}$ corresponds to a maximal chain of primes with length 1 greater between $\mathfrak q'$ and $(0)$ in $R[x]$. If $R$ is universally catenary these all have the same length equal to the height of $\mathfrak q'$. This proves that $\dim(S_\mathfrak q) = \dim(R[x]_{\mathfrak q'}) - 1$ and this implies equality holds as desired.
By the dimension formula (Lemma \ref{lemma-dimension-formula}) for any prime $\mathfrak q$ lying over $\mathfrak p$ we have $$ \dim(B_{\mathfrak q}) \leq \dim(A_{\mathfrak p}) - \text{trdeg}_{\kappa(\mathfrak p)} \kappa(\mathfrak q). $$ As the domain $B_\mathfrak q$ has at least $2$ prime ideals we see that $\dim(B_{\mathfrak q}) \geq 1$. We conclude that $\dim(B_{\mathfrak q}) = 1$ and that the extension $\kappa(\mathfrak p) \subset \kappa(\mathfrak q)$ is algebraic. Hence $\mathfrak q$ defines a closed point of its fibre $\Spec(B \otimes_A \kappa(\mathfrak p))$, see Lemma \ref{lemma-finite-residue-extension-closed}. Since $B \otimes_A \kappa(\mathfrak p)$ is a Noetherian ring the fibre $\Spec(B \otimes_A \kappa(\mathfrak p))$ is a Noetherian topological space, see Lemma \ref{lemma-Noetherian-topology}. A Noetherian topological space consisting of closed points is finite, see for example Topology, Lemma \ref{topology-lemma-Noetherian}.
By the Hilbert Nullstellensatz (Theorem \ref{theorem-nullstellensatz}) we know the residue field $\kappa = \kappa(\mathfrak m)$ is a finite extension of $k$. Denote $\alpha_i \in \kappa$ the image of $x_i$. Denote $\kappa_i = k(\alpha_1, \ldots, \alpha_i) \subset \kappa$, $i = 1, \ldots, n$ and $\kappa_0 = k$. Note that $\kappa_i = k[\alpha_1, \ldots, \alpha_i]$ by field theory. Define inductively elements $f_i \in \mathfrak m \cap k[x_1, \ldots, x_i]$ as follows: Let $P_i(T) \in \kappa_{i-1}[T]$ be the monic minimal polynomial of $\alpha_i $ over $\kappa_{i-1}$. Let $Q_i(T) \in k[x_1, \ldots, x_{i-1}][T]$ be a monic lift of $P_i(T)$ (of the same degree). Set $f_i = Q_i(x_i)$. Note that if $d_i = \deg_T(P_i) = \deg_T(Q_i) = \deg_{x_i}(f_i)$ then $d_1d_2\ldots d_i = [\kappa_i : k]$ by Fields, Lemmas \ref{fields-lemma-multiplicativity-degrees} and \ref{fields-lemma-degree-minimal-polynomial}. \medskip\noindent We claim that for all $i = 0, 1, \ldots, n$ there is an isomorphism $$ \psi_i : k[x_1, \ldots, x_i] /(f_1, \ldots, f_i) \cong \kappa_i. $$ By construction the composition $k[x_1, \ldots, x_i] \to k[x_1, \ldots, x_n] \to \kappa$ is surjective onto $\kappa_i$ and $f_1, \ldots, f_i$ are in the kernel. This gives a surjective homomorphism. We prove $\psi_i$ is injective by induction. It is clear for $i = 0$. Given the statement for $i$ we prove it for $i + 1$. The ring extension $k[x_1, \ldots, x_i]/(f_1, \ldots, f_i) \to k[x_1, \ldots, x_{i + 1}]/(f_1, \ldots, f_{i + 1})$ is generated by $1$ element over a field and one irreducible equation. By elementary field theory $k[x_1, \ldots, x_{i + 1}]/(f_1, \ldots, f_{i + 1})$ is a field, and hence $\psi_i$ is injective. \medskip\noindent This implies that $\mathfrak m = (f_1, \ldots, f_n)$. Moreover, we also conclude that $$ k[x_1, \ldots, x_n]/(f_1, \ldots, f_i) \cong \kappa_i[x_{i + 1}, \ldots, x_n]. $$ Hence $(f_1, \ldots, f_i)$ is a prime ideal. Thus $$ (0) \subset (f_1) \subset (f_1, f_2) \subset \ldots \subset (f_1, \ldots, f_n) = \mathfrak m $$ is a chain of primes of length $n$. The lemma follows.
By Proposition \ref{proposition-finite-gl-dim-polynomial-ring} any local ring of $k[x_1, \ldots, x_n]$ is regular. Hence all local rings are Cohen-Macaulay, see Lemma \ref{lemma-regular-ring-CM}. The local rings at maximal ideals have dimension $n$ hence every maximal chain of primes in $k[x_1, \ldots, x_n]$ has length $n$, see Lemma \ref{lemma-maximal-chain-CM}. Hence every maximal chain of primes between $(0)$ and $\mathfrak p$ has length $\text{height}(\mathfrak p)$, see Lemma \ref{lemma-CM-dim-formula} for example. Putting these together leads to the assertion of the lemma.
Write $S = k[x_1, \ldots, x_n]/\mathfrak p$. By Proposition \ref{proposition-finite-gl-dim-polynomial-ring} and Lemma \ref{lemma-dimension-height-polynomial-ring} all the maximal chains of primes in $S$ (which necessarily end with a maximal ideal) have length $n - \text{height}(\mathfrak p)$. Thus this number is the dimension of $S$ and of $S_{\mathfrak m}$ for any maximal ideal $\mathfrak m$ of $S$.
Let $X = \bigcup_{i \in I} Z_i$ be the decomposition of $X$ into its irreducible components. There are finitely many of them (see Lemmas \ref{lemma-obvious-Noetherian} and \ref{lemma-Noetherian-topology}). Let $I' = \{i \mid x \in Z_i\}$, and let $T = \bigcup_{i \not \in I'} Z_i$. Then $U = X \setminus T$ is an open subset of $X$ containing the point $x$. The number (2) is $\max_{i \in I'} \dim(Z_i)$. For any open $W \subset U$ with $x \in W$ the irreducible components of $W$ are the irreducible sets $W_i = Z_i \cap W$ for $i \in I'$ and $x$ is contained in each of these. Note that each $W_i$, $i \in I'$ contains a closed point because $X$ is Jacobson, see Section \ref{section-ring-jacobson}. Since $W_i \subset Z_i$ we have $\dim(W_i) \leq \dim(Z_i)$. The existence of a closed point implies, via Lemma \ref{lemma-dimension-spell-it-out}, that there is a chain of irreducible closed subsets of length equal to $\dim(Z_i)$ in the open $W_i$. Thus $\dim(W_i) = \dim(Z_i)$ for any $i \in I'$. Hence $\dim(W)$ is equal to the number (2). This proves that (1) $ = $ (2). \medskip\noindent Let $\mathfrak m \supset \mathfrak p$ be any maximal ideal containing $\mathfrak p$. Let $x_0 \in X$ be the corresponding point. First of all, $x_0$ is contained in all the irreducible components $Z_i$, $i \in I'$. Let $\mathfrak q_i$ denote the minimal primes of $S$ corresponding to the irreducible components $Z_i$. For each $i$ such that $x_0 \in Z_i$ (which is equivalent to $\mathfrak m \supset \mathfrak q_i$) we have a surjection $$ S_{\mathfrak m} \longrightarrow S_\mathfrak m/\mathfrak q_i S_\mathfrak m =(S/\mathfrak q_i)_{\mathfrak m} $$ Moreover, the primes $\mathfrak q_i S_\mathfrak m$ so obtained exhaust the minimal primes of the Noetherian local ring $S_{\mathfrak m}$, see Lemma \ref{lemma-irreducible-components-containing-x}. We conclude, using Lemma \ref{lemma-dimension-spell-it-out}, that the dimension of $S_{\mathfrak m}$ is the maximum of the dimensions of the $Z_i$ passing through $x_0$. To finish the proof of the lemma it suffices to show that we can choose $x_0$ such that $x_0 \in Z_i \Rightarrow i \in I'$. Because $S$ is Jacobson (as we saw above) it is enough to show that $V(\mathfrak p) \setminus T$ (with $T$ as above) is nonempty. And this is clear since it contains the point $x$ (i.e. $\mathfrak p$).
The equivalence of the two statements follows from Lemma \ref{lemma-disjoint-implies-product}. Let $\mathfrak m \subset S$ be a maximal ideal. Every maximal chain of primes in $S_{\mathfrak m}$ has the same length equal to $\dim(S_{\mathfrak m})$, see Lemma \ref{lemma-maximal-chain-CM}. Hence, the dimension of the irreducible components passing through the point corresponding to $\mathfrak m$ all have dimension equal to $\dim(S_{\mathfrak m})$, see Lemma \ref{lemma-dimension-spell-it-out}. Since $\Spec(S)$ is a Jacobson topological space the intersection of any two irreducible components of it contains a closed point if nonempty, see Lemmas \ref{lemma-finite-type-field-Jacobson} and \ref{lemma-jacobson}. Thus we have shown that any two irreducible components that meet have the same dimension. The lemma follows easily from this, and the fact that $\Spec(S)$ has a finite number of irreducible components (see Lemmas \ref{lemma-obvious-Noetherian} and \ref{lemma-Noetherian-topology}).
Write $g = \sum_{\nu \in N} a_\nu x^\nu$ with $a_\nu \in R$ not zero. Here $N$ is a finite set of multi-indices as in Lemma \ref{lemma-helper} and $x^\nu = x_1^{\nu_1} \ldots x_n^{\nu_n}$. Note that the leading term in $$ (x_1 + x_n^{e_1})^{\nu_1} \ldots (x_{n-1} + x_n^{e_{n-1}})^{\nu_{n-1}} x_n^{\nu_n} \quad\text{is}\quad x_n^{e_1\nu_1 + \ldots + e_{n-1}\nu_{n-1} + \nu_n}. $$ Hence the lemma follows from Lemma \ref{lemma-helper} which guarantees that there is exactly one nonzero term $a_\nu x^\nu$ of $g$ which gives rise to the leading term of $g(x_1 + x_n^{e_1}, x_2 + x_n^{e_2}, \ldots, x_{n - 1} + x_n^{e_{n - 1}}, x_n)$, i.e., $a = a_\nu$ for the unique $\nu \in N$ such that $e \cdot \nu$ is maximal.
Pick $f \in I$, $f\not = 0$. It suffices to show the lemma for $k[x_1, \ldots, x_n]/(f)$ since $S$ is a quotient of that ring. We will take $y_i = x_i - x_n^{e_i}$, $i = 1, \ldots, n-1$ for suitable integers $e_i$. When does this work? It suffices to show that $\overline{x_n} \in k[x_1, \ldots, x_n]/(f)$ is integral over the ring $k[y_1, \ldots, y_{n-1}]$. The equation for $\overline{x_n}$ over this ring is $$ f(y_1 + x_n^{e_1}, \ldots, y_{n-1} + x_n^{e_{n-1}}, x_n) = 0. $$ Hence we are done if we can show there exists integers $e_i$ such that the leading coefficient with respect to $x_n$ of the equation above is a nonzero element of $k$. This can be achieved for example by choosing $e_1 \gg e_2 \gg \ldots \gg e_{n-1}$, see Lemma \ref{lemma-helper-polynomial}.
By induction on $n$, with $n = 0$ being trivial. If $I = 0$, then take $r = n$ and $y_i = x_i$. If $I \not = 0$, then choose $y_1, \ldots, y_{n-1}$ as in Lemma \ref{lemma-one-relation}. Let $S' \subset S$ be the subring generated by the images of the $y_i$. By induction we can choose $r$ and $z_1, \ldots, z_r \in k[y_1, \ldots, y_{n-1}]$ such that (a), (b) hold for $k[z_1, \ldots, z_r] \to S'$. Since $S' \to S$ is injective and finite we see (a), (b) hold for $k[z_1, \ldots, z_r] \to S$. The last assertion follows from Lemma \ref{lemma-integral-sub-dim-equal}.
By induction on $n$. The case $n = 0$ is clear. Assume $n > 0$. If $r = n$, then $\mathfrak q = (0)$ and the result is clear. Choose a nonzero $f \in \mathfrak q$. Of course $f$ is nonconstant. After applying an automorphism of the form $$ k[x_1, \ldots, x_n] \longrightarrow k[x_1, \ldots, x_n], \quad x_n \mapsto x_n, \quad x_i \mapsto x_i + x_n^{e_i}\ (i < n) $$ we may assume that $f$ is monic in $x_n$ over $k[x_1, \ldots, x_n]$, see Lemma \ref{lemma-helper-polynomial}. Hence the ring map $$ k[y_1, \ldots, y_n] \longrightarrow k[x_1, \ldots, x_n], \quad y_n \mapsto f, \quad y_i \mapsto x_i\ (i < n) $$ is finite. Moreover $y_n \in \mathfrak q \cap k[y_1, \ldots, y_n]$ by construction. Thus $\mathfrak q \cap k[y_1, \ldots, y_n] = \mathfrak pk[y_1, \ldots, y_n] + (y_n)$ where $\mathfrak p \subset k[y_1, \ldots, y_{n - 1}]$ is a prime ideal. Note that $\kappa(\mathfrak p) \subset \kappa(\mathfrak q)$ is finite, and hence $r = \text{trdeg}_k\ \kappa(\mathfrak p)$. Apply the induction hypothesis to the pair $(k[y_1, \ldots, y_{n - 1}], \mathfrak p)$ and we obtain a finite ring map $k[z_1, \ldots, z_{n - 1}] \to k[y_1, \ldots, y_{n - 1}]$ such that $\mathfrak p \cap k[z_1, \ldots, z_{n - 1}] = (z_{r + 1}, \ldots, z_{n - 1})$. We extend the ring map $k[z_1, \ldots, z_{n - 1}] \to k[y_1, \ldots, y_{n - 1}]$ to a ring map $k[z_1, \ldots, z_n] \to k[y_1, \ldots, y_n]$ by mapping $z_n$ to $y_n$. The composition of the ring maps $$ k[z_1, \ldots, z_n] \to k[y_1, \ldots, y_n] \to k[x_1, \ldots, x_n] $$ solves the problem.