Sari la conținutul principal

Categorial םptics

Contribuitor

Generalized optics

Let there be two categories C\mathcal{C} and D\mathcal{D} with objects S,ACS, A \in \mathcal{C} and T,BDT, B \in \mathcal{D}. A (generalized) (,)(Ⓛ,Ⓡ)-optic from (S,T)(S, T) to focuses (A,B)(A, B) is an element of the following coend:

Optic,((A,B),(S,T))=MMC(S,MA)×D(MB,T)\mathbf{Optic}_{Ⓛ, Ⓡ}((A, B),(S, T)) = \int^{M \in \mathcal{M}} \mathcal{C}(S, M Ⓛ A) \times \mathcal{D}(M Ⓡ B, T)

where M\mathcal{M} is a monoidal category with two (left) actions , acting on C\mathcal{C}, respectively D\mathcal{D}.

An optic, conceptually, is a bidirectional data accessor. For a programmer it is easy to imagine that the optic basically decomposes a type or data structure through a monoidal action to view a nested structure. The result of the decomposition is a context or residual that recomposes with other data to get a composite object. A programmer would have trouble taking advantage of an optic in this form, especially because the most widely used programming languages do not support very abstract semantics like Haskell does, for instance.

The coend is a class of equivalence of all the ways the two hom-objects/hom-sets interact through the residual MM. When the monoidal actions are known and have convenient properties the coend reduces via these properties and Yoneda reductions to a more manageable form. Various specialized optics have already been presented in the literature, but we would like to review some of those and propose ways to make them more accessible to programmers. Furthermore, the literature actively has been growing the past years, and optics have been subjected to analysis from various viewpoints. Many of the results are dependent on the properties of the categories assumed by the authors when working optics. We will be focussing only on some aspects of optics and from here on out we will propose a few tweeks to optics relax some requirements.

First, we should point out that the most important aspect of optics is that they are higher lever abstractions that can be composed easily. The composition is a consequnce of the axioms of the monoidal actions. Any two (,)(Ⓛ,Ⓡ)-optics can be composed as follows:

Optic(,)((A,B),(S,T))×Optic(,)((Z,W),(A,B))=\mathbf{Optic}_{(Ⓛ,Ⓡ)}((A, B),(S, T)) \times \mathbf{Optic}_{(Ⓛ,Ⓡ)}((Z, W),(A, B)) = MMC(S,MA)×D(MB,T)×NMC(A,NZ)×D(NW,B)\int^{M \in \mathcal{M}} \mathcal{C}(S, M Ⓛ A) \times \mathcal{D}(M Ⓡ B, T) \times \int^{N \in \mathcal{M}} \mathcal{C}(A, N Ⓛ Z) \times \mathcal{D}(N Ⓡ W, B) \rightarrow M,NMC(S,MA)×C(A,NZ)×D(NW,B)×D(MB,T)\int^{M,N \in \mathcal{M}} \mathcal{C}(S, M Ⓛ A) \times \mathcal{C}(A, N Ⓛ Z) \times \mathcal{D}(N Ⓡ W, B) \times \mathcal{D}(M Ⓡ B, T) \rightarrow M,NMC(S,MNZ)×D(MNW,T)\int^{M,N \in \mathcal{M}} \mathcal{C}(S, M Ⓛ N Ⓛ Z) \times \mathcal{D}(M Ⓡ N Ⓡ W, T) \rightarrow M,NMC(S,(MN)Z)×D((MN)W,T)\int^{M,N \in \mathcal{M}} \mathcal{C}(S, (M \otimes N) Ⓛ Z) \times \mathcal{D}((M \otimes N) Ⓡ W, T) \rightarrow PMC(S,PZ)×D(PW,T)=\int^{P \in \mathcal{M}} \mathcal{C}(S, P Ⓛ Z) \times \mathcal{D}(P Ⓡ W, T) = Optic(,)((Z,W),(S,T))\mathbf{Optic}_{(Ⓛ,Ⓡ)}((Z, W),(S, T))

We can clearly observe from the demonstration that the two monoidal actions are conveniently strong just so we can compose the hom-sets in both directions. However, we can also see that the two monoidal actions work independently from one another; they only interact through the residuals. Element-wise, optics l1r1,l2r2\braket{l_{1}|r_{1}}, \braket{l_{2}|r_{2}}, l1:SMA,l2:ANZ,r1:MBT,r2:NWBl_{1}: S \rightarrow M Ⓛ A, l_{2}: A \rightarrow N Ⓛ Z, r_{1}: M Ⓡ B \rightarrow T, r_{2}: N Ⓡ W \rightarrow B compose as follows:

l2r2l1r1=αⓁ M,N,Z1(idMl2)l1r1(idMr2)αⓇ M,N,W\braket{l_{2}|r_{2}} \circ \braket{l_{1}|r_{1}} = \braket{\alpha^{-1}_{Ⓛ~M, N, Z} \circ (id_{M} Ⓛ l_{2}) \circ l_{1}|r_{1} \circ (id_{M} Ⓡ r_{2}) \circ \alpha_{Ⓡ~M, N, W}}

Theorem - Functorial lifting of functors

