Skip to content

Commit 42cf5b8

Browse files
committed
add Compare operational typeclass
1 parent 01a45bb commit 42cf5b8

File tree

24 files changed

+415
-374
lines changed

24 files changed

+415
-374
lines changed

theories/gaia/T1Bridge.v

Lines changed: 31 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -109,18 +109,18 @@ Proof.
109109
Qed.
110110

111111
Lemma compare_ref x :
112-
forall y, match T1.compare x y with
112+
forall y, match T1.compare_T1 x y with
113113
| Lt => T1lt (iota x) (iota y)
114114
| Eq => iota x = iota y
115115
| Gt => T1lt (iota y) (iota x)
116116
end.
117117
Proof.
118118
elim: x => [|x1 IHx1 n x2 IHx2]; case => //= y1 n0 y2.
119-
case H: (T1.compare x1 y1).
119+
case H: (T1.compare_T1 x1 y1).
120120
- specialize (IHx1 y1); rewrite H in IHx1.
121121
case H0: (PeanoNat.Nat.compare n n0).
122122
+ have ->: (n = n0) by apply Compare_dec.nat_compare_eq.
123-
case H1: (T1.compare x2 y2).
123+
case H1: (T1.compare_T1 x2 y2).
124124
* rewrite IHx1; f_equal.
125125
by specialize (IHx2 y2); now rewrite H1 in IHx2.
126126
* case (iota x1 < iota y1); [trivial|].
@@ -141,9 +141,10 @@ Qed.
141141
Lemma lt_ref (a b : hT1): T1.lt a b <-> (iota a < iota b).
142142
Proof.
143143
split.
144-
- rewrite /T1.lt; move => Hab; generalize (compare_ref a b); now rewrite Hab.
144+
- rewrite /T1.lt /Comparable.compare; move => Hab.
145+
generalize (compare_ref a b); now rewrite Hab.
145146
- move => Hab; red.
146-
case_eq (T1.compare a b).
147+
case_eq (T1.compare_T1 a b).
147148
+ move => Heq; generalize (compare_ref a b); rewrite Heq.
148149
move => H0; move: Hab; rewrite H0;
149150
move => Hb; by rewrite T1ltnn in Hb.
@@ -166,11 +167,12 @@ Qed.
166167
Lemma plus_ref : refines2 T1.plus T1add.
167168
Proof.
168169
move => x; elim: x => [|x1 IHx1 n x2 IHx2]; case => //= y1 n0 y2.
169-
case Hx1y1: (T1.compare x1 y1); move: (compare_ref x1 y1); rewrite Hx1y1 => H.
170-
- by rewrite H T1ltnn /=; f_equal.
171-
- by rewrite H /=; f_equal.
170+
case Hx1y1: (T1.compare_T1 x1 y1); move: (compare_ref x1 y1); rewrite Hx1y1 => H.
171+
- rewrite /Comparable.compare H T1ltnn /=; f_equal.
172+
by rewrite Hx1y1 -H /=.
173+
- by rewrite /Comparable.compare H Hx1y1.
172174
- replace (iota x1 < iota y1) with false.
173-
rewrite H /=; f_equal.
175+
rewrite /Comparable.compare H /= Hx1y1; f_equal.
174176
change (cons (iota y1) n0 (iota y2)) with (iota (T1.ocons y1 n0 y2)).
175177
by rewrite IHx2.
176178
by apply T1lt_anti in H.
@@ -285,27 +287,34 @@ Proof.
285287
Qed.
286288

287289

