wp_tactics.v 3.93 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
25
Ltac wp_done := rewrite -/of_val /= ?to_of_val; fast_done.

26
Tactic Notation "wp_rec" ">" :=
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' =>
32
33
34
      match eval hnf in e' with App ?e1 _ =>
(* hnf does not reduce through an of_val *)
(*      match eval hnf in e1 with Rec _ _ _ => *)
35
      wp_bind K; etrans; [|eapply wp_rec'; wp_done]; simpl_subst; wp_finish
36
(*      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
    match eval hnf in e' with App ?e1 _ =>
(*    match eval hnf in e1 with Rec BAnon _ _ => *)
45
    wp_bind K; etrans; [|eapply wp_lam; wp_done]; simpl_subst; wp_finish
46
(*    end *) end)
47
  end.
48
Tactic Notation "wp_lam" := wp_lam>; try strip_later.
49
50
51
52
53
54

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

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

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

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

90
91
92
93
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 _ _ _ =>
94
95
96
      wp_bind K;
      etrans; [|first[eapply wp_case_inl; wp_done|eapply wp_case_inr; wp_done]];
      wp_finish
97
98
99
100
    end)
  end.
Tactic Notation "wp_case" := wp_case>; try strip_later.

101
102
103
104
105
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.
106

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

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