generative.v 14.2 KB
Newer Older
1 2 3 4 5
(** More generative ADT example from "State-Dependent
Represenation Independence" by A. Ahmed, D. Dreyer, A. Rossberg. *)
From iris.proofmode Require Import tactics.
From iris.algebra Require Import auth gset frac.
From iris.base_logic.lib Require Import auth.
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
From iris_logrel Require Import logrel examples.counter examples.lock prelude.bij.

(** * 5.2 References for name generation *)

Definition nameGenTy : type := TExists (TProd (TArrow TUnit (TVar 0))
                                              (TArrow (TVar 0) (TArrow (TVar 0) TBool))).

Definition nameGen1 : val :=
  PackV (λ: <>, ref #()
        ,λ: "y" "z", "y" = "z").

Definition nameGen2 : expr :=
  let: "x" := ref #0 in
  Pack (λ: <>, FG_increment "x" #();; !"x"
       ,λ: "y" "z", "y" = "z").

Lemma nameGen1_typed Γ :
  typed Γ nameGen1 nameGenTy.
Proof.
  unlock nameGen1 nameGenTy.
  apply TPack with (Tref TUnit). asimpl.
  solve_typed.
Qed.
Hint Resolve nameGen1_typed : typeable.

Lemma nameGen2_typed Γ :
  typed Γ nameGen2 nameGenTy.
Proof.
  unlock nameGen2 nameGenTy.
  econstructor. 2: solve_typed.
  econstructor. cbn.
  eapply TPack with TNat. asimpl.
  econstructor; solve_typed.
  econstructor. cbn.
  econstructor. cbn. solve_typed.
  econstructor; eauto; solve_typed.
  econstructor; eauto; solve_typed.
Qed.
Hint Resolve nameGen2_typed : typeable.

Section namegen_refinement.
  Context `{logrelG Σ, PrePBijG loc nat Σ}.
  Notation D := (prodC valC valC -n> iProp Σ).

  Program Definition ngR (γ : gname) : D := λne vv,
    ( (l : loc) (n : nat), vv.1 = #l%V  vv.2 = #n
    inBij γ l n)%I.
  Next Obligation. solve_proper. Qed.

  Instance ngR_persistent γ ww : PersistentP (ngR γ ww).
  Proof. apply _. Qed.

  Definition ng_Inv (γ : gname) (c : loc) : iProp Σ :=
    ( (n : nat) (L : gset (loc * nat)),
        BIJ γ L  c ↦ₛ #n
       [ set] lk  L, match lk with
                        | (l, k) => l ↦ᵢ #()  k  n
                        end)%I.

  Lemma nameGen_ref1 Γ :
    Γ  nameGen1 log nameGen2 : nameGenTy.
  Proof.
    iIntros (Δ).
    unlock nameGenTy nameGen1 nameGen2.
    rel_alloc_r as c "Hc". rel_let_r.
    iApply fupd_logrel'.
    iMod alloc_empty_bij as (γ) "HB".
    pose (N:=logrelN.@"ng").
    iMod (inv_alloc N _ (ng_Inv γ c) with "[-]") as "#Hinv".
    { iNext. iExists 0, . iFrame.
      by rewrite big_sepS_empty. }
    iModIntro.
    iApply (bin_log_related_pack _ (ngR γ)).
    iApply bin_log_related_pair.
    - (* New name *)
      iApply bin_log_related_arrow_val; eauto.
      iAlways.
      iIntros (? ?) "/= [% %]"; simplify_eq.
      rel_seq_l. rel_seq_r.
      rel_alloc_l_atomic.
      iInv N as (n L) "(HB & Hc & HL)" "Hcl".
      iModIntro. iNext. iIntros (l') "Hl'".
      rel_rec_r.
      rel_apply_r (bin_log_FG_increment_r with "Hc").
      { solve_ndisj. }
      iIntros "Hc".
      rel_seq_r.
      rel_load_r.
      iAssert (⌜¬( y, (l', y)  L))%I with "[HL Hl']" as %Hl'.
      { iIntros (Hy). destruct Hy as [y Hy].
        rewrite (big_sepS_elem_of _ L (l',y) Hy).
        iDestruct "HL" as "[Hl _]".
        iDestruct (mapsto_valid_2 with "Hl Hl'") as %Hfoo.
        compute in Hfoo. eauto. }
      iAssert (⌜¬( x, (x, S n)  L))%I with "[HL]" as %Hc.
      { iIntros (Hx). destruct Hx as [x Hy].
        rewrite (big_sepS_elem_of _ L (x,S n) Hy).
        iDestruct "HL" as "[_ %]". omega. }
      iApply fupd_logrel.
      iMod (bij_alloc_alt _ _ γ _ l' (S n) with "HB") as "[HB #Hl'n]"; auto.
      iMod ("Hcl" with "[-]").
      { iNext. iExists _,_; iFrame.
        rewrite big_sepS_insert; last by naive_solver.
        iFrame. iSplit; eauto.
        iApply (big_sepS_mono _ _ L L with "HL"); first reflexivity.
        intros [l x] Hlx. apply uPred.sep_mono_r, uPred.pure_mono. eauto. }
      iModIntro.
      rel_vals. iModIntro. iAlways.
      iExists _, _; eauto.
    - (* Name comparison *)
      iApply bin_log_related_arrow_val; eauto.
      iAlways.
      iIntros (? ?) "/= #Hv".
      iDestruct "Hv" as (l n) "(% & % & #Hln)". simplify_eq.
      rel_seq_l. rel_seq_r.
      iApply bin_log_related_arrow_val; eauto.
      iAlways.
      iIntros (? ?) "/= #Hv".
      iDestruct "Hv" as (l' n') "(% & % & #Hl'n')". simplify_eq.
      rel_seq_l. rel_seq_r.
      (* we would like to have an atomic version of `rel_op_l`? *)
      rel_bind_l (#l = #l')%E.
      iApply bin_log_related_wp_atomic_l.
      { solve_atomic. }
      iInv N as (m L) "(>HB & >Hc & HL)" "Hcl".
      iModIntro. wp_op.
      rel_op_r.
      iDestruct (bij_agree with "HB Hln Hl'n'") as %Hag.
      destruct (decide (l = l')) as [->|Hll].
      + assert (n = n') as -> by (apply Hag; auto).
        destruct (PeanoNat.Nat.eq_dec n' n'); last congruence.
        iMod ("Hcl" with "[-]") as "_".
        { iNext. iExists _,_; iFrame. }
        iApply bin_log_related_bool.
      + assert (n  n') as Hnn'.
        { intros Hnn. apply Hll. by apply Hag. }
        destruct (PeanoNat.Nat.eq_dec n n'); first congruence.
        iMod ("Hcl" with "[-]") as "_".
        { iNext. iExists _,_; iFrame. }
        iApply bin_log_related_bool.
  Qed.

End namegen_refinement.
149 150 151 152 153 154 155 156 157 158 159 160 161 162

(** * 5.4 Cell class *)
Definition cellτ : type :=
  TForall (TExists (TProd (TProd (TArrow (TVar 1) (TVar 0))
                                 (TArrow (TVar 0) (TVar 1)))
                                 (TArrow (TVar 0) (TArrow (TVar 1) TUnit)))).
Definition cell1 : val :=
  Λ: Pack (λ: "x", ref "x", λ: "r", !"r", λ: "r" "x", "r" <- "x").
Lemma cell1_typed Γ :
  typed Γ cell1 cellτ.
Proof.
  unfold cellτ. unlock cell1.
  solve_typed.
Qed.
163
Hint Resolve cell1_typed : typeable.
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

Definition cell2 : val :=
  Λ: let: "l" := newlock #() in
    Pack ( λ: "x", acquire "l";; let: "v" := (ref #false, ref "x", ref "x") in
                   release "l";; "v"
        ,  λ: "r", acquire "l";;
                   let: "v" :=
                      if: !(Fst (Fst "r"))
                      then !(Snd "r")
                      else !(Snd (Fst "r")) in
                   release "l";;
                   "v"
         , λ: "r" "x", acquire "l";;
                       (if: !(Fst (Fst "r"))
                        then (Snd (Fst "r")) <- "x";; (Fst (Fst "r")) <- #false
                        else (Snd "r") <- "x";; (Fst (Fst "r")) <- #true);;
                       release "l").
Lemma cell2_typed Γ :
  typed Γ cell2 cellτ.
Proof.
  unfold cellτ. unlock cell2.
  solve_typed.
  econstructor.
  econstructor. 2: solve_typed.
  econstructor.
  eapply TPack with (TProd (TProd (Tref TBool) (Tref (TVar 0))) (Tref (TVar 0))).
  asimpl.
  econstructor; solve_typed.
  econstructor; solve_typed.
Qed.
194
Hint Resolve cell2_typed : typeable.
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

Definition refPool := gset ((loc * loc * loc) * loc).
Definition refPoolR := gsetUR ((loc * loc * loc) * loc).

Class refPoolG Σ := RefPoolG
{ refPool_inG :> authG Σ refPoolR }.

Section cell_refinement.
  Context `{logrelG Σ, lockG Σ, refPoolG Σ}.
  Notation D := (prodC valC valC -n> iProp Σ).

  Definition cellInt (R : D) (r1 r2 r3 r : loc) : iProp Σ :=
    ( (a b c : val), r ↦ₛ a  r2 ↦ᵢ b  r3 ↦ᵢ c 
    ( (r1 ↦ᵢ #true  R (c, a))
     (r1 ↦ᵢ #false  R (b, a))))%I.

  Definition cellRegInv (P : refPool) (R : D) : iProp Σ :=
    ([ set] rs  P, match rs with
                      | ((r1, r2, r3), r) => cellInt R r1 r2 r3 r
                     end)%I.

  Definition cellInv γ R : iProp Σ :=
    ( (P : refPool), own γ ( P)  cellRegInv P R)%I.

  Program Definition cellR (γ : gname) : D := λne vv,
    ( r1 r2 r3 r : loc, vv.1 = (#r1, #r2, #r3)%V  vv.2 = #r
    own γ ( {[(r1, r2, r3), r]}))%I.
  Next Obligation. solve_proper. Qed.

  Instance cellR_persistent γ ww : PersistentP (cellR γ ww).
  Proof. apply _. Qed.

  Lemma cellRegInv_excl (P : refPool) R (r1 r2 r3 r : loc) (v : val) :
    cellRegInv P R - r1 ↦ᵢ v - (r1, r2, r3, r)  P.
  Proof.
    rewrite /cellRegInv.
    iIntros "HP Hr1".
    iAssert ((r1, r2, r3, r)  P  False)%I as %Hbaz.
    {
      iIntros (HP).
      rewrite (big_sepS_elem_of _ P _ HP).
      iDestruct "HP" as (a b c) "(Hr & Hr2 & Hr3 & Hrs)".
      iDestruct "Hrs" as "[[Hr1' ?]|[Hr1' ?]]";
      iDestruct (mapsto_valid_2 r1 with "Hr1' Hr1") as %Hfoo;
      compute in Hfoo; contradiction.
    }
    iPureIntro. eauto.
  Qed.

  Lemma cellRegInv_open (P : refPool) R (r1 r2 r3 r : loc) :
    (r1, r2, r3, r)  P 
    cellRegInv P R -
    (cellInt R r1 r2 r3 r)  (cellInt R r1 r2 r3 r - cellRegInv P R).
  Proof.
    iIntros (Hrin) "Hreg".
    rewrite /cellRegInv.
    iDestruct (big_sepS_elem_of_acc _ P (r1, r2, r3, r) with "Hreg") as "[Hrs Hreg]"; first assumption.
    by iFrame.
  Qed.

  Lemma refPool_alloc γ (P : refPool) (r1 r2 r3 r : loc) :
    (r1, r2, r3, r)  P 
    own γ ( P)
    == own γ ( ({[(r1, r2, r3, r)]}  P))
       own γ ( ({[(r1, r2, r3, r)]})).
  Proof.
    iIntros (Hin) "HP".
    iMod (own_update with "HP") as "[HP Hfrag]".
    { eapply auth_update_alloc.
      eapply (gset_local_update _ _ ({[(r1, r2, r3, r)]}  P)).
      apply union_subseteq_r. }
    iFrame.
    rewrite -gset_op_union auth_frag_op.
    by iDestruct "Hfrag" as "[$ _]".
  Qed.

  Lemma refPool_lookup γ (P : refPool) (r1 r2 r3 r : loc) :
    own γ ( P) -
    own γ ( {[(r1, r2), r3, r]}) -
    (r1, r2, r3, r)  P.
  Proof.
    iIntros "Ho Hrs".
    iDestruct (own_valid_2 with "Ho Hrs") as %Hfoo.
    iPureIntro.
    apply auth_valid_discrete in Hfoo.
    simpl in Hfoo. destruct Hfoo as [Hfoo _].
    revert Hfoo. rewrite left_id.
    by rewrite gset_included elem_of_subseteq_singleton.
  Qed.

  Lemma cell2_cell1_refinement Γ :
    Γ  cell2 log cell1 : cellτ.
  Proof.
    iIntros (Δ).
    unlock cell2 cell1 cellτ.
    pose (N:=logrelN.@"locked").
    assert (Closed (dom _ Γ) (let: "l" := newlock #() in
        Pack
          (λ: "x", acquire "l";; let: "v" := (ref #false, ref "x", ref "x") in
                   release "l";; "v",
          λ: "r",
            acquire "l" ;;
            let: "v" := if: ! (Fst (Fst "r")) then
                        ! (Snd "r") else ! (Snd (Fst "r")) in
            release "l" ;; "v",
          λ: "r" "x",
            acquire "l" ;;
            (if: ! (Fst (Fst "r"))
             then Snd (Fst "r") <- "x" ;; Fst (Fst "r") <- #false
             else Snd "r" <- "x" ;;
                  Fst (Fst "r") <- #true) ;; release "l"))).
    { apply Closed_mono with ; eauto.
      set_solver. }
    assert (Closed (dom _ Γ) (Pack (λ: "x", ref "x", λ: "r", ! "r", λ: "r" "x", "r" <- "x"))).
    { apply Closed_mono with ; eauto.
      set_solver. }
    iApply bin_log_related_tlam; auto.
    iIntros (R HR) "!#".
    iApply fupd_logrel'.
    iMod (own_alloc ( ( : refPoolR))) as (γ) "Ho"; first done.
    iModIntro.
    rel_apply_l (bin_log_related_newlock_l N (cellInv γ R)%I with "[Ho]").
    { iExists _. iFrame. unfold cellRegInv.
      by rewrite big_sepS_empty. }
    iIntros (lk γl) "#Hlk".
    rel_let_l.
    iApply (bin_log_related_pack _ (cellR γ)).
    repeat iApply bin_log_related_pair.
    - (* New cell *)
      iApply bin_log_related_arrow_val; eauto.
      iAlways. iIntros (v1 v2) "/= #Hv".
      rel_let_l. rel_let_r.
      rel_apply_l (bin_log_related_acquire_l N _ lk with "Hlk"); first auto.
      iIntros "Hl".
      iDestruct 1 as (P) "[Ho HP]".
      rel_let_l.
      rel_alloc_l as r1 "Hr1".
      rel_alloc_l as r2 "Hr2".
      rel_alloc_l as r3 "Hr3".
      rel_alloc_r as r "Hr".
      rel_let_l.
      iDestruct (cellRegInv_excl with "HP Hr1") as %Hrs.
      iApply fupd_logrel'.
      iMod (refPool_alloc γ P r1 r2 r3 r with "Ho") as "[Ho #Hrs]"; first apply Hrs.
      iModIntro.
      rel_apply_l (bin_log_related_release_l with "Hlk Hl [-Hrs]"); first auto.
      { iExists _; iFrame. rewrite {2}/cellRegInv.
        rewrite big_sepS_insert; last assumption.
        iFrame. iExists _,_,_. iFrame. iRight; by iFrame. }
      rel_let_l.
      rel_vals. iModIntro. iAlways. iExists _,_,_,_. eauto.
    - (* Read cell *)
      iApply bin_log_related_arrow_val; eauto.
      iAlways. iIntros (v1 v2) "/=".
      iDestruct 1 as (r1 r2 r3 r) "[% [% #Hrs]]". simplify_eq.
      rel_let_l.
      rel_apply_l (bin_log_related_acquire_l N _ lk with "Hlk"); first auto.
      iIntros "Hl".
      iDestruct 1 as (P) "[Ho HP]".
      rel_let_l. repeat rel_proj_l.
      iDestruct (refPool_lookup with "Ho Hrs") as %Hrin.
      iDestruct (cellRegInv_open with "HP") as "[Hr HclP]"; first apply Hrin.
      iDestruct "Hr" as (a b c) "(Hr & Hr2 & Hr3 & [[Hr1 #HR] | [Hr1 #HR]])";
        rel_load_l; rel_if_l; repeat rel_proj_l; rel_load_l; rel_let_l;
        rel_let_r; rel_load_r.
      + rel_apply_l (bin_log_related_release_l with "Hlk Hl [-]"); auto.
        { iExists _; iFrame. iApply "HclP".
          iExists _,_,_; iFrame. iLeft; by iFrame. }
        rel_let_l. rel_vals; eauto.
      + rel_apply_l (bin_log_related_release_l with "Hlk Hl [-]"); auto.
        { iExists _; iFrame. iApply "HclP".
          iExists _,_,_; iFrame. iRight; by iFrame. }
        rel_let_l. rel_vals; eauto.
    - (* Insert cell *)
      iApply bin_log_related_arrow_val; eauto.
      iAlways. iIntros (v1 v2) "/=".
      iDestruct 1 as (r1 r2 r3 r) "[% [% #Hrs]]". simplify_eq.
      rel_let_l. rel_let_r.
      iApply bin_log_related_arrow_val; eauto.
      iAlways. iIntros (v1 v2) "/= #Hv".
      rel_let_l. rel_let_r.
      rel_apply_l (bin_log_related_acquire_l N _ lk with "Hlk"); first auto.
      iIntros "Hl".
      iDestruct 1 as (P) "[Ho HP]".
      rel_let_l. repeat rel_proj_l.
      iDestruct (refPool_lookup with "Ho Hrs") as %Hrin.
      iDestruct (cellRegInv_open with "HP") as "[Hr HclP]"; first apply Hrin.
      iDestruct "Hr" as (a b c) "(Hr & Hr2 & Hr3 & [[Hr1 #HR] | [Hr1 #HR]])";
        rel_load_l; rel_if_l; repeat rel_proj_l;
        rel_store_l; rel_let_l; repeat rel_proj_l;
        rel_store_l; rel_store_r; rel_let_l.
      + rel_apply_l (bin_log_related_release_l with "Hlk Hl [-]"); auto.
        { iExists _; iFrame. iApply "HclP".
          iExists _,_,_; iFrame. iRight; by iFrame. }
        iApply bin_log_related_unit.
      + rel_apply_l (bin_log_related_release_l with "Hlk Hl [-]"); auto.
        { iExists _; iFrame. iApply "HclP".
          iExists _,_,_; iFrame. iLeft; by iFrame. }
        iApply bin_log_related_unit.
    Qed.
End cell_refinement.