Lectures onType Theory
Chapter 141
Chapter 141Core route

Categories, Functors, and Representability

Three facts have been proved separately in this book, each about a different kind of composition. Type substitutions compose in diagrammatic order and id is a two-sided unit for that composition (definition 3.5). Simultaneous term substitutions act on terms, and extending a substitution by one more variable is the same as acting twice (equation 2.1). Context extensions ΔΓ compose, and a semantic value at Γ must act uniformly at every extension (chapter 49). Each proof is a short induction, and each is repeated whenever a new calculus arrives. The repetition is not harmless: comparing a syntax with its environments, or a calculus with a model, requires saying that two such composition structures agree, and that statement cannot be made until both structures are instances of one definition. The object that removes the repetition is a set of arrows with a typed composition satisfying three equations.

Substitutions compose

Fix the simply typed calculus of chapter 2: types A,B::=P2AB; terms built from variables, abstraction, application, the constants tt,ff, and the conditional if(e;e1;e2), typed by the rules Var, Lam, App, and the Boolean rules; and simultaneous capture-avoiding substitution e[σ] from definition 2.41. A context Γ=x1:A1,,xn:An declares distinct variables. As in convention 2.3, a term is an α-equivalence class, so equality of terms is equality up to renaming of bound variables; the symbol =α marks the places where a calculation is performed on representatives.

Definition 141.1 — Substitutions between contexts

Let Γ ctx and Δ=y1:B1,,ym:Bm. A substitution σ:ΓΔ is a list (b1,,bm) of terms with Γbj:Bj(1jm). Its action on a term Δe:A is the simultaneous substitution e[σ]:=e[b1/y1,,bm/ym]. We write σ(yj) for bj.

A substitution ΓΔ provides a Γ-term for each variable of Δ, so its action sends Δ-terms to Γ-terms: the arrow and the action point in opposite directions. Chapter 26 wrote f:ΓΔ for the dependent form of the same list (definition 26.45); here it is written with the arrow .

Three facts about substitution are used repeatedly and are stated once.

Lemma 141.2 — Trivial substitutions

Let θ be a simultaneous substitution and e a term.

  1. If y is not in the domain of θ, then e[θ,yy]=αe[θ].

  2. e[x1/x1,,xn/xn]=αe.

  3. If no free variable of e is in the domain of θ, then e[θ]=αe; in particular a closed term is fixed by every substitution.

Proof of Lemma 141.2 — Trivial substitutions

Proof. All three are proved by induction on e, choosing each bound name fresh for y, for the xi, and for the range of θ. At a variable, both sides of (1) are y if the variable is y and the value of θ otherwise; both sides of (2) are the variable itself; and in (3) the variable is free in e, hence outside the domain, and is unchanged. Application, the constants, and the conditional recurse in their immediate subterms, the hypothesis of (3) passing to each subterm because its free variables are among those of e. At an abstraction λz:B.b with z fresh, the abstraction clause of definition 2.41 removes z from the domain on both sides; for (3) the free variables of b are those of e together with z, which is not in the domain of θz, so the induction hypothesis applies to b, and λz is reattached. ◻

Lemma 141.3 — Action preserves typing

If σ:ΓΔ and Δe:A, then Γe[σ]:A.

Proof of Lemma 141.3 — Action preserves typing

Proof. Induct on the derivation of Δe:A, proving the statement for every Γ and every σ:ΓΔ at once.

Variable case. The term is yj with A=Bj, and yj[σ]=bj, which has type Bj in Γ by the definition of a substitution.

Application case. From Δe1:BA and Δe2:B the induction hypothesis gives Γe1[σ]:BA and Γe2[σ]:B, and App derives the type of e1[σ]e2[σ]=(e1e2)[σ]. The constants are unchanged by substitution and keep their type; the conditional recurses in its three subterms exactly as application does.

Binder case. The term is λy:B.b with Δ,y:Bb:C and A=BC. Choose the bound name y fresh for Γ, for Δ, and for every bj; convention 2.3 permits this. The list σ+:=(b1,,bm,y) is then a substitution Γ,y:BΔ,y:B: each bj keeps its type by lemma 2.14, and y has type B by Var. The induction hypothesis for b at σ+ gives Γ,y:Bb[σ+]:C, and b[σ+]=b[σ] by lemma 141.2(1). Then Lam derives Γλy:B.b[σ]:BC, whose subject is (λy:B.b)[σ] by the abstraction clause of definition 2.41. ◻

Definition 141.4 — Composition and identity

Let σ:ΓΔ and τ:ΔΘ with Θ=z1:C1,,zn:Cn and τ=(c1,,cn). Define τσ:=(c1[σ],,cn[σ]):ΓΘ,idΓ:=(x1,,xn):ΓΓ.

Both lists are substitutions: Γck[σ]:Ck by lemma 141.3, and Γxi:Ai by Var. Compute one composite before proving anything about it. Let Γ=f:22, x:2,Δ=y:2,Θ=z:22, and take σ=(fx):ΓΔ and τ=(λw:2.y):ΔΘ. Then τσ=((λw:2.y)[(fx)/y])=(λw:2.fx):ΓΘ. Now let Θe:2 be e:=ztt and compare the two ways of reaching a Γ-term: e[τσ]=(λw:2.fx)tt,e[τ][σ]=((λw:2.y)tt)[(fx)/y]=(λw:2.fx)tt. They agree. The composite τσ is “first σ, then τ” as arrows, and its action on terms is “first τ, then σ”. That the two calculations always agree is the following lemma.

Lemma 141.5 — Action law

For σ:ΓΔ, τ:ΔΘ, and Θe:A, e[τσ]=αe[τ][σ].

Proof of Lemma 141.5 — Action law

Proof. Induct on e, proving the equation for all Γ,Δ,Θ and all σ,τ at once, and choosing every bound name fresh for Γ, Δ, Θ, and the ranges of σ, τ, and τσ (convention 2.3).

Variable case. For e=zk, zk[τσ]=def. ck[σ]=def. actionzk[τ][σ].

Application case. For e=e1e2 and any substitution θ, (e1e2)[θ]=e1[θ]e2[θ] by the application clause, so both sides are applications whose two components are equal by the induction hypotheses for e1 and e2. The constants are fixed by every substitution, and the conditional recurses in its three subterms as application does.

Binder case. For e=λy:B.b with y fresh as above, y lies in the domain of neither substitution, so the abstraction clause of definition 2.41 reads (λy:B.b)[θ]=λy:B.b[θ] for θ{τσ, τ, σ}. Write σ+:=(σ,yy) and τ+:=(τ,yy), substitutions Γ,y:BΔ,y:B and Δ,y:BΘ,y:B. Two facts are used:

  1. t[θ]=t[θ+] for every term t and each θ, by lemma 141.2(1), since y is in no domain;

  2. (τσ)+=τ+σ+ componentwise: at zk the left side is ck[σ] and the right side is ck[σ+], equal by (1); at y both sides are y.

Then (λy:B.b)[τσ]=(1)λy:B.b[(τσ)+]=(2)λy:B.b[τ+σ+]=IHλy:B.b[τ+][σ+]=(1)(λy:B.b)[τ][σ], the induction hypothesis being applied to b with the substitutions σ+ and τ+. ◻

Example 141.6 — Capture

The binder case is where a careless definition fails. Take Θ=z:2, e:=λx:2.z, τ=(y):ΔΘ with Δ=y:2, and σ=(x):ΓΔ with Γ=x:2. Then e[τ]=λx:2.y, and applying σ without renaming would produce λx:2.x, a closed term, in which the free x of Γ has been captured. The fresh choice of the bound name gives λx:2.x instead, and this is also e[τσ] with τσ=(x).

Proposition 141.7 — The substitution algebra

For σ:ΓΔ, τ:ΔΘ, and ρ:ΘΛ:

  1. (ρτ)σ=ρ(τσ);

  2. idΔσ=σ and σidΓ=σ.

Proof of Proposition 141.7 — The substitution algebra

Proof. Let Λ=w1:D1,,wp:Dp and ρ=(d1,,dp). Compare the k-th components: ((ρτ)σ)(wk)=def.dk[τ][σ]=actionlawdk[τσ]=def.(ρ(τσ))(wk). For the identities, (idΔσ)(yj)=yj[σ]=bj by the variable clause, and (σidΓ)(yj)=bj[x1/x1,,xn/xn]=bj by lemma 141.2(2). ◻

Exercise 141.1

★☆☆ With Γ=f:22,x:2, Δ=y:2, Θ=z:22, σ=(fx), and τ=(λw:2.y) as in (141.1), let Λ=u:2 and ρ=(zff):ΘΛ. Compute (ρτ)σ and ρ(τσ) as lists of terms and confirm that they coincide.

