Lectures onType Theory
Chapter 170
Chapter 170Optional

Dependent Optics and Indexed Bidirectional Structure

Prerequisites. Direct starred prerequisites: Chapter 169. No later core chapter depends on this route.

Let A be a set of tags and B:ASet a family, and let S:=a:AB(a) be the set of tagged values. A client wants an accessor focusing on the tag. The read is the first projection SA. The write is not a function S×AS: replacing the tag a by a leaves a payload of type B(a) where one of type B(a) is required, and no total function repairs that when B(a) and B(a) are different sets.

The obstruction is not a missing equation. It is that the type of the residual part of the structure depends on the value being replaced, so the backward direction of the accessor cannot be typed in the category where the forward direction lives. Chapter 169 took both directions in one category C acted on by one monoidal category; here the two directions must be taken in categories that vary with the index.

The concrete dependent lens

Definition 170.1 — Dependent lens over a family

Let A be a set, B,B:ASet families, and put S:=a:AB(a) and S:=a:AB(a). A dependent lens from (S,S) to (A,A) consists of get:SA,put:s:Sa:AB(a), so that put takes a tagged value and a new tag and returns a payload lying in the fibre over the new tag.

The clumsiness of definition 170.1 is the point: the type of the result of put mentions its own argument. Written as a diagram, the data are two maps over a fixed base, and the base is where the index lives.

Proposition 170.2 — Dependent lenses as maps over a base

Let C:=Set and let C/A be the slice category. Writing X:=(SA) and X:=(SA) for the two projections as objects of C/A, a dependent lens in the sense of definition 170.1 is exactly a pair l:XπAYin C/A,r:πAYXin C/A, where Y=Y=(idA:AA) and πA is pullback along the identity, that is the identity functor.

Proof of Proposition 170.2 — Dependent lenses as maps over a base

Proof. An object of C/A over A is a family; X is the family B and X is B. A map XY in C/A with Y the terminal object of the slice is unique, so l carries no information beyond the existence of get, which is the structure map of X. A map YX in C/A is a section of X, that is a choice of element of B(a) for each a, and that is exactly the second component of put once the first argument has been discarded. ◻

Proposition 170.2 is degenerate because the base was fixed. A useful accessor changes it: the forward direction should be allowed to move from a base A to a base B, and the backward direction should return along the same move. That is a morphism of bicategories, and the next section makes it the definition.

Dependent optics

Convention 170.3 — Indexed categories

Let B be a bicategory. A B-indexed category is a pseudofunctor L:BopCat. We write LA for L(A) and f for L(f) when f is a 1-morphism, and L(m) for the natural transformation assigned to a 2-morphism m. We write θA:IdLA(idA) and θf,g:fg(gf) for the coherence isomorphisms. Two indexed categories are used throughout, L for the forward direction and R for the backward one; the functor assigned by R to f is written f.

Definition 170.4 — Dependent optic

The category OpticL,R has as objects the triples (X,X)A with A an object of B, X an object of LA and X an object of RA. Its hom-sets are OpticL,R((X,X)A,(Y,Y)B):=fB(A,B)LA(X,fY)×RA(fY,X). By proposition 169.11 an element is a pair (l,r) with l:XfY and r:fYX, quotiented by the relation generated by (L(m)Yl, r)  (l, rR(m)Y) for 2-morphisms m:fg with l:XfY and r:gYX. We write lr and call f its representative.

Definition 170.5 — Identity and composition

Id(X,X)A:=(θA)X | (θA1)X, l2r2l1r1:=(θf,g)Zf(l2)l1 | r1f(r2)(θf,g1)Z, for l1r1 with representative f:AB and l2r2 with representative g:BC.

Theorem 170.6 — Optic_ L, R is a category

Definition 170.5 is well defined and satisfies the unit and associativity laws.

Proof of Theorem 170.6 — Optic_ L, R is a category

Proof. Proof idea. The composite is written with an explicit representative gf; well-definedness is extranaturality of the formula in f and g, and each law is the corresponding coherence law of a pseudofunctor, transported across (170.1).

Well-definedness. Replace the representative of the second optic by one related through m:gg1. Both f and f are functors, so the composite changes by f(L(m)) on the left and f(R(m)) on the right, and pseudofunctoriality identifies those with L(fm) and R(fm) for the whiskered 2-morphism fm; then (170.1) at fm identifies the two composites. Replacing the representative of the first optic is the same argument with the whiskering on the other side.

