Let X be a topological space. One goal of algebraic topology is to study the topology of X by means of algebraic invariants, such as the singular cohomology groups H^{n}(X;G) of X with coefficients in an abelian group G. These cohomology groups have proven to be an extremely useful tool, due largely to the fact that they enjoy excellent formal properties, and the fact that they tend to be very computable. However, the usual definition of H^{n}(X;G) in terms of singular G-valued cochains on X is perhaps somewhat unenlightening. This raises the following question: can we understand the cohomology group H^{n}(X;G) in more conceptual terms?

As a first step toward answering this question, we observe that H^{n}(X;G) is a representable functor of X. That is, there exists an Eilenberg-MacLane space K(G,n) and a universal cohomology class η ∈ H^{n}(K(G,n);G) such that, for any topological space X, pullback of η determines a bijection

[X, K(G, n)] → H^{n}(X; G)

Here [X,K(G,n)] denotes the set of homotopy classes of maps from X to K(G,n). The space K(G,n) can be characterized up to homotopy equivalence by the above property, or by the the formula π_{k}K(G,n)≃ ∗ if k̸ ≠ n

or

G if k = n.

In the case n = 1, we can be more concrete. An Eilenberg MacLane space K(G,1) is called a classifying space for G, and is typically denoted by BG. The universal cover of BG is a contractible space EG, which carries a free action of the group G by covering transformations. We have a quotient map π : EG → BG. Each fiber of π is a discrete topological space, on which the group G acts simply transitively. We can summarize the situation by saying that EG is a G-torsor over the classifying space BG. For every continuous map X → BG, the fiber product X^{~} : EG × BG X has the structure of a G-torsor on X: that is, it is a space endowed with a free action of G and a homeomorphism X_{~}/G ≃ X. This construction determines a map from [X,BG] to the set of isomorphism classes of G-torsors on X. If X is a well-behaved space (such as a CW complex), then this map is a bijection. We therefore have (at least) three different ways of thinking about a cohomology class η ∈ H^{1}(X; G):

(1) As a G-valued singular cocycle on X, which is well-defined up to coboundaries.

(2) As a continuous map X → BG, which is well-defined up to homotopy.

(3) As a G-torsor on X, which is well-defined up to isomorphism.

The singular cohomology of a space X is constructed using continuous maps from simplices ∆^{k} into X. If there are not many maps into X (for example if every path in X is constant), then we cannot expect singular cohomology to tell us very much about X. The second definition uses maps from X into the classifying space BG, which (ultimately) relies on the existence of continuous real-valued functions on X. If X does not admit many real-valued functions, then the set of homotopy classes [X,BG] is also not a very useful invariant. For such spaces, the third approach is the most powerful: there is a good theory of G-torsors on an arbitrary topological space X.

There is another reason for thinking about H^{1}(X;G) in the language of G-torsors: it continues to make sense in situations where the traditional ideas of topology break down. If X is a G-torsor on a topological space X, then the projection map X → X is a local homeomorphism; we may therefore identify X with a sheaf of sets F on X. The action of G on X determines an action of G on F. The sheaf F (with its G-action) and the space X (with its G-action) determine each other, up to canonical isomorphism. Consequently, we can formulate the definition of a G-torsor in terms of the category ShvSet(X) of sheaves of sets on X without ever mentioning the topological space X itself. The same definition makes sense in any category which bears a sufficiently strong resemblance to the category of sheaves on a topological space: for example, in any Grothendieck topos. This observation allows us to construct a theory of torsors in a variety of nonstandard contexts, such as the étale topology of algebraic varieties.

Describing the cohomology of X in terms of the sheaf theory of X has still another advantage, which comes into play even when the space X is assumed to be a CW complex. For a general space X, isomorphism classes of G-torsors on X are classified not by the singular cohomology H^{1}_{sing}(X;G), but by the sheaf cohomology H^{1}_{sheaf}(X; G) of X with coefficients in the constant sheaf G associated to G. This sheaf cohomology is defined more generally for any sheaf of groups G on X. Moreover, we have a conceptual interpretation of H^{1}_{sheaf}(X; G) in general: it classifies G-torsors on X (that is, sheaves F on X which carry an action of G and locally admit a G-equivariant isomorphism F ≃ G) up to isomorphism. The general formalism of sheaf cohomology is extremely useful, even if we are interested only in the case where X is a nice topological space: it includes, for example, the theory of cohomology with coefficients in a local system on X.

Let us now attempt to obtain a similar interpretation for cohomology classes η ∈ H^{2} (X ; G). What should play the role of a G-torsor in this case? To answer this question, we return to the situation where X is a CW complex, so that η can be identified with a continuous map X → K(G,2). We can think of K(G,2) as the classifying space of a group: not the discrete group G, but instead the classifying space BG (which, if built in a sufficiently careful way, comes equipped with the structure of a topological abelian group). Namely, we can identify K(G, 2) with the quotient E/BG, where E is a contractible space with a free action of BG. Any cohomology class η ∈ H^{2}(X;G) determines a map X → K(G,2), and we can form the pullback X^{~} = E × _{BG} X. We now think of X as a torsor over X: not for the discrete group G, but instead for its classifying space BG.

To complete the analogy with our analysis in the case n = 1, we would like to interpret the fibration X → X as defining some kind of sheaf F on the space X. This sheaf F should have the property that for each x ∈ X, the stalk F_{x} can be identified with the fiber X^{~}_{x} ≃ BG. Since the space BG is not discrete (or homotopy equivalent to a discrete space), the situation cannot be adequately described in the usual language of set-valued sheaves. However, the classifying space BG is almost discrete: since the homotopy groups π_{i}BG vanish for i > 1, we can recover BG (up to homotopy equivalence) from its fundamental groupoid. This suggests that we might try to think about F as a “groupoid-valued sheaf” on X, or a stack (in groupoids) on X.