Mise à jour terminée. Pour connaître les apports de la version 13.8.4 par rapport à notre ancienne version vous pouvez lire les "Release Notes" suivantes :
https://about.gitlab.com/releases/2021/02/11/security-release-gitlab-13-8-4-released/
https://about.gitlab.com/releases/2021/02/05/gitlab-13-8-3-released/

Shared.v 12.7 KB
Newer Older
charguer's avatar
init  
charguer committed
1 2
Set Implicit Arguments.

charguer's avatar
polylet  
charguer committed
3

charguer's avatar
charguer committed
4 5 6 7 8 9 10 11 12 13

(********************************************************************)
(* TODO: move to libtactics *)

Ltac is_not_evar E :=
  first [ is_evar E; fail 1
        | idtac ].



charguer's avatar
polylet  
charguer committed
14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48
(********************************************************************)
(** Notation for functions expecting tuples as arguments *)

(** Note: this will later move to TLC *)

Notation "'fun' ''' ( x1 , x2 ) ':' T '=>' E" := 
  (fun p : T => let '(x1,x2) := p in E) 
  (at level 200) : fun_scope.

Notation "'fun' ''' ( x1 , x2 ) '=>' E" := 
  (fun p => let '(x1,x2) := p in E) 
  (at level 200, format "'fun'  ''' ( x1 , x2 )  '=>'  E") : fun_scope.

Notation "'fun' ''' ( x1 , x2 , x3 ) ':' T '=>' E" := 
  (fun p : T => let '(x1,x2,x3) := p in E) 
  (at level 200) : fun_scope.

Notation "'fun' ''' ( x1 , x2 , x3 ) '=>' E" := 
  (fun p => let '(x1,x2,x3) := p in E) 
  (at level 200, format "'fun'  ''' ( x1 , x2 , x3 )  '=>'  E") : fun_scope.

(* TODO: coqbug?
Notation "'fun' ''' ( x1 , x2 , x3 , x4) ':' T '=>' E" := 
  (fun p : T => let '(x1,x2,x3,x4) := p in E) 
  (at level 60, E at level 200) : fun_scope.

Notation "'fun' ''' ( x1 , x2 , x3 , x4 ) '=>' E" := 
  (fun p => match p with (x1,x2,x3,x4) => E end) 
  (at level 60, E at level 200, format "'fun'  ''' ( x1 , x2 , x3 , x4 )  '=>'  E") : fun_scope.
*)

Open Scope fun_scope.



charguer's avatar
init  
charguer committed
49 50 51 52 53 54 55 56 57
(********************************************************************)
(** Treatment of partially-applied equality *)

Require Import LibTactics LibCore LibEpsilon.

Hint Unfold pred_le.

Tactic Notation "false" "~" constr(E) := 
  false E; instantiate; auto_tilde. 
charguer's avatar
charguer committed
58
  (* todo: to work around a coq bug; still needed? *)
charguer's avatar
init  
charguer committed
59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94


(********************************************************************)
(** Treatment of partially-applied equality *)

Lemma if_eq_1 : forall A (x:bool) (v1 v2 y : A), 
  ((if x then = v1 else = v2) y) -> (y = (if x then v1 else v2)).
Proof. tautob~. Qed.

Lemma if_eq_2 : forall A (x:bool) (v1 v2 y : A), 
  ((if x then eq v1 else = v2) y) -> (y = (if x then v1 else v2)).
Proof. tautob~. Qed.

Lemma if_eq_3 : forall A (x:bool) (v1 v2 y : A), 
  ((if x then = v1 else eq v2) y) -> (y = (if x then v1 else v2)).
Proof. tautob~. Qed.

Lemma if_eq_4 : forall A (x:bool) (v1 v2 y : A), 
  ((if x then eq v1 else eq v2) y) -> (y = (if x then v1 else v2)).
Proof. tautob~. Qed.

Tactic Notation "if_eq" "in" hyp(H) :=
  let go L := apply L in H in
  first [ go if_eq_1 | go if_eq_2 | go if_eq_3 | go if_eq_4 ].

Tactic Notation "if_eq" :=
  repeat match goal with H: ((if _ then _ else _) _) |- _ => if_eq in H end.

Ltac calc_partial_eq tt :=
  repeat match goal with
  | H: (= _) _ |- _ => simpl in H 
  | H: ((if _ then _ else _) _) |- _ => if_eq in H
  end.


(************************************************************)
charguer's avatar
charguer committed
95 96 97
(* * Tactic [substs] for substitution of variables *)

(* todo: is "substs" still needed? *)
charguer's avatar
init  
charguer committed
98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144

(* todo: check not too slow *)

Ltac substs_core :=
  let go x y := first [ subst x | subst y ] in
  match goal with 
  | H: ?x = ?y |- _ => go x y
  | H: (= ?x) ?y |- _ => simpl in H; go x y
  end.

Ltac injects_core :=
  match goal with 
  | H: ?f _ = ?f _ |- _ => injects H
  | H: ?f _ _ = ?f _ _ |- _ => injects H
  | H: ?f _ _ _ = ?f _ _ _ |- _ => injects H
  | H: ?f _ _ _ _ = ?f _ _ _ _ |- _ => injects H 
  end.

Ltac substs_base :=
  try fold_bool; calc_partial_eq tt; repeat substs_core;
  try injects_core;
  try injects_core;
  try injects_core;
  try injects_core;
  try injects_core. (* temporary: to avoid loops *)

Tactic Notation "substs" := substs_base.

Tactic Notation "substs" constr(z) := 
  match goal with 
  | H: z = _ |- _ => subst z
  | H: (= _) z |- _ => hnf in H; subst z
  end.

Ltac subst_hyp_core H :=
  match type of H with 
  | ?x = ?y => first [ subst x | subst y ] 
  end.

Ltac subst_hyp_base H :=
  match type of H with 
  | ?x = ?y => first [ subst x | subst y ] 
  | istrue (isTrue (?x = ?y)) => apply istrue_isTrue_forw in H; first [ subst x | subst y ] 
  | istrue ?b => apply eq_true_r_back in H; subst_hyp_core H
  | istrue (! ?b) => apply eq_false_r_back in H; subst_hyp_core H
  end.

charguer's avatar
charguer committed
145 146 147 148

(************************************************************)
(* * Tactic [subst_hyp] for substitution of hypotheses *)

charguer's avatar
init  
charguer committed
149 150 151 152 153 154 155 156 157 158 159 160 161
Tactic Notation "subst_hyp" hyp(H) :=
  subst_hyp_base H.

Lemma demo_subst_hyp_1 : forall (x y : int),
  x '= y -> y '= x.
Proof. introv H. subst_hyp H. fold_prop. auto. Qed.

Lemma demo_subst_hyp_2 : forall (x y : bool),
  x -> !y -> x <> y.
Proof. introv H1 H2. subst_hyp H1. subst_hyp H2. auto. Qed.


(************************************************************)
charguer's avatar
charguer committed
162
(* * TODO: case_If: should be deprecated *)
charguer's avatar
init  
charguer committed
163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350

Ltac apply_to_If cont :=
  match goal with 
  | |- context [If ?B then _ else _] => cont B
  | K: context [If ?B then _ else _] |- _ => cont B
  end.

Ltac case_If_core B I1 I2:=
  let H1 := fresh "TEMP" in let H2 := fresh "TEMP" in
  destruct (classicT B) as [H1|H2]; 
  [ tryfalse; rew_logic in H1; revert H1; intros I1; tryfalse
  | tryfalse; rew_logic in H2; revert H2; intros I2; tryfalse ].

Tactic Notation "case_If" "as" simple_intropattern(I1) simple_intropattern(I2) :=
  apply_to_If ltac:(fun B => case_If_core B I1 I2).

Tactic Notation "case_If" "as" simple_intropattern(I) :=
  apply_to_If ltac:(fun B => case_If_core B I I).

Tactic Notation "case_If" :=
  let C := fresh "C" in case_If as C.

(* todo:
   case_if => handles If
   case_if_sym => handles If, symmetric equality
   cases_if => case_if + subst
   cases_if_sym => case_if + subst sym
*)


(**************************************************)
(** Tag for preventing reductions *)

Definition blocker_def (A:Type) (X:A) : { Y:A | Y = X }.
Proof. constructors. eauto. Qed.

Definition blocker (A:Type) (X:A) := proj1_sig (blocker_def X).

Implicit Arguments blocker [ A ].

Definition blocker_eq : forall (A:Type) (X:A), blocker X = X.
Proof. intros. unfold blocker. apply (proj2_sig (blocker_def X)). Qed.

Ltac unblock_base C :=
  match goal with |- appcontext [ blocker (?U) ] =>  
    match U with 
    | C ?X1 => idtac 
    | C ?X1 ?X2 => idtac  
    | C ?X1 ?X2 ?X3 => idtac  
    | C ?X1 ?X2 ?X3 ?X4 => idtac  
    | C ?X1 ?X2 ?X3 ?X4 ?X5 => idtac  
    | C ?X1 ?X2 ?X3 ?X4 ?X5 ?X6 => idtac  
    end;
  replace (blocker U) with U ; [ | instantiate; rewrite blocker_eq; auto ]
  end.

Tactic Notation "unblock" constr(C) :=
  unblock_base C.

Tactic Notation "unblock" := 
  repeat rewrite blocker_eq.

Notation "'%' X" := (@blocker _ X) (at level 1) : blocker_scope.
Open Scope blocker_scope.


(************************************************************)
(* * Predicate for post-conditions on boolean values *)
(* todo: DEPRECATED *)

Definition bool_of (P:Prop) :=
   fun b => ((istrue b) = P).

Require Import LibEpsilon.
Section BoolOf.
Variables (b:bool) (P:Prop).

Lemma bool_of_true : bool_of P b -> b -> P.
Proof. unfold bool_of. intros. subst~. Qed.

Lemma bool_of_false : bool_of P b -> !b -> ~ P.
Proof. unfold bool_of. intros. subst~. destruct~ b. Qed.

Lemma bool_of_true_back : b -> bool_of P b -> P.
Proof. unfold bool_of. intros. subst~. Qed.

Lemma bool_of_false_back : !b -> bool_of P b -> ~ P.
Proof. unfold bool_of. intros. subst~. destruct~ b. Qed.

Lemma bool_of_true_in : bool_of P true -> P.
Proof. unfold bool_of. intros. subst~. Qed.

Lemma bool_of_false_in : bool_of P false -> ~ P.
Proof. unfold bool_of. intros. subst~. Qed.

Lemma bool_of_true_in_forw : P -> bool_of P true.
Proof. intros. hnf. extens*. Qed.

Lemma bool_of_false_in_forw : ~ P -> bool_of P false.
Proof. intros. hnf. extens; auto_false*. Qed.

Lemma bool_of_True : bool_of P b -> P -> b.
Proof. unfold bool_of. intros. subst~. Qed.

Lemma bool_of_False : bool_of P b -> ~ P -> !b.
Proof. unfold bool_of. intros. subst~. rew_reflect~. Qed.

Lemma bool_of_prove : (b <-> P) -> bool_of P b.
Proof. intros. extens*. Qed.

(* todo: add  isTrue true = P to fold_prop *)
End BoolOf.

Lemma bool_of_eq : forall (P Q : Prop), 
  (P <-> Q) -> ((bool_of P) = (bool_of Q)).
Proof. 
  intros. apply prop_ext_1. intros_all. unfold bool_of;
  iff; rewrite H0; apply* prop_ext. 
Qed.

Lemma elim_istrue_true : forall (b:bool) (P:Prop), 
  b -> (istrue b = P) -> P.
Proof. intros. subst~. Qed.

Lemma elim_istrue_false : forall (b:bool) (P:Prop), 
  !b -> (istrue b = P) -> ~ P.
Proof. intros_all. subst~. destruct b; simpls; false. Qed.

Lemma bool_of_impl : forall (P Q : Prop) x, 
  bool_of P x -> (P <-> Q) -> bool_of Q x.
Proof. unfold bool_of. intros. subst. extens*. Qed.

Lemma bool_of_impl_neg : forall (P Q : Prop) x, 
  bool_of P x -> (~P <-> Q) -> bool_of Q (!x).
Proof. unfold bool_of. intros. subst. extens. rew_reflect*. Qed.

Lemma bool_of_neg_impl : forall (P Q : Prop) x, 
  bool_of P (!x) -> (~P <-> Q) -> bool_of Q x.
Proof.
  unfold bool_of. introv M K. subst. extens.
  rew_reflect in K. rew_logic in K. auto.
Qed.

Lemma pred_le_bool_of : forall (P Q : Prop), 
  (P <-> Q) -> (pred_le (bool_of P) (bool_of Q)).
Proof. unfold bool_of; intros_all. rewrite H0. apply~ prop_ext. Qed.

(** Tactics for normalizing hypotheses *)

Lemma true_eq_P : forall (P:Prop),
  (istrue true = P) = P.
Proof. intros. apply prop_ext. iff. subst~. apply* prop_ext. Qed.
Hint Rewrite true_eq_P : rew_reflect.  

Hint Rewrite isTrue_istrue istrue_isTrue : rew_istrue.
Ltac rew_istrue := autorewrite with rew_istrue.

Ltac fix_bool_of_known tt := 
  match goal with 
  | H: bool_of ?P true |- _ => 
     applys_to H bool_of_true_in
  | H: bool_of ?P false |- _ => 
     applys_to H bool_of_false_in
  | H: bool_of ?P ?b, Hb: isTrue ?b |- _ => 
     applys_to H (@bool_of_true_back b P Hb); clear Hb
  | H: bool_of ?P ?b, Hb: isTrue (! ?b) |- _ => 
     applys_to H (@bool_of_false_back b P Hb); clear Hb 
  | |- bool_of ?P true => 
     apply bool_of_true_in_forw
  | |- bool_of ?P false => 
     apply bool_of_false_in_forw
  | |- bool_of ?P ?b =>
     first [ apply refl_equal 
           | apply bool_of_prove; 
             try (check_noevar_goal; rew_istrue) ]
  end.

Tactic Notation "boolof" := fix_bool_of_known tt.
Tactic Notation "boolofs" := subst; fix_bool_of_known tt.
Tactic Notation "boolof" "*" := boolof; auto_star.
Tactic Notation "boolofs" "*" := boolofs; auto_star.




(********************************************************************)
(* ** Clean boolean reflection and partially applied equality *)

charguer's avatar
charguer committed
351 352 353
(** [reflect_clean tt] normalizes partially-applied equality
    and calls the tactic [logics], which normalizes bool/prop
    coercions. *)
charguer's avatar
init  
charguer committed
354 355

Ltac reflect_clean tt :=
charguer's avatar
charguer committed
356
  calc_partial_eq tt; logics. 
charguer's avatar
init  
charguer committed
357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410



(********************************************************************)
(* ** Case analysis after pattern matching *)

(* todo: move to CFTactics *)

Ltac invert_first_hyp :=
  let H := get_last_hyp tt in inverts H.

Ltac invert_first_hyp ::=
  let H := get_last_hyp tt in symmetry in H; inverts H.


(********************************************************************)
(* ** Predicate weakening *)

Notation "P ==> Q" := (pred_le P Q) 
  (at level 55, right associativity) : func.

Open Scope func.

Hint Resolve pred_le_refl.

Lemma weaken_bool_of : forall (P Q : Prop), 
  (P <-> Q) -> ((bool_of P) ==> (bool_of Q)).
Proof. unfold bool_of. intros_all. rewrite H0. extens*. Qed.

Notation "P ===> Q" := (rel_le P Q) 
  (at level 55, right associativity) : func.

Open Scope func.

Lemma pred_le_extens : forall A (H1 H2 : A->Prop),
  H1 ==> H2 -> H2 ==> H1 -> H1 = H2.
Proof. intros. extens*. Qed.

Lemma pred_le_proj1 : forall A (H1 H2 : A->Prop),
  H1 = H2 -> H1 ==> H2.
Proof. intros. subst~. Qed.

Lemma pred_le_proj2 : forall A (H1 H2 : A->Prop),
  H1 = H2 -> H2 ==> H1.
Proof. intros. subst~. Qed.

Implicit Arguments pred_le_proj1 [A H1 H2].
Implicit Arguments pred_le_proj2 [A H1 H2].
Implicit Arguments pred_le_extens [A H1 H2].


(********************************************************************)
(* Hints *)

charguer's avatar
charguer committed
411
Hint Constructors Mem. (* todo: should be put in the developments *)
charguer's avatar
init  
charguer committed
412 413
Hint Resolve rel_le_refl.

charguer's avatar
charguer committed
414
(* todo: is this needed? *)
charguer's avatar
init  
charguer committed
415 416 417 418
Lemma not_True_to_False : ~ True -> False.
Proof. intros. rew_logic in *. auto. Qed.
Hint Immediate not_True_to_False.

charguer's avatar
charguer committed
419 420 421 422

(********************************************************************)
(* Inhabited types *)

charguer's avatar
init  
charguer committed
423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449
Hint Resolve (0%nat) : typeclass_instances.
Hint Resolve (0%Z) : typeclass_instances.
Hint Resolve @nil : typeclass_instances.
Hint Resolve true : typeclass_instances.
Hint Resolve @None : typeclass_instances.
Hint Resolve @pair : typeclass_instances.

(* todo: move *)
Lemma Inhab_intro : forall (A:Type),
  A -> Inhab A.
Proof. introv x. apply (prove_Inhab x). Qed.

Ltac inhab :=
   intros; apply Inhab_intro; intros; try solve 
    [ eauto 10 with typeclass_instances 
    | constructor; eauto 10 with typeclass_instances 
    | apply arbitrary 
    | apply @arbitrary; eauto 10 with typeclass_instances ].

Instance Z_inhab : Inhab Z.
Proof. apply (prove_Inhab 0%Z). Qed.

Hint Resolve bool_inhab.

Lemma inhab_demo : Inhab Z.
Proof. inhab. Qed.