lexico.v 5.83 KB
Newer Older
Robbert Krebbers's avatar
Robbert Krebbers committed
1
(* Copyright (c) 2012-2015, Robbert Krebbers. *)
2 3 4
(* This file is distributed under the terms of the BSD license. *)
(** This files defines a lexicographic order on various common data structures
and proves that it is a partial order having a strong variant of trichotomy. *)
5
Require Import numbers.
6 7 8 9 10 11 12 13

Notation cast_trichotomy T :=
  match T with
  | inleft (left _) => inleft (left _)
  | inleft (right _) => inleft (right _)
  | inright _ => inright _
  end.

14 15 16
Instance prod_lexico `{Lexico A, Lexico B} : Lexico (A * B) := λ p1 p2,
  (**i 1.) *) lexico (p1.1) (p2.1) 
  (**i 2.) *) p1.1 = p2.1  lexico (p1.2) (p2.2).
17

18 19 20 21 22
Instance bool_lexico : Lexico bool := λ b1 b2,
  match b1, b2 with false, true => True | _, _ => False end.
Instance nat_lexico : Lexico nat := (<).
Instance N_lexico : Lexico N := (<)%N.
Instance Z_lexico : Lexico Z := (<)%Z.
23 24 25 26 27
Typeclasses Opaque bool_lexico nat_lexico N_lexico Z_lexico.
Instance list_lexico `{Lexico A} : Lexico (list A) :=
  fix go l1 l2 :=
  let _ : Lexico (list A) := @go in
  match l1, l2 with
28
  | [], _ :: _ => True
29
  | x1 :: l1, x2 :: l2 => lexico (x1,l1) (x2,l2)
30
  | _, _ => False
31 32 33 34
  end.
Instance sig_lexico `{Lexico A} (P : A  Prop) `{ x, ProofIrrel (P x)} :
  Lexico (sig P) := λ x1 x2, lexico (`x1) (`x2).

35 36
Lemma prod_lexico_irreflexive `{Lexico A, Lexico B, !Irreflexive (@lexico A _)}
  (x : A) (y : B) : complement lexico y y  complement lexico (x,y) (x,y).
37
Proof. intros ? [?|[??]]. by apply (irreflexivity lexico x). done. Qed.
38 39
Lemma prod_lexico_transitive `{Lexico A, Lexico B, !Transitive (@lexico A _)}
    (x1 x2 x3 : A) (y1 y2 y3 : B) :
40 41 42 43
  lexico (x1,y1) (x2,y2)  lexico (x2,y2) (x3,y3) 
  (lexico y1 y2  lexico y2 y3  lexico y1 y3)  lexico (x1,y1) (x3,y3).
Proof.
  intros Hx12 Hx23 ?; revert Hx12 Hx23. unfold lexico, prod_lexico.
44 45
  intros [|[??]] [?|[??]]; simplify_equality'; auto.
  by left; transitivity x2.
46
Qed.
47

48
Instance prod_lexico_po `{Lexico A, Lexico B, !StrictOrder (@lexico A _)}
49
  `{!StrictOrder (@lexico B _)} : StrictOrder (@lexico (A * B) _).
50
Proof.
51 52 53
  split.
  * intros [x y]. apply prod_lexico_irreflexive.
    by apply (irreflexivity lexico y).
54
  * intros [??] [??] [??] ??.
55
    eapply prod_lexico_transitive; eauto. apply transitivity.
56
Qed.
57 58
Instance prod_lexico_trichotomyT `{Lexico A, tA : !TrichotomyT (@lexico A _)}
  `{Lexico B, tB : !TrichotomyT (@lexico B _)}: TrichotomyT (@lexico (A * B) _).
59 60
Proof.
 red; refine (λ p1 p2,
61
  match trichotomyT lexico (p1.1) (p2.1) with
62
  | inleft (left _) => inleft (left _)
63
  | inleft (right _) => cast_trichotomy (trichotomyT lexico (p1.2) (p2.2))
64
  | inright _ => inright _
65 66
  end); clear tA tB;
    abstract (unfold lexico, prod_lexico; auto using injective_projections).
67 68
Defined.

69 70
Instance bool_lexico_po : StrictOrder (@lexico bool _).
Proof. split. by intros [] ?. by intros [] [] [] ??. Qed.
71 72 73 74 75 76 77 78 79 80 81
Instance bool_lexico_trichotomy: TrichotomyT (@lexico bool _).
Proof.
 red; refine (λ b1 b2,
  match b1, b2 with
  | false, false => inleft (right _)
  | false, true => inleft (left _)
  | true, false => inright _
  | true, true => inleft (right _)
  end); abstract (unfold strict, lexico, bool_lexico; naive_solver).
