
quantifiers and modular arithmetic over input position indices. Specifically, counting quantifiers take
the form
∃=xi:ϕ(i)
where
x
is a count variable and
i
is a position index. They show that there exists
a single sentence in this logic that computes the output of the transformer for any input string of any
length. This is a powerful result because it shows that a simple logical formalism is fully sufficient to
describe all the complexity of a massive finite-precision transformer. It also provides an upper bound
on finite-precision transformers: any function that cannot be defined in first-order counting logic with
modular indexing cannot be expressed by the transformer.
However, Chiang et al.’s result is not fully general because it relies on the transformer precision being
fixed with respect to the transformer’s context length. More generally, as we will demonstrate in
Section 3, finite-precision transformers are a fundamentally weak variant of transformers: crucially,
cannot express uniform attention patterns, which are a core algorithmic primitive of transformers
(Weiss et al., 2018). In fact, we show that they can only attend to a constant number of input
positions, which may be seen as a rather limited generalization of hard attention.
1
For example,
Chiang et al. show that their logic for finite-precision transformers cannot recognize
ambm
, whereas
in practice, transformers can (Bhattamishra et al., 2020).
2
This motivates studying a formal model
of transformers where precision grows with context length (which we formalize as log-precision),
making it possible to capture uniform attention as well as other broad attention patterns. This is
useful both for recognizing ambmand more generally for reasoning globally over the input.
We demonstrate that log-precision transformer classifiers can also be expressed as sentences in a
simple logic: first-order logic with majority, or
FO(M)
, over inputs strings (Barrington et al., 1990).
In addition to standard existential and universal quantifiers,
FO(M)
has majority quantifiers that
return true iff at least half the propositions they quantify are true. It also allows comparing input
positions (e.g., ℓ < k in Figure 1) and accessing their individual bits. Our main result is as follows:
Theorem 1 (Informal version of Theorem 2).For any log-precision transformer
T
, there exists an
FO(M)sentence ϕthat computes the same function as T, i.e., ϕ(x) = T(x)for any input string x.
Upper bound. Theorem 2 shows transformers with more than finite precision can also be expressed
in a simple extension of first-order logic, going beyond Chiang et al. (2023)’s result. On the other
hand,
FO(M)
is a strict superset of Chiang et al.’s counting logic; it can simulate counting quantifiers
(see Section 2.2) and allows non-modular position comparisons. Thus, handling a more general class
of transformers powerful enough to express uniform attention slightly weakens the bound.
Still, our result constitutes (to our knowledge) the tightest upper bound on log-precision transformers
and the first defined in terms of logic, building on a line of complexity-theoretic work analyzing the
power of transformers (Hahn, 2020; Merrill et al., 2022; Liu et al., 2023; Merrill & Sabharwal, 2023).
In particular,
FO(M)
strengthens the upper bound of log-space-uniform
TC0
by Merrill & Sabharwal
(2023). The refined bound adds to the limitations of transformers identified by Merrill & Sabharwal
(2023): for example, it establishes unconditionally that log-precision transformers cannot compute
boolean matrix permanents, and shows that, in a certain formal sense, integer division and matching
parentheses are among the formally hardest problems that transformers can solve (see Section 4).3
Mechanistic interpretability. Beyond providing an upper bound on the reasoning problems solv-
able by transformers, we believe Theorem 1 could guide the design of “transformer-complete”
programming languages similar in spirit to RASP (Weiss et al., 2018). RASP is a declarative program-
ming language designed to capture transformer computation, and Lindner et al. (2023) implement a
compiler from RASP into transformers. Unlike RASP,
FO(M)
can provably express any transformer
(Theorem 1), which we believe justifies using it (or an equivalent but more user-friendly variant) as a
target language for programs extracted from transformers.
Similar to a decision tree, an
FO(M)
sentence has the interpretable property that each sub-sentence
corresponds to a constraint on input (see Figure 1). In contrast, the internal modules of a transformer
or circuit do not satisfy this since they map between arbitrary latent spaces. We speculate this property
1
Hard attention is provably substantially weaker than general attention (Hao et al., 2022; Merrill et al., 2022).
2
Technically, the empirical results of Bhattamishra et al. (2020) are for
ambmcm
, a harder variant of
ambm
.
3
To be clear, Theorem 1 is one-sided: every transformer can be expressed as an
FO(M)
sentence, but
not necessarily the other way. Moreover, we believe that many
FO(M)
sentences cannot be expressed by
transformers. An exact logical characterization of transformers remains an open problem.
2