coq_tactics.v 55.5 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 modality_instances.
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.

10
Notation pm_maybe_wand mP Q := (pm_from_option (λ P, P - Q)%I Q%I mP).
Ralf Jung's avatar
Ralf Jung committed
11

Robbert Krebbers's avatar
Robbert Krebbers committed
12
(* Coq versions of the tactics *)
Robbert Krebbers's avatar
Robbert Krebbers committed
13 14 15 16 17 18 19
Section bi_tactics.
Context {PROP : bi}.
Implicit Types Γ : env PROP.
Implicit Types Δ : envs PROP.
Implicit Types P Q : PROP.

Lemma of_envs_eq Δ :
20
  of_envs Δ = (envs_wf Δ⌝   [] env_intuitionistic Δ  [] env_spatial Δ)%I.
21
Proof. done. Qed.
22
(** An environment is a ∗ of something intuitionistic and the spatial environment.
23 24 25 26
TODO: Can we define it as such? *)
Lemma of_envs_eq' Δ :
  of_envs Δ  (envs_wf Δ⌝   [] env_intuitionistic Δ)  [] env_spatial Δ.
Proof. rewrite of_envs_eq persistent_and_sep_assoc //. Qed.
27

28 29 30 31 32 33 34 35 36
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
37 38 39 40 41 42
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.

43
Lemma envs_lookup_sound' Δ rp i p P :
44
  envs_lookup i Δ = Some (p,P) 
45
  of_envs Δ  ?p P  of_envs (envs_delete rp i p Δ).
Robbert Krebbers's avatar
Robbert Krebbers committed
46
Proof.
Robbert Krebbers's avatar
Robbert Krebbers committed
47
  rewrite /envs_lookup /envs_delete /of_envs=>?. apply pure_elim_l=> Hwf.
Robbert Krebbers's avatar
Robbert Krebbers committed
48
  destruct Δ as [Γp Γs], (Γp !! i) eqn:?; simplify_eq/=.
49
  - rewrite pure_True ?left_id; last (destruct Hwf, rp; constructor;
Robbert Krebbers's avatar
Robbert Krebbers committed
50
      naive_solver eauto using env_delete_wf, env_delete_fresh).
51
    destruct rp.
52 53 54 55
    + rewrite (env_lookup_perm Γp) //= intuitionistically_and.
      by rewrite and_sep_intuitionistically -assoc.
    + rewrite {1}intuitionistically_sep_dup {1}(env_lookup_perm Γp) //=.
      by rewrite intuitionistically_and and_elim_l -assoc.
Robbert Krebbers's avatar
Robbert Krebbers committed
56
  - destruct (Γs !! i) eqn:?; simplify_eq/=.
Robbert Krebbers's avatar
Robbert Krebbers committed
57 58 59 60
    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.
61 62 63 64
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
65
Lemma envs_lookup_persistent_sound Δ i P :
66
  envs_lookup i Δ = Some (true,P)  of_envs Δ   P  of_envs Δ.
