-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTypeClasses.v
482 lines (436 loc) · 13.1 KB
/
TypeClasses.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
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
Require Import Ltacs.
Require Import String.
(* Set Typeclasses Debug. *)
Class Dec (P : Prop) := { dec : P + ~ P }.
Theorem dec_impl_excluded_middle: forall P,
Dec P ->
(P \/ ~ P).
Proof.
intros.
destruct H; destruct dec0; eauto.
Qed.
Theorem dec_impl_equiv_bool : forall (P : Prop),
Dec P ->
exists f, (P <-> f = true) /\ (~ P <-> f = false).
Proof.
intros.
dest H.
dest dec0; eauto.
- exists true; split; split; eauto; qcon.
- exists false; split; split; eauto; qcon.
Qed.
Lemma dec_le_nat : forall (a b : nat),
Dec (a <= b).
Proof.
induction a; dest b; econstructor; eauto.
- left. ind b; eauto.
- right. qcon.
- specialize IHa with b. dest IHa.
dest dec0.
* left. eapply le_n_S. eauto.
* right. intros HC. eapply le_S_n in HC. cong.
Defined.
#[global]
Instance dec_le_nat_inst (a b : nat) : Dec (a <= b).
eapply dec_le_nat. Defined.
Class DecEq (A : Type) :=
{
decEq : forall x1 x2 : A, { x1 = x2 } + {x1 <> x2}
}.
Lemma nat_eq_dec' : forall n1 n2 : nat,
{n1 = n2} + {n1 <> n2}.
Proof with eauto.
ind n1; destruct n2...
destruct (IHn1 n2)...
Qed.
(** Setting up some baseline Decidable props related to equality
- This should allow for the other classes to be easily built
*)
#[global]
Instance nat_eq_dec (n1 n2 : nat) : Dec (n1 = n2).
destruct (nat_eq_dec' n1 n2); constructor; eauto.
Defined.
#[global]
Instance decEq_nat : DecEq nat.
constructor. ind x1; destruct x2; eauto.
specialize IHx1 with x2.
destruct IHx1; eauto. Defined.
#[global]
Instance decEq_string : DecEq string.
constructor. apply string_dec. Defined.
#[global]
Instance decEq_list (A : Type) `{DA : DecEq A} : DecEq (list A).
constructor.
ind x1; dest x2; eauto; try (right; qcon; fail).
(* Checking for list head equality *)
dest DA. dest (decEq0 a a0);
specialize IHx1 with x2;
dest IHx1; subst; eauto; try (right; qcon; congruence).
Defined.
#[global]
Instance decEq_pair (A B : Type) `{DA : DecEq A} `{DB : DecEq B} : DecEq (A * B).
constructor.
ind x1; dest x2; eauto; try (right; qcon; fail).
dest DA; dest DB; dest (decEq0 a a0); dest (decEq1 b b0);
subst; eauto; try (right; qcon; congruence).
Defined.
Class EqClass (A : Type) `{DE : DecEq A} :=
{
eqb : A -> A -> bool ;
eqb_leibniz : forall x y, eqb x y = true <-> x = y ;
neqb_leibniz : forall x y, eqb x y = false <-> x <> y;
}.
Theorem eqb_refl_all : forall {A : Type} `{H : EqClass A} (a : A),
eqb a a = true.
Proof.
destruct H; intros.
rewrite eqb_leibniz0. auto.
Qed.
Definition gen_deceq_eqb {A : Type} `{DE : DecEq A} (a1 a2 : A) : bool :=
match (decEq a1 a2) with
| left e => true
| right e => false
end.
Theorem gen_deceq_eqb_refl: forall {A : Type} `{DE : DecEq A} (a : A),
gen_deceq_eqb a a = true.
Proof.
intros.
unfold gen_deceq_eqb.
destruct DE. simpl.
destruct (decEq0 a a).
- refl.
- cong.
Qed.
Lemma gen_eqb_impl_eqb_leibniz : forall {A : Type} `{Eq : DecEq A} (x y : A),
gen_deceq_eqb x y = true <-> x = y.
Proof.
unfold gen_deceq_eqb.
intros.
destruct (decEq x y); split; eauto; try qcon.
Defined.
Lemma gen_eqb_impl_neqb_leibniz : forall {A : Type} `{Eq : DecEq A} (x y : A),
gen_deceq_eqb x y = false <-> x <> y.
Proof.
unfold gen_deceq_eqb.
intros.
destruct (decEq x y); split; eauto; try qcon.
Defined.
#[global]
Instance deceq_impl_eqb (A : Type) `{DE : DecEq A} : EqClass A :=
{
eqb := gen_deceq_eqb ;
eqb_leibniz := gen_eqb_impl_eqb_leibniz ;
neqb_leibniz := gen_eqb_impl_neqb_leibniz
}.
Class Partial_Order {A : Type} `{Eq : EqClass A} (R : A -> A -> Prop) :=
{
po_reflexive : forall (a : A), R a a ;
po_antiSym : forall (a b : A), R a b -> R b a -> eqb a b = true ;
po_transitivity : forall (a b c : A),
R a b ->
R b c ->
R a c
}.
Lemma nat_eqb_sn : forall (a b : nat),
eqb a b = true <->
eqb (S a) (S b) = true.
Proof.
ind a; dest b; eauto; split; eauto; intros.
- unfold eqb, deceq_impl_eqb, gen_deceq_eqb, decEq, decEq_nat, nat_rec, nat_rect in *.
destruct (match
(fix F (n : nat) : forall x2 : nat, {n = x2} + {n <> x2} :=
match n as n0 return (forall x2 : nat, {n0 = x2} + {n0 <> x2}) with
| 0 =>
fun x2 : nat =>
match x2 as n0 return ({0 = n0} + {0 <> n0}) with
| 0 => left eq_refl
| S n0 => right (O_S n0)
end
| S n0 =>
fun x2 : nat =>
match x2 as n1 return ({S n0 = n1} + {S n0 <> n1}) with
| 0 => right (not_eq_sym (O_S n0))
| S n1 =>
match F n0 n1 with
| left a => left (f_equal_nat nat S n0 n1 a)
| right b => right (not_eq_S n0 n1 b)
end
end
end) a b
with
| left a0 => left (f_equal_nat nat S a b a0)
| right b0 => right (not_eq_S a b b0)
end); eauto.
- unfold eqb, deceq_impl_eqb, gen_deceq_eqb, decEq, decEq_nat, nat_rec, nat_rect in *.
destruct (match
(fix F (n : nat) : forall x2 : nat, {n = x2} + {n <> x2} :=
match n as n0 return (forall x2 : nat, {n0 = x2} + {n0 <> x2}) with
| 0 =>
fun x2 : nat =>
match x2 as n0 return ({0 = n0} + {0 <> n0}) with
| 0 => left eq_refl
| S n0 => right (O_S n0)
end
| S n0 =>
fun x2 : nat =>
match x2 as n1 return ({S n0 = n1} + {S n0 <> n1}) with
| 0 => right (not_eq_sym (O_S n0))
| S n1 =>
match F n0 n1 with
| left a => left (f_equal_nat nat S n0 n1 a)
| right b => right (not_eq_S n0 n1 b)
end
end
end) a b
with
| left a0 => left (f_equal_nat nat S a b a0)
| right b0 => right (not_eq_S a b b0)
end); eauto.
Qed.
#[global]
Instance partial_order_nats : Partial_Order le.
constructor.
- induction a; eauto.
- induction a; destruct b; intros; eauto.
* inv H0.
* inv H.
* eapply le_S_n in H, H0. assert (eqb a b = true). eauto.
rewrite <- nat_eqb_sn; eauto.
- induction a; dest b; dest c; intros; eauto;
eapply PeanoNat.Nat.le_trans; eauto.
Defined.
Class Total_Order {A : Type} (R : A -> A -> Prop) `{PO : Partial_Order A R} :=
{
dec_t_order : forall (a b : A), R a b + R b a
}.
Theorem total_order_invertible : forall {A : Type} {R : A -> A -> Prop}
`{HT : Total_Order A R} (a b : A),
a <> b ->
R a b <-> ~ R b a.
Proof.
split; intros.
- dest PO. intros HC.
pose proof (po_antiSym0 _ _ H0 HC).
rewrite eqb_leibniz in H1. cong.
- dest (dec_t_order a b); eauto.
cong.
Qed.
Require Import List.
#[global]
Instance dec_in {A : Type} `{Heq : EqClass A} (a : A) (l : list A) : Dec (In a l).
constructor.
induction l; smp; eauto.
destruct IHl.
- (* In a l *)
eauto.
- (* ~ In a l *)
destruct (eqb a0 a) eqn:E.
* (* a0 = a *)
rewrite eqb_leibniz in E. eauto.
* (* a0 <> a *)
rewrite neqb_leibniz in E.
right. qcon.
Defined.
Require Import Lia.
Fixpoint t_order_max_in_list {A : Type} (l : list A)
(R : A -> A -> Prop) `{TO : Total_Order A R} : option A.
destruct l eqn:L'.
- apply None.
- destruct (t_order_max_in_list A l0 R DE Eq0 PO TO) eqn:Rec.
* (* rec = Some a0 *)
destruct (dec_t_order a a0).
** (* R a a0 - so a0 max *)
apply (Some a0).
** (* R a0 a - so a max *)
apply (Some a).
* (* rec = None *)
apply (Some a).
Defined.
Class WeakMaximal {A : Type} (R : A -> A -> Prop) :=
{
weakMaxElemProof : forall (l : list A), l <> nil -> exists (max : A),
(forall (elem : A), In elem l -> R elem max)
}.
Class Maximal {A : Type} (R : A -> A -> Prop) :=
{
maxElemProof : forall (l : list A), l <> nil -> exists (max : A),
((forall (elem : A), In elem l -> R elem max) /\ In max l)
}.
#[global]
Instance weak_maximal_nats : WeakMaximal le.
constructor.
induction l.
- intros. cong.
- intros.
destruct l; smp.
* (* l = nil *)
exists a; intros; eauto.
destruct H0; subst; eauto; try exfalso; eauto.
* (* l = n :: l *)
assert (n :: l <> nil). {
clearAll.
induction l; eauto; try qcon.
}
pose proof (IHl H0) as IHl.
clear H. clear H0.
destruct IHl as [max' H].
assert (n < max' \/ n = max' \/ max' < n). lia.
destruct H0.
** (* n < max' *)
assert (a < max' \/ a = max' \/ max' < a). lia.
destruct H1.
*** (* a < max' => max' is greatest *)
exists max'; intros; eauto.
destruct H2; eauto; subst; eauto; try lia.
*** destruct H1.
**** (* a = max' => a/max' is greatest *)
subst.
exists max'; intros; eauto.
destruct H1; eauto; subst; eauto; try lia.
**** (* max' < a => a is greatest *)
exists a; intros; eauto.
destruct H2; eauto; subst; eauto; try lia;
destruct H2; subst; eauto; try lia.
eapply PeanoNat.Nat.le_trans.
eapply H; eauto. lia.
** (* n = max' \/ max' < n*)
destruct H0.
*** (* n = max' *)
subst.
assert (a < max' \/ a = max' \/ max' < a). lia.
destruct H0.
**** exists max'; intros; eauto.
destruct H1; eauto; subst; eauto; try lia.
**** destruct H0.
***** subst. (* a = max' *)
exists max'; intros; eauto.
destruct H0; eauto.
***** (* max' < a *)
exists a; intros; eauto.
destruct H1; subst; eauto.
destruct H1; subst; eauto; try lia.
eapply PeanoNat.Nat.le_trans.
eapply H; eauto. lia.
*** (* max' < n *)
assert (a < n \/ a = n \/ n < a). lia.
destruct H1; eauto.
**** (* max' < n, a < n -> n greatest *)
exists n; intros; eauto.
destruct H2; subst; eauto; try lia.
destruct H2; subst; eauto; try lia.
eapply PeanoNat.Nat.le_trans.
eapply H; eauto. lia.
**** (* a = n \/ n < a *)
destruct H1; subst; eauto.
***** (* a = n -> n greatest *)
exists n; intros; eauto.
destruct H1; subst; eauto; try lia.
destruct H1; subst; eauto; try lia.
eapply PeanoNat.Nat.le_trans.
eapply H; eauto. lia.
***** (* n < a -> a greatest *)
exists a; intros; eauto.
destruct H2; subst; eauto; try lia.
destruct H2; subst; eauto; try lia.
eapply PeanoNat.Nat.le_trans.
eapply H; eauto. lia.
Defined.
#[global]
Instance maximal_nats : Maximal le.
constructor.
induction l; intros.
- cong.
- destruct l.
* (* only a in list, so a = max *)
exists a; split; smp; eauto.
intros. destruct H0; try lia.
* (* a :: n :: l = list *)
assert (n :: l <> nil). qcon.
pose proof (IHl H0) as IHl.
clear H. clear H0.
destruct IHl as [max' [Hel Hmax]].
smp.
destruct Hmax.
** (* n = max' *)
subst.
assert (a <= max' \/ max' <= a). lia.
destruct H.
*** (* a <= max' *)
exists max'; split; intros; eauto.
destruct H0 as [H0 | [H1 | H2]]; subst; eauto.
*** (* max' <= a *)
exists a; split; intros; eauto.
destruct H0 as [H0 | [H0 | H0]]; subst; eauto.
eapply PeanoNat.Nat.le_trans.
eapply Hel; eauto. lia.
** (* In max' l, n <> max also *)
assert (a <= max' \/ max' <= a). lia.
destruct H0.
*** (* a <= max' *)
exists max'; split; intros; eauto.
destruct H1 as [H1 | [H1 | H2]]; subst; eauto.
*** (* max' <= a *)
exists a; split; intros; eauto.
destruct H1 as [H1 | [H1 | H1]]; subst; eauto.
**** eapply PeanoNat.Nat.le_trans.
eapply Hel; eauto. lia.
**** eapply PeanoNat.Nat.le_trans.
eapply Hel; eauto. lia.
Defined.
Lemma nat_0_le_sn : forall (n : nat),
0 <= S n.
Proof.
induction n; eauto.
Qed.
#[global]
Instance total_order_nat : Total_Order le.
constructor.
induction a; destruct b; eauto.
- left. lia.
- right. lia.
- specialize IHa with b. dest IHa.
* (* a <= b *)
left. apply le_n_S; eauto.
* (* ~ a <= b *)
right. lia.
Defined.
Class WellFounded {A : Type} (R : A -> A -> Prop) `{PO : Partial_Order A R} :=
{
min : A ;
minElemProof :
(forall (a : A), (R min a)) /\
(forall (a : A), a <> min -> ~ (R a min))
}.
#[global]
Instance wf_nat : WellFounded le.
pose proof (Build_WellFounded _ le _ _ _ 0).
apply H. clear H.
intros. split.
- induction a; eauto.
- induction a; eauto.
intros; qcon.
Defined.
Class Defaultable (A : Type) :=
{
defVal : A
}.
#[global]
Instance nat_defaultable : Defaultable nat :=
{
defVal := 0
}.
#[global]
Instance string_defaultable : Defaultable string :=
{
defVal := ""
}.
#[global]
Instance list_defaultable {A : Type} : Defaultable (list A) :=
{
defVal := nil
}.
#[global]
Instance pair_defaultable {A B : Type} `{HA : Defaultable A} `{HB : Defaultable B} : Defaultable(A * B) :=
{
defVal := (defVal, defVal)
}.