Hamlet would be forgiven if he had lashed out violently against his stepfather after discovering that he had been responsible for his father's murder. But he has to be completely certain, so he uses the play to "catch the conscience" of Claudius. This demonstrates that he retains his rational faculties despite the extreme strain of the situation. Shortly after the play is over, he encounters his stepfather kneeling in his chamber in prayer, a wonderful opportunity to kill him. But he chooses not to, based on his belief that his father's murder would be better avenged if he killed Claudius while he was
...drunk asleep; or in his rage;
Or in the incestuous pleasure of his bed;
At game, a-swearing, or about some act
That has no relish of salvation in't...
Indeed, throughout the play, Hamlet is weighing the consequences of his actions. His famous "To be or not to be" soliloquy is a debate over the merits of life and death, and he comes to the conclusion that life is the better choice, since we cannot know what comes beyond. He sometimes acts impulsively, to be sure (for example, when he leaps into Ophelia's grave, or when he stabs at the curtain, killing Polonius,) but he is not purely governed by his irrational emptions.