How exactly does the structure theorem follow from Smith Normal Form? (Wikipedia statement)

It is said that a presentation (map from relations to generators) is put into Smith Normal form. Now, I see that the Smith Normal form applies to such a presentation, but I dont see how the structure theorem follows. I must say that the term presentation is new for me, I read through Free Presentation, Group presentation and looked up the terms relators and generators which I think I understood.

Smith Normal Form

A linear transformation $f$ between free modules $M$ and $N$ over a PID $R$ can be represented by the $m \times n$ following matrix where $a_i | a_{i+1} \forall 1 \leq i \lt r$.

$$ \begin{bmatrix} a_1 & 0 &0 &0 &... \\ 0 &a_2 &0 &0 &... \\ 0 &0 &a_3 &0 &... \\ ... \end{bmatrix} $$

Structure theorem for modules over PIDs

For every finitely generated module $M$ over a PID $R$ there exist unique ideals $(d_1) \supset (d_2) \supset (d_3) ... (d_n)$ with $M \cong R/(d_1) \oplus R/(d_2) \oplus ... R/(d_n)$


Let $m_1, \ldots, m_n$ be generators of $M$ and define an $R$-module homomorphism $f: R^n \rightarrow M$ by $(r_1, \ldots, r_n) \mapsto r_1m_1 + \ldots + r_nm_n$. Since the $m_i$ generate $M$, $f$ is surjective. Therefore $M \cong R^n/\ker f$.

Recall that an $R$-module is called Noetherian if every submodule is finitely generated. A ring $R$ is called Noetherian if it is Noetherian as a module over itself, which is equivalent to all ideals being finitely generated.

In our case, since $R$ is a PID it is automatically Noetherian (every ideal is generated by a single element, so is certainly finitely generated). Therefore finitely generated $R$-modules are Noetherian. In particular, $R^n$ is Noetherian, so the submodule $\ker f$ is finitely generated.

Pick generators $k_1, \ldots, k_m$ for $\ker f$ and define a homomorphism $g: R^m \rightarrow R^n$ by $(r_1, \ldots, r_m) \mapsto r_1k_2 + \ldots + r_mk_m$. Then $M \cong R^n / \ker f \cong R^n / \mathrm{im} \ g$. Writing $g$ in $n \times m$ Smith normal form with diagonal entries $a_1, \ldots, a_n$ as you suggest (where $a_{m+1}, \ldots, a_n$ are defined to be $0$ if $m<n$), we see that $M \cong R/(a_1) \oplus \ldots \oplus R/(a_n)$. Since $a_1 | \ldots | a_n$ we have $(a_1) \supset \ldots \supset (a_n)$. This proves existence.

For uniqueness, the cleanest way is with tensor products. Since $R$ is a PID we can take prime factorisations of the $a_i$. For each prime $p \in R$, the ideal $(p)$ is maximal, so $K :=R/(p)$ is a field, and the number of $a_j$ divisible by $p$ is simply the dimension of $K \otimes_R M$ as a $K$-vector space. More generally, the number of $a_j$ divisible by $p^d$ is the dimension of $K \otimes_R (p^{d-1}M)$. The dimensions of the $K \otimes_R (p^{d-1}M)$ are intrinsic to $M$, and using the fact that $a_1 | \ldots | a_m$ we can reconstruct the $a_j$ from them. (This won't detect any $a_i$ equal to $1$ but we can just delete these since $R/(1)=0$.)