rel_tactics.v 15.3 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 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 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 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 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 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
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.

Local Ltac tac_bind_helper :=
  rewrite ?fill_app /=;
  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.

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 *) 
  ].

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 *)].

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.


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;
  eapply (tac_rel_rec_l);
    [tac_bind_helper (* e = fill K' _ *)
    |try fast_done
    |solve_closed       
    |try fast_done (* to_val e' = Some v *)
    |try fast_done (* eres = subst ... *)
    |iNext; simpl; rewrite ?Closed_subst_id (* new goal *)].

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;
  eapply (tac_rel_rec_r);
    [solve_ndisj || fail "rel_rec_r: cannot prove 'nclose specN ⊆ ?'"
    |tac_bind_helper (* e = fill K' _ *)
    |simpl; fast_done
    |solve_closed
    |try fast_done (* to_val e' = Some v *)
    |try fast_done (* eres = subst ... *)
    |simpl; rewrite ?Closed_subst_id (* new goal *)].

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 *)

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.