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

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

26
Tactic Notation "wp_rec" ">" :=
27
  löb ltac:((* Find the redex and apply wp_rec *)
28
              idtac; (* <https://coq.inria.fr/bugs/show_bug.cgi?id=4584> *)
29
               lazymatch goal with
Ralf Jung's avatar
Ralf Jung committed
30 31 32
               | |- _  wp ?E ?e ?Q => reshape_expr e ltac:(fun K e' =>
                        match eval cbv in e' with
                        | App (Rec _ _ _) _ =>
33 34
                          wp_bind K; etrans; [|eapply wp_rec; reflexivity];
                          wp_finish
Ralf Jung's avatar
Ralf Jung committed
35
                        end)
36
               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
       wp_bind K; etrans; [|eapply wp_lam; reflexivity]; wp_finish
45 46
    end)
  end.
47
Tactic Notation "wp_lam" := wp_lam>; try strip_later.
48 49 50 51 52 53

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

54
Tactic Notation "wp_op" ">" :=
55 56 57
  match goal with
  | |- _  wp ?E ?e ?Q => reshape_expr e ltac:(fun K e' =>
    match eval cbv in e' with
58 59 60 61
    | 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 _ _ _ =>
62
       wp_bind K; etrans; [|eapply wp_bin_op; reflexivity]; wp_finish
63
    | UnOp _ _ =>
64
       wp_bind K; etrans; [|eapply wp_un_op; reflexivity]; wp_finish
65 66
    end)
  end.
67
Tactic Notation "wp_op" := wp_op>; try strip_later.
68

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

80 81 82 83 84
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.
85

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

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