On the Foundations of Grounding in Answer Set Programming