Proposition 141.7 mentions neither terms nor the action: it is three equations between composites of arrows. The definition that follows records what the proposition uses: objects, typed arrows, composition, identities, and the three equations; nothing else. The action e[σ] is not part of it; it returns in section 141.6 as additional structure over a category.

The definition

Definition 141.8 — Category

A category C consists of

  1. a collection of objects;

  2. for each pair of objects a,b a set homC(a,b), the hom-set, of arrows from a to b; we write f:ab for fhomC(a,b);

  3. for each triple a,b,c a composition function :homC(b,c)×homC(a,b)homC(a,c), written gf for f:ab and g:bc;

  4. for each object a an identity arrow ida:aa;

such that, for all objects a,b,c,d and all f:ab, g:bc, h:cd, (hg)f=h(gf),idbf=f,fida=f.

Composition is written in the order of functions, gf meaning “first f, then g”; chapter 3 wrote the composition of type substitutions in the opposite, diagrammatic order S;T. Each hom-set is a set; the objects need not form a set, and a category whose objects do form a set is called small. Size is noted at the places where it matters.

Example 141.9 — The category of contexts

Ctx has the contexts of chapter 2 as objects, the substitutions σ:ΓΔ of definition 141.1 as arrows, and the composition and identities of definition 141.4. Proposition 141.7 is exactly (141.2). The objects form a set, so Ctx is small.

Example 141.10 — Sets and functions

Set has sets as objects, functions as arrows, composition (gf)(x)=g(f(x)), and identities idA(x)=x. The three laws hold pointwise: each side of each equation is a function, and evaluating both sides at an arbitrary x gives the same element.

A category is a monoid whose multiplication is typed. In Ctx the product τσ exists only when the target of σ is the source of τ: in (141.1) the middle context Δ is what makes the two lists fit, and (fx):ΓΔ has no composite with an arrow out of any other context. The objects are the types of this partial multiplication. With one object the typing is vacuous.

Proposition 141.11 — One-object categories

Let C have exactly one object . Then M:=homC(,) with multiplication and unit id is a monoid. Conversely every monoid (M,,e) is the hom-set of a one-object category with gf:=gf and id:=e.

Proof of Proposition 141.11 — One-object categories

Proof. With one object, every pair of arrows is composable, so is a total binary operation on M, and (141.2) says precisely that it is associative with two-sided unit id. Conversely, the monoid axioms for (M,,e) are (141.2) with a=b=c=d=. ◻

Besides the three equations, the definition asks for two things: an identity arrow at every object, and a composite for every two arrows whose types match. For a subcollection of the arrows of Ctx these are closure conditions. The following three examples keep the objects of Ctx and restrict the arrows: the first restriction is a category, the second has no identities, and the third is not closed under composition.

Example 141.12 — Fewer arrows: renamings

Keep the objects of Ctx and keep only the substitutions whose components are variables, σ=(xi1,,xim) with Aij=Bj. Every identity is such a list, and the composite of two such lists is again one, because xi[σ] is a component of σ. So the restriction is a category, the category of renamings; in general a subcategory of C is a category with some of the objects and some of the arrows of C, composed as in C. The extensions ΔΓ of chapter 49 are the renamings ΔΓ whose components are the variables of Γ in order, the weakenings.

Example 141.13 — No identities: closed substitutions

Keep the objects of Ctx and keep only the substitutions all of whose components are closed terms. The collection is closed under composition: if τ has closed components ck, then ck[σ]=ck by lemma 141.2(3), so τσ has the same closed components. But idΓ has the variables of Γ as components, which are not closed unless Γ is empty. The collection is closed under composition but has no identity at any nonempty context, so it is not a category.

Example 141.14 — Not closed: normal substitutions

Keep the objects of Ctx and keep only the substitutions whose components are normal forms, terms with no p reduct. Every identity qualifies. Take Δ=y:22, Θ=z:2, τ=(ytt):ΔΘ, whose component is normal, and σ=(λw:2.w):ΓΔ for any Γ. Then τσ=((ytt)[σ])=((λw:2.w)tt), a redex. The collection contains every identity but is not closed under composition, so it is not a category. The restriction is a natural one to try, because normal forms are the terms one computes with; but substituting a normal form into a normal form creates the redexes that normalization then removes.

Three further examples change what an arrow carries rather than which arrows there are.

Example 141.15 — At most one arrow: preorders

A preorder is a set P with a reflexive and transitive relation . Take the elements of P as objects and let homP(a,b) have exactly one element when ab and be empty otherwise. Composition of ab and bc is the unique arrow ac, which exists by transitivity; ida is aa, which exists by reflexivity. The three laws hold because each side is an element of a set with at most one element. Conversely, a category with at most one arrow between any two objects is a preorder on its objects. The generality order σ1σ2 on type schemes (chapter 3) is a preorder, and so is extension of contexts, read with an arrow ΔΓ exactly when ΔΓ: this preorder is the subcategory of weakenings in example 141.12, which has at most one arrow between any two contexts. When arrows carry no data, the laws are automatic and the whole content is which hom-sets are inhabited.

Example 141.16 — Arrows with data: reduction paths

Take the terms of chapter 2 as objects and, as arrows ee, the finite reduction sequences e=e0pe1ppen=e, with n0. Composition is concatenation of sequences and ide is the empty sequence at e; concatenation is associative and the empty sequence is neutral, so this is a category, written Paths. It is not a preorder: in the context z:2, the term (λx:2.x)((λy:2.tt)z) reduces to tt by contracting the outer redex first, through (λy:2.tt)z, and by contracting the inner redex first, through (λx:2.x)tt; the two intermediate terms differ, so these are two different sequences of length two, hence two arrows with the same source and target. Forgetting the sequence and keeping only its existence gives the preorder p of chapter 2: the same objects, each hom-set collapsed as in example 141.15.

The construction of Paths used nothing about reduction except that it is a relation on terms: a set of vertices and a set of edges.

Definition 141.17 — Free category on a graph

A graph G, in the sense of vertices and edges rather than the graph of a function, consists of a set of vertices, a set of edges, and two functions assigning to each edge a source and a target vertex. The free category Free(G) has the vertices as objects and, as arrows vv, the finite paths v=v0v1vn=v of edges, with concatenation as composition and the empty path at v as idv. Concatenation of paths is associative and the empty path is neutral on either side, so (141.2) hold.

Paths is the free category on the graph whose vertices are terms and whose edges are the one-step reductions.

Example 141.18 — A finite graph, an infinite category

Let G have two vertices a,b, two edges ϵ1,ϵ2:ab, and one edge :aa. In Free(G), homFree(G)(a,a)={nn0},homFree(G)(a,b)={ϵini=1,2, n0},homFree(G)(b,a)=,homFree(G)(b,b)={idb}, where n is the path that traverses n times and 0=ida. Three edges generate infinitely many arrows, and ϵ1 and ϵ2 are different arrows with the same source and target: an arrow records the path taken, not only that the target is reachable. Collapsing each hom-set to at most one arrow, as in example 141.15, leaves the reachability preorder aa, ab, bb.

Example 141.19 — Same objects, other arrows: relations

Rel has sets as objects and relations RA×B as arrows AB, composed by SR:={(x,z)y. (x,y)R(y,z)S}for R:AB, S:BC, with idA={(x,x)xA}. Associativity is the commutation of two existential quantifiers: for T:CD, (x,w)(TS)Ry.(x,y)R(z.(y,z)S(z,w)T)yz.(x,y)R(y,z)S(z,w)Tz.(y.(x,y)R(y,z)S)(z,w)T(x,w)T(SR). For the identities, (x,y)idBR iff y.(x,y)Ry=y iff (x,y)R, and (x,y)RidA iff x.x=x(x,y)R iff (x,y)R. A function f:AB is the relation {(x,f(x))xA}, and composing two such relations gives the graph of the composite function, so Set sits inside Rel with the same objects. Set and Rel have the same objects and are different categories: the arrows, not the objects, carry the structure.

Exercise 141.2

★☆☆ Show that a relation RA×B is the graph of a function AB if and only if idARR and RRidB, where R:={(y,x)(x,y)R}. Express each inclusion as a quantified statement first, then identify it with totality or with single-valuedness.

Example 141.20 — Monoids and homomorphisms

A monoid is a one-object category by proposition 141.11; monoids also form a category. Mon has monoids (M,,e) as objects and monoid homomorphisms as arrows: functions h:MN with h(xy)=h(x)h(y) and h(e)=e. Composition and identities are those of Set, and the laws hold because they hold in Set; what has to be checked is closure, namely that gh is again a homomorphism: g(h(xy))=g(h(x)h(y))=g(h(x))g(h(y)) and g(h(e))=g(e)=e.

Isomorphisms, duality, and terminal objects

Definition 141.21 — Isomorphism

An arrow f:ab is an isomorphism if there is g:ba with gf=ida and fg=idb. Objects a,b are isomorphic, ab, when such an f exists.

