coq_tactics.v 58.2 KB
Newer Older
Robbert Krebbers's avatar
Robbert Krebbers committed
1 2
From iris.bi Require Export bi.
From iris.bi Require Import tactics.
3
From iris.proofmode Require Export base environments classes.
4
Set Default Proof Using "Type".
Robbert Krebbers's avatar
Robbert Krebbers committed
5
Import bi.
6
Import env_notations.
Robbert Krebbers's avatar
Robbert Krebbers committed
7

8 9
Local Notation "b1 && b2" := (if b1 then b2 else false) : bool_scope.

Robbert Krebbers's avatar
Robbert Krebbers committed
10 11
Record envs (PROP : bi) :=
  Envs { env_persistent : env PROP; env_spatial : env PROP }.
Robbert Krebbers's avatar
Robbert Krebbers committed
12 13 14 15 16
Add Printing Constructor envs.
Arguments Envs {_} _ _.
Arguments env_persistent {_} _.
Arguments env_spatial {_} _.

Robbert Krebbers's avatar
Robbert Krebbers committed
17
Record envs_wf {PROP} (Δ : envs PROP) := {
Robbert Krebbers's avatar
Robbert Krebbers committed
18 19 20 21 22
  env_persistent_valid : env_wf (env_persistent Δ);
  env_spatial_valid : env_wf (env_spatial Δ);
  envs_disjoint i : env_persistent Δ !! i = None  env_spatial Δ !! i = None
}.

23
Definition of_envs {PROP} (Δ : envs PROP) : PROP :=
24
  (envs_wf Δ⌝   [] env_persistent Δ  [] env_spatial Δ)%I.
25
Instance: Params (@of_envs) 1.
Robbert Krebbers's avatar
Robbert Krebbers committed
26
Arguments of_envs : simpl never.
27

28 29 30 31 32 33 34
(* We seal [envs_entails], so that it does not get unfolded by the
   proofmode's own tactics, such as [iIntros (?)]. *)
Definition envs_entails_aux : seal (λ PROP (Δ : envs PROP) (Q : PROP), (of_envs Δ  Q)).
Proof. by eexists. Qed.
Definition envs_entails := unseal envs_entails_aux.
Definition envs_entails_eq : envs_entails = _ := seal_eq envs_entails_aux.
Arguments envs_entails {PROP} Δ Q%I : rename.
35 36
Instance: Params (@envs_entails) 1.

Robbert Krebbers's avatar
Robbert Krebbers committed
37
Record envs_Forall2 {PROP : bi} (R : relation PROP) (Δ1 Δ2 : envs PROP) := {
38 39 40
  env_persistent_Forall2 : env_Forall2 R (env_persistent Δ1) (env_persistent Δ2);
  env_spatial_Forall2 : env_Forall2 R (env_spatial Δ1) (env_spatial Δ2)
}.
Robbert Krebbers's avatar
Robbert Krebbers committed
41

42
Definition envs_dom {PROP} (Δ : envs PROP) : list ident :=
43
  env_dom (env_persistent Δ) ++ env_dom (env_spatial Δ).
Robbert Krebbers's avatar
Robbert Krebbers committed
44

45
Definition envs_lookup {PROP} (i : ident) (Δ : envs PROP) : option (bool * PROP) :=
Robbert Krebbers's avatar
Robbert Krebbers committed
46 47
  let (Γp,Γs) := Δ in
  match env_lookup i Γp with
Robbert Krebbers's avatar
Robbert Krebbers committed
48 49
  | Some P => Some (true, P)
  | None => P  env_lookup i Γs; Some (false, P)
Robbert Krebbers's avatar
Robbert Krebbers committed
50 51
  end.

52 53
Definition envs_delete {PROP} (remove_persistent : bool)
    (i : ident) (p : bool) (Δ : envs PROP) : envs PROP :=
Robbert Krebbers's avatar
Robbert Krebbers committed
54 55
  let (Γp,Γs) := Δ in
  match p with
56
  | true => Envs (if remove_persistent then env_delete i Γp else Γp) Γs
Robbert Krebbers's avatar
Robbert Krebbers committed
57
  | false => Envs Γp (env_delete i Γs)
Robbert Krebbers's avatar
Robbert Krebbers committed
58 59
  end.

60 61
Definition envs_lookup_delete {PROP} (remove_persistent : bool)
    (i : ident) (Δ : envs PROP) : option (bool * PROP * envs PROP) :=
Robbert Krebbers's avatar
Robbert Krebbers committed
62 63
  let (Γp,Γs) := Δ in
  match env_lookup_delete i Γp with
64
  | Some (P,Γp') => Some (true, P, Envs (if remove_persistent then Γp' else Γp) Γs)
Robbert Krebbers's avatar
Robbert Krebbers committed
65
  | None => ''(P,Γs')  env_lookup_delete i Γs; Some (false, P, Envs Γp Γs')
Robbert Krebbers's avatar
Robbert Krebbers committed
66 67
  end.

68 69
Fixpoint envs_lookup_delete_list {PROP} (remove_persistent : bool)
    (js : list ident) (Δ : envs PROP) : option (bool * list PROP * envs PROP) :=
70 71 72
  match js with
  | [] => Some (true, [], Δ)
  | j :: js =>
73 74 75
     ''(p,P,Δ')  envs_lookup_delete remove_persistent j Δ;
     ''(q,Hs,Δ'')  envs_lookup_delete_list remove_persistent js Δ';
     Some ((p:bool) && q, P :: Hs, Δ'')
76 77
  end.

Robbert Krebbers's avatar
Robbert Krebbers committed
78
Definition envs_snoc {PROP} (Δ : envs PROP)
79
    (p : bool) (j : ident) (P : PROP) : envs PROP :=
80 81 82
  let (Γp,Γs) := Δ in
  if p then Envs (Esnoc Γp j P) Γs else Envs Γp (Esnoc Γs j P).

Robbert Krebbers's avatar
Robbert Krebbers committed
83 84
Definition envs_app {PROP : bi} (p : bool)
    (Γ : env PROP) (Δ : envs PROP) : option (envs PROP) :=
Robbert Krebbers's avatar
Robbert Krebbers committed
85 86 87 88 89 90
  let (Γp,Γs) := Δ in
  match p with
  | true => _  env_app Γ Γs; Γp'  env_app Γ Γp; Some (Envs Γp' Γs)
  | false => _  env_app Γ Γp; Γs'  env_app Γ Γs; Some (Envs Γp Γs')
  end.

