coq_tactics.v 59.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.

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

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

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

29 30 31 32
(* 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.
33 34
Definition envs_entails := envs_entails_aux.(unseal).
Definition envs_entails_eq : envs_entails = _ := envs_entails_aux.(seal_eq).
35
Arguments envs_entails {PROP} Δ Q%I : rename.
36 37
Instance: Params (@envs_entails) 1.

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

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

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

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

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

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

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

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

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

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

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

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

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

Definition envs_incr_counter {PROP} (Δ : envs PROP) : envs PROP :=
115
  Envs (env_intuitionistic Δ) (env_spatial Δ) (Pos.succ (env_counter Δ)).
116

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

Jacques-Henri Jourdan's avatar
Jacques-Henri Jourdan committed
133 134 135 136 137 138
Definition prop_of_env {PROP : bi} (Γ : env PROP) : PROP :=
  let fix aux Γ acc :=
    match Γ with Enil => acc | Esnoc Γ _ P => aux Γ (P  acc)%I end
  in
  match Γ with Enil => emp%I | Esnoc Γ _ P => aux Γ P end.

Ralf Jung's avatar
Ralf Jung committed
139 140 141 142 143 144
Definition maybe_wand {PROP : bi} (P : option PROP) (Q : PROP) : PROP :=
  match P with
  | None => Q
  | Some P => (P - Q)%I
  end.

Robbert Krebbers's avatar
Robbert Krebbers committed
145
(* Coq versions of the tactics *)
Robbert Krebbers's avatar
Robbert Krebbers committed
146 147 148 149 150 151 152
Section bi_tactics.
Context {PROP : bi}.
Implicit Types Γ : env PROP.
Implicit Types Δ : envs PROP.
Implicit Types P Q : PROP.

Lemma of_envs_eq Δ :
153
  of_envs Δ = (envs_wf Δ⌝   [] env_intuitionistic Δ  [] env_spatial Δ)%I.
154 155
Proof. done. Qed.

156 157 158 159 160 161 162 163 164
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
165 166 167 168 169 170
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.

171
Lemma envs_lookup_sound' Δ rp i p P :
172
  envs_lookup i Δ = Some (p,P) 
173
  of_envs Δ  ?p P  of_envs (envs_delete rp i p Δ).
Robbert Krebbers's avatar
Robbert Krebbers committed
174
Proof.
Robbert Krebbers's avatar
Robbert Krebbers committed
175
  rewrite /envs_lookup /envs_delete /of_envs=>?. apply pure_elim_l=> Hwf.
Robbert Krebbers's avatar
Robbert Krebbers committed
176
  destruct Δ as [Γp Γs], (Γp !! i) eqn:?; simplify_eq/=.
177
  - rewrite pure_True ?left_id; last (destruct Hwf, rp; constructor;
Robbert Krebbers's avatar
Robbert Krebbers committed
178
      naive_solver eauto using env_delete_wf, env_delete_fresh).
179
    destruct rp.
180 181 182 183
    + 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
184
  - destruct (Γs !! i) eqn:?; simplify_eq/=.
Robbert Krebbers's avatar
Robbert Krebbers committed
185 186 187 188
    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.
189 190 191 192
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
193
Lemma envs_lookup_persistent_sound Δ i P :
194
  envs_lookup i Δ = Some (true,P)  of_envs Δ   P  of_envs Δ.
195
Proof. intros ?%(envs_lookup_sound' _ false). by destruct Δ. Qed.
Robbert Krebbers's avatar
Robbert Krebbers committed
196 197

Lemma envs_lookup_split Δ i p P :
198
  envs_lookup i Δ = Some (p,P)  of_envs Δ  ?p P  (?p P - of_envs Δ).
Robbert Krebbers's avatar
Robbert Krebbers committed
199
Proof.
Robbert Krebbers's avatar
Robbert Krebbers committed
200
  rewrite /envs_lookup /of_envs=>?. apply pure_elim_l=> Hwf.
Robbert Krebbers's avatar
Robbert Krebbers committed
201
  destruct Δ as [Γp Γs], (Γp !! i) eqn:?; simplify_eq/=.
202
  - rewrite pure_True // left_id (env_lookup_perm Γp) //=
203
      intuitionistically_and and_sep_intuitionistically.
204
    cancel [ P]%I. apply wand_intro_l. solve_sep_entails.
Robbert Krebbers's avatar
Robbert Krebbers committed
205
  - destruct (Γs !! i) eqn:?; simplify_eq/=.
206
    rewrite (env_lookup_perm Γs) //=. rewrite pure_True // left_id.
Robbert Krebbers's avatar
Robbert Krebbers committed
207 208 209
    cancel [P]. apply wand_intro_l. solve_sep_entails.
Qed.

210 211 212
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
213

214 215
Lemma envs_lookup_delete_list_sound Δ Δ' rp js p Ps :
  envs_lookup_delete_list rp js Δ = Some (p,Ps,Δ') 
216
  of_envs Δ  ?p [] Ps  of_envs Δ'.
217 218
Proof.
  revert Δ Δ' p Ps. induction js as [|j js IH]=> Δ Δ'' p Ps ?; simplify_eq/=.
219
  { by rewrite intuitionistically_emp left_id. }
220
  destruct (envs_lookup_delete rp j Δ) as [[[q1 P] Δ']|] eqn:Hj; simplify_eq/=.
221
  apply envs_lookup_delete_Some in Hj as [Hj ->].
222
  destruct (envs_lookup_delete_list _ js _) as [[[q2 Ps'] ?]|] eqn:?; simplify_eq/=.
223
  rewrite -intuitionistically_if_sep_2 -assoc.
224
  rewrite envs_lookup_sound' //; rewrite IH //.
225
  repeat apply sep_mono=>//; apply intuitionistically_if_flag_mono; by destruct q1.
226 227
Qed.

Joseph Tassarotti's avatar
Joseph Tassarotti committed
228 229 230 231 232 233 234 235 236 237
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.

238 239 240 241 242 243 244 245 246 247 248 249 250 251
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 :
252
  envs_lookup i Δ = None  of_envs Δ  ?p P - of_envs (envs_snoc Δ p i P).
253
Proof.
Robbert Krebbers's avatar
Robbert Krebbers committed
254
  rewrite /envs_lookup /envs_snoc /of_envs=> ?; apply pure_elim_l=> Hwf.
255 256
  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
257
  - apply and_intro; [apply pure_intro|].
258
    + destruct Hwf; constructor; simpl; eauto using Esnoc_wf.
259
      intros j; destruct (ident_beq_reflect j i); naive_solver.
260
    + by rewrite intuitionistically_and and_sep_intuitionistically assoc.
Robbert Krebbers's avatar
Robbert Krebbers committed
261
  - apply and_intro; [apply pure_intro|].
262
    + destruct Hwf; constructor; simpl; eauto using Esnoc_wf.
263
      intros j; destruct (ident_beq_reflect j i); naive_solver.
264 265 266
    + solve_sep_entails.
Qed.

267
Lemma envs_app_sound Δ Δ' p Γ :
268 269
  envs_app p Γ Δ = Some Δ' 
  of_envs Δ  (if p then  [] Γ else [] Γ) - of_envs Δ'.
Robbert Krebbers's avatar
Robbert Krebbers committed
270
Proof.
Robbert Krebbers's avatar
Robbert Krebbers committed
271
  rewrite /of_envs /envs_app=> ?; apply pure_elim_l=> Hwf.
Robbert Krebbers's avatar
Robbert Krebbers committed
272 273 274
  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
275
    apply wand_intro_l, and_intro; [apply pure_intro|].
Robbert Krebbers's avatar
Robbert Krebbers committed
276 277 278 279
    + 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') //.
280
      rewrite big_opL_app intuitionistically_and and_sep_intuitionistically.
281
      solve_sep_entails.
Robbert Krebbers's avatar
Robbert Krebbers committed
282 283
  - destruct (env_app Γ Γp) eqn:Happ,
      (env_app Γ Γs) as [Γs'|] eqn:?; simplify_eq/=.
Robbert Krebbers's avatar
Robbert Krebbers committed
284
    apply wand_intro_l, and_intro; [apply pure_intro|].
Robbert Krebbers's avatar
Robbert Krebbers committed
285 286 287
    + 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.
288
    + rewrite (env_app_perm _ _ Γs') // big_opL_app. solve_sep_entails.
Robbert Krebbers's avatar
Robbert Krebbers committed
289 290
Qed.

291
Lemma envs_app_singleton_sound Δ Δ' p j Q :
292
  envs_app p (Esnoc Enil j Q) Δ = Some Δ'  of_envs Δ  ?p Q - of_envs Δ'.
293 294
Proof. move=> /envs_app_sound. destruct p; by rewrite /= right_id. Qed.

Robbert Krebbers's avatar
Robbert Krebbers committed
295 296
Lemma envs_simple_replace_sound' Δ Δ' i p Γ :
  envs_simple_replace i p Γ Δ = Some Δ' 
297
  of_envs (envs_delete true i p Δ)  (if p then  [] Γ else [] Γ) - of_envs Δ'.
Robbert Krebbers's avatar
Robbert Krebbers committed
298 299
Proof.
  rewrite /envs_simple_replace /envs_delete /of_envs=> ?.
Robbert Krebbers's avatar
Robbert Krebbers committed
300
  apply pure_elim_l=> Hwf. destruct Δ as [Γp Γs], p; simplify_eq/=.
Robbert Krebbers's avatar
Robbert Krebbers committed
301 302
  - destruct (env_app Γ Γs) eqn:Happ,
      (env_replace i Γ Γp) as [Γp'|] eqn:?; simplify_eq/=.
Robbert Krebbers's avatar
Robbert Krebbers committed
303
    apply wand_intro_l, and_intro; [apply pure_intro|].
Robbert Krebbers's avatar
Robbert Krebbers committed
304 305 306 307
    + 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') //.
308
      rewrite big_opL_app intuitionistically_and and_sep_intuitionistically.
309
      solve_sep_entails.
Robbert Krebbers's avatar
Robbert Krebbers committed
310 311
  - destruct (env_app Γ Γp) eqn:Happ,
      (env_replace i Γ Γs) as [Γs'|] eqn:?; simplify_eq/=.
Robbert Krebbers's avatar
Robbert Krebbers committed
312
    apply wand_intro_l, and_intro; [apply pure_intro|].
Robbert Krebbers's avatar
Robbert Krebbers committed
313 314 315
    + 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.
316
    + rewrite (env_replace_perm _ _ Γs') // big_opL_app. solve_sep_entails.
Robbert Krebbers's avatar
Robbert Krebbers committed
317 318
Qed.

319 320
Lemma envs_simple_replace_singleton_sound' Δ Δ' i p j Q :
  envs_simple_replace i p (Esnoc Enil j Q) Δ = Some Δ' 
321
  of_envs (envs_delete true i p Δ)  ?p Q - of_envs Δ'.
322 323
Proof. move=> /envs_simple_replace_sound'. destruct p; by rewrite /= right_id. Qed.

Robbert Krebbers's avatar
Robbert Krebbers committed
324 325
Lemma envs_simple_replace_sound Δ Δ' i p P Γ :
  envs_lookup i Δ = Some (p,P)  envs_simple_replace i p Γ Δ = Some Δ' 
326
  of_envs Δ  ?p P  ((if p then  [] Γ else [] Γ) - of_envs Δ').
Robbert Krebbers's avatar
Robbert Krebbers committed
327 328
Proof. intros. by rewrite envs_lookup_sound// envs_simple_replace_sound'//. Qed.

329 330 331
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 Δ' 
332
  of_envs Δ  ?p P  (?p Q - of_envs Δ').
333 334 335 336
Proof.
  intros. by rewrite envs_lookup_sound// envs_simple_replace_singleton_sound'//.
Qed.

Robbert Krebbers's avatar
Robbert Krebbers committed
337
Lemma envs_replace_sound' Δ Δ' i p q Γ :
338
  envs_replace i p q Γ Δ = Some Δ' 
339
  of_envs (envs_delete true i p Δ)  (if q then  [] Γ else [] Γ) - of_envs Δ'.
Robbert Krebbers's avatar
Robbert Krebbers committed
340
Proof.
341
  rewrite /envs_replace; destruct (Bool.eqb _ _) eqn:Hpq.
Robbert Krebbers's avatar
Robbert Krebbers committed
342 343 344 345
  - apply eqb_prop in Hpq as ->. apply envs_simple_replace_sound'.
  - apply envs_app_sound.
Qed.

346 347
Lemma envs_replace_singleton_sound' Δ Δ' i p q j Q :
  envs_replace i p q (Esnoc Enil j Q) Δ = Some Δ' 
348
  of_envs (envs_delete true i p Δ)  ?q Q - of_envs Δ'.
349 350
Proof. move=> /envs_replace_sound'. destruct q; by rewrite /= ?right_id. Qed.

Robbert Krebbers's avatar
Robbert Krebbers committed
351 352
Lemma envs_replace_sound Δ Δ' i p q P Γ :
  envs_lookup i Δ = Some (p,P)  envs_replace i p q Γ Δ = Some Δ' 
353
  of_envs Δ  ?p P  ((if q then  [] Γ else [] Γ) - of_envs Δ').
Robbert Krebbers's avatar
Robbert Krebbers committed
354 355
Proof. intros. by rewrite envs_lookup_sound// envs_replace_sound'//. Qed.

356 357 358
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 Δ' 
359
  of_envs Δ  ?p P  (?q Q - of_envs Δ').
360 361
Proof. intros. by rewrite envs_lookup_sound// envs_replace_singleton_sound'//. Qed.

362 363
Lemma envs_lookup_envs_clear_spatial Δ j :
  envs_lookup j (envs_clear_spatial Δ)
Robbert Krebbers's avatar
Robbert Krebbers committed
364
  = ''(p,P)  envs_lookup j Δ; if p : bool then Some (p,P) else None.
Robbert Krebbers's avatar
Robbert Krebbers committed
365
Proof.
366 367 368
  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
369 370
Qed.

Robbert Krebbers's avatar
Robbert Krebbers committed
371
Lemma envs_clear_spatial_sound Δ :
372
  of_envs Δ  of_envs (envs_clear_spatial Δ)  [] env_spatial Δ.
373
Proof.
Robbert Krebbers's avatar
Robbert Krebbers committed
374 375 376
  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.
377 378
Qed.

379
Lemma env_spatial_is_nil_intuitionistically Δ :
380
  env_spatial_is_nil Δ = true  of_envs Δ   of_envs Δ.
Robbert Krebbers's avatar
Robbert Krebbers committed
381 382
Proof.
  intros. unfold of_envs; destruct Δ as [? []]; simplify_eq/=.
383
  rewrite !right_id /bi_intuitionistically {1}affinely_and_r persistently_and.
Ralf Jung's avatar
Ralf Jung committed
384
  by rewrite persistently_affinely_elim persistently_idemp persistently_pure.
Robbert Krebbers's avatar
Robbert Krebbers committed
385
Qed.
Robbert Krebbers's avatar
Robbert Krebbers committed
386

387 388
Lemma envs_lookup_envs_delete Δ i p P :
  envs_wf Δ 
389
  envs_lookup i Δ = Some (p,P)  envs_lookup i (envs_delete true i p Δ) = None.
390 391 392 393 394 395 396
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.
397 398
Lemma envs_lookup_envs_delete_ne Δ rp i j p :
  i  j  envs_lookup i (envs_delete rp j p Δ) = envs_lookup i Δ.
399 400
Proof.
  rewrite /envs_lookup /envs_delete=> ?. destruct Δ as [Γp Γs],p; simplify_eq/=.
401
  - destruct rp=> //. by rewrite env_lookup_env_delete_ne.
402 403 404 405 406
  - 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
407
  envs_split_go js Δ1 Δ2 = Some (Δ1',Δ2') 
408
  of_envs Δ1  of_envs Δ2  of_envs Δ1'  of_envs Δ2'.
409
Proof.
410 411
  revert Δ1 Δ2.
  induction js as [|j js IH]=> Δ1 Δ2 Hlookup HΔ; simplify_eq/=; [done|].
Robbert Krebbers's avatar
Robbert Krebbers committed
412 413
  apply pure_elim with (envs_wf Δ1)=> [|Hwf].
  { by rewrite /of_envs !and_elim_l sep_elim_l. }
414 415
  destruct (envs_lookup_delete _ j Δ1)
    as [[[[] P] Δ1'']|] eqn:Hdel; simplify_eq/=; auto.
416 417
  apply envs_lookup_delete_Some in Hdel as [??]; subst.
  rewrite envs_lookup_sound //; rewrite /= (comm _ P) -assoc.
418 419 420 421
  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. }
422 423
  rewrite (envs_snoc_sound Δ2 false j P) /= ?wand_elim_r; eauto.
Qed.
424
Lemma envs_split_sound Δ d js Δ1 Δ2 :
425
  envs_split d js Δ = Some (Δ1,Δ2)  of_envs Δ  of_envs Δ1  of_envs Δ2.
426
Proof.
427
  rewrite /envs_split=> ?. rewrite -(idemp bi_and (of_envs Δ)).
Robbert Krebbers's avatar
Robbert Krebbers committed
428
  rewrite {2}envs_clear_spatial_sound.
429 430
  rewrite (env_spatial_is_nil_intuitionistically (envs_clear_spatial _)) //.
  rewrite -persistently_and_intuitionistically_sep_l.
431
  rewrite (and_elim_l (<pers> _)%I)
432
          persistently_and_intuitionistically_sep_r intuitionistically_elim.
433 434 435
  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=> ->. }
436
  destruct d; simplify_eq/=; solve_sep_entails.
437 438
Qed.

Jacques-Henri Jourdan's avatar
Jacques-Henri Jourdan committed
439 440 441 442 443 444 445 446
Lemma prop_of_env_sound Δ : of_envs Δ  prop_of_env (env_spatial Δ).
Proof.
  destruct Δ as [? Γ]. rewrite /of_envs /= and_elim_r sep_elim_r.
  destruct Γ as [|Γ ? P0]=>//=. revert P0.
  induction Γ as [|Γ IH ? P]=>P0; [rewrite /= right_id //|].
  rewrite /= assoc (comm _ P0 P) IH //.
Qed.

Ralf Jung's avatar
Ralf Jung committed
447 448 449 450
Lemma maybe_wand_sound (P : option PROP) Q :
  maybe_wand P Q  (default emp P id - Q).
Proof. destruct P; simpl; first done. rewrite emp_wand //. Qed.

Robbert Krebbers's avatar
Robbert Krebbers committed
451
Global Instance envs_Forall2_refl (R : relation PROP) :
452 453
  Reflexive R  Reflexive (envs_Forall2 R).
Proof. by constructor. Qed.
Robbert Krebbers's avatar
Robbert Krebbers committed
454
Global Instance envs_Forall2_sym (R : relation PROP) :
455 456
  Symmetric R  Symmetric (envs_Forall2 R).
Proof. intros ??? [??]; by constructor. Qed.
Robbert Krebbers's avatar
Robbert Krebbers committed
457
Global Instance envs_Forall2_trans (R : relation PROP) :
458 459
  Transitive R  Transitive (envs_Forall2 R).
Proof. intros ??? [??] [??] [??]; constructor; etrans; eauto. Qed.
Robbert Krebbers's avatar
Robbert Krebbers committed
460
Global Instance envs_Forall2_antisymm (R R' : relation PROP) :
461 462
  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
463
Lemma envs_Forall2_impl (R R' : relation PROP) Δ1 Δ2 :
464 465 466
  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
467
Global Instance of_envs_mono : Proper (envs_Forall2 () ==> ()) (@of_envs PROP).
468
Proof.
Robbert Krebbers's avatar
Robbert Krebbers committed
469 470
  intros [Γp1 Γs1] [Γp2 Γs2] [Hp Hs]; apply and_mono; simpl in *.
  - apply pure_mono=> -[???]. constructor;
471 472 473
      naive_solver eauto using env_Forall2_wf, env_Forall2_fresh.
  - by repeat f_equiv.
Qed.
Robbert Krebbers's avatar
Robbert Krebbers committed
474 475
Global Instance of_envs_proper :
  Proper (envs_Forall2 () ==> ()) (@of_envs PROP).
476
Proof.
477 478
  intros Δ1 Δ2 HΔ; apply (anti_symm ()); apply of_envs_mono;
    eapply (envs_Forall2_impl ()); [| |symmetry|]; eauto using equiv_entails.
479
Qed.
480
Global Instance Envs_proper (R : relation PROP) :
481
  Proper (env_Forall2 R ==> env_Forall2 R ==> eq ==> envs_Forall2 R) (@Envs PROP).
482 483
Proof. by constructor. Qed.

484
Global Instance envs_entails_proper :
485
  Proper (envs_Forall2 () ==> () ==> iff) (@envs_entails PROP).
486
Proof. rewrite envs_entails_eq. solve_proper. Qed.
487
Global Instance envs_entails_flip_mono :
488
  Proper (envs_Forall2 () ==> flip () ==> flip impl) (@envs_entails PROP).
489
Proof. rewrite envs_entails_eq=> Δ1 Δ2 ? P1 P2 <- <-. by f_equiv. Qed.
490

Robbert Krebbers's avatar
Robbert Krebbers committed
491
(** * Adequacy *)
492
Lemma tac_adequate P : envs_entails (Envs Enil Enil 1) P  P.
Robbert Krebbers's avatar
Robbert Krebbers committed
493
Proof.
494 495
  rewrite envs_entails_eq /of_envs /= intuitionistically_True_emp
          left_id=><-.
Robbert Krebbers's avatar
Robbert Krebbers committed
496
  apply and_intro=> //. apply pure_intro; repeat constructor.
Robbert Krebbers's avatar
Robbert Krebbers committed
497 498 499
Qed.

(** * Basic rules *)
500
Lemma tac_eval Δ Q Q' :
501 502 503
  ( (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]. *)
504
  envs_entails Δ Q'  envs_entails Δ Q.
505
Proof. by intros <-. Qed.
506

507 508
Lemma tac_eval_in Δ Δ' i p P P' Q :
  envs_lookup i Δ = Some (p, P) 
509
  ( (P'':=P'), P  P') 
510 511 512
  envs_simple_replace i p (Esnoc Enil i P') Δ  = Some Δ' 
  envs_entails Δ' Q  envs_entails Δ Q.
Proof.
513 514 515
  rewrite envs_entails_eq /=. intros ? HP ? <-.
  rewrite envs_simple_replace_singleton_sound //; simpl.
  by rewrite HP wand_elim_r.
516
Qed.
517

518 519 520 521 522 523 524
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.

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

529
Instance affine_env_spatial Δ :
530 531 532
  AffineEnv (env_spatial Δ)  Affine ([] env_spatial Δ).
Proof. intros H. induction H; simpl; apply _. Qed.

533 534
Lemma tac_emp_intro Δ : AffineEnv (env_spatial Δ)  envs_entails Δ emp.
Proof. intros. by rewrite envs_entails_eq (affine (of_envs Δ)). Qed.
535

Robbert Krebbers's avatar
Robbert Krebbers committed
536
Lemma tac_assumption Δ Δ' i p P Q :
537
  envs_lookup_delete true i Δ = Some (p,P,Δ') 
Robbert Krebbers's avatar
Robbert Krebbers committed
538
  FromAssumption p P Q 
539 540
  (if env_spatial_is_nil Δ' then TCTrue
   else TCOr (Absorbing Q) (AffineEnv (env_spatial Δ'))) 
541
  envs_entails Δ Q.
Robbert Krebbers's avatar
Robbert Krebbers committed
542
Proof.
543
  intros ?? H. rewrite envs_entails_eq envs_lookup_delete_sound //.
Robbert Krebbers's avatar
Robbert Krebbers committed
544
  destruct (env_spatial_is_nil Δ') eqn:?.
545
  - by rewrite (env_spatial_is_nil_intuitionistically Δ') // sep_elim_l.
Robbert Krebbers's avatar
Robbert Krebbers committed
546
  - rewrite from_assumption. destruct H; by rewrite sep_elim_l.
Robbert Krebbers's avatar
Robbert Krebbers committed
547
Qed.
Robbert Krebbers's avatar
Robbert Krebbers committed
548 549 550 551

Lemma tac_rename Δ Δ' i j p P Q :
  envs_lookup i Δ = Some (p,P) 
  envs_simple_replace i p (Esnoc Enil j P) Δ = Some Δ' 
552 553
  envs_entails Δ' Q 
  envs_entails Δ Q.
Robbert Krebbers's avatar
Robbert Krebbers committed
554
Proof.
555
  rewrite envs_entails_eq=> ?? <-. rewrite envs_simple_replace_singleton_sound //.
556
  by rewrite wand_elim_r.
Robbert Krebbers's avatar
Robbert Krebbers committed
557
Qed.
Robbert Krebbers's avatar
Robbert Krebbers committed
558

Robbert Krebbers's avatar
Robbert Krebbers committed
559
Lemma tac_clear Δ Δ' i p P Q :
560
  envs_lookup_delete true i Δ = Some (p,P,Δ') 
Robbert Krebbers's avatar
Robbert Krebbers committed
561
  (if p then TCTrue else TCOr (Affine P) (Absorbing Q)) 
562
  envs_entails Δ' Q 
563
  envs_entails Δ Q.
Robbert Krebbers's avatar
Robbert Krebbers committed
564
Proof.
565
  rewrite envs_entails_eq=> ?? HQ. rewrite envs_lookup_delete_sound //.
566
  by destruct p; rewrite /= HQ sep_elim_r.
Robbert Krebbers's avatar
Robbert Krebbers committed
567
Qed.
Robbert Krebbers's avatar
Robbert Krebbers committed
568 569

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

Robbert Krebbers's avatar
Robbert Krebbers committed
573 574 575
Lemma tac_false_destruct Δ i p P Q :
  envs_lookup i Δ = Some (p,P) 
  P = False%I 
576
  envs_entails Δ Q.
Robbert Krebbers's avatar
Robbert Krebbers committed
577
Proof.
578
  rewrite envs_entails_eq => ??. subst. rewrite envs_lookup_sound //; simpl.
579
  by rewrite intuitionistically_if_elim sep_elim_l False_elim.
Robbert Krebbers's avatar
Robbert Krebbers committed
580 581
Qed.

Robbert Krebbers's avatar
Robbert Krebbers committed
582
(** * Pure *)
Jacques-Henri Jourdan's avatar
Jacques-Henri Jourdan committed
583 584
(* This relies on the invariant that [FromPure false] implies
   [FromPure true] *)
585 586 587 588
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.
589 590
  - rewrite env_spatial_is_nil_intuitionistically //= /bi_intuitionistically.
    f_equiv. by apply pure_intro.
591 592
  - by apply pure_intro.
Qed.
593

Robbert Krebbers's avatar
Robbert Krebbers committed
594
Lemma tac_pure Δ Δ' i p P φ Q :
595
  envs_lookup_delete true i Δ = Some (p, P, Δ') 
Robbert Krebbers's avatar
Robbert Krebbers committed
596
  IntoPure P φ 
597
  (if p then TCTrue else TCOr (Affine P) (Absorbing Q)) 
598
  (φ  envs_entails Δ' Q)  envs_entails Δ Q.
Robbert Krebbers's avatar
Robbert Krebbers committed
599
Proof.
600
  rewrite envs_entails_eq=> ?? HPQ HQ.
601
  rewrite envs_lookup_delete_sound //; simpl. destruct p; simpl.
602
  - rewrite (into_pure P) -persistently_and_intuitionistically_sep_l persistently_pure.
Robbert Krebbers's avatar
Robbert Krebbers committed
603
    by apply pure_elim_l.
604
  - destruct HPQ.
605
    + rewrite -(affine_affinely P) (into_pure P) -persistent_and_affinely_sep_l.
606
      by apply pure_elim_l.
607
    + rewrite (into_pure P) -(persistent_absorbingly_affinely  _ %I) absorbingly_sep_lr.
608
      rewrite -persistent_and_affinely_sep_l. apply pure_elim_l=> ?. by rewrite HQ.
Robbert Krebbers's avatar
Robbert Krebbers committed
609 610
Qed.

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

614
(** * Persistence *)
Robbert Krebbers's avatar
Robbert Krebbers committed
615
Lemma tac_persistent Δ Δ' i p P P' Q :
616
  envs_lookup i Δ = Some (p, P) 
617
  IntoPersistent p P P' 
618
  (if p then TCTrue else TCOr (Affine P) (Absorbing Q)) 
Robbert Krebbers's avatar
Robbert Krebbers committed
619
  envs_replace i p true (Esnoc Enil i P') Δ = Some Δ' 
620
  envs_entails Δ' Q  envs_entails Δ Q.
Robbert Krebbers's avatar
Robbert Krebbers committed
621
Proof.
622
  rewrite envs_entails_eq=>?? HPQ ? HQ. rewrite envs_replace_singleton_sound //=.
Ralf Jung's avatar