coq_tactics.v 54.6 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.
Robbert Krebbers's avatar
Robbert Krebbers committed
68 69

Lemma envs_lookup_split Δ i p P :
70
  envs_lookup i Δ = Some (p,P)  of_envs Δ  ?p P  (?p P - of_envs Δ).
Robbert Krebbers's avatar
Robbert Krebbers committed
71
Proof.
Robbert Krebbers's avatar
Robbert Krebbers committed
72
  rewrite /envs_lookup /of_envs=>?. apply pure_elim_l=> Hwf.
Robbert Krebbers's avatar
Robbert Krebbers committed
73
  destruct Δ as [Γp Γs], (Γp !! i) eqn:?; simplify_eq/=.
74
  - rewrite pure_True // left_id (env_lookup_perm Γp) //=
75
      intuitionistically_and and_sep_intuitionistically.
76
    cancel [ P]%I. apply wand_intro_l. solve_sep_entails.
Robbert Krebbers's avatar
Robbert Krebbers committed
77
  - destruct (Γs !! i) eqn:?; simplify_eq/=.
78
    rewrite (env_lookup_perm Γs) //=. rewrite pure_True // left_id.
Robbert Krebbers's avatar
Robbert Krebbers committed
79 80 81
    cancel [P]. apply wand_intro_l. solve_sep_entails.
Qed.

82 83 84
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
85

