Towards Understanding Grokking: An Effective Theory of Representation Learning