How to prove an equivalent definition of injective? 
Let $A,B$ be non-empty sets and $f:A\to B$ a function. Proof that $f$ is injective, iff $f\circ g=f\circ h$ implies that $g=h$ for all functions $g,h:Y\to A$, for every set $Y$?

I can see why this is. But how do I prove this? I get confused by the if and only if part.
 A: Assume $f\colon A\to B$ is injective.
Let $g,h\colon Y\to A$ be two functions with $f\circ g=f\circ h$.
Then for $a\in A$ we have $f(g(a))=f(h(a))$, hence by injectivity of $f$ also $g(a)=h(a)$. Since $a\in A$ was arbitrary, we conclude $g=h$.
Conversely assume that $f\circ g=f\circ h$ implies $g=h$ for all $g,h\colon Y\to A$.
Let $x,y\in A$ with $f(x)=f(y)$.
Define $g,h\colon\{0\}\to A$ by $g(0)=x$, $h(0)=y$. Then $f\circ g=f\circ h$, hence $g=h$, hence $g(0)=h(0)$, i.e. $x=y$. We conclude that $f$ is injective.
A: On the one hand, suppose that $f$ is injective, and let $g,h:Y\to A$ for some set $Y$ such that $f\circ g=f\circ h$. We must show that for every $y\in Y,$ we have $g(y)=h(y)$. Indeed, $f(g(y))=f(h(y))$ by our choice of $g,h,$ so $g(y)=h(y)$ by injectivity of $f$.
On the other hand, suppose that $f$ is not injective, so that there exist two different $a,b\in A$ such that $f(a)=f(b)$. Can you come up with distinct constant functions $g,h$ into $A$ such that $f\circ g=f\circ h$?
A: Suppose $f$ is injective, and $g,h$ are such that $f \circ g = f \circ h$. Now let $y \in Y$, then $f(g(y)) = f(h(y))$, and since $f$ is injective, we have $g(y) = h(y)$. Since this is true for all $y$, we have $g=h$.
Now suppose $f \circ g = f \circ h$ for all $g,h:Y \to A$ implies $g=h$. Suppose $f(a_1) =f(a_2)$. Let $Y = \{0\}$ and define $g:Y \to A$ by $g(0) = a_1$, and similarly, let $h(0) = a_2$. Then since $f(g(0)) = f(h(0))$, we have $f \circ g = f \circ h$, and hence $g=h$. In particular, $a_1 = a_2$.
