ticket_lock.v 18.5 KB
Newer Older
Dan Frumin's avatar
Dan Frumin committed
1
From iris.proofmode Require Import tactics.
Dan Frumin's avatar
Dan Frumin committed
2
From iris.algebra Require Export auth gset excl.
Dan Frumin's avatar
Dan Frumin committed
3
From iris.base_logic Require Import auth.
4
From iris_logrel Require Export logrel examples.lock examples.counter.
Dan Frumin's avatar
Dan Frumin committed
5 6 7 8 9 10 11 12 13 14

Definition wait_loop: val :=
  rec: "wait_loop" "x" "lk" :=
    if: "x" = !(Fst "lk")
      then #() (* my turn *)
      else "wait_loop" "x" "lk".

Definition newlock : val :=
  λ: <>, ((* owner *) ref #0, (* next *) ref #0).

15 16 17
Definition acquire : val := λ: "lk",
  let: "n" := FG_increment (Snd "lk") #() in
  wait_loop "n" "lk".
Dan Frumin's avatar
Dan Frumin committed
18

19 20 21
Definition wkincr : val := λ: "l",
  "l" <- !"l" + #1.
Definition release : val := λ: "lk", wkincr (Fst "lk").
Dan Frumin's avatar
Dan Frumin committed
22

23
Definition LockType : type := ref TNat × ref TNat.
Dan Frumin's avatar
Dan Frumin committed
24 25 26

Hint Unfold LockType : typeable.

27
Lemma newlock_type Γ : typed Γ newlock (Unit  LockType).
Dan Frumin's avatar
Dan Frumin committed
28 29 30 31
Proof. solve_typed. Qed.

Hint Resolve newlock_type : typeable.

32
Lemma acquire_type Γ : typed Γ acquire (LockType  TUnit).
33 34 35 36 37 38 39
Proof.
  unlock acquire wait_loop.
  econstructor; cbn; solve_typed.
  econstructor; cbn; solve_typed.
  econstructor; cbn; solve_typed.
  econstructor; cbn; solve_typed.
Qed.
Dan Frumin's avatar
Dan Frumin committed
40 41 42

Hint Resolve acquire_type : typeable.

43
Lemma release_type Γ : typed Γ release (LockType  TUnit).
44
Proof. unlock release wkincr. solve_typed. Qed.
Dan Frumin's avatar
Dan Frumin committed
45 46 47

Hint Resolve release_type : typeable.

48 49
Definition lockT : type := : (Unit  TVar 0) × (TVar 0  Unit) × (TVar 0  Unit).
Lemma ticket_lock_typed Γ : typed Γ (Pack (newlock, acquire, release)) lockT.
Dan Frumin's avatar
Dan Frumin committed
50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67
Proof.
  apply TPack with LockType.
  asimpl. solve_typed.
Qed.

Class tlockG Σ :=
  tlock_G :> authG Σ (gset_disjUR nat).
Definition tlockΣ : gFunctors :=
  #[ authΣ (gset_disjUR nat) ].

Definition lockPool := gset ((loc * loc * gname) * loc).
Definition lockPoolR := gsetUR ((loc * loc * gname) * loc).

Class lockPoolG Σ :=
  lockPool_inG :> authG Σ lockPoolR.
Section refinement.
  Context `{logrelG Σ, tlockG Σ, lockPoolG Σ}.

68
  (** * Basic abstractions around the concrete RA *)
Dan Frumin's avatar
Dan Frumin committed
69

70 71 72 73 74 75 76 77
  (** ticket with the id `n` *)
  Definition ticket (γ : gname) (n : nat) := own γ ( GSet {[ n ]}).
  (** total number of issued tickets is `n` *)
  Definition issuedTickets (γ : gname) (n : nat) := own γ ( GSet (seq_set 0 n)).
  (** the locks `(ln, lo)` and `l'` are linked together in the pool P` *)
  Definition inPool (γP : gname) (lo ln : loc) (γ : gname) (l' : loc) := own γP ( {[(lo, ln, γ), l']}).
  (** the set `P` is in fact the lock pool associated with P` *)
  Definition isPool (γP : gname) (P : lockPool) := own γP ( P).
Dan Frumin's avatar
Dan Frumin committed
78

79 80 81 82 83 84
  Lemma ticket_nondup γ n : ticket γ n - ticket γ n - False.
  Proof.
    iIntros "Ht1 Ht2".
    iDestruct (own_valid_2 with "Ht1 Ht2") as %?%gset_disj_valid_op.
    set_solver.
  Qed.
Dan Frumin's avatar
Dan Frumin committed
85

86 87
  Lemma newIssuedTickets : (|==>  γ, issuedTickets γ 0)%I.
  Proof. iMod (own_alloc ( (GSet ))) as (γ) "Hγ"; [done|eauto]. Qed.
Dan Frumin's avatar
Dan Frumin committed
88

89 90 91
  Lemma issueNewTicket γ m :
    issuedTickets γ m ==
    issuedTickets γ (S m)  ticket γ m.
Dan Frumin's avatar
Dan Frumin committed
92
  Proof.
93 94 95 96 97 98
    iIntros "Hseq".
    iMod (own_update with "Hseq") as "[Hseq Hticket]".
    { eapply auth_update_alloc.
      eapply (gset_disj_alloc_empty_local_update _ {[ m ]}).
      apply (seq_set_S_disjoint 0). }
    rewrite -(seq_set_S_union_L 0).
Dan Frumin's avatar
Dan Frumin committed
99 100 101
    by iFrame.
  Qed.

102 103 104 105 106 107
  Instance inPool_persistent γP lo ln γ l' : Persistent (inPool γP lo ln γ l').
  Proof. apply _. Qed.

  Lemma inPool_lookup γP lo ln γ l' P :
    inPool γP lo ln γ l' - isPool γP P -
    (lo, ln, γ, l')  P.
Dan Frumin's avatar
Dan Frumin committed
108
  Proof.
109
    iIntros "Hrs Ho".
Dan Frumin's avatar
Dan Frumin committed
110 111 112 113 114 115 116 117
    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.

118 119 120 121 122 123 124 125 126 127 128 129 130 131 132
  Lemma isPool_insert γP lo ln γ l' P :
    isPool γP P ==
    inPool γP lo ln γ l'  isPool γP ({[(lo, ln, γ, l')]}  P).
  Proof.
    iIntros "HP".
    iMod (own_update with "HP") as "[HP Hls]".
    { eapply auth_update_alloc.
      eapply (gset_local_update _ _ ({[(lo, ln, γ, l')]}  P)).
      apply union_subseteq_r. }
    iFrame "HP".
    rewrite -gset_op_union.
    by iDestruct "Hls" as "[#Hls _]".
  Qed.

  Lemma newIsPool (P : lockPool) : (|==>  γP, isPool γP P)%I.
Dan Frumin's avatar
Dan Frumin committed
133
  Proof.
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
    apply (own_alloc ( (P : lockPoolR))).
    by apply auth_auth_valid.
  Qed.

  Instance isPool_timeless γP P : Timeless (isPool γP P).
  Proof. apply _. Qed.
  Instance inPool_timeless γP lo ln γ l' : Timeless (inPool γP lo ln γ l').
  Proof. apply _. Qed.
  Instance ticket_timeless γ n : Timeless (ticket γ n).
  Proof. apply _. Qed.
  Instance issuedTickets_timeless γ n : Timeless (issuedTickets γ n).
  Proof. apply _. Qed.

  Opaque ticket issuedTickets inPool isPool.

  (** * Invariants and abstracts for them *)
  Definition lockInv (lo ln : loc) (γ : gname) (l' : loc) : iProp Σ :=
    ( (o n : nat) (b : bool), lo ↦ᵢ #o  ln ↦ᵢ #n
    issuedTickets γ n  l' ↦ₛ #b
    if b then ticket γ o else True)%I.

  Instance ifticket_timeless (b : bool) γ o : Timeless (if b then ticket γ o else True%I).
  Proof. destruct b; apply _. Qed.
  Instance lockInv_timeless lo ln γ l' : Timeless (lockInv lo ln γ l').
  Proof. apply _. Qed.

  Definition lockPoolInv (P : lockPool) : iProp Σ :=
    ([ set] rs  P, match rs with
                     | ((lo, ln, γ), l') => lockInv lo ln γ l'
                     end)%I.

  Instance lockPoolInv_timeless P : Timeless (lockPoolInv P).
  Proof.
    apply big_sepS_timeless.
    intros [[[? ?] ?] ?]. apply _.
  Qed.

  Lemma lockPoolInv_empty : lockPoolInv .
  Proof. by rewrite /lockPoolInv big_sepS_empty. Qed.

  Lemma lockPool_open γP (P : lockPool) (lo ln : loc) (γ : gname) (l' : loc) :
    isPool γP P -
    inPool γP lo ln γ l' -
    lockPoolInv P -
    isPool γP P  (lockInv lo ln γ l')  (lockInv lo ln γ l' - lockPoolInv P).
  Proof.
    iIntros "HP #Hin HPinv".
    iDestruct (inPool_lookup with "Hin HP") as %Hin.
Dan Frumin's avatar
Dan Frumin committed
182
    rewrite /lockPoolInv.
183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198
    iDestruct (big_sepS_elem_of_acc _ P _ with "HPinv") as "[Hrs Hreg]"; first apply Hin.
    by iFrame.
  Qed.

  Lemma lockPool_insert γP (P : lockPool) (lo ln : loc) γ l' :
    isPool γP P -
    lockPoolInv P -
    lockInv lo ln γ l' ==
    isPool γP ({[(lo, ln, γ, l')]}  P)
     lockPoolInv ({[(lo, ln, γ, l')]}  P)
     inPool γP lo ln γ l'.
  Proof.
    iIntros "HP HPinv".
    iDestruct 1 as (n o b) "(Hlo & Hln & Hissued & Hl' & Hticket)".
    iMod (isPool_insert γP lo ln γ l' P with "HP") as "[$ $]".
    rewrite /lockInv.
Dan Frumin's avatar
Dan Frumin committed
199
    iAssert ((lo, ln, γ, l')  P  False)%I as %Hbaz.
200 201
    { iIntros (HP).
      rewrite /lockPoolInv.
Dan Frumin's avatar
Dan Frumin committed
202
      rewrite (big_sepS_elem_of _ P _ HP).
203 204 205 206 207 208
      iDestruct "HPinv" as (? ? ?) "(Hlo' & Hln' & ?)".
      iDestruct (mapsto_valid_2 with "Hlo' Hlo") as %Hfoo.
      compute in Hfoo; contradiction. }
    rewrite /lockPoolInv.
    rewrite big_sepS_insert; last assumption.
    iFrame. iExists _,_,_. by iFrame.
Dan Frumin's avatar
Dan Frumin committed
209 210
  Qed.

211 212 213 214 215 216 217 218 219 220 221 222 223 224
  Opaque lockPoolInv.

  Definition moduleInv γp : iProp Σ :=
    ( (P : lockPool), isPool γp P  lockPoolInv P)%I.

  Program Definition lockInt (γp : gname) := λne vv,
    ( (lo ln : loc) (γ : gname) (l' : loc),
        vv.1 = (#lo, #ln)%V  vv.2 = #l'⌝
       inPool γp lo ln γ l')%I.
  Next Obligation. solve_proper. Qed.

  Instance lockInt_persistent γp ww : Persistent (lockInt γp ww).
  Proof. apply _. Qed.

225
  (** * Refinement proofs *)
226 227
  Definition N := logrelN.@"locked".

228 229 230 231
  Local Ltac openI N := iInv N as (P) ">[HP HPinv]" "Hcl".
  Local Ltac closeI := iMod ("Hcl" with "[-]") as "_";
    first by (iNext; iExists _; iFrame).

232 233 234 235 236 237 238 239 240 241
  (* Allocating a new lock *)
  Lemma newlock_refinement Δ Γ γp:
    inv N (moduleInv γp) -
    {(lockInt γp :: Δ); ⤉Γ}  newlock log lock.newlock : (Unit  TVar 0).
  Proof.
    iIntros "#Hinv".
    unlock newlock.
    iApply bin_log_related_arrow_val; eauto.
    { by unlock lock.newlock. }
    iAlways. iIntros (? ?) "/= [% %]"; simplify_eq.
242
    (* Reducing to a value on the LHS *)
243 244
    rel_let_l.
    rel_alloc_l as lo "Hlo".
245 246
    rel_alloc_l as ln "Hln".
    (* Reducing to a value on the RHS *)
247 248 249
    rel_apply_r bin_log_related_newlock_r.
    { solve_ndisj. }
    iIntros (l') "Hl'".
250 251 252 253 254 255 256 257
    (* Establishing the invariant *)
    openI N.
    iMod newIssuedTickets as (γ) "Hγ".
    iMod (lockPool_insert _ _ lo ln with "HP HPinv [Hlo Hln Hl' Hγ]") as "(HP & HPinv & #Hin)".
    { iExists _,_,_; by iFrame. }
    closeI.
    rel_vals; iModIntro; iAlways.
    iExists _,_,_,_. by iFrame "Hin".
258 259 260
  Qed.

  (* Acquiring a lock *)
261
  (* helper lemma *)
262 263
  Lemma wait_loop_refinement Δ Γ γp (lo ln : loc) γ (l' : loc) (m : nat) :
    inv N (moduleInv γp) -
264 265
    inPool γp lo ln γ l' - (* two locks are linked *)
    ticket γ m -
266 267 268
    {(lockInt γp :: Δ); ⤉Γ} 
      wait_loop #m (#lo, #ln) log lock.acquire #l' : TUnit.
  Proof.
269
    iIntros "#Hinv #Hin Hticket".
270 271
    rel_rec_l.
    iLöb as "IH".
272
    unlock {2}wait_loop. simpl.
273 274
    rel_let_l. rel_proj_l.
    rel_load_l_atomic.
275 276
    openI N.
    iDestruct (lockPool_open with "HP Hin HPinv") as "(HP & Hls & HPinv)".
277
    rewrite {1}/lockInv.
278
    iDestruct "Hls" as (o n b) "(Hlo & Hln & Hissued & Hl' & Hrest)".
279 280 281 282 283 284
    iModIntro. iExists _; iFrame; iNext.
    iIntros "Hlo".
    rel_op_l.
    case_decide; subst; rel_if_l.
    (* Whether the ticket is called out *)
    - destruct b.
285
      { iDestruct (ticket_nondup with "Hticket Hrest") as %[]. }
286 287 288
      rel_apply_r (bin_log_related_acquire_r with "Hl'").
      { solve_ndisj. }
      iIntros "Hl'".
289 290 291
      iSpecialize ("HPinv" with "[Hlo Hln Hl' Hissued Hticket]").
      { iExists _,_,_. by iFrame. }
      closeI.
292 293 294
      iApply bin_log_related_unit.
    - iMod ("Hcl" with "[-Hticket]") as "_".
      { iNext. iExists P; iFrame.
295
        iApply "HPinv". iExists _,_,_; by iFrame. }
296
      rel_rec_l.
297
      unlock wait_loop. simpl_subst/=. by iApply "IH".
298 299
  Qed.

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
  (** Logically atomic spec for `acquire`.
      Parameter type: nat
      Precondition:
        λo,  n, lo ↦ᵢ o  ln ↦ᵢ n  issuedTickets γ n
      Postcondition:
        λo,  n, lo ↦ᵢ o  ln ↦ᵢ n  issuedTickets γ n  ticket γ o *)
  Lemma acquire_l_logatomic R P γ Δ Γ E1 E2 K lo ln t τ :
    P -
     (|={E1,E2}=>  o n : nat, lo ↦ᵢ #o  ln ↦ᵢ #n  issuedTickets γ n  R o 
       (( o n : nat, lo ↦ᵢ #o  ln ↦ᵢ #n  issuedTickets γ n  R o) ={E2,E1}= True) 
        ( o n : nat, lo ↦ᵢ #o  ln ↦ᵢ #n  issuedTickets γ n  ticket γ o  R o - P -
            {E2,E1;Δ;Γ}  fill K #() log t : τ))
    - ({E1;Δ;Γ}  fill K (acquire (#lo, #ln)) log t : τ).
  Proof.
    iIntros "HP #H".
    rewrite /acquire. unlock. simpl.
    rel_rec_l. rel_proj_l.
    rel_bind_l ((FG_increment #ln) #())%E.
    rel_rec_l.
    rel_apply_l (bin_log_FG_increment_logatomic _ (fun n : nat =>  o : nat, lo ↦ᵢ #o  issuedTickets γ n  R o)%I P%I with "HP").
    iAlways.
    iPoseProof "H" as "H2".
    iMod "H" as (o n) "(Hlo & Hln & Hissued & HR & Hrest)". iModIntro.
    iExists _; iFrame.
    iSplitL "Hlo HR".
    { iExists _. iFrame. }
    iSplit.
    - iDestruct "Hrest" as "[H _]".
      iDestruct 1 as (n') "[Hln Ho]".
      iDestruct "Ho" as (o') "[Ho HR]".
      iApply "H".
      iExists _, _. iFrame.
    - iDestruct "Hrest" as "[H _]".
      iIntros (n') "[Hln Ho] HP".
      iDestruct "Ho" as (o') "[Ho [Hissued HR]]".
      iMod (issueNewTicket with "Hissued") as "[Hissued Hm]".
      iMod ("H" with "[-HP Hm]") as "_".
      { iExists _,_. iFrame. }
      rel_let_l. clear o n o'.
      rel_rec_l.
      iLöb as "IH".
      unlock wait_loop. simpl.
      rel_rec_l. rel_proj_l.
      rel_load_l_atomic.
      iMod "H2" as (o n) "(Hlo & Hln & Hissued & HR & Hrest)". iModIntro.
      iExists _. iFrame. iNext. iIntros "Hlo".
      rel_op_l.
      case_decide; subst; rel_if_l.
      (* Whether the ticket is called out *)
      + iDestruct "Hrest" as "[_ H]".
        iApply ("H" with "[-HP] HP").
        { iFrame. }
      + iDestruct "Hrest" as "[H _]".
        iMod ("H" with "[-HP Hm]") as "_".
        { iExists _,_; iFrame. }
        rel_rec_l. iApply ("IH" with "HP Hm").
  Qed.

358 359 360
  Lemma acquire_refinement Δ Γ γp :
    inv N (moduleInv γp) -
    {(lockInt γp :: Δ); ⤉Γ}  acquire log lock.acquire : (TVar 0  Unit).
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
  Proof.
    iIntros "#Hinv".
    iApply bin_log_related_arrow_val; eauto.
    { by unlock acquire. }
    { by unlock lock.acquire. }
    iAlways. iIntros (? ?) "/= #Hl".
    iDestruct "Hl" as (lo ln γ l') "(% & % & Hin)". simplify_eq/=.
    rel_apply_l (acquire_l_logatomic
                   (fun o =>  (b : bool),
                             l' ↦ₛ #b 
                             if b then ticket γ o else True)%I
                   True%I γ); first done.
    iAlways.
    openI N.
    iDestruct (lockPool_open with "HP Hin HPinv") as "(HP & Hls & HPinv)".
    rewrite {1}/lockInv.
    iDestruct "Hls" as (o n b) "(Hlo & Hln & Hissued & Hl' & Hticket)".
    iModIntro. iExists _,_; iFrame.
    iSplitL "Hticket Hl'".
    { iExists _. iFrame. }
    clear b o n.
    iSplit.
    - iDestruct 1 as (o' n') "(Hlo & Hln & Hissued & Hrest)".
      iDestruct "Hrest" as (b) "[Hl' Ht]".
      iApply ("Hcl" with "[-]").
      iNext. iExists P; iFrame.
      iApply "HPinv". iExists _,_,_. by iFrame.
    - iIntros (o n) "(Hlo & Hln & Hissued & Ht & Hrest) _".
      iDestruct "Hrest" as (b) "[Hl' Ht']".
      destruct b.
      { iDestruct (ticket_nondup with "Ht Ht'") as %[]. }
      rel_apply_r (bin_log_related_acquire_r with "Hl'").
      { solve_ndisj. }
      iIntros "Hl'".
      iMod ("Hcl" with "[-]") as "_".
      { iNext. iExists P; iFrame.
        iApply "HPinv". iExists _,_,_; by iFrame. }
      iApply bin_log_related_unit.
  Qed.

  Lemma acquire_refinement_direct Δ Γ γp :
    inv N (moduleInv γp) -
    {(lockInt γp :: Δ); ⤉Γ}  acquire log lock.acquire : (TVar 0  Unit).
404 405 406 407 408 409
  Proof.
    iIntros "#Hinv".
    unlock acquire; simpl.
    iApply bin_log_related_arrow_val; eauto.
    { by unlock lock.acquire. }
    iAlways. iIntros (? ?) "/= #Hl".
410
    iDestruct "Hl" as (lo ln γ l') "(% & % & Hin)". simplify_eq.
411 412 413 414
    rel_let_l. repeat rel_proj_l.
    (* rel_rec_l. (* TODO: cannot find the reduct *) *)
    rel_bind_l (FG_increment _ #()).
    rel_rec_l.
415
    rel_apply_l (bin_log_FG_increment_logatomic _ (issuedTickets γ)%I True%I); first done.
416
    iAlways.
417 418
    openI N.
    iDestruct (lockPool_open with "HP Hin HPinv") as "(HP & Hls & HPinv)".
419
    rewrite {1}/lockInv.
420
    iDestruct "Hls" as (o n b) "(Hlo & Hln & Hissued & Hl' & Hticket)".
421 422 423 424 425
    iModIntro. iExists _; iFrame.
    iSplit.
    - iDestruct 1 as (m) "[Hln ?]".
      iApply ("Hcl" with "[-]").
      iNext. iExists P; iFrame.
426 427 428 429
      iApply "HPinv". iExists _,_,_; by iFrame.
    - iIntros (m) "[Hln Hissued] _".
      iMod (issueNewTicket with "Hissued") as "[Hissued Hm]".
      iMod ("Hcl" with "[-Hm]") as "_".
430
      { iNext. iExists P; iFrame.
431 432
        iApply "HPinv". iExists _,_,_; by iFrame. }
      rel_let_l.
433 434 435 436
      by iApply wait_loop_refinement.
  Qed.

  (* Releasing the lock *)
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
  Lemma wkincr_l x (n : nat) Δ Γ E K t τ :
    x ↦ᵢ #n -
    (x ↦ᵢ #(n+1) - {E;Δ;Γ}  fill K Unit log t : τ) -
    ({E;Δ;Γ}  fill K (wkincr #x) log t : τ).
  Proof.
    iIntros "Hx Hlog".
    unlock wkincr. rel_rec_l.
    rel_load_l. rel_op_l. rel_store_l.
    by iApply "Hlog".
  Qed.

  (* Logically atomic specification for wkincr,
     cf wkIncr specification from (da Rocha Pinto, Dinsdale-Young, Gardner)
     Parameter type: nat
     Precondition: λn, x ↦ᵢ n
     Postcondition λ_,  n, x ↦ᵢ n
 *)
  Lemma wkincr_atomic_l R1 R2 Δ Γ E1 E2 K x t τ  :
    R2 -
     (|={E1,E2}=>  n : nat, x ↦ᵢ #n  R1 n 
       (( n : nat, x ↦ᵢ #n  R1 n) ={E2,E1}= True) 
        ( m, ( n : nat, x ↦ᵢ #n)  R1 m - R2 -
            {E2,E1;Δ;Γ}  fill K Unit log t : τ))
    - ({E1;Δ;Γ}  fill K (wkincr #x) log t : τ).
  Proof.
    iIntros "HR2 #H".
    unlock wkincr.
    rel_rec_l.
    iPoseProof "H" as "H2".
    rel_load_l_atomic.
    iMod "H" as (n) "[Hx [HR1 [Hrev _]]]". iModIntro.
    iExists _; iFrame. iNext. iIntros "Hx".
    iMod ("Hrev" with "[HR1 Hx]") as "_"; simpl.
    { iExists _. iFrame. }
    rel_op_l.
    rel_store_l_atomic.
    iMod "H2" as (m) "[Hx [HR1 [_ Hmod]]]". iModIntro.
    iExists _; iFrame. iNext. iIntros "Hx".
    iApply ("Hmod" with "[$HR1 Hx] HR2").
    iExists _; iFrame.
  Qed.

479 480 481 482 483 484 485 486 487
  Lemma release_refinement Δ Γ γp :
    inv N (moduleInv γp) -
    {(lockInt γp :: Δ); ⤉Γ}  release log lock.release : (TVar 0  Unit).
  Proof.
    iIntros "#Hinv".
    unlock release.
    iApply bin_log_related_arrow_val; eauto.
    { by unlock lock.release. }
    iAlways. iIntros (? ?) "/= #Hl".
488
    iDestruct "Hl" as (lo ln γ l') "(% & % & Hin)". simplify_eq.
489 490 491 492 493 494 495
    rel_let_l. rel_proj_l.
    pose (R := fun (o : nat) =>
                 ( (n : nat) (b : bool), ln ↦ᵢ #n
                  issuedTickets γ n  l' ↦ₛ #b
                  if b then ticket γ o else True)%I).
    rel_apply_l (wkincr_atomic_l R True%I); first done.
    iAlways.
496 497
    openI N.
    iDestruct (lockPool_open with "HP Hin HPinv") as "(HP & Hls & HPinv)".
498
    rewrite {1}/lockInv.
499
    iDestruct "Hls" as (o n b) "(Hlo & Hrest)".
500
    iModIntro. iExists _; iFrame.
501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518
    rewrite {1}/R. iSplitL "Hrest".
    { iExists _,_; iFrame. } clear o n b.
    iSplit.
    - iDestruct 1 as (o) "[Hlo HR]".
      unfold R. iDestruct "HR" as (n b) "HR".
      iApply "Hcl".
      iNext. iExists P; iFrame.
      iApply "HPinv". iExists _,_,_; by iFrame.
    - iIntros (?) "[Hlo HR] _".
      iDestruct "Hlo" as (o) "Hlo".
      unfold R. iDestruct "HR" as (n b) "(Hln & Hissued & Hl' & Hticket)".
      rel_apply_r (bin_log_related_release_r with "Hl'").
      { solve_ndisj. }
      iIntros "Hl'".
      iMod ("Hcl" with "[-]") as "_".
      { iNext. iExists P; iFrame.
        iApply "HPinv". iExists _,_,_. by iFrame. }
      iApply bin_log_related_unit.
519 520
  Qed.

Dan Frumin's avatar
Dan Frumin committed
521 522 523
  Lemma ticket_lock_refinement Γ :
    Γ  Pack (newlock, acquire, release)
      log
524
        Pack (lock.newlock, lock.acquire, lock.release) : lockT.
Dan Frumin's avatar
Dan Frumin committed
525 526
  Proof.
    iIntros (Δ).
527
    iMod (newIsPool ) as (γp) "HP".
Dan Frumin's avatar
Dan Frumin committed
528
    iMod (inv_alloc N _ (moduleInv γp) with "[HP]") as "#Hinv".
529
    { iNext. iExists _; iFrame. iApply lockPoolInv_empty. }
Dan Frumin's avatar
Dan Frumin committed
530 531
    iApply (bin_log_related_pack _ (lockInt γp)).
    repeat iApply bin_log_related_pair.
532 533 534
    - by iApply newlock_refinement.
    - by iApply acquire_refinement.
    - by iApply release_refinement.
Dan Frumin's avatar
Dan Frumin committed
535
  Qed.
Dan Frumin's avatar
Dan Frumin committed
536

Dan Frumin's avatar
Dan Frumin committed
537
End refinement.