91
Definition envs_simple_replace {PROP : bi} (i : ident) (p : bool)
Robbert Krebbers's avatar
Robbert Krebbers committed
92
    (Γ : env PROP) (Δ : envs PROP) : option (envs PROP) :=
Robbert Krebbers's avatar
Robbert Krebbers committed
93 94 95 96 97 98
  let (Γp,Γs) := Δ in
  match p with
  | true => _  env_app Γ Γs; Γp'  env_replace i Γ Γp; Some (Envs Γp' Γs)
  | false => _  env_app Γ Γp; Γs'  env_replace i Γ Γs; Some (Envs Γp Γs')
  end.

99
Definition envs_replace {PROP : bi} (i : ident) (p q : bool)
Robbert Krebbers's avatar
Robbert Krebbers committed
100
    (Γ : env PROP) (Δ : envs PROP) : option (envs PROP) :=
Robbert Krebbers's avatar
Robbert Krebbers committed
101
  if eqb p q then envs_simple_replace i p Γ Δ
102
  else envs_app q Γ (envs_delete true i p Δ).
Robbert Krebbers's avatar
Robbert Krebbers committed
103

Robbert Krebbers's avatar
Robbert Krebbers committed
104
Definition env_spatial_is_nil {PROP} (Δ : envs PROP) : bool :=
Robbert Krebbers's avatar
Robbert Krebbers committed
105 106
  if env_spatial Δ is Enil then true else false.

Robbert Krebbers's avatar
Robbert Krebbers committed
107
Definition envs_clear_spatial {PROP} (Δ : envs PROP) : envs PROP :=
108 109
  Envs (env_persistent Δ) Enil.

Robbert Krebbers's avatar
Robbert Krebbers committed
110
Definition envs_clear_persistent {PROP} (Δ : envs PROP) : envs PROP :=
111 112
  Envs Enil (env_spatial Δ).

Robbert Krebbers's avatar
Robbert Krebbers committed
113
Fixpoint envs_split_go {PROP}
114
    (js : list ident) (Δ1 Δ2 : envs PROP) : option (envs PROP * envs PROP) :=
115 116 117
  match js with
  | [] => Some (Δ1, Δ2)
  | j :: js =>
