Skip to content

Commit 4eeaab4

Browse files
Complete implementation of walking adjunction
1 parent 4dabba2 commit 4eeaab4

File tree

4 files changed

+122
-91
lines changed

4 files changed

+122
-91
lines changed

proarrow.cabal

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ library
4444
Proarrow.Adjunction
4545
Proarrow.Category
4646
Proarrow.Category.Bicategory
47-
-- Proarrow.Category.Bicategory.Adj
47+
Proarrow.Category.Bicategory.Adj
4848
Proarrow.Category.Bicategory.Bidiscrete
4949
Proarrow.Category.Bicategory.CategoryAsBi
5050
Proarrow.Category.Bicategory.Co

src/Proarrow/Category/Bicategory.hs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ instance (Ob (I :: kk i i)) => ObUnit kk i
6868
-- * 1-cells are types of kind @kk j k@ for any 0-cells @j@ and @k@, satisfying the @Ob@ constraint.
6969
-- * 2-cells are values of type @p ~> q@, where @p@ and @q@ are 1-cells.
7070
type Bicategory :: forall {s}. CAT s -> Constraint
71-
class (Locally CategoryOf kk, CategoryOf s, forall i. (Ob0 kk i) => ObUnit kk i) => Bicategory (kk :: CAT s) where
71+
class (Locally CategoryOf kk, forall i. (Ob0 kk i) => ObUnit kk i) => Bicategory (kk :: CAT s) where
7272
type Ob0 kk (j :: k) :: Constraint
7373
type Ob0 kk j = ()
7474
type I :: kk i i
@@ -86,6 +86,7 @@ class (Locally CategoryOf kk, CategoryOf s, forall i. (Ob0 kk i) => ObUnit kk i)
8686

8787
-- | Get proof that the source and target of a 1-cell are 0-cells.
8888
withOb0s :: forall {j} {k} a r. (Ob (a :: kk j k)) => ((Ob0 kk j, Ob0 kk k) => r) -> r
89+
-- no default implementation possible since withOb0s has an ambiguous type
8990

