Why think step by step? Reasoning emerges from the locality of experience