67
Proof. intros ?%(envs_lookup_sound' _ false). by destruct Δ. Qed.
68 69 70 71 72 73 74 75 76 77 78 79 80
Lemma envs_lookup_sound_2 Δ i p P :
  envs_wf Δ  envs_lookup i Δ = Some (p,P) 
  ?p P  of_envs (envs_delete true i p Δ)  of_envs Δ.
Proof.
  rewrite /envs_lookup /of_envs=>Hwf ?. rewrite [envs_wf Δ⌝%I]pure_True // left_id.
  destruct Δ as [Γp Γs], (Γp !! i) eqn:?; simplify_eq/=.
  - rewrite (env_lookup_perm Γp) //= intuitionistically_and
      and_sep_intuitionistically and_elim_r.
    cancel [ P]%I. solve_sep_entails.
  - destruct (Γs !! i) eqn:?; simplify_eq/=.
    rewrite (env_lookup_perm Γs) //= and_elim_r.
    cancel [P]. solve_sep_entails.
Qed.
Robbert Krebbers's avatar
Robbert Krebbers committed
81 82

Lemma envs_lookup_split Δ i p P :
83
  envs_lookup i Δ = Some (p,P)  of_envs Δ  ?p P  (?p P - of_envs Δ).
Robbert Krebbers's avatar
Robbert Krebbers committed
84
Proof.
85 86 87 88
  intros. apply pure_elim with (envs_wf Δ).
  { rewrite of_envs_eq. apply and_elim_l. }
  intros. rewrite {1}envs_lookup_sound//.
  apply sep_mono_r. apply wand_intro_l, envs_lookup_sound_2; done.
Robbert Krebbers's avatar
Robbert Krebbers committed
89 90
Qed.

91 92 93
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
94

95 96
Lemma envs_lookup_delete_list_sound Δ Δ' rp js p Ps :
  envs_lookup_delete_list rp js Δ = Some (p,Ps,Δ') 
97
  of_envs Δ  ?p [] Ps  of_envs Δ'.
98 99
Proof.
  revert Δ Δ' p Ps. induction js as [|j js IH]=> Δ Δ'' p Ps ?; simplify_eq/=.
100
  { by rewrite intuitionistically_emp left_id. }
101
  destruct (envs_lookup_delete rp j Δ) as [[[q1 P] Δ']|] eqn:Hj; simplify_eq/=.
102
  apply envs_lookup_delete_Some in Hj as [Hj ->].
103
  destruct (envs_lookup_delete_list _ js _) as [[[q2 Ps'] ?]|] eqn:?; simplify_eq/=.
104
  rewrite -intuitionistically_if_sep_2 -assoc.
105
  rewrite envs_lookup_sound' //; rewrite IH //.
106
  repeat apply sep_mono=>//; apply intuitionistically_if_flag_mono; by destruct q1.
107 108
Qed.

Joseph Tassarotti's avatar
Joseph Tassarotti committed
109 110 111 112 113 114 115 116 117 118
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.

119 120 121 122 123 124 125 126 127 128 129 130 131 132
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 :
133
  envs_lookup i Δ = None  of_envs Δ  ?p P - of_envs (envs_snoc Δ p i P).
134
Proof.
Robbert Krebbers's avatar
Robbert Krebbers committed
135
  rewrite /envs_lookup /envs_snoc /of_envs=> ?; apply pure_elim_l=> Hwf.
136 137
  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
138
  - apply and_intro; [apply pure_intro|].
139
    + destruct Hwf; constructor; simpl; eauto using Esnoc_wf.
140
      intros j; destruct (ident_beq_reflect j i); naive_solver.
141
    + by rewrite intuitionistically_and and_sep_intuitionistically assoc.
Robbert Krebbers's avatar
Robbert Krebbers committed
142
  - apply and_intro; [apply pure_intro|].
143
    + destruct Hwf; constructor; simpl; eauto using Esnoc_wf.
144
      intros j; destruct (ident_beq_reflect j i); naive_solver.
145 146 147
    + solve_sep_entails.
Qed.

148
Lemma envs_app_sound Δ Δ' p Γ :
149 150
  envs_app p Γ Δ = Some Δ' 
  of_envs Δ  (if p then  [] Γ else [] Γ) - of_envs Δ'.
Robbert Krebbers's avatar
Robbert Krebbers committed
151
Proof.
Robbert Krebbers's avatar
Robbert Krebbers committed
152
  rewrite /of_envs /envs_app=> ?; apply pure_elim_l=> Hwf.
Robbert Krebbers's avatar
Robbert Krebbers committed
153 154 155
  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
156
    apply wand_intro_l, and_intro; [apply pure_intro|].
Robbert Krebbers's avatar
Robbert Krebbers committed
157 158 159 160
    + 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') //.
161
      rewrite big_opL_app intuitionistically_and and_sep_intuitionistically.
162
      solve_sep_entails.
Robbert Krebbers's avatar
Robbert Krebbers committed
163 164
  - destruct (env_app Γ Γp) eqn:Happ,
      (env_app Γ Γs) as [Γs'|] eqn:?; simplify_eq/=.
Robbert Krebbers's avatar
Robbert Krebbers committed
165
    apply wand_intro_l, and_intro; [apply pure_intro|].
Robbert Krebbers's avatar
Robbert Krebbers committed
166 167 168
    + 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.
169
    + rewrite (env_app_perm _ _ Γs') // big_opL_app. solve_sep_entails.
Robbert Krebbers's avatar
Robbert Krebbers committed
170 171
Qed.

172
Lemma envs_app_singleton_sound Δ Δ' p j Q :
173
  envs_app p (Esnoc Enil j Q) Δ = Some Δ'  of_envs Δ  ?p Q - of_envs Δ'.
174 175
Proof. move=> /envs_app_sound. destruct p; by rewrite /= right_id. Qed.

Robbert Krebbers's avatar
Robbert Krebbers committed
176 177
Lemma envs_simple_replace_sound' Δ Δ' i p Γ :
  envs_simple_replace i p Γ Δ = Some Δ' 
178
  of_envs (envs_delete true i p Δ)  (if p then  [] Γ else [] Γ) - of_envs Δ'.
Robbert Krebbers's avatar
Robbert Krebbers committed
179 180
Proof.
  rewrite /envs_simple_replace /envs_delete /of_envs=> ?.
Robbert Krebbers's avatar
Robbert Krebbers committed
181
  apply pure_elim_l=> Hwf. destruct Δ as [Γp Γs], p; simplify_eq/=.
Robbert Krebbers's avatar
Robbert Krebbers committed
182 183
  - destruct (env_app Γ Γs) eqn:Happ,
      (env_replace i Γ Γp) as [Γp'|] eqn:?; simplify_eq/=.
Robbert Krebbers's avatar
Robbert Krebbers committed
184
    apply wand_intro_l, and_intro; [apply pure_intro|].
Robbert Krebbers's avatar
Robbert Krebbers committed
185 186 187 188
    + 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') //.
189
      rewrite big_opL_app intuitionistically_and and_sep_intuitionistically.
190
      solve_sep_entails.
Robbert Krebbers's avatar
Robbert Krebbers committed
191 192
  - destruct (env_app Γ Γp) eqn:Happ,
      (env_replace i Γ Γs) as [Γs'|] eqn:?; simplify_eq/=.
Robbert Krebbers's avatar
Robbert Krebbers committed
193
    apply wand_intro_l, and_intro; [apply pure_intro|].
Robbert Krebbers's avatar
Robbert Krebbers committed
194 195 196
    + 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.
197
    + rewrite (env_replace_perm _ _ Γs') // big_opL_app. solve_sep_entails.
Robbert Krebbers's avatar
Robbert Krebbers committed
198 199
Qed.

200 201
Lemma envs_simple_replace_singleton_sound' Δ Δ' i p j Q :
  envs_simple_replace i p (Esnoc Enil j Q) Δ = Some Δ' 
202
  of_envs (envs_delete true i p Δ)  ?p Q - of_envs Δ'.
203 204
Proof. move=> /envs_simple_replace_sound'. destruct p; by rewrite /= right_id. Qed.

Robbert Krebbers's avatar
Robbert Krebbers committed
205 206
Lemma envs_simple_replace_sound Δ Δ' i p P Γ :
  envs_lookup i Δ = Some (p,P)  envs_simple_replace i p Γ Δ = Some Δ' 
207
  of_envs Δ  ?p P  ((if p then  [] Γ else [] Γ) - of_envs Δ').
Robbert Krebbers's avatar
Robbert Krebbers committed
208 209
Proof. intros. by rewrite envs_lookup_sound// envs_simple_replace_sound'//. Qed.

210 211 212 213 214 215 216 217 218 219 220
Lemma envs_simple_replace_maybe_sound Δ Δ' i p P Γ :
  envs_lookup i Δ = Some (p,P)  envs_simple_replace i p Γ Δ = Some Δ' 
  of_envs Δ  ?p P  (((if p then  [] Γ else [] Γ) - of_envs Δ')  (?p P - of_envs Δ)).
Proof.
  intros. apply pure_elim with (envs_wf Δ).
  { rewrite of_envs_eq. apply and_elim_l. }
  intros. rewrite {1}envs_lookup_sound//. apply sep_mono_r, and_intro.
  - rewrite envs_simple_replace_sound'//.
  - apply wand_intro_l, envs_lookup_sound_2; done.
Qed.

221 222 223
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 Δ' 
224
  of_envs Δ  ?p P  (?p Q - of_envs Δ').
225 226 227 228
Proof.
  intros. by rewrite envs_lookup_sound// envs_simple_replace_singleton_sound'//.
Qed.

Robbert Krebbers's avatar
Robbert Krebbers committed
229
Lemma envs_replace_sound' Δ Δ' i p q Γ :
230
  envs_replace i p q Γ Δ = Some Δ' 
231
  of_envs (envs_delete true i p Δ)  (if q then  [] Γ else [] Γ) - of_envs Δ'.
Robbert Krebbers's avatar
Robbert Krebbers committed
232
Proof.
233
  rewrite /envs_replace; destruct (beq _ _) eqn:Hpq.
Robbert Krebbers's avatar
Robbert Krebbers committed
234 235 236 237
  - apply eqb_prop in Hpq as ->. apply envs_simple_replace_sound'.
  - apply envs_app_sound.
Qed.

238 239
Lemma envs_replace_singleton_sound' Δ Δ' i p q j Q :
  envs_replace i p q (Esnoc Enil j Q) Δ = Some Δ' 
240
  of_envs (envs_delete true i p Δ)  ?q Q - of_envs Δ'.
241 242
Proof. move=> /envs_replace_sound'. destruct q; by rewrite /= ?right_id. Qed.

Robbert Krebbers's avatar
Robbert Krebbers committed
243 244
Lemma envs_replace_sound Δ Δ' i p q P Γ :
  envs_lookup i Δ = Some (p,P)  envs_replace i p q Γ Δ = Some Δ' 
245
  of_envs Δ  ?p P  ((if q then  [] Γ else [] Γ) - of_envs Δ').
Robbert Krebbers's avatar
Robbert Krebbers committed
246 247
Proof. intros. by rewrite envs_lookup_sound// envs_replace_sound'//. Qed.

248 249 250
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 Δ' 
251
  of_envs Δ  ?p P  (?q Q - of_envs Δ').
252 253
Proof. intros. by rewrite envs_lookup_sound// envs_replace_singleton_sound'//. Qed.

254 255
Lemma envs_lookup_envs_clear_spatial Δ j :
  envs_lookup j (envs_clear_spatial Δ)
Robbert Krebbers's avatar
Robbert Krebbers committed
256
  = ''(p,P)  envs_lookup j Δ; if p : bool then Some (p,P) else None.
Robbert Krebbers's avatar
Robbert Krebbers committed
257
Proof.
258 259 260
  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
261 262
Qed.

Robbert Krebbers's avatar
Robbert Krebbers committed
263
Lemma envs_clear_spatial_sound Δ :
264
  of_envs Δ  of_envs (envs_clear_spatial Δ)  [] env_spatial Δ.
265
Proof.
Robbert Krebbers's avatar
Robbert Krebbers committed
266 267 268
  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.
269 270
Qed.

271
Lemma env_spatial_is_nil_intuitionistically Δ :
272
  env_spatial_is_nil Δ = true  of_envs Δ   of_envs Δ.
Robbert Krebbers's avatar
Robbert Krebbers committed
273 274
Proof.
  intros. unfold of_envs; destruct Δ as [? []]; simplify_eq/=.
275
  rewrite !right_id /bi_intuitionistically {1}affinely_and_r persistently_and.
Ralf Jung's avatar
Ralf Jung committed
276
  by rewrite persistently_affinely_elim persistently_idemp persistently_pure.
Robbert Krebbers's avatar
Robbert Krebbers committed
277
Qed.
Robbert Krebbers's avatar
Robbert Krebbers committed
278

279 280
Lemma envs_lookup_envs_delete Δ i p P :
  envs_wf Δ 
281
  envs_lookup i Δ = Some (p,P)  envs_lookup i (envs_delete true i p Δ) = None.
282 283 284 285 286 287 288
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.
289 290
Lemma envs_lookup_envs_delete_ne Δ rp i j p :
  i  j  envs_lookup i (envs_delete rp j p Δ) = envs_lookup i Δ.
291 292
Proof.
  rewrite /envs_lookup /envs_delete=> ?. destruct Δ as [Γp Γs],p; simplify_eq/=.
293
  - destruct rp=> //. by rewrite env_lookup_env_delete_ne.
294 295 296 297 298
  - 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
299
  envs_split_go js Δ1 Δ2 = Some (Δ1',Δ2') 
300
  of_envs Δ1  of_envs Δ2  of_envs Δ1'  of_envs Δ2'.
301
Proof.
302 303
  revert Δ1 Δ2.
  induction js as [|j js IH]=> Δ1 Δ2 Hlookup HΔ; simplify_eq/=; [done|].
Robbert Krebbers's avatar
Robbert Krebbers committed
304 305
  apply pure_elim with (envs_wf Δ1)=> [|Hwf].
  { by rewrite /of_envs !and_elim_l sep_elim_l. }
306 307
  destruct (envs_lookup_delete _ j Δ1)
    as [[[[] P] Δ1'']|] eqn:Hdel; simplify_eq/=; auto.
308 309
  apply envs_lookup_delete_Some in Hdel as [??]; subst.
  rewrite envs_lookup_sound //; rewrite /= (comm _ P) -assoc.
310 311 312 313
  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. }
314 315
  rewrite (envs_snoc_sound Δ2 false j P) /= ?wand_elim_r; eauto.
Qed.
316
Lemma envs_split_sound Δ d js Δ1 Δ2 :
317
  envs_split d js Δ = Some (Δ1,Δ2)  of_envs Δ  of_envs Δ1  of_envs Δ2.
318
Proof.
319
  rewrite /envs_split=> ?. rewrite -(idemp bi_and (of_envs Δ)).
Robbert Krebbers's avatar
Robbert Krebbers committed
320
  rewrite {2}envs_clear_spatial_sound.
321 322
  rewrite (env_spatial_is_nil_intuitionistically (envs_clear_spatial _)) //.
  rewrite -persistently_and_intuitionistically_sep_l.
323
  rewrite (and_elim_l (<pers> _)%I)
324
          persistently_and_intuitionistically_sep_r intuitionistically_elim.
325 326 327
  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=> ->. }
328
  destruct d; simplify_eq/=; solve_sep_entails.
329 330
Qed.

331
Lemma prop_of_env_sound Γ : prop_of_env Γ  [] Γ.
Jacques-Henri Jourdan's avatar
Jacques-Henri Jourdan committed
332
Proof.
333 334 335 336
  destruct Γ as [|Γ ? P]; simpl; first done.
  revert P. induction Γ as [|Γ IH ? Q]=>P; simpl.
  - by rewrite right_id.
  - rewrite /= IH (comm _ Q _) assoc. done.
Jacques-Henri Jourdan's avatar
Jacques-Henri Jourdan committed
337 338
Qed.

339 340
Lemma pm_maybe_wand_sound mP Q :
  pm_maybe_wand mP Q  (default emp mP - Q).
341
Proof. destruct mP; simpl; first done. rewrite emp_wand //. Qed.
Ralf Jung's avatar
Ralf Jung committed
342

Robbert Krebbers's avatar
Robbert Krebbers committed
343
Global Instance envs_Forall2_refl (R : relation PROP) :
344 345
  Reflexive R  Reflexive (envs_Forall2 R).
Proof. by constructor. Qed.
Robbert Krebbers's avatar
Robbert Krebbers committed
346
Global Instance envs_Forall2_sym (R : relation PROP) :
347 348
  Symmetric R  Symmetric (envs_Forall2 R).
Proof. intros ??? [??]; by constructor. Qed.
Robbert Krebbers's avatar
Robbert Krebbers committed
349
Global Instance envs_Forall2_trans (R : relation PROP) :
350 351
  Transitive R  Transitive (envs_Forall2 R).
Proof. intros ??? [??] [??] [??]; constructor; etrans; eauto. Qed.
Robbert Krebbers's avatar
Robbert Krebbers committed
352
Global Instance envs_Forall2_antisymm (R R' : relation PROP) :
353 354
  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
355
Lemma envs_Forall2_impl (R R' : relation PROP) Δ1 Δ2 :
356 357 358
  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
359
Global Instance of_envs_mono : Proper (envs_Forall2 () ==> ()) (@of_envs PROP).
360
Proof.
Robbert Krebbers's avatar
Robbert Krebbers committed
361 362
  intros [Γp1 Γs1] [Γp2 Γs2] [Hp Hs]; apply and_mono; simpl in *.
  - apply pure_mono=> -[???]. constructor;
363 364 365
      naive_solver eauto using env_Forall2_wf, env_Forall2_fresh.
  - by repeat f_equiv.
Qed.
Robbert Krebbers's avatar
Robbert Krebbers committed
366 367
Global Instance of_envs_proper :
  Proper (envs_Forall2 () ==> ()) (@of_envs PROP).
368
Proof.
369 370
  intros Δ1 Δ2 HΔ; apply (anti_symm ()); apply of_envs_mono;
    eapply (envs_Forall2_impl ()); [| |symmetry|]; eauto using equiv_entails.
371
Qed.
372
Global Instance Envs_proper (R : relation PROP) :
373
  Proper (env_Forall2 R ==> env_Forall2 R ==> eq ==> envs_Forall2 R) (@Envs PROP).
374 375
Proof. by constructor. Qed.

376
Global Instance envs_entails_proper :
377
  Proper (envs_Forall2 () ==> () ==> iff) (@envs_entails PROP).
378
Proof. rewrite envs_entails_eq. solve_proper. Qed.
379
Global Instance envs_entails_flip_mono :
380
  Proper (envs_Forall2 () ==> flip () ==> flip impl) (@envs_entails PROP).
381
Proof. rewrite envs_entails_eq=> Δ1 Δ2 ? P1 P2 <- <-. by f_equiv. Qed.
382

Robbert Krebbers's avatar
Robbert Krebbers committed
383
(** * Adequacy *)
384
Lemma tac_adequate P : envs_entails (Envs Enil Enil 1) P  P.
Robbert Krebbers's avatar
Robbert Krebbers committed
385
Proof.
386 387
  rewrite envs_entails_eq /of_envs /= intuitionistically_True_emp
          left_id=><-.
Robbert Krebbers's avatar
Robbert Krebbers committed
388
  apply and_intro=> //. apply pure_intro; repeat constructor.
Robbert Krebbers's avatar
Robbert Krebbers committed
389 390 391
Qed.

(** * Basic rules *)
392
Lemma tac_eval Δ Q Q' :
393 394 395
  ( (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]. *)
396
  envs_entails Δ Q'  envs_entails Δ Q.
397
Proof. by intros <-. Qed.
398

399 400
Lemma tac_eval_in Δ Δ' i p P P' Q :
  envs_lookup i Δ = Some (p, P) 
401
  ( (P'':=P'), P  P') 
402 403 404
  envs_simple_replace i p (Esnoc Enil i P') Δ  = Some Δ' 
  envs_entails Δ' Q  envs_entails Δ Q.
Proof.
405 406 407
  rewrite envs_entails_eq /=. intros ? HP ? <-.
  rewrite envs_simple_replace_singleton_sound //; simpl.
  by rewrite HP wand_elim_r.
408
Qed.
409

410 411 412 413 414 415 416
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.

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

421
Instance affine_env_spatial Δ :
422 423 424
  AffineEnv (env_spatial Δ)  Affine ([] env_spatial Δ).
Proof. intros H. induction H; simpl; apply _. Qed.

425 426
Lemma tac_emp_intro Δ : AffineEnv (env_spatial Δ)  envs_entails Δ emp.
Proof. intros. by rewrite envs_entails_eq (affine (of_envs Δ)). Qed.
427

Robbert Krebbers's avatar
Robbert Krebbers committed
428
Lemma tac_assumption Δ Δ' i p P Q :
429
  envs_lookup_delete true i Δ = Some (p,P,Δ') 
Robbert Krebbers's avatar
Robbert Krebbers committed
430
  FromAssumption p P Q 
431 432
  (if env_spatial_is_nil Δ' then TCTrue
   else TCOr (Absorbing Q) (AffineEnv (env_spatial Δ'))) 
433
  envs_entails Δ Q.
Robbert Krebbers's avatar
Robbert Krebbers committed
434
Proof.
435
  intros ?? H. rewrite envs_entails_eq envs_lookup_delete_sound //.
Robbert Krebbers's avatar
Robbert Krebbers committed
436
  destruct (env_spatial_is_nil Δ') eqn:?.
437
  - by rewrite (env_spatial_is_nil_intuitionistically Δ') // sep_elim_l.
Robbert Krebbers's avatar
Robbert Krebbers committed
438
  - rewrite from_assumption. destruct H; by rewrite sep_elim_l.
Robbert Krebbers's avatar
Robbert Krebbers committed
439
Qed.
Robbert Krebbers's avatar
Robbert Krebbers committed
440 441 442 443

Lemma tac_rename Δ Δ' i j p P Q :
  envs_lookup i Δ = Some (p,P) 
  envs_simple_replace i p (Esnoc Enil j P) Δ = Some Δ' 
444 445
  envs_entails Δ' Q 
  envs_entails Δ Q.
Robbert Krebbers's avatar
Robbert Krebbers committed
446
Proof.
447
  rewrite envs_entails_eq=> ?? <-. rewrite envs_simple_replace_singleton_sound //.
448
  by rewrite wand_elim_r.
Robbert Krebbers's avatar
Robbert Krebbers committed
449
Qed.
Robbert Krebbers's avatar
Robbert Krebbers committed
450

Robbert Krebbers's avatar
Robbert Krebbers committed
451
Lemma tac_clear Δ Δ' i p P Q :
452
  envs_lookup_delete true i Δ = Some (p,P,Δ') 
Robbert Krebbers's avatar
Robbert Krebbers committed
453
  (if p then TCTrue else TCOr (Affine P) (Absorbing Q)) 
454
  envs_entails Δ' Q 
455
  envs_entails Δ Q.
Robbert Krebbers's avatar
Robbert Krebbers committed
456
Proof.
457
  rewrite envs_entails_eq=> ?? HQ. rewrite envs_lookup_delete_sound //.
458
  by destruct p; rewrite /= HQ sep_elim_r.
Robbert Krebbers's avatar
Robbert Krebbers committed
459
Qed.
Robbert Krebbers's avatar
Robbert Krebbers committed
460 461

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

Robbert Krebbers's avatar
Robbert Krebbers committed
465 466 467
Lemma tac_false_destruct Δ i p P Q :
  envs_lookup i Δ = Some (p,P) 
  P = False%I 
468
  envs_entails Δ Q.
Robbert Krebbers's avatar
Robbert Krebbers committed
469
Proof.
470
  rewrite envs_entails_eq => ??. subst. rewrite envs_lookup_sound //; simpl.
471
  by rewrite intuitionistically_if_elim sep_elim_l False_elim.
Robbert Krebbers's avatar
Robbert Krebbers committed
472 473
Qed.

Robbert Krebbers's avatar
Robbert Krebbers committed
474
(** * Pure *)
Jacques-Henri Jourdan's avatar
Jacques-Henri Jourdan committed
475 476
(* This relies on the invariant that [FromPure false] implies
   [FromPure true] *)
477 478 479 480
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.
481 482
  - rewrite env_spatial_is_nil_intuitionistically //= /bi_intuitionistically.
    f_equiv. by apply pure_intro.
483 484
  - by apply pure_intro.
Qed.
485

Robbert Krebbers's avatar
Robbert Krebbers committed
486
Lemma tac_pure Δ Δ' i p P φ Q :
487
  envs_lookup_delete true i Δ = Some (p, P, Δ') 
Robbert Krebbers's avatar
Robbert Krebbers committed
488
  IntoPure P φ 
489
  (if p then TCTrue else TCOr (Affine P) (Absorbing Q)) 
490
  (φ  envs_entails Δ' Q)  envs_entails Δ Q.
Robbert Krebbers's avatar
Robbert Krebbers committed
491
Proof.
492
  rewrite envs_entails_eq=> ?? HPQ HQ.
493
  rewrite envs_lookup_delete_sound //; simpl. destruct p; simpl.
494
  - rewrite (into_pure P) -persistently_and_intuitionistically_sep_l persistently_pure.
Robbert Krebbers's avatar
Robbert Krebbers committed
495
    by apply pure_elim_l.
496
  - destruct HPQ.
497
    + rewrite -(affine_affinely P) (into_pure P) -persistent_and_affinely_sep_l.
498
      by apply pure_elim_l.
499
    + rewrite (into_pure P) -(persistent_absorbingly_affinely  _ %I) absorbingly_sep_lr.
500
      rewrite -persistent_and_affinely_sep_l. apply pure_elim_l=> ?. by rewrite HQ.
Robbert Krebbers's avatar
Robbert Krebbers committed
501 502
Qed.

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

506
(** * Persistence *)
Robbert Krebbers's avatar
Robbert Krebbers committed
507
Lemma tac_persistent Δ Δ' i p P P' Q :
508
  envs_lookup i Δ = Some (p, P) 
509
  IntoPersistent p P P' 
510
  (if p then TCTrue else TCOr (Affine P) (Absorbing Q)) 
Robbert Krebbers's avatar
Robbert Krebbers committed
511
  envs_replace i p true (Esnoc Enil i P') Δ = Some Δ' 
512
  envs_entails Δ' Q  envs_entails Δ Q.
Robbert Krebbers's avatar
Robbert Krebbers committed
513
Proof.
514
  rewrite envs_entails_eq=>?? HPQ ? HQ. rewrite envs_replace_singleton_sound //=.
515
  destruct p; simpl; rewrite /bi_intuitionistically.
516 517
  - by rewrite -(into_persistent _ P) /= wand_elim_r.
  - destruct HPQ.
518
    + rewrite -(affine_affinely P) (_ : P = <pers>?false P)%I //
519
              (into_persistent _ P) wand_elim_r //.
520
    + rewrite (_ : P = <pers>?false P)%I // (into_persistent _ P).
Ralf Jung's avatar
Ralf Jung committed
521
      by rewrite -{1}absorbingly_intuitionistically_into_persistently
522
        absorbingly_sep_l wand_elim_r HQ.
Robbert Krebbers's avatar
Robbert Krebbers committed
523 524 525
Qed.

(** * Implication and wand *)
526 527
Lemma tac_impl_intro Δ Δ' i P P' Q R :
  FromImpl R P Q 
528
  (if env_spatial_is_nil Δ then TCTrue else Persistent P) 
529
  envs_app false (Esnoc Enil i P') Δ = Some Δ' 
530
  FromAffinely P' P 
531
  envs_entails Δ' Q  envs_entails Δ R.
Robbert Krebbers's avatar
Robbert Krebbers committed
532
Proof.
533
  rewrite /FromImpl envs_entails_eq => <- ??? <-. destruct (env_spatial_is_nil Δ) eqn:?.
534
  - rewrite (env_spatial_is_nil_intuitionistically Δ) //; simpl. apply impl_intro_l.
535
    rewrite envs_app_singleton_sound //; simpl.
536
    rewrite -(from_affinely P') -affinely_and_lr.
537
    by rewrite persistently_and_intuitionistically_sep_r intuitionistically_elim wand_elim_r.
538
  - apply impl_intro_l. rewrite envs_app_singleton_sound //; simpl.
539
    by rewrite -(from_affinely P') persistent_and_affinely_sep_l_1 wand_elim_r.
Robbert Krebbers's avatar
Robbert Krebbers committed
540
Qed.
541 542
Lemma tac_impl_intro_persistent Δ Δ' i P P' Q R :
  FromImpl R P Q 
543
  IntoPersistent false P P' 
Robbert Krebbers's avatar
Robbert Krebbers committed
544
  envs_app true (Esnoc Enil i P') Δ = Some Δ' 
545
  envs_entails Δ' Q  envs_entails Δ R.
Robbert Krebbers's avatar
Robbert Krebbers committed
546
Proof.
547
  rewrite /FromImpl envs_entails_eq => <- ?? <-.
548
  rewrite envs_app_singleton_sound //=. apply impl_intro_l.
549
  rewrite (_ : P = <pers>?false P)%I // (into_persistent false P).
550
  by rewrite persistently_and_intuitionistically_sep_l wand_elim_r.
551
Qed.
552 553
Lemma tac_impl_intro_drop Δ P Q R :
  FromImpl R P Q  envs_entails Δ Q  envs_entails Δ R.
554 555 556
Proof.
  rewrite /FromImpl envs_entails_eq => <- ?. apply impl_intro_l. by rewrite and_elim_r.
Qed.
Robbert Krebbers's avatar
Robbert Krebbers committed
557

558 559
Lemma tac_wand_intro Δ Δ' i P Q R :
  FromWand R P Q 
560
  envs_app false (Esnoc Enil i P) Δ = Some Δ' 
561
  envs_entails Δ' Q  envs_entails Δ R.
Robbert Krebbers's avatar
Robbert Krebbers committed
562
Proof.
563
  rewrite /FromWand envs_entails_eq => <- ? HQ.
564
  rewrite envs_app_sound //; simpl. by rewrite right_id HQ.
Robbert Krebbers's avatar
Robbert Krebbers committed
565
Qed.
566 567
Lemma tac_wand_intro_persistent Δ Δ' i P P' Q R :
  FromWand R P Q 
568
  IntoPersistent false P P' 
569
  TCOr (Affine P) (Absorbing Q) 
Robbert Krebbers's avatar
Robbert Krebbers committed
570
  envs_app true (Esnoc Enil i P') Δ = Some Δ' 
571
  envs_entails Δ' Q  envs_entails Δ R.
Robbert Krebbers's avatar
Robbert Krebbers committed
572
Proof.
573
  rewrite /FromWand envs_entails_eq => <- ? HPQ ? HQ.
574
  rewrite envs_app_singleton_sound //=. apply wand_intro_l. destruct HPQ.
575
  - rewrite -(affine_affinely P) (_ : P = <pers>?false P)%I //
576
            (into_persistent _ P) wand_elim_r //.
577
  - rewrite (_ : P = <pers>?false P)%I // (into_persistent _ P).
Ralf Jung's avatar
Ralf Jung committed
578
    by rewrite -{1}absorbingly_intuitionistically_into_persistently
579
      absorbingly_sep_l wand_elim_r HQ.
580
Qed.
581 582
Lemma tac_wand_intro_pure Δ P φ Q R :
  FromWand R P Q 
583
  IntoPure P φ 
584
  TCOr (Affine P) (Absorbing Q) 
585
  (φ  envs_entails Δ Q)  envs_entails Δ R.
586
Proof.