86 87
Lemma envs_lookup_delete_list_sound Δ Δ' rp js p Ps :
  envs_lookup_delete_list rp js Δ = Some (p,Ps,Δ') 
88
  of_envs Δ  ?p [] Ps  of_envs Δ'.
89 90
Proof.
  revert Δ Δ' p Ps. induction js as [|j js IH]=> Δ Δ'' p Ps ?; simplify_eq/=.
91
  { by rewrite intuitionistically_emp left_id. }
92
  destruct (envs_lookup_delete rp j Δ) as [[[q1 P] Δ']|] eqn:Hj; simplify_eq/=.
93
  apply envs_lookup_delete_Some in Hj as [Hj ->].
94
  destruct (envs_lookup_delete_list _ js _) as [[[q2 Ps'] ?]|] eqn:?; simplify_eq/=.
95
  rewrite -intuitionistically_if_sep_2 -assoc.
96
  rewrite envs_lookup_sound' //; rewrite IH //.
97
  repeat apply sep_mono=>//; apply intuitionistically_if_flag_mono; by destruct q1.
98 99
Qed.

Joseph Tassarotti's avatar
Joseph Tassarotti committed
100 101 102 103 104 105 106 107 108 109
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.

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

139
Lemma envs_app_sound Δ Δ' p Γ :
140 141
  envs_app p Γ Δ = Some Δ' 
  of_envs Δ  (if p then  [] Γ else [] Γ) - of_envs Δ'.
Robbert Krebbers's avatar
Robbert Krebbers committed
142
Proof.
Robbert Krebbers's avatar
Robbert Krebbers committed
143
  rewrite /of_envs /envs_app=> ?; apply pure_elim_l=> Hwf.
Robbert Krebbers's avatar
Robbert Krebbers committed
144 145 146
  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
147
    apply wand_intro_l, and_intro; [apply pure_intro|].
Robbert Krebbers's avatar
Robbert Krebbers committed
148 149 150 151
    + 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') //.
152
      rewrite big_opL_app intuitionistically_and and_sep_intuitionistically.
153
      solve_sep_entails.
Robbert Krebbers's avatar
Robbert Krebbers committed
154 155
  - destruct (env_app Γ Γp) eqn:Happ,
      (env_app Γ Γs) as [Γs'|] 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
    + 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.
160
    + rewrite (env_app_perm _ _ Γs') // big_opL_app. solve_sep_entails.
Robbert Krebbers's avatar
Robbert Krebbers committed
161 162
Qed.

163
Lemma envs_app_singleton_sound Δ Δ' p j Q :
164
  envs_app p (Esnoc Enil j Q) Δ = Some Δ'  of_envs Δ  ?p Q - of_envs Δ'.
165 166
Proof. move=> /envs_app_sound. destruct p; by rewrite /= right_id. Qed.

Robbert Krebbers's avatar
Robbert Krebbers committed
167 168
Lemma envs_simple_replace_sound' Δ Δ' i p Γ :
  envs_simple_replace i p Γ Δ = Some Δ' 
169
  of_envs (envs_delete true i p Δ)  (if p then  [] Γ else [] Γ) - of_envs Δ'.
Robbert Krebbers's avatar
Robbert Krebbers committed
170 171
Proof.
  rewrite /envs_simple_replace /envs_delete /of_envs=> ?.
Robbert Krebbers's avatar
Robbert Krebbers committed
172
  apply pure_elim_l=> Hwf. destruct Δ as [Γp Γs], p; simplify_eq/=.
Robbert Krebbers's avatar
Robbert Krebbers committed
173 174
  - destruct (env_app Γ Γs) eqn:Happ,
      (env_replace i Γ Γp) as [Γp'|] eqn:?; simplify_eq/=.
Robbert Krebbers's avatar
Robbert Krebbers committed
175
    apply wand_intro_l, and_intro; [apply pure_intro|].
Robbert Krebbers's avatar
Robbert Krebbers committed
176 177 178 179
    + 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') //.
180
      rewrite big_opL_app intuitionistically_and and_sep_intuitionistically.
181
      solve_sep_entails.
Robbert Krebbers's avatar
Robbert Krebbers committed
182 183
  - destruct (env_app Γ Γp) eqn:Happ,
      (env_replace i Γ Γs) as [Γs'|] 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
    + 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.
188
    + rewrite (env_replace_perm _ _ Γs') // big_opL_app. solve_sep_entails.
Robbert Krebbers's avatar
Robbert Krebbers committed
189 190
Qed.

191 192
Lemma envs_simple_replace_singleton_sound' Δ Δ' i p j Q :
  envs_simple_replace i p (Esnoc Enil j Q) Δ = Some Δ' 
193
  of_envs (envs_delete true i p Δ)  ?p Q - of_envs Δ'.
194 195
Proof. move=> /envs_simple_replace_sound'. destruct p; by rewrite /= right_id. Qed.

Robbert Krebbers's avatar
Robbert Krebbers committed
196 197
Lemma envs_simple_replace_sound Δ Δ' i p P Γ :
  envs_lookup i Δ = Some (p,P)  envs_simple_replace i p Γ Δ = Some Δ' 
198
  of_envs Δ  ?p P  ((if p then  [] Γ else [] Γ) - of_envs Δ').
Robbert Krebbers's avatar
Robbert Krebbers committed
199 200
Proof. intros. by rewrite envs_lookup_sound// envs_simple_replace_sound'//. Qed.

201 202 203
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 Δ' 
204
  of_envs Δ  ?p P  (?p Q - of_envs Δ').
205 206 207 208
Proof.
  intros. by rewrite envs_lookup_sound// envs_simple_replace_singleton_sound'//.
Qed.

Robbert Krebbers's avatar
Robbert Krebbers committed
209
Lemma envs_replace_sound' Δ Δ' i p q Γ :
210
  envs_replace i p q Γ Δ = Some Δ' 
211
  of_envs (envs_delete true i p Δ)  (if q then  [] Γ else [] Γ) - of_envs Δ'.
Robbert Krebbers's avatar
Robbert Krebbers committed
212
Proof.
213
  rewrite /envs_replace; destruct (beq _ _) eqn:Hpq.
Robbert Krebbers's avatar
Robbert Krebbers committed
214 215 216 217
  - apply eqb_prop in Hpq as ->. apply envs_simple_replace_sound'.
  - apply envs_app_sound.
Qed.

218 219
Lemma envs_replace_singleton_sound' Δ Δ' i p q j Q :
  envs_replace i p q (Esnoc Enil j Q) Δ = Some Δ' 
220
  of_envs (envs_delete true i p Δ)  ?q Q - of_envs Δ'.
221 222
Proof. move=> /envs_replace_sound'. destruct q; by rewrite /= ?right_id. Qed.

Robbert Krebbers's avatar
Robbert Krebbers committed
223 224
Lemma envs_replace_sound Δ Δ' i p q P Γ :
  envs_lookup i Δ = Some (p,P)  envs_replace i p q Γ Δ = Some Δ' 
225
  of_envs Δ  ?p P  ((if q then  [] Γ else [] Γ) - of_envs Δ').
Robbert Krebbers's avatar
Robbert Krebbers committed
226 227
Proof. intros. by rewrite envs_lookup_sound// envs_replace_sound'//. Qed.

228 229 230
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 Δ' 
231
  of_envs Δ  ?p P  (?q Q - of_envs Δ').
232 233
Proof. intros. by rewrite envs_lookup_sound// envs_replace_singleton_sound'//. Qed.

234 235
Lemma envs_lookup_envs_clear_spatial Δ j :
  envs_lookup j (envs_clear_spatial Δ)
Robbert Krebbers's avatar
Robbert Krebbers committed
236
  = ''(p,P)  envs_lookup j Δ; if p : bool then Some (p,P) else None.
Robbert Krebbers's avatar
Robbert Krebbers committed
237
Proof.
238 239 240
  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
241 242
Qed.

Robbert Krebbers's avatar
Robbert Krebbers committed
243
Lemma envs_clear_spatial_sound Δ :
244
  of_envs Δ  of_envs (envs_clear_spatial Δ)  [] env_spatial Δ.
245
Proof.
Robbert Krebbers's avatar
Robbert Krebbers committed
246 247 248
  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.
249 250
Qed.

251
Lemma env_spatial_is_nil_intuitionistically Δ :
252
  env_spatial_is_nil Δ = true  of_envs Δ   of_envs Δ.
Robbert Krebbers's avatar
Robbert Krebbers committed
253 254
Proof.
  intros. unfold of_envs; destruct Δ as [? []]; simplify_eq/=.
255
  rewrite !right_id /bi_intuitionistically {1}affinely_and_r persistently_and.
Ralf Jung's avatar
Ralf Jung committed
256
  by rewrite persistently_affinely_elim persistently_idemp persistently_pure.
Robbert Krebbers's avatar
Robbert Krebbers committed
257
Qed.
Robbert Krebbers's avatar
Robbert Krebbers committed
258

259 260
Lemma envs_lookup_envs_delete Δ i p P :
  envs_wf Δ 
261
  envs_lookup i Δ = Some (p,P)  envs_lookup i (envs_delete true i p Δ) = None.
262 263 264 265 266 267 268
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.
269 270
Lemma envs_lookup_envs_delete_ne Δ rp i j p :
  i  j  envs_lookup i (envs_delete rp j p Δ) = envs_lookup i Δ.
271 272
Proof.
  rewrite /envs_lookup /envs_delete=> ?. destruct Δ as [Γp Γs],p; simplify_eq/=.
273
  - destruct rp=> //. by rewrite env_lookup_env_delete_ne.
274 275 276 277 278
  - 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
279
  envs_split_go js Δ1 Δ2 = Some (Δ1',Δ2') 
280
  of_envs Δ1  of_envs Δ2  of_envs Δ1'  of_envs Δ2'.
281
Proof.
282 283
  revert Δ1 Δ2.
  induction js as [|j js IH]=> Δ1 Δ2 Hlookup HΔ; simplify_eq/=; [done|].
Robbert Krebbers's avatar
Robbert Krebbers committed
284 285
  apply pure_elim with (envs_wf Δ1)=> [|Hwf].
  { by rewrite /of_envs !and_elim_l sep_elim_l. }
286 287
  destruct (envs_lookup_delete _ j Δ1)
    as [[[[] P] Δ1'']|] eqn:Hdel; simplify_eq/=; auto.
288 289
  apply envs_lookup_delete_Some in Hdel as [??]; subst.
  rewrite envs_lookup_sound //; rewrite /= (comm _ P) -assoc.
290 291 292 293
  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. }
294 295
  rewrite (envs_snoc_sound Δ2 false j P) /= ?wand_elim_r; eauto.
Qed.
296
Lemma envs_split_sound Δ d js Δ1 Δ2 :
297
  envs_split d js Δ = Some (Δ1,Δ2)  of_envs Δ  of_envs Δ1  of_envs Δ2.
298
Proof.
299
  rewrite /envs_split=> ?. rewrite -(idemp bi_and (of_envs Δ)).
Robbert Krebbers's avatar
Robbert Krebbers committed
300
  rewrite {2}envs_clear_spatial_sound.
301 302
  rewrite (env_spatial_is_nil_intuitionistically (envs_clear_spatial _)) //.
  rewrite -persistently_and_intuitionistically_sep_l.
303
  rewrite (and_elim_l (<pers> _)%I)
304
          persistently_and_intuitionistically_sep_r intuitionistically_elim.
305 306 307
  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=> ->. }
308
  destruct d; simplify_eq/=; solve_sep_entails.
309 310
Qed.

311
Lemma prop_of_env_sound Γ : prop_of_env Γ  [] Γ.
Jacques-Henri Jourdan's avatar
Jacques-Henri Jourdan committed
312
Proof.
313 314 315 316
  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
317 318
Qed.

319 320
Lemma pm_maybe_wand_sound mP Q :
  pm_maybe_wand mP Q  (default emp mP - Q).
321
Proof. destruct mP; simpl; first done. rewrite emp_wand //. Qed.
Ralf Jung's avatar
Ralf Jung committed
322

Robbert Krebbers's avatar
Robbert Krebbers committed
323
Global Instance envs_Forall2_refl (R : relation PROP) :
324 325
  Reflexive R  Reflexive (envs_Forall2 R).
Proof. by constructor. Qed.
Robbert Krebbers's avatar
Robbert Krebbers committed
326
Global Instance envs_Forall2_sym (R : relation PROP) :
327 328
  Symmetric R  Symmetric (envs_Forall2 R).
Proof. intros ??? [??]; by constructor. Qed.
Robbert Krebbers's avatar
Robbert Krebbers committed
329
Global Instance envs_Forall2_trans (R : relation PROP) :
330 331
  Transitive R  Transitive (envs_Forall2 R).
Proof. intros ??? [??] [??] [??]; constructor; etrans; eauto. Qed.
Robbert Krebbers's avatar
Robbert Krebbers committed
332
Global Instance envs_Forall2_antisymm (R R' : relation PROP) :
333 334
  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
335
Lemma envs_Forall2_impl (R R' : relation PROP) Δ1 Δ2 :
336 337 338
  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
339
Global Instance of_envs_mono : Proper (envs_Forall2 () ==> ()) (@of_envs PROP).
340
Proof.
Robbert Krebbers's avatar
Robbert Krebbers committed
341 342
  intros [Γp1 Γs1] [Γp2 Γs2] [Hp Hs]; apply and_mono; simpl in *.
  - apply pure_mono=> -[???]. constructor;
343 344 345
      naive_solver eauto using env_Forall2_wf, env_Forall2_fresh.
  - by repeat f_equiv.
Qed.
Robbert Krebbers's avatar
Robbert Krebbers committed
346 347
Global Instance of_envs_proper :
  Proper (envs_Forall2 () ==> ()) (@of_envs PROP).
348
Proof.
349 350
  intros Δ1 Δ2 HΔ; apply (anti_symm ()); apply of_envs_mono;
    eapply (envs_Forall2_impl ()); [| |symmetry|]; eauto using equiv_entails.
351
Qed.
352
Global Instance Envs_proper (R : relation PROP) :
353
  Proper (env_Forall2 R ==> env_Forall2 R ==> eq ==> envs_Forall2 R) (@Envs PROP).
354 355
Proof. by constructor. Qed.

356
Global Instance envs_entails_proper :
357
  Proper (envs_Forall2 () ==> () ==> iff) (@envs_entails PROP).
358
Proof. rewrite envs_entails_eq. solve_proper. Qed.
359
Global Instance envs_entails_flip_mono :
360
  Proper (envs_Forall2 () ==> flip () ==> flip impl) (@envs_entails PROP).
361
Proof. rewrite envs_entails_eq=> Δ1 Δ2 ? P1 P2 <- <-. by f_equiv. Qed.
362

Robbert Krebbers's avatar
Robbert Krebbers committed
363
(** * Adequacy *)
364
Lemma tac_adequate P : envs_entails (Envs Enil Enil 1) P  P.
Robbert Krebbers's avatar
Robbert Krebbers committed
365
Proof.
366 367
  rewrite envs_entails_eq /of_envs /= intuitionistically_True_emp
          left_id=><-.
Robbert Krebbers's avatar
Robbert Krebbers committed
368
  apply and_intro=> //. apply pure_intro; repeat constructor.
Robbert Krebbers's avatar
Robbert Krebbers committed
369 370 371
Qed.

(** * Basic rules *)
372
Lemma tac_eval Δ Q Q' :
373 374 375
  ( (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]. *)
376
  envs_entails Δ Q'  envs_entails Δ Q.
377
Proof. by intros <-. Qed.
378

379 380
Lemma tac_eval_in Δ Δ' i p P P' Q :
  envs_lookup i Δ = Some (p, P) 
381
  ( (P'':=P'), P  P') 
382 383 384
  envs_simple_replace i p (Esnoc Enil i P') Δ  = Some Δ' 
  envs_entails Δ' Q  envs_entails Δ Q.
Proof.
385 386 387
  rewrite envs_entails_eq /=. intros ? HP ? <-.
  rewrite envs_simple_replace_singleton_sound //; simpl.
  by rewrite HP wand_elim_r.
388
Qed.
389

390 391 392 393 394 395 396
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.

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

401
Instance affine_env_spatial Δ :
402 403 404
  AffineEnv (env_spatial Δ)  Affine ([] env_spatial Δ).
Proof. intros H. induction H; simpl; apply _. Qed.

405 406
Lemma tac_emp_intro Δ : AffineEnv (env_spatial Δ)  envs_entails Δ emp.
Proof. intros. by rewrite envs_entails_eq (affine (of_envs Δ)). Qed.
407

Robbert Krebbers's avatar
Robbert Krebbers committed
408
Lemma tac_assumption Δ Δ' i p P Q :
409
  envs_lookup_delete true i Δ = Some (p,P,Δ') 
Robbert Krebbers's avatar
Robbert Krebbers committed
410
  FromAssumption p P Q 
411 412
  (if env_spatial_is_nil Δ' then TCTrue
   else TCOr (Absorbing Q) (AffineEnv (env_spatial Δ'))) 
413
  envs_entails Δ Q.
Robbert Krebbers's avatar
Robbert Krebbers committed
414
Proof.
415
  intros ?? H. rewrite envs_entails_eq envs_lookup_delete_sound //.
Robbert Krebbers's avatar
Robbert Krebbers committed
416
  destruct (env_spatial_is_nil Δ') eqn:?.
417
  - by rewrite (env_spatial_is_nil_intuitionistically Δ') // sep_elim_l.
Robbert Krebbers's avatar
Robbert Krebbers committed
418
  - rewrite from_assumption. destruct H; by rewrite sep_elim_l.
Robbert Krebbers's avatar
Robbert Krebbers committed
419
Qed.
Robbert Krebbers's avatar
Robbert Krebbers committed
420 421 422 423

Lemma tac_rename Δ Δ' i j p P Q :
  envs_lookup i Δ = Some (p,P) 
  envs_simple_replace i p (Esnoc Enil j P) Δ = Some Δ' 
424 425
  envs_entails Δ' Q 
  envs_entails Δ Q.
Robbert Krebbers's avatar
Robbert Krebbers committed
426
Proof.
427
  rewrite envs_entails_eq=> ?? <-. rewrite envs_simple_replace_singleton_sound //.
428
  by rewrite wand_elim_r.
Robbert Krebbers's avatar
Robbert Krebbers committed
429
Qed.
Robbert Krebbers's avatar
Robbert Krebbers committed
430

Robbert Krebbers's avatar
Robbert Krebbers committed
431
Lemma tac_clear Δ Δ' i p P Q :
432
  envs_lookup_delete true i Δ = Some (p,P,Δ') 
Robbert Krebbers's avatar
Robbert Krebbers committed
433
  (if p then TCTrue else TCOr (Affine P) (Absorbing Q)) 
434
  envs_entails Δ' Q 
435
  envs_entails Δ Q.
Robbert Krebbers's avatar
Robbert Krebbers committed
436
Proof.
437
  rewrite envs_entails_eq=> ?? HQ. rewrite envs_lookup_delete_sound //.
438
  by destruct p; rewrite /= HQ sep_elim_r.
Robbert Krebbers's avatar
Robbert Krebbers committed
439
Qed.
Robbert Krebbers's avatar
Robbert Krebbers committed
440 441

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

Robbert Krebbers's avatar
Robbert Krebbers committed
445 446 447
Lemma tac_false_destruct Δ i p P Q :
  envs_lookup i Δ = Some (p,P) 
  P = False%I 
448
  envs_entails Δ Q.
Robbert Krebbers's avatar
Robbert Krebbers committed
449
Proof.
450
  rewrite envs_entails_eq => ??. subst. rewrite envs_lookup_sound //; simpl.
451
  by rewrite intuitionistically_if_elim sep_elim_l False_elim.
Robbert Krebbers's avatar
Robbert Krebbers committed
452 453
Qed.

Robbert Krebbers's avatar
Robbert Krebbers committed
454
(** * Pure *)
Jacques-Henri Jourdan's avatar
Jacques-Henri Jourdan committed
455 456
(* This relies on the invariant that [FromPure false] implies
   [FromPure true] *)
457 458 459 460
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.
461 462
  - rewrite env_spatial_is_nil_intuitionistically //= /bi_intuitionistically.
    f_equiv. by apply pure_intro.
463 464
  - by apply pure_intro.
Qed.
465

Robbert Krebbers's avatar
Robbert Krebbers committed
466
Lemma tac_pure Δ Δ' i p P φ Q :
467
  envs_lookup_delete true i Δ = Some (p, P, Δ') 
Robbert Krebbers's avatar
Robbert Krebbers committed
468
  IntoPure P φ 
469
  (if p then TCTrue else TCOr (Affine P) (Absorbing Q)) 
470
  (φ  envs_entails Δ' Q)  envs_entails Δ Q.
Robbert Krebbers's avatar
Robbert Krebbers committed
471
Proof.
472
  rewrite envs_entails_eq=> ?? HPQ HQ.
473
  rewrite envs_lookup_delete_sound //; simpl. destruct p; simpl.
474
  - rewrite (into_pure P) -persistently_and_intuitionistically_sep_l persistently_pure.
Robbert Krebbers's avatar
Robbert Krebbers committed
475
    by apply pure_elim_l.
476
  - destruct HPQ.
477
    + rewrite -(affine_affinely P) (into_pure P) -persistent_and_affinely_sep_l.
478
      by apply pure_elim_l.
479
    + rewrite (into_pure P) -(persistent_absorbingly_affinely  _ %I) absorbingly_sep_lr.
480
      rewrite -persistent_and_affinely_sep_l. apply pure_elim_l=> ?. by rewrite HQ.
Robbert Krebbers's avatar
Robbert Krebbers committed
481 482
Qed.

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

486
(** * Persistence *)
Robbert Krebbers's avatar
Robbert Krebbers committed
487
Lemma tac_persistent Δ Δ' i p P P' Q :
488
  envs_lookup i Δ = Some (p, P) 
489
  IntoPersistent p P P' 
490
  (if p then TCTrue else TCOr (Affine P) (Absorbing Q)) 
Robbert Krebbers's avatar
Robbert Krebbers committed
491
  envs_replace i p true (Esnoc Enil i P') Δ = Some Δ' 
492
  envs_entails Δ' Q  envs_entails Δ Q.
Robbert Krebbers's avatar
Robbert Krebbers committed
493
Proof.
494
  rewrite envs_entails_eq=>?? HPQ ? HQ. rewrite envs_replace_singleton_sound //=.
495
  destruct p; simpl; rewrite /bi_intuitionistically.
496 497
  - by rewrite -(into_persistent _ P) /= wand_elim_r.
  - destruct HPQ.
498
    + rewrite -(affine_affinely P) (_ : P = <pers>?false P)%I //
499
              (into_persistent _ P) wand_elim_r //.
500
    + rewrite (_ : P = <pers>?false P)%I // (into_persistent _ P).
Ralf Jung's avatar
Ralf Jung committed
501
      by rewrite -{1}absorbingly_intuitionistically_into_persistently
502
        absorbingly_sep_l wand_elim_r HQ.
Robbert Krebbers's avatar
Robbert Krebbers committed
503 504 505
Qed.

(** * Implication and wand *)
506 507
Lemma tac_impl_intro Δ Δ' i P P' Q R :
  FromImpl R P Q 
508
  (if env_spatial_is_nil Δ then TCTrue else Persistent P) 
509
  envs_app false (Esnoc Enil i P') Δ = Some Δ' 
510
  FromAffinely P' P 
511
  envs_entails Δ' Q  envs_entails Δ R.
Robbert Krebbers's avatar
Robbert Krebbers committed
512
Proof.
513
  rewrite /FromImpl envs_entails_eq => <- ??? <-. destruct (env_spatial_is_nil Δ) eqn:?.
514
  - rewrite (env_spatial_is_nil_intuitionistically Δ) //; simpl. apply impl_intro_l.
515
    rewrite envs_app_singleton_sound //; simpl.
516
    rewrite -(from_affinely P') -affinely_and_lr.
517
    by rewrite persistently_and_intuitionistically_sep_r intuitionistically_elim wand_elim_r.
518
  - apply impl_intro_l. rewrite envs_app_singleton_sound //; simpl.
519
    by rewrite -(from_affinely P') persistent_and_affinely_sep_l_1 wand_elim_r.
Robbert Krebbers's avatar
Robbert Krebbers committed
520
Qed.
521 522
Lemma tac_impl_intro_persistent Δ Δ' i P P' Q R :
  FromImpl R P Q 
523
  IntoPersistent false P P' 
Robbert Krebbers's avatar
Robbert Krebbers committed
524
  envs_app true (Esnoc Enil i P') Δ = Some Δ' 
525
  envs_entails Δ' Q  envs_entails Δ R.
Robbert Krebbers's avatar
Robbert Krebbers committed
526
Proof.
527
  rewrite /FromImpl envs_entails_eq => <- ?? <-.
528
  rewrite envs_app_singleton_sound //=. apply impl_intro_l.
529
  rewrite (_ : P = <pers>?false P)%I // (into_persistent false P).
530
  by rewrite persistently_and_intuitionistically_sep_l wand_elim_r.
531
Qed.
532 533
Lemma tac_impl_intro_drop Δ P Q R :
  FromImpl R P Q  envs_entails Δ Q  envs_entails Δ R.
534 535 536
Proof.
  rewrite /FromImpl envs_entails_eq => <- ?. apply impl_intro_l. by rewrite and_elim_r.
Qed.
Robbert Krebbers's avatar
Robbert Krebbers committed
537

538 539
Lemma tac_wand_intro Δ Δ' i P Q R :
  FromWand R P Q 
540
  envs_app false (Esnoc Enil i P) Δ = Some Δ' 
541
  envs_entails Δ' Q  envs_entails Δ R.
Robbert Krebbers's avatar
Robbert Krebbers committed
542
Proof.
543
  rewrite /FromWand envs_entails_eq => <- ? HQ.
544
  rewrite envs_app_sound //; simpl. by rewrite right_id HQ.
Robbert Krebbers's avatar
Robbert Krebbers committed
545
Qed.
546 547
Lemma tac_wand_intro_persistent Δ Δ' i P P' Q R :
  FromWand R P Q 
548
  IntoPersistent false P P' 
549
  TCOr (Affine P) (Absorbing Q) 
Robbert Krebbers's avatar
Robbert Krebbers committed
550
  envs_app true (Esnoc Enil i P') Δ = Some Δ' 
551
  envs_entails Δ' Q  envs_entails Δ R.
Robbert Krebbers's avatar
Robbert Krebbers committed
552
Proof.
553
  rewrite /FromWand envs_entails_eq => <- ? HPQ ? HQ.
554
  rewrite envs_app_singleton_sound //=. apply wand_intro_l. destruct HPQ.
555
  - rewrite -(affine_affinely P) (_ : P = <pers>?false P)%I //
556
            (into_persistent _ P) wand_elim_r //.
557
  - rewrite (_ : P = <pers>?false P)%I // (into_persistent _ P).
Ralf Jung's avatar
Ralf Jung committed
558
    by rewrite -{1}absorbingly_intuitionistically_into_persistently
559
      absorbingly_sep_l wand_elim_r HQ.
560
Qed.
561 562
Lemma tac_wand_intro_pure Δ P φ Q R :
  FromWand R P Q 
563
  IntoPure P φ 
564
  TCOr (Affine P) (Absorbing Q) 
565
  (φ  envs_entails Δ Q)  envs_entails Δ R.
566
Proof.
567
  rewrite /FromWand envs_entails_eq. intros <- ? HPQ HQ. apply wand_intro_l. destruct HPQ.
568
  - rewrite -(affine_affinely P) (into_pure P) -persistent_and_affinely_sep_l.
569
    by apply pure_elim_l.
570
  - rewrite (into_pure P) -(persistent_absorbingly_affinely  _ %I) absorbingly_sep_lr.
571
    rewrite -persistent_and_affinely_sep_l. apply pure_elim_l=> ?. by rewrite HQ.
Robbert Krebbers's avatar
Robbert Krebbers committed
572
Qed.
573 574
Lemma tac_wand_intro_drop Δ P Q R :
  FromWand R P Q 
Robbert Krebbers's avatar
Robbert Krebbers committed
575
  TCOr (Affine P) (Absorbing Q) 
576
  envs_entails Δ Q 
577
  envs_entails Δ R.
578
Proof.
579
  rewrite envs_entails_eq /FromWand => <- HPQ ->. apply wand_intro_l. by rewrite sep_elim_r.
Robbert Krebbers's avatar
Robbert Krebbers committed
580 581 582 583 584
Qed.

(* This is pretty much [tac_specialize_assert] with [js:=[j]] and [tac_exact],
but it is doing some work to keep the order of hypotheses preserved. *)
Lemma tac_specialize Δ Δ' Δ'' i p j q P1 P2 R Q :
585 586
  envs_lookup_delete false i Δ = Some (p, P1, Δ') 
  envs_lookup j Δ' = Some (q, R) 
Robbert Krebbers's avatar
Robbert Krebbers committed
587
  IntoWand q p R P1 P2 
Robbert Krebbers's avatar
Robbert Krebbers committed
588 589 590 591 592
  match p with
  | true  => envs_simple_replace j q (Esnoc Enil j P2) Δ
  | false => envs_replace j q false (Esnoc Enil j P2) Δ'
             (* remove [i] and make [j] spatial *)
  end = Some Δ'' 
593
  envs_entails Δ'' Q  envs_entails Δ Q.
Robbert Krebbers's avatar
Robbert Krebbers committed
594
Proof.
595 596 597
  rewrite envs_entails_eq. intros [? ->]%envs_lookup_delete_Some Hj ? Hj' <-.
  rewrite (envs_lookup_sound' _ false) //; simpl. destruct p; simpl.
  - move: Hj; rewrite envs_delete_persistent=> Hj.
598
    rewrite envs_simple_replace_singleton_sound //; simpl.
599 600 601
    rewrite -intuitionistically_if_idemp -intuitionistically_idemp into_wand /=.
    rewrite assoc (intuitionistically_intuitionistically_if q).
    by rewrite intuitionistically_if_sep_2 wand_elim_r wand_elim_r.
602
  - move: Hj Hj'; rewrite envs_delete_spatial=> Hj Hj'.
603 604
    rewrite envs_lookup_sound // (envs_replace_singleton_sound' _ Δ'') //; simpl.
    by rewrite into_wand /= assoc wand_elim_r wand_elim_r.
Robbert Krebbers's avatar
Robbert Krebbers committed
605 606
Qed.

607
Lemma tac_specialize_assert Δ Δ' Δ1 Δ2' j q neg js R P1 P2 P1' Q :
608
  envs_lookup_delete true j Δ = Some (q, R, Δ') 
609
  IntoWand q false R P1 P2  AddModal P1' P1 Q 
Robbert Krebbers's avatar
Robbert Krebbers committed
610
  (''(Δ1,Δ2)  envs_split (if neg is true then Right else Left) js Δ';
611