Lemma 141.22 — Inverses are unique

If gf=ida and fg=idb, then g=g.

Proof of Lemma 141.22 — Inverses are unique

Proof. g=unitgidb=hyp.g(fg)=assoc.(gf)g=hyp.idag=unitg. ◻

The inverse of an isomorphism f is therefore well defined and is written f1. In Set the isomorphisms are the bijections; in a preorder ab means ab and ba; in a one-object category they are the invertible elements of the monoid.

Exercise 141.3

★★☆ Show that ΓΔ in Ctx if and only if Δ is obtained from Γ by permuting declarations and renaming variables. Hint: if τσ=idΔ then τ(yj)[σ]=yj for each j, and a term whose substitution instance is a variable is itself a variable.

The paths p:a=b of chapter 30 compose, have inverses p1, and satisfy the laws of a category up to higher paths. The strict form of that structure has a name.

Definition 141.23 — Groupoid

A groupoid is a category in which every arrow is an isomorphism.

A one-object groupoid is a group: by proposition 141.11 it is a monoid, and every element has an inverse. A preorder is a groupoid exactly when its relation is symmetric, that is, an equivalence relation: the arrow ab has an inverse only if ba, and conversely when ba the two composites are identities because each hom-set has at most one element. Every category C contains a groupoid with the same objects, whose arrows are the isomorphisms of C: identities are isomorphisms, and the composite of isomorphisms f,g is an isomorphism with inverse f1g1. For Ctx, exercise 141.3 identifies these arrows as the permutation-renamings.

Example 141.24 — Reduction paths do not form a groupoid

An arrow of Paths of positive length has no inverse: the concatenation of a path of length n1 with any path has length at least n, while ide is the empty path, of length 0. For the same length reason, no free category on a graph with an edge is a groupoid. For typed terms more is true: there is no path from e back to e of positive length at all, since repeating one would give an infinite reduction sequence from e, which strong normalization (theorem 2.43) forbids.

Every definition of this section has a mirror image obtained by reversing all arrows, and the reversal is itself a category.

Definition 141.25 — Opposite category

For a category C, the category Cop has the same objects, homCop(a,b):=homC(b,a), the identities of C, and composition gopf:=fg.

The laws for Cop follow from those of C; for associativity, (hopg)opf=def.f(gh)=assoc.(fg)h=def.hop(gopf), and the unit laws likewise; also (Cop)op=C. An arrow ab in Cop is an arrow ba in C; nothing else changes. A statement S about an arbitrary category, formulated in terms of objects, arrows, composition, and identities, has a dual statement Sop obtained by reversing every arrow and every composite. If S holds in every category, then so does Sop: applying S to Cop yields Sop for C. Lemma 141.27 is proved once and used twice in this way. Concretely, for RA×B put R:={(b,a)(a,b)R}. Directly from example 141.19, (SR)=RS,idA=idA,(R)=R. Thus converse is an isomorphism RelRelop that is the identity on objects and is its own inverse. A preorder’s opposite is the same set with reversed.

Definition 141.26 — Terminal and initial objects

An object 1 of C is terminal if for every object a there is exactly one arrow a1. Dually, an object 0 is initial if for every object a there is exactly one arrow 0a; an initial object of C is a terminal object of Cop.

Lemma 141.27 — Uniqueness up to isomorphism

Any two terminal objects are isomorphic by a unique isomorphism, and likewise any two initial objects.

Proof of Lemma 141.27 — Uniqueness up to isomorphism

Proof. Let 1 and 1 be terminal and let t:11 and t:11 be the unique arrows. Then tt:11 and id1 are both arrows 11, so they are equal by uniqueness; likewise tt=id1. Any isomorphism 11 is an arrow 11, hence equals t. The statement for initial objects is the dual. ◻

Exercise 141.4

★☆☆ Write out the proof of lemma 141.27 for initial objects without mentioning Cop, and check that it is the displayed proof with every composite reversed.

In Ctx the empty context is terminal, the only substitution Γ being the empty list. Ctx has no initial object: for any context Γ, the two lists (tt) and (ff) are distinct substitutions Γ2, where A:=(x:A) denotes the one-declaration context; so no hom-set out of Γ into 2 has exactly one element. In Set a one-element set is terminal and the empty set is initial; in a preorder a greatest element is terminal and a least element is initial.

Cancellation and test objects

Injectivity of a function f:AB is a statement about elements: f(x)=f(x) implies x=x. In a category there are no elements, only arrows, so the statement must be made about arrows into A.

Definition 141.28 — Monomorphism, epimorphism

An arrow f:ab is a monomorphism (is monic) if for every object t and all u,u:ta, fu=fu  u=u. It is an epimorphism (is epic) if for every object t and all v,v:bt, vf=vf implies v=v.

The quantifier over t is the point: f is monic when it cannot identify two arrows from any test object t. An epimorphism in C is a monomorphism in Cop: the two notions are dual, and every fact proved about one for all categories holds for the other. In Set one test object suffices for each.

Proposition 141.29 — Cancellation in

A function f:AB is monic if and only if it is injective, and epic if and only if it is surjective.

Proof of Proposition 141.29 — Cancellation in

Proof. Let 1:={} be a one-element set. A function u:1A is determined by the single element u()A, and every element xA is u() for exactly one such u, namely the constant function at x, written x. Then fx=f(x).

Suppose f is monic and f(x)=f(x). Then fx=(141.3)f(x)=hyp.f(x)=(141.3)fx, so x=x by cancellation at t=1, and x=x. Conversely suppose f injective and fu=fu for u,u:tA. For each st, f(u(s))=f(u(s)), so u(s)=u(s); hence u=u.

Let 2:={0,1}. Suppose f is epic and let yB; define v,v:B2 by v(z)=1 for all z, and v(z)=1 if z is in the image of f and v(z)=0 otherwise. Then vf=vf, both being constantly 1 on A, so v=v by cancellation at t=2; evaluating at y gives v(y)=1, so y is in the image of f. Conversely suppose f surjective and vf=vf for v,v:Bt. For each yB choose x with f(x)=y; then v(y)=choicev(f(x))=hyp.v(f(x))=choicev(y), so v=v. ◻

The proof used two facts about Set: arrows 1A are the elements of A, and arrows B2 are the subsets of B. Each is a statement that a set-theoretic notion is represented by maps from or to one fixed test object.

Definition 141.30 — Global elements

Let C have a terminal object 1. A global element of an object a is an arrow 1a.

In Set the global elements of A are its elements. In Ctx a global element Γ is a list of closed terms (ai:Ai)i, one for each declaration of Γ. Call such a list an environment for Γ: it assigns a closed term to each variable, as the environments η of chapter 12 assign an element of a domain to each variable. Evaluating Γe:A in the environment ρ is the action e[ρ], a closed term.

Exercise 141.5

★☆☆ An arrow f:ab is a split monomorphism if some r:ba satisfies rf=ida. Show that a split monomorphism is monic, and that an arrow which is both a split monomorphism and epic is an isomorphism. Each proof is a chain of at most four equalities.

In Set an arrow that is both monic and epic is a bijection, hence an isomorphism. In a preorder this fails: every arrow ab is monic and epic, the hom-sets having at most one element, and it is an isomorphism only when also ba. It also fails when arrows carry data.

Proposition 141.31 — Monic and epic but not invertible

In Mon, the inclusion i:(N,+,0)(Z,+,0) is monic and epic but not an isomorphism.

Proof of Proposition 141.31 — Monic and epic but not invertible

Proof. i is injective, hence monic by the argument of proposition 141.29, which used only that arrows are functions. It is not an isomorphism: an inverse j:ZN would be a homomorphism with ji=idN, and j(1)+j(1)=hom.j(1+(1))=j(0)=hom.0 gives j(1)=0 in N, since 0 is the only element of N with an additive inverse, while j(1)=j(i(1))=1.

To see that i is epic, let v,v:ZM be homomorphisms with vi=vi, that is, v(n)=v(n) for all n0. For n0, v(n)v(n)=hom.v(n+n)=v(0)=hom.e,v(n)v(n)=e likewise, so v(n) is a two-sided inverse of v(n) in M. The same holds for v(n) and v(n)=v(n). Inverses in a monoid are unique, by the calculation of lemma 141.22 read in the one-object category of M. Hence v(n)=v(n), and v=v. ◻

Functors

A monoid homomorphism preserves the multiplication and the unit. The corresponding notion for categories preserves composition and identities, and must also respect the typing of arrows.

Definition 141.32 — Functor

A functor F:CD assigns to each object a of C an object F(a) of D and to each arrow f:ab an arrow F(f):F(a)F(b), such that for all f:ab and g:bc, F(gf)=F(g)F(f),F(ida)=idF(a).

Example 141.33 — Homomorphisms and monotone maps

