Prerequisites. Direct starred prerequisites: none. No later core chapter depends on this route.
A configuration record has a field, and a program wants to read it and to replace it. Write 𝑆 for the record type and 𝐴 for the field type. The pair of operations is 𝗀𝖾𝗍:𝑆→𝐴,𝗉𝗎𝗍:𝑆×𝐴→𝑆, and every question in this chapter comes from asking what makes a particular pair a legitimate accessor and how two accessors compose.
Take 𝑆:=𝐴 ×𝐵 with 𝗀𝖾𝗍:=𝗉𝗋1 and 𝗉𝗎𝗍((𝑎,𝑏),𝑎′):=(𝑎′,𝑏). Now take the same 𝑆 with 𝗀𝖾𝗍:=𝗉𝗋1 and 𝗉𝗎𝗍((𝑎,𝑏),𝑎′):=(𝑎,𝑏), which ignores the new value. Both pairs have the same type. The second is not an accessor, and saying why requires an equation, not a type: 𝗀𝖾𝗍(𝗉𝗎𝗍(𝑠,𝑎)) =𝑎 fails for it. A third pair, with 𝗉𝗎𝗍((𝑎,𝑏),𝑎′):=(𝑎′,𝑏0) for a fixed 𝑏0, satisfies that equation and fails 𝗉𝗎𝗍(𝑠,𝗀𝖾𝗍 𝑠) =𝑠. A fourth, storing a counter of how many writes have occurred, satisfies both and fails 𝗉𝗎𝗍(𝗉𝗎𝗍(𝑠,𝑎),𝑎′) =𝗉𝗎𝗍(𝑠,𝑎′).
A lens from 𝑆 to 𝐴 is a pair 𝗀𝖾𝗍 :𝑆 →𝐴 and 𝗉𝗎𝗍 :𝑆 ×𝐴 →𝑆. It is lawful when 𝗀𝖾𝗍(𝗉𝗎𝗍(𝑠,𝑎))=𝑎,𝗉𝗎𝗍(𝑠,𝗀𝖾𝗍𝑠)=𝑠,𝗉𝗎𝗍(𝗉𝗎𝗍(𝑠,𝑎),𝑎′)=𝗉𝗎𝗍(𝑠,𝑎′), called put-get, get-put and put-put.
Referenced from 4 locations
Let (𝗀𝖾𝗍1,𝗉𝗎𝗍1) be a lens from 𝑆 to 𝐴 and (𝗀𝖾𝗍2,𝗉𝗎𝗍2) a lens from 𝐴 to 𝐵. Then 𝗀𝖾𝗍:=𝗀𝖾𝗍2∘𝗀𝖾𝗍1,𝗉𝗎𝗍(𝑠,𝑏):=𝗉𝗎𝗍1(𝑠,𝗉𝗎𝗍2(𝗀𝖾𝗍1𝑠,𝑏)) is a lens from 𝑆 to 𝐵, and it is lawful when both are.
Referenced from 3 locations
Proof of Proposition 169.2 — Composition of lenses
Proof. Put-get. 𝗀𝖾𝗍(𝗉𝗎𝗍(𝑠,𝑏))𝑑𝑒𝑓.=𝗀𝖾𝗍2(𝗀𝖾𝗍1𝗉𝗎𝗍1(𝑠,𝗉𝗎𝗍2(𝗀𝖾𝗍1𝑠,𝑏)))𝑝𝑢𝑡−𝑔𝑒𝑡1=𝗀𝖾𝗍2(𝗉𝗎𝗍2(𝗀𝖾𝗍1𝑠,𝑏))𝑝𝑢𝑡−𝑔𝑒𝑡2=𝑏.
Get-put. 𝗉𝗎𝗍(𝑠,𝗀𝖾𝗍𝑠)𝑑𝑒𝑓.=𝗉𝗎𝗍1(𝑠,𝗉𝗎𝗍2(𝗀𝖾𝗍1𝑠,𝗀𝖾𝗍2𝗀𝖾𝗍1𝑠))𝑔𝑒𝑡−𝑝𝑢𝑡2=𝗉𝗎𝗍1(𝑠,𝗀𝖾𝗍1𝑠)𝑔𝑒𝑡−𝑝𝑢𝑡1=𝑠.
Put-put. Abbreviate 𝑎:=𝗀𝖾𝗍1𝑠. Then 𝗉𝗎𝗍(𝗉𝗎𝗍(𝑠,𝑏),𝑏′)𝑑𝑒𝑓.=𝗉𝗎𝗍1⟨𝗉𝗎𝗍1(𝑠,𝗉𝗎𝗍2(𝑎,𝑏)), 𝗉𝗎𝗍2(𝗀𝖾𝗍1𝗉𝗎𝗍1(𝑠,𝗉𝗎𝗍2(𝑎,𝑏)),𝑏′)⟩𝑝𝑢𝑡−𝑔𝑒𝑡1=𝗉𝗎𝗍1⟨𝗉𝗎𝗍1(𝑠,𝗉𝗎𝗍2(𝑎,𝑏)), 𝗉𝗎𝗍2(𝗉𝗎𝗍2(𝑎,𝑏),𝑏′)⟩𝑝𝑢𝑡−𝑝𝑢𝑡2=𝗉𝗎𝗍1⟨𝗉𝗎𝗍1(𝑠,𝗉𝗎𝗍2(𝑎,𝑏)), 𝗉𝗎𝗍2(𝑎,𝑏′)⟩𝑝𝑢𝑡−𝑝𝑢𝑡1=𝗉𝗎𝗍1(𝑠,𝗉𝗎𝗍2(𝑎,𝑏′))𝑑𝑒𝑓.=𝗉𝗎𝗍(𝑠,𝑏′). ◻
Three failures produced three laws, and the laws compose. The next paragraph produces the accessor that a lens cannot be.
Let 𝑆:=𝐴 +𝐵 and let a client wish to modify the left summand when it is present and leave the value alone otherwise. A 𝗀𝖾𝗍 :𝑆 →𝐴 does not exist: there is nothing to return at an element of 𝐵. The two operations that do exist are 𝗆𝖺𝗍𝖼𝗁:𝑆→𝐴+𝑆,𝖻𝗎𝗂𝗅𝖽:𝐴→𝑆, with 𝗆𝖺𝗍𝖼𝗁(𝗂𝗇𝗅 𝑎) =𝗂𝗇𝗅 𝑎, 𝗆𝖺𝗍𝖼𝗁(𝗂𝗇𝗋 𝑏) =𝗂𝗇𝗋(𝗂𝗇𝗋 𝑏) and 𝖻𝗎𝗂𝗅𝖽 =𝗂𝗇𝗅. Such a pair is a prism, and it is lawful when 𝗆𝖺𝗍𝖼𝗁(𝖻𝗎𝗂𝗅𝖽𝑎)=𝗂𝗇𝗅𝑎and𝗆𝖺𝗍𝖼𝗁𝑠=𝗂𝗇𝗅𝑎 implies 𝖻𝗎𝗂𝗅𝖽𝑎=𝑠.
Referenced from 3 locations
Let 𝑆 be a list of 𝐴’s and let a client wish to replace every element. Neither a lens nor a prism applies: there is not one focus but many. The operation that exists is 𝖾𝗑𝗍𝗋𝖺𝖼𝗍:𝑆→∐𝑛∈ℕ𝐴𝑛×(𝐴𝑛→𝑆), sending a list to its length, its elements, and the function rebuilding a list of that length. A traversal is such an operation. Its laws are stated in section 169.7 once the machinery that makes them short is available.
Referenced from 4 locations
Three accessors with three shapes, three law sets, and no common composition. The rest of the chapter produces one definition covering all of them, one composition, and one notion of lawfulness, and proves that each specialises correctly.
Profunctors
Let C be a category. A profunctor on C is a functor 𝑃 :Cop ×C →𝐒𝐞𝐭. Concretely, 𝑃 assigns a set 𝑃(𝐴,𝐵) to each pair of objects and a function 𝖽𝗂𝗆𝖺𝗉:homC(𝐴′,𝐴)×homC(𝐵,𝐵′)×𝑃(𝐴,𝐵)⟶𝑃(𝐴′,𝐵′) satisfying 𝖽𝗂𝗆𝖺𝗉(id𝐴,id𝐵)=id𝑃(𝐴,𝐵),𝖽𝗂𝗆𝖺𝗉(𝑓′,𝑔′)∘𝖽𝗂𝗆𝖺𝗉(𝑓,𝑔)=𝖽𝗂𝗆𝖺𝗉(𝑓∘𝑓′, 𝑔′∘𝑔).
Referenced from 5 locations
The variance is forced by the intended reading: an element of 𝑃(𝐴,𝐵) is a transformation consuming an 𝐴 and producing a 𝐵, so a map into the consumed position and a map out of the produced position both act on it, in opposite directions.
Fun(𝐴,𝐵):=hom𝐒𝐞𝐭(𝐴,𝐵), with 𝖽𝗂𝗆𝖺𝗉(𝑓,𝑔)(ℎ):=𝑔 ∘ℎ ∘𝑓.
Const𝑅(𝐴,𝐵):=𝑅 for a fixed set 𝑅, with 𝖽𝗂𝗆𝖺𝗉(𝑓,𝑔):=id𝑅. This forgets both arguments.
Tagged(𝐴,𝐵):=𝐵, with 𝖽𝗂𝗆𝖺𝗉(𝑓,𝑔):=𝑔. This uses only the produced position.
Kl𝑇(𝐴,𝐵):=hom𝐒𝐞𝐭(𝐴,𝑇 𝐵) for a monad 𝑇, with 𝖽𝗂𝗆𝖺𝗉(𝑓,𝑔)(ℎ):=𝑇(𝑔) ∘ℎ ∘𝑓. Taking 𝑇:=( −) ×𝑊 for a monoid 𝑊 gives an effect-sensitive example: the transformation may also emit an element of 𝑊.
Each satisfies the two laws of definition 169.5: for item 1, both are associativity and unitality of composition; for item 2 both sides are the identity; for item 3 they are functoriality of the identity functor; for item 4 they are functoriality of 𝑇 together with item 1.
Referenced from 4 locations
Let (M, ⊗,𝐼) be a monoidal category acting on C by ⋅ :M ×C →C (definition 169.13). A Tambara structure for the action on a profunctor 𝑃 is a family 𝜁𝐴,𝐵,𝑀:𝑃(𝐴,𝐵)⟶𝑃(𝑀⋅𝐴, 𝑀⋅𝐵) natural in 𝐴 and 𝐵, dinatural in 𝑀, and satisfying 𝜁𝐴,𝐵,𝐼=𝑃(𝜆−1𝐴,𝜆𝐵),𝜁𝑀⋅𝐴,𝑀⋅𝐵,𝑁∘𝜁𝐴,𝐵,𝑀=𝑃(𝛼−1𝑁,𝑀,𝐴,𝛼𝑁,𝑀,𝐵)∘𝜁𝐴,𝐵,𝑁⊗𝑀, where 𝜆 and 𝛼 are the unit and associativity data of the action.
Referenced from 9 locations
Take C:=𝐒𝐞𝐭.
For the action 𝑀 ⋅𝐴:=𝑀 ×𝐴 of (𝐒𝐞𝐭, ×,{ ∗}), a Tambara structure on Fun is the operation ℎ ↦id𝑀 ×ℎ, which is exactly what the 𝗉𝗎𝗍 of a lens applies to a modification of the focus.
For the action 𝑀 ⋅𝐴:=𝑀 +𝐴 of (𝐒𝐞𝐭, +,∅), a Tambara structure on Fun is ℎ ↦id𝑀 +ℎ, which is what the 𝗆𝖺𝗍𝖼𝗁 of a prism applies on the branch where the focus is present.
Const𝑅 carries a Tambara structure for every action, namely the identity; Tagged carries one for the product action only after a choice of element of 𝑀, and none uniformly.
Referenced from 4 locations
Proof of Proposition 169.8 — Where the structure comes from
Proof. For 1 and 2, the two equations of definition 169.7 are the unit and associativity coherences of the product and of the coproduct. For 3, the identity map makes both sides of each equation the identity; and a natural family 𝐵 ⟶𝑀 ×𝐵 would give, at 𝐵 ={ ∗}, an element of 𝑀 natural in 𝑀, hence a natural transformation from the terminal object to the identity functor on 𝐒𝐞𝐭, which does not exist since ∅ has no element. ◻
Proposition 169.8 is the answer to a question that library documentation usually leaves open: the classes named “strong” and “choice” are not stipulations but the two instances of definition 169.7 for the two monoidal actions that the two accessor shapes use.
Dinaturality, ends, and coends
Let 𝑃,𝑄 :Cop ×C →𝐒𝐞𝐭. A transformation between them should be a family 𝜃𝐴,𝐵 :𝑃(𝐴,𝐵) ⟶𝑄(𝐴,𝐵), and naturality is the usual square. Now consider instead a family 𝜃𝐴 :𝑃(𝐴,𝐴) ⟶𝑄(𝐴,𝐴), in which the same object occupies both positions. Attempting to state naturality for 𝑓 :𝐴 →𝐵 requires a square 𝑃(𝐴,𝐴) 𝜃𝐴 ←←←←←←←→𝑄(𝐴,𝐴)↓↓𝑃(𝐵,𝐵) 𝜃𝐵 ←←←←←←←→𝑄(𝐵,𝐵) and neither vertical map exists: 𝑃 is contravariant in the first argument and covariant in the second, so 𝑓 induces 𝑃(𝐵,𝐴) ⟶𝑃(𝐴,𝐴) and 𝑃(𝐴,𝐴) ⟶𝑃(𝐴,𝐵), not a map 𝑃(𝐴,𝐴) ⟶𝑃(𝐵,𝐵). The repair is to put the mixed object in the middle.
A dinatural transformation 𝜃 :𝑃 →𝑄 is a family 𝜃𝐴 :𝑃(𝐴,𝐴) ⟶𝑄(𝐴,𝐴) such that for every 𝑓 :𝐴 →𝐵 the hexagon 𝑄(𝑓,id𝐴)∘𝜃𝐴∘𝑃(id𝐴,𝑓)=𝑄(id𝐵,𝑓)∘𝜃𝐵∘𝑃(𝑓,id𝐵) : 𝑃(𝐵,𝐴)⟶𝑄(𝐴,𝐵) commutes.
Referenced from 4 locations
An end of 𝑃 is an object ∫𝐴𝑃(𝐴,𝐴) with a dinatural family 𝜋𝐴 :∫𝐴𝑃(𝐴,𝐴) ⟶𝑃(𝐴,𝐴) universal among such: every dinatural family out of a constant factors uniquely through it. A coend is an object ∫𝐴𝑃(𝐴,𝐴) with a dinatural family 𝜄𝐴 :𝑃(𝐴,𝐴) ⟶∫𝐴𝑃(𝐴,𝐴) universal among dinatural families into a constant.
Referenced from 2 locations
Let C be small and 𝑃 :Cop ×C →𝐒𝐞𝐭.
∫𝐴𝑃(𝐴,𝐴) is the set of families (𝑥𝐴)𝐴 with 𝑥𝐴 ∈𝑃(𝐴,𝐴) such that 𝑃(id𝐴,𝑓)(𝑥𝐴) =𝑃(𝑓,id𝐵)(𝑥𝐵) for every 𝑓 :𝐴 →𝐵.
∫𝐴𝑃(𝐴,𝐴) is the quotient of ∐𝐴𝑃(𝐴,𝐴) by the equivalence relation generated by (𝐴, 𝑃(𝑓,id𝐴)(𝑦)) ∼ (𝐵, 𝑃(id𝐵,𝑓)(𝑦)) for 𝑓 :𝐴 →𝐵 and 𝑦 ∈𝑃(𝐵,𝐴).
Referenced from 10 locations
Proof of Proposition 169.11 — Ends and coends in
Proof. End. The displayed set carries the projections 𝜋𝐴, and the condition is exactly the hexagon of definition 169.9 for a constant source, whose two composites reduce to the two sides of the displayed equation. A dinatural family out of a set 𝑋 assigns 𝑥 ↦(𝜃𝐴𝑥), which lands in the displayed set by dinaturality and is the unique factoring map.
Coend. The quotient carries the injections 𝜄𝐴, and the generating relation is exactly the identification the hexagon forces on the disjoint union. A dinatural family into a set 𝑌 identifies the two sides of every generating relation, hence factors through the quotient; the factoring is unique because the injections are jointly surjective. ◻
For 𝐾 :C →𝐒𝐞𝐭 and 𝑋 ∈C, ∫𝐶homC(𝑋,𝐶)×𝐾𝐶 ≅ 𝐾𝑋,∫𝐶(homC(𝐶,𝑋)⟶𝐾𝐶) ≅ 𝐾𝑋 when 𝐾 is contravariant in the second display.
Referenced from 9 locations
Proof of Lemma 169.12 — Yoneda reduction
Proof. For the first, the map from left to right sends the class of (𝑓,𝑘) to 𝐾(𝑓)(𝑘); it is well defined because the generating relation of proposition 169.11 identifies (𝑔 ∘𝑓,𝑘) with (𝑓,𝐾(𝑔)(𝑘)), and both have image 𝐾(𝑔 ∘𝑓)(𝑘). Its inverse sends 𝑥 ∈𝐾 𝑋 to the class of (id𝑋,𝑥). One round trip is 𝐾(id𝑋) =id; the other sends the class of (𝑓,𝑘) to the class of (id𝑋,𝐾(𝑓)(𝑘)), and those are identified by the generating relation at 𝑓. The second display is the same argument with the quotient replaced by the compatible-family description of proposition 169.11(1). ◻
★★☆ Let C be the two-object category with objects 0,1, one non-identity map 𝑓 :0 →1, and let 𝑃(𝐴,𝐵):=homC(𝐴,𝐵).
List the four sets 𝑃(𝐴,𝐵) and the disjoint union 𝑃(0,0) ⊔𝑃(1,1).
Compute the generating relation of proposition 169.11(2) explicitly and the resulting quotient.
Compute ∫𝐴𝑃(𝐴,𝐴) and say why the two answers differ in cardinality.
Referenced from 2 locations
The monoidal action
Chapter 159 supplies a symmetric monoidal category (M, ⊗,𝐼) with its associator, unitors and symmetry. An optic needs more: a way for M to act on the category C in which the data live. The two are different, and the difference is what allows a lens and a prism to be instances of one definition.
A monoidal action of (M, ⊗,𝐼) on a category C consists of a functor ⋅ :M ×C →C together with natural isomorphisms 𝜆𝐴:𝐼⋅𝐴 ≅ 𝐴,𝛼𝑁,𝑀,𝐴:(𝑁⊗𝑀)⋅𝐴 ≅ 𝑁⋅(𝑀⋅𝐴), subject to the two coherence conditions: the pentagon 𝛼𝑁,𝑀,𝑃⋅𝐴∘𝛼𝑁⊗𝑀,𝑃,𝐴=(id𝑁⋅𝛼𝑀,𝑃,𝐴)∘𝛼𝑁,𝑀⊗𝑃,𝐴∘(𝑎𝑁,𝑀,𝑃⋅id𝐴), where 𝑎 is the associator of M, and the triangle (id𝑀⋅𝜆𝐴)∘𝛼𝑀,𝐼,𝐴=𝑟𝑀⋅id𝐴, where 𝑟 is the right unitor of M.
Referenced from 9 locations
(𝐒𝐞𝐭, ×,{ ∗}) acts on 𝐒𝐞𝐭 by 𝑀 ⋅𝐴:=𝑀 ×𝐴, with 𝜆 and 𝛼 the evident bijections.
(𝐒𝐞𝐭, +,∅) acts on 𝐒𝐞𝐭 by 𝑀 ⋅𝐴:=𝑀 +𝐴, with 𝜆 and 𝛼 the evident bijections.
Both satisfy definition 169.13.
Referenced from 4 locations
Proof of Proposition 169.14 — Two actions on
Proof. In each case 𝜆 and 𝛼 are bijections of finite constructions, and each coherence condition is an equation between two bijections built from the same components; following an arbitrary element through both sides gives the same result. For item 1 at the pentagon, an element (((𝑛,𝑚),𝑝),𝑎) is sent by both composites to (𝑛,(𝑚,(𝑝,𝑎))); for item 2, an element of a four-fold coproduct is sent by both composites to its copy in the corresponding summand. ◻
Optics as a coend
Fix an action of (M, ⊗,𝐼) on C. For pairs of objects (𝑆,𝑆′) and (𝐴,𝐴′) of C, set Optic((𝑆,𝑆′),(𝐴,𝐴′)):=∫𝑀∈MhomC(𝑆,𝑀⋅𝐴)×homC(𝑀⋅𝐴′,𝑆′). By proposition 169.11 this is the set of pairs (𝑙,𝑟) with 𝑙 :𝑆 →𝑀 ⋅𝐴 and 𝑟 :𝑀 ⋅𝐴′ →𝑆′, quotiented by the relation generated by ((𝑓⋅id𝐴)∘𝑙, 𝑟) ∼ (𝑙, 𝑟∘(𝑓⋅id𝐴′)) for 𝑙 :𝑆 →𝑀 ⋅𝐴, 𝑟 :𝑁 ⋅𝐴′ →𝑆′ and 𝑓 :𝑀 →𝑁 in M. Write ⟨𝑙 ∣𝑟⟩ for the class of (𝑙,𝑟) and call 𝑀 its residual.
Referenced from 8 locations
The residual is scratch space: information taken out of 𝑆 that must be kept in order to rebuild 𝑆′. Relation (169.1) says that no client may inspect it, since moving a map 𝑓 from one side to the other does not change the optic.
The assignment ⟨𝑙2∣𝑟2⟩∘⟨𝑙1∣𝑟1⟩:=⟨(𝑀1⋅𝑙2)∘𝑙1 ∣ 𝑟1∘(𝑀1⋅𝑟2)⟩ is well defined and makes pairs of objects of C the objects of a category Optic, with id(𝑆,𝑆′):=⟨𝜆−1𝑆 ∣𝜆𝑆′⟩.
Referenced from 7 locations
Proof of Proposition 169.17 — Composition
Proof. Well-definedness. Replace the representative of the second optic by one related through 𝑓 :𝑀2 →𝑁2. The composite changes by 𝑀1 ⋅𝑓, and (169.1) applied at 𝑀1 ⊗𝑀2 →𝑀1 ⊗𝑁2 identifies the two composites, using 𝛼 to rewrite 𝑀1 ⋅(𝑀2 ⋅𝐴) as (𝑀1 ⊗𝑀2) ⋅𝐴. Replacing the representative of the first optic is the same argument with 𝑓 ⋅id on the outside.
Identity. For ⟨𝑙 ∣𝑟⟩ with residual 𝑀, ⟨𝑙∣𝑟⟩∘⟨𝜆−1𝑆∣𝜆𝑆′⟩𝑑𝑒𝑓.=⟨(𝐼⋅𝑙)∘𝜆−1𝑆∣𝜆𝑆′∘(𝐼⋅𝑟)⟩𝜆𝑛𝑎𝑡𝑢𝑟𝑎𝑙=⟨𝜆−1𝑀⋅𝐴∘𝑙∣𝑟∘𝜆𝑀⋅𝐴′⟩(169.1)=⟨𝑙∣𝑟⟩, the last step moving the isomorphism 𝜆 :𝐼 ⊗𝑀 →𝑀 across, which is legal because it is a map of M. The other unit law is the same calculation on the other side.
Associativity. Choose representatives of three optics with residuals 𝑀1,𝑀2,𝑀3 simultaneously, which is legitimate because a coend in 𝐒𝐞𝐭 over a product of indices may be computed one index at a time. Both bracketings give the pair ((𝑀1⋅(𝑀2⋅𝑙3))∘(𝑀1⋅𝑙2)∘𝑙1, 𝑟1∘(𝑀1⋅𝑟2)∘(𝑀1⋅(𝑀2⋅𝑟3))) after using 𝛼 to associate the residual, and 𝛼 is an isomorphism of M, so (169.1) identifies the two. ◻
For C:=𝐒𝐞𝐭 with the action 𝑀 ⋅𝐴 =𝑀 ×𝐴, Optic((𝑆,𝑆),(𝐴,𝐴)) ≅ hom𝐒𝐞𝐭(𝑆,𝐴)×hom𝐒𝐞𝐭(𝑆×𝐴,𝑆), and the isomorphism sends ⟨𝑙 ∣𝑟⟩ to (𝗉𝗋2 ∘𝑙, 𝑟 ∘(𝗉𝗋1𝑙 ×id𝐴)) and (𝗀𝖾𝗍,𝗉𝗎𝗍) to ⟨⟨id𝑆,𝗀𝖾𝗍⟩ ∣𝗉𝗎𝗍⟩.
Referenced from 10 locations
Proof of Proposition 169.18 — Lenses are optics for the product action
Proof. Compute the coend: ∫𝑀hom(𝑆,𝑀×𝐴)×hom(𝑀×𝐴,𝑆)≅∫𝑀hom(𝑆,𝑀)×hom(𝑆,𝐴)×hom(𝑀×𝐴,𝑆)≅hom(𝑆,𝐴)×hom(𝑆×𝐴,𝑆), The second step applies lemma 169.12 with 𝑋:=𝑆 and 𝐾 𝑀:=hom(𝑆,𝐴) ×hom(𝑀 ×𝐴,𝑆), which is covariant in 𝑀 only through the second factor, contravariantly; the reduction therefore substitutes 𝑆 for 𝑀. Tracing the two composites through the chain gives the displayed formulas. ◻
For C:=𝐒𝐞𝐭 with the action 𝑀 ⋅𝐴 =𝑀 +𝐴, Optic((𝑆,𝑆),(𝐴,𝐴)) ≅ hom𝐒𝐞𝐭(𝑆,𝐴+𝑆)×hom𝐒𝐞𝐭(𝐴,𝑆).
Referenced from 6 locations
Proof of Proposition 169.19 — Prisms are optics for the coproduct action
Proof. The same computation with + in place of ×: hom(𝑀 +𝐴,𝑆) ≅hom(𝑀,𝑆) ×hom(𝐴,𝑆) by the universal property of the coproduct, and then lemma 169.12 on the contravariant factor hom(𝑀,𝑆) substitutes for 𝑀, leaving hom(𝑆,𝐴 +𝑆) ×hom(𝐴,𝑆). ◻
★★☆ Take M to be the terminal monoidal category, with one object 𝐼 and one map, acting on C by 𝐼 ⋅𝐴 =𝐴.
Show that Optic((𝑆,𝑆′),(𝐴,𝐴′)) is then homC(𝑆,𝐴) ×homC(𝐴′,𝑆′), and identify the accessor family this describes.
Show that the quotient (169.1) is the identity relation in this case, and say which hypothesis of remark 169.20 therefore fails to bite.
Give an action for which Optic((𝑆,𝑆′),(𝐴,𝐴′)) is the set of functions 𝑆 →𝑆′ ignoring 𝐴 and 𝐴′ entirely, and name the accessor family.
Referenced from 2 locations
The profunctor representation
Fix an action of M on C. Objects of Tamb are pairs (𝑃,𝜁) of a profunctor and a Tambara structure (definition 169.7); a morphism (𝑃,𝜁) →(𝑄,𝜉) is a natural transformation 𝜃 :𝑃 ⇒𝑄 with 𝜉𝐴,𝐵,𝑀 ∘𝜃𝐴,𝐵 =𝜃𝑀⋅𝐴,𝑀⋅𝐵 ∘𝜁𝐴,𝐵,𝑀. Write 𝑈 :Tamb →Prof for the forgetful functor.
Referenced from 2 locations
For a profunctor 𝑃 define (Φ𝑃)(𝑋,𝑌):=∫𝑀∈M∫𝐶,𝐷∈ChomC(𝑋,𝑀⋅𝐶)×𝑃(𝐶,𝐷)×homC(𝑀⋅𝐷,𝑌), with the Tambara structure that reindexes the residual by 𝑁 ⊗( −). For the representable profunctor 𝐸𝐴,𝐴′(𝐶,𝐷):=homC(𝐶,𝐴) ×homC(𝐴′,𝐷) this gives, by lemma 169.12 twice, (Φ𝐸𝐴,𝐴′)(𝑋,𝑌)≅∫𝑀homC(𝑋,𝑀⋅𝐴)×homC(𝑀⋅𝐴′,𝑌)=Optic((𝑋,𝑌),(𝐴,𝐴′)).
Referenced from 5 locations
Tamb(Φ𝑃, 𝑇) ≅Prof(𝑃, 𝑈 𝑇), naturally.
Referenced from 6 locations
Proof of Lemma 169.23 — Φ is left adjoint to U
Proof. A morphism Φ𝑃 →𝑇 in Tamb is, by proposition 169.11, a family of functions out of the coend, that is a dinatural family hom(𝑋,𝑀⋅𝐶)×𝑃(𝐶,𝐷)×hom(𝑀⋅𝐷,𝑌)⟶𝑇(𝑋,𝑌), compatible with the Tambara structures. Given 𝜃 :𝑃 ⇒𝑈 𝑇, define it by sending (𝑙,𝑝,𝑟) to 𝑇(𝑙,𝑟)(𝜉𝐶,𝐷,𝑀(𝜃𝐶,𝐷 𝑝)), where 𝜉 is the structure of 𝑇; compatibility with the structures is the second equation of definition 169.7 for 𝜉. Conversely, restricting a morphism Φ𝑃 →𝑇 along 𝑀:=𝐼 and 𝑙,𝑟 the unitors gives a natural transformation 𝑃 ⇒𝑈 𝑇, and the two constructions are mutually inverse by the unit equation of definition 169.7 and the coend relation. ◻
For all objects 𝑆,𝑆′,𝐴,𝐴′ of C, Optic((𝑆,𝑆′),(𝐴,𝐴′)) ≅ ∫(𝑃,𝜁)∈Tamb(𝑃(𝐴,𝐴′)⟶𝑃(𝑆,𝑆′)), the end being the set of families 𝑡𝑃 :𝑃(𝐴,𝐴′) ⟶𝑃(𝑆,𝑆′) natural in 𝑃 over Tamb.
Referenced from 10 locations
Proof of Theorem 169.24 — Profunctor representation
Proof. Proof idea. Both sides are computed by Yoneda: the right-hand side is a set of natural transformations out of an evaluation functor, and lemma 169.23 identifies that evaluation functor with a representable one, whose representing object is the optic set of definition 169.22.
Write ev𝐴,𝐴′:=(𝑈 −)(𝐴,𝐴′) :Tamb →𝐒𝐞𝐭. By lemma 169.23 at 𝑃:=𝐸𝐴,𝐴′ and the Yoneda lemma in Prof, Tamb(Φ𝐸𝐴,𝐴′, 𝑇)≅Prof(𝐸𝐴,𝐴′, 𝑈𝑇)≅(𝑈𝑇)(𝐴,𝐴′)=ev𝐴,𝐴′(𝑇), so ev𝐴,𝐴′ is represented by Φ𝐸𝐴,𝐴′. Hence ∫𝑃(ev𝐴,𝐴′(𝑃)⟶ev𝑆,𝑆′(𝑃))𝑟𝑒𝑝𝑟.=∫𝑃(Tamb(Φ𝐸𝐴,𝐴′,𝑃)⟶ev𝑆,𝑆′(𝑃))𝑌𝑜𝑛𝑒𝑑𝑎=ev𝑆,𝑆′(Φ𝐸𝐴,𝐴′), and the last set is Optic((𝑆,𝑆′),(𝐴,𝐴′)) by definition 169.22. ◻
Let 𝑝 =⟨𝑙 ∣𝑟⟩ with 𝑙 :𝑆 →𝑀 ⋅𝐴 and 𝑟 :𝑀 ⋅𝐴′ →𝑆′. The corresponding family is ˜𝑝𝑃:=𝑃(𝑙,𝑟)∘𝜁𝐴,𝐴′,𝑀. Conversely, a family 𝑡 is determined by its component at Φ𝐸𝐴,𝐴′, and that component is determined by its value at ⟨𝜆−1𝐴 ∣𝜆𝐴′⟩, which recovers ⟨𝑙 ∣𝑟⟩.
Referenced from 7 locations
Proof of Corollary 169.25 — Both directions, explicitly
Proof. The displayed formula is the image of 𝑝 under the two isomorphisms of theorem 169.24, read off from the Yoneda step. The converse is the content of the same two isomorphisms taken in the other direction: the Yoneda isomorphism evaluates a natural family at the identity, which here is ⟨𝜆−1𝐴 ∣𝜆𝐴′⟩. ◻
Lawfulness
For an optic 𝑝 =⟨𝑙 ∣𝑟⟩ :(𝑆,𝑆) →(𝐴,𝐴) with residual 𝑀 define outside(𝑝):=𝑟∘𝑙:𝑆→𝑆,once(𝑝):=⟨𝑙∣id𝑀⋅𝐴∣𝑟⟩,twice(𝑝):=⟨𝑙∣𝑟∘𝑙∣𝑟⟩, the last two being elements of the two-hole optic set ∫𝑀1,𝑀2hom(𝑆,𝑀1⋅𝐴)×hom(𝑀1⋅𝐴,𝑀2⋅𝐴)×hom(𝑀2⋅𝐴,𝑆). The optic 𝑝 is lawful when outside(𝑝) =id𝑆 and once(𝑝) =twice(𝑝).
Referenced from 6 locations
For the product action on 𝐒𝐞𝐭, an optic (𝑆,𝑆) →(𝐴,𝐴) corresponding under proposition 169.18 to (𝗀𝖾𝗍,𝗉𝗎𝗍) is lawful in the sense of definition 169.27 if and only if the three laws of definition 169.1 hold.
Referenced from 6 locations
Proof of Theorem 169.28 — Lawfulness specialises to the lens laws
Proof. Proof idea. Compute the two-hole optic set by the same two moves as in proposition 169.18 and read off what the two conditions say.
Take 𝑙 =⟨id𝑆,𝗀𝖾𝗍⟩ and 𝑟 =𝗉𝗎𝗍, the representative supplied by proposition 169.18, with residual 𝑆.
Outside. outside(𝑝) =𝗉𝗎𝗍 ∘⟨id𝑆,𝗀𝖾𝗍⟩, whose value at 𝑠 is 𝗉𝗎𝗍(𝑠,𝗀𝖾𝗍 𝑠). Equality with id𝑆 is get-put.
The two-hole set. Applying the universal property of the product and lemma 169.12 twice, as in proposition 169.18, gives ∫𝑀1,𝑀2hom(𝑆,𝑀1×𝐴)×hom(𝑀1×𝐴,𝑀2×𝐴)×hom(𝑀2×𝐴,𝑆)≅hom(𝑆,𝐴)×hom(𝑆×𝐴,𝐴)×hom(𝑆×𝐴,𝑆), the three components being the first read, the second read after the first write, and the final write.
Once and twice. Under that isomorphism, once(𝑝) has components (𝗀𝖾𝗍, 𝗉𝗋2, 𝗉𝗎𝗍) and twice(𝑝) has components (𝗀𝖾𝗍, 𝗀𝖾𝗍 ∘𝗉𝗎𝗍, 𝗉𝗎𝗍 ∘(𝗉𝗎𝗍 ×id𝐴) ∘𝛿), where 𝛿 duplicates the state component. Equality of the second components is 𝗀𝖾𝗍(𝗉𝗎𝗍(𝑠,𝑎)) =𝑎, which is put-get; equality of the third is 𝗉𝗎𝗍(𝗉𝗎𝗍(𝑠,𝑎),𝑎′) =𝗉𝗎𝗍(𝑠,𝑎′), which is put-put. Conversely the three laws give the two equalities by the same reading. ◻
Take 𝑆:=𝐴 ×𝐵 and 𝗀𝖾𝗍:=𝗉𝗋1, 𝗉𝗎𝗍((𝑎,𝑏),𝑎′):=(𝑎′,𝑏0) for a fixed 𝑏0 ∈𝐵 with 𝐵 having at least two elements. By proposition 169.18 this is an element of Optic((𝑆,𝑆),(𝐴,𝐴)), hence by theorem 169.24 a natural family over Tamb, hence an inhabitant of the polymorphic type of remark 169.26. By theorem 169.28 it is not lawful: get-put fails at any (𝑎,𝑏) with 𝑏 ≠𝑏0. Naturality is therefore strictly weaker than lawfulness, and no amount of parametricity supplies the missing equations.
Referenced from 6 locations
If 𝑝 :(𝑆,𝑆) →(𝐴,𝐴) and 𝑞 :(𝐴,𝐴) →(𝐵,𝐵) are lawful then so is 𝑞 ∘𝑝.
Referenced from 4 locations
Proof of Proposition 169.30 — Lawful optics compose
Proof. Write 𝑝 =⟨𝑙1 ∣𝑟1⟩ and 𝑞 =⟨𝑙2 ∣𝑟2⟩ with residuals 𝑀1,𝑀2.
Outside. outside(𝑞∘𝑝)𝑝𝑟𝑜𝑝𝑜𝑠𝑖𝑡𝑖𝑜𝑛169.17=𝑟1∘(𝑀1⋅𝑟2)∘(𝑀1⋅𝑙2)∘𝑙1outside(𝑞)=id=𝑟1∘𝑙1outside(𝑝)=id=id𝑆.
Once and twice. Both sides of once(𝑞 ∘𝑝) =twice(𝑞 ∘𝑝) expand, by the same composition formula applied inside the two-hole set, into expressions in which the inner occurrence is once(𝑞) or twice(𝑞) and the outer one is once(𝑝) or twice(𝑝). Substituting the two hypotheses turns the second expression into the first. ◻
For the coproduct action on 𝐒𝐞𝐭, an optic corresponding under proposition 169.19 to (𝗆𝖺𝗍𝖼𝗁,𝖻𝗎𝗂𝗅𝖽) is lawful if and only if 𝗆𝖺𝗍𝖼𝗁(𝖻𝗎𝗂𝗅𝖽 𝑎) =𝗂𝗇𝗅 𝑎 and 𝗆𝖺𝗍𝖼𝗁 𝑠 =𝗂𝗇𝗅 𝑎 implies 𝖻𝗎𝗂𝗅𝖽 𝑎 =𝑠.
Referenced from 4 locations
Proof of Proposition 169.31 — Lawfulness specialises to the prism laws
Proof. As in theorem 169.28, with the coproduct computation of proposition 169.19 in place of the product one. The condition outside(𝑝) =id𝑆 becomes: the composite that matches and then rebuilds is the identity, which is the second displayed law. The condition once(𝑝) =twice(𝑝) becomes: matching a built value returns that value, which is the first. ◻
Traversals
Example 169.4 left a shape without an action. It has one.
Let M be the category of applicative functors on 𝐒𝐞𝐭 and natural transformations respecting the applicative structure, with ⊗ the composition of applicative functors and 𝐼 the identity functor. Let it act on 𝐒𝐞𝐭 by 𝐹 ⋅𝐴:=𝐹 𝐴. The unit and associativity isomorphisms of definition 169.13 are the identities of functor composition.
Referenced from 3 locations
For the action of definition 169.32, Optic((𝑆,𝑆′),(𝐴,𝐴′)) ≅ ∫𝐹(hom(𝐴,𝐹𝐴′)⟶hom(𝑆,𝐹𝑆′)), the end over applicative functors. For 𝑆 a list type and 𝐴 its element type, an element of the right-hand side is exactly an operation 𝗍𝗋𝖺𝗏𝖾𝗋𝗌𝖾:∏𝐹:(𝐴→𝐹𝐴′)→(𝑆→𝐹𝑆′) natural and applicative-preserving in 𝐹.
Referenced from 5 locations
Proof of Proposition 169.33 — Traversals are optics for that action
Proof. The displayed isomorphism is theorem 169.24 for this action, after observing that a Tambara module for it is exactly a profunctor with a strength for every applicative functor, and that the end over Tamb reduces to the end over M by evaluating at the representable Tambara modules of definition 169.22. The reading of the right-hand side as 𝗍𝗋𝖺𝗏𝖾𝗋𝗌𝖾 is corollary 169.25 with 𝑃 taken to be Kl𝐹 of example 169.6(4). ◻
Let the forward direction be a function 𝑆 →𝑀 ⋅𝐴 in 𝐒𝐞𝐭 and the backward direction a function 𝑀 ⋅𝐴′ →𝑆′ that may fail, that is a map in the Kleisli category of the partiality monad. No single C contains both as morphisms with the composition of proposition 169.17: composing two backward maps must compose two partial functions, and composing a forward with a backward map must not. The repair is to let the two hom-sets in definition 169.16 be taken in two different categories, both acted on by M, which is the definition of a mixed optic.
Referenced from 4 locations
Boundary
Proved here. The concrete lens laws and their composition (definition 169.1, proposition 169.2); profunctors and four examples (definition 169.5, example 169.6); the Tambara structure and its origin in the two accessor shapes (definition 169.7, proposition 169.8); dinaturality, ends and coends with the explicit description in 𝐒𝐞𝐭 (definition 169.9, proposition 169.11, lemma 169.12); the monoidal action and two instances (definition 169.13, proposition 169.14); the optic coend, its composition, and the specialisations to lenses and prisms (definition 169.16, proposition 169.17, proposition 169.18, proposition 169.19); the near miss when the quotient is dropped (remark 169.20); the representation theorem in both directions (lemma 169.23, theorem 169.24, corollary 169.25); lawfulness with its two specialisations and its composition (definition 169.27, theorem 169.28, proposition 169.31, proposition 169.30); an inhabitant that is not lawful (example 169.29); and traversals as optics for the applicative action (proposition 169.33).
Owned elsewhere. The identification of traversable functors with finitary containers, the composition of optics across different actions, and the mixed and enriched settings, as recorded in remark 169.34. The modular library encoding and its executable baseline are due to Pickering, Gibbons and Wu; the optic category and the notion of lawfulness used in definition 169.27 are Riley’s.
Not a foundation. Nothing above changes the ambient type theory. Optics are a construction inside a category, and the chapter’s role is to give existential types, variance, profunctors and monoidal actions a proof-bearing application. It is not a survey of accessor libraries, and no library encoding is treated as evidence for a theorem: example 169.29 is the standing reminder that a well-typed inhabitant of the encoding need not be an accessor.
Suggested first pass.
Problems exercise 169.3, exercise 169.4, and exercise 169.6 form the suggested first pass. None of these problems is a prerequisite for a later chapter.
★★☆ A setter is an operation 𝗈𝗏𝖾𝗋 :(𝐴 →𝐴′) →(𝑆 →𝑆′).
Find a monoidal action for which Optic((𝑆,𝑆′),(𝐴,𝐴′)) is the set of setters, and verify the two coherence conditions of definition 169.13.
Compute the coend for that action as in proposition 169.18, displaying each use of lemma 169.12.
State what definition 169.27 says for a setter, and show that it is equivalent to 𝗈𝗏𝖾𝗋(id) =id together with 𝗈𝗏𝖾𝗋(𝑓) ∘𝗈𝗏𝖾𝗋(𝑔) =𝗈𝗏𝖾𝗋(𝑓 ∘𝑔).
Referenced from 4 locations
★★★ Theorem 169.24 was proved by two applications of Yoneda.
Write out lemma 169.23 in full, checking that the constructed transformation respects the Tambara structures and that the two constructions are mutually inverse.
Give the direction from an optic to a natural family without appealing to the theorem, by exhibiting ˜𝑝𝑃 of corollary 169.25 and checking naturality directly.
Give the direction back, and check that the round trip through ⟨𝜆−1𝐴 ∣𝜆𝐴′⟩ is the identity.
Identify the step at which the smallness of C is used, and say what replaces it when C is large.
Referenced from 3 locations
★★★ Example 169.35 exhibits a client needing two categories.
Write the definition of a mixed optic: two categories C,D acted on by one M, and the coend ∫𝑀homC(𝑆,𝑀 ⋅𝐴) ×homD(𝑀 ⋅𝐴′,𝑆′).
Show that the composition of proposition 169.17 is still well defined, and identify which of its three verifications now uses two actions rather than one.
Exhibit a mixed composition that a same-category encoding rejects, taking D to be the Kleisli category of the partiality monad, and state which typing constraint fails in the same-category version.
Referenced from 2 locations
★★★ Practical project.optic-law-corpus Implement, in Agda, the optic hierarchy of this chapter together with a law corpus, and run a data update through the profunctor representation and back.
Calculus to implement. The profunctor interface of definition 169.5; the Tambara structures of definition 169.7 for the product action and the coproduct action; the representation maps of corollary 169.25 in both directions; the concrete lens, prism and setter representations of proposition 169.18, proposition 169.19 and exercise 169.3; the composition of proposition 169.17; and decidable checks for the three lens laws, the two prism laws, and the two setter laws. Represent an optic in the concrete form and convert to the profunctor form only where a composition requires it.
Invariant. Each conversion must be a round trip: converting a concrete optic to its profunctor form and back must return the original, and the program must check that equation for every optic it constructs. That check is the executable form of corollary 169.25. Composition must be performed in the profunctor form and the result converted back, so that proposition 169.30 is exercised rather than assumed.
Concrete result. For a nested record value, a report giving: the value before and after an update performed through a composite optic; the round-trip check for each optic used; and the verdict of each law check, positive or negative, with the witness when negative.
Acceptance test. Use the record 𝑆:=𝐴 ×(𝐵 ×𝐶) with 𝐴 =𝐵 =𝐶 =𝟐, the first-component lens 𝑝 on 𝑆, the first-component lens 𝑞 on 𝐵 ×𝐶, and the composite 𝑞 ∘𝑝 obtained through the profunctor form. Updating (𝗍𝗍,(𝗍𝗍,𝗍𝗍)) through 𝑞 ∘𝑝 with the constant-𝖿𝖿 modification must give (𝗍𝗍,(𝖿𝖿,𝗍𝗍)), and the round-trip check must pass for 𝑝, 𝑞 and the composite. The prism of example 169.3 on 𝟐 +𝟐 must pass both prism laws, and its composition with 𝑝 must be rejected by the type checker, since the shapes do not match. The unlawful lens of example 169.29 must pass the round-trip check and fail the get-put check, with the failing pair printed; that pair of outcomes is the executable form of the separation between naturality and lawfulness. Produce three mutations that still typecheck — drop the residual quotient by fixing a residual, use 𝗉𝗋1 where 𝗉𝗋2 is required in the lens conversion, and omit the applicative-preservation condition from the traversal structure — and confirm that each makes a named case fail. State explicitly that the program checks the laws at finitely many values and proves neither theorem 169.24 nor theorem 169.28.
Referenced from 3 locations