288-
Lemma Comparable_T1lt_eq a b:
289-
Comparable.lt_b a b = (iota a < iota b).
290+
Lemma Comparable_T1lt_eq a b:
291+
DecPreOrder.bool_decide (T1.lt a b) = (iota a < iota b).
290292
Proof.
291-
rewrite /Comparable.lt_b; generalize (compare_ref a b).
292-
case_eq (T1.compare a b).
293-
- move => _ ->; by rewrite T1ltnn.
294-
- by [].
295-
- move => _ H; case_eq (iota a < iota b).
296-
+ move => H0; have H1 : (iota b < iota b) by
297-
apply T1lt_trans with (iota a).
298-
by rewrite T1ltnn in H1.
299-
+ by [].
293+
rewrite /T1.lt; generalize (compare_ref a b);
294+
rewrite /Comparable.compare /=.
295+
destruct (DecPreOrder.decide (T1.compare_T1 a b = Lt)).
296+
- pose proof e as bd.
297+
apply T1.bool_decide_eq_true in bd.
298+
by rewrite bd e.
299+
- pose proof n as bd.
300+
apply T1.bool_decide_eq_false in bd.
301+
rewrite bd.
302+
destruct (T1.compare_T1 a b).
303+
* by move => ->; rewrite T1ltnn.
304+
* by [].
305+
* move => Hlt.
306+
symmetry.
307+
apply/negP => Hlt'.
308+
have H1 : (iota b < iota b) by apply T1lt_trans with (iota a).
309+
by rewrite T1ltnn in H1.
300310
Qed.
301311

302-
303312
Lemma nf_ref a : T1.nf_b a = T1nf (iota a).
304313
Proof.
305314
elim: a => //.
306315
- move => a IHa n b IHb; rewrite T1.nf_b_cons_eq; simpl T1nf.
307316
rewrite IHa IHb; change (phi0 (iota a)) with (iota (T1.phi0 a)).
308-
rewrite andbA; cbn; by rewrite Comparable_T1lt_eq.
317+
by rewrite andbA Comparable_T1lt_eq.
309318
Qed.
310319

311320

theories/ordinals/Epsilon0/Canon.v

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ Pierre Casteran,
1313
From Coq Require Export Arith Lia.
1414
Import Relations Relation_Operators.
1515

16+
From hydras.Prelude Require Import DecPreOrder.
1617
From hydras.Epsilon0 Require Export T1 E0.
1718

1819
Set Implicit Arguments.
@@ -916,20 +917,20 @@ Fixpoint approx alpha beta fuel i :=
916917
FO => None
917918
| Fuel.FS f =>
918919
let gamma := canonS alpha i in
919-
if lt_b beta gamma
920+
if decide (lt beta gamma)
920921
then Some (i,gamma)
921922
else approx alpha beta (f tt) (S i)
922923
end.
923924

924925

925926
Lemma approx_ok alpha beta :
926927
forall fuel i j gamma, approx alpha beta fuel i = Some (j,gamma) ->
927-
gamma = canonS alpha j /\ lt_b beta gamma.
928+
gamma = canonS alpha j /\ lt beta gamma.
928929
Proof.
929930
induction fuel as [| f IHfuel ].
930931
- cbn; discriminate.
931932
- intros i j gamma H0; cbn in H0.
932-
case_eq (lt_b beta (canonS alpha i));intro H1; rewrite H1 in *.
933+
destruct (decide (lt beta (canonS alpha i))) as [H1|H1].
933934
+ injection H0; intros; subst; split;auto.
934935
+ now specialize (IHfuel tt (S i) _ _ H0).
935936
Qed.

theories/ordinals/Epsilon0/E0.v

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -316,12 +316,11 @@ Proof.
316316
apply LT_trans.
317317
Qed.
318318

319-
Definition compare (alpha beta:E0): comparison :=
320-
T1.compare (cnf alpha) (cnf beta).
319+
Instance compare_E0 : Compare E0 :=
320+
fun (alpha beta:E0) => compare (cnf alpha) (cnf beta).
321321

322-
Lemma compare_correct alpha beta :
323-
CompareSpec (alpha = beta) (alpha o< beta) (beta o< alpha)
324-
(compare alpha beta).
322+
Lemma compare_correct (alpha beta : E0) :
323+
CompSpec eq Lt alpha beta (compare alpha beta).
325324
Proof.
326325
destruct alpha, beta; unfold compare, Lt; cbn;
327326
destruct (T1.compare_correct cnf0 cnf1).