118
     ''(p,P,Δ1')  envs_lookup_delete true j Δ1;
Robbert Krebbers's avatar
Robbert Krebbers committed
119
     if p : bool then envs_split_go js Δ1 Δ2 else
120 121
     envs_split_go js Δ1' (envs_snoc Δ2 false j P)
  end.
122 123
(* if [d = Right] then [result = (remaining hyps, hyps named js)] and
   if [d = Left] then [result = (hyps named js, remaining hyps)] *)
124
Definition envs_split {PROP} (d : direction)
125
    (js : list ident) (Δ : envs PROP) : option (envs PROP * envs PROP) :=
Robbert Krebbers's avatar
Robbert Krebbers committed
126
  ''(Δ1,Δ2)  envs_split_go js Δ (envs_clear_spatial Δ);
127
  if d is Right then Some (Δ1,Δ2) else Some (Δ2,Δ1).
128

Robbert Krebbers's avatar
Robbert Krebbers committed
129
(* Coq versions of the tactics *)
Robbert Krebbers's avatar
Robbert Krebbers committed
130 131 132 133 134 135 136
Section bi_tactics.
Context {PROP : bi}.
Implicit Types Γ : env PROP.
Implicit Types Δ : envs PROP.
Implicit Types P Q : PROP.

Lemma of_envs_eq Δ :
137
  of_envs Δ = (envs_wf Δ⌝   [] env_persistent Δ  [] env_spatial Δ)%I.
138 139
Proof. done. Qed.

140 141 142 143 144 145 146 147 148
Lemma envs_delete_persistent Δ i : envs_delete false i true Δ = Δ. 
Proof. by destruct Δ. Qed.
Lemma envs_delete_spatial Δ i :
  envs_delete false i false Δ = envs_delete true i false Δ.
Proof. by destruct Δ. Qed.

Lemma envs_lookup_delete_Some Δ Δ' rp i p P :
  envs_lookup_delete rp i Δ = Some (p,P,Δ')
   envs_lookup i Δ = Some (p,P)  Δ' = envs_delete rp i p Δ.
Robbert Krebbers's avatar
Robbert Krebbers committed
149 150 151 152 153 154
Proof.
  rewrite /envs_lookup /envs_delete /envs_lookup_delete.
  destruct Δ as [Γp Γs]; rewrite /= !env_lookup_delete_correct.
  destruct (Γp !! i), (Γs !! i); naive_solver.
Qed.

155
Lemma envs_lookup_sound' Δ rp i p P :
156
  envs_lookup i Δ = Some (p,P) 
157
  of_envs Δ  ?p P  of_envs (envs_delete rp i p Δ).
Robbert Krebbers's avatar
Robbert Krebbers committed
158
Proof.
Robbert Krebbers's avatar
Robbert Krebbers committed
159
  rewrite /envs_lookup /envs_delete /of_envs=>?. apply pure_elim_l=> Hwf.
Robbert Krebbers's avatar
Robbert Krebbers committed
160
  destruct Δ as [Γp Γs], (Γp !! i) eqn:?; simplify_eq/=.
161
  - rewrite pure_True ?left_id; last (destruct Hwf, rp; constructor;
Robbert Krebbers's avatar
Robbert Krebbers committed
162
      naive_solver eauto using env_delete_wf, env_delete_fresh).
163 164 165 166 167
    destruct rp.
    + rewrite (env_lookup_perm Γp) //= affinely_persistently_and.
      by rewrite and_sep_affinely_persistently -assoc.
    + rewrite {1}affinely_persistently_sep_dup {1}(env_lookup_perm Γp) //=.
      by rewrite affinely_persistently_and and_elim_l -assoc.
Robbert Krebbers's avatar
Robbert Krebbers committed
168
  - destruct (Γs !! i) eqn:?; simplify_eq/=.
Robbert Krebbers's avatar
Robbert Krebbers committed
169 170 171 172
    rewrite pure_True ?left_id; last (destruct Hwf; constructor;
      naive_solver eauto using env_delete_wf, env_delete_fresh).
    rewrite (env_lookup_perm Γs) //=. by rewrite !assoc -(comm _ P).
Qed.
173 174 175 176
Lemma envs_lookup_sound Δ i p P :
  envs_lookup i Δ = Some (p,P) 
  of_envs Δ  ?p P  of_envs (envs_delete true i p Δ).
Proof. apply envs_lookup_sound'. Qed.
Robbert Krebbers's avatar
Robbert Krebbers committed
177
Lemma envs_lookup_persistent_sound Δ i P :
178
  envs_lookup i Δ = Some (true,P)  of_envs Δ   P  of_envs Δ.
179
Proof. intros ?%(envs_lookup_sound' _ false). by destruct Δ. Qed.
Robbert Krebbers's avatar
Robbert Krebbers committed
180 181

Lemma envs_lookup_split Δ i p P :
182
  envs_lookup i Δ = Some (p,P)  of_envs Δ  ?p P  (?p P - of_envs Δ).
Robbert Krebbers's avatar
Robbert Krebbers committed
183
Proof.
Robbert Krebbers's avatar
Robbert Krebbers committed
184
  rewrite /envs_lookup /of_envs=>?. apply pure_elim_l=> Hwf.
Robbert Krebbers's avatar
Robbert Krebbers committed
185
  destruct Δ as [Γp Γs], (Γp !! i) eqn:?; simplify_eq/=.
186
  - rewrite pure_True // left_id (env_lookup_perm Γp) //=
187
      affinely_persistently_and and_sep_affinely_persistently.
188
    cancel [ P]%I. apply wand_intro_l. solve_sep_entails.
Robbert Krebbers's avatar
Robbert Krebbers committed
189
  - destruct (Γs !! i) eqn:?; simplify_eq/=.
190
    rewrite (env_lookup_perm Γs) //=. rewrite pure_True // left_id.
Robbert Krebbers's avatar
Robbert Krebbers committed
191 192 193
    cancel [P]. apply wand_intro_l. solve_sep_entails.
Qed.

194 195 196
Lemma envs_lookup_delete_sound Δ Δ' rp i p P :
  envs_lookup_delete rp i Δ = Some (p,P,Δ')  of_envs Δ  ?p P  of_envs Δ'.
Proof. intros [? ->]%envs_lookup_delete_Some. by apply envs_lookup_sound'. Qed.
Robbert Krebbers's avatar
Robbert Krebbers committed
197

198 199
Lemma envs_lookup_delete_list_sound Δ Δ' rp js p Ps :
  envs_lookup_delete_list rp js Δ = Some (p,Ps,Δ') 
200
  of_envs Δ  ?p [] Ps  of_envs Δ'.
201 202
Proof.
  revert Δ Δ' p Ps. induction js as [|j js IH]=> Δ Δ'' p Ps ?; simplify_eq/=.
203
  { by rewrite affinely_persistently_emp left_id. }
204
  destruct (envs_lookup_delete rp j Δ) as [[[q1 P] Δ']|] eqn:Hj; simplify_eq/=.
205
  apply envs_lookup_delete_Some in Hj as [Hj ->].
206 207 208 209
  destruct (envs_lookup_delete_list _ js _) as [[[q2 Ps'] ?]|] eqn:?; simplify_eq/=.
  rewrite -affinely_persistently_if_sep_2 -assoc.
  rewrite envs_lookup_sound' //; rewrite IH //.
  repeat apply sep_mono=>//; apply affinely_persistently_if_flag_mono; by destruct q1.
210 211
Qed.

Joseph Tassarotti's avatar
Joseph Tassarotti committed
212 213 214 215 216 217 218 219 220 221
Lemma envs_lookup_delete_list_cons Δ Δ' Δ'' rp j js p1 p2 P Ps :
  envs_lookup_delete rp j Δ = Some (p1, P, Δ') 
  envs_lookup_delete_list rp js Δ' = Some (p2, Ps, Δ'') 
  envs_lookup_delete_list rp (j :: js) Δ = Some (p1 && p2, (P :: Ps), Δ'').
Proof. rewrite //= => -> //= -> //=. Qed.

Lemma envs_lookup_delete_list_nil Δ rp :
  envs_lookup_delete_list rp [] Δ = Some (true, [], Δ).
Proof. done. Qed.

222 223 224 225 226 227 228 229 230 231 232 233 234 235
Lemma envs_lookup_snoc Δ i p P :
  envs_lookup i Δ = None  envs_lookup i (envs_snoc Δ p i P) = Some (p, P).
Proof.
  rewrite /envs_lookup /envs_snoc=> ?.
  destruct Δ as [Γp Γs], p, (Γp !! i); simplify_eq; by rewrite env_lookup_snoc.
Qed.
Lemma envs_lookup_snoc_ne Δ i j p P :
  i  j  envs_lookup i (envs_snoc Δ p j P) = envs_lookup i Δ.
Proof.
  rewrite /envs_lookup /envs_snoc=> ?.
  destruct Δ as [Γp Γs], p; simplify_eq; by rewrite env_lookup_snoc_ne.
Qed.

Lemma envs_snoc_sound Δ p i P :
236
  envs_lookup i Δ = None  of_envs Δ  ?p P - of_envs (envs_snoc Δ p i P).
237
Proof.
Robbert Krebbers's avatar
Robbert Krebbers committed
238
  rewrite /envs_lookup /envs_snoc /of_envs=> ?; apply pure_elim_l=> Hwf.
239 240
  destruct Δ as [Γp Γs], (Γp !! i) eqn:?, (Γs !! i) eqn:?; simplify_eq/=.
  apply wand_intro_l; destruct p; simpl.
Robbert Krebbers's avatar
Robbert Krebbers committed
241
  - apply and_intro; [apply pure_intro|].
242
    + destruct Hwf; constructor; simpl; eauto using Esnoc_wf.
243
      intros j; destruct (ident_beq_reflect j i); naive_solver.
244
    + by rewrite affinely_persistently_and and_sep_affinely_persistently assoc.
Robbert Krebbers's avatar
Robbert Krebbers committed
245
  - apply and_intro; [apply pure_intro|].
246
    + destruct Hwf; constructor; simpl; eauto using Esnoc_wf.
247
      intros j; destruct (ident_beq_reflect j i); naive_solver.
248 249 250
    + solve_sep_entails.
Qed.

251
Lemma envs_app_sound Δ Δ' p Γ :
252 253
  envs_app p Γ Δ = Some Δ' 
  of_envs Δ  (if p then  [] Γ else [] Γ) - of_envs Δ'.
Robbert Krebbers's avatar
Robbert Krebbers committed
254
Proof.
Robbert Krebbers's avatar
Robbert Krebbers committed
255
  rewrite /of_envs /envs_app=> ?; apply pure_elim_l=> Hwf.
Robbert Krebbers's avatar
Robbert Krebbers committed
256 257 258
  destruct Δ as [Γp Γs], p; simplify_eq/=.
  - destruct (env_app Γ Γs) eqn:Happ,
      (env_app Γ Γp) as [Γp'|] eqn:?; simplify_eq/=.
Robbert Krebbers's avatar
Robbert Krebbers committed
259
    apply wand_intro_l, and_intro; [apply pure_intro|].
Robbert Krebbers's avatar
Robbert Krebbers committed
260 261 262 263
    + destruct Hwf; constructor; simpl; eauto using env_app_wf.
      intros j. apply (env_app_disjoint _ _ _ j) in Happ.
      naive_solver eauto using env_app_fresh.
    + rewrite (env_app_perm _ _ Γp') //.
264
      rewrite big_opL_app affinely_persistently_and and_sep_affinely_persistently.
265
      solve_sep_entails.
Robbert Krebbers's avatar
Robbert Krebbers committed
266 267
  - destruct (env_app Γ Γp) eqn:Happ,
      (env_app Γ Γs) as [Γs'|] eqn:?; simplify_eq/=.
Robbert Krebbers's avatar
Robbert Krebbers committed
268
    apply wand_intro_l, and_intro; [apply pure_intro|].
Robbert Krebbers's avatar
Robbert Krebbers committed
269 270 271
    + destruct Hwf; constructor; simpl; eauto using env_app_wf.
      intros j. apply (env_app_disjoint _ _ _ j) in Happ.
      naive_solver eauto using env_app_fresh.
272
    + rewrite (env_app_perm _ _ Γs') // big_opL_app. solve_sep_entails.
Robbert Krebbers's avatar
Robbert Krebbers committed
273 274
Qed.

275
Lemma envs_app_singleton_sound Δ Δ' p j Q :
276
  envs_app p (Esnoc Enil j Q) Δ = Some Δ'  of_envs Δ  ?p Q - of_envs Δ'.
277 278
Proof. move=> /envs_app_sound. destruct p; by rewrite /= right_id. Qed.

Robbert Krebbers's avatar
Robbert Krebbers committed
279 280
Lemma envs_simple_replace_sound' Δ Δ' i p Γ :
  envs_simple_replace i p Γ Δ = Some Δ' 
281
  of_envs (envs_delete true i p Δ)  (if p then  [] Γ else [] Γ) - of_envs Δ'.
Robbert Krebbers's avatar
Robbert Krebbers committed
282 283
Proof.
  rewrite /envs_simple_replace /envs_delete /of_envs=> ?.
Robbert Krebbers's avatar
Robbert Krebbers committed
284
  apply pure_elim_l=> Hwf. destruct Δ as [Γp Γs], p; simplify_eq/=.
Robbert Krebbers's avatar
Robbert Krebbers committed
285 286
  - destruct (env_app Γ Γs) eqn:Happ,
      (env_replace i Γ Γp) as [Γp'|] eqn:?; simplify_eq/=.
Robbert Krebbers's avatar
Robbert Krebbers committed
287
    apply wand_intro_l, and_intro; [apply pure_intro|].
Robbert Krebbers's avatar
Robbert Krebbers committed
288 289 290 291
    + destruct Hwf; constructor; simpl; eauto using env_replace_wf.
      intros j. apply (env_app_disjoint _ _ _ j) in Happ.
      destruct (decide (i = j)); try naive_solver eauto using env_replace_fresh.
    + rewrite (env_replace_perm _ _ Γp') //.
292
      rewrite big_opL_app affinely_persistently_and and_sep_affinely_persistently.
293
      solve_sep_entails.
Robbert Krebbers's avatar
Robbert Krebbers committed
294 295
  - destruct (env_app Γ Γp) eqn:Happ,
      (env_replace i Γ Γs) as [Γs'|] eqn:?; simplify_eq/=.
Robbert Krebbers's avatar
Robbert Krebbers committed
296
    apply wand_intro_l, and_intro; [apply pure_intro|].
Robbert Krebbers's avatar
Robbert Krebbers committed
297 298 299
    + destruct Hwf; constructor; simpl; eauto using env_replace_wf.
      intros j. apply (env_app_disjoint _ _ _ j) in Happ.
      destruct (decide (i = j)); try naive_solver eauto using env_replace_fresh.
300
    + rewrite (env_replace_perm _ _ Γs') // big_opL_app. solve_sep_entails.
Robbert Krebbers's avatar
Robbert Krebbers committed
301 302
Qed.

303 304
Lemma envs_simple_replace_singleton_sound' Δ Δ' i p j Q :
  envs_simple_replace i p (Esnoc Enil j Q) Δ = Some Δ' 
305
  of_envs (envs_delete true i p Δ)  ?p Q - of_envs Δ'.
306 307
Proof. move=> /envs_simple_replace_sound'. destruct p; by rewrite /= right_id. Qed.

Robbert Krebbers's avatar
Robbert Krebbers committed
308 309
Lemma envs_simple_replace_sound Δ Δ' i p P Γ :
  envs_lookup i Δ = Some (p,P)  envs_simple_replace i p Γ Δ = Some Δ' 
310
  of_envs Δ  ?p P  ((if p then  [] Γ else [] Γ) - of_envs Δ').
Robbert Krebbers's avatar
Robbert Krebbers committed
311 312
Proof. intros. by rewrite envs_lookup_sound// envs_simple_replace_sound'//. Qed.

313 314 315
Lemma envs_simple_replace_singleton_sound Δ Δ' i p P j Q :
  envs_lookup i Δ = Some (p,P) 
  envs_simple_replace i p (Esnoc Enil j Q) Δ = Some Δ' 
316
  of_envs Δ  ?p P  (?p Q - of_envs Δ').
317 318 319 320
Proof.
  intros. by rewrite envs_lookup_sound// envs_simple_replace_singleton_sound'//.
Qed.

Robbert Krebbers's avatar
Robbert Krebbers committed
321
Lemma envs_replace_sound' Δ Δ' i p q Γ :
322
  envs_replace i p q Γ Δ = Some Δ' 
323
  of_envs (envs_delete true i p Δ)  (if q then  [] Γ else [] Γ) - of_envs Δ'.
Robbert Krebbers's avatar
Robbert Krebbers committed
324 325 326 327 328 329
Proof.
  rewrite /envs_replace; destruct (eqb _ _) eqn:Hpq.
  - apply eqb_prop in Hpq as ->. apply envs_simple_replace_sound'.
  - apply envs_app_sound.
Qed.

330 331
Lemma envs_replace_singleton_sound' Δ Δ' i p q j Q :
  envs_replace i p q (Esnoc Enil j Q) Δ = Some Δ' 
332
  of_envs (envs_delete true i p Δ)  ?q Q - of_envs Δ'.
333 334
Proof. move=> /envs_replace_sound'. destruct q; by rewrite /= ?right_id. Qed.

Robbert Krebbers's avatar
Robbert Krebbers committed
335 336
Lemma envs_replace_sound Δ Δ' i p q P Γ :
  envs_lookup i Δ = Some (p,P)  envs_replace i p q Γ Δ = Some Δ' 
337
  of_envs Δ  ?p P  ((if q then  [] Γ else [] Γ) - of_envs Δ').
Robbert Krebbers's avatar
Robbert Krebbers committed
338 339
Proof. intros. by rewrite envs_lookup_sound// envs_replace_sound'//. Qed.

340 341 342
Lemma envs_replace_singleton_sound Δ Δ' i p q P j Q :
  envs_lookup i Δ = Some (p,P) 
  envs_replace i p q (Esnoc Enil j Q) Δ = Some Δ' 
343
  of_envs Δ  ?p P  (?q Q - of_envs Δ').
344 345
Proof. intros. by rewrite envs_lookup_sound// envs_replace_singleton_sound'//. Qed.

346 347
Lemma envs_lookup_envs_clear_spatial Δ j :
  envs_lookup j (envs_clear_spatial Δ)
Robbert Krebbers's avatar
Robbert Krebbers committed
348
  = ''(p,P)  envs_lookup j Δ; if p : bool then Some (p,P) else None.
Robbert Krebbers's avatar
Robbert Krebbers committed
349
Proof.
350 351 352
  rewrite /envs_lookup /envs_clear_spatial.
  destruct Δ as [Γp Γs]; simpl; destruct (Γp !! j) eqn:?; simplify_eq/=; auto.
  by destruct (Γs !! j).
Robbert Krebbers's avatar
Robbert Krebbers committed
353 354
Qed.

Robbert Krebbers's avatar
Robbert Krebbers committed
355
Lemma envs_clear_spatial_sound Δ :
356
  of_envs Δ  of_envs (envs_clear_spatial Δ)  [] env_spatial Δ.
357
Proof.
Robbert Krebbers's avatar
Robbert Krebbers committed
358 359 360
  rewrite /of_envs /envs_clear_spatial /=. apply pure_elim_l=> Hwf.
  rewrite right_id -persistent_and_sep_assoc. apply and_intro; [|done].
  apply pure_intro. destruct Hwf; constructor; simpl; auto using Enil_wf.
361 362
Qed.

363
Lemma env_spatial_is_nil_affinely_persistently Δ :
364
  env_spatial_is_nil Δ = true  of_envs Δ   of_envs Δ.
Robbert Krebbers's avatar
Robbert Krebbers committed
365 366
Proof.
  intros. unfold of_envs; destruct Δ as [? []]; simplify_eq/=.
367 368
  rewrite !right_id {1}affinely_and_r persistently_and.
  by rewrite persistently_affinely persistently_idemp persistently_pure.
Robbert Krebbers's avatar
Robbert Krebbers committed
369
Qed.
Robbert Krebbers's avatar
Robbert Krebbers committed
370

371 372
Lemma envs_lookup_envs_delete Δ i p P :
  envs_wf Δ 
373
  envs_lookup i Δ = Some (p,P)  envs_lookup i (envs_delete true i p Δ) = None.
374 375 376 377 378 379 380
Proof.
  rewrite /envs_lookup /envs_delete=> -[?? Hdisj] Hlookup.
  destruct Δ as [Γp Γs], p; simplify_eq/=.
  - rewrite env_lookup_env_delete //. revert Hlookup.
    destruct (Hdisj i) as [->| ->]; [|done]. by destruct (Γs !! _).
  - rewrite env_lookup_env_delete //. by destruct (Γp !! _).
Qed.
381 382
Lemma envs_lookup_envs_delete_ne Δ rp i j p :
  i  j  envs_lookup i (envs_delete rp j p Δ) = envs_lookup i Δ.
383 384
Proof.
  rewrite /envs_lookup /envs_delete=> ?. destruct Δ as [Γp Γs],p; simplify_eq/=.
385
  - destruct rp=> //. by rewrite env_lookup_env_delete_ne.
386 387 388 389 390
  - destruct (Γp !! i); simplify_eq/=; by rewrite ?env_lookup_env_delete_ne.
Qed.

Lemma envs_split_go_sound js Δ1 Δ2 Δ1' Δ2' :
  ( j P, envs_lookup j Δ1 = Some (false, P)  envs_lookup j Δ2 = None) 
Robbert Krebbers's avatar
Robbert Krebbers committed
391
  envs_split_go js Δ1 Δ2 = Some (Δ1',Δ2') 
392
  of_envs Δ1  of_envs Δ2  of_envs Δ1'  of_envs Δ2'.
393
Proof.
394 395
  revert Δ1 Δ2.
  induction js as [|j js IH]=> Δ1 Δ2 Hlookup HΔ; simplify_eq/=; [done|].
Robbert Krebbers's avatar
Robbert Krebbers committed
396 397
  apply pure_elim with (envs_wf Δ1)=> [|Hwf].
  { by rewrite /of_envs !and_elim_l sep_elim_l. }
398 399
  destruct (envs_lookup_delete _ j Δ1)
    as [[[[] P] Δ1'']|] eqn:Hdel; simplify_eq/=; auto.
400 401
  apply envs_lookup_delete_Some in Hdel as [??]; subst.
  rewrite envs_lookup_sound //; rewrite /= (comm _ P) -assoc.
402 403 404 405
  rewrite -(IH _ _ _ HΔ); last first.
   { intros j' P'; destruct (decide (j = j')) as [->|].
     - by rewrite (envs_lookup_envs_delete _ _ _ P).
     - rewrite envs_lookup_envs_delete_ne // envs_lookup_snoc_ne //. eauto. }
406 407
  rewrite (envs_snoc_sound Δ2 false j P) /= ?wand_elim_r; eauto.
Qed.
408
Lemma envs_split_sound Δ d js Δ1 Δ2 :
409
  envs_split d js Δ = Some (Δ1,Δ2)  of_envs Δ  of_envs Δ1  of_envs Δ2.
410
Proof.
411
  rewrite /envs_split=> ?. rewrite -(idemp bi_and (of_envs Δ)).
Robbert Krebbers's avatar
Robbert Krebbers committed
412
  rewrite {2}envs_clear_spatial_sound.
413 414
  rewrite (env_spatial_is_nil_affinely_persistently (envs_clear_spatial _)) //.
  rewrite -persistently_and_affinely_sep_l.
415
  rewrite (and_elim_l (bi_persistently _)%I)
416
          persistently_and_affinely_sep_r affinely_persistently_elim.
417 418 419
  destruct (envs_split_go _ _) as [[Δ1' Δ2']|] eqn:HΔ; [|done].
  apply envs_split_go_sound in HΔ as ->; last first.
  { intros j P. by rewrite envs_lookup_envs_clear_spatial=> ->. }
420
  destruct d; simplify_eq/=; solve_sep_entails.
421 422
Qed.

Robbert Krebbers's avatar
Robbert Krebbers committed
423
Global Instance envs_Forall2_refl (R : relation PROP) :
424 425
  Reflexive R  Reflexive (envs_Forall2 R).
Proof. by constructor. Qed.
Robbert Krebbers's avatar
Robbert Krebbers committed
426
Global Instance envs_Forall2_sym (R : relation PROP) :
427 428
  Symmetric R  Symmetric (envs_Forall2 R).
Proof. intros ??? [??]; by constructor. Qed.
Robbert Krebbers's avatar
Robbert Krebbers committed
429
Global Instance envs_Forall2_trans (R : relation PROP) :
430 431
  Transitive R  Transitive (envs_Forall2 R).
Proof. intros ??? [??] [??] [??]; constructor; etrans; eauto. Qed.
Robbert Krebbers's avatar
Robbert Krebbers committed
432
Global Instance envs_Forall2_antisymm (R R' : relation PROP) :
433 434
  AntiSymm R R'  AntiSymm (envs_Forall2 R) (envs_Forall2 R').
Proof. intros ??? [??] [??]; constructor; by eapply (anti_symm _). Qed.
Robbert Krebbers's avatar
Robbert Krebbers committed
435
Lemma envs_Forall2_impl (R R' : relation PROP) Δ1 Δ2 :
436 437 438
  envs_Forall2 R Δ1 Δ2  ( P Q, R P Q  R' P Q)  envs_Forall2 R' Δ1 Δ2.
Proof. intros [??] ?; constructor; eauto using env_Forall2_impl. Qed.

Robbert Krebbers's avatar
Robbert Krebbers committed
439
Global Instance of_envs_mono : Proper (envs_Forall2 () ==> ()) (@of_envs PROP).
440
Proof.
Robbert Krebbers's avatar
Robbert Krebbers committed
441 442
  intros [Γp1 Γs1] [Γp2 Γs2] [Hp Hs]; apply and_mono; simpl in *.
  - apply pure_mono=> -[???]. constructor;
443 444 445
      naive_solver eauto using env_Forall2_wf, env_Forall2_fresh.
  - by repeat f_equiv.
Qed.
Robbert Krebbers's avatar
Robbert Krebbers committed
446 447
Global Instance of_envs_proper :
  Proper (envs_Forall2 () ==> ()) (@of_envs PROP).
448
Proof.
449 450
  intros Δ1 Δ2 HΔ; apply (anti_symm ()); apply of_envs_mono;
    eapply (envs_Forall2_impl ()); [| |symmetry|]; eauto using equiv_entails.
451
Qed.
452
Global Instance Envs_proper (R : relation PROP) :
Robbert Krebbers's avatar
Robbert Krebbers committed
453
  Proper (env_Forall2 R ==> env_Forall2 R ==> envs_Forall2 R) (@Envs PROP).
454 455
Proof. by constructor. Qed.

456
Global Instance envs_entails_proper :
457
  Proper (envs_Forall2 () ==> () ==> iff) (@envs_entails PROP).
458
Proof. rewrite envs_entails_eq. solve_proper. Qed.
459
Global Instance envs_entails_flip_mono :
460
  Proper (envs_Forall2 () ==> flip () ==> flip impl) (@envs_entails PROP).
461
Proof. rewrite envs_entails_eq=> Δ1 Δ2 ? P1 P2 <- <-. by f_equiv. Qed.
462

Robbert Krebbers's avatar
Robbert Krebbers committed
463
(** * Adequacy *)
464
Lemma tac_adequate P : envs_entails (Envs Enil Enil) P  P.
Robbert Krebbers's avatar
Robbert Krebbers committed
465
Proof.
466 467
  rewrite envs_entails_eq /of_envs /= persistently_True_emp
          affinely_persistently_emp left_id=><-.
Robbert Krebbers's avatar
Robbert Krebbers committed
468
  apply and_intro=> //. apply pure_intro; repeat constructor.
Robbert Krebbers's avatar
Robbert Krebbers committed
469 470 471
Qed.

(** * Basic rules *)
472
Lemma tac_eval Δ Q Q' :
473 474 475
  ( (Q'':=Q'), Q''  Q)  (* We introduce [Q''] as a let binding so that
    tactics like `reflexivity` as called by [rewrite //] do not eagerly unify
    it with [Q]. See [test_iEval] in [tests/proofmode]. *)
476
  envs_entails Δ Q'  envs_entails Δ Q.
477
Proof. by intros <-. Qed.
478

479 480
Lemma tac_eval_in Δ Δ' i p P P' Q :
  envs_lookup i Δ = Some (p, P) 
481
  ( (P'':=P'), P  P') 
482 483 484
  envs_simple_replace i p (Esnoc Enil i P') Δ  = Some Δ' 
  envs_entails Δ' Q  envs_entails Δ Q.
Proof.
485 486 487
  rewrite envs_entails_eq /=. intros ? HP ? <-.
  rewrite envs_simple_replace_singleton_sound //; simpl.
  by rewrite HP wand_elim_r.
488
Qed.
489

490 491 492 493 494 495 496
Class AffineEnv (Γ : env PROP) := affine_env : Forall Affine Γ.
Global Instance affine_env_nil : AffineEnv Enil.
Proof. constructor. Qed.
Global Instance affine_env_snoc Γ i P :
  Affine P  AffineEnv Γ  AffineEnv (Esnoc Γ i P).
Proof. by constructor. Qed.

497
(* If the BI is affine, no need to walk on the whole environment. *)
498
Global Instance affine_env_bi `(BiAffine PROP) Γ : AffineEnv Γ | 0.
499 500
Proof. induction Γ; apply _. Qed.

501
Instance affine_env_spatial Δ :
502 503 504
  AffineEnv (env_spatial Δ)  Affine ([] env_spatial Δ).
Proof. intros H. induction H; simpl; apply _. Qed.

505 506
Lemma tac_emp_intro Δ : AffineEnv (env_spatial Δ)  envs_entails Δ emp.
Proof. intros. by rewrite envs_entails_eq (affine (of_envs Δ)). Qed.
507

Robbert Krebbers's avatar
Robbert Krebbers committed
508
Lemma tac_assumption Δ Δ' i p P Q :
509
  envs_lookup_delete true i Δ = Some (p,P,Δ') 
Robbert Krebbers's avatar
Robbert Krebbers committed
510
  FromAssumption p P Q 
511 512
  (if env_spatial_is_nil Δ' then TCTrue
   else TCOr (Absorbing Q) (AffineEnv (env_spatial Δ'))) 
513
  envs_entails Δ Q.
Robbert Krebbers's avatar
Robbert Krebbers committed
514
Proof.
515
  intros ?? H. rewrite envs_entails_eq envs_lookup_delete_sound //.
Robbert Krebbers's avatar
Robbert Krebbers committed
516
  destruct (env_spatial_is_nil Δ') eqn:?.
517
  - by rewrite (env_spatial_is_nil_affinely_persistently Δ') // sep_elim_l.
Robbert Krebbers's avatar
Robbert Krebbers committed
518
  - rewrite from_assumption. destruct H; by rewrite sep_elim_l.
Robbert Krebbers's avatar
Robbert Krebbers committed
519
Qed.
Robbert Krebbers's avatar
Robbert Krebbers committed
520 521 522 523

Lemma tac_rename Δ Δ' i j p P Q :
  envs_lookup i Δ = Some (p,P) 
  envs_simple_replace i p (Esnoc Enil j P) Δ = Some Δ' 
524 525
  envs_entails Δ' Q 
  envs_entails Δ Q.
Robbert Krebbers's avatar
Robbert Krebbers committed
526
Proof.
527
  rewrite envs_entails_eq=> ?? <-. rewrite envs_simple_replace_singleton_sound //.
528
  by rewrite wand_elim_r.
Robbert Krebbers's avatar
Robbert Krebbers committed
529
Qed.
Robbert Krebbers's avatar
Robbert Krebbers committed
530

Robbert Krebbers's avatar
Robbert Krebbers committed
531
Lemma tac_clear Δ Δ' i p P Q :
532
  envs_lookup_delete true i Δ = Some (p,P,Δ') 
Robbert Krebbers's avatar
Robbert Krebbers committed
533
  (if p then TCTrue else TCOr (Affine P) (Absorbing Q)) 
534
  envs_entails Δ' Q 
535
  envs_entails Δ Q.
Robbert Krebbers's avatar
Robbert Krebbers committed
536
Proof.
537
  rewrite envs_entails_eq=> ?? HQ. rewrite envs_lookup_delete_sound //.
538
  by destruct p; rewrite /= HQ sep_elim_r.
Robbert Krebbers's avatar
Robbert Krebbers committed
539
Qed.
Robbert Krebbers's avatar
Robbert Krebbers committed
540 541

(** * False *)
542
Lemma tac_ex_falso Δ Q : envs_entails Δ False  envs_entails Δ Q.
543
Proof. by rewrite envs_entails_eq -(False_elim Q). Qed.
Robbert Krebbers's avatar
Robbert Krebbers committed
544

Robbert Krebbers's avatar
Robbert Krebbers committed
545 546 547
Lemma tac_false_destruct Δ i p P Q :
  envs_lookup i Δ = Some (p,P) 
  P = False%I 
548
  envs_entails Δ Q.
Robbert Krebbers's avatar
Robbert Krebbers committed
549
Proof.
550
  rewrite envs_entails_eq => ??. subst. rewrite envs_lookup_sound //; simpl.
551
  by rewrite affinely_persistently_if_elim sep_elim_l False_elim.
Robbert Krebbers's avatar
Robbert Krebbers committed
552 553
Qed.

Robbert Krebbers's avatar
Robbert Krebbers committed
554
(** * Pure *)
Jacques-Henri Jourdan's avatar
Jacques-Henri Jourdan committed
555 556
(* This relies on the invariant that [FromPure false] implies
   [FromPure true] *)
557 558 559 560 561 562 563 564
Lemma tac_pure_intro Δ Q φ af :
  env_spatial_is_nil Δ = af  FromPure af Q φ  φ  envs_entails Δ Q.
Proof.
  intros ???. rewrite envs_entails_eq -(from_pure _ Q). destruct af.
  - rewrite env_spatial_is_nil_affinely_persistently //=. f_equiv.
    by apply pure_intro.
  - by apply pure_intro.
Qed.
565

Robbert Krebbers's avatar
Robbert Krebbers committed
566
Lemma tac_pure Δ Δ' i p P φ Q :
567
  envs_lookup_delete true i Δ = Some (p, P, Δ') 
Robbert Krebbers's avatar
Robbert Krebbers committed
568
  IntoPure P φ 
569
  (if p then TCTrue else TCOr (Affine P) (Absorbing Q)) 
570
  (φ  envs_entails Δ' Q)  envs_entails Δ Q.
Robbert Krebbers's avatar
Robbert Krebbers committed
571
Proof.
572
  rewrite envs_entails_eq=> ?? HPQ HQ.
573
  rewrite envs_lookup_delete_sound //; simpl. destruct p; simpl.
574
  - rewrite (into_pure P) -persistently_and_affinely_sep_l persistently_pure.
Robbert Krebbers's avatar
Robbert Krebbers committed
575
    by apply pure_elim_l.
576
  - destruct HPQ.
577
    + rewrite -(affine_affinely P) (into_pure P) -persistent_and_affinely_sep_l.
578
      by apply pure_elim_l.
579 580
    + rewrite (into_pure P) (persistent_absorbingly_affinely  _ %I) absorbingly_sep_lr.
      rewrite -persistent_and_affinely_sep_l. apply pure_elim_l=> ?. by rewrite HQ.
Robbert Krebbers's avatar
Robbert Krebbers committed
581 582
Qed.

583
Lemma tac_pure_revert Δ φ Q : envs_entails Δ (⌜φ⌝  Q)  (φ  envs_entails Δ Q).
584
Proof. rewrite envs_entails_eq. intros HΔ ?. by rewrite HΔ pure_True // left_id. Qed.
Robbert Krebbers's avatar
Robbert Krebbers committed
585

586
(** * Persistence *)
Robbert Krebbers's avatar
Robbert Krebbers committed
587
Lemma tac_persistent Δ Δ' i p P P' Q :
588
  envs_lookup i Δ = Some (p, P) 
589
  IntoPersistent p P P' 
590
  (if p then TCTrue else TCOr (Affine P) (Absorbing Q)) 
Robbert Krebbers's avatar
Robbert Krebbers committed
591
  envs_replace i p true (Esnoc Enil i P') Δ = Some Δ' 
592
  envs_entails Δ' Q  envs_entails Δ Q.
Robbert Krebbers's avatar
Robbert Krebbers committed
593
Proof.
594
  rewrite envs_entails_eq=>?? HPQ ? HQ. rewrite envs_replace_singleton_sound //=.
595 596 597
  destruct p; simpl.
  - by rewrite -(into_persistent _ P) /= wand_elim_r.
  - destruct HPQ.
598
    + rewrite -(affine_affinely P) (_ : P = bi_persistently_if false P)%I //
599 600
              (into_persistent _ P) wand_elim_r //.
    + rewrite (_ : P = bi_persistently_if false P)%I // (into_persistent _ P).
601 602
      by rewrite {1}(persistent_absorbingly_affinely (bi_persistently _)%I)
                 absorbingly_sep_l wand_elim_r HQ.
Robbert Krebbers's avatar
Robbert Krebbers committed
603 604 605
Qed.

(** * Implication and wand *)
606 607
Lemma tac_impl_intro Δ Δ' i P P' Q R :
  FromImpl R P Q 
608
  (if env_spatial_is_nil Δ then TCTrue else Persistent P) 
609
  envs_app false (Esnoc Enil i P') Δ = Some Δ' 
610
  FromAffinely P' P 
611
  envs_entails Δ' Q  envs_entails Δ R.
Robbert Krebbers's avatar
Robbert Krebbers committed
612
Proof.
613
  rewrite /FromImpl envs_entails_eq => <- ??? <-. destruct (env_spatial_is_nil Δ) eqn:?.
614
  - rewrite (env_spatial_is_nil_affinely_persistently Δ) //; simpl. apply impl_intro_l.
615
    rewrite envs_app_singleton_sound //; simpl.
616 617
    rewrite -(from_affinely P') -affinely_and_lr.
    by rewrite persistently_and_affinely_sep_r affinely_persistently_elim wand_elim_r.
618
  - apply impl_intro_l. rewrite envs_app_singleton_sound //; simpl.
619
    by rewrite -(from_affinely P') persistent_and_affinely_sep_l_1 wand_elim_r.
Robbert Krebbers's avatar
Robbert Krebbers committed
620
Qed.
621 622
Lemma tac_impl_intro_persistent Δ Δ' i P P' Q R :
  FromImpl R P Q 
623
  IntoPersistent false P P' 
Robbert Krebbers's avatar
Robbert Krebbers committed
624
  envs_app true (Esnoc Enil i P') Δ = Some Δ' 
625
  envs_entails Δ' Q  envs_entails Δ R.
Robbert Krebbers's avatar
Robbert Krebbers committed
626
Proof.
627
  rewrite /FromImpl envs_entails_eq => <- ?? <-.
628
  rewrite envs_app_singleton_sound //=. apply impl_intro_l.
629
  rewrite (_ : P = bi_persistently_if false P)%I // (into_persistent false P).
630
  by rewrite persistently_and_affinely_sep_l wand_elim_r.
631
Qed.
632 633
Lemma tac_impl_intro_drop Δ P Q R :
  FromImpl R P Q  envs_entails Δ Q  envs_entails Δ R.
634 635 636
Proof.
  rewrite /FromImpl envs_entails_eq => <- ?. apply impl_intro_l. by rewrite and_elim_r.
Qed.
Robbert Krebbers's avatar
Robbert Krebbers committed
637