wp_tactics.v 3.23 KB
Newer Older
1
2
3
From heap_lang Require Export tactics substitution.
Import uPred.

4
5
6
7
Ltac revert_intros tac :=
  lazymatch goal with
  | |-  _, _ => let H := fresh in intro H; revert_intros tac; revert H
  | |- _ => tac
8
  end.
9
10
11
12
13
14
15
16
17
Ltac wp_strip_later :=
  let rec go :=
    lazymatch goal with
    | |- _  (_  _) => apply sep_mono; go
    | |- _   _ => apply later_intro
    | |- _  _ => reflexivity
    end
  in revert_intros ltac:(etransitivity; [|go]).

18
19
20
Ltac wp_bind K :=
  lazymatch eval hnf in K with
  | [] => idtac
21
  | _ => etransitivity; [|solve [ apply (wp_bind K) ]]; simpl
22
  end.
23
24
25
26
Ltac wp_finish :=
  let rec go :=
  match goal with
  | |- _   _ => etransitivity; [|apply later_mono; go; reflexivity]
27
  | |- _  wp _ _ _ =>
28
     etransitivity; [|eapply wp_value_pvs; reflexivity];
29
30
     (* sometimes, we will have to do a final view shift, so only apply
     wp_value if we obtain a consecutive wp *)
31
32
     try (eapply pvs_intro;
          match goal with |- _  wp _ _ _ => simpl | _ => fail end)
33
  | _ => idtac
34
  end in simpl; revert_intros go.
35

36
Tactic Notation "wp_rec" ">" :=
37
38
39
  match goal with
  | |- _  wp ?E ?e ?Q => reshape_expr e ltac:(fun K e' =>
    match eval cbv in e' with
40
41
    | App (Rec _ _ _) _ =>
       wp_bind K; etransitivity; [|eapply wp_rec; reflexivity]; wp_finish
42
43
    end)
  end.
44
Tactic Notation "wp_rec" := wp_rec>; wp_strip_later.
45

46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
Tactic Notation "wp_lam" ">" :=
  match goal with
  | |- _  wp ?E ?e ?Q => reshape_expr e ltac:(fun K e' =>
    match eval cbv in e' with
    | App (Rec "" _ _) _ =>
       wp_bind K; etransitivity; [|eapply wp_lam; reflexivity]; wp_finish
    end)
  end.
Tactic Notation "wp_lam" := wp_lam>; wp_strip_later.

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

61
Tactic Notation "wp_op" ">" :=
62
63
64
  match goal with
  | |- _  wp ?E ?e ?Q => reshape_expr e ltac:(fun K e' =>
    match eval cbv in e' with
65
66
67
68
69
70
71
    | 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 _ _ _ =>
       wp_bind K; etransitivity; [|eapply wp_bin_op; reflexivity]; wp_finish
    | UnOp _ _ =>
       wp_bind K; etransitivity; [|eapply wp_un_op; reflexivity]; wp_finish
72
73
    end)
  end.
74
75
Tactic Notation "wp_op" := wp_op>; wp_strip_later.

76
Tactic Notation "wp_if" ">" :=
77
78
79
80
  match goal with
  | |- _  wp ?E ?e ?Q => reshape_expr e ltac:(fun K e' =>
    match eval cbv in e' with
    | If _ _ _ =>
81
82
       wp_bind K;
       etransitivity; [|apply wp_if_true || apply wp_if_false]; wp_finish
83
84
    end)
  end.
85
Tactic Notation "wp_if" := wp_if>; wp_strip_later.
86

87
88
89
90
91
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.
92

93
Tactic Notation "wp" ">" tactic(tac) :=
94
95
96
  match goal with
  | |- _  wp ?E ?e ?Q => reshape_expr e ltac:(fun K e' => wp_bind K; tac)
  end.
97
Tactic Notation "wp" tactic(tac) := (wp> tac); wp_strip_later.
98
99
100
101

(* In case the precondition does not match *)
Tactic Notation "ewp" tactic(tac) := wp (etransitivity; [|tac]).
Tactic Notation "ewp" ">" tactic(tac) := wp> (etransitivity; [|tac]).