# If $R$ is a PID and $M$ is finitely generated $R$-module, why is $M=\operatorname{coker}[R^m\to R^n]$

I read a book and I don't understand one transition:

$$R$$ is a PID. $$M$$ is a finitely generated module. Thus there exists a surjective $$\varphi:R^n\to M$$. $$R$$ is PID therefore $$R$$ is noetherian, thus $$R^n$$ is noetherian thus $$\ker\varphi\subseteq R^n$$ is finitely generated. Thus there exists $$\psi :R^m\to R^n$$ such that $$\operatorname{Im} \psi=\ker\varphi$$.

The transition that I don't understand is why is $$M=\operatorname{coker} \psi$$?

I know that $$\operatorname{coker}\psi=R^n/\operatorname {Im}\psi=R^n/\ker\varphi$$. I don't understand how does $$M=\operatorname {coker}\psi$$. Thanks.

We have $$M = \text{im}(\varphi) \cong R^n/\text{ker}(\varphi) = R^n/\text{im}(\psi) = \text{coker}(\psi)$$.
The image of $$\varphi$$ is isomorphic to $$R^n/\ker\varphi$$. But $$\varphi$$ is surjective, so that image is just $$M$$.
There exists a surjective morphism $$\;p:R^m\longrightarrow\ker\varphi$$ for some $$m$$, and $$\psi$$ is just the composition of $$p$$ by the canonical injection $$\;i:\ker\varphi\hookrightarrow R^n$$.