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
36 | interface Monoidal cat ten i =>
37 | Cartesian (0 cat : Hom obj) (ten : obj -> obj -> obj) (i : obj) | cat,ten where
38 | constructor MkCartesian
44 | projl : {a,b : _} -> cat (a `ten` b) a
45 | projl = Core.(.) {cat} (unitr {cat,ten,i}) (mapr' {cat,f=ten} $
elim {ten})
48 | projr : {a,b : _} -> cat (a `ten` b) b
49 | projr = Core.(.) {cat} (unitl {cat,ten,i}) (mapl' {cat,f=ten} $
elim {ten})
52 | prod : {a,b,b' : _} -> cat a b -> cat a b' -> cat a (b `ten` b')
53 | prod f g = Core.(.) (bimap' f g) split
56 | split : {a : _} -> cat a (a `ten` a)
57 | split = Cartesian.prod {ten} Core.id Core.id
60 | elim : {a : _} -> cat a i
61 | elim = Core.(.) (projl {ten}) (unitl' {ten})
67 | public export %inline %tcinline
68 | (&&&) : {ten,i : _} -> Cartesian cat ten i => {a,b,b' : _} ->
69 | cat a b -> cat a b' -> cat a (b `ten` b')
74 | PreCartesian : (cat : Hom obj) -> (ten : obj -> obj -> obj) -> (i : obj) -> Type
75 | PreCartesian = Cartesian
84 | proj : Cartesian cat ten i => {xs : _} ->
85 | (x : Fin (length xs)) -> cat (TenSeq ten i xs) (index' xs x)
86 | proj @{c@(MkCartesian{})} {xs=[_]} FZ = id
87 | proj @{c@(MkCartesian{})} {xs=[_,_]} (FS FZ) = projr
88 | proj @{c@(MkCartesian{})} {xs=_::_::_} FZ = projl
89 | proj @{c@(MkCartesian{})} {xs=_::_::_} (FS x) = proj x . projr
94 | Swizzle : (n : Nat) -> Type
95 | Swizzle n = List (Fin n)
99 | swizzleList : (xs : List a) -> Swizzle (length xs) -> List a
100 | swizzleList xs sw = map (index' xs) sw
104 | swizzle : Cartesian cat ten i => {xs : _} ->
105 | (sw : Swizzle (length xs)) -> cat (TenSeq ten i xs) (TenSeq ten i $
swizzleList xs sw)
106 | swizzle @{c@(MkCartesian{})} [] = elim {ten}
107 | swizzle @{c@(MkCartesian{})} {xs=_::_} [i] = proj i
108 | swizzle @{c@(MkCartesian{})} {xs=_::_} (i::is@(_::_)) =
109 | bimap' (proj {ten} i) (swizzle is) . split
120 | [FromCartesian] {ten,i : _} -> Cartesian cat ten i => Braided cat ten i where
121 | braid = prod projr projl
129 | Cartesian Morphism Pair () where
132 | prod f g = (,) <$> f <*> g
134 | elim = Mor $
const ()
136 | namespace Cartesian
138 | [Function] Cartesian (~~>) Pair ()
139 | using Braided.FuncPair where
142 | prod f g x = (f x, g x)
147 | public export %hint
148 | PreCartesianKleisliPair : Monad m => PreCartesian (Kleislimorphism m) Pair ()
149 | PreCartesianKleisliPair = Impl
151 | [Impl] Cartesian (Kleislimorphism m) Pair () where
152 | projl = Kleisli $
pure . fst
153 | projr = Kleisli $
pure . snd
154 | prod f g = (,) <$> f <*> g
155 | split = Kleisli $
pure . dup
156 | elim = Kleisli $
pure . const ()