rel_tactics.v 34.2 KB
Newer Older
1 2 3
From iris.program_logic Require Export weakestpre.
From iris.proofmode Require Import coq_tactics sel_patterns.
From iris.proofmode Require Export tactics.
4
From iris_logrel.F_mu_ref_conc Require Import rules rules_binary hax.
5 6 7 8
From iris_logrel.F_mu_ref_conc Require Export lang tactics logrel_binary relational_properties.
Set Default Proof Using "Type".
Import lang.

9 10 11
(* Applied to goals that are equalities of expressions. Will try to unlock the
   LHS once if necessary, to get rid of the lock added by the syntactic sugar. *)
Ltac solve_of_val_unlock := try apply of_val_unlock; fast_done.
12

13
Lemma tac_rel_bind_gen `{logrelG Σ} Δ E1 E2 Γ e e' t t' τ :
14 15 16 17 18 19 20 21
  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.

22
Lemma tac_rel_bind_l `{logrelG Σ} e' K Δ E1 E2 Γ e t τ :
23 24 25 26 27
  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.

28
Lemma tac_rel_bind_r `{logrelG Σ} t' K Δ E1 E2 Γ e t τ :
29 30 31 32 33
  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.

34
Ltac tac_bind_helper :=
35 36 37 38 39 40 41 42 43 44 45 46
  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.

47 48 49 50 51 52 53 54 55
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.

56 57 58 59 60 61 62 63 64
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.

65 66 67 68 69 70 71 72 73 74 75 76 77 78
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 *) 
  ].

79
Lemma tac_rel_rec_l `{logrelG Σ} Δ E1 Γ e K' f x ef e' efbody v eres t τ :
80 81 82 83
  e = fill K' (App ef e') 
  ef = Rec f x efbody 
  Closed (x :b: f :b: ) efbody 
  to_val e' = Some v 
84
  eres = lamsubst ef v 