Unit law. Let lr have representative f. Then lrId(X,X)A=definition170.5(θidA,f)Y(idA)(l)(θA)X|=θAnatural(θidA,f)Y(θA)fYl|=unitcoherencelr, the last step because the composite (θidA,f)Y(θA)fY is the identity by the left unit coherence law of the pseudofunctor L, and dually on the right; the omitted backward components are the mirror calculation with θ. The other unit law uses the right unit coherence.

Associativity. Choose representatives f,g,h of three optics simultaneously, which is legitimate because the coend over B(A,B)×B(B,C)×B(C,D) may be computed one factor at a time. Both bracketings produce the pair whose forward component is (θf,hg)Wf((θg,h)Wg(l3)l2)l1, and the associativity coherence of L identifies the two ways of reassociating the θ’s; the backward components are the same calculation for R, and (170.1) at the resulting isomorphism of representatives identifies the two classes. ◻

Two constructions recovered

Proposition 170.7 — Mixed optics are the one-object case

Let CL and CR be categories acted on by a monoidal category M, and let BM be the bicategory with one object and BM(,)=M. The two actions correspond to BM-indexed categories L,R, and mixed optics for the two actions are exactly the morphisms of OpticL,R with B:=BMop.

Proof of Proposition 170.7 — Mixed optics are the one-object case

Proof. An action M[C,C] is the same as a pseudofunctor out of the delooping, by definition 169.13: the unit and associativity data of the action are the coherence isomorphisms θ and θf,g. With one object, B(A,B)=M for the unique pair, so the coend of definition 170.4 is the coend of definition 169.16 with C split into the two categories, which is the definition of a mixed optic recalled in example 169.35. The composition formulas agree because θf,g is the associativity datum of the action. ◻

Proposition 170.8 — Functor lenses are the trivial-forward case

Let B be a 1-category, let R be a B-indexed category, and let be the terminal B-indexed category. Then Optic,R is the category obtained from R by the Grothendieck construction on its pointwise opposite.

Proof of Proposition 170.8 — Functor lenses are the trivial-forward case

Proof. An object of Optic,R is a triple (X,X)A with X the unique object of A, hence a pair (A,X) with XRA: the objects of the Grothendieck construction. A 1-category has only identity 2-morphisms, so (170.1) is the identity relation and the coend of definition 170.4 is a coproduct: fB(A,B)RA(fY,X)  fB(A,B)RA(fY,X), which is the hom-set of the Grothendieck construction on the pointwise opposite: a morphism is a map f of the base together with a map fYX in the fibre. Composition of definition 170.5 reduces to the composition there, since the forward components are identities and the θ’s are the coherence data of the fibration. ◻

Remark 170.9 — Why one framework covers both

Proposition 170.7, Proposition 170.8 pull in opposite directions. A mixed optic has one base and a nontrivial residual, so all the content is in the 2-morphisms of B; a functor lens has many bases and no 2-morphisms, so all the content is in the 1-morphisms. Definition 170.4 carries both because it quotients over 2-morphisms and sums over 1-morphisms, and the accessor of the opening needs both: the representative records which tag was read, and the base records that the tag may change.

Dependent lenses computed

Definition 170.10 — The span bicategory and dependent lenses

Let C be a finitely complete category and SpanC its bicategory of spans: objects those of C, 1-morphisms AB the spans AMB, composition by pullback, and 2-morphisms the maps of spans. Let C/:SpanCopCat send A to the slice C/A and a span to the composite of pullback along its left leg with pushforward along its right leg. Set DLensC:=OpticC/, C/. Its objects are cospans XAX.

Theorem 170.11 — The hom-set of dependent lenses

For objects (X,X)A and (Y,Y)B of DLensC, DLensC((X,X)A,(Y,Y)B)  XYC/A(X×BY, X), the product ranging over the maps XY over A×B.

Proof of Theorem 170.11 — The hom-set of dependent lenses

Proof. Unfold definition 170.4 for this indexed category: MC/(A×B)C/A(X, M×BY)×C/A(M×BY, X)M(XYC/(A×B)(X,M))×C/A(M×BY,X)XYMC/(A×B)(X,M)×C/A(M×BY,X)XYC/A(X×BY, X), The first step is the universal property of the pullback; the second uses that coends commute with products; the third is lemma 169.12. In detail, the first splits a map into M×BY over A into its component into M over A×B and its component into Y over B, the latter being the index of the product. The last step is Yoneda reduction in M, which substitutes X for M. ◻

