Debian Codegraph @

src/Util/Strings/String_as_OT.v

raw
1 From Coq Require Import Orders.
2 From Coq Require Import OrdersEx.
3 From Coq Require Import Ascii String.
4 From Coq Require Import Arith.
5 From Coq Require Import NArith.
6 Require Export Crypto.Util.FixCoqMistakes.
7
8 (** This should disappear whenever we bump the minimum version to a version where https://github.com/coq/coq/pull/14096 has been merged *)
9
10 Module Ascii_as_OT <: UsualOrderedType.
11 Definition t := ascii.
12 Include HasUsualEq <+ UsualIsEq.
13 Definition eqb := Ascii.eqb.
14 Definition eqb_eq := Ascii.eqb_eq.
15 Include HasEqBool2Dec.
16
17 Definition compare (a b : ascii) := N_as_OT.compare (N_of_ascii a) (N_of_ascii b).
18 Definition lt (a b : ascii) := N_as_OT.lt (N_of_ascii a) (N_of_ascii b).
19
20 #[global]
21 Instance lt_compat : Proper (eq==>eq==>iff) lt.
22 Proof.
23 intros x x' Hx y y' Hy. rewrite Hx, Hy; intuition.
24 Qed.
25
26 #[global]
27 Instance lt_strorder : StrictOrder lt.
28 Proof.
29 split; unfold lt; [ intro | intros ??? ]; eapply N_as_OT.lt_strorder.
30 Qed.
31
32 Lemma compare_spec : forall x y, CompSpec eq lt x y (compare x y).
33 Proof.
34 intros x y; unfold eq, lt, compare.
35 destruct (N_as_OT.compare_spec (N_of_ascii x) (N_of_ascii y)) as [H|H|H]; constructor; try assumption.
36 now rewrite <- (ascii_N_embedding x), <- (ascii_N_embedding y), H.
37 Qed.
38 End Ascii_as_OT.
39
40 (** [String] is an ordered type with respect to the usual lexical order. *)
41
42 Module String_as_OT <: UsualOrderedType.
43 Definition t := string.
44 Include HasUsualEq <+ UsualIsEq.
45 Definition eqb := String.eqb.
46 Definition eqb_eq := String.eqb_eq.
47 Include HasEqBool2Dec.
48
49 Fixpoint compare (a b : string)
50 := match a, b with
51 | EmptyString, EmptyString => Eq
52 | EmptyString, _ => Lt
53 | String _ _, EmptyString => Gt
54 | String a_head a_tail, String b_head b_tail =>
55 match Ascii_as_OT.compare a_head b_head with
56 | Lt => Lt
57 | Gt => Gt
58 | Eq => compare a_tail b_tail
59 end
60 end.
61
62 Definition lt (a b : string) := compare a b = Lt.
63
64 #[global]
65 Instance lt_compat : Proper (eq==>eq==>iff) lt.
66 Proof.
67 intros x x' Hx y y' Hy. rewrite Hx, Hy; intuition.
68 Qed.
69
70 Lemma compare_spec : forall x y, CompSpec eq lt x y (compare x y).
71 Proof.
72 unfold eq, lt.
73 induction x as [|x xs IHxs], y as [|y ys]; cbn [compare]; try constructor; cbn [compare]; try reflexivity.
74 specialize (IHxs ys).
75 destruct (Ascii_as_OT.compare x y) eqn:H; [ destruct IHxs; constructor | constructor | constructor ]; cbn [compare].
76 all: destruct (Ascii_as_OT.compare_spec y x), (Ascii_as_OT.compare_spec x y); cbv [Ascii_as_OT.eq] in *; try congruence; subst.
77 all: exfalso; eapply irreflexivity; (idtac + etransitivity); eassumption.
78 Qed.
79
80 #[global]
81 Instance lt_strorder : StrictOrder lt.
82 Proof.
83 split; unfold lt; [ intro x | intros x y z ]; unfold complement.
84 { induction x as [|x xs IHxs]; cbn [compare]; [ congruence | ].
85 destruct (Ascii_as_OT.compare x x) eqn:H; try congruence.
86 exfalso; eapply irreflexivity; eassumption. }
87 { revert x y z.
88 induction x as [|x xs IHxs], y as [|y ys], z as [|z zs]; cbn [compare]; try congruence.
89 specialize (IHxs ys zs).
90 destruct (Ascii_as_OT.compare x y) eqn:Hxy, (Ascii_as_OT.compare y z) eqn:Hyz, (Ascii_as_OT.compare x z) eqn:Hxz;
91 try intuition (congruence || eauto).
92 all: destruct (Ascii_as_OT.compare_spec x y), (Ascii_as_OT.compare_spec y z), (Ascii_as_OT.compare_spec x z);
93 try discriminate.
94 all: unfold Ascii_as_OT.eq in *; subst.
95 all: exfalso; eapply irreflexivity; (idtac + etransitivity); (idtac + etransitivity); eassumption. }
96 Qed.
97 End String_as_OT.