0 | module Control.Category.Promonad
  1 |
  2 | import Control.Category.Core
  3 | import Control.Category.Semigroupoid
  4 | import Control.Category.Functor
  5 | import Control.Category.Monoidal
  6 | import Control.Category.Braided
  7 | import Control.Category.Cartesian
  8 | import Control.Category.Cocartesian
  9 | import Control.Category.Traced
 10 | import Control.Category.Bimonoidal
 11 | import Data.Profunctor
 12 | import Data.Profunctor.Costrong
 13 | import Data.Either
 14 | import Data.Morphisms
 15 | import Data.Vect
 16 | import Data.Wrap0
 17 |
 18 | %default total
 19 |
 20 | ------------------------------------------------------------
 21 | -- Interface
 22 | ------------------------------------------------------------
 23 |
 24 | ||| A *promonad* (short for profunctor monad, no relation to regular
 25 | ||| Prelude monads) is a monad in the bicategory of profunctors.
 26 | |||
 27 | ||| Equivalently, a promonad is a category equipped with an
 28 | ||| identity-on-objects functor from the category `Typ`.
 29 | |||
 30 | ||| Laws - `fromFunc` is functorial (see `CatFunctor`), meaning:
 31 | ||| * `fromFunc id = id`
 32 | ||| * `fromFunc f . fromFunc g = fromFunc (f . g)`
 33 | public export
 34 | interface Category cat => Promonad (0 cat : Hom Type) where
 35 |   constructor MkPromonad
 36 |   ||| The unit transformation of the promonad.
 37 |   ||| Equivalently, the action of the unit functor on morphisms.
 38 |   fromFunc : (a -> b) -> cat a b
 39 |
 40 | ||| Like `Promonad`, but allows the category's objects to be wrapped
 41 | ||| using `Wrap0`. This is typically the more practical option.
 42 | |||
 43 | ||| See `Promonad` for required laws.
 44 | public export
 45 | interface Category cat => Promonad0 (0 cat : Hom Type0) where
 46 |   constructor MkPromonad0
 47 |   ||| The unit transformation of the promonad.
 48 |   ||| Equivalently, the action of the unit functor on morphisms.
 49 |   fromFuncW : (a -> b) -> cat (W0 a) (W0 b)
 50 |
 51 | ------------------------------------------------------------
 52 | -- Existing Instances
 53 | ------------------------------------------------------------
 54 |
 55 | ||| The unit identity-on-objects functor of a promonad.
 56 | public export
 57 | [PromonadUnit] Promonad cat => CatFunctor Morphism cat Prelude.id where
 58 |   map = fromFunc . applyMor
 59 |
 60 | ||| The unit identity-on-objects functor of a promonad.
 61 | public export
 62 | [PromonadUnit0] Promonad0 cat => CatFunctor Morphism cat (\x => W0 x) where
 63 |   map = fromFuncW . applyMor
 64 |
 65 | namespace Monoidal
 66 |   ||| Convert a promonad into a (pre)monoidal category.
 67 |   public export
 68 |   [FromPromonad] {ten,i : _} -> (Promonad cat, CatEndoBifunctor cat ten, Monoidal Morphism ten i) =>
 69 |       Monoidal cat ten i where
 70 |     assoc = fromFunc $ applyMor assoc
 71 |     assoc' = fromFunc $ applyMor assoc'
 72 |     unitl = fromFunc $ applyMor unitl
 73 |     unitl' = fromFunc $ applyMor unitl'
 74 |     unitr = fromFunc $ applyMor unitr
 75 |     unitr' = fromFunc $ applyMor unitr'
 76 |
 77 | namespace Braided
 78 |   ||| Convert a promonad into a braided (pre)monoidal category.
 79 |   public export
 80 |   [FromPromonad] {ten,i : _} -> (Promonad cat, CatEndoBifunctor cat ten, Braided Morphism ten i) =>
 81 |       Braided cat ten i using Monoidal.FromPromonad where
 82 |     braid = fromFunc $ applyMor braid
 83 |
 84 | namespace MonSymmetric
 85 |   ||| Convert a promonad into a symmetric (pre)monoidal category.
 86 |   public export
 87 |   [FromPromonad] {ten,i : _} -> (Promonad cat, CatEndoBifunctor cat ten, MonSymmetric Morphism ten i) =>
 88 |       MonSymmetric cat ten i using Braided.FromPromonad where
 89 |
 90 | namespace Cartesian
 91 |   ||| Convert a promonad into a cartesian (pre)monoidal category.
 92 |   public export
 93 |   [FromPromonad] {ten,i : _} -> (Promonad cat, CatEndoBifunctor cat ten, Cartesian Morphism ten i) =>
 94 |       Cartesian cat ten i using Monoidal.FromPromonad where
 95 |     projl = fromFunc $ applyMor projl
 96 |     projr = fromFunc $ applyMor projr
 97 |     prod f g = Core.(.) {cat} (bimap' f g) (split {cat,ten,i})
 98 |     split = fromFunc $ applyMor split
 99 |     elim = fromFunc $ applyMor $ elim {ten}
100 |
101 | namespace Cocartesian
102 |   ||| Convert a promonad into a cocartesian (pre)monoidal category.
103 |   public export
104 |   [FromPromonad] {ten,i : _} -> (Promonad cat, CatEndoBifunctor cat ten, Cocartesian Morphism ten i) =>
105 |       Cocartesian cat ten i using Monoidal.FromPromonad where
106 |     injl = fromFunc $ applyMor injl
107 |     injr = fromFunc $ applyMor injr
108 |     coprod f g = Core.(.) {cat} (merge {cat,ten,i}) (bimap' f g)
109 |     merge = fromFunc $ applyMor merge
110 |     intro = fromFunc $ applyMor $ intro {ten}
111 |
112 | namespace Bimonoidal
113 |   ||| Convert a promonad into a (pre)bimonoidal category.
114 |   public export
115 |   [FromPromonad] {add,mul,z,i : _} -> (Promonad cat, CatEndoBifunctor cat add, CatEndoBifunctor cat mul,
116 |                   Bimonoidal Morphism add mul z i) => Bimonoidal cat add mul z i
117 |       using Monoidal.FromPromonad where
118 |     distribl = fromFunc $ applyMor distribl
119 |     distribl' = fromFunc $ applyMor distribl'
120 |     distribr = fromFunc $ applyMor distribr
121 |     distribr' = fromFunc $ applyMor distribr'
122 |     absorbl = fromFunc $ applyMor $ absorbl {add,mul}
123 |     absorbl' = fromFunc $ applyMor $ absorbl' {add,mul}
124 |     absorbr = fromFunc $ applyMor $ absorbr {add,mul}
125 |     absorbr' = fromFunc $ applyMor $ absorbr' {add,mul}
126 |
127 |
128 | -- These instances should not be used unless necessary, as they have
129 | -- poor runtime quantity behavior. Prefer `Typ` over base's `Morphism`
130 | -- and `Kleisli` over base's `Kleislimorphism`.
131 |
132 | public export
133 | Promonad Morphism where
134 |   fromFunc = Mor
135 |
136 | namespace Promonad
137 |   public export
138 |   [Function] Promonad (~~>) using Category.Function where
139 |     fromFunc = id
140 |
141 | public export
142 | Monad m => Promonad (Kleislimorphism m) where
143 |   fromFunc f = Kleisli $ pure . f
144 |