Example 170.12 — The accessor of the opening

Take C:=Set, A the set of tags, X:=(SA) the family B, X:=(SA) the family B, and let the target be (Y,Y){} with Y=Y=A regarded as a set over the point. A span AM{} is a set M over A, and theorem 170.11 gives DLensSet((X,X)A,(A,A){})  get:SA aA (B(a)×AB(a)) after unfolding the slices: the forward component is the read get, and the backward component assigns, to each tag a, each payload in B(a) and each new tag, an element of B(a). The type of the result now mentions the old index a rather than the new one, which is what makes it a function; the change of index is recorded by the base of the cospan, not by the fibre.

Lemma 170.13 — Coproducts in the span bicategory

If C is lextensive, the inclusion CSpanC preserves coproducts.

Proof of Lemma 170.13 — Coproducts in the span bicategory

Proof. Let A=iAi. For every B, SpanC(A,B)=def.C/(A×B)=lextensiveC/(iAi×B)=lextensiveiC/(Ai×B)=def.iSpanC(Ai,B), so A has the universal property of the coproduct in SpanC. ◻

Proposition 170.14 — Coproducts of dependent optics

Let B have finite coproducts and let L and R send finite coproducts in B to finite products in Cat. Then OpticL,R has finite coproducts. In particular DLensC has finite coproducts when C is lextensive.

Proof of Proposition 170.14 — Coproducts of dependent optics

Proof. Let (Xi,Xi)Ai be a finite family. Put A:=iAi. By hypothesis LAiLAi and likewise for R, so the families (Xi) and (Xi) assemble into single objects X of LA and X of RA. A morphism (X,X)A(Y,Y)B is, by definition 170.4 and the hypothesis, a family of morphisms (Xi,Xi)Ai(Y,Y)B, since B(A,B)iB(Ai,B) and the coend of a product over a product of indices is the product of the coends. That is the universal property of the coproduct.

For the particular case, lemma 170.13 gives coproducts in SpanC, and C/ turns them into products because C/iAiiC/Ai in a lextensive category. ◻

Proposition 170.14 is the concrete payoff of the indexing. The category of ordinary lenses of chapter 169 does not have coproducts: a lens into a coproduct would need a single residual serving both summands, and the two summands have different ones. Allowing the base to vary supplies the missing object.

Tambara representations and the profunctor encoding

Definition 170.15 — Tambara representation

Let D be a category. A D-valued Tambara representation consists of

  • a functor PA:LAop×RAD for each object A of B;

  • a natural transformation ζf:PB(,=)PA(f, f=) for each 1-morphism f:AB, extranatural in f,

subject to PA(θA,θA1)ζidA=IdPA,PA(θf,g,θf,g1)ζgf=(ζf)g(),g(=)ζg. A morphism (P,ζ)(Q,ξ) is a family of natural transformations ηA:PAQA with ηf(),f(=)Aζf=ξfηB. Write TambD for the resulting category.

Definition 170.16 — The universal representation

For each object A define ιA:LA×(RA)opOpticL,R by ιA(X,X):=(X,X)A on objects, and on a morphism (l,r):(X0,X0)(X1,X1) by the optic with representative idA whose two components are l and r transported along θA and θA. The family ιop is a Opticop-valued Tambara representation, with ζf the map sending a morphism to its composite with the optic θθ1 of representative f.

Theorem 170.17 — Classification of contravariant functors

For every category D, precomposition with ιop is an isomorphism of categories ()ιop:[OpticL,Rop, D]  TambD.

Proof of Theorem 170.17 — Classification of contravariant functors

Proof. Proof idea. Extranaturality of ζf in f is exactly the condition needed to descend a family of maps along the coend of definition 170.4, and the two coherence equations of definition 170.15 are exactly preservation of identity and composition for the descended functor.

From a representation to a functor. Given (P,ζ), define P~ on objects by P~((X,X)A):=PA(X,X). On morphisms, consider LA(X,fY)×RA(fY,X)D(PB(Y,Y), PA(X,X)),(l,r)PA(l,r)(ζf)Y,Y. This is extranatural in f by the hypothesis on ζ, so by proposition 169.11 it descends to the coend, giving P~ on hom-sets. The first equation of definition 170.15 makes P~ preserve identities and the second makes it preserve composition, by comparison with definition 170.5.

