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/

CFTactics.v 77.2 KB
Newer Older
charguer's avatar
init  
charguer committed
1
Set Implicit Arguments.
charguer's avatar
charguer committed
2
Require Export LibInt CFSpec CFPrint.
charguer's avatar
init  
charguer committed
3

charguer's avatar
demo  
charguer committed
4

charguer's avatar
init  
charguer committed
5 6 7 8 9 10 11 12 13 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 49 50 51 52 53 54 55 56 57 58 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

(********************************************************************)
(* ** Tactics *)

(*--------------------------------------------------------*)
(* ** Tools for specifications *)

(** [spec_fun_arity S] returns the function which is being
    specified or reasoned about in the term [S], together
    with its arity, as a pair [(n,f)]. The tactic [spec_fun S]
    returns only the function [f], while the tactic [spec_arity S] 
    returns only the arity [n]. *)

Ltac spec_fun_arity S :=
  match S with 
  | spec_1 _ ?f => constr:(1%nat,f)
  | spec_2 _ ?f => constr:(2%nat,f)
  | spec_3 _ ?f => constr:(3%nat,f)
  | spec_4 _ ?f => constr:(4%nat,f)
  | app_1 ?f _ _ _ => constr:(1%nat,f)
  | app_2 ?f _ _ _ _ => constr:(2%nat,f)
  | app_3 ?f _ _ _ _ _ => constr:(3%nat,f)
  | app_4 ?f _ _ _ _ _ _ => constr:(4%nat,f)
  | App ?f _; _ _ => constr:(1%nat,f)
  | App ?f _ _; _ _ => constr:(2%nat,f)
  | App ?f _ _ _; _ _ => constr:(3%nat,f)
  | App ?f _ _ _ _; _ _ => constr:(4%nat,f)
  | curried_1 _ _ ?f => constr:(1%nat,f)
  | curried_2 _ _ _ ?f => constr:(2%nat,f)
  | curried_3 _ _ _ _ ?f => constr:(3%nat,f)
  | curried_4 _ _ _ _ _ ?f => constr:(4%nat,f)
  | context [ spec_1 _ ?f ] => constr:(1%nat,f)
  | context [ spec_2 _ ?f ] => constr:(2%nat,f)
  | context [ spec_3 _ ?f ] => constr:(3%nat,f)
  | context [ spec_4 _ ?f ] => constr:(4%nat,f)
  | context [ app_1 ?f _ _ _ ] => constr:(1%nat,f)
  | context [ app_2 ?f _ _ _ _ ] => constr:(2%nat,f)
  | context [ app_3 ?f _ _ _ _ _ ] => constr:(3%nat,f)
  | context [ app_4 ?f _ _ _ _ _ _ ] => constr:(4%nat,f)
  end. 

Ltac spec_fun S :=
  match spec_fun_arity S with (_,?f) => constr:(f) end.

Ltac spec_arity S :=
  match spec_fun_arity S with (?n,_) => constr:(n) end.

(** [spec_term_arity] is similar to [spec_arity] except that
    it can perform one step of unfolding in order to get to
    a form on which [spec_arity] can succeed. *)

