The dynamics of message passing on dense graphs, with applications to compressed sensing