tactics.v 7.61 KB
Newer Older
Ralf Jung's avatar
Ralf Jung committed
1
From stdpp Require Import gmap.
2
From iris.base_logic Require Export base_logic big_op.
3
Set Default Proof Using "Type".
4
Import uPred.
5

6
Module uPred_reflection. Section uPred_reflection.
7
  Context {M : ucmraT}.
8 9 10 11 12 13 14 15

  Inductive expr :=
    | ETrue : expr
    | EVar : nat  expr
    | ESep : expr  expr  expr.
  Fixpoint eval (Σ : list (uPred M)) (e : expr) : uPred M :=
    match e with
    | ETrue => True
16
    | EVar n => from_option id True%I (Σ !! n)
17
    | ESep e1 e2 => eval Σ e1  eval Σ e2
18 19 20 21 22 23 24 25
    end.
  Fixpoint flatten (e : expr) : list nat :=
    match e with
    | ETrue => []
    | EVar n => [n]
    | ESep e1 e2 => flatten e1 ++ flatten e2
    end.

26 27
  Notation eval_list Σ l := ([ list] n  l, from_option id True (Σ !! n))%I.

28
  Lemma eval_flatten Σ e : eval Σ e ⊣⊢ eval_list Σ (flatten e).
29 30
  Proof.
    induction e as [| |e1 IH1 e2 IH2];
31
      rewrite /= ?right_id ?big_opL_app ?IH1 ?IH2 //.
32 33
  Qed.
  Lemma flatten_entails Σ e1 e2 :
Robbert Krebbers's avatar
Robbert Krebbers committed
34
    flatten e2 + flatten e1  eval Σ e1  eval Σ e2.
35
  Proof. intros. rewrite !eval_flatten. by apply big_sepL_submseteq. Qed.
36
  Lemma flatten_equiv Σ e1 e2 :
37
    flatten e2 ≡ₚ flatten e1  eval Σ e1 ⊣⊢ eval Σ e2.
38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55
  Proof. intros He. by rewrite !eval_flatten He. Qed.

  Fixpoint prune (e : expr) : expr :=
    match e with
    | ETrue => ETrue
    | EVar n => EVar n
    | ESep e1 e2 =>
       match prune e1, prune e2 with
       | ETrue, e2' => e2'
       | e1', ETrue => e1'
       | e1', e2' => ESep e1' e2'
       end
    end.
  Lemma flatten_prune e : flatten (prune e) = flatten e.
  Proof.
    induction e as [| |e1 IH1 e2 IH2]; simplify_eq/=; auto.
    rewrite -IH1 -IH2. by repeat case_match; rewrite ?right_id_L.
  Qed.
56
  Lemma prune_correct Σ e : eval Σ (prune e) ⊣⊢ eval Σ e.
57 58 59 60 61 62 63 64 65 66 67 68
  Proof. by rewrite !eval_flatten flatten_prune. Qed.

  Fixpoint cancel_go (n : nat) (e : expr) : option expr :=
    match e with
    | ETrue => None
    | EVar n' => if decide (n = n') then Some ETrue else None
    | ESep e1 e2 => 
       match cancel_go n e1 with
       | Some e1' => Some (ESep e1' e2)
       | None => ESep e1 <$> cancel_go n e2
       end
    end.
69 70
  Definition cancel (ns : list nat) (e: expr) : option expr :=
    prune <$> fold_right (mbind  cancel_go) (Some e) ns.
71 72 73 74 75
  Lemma flatten_cancel_go e e' n :
    cancel_go n e = Some e'  flatten e ≡ₚ n :: flatten e'.
  Proof.
    revert e'; induction e as [| |e1 IH1 e2 IH2]; intros;
      repeat (simplify_option_eq || case_match); auto.
76 77
    - by rewrite IH1 //.
    - by rewrite IH2 // Permutation_middle.
78
  Qed.
79 80
  Lemma flatten_cancel e e' ns :
    cancel ns e = Some e'  flatten e ≡ₚ ns ++ flatten e'.
81
  Proof.
82 83 84
    rewrite /cancel fmap_Some=> -[{e'}e' [He ->]]; rewrite flatten_prune.
    revert e' He; induction ns as [|n ns IH]=> e' He; simplify_option_eq; auto.
    rewrite Permutation_middle -flatten_cancel_go //; eauto.
85
  Qed.
86 87
  Lemma cancel_entails Σ e1 e2 e1' e2' ns :
    cancel ns e1 = Some e1'  cancel ns e2 = Some e2' 
88
    (eval Σ e1'  eval Σ e2')  eval Σ e1  eval Σ e2.
89 90
  Proof.
    intros ??. rewrite !eval_flatten.
91
    rewrite (flatten_cancel e1 e1' ns) // (flatten_cancel e2 e2' ns) //; csimpl.
92
    rewrite !big_opL_app. apply sep_mono_r.
93 94
  Qed.

95 96 97 98 99 100 101
  Fixpoint to_expr (l : list nat) : expr :=
    match l with
    | [] => ETrue
    | [n] => EVar n
    | n :: l => ESep (EVar n) (to_expr l)
    end.
  Arguments to_expr !_ / : simpl nomatch.
102
  Lemma eval_to_expr Σ l : eval Σ (to_expr l) ⊣⊢ eval_list Σ l.
103 104 105 106 107
  Proof.
    induction l as [|n1 [|n2 l] IH]; csimpl; rewrite ?right_id //.
    by rewrite IH.
  Qed.

108
  Lemma split_l Σ e ns e' :
109
    cancel ns e = Some e'  eval Σ e ⊣⊢ (eval Σ (to_expr ns)  eval Σ e').
110
  Proof.
111
    intros He%flatten_cancel.
112
    by rewrite eval_flatten He big_opL_app eval_to_expr eval_flatten.
