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

5
(** wp-specific helper tactics *)
6 7 8
Ltac wp_bind K :=
  lazymatch eval hnf in K with
  | [] => idtac
9
  | _ => etrans; [|fast_by apply (wp_bind K)]; simpl
10
  end.
11 12 13
Ltac wp_finish :=
  let rec go :=
  match goal with
14
  | |- _   _ => etrans; [|fast_by apply later_mono; go]
15
  | |- _  wp _ _ _ =>
16
    etrans; [|eapply wp_value_pvs; fast_done];
17 18 19 20
    (* 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
  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' =>
30 31 32 33
      match eval hnf in e' with App ?e1 _ =>
(* hnf does not reduce through an of_val *)
(*      match eval hnf in e1 with Rec _ _ _ => *)
      wp_bind K; etrans;
34
         [|eapply wp_rec'; repeat rewrite /= to_of_val; fast_done];
35 36
         simpl_subst; wp_finish
(*      end *) end)
37
     end).
38
Tactic Notation "wp_rec" := wp_rec>; try strip_later.
39

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

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

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

72 73 74 75 76 77 78 79 80 81 82 83
Tactic Notation "wp_proj" ">" :=
  match goal with
  | |- _  wp ?E ?e ?Q => reshape_expr e ltac:(fun K e' =>
    match eval hnf in e' with
    | Fst _ =>
       wp_bind K; etrans; [|eapply wp_fst; try fast_done]; wp_finish
    | Snd _ =>
       wp_bind K; etrans; [|eapply wp_snd; try fast_done]; wp_finish
    end)
  end.
Tactic Notation "wp_proj" := wp_proj>; try strip_later.

84
Tactic Notation "wp_if" ">" :=
85 86
  match goal with
  | |- _  wp ?E ?e ?Q => reshape_expr e ltac:(fun K e' =>
87 88
    match eval hnf in e' with If _ _ _ =>
    wp_bind K;
89
    etrans; [|eapply wp_if_true || eapply wp_if_false]; wp_finish
90 91
    end)
  end.
92
Tactic Notation "wp_if" := wp_if>; try strip_later.
93

94 95 96 97 98 99 100 101 102 103
Tactic Notation "wp_case" ">" :=
  match goal with
  | |- _  wp ?E ?e ?Q => reshape_expr e ltac:(fun K e' =>
    match eval hnf in e' with Case _ _ _ =>
    wp_bind K;
    etrans; [|eapply wp_case_inl || eapply wp_case_inr]; wp_finish
    end)
  end.
Tactic Notation "wp_case" := wp_case>; try strip_later.

104 105 106 107 108
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.
109

110
Tactic Notation "wp" ">" tactic(tac) :=
111 112 113
  match goal with
  | |- _  wp ?E ?e ?Q => reshape_expr e ltac:(fun K e' => wp_bind K; tac)
  end.
114
Tactic Notation "wp" tactic(tac) := (wp> tac); [try strip_later|..].
115

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