Functions such that $f(g(x)) = x$ but $g(f(x)) \neq x $ I want to find functions  $f: A \to B$ and $g: B \to A$ such that $g(f) = i_A$ but $f(g) \neq i_B$. 
Is this possible? An exercise for one of my classes is to actually prove that if $g(f) = i_A$, then $f(g) = i_B$. 
I've found functions where $f(g(x)) = x$ for most $x$, but failed for an exceptional choice of $x$. Would that be a counter example? 
Are there examples where $g(f(x)) = x$ for all $x \in A$ and $f(g(x)) \neq x$ for all $x \in B$?
 A: The fact that $g(f)=i_A$ tells you that $f$ must be injective and $g$ must be surjective. If you want a counterexample, you must look at functions $f$ and $g$ such that 


*

*$f$ is injective

*$g$ is surjective

*$f$ is not surjective

*$g$ is not injective


An example can be $f:\{0\}\to \{0,1\}$, 
$g:\{0,1\}\to \{0\}$ with $$f(0)=0, g(0)=0, g(1)=0$$
then $g(f)=i_{\{0\}}$ since $g(f(0))=g(0)=0$ but $f(g)\neq i_{\{0,1\}}$ since $f(g(1))=f(0)=0$.
A: If your counterexample satisfies what you claim it does, it is a valid counterexample. Either there is a mistake in your exercise, or there is some condition that you've forgotten to tell us [e.g. $A$ and $B$ are finite sets of same cardinality].
As long as $A$ and $B$ are both non-empty, there are no functions so that $g(f(a)) = a$ and $f(g(b)) \ne b$ for all $a \in A, b \in B$. This can be seem by choosing an arbitrary $a \in A$ and setting $b = f(a)$. Then $f(g(b)) = f(g(f(a)) = f(a) = b$.
A: Take for $A$ and $B$ the set of real sequences.
For $f$ the map $a_n \mapsto a_{n+1}$ and for $g$ the map $a_0=1$ and $a_n \mapsto a_{n-1}$ for $n > 0$.
A: There are quite a few examples where such functions play a role:
1. Let $f\colon A\to B$ be injection. Then it has a left inverse.
Proof. Define $g\colon B\to A$ such that $g(f(a)) = a$, and $g(b) = a_0$ for $b\notin f(A)$ and some constant $a_0\in A$. This is well defined precisely because $f$ is injective. QED
Notice that if $f$ is not surjective, we cannot have $f(g(b)) = b$ since it would imply that $f$ is bijective, thus surjective.
2. Injective endomorphisms of infinite dimensional spaces need not be isomorphisms (in contrast to finite-dimensional case).
This we can show by giving counterexample. Take $l^p$ space and define $$\begin{align}L&\colon (a_1, a_2,\ldots) \mapsto (a_2, a_3,\ldots)\\ R&\colon (a_1, a_2,\ldots) \mapsto (0, a_1,\ldots)\end{align}$$ called one-sided shift operators (left and right, respectively). Obviously, $LR = \operatorname{id}$, so $R$ is injective. If $R$ were isomorphism, we would have $$LR = \operatorname{id}\implies LRR^{-1} = R^{-1}\implies L = R^{-1}$$ but, $RL\neq \operatorname{id}$, so $R$ is injective endomorphism which is not isomorphism.
3. Let $f\colon A\to B$ be any function. Define $\sigma\colon x\mapsto (x,f(x))$ and $\pi\colon (x,y)\mapsto x$. Then we have $\pi\sigma = \operatorname{id}$, but $\sigma\pi \neq \operatorname{id}$. This is easily visualized as a graph of function and projection onto the first axis. In topology, this is a motivating example of section of fiber bundle.
4. In category theory, products of objects come equipped with universal morphisms $$p_1\colon A\times B\to A,\ p_2\colon A\times B\to B$$ and coproducts come equipped with universal morphisms $$i_1\colon A\to A\amalg B,\ i_2\colon B\to A\amalg B$$
In case of Abelian categories (category of Abelian groups, category of modules, for example) products and coproducts coincide and universal morphisms satisfy relations
$$ p_k\circ i_l = \left\{
\begin{array}{r l}
\operatorname{id}, & \ k = l \\ 
0, & \ k\neq l
\end{array}
\right.$$ $$i_1\circ p_1 + i_2\circ p_2 = \operatorname{id}$$ and thus, $p_k\circ i_k = \operatorname{id}$, but $i_k\circ p_k \neq \operatorname{id}$.
