Solution (source code)

= Solution

Take $fg=f\circ g$. \b[The assertion can fail on an <infinite set>.] On $X=\mathbb N_0$, <set> $g(n)=n+1$, $f(0)=0$, and $f(n)=n-1$ for $n\geq1$. Then $f(g(n))=n$ for every $n$, but $g(f(0))=1\ne0$. Thus a <right inverse> need not be a <left inverse>.

\b[On a <finite set> it is always true.] From $fg=\operatorname{id}_X$, $g$ is an <injection>: equality of two $g$-values gives equality after applying $f$. An injective self-map of a finite <set> is a <bijection>. Composing $fg=\operatorname{id}_X$ with $g^{-1}$ shows $f=g^{-1}$, and hence $gf=\operatorname{id}_X$. This includes the empty <set>.