The two constructions are inverse. On objects, (P~ιop)A(X,X)=P~((X,X)A)=PA(X,X). On morphisms, ιA(l,r) has representative idA, so applying P~ to it gives PA(l,r)ζidA, which is PA(l,r) by the first equation of definition 170.15. Conversely, a functor F out of Opticop is determined by its values on the optics ιA(l,r) and on the optics θθ1 of each representative, because every optic factors as one of the latter followed by one of the former; that factorization is the composition formula of definition 170.5 with l2 and r2 the coherence maps. ◻

Corollary 170.18 — Profunctor encoding of dependent optics

For objects (X,X)A and (Y,Y)B, OpticL,R((X,X)A,(Y,Y)B)  PTambSetSet(PB(Y,Y), PA(X,X)).

Proof of Corollary 170.18 — Profunctor encoding of dependent optics

Proof. By theorem 170.17 at D:=Set, the category TambSet is isomorphic to the category of presheaves on OpticL,R. For any category E and objects S,T, the Yoneda lemma gives E(S,T)=YonedaE(,T)(S)=YonedaFE^Set(F(T), F(S)), the second step because E^(E(,T),F)F(T). Apply this with E:=OpticL,R, S:=(X,X)A and T:=(Y,Y)B, and transport along the isomorphism of theorem 170.17. ◻

Corollary 170.18 is the dependent counterpart of theorem 169.24: an optic is a family of maps natural in the representation, with no residual mentioned. The two statements are not instances of one another; proposition 170.7 makes the earlier one the one-object case of the earlier definition, and theorem 170.17 is proved for a general bicategory.

Boundary

Proved here. The concrete dependent lens and its reading over a base (definition 170.1, proposition 170.2); the category of dependent optics (definition 170.4, definition 170.5, theorem 170.6); the two comparisons (proposition 170.7, proposition 170.8); the hom-set of dependent lenses and the accessor of the opening (definition 170.10, theorem 170.11, example 170.12); coproducts (lemma 170.13, proposition 170.14); and the Tambara classification with its profunctor corollary (definition 170.15, theorem 170.17, corollary 170.18).

Exported interface. Exactly the statements listed above, at the hypotheses displayed with them: B a bicategory, L and R pseudofunctors into Cat, and for theorem 170.11, proposition 170.14 the further hypotheses that C be finitely complete and, for coproducts, lextensive. No later development may strengthen the interface by appeal to an implementation or to a comparison.

Comparisons only. Two neighbouring uses of bidirectional structure appear in the literature and are not theorems of this chapter. The first is borrowing safety: an accessor that yields a temporary view of a field, valid for a bounded region, is not an optic of definition 170.4, because the forward and backward directions there carry no lifetime and the composition of definition 170.5 imposes no ordering on their use. The second is mutable-value independence: a semantics in which a value’s identity is independent of the store is not established by corollary 170.18, which classifies functors out of the optic category and says nothing about a store. Both are comparisons, and neither is used as a premise anywhere above.

The bicategory of dependent optics. Definition 170.4 quotients over 2-morphisms, so the representative of a composite is determined only up to that quotient. A construction retaining the representative would be a bicategory of dependent optics rather than a category, and theorem 170.6 is not a statement about one.

Suggested first pass.

Problems exercise 170.1, exercise 170.2, and exercise 170.4 form the suggested first pass. None of these problems is a prerequisite for a later chapter.

Exercise 170.1

★★☆ Carry out by hand the calculation that exposes the obstruction of the opening.

  1. Take A:=2, B(tt):=1, B(ff):=2, and S:=a:AB(a). List the elements of S.

  2. Show that no function put:S×AS satisfies put-get together with the requirement that the second component of the result lie in B of the new tag, by displaying the pair at which the two demands conflict.

  3. Compute the hom-set of theorem 170.11 for this instance and exhibit its elements explicitly.

  4. State which component of the result records the change of index, and why it is not a component of an optic in the sense of definition 169.16.

Exercise 170.2

★★★ Theorem 170.6 is the smallest invariant used by theorem 170.17: without it there is no category to classify functors out of.

  1. Write the associativity verification in full, displaying every use of the pseudofunctor coherence and every use of (170.1).

  2. Show that the quotient (170.1) cannot be dropped, by exhibiting two representatives of one accessor whose composites with a third have non-isomorphic representatives.

  3. State what fails in proposition 170.8 if B is allowed nontrivial 2-morphisms, and identify the step of its proof that uses their absence.

