wp_tactics.v 3.96 KB
Newer Older
1
2
From iris.algebra Require Export upred_tactics.
From iris.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
Ltac wp_done := rewrite /= ?to_of_val; fast_done.
13

Robbert Krebbers's avatar
Robbert Krebbers committed
14
15
16
17
18
19
20
21
22
23
24
Ltac wp_value_head :=
  match goal with
  | |- _  wp _ _ _ =>
    etrans; [|eapply wp_value_pvs; wp_done]; lazy beta;
    (* sometimes, we will have to do a final view shift, so only apply
    pvs_intro if we obtain a consecutive wp *)
    try (
      etrans; [|apply pvs_intro];
      match goal with |- _  wp _ _ _ => simpl | _ => fail end)
  end.

25
26
27
28
29
Ltac wp_seq_head :=
  lazymatch goal with
  | |- _  wp ?E (Seq _ _) ?Q => etrans; [|eapply wp_seq; wp_done]; strip_later
  end.

Robbert Krebbers's avatar
Robbert Krebbers committed
30
Ltac wp_finish := intros_revert ltac:(
31
32
  rewrite /= ?to_of_val; try strip_later; try wp_value_head);
  repeat wp_seq_head.
Robbert Krebbers's avatar
Robbert Krebbers committed
33
34

Tactic Notation "wp_value" :=
35
  lazymatch goal with
Robbert Krebbers's avatar
Robbert Krebbers committed
36
  | |- _  wp ?E ?e ?Q => reshape_expr e ltac:(fun K e' =>
37
38
    wp_bind K; wp_value_head) || fail "wp_value: cannot find value in" e
  | _ => fail "wp_value: not a wp"
Robbert Krebbers's avatar
Robbert Krebbers committed
39
40
  end.

41
Tactic Notation "wp_rec" :=
Robbert Krebbers's avatar
Robbert Krebbers committed
42
43
44
  lazymatch goal with
  | |- _  wp ?E ?e ?Q => reshape_expr e ltac:(fun K e' =>
    match eval hnf in e' with App ?e1 _ =>
45
46
(* hnf does not reduce through an of_val *)
(*      match eval hnf in e1 with Rec _ _ _ => *)
47
    wp_bind K; etrans; [|eapply wp_rec; wp_done]; simpl_subst; wp_finish
48
49
50
(*      end *) end) || fail "wp_rec: cannot find 'Rec' in" e
  | _ => fail "wp_rec: not a 'wp'"
  end.
51

52
Tactic Notation "wp_lam" :=
53
  lazymatch goal with
54
  | |- _  wp ?E ?e ?Q => reshape_expr e ltac:(fun K e' =>
55
56
    match eval hnf in e' with App ?e1 _ =>
(*    match eval hnf in e1 with Rec BAnon _ _ => *)
57
    wp_bind K; etrans; [|eapply wp_lam; wp_done]; simpl_subst; wp_finish
58
59
(*    end *) end) || fail "wp_lam: cannot find 'Lam' in" e
  | _ => fail "wp_lam: not a 'wp'"
60
61
62
63
64
  end.

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

65
Tactic Notation "wp_op" :=
66
  lazymatch goal with
67
  | |- _  wp ?E ?e ?Q => reshape_expr e ltac:(fun K e' =>
68
    match eval hnf in e' with
69
70
71
72
    | 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 _ _ _ =>
73
       wp_bind K; etrans; [|eapply wp_bin_op; try fast_done]; wp_finish
74
    | UnOp _ _ =>
75
       wp_bind K; etrans; [|eapply wp_un_op; try fast_done]; wp_finish
76
77
    end) || fail "wp_op: cannot find 'BinOp' or 'UnOp' in" e
  | _ => fail "wp_op: not a 'wp'"
78
  end.
79

80
Tactic Notation "wp_proj" :=
81
  lazymatch goal with
82
  | |- _  wp ?E ?e ?Q => reshape_expr e ltac:(fun K e' =>
83
    match eval hnf in e' with
84
85
    | Fst _ => wp_bind K; etrans; [|eapply wp_fst; wp_done]; wp_finish
    | Snd _ => wp_bind K; etrans; [|eapply wp_snd; wp_done]; wp_finish
86
87
    end) || fail "wp_proj: cannot find 'Fst' or 'Snd' in" e
  | _ => fail "wp_proj: not a 'wp'"
88
89
  end.

90
Tactic Notation "wp_if" :=
91
  lazymatch goal with
92
  | |- _  wp ?E ?e ?Q => reshape_expr e ltac:(fun K e' =>
93
94
95
96
97
98
    match eval hnf in e' with
    | If _ _ _ =>
      wp_bind K;
      etrans; [|eapply wp_if_true || eapply wp_if_false]; wp_finish
    end) || fail "wp_if: cannot find 'If' in" e
  | _ => fail "wp_if: not a 'wp'"
99
  end.
100

101
Tactic Notation "wp_match" :=
102
  lazymatch goal with
103
  | |- _  wp ?E ?e ?Q => reshape_expr e ltac:(fun K e' =>
104
105
    match eval hnf in e' with
    | Case _ _ _ =>
106
      wp_bind K;
107
108
109
110
      etrans; [|first[eapply wp_match_inl; wp_done|eapply wp_match_inr; wp_done]];
      simpl_subst; wp_finish
    end) || fail "wp_match: cannot find 'Match' in" e
  | _ => fail "wp_match: not a 'wp'"
111
112
  end.

113
Tactic Notation "wp_focus" open_constr(efoc) :=
114
  lazymatch goal with
115
  | |- _  wp ?E ?e ?Q => reshape_expr e ltac:(fun K e' =>
116
117
118
119
    match e' with
    | efoc => unify e' efoc; wp_bind K
    end) || fail "wp_focus: cannot find" efoc "in" e
  | _ => fail "wp_focus: not a 'wp'"
120
  end.