rel_tactics.v 29.2 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
From iris.program_logic Require Export weakestpre.
From iris.proofmode Require Import coq_tactics sel_patterns.
From iris.proofmode Require Export tactics.
From iris_logrel.F_mu_ref_conc Require Import rules rules_binary.
From iris_logrel.F_mu_ref_conc Require Export lang tactics logrel_binary relational_properties.
Set Default Proof Using "Type".
Import lang.


Lemma tac_rel_bind_gen `{heapIG Σ, !cfgSG Σ} Δ E1 E2 Γ e e' t t' τ :
  e = e' 
  t = t' 
  (Δ  bin_log_related E1 E2 Γ e' t' τ) 
  (Δ  bin_log_related E1 E2 Γ e t τ).
Proof.
  intros. subst t e. assumption.
Qed.

Lemma tac_rel_bind_l `{heapIG Σ, !cfgSG Σ} e' K Δ E1 E2 Γ e t τ :
  e = fill K e' 
  (Δ  bin_log_related E1 E2 Γ (fill K e') t τ) 
  (Δ  bin_log_related E1 E2 Γ e t τ).
Proof. intros. eapply tac_rel_bind_gen; eauto. Qed.

Lemma tac_rel_bind_r `{heapIG Σ, !cfgSG Σ} t' K Δ E1 E2 Γ e t τ :
  t = fill K t' 
  (Δ  bin_log_related E1 E2 Γ e (fill K t') τ) 
  (Δ  bin_log_related E1 E2 Γ e t τ).
Proof. intros. eapply tac_rel_bind_gen; eauto. Qed.

31
Ltac tac_bind_helper :=
32 33 34 35 36 37 38 39 40 41 42 43
  lazymatch goal with   
  | |- fill ?K ?e = fill _ ?efoc =>
     reshape_expr e ltac:(fun K' e' =>
       unify e' efoc;
       let K'' := eval cbn[app] in (K' ++ K) in
       replace (fill K e) with (fill K'' e') by (by rewrite ?fill_app))
  | |- ?e = fill _ ?efoc =>
     reshape_expr e ltac:(fun K' e' =>
       unify e' efoc;
       replace e with (fill K' e') by (by rewrite ?fill_app))
  end; reflexivity.

44 45 46 47 48 49 50 51 52
Ltac rel_reshape_cont_r tac :=
  lazymatch goal with
  | |- _  bin_log_related _ _ _ _ (fill ?K ?e) _ =>
    reshape_expr e ltac:(fun K' e' =>
      tac (K' ++ K) e')
  | |- _  bin_log_related _ _ _ _ ?e _ =>
    reshape_expr e ltac:(fun K' e' => tac K' e')
  end.

53 54 55 56 57 58 59 60 61
Ltac rel_reshape_cont_l tac :=
  lazymatch goal with
  | |- _  bin_log_related _ _ _ (fill ?K ?e) _ _ =>
    reshape_expr e ltac:(fun K' e' =>
      tac (K' ++ K) e')
  | |- _  bin_log_related _ _ _ ?e _ _ =>
    reshape_expr e ltac:(fun K' e' => tac K' e')
  end.

62 63 64 65 66 67 68 69 70 71 72 73 74 75
Tactic Notation "rel_bind_l" open_constr(efoc) :=
  iStartProof;
  eapply (tac_rel_bind_l efoc);
  [ tac_bind_helper
  | (* new goal *) 
  ].

Tactic Notation "rel_bind_r" open_constr(efoc) :=
  iStartProof;
  eapply (tac_rel_bind_r efoc);
  [ tac_bind_helper
  | (* new goal *) 
  ].

76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264
Lemma tac_rel_rec_l `{heapIG Σ, !cfgSG Σ} Δ E1 Γ e K' f x ef e' efbody v eres t τ :
  e = fill K' (App ef e') 
  ef = Rec f x efbody 
  Closed (x :b: f :b: ) efbody 
  to_val e' = Some v 
  eres = subst' f ef (subst' x e' efbody) 
  (Δ   bin_log_related E1 E1 Γ (fill K' eres) t τ) 
  (Δ  bin_log_related E1 E1 Γ e t τ).
Proof.
  intros ??????.
  subst e ef eres.
  rewrite -(bin_log_related_rec_l Γ E1); eassumption.
Qed.

Tactic Notation "rel_rec_l" :=
  iStartProof;
  rel_reshape_cont_l ltac:(fun K e' =>
      match eval hnf in e' with App ?e1 ?e2 =>
        eapply (tac_rel_rec_l _ _ _ _ _ _ _ e1 e2);
        [tac_bind_helper (* e = fill K' _ *)
        |simpl; fast_done
        |solve_closed
        |fast_done (* to_val e' = Some v *)
        |try fast_done (* eres = subst ... *)
        |simpl; rewrite ?Closed_subst_id; iNext (* new goal *)]
      end)
  || fail "rel_rec_l: cannot find '(λx.e) ..'".

Tactic Notation "rel_seq_l" := rel_rec_l.
Tactic Notation "rel_let_l" := rel_rec_l.

Lemma tac_rel_binop_l `{heapIG Σ, !cfgSG Σ} Δ E1 Γ e K' op a b eres t τ :
  e = fill K' (BinOp op (#n a) (#n b)) 
  eres = of_val (binop_eval op a b) 
  (Δ   bin_log_related E1 E1 Γ (fill K' eres) t τ) 
  (Δ  bin_log_related E1 E1 Γ e t τ).
Proof.
  intros ???.
  subst e eres.
  rewrite -(bin_log_related_binop_l Γ E1); eassumption.
Qed.

Tactic Notation "rel_op_l" :=
  iStartProof;
  eapply (tac_rel_binop_l);
    [tac_bind_helper (* e = fill K' ... *)
    |simpl; reflexivity (* eres = of_val .. *)
    |iNext (* new goal *)].

Lemma tac_rel_fork_l `{heapIG Σ, !cfgSG Σ} Δ1 E1 E2 e' K' Γ e t τ :
  e = fill K' (Fork e') 
  Closed  e' 
  (Δ1  |={E1,E2}=>  WP e' {{ _ , True }}  bin_log_related E2 E1 Γ (fill K' (Lit Unit)) t τ) 
  (Δ1  bin_log_related E1 E1 Γ e t τ).
Proof.
  intros ???.
  subst e.
  rewrite -(bin_log_related_fork_l Γ E1 E2); eassumption.
Qed.

Tactic Notation "rel_fork_l" :=
  iStartProof;
  eapply (tac_rel_fork_l);
    [tac_bind_helper || fail "rel_fork_l: cannot find 'fork'"
    |solve_closed
    |simpl (* new goal *) ].

Lemma tac_rel_alloc_l `{heapIG Σ, !cfgSG Σ} nam nam_cl Δ1 Δ2 E1 E2 p i1 N P e' v' K' Γ e t τ :
  nclose N  E1 
  envs_lookup i1 Δ1 = Some (p, inv N P) 
  E2 = E1  N 
  e = fill K' (Alloc e') 
  to_val e' = Some v' 
  envs_lookup nam Δ1 = None 
  envs_lookup nam_cl Δ1 = None 
  nam_cl  nam 
  Δ2 = envs_snoc (envs_snoc Δ1 false nam ( P)%I) false nam_cl ( P ={E1  N,E1}= True)%I 
  (Δ2  |={E2}=>  l,
     (l ↦ᵢ v' - bin_log_related E2 E1 Γ (fill K' (Loc l)) t τ)) 
  (Δ1  bin_log_related E1 E1 Γ e t τ).
Proof.
  intros ??????????.
  rewrite -(idemp uPred_and Δ1).
  rewrite {1}envs_lookup_sound'. 2: eassumption.
  rewrite uPred.sep_elim_l uPred.always_and_sep_l.
  rewrite inv_open. 2: eassumption.
  subst e.
  rewrite -(bin_log_related_alloc_l Γ E1 E2). 2: eassumption.
  rewrite fupd_frame_r.
  rewrite -(fupd_trans E1 E2 E2).
  subst E2.
  apply fupd_mono.  
  rewrite -H9.
  subst Δ2.
  rewrite (envs_snoc_sound Δ1 false nam (P)) /=. 2: eassumption.
  rewrite comm.
  rewrite assoc.
  rewrite uPred.wand_elim_l.
  rewrite (envs_snoc_sound (envs_snoc Δ1 false nam ( P)) false nam_cl ( P ={E1  N,E1}= True)) //;
          last first.
  { rewrite (envs_lookup_snoc_ne Δ1); eassumption. }
  apply uPred.wand_elim_l.
Qed.

Tactic Notation "rel_alloc_l" "under" constr(N) "as" constr(nam) constr(nam_cl) :=
  iStartProof;
  eapply (tac_rel_alloc_l nam nam_cl);
    [solve_ndisj || fail "rel_alloc_l: cannot prove 'nclose " N " ⊆ ?'"
    |iAssumptionCore || fail "rel_alloc_l: cannot find inv " N " ?" 
    |try fast_done (* E2 = E1 \ N *)
    |tac_bind_helper (* e = fill K' (Store (Loc l) e') *)
    |try fast_done (* to_val e' = Some v *)
    |try fast_done (* nam fresh *)
    |try fast_done (* nam_cl fresh *)
    |eauto (* nam =/= nam_cl *)
    |env_cbv; reflexivity || fail "rel_alloc_l: this should not happen"
    |(* new goal *)].

Lemma tac_rel_alloc_l_simp `{heapIG Σ, !cfgSG Σ} Δ1 E1 e' v' K' Γ e t τ :
  e = fill K' (Alloc e') 
  to_val e' = Some v' 
  (Δ1   l,
     (l ↦ᵢ v' - bin_log_related E1 E1 Γ (fill K' (Loc l)) t τ)) 
  (Δ1  bin_log_related E1 E1 Γ e t τ).
Proof.
  intros ???.
  subst e.
  rewrite -(bin_log_related_alloc_l' Γ E1). 2: eassumption.
  done.
Qed.

Tactic Notation "rel_alloc_l" "as" ident(l) constr(H) :=
  iStartProof;
  eapply (tac_rel_alloc_l_simp);
    [tac_bind_helper (* e = fill K' .. *)
    |try fast_done (* to_val e' = Some v *)
    |iIntros (l) H (* new goal *)].


Lemma tac_rel_load_l `{heapIG Σ, !cfgSG Σ} nam nam_cl Δ1 Δ2 E1 E2 p i1 N P l K' Γ e t τ :
  nclose N  E1 
  envs_lookup i1 Δ1 = Some (p, inv N P) 
  E2 = E1  N 
  e = fill K' (Load (Loc l)) 
  envs_lookup nam Δ1 = None 
  envs_lookup nam_cl Δ1 = None 
  nam_cl  nam 
  Δ2 = envs_snoc (envs_snoc Δ1 false nam ( P)%I) false nam_cl ( P ={E1  N,E1}= True)%I 
  (Δ2  |={E2}=>  v,  (l ↦ᵢ v) 
     (l ↦ᵢ v - bin_log_related E2 E1 Γ (fill K' (of_val v)) t τ)) 
  (Δ1  bin_log_related E1 E1 Γ e t τ).
Proof.
  intros ?????????.
  rewrite -(idemp uPred_and Δ1).
  rewrite {1}envs_lookup_sound'. 2: eassumption.
  rewrite uPred.sep_elim_l uPred.always_and_sep_l.
  rewrite inv_open. 2: eassumption.
  subst e.
  rewrite -(bin_log_related_load_l Γ E1 E2).
  rewrite fupd_frame_r.
  rewrite -(fupd_trans E1 E2 E2).
  subst E2.
  apply fupd_mono.  
  rewrite -H8.
  subst Δ2.
  rewrite (envs_snoc_sound Δ1 false nam (P)) /=. 2: eassumption.
  rewrite comm.
  rewrite assoc.
  rewrite uPred.wand_elim_l.
  rewrite (envs_snoc_sound (envs_snoc Δ1 false nam ( P)) false nam_cl ( P ={E1  N,E1}= True)) //;
          last first.
  { rewrite (envs_lookup_snoc_ne Δ1); eassumption. }
  rewrite uPred.wand_elim_l.
  done.
Qed.

Tactic Notation "rel_load_l" "under" constr(N) "as" constr(nam) constr(nam_cl) :=
  iStartProof;
  eapply (tac_rel_load_l nam nam_cl);
    [solve_ndisj || fail "rel_load_l: cannot prove 'nclose " N " ⊆ ?'"
    |iAssumptionCore || fail "rel_load_l: cannot find inv " N " ?" 
    |try fast_done (* E2 = E1 \ N *)
    |tac_bind_helper (* e = fill K' .. *)
    |try fast_done (* nam fresh *)
    |try fast_done (* nam_cl fresh *)
    |eauto (* nam =/= nam_cl *)
    |env_cbv; reflexivity || fail "rel_load_l: this should not happen"
    |(* new goal *)].

265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316
Lemma tac_rel_store_l `{heapIG Σ, !cfgSG Σ} nam nam_cl Δ1 Δ2 E1 E2 p i1 N P l e' v' K' Γ e t τ :
  nclose N  E1 
  envs_lookup i1 Δ1 = Some (p, inv N P) 
  E2 = E1  N 
  e = fill K' (Store (Loc l) e') 
  to_val e' = Some v' 
  envs_lookup nam Δ1 = None 
  envs_lookup nam_cl Δ1 = None 
  nam_cl  nam 
  Δ2 = envs_snoc (envs_snoc Δ1 false nam ( P)%I) false nam_cl ( P ={E1  N,E1}= True)%I 
  (Δ2  |={E2}=>  v,  (l ↦ᵢ v) 
     (l ↦ᵢ v' - bin_log_related E2 E1 Γ (fill K' (Lit Unit)) t τ)) 
  (Δ1  bin_log_related E1 E1 Γ e t τ).
Proof.
  intros ??????????.
  rewrite -(idemp uPred_and Δ1).
  rewrite {1}envs_lookup_sound'. 2: eassumption.
  rewrite uPred.sep_elim_l uPred.always_and_sep_l.
  rewrite inv_open. 2: eassumption.
  subst e.
  rewrite -(bin_log_related_store_l Γ E1 E2). 2: eassumption.
  rewrite fupd_frame_r.
  rewrite -(fupd_trans E1 E2 E2).
  subst E2.
  apply fupd_mono.  
  rewrite -H9.
  subst Δ2.
  rewrite (envs_snoc_sound Δ1 false nam (P)) /=. 2: eassumption.
  rewrite comm.
  rewrite assoc.
  rewrite uPred.wand_elim_l.
  rewrite (envs_snoc_sound (envs_snoc Δ1 false nam ( P)) false nam_cl ( P ={E1  N,E1}= True)) //;
          last first.
  { rewrite (envs_lookup_snoc_ne Δ1); eassumption. }
  rewrite uPred.wand_elim_l.
  done.
Qed.

Tactic Notation "rel_store_l" "under" constr(N) "as" constr(nam) constr(nam_cl) :=
  iStartProof;
  eapply (tac_rel_store_l nam nam_cl);
    [solve_ndisj || fail "rel_store_l: cannot prove 'nclose " N " ⊆ ?'"
    |iAssumptionCore || fail "rel_store_l: cannot find inv " N " ?" 
    |try fast_done (* E2 = E1 \ N *)
    |tac_bind_helper (* e = fill K' (Store (Loc l) e') *)
    |try fast_done (* to_val e' = Some v *)
    |try fast_done (* nam fresh *)
    |try fast_done (* nam_cl fresh *)
    |eauto (* nam =/= nam_cl *)
    |env_cbv; reflexivity || fail "rel_store_l: this should not happen"
    |(* new goal *)].

317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428

Lemma tac_rel_store_l_simp `{heapIG Σ, !cfgSG Σ} Δ1 Δ2 i1 E1 l v e' v' K' Γ e t τ :
  e = fill K' (Store (Loc l) e') 
  to_val e' = Some v' 
  envs_lookup i1 Δ1 = Some (false, l ↦ᵢ v)%I 
  envs_simple_replace i1 false (Esnoc Enil i1 (l ↦ᵢ v')) Δ1 = Some Δ2 
  (Δ2  bin_log_related E1 E1 Γ (fill K' (Lit Unit)) t τ) 
  (Δ1  bin_log_related E1 E1 Γ e t τ).
Proof.
  intros ?????.
  subst e.
  rewrite envs_simple_replace_sound //; simpl.
  rewrite right_id.
  rewrite (uPred.later_intro (l ↦ᵢ v)%I).
  rewrite (bin_log_related_store_l' Γ E1). 2: eassumption.
  rewrite H4.
  apply uPred.wand_elim_l.
Qed.

Tactic Notation "rel_store_l" :=
  iStartProof;
  eapply (tac_rel_store_l_simp);
    [tac_bind_helper (* e = fill K' .. *)    
    |try fast_done (* to_val e' = Some v' *)
    |iAssumptionCore || fail "rel_store_l: cannot find '? ↦ᵢ ?'"
    |env_cbv; reflexivity || fail "rel_store_l: this should not happen"
    | (* new goal *)].

Lemma tac_rel_cas_l `{heapIG Σ, !cfgSG Σ} nam nam_cl Δ1 Δ2 E1 E2 p i1 N P l e1 e2 v1 v2 K' Γ e t τ :
  nclose N  E1 
  envs_lookup i1 Δ1 = Some (p, inv N P) 
  E2 = E1  N 
  e = fill K' (CAS (Loc l) e1 e2) 
  to_val e1 = Some v1 
  to_val e2 = Some v2 
  envs_lookup nam Δ1 = None 
  envs_lookup nam_cl Δ1 = None 
  nam_cl  nam 
  Δ2 = envs_snoc (envs_snoc Δ1 false nam ( P)%I) false nam_cl ( P ={E1  N,E1}= True)%I 
  (Δ2  |={E2}=>  v,  (l ↦ᵢ v) 
     ((v  v1  (l ↦ᵢ v  - {E2,E1;Γ}  fill K' (# false) log t : τ)) 
      (v = v1  (l ↦ᵢ v2 - {E2,E1;Γ}  fill K' (# true) log t : τ)))) 
  (Δ1  bin_log_related E1 E1 Γ e t τ).
Proof.
  intros ???????????.
  rewrite -(idemp uPred_and Δ1).
  rewrite {1}envs_lookup_sound'. 2: eassumption.
  rewrite uPred.sep_elim_l uPred.always_and_sep_l.
  rewrite inv_open. 2: eassumption.
  subst e.
  rewrite -(bin_log_related_cas_l Γ E1 E2); try eassumption.
  rewrite fupd_frame_r.
  rewrite -(fupd_trans E1 E2 E2).
  subst E2.
  apply fupd_mono.  
  subst Δ2.
  rewrite (envs_snoc_sound Δ1 false nam (P)) /=. 2: eassumption.
  rewrite comm.
  rewrite assoc.
  rewrite uPred.wand_elim_l.
  rewrite (envs_snoc_sound (envs_snoc Δ1 false nam ( P)) false nam_cl ( P ={E1  N,E1}= True)) //;
          last first.
  { rewrite (envs_lookup_snoc_ne Δ1); eassumption. }
  rewrite H10.
  rewrite uPred.wand_elim_l.
  apply fupd_mono.
  iDestruct 1 as (v) "[Hl Hv]". iExists v. iFrame "Hl".
  iDestruct "Hv" as "[[% Hv] | [% Hv]]"; subst.
  - iSplitL; last first; iIntros "%". by exfalso.
    done.
  - iSplitR; iIntros "%". by exfalso.
    done.
Qed.

Tactic Notation "rel_cas_l" "under" constr(N) "as" constr(nam) constr(nam_cl) :=
  iStartProof;
  eapply (tac_rel_cas_l nam nam_cl);
    [solve_ndisj || fail "rel_store_l: cannot prove 'nclose " N " ⊆ ?'"
    |iAssumptionCore || fail "rel_store_l: cannot find inv " N " ?" 
    |try fast_done (* E2 = E1 \ N *)
    |tac_bind_helper (* e = fill K' ... *)
    |try fast_done (* to_val e1 = Some .. *)
    |try fast_done (* to_val e2 = Some .. *)
    |try fast_done (* nam fresh *)
    |try fast_done (* nam_cl fresh *)
    |eauto (* nam =/= nam_cl *)
    |env_cbv; reflexivity || fail "rel_store_l: this should not happen"
    |(* new goal *)].


(********************************)

Lemma tac_rel_fork_r `{heapIG Σ, !cfgSG Σ} Δ1 E1 E2  t' K' Γ e t τ :
  nclose specN  E1 
  t = fill K' (Fork t') 
  Closed  t' 
  (Δ1   i, i  t' - bin_log_related E1 E2 Γ e (fill K' (Lit Unit)) τ) 
  (Δ1  bin_log_related E1 E2 Γ e t τ).
Proof.
  intros ????.
  subst t.
  rewrite -(bin_log_related_fork_r Γ E1 E2); eassumption.
Qed.

Tactic Notation "rel_fork_r" "as" ident(i) constr(H) :=
  iStartProof;
  eapply (tac_rel_fork_r);
    [solve_ndisj || fail "rel_fork_r: cannot prove 'nclose specN ⊆ ?'"
    |tac_bind_helper || fail "rel_fork_r: cannot find 'alloc'"
    |solve_closed
    |simpl; iIntros (i) H (* new goal *)].

429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481
Lemma tac_rel_store_r `{heapIG Σ, !cfgSG Σ} Δ1 Δ2 E1 E2 i1 l t' v' K' Γ e t τ v :
  nclose specN  E1 
  t = fill K' (Store (Loc l) t') 
  to_val t' = Some v' 
  envs_lookup i1 Δ1 = Some (false, l ↦ₛ v)%I 
  envs_simple_replace i1 false (Esnoc Enil i1 (l ↦ₛ v')) Δ1 = Some Δ2 
  (Δ2  bin_log_related E1 E2 Γ e (fill K' (Lit Unit)) τ) 
  (Δ1  bin_log_related E1 E2 Γ e t τ).
Proof.
  intros ??????.
  rewrite envs_simple_replace_sound //; simpl.
  rewrite right_id.
  subst t.
  rewrite (bin_log_related_store_r Γ K' E1 E2 l); [ | eassumption | eassumption ].
  rewrite H5. 
  apply uPred.wand_elim_l.
Qed.

Tactic Notation "rel_store_r" :=
  iStartProof;
  eapply (tac_rel_store_r);
    [solve_ndisj || fail "rel_store_r: cannot prove 'nclose specN ⊆ ?'"
    |tac_bind_helper (* e = fill K' (Store (Loc l) e') *)
    |try fast_done (* to_val e' = Some v *)
    |iAssumptionCore || fail "rel_store_l: cannot find ? ↦ₛ ?" 
    |env_cbv; reflexivity || fail "rel_store_r: this should not happen"
    |(* new goal *)].

Lemma tac_rel_alloc_r `{heapIG Σ, !cfgSG Σ} Δ1 E1 E2  t' v' K' Γ e t τ :
  nclose specN  E1 
  t = fill K' (Alloc t') 
  to_val t' = Some v' 
  (Δ1   l, l ↦ₛ v' - bin_log_related E1 E2 Γ e (fill K' (Loc l)) τ) 
  (Δ1  bin_log_related E1 E2 Γ e t τ).
Proof.
  intros ????.
  subst t.
  rewrite -(bin_log_related_alloc_r Γ K' E1 E2); eassumption.
Qed.

Tactic Notation "rel_alloc_r" "as" ident(l) constr(H) :=
  iStartProof;
  eapply (tac_rel_alloc_r);
    [solve_ndisj || fail "rel_alloc_r: cannot prove 'nclose specN ⊆ ?'"
    |tac_bind_helper || fail "rel_alloc_r: cannot find 'alloc'"
    |try fast_done (* to_val t' = Some v' *)
    |simpl; iIntros (l) H (* new goal *)].

Tactic Notation "rel_alloc_r" :=
  let l := fresh in
  let H := iFresh "H" in
  rel_alloc_r as l H.

482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575
Lemma tac_rel_load_r `{heapIG Σ, !cfgSG Σ} Δ1 Δ2 E1 E2 i1 l K' Γ e t τ v :
  nclose specN  E1 
  t = fill K' (Load (Loc l)) 
  envs_lookup i1 Δ1 = Some (false, l ↦ₛ v)%I 
  envs_simple_replace i1 false 
    (Esnoc Enil i1 (l ↦ₛ v)%I) Δ1 = Some Δ2 
  (Δ2  bin_log_related E1 E2 Γ e (fill K' (of_val v)) τ) 
  (Δ1  bin_log_related E1 E2 Γ e t τ).
Proof.
  intros ?????.
  rewrite (envs_simple_replace_sound Δ1 Δ2 i1) //; simpl. 
  rewrite right_id.
  subst t.
  rewrite {1}(bin_log_related_load_r Γ K' E1 E2 l); [ | eassumption ].
  rewrite H4.
  apply uPred.wand_elim_l.
Qed.

Tactic Notation "rel_load_r" :=
  iStartProof;
  eapply (tac_rel_load_r);
    [solve_ndisj || fail "rel_load_r: cannot prove 'nclose specN ⊆ ?'"
    |tac_bind_helper (* e = fill K' (Store (Loc l) e') *)
    |iAssumptionCore || fail "rel_load_l: cannot find ? ↦ₛ ?" 
    |env_cbv; reflexivity || fail "rel_load_r: this should not happen"
    |simpl (* new goal *)].

Lemma tac_rel_cas_fail_r `{heapIG Σ, !cfgSG Σ} Δ1 Δ2 E1 E2 i1 l K' Γ e t e1 e2 v1 v2 τ v :
  nclose specN  E1 
  t = fill K' (CAS (Loc l) e1 e2) 
  to_val e1 = Some v1 
  to_val e2 = Some v2 
  envs_lookup i1 Δ1 = Some (false, l ↦ₛ v)%I 
  v  v1 
  envs_simple_replace i1 false 
    (Esnoc Enil i1 (l ↦ₛ v)%I) Δ1 = Some Δ2 
  (Δ2  bin_log_related E1 E2 Γ e (fill K' (# false)) τ) 
  (Δ1  bin_log_related E1 E2 Γ e t τ).
Proof.
  intros ????????.
  rewrite (envs_simple_replace_sound Δ1 Δ2 i1) //; simpl. 
  rewrite right_id.
  subst t.
  rewrite {1}(bin_log_related_cas_fail_r Γ E1 E2 _ l e1 e2 v1 v2 v); eauto.
  rewrite H7.
  apply uPred.wand_elim_l.
Qed.

Tactic Notation "rel_cas_fail_r" :=
  iStartProof;
  eapply (tac_rel_cas_fail_r);
    [solve_ndisj || fail "rel_cas_fail_r: cannot prove 'nclose specN ⊆ ?'"
    |tac_bind_helper || fail "rel_cas_fail_r: cannot find 'CAS ..'"
    |try fast_done
    |try fast_done
    |iAssumptionCore || fail "rel_cas_fail_l: cannot find ? ↦ₛ ?" 
    |try fast_done (* v  v1 *)
    |env_cbv; reflexivity || fail "rel_load_r: this should not happen"
    |(* new goal *)].


Lemma tac_rel_cas_suc_r `{heapIG Σ, !cfgSG Σ} Δ1 Δ2 E1 E2 i1 l K' Γ e t e1 e2 v1 v2 τ v :
  nclose specN  E1 
  t = fill K' (CAS (Loc l) e1 e2) 
  to_val e1 = Some v1 
  to_val e2 = Some v2 
  envs_lookup i1 Δ1 = Some (false, l ↦ₛ v)%I 
  v = v1 
  envs_simple_replace i1 false 
    (Esnoc Enil i1 (l ↦ₛ v2)%I) Δ1 = Some Δ2 
  (Δ2  bin_log_related E1 E2 Γ e (fill K' (# true)) τ) 
  (Δ1  bin_log_related E1 E2 Γ e t τ).
Proof.
  intros ????????.
  rewrite (envs_simple_replace_sound Δ1 Δ2 i1) //; simpl. 
  rewrite right_id.
  subst t.
  rewrite {1}(bin_log_related_cas_suc_r Γ E1 E2 _ l e1 e2 v1 v2 v); eauto.
  rewrite H7.
  apply uPred.wand_elim_l.
Qed.

Tactic Notation "rel_cas_suc_r" :=
  iStartProof;
  eapply (tac_rel_cas_suc_r);
    [solve_ndisj || fail "rel_cas_suc_r: cannot prove 'nclose specN ⊆ ?'"
    |tac_bind_helper || fail "rel_cas_suc_r: cannot find 'CAS ..'"
    |try fast_done
    |try fast_done
    |iAssumptionCore || fail "rel_cas_suc_l: cannot find ? ↦ₛ ?" 
    |try fast_done (* v = v1 *)
    |env_cbv; reflexivity || fail "rel_load_r: this should not happen"
    |(* new goal *)].

576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593

Lemma tac_rel_rec_r `{heapIG Σ, !cfgSG Σ} Δ E1 E2 Γ e K' f x ef e' efbody v eres t τ :
  nclose specN  E1 
  e = fill K' (App (Rec f x efbody) e') 
  ef = Rec f x efbody 
  Closed (x :b: f :b: ) efbody 
  to_val e' = Some v 
  eres = subst' f ef (subst' x e' efbody) 
  (Δ  bin_log_related E1 E2 Γ t (fill K' eres) τ) 
  (Δ  bin_log_related E1 E2 Γ t e τ).
Proof.
  intros ???????.
  subst e ef eres.
  rewrite -(bin_log_related_rec_r Γ E1 E2); eassumption.
Qed.

Tactic Notation "rel_rec_r" :=
  iStartProof;
594 595 596 597 598 599 600 601 602 603 604 605
  rel_reshape_cont_r ltac:(fun K e' =>
      match eval hnf in e' with App ?e1 ?e2 =>
        eapply (tac_rel_rec_r _ _ _ _ _ _ _ _ e1 e2);
        [solve_ndisj || fail "rel_rec_r: cannot prove 'nclose specN ⊆ ?'"
        |tac_bind_helper (* e = fill K' _ *)
        |simpl; fast_done
        |solve_closed
        |fast_done (* to_val e' = Some v *)
        |try fast_done (* eres = subst ... *)
        |simpl; rewrite ?Closed_subst_id (* new goal *)]
      end)
  || fail "rel_rec_r: cannot find '(λx.e) ..'".
606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839

Tactic Notation "rel_seq_r" := rel_rec_r.
Tactic Notation "rel_let_r" := rel_rec_r.

Lemma tac_rel_fst_r `{heapIG Σ, !cfgSG Σ} Δ E1 E2 Γ e K' e1 e2 v1 v2 t τ :
  nclose specN  E1 
  e = fill K' (Fst (Pair e1 e2)) 
  to_val e1 = Some v1 
  to_val e2 = Some v2 
  (Δ  bin_log_related E1 E2 Γ t (fill K' e1) τ) 
  (Δ  bin_log_related E1 E2 Γ t e τ).
Proof.
  intros ?????.
  subst e. 
  rewrite -(of_to_val e1 v1); [| eassumption].
  rewrite -(of_to_val e2 v2); [| eassumption].
  rewrite -(bin_log_related_fst_r Γ E1 E2); [| eassumption].
  rewrite (of_to_val e1); eauto.
Qed.

Tactic Notation "rel_fst_r" :=
  iStartProof;
  eapply (tac_rel_fst_r);
    [solve_ndisj || fail "rel_fst_r: cannot prove 'nclose specN ⊆ ?'"
    |tac_bind_helper (* e = fill K' _ *)
    |try fast_done (* to_val e1 = Some .. *)
    |try fast_done (* to_val e2 = Some .. *)
    |simpl (* new goal *)].

Lemma tac_rel_snd_r `{heapIG Σ, !cfgSG Σ} Δ E1 E2 Γ e K' e1 e2 v1 v2 t τ :
  nclose specN  E1 
  e = fill K' (Snd (Pair e1 e2)) 
  to_val e1 = Some v1 
  to_val e2 = Some v2 
  (Δ  bin_log_related E1 E2 Γ t (fill K' e2) τ) 
  (Δ  bin_log_related E1 E2 Γ t e τ).
Proof.
  intros ?????.
  subst e. 
  rewrite -(of_to_val e1 v1); [| eassumption].
  rewrite -(of_to_val e2 v2); [| eassumption].
  rewrite -(bin_log_related_snd_r Γ E1 E2); [| eassumption].
  rewrite (of_to_val e2); eauto.
Qed.

Tactic Notation "rel_snd_r" :=
  iStartProof;
  eapply (tac_rel_snd_r);
    [solve_ndisj || fail "rel_snd_r: cannot prove 'nclose specN ⊆ ?'"
    |tac_bind_helper (* e = fill K' _ *)
    |try fast_done (* to_val e1 = Some .. *)
    |try fast_done (* to_val e2 = Some .. *)
    |simpl (* new goal *)].

Lemma tac_rel_tlam_r `{heapIG Σ, !cfgSG Σ} Δ E1 E2 Γ e K' e' t τ :
  nclose specN  E1 
  e = fill K' (TApp (TLam e')) 
  Closed  e' 
  (Δ  bin_log_related E1 E2 Γ t (fill K' e') τ) 
  (Δ  bin_log_related E1 E2 Γ t e τ).
Proof.
  intros ????.
  subst e. 
  rewrite -(bin_log_related_tlam_r Γ E1 E2); eassumption.
Qed.

Tactic Notation "rel_tlam_r" :=
  iStartProof;
  eapply (tac_rel_tlam_r);
    [solve_ndisj || fail "rel_tlam_r: cannot prove 'nclose specN ⊆ ?'"
    |tac_bind_helper || fail "rel_tlam_r: cannot find '(Λ.e)[]'"
    |solve_closed
    |simpl (* new goal *)].

Lemma tac_rel_fold_r `{heapIG Σ, !cfgSG Σ} Δ E1 E2 Γ e K' e' v t τ :
  nclose specN  E1 
  e = fill K' (Unfold (Fold e')) 
  to_val e' = Some v 
  (Δ  bin_log_related E1 E2 Γ t (fill K' e') τ) 
  (Δ  bin_log_related E1 E2 Γ t e τ).
Proof.
  intros ????.
  subst e.
  rewrite -(bin_log_related_fold_r Γ E1 E2); eassumption.
Qed.

Tactic Notation "rel_fold_r" :=
  iStartProof;
  eapply (tac_rel_fold_r);
    [solve_ndisj || fail "rel_fold_r: cannot prove 'nclose specN ⊆ ?'"
    |tac_bind_helper || fail "rel_fold_r: cannot find 'Unfold (Fold e)'"
    |try fast_done (* to_val e' = Some .. *)
    |simpl (* new goal *)].

Lemma tac_rel_case_inl_r `{heapIG Σ, !cfgSG Σ} Δ E1 E2 Γ e K' e0 e1 e2 v t τ :
  nclose specN  E1 
  e = fill K' (Case (InjL e0) e1 e2) 
  Closed  e1 
  Closed  e2 
  to_val e0 = Some v 
  (Δ  bin_log_related E1 E2 Γ t (fill K' (App e1 e0)) τ) 
  (Δ  bin_log_related E1 E2 Γ t e τ).
Proof.
  intros ??????.
  subst e.
  rewrite -(bin_log_related_case_inl_r Γ E1 E2); eassumption.
Qed.

Tactic Notation "rel_case_inl_r" :=
  iStartProof;
  eapply (tac_rel_case_inl_r);
    [solve_ndisj || fail "rel_case_inl_r: cannot prove 'nclose specN ⊆ ?'"
    |tac_bind_helper || fail "rel_case_inl_r: cannot find 'match InjL e with ..'"
    |solve_closed
    |solve_closed
    |try fast_done (* to_val e0 = Some .. *)
    |simpl (* new goal *)].

Lemma tac_rel_case_inr_r `{heapIG Σ, !cfgSG Σ} Δ E1 E2 Γ e K' e0 e1 e2 v t τ :
  nclose specN  E1 
  e = fill K' (Case (InjR e0) e1 e2) 
  Closed  e1 
  Closed  e2 
  to_val e0 = Some v 
  (Δ  bin_log_related E1 E2 Γ t (fill K' (App e2 e0)) τ) 
  (Δ  bin_log_related E1 E2 Γ t e τ).
Proof.
  intros ??????.
  subst e.
  rewrite -(bin_log_related_case_inr_r Γ E1 E2); eassumption.
Qed.

Tactic Notation "rel_case_inr_r" :=
  iStartProof;
  eapply (tac_rel_case_inr_r);
    [solve_ndisj || fail "rel_case_inr_r: cannot prove 'nclose specN ⊆ ?'"
    |tac_bind_helper || fail "rel_case_inr_r: cannot find 'match InjR e with ..'"
    |solve_closed
    |solve_closed
    |try fast_done (* to_val e0 = Some .. *)
    |simpl (* new goal *)].

Tactic Notation "rel_case_r" := rel_case_inl_r || rel_case_inr_r.

Lemma tac_rel_if_true_r `{heapIG Σ, !cfgSG Σ} Δ E1 E2 Γ e K' e1 e2 t τ :
  nclose specN  E1 
  e = fill K' (If (# true) e1 e2) 
  Closed  e1 
  Closed  e2 
  (Δ  bin_log_related E1 E2 Γ t (fill K' e1) τ) 
  (Δ  bin_log_related E1 E2 Γ t e τ).
Proof.
  intros ?????.
  subst e.
  rewrite -(bin_log_related_if_true_r Γ); eassumption.
Qed.

Tactic Notation "rel_if_true_r" :=
  iStartProof;
  eapply (tac_rel_if_true_r);
    [solve_ndisj || fail "rel_if_true_r: cannot prove 'nclose specN ⊆ ?'"
    |tac_bind_helper || fail "rel_if_true_r: cannot find 'if true ..'"
    |solve_closed
    |solve_closed
    |simpl (* new goal *)].

Lemma tac_rel_if_false_r `{heapIG Σ, !cfgSG Σ} Δ E1 E2 Γ e K' e1 e2 t τ :
  nclose specN  E1 
  e = fill K' (If (# false) e1 e2) 
  Closed  e1 
  Closed  e2 
  (Δ  bin_log_related E1 E2 Γ t (fill K' e2) τ) 
  (Δ  bin_log_related E1 E2 Γ t e τ).
Proof.
  intros ?????.
  subst e.
  rewrite -(bin_log_related_if_false_r Γ); eassumption.
Qed.

Tactic Notation "rel_if_false_r" :=
  iStartProof;
  eapply (tac_rel_if_false_r);
    [solve_ndisj || fail "rel_if_false_r: cannot prove 'nclose specN ⊆ ?'"
    |tac_bind_helper || fail "rel_if_false_r: cannot find 'if false ..'"
    |solve_closed
    |solve_closed
    |simpl (* new goal *)].

Lemma tac_rel_if_r `{heapIG Σ, !cfgSG Σ} Δ E1 E2 Γ e K' b eres e1 e2 t τ :
  nclose specN  E1 
  e = fill K' (If (# b) e1 e2) 
  Closed  e1 
  Closed  e2 
  eres = (if b then e1 else e2) 
  (Δ  bin_log_related E1 E2 Γ t (fill K' eres) τ) 
  (Δ  bin_log_related E1 E2 Γ t e τ).
Proof.
  intros ??????.
  subst e.
  destruct b; subst eres.
  + rewrite -(bin_log_related_if_true_r Γ); eassumption.
  + rewrite -(bin_log_related_if_false_r Γ); eassumption.
Qed.

Tactic Notation "rel_if_r" :=
  iStartProof;
  eapply (tac_rel_if_r);
    [solve_ndisj || fail "rel_if_r: cannot prove 'nclose specN ⊆ ?'"
    |tac_bind_helper || fail "rel_if_r: cannot find 'if (#♭ ..) ..'"
    |solve_closed
    |solve_closed
    |simpl; fast_done || fail "rel_if_r: cannot compute the boolean value"
    |simpl (* new goal *)].

Lemma tac_rel_binop_r `{heapIG Σ, !cfgSG Σ} Δ E1 E2 Γ e K' op a b t τ :
  nclose specN  E1 
  e = fill K' (BinOp op (#n a) (#n b)) 
  (Δ  bin_log_related E1 E2 Γ t (fill K' (of_val (binop_eval op a b))) τ) 
  (Δ  bin_log_related E1 E2 Γ t e τ).
Proof.
  intros ???.
  subst e.
  rewrite -(bin_log_related_binop_r Γ); eassumption.
Qed.

Tactic Notation "rel_op_r" :=
  iStartProof;
  eapply (tac_rel_binop_r);
    [solve_ndisj || fail "rel_op_r: cannot prove 'nclose specN ⊆ ?'"
    |tac_bind_helper || fail "rel_op_r: cannot find an operator"
    |simpl (* new goal *)].

(* TODO: tac_rel_pack_r *)

840 841 842 843 844 845
Tactic Notation "rel_vals" :=
  iStartProof;
  iApply bin_log_related_val; [ try fast_done | try fast_done | ];
  let d := fresh "Δ" in
  iIntros (d); simpl.

846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876
Section test.
  Context `{heapIG Σ, cfgSG Σ}.

  Definition choiceN : namespace := nroot .@ "choice".

  Definition choice_inv y y' : iProp Σ :=
    ( f, y ↦ᵢ (#v f)  y' ↦ₛ (#v f))%I.

  Definition storeFalse : val := λ: "y", "y" <- # false.

  Lemma test_store Γ y y' :
    inv choiceN (choice_inv y y')
    - Γ  storeFalse #y log storeFalse #y' : TUnit.
  Proof.
    iIntros "#Hinv".
    unfold storeFalse. unlock.
    rel_rec_l.
    rel_rec_r.

    rel_store_l under choiceN as "Hs" "Hcl".
      iDestruct "Hs" as (f) "[>Hy >Hy']". iExists _. iFrame "Hy".
      iModIntro. iIntros "Hy".
      rel_store_r. simpl.

    iMod ("Hcl" with "[Hy Hy']").
    { iNext. iExists _. iFrame. }

    iApply bin_log_related_val; eauto.
  Qed.

End test.