lateearlychoice.v 7.62 KB
Newer Older
1 2 3 4
From iris.proofmode Require Import tactics.
From iris.algebra Require Import auth.
From iris.base_logic Require Import lib.auth.
From iris_logrel.F_mu_ref_conc Require Export examples.lock.
5
From iris_logrel.F_mu_ref_conc Require Import tactics rel_tactics soundness_binary relational_properties.
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
From iris.program_logic Require Import adequacy.

From iris_logrel.F_mu_ref_conc Require Import hax.

Definition rand : val := λ: <>,
  let: "y" := (ref (# false))
  in Fork ("y" <- # true);;
     !"y".
Definition lateChoice : val := λ: "x",
  "x" <- #n 0;; rand #().
Definition earlyChoice : val := λ: "x",
  let: "r" := rand #() in "x" <- #n 0;; "r".

Section Refinement.
  Context `{heapIG Σ, cfgSG Σ}.

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

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

  Lemma wp_rand :
    (WP rand #() {{ v, v = #v true  v = #v false}})%I.
  Proof.
    iStartProof.
    unfold rand. unlock.
    iApply wp_rec; eauto. solve_closed. iNext. simpl.
    wp_bind (Alloc _). iApply wp_alloc; auto. iNext. iIntros (y) "Hy".
    iMod (inv_alloc choiceN _ (y ↦ᵢ (#v false)  y ↦ᵢ (#v true))%I with "[Hy]") as "#Hinv"; eauto.
    iApply wp_rec; eauto. solve_closed. iNext. simpl.
    wp_bind (Fork _). iApply wp_fork. iNext.
    iSplitL.
    - iModIntro. iApply wp_rec; eauto. solve_closed. iNext; simpl.
      iInv choiceN as "[Hy | Hy]" "Hcl"; iApply (wp_load with "Hy"); eauto; iNext;
        iIntros "Hy"; iMod ("Hcl" with "[Hy]"); eauto.
    - iInv choiceN as "[Hy | Hy]" "Hcl"; iApply (wp_store with "Hy"); eauto; iNext;
        iIntros "Hy"; iMod ("Hcl" with "[Hy]"); eauto.
  Qed.

  Lemma rand_l Γ E1 K ρ t τ :
    choiceN  E1 
    spec_ctx ρ - ( b, {E1,E1;Γ}  fill K (# b) log t : τ)
    - {E1,E1;Γ}  fill K (rand #())%E log t : τ.
  Proof.
    iIntros (?) "#Hs Hlog".
    unfold rand at 1. unlock. simpl.
    iApply (bin_log_related_rec_l Γ E1 K BAnon BAnon _ #()%E); first done.
    iNext. simpl.
    rel_bind_l (Alloc _).
    iApply bin_log_related_alloc_l'; first eauto. iIntros (y) "Hy". simpl.
    iApply (bin_log_related_rec_l _ _ K); first eauto. iNext. simpl.
    iMod (inv_alloc choiceN _ ( b, y ↦ᵢ (#v b))%I with "[Hy]") as "#Hinv".
    { iNext. eauto. }
    rel_bind_l (Fork _).
    iApply bin_log_related_fork_l. iModIntro.
    iSplitR.
    - iNext.
      iInv choiceN as (b) "Hy" "Hcl".
      iApply (wp_store with "Hy"); eauto. iNext. iIntros "Hy".
      iMod ("Hcl" with "[Hy]").
      { iNext. iExists true. by iFrame. }
      done.
    - simpl.
      iApply (bin_log_related_rec_l _ _ K); first eauto. iNext. simpl.
      iApply (bin_log_related_load_l _ _ _ K).
      iInv choiceN as (b) "Hy" "Hcl". iModIntro.
      iExists (#v b). iFrame. iIntros "Hy".
      iMod ("Hcl" with "[Hy]").
      { iNext. iExists b. iFrame. }
      done. 
  Qed.
 
  Lemma lateChoice_l Γ x v ρ t :
    spec_ctx ρ - x ↦ᵢ v -
    (x ↦ᵢ (#nv 0) -  b, Γ  (# b) log t : TBool) -
    Γ  lateChoice #x log t : TBool.
  Proof.  
    iIntros "#Hs Hx Hlog".
    unfold lateChoice. unlock.
    iApply (bin_log_related_rec_l _ _ []); eauto. iNext. simpl. rewrite !Closed_subst_id.
    rel_bind_l (#x <- _)%E.
    iApply (bin_log_related_store_l' with "Hx"); eauto. iIntros "Hx".
    simpl.
    iApply (bin_log_related_rec_l _ _ []); eauto. iNext. simpl.
    
    unfold rand at 1. unlock.
    iApply (bin_log_related_rec_l _ _ []); eauto. iNext. simpl.
    rel_bind_l (Alloc _).
    iApply bin_log_related_alloc_l'; eauto. iIntros (y) "Hy". simpl.
    iApply (bin_log_related_rec_l _ _ []); eauto. iNext. simpl.
    
    iMod (inv_alloc choiceN _ ( b, y ↦ᵢ (#v b))%I with "[Hy]") as "#Hinv".
    { iNext. eauto. }
    rel_bind_l (Fork _).
    iApply bin_log_related_fork_l. iModIntro.
    iSplitR.
    - iNext.
      iInv choiceN as (b) "Hy" "Hcl".
      iApply (wp_store with "Hy"); eauto. iNext. iIntros "Hy".
      iMod ("Hcl" with "[Hy]").
      { iNext. iExists true. by iFrame. }
      done.
    - simpl.
      iApply (bin_log_related_rec_l _ _ []); eauto. iNext. simpl.
      iApply (bin_log_related_load_l _ _ _ []).
      iInv choiceN as (b) "Hy" "Hcl". iModIntro.
      iExists (#v b). iFrame. iIntros "Hy".
      iMod ("Hcl" with "[Hy]").
      { iNext. iExists b. iFrame. }
      simpl. iApply ("Hlog" with "Hx").
  Qed.
  
  Lemma prerefinement Γ x x' n ρ :
    (spec_ctx ρ - x ↦ᵢ (#nv n) - x' ↦ₛ (#nv n) -
      Γ  lateChoice #x log earlyChoice #x' : TBool)%I.
  Proof.
    iIntros "#Hspec Hx Hx'".
    unfold lateChoice, earlyChoice. unlock.
    iApply (bin_log_related_rec_l _ _ []); eauto. iNext. simpl. rewrite !Closed_subst_id.
    iApply (bin_log_related_rec_r _ _ _ []); eauto. simpl. rewrite !Closed_subst_id.

    rel_bind_l (#x <- _)%E.
    iApply (bin_log_related_store_l' with "Hx"); eauto. iIntros "Hx".
    simpl.
    iApply (bin_log_related_rec_l _ _ []); eauto. iNext. simpl.

    unfold rand at 1. unlock.
    iApply (bin_log_related_rec_l _ _ []); eauto. iNext. simpl.
    rel_bind_l (Alloc _).
    iApply bin_log_related_alloc_l'; eauto. iIntros (y) "Hy". simpl.
    iApply (bin_log_related_rec_l _ _ []); eauto. iNext. simpl.

    rel_bind_r (rand #())%E. unfold rand. unlock.
    iApply (bin_log_related_rec_r _ _); eauto. simpl.
    rel_bind_r (Alloc _).
    iApply bin_log_related_alloc_r; eauto. iIntros (y') "Hy'". simpl.
    rel_bind_r (App _ #y')%E.
    iApply (bin_log_related_rec_r _ _); eauto. simpl.

    iAssert (choice_inv y y') with "[Hy Hy']" as "Hinv".
    { iExists false. by iFrame. }
    iMod (inv_alloc choiceN with "[Hinv]") as "#Hinv".
    { iNext. iApply "Hinv". }
    rel_bind_r (Fork _).
    iApply bin_log_related_fork_r; eauto. iIntros (i) "Hi".

    rel_bind_l (Fork _).
    iApply bin_log_related_fork_l. iModIntro.
    iSplitL "Hi".
    - iNext.
      iInv choiceN as (f) "[Hy Hy']" "Hcl".
      iApply (wp_store with "Hy"); eauto. iNext. iIntros "Hy".
      tp_store i.
      iMod ("Hcl" with "[Hy Hy']").
      { iNext. iExists true. by iFrame. }
      done.
    - simpl.
      iApply (bin_log_related_rec_l _ _ []); eauto. iNext. simpl.
      rel_bind_r (App _ #())%E.
      iApply bin_log_related_rec_r; eauto. simpl.
      rel_bind_r (Load #y')%E.
      iApply (bin_log_related_load_l _ _ _ []).
      iInv choiceN as (f) "[Hy >Hy']" "Hcl". iModIntro.
      iExists (#v f). iFrame. iIntros "Hy".
      iApply (bin_log_related_load_r with "Hy'"). { solve_ndisj. }
      iIntros "Hy'". 
      iMod ("Hcl" with "[Hy Hy']").
      { iNext. iExists f. iFrame. }
      simpl.

      iApply (bin_log_related_rec_r _ _ _ []); eauto. simpl.
      rel_bind_r (Store _ _).
      iApply (bin_log_related_store_r with "Hx'"); eauto. iIntros "Hx'". simpl.
      iApply (bin_log_related_rec_r _ _ _ []); eauto. simpl.
      iApply bin_log_related_val; eauto.
      { iIntros (Δ). iModIntro. simpl. eauto. }
  Qed.

  Lemma refinement Γ ρ :
    (spec_ctx ρ -
      Γ  lateChoice log earlyChoice : TArrow (Tref TNat) TBool)%I.
  Proof.
    iIntros "#Hspec".
    unfold lateChoice in *. unfold earlyChoice in *. unlock.
    iApply bin_log_related_arrow.
    iAlways. iIntros (Δ (l,l')) "Hxx'". simpl.
    iDestruct "Hxx'" as ([x x']) "[% #Hxx']". inversion H1; subst. simpl.    
    replace (λ: "x", "x" <- Nat 0 ;; rand #())%E
      with (of_val lateChoice); last first.
    { unfold lateChoice. unlock. reflexivity. }
    replace (λ: "x", (λ: "r", "x" <- Nat 0 ;; "r") (rand #()))%E
      with (of_val earlyChoice); last first.
    { unfold earlyChoice. unlock. reflexivity. }
    Abort.
    (* iApply prerefinement; eauto. *)
    
End Refinement.