counter.v 10.5 KB
Newer Older
1
From iris.proofmode Require Import tactics.
2 3
From iris_logrel Require Export logrel.
From iris_logrel.examples Require Import lock.
4

5
Definition CG_increment : val := λ: "x" "l" <>,
6
  acquire "l";;
7
  "x" <- #1 + !"x";;
8
  release "l".
9

10
Definition counter_read : val := λ: "x" <>, !"x".
11

Dan Frumin's avatar
Dan Frumin committed
12
Definition CG_counter : val := λ: <>,
13 14
  let: "l" := newlock #() in
  let: "x" := ref #0 in
15
  (CG_increment "x" "l", counter_read "x").
16

17
Definition FG_increment : val := λ: "v", rec: "inc" <> :=
Dan Frumin's avatar
Dan Frumin committed
18
  let: "c" := !"v" in
19 20 21
  if: CAS "v" "c" (#1 + "c")
  then #()
  else "inc" #().
22

Dan Frumin's avatar
Dan Frumin committed
23
Definition FG_counter : val := λ: <>,
24
  let: "x" := ref #0 in
25
  (FG_increment "x", counter_read "x").
Robbert Krebbers's avatar
Robbert Krebbers committed
26

27
Section CG_Counter.
28
  Context `{logrelG Σ}.
29
  Variable (Δ : list (prodC valC valC -n> iProp Σ)).
Dan Frumin's avatar
Dan Frumin committed
30
  Open Scope expr_scope.
Dan Frumin's avatar
Dan Frumin committed
31
  
32
  (* Coarse-grained increment *)  
33
  Lemma CG_increment_type Γ :
34
    typed Γ CG_increment (TArrow (Tref TNat) (TArrow LockType (TArrow TUnit TUnit))).
Dan Frumin's avatar
Dan Frumin committed
35
  Proof. solve_typed. Qed.
36

Dan Frumin's avatar
Dan Frumin committed
37
  Hint Resolve CG_increment_type : typeable.
38

39
  Lemma bin_log_related_CG_increment_r Γ K E1 E2 t τ (x l : loc) (n : nat) :
40 41 42
    nclose specN  E1 
    (x ↦ₛ (#nv n) - l ↦ₛ (#v false) -
    (x ↦ₛ (#nv S n) - l ↦ₛ (#v false) -
43 44
      ({E1,E2;Δ;Γ}  t log fill K (Lit tt) : τ)) -
    {E1,E2;Δ;Γ}  t log fill K ((CG_increment $/ (LitV (Loc x)) $/ LitV (Loc l)) #())%E : τ)%I.
45
  Proof.
46
    iIntros (?) "Hx Hl Hlog".
47
    unfold CG_increment. unlock. simpl_subst/=.
Dan Frumin's avatar
Dan Frumin committed
48 49 50
    rel_rec_r.
    rel_apply_r (bin_log_related_acquire_r with "Hl"); eauto.
    iIntros "Hl".
51
    rel_rec_r.
52 53 54
    rel_load_r.
    rel_op_r.
    rel_store_r. simpl.
55
    rel_rec_r.
Dan Frumin's avatar
Dan Frumin committed
56
    rel_apply_r (bin_log_related_release_r with "Hl"); eauto.
57
    by iApply "Hlog".
58 59
  Qed.
 
60 61
  Lemma counter_read_type Γ :
    typed Γ counter_read (TArrow (Tref TNat) (TArrow TUnit TNat)).
Dan Frumin's avatar
Dan Frumin committed
62
  Proof. solve_typed. Qed.
63

Dan Frumin's avatar
Dan Frumin committed
64 65
  Hint Resolve counter_read_type : typeable.

66
  Lemma CG_counter_type Γ :
Dan Frumin's avatar
Dan Frumin committed
67 68
    typed Γ CG_counter (TArrow TUnit (TProd (TArrow TUnit TUnit) (TArrow TUnit TNat))).
  Proof. solve_typed. Qed.
69

Dan Frumin's avatar
Dan Frumin committed
70 71
  Hint Resolve CG_counter_type : typeable.

72
  (* Fine-grained increment *)
73 74
  Lemma FG_increment_type Γ :
    typed Γ FG_increment (TArrow (Tref TNat) (TArrow TUnit TUnit)).
Dan Frumin's avatar
Dan Frumin committed
75
  Proof. solve_typed. Qed.
76

Dan Frumin's avatar
Dan Frumin committed
77 78
  Hint Resolve FG_increment_type : typeable.

79 80
  Lemma bin_log_FG_increment_l Γ K E x n t τ :
    x ↦ᵢ (#nv n) -
81 82
    (x ↦ᵢ (#nv (S n)) - {E,E;Δ;Γ}  fill K (Lit tt) log t : τ) -
    {E,E;Δ;Γ}  fill K (FG_increment #x #()) log t : τ.
83 84
  Proof.
    iIntros "Hx Hlog".
Dan Frumin's avatar
Dan Frumin committed
85
    iApply bin_log_related_wp_l.
86 87
    wp_bind (FG_increment #x).
    unfold FG_increment. unlock.
88
    iApply wp_rec; eauto.
89 90
    iNext. simpl.
    iApply wp_value; eauto. simpl. by rewrite decide_left.
91
    iApply wp_rec; eauto.
92
    iNext. simpl.
93
    wp_bind (Load (# x)).
94
    iApply (wp_load with "[Hx]"); auto. iNext.
95
    iIntros "Hx".
96
    iApply wp_rec; eauto.
97
    iNext. simpl.
98
    wp_bind (BinOp _ _ _).
Dan Frumin's avatar
Dan Frumin committed
99
    iApply (wp_nat_binop);eauto. iNext. iModIntro. simpl.    
100 101 102 103 104
    wp_bind (CAS _ _ _).    
    iApply (wp_cas_suc with "[Hx]"); auto.
    iNext. iIntros "Hx".
    iApply wp_if_true. iNext.
    iApply wp_value; auto.
105
    by iApply "Hlog".
106 107
  Qed.

108
  Lemma FG_counter_type Γ :
Dan Frumin's avatar
Dan Frumin committed
109 110
    typed Γ FG_counter (TArrow TUnit (TProd (TArrow TUnit TUnit) (TArrow TUnit TNat))).
  Proof. solve_typed. Qed.
111

Dan Frumin's avatar
Dan Frumin committed
112
  Hint Resolve FG_counter_type : typeable.
113

114 115
  Definition counterN : namespace := nroot .@ "counter".

116 117 118
  Definition counter_inv l cnt cnt' : iProp Σ :=
    ( n, l ↦ₛ (#v false)  cnt ↦ᵢ (#nv n)  cnt' ↦ₛ (#nv n))%I.

119 120 121 122
  Lemma bin_log_counter_read_r Γ E1 E2 K x n t τ
    (Hspec : nclose specN  E1) :
    x ↦ₛ (#nv n)
    - (x ↦ₛ (#nv n)
123
         - {E1,E2;Δ;Γ}  t log fill K (#n n)%E : τ)%I
124
    - {E1,E2;Δ;Γ}  t log fill K ((counter_read $/ LitV (Loc x)) #())%E : τ.
125 126
  Proof.
    iIntros "Hx Hlog".
127
    unfold counter_read. unlock. simpl.
128 129 130
    rel_rec_r.
    rel_load_r.
    by iApply "Hlog".
131 132
  Qed.

133 134 135 136 137 138 139
  (* A logically atomic specification for
     a fine-grained increment with a baked in frame. *)
  (* Unfortunately, the precondition is not baked in the rule so you can only use it when your spatial context is empty *)
  Lemma bin_log_FG_increment_logatomic R Γ E1 E2 K x t τ  :
     (|={E1,E2}=>  n, x ↦ᵢ (#nv n)  R(n) 
       (( n, x ↦ᵢ (#nv n)  R(n)) ={E2,E1}= True) 
        ( m, x ↦ᵢ (#nv (S m))  R(m) - 
140 141
            {E2,E1;Δ;Γ}  fill K (Lit tt) log t : τ))
    - ({E1,E1;Δ;Γ}  fill K ((FG_increment $/ LitV (Loc x)) #())%E log t : τ).
142
  Proof.
143
    iIntros "#H".
144
    unfold FG_increment. unlock. simpl.
145 146 147
    iLöb as "IH".
    rel_rec_l.
    iPoseProof "H" as "H2". (* lolwhat *)
Dan Frumin's avatar
Dan Frumin committed
148
    rel_load_l_atomic.
149
    iMod "H" as (n) "[Hx [HR Hrev]]".  iModIntro.
Dan Frumin's avatar
Dan Frumin committed
150
    iExists (#nv n). iFrame. iNext. iIntros "Hx".
151
    iDestruct "Hrev" as "[Hrev _]".
152 153
    iApply fupd_logrel.
    iMod ("Hrev" with "[HR Hx]").
154
    { iExists _. iFrame. } iModIntro. simpl.
155
    rel_rec_l.
156
    rel_op_l.
Dan Frumin's avatar
Dan Frumin committed
157
    rel_cas_l_atomic.
158
    iMod "H2" as (n') "[Hx [HR HQ]]". iModIntro. simpl.
159
    destruct (decide (n = n')); subst.
160
    - iExists (#nv n'). iFrame.
161
      iSplitR; eauto. { iDestruct 1 as %Hfoo. exfalso. done. }
Dan Frumin's avatar
Dan Frumin committed
162
      iIntros "_ !> Hx". simpl.
163
      iDestruct "HQ" as "[_ HQ]".
164
      iSpecialize ("HQ" $! n' with "[Hx HR]"). { iFrame. }
Dan Frumin's avatar
Dan Frumin committed
165
      rel_if_true_l. done.
166 167 168
    - iExists (#nv n'). iFrame. 
      iSplitL; eauto; last first.
      { iDestruct 1 as %Hfoo. exfalso. simplify_eq. }
Dan Frumin's avatar
Dan Frumin committed
169
      iIntros "_ !> Hx". simpl.
Dan Frumin's avatar
Dan Frumin committed
170
      rel_if_false_l.
171
      iDestruct "HQ" as "[HQ _]".
172
      iMod ("HQ" with "[Hx HR]").
Dan Frumin's avatar
Dan Frumin committed
173
      { iExists _; iFrame. }
174
      iApply "IH".
Dan Frumin's avatar
Dan Frumin committed
175
  Qed.
176

177 178 179 180 181
  (* A similar atomic specification for the counter_read fn *)
  Lemma bin_log_counter_read_atomic_l R Γ E1 E2 K x t τ :
     (|={E1,E2}=>  n, x ↦ᵢ (#nv n)  R(n) 
       (( n, x ↦ᵢ (#nv n)  R(n)) ={E2,E1}= True) 
        ( m, x ↦ᵢ (#nv m)  R(m) -
182
            {E2,E1;Δ;Γ}  fill K (#n m) log t : τ))
183
    - {E1,E1;Δ;Γ}  fill K ((counter_read $/ LitV (Loc x)) #())%E log t : τ.
184 185
  Proof.
    iIntros "#H".
186
    unfold counter_read. unlock. simpl.
187
    rel_rec_l.
Dan Frumin's avatar
Dan Frumin committed
188
    rel_load_l_atomic.
189
    iMod "H" as (n) "[Hx [HR Hfin]]". iModIntro.
Dan Frumin's avatar
Dan Frumin committed
190
    iExists _; iFrame "Hx". iNext.
191
    iIntros "Hx".
192
    iDestruct "Hfin" as "[_ Hfin]".
193 194 195
    iApply "Hfin". by iFrame.
  Qed.

196
  (* TODO: try to use with_lock rules *)
197 198
  Lemma FG_CG_increment_refinement l cnt cnt' Γ :
    inv counterN (counter_inv l cnt cnt') -
199
    {,;Δ;Γ}  FG_increment $/ LitV (Loc cnt) log CG_increment $/ LitV (Loc cnt') $/ LitV (Loc l) : TArrow TUnit TUnit.
200 201
  Proof.
    iIntros "#Hinv".
202
    iApply bin_log_related_arrow_val.
203 204 205 206
    { unfold FG_increment. unlock; simpl_subst. reflexivity. }
    { unfold CG_increment. unlock; simpl_subst. reflexivity. }
    { unfold FG_increment. unlock; simpl_subst/=. solve_closed. (* TODO: add a clause to the reflection mechanism that reifies a [lambdasubst] expression as a closed one *) }
    { unfold CG_increment. unlock; simpl_subst/=. solve_closed. }
207

208
    iAlways. iIntros (v v') "[% %]"; simpl in *; subst.
209 210
    iApply (bin_log_FG_increment_logatomic (fun n => (l ↦ₛ (#v false))  cnt' ↦ₛ #nv n)%I _ _ _ [] cnt with "[Hinv]").
    iAlways.
211
    iInv counterN as ">Hcnt" "Hcl". iModIntro.
212 213 214 215
    iDestruct "Hcnt" as (n) "[Hl [Hcnt Hcnt']]".
    iExists n. iFrame. clear n.
    iSplit.
    - iDestruct 1 as (n) "[Hcnt [Hl Hcnt']]".
216
      iMod ("Hcl" with "[-]").
217 218 219
      { iNext. iExists _. iFrame. }
      done.
    - iIntros (m) "[Hcnt [Hl Hcnt']]".
220
      iApply (bin_log_related_CG_increment_r _ [] with "[Hcnt'] [Hl]"); auto. { solve_ndisj.  }
221
      iIntros "Hcnt' Hl".
222
      iMod ("Hcl" with "[-]").
223 224
      { iNext. iExists _. iFrame. }
      simpl.
225
      by rel_vals.
226
  Qed.
227

228 229
  Lemma counter_read_refinement l cnt cnt' Γ :
    inv counterN (counter_inv l cnt cnt') -
230
    {,;Δ;Γ}  counter_read $/ LitV (Loc cnt) log counter_read $/ LitV (Loc cnt') : TArrow TUnit TNat.
231 232
  Proof.
    iIntros "#Hinv".
233
    iApply bin_log_related_arrow_val.
234 235 236 237
    { unfold counter_read. unlock. simpl. reflexivity. }
    { unfold counter_read. unlock. simpl. reflexivity. }
    { unfold counter_read. unlock. simpl. solve_closed. }
    { unfold counter_read. unlock. simpl. solve_closed. }
238
    iAlways. iIntros (v v') "[% %]"; simpl in *; subst.
239 240
    iApply (bin_log_counter_read_atomic_l (fun n => (l ↦ₛ (#v false))  cnt' ↦ₛ #nv n)%I _ _ _ [] cnt with "[Hinv]").
    iAlways. iInv counterN as (n) "[>Hl [>Hcnt >Hcnt']]" "Hclose". 
241
    iModIntro. 
242 243 244 245 246 247 248 249 250 251
    iExists n. iFrame "Hcnt Hcnt' Hl". clear n.
    iSplit.
    - iDestruct 1 as (n) "(Hcnt & Hl & Hcnt')". iApply "Hclose".
      iNext. iExists n. by iFrame.
    - iIntros (m) "(Hcnt & Hl & Hcnt') /=".
      iApply (bin_log_counter_read_r _ _ _ [] with "Hcnt'").
      { solve_ndisj. }
      iIntros "Hcnt'".
      iMod ("Hclose" with "[Hl Hcnt Hcnt']"); simpl.
      { iNext. iExists _. by iFrame. }
252
      rel_vals. simpl. eauto.
253
  Qed.
254

255
  Lemma FG_CG_counter_refinement :
256
    {,;Δ;}  FG_counter log CG_counter :
Dan Frumin's avatar
Dan Frumin committed
257
          TArrow TUnit (TProd (TArrow TUnit TUnit) (TArrow TUnit TNat)).
258
  Proof.
259
    unfold FG_counter, CG_counter.
Dan Frumin's avatar
Dan Frumin committed
260
    iApply bin_log_related_arrow; try by (unlock; eauto).
261
    iAlways. iIntros (? ?) "/= ?"; simplify_eq/=.
Dan Frumin's avatar
Dan Frumin committed
262
    unlock. rel_rec_l. rel_rec_r.
Dan Frumin's avatar
Dan Frumin committed
263
    rel_apply_r bin_log_related_newlock_r; auto.
264
    iIntros (l) "Hl".
265 266
    rel_rec_r.
    rel_alloc_r as cnt' "Hcnt'".
Dan Frumin's avatar
Dan Frumin committed
267
    rel_alloc_l as cnt "Hcnt". simpl.
268

269
    rel_rec_l.
270
    rel_rec_r.
271 272 273 274 275 276 277

    (* establishing the invariant *)
    iAssert (counter_inv l cnt cnt')
      with "[Hl Hcnt Hcnt']" as "Hinv".
    { iExists _. by iFrame. }
    iMod (inv_alloc counterN with "[Hinv]") as "#Hinv"; trivial.

278
    iApply (bin_log_related_pair _ with "[]").
279
    - rel_rec_l.
280
      unfold CG_increment. unlock.
281 282
      rel_rec_r.
      rel_rec_r.
283 284
      replace (λ: <>, acquire # l ;; #cnt' <- #1 + (! #cnt');; release # l)%E
        with (CG_increment $/ LitV (Loc cnt') $/ LitV (Loc l))%E; last first.
285
      { unfold CG_increment. unlock; simpl_subst/=. reflexivity. }
286 287 288 289
      iApply (FG_CG_increment_refinement with "Hinv").
    - rel_rec_l.
      rel_rec_r.
      iApply (counter_read_refinement with "Hinv").
290
  Qed.
291 292
End CG_Counter.

293
Theorem counter_ctx_refinement :
294
    FG_counter ctx CG_counter :
Dan Frumin's avatar
Dan Frumin committed
295
         TArrow TUnit (TProd (TArrow TUnit TUnit) (TArrow TUnit TNat)).
296
Proof.
297 298
  eapply (logrel_ctxequiv logrelΣ); [solve_closed.. | intros ].
  apply FG_CG_counter_refinement.
299
Qed.