By proposition 141.11, a functor between one-object categories is exactly a monoid homomorphism: (141.4) is the homomorphism condition. By example 141.15, a functor between preorders is exactly a monotone map: the object assignment is a function F:PQ, the arrow assignment says that ab implies F(a)F(b), and the laws are automatic because hom-sets have at most one element.

Example 141.34 — Forgetting

U:MonSet sends a monoid to its carrier and a homomorphism to itself as a function. Both laws hold because composition and identities in Mon are those of Set.

A functor out of a free category is determined by its values on the edges, and any choice of values extends to one.

Proposition 141.35 — Functors out of a free category

Let G be a graph and D a category. An assignment of an object F(v) to each vertex and an arrow F(ϵ):F(v)F(v) to each edge ϵ:vv extends to exactly one functor F:Free(G)D.

Proof of Proposition 141.35 — Functors out of a free category

Proof. A path is a composite of its edges, ϵnϵ1, and the empty path is an identity, so a functor F must send the path to F(ϵn)F(ϵ1) and the empty path to idF(v); this fixes F on every arrow. Conversely that assignment is a functor: it sends the empty path to an identity, and it sends the concatenation of two paths to the composite of their images. When one of the paths is empty this is a unit law in D; when both are nonempty, both sides are the composite of the same sequence of edge images, bracketed in two ways, and repeated use of associativity in D identifies any two bracketings of one sequence. ◻

For example, the assignment sending every term to the single object and every one-step reduction to 1 in the one-object category of the monoid (N,+,0) extends to the functor Paths(N,+,0) that sends a reduction sequence to its length.

The functor that relates syntax to environments is the following. For a context Γ, let Env(Γ):=homCtx(,Γ) be the set of environments for Γ, its global elements. A substitution σ:ΓΔ turns an environment for Γ into one for Δ: if ρ assigns closed terms to the variables of Γ, then σρ assigns to yj the closed term σ(yj)[ρ].

Proposition 141.36 — Environments form a functor

Env:CtxSet, with Env(Γ):=homCtx(,Γ) and Env(σ)(ρ):=σρ, is a functor.

Proof of Proposition 141.36 — Environments form a functor

Proof. For σ:ΓΔ, τ:ΔΘ, and ρEnv(Γ), Env(τσ)(ρ)=(τσ)ρ=assoc.τ(σρ)=Env(τ)(Env(σ)(ρ)),Env(idΓ)(ρ)=idΓρ=unitρ, using the laws of proposition 141.7. ◻

Nothing in that proof mentioned terms. Replacing by any object r of any category gives the same functor.

Proposition 141.37 — Hom-functors

For an object r of C, the assignments homC(r,)(a):=homC(r,a), where marks the argument that varies, and homC(r,)(f)(u):=fu for f:ab and u:ra define a functor homC(r,):CSet.

Proof of Proposition 141.37 — Hom-functors

Proof. The calculation of proposition 141.36 with replaced by r: (gf)u=g(fu) and idau=u. ◻

Functors compose: for F:CD and G:DE, the assignments aG(F(a)) and fG(F(f)), written GF, satisfy (141.4) because each of F and G does, and the identity assignment is a functor CC. Small categories and functors therefore form a category Cat; the restriction to small categories keeps each hom-set a set.

Exercise 141.6

★★☆ Show that a functor sends isomorphisms to isomorphisms, with F(f1)=F(f)1. Then show that the converse fails: give a functor F:PQ between preorders and an arrow f of P such that F(f) is an isomorphism but f is not.

Variance and presheaves

Environments vary with the context in the direction of the arrows. Terms vary against it. For a fixed type A and a context Γ, let TmA(Γ):={eΓe:A} be the set of terms of type A in context Γ, up to α-equivalence. A substitution σ:ΓΔ acts by ee[σ], which by lemma 141.3 is a function TmA(Δ)TmA(Γ), from the target of σ to its source. The functor laws hold in this reversed form: lemma 141.5 says e[τσ]=e[τ][σ], and the identity law of proposition 141.7 says e[idΓ]=e. Rather than a second definition of functor with the arrows reversed, the category is reversed: a functor out of Cop (definition 141.25) is a functor that sends an arrow ab of C to an arrow from the image of b to the image of a.

Definition 141.38 — Presheaf

A presheaf on C is a functor K:CopSet. Unwinding definition 141.25, a presheaf assigns a set K(a) to each object and a function K(f):K(b)K(a) to each arrow f:ab of C, with K(gf)=K(f)K(g),K(ida)=idK(a).

Proposition 141.39 — Terms form a presheaf

For each type A, TmA with TmA(σ)(e):=e[σ] is a presheaf on Ctx.

Proof of Proposition 141.39 — Terms form a presheaf

Proof. For σ:ΓΔ, τ:ΔΘ, and eTmA(Θ), TmA(τσ)(e)=e[τσ]=actionlawe[τ][σ]=TmA(σ)(TmA(τ)(e)),TmA(idΓ)(e)=e[idΓ]=unite, by lemma 141.5 and proposition 141.7. ◻

Presheaves combine pointwise. For presheaves K,K on C, the assignment aK(a)×K(a) with (K×K)(f)(u,u):=(K(f)(u),K(f)(u)) satisfies (141.5) in each component, so it is a presheaf, the product K×K. For types A and B, the product TmAB×TmA has as value at Γ the set of pairs (e1,e2) with Γe1:AB and Γe2:A. Application sends such a pair to e1e2TmB(Γ) by App, and the application clause of definition 2.41 is the equation (e1e2)[σ]=e1[σ]e2[σ]for every σ:ΓΔ.

A presheaf is a system of sets indexed by the objects and acted on contravariantly by the arrows. The semantic values of chapter 49, which are given at every context and restrict along every extension ΔΓ, are presheaves on the preorder of extensions of example 141.15, read with its arrow ΔΓ: restriction goes from the value at Γ to the value at Δ, against the arrow, and the restriction law aΓ=a together with the compatibility of successive restrictions is (141.5).

Exercise 141.7

★★☆ Write out the verification that K×K satisfies (141.5) for arbitrary presheaves K,K. Then show that the conditional defines, for each Γ, a function Tm2(Γ)×TmA(Γ)×TmA(Γ)TmA(Γ), (e,e1,e2)if(e;e1;e2), and state and prove the analogue of (141.6) for it.

Natural transformations

Application is a family of functions appΓ:TmAB(Γ)×TmA(Γ)TmB(Γ), one for each context, and (141.6) says that substituting and then applying gives the same term as applying and then substituting: the family commutes with the action of every arrow. Other families of maps between term sets do not.

Example 141.40 — A family that inspects syntax

For each Γ define cΓ:Tm2(Γ)Tm2(Γ) by cΓ(e):=tt if e is a variable and cΓ(e):=e otherwise. Let Δ=y:2, Γ=f:22, x:2, and σ=(fx):ΓΔ. Then cΓ(y[σ])=cΓ(fx)=fx,cΔ(y)[σ]=tt[σ]=tt. Substituting first and inspecting second differs from inspecting first and substituting second. The family is defined at every context but is not compatible with the arrows between contexts.

The condition that a family commute with every arrow is an equation between two composites, and such equations are often displayed as diagrams. The equation gf=kh for f:ab, g:bd, h:ac, k:cd is drawn as Mathematical diagramDiagram and the square is said to commute. A diagram is a directed graph whose vertices are objects and whose edges are arrows; it commutes when, for every two paths with the same start and end, the composites along the two paths are equal. A diagram is therefore a finite list of equations, one for each pair of parallel paths, and a diagram is always displayed together with the equations it abbreviates. The picture saves bookkeeping when several equations share arrows; it proves nothing by itself.

Definition 141.41 — Natural transformation

Let F,G:CD be functors. A natural transformation α:FG is a family of arrows αa:F(a)G(a), one for each object a of C, such that for every arrow f:ab of C, G(f)αa=αbF(f)in homD(F(a),G(b)). The arrows αa are the components of α, and (141.7) is naturality at f. Drawn as a square, Mathematical diagramDiagram commutes for every f.

The arrow is the symbol chapter 26 used for context substitutions f:ΓΔ; here it relates two functors and never two contexts, which are related by . Write Nat(F,G) for the collection of natural transformations FG; when C is small, it is a set because its components form a C-indexed family of sets.

Two instances outside syntax show the condition at its simplest. Between monotone maps F,G:PQ of preorders, a natural transformation exists exactly when F(a)G(a) for every a, and then it is unique: the components are the arrows F(a)G(a), and (141.7) is automatic because homQ(F(a),G(b)) has at most one element. In Set, with Id the identity functor and D(A):=A×A on objects and D(f)(x,y):=(f(x),f(y)) on arrows, the diagonal δA(x):=(x,x) is natural IdD: D(f)(δA(x))=(f(x),f(x))=δB(f(x)).

