0 | module Control.Category.Cartesian
2 | import Control.Category.Core
3 | import Control.Category.Functor
4 | import Control.Category.Monoidal
5 | import Control.Category.Braided
6 | import Data.Morphisms
37 | interface Monoidal cat ten i =>
38 | Cartesian (0 cat : Hom obj) (ten : obj -> obj -> obj) (i : obj) | cat,ten where
39 | constructor MkCartesian
45 | projl : {a,b : _} -> cat (a `ten` b) a
46 | projl = Core.(.) {cat} (unitr {cat,ten,i}) (mapr' {cat,f=ten} $
elim {ten})
49 | projr : {a,b : _} -> cat (a `ten` b) b
50 | projr = Core.(.) {cat} (unitl {cat,ten,i}) (mapl' {cat,f=ten} $
elim {ten})
53 | prod : {a,b,b' : _} -> cat a b -> cat a b' -> cat a (b `ten` b')
54 | prod f g = Core.(.) (bimap' f g) split
57 | split : {a : _} -> cat a (a `ten` a)
58 | split = Cartesian.prod {ten} Core.id Core.id
61 | elim : {a : _} -> cat a i
62 | elim = Core.(.) (projl {ten}) (unitl' {ten})
68 | public export %inline %tcinline
69 | (&&&) : {ten,i : _} -> Cartesian cat ten i => {a,b,b' : _} ->
70 | cat a b -> cat a b' -> cat a (b `ten` b')
75 | PreCartesian : (cat : Hom obj) -> (ten : obj -> obj -> obj) -> (i : obj) -> Type
76 | PreCartesian = Cartesian
85 | proj : Cartesian cat ten i => {xs : _} ->
86 | (x : Fin (length xs)) -> cat (TenSeq ten i xs) (index' xs x)
87 | proj @{c@(MkCartesian{})} {xs=[_]} FZ = id
88 | proj @{c@(MkCartesian{})} {xs=[_,_]} (FS FZ) = projr
89 | proj @{c@(MkCartesian{})} {xs=_::_::_} FZ = projl
90 | proj @{c@(MkCartesian{})} {xs=_::_::_} (FS x) = proj x . projr
95 | Swizzle : (n : Nat) -> Type
96 | Swizzle n = List (Fin n)
100 | swizzleList : (xs : List a) -> Swizzle (length xs) -> List a
101 | swizzleList xs sw = map (index' xs) sw
105 | swizzle : Cartesian cat ten i => {xs : _} ->
106 | (sw : Swizzle (length xs)) -> cat (TenSeq ten i xs) (TenSeq ten i $
swizzleList xs sw)
107 | swizzle @{c@(MkCartesian{})} [] = elim {ten}
108 | swizzle @{c@(MkCartesian{})} {xs=_::_} [i] = proj i
109 | swizzle @{c@(MkCartesian{})} {xs=_::_} (i::is@(_::_)) = prod (proj {ten} i) (swizzle is)
120 | [FromCartesian] {ten,i : _} -> Cartesian cat ten i => Braided cat ten i where
121 | braid = prod projr projl
123 | namespace MonSymmetric
127 | [FromCartesian] {ten,i : _} -> Cartesian cat ten i => MonSymmetric cat ten i
128 | using Braided.FromCartesian where
130 | let invt = toList $
permSeq $
inverse p
131 | in replace {p = cat _ . TenSeq ten i} (believe_me $
Refl {x=()}) (swizzle invt)
139 | Cartesian Morphism Pair () where
142 | prod f g = (,) <$> f <*> g
144 | elim = Mor $
const ()
146 | namespace Cartesian
148 | [Function] Cartesian (~~>) Pair ()
149 | using Braided.FuncPair where
152 | prod f g x = (f x, g x)
157 | public export %hint
158 | PreCartesianKleisli : Monad m => PreCartesian (Kleislimorphism m) Pair ()
159 | PreCartesianKleisli = Impl
161 | [Impl] Cartesian (Kleislimorphism m) Pair () where
162 | projl = Kleisli $
pure . fst
163 | projr = Kleisli $
pure . snd
164 | prod f g = (,) <$> f <*> g
165 | split = Kleisli $
pure . dup
166 | elim = Kleisli $
pure . const ()