coq_tactics.v 59 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_persistent : env PROP; env_spatial : env PROP; env_counter : positive }.
Robbert Krebbers's avatar
Robbert Krebbers committed
12
Add Printing Constructor envs.
13
Arguments Envs {_} _ _ _.
Robbert Krebbers's avatar
Robbert Krebbers committed
14 15
Arguments env_persistent {_} _.
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) := {
Robbert Krebbers's avatar
Robbert Krebbers committed
19 20 21 22 23
  env_persistent_valid : env_wf (env_persistent Δ);
  env_spatial_valid : env_wf (env_spatial Δ);
  envs_disjoint i : env_persistent Δ !! i = None  env_spatial Δ !! i = None
}.

24
Definition of_envs {PROP} (Δ : envs PROP) : PROP :=
25
  (envs_wf Δ⌝   [] env_persistent Δ  [] 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 40 41
  env_persistent_Forall2 : env_Forall2 R (env_persistent Δ1) (env_persistent Δ2);
  env_spatial_Forall2 : env_Forall2 R (env_spatial Δ1) (env_spatial Δ2)
}.
Robbert Krebbers's avatar
Robbert Krebbers committed
42

43
Definition envs_dom {PROP} (Δ : envs PROP) : list ident :=
44
  env_dom (env_persistent Δ) ++ 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) :=
Robbert Krebbers's avatar
Robbert Krebbers committed
102
  if 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_persistent Δ) Enil (env_counter Δ).
110

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