Let there be functors Fcst:CCF_{cst} : \mathcal{C} \rightarrow \mathcal{C'} and Fstr:DDF_{str} : \mathcal{D} \rightarrow \mathcal{D'} exhibiting tensorial (left-)costrength respectively tensorial (left-)strength over the respective (left-)actions of a (Ⓛ,Ⓡ)-optic. The pair (Fcst,Fstr)(F_{cst}, F_{str}) determines a (pseudo)functor mapping (Ⓛ,Ⓡ)-optics to (Ⓛ',Ⓡ')-optics enabled by costrength respectively strength by mapping obj:

(Fcst,Fstr):Optic,Optic,(F_{cst}, F_{str}) : \mathbf{Optic}_{Ⓛ,Ⓡ} \rightarrow \mathbf{Optic}_{Ⓛ',Ⓡ'}

Basically, the functor pair applies the following way by pairs of objects (Fcst,Fstr)((A,B))=(FcstA,FstrB)(F_{cst}, F_{str})((A, B)) = (F_{cst}A, F_{str}B) and optics lr, l:SMA, r:MBT\braket{l|r},~l : S \rightarrow M Ⓛ A,~r : M Ⓡ B \rightarrow T to (Fcst,Fstr)(lr)=cstFcstlFstrstr(F_{cst}, F_{str})(\braket{l|r}) = \braket{cst \circ F_{cst} l|F_{str} \circ str}:

Optic,((A,B),(S,T))(Fcst,Fstr)\mathbf{Optic}_{Ⓛ,Ⓡ}((A, B), (S, T)) \xrightarrow{(F_{cst}, F_{str})} MMC(FcstS,Fcst(MA))×D(Fstr(MB),FstrT)cstM,A,strM,B\int^{M \in \mathcal{M}}\mathcal{C}(F_{cst}S, F_{cst}(M Ⓛ A)) \times \mathcal{D}(F_{str}(M Ⓡ B), F_{str}T) \xrightarrow{cst_{M,A}, str_{M,B}} MMC(FcstS,MFcstA)×D(MFstrB,FstrT)=\int^{M \in \mathcal{M}}\mathcal{C}(F_{cst}S, M Ⓛ' F_{cst}A) \times \mathcal{D}(M Ⓡ' F_{str}B, F_{str}T) = Optic,((FcstS,FstrT),(FcstA,FstrB))=\mathbf{Optic}_{Ⓛ',Ⓡ'}((F_{cst}S, F_{str}T), (F_{cst}A, F_{str}B)) = (Fcst,Fstr)(Optic,((A,B),(S,T)))(F_{cst}, F_{str})(\mathbf{Optic}_{Ⓛ,Ⓡ}((A, B), (S, T)))

Proof. For (Fcst,Fstr)(F_{cst}, F_{str}) to be a functor it must preserve identity and composition. The identity for optics is induced by the unitors, as pointed out by, id(S,T)=λ1λid_{(S, T)} = \braket{\lambda^{-1}_{Ⓛ}|\lambda_{Ⓡ}}.

(Fcst,Fstr)(id(S,T))=(Fcst,Fstr)(λ,S1λ,T)=(F_{cst}, F_{str})(id_{(S, T)}) = (F_{cst}, F_{str})(\braket{\lambda^{-1}_{Ⓛ,S}|\lambda_{Ⓡ, T}}) = Fcstλ,S1Fstrλ,TcstIC,S,strID,Tλ,FcstS1λ,FstrT\braket{F_{cst}\lambda^{-1}_{Ⓛ,S}|F_{str}\lambda_{Ⓡ,T}} \xrightarrow{cst_{I_{\mathcal{C}},S}, str_{I_{\mathcal{D}},T}} \braket{\lambda^{-1}_{Ⓛ',F_{cst}S}|\lambda_{Ⓡ',F_{str}T}} Optic,((A,B),(S,T))×Optic,((Z,W),(A,B))=\mathbf{Optic}_{Ⓛ, Ⓡ}((A, B),(S, T)) \times \mathbf{Optic}_{Ⓛ, Ⓡ}((Z, W),(A, B)) = Optic,((Z,W),(S,T))\mathbf{Optic}_{Ⓛ, Ⓡ}((Z, W),(S, T)) (Fcst,Fstr)(Optic,((A,B),(S,T)))×(Fcst,Fstr)(Optic,((Z,W),(A,B)))=(F_{cst}, F_{str})(\mathbf{Optic}_{Ⓛ, Ⓡ}((A, B),(S, T))) \times (F_{cst}, F_{str})(\mathbf{Optic}_{Ⓛ, Ⓡ}((Z, W),(A, B))) = Optic,((FcstA,FstrB),(FcstS,FstrT))×Optic,((FcstZ,FstrW),(FcstA,FstrB))\mathbf{Optic}_{Ⓛ, Ⓡ}((F_{cst}A, F_{str}B),(F_{cst}S, F_{str}T)) \times \mathbf{Optic}_{Ⓛ, Ⓡ}((F_{cst}Z, F_{str}W),(F_{cst}A, F_{str}B)) \rightarrow Optic,((FcstZ,FstrW),(FcstS,FstrT))=(Fcst,Fstr)(Optic,((Z,W),(S,T)))\mathbf{Optic}_{Ⓛ', Ⓡ'}((F_{cst}Z, F_{str}W),(F_{cst}S, F_{str}T)) = (F_{cst}, F_{str})(\mathbf{Optic}_{Ⓛ, Ⓡ}((Z, W),(S, T)))

Resources