3 | module Control.Category.Instances.Linear
5 | import Control.Category
6 | import Control.Category.Records
7 | import Control.Category.Instances.Type
8 | import public Data.Linear
9 | import Data.Linear.LEither
10 | import Data.Linear.LMaybe
11 | import Data.Morphisms
18 | data Linear : (a,b : Type0) -> Type where
19 | MkLinear : (1 _ : a.runW0 -@ b.runW0) -> Linear a b
21 | public export %inline %tcinline
22 | runLinear : Linear a b -@ a.runW0 -@ b.runW0
23 | runLinear (MkLinear f) = f
25 | public export %inline %tcinline
26 | (.runLinear) : Linear a b -@ a.runW0 -@ b.runW0
27 | (.runLinear) = runLinear
29 | public export %inline
30 | Linear_ : (0 a,b : Type) -> Type
31 | Linear_ a b = Linear (W0 a) (W0 b)
35 | LPair : Type0 -> Type0 -> Type0
36 | LPair = liftW2 LPair
39 | LEither : Type0 -> Type0 -> Type0
40 | LEither = liftW2 LEither
43 | LinearHom : Type0 -> Type0 -> Type0
44 | LinearHom a b = W0 (Linear a b)
48 | leither : a -@ c -> b -@ c -> LEither a b -@ c
49 | leither f g (Left x) = f x
50 | leither f g (Right y) = g y
58 | Category Linear where
60 | MkLinear f . MkLinear g = MkLinear (f . g)
63 | LinearSemigroupoid : Semigroupoid Linear
64 | LinearSemigroupoid = FromCategory
72 | CatFunctor Linear Typ Prelude.id where
73 | map (MkLinear f) = MkTyp $
\x => f x
76 | CatFunctor Linear Linear (liftW LMaybe) where
77 | map (MkLinear f) = MkLinear $
(<$>) f
80 | CatBifunctor Linear Linear Linear LPair where
81 | bimap (MkLinear f) (MkLinear g) = MkLinear (\(x # y) => f x # g y)
84 | CatBifunctor Linear Linear Linear LEither where
85 | bimap (MkLinear f) (MkLinear g) = MkLinear (leither (Left . f) (Right . g))
88 | Monoidal Linear LPair (W0 ()) where
89 | assoc = MkLinear $
\((x # y) # z) => x # (y # z)
90 | assoc' = MkLinear $
\(x # (y # z)) => (x # y) # z
91 | unitl = MkLinear $
\(() # x) => x
92 | unitl' = MkLinear (() #)
93 | unitr = MkLinear $
\(x # ()) => x
94 | unitr' = MkLinear (# ())
97 | Monoidal Linear LEither (W0 Void) where
98 | assoc = MkLinear $
leither (leither Left (Right . Left)) (Right . Right)
99 | assoc' = MkLinear $
leither (Left . Left) (leither (Left . Right) Right)
100 | unitl = MkLinear $
leither (\_ impossible) id
101 | unitl' = MkLinear Right
102 | unitr = MkLinear $
leither id (\_ impossible)
103 | unitr' = MkLinear Left
106 | Braided Linear LPair (W0 ()) where
107 | braid = MkLinear $
\(x # y) => y # x
110 | Braided Linear LEither (W0 Void) where
111 | braid = MkLinear $
leither Right Left
114 | Bimonoidal Linear LEither LPair (W0 Void) (W0 ()) where
115 | distribl = MkLinear $
\(x # y) => case y of
116 | Left y' => Left (x # y')
117 | Right y' => Right (x # y')
118 | distribl' = MkLinear $
leither (\(x # y) => x # Left y) (\(x # y) => x # Right y)
119 | distribr = MkLinear $
\(x # y) => case x of
120 | Left x' => Left (x' # y)
121 | Right x' => Right (x' # y)
122 | distribr' = MkLinear $
leither (\(x # y) => Left x # y) (\(x # y) => Right x # y)
123 | absorbl = MkLinear $
\(_ # _) impossible
124 | absorbl' = MkLinear $
\_ impossible
125 | absorbr = MkLinear $
\(_ # _) impossible
126 | absorbr' = MkLinear $
\_ impossible
129 | Closed Linear LPair LinearHom (W0 ()) where
130 | curry (MkLinear f) = MkLinear $
\x => MkLinear $
\y => f (x # y)
131 | uncurry (MkLinear f) = MkLinear $
\(x # y) => case f x of MkLinear f' => f' y
138 | namespace SemigroupoidR
140 | Linear : SemigroupoidR
141 | Linear = MkSemigroupoidR Linear
143 | namespace CategoryR
146 | Linear = MkCategoryR Linear
150 | LinearToTyp : FunctorR Linear Typ
151 | LinearToTyp = MkFunctorR id
152 | {impl = MkCatFunctor $
\(MkLinear f) => MkTyp (\x => f x)}
155 | LMaybe : EndofunctorR Linear
156 | LMaybe = MkFunctorR (liftW LMaybe)
158 | namespace BifunctorR
160 | LPair : EndoBifunctorR Linear
161 | LPair = MkBifunctorR LPair
164 | LEither : EndoBifunctorR Linear
165 | LEither = MkBifunctorR LEither
167 | namespace MonoidalR
169 | LinearLPair : MonoidalR
170 | LinearLPair = MkMonoidalR Linear LPair (W0 ())
173 | LinearLEither : MonoidalR
174 | LinearLEither = MkMonoidalR Linear LEither (W0 Void)
178 | LinearLPair : BraidedR
179 | LinearLPair = MkBraidedR Linear LPair (W0 ())
182 | LinearLEither : BraidedR
183 | LinearLEither = MkBraidedR Linear LEither (W0 Void)
185 | namespace BimonoidalR
187 | Linear : BimonoidalR
188 | Linear = MkBimonoidalR Linear LEither LPair (W0 Void) (W0 ())
190 | namespace RigCategoryR
192 | Linear : RigCategoryR
193 | Linear = MkRigCategoryR Linear LEither LPair (W0 Void) (W0 ())
195 | namespace SymRigCategoryR
197 | Linear : SymRigCategoryR
198 | Linear = MkSymRigCategoryR Linear LEither LPair (W0 Void) (W0 ())
203 | Linear = MkClosedR Linear LPair LinearHom (W0 ())