113
  Qed.
114
  Lemma split_r Σ e ns e' :
115
    cancel ns e = Some e'  eval Σ e ⊣⊢ (eval Σ e'  eval Σ (to_expr ns)).
116 117
  Proof. intros. rewrite /= comm. by apply split_l. Qed.

118 119 120 121 122
  Class Quote (Σ1 Σ2 : list (uPred M)) (P : uPred M) (e : expr) := {}.
  Global Instance quote_True Σ : Quote Σ Σ True ETrue.
  Global Instance quote_var Σ1 Σ2 P i:
    rlist.QuoteLookup Σ1 Σ2 P i  Quote Σ1 Σ2 P (EVar i) | 1000.
  Global Instance quote_sep Σ1 Σ2 Σ3 P1 P2 e1 e2 :
123
    Quote Σ1 Σ2 P1 e1  Quote Σ2 Σ3 P2 e2  Quote Σ1 Σ3 (P1  P2) (ESep e1 e2).
124 125 126 127 128 129

  Class QuoteArgs (Σ: list (uPred M)) (Ps: list (uPred M)) (ns: list nat) := {}.
  Global Instance quote_args_nil Σ : QuoteArgs Σ nil nil.
  Global Instance quote_args_cons Σ Ps P ns n :
    rlist.QuoteLookup Σ Σ P n 
    QuoteArgs Σ Ps ns  QuoteArgs Σ (P :: Ps) (n :: ns).
130
  End uPred_reflection.
131 132 133

  Ltac quote :=
    match goal with
134
    | |- ?P1  ?P2 =>
135 136
      lazymatch type of (_ : Quote [] _ P1 _) with Quote _ ?Σ2 _ ?e1 =>
      lazymatch type of (_ : Quote Σ2 _ P2 _) with Quote _ ?Σ3 _ ?e2 =>
137
        change (eval Σ3 e1  eval Σ3 e2) end end
138 139 140
    end.
  Ltac quote_l :=
    match goal with
141
    | |- ?P1  ?P2 =>
142
      lazymatch type of (_ : Quote [] _ P1 _) with Quote _ ?Σ2 _ ?e1 =>
143
        change (eval Σ2 e1  P2) end
144
    end.
145
End uPred_reflection.
146

147
Tactic Notation "solve_sep_entails" :=
148
  uPred_reflection.quote; apply uPred_reflection.flatten_entails;
149 150
  apply (bool_decide_unpack _); vm_compute; exact Logic.I.

151 152 153 154 155 156 157 158 159 160 161
Ltac close_uPreds Ps tac :=
  let M := match goal with |- @uPred_entails ?M _ _ => M end in
  let rec go Ps Qs :=
    lazymatch Ps with
    | [] => let Qs' := eval cbv [reverse rev_append] in (reverse Qs) in tac Qs'
    | ?P :: ?Ps => find_pat P ltac:(fun Q => go Ps (Q :: Qs))
    end in
  (* avoid evars in case Ps = @nil ?A *)
  try match Ps with [] => unify Ps (@nil (uPred M)) end;
  go Ps (@nil (uPred M)).

162
Tactic Notation "cancel" constr(Ps) :=
163
  uPred_reflection.quote;
164
  let Σ := match goal with |- uPred_reflection.eval ?Σ _  _ => Σ end in
165 166 167 168 169
  let ns' := lazymatch type of (_ : uPred_reflection.QuoteArgs Σ Ps _) with
             | uPred_reflection.QuoteArgs _ _ ?ns' => ns'
             end in
  eapply uPred_reflection.cancel_entails with (ns:=ns');
    [cbv; reflexivity|cbv; reflexivity|simpl].
170 171

Tactic Notation "ecancel" open_constr(Ps) :=
172
  close_uPreds Ps ltac:(fun Qs => cancel Qs).
173

174
(** [to_front [P1, P2, ..]] rewrites in the premise of  such that
175 176
    the assumptions P1, P2, ... appear at the front, in that order. *)
Tactic Notation "to_front" open_constr(Ps) :=
177 178
  close_uPreds Ps ltac:(fun Ps =>
    uPred_reflection.quote_l;
179
    let Σ := match goal with |- uPred_reflection.eval ?Σ _  _ => Σ end in
180 181 182 183
    let ns' := lazymatch type of (_ : uPred_reflection.QuoteArgs Σ Ps _) with
               | uPred_reflection.QuoteArgs _ _ ?ns' => ns'
               end in
    eapply entails_equiv_l;
184
      first (apply uPred_reflection.split_l with (ns:=ns'); cbv; reflexivity);
185 186 187 188 189
      simpl).

Tactic Notation "to_back" open_constr(Ps) :=
  close_uPreds Ps ltac:(fun Ps =>
    uPred_reflection.quote_l;
190
    let Σ := match goal with |- uPred_reflection.eval ?Σ _  _ => Σ end in
191 192 193 194
    let ns' := lazymatch type of (_ : uPred_reflection.QuoteArgs Σ Ps _) with
               | uPred_reflection.QuoteArgs _ _ ?ns' => ns'
               end in
    eapply entails_equiv_l;
195
      first (apply uPred_reflection.split_r with (ns:=ns'); cbv; reflexivity);
196
      simpl).
197

198
(** [sep_split] is used to introduce a ().
199 200 201 202
    Use [sep_split left: [P1, P2, ...]] to define which assertions will be
    taken to the left; the rest will be available on the right.
    [sep_split right: [P1, P2, ...]] works the other way around. *)
Tactic Notation "sep_split" "right:" open_constr(Ps) :=
203
  to_back Ps; apply sep_mono.
204
Tactic Notation "sep_split" "left:" open_constr(Ps) :=
205
  to_front Ps; apply sep_mono.