**Proof plan.** Factor a surjection $R^m \twoheadrightarrow M$ through the Smith normal form of a presentation matrix. Row operations change the basis of $R^m$; column operations change generators of the kernel. Reading off the Smith normal form then gives the decomposition directly.
**Step 1: Present $M$ as a quotient.**
Since $M$ is finitely generated, there is a surjective $R$-module homomorphism $\varphi : R^m \twoheadrightarrow M$ for some $m \geq 1$. By the [First Isomorphism Theorem for Modules](/theorems/862), $M \cong R^m / \ker\varphi$.
**Step 2: $\ker\varphi$ is generated by at most $m$ elements.**
[claim: Kernel Is Finitely Generated]
Any submodule $N \leq R^m$ is generated by at most $m$ elements.
[/claim]
[proof]
By induction on $m$. Let $I = \{r \in R : (r, r_2, \ldots, r_m) \in N \text{ for some } r_2, \ldots, r_m\}$; this is an ideal of $R$, hence principal: $I = (a)$. Choose $n = (a, a_2, \ldots, a_m) \in N$. Any $v = (r_1, r_2, \ldots, r_m) \in N$ has $r_1 \in I = (a)$, so $r_1 = ra$, and $v - rn = (0, r_2 - ra_2, \ldots) \in N \cap (\{0\} \times R^{m-1})$. By induction, $N \cap (\{0\} \times R^{m-1})$ is generated by at most $m-1$ elements, giving at most $m$ generators for $N$ in total.
[/proof]
**Step 3: Smith normal form of the presentation matrix.**
Let $x_1, \ldots, x_n$ (with $n \leq m$) generate $\ker\varphi$, arranged as columns of an $m \times n$ matrix $A$. By the [Smith Normal Form Theorem](/theorems/863), there exist invertible matrices $P \in M_{m \times m}(R)$ and $Q \in M_{n \times n}(R)$ such that
\begin{align*}
PAQ = \begin{pmatrix} d_1 & & & \\ & d_2 & & \\ & & \ddots & \\ & & & d_r \\ & & & & 0 \\ & & & & & \ddots \end{pmatrix},
\end{align*}
where $d_i \neq 0$ and $d_1 \mid d_2 \mid \cdots \mid d_r$.
**Step 4: Read off the decomposition.**
[claim: Decomposition]
The change of basis given by $P$ (which gives a new basis $v_1, \ldots, v_m$ of $R^m$) and the change of generators given by $Q$ (which gives new generators of $\ker\varphi$ equal to $d_1v_1, \ldots, d_rv_r$) yield
\begin{align*}
M \cong \frac{R^m}{\ker\varphi} \cong \frac{R}{(d_1)} \oplus \frac{R}{(d_2)} \oplus \cdots \oplus \frac{R}{(d_r)} \oplus R^{m-r}.
\end{align*}
[/claim]
[proof]
Row operations on $A$ (left-multiplication by invertible $m \times m$ matrices) correspond to change of basis in $R^m$: the new basis $v_i = P^{-1}e_i$ gives $R^m = Rv_1 \oplus \cdots \oplus Rv_m$. Column operations on $A$ (right-multiplication by invertible $n \times n$ matrices) change the generators of $\ker\varphi$; the new generators are $d_1v_1, \ldots, d_rv_r$. The quotient in each $v_i$-component is $Rv_i / R(d_iv_i) \cong R/(d_i)$ for $i \leq r$, and $Rv_i$ (free) for $i > r$.
[/proof]
This gives the desired isomorphism. $\square$