The general line here is: joke = humor = laughter
There are some things though that both of these explanations do not account for:
Are you laughing yet? Why not? These sentences contained all suggested ingredients of jokes. Maybe, these levels of references are socially acceptable? I think so, but we couldn't make these sentences funny by making "forbidden" references more explicit. Maybe they were too brief? Then imagine how much you'd laugh at a research paper on prostate cancer and corresponding mortality rates. Or maybe, some crucial ingredient of humor just wasn't there? Then what is that missing ingredient?
Why do kids consider peek-a-boo funny? Why is it funny when I pull a pig out of my pocket during a conversation?
How about the following jokes: (bear with me, it's for scientific purpose)
"It's either the wallpaper or me. One of us has to go."
Instead of loving your enemies, treat your friends a little better.
I hope you found at least some of these funny. Now, how many references to sex, violence and stupidity did you see there?
Enough of criticism. Now I want to suggest an explanation of humor that explains all of the above cases and has (or so it seems to me) apparent biological roots and social utility.
The keyword here is "surprise". Or, "twist". Something unexpected. Something that breaks the rules - not the social rules! - but the rules of logic, of common sense, breaks your expectations. Like a pig pulled out of a pocket. Or the peek-a-boo where a child finds the transition [now you see me - now you don't] amusing, but adults, for whom it is not new, don't. Or a "punch line" that adds a twist that all the joke was built for.
It is programmed into us biologically, to look for all kinds of exceptions in the outside world: changes in the level of signals, something suddenly appearing, blinking, bursting, jumping, etc. This attention to surprises is a result of millions of years of evolution, and can be traced from bacteria to humans. It is natural to expect that increasingly intelligent organisms would pay attention to increasingly complex surprises - including those that challenge their internal models of the world by suggesting unexpected connections between different ideas and interpretations. I heard a story about a dog that had its favorite joke, but my model example here would be closer to modern humans - e.g., a group of young Neanderthals. The first and most important common project of humans was joint construction of the mental models of the world. The cooperation went through the language, by sharing facts for building models, passing models that seemed right - AND sharing unexpected twists that either challenge the models, or help define limits of their applicability, or teach when [not] to use them, or just train your brain on amusing puzzles.
Neanderthals were too stupid (just as modern humans) to consciously seek and share semantic surprises "for betterment of the common knowledge base". Built-in neural loop producing pleasure upon detection of semantic twists helped a lot.
So, "funny" is an internal reaction to humor; it produces pleasure. Smile and laughter are social expressions of pleasure - but not just from humor. We smile or laugh when we see little kids play, or win lottery, or think of something nice, or are just happy. Humor is just one of the inputs for this reaction. When we tell people things, we try to make sure that all their elements are interesting, so we include twists, references to sex, things they may like to hear or see or things their enemies wouldn't like to see. Analogously, when we have a party, we provide space, drinks, light, heat, music, food, chairs, etc. This doesn't mean those are the same thing - they are different, have different reasons, and under other conditions may not be present together.
In the course of history, people learned to abuse the natural pleasures they used to receive from simple natural urges and sensations. Pleasures we derived from sweets, fats, watching rapidly moving objects and noticing simple contradictions and little logical surprises that used to be important for finding better food, avoiding predators, and learning new things, have long been artificially satisfied with donuts, videogames, and jokes. If we want to understand their nature though, we should not play with current cultural artifacts, but look into the original reasons why these things were developed. And their roots were all developed for one purpose - to help the animals navigate the environment.
Unfortunately, humans are still wired as if they lived in the jungle. They could understand the environment a lot better if they studied more science and read some good philosophy - but they are still trying to get pleasure exercising their reactions to [simulated/perverted] fast-moving game objects, or sharing silly little twists of logic. Many people realize quite well that these things are a waste of time, but they still can't help it: the atavistic urges are still running us. Sometimes people try to combine "learning and fun", but this requires a lot of tricks, as most natural implementations of "fun" have lost their intended utility quite awhile ago.
This also explains why there are so many more freelance comedians than philosophers. And why inventors of good videogames are much richer than authors of great theories. It's because the comparisons of their values are performed by neural networks taken from animals.
I personally find some bitter irony in this situation.