wp_tactics.v 3.29 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; [|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
  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
34
35
36
      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;
         [|eapply wp_rec'; repeat rewrite /= to_of_val; reflexivity];
         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
46
47
48
    match eval hnf in e' with App ?e1 _ =>
(*    match eval hnf in e1 with Rec BAnon _ _ => *)
    wp_bind K; etrans;
       [|eapply wp_lam; repeat (reflexivity || rewrite /= to_of_val)];
       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; reflexivity]; wp_finish
66
    | UnOp _ _ =>
67
       wp_bind K; etrans; [|eapply wp_un_op; reflexivity]; wp_finish
68
69
    end)
  end.
70
Tactic Notation "wp_op" := wp_op>; try strip_later.
71

72
Tactic Notation "wp_if" ">" :=
73
74
  match goal with
  | |- _  wp ?E ?e ?Q => reshape_expr e ltac:(fun K e' =>
75
76
77
    match eval hnf in e' with If _ _ _ =>
    wp_bind K;
    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]).