Defined.

82
Instance nat_lexico_po : StrictOrder (@lexico nat _).
83 84 85 86 87
Proof. unfold lexico, nat_lexico. apply _. Qed.
Instance nat_lexico_trichotomy: TrichotomyT (@lexico nat _).
Proof.
 red; refine (λ n1 n2,
  match Nat.compare n1 n2 as c return Nat.compare n1 n2 = c  _ with
88
  | Lt => λ H, inleft (left (nat_compare_Lt_lt _ _ H))
89
  | Eq => λ H, inleft (right (nat_compare_eq _ _ H))
90
  | Gt => λ H, inright (nat_compare_Gt_gt _ _ H)
91 92 93
  end eq_refl).
Defined.

94
Instance N_lexico_po : StrictOrder (@lexico N _).
95 96 97 98 99
Proof. unfold lexico, N_lexico. apply _. Qed.
Instance N_lexico_trichotomy: TrichotomyT (@lexico N _).
Proof.
 red; refine (λ n1 n2,
  match N.compare n1 n2 as c return N.compare n1 n2 = c  _ with
100
  | Lt => λ H, inleft (left (proj2 (N.compare_lt_iff _ _) H))
101
  | Eq => λ H, inleft (right (N.compare_eq _ _ H))
102
  | Gt => λ H, inright (proj1 (N.compare_gt_iff _ _) H)
103 104 105
  end eq_refl).
Defined.

106
Instance Z_lexico_po : StrictOrder (@lexico Z _).
107 108 109 110 111
Proof. unfold lexico, Z_lexico. apply _. Qed.
Instance Z_lexico_trichotomy: TrichotomyT (@lexico Z _).
Proof.
 red; refine (λ n1 n2,
  match Z.compare n1 n2 as c return Z.compare n1 n2 = c  _ with
112
  | Lt => λ H, inleft (left (proj2 (Z.compare_lt_iff _ _) H))
113
  | Eq => λ H, inleft (right (Z.compare_eq _ _ H))
114
  | Gt => λ H, inright (proj1 (Z.compare_gt_iff _ _) H)
115 116 117
  end eq_refl).
Defined.

118
Instance list_lexico_po `{Lexico A, !StrictOrder (@lexico A _)} :
119
  StrictOrder (@lexico (list A) _).
120
Proof.
121 122
  split.
  * intros l. induction l. by intros ?. by apply prod_lexico_irreflexive.
123 124 125
  * intros l1. induction l1 as [|x1 l1]; intros [|x2 l2] [|x3 l3] ??; try done.
    eapply prod_lexico_transitive; eauto.
Qed.
126
Instance list_lexico_trichotomy `{Lexico A, tA : !TrichotomyT (@lexico A _)} :
127 128 129 130 131 132 133 134 135 136
  TrichotomyT (@lexico (list A) _).
Proof.
 refine (
  fix go l1 l2 :=
  let go' : TrichotomyT (@lexico (list A) _) := @go in
  match l1, l2 with
  | [], [] => inleft (right _)
  | [], _ :: _ => inleft (left _)
  | _ :: _, [] => inright _
  | x1 :: l1, x2 :: l2 => cast_trichotomy (trichotomyT lexico (x1,l1) (x2,l2))
137 138
  end); clear tA go go';
    abstract (repeat (done || constructor || congruence || by inversion 1)).
139 140
Defined.

141
Instance sig_lexico_po `{Lexico A, !StrictOrder (@lexico A _)}
142
  (P : A  Prop) `{ x, ProofIrrel (P x)} : StrictOrder (@lexico (sig P) _).
143
Proof.
144 145 146
  unfold lexico, sig_lexico. split.
  * intros [x ?] ?. by apply (irreflexivity lexico x). 
  * intros [x1 ?] [x2 ?] [x3 ?] ??. by transitivity x2.
147
Qed.
148
Instance sig_lexico_trichotomy `{Lexico A, tA : !TrichotomyT (@lexico A _)}
149 150 151 152 153
  (P : A  Prop) `{ x, ProofIrrel (P x)} : TrichotomyT (@lexico (sig P) _).
Proof.
 red; refine (λ x1 x2, cast_trichotomy (trichotomyT lexico (`x1) (`x2)));
  abstract (repeat (done || constructor || apply (sig_eq_pi P))).
Defined.