theories/ordinals/Epsilon0/Hessenberg.v

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -359,8 +359,9 @@ Proof.
359359
- intros; rewrite (oplus_eqn (ocons a n b) (ocons x1 n0 x2)).
360360
apply nf_helper_phi0 in H1.
361361
destruct (lt_inv H1).
362-
+ unfold T1.lt, lt_b in H2; rewrite compare_rev.
363-
destruct (compare x1 a);auto; try discriminate.
362+
+ unfold T1.lt in H2.
363+
rewrite compare_rev, H2.
364+
reflexivity.
364365
+ decompose [or and] H2.
365366
* inversion H5.
366367
* T1_inversion H6.
@@ -522,7 +523,7 @@ Section Proof_of_oplus_lt1.
522523
- simpl; auto with T1.
523524
- rewrite oplus_eqn;case_eq (compare x1 a1).
524525
+ auto with T1 arith.
525-
+ intros H2; apply head_lt; unfold T1.lt, lt_b; now rewrite H2.
526+
+ intros H2; apply head_lt. unfold T1.lt; now rewrite H2.
526527
+ intro; apply tail_lt, H1 ; trivial.
527528
eapply nf_inv2, H.
528529
now apply tail_lt_ocons.
@@ -619,7 +620,7 @@ Proof with eauto with T1.
619620
absurd (T1.lt (ocons a1 n0 b2) (ocons c1 n1 c2)).
620621
- apply lt_not_gt.
621622
apply head_lt.
622-
unfold T1.lt, lt_b;rewrite compare_rev. now rewrite H2.
623+
unfold T1.lt;rewrite compare_rev. now rewrite H2.
623624
- auto.
624625
}
625626
{
@@ -652,7 +653,7 @@ Proof with eauto with T1.
652653
case_eq (compare a1 c2_1).
653654
intro.
654655
apply coeff_lt; auto with arith.
655-
intro H6; apply head_lt; unfold T1.lt, lt_b; now rewrite H6.
656+
intro H6; apply head_lt; unfold T1.lt; now rewrite H6.
656657
intros; apply tail_lt.
657658
apply oplus_lt1; trivial.
658659
T1_inversion H5.

theories/ordinals/Epsilon0/Paths.v

Lines changed: 33 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414

1515
(** TODO: Check wether the predicates ...PathS... are still useful *)
1616

17-
Require Import Canon MoreLists First_toggle OrdNotations.
17+
From hydras Require Import DecPreOrder Canon MoreLists First_toggle OrdNotations.
1818
Import Relations Relation_Operators.
1919
From Coq Require Import Lia.
2020

@@ -2592,23 +2592,22 @@ Section Constant_to_standard_Proof.
25922592
unfold t; pattern (proj1_sig Rem2); apply proj2_sig.
25932593
Qed.
25942594

2595-
Let P (i:nat) := le_b beta (standard_gnaw (S n) alpha i).
2595+
Let P (i:nat) := compare (standard_gnaw (S n) alpha i) beta <> Datatypes.Lt.
25962596

25972597
Remark Rem04 : P 0.
25982598
Proof.
2599-
unfold P;red; simpl.
2600-
unfold le_b, lt_b.
2601-
eenough (T1.compare alpha beta = Datatypes.Gt) as -> by auto.
2599+
unfold P;red; simpl.
2600+
enough (Hgt: (compare alpha beta = Datatypes.Gt)) by congruence.
26022601
apply compare_gt_iff.
26032602
generalize (const_pathS_LT Halpha Hpa).
26042603
destruct 1; tauto.
2605-
Qed.
2604+
Qed.
26062605

26072606

2608-
Remark Rem05 : P t = false.
2607+
Remark Rem05 : ~ P t.
26092608
Proof.
2610-
unfold P, le_b, lt_b;
2611-
enough(T1.compare (standard_gnaw (S n) alpha t) beta = Datatypes.Lt) as -> by reflexivity.
2609+
unfold P.
2610+
enough (compare (standard_gnaw (S n) alpha t) beta = Datatypes.Lt) by congruence.
26122611
apply compare_lt_iff.
26132612
destruct Rem03; tauto.
26142613
Qed.
@@ -2621,8 +2620,21 @@ Section Constant_to_standard_Proof.
26212620
eapply const_pathS_LT; eauto.
26222621
- auto with arith.
26232622
Qed.
2623+
2624+
Instance P_dec i : Decision (P i).
2625+
Proof.
2626+
destruct (standard_gnaw (S n) alpha i <?> beta) eqn:Hc.
2627+
- left; unfold P.
2628+
rewrite Hc; discriminate.
2629+
- right; unfold P.
2630+
rewrite Hc.
2631+
intro H.
2632+
contradiction.
2633+
- left; unfold P.
2634+
rewrite Hc; discriminate.
2635+
Defined.
26242636

2625-
Let l_def := first_toggle P 0 t Rem06 Rem04 Rem05.
2637+
Let l_def := first_toggle P P_dec 0 t Rem06 Rem04 Rem05.
26262638

26272639
Let l := proj1_sig l_def.
26282640

@@ -2633,21 +2645,21 @@ Section Constant_to_standard_Proof.
26332645
(l < t)%nat /\
26342646
(forall i : nat,
26352647
(0 <= i)%nat ->
2636-
(i <= l)%nat -> P i = true) /\ P (S l) = false.
2648+
(i <= l)%nat -> P i) /\ ~ P (S l).
26372649
Proof.
26382650
unfold l; pattern (proj1_sig l_def); apply proj2_sig.
26392651
Qed.
26402652

