Shakespeare's tragedies tend to include comic relief in them at some point and to some degree. These instances of comic relief often occur right before a major plot twist or development. In "Romeo and Juliet", for example, the scene where Mercutio makes fun of the nurse in the square shortly before he is accidentally killed by Tybalt, is a funny scene. It precedes Romeo's killing of Tybalt that gets him exiled. Another example is "Macbeth" and the scene with the drunken gate keeper that precedes the discovery of the dead King Duncan. This scene in Act 5 of "Hamlet" with the gravedigger is another instance of comic relief. It also serves as an opportunity for us to hear more of Hamlet's contemplations. The contemplation of what happens to a man once he's dead (ll.210-219) is something of a repeat of an idea that Hamlet expressed in Act 4, sc. 2 (ll.19-25). In that Act 4 scene, Hamlet is supposedly spouting gibberish as he carries on his "antic disposition". That he repeats this idea in serious dialogue with Horatio gives some evidence to the idea that Hamlet is not mad. The gravedigger is not a mad character at all, but one who has become desensitized to what he does, as Horatio said in l. 69, "Custom hath made it in him a property of easiness." The gravedigger jokes and sings because he's done his job for many years and the sadness or horror of gravedigging is no longer with him.