Invariant Mechanisms in Transfer Learning and Causal Inference: Some Theoretical Perspectives and Algorithms