-
Notifications
You must be signed in to change notification settings - Fork 1
/
Connection.v
351 lines (288 loc) · 9.48 KB
/
Connection.v
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
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
Require Import TErrorMonad Showable Decidable HoTT PartialOrder.
Set Universe Polymorphism.
(** ** Monotone type-theoretic Galois connection *)
(* =IsPartialEquiv= *)
Class IsConnection {A B} `{IsPartialOrder_pp A} `{IsPartialOrder_pp B}
(f: A --> B) := {
c_inv: B --> A;
c_sect: id ≼ c_inv ° f ;
c_retr: f ° c_inv ≼ id;
}.
(* =end= *)
Arguments c_inv {_ _ _ _} _ {_}.
Arguments c_sect {_ _ _ _} _ {_} _.
Arguments c_retr {_ _ _ _} _ {_} _.
(* =CanonicalPartialEquiv= *)
Class Connection A B `{IsPartialOrder_pp A} `{IsPartialOrder_pp B} := {
c_fun: A --> B ;
c_isconn:> IsConnection c_fun
}.
Notation "A ≲ B" := (Connection A B)
(* =end= *)
(at level 20).
(* =IsConnectionIsHProp= *)
Instance IsConnectionIsHProp A B
`{IsPartialOrder_pp A}`{IsPartialOrder_pp B}(f: A --> B):
IsHProp (IsConnection f).
(* =end= *)
Proof.
intros g g'. destruct g, g'.
assert (c_inv0 = c_inv1).
{
destruct c_inv0, c_inv1.
assert (f_ord = f_ord0).
{
apply rel_antisym.
- intro x. eapply rel_trans. apply c_sect1. simpl. apply mon0. apply c_retr0.
- intro x. eapply rel_trans. apply c_sect0. simpl. apply mon. apply c_retr1.
}
subst.
assert (mon = mon0) by apply is_hprop.
assert (p_mon = p_mon0) by apply is_hprop.
subst. reflexivity.
}
subst.
assert (c_sect0 = c_sect1) by apply is_hprop.
assert (c_retr0 = c_retr1) by apply is_hprop.
subst. reflexivity.
Defined.
(** ** Monotone type-theoretic partial Galois connection *)
(**
A monotone function [f: A → B] defines a partial coercion
[PartialConnection] if some of the [b: B] maps back to the same [a: A]
they come from. Think of [A] as being a refinement of [B] and [f] as
dropping the refinement: not all [B]'s are valid [A]'s but the one
which are can easily be mapped back to [A].
*)
(* =IsPartialEquivK= *)
Class IsConnectionK {A B: HSet} (f: A ⇀ B) := {
pc_inv: B ⇀ A;
pc_sect: creturn ≼ pc_inv °° f;
pc_retr: f °° pc_inv ≼ creturn ;
}.
(* =end= *)
Arguments pc_inv {_ _} _ {_}.
Arguments pc_sect {_ _} _ {_} _.
Arguments pc_retr {_ _} _ {_} _.
(* =PartialEquivK= *)
Record ConnectionK (A B: HSet) := {
pc_fun: A ⇀ B ;
pc_isconn: IsConnectionK pc_fun
}.
Notation "A ≲K B" := (ConnectionK A B)
(* =end= *)
(at level 20).
Arguments pc_fun {_ _} _ _.
Arguments pc_isconn {_ _ _}.
(* =IsPartialEquiv= *)
Class IsAnticonnection {A B}
`{IsPartialOrder_pp A} `{IsPartialOrder_pp B} (f: A --> B) := {
ac_inv: B --> A;
ac_sect: ac_inv ° f ≼ id ;
ac_retr: f ° ac_inv ≼ id;
}.
(* =end= *)
Arguments ac_inv {_ _ _ _} _ {_}.
Arguments ac_sect {_ _ _ _} _ {_} _.
Arguments ac_retr {_ _ _ _} _ {_} _.
(* =CanonicalPartialEquiv= *)
Class Anticonnection A B `{IsPartialOrder_pp A} `{IsPartialOrder_pp B} :=
{ ac_fun: A --> B ;
ac_isconn:> IsAnticonnection ac_fun
}.
Notation "A ≈ B" := (Anticonnection A B)
(* =end= *)
(at level 20).
(* =IsAPartialEquivK= *)
Class IsAnticonnectionK {A B: HSet} (f: A ⇀ B) :=
{ apc_inv: B ⇀ A;
apc_sect: apc_inv °° f ≼ creturn;
apc_retr: f °° apc_inv ≼ creturn ;
}.
(* =end= *)
Arguments apc_inv {_ _} _ {_}.
Arguments apc_sect {_ _} _ {_} _.
Arguments apc_retr {_ _} _ {_} _.
(* =APartialEquivK= *)
Record AnticonnectionK (A B: HSet) := {
apc_fun: A ⇀ B ;
apc_isconn: IsAnticonnectionK apc_fun
}.
Notation "A ≈K B" := (AnticonnectionK A B)
(* =end= *)
(at level 20).
Arguments apc_fun {_ _} _ _.
Arguments apc_isconn {_ _ _}.
(**
Sanity Check: lifting an equivalance yields
a partial equivalence in the Kleisli category
*)
Ltac etransitivity := eapply concat.
(* =EquivToPartialEquivK= *)
Definition EquivToConnectionK (A B: HSet) (f: A -> B) :
IsEquiv f -> IsConnectionK (clift f).
(* =end= *)
Proof.
intro H.
simple refine (Build_IsConnectionK _ _ _ (clift (e_inv f)) _ _).
- intro x. exact (HoTT.ap Some (e_sect f x))^.
- intro x. exact (HoTT.ap Some (e_retr f x)).
Defined.
(**
we have a subset equivalence between [B] and [{C,P}]
if the type family [B] is isomorphic with a (computationally-relevant)
type [C] and a (computationally-irrelevant) relation [P], ie.
<<
[B a ≅ { c : C & P a c }]
>>
Remarks:
- [C] is non-dependent in [a : A], by definition.
- Usually, [A] plays the role of the indices, [B] is an inductive
family indexed over [A]. [C] are the "raw", computational terms
while [P] is a validity predicate.
*)
(* conversion between coercion and anticoercion *)
(* =Coercion_AntiCoercion= *)
Definition Connection_Anticonnection (A B: HSet) : A ≲K B -> A ≈K B.
(* =end= *)
Proof.
intro H; unshelve econstructor.
- exact (pc_fun H).
- unshelve econstructor.
+ refine (pc_inv (pc_fun H)). exact pc_isconn.
+ intro a. pose (foo := @pc_sect _ _ (pc_fun H) pc_isconn a). cbn in *.
clearbody foo. revert foo.
destruct ((pc_inv (pc_fun H)) °° (pc_fun H) a). symmetry. exact foo.
intro; exact I.
+ exact (pc_retr (pc_fun H)).
Defined.
Instance Connection_IsAnticonnectionK (A B: HSet)
(H : A ≈K B) : IsAnticonnectionK (apc_fun H) := apc_isconn.
(* =AntiCoercion_Coercion= *)
Definition Anticonnection_Connection (A B: HSet) (H : A ≈K B):
creturn ≼ (apc_inv (apc_fun H)) °° (apc_fun H) -> A ≲K B.
(* =end= *)
Proof.
intro H'; unshelve econstructor.
- exact (apc_fun H).
- unshelve econstructor.
+ exact (apc_inv (apc_fun H)).
+ exact H'.
+ exact (apc_retr (apc_fun H)).
Defined.
Instance Connection_Anticonnection_K (A B: Type)
`{IsPartialOrder_pp A} `{IsPartialOrder_pp B}
(H : A ≈ B) (H' : id ≼ (ac_inv ac_fun) ° (@ac_fun _ _ _ _ H)) :
A ≲ B.
Proof.
destruct H1; unshelve econstructor; try solve [auto].
destruct ac_isconn; unshelve econstructor; try solve [auto].
Defined.
Instance Anticonnection_Connection_K (A B: Type)
`{IsPartialOrder_pp A} `{IsPartialOrder_pp B}
(H : A ≲ B) (H' : (c_inv c_fun) ° (@c_fun _ _ _ _ H) ≼ id ) :
A ≈ B.
Proof.
destruct H1; unshelve econstructor; try solve [auto].
destruct c_isconn; unshelve econstructor; try solve [auto].
Defined.
Notation "{ x : A & P }" := (sigT_HSet A (fun x => P)) : type_scope.
(* =to_subset= *)
Definition to_subset {C: HSet} {P} `{forall c, Checkable (P c)}: C ⇀ ({c:C & P c})
:= fun c =>
match dec (check (P c)) with
| inl p => Some (c; convert p)
| inr _ => Fail (_with ("subset conversion"))
end.
(* =end= *)
Instance Checkable_IsAnticonnectionK (C: HSet) P `{forall c, Checkable (P c)}:
IsAnticonnectionK (clift π1: {c: C & P c} ⇀ C).
Proof.
unshelve refine ({| apc_inv := to_subset |}).
- intro c. unfold kleisliComp.
unfold to_subset in *. simpl.
destruct dec; simpl;
[
| eauto ].
simpl. unfold creturn. apply HoTT.ap. refine (path_sigma_uncurried _ _ _ _).
exists eq_refl. apply is_hprop.
- intro c. unfold kleisliComp.
unfold to_subset in *.
destruct (dec); simpl;
[ | eauto].
reflexivity.
Defined.
(* =Checkable_AntiPartialEquivK= *)
Definition Checkable_AnticonnectionK (C:HSet) P `{forall c, Checkable (P c)}:
{c:C & P c} ≈K C :=
{| apc_fun := clift π1 : {c:C & P c} ⇀ C |}.
(* =end= *)
(* =Decidable_ConnectionK= *)
Definition Decidable_ConnectionK (C:HSet) P `{forall c, Decidable (P c)}:
{c:C & P c} ≲K C.
(* =end= *)
Proof.
unshelve eapply Anticonnection_Connection.
- apply Checkable_AnticonnectionK; typeclasses eauto.
- intro c. unfold kleisliComp.
cbn. unfold to_subset in *. simpl.
destruct dec; simpl;
[
| eauto ].
simpl. unfold creturn. apply HoTT.ap. refine (path_sigma_uncurried _ _ _ _).
exists eq_refl. apply is_hprop.
destruct (n c.2).
Defined.
(** ** Injective functions *)
(** This class is particularly useful to reflect constructors and take
advantage of their injectivity *)
(* =IsInjective= *)
Class IsInjective {A B: HSet} (f: A -> B) := {
i_inv: B ⇀ A;
i_sect: i_inv ° f == creturn ;
i_retr: (clift f) °° i_inv ≼ creturn
}.
(* =end= *)
Notation "f ^?-1" := (@i_inv _ _ f _) (at level 3, format "f '^?-1'").
Instance Injective_comp {A B C : HSet}
(f : A -> B) (g : B -> C)
`{IsInjective A B f} `{IsInjective B C g}:
IsInjective (g ° f)
:= {| i_inv := (f^?-1 °° (g^?-1)) |}.
Proof.
- unfold compose, kleisliComp. intro a.
pose (i_sect (f:=g) (f a)). unfold compose in e.
rewrite e.
exact (i_sect (f:=f) a).
- intros c. cbn.
pose (r := i_retr (f:=g) c).
unfold compose, kleisliComp in *.
cbn in *.
generalize dependent r. destruct (g^?-1 c) as [b|]; cbn in *; [| eauto].
pose (r := i_retr (f:=f) b).
unfold compose, kleisliComp in *.
generalize dependent r. destruct (f^?-1 b); cbn in *; [| eauto].
unfold clift. intros.
apply HoTT.ap.
inversion r0. inversion r. auto.
Defined.
(** *** Instances of IsInjective *)
(** The identity is an injection : *)
Definition IsInjective_id {A:HSet}: IsInjective (fun x:A => x).
Proof.
simple refine (Build_IsInjective _ _ _ (@Some A _) _ _).
intro a. reflexivity.
intros a. reflexivity.
Defined.
(** The predecessor over natural numbers is a partial equivalence: *)
Definition S_inv (n: nat) :=
match n with
| O => Fail (_with "invalid index")
| S n' => Some n'
end.
Instance IsInjective_S: @IsInjective Hnat Hnat S :=
{| i_inv := S_inv : Hnat ⇀ Hnat |}.
Proof.
intro a. reflexivity.
intros n. destruct n; cbn in *; eauto.
Defined.