The ecological evolution of reefs