| 1 |
(* File inspired from https://gitlab.mpi-sws.org/iris/stdpp/-/blob/8c98553ad0ca2029b30cf18b58e321ec3a79172b/stdpp/base.v *) |
| 2 |
From Corelib Require Export Morphisms RelationClasses ListDef Setoid. |
| 3 |
From Stdlib Require Export List Peano Utf8 Permutation. |
| 4 |
From Stdlib Require Export Program.Basics Program.Syntax. |
| 5 |
|
| 6 |
Export ListNotations. |
| 7 |
|
| 8 |
|
| 9 |
Notation length := Datatypes.length. |
| 10 |
Global Generalizable All Variables. |
| 11 |
Global Unset Transparent Obligations. |
| 12 |
|
| 13 |
Definition tc_opaque {A} (x : A) : A := x. |
| 14 |
(* Typeclasses Opaque tc_opaque. *) |
| 15 |
|
| 16 |
Global Arguments tc_opaque {_} _ /. |
| 17 |
Declare Scope stdpp_scope. |
| 18 |
Delimit Scope stdpp_scope with stdpp. |
| 19 |
Global Open Scope stdpp_scope. |
| 20 |
Notation "(=)" := eq (only parsing) : stdpp_scope. |
| 21 |
Notation "( x =.)" := (eq x) (only parsing) : stdpp_scope. |
| 22 |
Notation "(.= x )" := (λ y, eq y x) (only parsing) : stdpp_scope. |
| 23 |
Notation "(≠)" := (λ x y, x ≠ y) (only parsing) : stdpp_scope. |
| 24 |
Notation "( x ≠.)" := (λ y, x ≠ y) (only parsing) : stdpp_scope. |
| 25 |
Notation "(.≠ x )" := (λ y, y ≠ x) (only parsing) : stdpp_scope. |
| 26 |
Infix "=@{ A }" := (@eq A) |
| 27 |
(at level 70, only parsing, no associativity) : stdpp_scope. |
| 28 |
Notation "(=@{ A } )" := (@eq A) (only parsing) : stdpp_scope. |
| 29 |
Notation "(≠@{ A } )" := (λ X Y, ¬X =@{A} Y) (only parsing) : stdpp_scope. |
| 30 |
Notation "X ≠@{ A } Y":= (¬X =@{ A } Y) |
| 31 |
(at level 70, only parsing, no associativity) : stdpp_scope. |
| 32 |
|
| 33 |
Global Hint Extern 0 (_ = _) => reflexivity : core. |
| 34 |
Global Hint Extern 100 (_ ≠ _) => discriminate : core. |
| 35 |
|
| 36 |
Global Instance: ∀ A, PreOrder (=@{A}). |
| 37 |
Proof. split; repeat intro; congruence. Qed. |
| 38 |
Class Equiv A := equiv: relation A. |
| 39 |
Global Instance equiv_rewrite_relation `{Equiv A} : |
| 40 |
RewriteRelation (@equiv A _) | 150 := {}. |
| 41 |
|
| 42 |
Infix "≡" := equiv (at level 70, no associativity) : stdpp_scope. |
| 43 |
Infix "≡@{ A }" := (@equiv A _) |
| 44 |
(at level 70, only parsing, no associativity) : stdpp_scope. |
| 45 |
|
| 46 |
Notation "(≡)" := equiv (only parsing) : stdpp_scope. |
| 47 |
Notation "( X ≡.)" := (equiv X) (only parsing) : stdpp_scope. |
| 48 |
Notation "(.≡ X )" := (λ Y, Y ≡ X) (only parsing) : stdpp_scope. |
| 49 |
Notation "(≢)" := (λ X Y, ¬X ≡ Y) (only parsing) : stdpp_scope. |
| 50 |
Notation "X ≢ Y":= (¬X ≡ Y) (at level 70, no associativity) : stdpp_scope. |
| 51 |
Notation "( X ≢.)" := (λ Y, X ≢ Y) (only parsing) : stdpp_scope. |
| 52 |
Notation "(.≢ X )" := (λ Y, Y ≢ X) (only parsing) : stdpp_scope. |
| 53 |
|
| 54 |
Notation "(≡@{ A } )" := (@equiv A _) (only parsing) : stdpp_scope. |
| 55 |
Notation "(≢@{ A } )" := (λ X Y, ¬X ≡@{A} Y) (only parsing) : stdpp_scope. |
| 56 |
Notation "X ≢@{ A } Y":= (¬X ≡@{ A } Y) |
| 57 |
(at level 70, only parsing, no associativity) : stdpp_scope. |
| 58 |
Class LeibnizEquiv A `{Equiv A} := |
| 59 |
leibniz_equiv (x y : A) : x ≡ y → x = y. |
| 60 |
Global Hint Mode LeibnizEquiv ! - : typeclass_instances. |
| 61 |
|
| 62 |
Global Instance: Params (@equiv) 2 := {}. |
| 63 |
Global Instance equiv_default_relation `{Equiv A} : |
| 64 |
DefaultRelation (≡@{A}) | 3 := {}. |
| 65 |
Global Hint Extern 0 (_ ≡ _) => reflexivity : core. |
| 66 |
Global Hint Extern 0 (_ ≡ _) => symmetry; assumption : core. |
| 67 |
|
| 68 |
|
| 69 |
Class Inj {A B} (R : relation A) (S : relation B) (f : A → B) : Prop := |
| 70 |
inj x y : S (f x) (f y) → R x y. |
| 71 |
|
| 72 |
Class Inj2 {A B C} (R1 : relation A) (R2 : relation B) |
| 73 |
(S : relation C) (f : A → B → C) : Prop := |
| 74 |
inj2 x1 x2 y1 y2 : S (f x1 x2) (f y1 y2) → R1 x1 y1 ∧ R2 x2 y2. |
| 75 |
|
| 76 |
Global Arguments irreflexivity {_} _ {_} _ _ : assert. |
| 77 |
Global Arguments inj {_ _ _ _} _ {_} _ _ _ : assert. |
| 78 |
Global Arguments inj2 {_ _ _ _ _ _} _ {_} _ _ _ _ _: assert. |
| 79 |
|
| 80 |
Global Instance inj2_inj_1 `{Inj2 A B C R1 R2 R3 f} y : Inj R1 R3 (λ x, f x y). |
| 81 |
Proof. repeat intro; edestruct (inj2 f); eauto. Qed. |
| 82 |
Global Instance inj2_inj_2 `{Inj2 A B C R1 R2 R3 f} x : Inj R2 R3 (f x). |
| 83 |
Proof. repeat intro; edestruct (inj2 f); eauto. Qed. |
| 84 |
|
| 85 |
Notation "(∧)" := and (only parsing) : stdpp_scope. |
| 86 |
Notation "( A ∧.)" := (and A) (only parsing) : stdpp_scope. |
| 87 |
Notation "(.∧ B )" := (λ A, A ∧ B) (only parsing) : stdpp_scope. |
| 88 |
|
| 89 |
Notation "(∨)" := or (only parsing) : stdpp_scope. |
| 90 |
Notation "( A ∨.)" := (or A) (only parsing) : stdpp_scope. |
| 91 |
Notation "(.∨ B )" := (λ A, A ∨ B) (only parsing) : stdpp_scope. |
| 92 |
|
| 93 |
Notation "(↔)" := iff (only parsing) : stdpp_scope. |
| 94 |
Notation "( A ↔.)" := (iff A) (only parsing) : stdpp_scope. |
| 95 |
Notation "(.↔ B )" := (λ A, A ↔ B) (only parsing) : stdpp_scope. |
| 96 |
|
| 97 |
Global Hint Extern 0 (_ ↔ _) => reflexivity : core. |
| 98 |
Global Hint Extern 0 (_ ↔ _) => symmetry; assumption : core. |
| 99 |
|
| 100 |
Notation "(→)" := (λ A B, A → B) (only parsing) : stdpp_scope. |
| 101 |
Notation "( A →.)" := (λ B, A → B) (only parsing) : stdpp_scope. |
| 102 |
Notation "(.→ B )" := (λ A, A → B) (only parsing) : stdpp_scope. |
| 103 |
|
| 104 |
Notation "t $ r" := (t r) |
| 105 |
(at level 65, right associativity, only parsing) : stdpp_scope. |
| 106 |
Notation "($)" := (λ f x, f x) (only parsing) : stdpp_scope. |
| 107 |
Notation "(.$ x )" := (λ f, f x) (only parsing) : stdpp_scope. |
| 108 |
|
| 109 |
Infix "∘" := compose : stdpp_scope. |
| 110 |
Notation "(∘)" := compose (only parsing) : stdpp_scope. |
| 111 |
Notation "( f ∘.)" := (compose f) (only parsing) : stdpp_scope. |
| 112 |
Notation "(.∘ f )" := (λ g, compose g f) (only parsing) : stdpp_scope. |
| 113 |
(** Ensure that [simpl] unfolds [id], [compose], and [flip] when fully |
| 114 |
applied. *) |
| 115 |
Global Arguments id _ _ / : assert. |
| 116 |
Global Arguments compose _ _ _ _ _ _ / : assert. |
| 117 |
Global Arguments flip _ _ _ _ _ _ / : assert. |
| 118 |
Global Arguments const _ _ _ _ / : assert. |
| 119 |
|
| 120 |
Definition fun_map {A A' B B'} (f: A' → A) (g: B → B') (h : A → B) : A' → B' := |
| 121 |
g ∘ h ∘ f. |
| 122 |
|
| 123 |
Global Instance id_inj {A} : Inj (=) (=) (@id A). |
| 124 |
Proof. intros ??; auto. Qed. |
| 125 |
Global Instance compose_inj {A B C} R1 R2 R3 (f : A → B) (g : B → C) : |
| 126 |
Inj R1 R2 f → Inj R2 R3 g → Inj R1 R3 (g ∘ f). |
| 127 |
Proof. red; intuition. Qed. |
| 128 |
|
| 129 |
(** ** Products *) |
| 130 |
Notation "( x ,.)" := (pair x) (only parsing) : stdpp_scope. |
| 131 |
Notation "(., y )" := (λ x, (x,y)) (only parsing) : stdpp_scope. |
| 132 |
|
| 133 |
Notation "p .1" := (fst p) (at level 1, left associativity, format "p .1"). |
| 134 |
Notation "p .2" := (snd p) (at level 1, left associativity, format "p .2"). |
| 135 |
|
| 136 |
Definition prod_map {A A' B B'} (f: A → A') (g: B → B') (p : A * B) : A' * B' := |
| 137 |
(f (p.1), g (p.2)). |
| 138 |
Global Arguments prod_map {_ _ _ _} _ _ !_ / : assert. |
| 139 |
|
| 140 |
Global Instance pair_inj {A B} : Inj2 (=) (=) (=) (@pair A B). |
| 141 |
Proof. injection 1; auto. Qed. |
| 142 |
Global Instance prod_map_inj {A A' B B'} (f : A → A') (g : B → B') : |
| 143 |
Inj (=) (=) f → Inj (=) (=) g → Inj (=) (=) (prod_map f g). |
| 144 |
Proof. |
| 145 |
intros ?? [??] [??] ?; simpl in *; f_equal; |
| 146 |
[apply (inj f)|apply (inj g)]; congruence. |
| 147 |
Qed. |
| 148 |
|
| 149 |
Definition prod_relation {A B} (R1 : relation A) (R2 : relation B) : |
| 150 |
relation (A * B) := λ x y, R1 (x.1) (y.1) ∧ R2 (x.2) (y.2). |
| 151 |
|
| 152 |
Section prod_relation. |
| 153 |
Context `{RA : relation A, RB : relation B}. |
| 154 |
Global Instance pair_inj' : Inj2 RA RB (prod_relation RA RB) pair. |
| 155 |
Proof. inversion_clear 1; eauto. Qed. |
| 156 |
End prod_relation. |
| 157 |
|
| 158 |
Global Instance prod_equiv `{Equiv A,Equiv B} : Equiv (A * B) := |
| 159 |
prod_relation (≡) (≡). |
| 160 |
|
| 161 |
Section prod_setoid. |
| 162 |
Context `{Equiv A, Equiv B}. |
| 163 |
Global Instance pair_equiv_inj : Inj2 (≡) (≡) (≡@{A*B}) pair := _. |
| 164 |
End prod_setoid. |
| 165 |
|
| 166 |
(* Typeclasses Opaque prod_equiv. *) |
| 167 |
|
| 168 |
(** ** Sums *) |
| 169 |
Definition sum_map {A A' B B'} (f: A → A') (g: B → B') (xy : A + B) : A' + B' := |
| 170 |
match xy with inl x => inl (f x) | inr y => inr (g y) end. |
| 171 |
Global Arguments sum_map {_ _ _ _} _ _ !_ / : assert. |
| 172 |
|
| 173 |
Global Instance inl_inj {A B} : Inj (=) (=) (@inl A B). |
| 174 |
Proof. injection 1; auto. Qed. |
| 175 |
Global Instance inr_inj {A B} : Inj (=) (=) (@inr A B). |
| 176 |
Proof. injection 1; auto. Qed. |
| 177 |
|
| 178 |
Global Instance sum_map_inj {A A' B B'} (f : A → A') (g : B → B') : |
| 179 |
Inj (=) (=) f → Inj (=) (=) g → Inj (=) (=) (sum_map f g). |
| 180 |
Proof. intros ?? [?|?] [?|?] [=]; f_equal; apply (inj _); auto. Qed. |
| 181 |
|
| 182 |
Inductive sum_relation {A B} |
| 183 |
(RA : relation A) (RB : relation B) : relation (A + B) := |
| 184 |
| inl_related x1 x2 : RA x1 x2 → sum_relation RA RB (inl x1) (inl x2) |
| 185 |
| inr_related y1 y2 : RB y1 y2 → sum_relation RA RB (inr y1) (inr y2). |
| 186 |
|
| 187 |
Section sum_relation. |
| 188 |
Context `{RA : relation A, RB : relation B}. |
| 189 |
Global Instance inl_inj' : Inj RA (sum_relation RA RB) inl. |
| 190 |
Proof. inversion_clear 1; auto. Qed. |
| 191 |
Global Instance inr_inj' : Inj RB (sum_relation RA RB) inr. |
| 192 |
Proof. inversion_clear 1; auto. Qed. |
| 193 |
End sum_relation. |
| 194 |
|
| 195 |
Global Instance sum_equiv `{Equiv A, Equiv B} : Equiv (A + B) := sum_relation (≡) (≡). |
| 196 |
|
| 197 |
Global Instance inl_equiv_inj `{Equiv A, Equiv B} : Inj (≡) (≡) (@inl A B) := _. |
| 198 |
Global Instance inr_equiv_inj `{Equiv A, Equiv B} : Inj (≡) (≡) (@inr A B) := _. |
| 199 |
|
| 200 |
Notation "` x" := (proj1_sig x) (at level 10, format "` x") : stdpp_scope. |
| 201 |
|
| 202 |
Definition f := Nat.add 0. |
| 203 |
Global Instance h: Inj eq eq f. |
| 204 |
unfold f. simpl. easy. |
| 205 |
Qed. |
| 206 |
|
| 207 |
Goal Inj eq eq (compose (@id nat) id). |
| 208 |
apply _. |
| 209 |
Qed. |
| 210 |
|
| 211 |
Goal Inj eq eq (compose (compose (@id nat) id) id). |
| 212 |
apply _. |
| 213 |
Qed. |
| 214 |
|
| 215 |
(* Goal Inj eq eq (fun (x:nat) => id (id x)). |
| 216 |
apply _. |
| 217 |
Qed. *) |
| 218 |
|
| 219 |
Goal Inj eq eq (fun (x: nat) => (compose id id) (id x)). |
| 220 |
apply (compose_inj eq eq); apply _. |
| 221 |
Qed. |