Towards a Theoretical Framework of Out-of-Distribution Generalization