For presheaves K,K:CopSet the arrows reverse: α:KK has components αa:K(a)K(a) and naturality at f:ab in C reads K(f)αb=αaK(f) as functions K(b)K(a).

Example 141.42 — Application is natural

With K:=TmAB×TmA and K:=TmB, naturality of app at σ:ΓΔ is, for (e1,e2)K(Δ), K(σ)(appΔ(e1,e2))=(e1e2)[σ]=(141.6)e1[σ]e2[σ]=appΓ(K(σ)(e1,e2)). The family c of example 141.40 is not natural: the displayed calculation there is the failure of (141.7) at one σ. Naturality is the exact form of “defined uniformly in the context”: a natural family may use its argument only through the operations that substitution preserves.

Exercise 141.8

★☆☆ For a closed term t:A, define atΓ:TmAB(Γ)TmB(Γ) by atΓ(e):=et. Show that at is natural, and identify the one property of t the proof uses.

Natural transformations compose componentwise. For α:FG and β:GH, set (βα)a:=βaαa. Naturality at f:ab is H(f)βaαa=nat. ββbG(f)αa=nat. αβbαbF(f), and idF with components idF(a) is natural because both sides of (141.7) are F(f). Associativity and the unit laws hold componentwise, so functors CD and natural transformations form a category, the functor category [C,D], with hom[C,D](F,G)=Nat(F,G). Size matters here: Nat(F,G) is a family indexed by the objects of C, and it is a set when C is small, which Ctx and every preorder in this chapter are. The presheaf category is [Cop,Set]:=[Cop,Set].

Proposition 141.43 — Natural isomorphisms

α:FG is an isomorphism in [C,D] if and only if every component αa is an isomorphism in D.

Proof of Proposition 141.43 — Natural isomorphisms

Proof. If βα=idF and αβ=idG, then at each a the components satisfy βaαa=idF(a) and αaβa=idG(a). Conversely suppose each αa has an inverse αa1. The family βa:=αa1 is natural: for f:ab, F(f)βa=βbαb=idβbαbF(f)βa=nat. αβbG(f)αaβa=αaβa=idβbG(f). Then βα and αβ are the identities componentwise. ◻

Such an α is a natural isomorphism, and F and G are naturally isomorphic.

Equivalence of categories

Two categories can agree in everything that is said about arrows and still have different objects. Ctx has as objects all contexts, under all choices of variable names; a context and its renaming are isomorphic by the renaming substitution and its inverse, and nothing said in terms of arrows distinguishes them. The comparison of Ctx with its nameless form requires the following notions.

Definition 141.44 — Full, faithful, essentially surjective

A functor F:CD is

  1. faithful if for all objects a,b the function fF(f) from homC(a,b) to homD(F(a),F(b)) is injective;

  2. full if each such function is surjective;

  3. fully faithful if it is both;

  4. essentially surjective if every object d of D is isomorphic to F(c) for some object c of C.

A full subcategory of D is a subcategory whose hom-sets are exactly those of D; the functor including it into D is fully faithful.

The forgetful functor U:MonSet is faithful, because a homomorphism is a function, and not full, because not every function between carriers is a homomorphism. A monotone map F:PQ is always faithful, hom-sets having at most one element, and it is full exactly when F(a)F(b) implies ab. The renaming category of example 141.12 is not a full subcategory of Ctx: it has the same objects, but the arrow (fx) of (141.1) is not a renaming.

Definition 141.45 — Equivalence of categories

A functor F:CD is an equivalence if there are a functor G:DC and natural isomorphisms η:IdCGF and ε:FGIdD. When there is a G with GF=IdC and FG=IdD as functors, F is an isomorphism of categories.

An isomorphism of categories is a bijection on objects and on each hom-set. An equivalence need not be: it identifies objects only up to isomorphism, which is the identification the arrows can see.

Theorem 141.46 — Characterization of equivalences

A functor F:CD is an equivalence if and only if it is fully faithful and essentially surjective.

Proof of Theorem 141.46 — Characterization of equivalences

Proof. Only if. Let G,η,ε be as in definition 141.45. Essential surjectivity: for an object d, εd:F(G(d))d is an isomorphism, so dF(c) with c:=G(d). Faithfulness: for f,f:ab with F(f)=F(f), naturality of η at f reads G(F(f))ηa=ηbf, so f=ηbisoηb1ηbf=nat. ηηb1G(F(f))ηa=hyp.ηb1G(F(f))ηa=nat. ηηb1ηbf=f. The functor G is faithful as well. If g,g:dd and G(g)=G(g), naturality of ε gives g=εdisogεdεd1=nat. εεdF(G(g))εd1=hyp.εdF(G(g))εd1=nat. εgεdεd1=εdisog. Fullness: let g:F(a)F(b) and set f:=ηb1G(g)ηa:ab. Then G(F(f))=nat. ηηbfηa1=def. fηbηb1G(g)ηaηa1=isoG(g), and faithfulness of G gives F(f)=g.

If. Let F be fully faithful and essentially surjective. The inverse functor is built in four stages.

Objects. By essential surjectivity, choose for each object d of D an object G(d) of C and an isomorphism εd:F(G(d))d; this choice is discussed after the proof.

Arrows. For an arrow g:dd, the composite εd1gεd is an arrow F(G(d))F(G(d)), and since F is fully faithful there is exactly one arrow G(g):G(d)G(d) with F(G(g))=εd1gεd. G is a functor: F(G(idd))=(141.8)εd1εd=isoidF(G(d))=functorF(idG(d)), and for g:dd, F(G(g)G(g))=F(G(g))F(G(g))=(141.8)εd1gεdεd1gεd=εd1(gg)εd=(141.8)F(G(gg)), so faithfulness of F gives the two functor laws.

The counit. Equation (141.8) rearranged, gεd=εdF(G(g)), is naturality of ε:FGIdD, and its components are isomorphisms, so ε is a natural isomorphism by proposition 141.43.

The unit. For each object c of C, εF(c):F(G(F(c)))F(c) is an isomorphism between two objects in the image of F, so by full faithfulness there is exactly one ηc:cG(F(c)) with F(ηc)=εF(c)1. It is an isomorphism: for the unique θ with F(θ)=εF(c), F(θηc)=εF(c)εF(c)1=F(idc), so θηc=idc by faithfulness, and likewise ηcθ=idG(F(c)). Naturality of η at f:cc is G(F(f))ηc=ηcf; apply F to both sides: F(G(F(f)))F(ηc)=(141.8)εF(c)1F(f)εF(c)εF(c)1=εF(c)1F(f)=F(ηc)F(f), and faithfulness of F gives the equation itself. ◻

Lemma 141.47 — Inverse functors are unique up to natural isomorphism

Let F:CD be an equivalence. Suppose (G,η,ε) and (G,η,ε) are two choices of the data in definition 141.45. Then G and G are naturally isomorphic.

Proof of Lemma 141.47 — Inverse functors are unique up to natural isomorphism

Proof. For each object d of D, define αd:=G(εd)ηG(d):G(d)G(d). Both factors are isomorphisms. For g:de, functoriality, naturality of ε, and naturality of η give G(g)αd=def.G(gεd)ηG(d)=nat. εG(εe)G(F(G(g)))ηG(d)=nat. ηG(εe)ηG(e)G(g)=def.αeG(g). Thus α:GG is natural, and proposition 141.43 makes it a natural isomorphism. ◻

The “if” direction chose one object G(d) and one isomorphism εd for each d. When no rule singles them out, this is a use of the axiom of choice, over a class when the objects of D do not form a set; and the inverse functor G depends on the choices, two inverse functors from different choices being naturally isomorphic by lemma 141.47. In the case that follows the choice is canonical, a renaming to x1,,xn, and no choice principle is used.

Proposition 141.48 — Named and nameless contexts

Let Ctx0 be the full subcategory of Ctx on the contexts whose variables are x1,x2,,xn in order, for some n. The inclusion Ctx0Ctx is an equivalence of categories, and not an isomorphism of categories.

Proof of Proposition 141.48 — Named and nameless contexts

Proof. The inclusion of a full subcategory is fully faithful. It is essentially surjective: a context Γ=y1:A1,,yn:An is isomorphic to X:=x1:A1,,xn:An by the renaming σ:=(y1,,yn):ΓX and τ:=(x1,,xn):XΓ, since τσ=(x1[σ],,xn[σ])=(y1,,yn)=idΓ and στ=(y1[τ],,yn[τ])=(x1,,xn)=idX. So the inclusion is an equivalence by theorem 141.46. It is not an isomorphism of categories: an isomorphism of categories is a bijection on objects, and the context y:2 is not in Ctx0. ◻

An object of Ctx0 is determined by its list of types, so the contexts of Ctx0 are nameless, a variable being its position, while its arrows are still lists of named terms. The proposition is the precise statement that the choice of variable names in a context adds nothing that substitutions can see. An inverse to the inclusion sends each context to its canonical renaming; a different choice of canonical names gives another inverse, naturally isomorphic to it by lemma 141.47.

