counter.v 3.58 KB
Newer Older
1 2 3 4 5 6 7 8 9 10
(* Monotone counter, but using an explicit CMRA instead of auth *)
From iris.program_logic Require Export global_functor.
From iris.program_logic Require Import auth.
From iris.proofmode Require Import invariants ghost_ownership coq_tactics.
From iris.heap_lang Require Import proofmode notation.
Import uPred.

Definition newcounter : val := λ: <>, ref #0.
Definition inc : val :=
  rec: "inc" "l" :=
11 12 13
    let: "n" := !"l" in
    if: CAS "l" "n" (#1 + "n") then #() else "inc" "l".
Definition read : val := λ: "l", !"l".
14 15 16 17 18 19 20 21 22
Global Opaque newcounter inc get.

(** The CMRA we need. *)
Class counterG Σ := CounterG { counter_tokG :> authG heap_lang Σ mnatUR }.
Definition counterGF : gFunctorList := [authGF mnatUR].
Instance inGF_counterG `{H : inGFs heap_lang Σ counterGF} : counterG Σ.
Proof. destruct H; split; apply _. Qed.

Section proof.
23
Context `{!heapG Σ, !counterG Σ} (N : namespace).
24 25 26 27 28
Local Notation iProp := (iPropG heap_lang Σ).

Definition counter_inv (l : loc) (n : mnat) : iProp := (l  #n)%I.

Definition counter (l : loc) (n : nat) : iProp :=
29 30
  ( γ, heapN  N  heap_ctx 
        auth_ctx γ N (counter_inv l)  auth_own γ (n:mnat))%I.
31 32 33 34 35

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

36
Lemma newcounter_spec (R : iProp) Φ :
37
  heapN  N 
38
  heap_ctx  ( l, counter l 0 - Φ #l)  WP newcounter #() {{ Φ }}.
39
Proof.
40 41
  iIntros (?) "[#Hh HΦ]". rewrite /newcounter. wp_seq. wp_alloc l as "Hl".
  iPvs (auth_alloc (counter_inv l) N _ (O:mnat) with "[Hl]")
42
    as (γ) "[#? Hγ]"; try by auto.
43 44 45 46 47 48 49
  iPvsIntro. iApply "HΦ". rewrite /counter; eauto 10.
Qed.

Lemma inc_spec l j (Φ : val  iProp) :
  counter l j  (counter l (S j) - Φ #())  WP inc #l {{ Φ }}.
Proof.
  iIntros "[Hl HΦ]". iLöb as "IH". wp_rec.
50
  iDestruct "Hl" as (γ) "(% & #? & #Hγ & Hγf)".
51
  wp_focus (! _)%E.
52
  iApply (auth_fsa (counter_inv l) (wp_fsa _) _ N); auto with fsaV.
53
  iIntros "{$Hγ $Hγf}"; iIntros (j') "[% Hl] /="; rewrite {2}/counter_inv.
54
  wp_load; iPvsIntro; iExists j; iSplit; [done|iIntros "{$Hl} Hγf"].
55
  wp_let; wp_op. wp_focus (CAS _ _ _).
56
  iApply (auth_fsa (counter_inv l) (wp_fsa _) _ N); auto with fsaV.
57
  iIntros "{$Hγ $Hγf}"; iIntros (j'') "[% Hl] /="; rewrite {2}/counter_inv.
58 59 60 61 62 63 64
  destruct (decide (j `max` j'' = j `max` j')) as [Hj|Hj].
  - wp_cas_suc; first (by do 3 f_equal); iPvsIntro.
    iExists (1 + j `max` j')%nat; iSplit.
    { iPureIntro. apply mnat_local_update. abstract lia. }
    rewrite {2}/counter_inv !mnat_op_max (Nat.max_l (S _)); last abstract lia.
    rewrite Nat2Z.inj_succ -Z.add_1_l.
    iIntros "{$Hl} Hγf". wp_if.
65
    iPvsIntro; iApply "HΦ"; iExists γ; repeat iSplit; eauto.
66 67 68 69 70 71 72 73 74 75
    iApply (auth_own_mono with "Hγf"). apply mnat_included. abstract lia.
  - wp_cas_fail; first (rewrite !mnat_op_max; by intros [= ?%Nat2Z.inj]).
    iPvsIntro. iExists j; iSplit; [done|iIntros "{$Hl} Hγf"].
    wp_if. iApply ("IH" with "[Hγf] HΦ"). rewrite {3}/counter; eauto 10.
Qed.

Lemma read_spec l j (Φ : val  iProp) :
  counter l j  ( i,  (j  i)%nat  counter l i - Φ #i)
   WP read #l {{ Φ }}.
Proof.
76
  iIntros "[Hc HΦ]". iDestruct "Hc" as (γ) "(% & #? & #Hγ & Hγf)".
77
  rewrite /read. wp_let.
78
  iApply (auth_fsa (counter_inv l) (wp_fsa _) _ N); auto with fsaV.
79
  iIntros "{$Hγ $Hγf}"; iIntros (j') "[% Hl] /=".
80 81 82 83 84 85
  wp_load; iPvsIntro; iExists (j `max` j'); iSplit.
  { iPureIntro; apply mnat_local_update; abstract lia. }
  rewrite !mnat_op_max -Nat.max_assoc Nat.max_idempotent; iIntros "{$Hl} Hγf".
  iApply ("HΦ" with "[%]"); first abstract lia; rewrite /counter; eauto 10.
Qed.
End proof.