counter.v 6.74 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 auth.
6
From iris.heap_lang Require Import proofmode notation.
7
Set Default Proof Using "Type".
8

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

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

19
Instance subG_mcounterΣ {Σ} : subG mcounterΣ Σ  mcounterG Σ.
20
Proof. solve_inG. Qed.
21

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

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

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

  (** The main proofs. *)
32
  Global Instance mcounter_persistent l n : Persistent (mcounter l n).
33 34
  Proof. apply _. Qed.

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

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

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

(** Counter with contributions *)
Class ccounterG Σ :=
86
  CCounterG { ccounter_inG :> inG Σ (frac_authR natR) }.
87
Definition ccounterΣ : gFunctors :=
88
  #[GFunctor (frac_authR natR)].
89 90

Instance subG_ccounterΣ {Σ} : subG ccounterΣ Σ  ccounterG Σ.
Robbert Krebbers's avatar
Robbert Krebbers committed
91
Proof. solve_inG. Qed.
92 93 94 95 96

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

  Definition ccounter_inv (γ : gname) (l : loc) : iProp Σ :=
97
    ( n, own γ (! n)  l  #n)%I.
98 99

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

  Definition ccounter (γ : gname) (q : frac) (n : nat) : iProp Σ :=
103
    own γ (!{q} n).
104 105 106

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

110
  Lemma newcounter_contrib_spec (R : iProp Σ) :
111
    {{{ True }}} newcounter #()
112
    {{{ γ l, RET #l; ccounter_ctx γ l  ccounter γ 1 0 }}}.
113
  Proof.
Robbert Krebbers's avatar
Robbert Krebbers committed
114
    iIntros (Φ) "_ HΦ". rewrite -wp_fupd /newcounter /=. wp_seq. wp_alloc l as "Hl".
115
    iMod (own_alloc (! O%nat  ! 0%nat)) 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
      { apply frac_auth_update, (nat_local_update _ _ (S c) (S n)); omega. }
133
      wp_cas_suc. iMod ("Hclose" with "[Hl Hγ]") as "_".
134
      { iNext. iExists (S c). rewrite Nat2Z.inj_succ Z.add_1_l. by iFrame. }
135
      iModIntro. wp_if. by iApply "HΦ".
136
    - wp_cas_fail; first (by intros [= ?%Nat2Z.inj]).
137
      iMod ("Hclose" with "[Hl Hγ]") as "_"; [iNext; iExists c'; by iFrame|].
Ralf Jung's avatar
Ralf Jung committed
138
      iModIntro. wp_if. by iApply ("IH" with "[Hγf] [HΦ]"); auto.
139 140
  Qed.

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

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