Example 141.49 — Finite sets and their sizes

Let Fin be the full subcategory of Set on the finite sets, and Fin0 its full subcategory on the sets [n]:={0,,n1} for n0. The inclusion Fin0Fin is fully faithful, and essentially surjective because a set with n elements is in bijection with [n]; so it is an equivalence by theorem 141.46. It is not an isomorphism of categories, since {} is not of the form [n]. Here the choice in the “if” direction is genuine: an inverse functor must fix, for each finite set, one enumeration of its elements, and no enumeration is canonical; two inverses built from different enumerations are naturally isomorphic by lemma 141.47. Up to equivalence, a finite set is its size; up to isomorphism of categories it is not.

Exercise 141.9

★★★ For a preorder P, let P/ be the set of equivalence classes of the relation ab iff ab and ba, ordered by [a][b] iff ab. Show that [a][b] is well defined and antisymmetric, that the quotient map PP/ is an equivalence of categories, and that it is an isomorphism of categories exactly when is antisymmetric. For the generality order on type schemes of chapter 3, show that two schemes lie in one class exactly when they have the same monotype instances.

Representable functors and the Yoneda lemma

A functor K:CSet assigns to each object a a set of observations K(a), and to each arrow f:ab a way K(f) of transporting observations. The hom-functor homC(r,) of proposition 141.37 is the system whose observations at a are the arrows ra, transported by composition. The question this section answers is when an arbitrary K is of this form: when every observation in every K(a) arises, by transport along a unique arrow, from one observation at one test object r.

Definition 141.50 — Representation

A functor K:CSet is representable if there are an object r and a natural isomorphism ϕ:homC(r,)K. The pair (r,ϕ) is a representation, and u:=ϕr(idr)K(r) is its universal element.

The following calculation shows that ϕ is recovered from u alone, which is why u is called universal. For any natural ϕ:homC(r,)K, any object a, and any f:ra, ϕa(f)=unitϕa(fidr)=homfunctorϕa(homC(r,)(f)(idr))=nat. ϕatfK(f)(ϕr(idr))=K(f)(u). So every observation ϕa(f) is the transport K(f)(u) of the one element u, and when ϕ is an isomorphism every element of K(a) is such a transport for exactly one f. Calculation (141.9) did not use that ϕ is an isomorphism; it holds for every natural transformation out of a hom-functor, and that is the lemma. One more family is needed to state how the lemma varies with r.

Lemma 141.51 — Precomposition

For g:sr, the functions homC(g,)a:homC(r,a)homC(s,a), hhg, form a natural transformation homC(g,):homC(r,)homC(s,).

Proof of Lemma 141.51 — Precomposition

Proof. At f:ab both composites of (141.7) send hhomC(r,a) to fhg, by associativity. ◻

Theorem 141.52 — Yoneda

Let K:CSet be a functor and r an object. The functions ΦrK:Nat(homC(r,),K)K(r),ΦrK(ϕ):=ϕr(idr),ΨrK:K(r)Nat(homC(r,),K),ΨrK(u)a(f):=K(f)(u), are mutually inverse. The bijection is natural in K and in r: for every natural α:KK and every ϕ:homC(r,)K, and for every g:sr and every ψ:homC(s,)K, ΦrK(αϕ)=αr(ΦrK(ϕ)),ΦrK(ψhomC(g,))=K(g)(ΦsK(ψ)).

Proof of Theorem 141.52 — Yoneda

Proof. The proof has five parts: Ψ(u) is natural, ΦΨ and ΨΦ are identities, and the two naturality equations hold. The superscript and subscript of Φ,Ψ are omitted when fixed.

Ψ(u) is natural. Fix uK(r). The components Ψ(u)a:homC(r,a)K(a) are functions. For f:ab and hhomC(r,a), K(f)(Ψ(u)a(h))=K(f)(K(h)(u))=(141.4)K(fh)(u)=Ψ(u)b(fh)=Ψ(u)b(homC(r,)(f)(h)), which is (141.7) at f.

ΦΨ=idK(r). For uK(r), Φ(Ψ(u))=Ψ(u)r(idr)=K(idr)(u)=(141.4)idK(r)(u)=u.

ΨΦ is the identity. For natural ϕ:homC(r,)K, an object a, and f:ra, Ψ(Φ(ϕ))a(f)=K(f)(ϕr(idr))=(141.9)ϕa(f), so Ψ(Φ(ϕ)) and ϕ agree at every component and every argument.

Naturality in K. For α:KK and ϕ:homC(r,)K, the composite αϕ is natural, being a composite of natural transformations, and ΦrK(αϕ)=(αϕ)r(idr)=αr(ϕr(idr))=αr(ΦrK(ϕ)).

Naturality in r. For g:sr and ψ:homC(s,)K, the composite ψhomC(g,) is a natural transformation homC(r,)K by lemma 141.51, and ΦrK(ψhomC(g,))=ψr(homC(g,)r(idr))=ψr(idrg)=unitψr(g)=(141.9)K(g)(ψs(ids))=K(g)(ΦsK(ψ)), where (141.9) is applied to ψ at the arrow g:sr. ◻

The theorem applies to presheaves through the opposite category. A presheaf K:CopSet is a functor on Cop, and the hom-functor of Cop at r is homCop(r,)=homC(,r), the presheaf ahomC(a,r) with f:ab acting by hhf. Theorem 141.52 for Cop therefore reads:

Corollary 141.53 — Yoneda for presheaves

For a presheaf K on C and an object r, the functions Φ(ϕ):=ϕr(idr)K(r),Ψ(u)a(h):=K(h)(u)for h:ar, are mutually inverse between Nat(homC(,r),K) and K(r), naturally in K and in r.

Proof of Corollary 141.53 — Yoneda for presheaves

Proof. Every arrow and composite in the proof of theorem 141.52 is read in Cop; by definition 141.25, an arrow f:ab of Cop is an arrow ba of C and homCop(r,)(f) is hhf in C. No step of that proof used anything about C beyond (141.2), which hold in Cop. ◻

The two naturality equations (141.10) say one thing about a function of two arguments, an object and a functor. To state it as one naturality, form a category of pairs.

Definition 141.54 — Product category

For categories C and D, the category C×D has pairs (a,b) of objects as objects, pairs (f,g) of arrows f:aa, g:bb as arrows (a,b)(a,b), composition (f,g)(f,g):=(ff,gg), and identities (ida,idb). A functor out of a product category is called a bifunctor.

The laws hold in each coordinate separately. A bifunctor F:C×DE is a functor in each argument when the other is fixed, since F(f,idb) and F(ida,g) satisfy (141.4); and the two partial actions commute, F(f,idb)F(ida,g)=F(f,g)=F(ida,g)F(f,idb), because (f,idb)(ida,g)=(f,g)=(ida,g)(f,idb) in C×D.

Proposition 141.55 — The hom bifunctor

The assignments (a,b)homC(a,b) and, for f:aa and g:bb, homC(f,g):homC(a,b)homC(a,b),hghf, define a functor homC(,):Cop×CSet.

Proof of Proposition 141.55 — The hom bifunctor

Proof. An arrow (a,b)(a,b) of Cop×C is a pair of an arrow f:aa of C and an arrow g:bb, so the typing of homC(f,g) is as stated. For a second pair f:aa, g:bb, the composite in Cop×C is (ff,gg), and homC(ff,gg)(h)=(gg)h(ff)=assoc.g(ghf)f=homC(f,g)(homC(f,g)(h)); identities are preserved by the unit laws. ◻

Fixing a recovers proposition 141.37; fixing b gives the presheaf homC(,b). In Ctx the bifunctor reads homCtx(σ,τ)(ρ)=τρσ: substitute on both sides.

Lemma 141.56 — Naturality in each variable

Let F,G:C×DE be bifunctors and αa,b:F(a,b)G(a,b) a family of arrows. Then α is natural as a transformation FG if and only if it is natural in each variable with the other fixed: for all f:aa and g:bb, G(f,idb)αa,b=αa,bF(f,idb) and G(ida,g)αa,b=αa,bF(ida,g).

Proof of Lemma 141.56 — Naturality in each variable

Proof. Joint naturality at (f,g) specializes to the two separate conditions at (f,idb) and (ida,g). Conversely, since (f,g)=(ida,g)(f,idb) in C×D, G(f,g)αa,b=G(ida,g)G(f,idb)αa,b=nat. inaG(ida,g)αa,bF(f,idb)=nat. inbαa,bF(ida,g)F(f,idb)=αa,bF(f,g). ◻

