A theory of dreaming