Compositional generalization through abstract representations in human and artificial neural networks