Ltac spec_term_arity T := 
  let S := type of T in
  match tt with
  | tt => spec_arity S 
  | _ => let h := get_head S in 
         let S' := (eval unfold h in S) in
         spec_arity S'
         (* todo: several unfold: call spec_term_arity T' -- check no loop *)
  end.

(** [spec_goal_fun] and [spec_goal_arity] are specialized versions
   of [spec_fun] and [spec_arity] that apply to the current goal *)

Ltac spec_goal_fun tt :=
  match goal with |- ?S => spec_fun S end.

Ltac spec_goal_arity tt :=
  match goal with |- ?S => spec_arity S end.

(** [get_spec_hyp f] returns the hypothesis that contains a 
    specification for the function [f]. *)

Ltac get_spec_hyp f :=
  match goal with 
  | H: context [ spec_1 _ f ] |- _ => constr:(H)
  | H: context [ spec_2 _ f ] |- _ => constr:(H)
  | H: context [ spec_3 _ f ] |- _ => constr:(H) 
  | H: context [ spec_4 _ f ] |- _ => constr:(H) 
  | H: ?P f |- _ => constr:(H) (* todo: higher order pattern *)
charguer's avatar
charguer committed
85
  (* deprecated (coq changed):  | H: context [ ?P f ] |- _ => constr:(H) *)
charguer's avatar
init  
charguer committed
86 87
  end.

charguer's avatar
charguer committed
88

charguer's avatar
init  
charguer committed
89 90 91 92 93 94 95 96 97 98 99
(** [get_app_hyp f] returns the hypothesis that contains
    a proposition regarding an application of [f] to arguments *)

Ltac get_app_hyp f :=
  match goal with
  | H: context [ app_1 f _ _ ] |- _ => constr:(H)
  | H: context [ app_2 f _ _ _ ] |- _ => constr:(H)
  | H: context [ app_3 f _ _ _ _ ] |- _ => constr:(H) 
  | H: context [ app_4 f _ _ _ _ ] |- _ => constr:(H) 
  end.

charguer's avatar
charguer committed
100 101 102 103 104 105 106 107 108 109 110
(** [unfolds_to_spec tt] is a helper tactic that unfolds definition
    at the head of the goal until reaching a [spec_n] predicate. *)
Ltac unfolds_to_spec tt := 
  match goal with 
  | |- spec_1 _ ?f => idtac
  | |- spec_2 _ ?f => idtac
  | |- spec_3 _ ?f => idtac
  | |- spec_4 _ ?f => idtac
  | _ => progress(unfolds); unfolds_to_spec tt
  end. 

charguer's avatar
charguer committed
111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132
(** [term_is_app E] returns a boolean indicating whether [E]
    is an instance of the App predicate. *)

Ltac term_is_app E :=
  match E with 
  | app_1 ?f _ _ _ => constr:(true)
  | app_2 ?f _ _ _ _ => constr:(true)
  | app_3 ?f _ _ _ _ _ => constr:(true)
  | app_4 ?f _ _ _ _ _ _ => constr:(true)
  | App ?f _; _ _ => constr:(true)
  | App ?f _ _; _ _ => constr:(true)
  | App ?f _ _ _; _ _ => constr:(true)
  | App ?f _ _ _ _; _ _ => constr:(true)
  | _ => constr:(false)
  end.

(** [goal_is_app tt] returns a boolean indicating whether the 
    the goal is an instance of the App predicate. *)

Ltac goal_is_app tt :=
  match goal with |- ?S => term_is_app S end.

charguer's avatar
charguer committed
133

charguer's avatar
init  
charguer committed
134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 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
(*--------------------------------------------------------*)
(* ** Return lemmas from [FuncDefs] depending on the arity *)

(** Returns the lemma [app_spec_n] *)

Ltac get_app_spec_x n :=
  match n with
  | 1%nat => constr:(app_spec_1)
  | 2%nat => constr:(app_spec_2)
  | 3%nat => constr:(app_spec_3)
  | 4%nat => constr:(app_spec_4)
  end.

(** Returns the lemma [spec_elim_n_m] *)

Ltac get_spec_elim_x_y x y := 
  match constr:(x,y) with 
     | (1%nat,1%nat) => constr:(spec_elim_1_1)
     | (1%nat,2%nat) => constr:(spec_elim_1_2)
     | (1%nat,3%nat) => constr:(spec_elim_1_3)
     | (1%nat,4%nat) => constr:(spec_elim_1_4)
     | (2%nat,1%nat) => constr:(spec_elim_2_1)
     | (2%nat,2%nat) => constr:(spec_elim_2_2)
     | (2%nat,3%nat) => constr:(spec_elim_2_3)
     | (2%nat,4%nat) => constr:(spec_elim_2_4)
     | (3%nat,1%nat) => constr:(spec_elim_3_1)
     | (3%nat,2%nat) => constr:(spec_elim_3_2)
     | (3%nat,3%nat) => constr:(spec_elim_3_3)
     | (3%nat,4%nat) => constr:(spec_elim_3_4)
     | (4%nat,1%nat) => constr:(spec_elim_4_1)
     | (4%nat,2%nat) => constr:(spec_elim_4_2)
     | (4%nat,3%nat) => constr:(spec_elim_4_3)
     | (4%nat,4%nat) => constr:(spec_elim_4_4)
   end.

(** Returns the lemma [spec_intro_n] *)

Ltac get_spec_intro_x x :=
  match x with
     | 1%nat => constr:(spec_intro_1)
     | 2%nat => constr:(spec_intro_2)
     | 3%nat => constr:(spec_intro_3)
     | 4%nat => constr:(spec_intro_4)
  end.

(** Returns the lemma [spec_weaken_n] *)

Ltac get_spec_weaken_x x :=
  match x with
     | 1%nat => constr:(spec_weaken_1)
     | 2%nat => constr:(spec_weaken_2)
     | 3%nat => constr:(spec_weaken_3)
     | 4%nat => constr:(spec_weaken_4)
  end.

(** Returns the lemma [get_app_intro_n_m] *)

Lemma id_proof : forall (P:Prop), P -> P.
Proof. auto. Qed.


(*--------------------------------------------------------*)
(* ** tools for post-conditions *)

charguer's avatar
demo  
charguer committed
198
Ltac is_evar_as_bool E :=
charguer's avatar
charguer committed
199
  constr:(ltac:(first 
charguer's avatar
demo  
charguer committed
200
    [ is_evar E; exact true 
charguer's avatar
charguer committed
201
    | exact false ])).
charguer's avatar
demo  
charguer committed
202 203

Ltac get_post tt :=
charguer's avatar
init  
charguer committed
204
  match goal with |- ?E => 
charguer's avatar
demo  
charguer committed
205 206 207 208 209 210
  match get_fun_arg E with (_,?Q) => constr:(Q) 
  end end.

Ltac post_is_meta tt := 
  let Q := get_post tt in is_evar_as_bool Q.

charguer's avatar
init  
charguer committed
211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228

(*--------------------------------------------------------*)
(* ** [xextractible] *)

(** [xextractible tt] applies to a goal of the form (R H Q)
    and raises an error if [H] contains extractible quantifiers 
    or facts. *)

Ltac xextractible tt :=
  match goal with |- ?R ?H ?Q => hextractible_rec H end.



(*--------------------------------------------------------*)
(* ** [xclean] *)

(** [xclean] performs some basic simplification is the
    context in order to beautify hypotheses that have been 
charguer's avatar
charguer committed
229 230
    inferred. 
    Remark: this tactic is automatically called by [xextract]. *)
charguer's avatar
init  
charguer committed
231

charguer's avatar
charguer committed
232 233
Ltac xclean_core :=
  reflect_clean tt.
charguer's avatar
init  
charguer committed
234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249
Tactic Notation "xclean" :=
  reflect_clean tt.


(*--------------------------------------------------------*)
(* ** [xok] *)

Ltac xok_core cont := 
  solve [ cbv beta; apply rel_le_refl
        | apply pred_le_refl
        | apply hsimpl_to_qunit; reflexivity
        | hsimpl; cont tt ].

Tactic Notation "xok" := 
  xok_core ltac:(idcont).
Tactic Notation "xok" "~" := 
charguer's avatar
charguer committed
250
  xok_core ltac:(fun _ => auto_tilde).
charguer's avatar
init  
charguer committed
251
Tactic Notation "xok" "*" := 
charguer's avatar
charguer committed
252
  xok_core ltac:(fun _ => auto_star).
charguer's avatar
init  
charguer committed
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


(*--------------------------------------------------------*)
(* ** [xauto] *)

(* [xauto] is a specialized version of [auto] that works
   well in program verification. One of its main strength
   is the ability to perform substitution before calling auto. *)

Ltac math_0 ::= xclean.

Ltac check_not_a_tag tt :=
  match goal with 
  | |- tag _ _ _ _ => fail 1 (* todo: not needed? *)
  | |- tag _ _ _ _ _ => fail 1
  | |- _ => idtac
  end.


Ltac xauto_common cont :=
  check_not_a_tag tt;  
  try solve [ cont tt 
            | solve [ apply refl_equal ]
            | xok_core ltac:(fun _ => solve [ cont tt | substs; cont tt ] ) 
            | substs; if_eq; solve [ cont tt | apply refl_equal ]  ].

Ltac xauto_tilde_default cont := xauto_common cont.
Ltac xauto_star_default cont := xauto_common cont.

Ltac xauto_tilde := xauto_tilde_default ltac:(fun _ => auto_tilde).
Ltac xauto_star := xauto_star_default ltac:(fun _ => auto_star). 

Tactic Notation "xauto" "~" := xauto_tilde.
Tactic Notation "xauto" "*" := xauto_star.
Tactic Notation "xauto" := xauto~.

Tactic Notation "hsimpl" "~" constr(L) :=
  hsimpl L; xauto~.
Tactic Notation "hsimpl" "~" constr(X1) constr(X2) :=
  hsimpl X1 X2; xauto~.
Tactic Notation "hsimpl" "~" constr(X1) constr(X2) constr(X3) :=
  hsimpl X1 X2 X3; xauto~.


(*--------------------------------------------------------*)
(* ** [xisspec] *)

(** [xisspec] is a helper function to prove a goal of the
    form [is_spec K], which basically amounts to showing
    that [K x1 .. xN] is weakenable. The tactic [intuition eauto]
    called by [xisspec] discharges this obligation is most cases.
    Cases that are not handled by this tactic are typically those
    involving case analysis. *)

Ltac xisspec_core :=
charguer's avatar
charguer committed
308
  solve [ intros_all; unfolds rel_le, pred_le; auto; auto_star ].
charguer's avatar
init  
charguer committed
309 310 311 312 313 314 315 316

Tactic Notation "xisspec" :=
  (* check_noevar_goal; *) xisspec_core.


(*--------------------------------------------------------*)
(* ** [xlocal] *)

charguer's avatar
demo  
charguer committed
317 318 319 320
Ltac xlocal_core tt ::=
  first [ assumption
 	| apply local_is_local 
        | apply app_local_1 
charguer's avatar
charguer committed
321
        | match goal with H: is_local_pred ?S |- is_local (?S _) => apply H end ].
charguer's avatar
init  
charguer committed
322 323 324 325 326 327 328


(*--------------------------------------------------------*)
(* ** [xcf] *)

(** [xcf] applies to a goal of the form [Spec_n f K]
    and uses the characteristic formula known for [f]
charguer's avatar
charguer committed
329 330 331 332 333
    in order to get started proving the goal.

    It also applies to a goal of the form 
    [app_n f x1 .. xN H Q], and exploits the characteristic
    formula for [f] in order to get started proving the goal. *)
charguer's avatar
init  
charguer committed
334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367

Ltac remove_head_unit tt :=
  repeat match goal with 
  | |- unit -> _ => intros _
  end.

Ltac xcf_post tt :=
  cbv beta; remove_head_unit tt.

Ltac solve_type :=
  match goal with |- Type => exact unit end.

Ltac xcf_for_core_hyp H :=
  match type of H with
  | @tag tag_top_fun _ _ _ => sapply H; instantiate; try solve_type; [ try xisspec | ]
  | _ => sapply H; try solve_type
  end; clear H; xcf_post tt.

Ltac xcf_for_core f :=
  ltac_database_get database_cf f;
  let H := fresh "TEMP" in intros H; 
  xcf_for_core_hyp H.

Tactic Notation "xcf" constr(Sf) :=
  generalize Sf; 
  let H := fresh "TEMP" in intros H; 
  xcf_for_core_hyp H.

Ltac xcf_core :=
  intros; first 
  [ let f := spec_goal_fun tt in xcf_for_core f 
  | match goal with |- ?f = _ => xcf_for_core f end
  | let f := spec_goal_fun tt in let H := get_spec_hyp f in sapply H; [ try xisspec | ] ].

charguer's avatar
charguer committed
368 369 370 371
(** [xcf_for f] is a tactic used for specifying explicitly  
    the name of the function for which a characteristic formula
    should be searched. *)

charguer's avatar
init  
charguer committed
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
Tactic Notation "xcf_for" constr(f) := xcf_for_core f.

(** [xcf_app] applies to a goal of the form 
    [app_n f x1 .. xN H Q], and exploits the characteristic
    formula for [f] in order to get started proving the goal. *)

Ltac intro_subst_arity n :=
  let x1 := fresh "TEMP" in let x2 := fresh "TEMP" in
  let x3 := fresh "TEMP" in let x4 := fresh "TEMP" in 
  let H1 := fresh "TEMP" in let H2 := fresh "TEMP" in
  let H3 := fresh "TEMP" in let H4 := fresh "TEMP" in
  match n with
  | 1%nat => intros x1 H1; subst x1
  | 2%nat => intros x1 x2 H1 H2; subst x1 x2
  | 3%nat => intros x1 x2 x3 H1 H2 H3; subst x1 x2 x3
  | 4%nat => intros x1 x2 x3 x4 H1 H2 H3 H4; subst x1 x2 x3 x4
  end.

Ltac xcf_app_core :=
  let n := spec_goal_arity tt in 
  let H := get_app_spec_x n in
  apply H; xcf_core; try intro_subst_arity n.

Ltac xcf_app_base :=
  try (xuntag tag_apply);
  xcf_app_core.

Tactic Notation "xcf_app" := xcf_app_base.

charguer's avatar
charguer committed
401 402 403 404 405 406 407 408 409
Ltac xcf_select_core :=
  intros;
  match goal_is_app tt with
  | true => xcf_app
  | false => xcf_core
  end.

Tactic Notation "xcf" := xcf_select_core.

charguer's avatar
init  
charguer committed
410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432

(*--------------------------------------------------------*)
(* ** [xfind] *)

Ltac xfind_by_core db f :=
  ltac_database_get db f.

Ltac xfind_ctx f :=
  let H := get_spec_hyp f in generalize H.

(** [xfind_by db f] displays the specification registered with [f]
    either in the context or in the database [db].
    (by inserting it as new hypothesis at head of the goal). *)

Tactic Notation "xfind_by" constr(db) constr(f) :=  
  xfind_by_core db f.

(** [xfind_by db] calls [xfind_by db f] on the function that 
    appears in the goal. *)

Tactic Notation "xfind_by" constr(db) := 
  let f := spec_goal_fun tt in xfind_by db f.

charguer's avatar
cfheap  
charguer committed
433 434
(** [xfind f] first tries [xfind_by database_spec_credits f]
    then tries [xfind_by database_spec f]. *)
charguer's avatar
init  
charguer committed
435

charguer's avatar
credits  
charguer committed
436
Ltac xfind_core f := 
charguer's avatar
cfheap  
charguer committed
437
  first [ xfind_ctx f | xfind_by database_spec_credits f | xfind_by database_spec f ].
charguer's avatar
init  
charguer committed
438 439 440 441 442 443 444 445 446 447 448 449 450 451 452

Tactic Notation "xfind" constr(f) :=
  xfind_core f.

(** [xfind] without argument calls [xfind f] for the function
    [f] that appear in the current goal *)

Tactic Notation "xfind" := 
  let f := spec_goal_fun tt in xfind f.


(*--------------------------------------------------------*)
(* ** [xcurried] *)

(** [xcurried] helps proving a goal of the form [curried_n f],
charguer's avatar
charguer committed
453
    by proving that [f] accepts [True] as post-condition.
charguer's avatar
init  
charguer committed
454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512
    The latter proof is set up by invoking the characteristic
    formula for [f]. *)

Ltac xcurried_core :=
  try solve [ 
    unfold curried_1, curried_2, curried_3, curried_4;
    xcf; check_noevar_goal; auto ].

Tactic Notation "xcurried" := xcurried_core.

Ltac xcurried_using H :=
  try solve [ 
    unfold curried_1, curried_2, curried_3, curried_4;
    eapply H; check_noevar_goal; auto ].

Tactic Notation "xcurried" constr(H) :=
  sapply H; instantiate; try solve_type; [ try xisspec | auto ].

Ltac xcurried_debug :=
  unfold curried_1, curried_2, curried_3, curried_4; 
  idtac "type xcf".



(*--------------------------------------------------------*)
(* ** [xextract] *)

Ltac xextract_core :=
  match goal with
  | |- _ ==> _ => hextract; xclean
  | |- _ ===> _ => let r := fresh "r" in intros r; hextract; xclean
  | |- _ => simpl; hclean; instantiate
  end.

(* todo: use continuations *)
(* todo: check that an arrow is visible before doing intros *)
Tactic Notation "xextract" := 
  xextract_core; xclean.
Tactic Notation "xextract" "as" simple_intropattern(I1) := 
  xextract; intros I1; xclean.
Tactic Notation "xextract" "as" simple_intropattern(I1) simple_intropattern(I2) := 
  xextract; intros I1 I2; xclean. 
Tactic Notation "xextract" "as" simple_intropattern(I1) simple_intropattern(I2) 
 simple_intropattern(I3) := 
  xextract; intros I1 I2 I3; xclean.
Tactic Notation "xextract" "as" simple_intropattern(I1) simple_intropattern(I2) 
 simple_intropattern(I3) simple_intropattern(I4) := 
  xextract; intros I1 I2 I3 I4; xclean.
Tactic Notation "xextract" "as" simple_intropattern(I1) simple_intropattern(I2) 
 simple_intropattern(I3) simple_intropattern(I4) simple_intropattern(I5) := 
  xextract; intros I1 I2 I3 I4 I5; xclean.
Tactic Notation "xextract" "as" simple_intropattern(I1) simple_intropattern(I2) 
 simple_intropattern(I3) simple_intropattern(I4) simple_intropattern(I5)
 simple_intropattern(I6) := 
  xextract; intros I1 I2 I3 I4 I5 I6; xclean.
Tactic Notation "xextract" "as" simple_intropattern(I1) simple_intropattern(I2) 
 simple_intropattern(I3) simple_intropattern(I4) simple_intropattern(I5)
 simple_intropattern(I6) simple_intropattern(I7) := 
  xextract; intros I1 I2 I3 I4 I5 I6 I7; xclean.
charguer's avatar
ready  
charguer committed
513 514 515 516 517 518 519 520 521 522 523 524 525 526 527
Tactic Notation "xextract" "as" simple_intropattern(I1) simple_intropattern(I2) 
 simple_intropattern(I3) simple_intropattern(I4) simple_intropattern(I5)
 simple_intropattern(I6) simple_intropattern(I7) simple_intropattern(I8) := 
  xextract; intros I1 I2 I3 I4 I5 I6 I7 I8; xclean.
Tactic Notation "xextract" "as" simple_intropattern(I1) simple_intropattern(I2) 
 simple_intropattern(I3) simple_intropattern(I4) simple_intropattern(I5)
 simple_intropattern(I6) simple_intropattern(I7) simple_intropattern(I8) 
 simple_intropattern(I9) := 
  xextract; intros I1 I2 I3 I4 I5 I6 I7 I8 I9; xclean.
Tactic Notation "xextract" "as" simple_intropattern(I1) simple_intropattern(I2) 
 simple_intropattern(I3) simple_intropattern(I4) simple_intropattern(I5)
 simple_intropattern(I6) simple_intropattern(I7) simple_intropattern(I8) 
 simple_intropattern(I9) simple_intropattern(I10) := 
  xextract; intros I1 I2 I3 I4 I5 I6 I7 I8 I9 I10; xclean.

charguer's avatar
init  
charguer committed
528 529 530 531 532 533 534 535

Tactic Notation "xextracts" :=
  let E := fresh "TEMP" in xextract as E; subst_hyp E.


(*--------------------------------------------------------*)
(* ** [xsimpl] *)

charguer's avatar
charguer committed
536 537 538 539 540
(** [xsimpl] performs a heap entailement simplification using 
  [hsimpl], then calls the tactic [xclean]. *)

Ltac xsimpl_core := hsimpl; xclean.

charguer's avatar
xfocus  
charguer committed
541
Tactic Notation "xsimpl" := xsimpl_core. 
charguer's avatar
init  
charguer committed
542 543 544
Tactic Notation "xsimpl" "~" := xsimpl; xauto~.
Tactic Notation "xsimpl" "*" := xsimpl; xauto*.

charguer's avatar
ready  
charguer committed
545 546 547 548 549 550 551
(* TODO: factorize below with above *)

Ltac xsimpl_core_with E := hsimpl E; xclean.
Tactic Notation "xsimpl" constr(E) := xsimpl_core_with E. 
Tactic Notation "xsimpl" "~" constr(E) := xsimpl E; xauto~.
Tactic Notation "xsimpl" "*" constr(E) := xsimpl E; xauto*.

charguer's avatar
charguer committed
552

charguer's avatar
init  
charguer committed
553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581
(*--------------------------------------------------------*)
(* ** [xunfold] *)

Tactic Notation "xunfold" :=
  hunfold.
Tactic Notation "xunfold" constr(E) := 
  hunfold E.
Tactic Notation "xunfold" constr(E) "at" constr(K) := 
  hunfold E at K.



(*--------------------------------------------------------*)
(* ** [xlet] *)

(** [xlet] is used to reason on a let-node, i.e. on a goal
    of the form [(Let x := Q1 in Q2) P]. The general form
    is [xlet Q as y], where [y] is the name to be used
    in place of [x].
    The arguments are optional, so the following forms are
    allowed: [xlet], [xlet as x], [xlet Q], [xlet Q as x]. *)

Ltac xlet_core cont0 cont1 cont2 :=
  xextractible tt;
  apply local_erase; cont0 tt; split; [ | cont1 tt; cont2 tt ].

Tactic Notation "xlet_def" tactic(c0) tactic(c1) tactic(c2) :=
  xlet_core ltac:(c0) ltac:(c1) ltac:(c2).

582
Tactic Notation "xlet" constr(Q) "as" simple_intropattern(x) :=
charguer's avatar
init  
charguer committed
583 584 585
  xlet_def (fun _ => exists Q) (fun _ => intros x) (fun _ => try xextract).
Tactic Notation "xlet" constr(Q) :=
  xlet_def (fun _ => exists Q) (fun _ => intro) (fun _ => try xextract).
586
Tactic Notation "xlet" "as" simple_intropattern(x) :=
charguer's avatar
init  
charguer committed
587 588 589 590
  xlet_def (fun _ => esplit) (fun _ => intros x) (fun _ => idtac).
Tactic Notation "xlet" :=
  xlet_def (fun _ => esplit) (fun _ => intro) (fun _ => idtac).

charguer's avatar
charguer committed
591 592 593 594
Tactic Notation "xlets" constr(Q) :=
  xlet Q; [ | intro_subst ].


charguer's avatar
init  
charguer committed
595 596 597 598 599 600 601 602 603 604
Tactic Notation "xseq" :=
  xlet_def (fun _ => esplit) (fun _ => idtac) (fun _ => idtac).
Tactic Notation "xseq" constr(H) :=
  xlet_def (fun _ => first [ exists (#H) | exists H ]) (fun _ => idtac) (fun _ => try xextract).

(** TODO: comment xseq *)

Tactic Notation "xseq_no_xextract" constr(H) :=
  xlet_def (fun _ => first [ exists (#H) | exists H ]) (fun _ => idtac) (fun _ => idtac).

charguer's avatar
charguer committed
605
Tactic Notation "xlet" "~" := xlet; auto_tilde. (* todo: xauto ! *)
606
Tactic Notation "xlet" "~" "as" simple_intropattern(x) := xlet as x; auto_tilde.
charguer's avatar
charguer committed
607 608
Tactic Notation "xseq" "~" := xseq; auto_tilde. (* todo: xauto ! *)
Tactic Notation "xlet" "~" constr(Q) := xlet Q; auto_tilde.
609
Tactic Notation "xlet" "~" constr(Q) "as" simple_intropattern(x) := xlet Q as x; auto_tilde.
charguer's avatar
charguer committed
610 611
Tactic Notation "xseq" "~" constr(H) := xseq H; auto_tilde.
Tactic Notation "xlet" "*" := xlet; auto_star.
612
Tactic Notation "xlet" "*" "as" simple_intropattern(x) := xlet as x; auto_star.
charguer's avatar
charguer committed
613 614
Tactic Notation "xseq" "*" := xseq; auto_star. (* todo: xauto ! *)
Tactic Notation "xlet" "*" constr(Q) := xlet Q; auto_star.
615
Tactic Notation "xlet" "*" constr(Q) "as" simple_intropattern(x) := xlet Q as x; auto_star.
charguer's avatar
charguer committed
616
Tactic Notation "xseq" "*" constr(H) := xseq H; auto_star.
charguer's avatar
init  
charguer committed
617 618 619 620 621 622 623


(*--------------------------------------------------------*)
(* ** [xval] *)

(** [xval] is used to reason on a let-node binding a value. *)

charguer's avatar
polylet  
charguer committed
624
Ltac xval_impl x Hx :=
charguer's avatar
init  
charguer committed
625 626
  apply local_erase; intros x Hx.

charguer's avatar
polylet  
charguer committed
627 628 629
Tactic Notation "xval" "as" simple_intropattern(x) simple_intropattern(Hx) :=
  xval_impl x Hx.

630
Tactic Notation "xval" "as" simple_intropattern(x) :=
charguer's avatar
polylet  
charguer committed
631 632 633 634 635
  let Hx := fresh "P" x in xval_impl x Hx.

Ltac xval_anonymous_impl tt :=
  apply local_erase; intro; let x := get_last_hyp tt in revert x; 
  let Hx := fresh "P" x in intros x Hx.
charguer's avatar
init  
charguer committed
636 637

Tactic Notation "xval" :=
charguer's avatar
polylet  
charguer committed
638
  xval_anonymous_impl tt.
charguer's avatar
init  
charguer committed
639 640 641

(** [xvals] substitutes the bound value right away. *)

charguer's avatar
polylet  
charguer committed
642
Ltac xvals_impl tt :=
charguer's avatar
init  
charguer committed
643 644
  apply local_erase; intro; intro_subst.

charguer's avatar
polylet  
charguer committed
645 646 647
Tactic Notation "xvals" :=
  xvals_impl tt. 

charguer's avatar
init  
charguer committed
648 649
(** [xval_st P] is used to assign an abstract specification to the value *)

charguer's avatar
polylet  
charguer committed
650 651 652 653 654 655
Ltac xval_st_core P x Hx :=
  let E := fresh in intros x E; asserts Hx: (P x); [ subst x | clear E ].

Ltac xval_st_impl P x Hx :=
  apply local_erase; xval_st_core P x Hx.

656
Tactic Notation "xval_st" constr(P) "as" simple_intropattern(x) simple_intropattern(Hx) :=
charguer's avatar
polylet  
charguer committed
657
  xval_st_impl P x Hx.
charguer's avatar
init  
charguer committed
658

659
Tactic Notation "xval_st" constr(P) "as" simple_intropattern(x) :=
charguer's avatar
polylet  
charguer committed
660 661 662 663 664
  let Hx := fresh "P" x in xval_st_impl P x Hx.

Ltac xval_st_anonymous_impl P :=
  apply local_erase; intro; let x := get_last_hyp tt in revert x; 
  let Hx := fresh "P" x in xval_st_core P x Hx.
charguer's avatar
init  
charguer committed
665 666

Tactic Notation "xval_st" constr(P) :=
charguer's avatar
polylet  
charguer committed
667
  xval_st_anonymous_impl P.
charguer's avatar
init  
charguer committed
668 669 670 671 672 673 674 675 676 677


(*--------------------------------------------------------*)
(* ** [xfail], [xdone] *)

(** [xfail] simplifies a proof obligation of the form [Fail],
    which is in fact equivalent to [False].
    [xfail_noclean] is also available. *)

Tactic Notation "xfail_noclean" :=
charguer's avatar
charguer committed
678
  xextractible tt; xuntag tag_fail; apply local_erase.
charguer's avatar
init  
charguer committed
679 680 681 682 683 684 685
Tactic Notation "xfail" := 
  xfail_noclean; xclean.
Tactic Notation "xfail" "~" :=  
  xfail; xauto~.
Tactic Notation "xfail" "*" :=  
  xfail; xauto*.

charguer's avatar
charguer committed
686 687 688 689 690
(** [xfail C] is like [xfail; false C]. *)

Tactic Notation "xfail" constr(C) := 
  xfail; false C.

charguer's avatar
init  
charguer committed
691 692 693 694 695 696
(** [xdone] proves a goal of the form [Done],
    which is in fact equivalent to [True]. *)

Tactic Notation "xdone" :=
  xuntag tag_done; apply local_erase; split.

charguer's avatar
charguer committed
697 698 699 700 701 702 703 704 705 706 707
(*--------------------------------------------------------*)
(* ** [xpay] *)

(** [xpay] is used to pay one credit *)

Ltac xpay_start tt :=
  xuntag tag_pay; apply local_erase; esplit; split.

Ltac xpay_core tt :=
  xpay_start tt; [ unfold pay_one; hsimpl | ].

charguer's avatar
charguer committed
708 709 710
Ltac xpay_nat_core tt :=
  xpay_start tt; [ rewrite pay_one_nat; hsimpl | ].

charguer's avatar
charguer committed
711 712 713
Tactic Notation "xpay" := xpay_core tt.
 
(** [xpay_skip] is used to skip the paying of one credit;
charguer's avatar
charguer committed
714
    only for development purpose. *)
charguer's avatar
charguer committed
715 716 717 718

Ltac xpay_fake tt :=
  xpay_start tt; 
  [ assert (pay_one_fake: forall H, pay_one H H); 
charguer's avatar
charguer committed
719
     [ admit (* for development only *)
charguer's avatar
charguer committed
720 721 722 723 724
     | apply pay_one_fake ] 
  | ].

Tactic Notation "xpay_skip" := xpay_fake tt.

charguer's avatar
init  
charguer committed
725

charguer's avatar
cfheap  
charguer committed
726 727 728 729
(*--------------------------------------------------------*)
(* ** [xskip_credits] *)

(** Tactic [xskip_credits] runs [skip_credits] then [hsimpl].
charguer's avatar
charguer committed
730
    Should only be used for development purpose. *)
charguer's avatar
cfheap  
charguer committed
731 732 733 734 735 736 737 738

Ltac xskip_credits_base := 
  skip_credits_core; hsimpl.

Tactic Notation "xskip_credits" := 
  xskip_credits_base.


charguer's avatar
init  
charguer committed
739 740 741
(*--------------------------------------------------------*)
(* ** [xret] *)

charguer's avatar
charguer committed
742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771
(** Lemma used by [xret] *)

Lemma xret_lemma : forall HG B (v:B) H (Q:B->hprop),
  H ==> Q v \* HG -> 
  local (fun H' Q' => H' ==> Q' v) H Q.
Proof using.  
  introv W. eapply (@local_gc_pre HG).
  auto. rewrite star_comm. apply W.
  apply~ local_erase.
Qed.

(** Lemma used by [xret_no_gc] *)

Lemma xret_no_gc_lemma : forall B (v:B) H (Q:B->hprop),
  H ==> Q v -> 
  local (fun H' Q' => H' ==> Q' v) H Q.
Proof using.  
  introv W. apply~ local_erase.
Qed.

(** Lemma used by [xret] and [xret_no_gc] 
    for when post-condition unifies trivially *)

Lemma xret_lemma_unify : forall B (v:B) H,
  local (fun H' Q' => H' ==> Q' v) H (fun x => \[x = v] \* H).
Proof using.  
  intros. apply~ local_erase. hsimpl. auto. 
Qed.


charguer's avatar
init  
charguer committed
772 773 774 775
(** [xret]. *)

Ltac xret_core :=
  first [ apply xret_lemma_unify
charguer's avatar
charguer committed
776
        | eapply xret_lemma ].
charguer's avatar
init  
charguer committed
777 778 779

Ltac xret_no_gc_core :=
  first [ apply xret_lemma_unify
charguer's avatar
charguer committed
780
        | eapply xret_no_gc_lemma ].
charguer's avatar
init  
charguer committed
781 782

Ltac xret_pre cont := 
charguer's avatar
ready  
charguer committed
783
  xextractible tt;
charguer's avatar
init  
charguer committed
784 785 786 787 788
  match ltac_get_tag tt with
  | tag_ret => cont tt
  | tag_let_trm => xlet; [ cont tt | instantiate ]
  end. 

charguer's avatar
charguer committed
789 790 791 792 793 794 795 796 797
  (* todo: special treatment of xlet/xret
  Ltac xret_pre cont := 
    match ltac_get_tag tt with
    | tag_ret => cont tt
    | tag_let_pure => xlet; [ cont tt | instantiate ]
    end. *)

(** [xret_no_clean] is like [xret] but it does not call 
    [xclean] on the goal. *)
charguer's avatar
init  
charguer committed
798 799 800 801 802 803 804 805 806 807 808

Tactic Notation "xret_no_clean" := 
  xret_pre ltac:(fun _ => xret_core).

Tactic Notation "xret" := 
  xret_pre ltac:(fun _ => xret_core; xclean).
Tactic Notation "xret" "~" :=  
  xret; xauto~.
Tactic Notation "xret" "*" :=  
  xret; xauto*.

charguer's avatar
charguer committed
809 810 811
(** [xret_no_gc] can be used to not introduce an existentially-
    quantified heap for garbage collection *)

charguer's avatar
init  
charguer committed
812 813 814 815 816 817 818
Tactic Notation "xret_no_gc" := 
  xret_no_gc_core.
Tactic Notation "xret_no_gc" "~" :=  
  xret_no_gc; xauto~.
Tactic Notation "xret_no_gc" "*" :=  
  xret_no_gc; xauto*.

charguer's avatar
charguer committed
819 820
(** [xrets] is short for [xret; xsimpl] *)

charguer's avatar
init  
charguer committed
821 822 823 824 825 826 827 828 829 830 831
Ltac xrets_base :=
  xret; xsimpl.

Tactic Notation "xrets" :=
  xrets_base.
Tactic Notation "xrets" "~" :=  
  xrets; xauto~.
Tactic Notation "xrets" "*" :=  
  xrets; xauto*.


charguer's avatar
ready  
charguer committed
832 833 834 835 836 837
(*--------------------------------------------------------*)
(* ** [xpre] *)

Tactic Notation "xpre" constr(H) :=
  eapply (@local_weaken_pre_gc H); [ try xlocal | | ].

charguer's avatar
init  
charguer committed
838 839 840
(*--------------------------------------------------------*)
(* ** [xpost] *)

charguer's avatar
charguer committed
841 842 843 844 845 846 847 848 849 850
(** Lemma used by [xpost], 
    for introducing an evar for the post-condition *)

Lemma xpost_lemma : forall B Q' Q (F:~~B) H,
  is_local F -> 
  F H Q' -> 
  Q' ===> Q ->
  F H Q.
Proof using. intros. applys* local_weaken. Qed.

charguer's avatar
init  
charguer committed
851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900
Tactic Notation "xpost" :=
  eapply xpost_lemma; [ try xlocal | | ].

Lemma fix_post : forall (B:Type) (Q':B->hprop) (F:~~B) (H:hprop) Q,
  is_local F ->
  F H Q' -> 
  Q' ===> Q ->
  F H Q.
Proof. intros. apply* local_weaken. Qed.

Tactic Notation "xpost" constr(Q) := 
  apply (@fix_post _ Q); [ try xlocal | | try apply rel_le_refl ].


(*--------------------------------------------------------*)
(* ** [xgen] *)

Lemma xgen_lemma : forall A (J:A->hprop) (E:A),
  J E ==> heap_is_pack J.
Proof. intros. hsimpl*. Qed.

Ltac xgen_abstract H E :=
  let Jx := eval pattern E in H in
  match Jx with ?J _ => constr:(J) end.

Ltac xgen_nosimpl E :=
  match goal with |- ?H ==> _ =>
    let J := xgen_abstract H E in 
    eapply pred_le_trans; [ apply (@xgen_lemma _ J E) | ] end.

Ltac xgen_base E := 
  xgen_nosimpl E.    

Tactic Notation "xgen" constr(E1) :=
  xgen_base E1.
Tactic Notation "xgen" constr(E1) constr(E2) :=
  xgen_base E1; xgen_base E2.

Lemma xgen_demo : forall (x E y F:int) H1 R,
  (forall H2, x ~> R E \* y ~> R F \* H1 ==> H2 -> H2 = H2 -> True) -> True.
Proof.
  introv H. dup.
  eapply H. xgen E. xgen F. xok. auto.
  eapply H. xgen E F. xok. auto.
Qed.


(*--------------------------------------------------------*)
(* ** [xgc] *)

charguer's avatar
charguer committed
901 902 903 904 905 906 907 908 909 910 911
(** Lemma used by [xgc_all],
    to remove everything from the pre-condition *)

Lemma local_gc_pre_all : forall B Q (F:~~B) H,
  is_local F -> 
  F \[] Q ->
  F H Q.
Proof using. intros. apply* (@local_gc_pre H). hsimpl. Qed.



charguer's avatar
init  
charguer committed
912 913 914
Ltac xgc_core :=
  xextractible tt;
  eapply local_gc_post; 
charguer's avatar
charguer committed
915
  [ try xlocal | | ].
charguer's avatar
init  
charguer committed
916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946

Ltac xgc_remove_core H :=
  xextractible tt;
  eapply local_gc_pre with (HG := H);
    [ try xlocal
    | hsimpl
    | ].

Ltac xgc_keep_core H :=
  xextractible tt;
  eapply local_gc_pre with (H' := H);
    [ try xlocal
    | hsimpl
    | ].

Tactic Notation "xgc" constr(H) := 
  xgc_remove_core H.

Tactic Notation "xgc" "-" constr(H) := 
  xgc_keep_core H.

Tactic Notation "xgc" := 
  xgc_core.

Tactic Notation "xgc_all" := 
  eapply local_gc_pre_all; [ try xlocal | ].


(*--------------------------------------------------------*)
(* ** [xframe] *)

charguer's avatar
charguer committed
947 948 949 950 951 952 953 954 955 956 957
(** Lemma used by [xframe] *)

Lemma xframe_lemma : forall H1 H2 B Q1 (F:~~B) H Q,
  is_local F -> 
  H ==> H1 \* H2 -> 
  F H1 Q1 -> 
  Q1 \*+ H2 ===> Q ->
  F H Q.
Proof using. intros. apply* local_wframe. Qed.


charguer's avatar
init  
charguer committed
958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016
Ltac xframe_remove_core H :=
  xextractible tt;
  eapply xframe_lemma with (H2 := H);
    [ try xlocal
    | hsimpl
    | 
    | ].

Ltac xframe_keep_core H :=
  xextractible tt;
  eapply xframe_lemma with (H1 := H);
    [ try xlocal
    | hsimpl
    | 
    | ].

Tactic Notation "xframe" constr(H) := 
  xframe_remove_core H.

Tactic Notation "xframe" "-" constr(H) := 
  xframe_keep_core H.


(*--------------------------------------------------------*)
(* ** [xframes] *)

Ltac xframes_core_1 s := 
  match goal with |- ?R ?H ?Q =>
    match H with context [ s ~> ?M ] =>
      xframe (s ~> M) end end.

Tactic Notation "xframes" constr(s1) := 
  xframes_core_1 s1.

Ltac xframes_core_2 s1 s2 := 
  match goal with |- ?R ?H ?Q =>
    match H with context [ s1 ~> ?M1 ] =>
      match H with context [ s2 ~> ?M2 ] =>
        xframe (s1 ~> M1 \* s2 ~> M2) end end end.

Tactic Notation "xframes" constr(s1) constr(s2) := 
  xframes_core_2 s1 s2.

Ltac xframes_core_3 s1 s2 s3 := 
  match goal with |- ?R ?H ?Q =>
    match H with context [ s1 ~> ?M1 ] =>
      match H with context [ s2 ~> ?M2 ] =>
        match H with context [ s3 ~> ?M3 ] =>
          xframe (s1 ~> M1 \* s2 ~> M2 \* s3 ~> M3) 
  end end end end.

Tactic Notation "xframes" constr(s1) constr(s2) constr(s3) := 
  xframes_core_3 s1 s2 s3.



(*--------------------------------------------------------*)
(* ** [xchange] *)

charguer's avatar
charguer committed
1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030
(** Lemma used by [xchange] *)

Lemma xchange_lemma : forall H1 H1' H2 B H Q (F:~~B),
  is_local F -> 
  (H1 ==> H1') -> 
  (H ==> H1 \* H2) -> 
  F (H1' \* H2) Q -> 
  F H Q.
Proof using.
  introv W1 L W2 M. applys local_wframe __ \[]; eauto.
  hsimpl. hchange~ W2. hsimpl~. rew_heap~. 
Qed.


charguer's avatar
init  
charguer committed
1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136
Ltac xchange_apply L cont :=
   eapply xchange_lemma; 
     [ try xlocal | applys L | cont tt | ].

(* note: modif should be himpl_proj1 or himpl_proj2 *)
Ltac xchange_forwards L modif cont :=
  forwards_nounfold_then L ltac:(fun K =>
  match modif with
  | __ => 
     match type of K with
     | _ = _ => xchange_apply (@pred_le_proj1 _ _ _ K) cont
     | _ => xchange_apply K cont
     end
  | _ => xchange_apply (@modif _ _ _ K) cont
  end).

Ltac xchange_with_core H H' :=
  eapply xchange_lemma with (H1:=H) (H1':=H'); 
    [ try xlocal | | hsimpl | ].

Ltac xchange_core E modif :=
  match E with
  | ?H ==> ?H' => xchange_with_core H H'
  | _ => xchange_forwards E modif ltac:(fun _ => instantiate; hsimpl)
  end.

Ltac xchange_base E modif :=
  xextractible tt;
  match goal with
  | |- _ ==> _ => hchange_base E modif
  | |- _ ===> _ => hchange_base E modif
  | _ => xchange_core E modif
  end.

Tactic Notation "xchange_debug" constr(E) :=
  xchange_forwards E __ ltac:(idcont).
Tactic Notation "xchange_debug" "->" constr(E) :=
  xchange_forwards E pred_le_proj1 ltac:(idcont).
Tactic Notation "xchange_debug" "<-" constr(E) :=
  xchange_forwards pred_le_proj2 ltac:(idcont).

Tactic Notation "xchange" constr(E) :=
  xchange_base E __.
Tactic Notation "xchange" "->" constr(E) :=
  xchange_base E pred_le_proj1.
Tactic Notation "xchange" "<-" constr(E) :=
  xchange_base E pred_le_proj2.

Tactic Notation "xchange" constr(E) "as" := 
  xchange E; try xextract.
Tactic Notation "xchange" constr(E) "as" simple_intropattern(I1) := 
  xchange E; try xextract as I1.
Tactic Notation "xchange" constr(E) "as" simple_intropattern(I1) simple_intropattern(I2) := 
  xchange E; try xextract as I1 I2.
Tactic Notation "xchange" constr(E) "as" simple_intropattern(I1) simple_intropattern(I2)
 simple_intropattern(I3) := 
  xchange E; try xextract as I1 I2 I3.
Tactic Notation "xchange" constr(E) "as" simple_intropattern(I1) simple_intropattern(I2)
 simple_intropattern(I3) simple_intropattern(I4) := 
  xchange E; try xextract as I1 I2 I3 I4. 
Tactic Notation "xchange" constr(E) "as" simple_intropattern(I1) simple_intropattern(I2)
 simple_intropattern(I3) simple_intropattern(I4) simple_intropattern(I5) := 
  xchange E; try xextract as I1 I2 I3 I4 I5. 

Tactic Notation "xchange" "~" constr(E) :=
  xchange E; auto_tilde.
Tactic Notation "xchange" "~" constr(E) "as" := 
  xchange~ E; try xextract.
Tactic Notation "xchange" "~" constr(E) "as" simple_intropattern(I1) := 
  xchange~ E; try xextract as I1.
Tactic Notation "xchange" "~" constr(E) "as" simple_intropattern(I1) simple_intropattern(I2) := 
  xchange~ E; try xextract as I1 I2.
Tactic Notation "xchange" "~" constr(E) "as" simple_intropattern(I1) simple_intropattern(I2)
 simple_intropattern(I3) := 
  xchange~ E; try xextract as I1 I2 I3.
Tactic Notation "xchange" "~" constr(E) "as" simple_intropattern(I1) simple_intropattern(I2)
 simple_intropattern(I3) simple_intropattern(I4) := 
  xchange~ E; try xextract as I1 I2 I3 I4. 
Tactic Notation "xchange" "~" constr(E) "as" simple_intropattern(I1) simple_intropattern(I2)
 simple_intropattern(I3) simple_intropattern(I4) simple_intropattern(I5) := 
  xchange~ E; try xextract as I1 I2 I3 I4 I5. 

Tactic Notation "xchange" "*" constr(E) :=
  xchange E; auto_star.
Tactic Notation "xchange" "*" constr(E) "as" := 
  xchange* E; try xextract.
Tactic Notation "xchange" "*" constr(E) "as" simple_intropattern(I1) := 
  xchange* E; try xextract as I1.
Tactic Notation "xchange" "*" constr(E) "as" simple_intropattern(I1) simple_intropattern(I2) := 
  xchange* E; try xextract as I1 I2.
Tactic Notation "xchange" "*" constr(E) "as" simple_intropattern(I1) simple_intropattern(I2)
 simple_intropattern(I3) := 
  xchange* E; try xextract as I1 I2 I3.
Tactic Notation "xchange" "*" constr(E) "as" simple_intropattern(I1) simple_intropattern(I2)
 simple_intropattern(I3) simple_intropattern(I4) := 
  xchange* E; try xextract as I1 I2 I3 I4. 
Tactic Notation "xchange" "*" constr(E) "as" simple_intropattern(I1) simple_intropattern(I2)
 simple_intropattern(I3) simple_intropattern(I4) simple_intropattern(I5) := 
  xchange* E; try xextract as I1 I2 I3 I4 I5. 

Tactic Notation "xchange" constr(E1) constr(E2) :=
  xchange E1; xchange E2.
Tactic Notation "xchange" constr(E1) constr(E2) constr(E3) :=
  xchange E1; xchange E2 E3.


charguer's avatar
ready  
charguer committed
1137
(* TODO: xchanges *)
charguer's avatar
init  
charguer committed
1138

charguer's avatar
charguer committed
1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196
(*--------------------------------------------------------*)
(* ** [xspec_show_types] *)

(* [xspec_show_types H] displays the type involved in the 
   specification lemma [H]. *)

Ltac xspec_show_types_impl H :=
  let one T := 
    idtac T; idtac " -> " in
  match type of H with
  | @spec_1 ?A1 ?B _ _ => one A1; idtac B
  | @spec_2 ?A1 ?A2 ?B _ _ => one A1; one A2; idtac B
  | @spec_3 ?A1 ?A2 ?A3 ?B _ _ => one A1; one A2; one A3; idtac B
  | @spec_4 ?A1 ?A2 ?A3 ?A4 ?B _ _ => one A1; one A2; one A3; one A4; idtac B
  end.

Tactic Notation "xspec_show_types" constr(H) :=
  xspec_show_types_impl H.


(*--------------------------------------------------------*)
(* ** [xapp_show_types] *)

(* [xapp_show_types] displays the type involved in an application *)

Ltac show_post_type Q :=
  match type of Q with
  | ?A -> _ => idtac A
  end.

Ltac xapp_show_types_app tt := 
  let one T := 
    idtac T; idtac " -> " in
  let common B Q := 
    idtac B; idtac "  specified as "; show_post_type Q in
  match goal with
  | |- @app_1 ?A1 ?B _ _ _ ?Q =>
    one A1; common B Q
  | |- @app_2 ?A1 ?A2 ?B _ _ _ ?Q =>
    one A1; one A2; common B Q
  | |- @app_3 ?A1 ?A2 ?A3 ?B _ _ _ ?Q =>
    one A1; one A2; one A3; common B Q
  | |- @app_4 ?A1 ?A2 ?A3 ?A4 ?B _ _ _ ?Q =>
    one A1; one A2; one A3; one A4; common B Q
  end.

Ltac xapp_show_types_impl tt := 
  let go tt := xuntag tag_apply; xapp_show_types_app tt in
  match ltac_get_tag tt with
  | tag_apply => go tt
  | tag_let_trm => xlet; [ go tt | ]
  | tag_seq => xseq; [ go tt | ]
  end.

Tactic Notation "xapp_show_types" :=
  xapp_show_types_impl tt.


charguer's avatar
init  
charguer committed
1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235
(*--------------------------------------------------------*)
(* ** [xapp_show_spec] *)

(* [xapp_show_spec] and [xapp_show_spec_by db] show the spec
   that [xapp] would use. *)

Ltac xapp_show_spec_core xfind_tactic := 
  xuntag; let f := spec_goal_fun tt in
  xfind_tactic f; let H := fresh in intro H.

Ltac xapp_show_spec_pre xfind_tactic := 
  let go tt := xapp_show_spec_core xfind_tactic in
  match ltac_get_tag tt with
  | tag_apply => go tt
  | tag_let_trm => xlet; [ go tt | ]
  | tag_seq => xseq; [ go tt | ]
  end.

Tactic Notation "xapp_show_spec" :=
  let xfind_tactic := fun f => xfind f in
  xapp_show_spec_pre xfind_tactic.

Tactic Notation "xapp_show_spec_by" constr(db) :=
  let xfind_tactic := fun f => xfind_by db f in
  xapp_show_spec_pre xfind_tactic.


(*--------------------------------------------------------*)
(* ** [xapp] *)

(* todo: when arities differ *)

Ltac xapp_compact KR args :=
  let args := list_boxer_of args in
  constr:((boxer KR)::args).

Ltac xapp_final HR :=
  eapply local_wframe; 
     [ xlocal
charguer's avatar
charguer committed
1236
     | eapply HR
charguer's avatar
init  
charguer committed
1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410
     | hsimpl
     | try xok ].

Ltac xapp_inst args solver :=
  let R := fresh "R" in let LR := fresh "L" R in 
  let KR := fresh "K" R in let IR := fresh "I" R in
  intros R LR KR; hnf in KR; (* lazy beta in *)
  let H := xapp_compact KR args in
  forwards_then H ltac:(fun HR => try xapp_final HR);    
  try clears R; solver tt.
(* todo: should clear R in indirect subgoals *)

Ltac xapp_spec_core H cont :=
   let arity_goal := spec_goal_arity tt in
   let arity_hyp := spec_term_arity H in
   match constr:(arity_goal, arity_hyp) with (?n,?n) => idtac | _ => fail 1 end;
   let lemma := get_spec_elim_x_y arity_hyp arity_goal in
   eapply lemma; [ sapply H | cont tt ]. 

Ltac xapp_manual_intros tt :=
  let R := fresh "R" in let LR := fresh "L" R in 
  let KR := fresh "K" R in intros R LR KR; lazy beta in KR.

   
Inductive Spec_database : Type :=
  | spec_database : forall A, A -> Spec_database.

Implicit Arguments spec_database [A].

Ltac xapp_core spec cont :=
  let f := spec_goal_fun tt in
  match spec with
  | @spec_database _ ?db =>  
      xfind_by db f; let H := fresh in intro H;
      xapp_spec_core H cont; clear H
  | ___ => 
    first [ xfind_ctx f
          | xfind f ];
    let H := fresh in intro H;
    xapp_spec_core H cont; clear H
  | ?H => xapp_spec_core H cont
  end.

Ltac xapp_pre cont :=
  xextractible tt;
  match ltac_get_tag tt with
  | tag_apply =>
    match post_is_meta tt with
    | false => xgc; [ xuntag tag_apply; cont tt | ]
    | true => xuntag tag_apply; cont tt
    end
  | tag_let_trm => xlet; [ xuntag tag_apply; cont tt | instantiate; xextract ]
  | tag_seq => xseq; [ xuntag tag_apply; cont tt | instantiate; xextract ]
  end.

Ltac xapp_then spec cont :=
  xapp_pre ltac:(fun _ => xapp_core spec cont).

Ltac xapp_with spec args solver :=
  xapp_then spec ltac:(fun _ => xapp_inst args solver).
  

Tactic Notation "xapp" := 
  xapp_with ___ (>>) ltac:(fun _ => idtac).
Tactic Notation "xapp" constr(E) := 
  xapp_with ___ E ltac:(fun _ => idtac).
Tactic Notation "xapp" constr(E1) constr(E2) := 
  xapp (>> E1 E2).
Tactic Notation "xapp" constr(E1) constr(E2) constr(E3) := 
  xapp (>> E1 E2 E3).
Tactic Notation "xapp" constr(E1) constr(E2) constr(E3) constr(E4) := 
  xapp (>> E1 E2 E3 E4).
Tactic Notation "xapp" constr(E1) constr(E2) constr(E3) constr(E4) constr(E5) := 
  xapp (>> E1 E2 E3 E4 E5).

Tactic Notation "xapp" "~" := 
  xapp_with ___ (>>) ltac:(fun _ => xauto~). (* ; xauto~.*)
Tactic Notation "xapp" "~" constr(E) := 
  xapp_with ___ E ltac:(fun _ => xauto~).
Tactic Notation "xapp" "~" constr(E1) constr(E2) := 
  xapp~ (>> E1 E2).
Tactic Notation "xapp" "~" constr(E1) constr(E2) constr(E3) := 
  xapp~ (>> E1 E2 E3).
Tactic Notation "xapp" "~" constr(E1) constr(E2) constr(E3) constr(E4) := 
  xapp~ (>> E1 E2 E3 E4).
Tactic Notation "xapp" "~" constr(E1) constr(E2) constr(E3) constr(E4) constr(E5) :=
   xapp~ (>> E1 E2 E3 E4 E5).

Tactic Notation "xapp" "*" := 
  xapp_with ___ (>>) ltac:(fun _ => xauto*).
Tactic Notation "xapp" "*" constr(E) := 
  xapp_with ___ E ltac:(fun _ => xauto*).
Tactic Notation "xapp" "*" constr(E1) constr(E2) := 
  xapp* (>> E1 E2).
Tactic Notation "xapp" "*" constr(E1) constr(E2) constr(E3) := 
  xapp* (>> E1 E2 E3).
Tactic Notation "xapp" "*" constr(E1) constr(E2) constr(E3) constr(E4) := 
  xapp* (>> E1 E2 E3 E4).
Tactic Notation "xapp" "*" constr(E1) constr(E2) constr(E3) constr(E4) constr(E5) :=
   xapp* (>> E1 E2 E3 E4 E5).

Tactic Notation "xapp_spec" constr(H) := 
  xapp_with H (>>) ltac:(fun _ => idtac).
Tactic Notation "xapp_spec" constr(H) constr(E) := 
  xapp_with H E ltac:(fun _ => idtac).
Tactic Notation "xapp_spec" "~" constr(H) := 
  xapp_with H (>>) ltac:(fun _ => xauto~). (* ; xauto~.*)
Tactic Notation "xapp_spec" "~" constr(H) constr(E) := 
  xapp_with H E ltac:(fun _ => xauto~).
Tactic Notation "xapp_spec" "*" constr(H) := 
  xapp_with H (>>) ltac:(fun _ => xauto*).
Tactic Notation "xapp_spec" "*" constr(H) constr(E) := 
  xapp_with H E ltac:(fun _ => xauto*).

Tactic Notation "xapp_manual" := 
  xapp_then ___ ltac:(xapp_manual_intros).
Tactic Notation "xapp_spec_manual" constr(H) := 
  xapp_then H ltac:(xapp_manual_intros).
Tactic Notation "xapp_manual" "as" := 
  xapp_then ___ ltac:(fun _ => idtac).
Tactic Notation "xapp_spec_manual" constr(H) "as" := 
  xapp_then H ltac:(fun _ => idtac).

Tactic Notation "xapp" "as" simple_intropattern(x) :=
  xlet as x; [ xapp | instantiate; xextract ].

Ltac xapps_core spec args solver := 
  let cont1 tt :=
    xapp_with spec args solver in
  let cont2 tt :=
    instantiate; xextract; try intro_subst in    
  match ltac_get_tag tt with
  | tag_let_trm => xlet; [ cont1 tt | cont2 tt ]
  | tag_seq =>     xseq; [ cont1 tt | cont2 tt ]
  | tag_apply => xapp_with spec args solver
  end.

Tactic Notation "xapps" := 
  xapps_core ___ (>>) ltac:(fun _ => idtac).
Tactic Notation "xapps" constr(E) := 
  xapps_core ___ E ltac:(fun _ => idtac).
Tactic Notation "xapps" constr(E1) constr(E2) := 
  xapps_core (>> E1 E2).
Tactic Notation "xapps" constr(E1) constr(E2) constr(E3) := 
  xapps_core (>> E1 E2 E3).
Tactic Notation "xapps" constr(E1) constr(E2) constr(E3) constr(E4) := 
  xapps_core (>> E1 E2 E3 E4).
Tactic Notation "xapps" constr(E1) constr(E2) constr(E3) constr(E4) constr(E5) := 
  xapps_core (>> E1 E2 E3 E4 E5).

Tactic Notation "xapps" "~" := 
  xapps; auto_tilde.
Tactic Notation "xapps" "*" := 
  xapps; auto_star.
Tactic Notation "xapps" "~" constr(E) := 
  xapps E; auto_tilde.
Tactic Notation "xapps" "*" constr(E) := 
  xapps E; auto_star.

(* todo: when hypothesis in an app instance *)

Tactic Notation "xapp_body" :=
  xuntag; let f := spec_goal_fun tt in
  xfind f; let H := fresh "TEMP" in intro H; 
  eapply app_spec_1; apply H; clear H; try xisspec.

Tactic Notation "xapp_hyp" := (* todo: remove*)
  eapply local_weaken; 
    [ xlocal
    | let f := spec_goal_fun tt in let H := get_spec_hyp f in 
      apply (proj2 H) (* todo generalize to arities*)
    | hsimpl
    | hsimpl ].

charguer's avatar
ready  
charguer committed
1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467

(* TODO: merge with xapp_final, and make xapp work directly *)

Ltac xapp_final' HR dosimpl :=
  eapply local_wframe; 
     [ xlocal
     | eapply HR
     | match dosimpl with
       | true => hsimpl
       | false => idtac
       end
     | try xok ].

Ltac xapp_inst' args solver dosimpl :=
  let R := fresh "R" in let LR := fresh "L" R in 
  let KR := fresh "K" R in let IR := fresh "I" R in
  intros R LR KR; hnf in KR; (* lazy beta in *)
  let H := xapp_compact KR args in
  forwards_then H ltac:(fun HR => try xapp_final' HR dosimpl);    
  try clears R; solver tt.

Ltac xapp_with' spec args solver dosimpl :=
  xapp_then spec ltac:(fun _ => xapp_inst' args solver dosimpl).

Tactic Notation "xapp_nosimpl" := 
  xapp_with' ___ (>>) ltac:(fun _ => idtac) false.
Tactic Notation "xapp_nosimpl" constr(E) := 
  xapp_with' ___ E ltac:(fun _ => idtac) false.
Tactic Notation "xapp_nosimpl" "~" := 
  xapp_with' ___ (>>) ltac:(fun _ => xauto~) false. (* ; xauto~.*)
Tactic Notation "xapp_nosimpl" "~" constr(E) := 
  xapp_with' ___ E ltac:(fun _ => xauto~) false.
Tactic Notation "xapp_nosimpl" "*" := 
  xapp_with' ___ (>>) ltac:(fun _ => xauto*) false.
Tactic Notation "xapp_nosimpl" "*" constr(E) := 
  xapp_with' ___ E ltac:(fun _ => xauto*) false.


Ltac xapp_body_core :=
  xuntag; let f := spec_goal_fun tt in
  xfind f; let H := fresh "TEMP" in intro H; 
  let n := spec_goal_arity tt in 
  let E := get_app_spec_x n in
  eapply E; apply H; clear H; try xisspec;
  try intro_subst_arity n.

Tactic Notation "xapp_body" :=
  xapp_body_core.









charguer's avatar
init  
charguer committed
1468 1469 1470 1471 1472 1473
Ltac xapp_as_base spec args solver x :=
  let cont tt := xapp_inst args solver in
  xlet as x; 
  [ xuntag tag_apply; xapp_core spec cont
  | instantiate; xextract ].

1474
Tactic Notation "xapp" "as" simple_intropattern(x) :=
charguer's avatar
init  
charguer committed
1475
  xapp_as_base ___ (>>) ltac:(fun _ => idtac) x.
1476
Tactic Notation "xapp" "~" "as" simple_intropattern(x) :=
charguer's avatar
init  
charguer committed
1477
  xapp_as_base ___ (>>) ltac:(fun _ => xauto~) x.
1478
Tactic Notation "xapp" "*" "as" simple_intropattern(x) :=
charguer's avatar
init  
charguer committed
1479
  xapp_as_base ___ (>>) ltac:(fun _ => xauto* ) x.
charguer's avatar
charguer committed
1480 1481 1482 1483
Tactic Notation "xapp" constr(E) "as" simple_intropattern(x) :=
  xlet as x; [ xapp E | instantiate; xextract ].
Tactic Notation "xapp" "*" constr(E) "as" simple_intropattern(x) :=
  xapp E as K; auto_star.
charguer's avatar
init  
charguer committed
1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513

(* Specifying the database *)

Tactic Notation "xapp_by" constr(db) := 
  xapp_with (spec_database db) (>>) ltac:(fun _ => idtac).
Tactic Notation "xapp_by" constr(db) constr(E) := 
  xapp_with (spec_database db) E ltac:(fun _ => idtac).
Tactic Notation "xapp_by" "~" constr(db) := 
  xapp_with (spec_database db) (>>) ltac:(fun _ => xauto~ ). 
Tactic Notation "xapp_by" "~" constr(db) constr(E) := 
  xapp_with (spec_database db) E ltac:(fun _ => xauto~).
Tactic Notation "xapp_by" "*" constr(db) := 
  xapp_with (spec_database db) (>>) ltac:(fun _ => xauto* ).
Tactic Notation "xapp_by" "*" constr(db) constr(E) := 
  xapp_with (spec_database db) E ltac:(fun _ => xauto* ).

Tactic Notation "xapps_by" constr(db) := 
  xapps_core (spec_database db) (>>) ltac:(fun _ => idtac).
Tactic Notation "xapps_by" constr(db) constr(E) := 
  xapps_core (spec_database db) E ltac:(fun _ => idtac).
Tactic Notation "xapps_by" "~" constr(db) := 
  xapps_by db; auto_tilde.
Tactic Notation "xapps_by" "*" constr(db) := 
  xapps_by db; auto_star.
Tactic Notation "xapps_by" "~" constr(db) constr(E) := 
  xapps_by db E; auto_tilde.
Tactic Notation "xapps_by" "*" constr(db) constr(E) := 
  xapps_by db E; auto_star.


charguer's avatar
charguer committed
1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530
(** Tactics for debugging xapp *)

Ltac xapp_manual_inst := 
  xapp_inst (>>) ltac:(fun _ => idtac).

Ltac xapp_inst_1 :=
  let R := fresh "R" in let LR := fresh "L" R in 
  let KR := fresh "K" R in let IR := fresh "I" R in
  intros R LR KR; hnf in KR. (* lazy beta in *)

Ltac xapp_inst_2 :=
  match goal with KR: _ |- _ => 
    forwards_then KR ltac:(fun HR => try xapp_final HR)
  end.



charguer's avatar
init  
charguer committed
1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583
(*--------------------------------------------------------*)
(* ** [xapp_partial] *)

Ltac xapp_partial_spec_inner H :=
  let arity_goal := spec_goal_arity tt in
   let arity_hyp := spec_term_arity H in
   let lemma := get_spec_elim_x_y arity_hyp arity_goal in
   applys (>> lemma H).

(* todo: factorize with xapp_final HR *)
Ltac xapp_frame_around cont :=
   eapply local_wframe; [ xlocal | cont tt | hsimpl | try xok ].

Ltac xapp_partial_spec_core H :=
  xapp_frame_around ltac:(fun _ => xapp_partial_spec_inner H).

Ltac xapp_partial_core spec :=
  match spec with
  | ___ =>
      let f := spec_goal_fun tt in
      xfind f; let H := fresh in intro H;
      xapp_partial_spec_core H; clear H
  | ?H => xapp_partial_spec_core H
  end.

Ltac xapp_partial_then spec :=
  xapp_pre ltac:(fun _ => xapp_partial_core spec).

Tactic Notation "xapp_partial" :=
  xapp_partial_then ___.
Tactic Notation "xapp_partial_spec" constr(S) :=
  xapp_partial_then S.

Tactic Notation "xapp_partial" "as" simple_intropattern(x) :=
  xlet as x; [ xapp_partial | instantiate; xextract ].
Tactic Notation "xapp_partial_spec" constr(S) "as" simple_intropattern(x) :=
  xlet as x; [ xapp_partial_spec S | instantiate; xextract ].



(*--------------------------------------------------------*)
(* ** [xinduction] *)

(** [xinduction_heap E] applies to a goal of the form 
    [Spec_n f (fun x1 .. xN R => forall x0, L x0 x1 xN R)] 
    and replaces it with a weaker goal, which describes the same
    specification but including an induction hypothesis. 
    The argument [E] describes the termination arguments. 
    If [f] has type [A1 -> .. -> AN -> B], then [E] should be one of
    - a measure of type [A0*A1*..*AN -> nat] 
    - a binary relation of type [A0*A1*..*AN -> A0*A1*..*AN -> Prop] 
    - a proof that a well-foundedness for such a relation.
    
charguer's avatar
charguer committed
1584 1585
    [xinduction E] is similar but does not depend on [x0]. 

charguer's avatar
init  
charguer committed
1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605
    Measures and binary relations can also be provided in
    a curried fashion, at type [A0 -> A1 -> .. -> AN -> nat] and
    [A0 -> A0 -> A1 -> A1 -> A2 -> A2 -> .. -> AN -> AN -> Prop], respectively.
    
    The combinators [unprojNK] are useful for building new binary
    relations. For example, if [R] has type [A->A->Prop], then
    [unproj21 B R] has type [A*B -> A*B -> Prop] and compares pairs
    with respect to their first component only, using [R]. *)

(* todo: reimplement using  goal_arity and options *)

Ltac xinduction_heap_base_lt lt :=
  first   
    [ apply (spec_induction_1 (lt:=lt))
    | apply (spec_induction_2 (lt:=lt))
    | apply (spec_induction_3 (lt:=lt)) 
    | apply (spec_induction_4 (lt:=lt))
    | apply (spec_induction_2 (lt:=uncurryp2 lt))
    | apply (spec_induction_3 (lt:=uncurryp3 lt))
    | apply (spec_induction_4 (lt:=uncurryp4 lt)) 
charguer's avatar
charguer committed
1606 1607 1608 1609
    | apply (spec_induction_1_noarg (lt:=lt)) 
    | apply (spec_induction_2_noarg (lt:=lt)) 
    | apply (spec_induction_3_noarg (lt:=lt)) 
    | apply (spec_induction_4_noarg (lt:=lt)) ];
charguer's avatar
init  
charguer committed
1610 1611 1612 1613 1614 1615 1616 1617 1618