A Mathematical Theory of Attention