\import Category
\import Category.Functor
\import Category.Limit
\import Category.Subcat
\import Equiv
\import Function.Meta
\import Logic
\import Meta
\import Paths
\import Paths.Meta
\import Set.SetCategory
-- | Presheaves valued in {D}
\record VPresheaf (D : Precat) (C : Precat) (\coerce F : Functor C.op D) {
\func toPresheaf.{u} (U : Functor D SetCat.{u}) : Presheaf C \cowith
| F => Comp U F
} \where {
\func functor-at-point {D : Cat} {C : Precat} {J : Precat} (G : Functor J (VPresheafCat D C)) (c : C) : Functor J D \cowith
| F j => G j c
| Func f => G.Func f c
| Func-id {X} => rewrite (G.Func-id {X}) idp
| Func-o => rewrite G.Func-o idp
}
-- | Presheaves valued in sets
\func Presheaf.{u} (C : Precat) => VPresheaf SetCat.{u} C
\instance VPresheafCat (D : Cat) (C : Precat) : Cat
=> subCat {FunctorCat C.op D} {VPresheaf D C} (\new Embedding {
| f P => P
| isEmb _ _ => \new Retraction {
| sec p => ext p
| f_sec => idpe
}
})
\instance PresheafCat.{u} (C : Precat) : Cat
=> VPresheafCat SetCat.{u} C
\instance VPresheafComplete.{u} (D : CompleteCat.{u}) (C : Precat) : CompleteCat.{u}
| Cat => VPresheafCat D C
| limit => limit
| terminal {
| apex => terminalPresheaf
| proj => \case __
| tupleMap _ => \new NatTrans {
| trans _ => D.terminalMap
| natural _ => D.terminal-unique
}
| tupleBeta {_} {_} {j} => \case j
| tupleEq _ => exts \lam a => D.terminal-unique
}
| Bprod S T => \new Product {
| apex => BprodPresheaf S T
| proj => \case \elim __ \with {
| 0 => \new NatTrans {
| trans a => D.proj1
| natural f => D.beta1
}
| 1 => \new NatTrans {
| trans a => D.proj2
| natural f => D.beta2
}
}
| tupleMap h => \new NatTrans {
| trans a => D.pair (h 0 a) (h 1 a)
| natural f => D.pair-unique
(inv o-assoc *> pmap (∘ _) D.beta1 *> (h 0).natural f *> pmap (_ ∘) (inv D.beta1) *> inv o-assoc *> pmap (∘ _) (inv D.beta1) *> o-assoc)
(inv o-assoc *> pmap (∘ _) D.beta2 *> (h 1).natural f *> pmap (_ ∘) (inv D.beta2) *> inv o-assoc *> pmap (∘ _) (inv D.beta2) *> o-assoc)
}
| tupleBeta {z} {f} {j} => \case \elim j \with {
| 0 => exts \lam a => D.beta1
| 1 => exts \lam a => D.beta2
}
| tupleEq c => exts \lam a => D.pair-unique (path \lam i => c 0 i a) (path \lam i => c 1 i a)
}
\where {
\protected \func limit.{u} {D : CompleteCat.{u}} {C : Precat} {J : Precat.{u,u}} (G : Functor J (VPresheafCat D C)) : Limit G \cowith
| apex => apex
| coneMap => cone-nat-map
| coneCoh h => exts \lam _ => coneCoh h
| limMap => lim-nat-map
| limBeta {z} z-cone _ => exts \lam X => rewrite (limBeta {L X} (cone-at-point X z z-cone)) idp
| limUnique j-unique => exts \lam X => limUnique $ \lam j => path (\lam i => j-unique j i X)
\where {
\func L (c : C) => D.limit {J} (VPresheaf.functor-at-point G c)
\func cone {X Y : C} (f : Hom Y X) : Cone (VPresheaf.functor-at-point G Y) \cowith
| apex => Cone.apex {L X}
| coneMap j => Func f ∘ coneMap {L X} j
| coneCoh h => rewriteI (coneCoh {L X} h, o-assoc, o-assoc)
(rewrite (natural {Func {G} h} f) idp)
\func apex : VPresheaf D C \cowith
| F => \new Functor {
| F c => L c
| Func f => limMap (cone f)
| Func-id {X} => limUnique \lam j => limBeta {L X} (cone id) j *> pmap (∘ _) Func-id *> id-left *> inv id-right
| Func-o {_} {Y} {Z} {g} {f} => limUnique (\lam j => rewrite (limBeta {L Z} (cone (f ∘ g)) j, inv o-assoc,
limBeta {L Z} (cone g) j) (unfold coneMap
(rewrite (Func-o {G j}, o-assoc, o-assoc) (rewrite (limBeta {L Y} (cone f) j) (unfold coneMap idp)))))
}
\func cone-nat-map (j : J) : Hom apex (G j) => \new NatTrans {
| trans c => coneMap {L c} j
| natural {_} {Y} f => rewrite (limBeta {L Y} (cone f) j) idp
}
\func cone-at-point (X : C) (H : VPresheaf D C) (z-cone : Cone {J} G H) : Cone (VPresheaf.functor-at-point G X) (H X) \cowith
| coneMap j => coneMap {z-cone} j X
| coneCoh h => path (\lam i => ((coneCoh {z-cone} h) i) X)
\func lim-nat-map {z : VPresheaf D C} (z-cone : Cone G z) : Hom z apex => \new NatTrans {
| trans X => limMap $ cone-at-point X z z-cone
| natural {X} {Y} f => limUnique \lam j =>
rewrite (inv o-assoc, (L Y).limBeta (cone-at-point Y z z-cone) j,
inv o-assoc, (L Y).limBeta (cone f), o-assoc,
(L X).limBeta (cone-at-point X z z-cone))
$ natural {coneMap {z-cone} j} f
}
}
\func terminalPresheaf : VPresheaf D C \cowith
| F {
| F _ => terminal
| Func _ => D.terminalMap
| Func-id => D.terminal-unique
| Func-o => D.terminal-unique
}
\func BprodPresheaf (S T : VPresheaf D C) : VPresheaf D C \cowith
| F {
| F a => Bprod (S a) (T a)
| Func f => D.prodMap (S.F.Func f) (T.F.Func f)
| Func-id => pmap2 D.prodMap S.F.Func-id T.F.Func-id *> D.prod-id
| Func-o => pmap2 D.prodMap S.F.Func-o T.F.Func-o *> inv D.prodMap-comp
}
}
-- | Evaluation at an object of {C}.
\func evalVPresheaf {D : Cat} {C : Precat} (c : C) : Functor (VPresheafCat D C) D \cowith
| F P => P c
| Func a => a c
| Func-id => idp
| Func-o => idp
{- | If you have a cone over a diagram of presheaves, and that cone evaluates to a limit cone at every single object $c$,
then the original cone is a limit cone in the presheaf category-}
\lemma vpresheafCone-fromPointwise {D : Cat} {C : Precat} {J : Precat} {G : Functor J (VPresheafCat D C)}
(L : Cone G) (p : \Pi (c : C) -> IsLimitCone (Cone.map (evalVPresheaf c) L))
: IsLimitCone L
=> \lam P => inP \new QEquiv {
| ret K => \new NatTrans {
| trans c => (pointLimit c).limMap (Cone.map (evalVPresheaf c) K)
| natural {X} {Y} u => (pointLimit Y).limUnique \lam j =>
inv o-assoc *> pmap (∘ _) ((pointLimit Y).limBeta (Cone.map (evalVPresheaf Y) K) j) *>
(K.coneMap j).natural u *>
pmap (_ ∘) (inv ((pointLimit X).limBeta (Cone.map (evalVPresheaf X) K) j)) *>
inv o-assoc *> pmap (∘ _) (inv ((L.coneMap j).natural u)) *> o-assoc
}
| ret_f u => exts \lam c => (pointLimit c).limUniqueBeta \lam j => idp
| f_sec K => exts \lam j => exts \lam c => (pointLimit c).limBeta (Cone.map (evalVPresheaf c) K) j
}
\where {
\private \func pointLimit (c : C) : Limit (Comp (evalVPresheaf c) G) (L.apex c)
=> Limit.fromIsLimit (Cone.map (evalVPresheaf c) L) (p c)
}
{- | The evaluation functor $ev_c : [C^{op}, D] \to D$, which takes a presheaf $F$ and evaluates it at an object $c$ (giving $F(c)$), preserves limits. -}
\lemma evalVPresheaf-preservesLimit.{u} {D : CompleteCat.{u}} {C : Precat} {J : Precat.{u,u}}
(G : Functor J (VPresheafCat D C)) (c : C)
: PreservesLimit (evalVPresheaf c) G
=> preservesLimit-fromLimit (evalVPresheaf c) (VPresheafComplete.limit G)
(D.limit (Comp (evalVPresheaf c) G)).isLimit
{- | If $L$ is a limit of a diagram of presheaves $G$, then for any object $c$, $L(c)$ is a limit of the evaluated diagram $G(c)$ -}
\func vpresheafLimit-pointwise.{u} {D : CompleteCat.{u}} {C : Precat} {J : Precat.{u,u}}
{G : Functor J (VPresheafCat D C)} (L : Limit G) (c : C)
: Limit (Comp (evalVPresheaf c) G) (L.apex c)
=> Limit.fromIsLimit (Cone.map (evalVPresheaf c) L) (evalVPresheaf-preservesLimit G c L L.isLimit)