Let C be small, so that [C,Set] is a category. Define two functors C×[C,Set]Set on objects by Ev(r,K):=K(r),Nt(r,K):=Nat(homC(r,),K), and on an arrow (g,α):(s,K)(r,K), that is g:sr and α:KK, by Ev(g,α)(u):=αr(K(g)(u)),Nt(g,α)(ψ):=αψhomC(g,).

Proposition 141.57 — Yoneda as a natural isomorphism

Ev and Nt are bifunctors, and the family ΦrK:Nt(r,K)Ev(r,K) of theorem 141.52 is a natural isomorphism NtEv.

Proof of Proposition 141.57 — Yoneda as a natural isomorphism

Proof. Ev(idr,idK) is the identity, and for (g,α):(r,K)(r,K) and uK(s), Ev(g,α)(Ev(g,α)(u))=αr(K(g)(αr(K(g)(u))))=nat. αatgαr(αr(K(g)(K(g)(u))))=(141.4)(αα)r(K(gg)(u))=Ev(gg,αα)(u). Nt(idr,idK)(ψ)=ψ because homC(idr,) is the identity transformation, and the composition law is associativity of composition of natural transformations together with homC(gg,)=homC(g,)homC(g,), both sides sending h to hgg. By lemma 141.56, naturality of Φ may be checked in each variable separately, and the two conditions are exactly the two equations of (141.10). Each component is a bijection by theorem 141.52, so Φ is a natural isomorphism by proposition 141.43. ◻

A presheaf K on C is representable when KhomC(,r) in [Cop,Set] for some object r, and its universal element is again ϕr(idr)K(r) for the isomorphism ϕ.

The two facts used in proposition 141.29 are representations. The identity functor Id:SetSet is represented by 1 with universal element 1: by theorem 141.52, Nat(homSet(1,),Id)Id(1)=1, and the component at A of the transformation determined by sends x:1A to Id(x)()=x, the bijection between arrows 1A and elements of A. The presheaf P(B):={SSB}, with P(f) the inverse image along f, is represented by 2 with universal element {1}2: the transformation determined by {1} sends χ:B2 to χ1{1}, the bijection between arrows B2 and subsets of B. In Ctx, the test object observes environments (proposition 141.36), and proposition 141.60 shows that the test object A observes terms.

Definition 141.58 — Yoneda embedding

The functor y:C[Cop,Set] sends an object r to y(r):=homC(,r) and an arrow g:rr to the natural transformation y(g):y(r)y(r) with components hgh.

That y(g) is natural and that y satisfies (141.4) are both associativity: g(hf)=(gh)f gives naturality, and (gg)h=g(gh) gives y(gg)=y(g)y(g).

Corollary 141.59 — The embedding is fully faithful

The functor y:C[Cop,Set] is fully faithful: for objects r,r, the function gy(g):homC(r,r)Nat(y(r),y(r)) is a bijection. Consequently y(r)y(r) in [Cop,Set] implies rr in C: a representable presheaf determines its representing object up to isomorphism. The name embedding records these two properties; by theorem 141.46, y is an equivalence onto the full subcategory of representable presheaves.

Proof of Corollary 141.59 — The embedding is fully faithful

Proof. Apply corollary 141.53 with K:=y(r): Nat(y(r),y(r))y(r)(r)=homC(r,r), and the inverse Ψ sends g to the transformation hy(r)(h)(g)=gh, which is y(g). For the consequence, let ϕ:y(r)y(r) and ψ:y(r)y(r) be mutually inverse. By the bijection just proved, ϕ=y(g) and ψ=y(g) for unique g:rr and g:rr, and y(gg)=functory(g)y(g)=choiceψϕ=hyp.idy(r)=functory(idr), so gg=idr by injectivity of y on arrows; likewise gg=idr. ◻

The presheaf of terms is representable, and the representing object is the smallest context that declares a variable of the right type.

Proposition 141.60 — Terms are represented by one variable

For each type A and its one-declaration context A=(x:A), the functions ϕΓ:homCtx(Γ,A)TmA(Γ),ϕΓ((e)):=e, form a natural isomorphism y(A)TmA, with universal element the variable xTmA(A).

Proof of Proposition 141.60 — Terms are represented by one variable

Proof. A substitution ΓA is a one-element list (e) with Γe:A, so ϕΓ is a bijection with inverse e(e). Naturality at σ:ΓΔ: for (e)homCtx(Δ,A), TmA(σ)(ϕΔ((e)))=e[σ]=def. ϕΓ((e)σ)=ϕΓ(y(A)(σ)((e))). The universal element is ϕA(idA), and idA=(x), so it is x. ◻

Theorem 141.61 — Natural operations on terms are substitutions

For types A,B, the natural transformations TmATmB are in bijection with terms x:Ab:B: the transformation determined by b has components TmA(Γ)TmB(Γ),eb[e/x], and every natural transformation arises from exactly one b, namely the image of the variable x under its component at A.

Proof of Theorem 141.61 — Natural operations on terms are substitutions

Proof. By proposition 141.60, TmAy(A), so composing with that isomorphism identifies Nat(TmA,TmB) with Nat(y(A),TmB). Corollary 141.53 with r=A and K=TmB identifies Nat(y(A),TmB) with TmB(A), the terms x:Ab:B. Tracing the bijections: a term b goes to Ψ(b), whose component at Γ sends (e):ΓA to TmB((e))(b)=b[(e)]=b[e/x]; and a natural α:TmATmB goes to Φ(αϕ)=αA(ϕA(idA))=αA(x). ◻

The theorem is the precise form of the slogan that a natural family may use its argument only through substitution-preserving operations. The family at of exercise 141.8 is the term xt; the family c of example 141.40 corresponds to no term, because c2(x)=tt would give cΓ(e)=tt[e/x]=tt for every e, which c does not satisfy.

A context with one more declaration decomposes, as a presheaf, into the shorter context and the terms of the added type.

Proposition 141.62 — Extension of a context

For a context Γ, a type A, and a variable x not declared in Γ, the functions homCtx(Δ,Γ,x:A)homCtx(Δ,Γ)×TmA(Δ), (b1,,bn,a)((b1,,bn),a), form a natural isomorphism y(Γ,x:A)y(Γ)×TmA.

Proof of Proposition 141.62 — Extension of a context

Proof. A substitution ΔΓ,x:A is a list of n+1 terms whose first n entries form a substitution ΔΓ and whose last entry is a term Δa:A, so the function is a bijection. Naturality at σ:ΔΔ: both composites send (b1,,bn,a) to ((b1[σ],,bn[σ]),a[σ]), by definition 141.4 on the left and componentwise on the right. ◻

Theorem 141.63 — Natural binary operations are two-variable terms

For types A,A,B, natural transformations TmA×TmATmB are in bijection with terms x:A,x:Ab:B. The term b determines the components (e,e)b[e/x,e/x]. When A=AB, application is represented by the term xx.

Proof of Theorem 141.63 — Natural binary operations are two-variable terms

Proof. Apply proposition 141.62 to the context x:A and the added declaration x:A, then apply proposition 141.60 for A. The resulting natural isomorphisms give y(x:A,x:A)y(x:A)×TmATmA×TmA. Yoneda (corollary 141.53) therefore identifies natural transformations from the product to TmB with TmB(x:A,x:A). Tracing the representing substitution sends (e,e) to b[e/x,e/x]. For A=AB, the term xx has type B, and its component is ordinary application. ◻

Representability says that every element of every K(a) is K(f)(u) for exactly one f:ar. Collect all the elements of all the K(a) into one structure, with the arrows that transport one to another, and this uniqueness becomes a terminal object. The case K=TmA shows the structure first. Its elements are the terms in context, the pairs (Γ,e) with Γe:A; a substitution σ:ΓΔ transports (Δ,e) to (Γ,e[σ]), so take as arrows (Γ,e)(Δ,e) the substitutions σ:ΓΔ with e[σ]=e. These compose: if also τ:ΔΘ with e[τ]=e, then e[τσ]=e[τ][σ]=e by lemma 141.5; and idΓ is an arrow (Γ,e)(Γ,e). The pair (A,x) is terminal: an arrow (Γ,e)(A,x) is a substitution σ:ΓA with x[σ]=e, that is, with σ(x)=e, and (e) is the only one. Write TmA for this category. The same recipe applies to any presheaf.

Definition 141.64 — Category of elements

For a presheaf K on C, the category of elements K has as objects the pairs (a,u) with a an object of C and uK(a), and as arrows (a,u)(b,v) the arrows f:ab of C with K(f)(v)=u, composed as in C. The projection πK:KC sends (a,u) to a and an arrow to itself.

The composite of f:(a,u)(b,v) and g:(b,v)(c,w) is an arrow of K because K(gf)(w)=(141.5)K(f)(K(g)(w))=garrowK(f)(v)=farrowu, and ida is one because K(ida)(u)=u; the laws are inherited from C. When C is a preorder P, K is again a preorder: (a,u)(b,v) exactly when ab and the restriction of v along ab is u. The elements of K are stacked over the elements of P, each uK(a) over its index a, and ordered by restriction; the projection forgets the upper layer.

