\chapter{Algebraic Integers}
	\setcounter{page}{1}
	\pagenumbering{arabic}
	\section{Integral closure and algebraic integers}
		Here we present fundamental properties of algebraic integers and related theories. It is not our intention to do a treatise of commutative algebra and Galois theory, so for example there would not be the fundamental theorem of Galois theory, or the definition of Dedekind domain. However, if some theorems are explicitly useful in the future, you will still see the theorem and the proof.
		\subsection{Definition and examples}
			Problems in solving polynomial equations give rise to a lot of concepts in algebra and geometry. If we are specifically interested in $\mds{Z}$, we have the concept of \textbf{algebraic integers}.
			\begin{definition}
				A finite extension $K$ of the rational number $\mds{Q}$ is called a \textbf{number field}. The integral closure of $\mds{Z}$ in $K$ is called the ring of \textbf{algebraic integers} of $K$, and is denoted by $\OK$. To be precise, every element $x \in \OK$ is a zero of a monic polynomial $f \in \Z[X].$
			\end{definition}

			For this concept we have a lot of classic examples:

			\begin{example}
				If $K=\Q$, then $\OK$ is simply $\Z$. This is intuitive because suppose $x=a/b \in \Q$ is integral over $\Z$ where $(a,b)=1$, then
				\[
				x^n+c_1x^{n-1}+\cdots+c_n = 0
				\]
				where $c_i \in \Z$. Multiplying by $b^n$ yields
				\[
				a^n+c_1a^{n-1}b+\cdots+c_nb = 0
				\]
				Hence $b$ divides $a^n$. But we also have $(a^n,b)=1$, hence $b=\pm 1$, which is to say $x \in \Z$.

				There is an more general setting. Since $\Z$ is a unique factorial domain (UFD), and UFD is integrally closed \href{https://proofwiki.org/wiki/Unique_Factorization_Domain_is_Integrally_Closed}{[proof]}, we have $\Z=\OK$.
			\end{example}

			\begin{example}
				The Gaussian rational $\Q(i)=K$. Indeed it is natural to consider Gaussian integer $\Z[i]$ first. For any $z=m+ni \in \Z[i]$, we have
				\[
				z^2-2mz+m^2+n^2=0
				\]
				Hence $\Z[i] \subset \OK$. The converse is similar to our proof when $K=\Q$.
			\end{example}

			\begin{example}
				Quadratic field $K=\Q(\sqrt{d})$, where $d$ is a square-free integer $>1$. This time the algebraic integer ring is different from what you may have thought: $\OK = \Z[\omega]$ where
				\[
				\omega = \begin{cases}
					\frac{1+\sqrt{d}}{2}, &\quad d = 4k+1, \\
					\sqrt{d}, &\quad \text{otherwise}.
				\end{cases}
				\]
			\end{example}
			It turns out we are studying polynomials such as
			\begin{itemize}
				\item $x^2+1=0$.
				\item $x^2-d=0$.
			\end{itemize}
			It also turns out that many properties are not restricted to $\Z$, but to a specific class of rings. Hence we will investigate some properties in the sense of commutative ring theory. The next example deserves more discussion.

			\subsubsection{The Cyclotomic Fields}
				Let $\omega = e^{2\pi i/m}$. This is the $m$-th root of $1$. If $\lambda$ is a conjugate of $\omega$, i.e. they both are roots of the same irreducible polynomial over $\Q$, then $\lambda$ is also an $m$-th root of $1$ and is not an $n$-th root of $1$ whenever $n<m$. If we find these $\lambda$, we have a better understanding of $\Q(\omega)$, which is called the cyclotomic field. We will do this in a general setting.

				\begin{definition}
					Let $k$ be a field. By a \textbf{root of unity} in $k$ we shall mean an element $\xi \in k$ such that $\xi^n=1$ for some integer $n \ge 1$. Let $n$ be a integer $\ge 2$ and not divisible by the characteristic. The generator for the cyclic group $\bm{\mu}_n$ of $n$-th root of unity is called \textbf{primitive}.
				\end{definition}

				\begin{remark}
					We shall not ignore the characteristic of $k$. Indeed, if the characteristic is $p$, then the equation
					\[
						X^{p^m}-1=0
					\]
					has only one root, namely $1$, and hence there is no $p^m$-th root of unity except $1$.
				\end{remark}
				\begin{remark}
					$\bm{\mu}_n$ has $n$ elements. The derivative of $X^n-1$ is $nX^{n-1} \ne 0$, and the only root of the derivative is $0$, so there is no common root. In the algebraic closure of $k$, the polynomial $X^n-1$ has $n$ distinct roots, which forms $\bm{\mu}_n$.
				\end{remark}

				The field extension in terms of root of unity can always be characterised for $(\Z/n\Z)^\ast$ as follows:

				\begin{theorem}\label{gal-cyclotomic}
					Let $k$ be any field, let $n$ be not divisible by the characteristic $p$. Let $\omega$ be a primitive $n$-th root of unity in the algebraic closure $k^\mathrm{a}$, then $\gal(k(\omega)/k)$ is a subgroup of $(\Z/n\Z)^\ast$.
				\end{theorem}

				\begin{proof}
					Let $\sigma$ be an embedding of $k(\omega)$ in $k^\mathrm{a}$ over $k$. Then
					\[
						(\sigma\omega)^n = \sigma(\omega^n)=1
					\]
					hence $\sigma\omega$ is also an $n$-th root of unity. It follows that $\sigma\omega=\omega^i$ for some $i=i(\sigma)$ as $\omega$ is primitive. $i(\sigma)$ is uniquely determined mod $n$. It follows that $\sigma$ maps $k(\omega)$ into itself, and hence $k(\omega)$ is normal over $k$. If $\tau$ is another automorphism of $k(\omega)$ over $k$, then
					\[
						\sigma\tau\omega = \omega^{i(\sigma)i(\tau)}.
					\]
					Since $\sigma$ and $\tau$ are isomorphisms, it follows that $i(\sigma)$ and $i(\tau)$ are prime to $n$ (otherwise, $\sigma\omega$ would have a period smaller than $n$). This yields a homomorphism of $\gal(k(\omega)/k)$ and is clearly injective because $i(\sigma)$ is uniquely determined by $\sigma$ mod $n$, and the effect of $\sigma$ on $k(\omega)$ is determined by its effect on $\omega$.
				\end{proof}

				For a specific field $k$, the question arises whether the image of $\gal(K(\omega)/K)$ in $(\Z/n\Z)^\ast$ is all of $(\Z/n\Z)^\ast$. Looking at $k=\mathds{R}$ or $k=\mathds{C}$, this is not always the case. We give an example on when it is the case.
				\begin{theorem}
					Let $\omega$ be a primitive $n$-th root of unity in $\Q^\mathrm{a}$, then
					\[
						[\Q(\omega):\Q] = \varphi(n)
					\]
					where $\varphi$ is the Euler function.
				\end{theorem}
				\begin{proof}
					According to theorem \ref{gal-cyclotomic}, $[\Q(\omega):\Q] \le \varphi(n)$, hence it suffices to prove the opposite.

					Let $f(X)$ be the minimal polynomial of $\omega$ over $\Q$. Then $f(X)$ divides $X^n-1$, and we can write $X^n-1=f(X)h(X)$, where both $f$ and $h$ have leading coefficient $1$. By the Gauss lemma, $f$ and $h$ have integral coefficients. We shall now prove that if $p$ is a prime number not dividing $n$, then $\omega^p$ is also a root of $f$.

					Suppose $\phi^p$ is not a root of $f$, then it is a root of $h$, and $\phi$ is a root of $h(X^p)$. Hence $f$ divides $h(X^p)$ as well, so we write
					\[
						h(X^p)=f(X)g(X).
					\]
					Note $g$ has integral coefficients as well. Since $a^p \equiv a \mod p$ for any integer $a$, we have
					\[
						h(X^p) \equiv h(X)^p \mod p,
					\]
					and therefore
					\[
						h(X)^p \equiv f(X)g(X) \mod p.
					\]
					Let $\overline{f}$ and $\overline{h}$ be the canonical image of $f$ and $h$ in $\Z/p\Z[X]$, we see that $f$ and $h$ are not relatively prime, hence have common factor(s). But if follows that
					\[
						X^n-\overline{1}=\overline{f}(X)\overline{g}(X)
					\]
					has multiple rules, which contradicts the remark we have made at the beginning.

					Since $\omega^p$ is also a primitive $n$-th root of unity, and any primitive $n$-th root of unity can be obtained by raising $\omega$ to a succession of prime powers with primes not dividing $n$, this implies that all primitive $n$-th roots of unity are roots of $f$, which forces the degree of $f$ to be not less than $\varphi(n).$

				\end{proof}

				It follows immediately that

				\begin{corollary}
					We have an isomorphism
					\[
						\gal(\Q(\omega):\Q) \cong (\Z/n\Z)^\ast.
					\]
				\end{corollary}

				\begin{corollary}
					If $n,m$ are relative prime integers $\ge 1$, then
					\[
						\Q(\omega_n) \cap \Q(\omega_m) = \Q.
					\]
				\end{corollary}

				We will prove that the cyclotomic integers (i.e. the algebraic integer with respect to $\Q(\omega)/\Q$) is actually $\Z[\omega]$. This is a highly non-trivial result and we can only deliver the proof after further study on prime ideals.
				% TODO: Finish this section and the study of Z[\omega]. Source can be found on Daniel A. Marcus.
		\subsection{Algebraic extension and integral closure}
			First of all we show that being algebraic almost implies being integral.
			\begin{lemma}\label{alg-int}
				Let $A$ be a domain, $K$ its quotient field, and $x$ algebraic over $K$. Then there exists an element $c \ne 0$ of $A$ such that $cx$ is integral over $A$.
			\end{lemma}
			\begin{proof}
				Since $x$ is algebraic, we have an equation
				\[
					a_nx^n+\cdots+a_0=0
				\]
				with $a_i \in A$ and $a_n \ne 0$. Hence
				\[
					a_n^{n-1}(a_nx^n+\cdots+a_0)=(a_nx)^n+\cdots+a_0a_n^{n-1}=0
				\]
				which is to say $a_nx$ is integral over $A$.
			\end{proof}

			Since finite extensions are algebraic, we are always free to use this lemma for the topic of number field.

			\begin{theorem}\label{closure-f-g}
				Let $A$ be an integrally closed Noetherian ring. Let $L$ be a finite separable extension of its quotient field $K$. Then the integral closure of $A$ in $L$ is finitely generated over $A$.
			\end{theorem}

			By being integrally closed we mean the ring is integrally closed in its quotient field. Some mathematicians also say it is being normal, but I think \textit{normal} does not carry a lot of information.

			\begin{proof}
				Since $A$ is Noetherian, all submodules of a finitely generated module over $A$ is finitely generated. Hence it suffices to prove that the integral closure of $A$ is contained in a finitely generated $A$-module.\\
				Let $w_1,\dots,w_n$ be a basis of $L$ over $K$. After multiplying each $w_i$ by some suitable element of $A$ (see lemma \ref{alg-int}), we may assume without loss of generality that the $w_i$ are integral over $A$. To study the integral closure of $A$ in $L$, we pick an arbitrary element $z = b_1w_1+\cdots+b_nw_n$ and study its coefficients. \\
				Since $L/K$ is separable, the \href{https://stacks.math.columbia.edu/tag/0BIF}{field trace} form
				\[
					Q_{L/K}:L \times L \to K, \quad (x,y) \mapsto \tr_{L/K}(xy)
				\]
				is non-degenerate  \href{https://stacks.math.columbia.edu/tag/0BIL}{[proof]}, so we claim that $L^\ast$ is isomorphic to $L$ under $Q_{L/K}$. Indeed, one can define a $K$-linear map
				\[
					d:L \to L^\ast, \quad x \mapsto (y \mapsto Q_{L/K}(x,y)=\tr(xy)).
				\]
				This map is injective because $Q_{L/K}$ is non-degenerate. Since $L$ and $L^\ast$ has the same dimension, $d$ has to be surjective.\\
				Let $w^1,\dots,w^n$ be the dual basis of $w_1,\dots,w_n$. If we put $v_i=d^{-1}(w^i)$, we have
				\[
					\tr(v_i w_j) = \delta_{ij}.
				\]
				Let $c \ne 0$ be an element of $A$ such that $cv_i$ is integral over $A$, then $cv_iz$ is integral and so is $\tr(cv_iz)$. Since $\tr$ is a $K$-valued function, we have
				\[
					\tr(czv_i)=c\tr(v_iz)=c d(v_i)(z) = cb_i \in A \implies b_i \in Ac^{-1}.
				\]
				Hence
				\[
					z \in Ac^{-1}w_1+\cdots+Ac^{-1}w_n
				\]
				which is to say $z$ is finitely generated. Since $z$ is arbitrarily picked, the closure itself is contained in a finitely generated $A$-module, which finishes the proof.
			\end{proof}
			Note $Z$ is itself a Noetherian ring and integrally closed. $\Q$ is the fraction ring of $\Z$, and finite extensions of $\Q$ are always separable. It follows (non-trivially) that
			\begin{corollary}
				$\OK$ is finitely generated over $\Z$.
			\end{corollary}

			Next we study the rank of $\OK$ over $\Z$. Being finitely generated is not exactly what we want.

			\begin{theorem}
				Let $A$ be a principal ideal ring, and $L$ a finite separable extension of its quotient field $K$, of degree $n$. Let $B$ be the integral closure of $A$ in $L$. Then $B$ is a free module of rank $n$ over $A$.
			\end{theorem}

			\begin{proof}
				Since $A$ is contained in $K$, $B$ is contained in $L$, whenever $ab=0$ with $a \in A$, $b \in B$, we have $a=0$ or $b=0$. Hence $B$ is torsion-free. Therefore as a finitely generated (theorem \ref{closure-f-g}) torsion-free module, $B$ is a free module over $A$ \href{http://du.ac.in/du/uploads/departments/mathematics/study-material/MMATH18-201\%20_MT_PID.pdf}{[Theorem 2.7]}. Since $L$ is a $n$-dimensional vector space over $K$, for $y \in L$ we have
				\[
					y = c_1e_1+\cdots+c_ne_n
				\]
				where $e_1,\dots,e_n$ is a basis and $c_1,\cdots,c_n \in K$. When $y \in B$, we must have $c_1,\cdots,c_n \in A$, which is to say $B$ has rank $[L:K]=n$.
			\end{proof}
			Hence the rank of $\OK$ over $\Z$ is determined by $[K:\Q]$.

		\subsection{Localisation}

			\begin{theorem}\label{int-clo-loc}
				Let $A \subset B$ be rings, and $S$ a multiplicatively closed subset of $A$. If $B$ is integral over $A$, then $S^{-1}B$ is integrally closed in $S^{-1}A$. If $C$ is the integral closure of $A$ in $B$, then $S^{-1}C$ is the integral closure of $S^{-1}A$.
			\end{theorem}

			\begin{proof}
				First we assume $B$ is integral over $A$. Pick $x/s \in S^{-1}B$ with $x \in B$ and $s \in S$. By definition we have
				\[
					x^n + a_1x^{n-1}+\cdots + a_n = 0
				\]
				with $a_i \in A$. Multiplying by $(1/s)^n$ gives
				\[
					(x/s)^n + (a_1/s)(x/s)^{n-1}+\cdots+a_n/s^n = 0
				\]
				which shows that $x/s$ is integral over $S^{-1}A$. Hence the first statement is proved. \\
				Now we assume $C$ is the integral closure of $A$ in $B$. By the first statement we see $S^{-1}C$ is integral over $S^{-1}A$. Suppose $b/s \in S^{-1}B$ is integral over $S^{-1}A$, we have an equation of the form
				\[
					(b/s)^n+(a_1/s_1)(b/s)^{n-1}+\cdots+a_n/s_n=0.
				\]
				Multiplying by $(st)^n$ where $t=s_1\cdots s_n$ gives an equation of integral independence for $bt$ over $A$. Hence $bt \in C$. But $b/s = bt/st$, hence $b/s \in S^{-1}C$ and we are done.
			\end{proof}

			If $S$ happens to be a complement of a prime ideal $\mfk{p}$, we have a satisfying result

			\begin{corollary}\label{int-loc}
				If $B$ is integral over $A$, then $B_\mfk{p}$ is integral over $A_\mfk{p}$.
			\end{corollary}

			If $B$ is replaced by a field extension $L$ of the quotient field of $A$, and $C$ is replaced by the integral closure of $A$, we have the following corollary:

			\begin{corollary}
				If $B$ is the integral closure of $A$ in some field extension $L$ of the quotient field of $A$, then $S^{-1}B$ is the integral closure of $S^{-1}A$ in $L$.
			\end{corollary}

			Being integrally closed is also a local property. And we will use it to prove that the algebraic integers in the field $\Q(\omega)$ is $\Z[\omega]$.
			\begin{corollary}
				Let $A$ be an integral domain. Then the following are equivalent:
				\begin{enumerate}
					\item $A$ is integrally closed.
					\item $A_\mfk{p}$ is integrally closed for each prime ideal $\mfk{p}$.
					\item $A_\mfk{m}$ is integrally closed for each maximal ideal $\mfk{m}$.
				\end{enumerate}
			\end{corollary}

			\begin{proof}
				Let $K$ be the quotient field of $A$, let $C$ be the integral closure of $A$ in $K$, and let $f:A \to C$ be the canonical embedding. Then $A$ is integrally closed if and only if $f$ is surjective.

				On the other hand, by theorem \ref{int-clo-loc}, $A_\mfk{p}$ (respectively $A_\mfk{m}$) is integrally closed if and only if $f_\mfk{p}$ (respectively $f_\mfk{m}$) is surjective.

				However, a $A$-module homomorphism $\phi:M \to N$ being surjective is a local property \href{https://www.maths.usyd.edu.au/u/de/AGR/CommutativeAlgebra/pp600-610.pdf}{[proof]}. Therefore we have
				\[
					\begin{aligned}
						A \text{ is integrally closed } &\iff f \text{ is surjective } \\
														&\iff f_\mfk{p} \text{ is surjective } \iff A_\mfk{p} \text{ is integrally closed } \\
														&\iff f_\mfk{m} \text{ is integrally closed } \iff A_\mfk{m} \text{ is integrally closed.}
					\end{aligned}
				\]
			\end{proof}

			% TODO: ADD SOME EXAMPLES
		\subsection{Prime Ideals}
			By theorem \ref{closure-f-g}, $\OK$ is a finitely-generated $\Z$-module, hence is a Noetherian domain. By transitivity of integral closures, $\OK$ is integrally closed. We are now interested in the Krull dimension of $\OK$. To do this, we investigate more of the prime ideal with respect to integral closure.
			\begin{definition}
				Let $B$ be a ring containing a ring $A$. Let $\mfk{p}$ be a prime ideal of $A$ and $\mfk{P}$ a prime ideal of $B$. We say that $\mfk{P}$ \textbf{lies above} $\mfk{p}$ if $\mfk{P} \cap A = \mfk{p}$ and we then write $\mfk{P}|\mfk{p}$.
			\end{definition}

			If $\mfk{P}|\mfk{p}$, we have a commutative diagram:
			\[
			\begin{tikzcd}
				B \arrow[r, "\pi'"]               & B/\mathfrak{P}                  \\
				A \arrow[r, "\pi"] \arrow[u, "i"] & A/\mathfrak{p} \arrow[u, "i'"']
			\end{tikzcd}
			\]
			where $i$ and $i'$ are inclusions, $\pi$ and $\pi'$ are canonical homomorphisms. \\
			If $B$ is integral over $A$, then $B/\mfk{P}$ is integral over $A/\mfk{p}$, this is because of the following lemma if we take $\sigma$ to be $\pi$:
			\begin{lemma}
				Let $A \subset B$ be rings, and $\sigma:B \to C$ be a homomorphism. If $B$ is integral over $A$, then $\sigma(B)$ is integral over $\sigma(A)$.
			\end{lemma}
			\begin{proof}
				If $B$ is integral over $A$, then for any $x \in B$ there is an equation
				\[
				x^n + a_{n-1}x^{n-1}+\cdots+a_0 = 0.
				\]
				Therefore
				\[
				\begin{aligned}
					\sigma(x^n+a_{n-1}x^{n-1}+\cdots+a_0) &= \sigma(x^n)+\sigma(a_{n-1}x^{n-1})+\cdots+\sigma(a_0) \\
					&= \sigma(x)^n + \sigma(a_{n-1})\sigma(x)^{n-1}+\cdots+\sigma(\sigma) \\
					&= 0.
				\end{aligned}
				\]
				Hence $\sigma(x)$ is integral in $\sigma(A)$.
			\end{proof}

			We want to show that prime ideals of $\OK$ is maximal, and they should be corresponded to prime ideals in $\Z$, which is maximal. For this reason we show the existence of lying-above prime ideals.

			\begin{theorem}\label{lying-above}
				Let $A$ be a ring, $\mfk{p}$ a prime ideal, and $B \supset A$ integral over $A$. Then $\mfk{p}B \ne B$, and there exists a prime ideal $\mfk{P}$ of $B$ lying above $\mfk{p}$.
			\end{theorem}

			\begin{proof}
				We know that $B_\mfk{p}$ is integral over $A_\mfk{p}$ (corollary \ref{int-loc}) and that $A_\mfk{p}$ is local with maximal ideal $\mfk{m}_\mfk{p}=\mfk{p}A_\mfk{p}$. It follows that
				\[
				\mfk{p}B_\mfk{p}=\mfk{p}A_\mfk{p}B = \mfk{p}A_\mfk{p}B_\mfk{p}=\mfk{m}_\mfk{p}B_\mfk{p}.
				\]
				Hence it suffices to prove our assertion when $A$ is local. If $\mfk{p}B=B$, we have an equation
				\[
				1 = a_1b_1+\cdots+a_nb_n
				\]
				with $a_i \in \mfk{p}$ and $b_i \in B$. Let $B_0 = A[b_1,\cdots,b_n]$. Then $\mfk{p}B_0=B_0$ and $B_0$ is a finitely generated $A$-module. Hence by Nakayama's lemma, $B_0=0$, which is absurd.

				To prove the existence of $\mfk{P}$, consider the following commutative diagram:
				\[
				\begin{tikzcd}
					B \arrow[r]           & B_\mathfrak{p}           \\
					A \arrow[r] \arrow[u] & A_\mathfrak{p} \arrow[u]
				\end{tikzcd}
				\]
				where all arrows are natural inclusions. As is proved, $\mfk{m}_\mfk{p}B_\mfk{p} \ne B_\mfk{p}$. Hence $\mfk{m}_\mfk{p}B_\mfk{p}$ is contained in a maximal ideal $\mfk{M}$ of $\mfk{p}$, and therefore $\mfk{M} \cap A_\mfk{p}$ contains $\mfk{m}_\mfk{p}$. And we pick $\mfk{P}=\mfk{M} \cap B$. Then $\mfk{P}$ is a prime ideal of $B$, and taking intersection with $A$ going both ways around our diagram shows that $\mfk{M} \cap A = \mfk{p}$, so that
				\[
				\mfk{P} \cap A = \mfk{p},
				\]
				as was to be shown.
			\end{proof}

			Now we proceed to the crucial theorem to determine whether a prime lying above is maximal.

			\begin{theorem}\label{lie-above-maximal}
				Let $A$ be a subring of $B$, and assume $B$ is integral over $A$. Let $\mfk{P}$ be a prime ideal of $B$ lying over a prime ideal $\mfk{p}$ of $A$. Then $\mfk{P}$ is maximal $\iff$ $\mfk{p}$ is maximal.
			\end{theorem}

			\begin{proof}
				$\implies$: Note $B/\mfk{P}$ is a field and is integral over the ring $A/\mfk{p}$. Were $A/\mfk{p}$ not a field, there would be a non-trivial ideal $\mfk{m}$ of it, and $B/\mfk{P}$ would have a prime ideal $\mfk{M}$ lying above $\mfk{m}$, by theorem \ref{lying-above}. A contradiction. \\
				$\impliedby$: Note $A/\mfk{p}$ is a field. It suffices to prove that a ring $R$ which is integral over a field $k$ is a field. If $k$ is a field and non-zero $x \in R$ is integral over $k$, we have a minimal polynomial
				\[
				x^n+c_{n-1}y^{n-1}+\cdots+c_0=0
				\]
				with $c_i \in k$. Since $R$ is integral, we have $c_0 \ne 0$. We can clearly write
				\[
				x^{-1}=-c_0^{-1}(x^{n-1}+c_{n-1}y_{n-2}+\cdots+c_1) \in R,
				\]
				which is to say $R$ is integral, and the theorem is therefore proved.
			\end{proof}
			By using local properties, we can show the stability of prime ideals lying above:
			\begin{corollary}
				Let $A \subset B$ be rings, $B$ integral over $A$; Let $\mfk{P}$ and $\mfk{P}'$ be prime ideals of $B$ such that $\mfk{P} \subset \mfk{P}'$ and both $\mfk{P}$ and $\mfk{P}'$ lie above a prime ideal $\mfk{p}$ of $A$, then $\mfk{P}=\mfk{P}'$.
			\end{corollary}
			\begin{proof}
				By corollary \ref{int-loc}, $B_\mfk{p}$ is integral over $A_\mfk{p}$. Let $\mfk{m}$ be the extension of $\mfk{p}$ in $A_\mfk{p}$ and $\mfk{M},\mfk{M}'$ be the extensions of $\mfk{P}$ and $\mfk{P}'$ respectively in $B_\mfk{p}$. Then $\mfk{m}$ is the maximal ideal of $A_\mfk{p}$; $\mfk{M} \subset \mfk{M}'$, and $\mfk{M}$, $\mfk{M}'$ lies above $\mfk{m}$. Hence by theorem \ref{lie-above-maximal}, $\mfk{M}$ and $\mfk{M}'$ are both maximal, hence equal. This reduces to $\mfk{P}=\mfk{P}'$.
			\end{proof}
			If the context is exactly localisation, we have a finer result:
			\begin{theorem}\label{loc-corr}
				Let $A$ be a commutative ring, $S \subset A$ be a multiplicatively closed set. We have a $1-1$-correspondence of prime ideals $\mfk{p}$ do not intersect $S$ and prime ideals of $S^{-1}A$:
				\[
					\mfk{p} \mapsto S^{-1}\mfk{p}, \quad \mfk{P} \mapsto \mfk{P} \cap A.
				\]
			\end{theorem}
			\begin{proof}
				To prove that $S^{-1}\mfk{p}=\mfk{P}$ is prime, suppose that $(a/s)(b/t)=ab/st = p/u \in \mfk{P}$. By definition there exists some $r \in S$ such that $r(abu-stp)=0$. It follows that $rabu=rstp \in \mfk{p}$. But $r,u$ are not in $\mfk{p}$, hence we can only have $ab \in \mfk{p}$, which gives that $a \in \mfk{p}$ or $b \in \mfk{p}$, and as a result $a/s \in \mfk{P}$ or $b/t \in \mfk{P}$. Besides we have $\mfk{p} = \mfk{P} \cap A$. $\subset$ inclusion is clear, but for the converse inclusion, note that if $p/s=a/1 \in \mfk{P} \cap A$, we have $up=uas$ for some $u \in S$, which forces $a$ to be an element of $\mfk{p}$. (Note we implicitly used the fact that $\mfk{p}$ does not intersect $S$.)

				For the converse, first note that $\mfk{p}=\mfk{P} \cap A$ is indeed a prime ideal. For any $(a/1)(b/1) \in \mfk{p} \subset \mfk{P}$, we must have $a/1 \in \mfk{P}$ or $b/1 \in \mfk{P}$, but both $a/1$ and $b/1$ are in $A$. Also, $\mfk{p}$ does not intersect $S$ because if $s \in S \cap \mfk{p}$, then $(1/s)s=1\in \mfk{p}$, which is absurd. It remains to show that $\mfk{P} = S^{-1}\mfk{p}$. It is clear that $S^{-1}\mfk{p} \subset \mfk{P}$. For the converse, pick any $a/s \in \mfk{P}$, we have $a/1=(a/s)(s/1) \in \mfk{p}$, and therefore $a/s=a(1/s) \in S^{-1}\mfk{p}$.
			\end{proof}
			In particular, we can also study the localised ring.
			\begin{corollary}
				Let $\mfk{m}_\mfk{p}=\mfk{p}A_\mfk{p}$ be the maximal ideal of $A_\mfk{p}$. We have a canonical embedding
				\[
					A/\mfk{p} \hookrightarrow A_\mfk{p}/\mfk{m}_\mfk{p} \cong K(A/\mfk{p})
				\]
				where $K(B)$ is the field of fractions of $B$. In particular, if $\mfk{p}$ is maximal, one has an isomorphism for all $n \ge 1$:
				\[
					A/\mfk{p}^n \cong A_\mfk{p}/\mfk{m}_\mfk{p}^n.
				\]
			\end{corollary}
			\begin{proof}
				Since $\mfk{p} = \mfk{m}_\mfk{p} \cap A$, the canonical map
				\[
					a \mod \mfk{p} \mapsto a/1 \mod \mfk{m}_\mfk{p}
				\]
				is injective. $A_\mfk{p}/\mfk{m}_\mfk{p}$ can be identified as the field of fraction of $A/\mfk{p}$ because given any non-zero $a/1 \mod \mfk{m}_\mfk{p}$, we see $a \in A \setminus \mfk{p}$, hence $1/a \mod \mfk{m}_\mfk{p}$ is exactly the inverse of $a/1 \mod \mfk{m}_\mfk{p}$.

				Next we assume that $\mfk{p}$ is maximal and consider the canonical map
				\[
					\varphi:a \mod \mfk{p}^n \mapsto a/1 \mod \mfk{m}_\mfk{p}^n.
				\]
				If $n=1$, then both $A/\mfk{p}$ and $A_\mfk{p}/\mfk{m}_\mfk{p}$ are fields, and the field of fractions of a field is itself. To prove it for all $n \ge 0$, we need the fact that $s \mod \mfk{p}^n$ is a unit whenever $n \ge 0$ and $s \in A \setminus \mfk{p}$, which will be shown later as a lemma.

				$\varphi$ is injective because if $a/1 \in \mfk{m}_\mfk{p}^n$, i.e. $a/1 = b/s$ where $b \in \mfk{p}^n$ and $s \not \in \mfk{p}$, then there exits some $u \not\in \mfk{p}$ such that $uas = ub \in \mfk{p}^n$. Since both $ u \mod \mfk{p}^n$ and $s \mod \mfk{p}^n$ are units, we see $a \mod \mfk{p}^n$ is zero in $A/\mfk{p}^n$.

				$\varphi$ is surjective because given $a/s \in A_\mfk{p}$ with $a \in A$ and $s \not\in \mfk{p}$, there exists some $a' \in A$ such that $a \equiv a's \mod \mfk{p}^n$. Indeed, let $t \mod \mfk{p}^n$ be the inverse of $s \mod \mfk{p}^n$, we can put $a' = at$. Therefore $a/s \equiv a' \mod \mfk{p}^nA_\mfk{p}$ (by theorem \ref{loc-corr}), which is equivalent to say that $a/s \mod \mfk{m}_\mfk{p}^n$ lies in the image of $\varphi$.
			\end{proof}
			\begin{lemma}
				Let $A$ be a ring and $\mfk{p}$ is a maximal ideal. Then $\mfk{p}^n+sA=A$ for all $n \ge 1$ and $s \in A \setminus \mfk{p}$. Besides, this implies that $s \mod \mfk{p}^n$ is a unit in $A/\mfk{p}^n$.
				\end{lemma}
			\begin{proof}
				When $n=1$, note $\mfk{p} \subsetneqq \mfk{p}+sA$, while $\mfk{p}$ is maximal, we must have $\mfk{p}+sA=A$. When $n \ge 2$, suppose it holds for $n-1$, then we have
				\[
				A = \mfk{p}^{n-1}+sA \implies \mfk{p}=\mfk{p}A = \mfk{p}(\mfk{p}^{n-1}+sA) \subsetneqq \mfk{p}^n+sA,
				\]
				which forces $\mfk{p}^n+sA$ to be $A$ itself.

				Now we consider $A$-modules
				\[
				\mfk{p}^n+sA = A.
				\]
				By taking the quotient, we obtain
				\[
				\overline{s}A/\mfk{p}^n=A/\mfk{p}^n.
				\]
				It follows that $s \mod \mfk{p}^n$ is a unit in $A/\mfk{p}^n$.
			\end{proof}
			And now we are more than ready to prove that $\OK$ is Dedekind.

			\begin{theorem}\label{o_k-dedekind}
				Every prime ideal $\mfk{P}$ in $\OK$ is maximal. Hence $\OK$ is of Krull dimension $1$ and is therefore Dedekind.
			\end{theorem}

			\begin{proof}
				Note it suffices to prove that every prime ideal $\mfk{P}$ of $\OK$ lies above some prime ideal of $\Z$, since $\Z$ has Krull dimension $1$, and the proof follows from theorem \ref{lie-above-maximal}. All we need to do is to prove that $\mfk{P} \cap \Z$ is non-zero: since the inverse image of a prime ideal is prime, we are done. For each $x \in \mfk{P}$, we have a minimal polynomial $f \in \Z[X]$ such that
				\[
				f(x) = x^n+c_{n-1}x^{n-1}+\cdots+c_0=0
				\]
				with $c_i \in \Z$ and $c_0 \ne 0$. It follows that
				\[
				c_0=-(x^n+c_{n-1}x^{n-1}+\cdots+c_1x) \in \mfk{P} \cap \Z,
				\]
				which is to say $\mfk{P} \cap \Z$ is indeed non-zero. This concludes the proof.
			\end{proof}
			\begin{example}
				As a classic example, consider $K=\Q(\sqrt{-5})$ and $\OK = \Z[\sqrt{-5}]$. This ring is not a unique factorial domain because we have
				\[
				6 = 2 \cdot 3 = (1-\sqrt{-5}) \cdot (1+\sqrt{-5}).
				\]
				But if we view it in the sense of product of ideals, we still have some uniqueness. Let $\mfk{m}$ be the maximal ideal containing $6$, then
				\[
				\mfk{m} = (2,1-\sqrt{-5})(2,1+\sqrt{-5})
				\]
				is unique. Note two ideals on the right hand side are indeed maximal (hence prime) because
				\[
				\begin{aligned}
					\frac{\Z[\sqrt{-5}]}{(2,1-\sqrt{-5})} &\cong \frac{\Z[X]/(X^2+5)}{(2,1-X,X^2+5)/(X^2+5)} \\
					&\cong \frac{\Z[X]}{(2,1-X,X^2+5)} \cong \frac{\Z_2[X]}{(1-X,X^2-1)} \cong \frac{\Z_2[X]}{(1-X)} \cong \Z_2.
				\end{aligned}
				\]
				Likewise,
				\[
				\begin{aligned}
					\frac{\Z[\sqrt{-5}]}{(3,1+\sqrt{-5})} &\cong \frac{\Z[X]/(X^2+5)}{(3,1+X,X^2+5)/(X^2+5)} \\
					&\cong \frac{\Z[X]}{(3,1+X,X^2+5)} \cong \frac{\Z_3[X]}{(1+X,X^2-1)} \cong \frac{\Z_3[X]}{(1+X)} \cong \Z_3.
				\end{aligned}
				\]
			\end{example}
			We will see when a Dedekind domain is UFD in the future.
	\section{Galois extensions}
		\subsection{Special subgroups of the Galois group}
			In this subsection we study the behaviour of Galois group with respect to integral closure, which can of course help us study number field and algebraic integers if we are interested in the extension itself.

			If $K$ is a Galois extension of $\Q$, then the Galois group allows us to transform amongst prime ideals in a natural way. This is because of the following theorem.
			\[
				\begin{tikzcd}
					\mathfrak{P} \arrow[rr, "\exists \sigma \in G"] &                                    & \mathfrak{Q} \\
					& \mathfrak{p} \arrow[lu] \arrow[ru] &
				\end{tikzcd}
			\]
			\begin{theorem}\label{galois-lie-above}
				Let $A$ be a ring, integrally closed in its quotient field $K$. Let $L$ be a finite Galois extension of $K$ with group $G$. Let $\mfk{p}$ be a maximal ideal of $A$, and let $\mfk{P}$, $\mfk{Q}$ be prime ideals of the integral closure of $A$ in $L$ lying above $\mfk{p}$. Then there exists $\sigma \in G$ such that $\sigma\mfk{P} = \mfk{Q}$.
			\end{theorem}
			\begin{proof}
				Suppose that $\mfk{P}=\sigma\mfk{Q}$ for all $\sigma \in G$. By the Chinese remainder theorem, we have some $x \in B$ such that
				\[
					\begin{aligned}
						x &\equiv 0 \mod \mfk{P} \\
						x &\equiv 1 \mod \sigma\mfk{Q}, \quad \forall \sigma \in G.
					\end{aligned}
				\]
				Then the norm
				\[
					N_K^L(x) = \prod_{\sigma \in G}\sigma{x}
				\]
				lies in $B \cap K = A$ since $A$ is integrally closed, and lies in $\mfk{P} \cap A = \mfk{p}=\mfk{Q} \cap A \subset \mfk{Q}$. But we also have $\sigma{x} \not\in \mfk{Q}$ for all $\sigma \in G$, hence $N_K^L(x) \not \in \mfk{Q}$, a contradiction.
			\end{proof}
			If one localise, the consideration on whether a prime ideal is maximal is not required. Besides, if $A$ is of Krull dimension $1$, then one has no need to consider as well. Since we have shown that $\OK$ is a Dedekind domain, this theorem can be applied as well. Next we show the finiteness of prime ideals lying above.

			\begin{corollary}
				Let $A$ be an integrally closed domain whose field of fraction is $K$. Let $E$ be a finite separable extension of $K$, and $B$ the integral closure of $A$ in $E$. Let $\mfk{p}$ be a maximal ideal of $A$. Then there exists only a finite number of prime ideals of $B$ lying above $\mfk{p}$.
			\end{corollary}
			\begin{proof}
				 If $E$ is Galois over $K$, then by theorem \ref{galois-lie-above}, $\sigma\mfk{P}_1 = \mfk{P}_2$ for some $\sigma \in \gal(E/K)$. Suppose $\mfk{P}_1|\mfk{p}$, then the set of prime ideals lying above $\mfk{p}$ is contained in the set
				\[
					\{\mfk{Q} \subset B: \mfk{Q}=\sigma\mfk{P}_1,\sigma\in\gal(E/K)\},
				\]
				hence is finite because $\gal(E/K)$ is finite. If $E$ is not necessarily Galois, we can pick the smallest Galois extension $L/K$ containing $E$, which is a finite extension as well. Let $C$ be the integral closure of $A$ in $L$. Suppose $\mfk{P},\mfk{Q} \in \spec(B)$ are two distinct prime ideals lying above $\mfk{p}$, and $\mfk{P}',\mfk{Q}' \in \spec(C)$ lying above $\mfk{P}$ and $\mfk{Q}$ respectively. Note $\mfk{P}' \ne \mfk{Q}'$ because if not then $\mfk{P}=\mfk{Q}$, a contradiction. Therefore the distinct prime ideals of $B$ lying above $\mfk{p}$ are less than the distinct prime ideals of $C$ lying above $\mfk{p}$, which proves our assertion.
				\[
					\begin{tikzcd}
						\mathfrak{P}'          &                                    & \mathfrak{Q}'          & C                   \\
						\mathfrak{P} \arrow[u] &                                    & \mathfrak{Q} \arrow[u] & B \arrow[u, dashed] \\
						& \mathfrak{p} \arrow[lu] \arrow[ru] &                        & A \arrow[u, dashed]
					\end{tikzcd}
				\]
			\end{proof}
			\begin{example}\label{gauss-int-1}
				Now take $K=\Q(i)$, $\OK=\Z[i]$ and $\mfk{p}=(5)$. Since $\gal(K/\Q)=\{1,\sigma\}$ where $\sigma$ is the complex conjugation, there are at most two prime ideals lying above $\mfk{p}$. First of all $5\Z[i]$ is not prime because
				\[
					5+0i = (2-i)(2+i).
				\]
				Since $\Z[i]$ is a PID, we only need to consider Gaussian integers that divide $5$. Keeping in mind that $\Z[i]$ is also an Euclidean domain, we have two non-trivial solutions up to units in $\Z[i]$:
				\[
					5 = (2-i)(2+i).
				\]

				As ideals, we have $(2-i)=(1+2i)=(-2+i)$. Since $2-i$ is irreducible in $\Z[i]$, we have found a prime ideal $\mfk{P}=(2-i)$ lies above $\mfk{p}$, and $\mfk{P}'=\sigma\mfk{P}=(2+i)$ has to be the remaining one. Note we have also established the factorisation of $5\Z[i]$ in the Dedekind domain $\OK$.

			\end{example}

			This example also shows that the norm defined in theorem \ref{galois-lie-above} makes sense, because we have
			\[
			N_{\Q}^{K}(a+bi)=\prod_{\sigma \in \gal(K/\Q)}\sigma(a+bi) = (a+bi)(a-bi)=a^2+b^2.
			\]
			We will study norm extensively in the future. %TODO: add reference if it is needed.

			Let $A$ be an integrally closed ring with quotient field $K$, and $B$ its integral closure in a finite Galois extension $L$. Then firstly $\sigma{B}=B$ for all $\sigma \in \gal(L/K)$ (Proof: $\sigma{B} \subset L$ is integral over $\sigma{A}=A$, hence has to be $B$ itself). Automorphisms fixing base field give rise to Galois group, and now we are interested in automorphisms that also fix ideals.

			\subsubsection{Decomposition groups and fields}

				\begin{definition}
					Notations being above, let $\mfk{p}$ be a maximal ideal of $A$, $\mfk{P}$ maximal in $B$ lying above $\mfk{p}$. Then the subgroup
					\[
						G_\mfk{P} = \{\sigma \in \gal(L/K):\sigma\mfk{P}=\mfk{P}\}
					\]
					is called the \textbf{decomposition group} of $\mfk{P}$. Its fixed field will be denoted by $L^d$, and will be called the \textbf{decomposition field} of $\mfk{P}$.
				\end{definition}
				Two non-trivial examples are given, abelian and non-abelian. We will frequently return to these examples after new concepts are introduced.
				% TODO: expand details of these two examples.
				\begin{example}
					Consider $K = \Q(\sqrt{-1},\sqrt{2},\sqrt{5})$, whose Galois group is isomorphic to $\Z/2\Z \times \Z/2\Z \times \Z/2\Z$. The decomposition field of $(5)$ is
					\[
						K^d = \Q(\sqrt{-1},\sqrt{2}).
					\]
				\end{example}
				\begin{example}
					Consider $K = \Q(\sqrt[3]{19},\omega)$ where $\omega = e^{2\pi i/3}$. The Galois group is $S_3$. The decomposition fields of $(3)$ are
					\[
						\Q(\sqrt[3]{19}),\quad \Q(\omega\sqrt[3]{19}),\quad\Q(\omega^2\sqrt[3]{19}).
					\]
					At this point we can only compute the field manually but after introducing ramification index things are much easier.
				\end{example}

				It is natural to think whether a decomposition group is normal, when the Galois group is non-abelian. For this question we have an pretty elegant result.

				\begin{theorem}
					Notation still being above, the decomposition group of $\sigma\mfk{P}$ where $\sigma \in \gal(L/K)$ is $\sigma G_\mfk{P}\sigma^{-1}$, i.e.
					\[
						G_{\sigma\mfk{P}} = \sigma G_\mfk{P}\sigma^{-1}.
					\]
				\end{theorem}
				This theorem says, the Galois group acting on itself by conjugation yields all decomposition groups of primes of $B$ lying above a certain prime of $A$.
				\begin{proof}
					We can write $\gal(L/K) = \bigcup\sigma_jG_\mfk{P}$ as a coset decomposition. We claim this decomposition determines distinct prime ideals lying above $\mfk{p}$. Note
					\[
						\sigma\mfk{P}=\tau\mfk{P} \iff \tau^{-1}\sigma\mfk{P}=\mfk{P} \iff \tau^{-1}\sigma \in G_\mfk{P}
					\]
					which is equivalent to say $\tau$ and $\sigma$ lie in the same coset mod $G_\mfk{P}$.

					This claim actually proves this theorem. On one hand, pick $\lambda \in G_\mfk{P}$, then
					\[
						\sigma\lambda\mfk{P} = \sigma\lambda\sigma^{-1}\sigma\mfk{P} = \sigma\mfk{P}
					\]
					Hence $\sigma G_{\mfk{P}}\sigma^{-1} \subset G_{\sigma\mfk{P}}$. Note we have used the fact that $\sigma\lambda,\sigma \in \sigma G_\mfk{P}$.

					On the other hand, if $\lambda\sigma\mfk{P} = \sigma\mfk{P}$, we have $\lambda\sigma \in \sigma G_\mfk{P}$, hence $\lambda \in \sigma G_\mfk{P}\sigma^{-1}$, which is equivalent to say $G_{\sigma\mfk{P}} \subset \sigma G_\mfk{P} \sigma^{-1}$.
				\end{proof}

				As for the field, we have a pretty concrete result:

				\begin{theorem}
					Assume $L/K$ is Galois and finite. The field $L^d$ is the smallest $E$ subfield of $L$ containing $K$ such that $\mfk{P}$ is the only prime of $B$ lying above $\mfk{P} \cap E$.
				\end{theorem}
				\begin{proof}
					Let $E$ be the smallest subfield of $L$ satisfying the property above, and write $\mfk{q} = \mfk{P} \cap E$. By the fundamental theorem of Galois theory ($L/E$ is Galois) and theorem \ref{galois-lie-above}, prime ideals of $B$ lying above $\mfk{q}$ differs by an element of $\gal(L/E)$. But by assumption there is only one such prime $\mfk{P}$, hence $H \subset G_\mfk{P}$ and therefore $E \supset L^d$.

					On the other hand, let $B^d$ be the integral closure of $A$ in $L^d$ (which is Dedekind as well), and let $\mfk{Q}=\mfk{P} \cap B^d$. By theorem \ref{galois-lie-above}, $\mfk{P}$ is the only prime of $B$ lying above $\mfk{Q}$ in $B^d$. Hence $E \subset L^d$, which proves the theorem.
				\end{proof}

				We are not done yet: the result can be even much sharpener:

				\begin{theorem}\label{residue-iso}
					Notation being above, the canonical injection $\varphi:A/\mfk{p} \to B^d/\mfk{Q}$ is an isomorphism.
				\end{theorem}

				\begin{proof}
					First of all we clarify what we mean by canonical injection:
					\[
						\varphi:x + \mfk{p} \mapsto x + \mfk{Q}.
					\]

					This is indeed injective because if if $\varphi(x+\mfk{p})=\mfk{Q}$, we have $x \in \mfk{Q}$. This gives
					\[
						x \in \mfk{Q} \cap A = (\mfk{P} \cap A) \cap B^d = \mfk{p} \cap B^d = \mfk{p}.
					\]

					It remains to prove that $\varphi$ is surjective. Given $x + \mfk{Q} \in B^d/\mfk{Q}$, we need to find an element $z \in A$ such that $\varphi(z) = x + \mfk{Q}$.

					Pick $\sigma \in \gal(L/K) \setminus G_\mfk{P}$. Let
					\[
						\mfk{Q}_\sigma = \sigma^{-1}\mfk{P} \cap B^d.
					\]
					Then $\mfk{Q}_\sigma \ne \mfk{Q}$ (note $\mfk{P}$ is assumed to be maximal hence so are $\mfk{Q}$ and $\mfk{Q}_\sigma$). Let $x$ be an element of $B^d$. By Chinese remainder theorem, there exists an element $y \in B^d$ such that
					\[
						\begin{aligned}
							y &\equiv x \mod \mfk{Q}, \\
							y &\equiv 1 \mod \mfk{Q}_\sigma.
						\end{aligned}
					\]
					Hence in particular,
					\[
						\begin{aligned}
							y &\equiv x \mod \mfk{P}, \\
							y &\equiv 1 \mod \sigma^{-1}\mfk{P}.
						\end{aligned}
					\]
					The second congruence gives
					\[
						\sigma{y} \equiv 1 \mod \mfk{P}.
					\]
					Note this holds for all $\sigma \not \in G_\mfk{P}$. For $\lambda \in G_\mfk{P}$, we have $\sigma{y}=y \equiv x \mod \mfk{Q}$. Thus we obtain
					\[
						z=N_K^L(y) \equiv x \mod \mfk{P}.
					\]
					First of all $z \in K$ by definition of norm. Since $z$ is integral over $A$, it has to be an element in $A$. We also have % TODO: 'definition of norm' deserves more explanation.
					\[
						z \equiv x \mod \mfk{Q}
					\]
					because both $z$ and $x$ lie in $B^d$. Hence we obtain $\varphi(z) = x + \mfk{P}$ as we wanted.
				\end{proof}

				% TODO: examples.

			\subsubsection{Inertia groups and fields}
				We are concerned about the homomorphism induced by the decomposition group.
				\begin{theorem}
					Notation still being above, $\overline{B}=B/\mfk{P}$ is a normal extension of $\overline{A}=A/\mfk{p}$, and the map $\sigma \to \overline{\sigma}$ induces a homomorphism of $G_\mfk{P}$ onto $\gal(\overline{B}/\overline{A})$
				\end{theorem}
				\begin{proof}
					Any element of $\overline{B}$ can be written as $\overline{x}$ for some $x \in B$. Let $\overline{x}$ generate a separable subextension of $\overline{A}$, and let $f$ be the irreducible polynomial for $x$ over $K$. The coefficient of $f$ lie in $A$ because $x$ is integral over $A$, and all the roots of $f$ are integral over $A$. Thus
					\[
					f(X) = \prod_{i=1}^{m}(X-x_i)
					\]
					splits into linear factors in $B$. Since
					\[
					\bar{f}(X)=\prod_{i=1}^{m}(X-\overline{x}_i)
					\]
					and all the $\overline{x}_i$ lie in $\overline{B}$, it follows that $\bar{f}$ splits into linear factors in $\overline{B}$. We observe that $f(x)=0 \implies \bar{f}(x)=0$. Hence $\overline{B}$ is normal over $\overline{A}$, and
					\[
					[\overline{A}(\overline{x}):\overline{A}] \le [K(x):K] \le [L:K].
					\]
					This implies that the maximal separable subextension of $\overline{A}$ in $\overline{B}$ is of finite degree over $A$ by the primitive element theorem, and in fact is always bounded by $[L:K]$.

					It remains to prove that $\sigma \mapsto \overline{\sigma}$ is actually a surjective homomorphism. By theorem \ref{residue-iso}, it suffices to take $K=L^d$ and $\gal(L/K)=G_\mfk{P}$. Take a generator $\overline{x}$ of the maximal subextension of $\overline{B}$ over $\overline{A}$, for some $x \in B$. Let $f$ be the irreducible polynomial of $x$ over $K$. Any automorphism of $\overline{B}$ is determined by its effect on $\overline{x}$, and maps $\overline{x}$ to some root of $\bar{f}$. Suppose that $x=x_1$. Given any root $x_i$ of $f$, there exists an element $\sigma$ of $G_\mfk{P}$ such that $\sigma x = x_i$. Hence $\overline{\sigma}\overline{x}=\overline{x}_i$. Hence the automorphism of $\overline{B}$ over $\overline{A}$ is induced by elements of $G$ operate transitively on the root of $\bar{f}$. Hence they give us all automorphisms of the residue class field, and the proof is complete.
				\end{proof}
				This enables us to work on algebraic closure with some ease:
				\begin{corollary}
					Let $\phi:A \to A/\mfk{p}$ be the canonical homomorphism, and let $\psi_1$, $\psi_2$ be two homomorphisms of $B$ extending $\varphi$ in a given algebraic closure of $A/\mfk{p}$. Then there exists an automorphism $\sigma$ of $L$ over $K$ such that
					\[
						\psi_1 = \psi_2 \circ \sigma
					\]
				\end{corollary}
				\begin{proof}
					The kernels of $\psi_1$ and $\psi_2$ are prime ideals of $B$ and according to theorem \ref{galois-lie-above} they differ by an automorphism. Hence there exists $\tau \in \gal(L/K)$ such that $\psi_1$ and $\psi_2$ have the same kernel $\mfk{P}$. Hence there exists an automorphism $\omega$ of $\psi_1(B)$ onto $\psi_2(B)$ such that $\omega \circ \psi_1 = \psi_2$. There exists an element $\sigma$ of $G_\mfk{P}$ such that $\omega \circ \psi_1 = \psi_1 \circ \sigma$, which proves what we want.
				\end{proof}

				\begin{definition}
					Let $\overline{G}_\mfk{P}$ be the automorphism group of $B/\mfk{P}$. The kernel of the map
					\[
						G_\mfk{P} \to \overline{G}_\mfk{P}
					\]
					is called the inertia group. That is, these elements induce the trivial automorphism in $B/\mfk{P}$.
				\end{definition}
		\subsection{Automorphisms}
	\section{Dedekind domain}

		\subsection{Operations of ideals}
		% TODO: Daniel Chapter 3.
		\subsection{Ramification index}

		\subsection{The norm and trace}
			We used the concept of norm and trace at the very beginning. Here is a good chance to study them extensively. Recall for $\mathds{C}$ and $\mathds{R}$ we have a natural faithful representation
			\[
				\begin{aligned}
					\rho:\mathds{C} &\to End(\mathds{R}^2) \\
							a+bi   &\mapsto \begin{pmatrix} a & -b \\ b & a \end{pmatrix}.
				\end{aligned}
			\]
			Note $|\rho(a+bi)|=a^2+b^2$, which is pretty close to the norm in analysis. It is not a good idea to take square root in the sense of algebra so we directly use the determinant. We will also be using trace of such matrices.
			\begin{definition}
				Let $E$ be a finite extension of $k$, which we view as a finite dimensional vector space over $k$. Each $\alpha \in E$ induces a linear map by multiplication:
				\[
					\begin{aligned}
						m_\alpha : E &\to E \\
								x &\mapsto \alpha{x}.
					\end{aligned}
				\]
				We define the norm and trace from $E$ to $k$ by
				\[
					\det(m_\alpha)=N_k^E(\alpha), \quad \tr(m_\alpha)=\tr_k^E(\alpha).
				\]
			\end{definition}

			Note this has nothing to do with norm in the sense of topology. The reason is, the norm lies in $k$, which is an arbitrary field. And a field alone has nothing to do with real numbers. Besides, consider the group field $\Q(\sqrt{2})/\Q$. In this field, the we have the matrix representation of $1+\sqrt{2}$ to be
			\[
				1+\sqrt{2} \mapsto \begin{pmatrix} 1 & 2 \\ 1 & 1 \end{pmatrix}
			\]
			whose determinant is $-1$. It makes little sense to compare this to norm in topology.

			% TODO: formula of norm and trace.
			% TODO: further properties.
		\subsection{Discrete valuation rings}
			A \textbf{discrete valuation ring} can be considered as a localisation of Dedekind domain. Indeed, if $A$ is a discrete valuation ring, then $A$ is Noetherian and of Krull dimension $1$, and is integrally closed, hence Dedekind. If $A$ is local and Dedekind, then $A$ is a discrete valuation ring. In general, a Noetherian domain $A$ of Krull dimension one is Dedekind if and only if the localisation $A_\mfk{p}$ is a discrete valuation ring for all prime $\mfk{p}$. With respect to localisation we have a natural result:
			\begin{theorem}
				Let $A$ be a Dedekind ring and $M,N$ two modules over $A$. If $M_\mfk{p} \subset N_\mfk{p}$ for all prime $\mfk{p}$, then $M \subset N$.
			\end{theorem}
			\begin{proof}
				Let $a \in M$. For each $\mfk{p}$ we can find $x_\mfk{p} \in N$ and $s_\mfk{p} \in A \setminus \mfk{p}$ such that $a = x_\mfk{p}/s_\mfk{p}$. Let $\mfk{b}$ be the ideal generated by the $s_\mfk{p}$, ranging through all $\mfk{p} \in \spec(A)$. Then $\mfk{b}$ is the unit ideal $A$, and we can write
				\[
					1 = \sum_{\mfk{p} \in \spec(A)} y_\mfk{p}s_\mfk{p}
				\]
				with elements $y_\mfk{p} \in A$ all but a finite number of which are $0$. This yields
				\[
					a = \sum_{\mfk{p} \in \spec(A)} y_\mfk{p}s_\mfk{p}a = \sum_{\mfk{p} \in \spec(A)} y_\mfk{p}x_\mfk{p} \in N
				\]
				as desired.
			\end{proof}

			Now we study torsion-free modules over a discrete valuation ring. If $A$ is a discrete valuation ring, then in particular, $A$ is a principal ideal ring, and any finitely generated torsion-free module $M$ over $A$ is free. If its rank is $n$, and if $\mfk{p}$ is the maximal idea, then $M/\mfk{p}M$ is a free module of rank $n$. Further, we have
			\begin{theorem}
				Let $A$ be a local ring and $M$ a free module of rank $n$ over $A$. Let $\mfk{p}$ be the maximal ideal of $A$. Then $M/\mfk{p}M$ is a vector space of dimension $n$ over $A/\mfk{p}$.
			\end{theorem}
			\begin{proof}
				Let $\{x_1,\dots,x_n\}$ be a basis of $M$ over $A$, then
				\[
					M \cong \bigoplus_{i}Ax_i
				\]
				and
				\[
					M/\mfk{p}M \cong \bigoplus_{i}(A/\mfk{p})\overline{x}_i
				\]
				where $\overline{x}_i$ is the residue class of $x_i$ mod $\mfk{p}$.
			\end{proof}


\chapter{Solution design}
\label {solutiondesign}

The executor contains of two parts: the main node and the agents. The agent runs on the created VMs, manages starting / updating / removing of the docker containers, executing of the distributed plan and provides information about current allocation and docker inspect output. The main node manages executing of the monolithic plan, and also provides current allocation and docker inspect output. The main node provides the allocation by sending requests to each known agent and aggregating answers.

\section{Problems}
\subsection{Bin packing problem}
  The monolithic approach given current allocation and the monolithic plan (resource demand for each tier) should do creation / deletion of VMs and allocation containers on them. This problem is similar to well-known Bin packing problem. A VM for us is a bin and a container is an object which we need to pack in the bin. The container has two dimensions: CPU cores and RAM, so this is 2D bin packing problem. The main difference to bin packing problem is that we have initial allocation (initial packing) and we should not only minimize number of bins used, but also number of movements required to reach the final allocation from the initial one.

  The linear optimisation problem was formulated to solve this 2d bin packing problem:

\begin{equation*}
\text{minimize } \displaystyle\sum\limits_{i \in I}^{}\sum\limits_{j \in J}^{} (w_{0ij} \alpha_{0ij} + w_{1ij} \alpha_{1ij}) + \sum\limits_{i \in I}^{} (w_{2i} \beta_{0i} + w_{3i} \beta_{1i})
\end{equation*}
\begin{equation*}
\begin{array}{ll@{}ll}
\text{subject to} & & &\\
\text{(1)}           & \displaystyle\sum\limits_{j \in J}^{} x_{ij} \leq m_{0i} \beta_{0i}  & & \forall i \in I\\
\text{(2)}           & \displaystyle\sum\limits_{j \in J}^{} y_{ij} \leq m_{1i} \beta_{0i} & & \forall i \in I\\
\text{(3)}           & \displaystyle  x_{ij} \leq m_{0i} \alpha_{ij} & & \forall i \in I, j \in J\\
\text{(4)}           & \displaystyle y_{ij} \leq m_{1i} \alpha_{ij} & & \forall i \in I, j \in J\\
\text{(5)}           & \displaystyle\sum\limits_{i \in I}^{} x_{ij} = d_{0j} & & \forall j \in J\\
\text{(6)}           & \displaystyle\sum\limits_{i \in I}^{} y_{ij} = d_{1j} & & \forall j \in J\\
\text{(7)}           & \displaystyle m_{1i} x_{ij} \geq y_{ij} & & \forall i \in I, j \in J\\
\text{(8)}           & \displaystyle m_{0i} y_{ij} \geq x_{ij} & & \forall i \in I, j \in J\\
\text{(9)}           & \displaystyle \alpha_{0ij} + \alpha_{1ij} = 1 & & \forall i \in I, j \in J\\
\text{(10)}          & \displaystyle \beta_{0i} + \beta_{1i} = 1 & & \forall i \in I\\
                     & \alpha_{0ij} \in \{0, 1\}  & & \forall i \in I, j \in J\\
                     & \alpha_{1ij} \in \{0, 1\}  & & \forall i \in I, j \in J\\
                     & \beta_{0i} \in \{0, 1\}  & & \forall i \in I\\
                     & \beta_{1i} \in \{0, 1\}  & & \forall i \in I\\
                     & x_{ij} \in \mathbb{Z}  & x_{ij} \geq 0 & \forall i \in I, j \in J\\
                     & y_{ij} \in \mathbb{Z}  & y_{ij} \geq 0 & \forall i \in I, j \in J\\
\end{array}
\end{equation*}
where \\
\indent $I$ is the set of VMs,\\
\indent $J$ is the set of Tiers,
\begin{sloppypar} $\alpha_{0ij}$ is "tier\_is\_used" binary variable that is true if we allocate container for tier[j] on VM[i],
\indent $\alpha_{1ij}$ is "tier\_is\_idle" binary variable that is true only if $\alpha_{0ij}$ is false, the equation (9) links them together,
\indent $\beta_{0i}$ is "vm\_is\_used" binary variable that is true if we allocate any container for any tier on VM[i],
\indent $\beta_{1i}$ is "vm\_is\_idle" binary variable that is true only if $\beta_{0i}$ is false, the equation (10) links them together,
\indent $x_{ij}$ is "cpu\_usage" variable: the number of CPU cores that tier[j] uses on VM[i],
\indent $y_{ij}$ is "mem\_usage" variable: "the number of RAM units (1 unit = 512Mb) that tier[j] uses on VM[i],
\indent (1) is CPU availability constraint and constraint for activation of "vm\_is\_used" variable, constant $m_{0i}$ is the number of CPU cores on VM[i] (maximum allowed value for $x_{ij}$),
\indent (2) is the same as (1), but for the RAM units, constant $m_{1i}$ is the number of RAM units on VM[i] (maximum allowed value for $y_{ij}$)
\indent (3) and (4) are activation of $\alpha_{ij}$ constraints similar to (1) and (2). (3) is for CPU cores, (4) is for RAM units,
\indent (6) is the RAM units demand equation, where $d_{1j}$ is the RAM units demand from the plan for the tier[j],
\indent (5) is the same as (4), but for the CPU cores, $d_{0j}$ is the CPU cores demand from the plan for the tier[j],
\indent (7) says that if tier[j] uses some RAM on VM[i], then it must use some CPU cores,
\indent (8) is the same as constraint (7), but other way round: if tier[j] uses some CPU cores on VM[i], then it must use some RAM units.
\end{sloppypar}

This formulation of the problem requires us to know the number of VMs beforehand, which we do not know. So the upper bound number of VMs is calculated and provided to the ILP solver. VMs that are used in the initial allocation are already created, but there are also VMs that are empty and are only going to be created if there will be some container on it.

Considering the initial allocation is done by constants $w_{0ij}, w_{1ij}, w_{2i}, w_{3i}$.
Where $w_{0ij}$ is the constant for using tier[j] on VM[i], so knowing initial allocation we can set this constant to the cost of the container creation if there is no tier[j] on VM[i] in the initial allocation, and the cost of the container update if there is tier[j] on VM[i] in the initial allocation. Here is a pitfall that if in the initial allocation container was used on the VM, and we use it in the new allocation, we can not differentiate if the value of CPU cores and RAM units used has changed (we need to run container update command) or is not changed (we need do nothing).

The weight constant $w_{1ij}$ is the cost of removing container of the tier[j] from the VM[i], this weight should be 0 if the tier[j] was not on the VM[i] in the initial allocation.

The weight $w_{2i}$ is the cost of VM[i] creation. if VM[i] is already used in the initial allocation this constant is 0.

The weight $w_{3i}$ is the cost of using the VM[i]. This cost is 0, if
the VM is not created yet, and it was not used in the initial allocation.

Considering NFR-2, the weights for actions should follow these rules:
\begin{enumerate}
    \item container delete \textless{ } container update \textless{ } container create
    \item VM delete \textless{ } VM use \textless{ } VM create
    \item VM delete + container delete  + container create \textless{ } \\ VM use + container update
\end{enumerate}

 \begin{sloppypar} The rule number 3 is introduced, because it is possible a situation when for example we can remove some VM[p], this VM had in the initial allocation 1 container. The weight of vm\_usage=30, the weight of vm\_deletion=25, the weight of container\_update=10, the weight of container\_deletion=5, the weight of container\_create=15. So to continue to use this VM the objective function will have $vm\_usage + container\_update=30 + 10 = 40$, and to remove this container in the worst case we need to create container in another place: $vm\_deletion + container\_deletion + container\_create = 25 + 5 + 15 = 45$. So ILP minimizing the objective function will choose not to remove the VM[p], but keep it. This is against our requirement NFR-0 and we want to keep the minimum number of VMs possible.
 \end{sloppypar}

 Also NFR-2 requires weight of "container use" be less than "container update", but I could not found the easy way to differentiate them in ILP. As we have initial allocation as constants, for example $c_{ij}$ is CPU cores used by container[j] on VM[i] and $m_{ij}$ is the same for RAM units. The new allocation in the ILP are variables $x_{ij}$ and $y_{ij}$ for CPU cores and RAM units accordingly. So to differentiate "container use" from "container update", we need either $x_{ij} + y_{ij} \neq c_{ij} + m_{ij}$ or $abs(x_{ij} + y_{ij} - c_{ij} - m_{ij}) > 0$ or some "if" constraint that all makes our ILP non linear. The effective workaround to solve this issue is considered out of scope of this paper.

\subsection{Scalability}
The tier can be scalable or not. The not scalable tier can have only 1 or 0 containers. If the tier is absent in the plan, it will have 0 containers, otherwise it will be 1, whilst the scalable tier can have any number of containers $\geq 0$ allocated on different VMs. In the ILP formulation not scalable containers are not considered, so the current workaround is too

\subsection{Limitation of the ILP formulation}
This ILP formulation has some limitations:
\begin{enumerate}
    \item This formulation of the problem requires us to know the number of VMs beforehand, which we do not know. But we can make upper-bound estimation.
    \item This formulation does not differentiate between "container update" and "container use" (do nothing with container).
    \item Weights should be chosen wisely to satisfy NFR-0.
    \item Bin packing problem is NP-hard
    \item We have 1 optimal solution and can not choose between several different solutions
\end{enumerate}

\subsection{Hooks}
Hooks are scripts that trigger on the tier / container scaling. The goal is to adjust the application's configuration after the allocation is changed.

We tried to extract and describe use cases of adjusting required:
\begin{enumerate}
    \item Jboss needs to adapt the number of threads considering the number of CPU allocated.
    \item LoadBalancer needs to be provided with the list of Jboss containers and resources allocated to adapt it weights.
    \item Jboss needs to be provided with the DB address on container creation / change.
\end{enumerate}

The adjustment 1 should be run for the container after it is created or updated. We call this adjustment "scale-hook" or "on\_node\_scale" hook. The script should be provided with 4 arguments: previous CPU cores allocated, previous RAM memory units allocated, and new CPU cores and RAM memory units allocated.

In the 2nd adjustment   the LoadBalancer tier depends on the Jboss tier. So the dependencies should be specified. Also the dependent node (LoadBalancer) should wait until all the containers of the dependee are processed (JBOSS). After that the adjustment script should be run on all the containers of the dependent node (LoadBalancer). We call this adjustment "tier-hook" or "on\_dependency\_scale" hook.

The 3d adjustment should be run only on the container start. To simpliy the first version of the application we decided not to implement this adjustment for the dependecy 1-to-n. Only for the dependencies n-to-1 and 1-to-1 after the dependee tier is processed, the ip of the dependee tier is provided to the dependent node on the creation (parameter "add-host").

This creates some limitations: the dependee container should be processed before dependent containers. For example, if the first plan will have only dependent containers, in the next plan it will be added dependee container, and in the last plan the dependence between them will be specified, then "add-host" feature will not be propagated and the dependee containers will not know about the dependant one.

Similar problem if we decided to remove the not-scalable tier from the plan, the dependee containers will have not valid "add-host" link and even if later we put the "not-scalable" tier back to the plan, it will have another IP address.

So considering these limitations we suggest one of the two workarounds.

The first is to manage the DB dependencies out of the topology and provide the IP address in the topology as "docker parameter".

The second is just use docker "add-host" feature as it. So if the dependent container is removed, or the dependency is added, the user should be aware that "add-host" will not be specified.

\subsection{Dependencies}
Considering the requirement FR-9 only 1-to-1, 1-to-n and n-to-1 dependencies are supported. The dependencies between scalable tiers are not supported. The dependencies are only used to manager tier-hooks and .
The flow for container allocation and hooks execution should be:
\begin{enumerate}
    \item get tiers dependency graph (G)
    \item get set of Tiers without outgoing dependencies (T)
    \item run container create / update / delete for each tier in T
    \item run scale hooks if needed for each tier container in T
    \item run tier hooks if needed for each tier container in T
    \item remove T from G
    \item if G is not empty go to 2
\end{enumerate}

\section{Approaches}
Besides the ILP approach to solve container allocation problem discussed above it was also tried another approache: Constraint Satisfaction Problem.

\subsection{Constraint Satisfaction Problem}

CSP is defined as a triple \textless X, D, C\textgreater, where $X$ is a set of variables, $D$ is a set of the respective domains of values, $C$ is a set of constraints.

In our context we have 2 matrices of variables as $X$: \\
$x_{ij}$ is a matrix that specify how many CPU is used by Tier[j] on VM[i],\\
$y_{ij}$ is a matrix that specify how many RAM units are used by Tier[j] on VM[i].

The domain $D$ for us is all possible values of CPU cores and RAM units. For example if we have AWS t2.medium with 2 CPU cores and 8 RAM units, we have: \\
$d_{0ij}$ is $\{0, 1, 2\}$ for CPU cores,\\
$d_{1ij}$ is $\{0, 1, 2, 3, 4, 5, 6, 7, 8\}$ for RAM mem units.

For a set of constraints $C$ we should consider similar to ILP:
\begin{itemize}
    \item availability constraints on each VM for CPU cores and for RAM mem units
    \item demand constraint considering the plan
    \item 0 CPU cores with RAM \textgreater{} 0 or other way round
\end{itemize}

\subsection{Limitation of the CSP}
This CSP formulation has also some limitations:
\begin{enumerate}
    \item Comparing to ILP, changing domain to continuous values may cause problems.
    \item The solution of the CSP is a list of different solutions, that can differ only by a permutation. We can have a factorial number of different solutions which we need to look to found the optimal one considering the initial allocation.
    \item The problem is NP-Hard and the complexity growth is much faster than the ILP.
\end{enumerate}

The CSP formulation has one insuperable problem that it does not scale. If we have 10 Tiers, 10 VMs, and 16 CPU cores and 64 RAM mem units, the CSP solution does not complete in appropriate time.

\section{Architecture}

\begin{figure}[ht]
  \centering
    \includegraphics[width=350px,natwidth=688,natheight=617]{./pictures/architecture}
    \caption{Architecture}
\end{figure}

The architecture consists of two main components: Executor Main Node and Executor Agent. The main node accepts a monolithic plan, orchestrates agents
and has a whole picture of VMs allocated. The agent node can accept a distributed plan to execute, can run / start / stop docker containers, provides the allocation information and output of docker inspect command.

These executor nodes are "Execute" part of the autonomous system MAPE framework. The Monitor component measures different metrics of applications that are
running in the containers on the Executor agents, and provide this information to the Analyze component. The Plan component together with the Analyze component produce a new plan (a resource demand for tiers) which is sent to Executor.

\clearpage
\subsection{Components}
\begin{figure}[ht]
  \centering
    \includegraphics[width=350px,natwidth=553,natheight=303]{./pictures/main-classes}
    \caption{Executor main node classes}
\end{figure}

\begin{figure}[ht]
  \centering
    \includegraphics[height=150px,natwidth=113,natheight=203]{./pictures/agent-classes}
    \caption{Executor agent node classes}
\end{figure}


\subsection{ReLU Fourier representation }%-- Simple version}

We introduce the Taylor expansion of $e^{iz}$ with integral remainder as follows.
\begin{lemma}
For $|z|\leq c$,
\begin{equation}
e^{iz} -  iz -1
=
- \int_{0}^c\left[(z - u)_+e^{iu} + (-z - u)_+e^{-iu} \right]du.
\end{equation}
\end{lemma}
\begin{proof}
By the Taylor expansion with integral remainder,
\begin{equation}
e^{iz} = 1 + iz  - \int_0^z e^{iu}(z-u)du.
\end{equation}
Let $u_+=\max (u, 0)$ and $u_-=\min(u,0)$. Then, $u_-=-(-u)_+$ and
$$
z-u=(z-u)_+ + (z-u)_-=(z-u)_+ - (u-z)_+.
$$
It follows that
\begin{equation}
\begin{split}
\int_{0}^z (z-u)e^{iu} du=&\int_{0}^z (z-u)_+e^{iu} du + \int_{0}^z -(u-z)_+e^{iu} du
\\
=&\int_{0}^z (z-u)_+e^{iu} du + \int_{0}^{-z}  (-u-z)_+e^{-iu} du
\\
=&\int_{0}^c (z-u)_+e^{iu} du + (-u-z)_+e^{-iu} du.
\end{split}
\end{equation}
Thus,
\begin{equation}
e^{iz} - 1 - iz
=
-\int_{0}^c\left[(z - u)_+e^{iu} + (-z - u)_+e^{-iu} \right]du,
\end{equation}
which completes the proof.
\end{proof}

Let $z=\omega\cdot x$, $u=\|\omega\|_{\ell_1}t$ and $\bar \omega={\omega\over \|\omega\|_{\ell_1}}$. If $\Omega$ is bounded, say $|x|\le T$, $|\bar \omega \cdot x|\le T$. There exists the following expansion for $e^{i\omega\cdot x}$.
\begin{lemma}\label{lm:talorcomplex}
If $|x|\le T$,
\begin{equation}
e^{i\omega\cdot x} - 1 -  i\omega\cdot x
=
- \|\omega\|_{\ell_1}^2\int_{0}^T\left[(\bar \omega\cdot x - t)_+e^{i\|\omega\|_{\ell_1}t}
+ (-\bar \omega\cdot x - t)_+e^{-i\|\omega\|_{\ell_1}t} \right]dt.
\end{equation}
Denote
$$
D^\alpha = \partial_1^{\alpha_1}\partial_2^{\alpha_2}\cdots \partial_d^{\alpha_d},\quad \omega^\alpha = \omega_1^{\alpha_1}\omega_2^{\alpha_2}\cdots \omega_d^{\alpha_d},\quad \alpha!=\alpha_1!\alpha_2!\cdots \alpha_d!.
$$
\end{lemma}
It follows  the following Taylor expansion with an integral remainder.
\begin{lemma}\label{lm:probabilityexpan}
Suppose $|x|\le T$. There exists
\begin{equation}
f(x) = f(0) + \nabla f(0)\cdot x
+  \int_{\{-1,1\}\times [0,T]\times \mathbb{R}^{d}}  g(x, \theta)\lambda(\theta)d\theta
\end{equation}
with  $g(x,\theta)$ and $\lambda(\theta)$ defined in \eqref{eq:straglam}.
\end{lemma}
\begin{proof}
Since $
 f(x) = \int_{\mathbb{R}^d} e^{i\omega\cdot x}\hat{f}(\omega)d\omega
$
and
$
\nabla f(x)=\int_{\mathbb{R}^d} i^{|\alpha|}\omega  e^{i\omega\cdot x}\hat{f}(\omega)d\omega,
$
\begin{eqnarray}
\nabla  f(0)\cdot x=\int_{\mathbb{R}^d} i\omega\cdot  x\hat{f}(\omega)d\omega.
\end{eqnarray}
 It follows that
\begin{equation}
\nabla  f(0) \cdot x=i \int_{\mathbb{R}^d} \omega\cdot x\hat{f}(\omega)d\omega
=  \int_{\mathbb{R}^d} i\omega\cdot x\hat{f}(\omega)d\omega.
\end{equation}
Let $\hat{f}(\omega)=|\hat{f}(\omega)|e^{ib(\omega)}$. Then, $e^{i\|\omega\|_{\ell_1}t}\hat{f}(\omega) = |\hat{f}(\omega)|e^{i(\|\omega\|_{\ell_1}t + b(\omega))}$.
By Lemma \ref{lm:talorcomplex},
\begin{equation}\label{eq:fftaylor}
\begin{split}
&f(x) - f(0) - \nabla  f(0) \cdot x
\\
= &\int_{\mathbb{R}^d} \big (e^{i\omega\cdot x} - 1 - i\omega\cdot x \big )\hat{f}(\omega)d\omega.
\\
=&{\rm Re} \bigg (-\int_{\mathbb{R}^d} \int_{0}^T\left[(\bar \omega\cdot x - t)_+e^{i\|\omega\|_{\ell_1}t}
+ (-\bar \omega\cdot x - t)_+e^{-i\|\omega\|_{\ell_1}t} \right]\hat{f}(\omega)\|\omega\|_{\ell_1}^{2}dt d\omega\bigg )
\\
=& \int_{\{-1,1\}}\int_{\mathbb{R}^d} \int_{0}^T (z\bar \omega\cdot x - t)_+ s(zt,\omega)  |\hat{f}(\omega)|\|\omega\|_{\ell_1}^{2}dtd\omega dz
\end{split}
\end{equation}
with $\int_{\{-1, 1\}} r(z) dz = r(-1) + r(1)$ and
\begin{equation}
s(zt,\omega)= -\cos(z\|\omega\|_{\ell_1}t + b(\omega))
\end{equation}
Define $G=\{-1,1\}\times [0,T]\times \mathbb{R}^{d}$, $\theta=(z, t, \omega)\in G$,
\begin{equation}\label{eq:straglam}
g(x,\theta)= (z\bar \omega\cdot x - t)_+ {\rm sgn} s(zt,\omega),\qquad \lambda(\theta)={\rho(\theta)\over
\int_{\{-1,1\}\times [0,T]\times \mathbb{R}^{d}} \rho(\theta)d\theta}.
\end{equation}
with $\rho(\theta) = |s(zt,\omega)||\hat{f}(\omega)|\|\omega\|_{\ell_1}^{2}$.

Then \eqref{eq:fftaylor} can be written as
\begin{equation}\label{eq:reluintegral}
f(x) = f(0) +  \nabla  f(0) \cdot x
+  \int_{\{-1,1\}\times [0,T]\times \mathbb{R}^{d}}  g(x, \theta)\lambda(\theta)d\theta,
\end{equation}
which completes the proof.
\end{proof}

An application of the Monte Carlo method in Lemma \ref{MC} to the integral \eqref{eq:reluintegral} gives the following estimate.
\begin{theorem}
Suppose $|x|\le T$ and
$$
 \int_{\mathbb{R}^{d}} |\hat{f}(\omega)|\|\omega\|_{\ell_1}^{2} d\omega<\infty.
$$
There exist  $\|\bar \omega_j\|_{\ell_1}=1$, $t\in [0,T]$ such that
$$
f_n(x)= f(0) + \nabla  f(0) \cdot x  + {1\over n}\sum_{j=1}^{n} (\bar \omega_j\cdot x - t_j)_+
$$
satisfies the following estimate
\begin{equation}
\|f - f_n \|_{L^2(\Omega)} \leq C n^{-{1\over 2}}.
\end{equation}
%\begin{equation}
%\|D^\beta (f(x)- f_n(x))\|_{L^2(\Omega)}\le \sqrt{2^{m-k-2}(2m-k)\over k!(m-k)!}|\Omega|^{1/2} n^{-{1\over 2}-{1\over d}},\quad |\beta|=k\le m.
%\end{equation}
\end{theorem}

\iffalse
\noindent\textbf{A modified analysis using stratified sampling}

According to \eqref{eq:straglam}, the main ingredient $(z\bar \omega\cdot x - t)_+$ of $g(x,\theta)$  only includes the direction $\bar\omega$ of $\omega$ which belongs to a bounded domain  $\mathbb{S}^{d-1}$. Thanks to the continuity of $(z\bar \omega\cdot x - t)_+$ with respect to $(z, \bar\omega, t)$ and the boundedness of $\mathbb{S}^{d-1}$,
the application of the stratified sampling to the residual term of the Taylor expansion leads to the following approximation property.
\begin{theorem}\label{est:stratify}
Suppose $|x|\le T$ and
$$
 \int_{\mathbb{R}^{d}} |\hat{f}(\omega)|\|\omega\|_{\ell_1}^{2} d\omega<\infty.
$$
There exist $\beta_j\in [-2^d,2^d]$, $\|\bar \omega_j\|_{\ell_1}=1$, $t\in [0,T]$ such that
$$
f_n(x)= f(0) + \nabla  f(0) \cdot x  + {1\over n}\sum_{j=1}^{n}\beta_j (\bar \omega_j\cdot x - t_j)_+
$$
satisfies the following estimate
\begin{equation}
\|f - f_n \|_{L^2(\Omega)} \leq C n^{-{1\over 2}-{1\over d}}.
\end{equation}
%\begin{equation}
%\|D^\beta (f(x)- f_n(x))\|_{L^2(\Omega)}\le \sqrt{2^{m-k-2}(2m-k)\over k!(m-k)!}|\Omega|^{1/2} n^{-{1\over 2}-{1\over d}},\quad |\beta|=k\le m.
%\end{equation}
\end{theorem}
\begin{proof}
By Lemma \ref{lem:stratifiedapprox}, for any decomposition $G=\cup_{i=1}^M G_i$, there exist $\{\theta_i\}_{i=1}^n$ and $\{\beta_i\}_{i=1}^n\in [0,1]$ such that
\begin{equation}
\|  f - f_n\|_{L^2(\Omega)} = \|  r - r_{n}\|_{L^2(\Omega)} \leq {1\over n^{1/2}}\max_{1\le j\le M}\sup_{\theta_{j},\theta_{j}'\in G_j} \|   g(x,\theta_j) - g(x,\theta_j') \|_{L^2(\Omega)}
\end{equation}
with
$$
f_n(x)= f(0) +  \nabla  f(0) \cdot x + r_{n}(x), \qquad r_{n}(x)={1\over n}\sum_{j=1}^{n}\beta_j (\bar \omega_j\cdot x - t_j)_+,
$$
$$
r(x)=\int_{\{-1,1\}\times [0,T]\times \mathbb{R}^{d}}  g(x, \theta)\lambda(\theta)d\theta.
$$
Consider a particular decomposition $G=\cup_{i=1}^M G_i$ as follows.
The variable $z$ is in the set $\{-1,1\}$, which can be divided into two subsets $\{-1\}$ and $\{1\}$.
Given a positive integer $n$, for the random variable $t$, the interval  $ [0,T]$ can be divided into $n_t$ subintervals $\{G_i^t\}_{i=1}^{n_t}$ such that
$$
|t-t'|<{1\over 2}n^{-{1\over d}}\quad t,t'\in G_i^t,\quad 1\leq i\leq n_t
$$
for $n_t>2\lceil T  n^{1\over d}\rceil$.
For variable $\bar \omega=\omega/\|\omega\|_{\ell_1}\in \mathbb{S}^{d-1}$ where $\mathbb{S}^{d-1}=\{\bar \omega\in \mathbb{R}^d: \|\bar \omega\|_{\ell_1}=1\}$. Note that $\mathbb{S}^{d-1}$ can be divided into $n_\alpha$ subdomains $\{G_i^s \}_{i=1}^{n_s}$ such that
$$
\|\bar \omega- \bar \omega'\|_{\ell_1}\leq {1\over 2}n^{-{1\over d}}\qquad \bar \omega, \bar \omega' \in G_i^s,\quad 1\leq i\leq n_s
$$
for $(2n^{1\over d})^{d-1}\leq n_s\leq \lceil (5n^{1\over d})^{d-1}\rceil$ \cite{klusowski2016uniform}.
Then
$$
G=\displaystyle \cup \{G_{ijk\ell}: 1\leq i\leq 2,\ 1\leq j\leq n_t,\ 1\leq k\leq n_s,\ 1\le \ell\le 2\}
$$
with
\begin{equation}
G_{ijk\ell} = \{(z, t, \omega): z=(-1)^i,\ t\in G_j^t, \bar \omega \in G_k^s,\ {\rm sgn} s(zt,\omega)=(-1)^\ell\}.
\end{equation}
Denote this decomposition of $G$ by $G=\cup_{i=1}^{M} G_i$ with $M=4n_sn_t\le 2^{d}n$. For each $G_i$,
\begin{equation}
z=z',\ |t-t'|<{1\over 2}n^{-{1\over d}},\ \|\bar \omega  - \bar \omega'\|_{\ell^1}<{1\over 2}n^{-{1\over d}}\qquad \forall \theta=(z, t, \omega),\ \theta'=(z', t', \omega')\in G_i.
\end{equation}
For any $\theta_i, \theta'_i\in G_i$ and $|\alpha |=1$,
$$
| g(x,\theta_i) - g(x,\theta_i') | =  | \bar\omega  -   \bar\omega' |\le n^{-{1\over d}}.
$$
Thus, there exist $\theta_{i,j}$ such that
\begin{equation}
\| f - f_n\|_{L^2(\Omega)} \le C  n^{-{1\over 2}-{1\over d}}.
\end{equation}
with
$$
f_n(x)=  f(0) + \nabla f(0) \cdot x + {1\over  n}\sum_{j=1}^{n}\beta_j (\bar \omega_j\cdot x - t_j)_+
$$
with $\beta_j\in [-2^d,2^d]$,
which completes the proof.
\end{proof}

\fi


\section{Useful Languages}

\paragraph{Dyck Language} $S \rarr bSeS | \epsilon$
\paragraph{Palindromes} $S \rarr aSa | bSb | a | b | \epsilon$.
\paragraph{Antisymmetric} For $a^nb^m$ with $0 \le n \le m \le kn$: $S \rarr aSb | \ldots | aSb^k | \epsilon$ (this is ambiguous, impose an order, from $aXb$ to $aYb^k$, on the rules to avoid that).
\paragraph{Math Expressions}
\begin{align*}
    E &\rarr E + T | T \\
    T &\rarr T * F | F \\
    F &\rarr (E) | i
\end{align*}
The operators in this example are left-associative.