Ed and Aaron, thank you very much for explaining the practical benefits of injectivity and surjectivity.
I have taken what I learned from you and cast it into my own thoughts and experiences. Please let me know of any errors.
Motivation
When I go hiking, I want to be able to retrace my steps and get back to my starting point.
When I go to a store, I want to be able to return home.
When I swim out from the shore, I want to be able to get back to the shore.
Going somewhere and then coming back to where one started is important in life.
And it is important in mathematics.
And it is important in functional programming.
Domain, Codomain, and Inverse
If a function maps a set of elements (the domain) to a set of values (the codomain) then it is often useful to have another function that can take the elements in the codomain and send them back to their original domain values. The latter is called the inverse function.
In order for a function to have an inverse function, it must possess two important properties, which I explain now.
The Injective Property
Let the domain be the set of days of the week. In Haskell one can create the set using a data type definition such as this:
data Day = Monday | Tuesday | Wednesday | Thursday | Friday | Saturday | Sunday
Let the codomain be the set of breakfasts. One can create this set using a data type definition such as this:
data Breakfast = Eggs | Cereal | Toast | Oatmeal | Pastry | Ham | Grits | Sausage
Now I create a function that maps each element of the domain to a value in the codomain.
Here is one such function. The first line is the function signature and the following lines is the function definition:
f :: Day -> Breakfast
f Monday = Eggs
f Tuesday = Cereal
f Wednesday = Toast
f Thursday = Oatmeal
f Friday = Pastry
f Saturday = Ham
f Sunday = Grits
An important thing to observe about the function is that no two elements in the domain map to the same codomain value. This function is called an injective function.
[Definition] An injective function is one such that no two elements in the domain map to the same value in the codomain.
Contrast with the following function, where two elements from the domain -- Monday and Tuesday -- both map to the same codomain value -- Eggs.
g :: Day -> Breakfast
g Monday = Eggs
g Tuesday = Eggs
g Wednesday = Toast
g Thursday = Oatmeal
g Friday = Pastry
g Saturday = Ham
g Sunday = Grits
The function is not injective.
Can you see a problem with creating an inverse function for g :: Day -> Breakfast
?
Specifically, what would an inverse function do with Eggs? Map it to Monday? Or map it to Tuesday? That is a problem.
[Important] If a function does not have the injective property then it cannot have an inverse function.
In other words, I can't find my way back home.
The Surjective Property
There is a second property that a function must possess in order for it to have an inverse function. I explain that next.
Did you notice in the codomain that there are 8 values:
data Breakfast = Eggs | Cereal | Toast | Oatmeal | Pastry | Ham | Grits | Sausage
So there are more values in the codomain than in the domain.
In function f :: Day -> Breakfast
there is no domain element that mapped to the codomain value Sausage.
So what would an inverse function do with Sausage? Map it to Monday? Tuesday? What?
The function is not surjective.
[Definition] A surjective function is one such that for each element in the codomain there is at least one element in the domain that maps to it.
[Important] If a function does not have the surjective property, then it does not have an inverse function.
[Important] In order for a function to have an inverse function, it must be both injective and surjective.
Injective + Surjective = Bijective
One final piece of terminology: a function that is both injective and surjective is said to be bijective. So, in order for a function to have an inverse function, it must be bijective.
Recap
If you want to be able to come back home after your function has taken you somewhere, then design your function to possess the properties of injectivity and surjectivity.
Best Answer
Renaming functions:
Let $f~:~\Bbb N\to\Bbb N,~f(x)=|x|$
Let $g~:~\Bbb Z\to\Bbb N,~g(x)=|x|$
Let $h~:~\Bbb N\to\Bbb Z,~h(x)=|x|$
Let $\ell~:~\Bbb Z\to\Bbb Z,~\ell(x)=|x|$
Note that all of these functions are very similar. They all map an input $x$ to an output $|x|$. These are indeed all considered functions. Their domains may be different however, and their codomains may be different as well.
Those with domain $\Bbb N$, $f$ and $h$, will be injective. It is clear that if $0<x_1<x_2$ that $|x_1|=x_1$ and $|x_2|=x_2$ and so $|x_1|<|x_2|$ implying that so long as $x_1\neq x_2$ we have $f(x_1)\neq f(x_2)$ and similar for $h$, and so they are injective.
Those with domain $\Bbb Z$, $g$ and $\ell$, will not be injective. Your example was a good one. $g(-4)=g(4)$ despite $-4\neq 4$.
Those with codomain $\Bbb N$ will be surjective. Given a value $y$ from the codomain of $\Bbb N$, it follows that $y$ is also an element of the domain and that $f(y)=|y|$ will equal $y$ since the absolute value of a non-negative real is equal to itself. Since every value in the codomain has an element from the domain who can map to it (that element being itself) surjectivity follows.
Those with codomain $\Bbb Z$ will not be surjective. Since absolute value is always going to give a non-negative result, there is no value which will map to a negative result. As a specific example, nothing will ever map to $-2$.
So again, $f$ is injective and surjective (thus bijective), $g$ is surjective but not injective, $h$ is injective but not surjective, and $\ell$ is neither injective nor surjective.
"All surjective functions have an inverse... but what is the inverse of $|x|$?"
All bijective functions have an inverse function. Surjective functions you can define an inverse relation but not necessarily an inverse function. In such an event it might be ambiguous which among several values to pick for the inverse to map to. For instance, with $g$, what should be the "inverse of $4$?" The question is, what value(s) from the domain map to $4$? The answer would be that both $4$ and $-4$ mapped to $4$. Usually we prefer just a single result.
"What is the inverse of $|x|$?" For $f$, it might help to recognize that instead of writing it as $f(x)=|x|$ we could have achieved the same effect as $f(x)=x$. The punchline is that in this specific instance, $f$ is it's own inverse... and both are what are known as the "identity function."