module Data.Category.Limit (
Diag(..)
, DiagF
, Cone
, Cocone
, coneVertex
, coconeVertex
, LimitFam
, Limit
, HasLimits(..)
, LimitFunctor(..)
, limitAdj
, ColimitFam
, Colimit
, HasColimits(..)
, ColimitFunctor(..)
, colimitAdj
, HasTerminalObject(..)
, HasInitialObject(..)
, Zero
, BinaryProduct
, HasBinaryProducts(..)
, ProductFunctor(..)
, (:*:)(..)
, BinaryCoproduct
, HasBinaryCoproducts(..)
, CoproductFunctor(..)
, (:+:)(..)
) where
import Prelude hiding ((.), Functor, product)
import qualified Control.Arrow as A ((&&&), (***), (|||), (+++))
import Data.Category
import Data.Category.Functor
import Data.Category.NaturalTransformation
import Data.Category.Adjunction
import Data.Category.Product
import Data.Category.Coproduct
import Data.Category.Discrete
infixl 3 ***
infixl 3 &&&
infixl 2 +++
infixl 2 |||
data Diag :: (* -> * -> *) -> (* -> * -> *) -> * where
Diag :: Diag j (~>)
type instance Dom (Diag j (~>)) = (~>)
type instance Cod (Diag j (~>)) = Nat j (~>)
type instance Diag j (~>) :% a = Const j (~>) a
instance (Category j, Category (~>)) => Functor (Diag j (~>)) where
Diag % f = Nat (Const $ src f) (Const $ tgt f) $ const f
type DiagF f = Diag (Dom f) (Cod f)
type Cone f n = Nat (Dom f) (Cod f) (ConstF f n) f
type Cocone f n = Nat (Dom f) (Cod f) f (ConstF f n)
coneVertex :: Cone f n -> Obj (Cod f) n
coneVertex (Nat (Const x) _ _) = x
coconeVertex :: Cocone f n -> Obj (Cod f) n
coconeVertex (Nat _ (Const x) _) = x
type family LimitFam j (~>) f :: *
type Limit f = LimitFam (Dom f) (Cod f) f
class (Category j, Category (~>)) => HasLimits j (~>) where
limit :: Obj (Nat j (~>)) f -> Cone f (Limit f)
limitFactorizer :: Obj (Nat j (~>)) f -> (forall n. Cone f n -> n ~> Limit f)
data LimitFunctor (j :: * -> * -> *) ((~>) :: * -> * -> *) = LimitFunctor
type instance Dom (LimitFunctor j (~>)) = Nat j (~>)
type instance Cod (LimitFunctor j (~>)) = (~>)
type instance LimitFunctor j (~>) :% f = LimitFam j (~>) f
instance HasLimits j (~>) => Functor (LimitFunctor j (~>)) where
LimitFunctor % n @ Nat{} = limitFactorizer (tgt n) (n . limit (src n))
limitAdj :: HasLimits j (~>) => Adjunction (Nat j (~>)) (~>) (Diag j (~>)) (LimitFunctor j (~>))
limitAdj = mkAdjunction diag LimitFunctor (\a -> limitFactorizer (diag % a) (diag % a)) (\f @ Nat{} -> limit f)
where diag = Diag
type family ColimitFam j (~>) f :: *
type Colimit f = ColimitFam (Dom f) (Cod f) f
class (Category j, Category (~>)) => HasColimits j (~>) where
colimit :: Obj (Nat j (~>)) f -> Cocone f (Colimit f)
colimitFactorizer :: Obj (Nat j (~>)) f -> (forall n. Cocone f n -> Colimit f ~> n)
data ColimitFunctor (j :: * -> * -> *) ((~>) :: * -> * -> *) = ColimitFunctor
type instance Dom (ColimitFunctor j (~>)) = Nat j (~>)
type instance Cod (ColimitFunctor j (~>)) = (~>)
type instance ColimitFunctor j (~>) :% f = ColimitFam j (~>) f
instance HasColimits j (~>) => Functor (ColimitFunctor j (~>)) where
ColimitFunctor % n @ Nat{} = colimitFactorizer (src n) (colimit (tgt n) . n)
colimitAdj :: HasColimits j (~>) => Adjunction (~>) (Nat j (~>)) (ColimitFunctor j (~>)) (Diag j (~>))
colimitAdj = mkAdjunction ColimitFunctor diag (\f @ Nat{} -> colimit f) (\a -> colimitFactorizer (diag % a) (diag % a))
where diag = Diag
class Category (~>) => HasTerminalObject (~>) where
type TerminalObject (~>) :: *
terminalObject :: Obj (~>) (TerminalObject (~>))
terminate :: Obj (~>) a -> a ~> TerminalObject (~>)
type instance LimitFam Void (~>) f = TerminalObject (~>)
instance (HasTerminalObject (~>)) => HasLimits Void (~>) where
limit (Nat f _ _) = voidNat (Const terminalObject) f
limitFactorizer Nat{} = terminate . coneVertex
instance HasTerminalObject (->) where
type TerminalObject (->) = ()
terminalObject = id
terminate _ _ = ()
instance HasTerminalObject Cat where
type TerminalObject Cat = CatW Unit
terminalObject = CatA Id
terminate (CatA _) = CatA $ Const Z
instance (Category c, HasTerminalObject d) => HasTerminalObject (Nat c d) where
type TerminalObject (Nat c d) = Const c d (TerminalObject d)
terminalObject = natId $ Const terminalObject
terminate (Nat f _ _) = Nat f (Const terminalObject) $ terminate . (f %)
instance (HasTerminalObject c1, HasTerminalObject c2) => HasTerminalObject (c1 :**: c2) where
type TerminalObject (c1 :**: c2) = (TerminalObject c1, TerminalObject c2)
terminalObject = terminalObject :**: terminalObject
terminate (a1 :**: a2) = terminate a1 :**: terminate a2
class Category (~>) => HasInitialObject (~>) where
type InitialObject (~>) :: *
initialObject :: Obj (~>) (InitialObject (~>))
initialize :: Obj (~>) a -> InitialObject (~>) ~> a
type instance ColimitFam Void (~>) f = InitialObject (~>)
instance HasInitialObject (~>) => HasColimits Void (~>) where
colimit (Nat f _ _) = voidNat f (Const initialObject)
colimitFactorizer Nat{} = initialize . coconeVertex
data Zero
instance HasInitialObject (->) where
type InitialObject (->) = Zero
initialObject = id
initialize _ x = x `seq` error "we never get this far"
instance HasInitialObject Cat where
type InitialObject Cat = CatW Void
initialObject = CatA Id
initialize (CatA _) = CatA Nil
instance (Category c, HasInitialObject d) => HasInitialObject (Nat c d) where
type InitialObject (Nat c d) = Const c d (InitialObject d)
initialObject = natId $ Const initialObject
initialize (Nat f _ _) = Nat (Const initialObject) f $ initialize . (f %)
instance (HasInitialObject c1, HasInitialObject c2) => HasInitialObject (c1 :**: c2) where
type InitialObject (c1 :**: c2) = (InitialObject c1, InitialObject c2)
initialObject = initialObject :**: initialObject
initialize (a1 :**: a2) = initialize a1 :**: initialize a2
type family BinaryProduct ((~>) :: * -> * -> *) x y :: *
class Category (~>) => HasBinaryProducts (~>) where
proj1 :: Obj (~>) x -> Obj (~>) y -> BinaryProduct (~>) x y ~> x
proj2 :: Obj (~>) x -> Obj (~>) y -> BinaryProduct (~>) x y ~> y
(&&&) :: (a ~> x) -> (a ~> y) -> (a ~> BinaryProduct (~>) x y)
(***) :: (a1 ~> b1) -> (a2 ~> b2) -> (BinaryProduct (~>) a1 a2 ~> BinaryProduct (~>) b1 b2)
l *** r = (l . proj1 (src l) (src r)) &&& (r . proj2 (src l) (src r))
type instance LimitFam (Discrete (S n)) (~>) f = BinaryProduct (~>) (f :% Z) (LimitFam (Discrete n) (~>) (f :.: Succ n))
instance (HasLimits (Discrete n) (~>), HasBinaryProducts (~>)) => HasLimits (Discrete (S n)) (~>) where
limit = limit'
where
limit' :: forall f. Obj (Nat (Discrete (S n)) (~>)) f -> Cone f (Limit f)
limit' l@Nat{} = Nat (Const $ x *** y) (srcF l) (\z -> unCom $ h z)
where
x = l ! Z
y = coneVertex limNext
limNext = limit (l `o` natId Succ)
h :: Obj (Discrete (S n)) z -> Com (ConstF f (LimitFam (Discrete (S n)) (~>) f)) f z
h Z = Com $ proj1 x y
h (S n) = Com $ limNext ! n . proj2 x y
limitFactorizer l@Nat{} c = c ! Z &&& limitFactorizer (l `o` natId Succ) ((c `o` natId Succ) . constPostcompInv (srcF c) Succ)
type instance BinaryProduct (->) x y = (x, y)
instance HasBinaryProducts (->) where
proj1 _ _ = fst
proj2 _ _ = snd
(&&&) = (A.&&&)
(***) = (A.***)
type instance BinaryProduct Cat (CatW c1) (CatW c2) = CatW (c1 :**: c2)
instance HasBinaryProducts Cat where
proj1 (CatA _) (CatA _) = CatA Proj1
proj2 (CatA _) (CatA _) = CatA Proj2
CatA f1 &&& CatA f2 = CatA ((f1 :***: f2) :.: DiagProd)
CatA f1 *** CatA f2 = CatA (f1 :***: f2)
type instance BinaryProduct (c1 :**: c2) (x1, x2) (y1, y2) = (BinaryProduct c1 x1 y1, BinaryProduct c2 x2 y2)
instance (HasBinaryProducts c1, HasBinaryProducts c2) => HasBinaryProducts (c1 :**: c2) where
proj1 (x1 :**: x2) (y1 :**: y2) = proj1 x1 y1 :**: proj1 x2 y2
proj2 (x1 :**: x2) (y1 :**: y2) = proj2 x1 y1 :**: proj2 x2 y2
(f1 :**: f2) &&& (g1 :**: g2) = (f1 &&& g1) :**: (f2 &&& g2)
(f1 :**: f2) *** (g1 :**: g2) = (f1 *** g1) :**: (f2 *** g2)
data ProductFunctor ((~>) :: * -> * -> *) = ProductFunctor
type instance Dom (ProductFunctor (~>)) = (~>) :**: (~>)
type instance Cod (ProductFunctor (~>)) = (~>)
type instance ProductFunctor (~>) :% (a, b) = BinaryProduct (~>) a b
instance HasBinaryProducts (~>) => Functor (ProductFunctor (~>)) where
ProductFunctor % (a1 :**: a2) = a1 *** a2
data p :*: q where
(:*:) :: (Functor p, Functor q, Dom p ~ Dom q, Cod p ~ (~>), Cod q ~ (~>), HasBinaryProducts (~>)) => p -> q -> p :*: q
type instance Dom (p :*: q) = Dom p
type instance Cod (p :*: q) = Cod p
type instance (p :*: q) :% a = BinaryProduct (Cod p) (p :% a) (q :% a)
instance (Category (Dom p), Category (Cod p)) => Functor (p :*: q) where
(p :*: q) % f = (p % f) *** (q % f)
type instance BinaryProduct (Nat c d) x y = x :*: y
instance (Category c, HasBinaryProducts d) => HasBinaryProducts (Nat c d) where
proj1 (Nat f _ _) (Nat g _ _) = Nat (f :*: g) f $ \z -> proj1 (f % z) (g % z)
proj2 (Nat f _ _) (Nat g _ _) = Nat (f :*: g) g $ \z -> proj2 (f % z) (g % z)
Nat a f af &&& Nat _ g ag = Nat a (f :*: g) $ \z -> af z &&& ag z
Nat f1 f2 f *** Nat g1 g2 g = Nat (f1 :*: g1) (f2 :*: g2) $ \z -> f z *** g z
type family BinaryCoproduct ((~>) :: * -> * -> *) x y :: *
class Category (~>) => HasBinaryCoproducts (~>) where
inj1 :: Obj (~>) x -> Obj (~>) y -> x ~> BinaryCoproduct (~>) x y
inj2 :: Obj (~>) x -> Obj (~>) y -> y ~> BinaryCoproduct (~>) x y
(|||) :: (x ~> a) -> (y ~> a) -> (BinaryCoproduct (~>) x y ~> a)
(+++) :: (a1 ~> b1) -> (a2 ~> b2) -> (BinaryCoproduct (~>) a1 a2 ~> BinaryCoproduct (~>) b1 b2)
l +++ r = (inj1 (tgt l) (tgt r) . l) ||| (inj2 (tgt l) (tgt r) . r)
type instance ColimitFam (Discrete (S n)) (~>) f = BinaryCoproduct (~>) (f :% Z) (ColimitFam (Discrete n) (~>) (f :.: Succ n))
instance (HasColimits (Discrete n) (~>), HasBinaryCoproducts (~>)) => HasColimits (Discrete (S n)) (~>) where
colimit = colimit'
where
colimit' :: forall f. Obj (Nat (Discrete (S n)) (~>)) f -> Cocone f (Colimit f)
colimit' l@Nat{} = Nat (srcF l) (Const $ x +++ y) (\z -> unCom $ h z)
where
x = l ! Z
y = coconeVertex colNext
colNext = colimit (l `o` natId Succ)
h :: Obj (Discrete (S n)) z -> Com f (ConstF f (ColimitFam (Discrete (S n)) (~>) f)) z
h Z = Com $ inj1 x y
h (S n) = Com $ inj2 x y . colNext ! n
colimitFactorizer l@Nat{} c = c ! Z ||| colimitFactorizer (l `o` natId Succ) (constPostcomp (tgtF c) Succ . (c `o` natId Succ))
type instance BinaryCoproduct (->) x y = Either x y
instance HasBinaryCoproducts (->) where
inj1 _ _ = Left
inj2 _ _ = Right
(|||) = (A.|||)
(+++) = (A.+++)
type instance BinaryCoproduct Cat (CatW c1) (CatW c2) = CatW (c1 :++: c2)
instance HasBinaryCoproducts Cat where
inj1 (CatA _) (CatA _) = CatA Inj1
inj2 (CatA _) (CatA _) = CatA Inj2
CatA f1 ||| CatA f2 = CatA (CodiagCoprod :.: (f1 :+++: f2))
CatA f1 +++ CatA f2 = CatA (f1 :+++: f2)
type instance BinaryCoproduct (c1 :**: c2) (x1, x2) (y1, y2) = (BinaryCoproduct c1 x1 y1, BinaryCoproduct c2 x2 y2)
instance (HasBinaryCoproducts c1, HasBinaryCoproducts c2) => HasBinaryCoproducts (c1 :**: c2) where
inj1 (x1 :**: x2) (y1 :**: y2) = inj1 x1 y1 :**: inj1 x2 y2
inj2 (x1 :**: x2) (y1 :**: y2) = inj2 x1 y1 :**: inj2 x2 y2
(f1 :**: f2) ||| (g1 :**: g2) = (f1 ||| g1) :**: (f2 ||| g2)
(f1 :**: f2) +++ (g1 :**: g2) = (f1 +++ g1) :**: (f2 +++ g2)
data CoproductFunctor ((~>) :: * -> * -> *) = CoproductFunctor
type instance Dom (CoproductFunctor (~>)) = (~>) :**: (~>)
type instance Cod (CoproductFunctor (~>)) = (~>)
type instance CoproductFunctor (~>) :% (a, b) = BinaryCoproduct (~>) a b
instance HasBinaryCoproducts (~>) => Functor (CoproductFunctor (~>)) where
CoproductFunctor % (a1 :**: a2) = a1 +++ a2
data p :+: q where
(:+:) :: (Functor p, Functor q, Dom p ~ Dom q, Cod p ~ (~>), Cod q ~ (~>), HasBinaryCoproducts (~>)) => p -> q -> p :+: q
type instance Dom (p :+: q) = Dom p
type instance Cod (p :+: q) = Cod p
type instance (p :+: q) :% a = BinaryCoproduct (Cod p) (p :% a) (q :% a)
instance (Category (Dom p), Category (Cod p)) => Functor (p :+: q) where
(p :+: q) % f = (p % f) +++ (q % f)
type instance BinaryCoproduct (Nat c d) x y = x :+: y
instance (Category c, HasBinaryCoproducts d) => HasBinaryCoproducts (Nat c d) where
inj1 (Nat f _ _) (Nat g _ _) = Nat f (f :+: g) $ \z -> inj1 (f % z) (g % z)
inj2 (Nat f _ _) (Nat g _ _) = Nat g (f :+: g) $ \z -> inj2 (f % z) (g % z)
Nat f a fa ||| Nat g _ ga = Nat (f :+: g) a $ \z -> fa z ||| ga z
Nat f1 f2 f +++ Nat g1 g2 g = Nat (f1 :+: g1) (f2 :+: g2) $ \z -> f z +++ g z