A General Theory of Magic