9091
-- | Observe constraints from a 2-cell value.
9192
(\\\) :: ((Ob0 kk i, Ob0 kk j, Ob ps, Ob qs) => r) -> (ps :: kk i j) ~> qs -> r
Lines changed: 108 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,21 @@
1+
{-# LANGUAGE AllowAmbiguousTypes #-}
2+
13
module Proarrow.Category.Bicategory.Adj where
24

5+
import Data.Kind (Constraint, Type)
6+
import Prelude (type (~))
7+
38
import Proarrow.Category.Bicategory
4-
( Bicategory (..)
9+
( Adjunction_ (..)
10+
, Bicategory (..)
511
, Comonad (..)
6-
, IsPath (..)
712
, Monad (..)
8-
, Path (..)
9-
, SPath (..)
10-
, withUnital
11-
, type (+++)
1213
)
14+
import Proarrow.Category.Bicategory qualified as Bi
15+
import Proarrow.Category.Bicategory.Strictified (Assoc, Path (..), type (+++))
16+
import Proarrow.Category.Equipment (Cotight, CotightAdjoint, Equipment (..), IsOb, Tight, TightAdjoint, WithObO2 (..))
1317
import Proarrow.Category.Instance.Simplex (Nat (..), Simplex (..))
14-
import Proarrow.Core (CAT, CategoryOf (..), Is, Profunctor (..), Promonad (..), UN, dimapDefault)
18+
import Proarrow.Core (CAT, CategoryOf (..), Is, Profunctor (..), Promonad (..), UN, dimapDefault, obj)
1519
import Proarrow.Object (src, tgt)
1620

1721
type data AB = A | B
@@ -33,9 +37,25 @@ data Adj ps qs where
3337
AdjCup :: Adj (AK ps) qs -> Adj (AK (R ::: L ::: ps)) qs
3438
AdjCap :: Adj ps (AK qs) -> Adj ps (AK (L ::: R ::: qs))
3539

36-
class IsRL rl where rOrL :: Adj (AK ps) (AK qs) -> Adj (AK (rl ::: ps)) (AK (rl ::: qs))
37-
instance IsRL L where rOrL = AdjL
38-
instance IsRL R where rOrL = AdjR
40+
type SAdj :: Path ABK i j -> Type
41+
data SAdj p where
42+
SNil :: SAdj Nil
43+
SL :: (IsLRPath ps) => SAdj (L ::: ps)
44+
SR :: (IsLRPath ps) => SAdj (R ::: ps)
45+
46+
type IsLRPath :: forall {i} {j}. Path ABK i j -> Constraint
47+
class (ps +++ Nil ~ ps, forall i h (qs :: Path ABK k h) (rs :: Path ABK h i). Assoc ps qs rs) => IsLRPath (ps :: Path ABK j k) where
48+
singPath :: SAdj ps
49+
withIsPath2 :: (IsLRPath qs) => ((IsLRPath (ps +++ qs)) => r) -> r
50+
instance IsLRPath Nil where
51+
singPath = SNil
52+
withIsPath2 r = r
53+
instance (IsLRPath ps) => IsLRPath (L ::: ps) where
54+
singPath = SL
55+
withIsPath2 @qs r = withIsPath2 @ps @qs r
56+
instance (IsLRPath ps) => IsLRPath (R ::: ps) where
57+
singPath = SR
58+
withIsPath2 @qs r = withIsPath2 @ps @qs r
3959

4060
instance Profunctor (Adj :: CAT (ADJK a b)) where
4161
dimap = dimapDefault
@@ -45,12 +65,12 @@ instance Profunctor (Adj :: CAT (ADJK a b)) where
4565
r \\ AdjCup f = r \\ f
4666
r \\ AdjCap f = r \\ f
4767
instance Promonad (Adj :: CAT (ADJK a b)) where
48-
id :: forall (ps :: ADJK a b). (Ob ps) => Adj ps ps
49-
id = go (singPath @(UN AK ps))
68+
id @ps = go (singPath @(UN AK ps))
5069
where
51-
go :: forall ps'. SPath ps' -> Adj (AK ps') (AK ps')
70+
go :: forall ps'. SAdj ps' -> Adj (AK ps') (AK ps')
5271
go SNil = AdjNil
53-
go (SCons @rl p ps) = rOrL @rl (go ps) \\ p
72+
go SL = AdjL id
73+
go SR = AdjR id
5474
AdjNil . f = f
5575
f . AdjNil = f
5676
AdjR f . AdjR g = AdjR (f . g)
@@ -64,28 +84,32 @@ instance Promonad (Adj :: CAT (ADJK a b)) where
6484

6585
instance CategoryOf (ADJK a b) where
6686
type (~>) = Adj
67-
type Ob ps = (Is AK ps)
87+
type Ob ps = (Is AK ps, IsLRPath (UN AK ps))
6888

6989
-- | The walking adjunction
7090
instance Bicategory ADJK where
71-
type Ob0 ADJK a = (IsRL a)
7291
type I = AK Nil
73-
type ps `O` qs = AK (UN AK ps +++ UN AK qs)
92+
type ps `O` qs = AK (UN AK qs +++ UN AK ps)
93+
withOb2 @ps @qs k = withIsPath2 @(UN AK qs) @(UN AK ps) k
94+
withOb0s r = r
7495
r \\\ AdjNil = r
7596
r \\\ AdjR f = r \\\ f
7697
r \\\ AdjL f = r \\\ f
7798
r \\\ AdjCup f = r \\\ f
7899
r \\\ AdjCap f = r \\\ f
79100
o :: forall {a} {b} (ps :: ADJK a b) qs rs ss. (ps ~> qs) -> (rs ~> ss) -> (ps `O` rs) ~> (qs `O` ss)
80-
o = o
81-
-- AdjNil `o` f = f \\ f
82-
-- f `o` AdjNil = f -- withUnital @(UN AK ps) (withUnital @(UN AK qs) f) \\ f
83-
-- AdjR f `o` g = AdjR (f `o` g)
84-
-- AdjL f `o` g = AdjL (f `o` g)
85-
-- AdjCup f `o` g = AdjCup (f `o` g)
86-
-- AdjCap f `o` g = AdjCap (f `o` g)
87-
leftUnitor AdjNil = AdjNil
88-
leftUnitor (AdjR f) = AdjR f
101+
AdjNil `o` f = f \\ f
102+
f `o` AdjNil = f \\ f
103+
f `o` AdjR g = AdjR (f `o` g)
104+
f `o` AdjL g = AdjL (f `o` g)
105+
f `o` AdjCup g = AdjCup (f `o` g)
106+
f `o` AdjCap g = AdjCap (f `o` g)
107+
leftUnitor = id
108+
leftUnitorInv = id
109+
rightUnitor = id
110+
rightUnitorInv = id
111+
associator @p @q @r = obj @p `o` obj @q `o` obj @r
112+
associatorInv @p @q @r = obj @p `o` obj @q `o` obj @r
89113

90114
type family RepLR (n :: Nat) :: Path ABK A A where
91115
RepLR Z = Nil
@@ -141,51 +165,65 @@ toSimplexOp (AdjR f) = go f id
141165
go (AdjL g) xny = xny (X (Y (toSimplexOp g)))
142166
go (AdjCap g) xny = go g (xny . X)
143167

144-
instance Monad (AK (L ::: R ::: Nil)) where
145-
eta = AdjCap AdjNil
146-
mu = AdjL (AdjCup (AdjR AdjNil))
168+
instance Adjunction_ (AK Nil) (AK Nil) where
169+
adj = Bi.Adj AdjNil AdjNil
147170

148-
-- instance Comonad (R ::: L ::: Nil) where
149-
-- epsilon = AdjCup AdjNil
150-
-- delta = AdjR (AdjCap (AdjL AdjNil))
171+
instance Adjunction_ (AK (L ::: Nil)) (AK (R ::: Nil)) where
172+
adj = Bi.Adj{adjUnit = AdjCap AdjNil, adjCounit = AdjCup AdjNil}
151173

152-
type ARRK :: CAT AB
153-
type data ARRK a b where
154-
IDA :: ARRK A A
155-
A2B :: ARRK A B
156-
IDB :: ARRK B B
174+
instance Adjunction_ (AK (L ::: R ::: L ::: Nil)) (AK (R ::: L ::: R ::: Nil)) where
175+
adj = Bi.Adj{adjUnit = AdjCap (AdjCap (AdjCap AdjNil)), adjCounit = AdjCup (AdjCup (AdjCup AdjNil))}
157176

158-
type Arr :: CAT (ARRK i j)
159-
data Arr a b where
160-
ArrId :: (Ob a) => Arr a a
177+
instance Monad (AK (L ::: R ::: Nil)) where
178+
eta = AdjCap AdjNil
179+
mu = AdjL (AdjCup (AdjR AdjNil))
161180

162-
instance Profunctor (Arr :: CAT (ARRK i j)) where
163-
dimap = dimapDefault
164-
r \\ ArrId = r
165-
instance Promonad (Arr :: CAT (ARRK i j)) where
166-
id = ArrId
167-
ArrId . ArrId = ArrId
168-
instance CategoryOf (ARRK i j) where
169-
type (~>) = Arr
170-
type Ob a = ()
171-
172-
instance Bicategory ARRK where
173-
type Ob0 ARRK a = ()
174-
r \\\ ArrId = r
175-
ArrId `o` ArrId = _
176-
177-
type family Arr2Adj (ps :: Path ARRK a b) :: Path ADJK a b
178-
type instance Arr2Adj (IDA ::: ps) = Arr2Adj ps
179-
type instance Arr2Adj (IDB ::: ps) = Arr2Adj ps
180-
type instance Arr2Adj (A2B ::: ps) = AK (L ::: Nil) ::: Arr2Adj ps
181-
182-
-- type AdjSq :: DOUBLE ADJK ARRK
183-
-- data AdjSq ps qs fs gs where
184-
-- AdjSq :: ps +++ Arr2Adj gs ~> Arr2Adj fs +++ qs -> AdjSq ps qs fs gs
185-
-- instance Double ADJK ARRK where
186-
-- type Sq ADJK ARRK = AdjSq
187-
-- object = AdjSq id
188-
-- hArr f = AdjSq f
189-
-- AdjSq f ||| AdjSq g = AdjSq (f `o` g)
190-
-- vArr ArrId = AdjSq id
191-
-- AdjSq f === AdjSq g = AdjSq (f `o` g)
181+
instance Comonad (AK (R ::: L ::: Nil)) where
182+
epsilon = AdjCup AdjNil
183+
delta = AdjR (AdjCap (AdjL AdjNil))
184+
185+
type SNilOrL :: Path ABK i j -> Type
186+
data SNilOrL p where
187+
SNilL :: SNilOrL Nil
188+
SLL :: SNilOrL (L ::: Nil)
189+
type SNilOrR :: Path ABK i j -> Type
190+
data SNilOrR p where
191+
SNilR :: SNilOrR Nil
192+
SRR :: SNilOrR (R ::: Nil)
193+
194+
type IsTight :: forall {i} {j}. Path ABK i j -> Constraint
195+
class (IsLRPath ps, IsCotight (CotightAdj ps), Adjunction_ (AK ps) (AK (CotightAdj ps))) => IsTight (ps :: Path ABK i j) where
196+
type CotightAdj (ps :: Path ABK i j) :: Path ABK j i
197+
isNilOrL :: SNilOrL ps
198+
instance IsTight Nil where
199+
type CotightAdj Nil = Nil
200+
isNilOrL = SNilL
201+
instance IsTight (L ::: Nil) where
202+
type CotightAdj (L ::: Nil) = R ::: Nil
203+
isNilOrL = SLL
204+
type IsCotight :: forall {i} {j}. Path ABK i j -> Constraint
205+
class (IsLRPath ps, IsTight (TightAdj ps), Adjunction_ (AK (TightAdj ps)) (AK ps)) => IsCotight (ps :: Path ABK i j) where
206+
type TightAdj (ps :: Path ABK i j) :: Path ABK j i
207+
isNilOrR :: SNilOrR ps
208+
instance IsCotight Nil where
209+
type TightAdj Nil = Nil
210+
isNilOrR = SNilR
211+
instance IsCotight (R ::: Nil) where
212+
type TightAdj (R ::: Nil) = L ::: Nil
213+
isNilOrR = SRR
214+
215+
type instance IsOb Tight (AK ps) = IsTight ps
216+
type instance IsOb Cotight (AK ps) = IsCotight ps
217+
type instance TightAdjoint (AK ps) = AK (TightAdj ps)
218+
type instance CotightAdjoint (AK ps) = AK (CotightAdj ps)
219+
instance WithObO2 Tight ADJK where
220+
withObO2 @(AK ps) @(AK qs) r = case (isNilOrL @ps, isNilOrL @qs) of
221+
(SNilL, _) -> r
222+
(SLL, SNilL) -> r
223+
instance WithObO2 Cotight ADJK where
224+
withObO2 @(AK ps) @(AK qs) r = case (isNilOrR @ps, isNilOrR @qs) of
225+
(SNilR, _) -> r
226+
(SRR, SNilR) -> r
227+
instance Equipment ADJK where
228+
withTightAdjoint r = r
229+
withCotightAdjoint r = r

src/Proarrow/Category/Instance/Simplex.hs

Lines changed: 11 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ import Proarrow.Functor (FunctorForRep (..))
1212
import Proarrow.Monoid (Monoid (..))
1313
import Proarrow.Object.Initial (HasInitialObject (..))
1414
import Proarrow.Object.Terminal (HasTerminalObject (..))
15-
import Proarrow.Profunctor.Representable (Representable (..), dimapRep)
1615

1716
type data Nat = Z | S Nat
1817
data SNat :: Nat -> Type where
@@ -117,21 +116,14 @@ instance Monoid (S Z) where
117116
mempty = Y ZZ
118117
mappend = X (X (Y ZZ))
119118

120-
type Replicate :: k -> Nat +-> k
121-
data Replicate m a b where
122-
Replicate :: (Ob b) => a ~> (Replicate m % b) -> Replicate m a b
123-
instance (Monoid m) => Profunctor (Replicate m) where
124-
dimap = dimapRep
125-
r \\ Replicate f = r \\ f
126-
instance (Monoid m) => Representable (Replicate m) where
127-
type Replicate m % Z = Unit
128-
type Replicate m % S b = m ** (Replicate m % b)
129-
index (Replicate f) = f
130-
tabulate = Replicate
131-
repMap ZZ = par0
132-
repMap (Y f) = let g = repMap @(Replicate m) f in (mempty @m `par` g) . leftUnitorInv \\ g
133-
repMap (X (Y f)) = obj @m `par` repMap @(Replicate m) f
134-
repMap (X (X @x f)) =
135-
let g = repMap @(Replicate m) (X f)
136-
b = repMap @(Replicate m) (src f)
137-
in g . (mappend @m `par` b) . associatorInv @_ @m @m @(Replicate m % x) \\ b
119+
data family Replicate :: k -> Nat +-> k
120+
instance (Monoid m) => FunctorForRep (Replicate m) where
121+
type Replicate m @ Z = Unit
122+
type Replicate m @ S b = m ** (Replicate m @ b)
123+
fmap ZZ = par0
124+
fmap (Y f) = let g = fmap @(Replicate m) f in (mempty @m `par` g) . leftUnitorInv \\ g
125+
fmap (X (Y f)) = obj @m `par` fmap @(Replicate m) f
126+
fmap (X (X @x f)) =
127+
let g = fmap @(Replicate m) (X f)
128+
b = fmap @(Replicate m) (src f)
129+
in g . (mappend @m `par` b) . associatorInv @_ @m @m @(Replicate m @ x) \\ b

0 commit comments

Comments
 (0)