Debian Codegraph @

src/Util/Strings/String_as_OT_old.v

raw
1 From Coq Require Import OrderedTypeEx.
2 From Coq Require Import Ascii String.
3 From Coq Require Import Arith.
4 From Coq Require Import NArith.
5 From Coq Require Import OrderedType.
6 Require Export Crypto.Util.FixCoqMistakes.
7
8 (** This is copied verbatim from coq/theories/Structures/OrderedTypeEx.v, and should be removed once we bump the version requirement to 8.12, where we get the benefit of https://github.com/coq/coq/pull/12044 *)
9 (** See the original file in the Coq sources for the license *)
10
11 Module Ascii_as_OT <: UsualOrderedType.
12 Definition t := ascii.
13
14 Definition eq := @eq ascii.
15 Definition eq_refl := @eq_refl t.
16 Definition eq_sym := @eq_sym t.
17 Definition eq_trans := @eq_trans t.
18
19 Definition cmp (a b : ascii) : comparison :=
20 N.compare (N_of_ascii a) (N_of_ascii b).
21
22 Lemma cmp_eq (a b : ascii):
23 cmp a b = Eq <-> a = b.
24 Proof.
25 unfold cmp.
26 rewrite N.compare_eq_iff.
27 split. 2:{ intro. now subst. }
28 intro H.
29 rewrite<- (ascii_N_embedding a).
30 rewrite<- (ascii_N_embedding b).
31 now rewrite H.
32 Qed.
33
34 Lemma cmp_lt_nat (a b : ascii):
35 cmp a b = Lt <-> (nat_of_ascii a < nat_of_ascii b)%nat.
36 Proof.
37 unfold cmp. unfold nat_of_ascii.
38 rewrite N2Nat.inj_compare.
39 rewrite Nat.compare_lt_iff.
40 reflexivity.
41 Qed.
42
43 Lemma cmp_antisym (a b : ascii):
44 cmp a b = CompOpp (cmp b a).
45 Proof.
46 unfold cmp.
47 apply N.compare_antisym.
48 Qed.
49
50 Definition lt (x y : ascii) := (N_of_ascii x < N_of_ascii y)%N.
51
52 Lemma lt_trans (x y z : ascii):
53 lt x y -> lt y z -> lt x z.
54 Proof.
55 apply N.lt_trans.
56 Qed.
57
58 Lemma lt_not_eq (x y : ascii):
59 lt x y -> x <> y.
60 Proof.
61 intros L H. subst.
62 exact (N.lt_irrefl _ L).
63 Qed.
64
65 Local Lemma compare_helper_eq {a b : ascii} (E : cmp a b = Eq):
66 a = b.
67 Proof.
68 now apply cmp_eq.
69 Qed.
70
71 Local Lemma compare_helper_gt {a b : ascii} (G : cmp a b = Gt):
72 lt b a.
73 Proof.
74 now apply N.compare_gt_iff.
75 Qed.
76
77 Definition compare (a b : ascii) : Compare lt eq a b :=
78 match cmp a b as z return _ = z -> _ with
79 | Lt => fun E => LT E
80 | Gt => fun E => GT (compare_helper_gt E)
81 | Eq => fun E => EQ (compare_helper_eq E)
82 end Logic.eq_refl.
83
84 Definition eq_dec (x y : ascii): {x = y} + { ~ (x = y)} := ascii_dec x y.
85 End Ascii_as_OT.
86
87 (** [String] is an ordered type with respect to the usual lexical order. *)
88
89 Module String_as_OT <: UsualOrderedType.
90
91 Definition t := string.
92
93 Definition eq := @eq string.
94 Definition eq_refl := @eq_refl t.
95 Definition eq_sym := @eq_sym t.
96 Definition eq_trans := @eq_trans t.
97
98 Inductive lts : string -> string -> Prop :=
99 | lts_empty : forall a s, lts EmptyString (String a s)
100 | lts_tail : forall a s1 s2, lts s1 s2 -> lts (String a s1) (String a s2)
101 | lts_head : forall (a b : ascii) s1 s2,
102 lt (nat_of_ascii a) (nat_of_ascii b) ->
103 lts (String a s1) (String b s2).
104
105 Definition lt := lts.
106
107 Lemma nat_of_ascii_inverse a b : nat_of_ascii a = nat_of_ascii b -> a = b.
108 Proof.
109 intro H.
110 rewrite <- (ascii_nat_embedding a).
111 rewrite <- (ascii_nat_embedding b).
112 apply f_equal; auto.
113 Qed.
114
115 Lemma lts_tail_unique a s1 s2 : lt (String a s1) (String a s2) ->
116 lt s1 s2.
117 Proof.
118 intro H; inversion H; subst; auto.
119 remember (nat_of_ascii a) as x.
120 apply Nat.lt_irrefl in H1; inversion H1.
121 Qed.
122
123 Lemma lt_trans : forall x y z : t, lt x y -> lt y z -> lt x z.
124 Proof.
125 induction x; intros y z H1 H2.
126 - destruct y as [| b y']; inversion H1.
127 destruct z as [| c z']; inversion H2; constructor.
128 - destruct y as [| b y']; inversion H1; subst;
129 destruct z as [| c z']; inversion H2; subst.
130 + constructor. eapply IHx; eauto.
131 + constructor; assumption.
132 + constructor; assumption.
133 + constructor. eapply Nat.lt_trans; eassumption.
134 Qed.
135
136 Lemma lt_not_eq : forall x y : t, lt x y -> ~ eq x y.
137 Proof.
138 induction x; intros y LT.
139 - inversion LT. intro. inversion H.
140 - inversion LT; subst; intros EQ.
141 * specialize (IHx s2 H2).
142 inversion EQ; subst; auto.
143 apply IHx; unfold eq; auto.
144 * inversion EQ; subst; auto.
145 apply Nat.lt_irrefl in H2; auto.
146 Qed.
147
148 Fixpoint cmp (a b : string) : comparison :=
149 match a, b with
150 | EmptyString, EmptyString => Eq
151 | EmptyString, _ => Lt
152 | String _ _, EmptyString => Gt
153 | String a_head a_tail, String b_head b_tail =>
154 match Ascii_as_OT.cmp a_head b_head with
155 | Lt => Lt
156 | Gt => Gt
157 | Eq => cmp a_tail b_tail
158 end
159 end.
160
161 Lemma cmp_eq (a b : string):
162 cmp a b = Eq <-> a = b.
163 Proof.
164 revert b.
165 induction a, b; try easy.
166 cbn.
167 remember (Ascii_as_OT.cmp _ _) as c eqn:Heqc. symmetry in Heqc.
168 destruct c; split; try discriminate;
169 try rewrite Ascii_as_OT.cmp_eq in Heqc; try subst;
170 try rewrite IHa; intro H.
171 { now subst. }
172 { now inversion H. }
173 { inversion H; subst. rewrite<- Heqc. now rewrite Ascii_as_OT.cmp_eq. }
174 { inversion H; subst. rewrite<- Heqc. now rewrite Ascii_as_OT.cmp_eq. }
175 Qed.
176
177 Lemma cmp_antisym (a b : string):
178 cmp a b = CompOpp (cmp b a).
179 Proof.
180 revert b.
181 induction a, b; try easy.
182 cbn. rewrite IHa. clear IHa.
183 remember (Ascii_as_OT.cmp _ _) as c eqn:Heqc. symmetry in Heqc.
184 destruct c; rewrite Ascii_as_OT.cmp_antisym in Heqc;
185 destruct Ascii_as_OT.cmp; cbn in *; easy.
186 Qed.
187
188 Lemma cmp_lt (a b : string):
189 cmp a b = Lt <-> lt a b.
190 Proof.
191 revert b.
192 induction a as [ | a_head a_tail ], b; try easy; cbn.
193 { split; trivial. intro. apply lts_empty. }
194 remember (Ascii_as_OT.cmp _ _) as c eqn:Heqc. symmetry in Heqc.
195 destruct c; split; intro H; try discriminate; trivial.
196 {
197 rewrite Ascii_as_OT.cmp_eq in Heqc. subst.
198 apply String_as_OT.lts_tail.
199 apply IHa_tail.
200 assumption.
201 }
202 {
203 rewrite Ascii_as_OT.cmp_eq in Heqc. subst.
204 inversion H; subst. { rewrite IHa_tail. assumption. }
205 exfalso. apply (Nat.lt_irrefl (nat_of_ascii a)). assumption.
206 }
207 {
208 apply String_as_OT.lts_head.
209 rewrite<- Ascii_as_OT.cmp_lt_nat.
210 assumption.
211 }
212 {
213 exfalso. inversion H; subst.
214 {
215 assert(X: Ascii_as_OT.cmp a a = Eq). { apply Ascii_as_OT.cmp_eq. trivial. }
216 rewrite Heqc in X. discriminate.
217 }
218 rewrite<- Ascii_as_OT.cmp_lt_nat in *. rewrite Heqc in *. discriminate.
219 }
220 Qed.
221
222 Local Lemma compare_helper_lt {a b : string} (L : cmp a b = Lt):
223 lt a b.
224 Proof.
225 now apply cmp_lt.
226 Qed.
227
228 Local Lemma compare_helper_gt {a b : string} (G : cmp a b = Gt):
229 lt b a.
230 Proof.
231 rewrite cmp_antisym in G.
232 rewrite CompOpp_iff in G.
233 now apply cmp_lt.
234 Qed.
235
236 Local Lemma compare_helper_eq {a b : string} (E : cmp a b = Eq):
237 a = b.
238 Proof.
239 now apply cmp_eq.
240 Qed.
241
242 Definition compare (a b : string) : Compare lt eq a b :=
243 match cmp a b as z return _ = z -> _ with
244 | Lt => fun E => LT (compare_helper_lt E)
245 | Gt => fun E => GT (compare_helper_gt E)
246 | Eq => fun E => EQ (compare_helper_eq E)
247 end Logic.eq_refl.
248
249 Definition eq_dec (x y : string): {x = y} + { ~ (x = y)} := string_dec x y.
250 End String_as_OT.