aboutsummaryrefslogtreecommitdiff
path: root/Category/Instance/Cospans.agda
blob: ae6d359880cb496349985f1ad0cd25b1c5758417 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
{-# OPTIONS --without-K --safe #-}

open import Categories.Category using (Category)
open import Category.Cocomplete.Finitely.Bundle using (FinitelyCocompleteCategory)
open import Function using (flip)
open import Level using (_⊔_)

module Category.Instance.Cospans {o  e} (𝒞 : FinitelyCocompleteCategory o  e) where

module 𝒞 = FinitelyCocompleteCategory 𝒞
open 𝒞 using (U; pushout)
open Category U hiding (_≈_)

open import Categories.Diagram.Pushout U using (Pushout)
open import Categories.Diagram.Pushout.Properties U using (pushout-resp-≈; up-to-iso)
open import Relation.Binary using (IsEquivalence)
open import Categories.Morphism U using (_≅_; module ≅)
open import Categories.Morphism.Reasoning U
  using
    ( switch-fromtoˡ
    ; glueTrianglesˡ
    ; pullˡ ; pullʳ
    ; cancelˡ
    )

open import Category.Diagram.Pushout U 
  using
    ( glue-i₁ ; glue-i₂
    ; pushout-f-id ; pushout-id-g
    ; extend-i₁-iso ; extend-i₂-iso
    )

record Cospan (A B : Obj) : Set (o  ) where

  constructor cospan

  field
    {N} : Obj
    f₁  : A  N
    f₂  : B  N

private
  variable
    A B C D : Obj

compose : Cospan A B  Cospan B C  Cospan A C
compose (cospan f g) (cospan h i) = cospan (i₁  f) (i₂  i)
  where
    open pushout g h

identity : Cospan A A
identity = cospan id id

compose-3 : Cospan A B  Cospan B C  Cospan C D  Cospan A D
compose-3 (cospan f₁ f₂) (cospan g₁ g₂) (cospan h₁ h₂) = cospan (P₃.i₁  P₁.i₁  f₁) (P₃.i₂  P₂.i₂  h₂)
  where
    module P₁ = pushout f₂ g₁
    module P₂ = pushout g₂ h₁
    module P₃ = pushout P₁.i₂ P₂.i₁

record _≈_ (C D : Cospan A B) : Set (  e) where

  module C = Cospan C
  module D = Cospan D

  field
    ≅N : C.N  D.N

  open _≅_ ≅N public
  module N = _≅_ ≅N

  field
    from∘f₁≈f₁ : from  C.f₁ 𝒞.≈ D.f₁
    from∘f₂≈f₂ : from  C.f₂ 𝒞.≈ D.f₂

private
  variable
    f g h : Cospan A B

≈-refl : f  f
≈-refl {f = cospan f₁ f₂} = record
    { ≅N = ≅.refl
    ; from∘f₁≈f₁ = from∘f₁≈f₁
    ; from∘f₂≈f₂ = from∘f₂≈f₂
    }
  where abstract
    from∘f₁≈f₁ : id  f₁ 𝒞.≈ f₁
    from∘f₁≈f₁ = identityˡ
    from∘f₂≈f₂ : id  f₂ 𝒞.≈ f₂
    from∘f₂≈f₂ = identityˡ

≈-sym : f  g  g  f
≈-sym f≈g = record
    { ≅N = ≅.sym ≅N
    ; from∘f₁≈f₁ = a
    ; from∘f₂≈f₂ = b
    }
  where abstract
    open _≈_ f≈g
    a : ≅N.to  D.f₁ 𝒞.≈ C.f₁
    a = Equiv.sym (switch-fromtoˡ ≅N from∘f₁≈f₁)
    b : ≅N.to  D.f₂ 𝒞.≈ C.f₂
    b = Equiv.sym (switch-fromtoˡ ≅N from∘f₂≈f₂)

≈-trans : f  g  g  h  f  h
≈-trans f≈g g≈h = record
    { ≅N = ≅.trans f≈g.≅N g≈h.≅N
    ; from∘f₁≈f₁ = a
    ; from∘f₂≈f₂ = b
    }
  where abstract
    module f≈g = _≈_ f≈g
    module g≈h = _≈_ g≈h
    a : (g≈h.≅N.from  f≈g.≅N.from)  f≈g.C.f₁ 𝒞.≈ g≈h.D.f₁
    a = glueTrianglesˡ g≈h.from∘f₁≈f₁ f≈g.from∘f₁≈f₁
    b : (g≈h.≅N.from  f≈g.≅N.from)  f≈g.C.f₂ 𝒞.≈ g≈h.D.f₂
    b = glueTrianglesˡ g≈h.from∘f₂≈f₂ f≈g.from∘f₂≈f₂

≈-equiv : {A B : 𝒞.Obj}  IsEquivalence (_≈_ {A} {B})
≈-equiv = record
    { refl = ≈-refl
    ; sym = ≈-sym
    ; trans = ≈-trans
    }

compose-idˡ : compose f identity  f
compose-idˡ {f = cospan {N} f₁ f₂} = record
    { ≅N = ≅P
    ; from∘f₁≈f₁ = from∘f₁≈f₁
    ; from∘f₂≈f₂ = from∘f₂≈f₂
    }
  where
    open HomReasoning
    P P′ : Pushout f₂ id
    P = pushout f₂ id
    P′ = pushout-f-id {f = f₂}
    module P = Pushout P
    ≅P : P.Q  N
    ≅P = up-to-iso P P′
    module P = _≅_ ≅P
    abstract
      from∘f₁≈f₁ : ≅P.from  P.i₁  f₁ 𝒞.≈ f₁
      from∘f₁≈f₁ = begin
          ≅P.from  P.i₁  f₁ ≈⟨ cancelˡ P.universal∘i₁≈h₁           f₁                        from∘f₂≈f₂ : ≅P.from  P.i₂  id 𝒞.≈ f₂
      from∘f₂≈f₂ = begin
          ≅P.from  P.i₂  id ≈⟨ refl⟩∘⟨ identityʳ           ≅P.from  P.i₂      ≈⟨ P.universal∘i₂≈h₂           f₂                  compose-idʳ : compose identity f  f
compose-idʳ {f = cospan {N} f₁ f₂} = record
    { ≅N = ≅P
    ; from∘f₁≈f₁ = from∘f₁≈f₁
    ; from∘f₂≈f₂ = from∘f₂≈f₂
    }
  where
    open HomReasoning
    P P′ : Pushout id f₁
    P = pushout id f₁
    module P = Pushout P
    P′ = pushout-id-g {g = f₁}
    ≅P : P.Q  N
    ≅P = up-to-iso P P′
    module P = _≅_ ≅P
    abstract
      from∘f₁≈f₁ : ≅P.from  P.i₁  id 𝒞.≈ f₁
      from∘f₁≈f₁ = begin
          ≅P.from  P.i₁  id ≈⟨ refl⟩∘⟨ identityʳ           ≅P.from  P.i₁      ≈⟨ P.universal∘i₁≈h₁           f₁                        from∘f₂≈f₂ : ≅P.from  P.i₂  f₂ 𝒞.≈ f₂
      from∘f₂≈f₂ = begin
          ≅P.from  P.i₂  f₂ ≈⟨ cancelˡ P.universal∘i₂≈h₂           f₂                  compose-id² : compose identity identity  identity {A}
compose-id² = compose-idˡ

compose-3-right : compose f (compose g h)  compose-3 f g h
compose-3-right {f = f} {g = g} {h = h} = record
    { ≅N = ≅N
    ; from∘f₁≈f₁ = from∘f₁≈f₁
    ; from∘f₂≈f₂ = from∘f₂≈f₂
    }
  where
    open HomReasoning
    module C₁ = Cospan f
    module C₂ = Cospan g
    module C₃ = Cospan h
    P₁ : Pushout C₁.f₂ C₂.f₁
    P₁ = pushout C₁.f₂ C₂.f₁
    P₂ : Pushout C₂.f₂ C₃.f₁
    P₂ = pushout C₂.f₂ C₃.f₁
    module P₁ = Pushout P₁
    module P₂ = Pushout P₂
    P₃ : Pushout P₁.i₂ P₂.i₁
    P₃ = pushout P₁.i₂ P₂.i₁
    module P₃ = Pushout P₃
    P₄ P₄′ : Pushout C₁.f₂ (P₂.i₁  C₂.f₁)
    P₄ = glue-i₂ P₁ P₃
    P₄′ = pushout C₁.f₂ (P₂.i₁  C₂.f₁)
    module P₄ = Pushout P₄
    module P₄ = Pushout P₄′
    ≅N : P₄′.Q  P₄.Q
    ≅N = up-to-iso P₄′ P₄
    module N = _≅_ ≅N
    abstract
      from∘f₁≈f₁ : ≅N.from  P₄′.i₁  C₁.f₁ 𝒞.≈ P₃.i₁  P₁.i₁  C₁.f₁
      from∘f₁≈f₁ = sym-assoc  P₄′.universal∘i₁≈h₁ ⟩∘⟨refl  assoc
      from∘f₂≈f₂ : ≅N.from  P₄′.i₂  P₂.i₂  C₃.f₂ 𝒞.≈ P₄.i₂  P₂.i₂  C₃.f₂
      from∘f₂≈f₂ = sym-assoc  P₄′.universal∘i₂≈h₂ ⟩∘⟨refl

compose-3-left : compose (compose f g) h  compose-3 f g h
compose-3-left {f = f} {g = g} {h = h} = record
    { ≅N = up-to-iso P₄′ P₄
    ; from∘f₁≈f₁ = from∘f₁≈f₁
    ; from∘f₂≈f₂ = from∘f₂≈f₂
    }
  where
    open HomReasoning
    module C₁ = Cospan f
    module C₂ = Cospan g
    module C₃ = Cospan h
    P₁ : Pushout C₁.f₂ C₂.f₁
    P₁ = pushout C₁.f₂ C₂.f₁
    P₂ : Pushout C₂.f₂ C₃.f₁
    P₂ = pushout C₂.f₂ C₃.f₁
    module P₁ = Pushout P₁
    module P₂ = Pushout P₂
    P₃ : Pushout P₁.i₂ P₂.i₁
    P₃ = pushout P₁.i₂ P₂.i₁
    module P₃ = Pushout P₃
    P₄ P₄′ : Pushout (P₁.i₂  C₂.f₂) C₃.f₁
    P₄ = glue-i₁ P₂ P₃
    P₄′ = pushout (P₁.i₂  C₂.f₂) C₃.f₁
    module P₄ = Pushout P₄
    module P₄ = Pushout P₄′
    ≅N : P₄′.Q  P₄.Q
    ≅N = up-to-iso P₄′ P₄
    module N = _≅_ ≅N
    abstract
      from∘f₁≈f₁ : ≅N.from  P₄′.i₁  P₁.i₁  C₁.f₁ 𝒞.≈ P₄.i₁  P₁.i₁  C₁.f₁
      from∘f₁≈f₁ = sym-assoc  P₄′.universal∘i₁≈h₁ ⟩∘⟨refl
      from∘f₂≈f₂ : ≅N.from  P₄′.i₂  C₃.f₂ 𝒞.≈ P₃.i₂  P₂.i₂  C₃.f₂
      from∘f₂≈f₂ = sym-assoc  P₄′.universal∘i₂≈h₂ ⟩∘⟨refl  assoc

compose-assoc
    : {c₁ : Cospan A B}
      {c₂ : Cospan B C}
      {c₃ : Cospan C D}
     compose c₁ (compose c₂ c₃)  (compose (compose c₁ c₂) c₃)
compose-assoc = ≈-trans compose-3-right (≈-sym compose-3-left)

compose-sym-assoc
    : {c₁ : Cospan A B}
      {c₂ : Cospan B C}
      {c₃ : Cospan C D}
     compose (compose c₁ c₂) c₃  compose c₁ (compose c₂ c₃)
compose-sym-assoc = ≈-trans compose-3-left (≈-sym compose-3-right)

compose-equiv
    : {c₂ c₂′ : Cospan B C}
      {c₁ c₁′ : Cospan A B}
     c₂  c₂′
     c₁  c₁′
     compose c₁ c₂  compose c₁′ c₂′
compose-equiv {_} {_} {_} {c₂} {c₂′} {c₁} {c₁′} ≈C₂ ≈C₁ = record
    { ≅N = ≅P
    ; from∘f₁≈f₁ = from∘f₁≈f₁
    ; from∘f₂≈f₂ = from∘f₂≈f₂
    }
  where
    module C₁ = Cospan c₁
    module C₁ = Cospan c₁′
    module C₂ = Cospan c₂
    module C₂ = Cospan c₂′
    P : Pushout C₁.f₂ C₂.f₁
    P = pushout C₁.f₂ C₂.f₁
    P′ : Pushout C₁′.f₂ C₂′.f₁
    P′ = pushout C₁′.f₂ C₂′.f₁
    module C₁ = _≈_ ≈C₁
    module C₂ = _≈_ ≈C₂
    P′′ : Pushout (≈C₁.to  C₁′.f₂) (≈C₂.to  C₂′.f₁)
    P′′ = extend-i₂-iso (extend-i₁-iso P′ (≅.sym ≈C₁.≅N)) (≅.sym ≈C₂.≅N)
    P″ : Pushout C₁.f₂ C₂.f₁
    P″ =
        pushout-resp-≈
            P′′
            (Equiv.sym (switch-fromtoˡ ≈C₁.≅N ≈C₁.from∘f₂≈f₂))
            (Equiv.sym (switch-fromtoˡ ≈C₂.≅N ≈C₂.from∘f₁≈f₁))
    module P = Pushout P
    module P = Pushout P′
    ≅P : P.Q  P′.Q
    ≅P = up-to-iso P P″
    module P = _≅_ ≅P
    open HomReasoning
    abstract
      from∘f₁≈f₁ : ≅P.from  P.i₁  C₁.f₁ 𝒞.≈ P′.i₁  C₁′.f₁
      from∘f₁≈f₁ = begin
          ≅P.from  P.i₁  C₁.f₁      ≈⟨ pullˡ P.universal∘i₁≈h₁           (P′.i₁  ≈C₁.from)  C₁.f₁  ≈⟨ pullʳ ≈C₁.from∘f₁≈f₁           P′.i₁  C₁′.f₁                    from∘f₂≈f₂ : ≅P.from  P.i₂  C₂.f₂ 𝒞.≈ P′.i₂  C₂′.f₂
      from∘f₂≈f₂ = begin
          ≅P.from  P.i₂  C₂.f₂      ≈⟨ pullˡ P.universal∘i₂≈h₂           (P′.i₂  ≈C₂.from)  C₂.f₂  ≈⟨ pullʳ ≈C₂.from∘f₂≈f₂           P′.i₂  C₂′.f₂              Cospans : Category o (o  ) (  e)
Cospans = record
    { Obj = Obj
    ; _⇒_ = Cospan
    ; _≈_ = _≈_
    ; id = identity
    ; _∘_ = flip compose
    ; assoc = compose-assoc
    ; sym-assoc = compose-sym-assoc
    ; identityˡ = compose-idˡ
    ; identityʳ = compose-idʳ
    ; identity² = compose-id²
    ; equiv = ≈-equiv
    ; ∘-resp-≈ = compose-equiv
    }