wp_tactics.v 3.16 KB
Newer Older
1
From algebra Require Export upred_tactics.
2 3 4
From heap_lang Require Export tactics substitution.
Import uPred.

5
(** wp-specific helper tactics *)
6 7 8
Ltac wp_bind K :=
  lazymatch eval hnf in K with
  | [] => idtac
9
  | _ => etrans; [|solve [ apply (wp_bind K) ]]; simpl
10
  end.
11 12 13
Ltac wp_finish :=
  let rec go :=
  match goal with
14
  | |- _   _ => etrans; [|apply later_mono; go; reflexivity]
15
  | |- _  wp _ _ _ =>
16 17 18 19 20
    etrans; [|eapply wp_value_pvs; reflexivity];
    (* sometimes, we will have to do a final view shift, so only apply
    pvs_intro if we obtain a consecutive wp *)
    try (eapply pvs_intro;
         match goal with |- _  wp _ _ _ => simpl | _ => fail end)
21
  | _ => idtac
22
  end in simpl; intros_revert go.
23

24
Tactic Notation "wp_rec" ">" :=
25 26 27 28 29 30 31
  löb ltac:(
    (* Find the redex and apply wp_rec *)
    idtac; (* <https://coq.inria.fr/bugs/show_bug.cgi?id=4584> *)
    lazymatch goal with
    | |- _  wp ?E ?e ?Q => reshape_expr e ltac:(fun K e' =>
      match eval cbv in e' with
      | App (Rec _ _ _) _ =>
32 33 34
         wp_bind K; etrans;
           [|eapply wp_rec; repeat (reflexivity || rewrite /= to_of_val)];
           wp_finish
35 36
      end)
     end).
37
Tactic Notation "wp_rec" := wp_rec>; try strip_later.
38

39 40 41 42 43
Tactic Notation "wp_lam" ">" :=
  match goal with
  | |- _  wp ?E ?e ?Q => reshape_expr e ltac:(fun K e' =>
    match eval cbv in e' with
    | App (Rec "" _ _) _ =>
44 45 46
       wp_bind K; etrans;
         [|eapply wp_lam; repeat (reflexivity || rewrite /= to_of_val)];
         wp_finish
47 48
    end)
  end.
49
Tactic Notation "wp_lam" := wp_lam>; try strip_later.
50 51 52 53 54 55

Tactic Notation "wp_let" ">" := wp_lam>.
Tactic Notation "wp_let" := wp_lam.
Tactic Notation "wp_seq" ">" := wp_let>.
Tactic Notation "wp_seq" := wp_let.

56
Tactic Notation "wp_op" ">" :=
57 58 59
  match goal with
  | |- _  wp ?E ?e ?Q => reshape_expr e ltac:(fun K e' =>
    match eval cbv in e' with
60 61 62 63
    | BinOp LtOp _ _ => wp_bind K; apply wp_lt; wp_finish
    | BinOp LeOp _ _ => wp_bind K; apply wp_le; wp_finish
    | BinOp EqOp _ _ => wp_bind K; apply wp_eq; wp_finish
    | BinOp _ _ _ =>
64
       wp_bind K; etrans; [|eapply wp_bin_op; reflexivity]; wp_finish
65
    | UnOp _ _ =>
66
       wp_bind K; etrans; [|eapply wp_un_op; reflexivity]; wp_finish
67 68
    end)
  end.
69
Tactic Notation "wp_op" := wp_op>; try strip_later.
70

71
Tactic Notation "wp_if" ">" :=
72 73 74 75
  match goal with
  | |- _  wp ?E ?e ?Q => reshape_expr e ltac:(fun K e' =>
    match eval cbv in e' with
    | If _ _ _ =>
76
       wp_bind K;
77
       etrans; [|apply wp_if_true || apply wp_if_false]; wp_finish
78 79
    end)
  end.
80
Tactic Notation "wp_if" := wp_if>; try strip_later.
81

82 83 84 85 86
Tactic Notation "wp_focus" open_constr(efoc) :=
  match goal with
  | |- _  wp ?E ?e ?Q => reshape_expr e ltac:(fun K e' =>
    match e' with efoc => unify e' efoc; wp_bind K end)
  end.
87

88
Tactic Notation "wp" ">" tactic(tac) :=
89 90 91
  match goal with
  | |- _  wp ?E ?e ?Q => reshape_expr e ltac:(fun K e' => wp_bind K; tac)
  end.
92
Tactic Notation "wp" tactic(tac) := (wp> tac); [try strip_later|..].
93

Ralf Jung's avatar
Ralf Jung committed
94 95
(* In case the precondition does not match.
   TODO: Have one tactic unifying wp and ewp. *)
96 97
Tactic Notation "ewp" tactic(tac) := wp (etrans; [|tac]).
Tactic Notation "ewp" ">" tactic(tac) := wp> (etrans; [|tac]).