Exercise 170.3

★★☆ Proposition 170.14 needs both hypotheses.

  1. Exhibit a bicategory with finite coproducts and an indexed category not sending them to products, and show that the conclusion fails.

  2. Show directly that the category of ordinary lenses over Set has no coproduct of the two one-element objects, by displaying the two candidate residuals.

  3. Explain, in one sentence, which datum of definition 170.4 supplies the missing object.

Exercise 170.4

★★★ Practical project.dependent-optic-checker First stage. Implement, in Agda, the concrete representation of dependent lenses given by theorem 170.11 over Set, with finite base sets and finite fibres.

Calculus to implement. A finite category C of finite sets and functions; slices C/A as pairs of a finite set and a function into A; spans AMB; pullback along a leg and pushforward along the other; and dependent lenses in the form of the right-hand side of theorem 170.11, that is a family indexed by maps XY over A×B of maps X×BYX over A.

Invariant. Every constructed lens must be checked to commute over the base: the two triangles of example 170.12 must commute pointwise, and the program must report the offending element when they do not.

Concrete result. For a named cospan and a named lens, an accept or reject verdict together with, on rejection, the element at which commutation fails.

Acceptance test. On the instance of exercise 170.1 with A=2, B(tt)=1 and B(ff)=2, the program must accept the lens whose backward component keeps the payload when the tag is unchanged and returns the designated element of the new fibre otherwise, and must print its four components. It must reject the attempted ordinary lens of exercise 170.1(2), naming the pair at which the fibre types disagree.

Exercise 170.5

★★★ Practical project.dependent-optic-checker Second stage. Continue exercise 170.4. Add composition and the two comparisons.

Calculus to implement. The composition of definition 170.5 specialised to spans, computed by pullback; the identity of definition 170.5; the embedding of ordinary optics of proposition 170.7 for a one-object base; and the embedding of functor lenses of proposition 170.8 for a discrete base.

Invariant. Composition must be computed on representatives and the result must be checked to be independent of the representatives chosen, by comparing the two composites obtained from two representatives related by a map of spans; the program must report the pair of representatives when they disagree. This is the executable form of theorem 170.6, well-definedness clause.

Concrete result. For three composable lenses, the two bracketings of their composite together with a verdict that they agree; and, for an ordinary optic and a functor lens, their images under the two embeddings with a verdict that composition is preserved.

Acceptance test. The two bracketings of a three-fold composite over the base 2 must agree componentwise. An ordinary lens on 2×2 embedded by proposition 170.7 must compose to the image of the ordinary composite. A functor lens over the discrete base {0,1,2} embedded by proposition 170.8 must have the hom-set computed as a coproduct, and the program must print its cardinality, which must equal the sum over base maps of the fibre hom-set sizes.

Exercise 170.6

★★★ Practical project.dependent-optic-checker Third stage. Continue exercise 170.5. Add the profunctor encoding and coproducts.

Calculus to implement. Finite Tambara representations in the sense of definition 170.15, given by a finite family of finite profunctors with their structure maps; the two directions of corollary 170.18 at those finite representations; and the coproduct of proposition 170.14 for a finite lextensive base.

Invariant. The two directions of the encoding must be mutually inverse on every constructed lens, and the program must check that round trip; and the coproduct injections must be checked to satisfy the universal property against every finite competitor it can enumerate.

Concrete result. A report giving, for each named lens, its profunctor form as a table of maps indexed by the enumerated representations, the result of the round trip, and the coproduct verdict.

Acceptance test. The round trip must succeed for the lens of exercise 170.4 and for the composite of exercise 170.5. The coproduct of the two objects (1,1){0} and (1,1){1} must be computed and its universal property verified against all finite competitors over bases of size at most three. The program must exhibit, as a counterexample, a base that is not lextensive together with two objects whose coproduct fails, and must print the failing cocone. Produce three mutations that still typecheck — drop the extranaturality check on ζ, compose without pulling back along the left leg, and identify two representatives related by a non-invertible map of spans — and confirm that each makes a named case fail. State explicitly that the program checks theorem 170.6, corollary 170.18, proposition 170.14 at finitely many finite instances and proves none of them.

Search the book

Type to search the local edition.