wp_tactics.v 3.81 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 25
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.

Ltac wp_finish := intros_revert ltac:(
26
  rewrite /= ?to_of_val; try strip_later; try wp_value_head).
Robbert Krebbers's avatar
Robbert Krebbers committed
27 28

Tactic Notation "wp_value" :=
29
  lazymatch goal with
Robbert Krebbers's avatar
Robbert Krebbers committed
30
  | |- _  wp ?E ?e ?Q => reshape_expr e ltac:(fun K e' =>
31 32
    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
33 34
  end.

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

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

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

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

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

84
Tactic Notation "wp_if" :=
85
  lazymatch goal with
86
  | |- _  wp ?E ?e ?Q => reshape_expr e ltac:(fun K e' =>
87 88 89 90 91 92
    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'"
93
  end.
94

95
Tactic Notation "wp_match" :=
96
  lazymatch goal with
97
  | |- _  wp ?E ?e ?Q => reshape_expr e ltac:(fun K e' =>
98 99
    match eval hnf in e' with
    | Case _ _ _ =>
100
      wp_bind K;
101 102 103 104
      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'"
105 106
  end.

107
Tactic Notation "wp_focus" open_constr(efoc) :=
108
  lazymatch goal with
109
  | |- _  wp ?E ?e ?Q => reshape_expr e ltac:(fun K e' =>
110 111 112 113
    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'"
114
  end.