26412653
Remark Rem09 : (l < t)%nat.
26422654
Proof. destruct Rem08; tauto. Qed.
26432655

2644-
Remark Rem10 : P l = true.
2656+
Remark Rem10 : P l.
26452657
Proof.
26462658
destruct Rem08 as [H H0];decompose [and] H0.
26472659
apply H3; auto with arith.
26482660
Qed.
26492661

2650-
Remark Rem11 : P (S l) = false.
2662+
Remark Rem11 : ~ P (S l).
26512663
Proof.
26522664
destruct Rem08 as [H H0]; now decompose [and] H0.
26532665
Qed.
@@ -2726,13 +2738,13 @@ Section Constant_to_standard_Proof.
27262738

27272739
Remark R19 : beta t1<= gamma.
27282740
Proof.
2729-
generalize Rem10; unfold P; fold gamma ;unfold le_b, lt_b.
2741+
generalize Rem10; unfold P; fold gamma.
27302742
intro H.
2731-
destruct (T1.compare gamma beta) eqn: Hcomp.
2743+
destruct (compare gamma beta) eqn: Hcomp.
27322744
- apply compare_eq_iff in Hcomp as ->.
27332745
apply LE_refl.
27342746
eapply LT_nf_r; eauto.
2735-
- intros; discriminate.
2747+
- contradiction.
27362748
- apply LE_r.
27372749
rewrite compare_gt_iff in Hcomp; repeat split; auto.
27382750
+ eapply LT_nf_r; eauto.
@@ -2787,15 +2799,14 @@ Section Constant_to_standard_Proof.
27872799

27882800
Remark R25 : delta t1< beta.
27892801
Proof.
2790-
rewrite R22; generalize Rem11;unfold P; intro H;
2791-
unfold le_b, lt_b in H.
2792-
destruct( T1.compare (standard_gnaw (S n) alpha (S l)) beta) eqn: H0.
2793-
- discriminate.
2802+
rewrite R22; generalize Rem11;unfold P; intro H.
2803+
destruct(compare (standard_gnaw (S n) alpha (S l)) beta) eqn: H0.
2804+
- contradict H; congruence.
27942805
- rewrite compare_lt_iff in H0.
27952806
repeat split;auto.
27962807
+ apply standard_gnaw_nf;auto.
27972808
+ eapply LT_nf_r; eauto.
2798-
- discriminate.
2809+
- contradict H; congruence.
27992810
Qed.
28002811

28012812
Remark R26 : ~ const_pathS (n+l) gamma beta.
@@ -3021,7 +3032,7 @@ Proof.
30213032
red in H0;unfold E0.cnf; simpl in *.
30223033
destruct beta.
30233034
+ destruct H; now apply E0_eq_intro.
3024-
+ destruct (T1.compare alpha beta1) eqn:H2.
3035+
+ destruct (compare alpha beta1) eqn:H2.
30253036
* unfold lt in H1; simpl in H1.
30263037
rewrite compare_eq_iff in H2; subst beta1.
30273038
destruct (LT_inv H1).

0 commit comments

Comments
 (0)