Library examples.pool_counter

Require Import iris.algebra.numbers.

Require Import zoo.prelude.
Require Import zoo.iris.bi.big_op.
Require Import zoo.iris.base_logic.lib.cinv.
Require Import zoo.iris.base_logic.lib.auth_frac.
Require Import zoo.base.
Require Export examples.pool_counter__code.
Require Import examples.pool_counter__types.
Require Import zoo.options.

Implicit Type n cnt contrib : nat.
Implicit Type r : location.
Implicit Type γ η : gname.

Class PoolCounterG Σ `{zoo۰G : !ZooG Σ} :=
  { #[local] pool_counter۰G۰pool۰G :: PoolG Σ
  ; #[local] pool_counter۰G۰cinv۰G :: cinvG Σ
  ; #[local] pool_counter۰G۰tokens۰G :: AuthFracG Σ natUR
  }.

Definition pool_counter۰Σ :=
  #[pool۰Σ
  ; cinvΣ
  ; auth_frac۰Σ natUR
  ].
#[global] Instance subGpool_counter۰Σ Σ `{zoo۰G : !ZooG Σ} :
  subG pool_counter۰Σ Σ
  PoolCounterG Σ.

Section pool_counter۰G.
  Context `{pool_counter۰G : PoolCounterG Σ}.

  #[local] Definition tokens۰auth γ cnt :=
    auth_frac۰auth γ cnt.
  #[local] Definition tokens۰frag γ n contrib :=
    auth_frac۰frag γ (1 / Qp۰of_nat n) contrib.

  #[local] Definition inv۰inner r γ : iProp Σ :=
     cnt : nat,
    r ↦ᵣ #cnt
    tokens۰auth γ cnt.
  #[local] Instance : CustomIpat "inv۰inner" :=
    " ( %cnt & >Hr & >Htokens_auth ) ".
  #[local] Definition inv r γ η :=
    cinv nroot η (inv۰inner r γ).

  #[local] Lemma tokensalloc n :
     |==>
       γ,
      tokens۰auth γ 0
      [∗ list] _ seq 0 n, tokens۰frag γ n 0.
  #[local] Lemma tokensincr γ cnt n contrib :
    tokens۰auth γ cnt -∗
    tokens۰frag γ n contrib ==∗
      tokens۰auth γ (cnt + 1)
      tokens۰frag γ n (contrib + 1).
  #[local] Lemma tokensagree γ cnt n :
    0 < n
    tokens۰auth γ cnt -∗
    ([∗ list] _ seq 0 n, tokens۰frag γ n 1) -∗
    cnt = n.

  Lemma pool_counter٠mainspec (num_dom n : nat) :
    0 < n
    {{{
      True
    }}}
      pool_counter٠main #num_dom #n
    {{{
      RET #n;
      True
    }}}.
End pool_counter۰G.

Require examples.pool_counter__opaque.