counter.v 7.07 KB
Newer Older
1
From iris.program_logic Require Export weakestpre.
2
From iris.base_logic.lib Require Export invariants.
3
From iris.heap_lang Require Export lang.
4
From iris.proofmode Require Import tactics.
5
From iris.algebra Require Import frac auth.
6 7
From iris.heap_lang Require Import proofmode notation.

8 9
Definition newcounter : val := λ: <>, ref #0.
Definition incr : val := rec: "incr" "l" :=
10
    let: "n" := !"l" in
11 12
    if: CAS "l" "n" (#1 + "n") then #() else "incr" "l".
Definition read : val := λ: "l", !"l".
13

14 15 16
(** Monotone counter *)
Class mcounterG Σ := MCounterG { mcounter_inG :> inG Σ (authR mnatUR) }.
Definition mcounterΣ : gFunctors := #[GFunctor (constRF (authR mnatUR))].
17

18
Instance subG_mcounterΣ {Σ} : subG mcounterΣ Σ  mcounterG Σ.
19
Proof. intros [?%subG_inG _]%subG_inv. split; apply _. Qed.
20

21 22 23 24
Section mono_proof.
  Context `{!heapG Σ, !mcounterG Σ} (N : namespace).

  Definition mcounter_inv (γ : gname) (l : loc) : iProp Σ :=
25
    ( n, own γ ( (n : mnat))  l  #n)%I.
26 27

  Definition mcounter (l : loc) (n : nat) : iProp Σ :=
28
    ( γ, inv N (mcounter_inv γ l)  own γ ( (n : mnat)))%I.
29 30 31 32 33

  (** The main proofs. *)
  Global Instance mcounter_persistent l n : PersistentP (mcounter l n).
  Proof. apply _. Qed.

34
  Lemma newcounter_mono_spec (R : iProp Σ) :
35
    {{{ True }}} newcounter #() {{{ l, RET #l; mcounter l 0 }}}.
36
  Proof.
37
    iIntros (Φ) "HΦ". rewrite -wp_fupd /newcounter /=. wp_seq. wp_alloc l as "Hl".
38 39
    iMod (own_alloc ( (O:mnat)   (O:mnat))) as (γ) "[Hγ Hγ']"; first done.
    iMod (inv_alloc N _ (mcounter_inv γ l) with "[Hl Hγ]").
40
    { iNext. iExists 0%nat. by iFrame. }
41
    iModIntro. iApply "HΦ". rewrite /mcounter; eauto 10.
42 43
  Qed.

44 45
  Lemma incr_mono_spec l n :
    {{{ mcounter l n }}} incr #l {{{ RET #(); mcounter l (S n) }}}.
46
  Proof.
Ralf Jung's avatar
Ralf Jung committed
47
    iIntros (Φ) "Hl HΦ". iLöb as "IH". wp_rec.
48
    iDestruct "Hl" as (γ) "[#Hinv Hγf]".
49
    wp_bind (! _)%E. iInv N as (c) ">[Hγ Hl]" "Hclose".
50 51
    wp_load. iMod ("Hclose" with "[Hl Hγ]") as "_"; [iNext; iExists c; by iFrame|].
    iModIntro. wp_let. wp_op.
52 53
    wp_bind (CAS _ _ _). iInv N as (c') ">[Hγ Hl]" "Hclose".
    destruct (decide (c' = c)) as [->|].
54
    - iDestruct (own_valid_2 with "Hγ Hγf")
55
        as %[?%mnat_included _]%auth_valid_discrete_2.
56
      iMod (own_update_2 with "Hγ Hγf") as "[Hγ Hγf]".
57
      { apply auth_update, (mnat_local_update _ _ (S c)); auto. } 
58
      wp_cas_suc. iMod ("Hclose" with "[Hl Hγ]") as "_".
59
      { iNext. iExists (S c). rewrite Nat2Z.inj_succ Z.add_1_l. by iFrame. }
60
      iModIntro. wp_if. iApply "HΦ"; iExists γ; repeat iSplit; eauto.
61 62 63
      iApply (own_mono with "Hγf"). apply: auth_frag_mono.
      by apply mnat_included, le_n_S.
    - wp_cas_fail; first (by intros [= ?%Nat2Z.inj]).
64
      iMod ("Hclose" with "[Hl Hγ]") as "_"; [iNext; iExists c'; by iFrame|].
Ralf Jung's avatar
Ralf Jung committed
65
      iModIntro. wp_if. iApply ("IH" with "[Hγf] [HΦ]"); last by auto.
66 67 68
      rewrite {3}/mcounter; eauto 10.
  Qed.

69
  Lemma read_mono_spec l j :
Ralf Jung's avatar
Ralf Jung committed
70
    {{{ mcounter l j }}} read #l {{{ i, RET #i; j  i%nat  mcounter l i }}}.
71
  Proof.
72
    iIntros (ϕ) "Hc HΦ". iDestruct "Hc" as (γ) "[#Hinv Hγf]".
73
    rewrite /read /=. wp_let. iInv N as (c) ">[Hγ Hl]" "Hclose". wp_load.
74
    iDestruct (own_valid_2 with "Hγ Hγf")
75
      as %[?%mnat_included _]%auth_valid_discrete_2.
76
    iMod (own_update_2 with "Hγ Hγf") as "[Hγ Hγf]".
77
    { apply auth_update, (mnat_local_update _ _ c); auto. }
78
    iMod ("Hclose" with "[Hl Hγ]") as "_"; [iNext; iExists c; by iFrame|].
79
    iApply ("HΦ" with "[-]"). rewrite /mcounter; eauto 10.
80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95
  Qed.
End mono_proof.

(** Counter with contributions *)
Class ccounterG Σ :=
  CCounterG { ccounter_inG :> inG Σ (authR (optionUR (prodR fracR natR))) }.
Definition ccounterΣ : gFunctors :=
  #[GFunctor (constRF (authR (optionUR (prodR fracR natR))))].

Instance subG_ccounterΣ {Σ} : subG ccounterΣ Σ  ccounterG Σ.
Proof. intros [?%subG_inG _]%subG_inv. split; apply _. Qed.

Section contrib_spec.
  Context `{!heapG Σ, !ccounterG Σ} (N : namespace).

  Definition ccounter_inv (γ : gname) (l : loc) : iProp Σ :=
96
    ( n, own γ ( Some (1%Qp, n))  l  #n)%I.
97 98

  Definition ccounter_ctx (γ : gname) (l : loc) : iProp Σ :=
99
    inv N (ccounter_inv γ l).
100 101 102 103 104 105

  Definition ccounter (γ : gname) (q : frac) (n : nat) : iProp Σ :=
    own γ ( Some (q, n)).

  (** The main proofs. *)
  Lemma ccounter_op γ q1 q2 n1 n2 :
106
    ccounter γ (q1 + q2) (n1 + n2)  ccounter γ q1 n1 ccounter γ q2 n2.
107 108
  Proof. by rewrite /ccounter -own_op -auth_frag_op. Qed.

109
  Lemma newcounter_contrib_spec (R : iProp Σ) :
110
    {{{ True }}} newcounter #()
111
    {{{ γ l, RET #l; ccounter_ctx γ l  ccounter γ 1 0 }}}.
112
  Proof.
113
    iIntros (Φ) "HΦ". rewrite -wp_fupd /newcounter /=. wp_seq. wp_alloc l as "Hl".
114
    iMod (own_alloc ( (Some (1%Qp, O%nat))   (Some (1%Qp, 0%nat))))
115
      as (γ) "[Hγ Hγ']"; first done.
116
    iMod (inv_alloc N _ (ccounter_inv γ l) with "[Hl Hγ]").
117
    { iNext. iExists 0%nat. by iFrame. }
118
    iModIntro. iApply "HΦ". rewrite /ccounter_ctx /ccounter; eauto 10.
119 120
  Qed.

121 122
  Lemma incr_contrib_spec γ l q n :
    {{{ ccounter_ctx γ l  ccounter γ q n }}} incr #l
123
    {{{ RET #(); ccounter γ q (S n) }}}.
124
  Proof.
125
    iIntros (Φ) "[#? Hγf] HΦ". iLöb as "IH". wp_rec.
126
    wp_bind (! _)%E. iInv N as (c) ">[Hγ Hl]" "Hclose".
127 128
    wp_load. iMod ("Hclose" with "[Hl Hγ]") as "_"; [iNext; iExists c; by iFrame|].
    iModIntro. wp_let. wp_op.
129 130
    wp_bind (CAS _ _ _). iInv N as (c') ">[Hγ Hl]" "Hclose".
    destruct (decide (c' = c)) as [->|].
131
    - iMod (own_update_2 with "Hγ Hγf") as "[Hγ Hγf]".
132 133
      { apply auth_update, option_local_update, prod_local_update_2.
        apply (nat_local_update _ _ (S c) (S n)); omega. }
134
      wp_cas_suc. iMod ("Hclose" with "[Hl Hγ]") as "_".
135
      { iNext. iExists (S c). rewrite Nat2Z.inj_succ Z.add_1_l. by iFrame. }
136
      iModIntro. wp_if. by iApply "HΦ".
137
    - wp_cas_fail; first (by intros [= ?%Nat2Z.inj]).
138
      iMod ("Hclose" with "[Hl Hγ]") as "_"; [iNext; iExists c'; by iFrame|].
Ralf Jung's avatar
Ralf Jung committed
139
      iModIntro. wp_if. by iApply ("IH" with "[Hγf] [HΦ]"); auto.
140 141
  Qed.

142
  Lemma read_contrib_spec γ l q n :
143
    {{{ ccounter_ctx γ l  ccounter γ q n }}} read #l
Ralf Jung's avatar
Ralf Jung committed
144
    {{{ c, RET #c; n  c%nat  ccounter γ q n }}}.
145
  Proof.
146
    iIntros (Φ) "[#? Hγf] HΦ".
147
    rewrite /read /=. wp_let. iInv N as (c) ">[Hγ Hl]" "Hclose". wp_load.
148
    iDestruct (own_valid_2 with "Hγ Hγf")
149
      as %[[? ?%nat_included]%Some_pair_included_total_2 _]%auth_valid_discrete_2.
150
    iMod ("Hclose" with "[Hl Hγ]") as "_"; [iNext; iExists c; by iFrame|].
151
    iApply ("HΦ" with "[-]"); rewrite /ccounter; eauto 10.
152 153
  Qed.

154
  Lemma read_contrib_spec_1 γ l n :
155
    {{{ ccounter_ctx γ l  ccounter γ 1 n }}} read #l
156
    {{{ n, RET #n; ccounter γ 1 n }}}.
157
  Proof.
158
    iIntros (Φ) "[#? Hγf] HΦ".
159
    rewrite /read /=. wp_let. iInv N as (c) ">[Hγ Hl]" "Hclose". wp_load.
160
    iDestruct (own_valid_2 with "Hγ Hγf") as %[Hn _]%auth_valid_discrete_2.
161
    apply (Some_included_exclusive _) in Hn as [= ->]%leibniz_equiv; last done.
162
    iMod ("Hclose" with "[Hl Hγ]") as "_"; [iNext; iExists c; by iFrame|].
163 164 165
    by iApply "HΦ".
  Qed.
End contrib_spec.