The logical explanation for this is that the Doctor knew this story was a predestination paradox from the moment Sally handed him the guide and was going through the motions from his respective point of view.
The Impossible Astronaut
Asylum of the Daleks
The Angels Take Manhattan
How does knowing your own future fix it?