Formal Algorithms for Transformers