Definition envs_incr_counter {PROP} (Δ : envs PROP) : envs PROP :=
  Envs (env_persistent Δ) (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.

Robbert Krebbers's avatar
Robbert Krebbers committed
139
(* Coq versions of the tactics *)
Robbert Krebbers's avatar
Robbert Krebbers committed
140 141 142 143 144 145 146
Section bi_tactics.
Context {PROP : bi}.
Implicit Types Γ : env PROP.
Implicit Types Δ : envs PROP.
Implicit Types P Q : PROP.

Lemma of_envs_eq Δ :
147
  of_envs Δ = (envs_wf Δ⌝   [] env_persistent Δ  [] env_spatial Δ)%I.
148 149
Proof. done. Qed.

150 151 152 153 154 155 156 157 158
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
159 160 161 162 163 164
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.

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

Lemma envs_lookup_split Δ i p P :
192
  envs_lookup i Δ = Some (p,P)  of_envs Δ  ?p P  (?p P - of_envs Δ).
Robbert Krebbers's avatar
Robbert Krebbers committed
193
Proof.
Robbert Krebbers's avatar
Robbert Krebbers committed
194
  rewrite /envs_lookup /of_envs=>?. apply pure_elim_l=> Hwf.
Robbert Krebbers's avatar
Robbert Krebbers committed
195
  destruct Δ as [Γp Γs], (Γp !! i) eqn:?; simplify_eq/=.
196
  - rewrite pure_True // left_id (env_lookup_perm Γp) //=
197
      affinely_persistently_and and_sep_affinely_persistently.
198
    cancel [ P]%I. apply wand_intro_l. solve_sep_entails.
Robbert Krebbers's avatar
Robbert Krebbers committed
199
  - destruct (Γs !! i) eqn:?; simplify_eq/=.
200
    rewrite (env_lookup_perm Γs) //=. rewrite pure_True // left_id.
Robbert Krebbers's avatar
Robbert Krebbers committed
201 202 203
    cancel [P]. apply wand_intro_l. solve_sep_entails.
Qed.

204 205 206
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
207

208 209
Lemma envs_lookup_delete_list_sound Δ Δ' rp js p Ps :
  envs_lookup_delete_list rp js Δ = Some (p,Ps,Δ') 
210
  of_envs Δ  ?p [] Ps  of_envs Δ'.
211 212
Proof.
  revert Δ Δ' p Ps. induction js as [|j js IH]=> Δ Δ'' p Ps ?; simplify_eq/=.
213
  { by rewrite affinely_persistently_emp left_id. }
214
  destruct (envs_lookup_delete rp j Δ) as [[[q1 P] Δ']|] eqn:Hj; simplify_eq/=.
215
  apply envs_lookup_delete_Some in Hj as [Hj ->].
216 217 218 219
  destruct (envs_lookup_delete_list _ js _) as [[[q2 Ps'] ?]|] eqn:?; simplify_eq/=.
  rewrite -affinely_persistently_if_sep_2 -assoc.
  rewrite envs_lookup_sound' //; rewrite IH //.
  repeat apply sep_mono=>//; apply affinely_persistently_if_flag_mono; by destruct q1.
220 221
Qed.

Joseph Tassarotti's avatar
Joseph Tassarotti committed
222 223 224 225 226 227 228 229 230 231
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.

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

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

285
Lemma envs_app_singleton_sound Δ Δ' p j Q :
286
  envs_app p (Esnoc Enil j Q) Δ = Some Δ'  of_envs Δ  ?p Q - of_envs Δ'.
287 288
Proof. move=> /envs_app_sound. destruct p; by rewrite /= right_id. Qed.

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

313 314
Lemma envs_simple_replace_singleton_sound' Δ Δ' i p j Q :
  envs_simple_replace i p (Esnoc Enil j Q) Δ = Some Δ' 
315
  of_envs (envs_delete true i p Δ)  ?p Q - of_envs Δ'.
316 317
Proof. move=> /envs_simple_replace_sound'. destruct p; by rewrite /= right_id. Qed.

Robbert Krebbers's avatar
Robbert Krebbers committed
318 319
Lemma envs_simple_replace_sound Δ Δ' i p P Γ :
  envs_lookup i Δ = Some (p,P)  envs_simple_replace i p Γ Δ = Some Δ' 
320
  of_envs Δ  ?p P  ((if p then  [] Γ else [] Γ) - of_envs Δ').
Robbert Krebbers's avatar
Robbert Krebbers committed
321 322
Proof. intros. by rewrite envs_lookup_sound// envs_simple_replace_sound'//. Qed.

323 324 325
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 Δ' 
326
  of_envs Δ  ?p P  (?p Q - of_envs Δ').
327 328 329 330
Proof.
  intros. by rewrite envs_lookup_sound// envs_simple_replace_singleton_sound'//.
Qed.

Robbert Krebbers's avatar
Robbert Krebbers committed
331
Lemma envs_replace_sound' Δ Δ' i p q Γ :
332
  envs_replace i p q Γ Δ = Some Δ' 
333
  of_envs (envs_delete true i p Δ)  (if q then  [] Γ else [] Γ) - of_envs Δ'.
Robbert Krebbers's avatar
Robbert Krebbers committed
334 335 336 337 338 339
Proof.
  rewrite /envs_replace; destruct (eqb _ _) eqn:Hpq.
  - apply eqb_prop in Hpq as ->. apply envs_simple_replace_sound'.
  - apply envs_app_sound.
Qed.

340 341
Lemma envs_replace_singleton_sound' Δ Δ' i p q j Q :
  envs_replace i p q (Esnoc Enil j Q) Δ = Some Δ' 
342
  of_envs (envs_delete true i p Δ)  ?q Q - of_envs Δ'.
343 344
Proof. move=> /envs_replace_sound'. destruct q; by rewrite /= ?right_id. Qed.

Robbert Krebbers's avatar
Robbert Krebbers committed
345 346
Lemma envs_replace_sound Δ Δ' i p q P Γ :
  envs_lookup i Δ = Some (p,P)  envs_replace i p q Γ Δ = Some Δ' 
347
  of_envs Δ  ?p P  ((if q then  [] Γ else [] Γ) - of_envs Δ').
Robbert Krebbers's avatar
Robbert Krebbers committed
348 349
Proof. intros. by rewrite envs_lookup_sound// envs_replace_sound'//. Qed.

350 351 352
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 Δ' 
353
  of_envs Δ  ?p P  (?q Q - of_envs Δ').
354 355
Proof. intros. by rewrite envs_lookup_sound// envs_replace_singleton_sound'//. Qed.

356 357
Lemma envs_lookup_envs_clear_spatial Δ j :
  envs_lookup j (envs_clear_spatial Δ)
Robbert Krebbers's avatar
Robbert Krebbers committed
358
  = ''(p,P)  envs_lookup j Δ; if p : bool then Some (p,P) else None.
Robbert Krebbers's avatar
Robbert Krebbers committed
359
Proof.
360 361 362
  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
363 364
Qed.

Robbert Krebbers's avatar
Robbert Krebbers committed
365
Lemma envs_clear_spatial_sound Δ :
366
  of_envs Δ  of_envs (envs_clear_spatial Δ)  [] env_spatial Δ.
367
Proof.
Robbert Krebbers's avatar
Robbert Krebbers committed
368 369 370
  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.
371 372
Qed.

373
Lemma env_spatial_is_nil_affinely_persistently Δ :
374
  env_spatial_is_nil Δ = true  of_envs Δ   of_envs Δ.
Robbert Krebbers's avatar
Robbert Krebbers committed
375 376
Proof.
  intros. unfold of_envs; destruct Δ as [? []]; simplify_eq/=.
377 378
  rewrite !right_id {1}affinely_and_r persistently_and.
  by rewrite persistently_affinely persistently_idemp persistently_pure.
Robbert Krebbers's avatar
Robbert Krebbers committed
379
Qed.
Robbert Krebbers's avatar
Robbert Krebbers committed
380

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

Jacques-Henri Jourdan's avatar
Jacques-Henri Jourdan committed
433 434 435 436 437 438 439 440
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.

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

474
Global Instance envs_entails_proper :
475
  Proper (envs_Forall2 () ==> () ==> iff) (@envs_entails PROP).
476
Proof. rewrite envs_entails_eq. solve_proper. Qed.
477
Global Instance envs_entails_flip_mono :
478
  Proper (envs_Forall2 () ==> flip () ==> flip impl) (@envs_entails PROP).
479
Proof. rewrite envs_entails_eq=> Δ1 Δ2 ? P1 P2 <- <-. by f_equiv. Qed.
480

Robbert Krebbers's avatar
Robbert Krebbers committed
481
(** * Adequacy *)
482
Lemma tac_adequate P : envs_entails (Envs Enil Enil 1) P  P.
Robbert Krebbers's avatar
Robbert Krebbers committed
483
Proof.
484 485
  rewrite envs_entails_eq /of_envs /= persistently_True_emp
          affinely_persistently_emp left_id=><-.
Robbert Krebbers's avatar
Robbert Krebbers committed
486
  apply and_intro=> //. apply pure_intro; repeat constructor.
Robbert Krebbers's avatar
Robbert Krebbers committed
487 488 489
Qed.

(** * Basic rules *)
490
Lemma tac_eval Δ Q Q' :
491 492 493
  ( (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]. *)
494
  envs_entails Δ Q'  envs_entails Δ Q.
495
Proof. by intros <-. Qed.
496

497 498
Lemma tac_eval_in Δ Δ' i p P P' Q :
  envs_lookup i Δ = Some (p, P) 
499
  ( (P'':=P'), P  P') 
500 501 502
  envs_simple_replace i p (Esnoc Enil i P') Δ  = Some Δ' 
  envs_entails Δ' Q  envs_entails Δ Q.
Proof.
503 504 505
  rewrite envs_entails_eq /=. intros ? HP ? <-.
  rewrite envs_simple_replace_singleton_sound //; simpl.
  by rewrite HP wand_elim_r.
506
Qed.
507

508 509 510 511 512 513 514
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.

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

519
Instance affine_env_spatial Δ :
520 521 522
  AffineEnv (env_spatial Δ)  Affine ([] env_spatial Δ).
Proof. intros H. induction H; simpl; apply _. Qed.

523 524
Lemma tac_emp_intro Δ : AffineEnv (env_spatial Δ)  envs_entails Δ emp.
Proof. intros. by rewrite envs_entails_eq (affine (of_envs Δ)). Qed.
525

Robbert Krebbers's avatar
Robbert Krebbers committed
526
Lemma tac_assumption Δ Δ' i p P Q :
527
  envs_lookup_delete true i Δ = Some (p,P,Δ') 
Robbert Krebbers's avatar
Robbert Krebbers committed
528
  FromAssumption p P Q 
529 530
  (if env_spatial_is_nil Δ' then TCTrue
   else TCOr (Absorbing Q) (AffineEnv (env_spatial Δ'))) 
531
  envs_entails Δ Q.
Robbert Krebbers's avatar
Robbert Krebbers committed
532
Proof.
533
  intros ?? H. rewrite envs_entails_eq envs_lookup_delete_sound //.
Robbert Krebbers's avatar
Robbert Krebbers committed
534
  destruct (env_spatial_is_nil Δ') eqn:?.
535
  - by rewrite (env_spatial_is_nil_affinely_persistently Δ') // sep_elim_l.
Robbert Krebbers's avatar
Robbert Krebbers committed
536
  - rewrite from_assumption. destruct H; by rewrite sep_elim_l.
Robbert Krebbers's avatar
Robbert Krebbers committed
537
Qed.
Robbert Krebbers's avatar
Robbert Krebbers committed
538 539 540 541

Lemma tac_rename Δ Δ' i j p P Q :
  envs_lookup i Δ = Some (p,P) 
  envs_simple_replace i p (Esnoc Enil j P) Δ = Some Δ' 
542 543
  envs_entails Δ' Q 
  envs_entails Δ Q.
Robbert Krebbers's avatar
Robbert Krebbers committed
544
Proof.
545
  rewrite envs_entails_eq=> ?? <-. rewrite envs_simple_replace_singleton_sound //.
546
  by rewrite wand_elim_r.
Robbert Krebbers's avatar
Robbert Krebbers committed
547
Qed.
Robbert Krebbers's avatar
Robbert Krebbers committed
548

Robbert Krebbers's avatar
Robbert Krebbers committed
549
Lemma tac_clear Δ Δ' i p P Q :
550
  envs_lookup_delete true i Δ = Some (p,P,Δ') 
Robbert Krebbers's avatar
Robbert Krebbers committed
551
  (if p then TCTrue else TCOr (Affine P) (Absorbing Q)) 
552
  envs_entails Δ' Q 
553
  envs_entails Δ Q.
Robbert Krebbers's avatar
Robbert Krebbers committed
554
Proof.
555
  rewrite envs_entails_eq=> ?? HQ. rewrite envs_lookup_delete_sound //.
556
  by destruct p; rewrite /= HQ sep_elim_r.
Robbert Krebbers's avatar
Robbert Krebbers committed
557
Qed.
Robbert Krebbers's avatar
Robbert Krebbers committed
558 559

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

Robbert Krebbers's avatar
Robbert Krebbers committed
563 564 565
Lemma tac_false_destruct Δ i p P Q :
  envs_lookup i Δ = Some (p,P) 
  P = False%I 
566
  envs_entails Δ Q.
Robbert Krebbers's avatar
Robbert Krebbers committed
567
Proof.
568
  rewrite envs_entails_eq => ??. subst. rewrite envs_lookup_sound //; simpl.
569
  by rewrite affinely_persistently_if_elim sep_elim_l False_elim.
Robbert Krebbers's avatar
Robbert Krebbers committed
570 571
Qed.

Robbert Krebbers's avatar
Robbert Krebbers committed
572
(** * Pure *)
Jacques-Henri Jourdan's avatar
Jacques-Henri Jourdan committed
573 574
(* This relies on the invariant that [FromPure false] implies
   [FromPure true] *)
575 576 577 578 579 580 581 582
Lemma tac_pure_intro Δ Q φ af :
  env_spatial_is_nil Δ = af  FromPure af Q φ  φ  envs_entails Δ Q.
Proof.
  intros ???. rewrite envs_entails_eq -(from_pure _ Q). destruct af.
  - rewrite env_spatial_is_nil_affinely_persistently //=. f_equiv.
    by apply pure_intro.
  - by apply pure_intro.
Qed.
583

Robbert Krebbers's avatar
Robbert Krebbers committed
584
Lemma tac_pure Δ Δ' i p P φ Q :
585
  envs_lookup_delete true i Δ = Some (p, P, Δ') 
Robbert Krebbers's avatar
Robbert Krebbers committed
586
  IntoPure P φ 
587
  (if p then TCTrue else TCOr (Affine P) (Absorbing Q)) 
588
  (φ  envs_entails Δ' Q)  envs_entails Δ Q.
Robbert Krebbers's avatar
Robbert Krebbers committed
589
Proof.
590
  rewrite envs_entails_eq=> ?? HPQ HQ.
591
  rewrite envs_lookup_delete_sound //; simpl. destruct p; simpl.
592
  - rewrite (into_pure P) -persistently_and_affinely_sep_l persistently_pure.
Robbert Krebbers's avatar
Robbert Krebbers committed
593
    by apply pure_elim_l.
594
  - destruct HPQ.
595
    + rewrite -(affine_affinely P) (into_pure P) -persistent_and_affinely_sep_l.
596
      by apply pure_elim_l.
597
    + rewrite (into_pure P) -(persistent_absorbingly_affinely  _ %I) absorbingly_sep_lr.
598
      rewrite -persistent_and_affinely_sep_l. apply pure_elim_l=> ?. by rewrite HQ.
Robbert Krebbers's avatar
Robbert Krebbers committed
599 600
Qed.

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

604
(** * Persistence *)
Robbert Krebbers's avatar
Robbert Krebbers committed
605
Lemma tac_persistent Δ Δ' i p P P' Q :
606
  envs_lookup i Δ = Some (p, P) 
607
  IntoPersistent p P P' 
608
  (if p then TCTrue else TCOr (Affine P) (Absorbing Q)) 
Robbert Krebbers's avatar
Robbert Krebbers committed
609
  envs_replace i p true (Esnoc Enil i P') Δ = Some Δ' 
610
  envs_entails Δ' Q  envs_entails Δ Q.
Robbert Krebbers's avatar
Robbert Krebbers committed
611
Proof.
612
  rewrite envs_entails_eq=>?? HPQ ? HQ. rewrite envs_replace_singleton_sound //=.
613 614 615
  destruct p; simpl.
  - by rewrite -(into_persistent _ P) /= wand_elim_r.
  - destruct HPQ.
616
    + rewrite -(affine_affinely P) (_ : P = <pers>?false P)%I //
617
              (into_persistent _ P) wand_elim_r //.
618
    + rewrite (_ : P = <pers>?false P)%I // (into_persistent _ P).
619 620
      by rewrite -{1}absorbingly_affinely_persistently
        absorbingly_sep_l wand_elim_r HQ.
Robbert Krebbers's avatar
Robbert Krebbers committed
621 622 623
Qed.

(** * Implication and wand *)
624 625
Lemma tac_impl_intro Δ Δ' i P P' Q R :
  FromImpl R P Q 
626
  (if env_spatial_is_nil Δ then TCTrue else Persistent P) 
627
  envs_app false (Esnoc Enil i P') Δ = Some Δ' 
628
  FromAffinely P' P 
629
  envs_entails Δ' Q  envs_entails Δ R.
Robbert Krebbers's avatar
Robbert Krebbers committed
630
Proof.