85 86 87
  (Δ   bin_log_related E1 E1 Γ (fill K' eres) t τ) 
  (Δ  bin_log_related E1 E1 Γ e t τ).
Proof.
88 89 90 91 92 93
  intros ????.
  subst ef. simpl.
  intros ?.
  subst e eres.  
  rewrite -(bin_log_related_rec_l Γ E1); last by eassumption.
  destruct f; simpl; rewrite (of_to_val e' v); eauto.
94 95 96 97 98 99 100 101
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' _ *)
102 103
        |solve_of_val_unlock
        |try solve_closed
Dan Frumin's avatar
Dan Frumin committed
104
        |solve_to_val (* to_val e' = Some v *)
105
        |try fast_done (* eres = subst ... *)
106
        |try simpl_subst/=; iNext (* new goal *)]
107 108 109 110 111 112
      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.

113
Lemma tac_rel_fst_l `{logrelG Σ} Δ E1 Γ e e1 e2 v1 v2 K' t τ :
Dan Frumin's avatar
Dan Frumin committed
114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134
  e = fill K' (Fst (Pair e1 e2)) 
  to_val e1 = Some v1 
  to_val e2 = Some v2 
  (Δ   bin_log_related E1 E1 Γ (fill K' e1) t τ) 
  (Δ  bin_log_related E1 E1 Γ e t τ).
Proof.
  intros ????.
  subst e.
  rewrite -(of_to_val e1 v1); auto. rewrite -(of_to_val e2 v2); auto.
  rewrite -(bin_log_related_fst_l Γ E1).
  by rewrite (of_to_val e1 v1).
Qed.

Tactic Notation "rel_fst_l" :=
  iStartProof;
  eapply (tac_rel_fst_l);
    [tac_bind_helper (* e = fill K' ... *)
    |solve_to_val
    |solve_to_val
    |iNext (* new goal *)].

135
Lemma tac_rel_snd_l `{logrelG Σ} Δ E1 Γ e e1 e2 v1 v2 K' t τ :
Dan Frumin's avatar
Dan Frumin committed
136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156
  e = fill K' (Snd (Pair e1 e2)) 
  to_val e1 = Some v1 
  to_val e2 = Some v2 
  (Δ   bin_log_related E1 E1 Γ (fill K' e2) t τ) 
  (Δ  bin_log_related E1 E1 Γ e t τ).
Proof.
  intros ????.
  subst e.
  rewrite -(of_to_val e1 v1); auto. rewrite -(of_to_val e2 v2); auto.
  rewrite -(bin_log_related_snd_l Γ E1).
  by rewrite (of_to_val e2 v2).
Qed.

Tactic Notation "rel_snd_l" :=
  iStartProof;
  eapply (tac_rel_snd_l);
    [tac_bind_helper (* e = fill K' ... *)
    |solve_to_val
    |solve_to_val
    |iNext (* new goal *)].

157
Lemma tac_rel_unfold_l `{logrelG Σ} Δ E1 Γ e e1 v K' t τ :
Dan Frumin's avatar
Dan Frumin committed
158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176
  e = fill K' (Unfold (Fold e1)) 
  to_val e1 = Some v 
  (Δ   bin_log_related E1 E1 Γ (fill K' e1) t τ) 
  (Δ  bin_log_related E1 E1 Γ e t τ).
Proof.
  intros ???.
  subst e.
  rewrite -(bin_log_related_fold_l Γ E1); eassumption.
Qed.

Tactic Notation "rel_unfold_l" :=
  iStartProof;
  eapply (tac_rel_unfold_l);
    [tac_bind_helper (* e = fill K' ... *)
    |solve_to_val
    |iNext (* new goal *)].

Tactic Notation "rel_fold_l" := rel_unfold_l.

177
Lemma tac_rel_if_true_l `{logrelG Σ} Δ E1 Γ e e1 e2 K' t τ :
Dan Frumin's avatar
Dan Frumin committed
178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196
  e = fill K' (If (# true) e1 e2) 
  Closed  e1 
  Closed  e2 
  (Δ   bin_log_related E1 E1 Γ (fill K' e1) t τ) 
  (Δ  bin_log_related E1 E1 Γ e t τ).
Proof.
  intros ????.
  subst e.
  rewrite -(bin_log_related_if_true_l Γ E1); eassumption.
Qed.

Tactic Notation "rel_if_true_l" :=
  iStartProof;
  eapply (tac_rel_if_true_l);
    [tac_bind_helper (* e = fill K' ... *)
    |try solve_closed
    |try solve_closed
    |iNext (* new goal *)].

197
Lemma tac_rel_if_false_l `{logrelG Σ} Δ E1 Γ e e1 e2 K' t τ :
Dan Frumin's avatar
Dan Frumin committed
198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216
  e = fill K' (If (# false) e1 e2) 
  Closed  e1 
  Closed  e2 
  (Δ   bin_log_related E1 E1 Γ (fill K' e2) t τ) 
  (Δ  bin_log_related E1 E1 Γ e t τ).
Proof.
  intros ????.
  subst e.
  rewrite -(bin_log_related_if_false_l Γ E1); eassumption.
Qed.

Tactic Notation "rel_if_false_l" :=
  iStartProof;
  eapply (tac_rel_if_false_l);
    [tac_bind_helper (* e = fill K' ... *)
    |try solve_closed
    |try solve_closed
    |iNext (* new goal *)].

217
Lemma tac_rel_case_inl_l `{logrelG Σ} Δ E1 Γ e e0 v e1 e2 K' t τ :
Dan Frumin's avatar
Dan Frumin committed
218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238
  e = fill K' (Case (InjL e0) e1 e2) 
  to_val e0 = Some v 
  Closed  e1 
  Closed  e2 
  (Δ   bin_log_related E1 E1 Γ (fill K' (e1 e0)) t τ) 
  (Δ  bin_log_related E1 E1 Γ e t τ).
Proof.
  intros ?????.
  subst e.
  rewrite -(bin_log_related_case_inl_l Γ E1); eassumption.
Qed.

Tactic Notation "rel_case_inl_l" :=
  iStartProof;
  eapply (tac_rel_case_inl_l);
    [tac_bind_helper (* e = fill K' ... *)
    |solve_to_val || fail 2 "In 'case: InjL v of ..' cannot show that 'v' is a value"
    |try solve_closed
    |try solve_closed
    |iNext (* new goal *)].

239
Lemma tac_rel_case_inr_l `{logrelG Σ} Δ E1 Γ e e0 v e1 e2 K' t τ :
Dan Frumin's avatar
Dan Frumin committed
240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260
  e = fill K' (Case (InjR e0) e1 e2) 
  to_val e0 = Some v 
  Closed  e1 
  Closed  e2 
  (Δ   bin_log_related E1 E1 Γ (fill K' (e2 e0)) t τ) 
  (Δ  bin_log_related E1 E1 Γ e t τ).
Proof.
  intros ?????.
  subst e.
  rewrite -(bin_log_related_case_inr_l Γ E1); eassumption.
Qed.

Tactic Notation "rel_case_inr_l" :=
  iStartProof;
  eapply (tac_rel_case_inr_l);
    [tac_bind_helper (* e = fill K' ... *)
    |solve_to_val || fail 2 "In 'case: InjR v of ..' cannot show that 'v' is a value"
    |try solve_closed
    |try solve_closed
    |iNext (* new goal *)].

261
Lemma tac_rel_binop_l `{logrelG Σ} Δ E1 Γ e K' op a b eres t τ :
262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278
  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 *)].

279
Lemma tac_rel_fork_l `{logrelG Σ} Δ1 E1 E2 e' K' Γ e t τ :
280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296
  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 *) ].

297
Lemma tac_rel_alloc_l `{logrelG Σ} nam nam_cl Δ1 Δ2 E1 E2 p i1 N P e' v' K' Γ e t τ :
298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340
  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') *)
341
    |solve_to_val (* to_val e' = Some v *)
342 343 344 345 346 347
    |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 *)].

348
Lemma tac_rel_alloc_l_simp `{logrelG Σ} Δ1 Δ2 E1 e' v' K' Γ e t τ :
349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364
  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' .. *)
365
    |solve_to_val (* to_val e' = Some v *)
366 367 368
    |iIntros (l) H (* new goal *)].


369
Lemma tac_rel_load_l `{logrelG Σ} nam nam_cl Δ1 Δ2 E1 E2 p i1 N P l K' Γ e t τ :
370 371 372 373 374 375 376 377 378
  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) 
Dan Frumin's avatar
Dan Frumin committed
379
      (l ↦ᵢ v - bin_log_related E2 E1 Γ (fill K' (of_val v)) t τ)) 
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
  (Δ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 *)].

419
Lemma tac_rel_load_l_simp `{logrelG Σ} Δ1 Δ2 E1 i1 l v K' Γ e t τ :
Dan Frumin's avatar
Dan Frumin committed
420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442
  e = fill K' (Load (Loc l)) 
  IntoLaterNEnvs 1 Δ1 Δ2 
  envs_lookup i1 Δ2 = Some (false, l ↦ᵢ v)%I 
  (Δ2  bin_log_related E1 E1 Γ (fill K' (of_val v)) t τ) 
  (Δ1  bin_log_related E1 E1 Γ e t τ).
Proof.
  intros ??? HΔ2.
  subst e.
  rewrite into_laterN_env_sound envs_lookup_split //; simpl.
  rewrite uPred.later_sep.
  rewrite HΔ2.
  apply uPred.wand_elim_l'.
  by rewrite -(bin_log_related_load_l' Γ E1).
Qed.

Tactic Notation "rel_load_l" :=
  iStartProof;
  eapply (tac_rel_alloc_l_simp);
    [tac_bind_helper (* e = fill K' .. *)
    |apply _  (* IntoLaterNenvs 1 Δ1 Δ2 *)
    |iAssumptionCore || fail 3 "rel_load_l: cannot find ? ↦ᵢ ?" 
    | (* new goal *)].

443
Lemma tac_rel_store_l `{logrelG Σ} nam nam_cl Δ1 Δ2 E1 E2 p i1 N P l e' v' K' Γ e t τ :
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 482 483 484 485 486 487
  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') *)
488
    |solve_to_val (* to_val e' = Some v *)
489 490 491 492 493 494
    |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 *)].

495

496
Lemma tac_rel_store_l_simp `{logrelG Σ} Δ1 Δ2 i1 E1 l v e' v' K' Γ e t τ :
497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517
  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' .. *)    
518
    |solve_to_val (* to_val e' = Some v' *)
519 520 521 522
    |iAssumptionCore || fail "rel_store_l: cannot find '? ↦ᵢ ?'"
    |env_cbv; reflexivity || fail "rel_store_l: this should not happen"
    | (* new goal *)].

523
Lemma tac_rel_cas_l `{logrelG Σ} nam nam_cl Δ1 Δ2 E1 E2 p i1 N P l e1 e2 v1 v2 K' Γ e t τ :
524 525 526 527 528 529 530 531 532 533 534
  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) 
Dan Frumin's avatar
Dan Frumin committed
535 536
     ((v  v1   (l ↦ᵢ v  - {E2,E1;Γ}  fill K' (# false) log t : τ)) 
      (v = v1   (l ↦ᵢ v2 - {E2,E1;Γ}  fill K' (# true) log t : τ)))) 
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
  (Δ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' ... *)
576 577
    |solve_to_val (* to_val e1 = Some .. *)
    |solve_to_val (* to_val e2 = Some .. *)
578 579 580 581 582 583 584 585 586
    |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 *)].


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

587
Lemma tac_rel_fork_r `{logrelG Σ} Δ1 E1 E2  t' K' Γ e t τ :
588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606
  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 *)].

607
Lemma tac_rel_store_r `{logrelG Σ} Δ1 Δ2 E1 E2 i1 l t' v' K' Γ e t τ v :
608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629
  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') *)
630
    |solve_to_val (* to_val e' = Some v *)
631 632 633 634
    |iAssumptionCore || fail "rel_store_l: cannot find ? ↦ₛ ?" 
    |env_cbv; reflexivity || fail "rel_store_r: this should not happen"
    |(* new goal *)].

635
Lemma tac_rel_alloc_r `{logrelG Σ} Δ1 E1 E2  t' v' K' Γ e t τ :
636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651
  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'"
652
    |solve_to_val (* to_val t' = Some v' *)
653 654 655 656 657 658 659
    |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.

660
Lemma tac_rel_load_r `{logrelG Σ} Δ1 Δ2 E1 E2 i1 l K' Γ e t τ v :
661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682
  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') *)
Dan Frumin's avatar
Dan Frumin committed
683
    |iAssumptionCore || fail "rel_load_r: cannot find ? ↦ₛ ?" 
684 685 686
    |env_cbv; reflexivity || fail "rel_load_r: this should not happen"
    |simpl (* new goal *)].

687
Lemma tac_rel_cas_fail_r `{logrelG Σ} Δ1 Δ2 E1 E2 i1 l K' Γ e t e1 e2 v1 v2 τ v :
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
  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 ..'"
713 714
    |solve_to_val
    |solve_to_val
715 716 717 718 719 720
    |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 *)].


721
Lemma tac_rel_cas_suc_r `{logrelG Σ} Δ1 Δ2 E1 E2 i1 l K' Γ e t e1 e2 v1 v2 τ v :
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
  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 ..'"
747 748
    |solve_to_val
    |solve_to_val
749 750 751 752 753
    |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 *)].

754

755
Lemma tac_rel_rec_r `{logrelG Σ} Δ E1 E2 Γ e K' f x ef e' efbody v eres t τ :
756
  nclose specN  E1 
757
  e = fill K' (App ef e') 
758 759 760
  ef = Rec f x efbody 
  Closed (x :b: f :b: ) efbody 
  to_val e' = Some v 
761
  eres = lamsubst ef v 
762 763 764
  (Δ  bin_log_related E1 E2 Γ t (fill K' eres) τ) 
  (Δ  bin_log_related E1 E2 Γ t e τ).
Proof.
765 766 767 768 769 770
  intros ????.
  subst ef. simpl.
  intros ??.
  subst e eres.  
  rewrite -(bin_log_related_rec_r Γ E1); try eassumption.
  destruct f; simpl; rewrite (of_to_val e' v); eauto.
771 772 773 774
Qed.

Tactic Notation "rel_rec_r" :=
  iStartProof;
775 776 777 778 779
  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' _ *)
780 781
        |solve_of_val_unlock
        |try solve_closed
782
        |solve_to_val (* to_val e' = Some v *)
783
        |try fast_done (* eres = subst ... *)
784
        |try simpl_subst/= (* new goal *)]
785 786
      end)
  || fail "rel_rec_r: cannot find '(λx.e) ..'".
787 788 789 790

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

791
Lemma tac_rel_fst_r `{logrelG Σ} Δ E1 E2 Γ e K' e1 e2 v1 v2 t τ :
792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811
  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' _ *)
812 813
    |solve_to_val (* to_val e1 = Some .. *)
    |solve_to_val (* to_val e2 = Some .. *)
814 815
    |simpl (* new goal *)].

816
Lemma tac_rel_snd_r `{logrelG Σ} Δ E1 E2 Γ e K' e1 e2 v1 v2 t τ :
817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836
  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' _ *)
837 838
    |solve_to_val (* to_val e1 = Some .. *)
    |solve_to_val (* to_val e2 = Some .. *)
839 840
    |simpl (* new goal *)].

841
Lemma tac_rel_tlam_r `{logrelG Σ} Δ E1 E2 Γ e K' e' t τ :
842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860
  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 *)].

861
Lemma tac_rel_fold_r `{logrelG Σ} Δ E1 E2 Γ e K' e' v t τ :
862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877
  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)'"
878
    |solve_to_val (* to_val e' = Some .. *)
879 880
    |simpl (* new goal *)].

881 882
Tactic Notation "rel_unfold_r" := rel_fold_r.

883
Lemma tac_rel_case_inl_r `{logrelG Σ} Δ E1 E2 Γ e K' e0 e1 e2 v t τ :
884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903
  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
904
    |solve_to_val (* to_val e0 = Some .. *)
905 906
    |simpl (* new goal *)].

907
Lemma tac_rel_case_inr_r `{logrelG Σ} Δ E1 E2 Γ e K' e0 e1 e2 v t τ :
908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927
  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
928
    |solve_to_val (* to_val e0 = Some .. *)
929 930 931 932
    |simpl (* new goal *)].

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

933
Lemma tac_rel_if_true_r `{logrelG Σ} Δ E1 E2 Γ e K' e1 e2 t τ :
934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954
  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 *)].

955
Lemma tac_rel_if_false_r `{logrelG Σ} Δ E1 E2 Γ e K' e1 e2 t τ :
956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976
  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 *)].

977
Lemma tac_rel_if_r `{logrelG Σ} Δ E1 E2 Γ e K' b eres e1 e2 t τ :
978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002
  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 *)].

1003
Lemma tac_rel_binop_r `{logrelG Σ} Δ E1 E2 Γ e K' op a b t τ :
1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022
  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 *)

1023 1024
Tactic Notation "rel_vals" :=
  iStartProof;
1025
  iApply bin_log_related_val; [ try solve_to_val | try solve_to_val | ];
1026 1027 1028
  let d := fresh "Δ" in
  iIntros (d); simpl.

1029
Section test.
1030
  Context `{logrelG Σ}.
1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059

  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.