| 1 |
Require Import TestSuite.admit. |
| 2 |
Require Import Setoid. |
| 3 |
|
| 4 |
Parameter A : Set. |
| 5 |
|
| 6 |
Axiom eq_dec : forall a b : A, {a = b} + {a <> b}. |
| 7 |
|
| 8 |
Inductive set : Set := |
| 9 |
| Empty : set |
| 10 |
| Add : A -> set -> set. |
| 11 |
|
| 12 |
Fixpoint In (a : A) (s : set) {struct s} : Prop := |
| 13 |
match s with |
| 14 |
| Empty => False |
| 15 |
| Add b s' => a = b \/ In a s' |
| 16 |
end. |
| 17 |
|
| 18 |
Definition same (s t : set) : Prop := forall a : A, In a s <-> In a t. |
| 19 |
|
| 20 |
Lemma setoid_set : Setoid_Theory set same. |
| 21 |
|
| 22 |
unfold same; split ; red. |
| 23 |
red; auto. |
| 24 |
|
| 25 |
red. |
| 26 |
intros. |
| 27 |
elim (H a); auto. |
| 28 |
|
| 29 |
intros. |
| 30 |
elim (H a); elim (H0 a). |
| 31 |
split; auto. |
| 32 |
Qed. |
| 33 |
|
| 34 |
Add Setoid set same setoid_set as setsetoid. |
| 35 |
|
| 36 |
Add Morphism In with signature (eq ==> same ==> iff) as In_ext. |
| 37 |
Proof. |
| 38 |
unfold same; intros a s t H; elim (H a); auto. |
| 39 |
Qed. |
| 40 |
|
| 41 |
Lemma add_aux : |
| 42 |
forall s t : set, |
| 43 |
same s t -> forall a b : A, In a (Add b s) -> In a (Add b t). |
| 44 |
unfold same; simple induction 2; intros. |
| 45 |
rewrite H1. |
| 46 |
simpl; left; reflexivity. |
| 47 |
|
| 48 |
elim (H a). |
| 49 |
intros. |
| 50 |
simpl; right. |
| 51 |
apply (H2 H1). |
| 52 |
Qed. |
| 53 |
|
| 54 |
Add Morphism Add with signature (eq ==> same ==> same) as Add_ext. |
| 55 |
split; apply add_aux. |
| 56 |
assumption. |
| 57 |
rewrite H. |
| 58 |
reflexivity. |
| 59 |
Qed. |
| 60 |
|
| 61 |
Fixpoint remove (a : A) (s : set) {struct s} : set := |
| 62 |
match s with |
| 63 |
| Empty => Empty |
| 64 |
| Add b t => |
| 65 |
match eq_dec a b with |
| 66 |
| left _ => remove a t |
| 67 |
| right _ => Add b (remove a t) |
| 68 |
end |
| 69 |
end. |
| 70 |
|
| 71 |
Lemma in_rem_not : forall (a : A) (s : set), ~ In a (remove a (Add a Empty)). |
| 72 |
|
| 73 |
intros. |
| 74 |
setoid_replace (remove a (Add a Empty)) with Empty. |
| 75 |
|
| 76 |
auto. |
| 77 |
|
| 78 |
unfold same. |
| 79 |
split. |
| 80 |
simpl. |
| 81 |
case (eq_dec a a). |
| 82 |
intros e ff; elim ff. |
| 83 |
|
| 84 |
intros; absurd (a = a); trivial. |
| 85 |
|
| 86 |
simpl. |
| 87 |
intro H; elim H. |
| 88 |
Qed. |
| 89 |
|
| 90 |
Parameter P : set -> Prop. |
| 91 |
Parameter P_ext : forall s t : set, same s t -> P s -> P t. |
| 92 |
|
| 93 |
Add Morphism P with signature (same ==> iff) as P_extt. |
| 94 |
intros; split; apply P_ext; (assumption || apply (Seq_sym _ _ setoid_set); assumption). |
| 95 |
Qed. |
| 96 |
|
| 97 |
Lemma test_rewrite : |
| 98 |
forall (a : A) (s t : set), same s t -> P (Add a s) -> P (Add a t). |
| 99 |
intros. |
| 100 |
rewrite <- H. |
| 101 |
rewrite H. |
| 102 |
setoid_rewrite <- H. |
| 103 |
setoid_rewrite H. |
| 104 |
setoid_rewrite <- H. |
| 105 |
trivial. |
| 106 |
Qed. |
| 107 |
|
| 108 |
(* Unifying the domain up to delta-conversion (example from emakarov) *) |
| 109 |
|
| 110 |
Definition id: Set -> Set := fun A => A. |
| 111 |
Definition rel : forall A : Set, relation (id A) := @eq. |
| 112 |
Definition f: forall A : Set, A -> A := fun A x => x. |
| 113 |
|
| 114 |
Add Relation (id A) (rel A) as eq_rel. |
| 115 |
|
| 116 |
Add Morphism (@f A) with signature (eq ==> eq) as f_morph. |
| 117 |
Proof. |
| 118 |
unfold rel, f. trivial. |
| 119 |
Qed. |
| 120 |
|
| 121 |
(* Submitted by Nicolas Tabareau *) |
| 122 |
(* Needs unification.ml to support environments with de Bruijn *) |
| 123 |
|
| 124 |
Goal forall |
| 125 |
(f : Prop -> Prop) |
| 126 |
(Q : (nat -> Prop) -> Prop) |
| 127 |
(H : forall (h : nat -> Prop), Q (fun x : nat => f (h x)) <-> True) |
| 128 |
(h:nat -> Prop), |
| 129 |
Q (fun x : nat => f (Q (fun b : nat => f (h x)))) <-> True. |
| 130 |
intros f0 Q H. |
| 131 |
setoid_rewrite H. |
| 132 |
tauto. |
| 133 |
Qed. |
| 134 |
|
| 135 |
(** Check proper refreshing of the lemma application for multiple |
| 136 |
different instances in a single setoid rewrite. *) |
| 137 |
|
| 138 |
Section mult. |
| 139 |
Context (fold : forall {A} {B}, (A -> B) -> A -> B). |
| 140 |
Context (add : forall A, A -> A). |
| 141 |
Context (fold_lemma : forall {A B f} {eqA : relation B} x, eqA (fold A B f (add A x)) (fold _ _ f x)). |
| 142 |
Context (ab : forall B, A -> B). |
| 143 |
Context (anat : forall A, nat -> A). |
| 144 |
|
| 145 |
Goal forall x, (fold _ _ (fun x => ab A x) (add A x) = anat _ (fold _ _ (ab nat) (add _ x))). |
| 146 |
Proof. intros. |
| 147 |
setoid_rewrite fold_lemma. |
| 148 |
change (fold A A (fun x0 : A => ab A x0) x = anat A (fold A nat (ab nat) x)). |
| 149 |
Abort. |
| 150 |
|
| 151 |
End mult. |
| 152 |
|
| 153 |
(** Current semantics for rewriting with typeclass constraints in the lemma |
| 154 |
does not fix the instance at the first unification, use [at], or simply rewrite for |
| 155 |
this semantics. *) |
| 156 |
|
| 157 |
Parameter beq_nat : forall x y : nat, bool. |
| 158 |
|
| 159 |
Class Foo (A : Type) := {foo_neg : A -> A ; foo_prf : forall x : A, x = foo_neg x}. |
| 160 |
#[export] Instance: Foo nat. admit. Defined. |
| 161 |
#[export] Instance: Foo bool. admit. Defined. |
| 162 |
|
| 163 |
Goal forall (x : nat) (y : bool), beq_nat (foo_neg x) 0 = foo_neg y. |
| 164 |
Proof. intros. setoid_rewrite <- foo_prf. change (beq_nat x 0 = y). Abort. |
| 165 |
|
| 166 |
Goal forall (x : nat) (y : bool), beq_nat (foo_neg x) 0 = foo_neg y. |
| 167 |
Proof. intros. setoid_rewrite <- @foo_prf at 1. change (beq_nat x 0 = foo_neg y). Abort. |
| 168 |
|
| 169 |
(* This should not raise an anomaly as it did for some time in early 2016 *) |
| 170 |
|
| 171 |
Definition t := nat -> bool. |
| 172 |
Definition h (a b : t) := forall n, a n = b n. |
| 173 |
|
| 174 |
#[export] Instance subrelh : subrelation h (Morphisms.pointwise_relation nat eq). |
| 175 |
Proof. intros x y H; assumption. Qed. |
| 176 |
|
| 177 |
Goal forall a b, h a b -> a 0 = b 0. |
| 178 |
intros. |
| 179 |
setoid_rewrite H. (* Fallback on ordinary rewrite without anomaly *) |
| 180 |
reflexivity. |
| 181 |
Qed. |
| 182 |
|
| 183 |
Module InType. |
| 184 |
Require Import CRelationClasses CMorphisms. |
| 185 |
|
| 186 |
Inductive All {A : Type} (P : A -> Type) : list A -> Type := |
| 187 |
| All_nil : All P nil |
| 188 |
| All_cons x (px : P x) xs (pxs : All P xs) : All P (x :: xs). |
| 189 |
|
| 190 |
Lemma All_impl {A} (P Q : A -> Type) l : (forall x, P x -> Q x) -> All P l -> All Q l. |
| 191 |
Proof. |
| 192 |
intros HP. induction 1; constructor; eauto. |
| 193 |
Qed. |
| 194 |
|
| 195 |
Axiom add_0_r_peq : forall x : nat, eq (x + 0)%nat x. |
| 196 |
|
| 197 |
#[export] Instance All_proper {A} : |
| 198 |
CMorphisms.Proper ((pointwise_relation A iffT) ==> eq ==> iffT) All. |
| 199 |
Proof. |
| 200 |
intros f g Hfg x y e. destruct e. split; apply All_impl, Hfg. |
| 201 |
Qed. |
| 202 |
|
| 203 |
Lemma rewrite_all {l : list nat} (Q : nat -> Type) : |
| 204 |
All (fun x => Q x) l -> |
| 205 |
All (fun x => Q (x + 0)) l. |
| 206 |
Proof. |
| 207 |
intros a. |
| 208 |
setoid_rewrite add_0_r_peq. |
| 209 |
exact a. |
| 210 |
Qed. |
| 211 |
|
| 212 |
Lemma rewrite_all_in {l : list nat} (Q : nat -> Type) : |
| 213 |
All (fun x => Q (x + 0)) l -> |
| 214 |
All (fun x => Q x) l. |
| 215 |
Proof. |
| 216 |
intros a. |
| 217 |
setoid_rewrite add_0_r_peq in a. |
| 218 |
exact a. |
| 219 |
Qed. |
| 220 |
|
| 221 |
Lemma rewrite_all_in2 {l : list nat} (Q : nat -> Type) (R : nat -> Type) : |
| 222 |
All (fun x => prod (Q (x + 0)%nat) (R x))%type l -> |
| 223 |
All (fun x => prod (Q x) (R x))%type l. |
| 224 |
Proof. |
| 225 |
intros a. |
| 226 |
setoid_rewrite add_0_r_peq in a. |
| 227 |
exact a. |
| 228 |
Qed. |
| 229 |
End InType. |
| 230 |
|
| 231 |
Module Polymorphism. |
| 232 |
Require Import CRelationClasses CMorphisms. |
| 233 |
|
| 234 |
#[universes(polymorphic, cumulative)] |
| 235 |
Inductive plist@{i} (A : Type@{i}) : Type@{i} := |
| 236 |
| pnil : plist A |
| 237 |
| pcons : A -> plist A -> plist A. |
| 238 |
Arguments pnil {A}. |
| 239 |
Arguments pcons {A}. |
| 240 |
#[universes(polymorphic, cumulative)] |
| 241 |
Record pprod@{i j} (A : Type@{i}) (B : Type@{j}) : Type@{max(i, j)} := |
| 242 |
{ pfst : A; |
| 243 |
psnd : B }. |
| 244 |
Arguments pfst {A B}. |
| 245 |
Arguments psnd {A B}. |
| 246 |
|
| 247 |
Notation "x :: xs" := (pcons x xs). |
| 248 |
|
| 249 |
#[universes(polymorphic)] |
| 250 |
Fixpoint All@{i j} {A : Type@{i}} (P : A -> Type@{j}) (l : plist A) : Type@{j} := |
| 251 |
match l with |
| 252 |
| pnil => unit |
| 253 |
| x :: xs => pprod (P x) (All P xs) |
| 254 |
end. |
| 255 |
(* |
| 256 |
#[universes(polymorphic, cumulative)] |
| 257 |
Inductive All {A : Type} (P : A -> Type) : list A -> Type := |
| 258 |
| All_nil : All P nil |
| 259 |
| All_cons x (px : P x) xs (pxs : All P xs) : All P (x :: xs). *) |
| 260 |
|
| 261 |
#[universes(polymorphic)] |
| 262 |
Lemma All_impl {A} (P Q : A -> Type) l : (forall x, P x -> Q x) -> All P l -> All Q l. |
| 263 |
Proof. |
| 264 |
intros HP. |
| 265 |
induction l; [intros|intros []]; constructor; eauto. |
| 266 |
Qed. |
| 267 |
Check pointwise_relation. |
| 268 |
|
| 269 |
#[universes(polymorphic)] |
| 270 |
Inductive peq@{i} (A : Type@{i}) (a : A) : A -> Type@{i} := |
| 271 |
peq_refl : peq A a a. |
| 272 |
|
| 273 |
Arguments peq {A}. |
| 274 |
Arguments peq_refl {A a}. |
| 275 |
|
| 276 |
#[universes(polymorphic)] |
| 277 |
Axiom add_0_r_peq : forall x : nat, peq (x + 0)%nat x. |
| 278 |
|
| 279 |
#[universes(polymorphic), export] |
| 280 |
Instance peq_left {A : Type} {B : Type} {R : crelation B} (f : A -> B) `{Reflexive B R} : Proper (peq ==> R) f. |
| 281 |
Admitted. |
| 282 |
|
| 283 |
#[export] Instance reflexive_eq_dom_reflexive@{i j jr mij mijr} {A : Type@{i}} {B : Type@{j}} (R : crelation@{j jr} B) : |
| 284 |
Reflexive@{j jr} R -> |
| 285 |
Reflexive@{mij mijr} (@peq A ==> R)%signatureT. |
| 286 |
Proof. |
| 287 |
intros hr x ? ? e. destruct e. apply hr. |
| 288 |
Qed. |
| 289 |
|
| 290 |
#[universes(polymorphic), export] |
| 291 |
Instance All_proper {A} : |
| 292 |
CMorphisms.Proper ((pointwise_relation A iffT) ==> peq ==> iffT) All. |
| 293 |
Proof. |
| 294 |
intros f g Hfg x y e. destruct e. split; apply All_impl, Hfg. |
| 295 |
Qed. |
| 296 |
|
| 297 |
#[universes(polymorphic), export] |
| 298 |
Instance eq_proper_proxy@{i} {A : Type@{i}} (x : A) : ProperProxy@{i i} peq x. |
| 299 |
Proof. red. exact peq_refl. Defined. |
| 300 |
|
| 301 |
#[universes(polymorphic), export] |
| 302 |
Instance peq_equiv {A} : Equivalence (@peq A). |
| 303 |
Proof. |
| 304 |
split. |
| 305 |
Admitted. |
| 306 |
|
| 307 |
Lemma rewrite_all {l : plist nat} (Q : nat -> Type) : |
| 308 |
All (fun x => Q x) l -> |
| 309 |
All (fun x => Q (x + 0)) l. |
| 310 |
Proof. |
| 311 |
intros a. |
| 312 |
setoid_rewrite add_0_r_peq. |
| 313 |
exact a. |
| 314 |
Qed. |
| 315 |
|
| 316 |
Lemma rewrite_all_in {l : plist nat} (Q : nat -> Type) : |
| 317 |
All (fun x => Q (x + 0)) l -> |
| 318 |
All (fun x => Q x) l. |
| 319 |
Proof. |
| 320 |
intros a. Show Universes. |
| 321 |
setoid_rewrite add_0_r_peq in a. |
| 322 |
exact a. |
| 323 |
Qed. |
| 324 |
|
| 325 |
Lemma rewrite_all_in2 {l : plist nat} (Q : nat -> Type) (R : nat -> Type) : |
| 326 |
All (fun x => pprod (Q (x + 0)%nat) (R x))%type l -> |
| 327 |
All (fun x => pprod (Q x) (R x))%type l. |
| 328 |
Proof. |
| 329 |
intros a. |
| 330 |
setoid_rewrite add_0_r_peq in a. |
| 331 |
exact a. |
| 332 |
Qed. |
| 333 |
End Polymorphism. |