Proposition 141.65 — Representability by a terminal element

A presheaf K on C is representable if and only if K has a terminal object. A terminal object (r,u) gives the representation Ψ(u):y(r)K with universal element u, and every representation arises so.

Proof of Proposition 141.65 — Representability by a terminal element

Proof. Let ϕ:y(r)K be a natural isomorphism with universal element u=ϕr(idr); by corollary 141.53, ϕ=Ψ(u), so ϕa(f)=K(f)(u) for f:ar. An arrow (a,v)(r,u) of K is an f:ar with K(f)(u)=v, that is, with ϕa(f)=v; since ϕa is a bijection there is exactly one, so (r,u) is terminal. Conversely let (r,u) be terminal. For each a and vK(a) there is exactly one f:ar with K(f)(u)=v, so Ψ(u)a:fK(f)(u) is a bijection homC(a,r)K(a); Ψ(u) is natural by corollary 141.53, hence a natural isomorphism by proposition 141.43, with universal element Ψ(u)r(idr)=K(idr)(u)=u. ◻

For K=TmA this is proposition 141.60 seen from inside. For the representable y(r)=homC(,r), the objects of y(r) are the arrows h:ar and an arrow (a,h)(b,h) is an f:ab with hf=h: the arrows into r with the factorizations between them. This category is called the slice of C over r and written C/r.

Exercise 141.10

★★☆ Reconstruct theorem 141.63 by tracing the two representing isomorphisms in the opposite order. Verify naturality of (e,e)b[e/x,e/x] directly from lemma 141.5, and calculate the application instance b:=xx when A=AB.

Exercise 141.11

★★☆ State corollary 141.53 for a preorder P regarded as a category, where a presheaf is a family of sets K(a) with restriction maps K(b)K(a) for ab. Show that it specializes to: a natural transformation from the down-set {aar} (with one-element sets) to K is the same as an element of K(r). Conclude, for the semantic values of chapter 49 regarded as a presheaf V on the extension preorder, that an element of V(Γ) is the same as a family (aΔ)ΔΓ with aΔV(Δ) compatible with restriction.

The chapter began with three composition structures proved one at a time. Two of them are objects of one kind: Ctx is a category, and the semantic values of chapter 49 form a presheaf on its preorder of extensions. The restriction lemma of that chapter, which states that evaluating a term and then restricting the value equals restricting the environment and then evaluating, is the statement that evaluation is a natural transformation between two presheaves on that preorder. That is the statement of agreement which the three repeated proofs could not express: two composition structures agree when a family of maps between them is natural. What theorem 141.61 does for one variable, theorem 141.63 does for two; the contexts that represent families of terms whose types depend on earlier variables belong to the later treatment of dependent categorical semantics.

Bibliographic notes.

Categories, functors, and natural transformations were introduced by Eilenberg and Mac Lane [EML45], who defined categories in order to define naturality. The lemma of section 141.9 is named for Yoneda; Mac Lane [ML98a] records its origin in a conversation with him in 1954, and proves the characterization of equivalences, theorem 141.46, as his Theorem IV.4.1. The treatment of monomorphisms and epimorphisms by test objects, and the example of proposition 141.31, follow Asperti and Longo [AL91], whose book develops category theory from typed lambda calculi. The representability of the presheaf of terms by a one-variable context, proposition 141.60, is the elementary case of the representable natural transformations that Awodey uses to define natural models of dependent type theory [Awo18].

Suggested first pass.

None of these problems is a prerequisite for later chapters. Begin with exercise 141.12, exercise 141.17, continue with exercise 141.15, exercise 141.20, and finish by building Ctx in exercise 141.23.

Exercise 141.12

★☆☆ Let Γ=u:22, Δ=y:22, v:2, and Θ=z:2. Take σ=(λw:2.uw, utt) and τ=(yv). Compute τσ, then compute (λu:2.z)[τσ] and (λu:2.z)[τ][σ] separately, choosing bound names explicitly at each step, and confirm lemma 141.5 on this instance. Say at which step a bound name had to be changed and why.

Exercise 141.13

★★☆ In Rel, show that (SR)=RS and (idA)=idA, and that RR is an isomorphism of categories RelRelop that is the identity on objects. Write each equation as a quantified statement before proving it.

Exercise 141.14

★★☆ Show that the product category P×Q of two preorders is the preorder on pairs with (a,b)(a,b) iff aa and bb, and that the hom bifunctor of proposition 141.55 on a preorder P is the functor Pop×PSet sending (a,b) to a one-element set if ab and to the empty set otherwise. Given a functor Pop×PSet whose values are empty or one-element sets, show that it is naturally isomorphic to the canonical functor determined by the monotone map Pop×P{01} that records whether each value is inhabited. Show that the hom bifunctor corresponds in this sense to the map sending (a,b) to 1 exactly when ab, whose monotonicity is transitivity of .

Exercise 141.15

★★★ Prove directly, without passing through Cop, that for objects r,s of C the function ghomC(g,) is a bijection homC(s,r)Nat(homC(r,),homC(s,)). Give the inverse explicitly and verify both composites, displaying the component types throughout. Conclude that rhomC(r,) is a fully faithful functor Cop[C,Set].

Exercise 141.16

★☆☆ Verify that [C,D] satisfies (141.2): state each law as an equation between natural transformations, reduce it to an equation between components at an arbitrary object a, and discharge it by the corresponding law in D.

Exercise 141.17

★★☆ For a context Γ=x1:A1,,xn:An and a type A, the weakening w:=(x1,,xn):Γ,x:AΓ is a substitution. Show that w is an epimorphism in Ctx, because its action on terms is injective, but not a monomorphism, and that it is not an isomorphism. Explain why the global-element argument of proposition 141.29 does not transfer to Ctx.

Exercise 141.18

★★☆ A category with a terminal object 1 has enough points if for all f,f:ab with ff there is a global element u:1a with fufu. Show that this holds exactly when the functor homC(1,) is faithful. Show that Set has enough points and that Ctx does not: exhibit two distinct substitutions out of P, for an atomic type P, that agree on every environment.

Exercise 141.19

★★☆ Let F:CD be an equivalence with inverse functors G and G, each with its pair of natural isomorphisms. Construct a natural isomorphism GG from the given data, and verify its naturality.

Exercise 141.20

★☆☆ Deduce from proposition 141.65, lemma 141.27 that a representing object is unique up to isomorphism, the consequence stated in corollary 141.59, and check that an isomorphism in K projects to an isomorphism in C.

Exercise 141.21

★☆☆ For a covariant functor K:CSet, define K with objects (a,u), uK(a), and arrows (a,u)(b,v) the f:ab with K(f)(u)=v. Describe Env for the functor of proposition 141.36: its objects, its arrows, and its initial object.

Exercise 141.22

★★☆ Let K be the presheaf on Ctx with K(Γ):={0,1} for every Γ and K(σ):=id{0,1} for every σ. Show that K is not representable. Hint: if Ky(Δ) then homCtx(,Δ) has exactly two elements; count the closed terms of a type, distinguishing an atomic type P from an inhabited type.

Exercise 141.23

★★★ Practical project.ctx-category Implement the category Ctx of example 141.9 for the calculus of chapter 2: terms with named binders, contexts, substitutions as lists of terms, the typing check of definition 141.1, the action e[σ] with capture-avoiding renaming, and the composition and identities of definition 141.4. Maintain the invariant that every constructed substitution is well typed at its declared source and target and that no action captures a free variable. The program must print each composite it computes. The acceptance test consists of the following named inputs and outcomes:

  1. τσ of (141.1) prints λw:2.fx up to the bound name;

  2. the two bracketings of exercise 141.1 print α-equal lists;

  3. the two actions of example 141.6 print an abstraction whose body is the free variable x, and agree;

  4. idΔσ and σidΓ for the σ of (141.1) both print σ;

  5. the two actions of exercise 141.12 agree;

  6. for b:=xtt with x:22b:2, the component of theorem 141.61 at Γ=f:22,x:2 applied to f prints ftt;

  7. evaluating that component family at the variable x in the context 22 prints xtt;

  8. for Γ=f:22,x:2, the renaming (f,x):Γ(x1:22,x2:2) of proposition 141.48 and its inverse (x1,x2) compose to the two identities.

  9. acting with σ=(fx):ΓΔ on if(tt;y;ff) prints if(tt;fx;ff) and preserves its Boolean type;

  10. attempting to compose τ with an identity whose target is not the source of τ is rejected before a substitution is constructed;

  11. if the avoid-set contains x,x,,x(16), where x(k) denotes x followed by k primes, freshening returns a name outside that seventeen-element set.

Search the book

Type to search the local edition.