(* iris/base_logic/lib/mono_Z.v *)
From iris.proofmode Require Import proofmode
From iris.algebra.lib Require Import mono_nat
From iris.bi.lib Require Import fractional
From iris.base_logic.lib Require Export own
From iris.base_logic.lib Require Import mono_nat
From iris.prelude Require Import options

Local OpenScope Z_scope.

Class mono_ZGΣ :=
  MonoZG { #[local] mono_ZG_natG :: mono_natG Σ; }.

Definition mono_ZΣ:= mono_natΣ.

Local Definitionmono_Z_auth_own_def `{!mono_ZG Σ}
    (γ : gname) (q : Qp) (n : Z) : iProp Σ :=
  ⌜0 ≤ n⌝ ∗ mono_nat_auth_own γ q (Z.to_nat n).

Local Definitionmono_Z_auth_own_aux : seal (@mono_Z_auth_own_def).
Proof. by eexists. Qed.

Definition mono_Z_auth_own:= mono_Z_auth_own_aux.(unseal).

Local Definitionmono_Z_auth_own_unseal :
  @mono_Z_auth_own = @mono_Z_auth_own_def := mono_Z_auth_own_aux.(seal_eq).

Global Argumentsmono_Z_auth_own {Σ _} γ q n.

Local Definitionmono_Z_lb_own_def `{!mono_ZG Σ} (γ : gname) (n : Z) : iProp Σ :=
  ⌜0 ≤ n⌝ ∗ mono_nat_lb_own γ (Z.to_nat n).

Local Definitionmono_Z_lb_own_aux : seal (@mono_Z_lb_own_def). Proof. by eexists. Qed.

Definition mono_Z_lb_own:= mono_Z_lb_own_aux.(unseal).

Local Definitionmono_Z_lb_own_unseal :
  @mono_Z_lb_own = @mono_Z_lb_own_def := mono_Z_lb_own_aux.(seal_eq).

Global Argumentsmono_Z_lb_own {Σ _} γ n.

Local Ltacunseal := rewrite
  ?mono_Z_auth_own_unseal /mono_Z_auth_own_def
  ?mono_Z_lb_own_unseal /mono_Z_lb_own_def.

Global Instancemono_Z_auth_own_timeless γ q n : Timeless (mono_Z_auth_own γ q n).
  Proof. unseal. apply _. Qed.

Global Instancemono_Z_lb_own_timeless γ n : Timeless (mono_Z_lb_own γ n).
  Proof. unseal. apply _. Qed.

Global Instancemono_Z_lb_own_persistent γ n : Persistent (mono_Z_lb_own γ n).
  Proof. unseal. apply _. Qed.

Global Instancemono_Z_auth_own_fractional γ n :
    Fractional (λ q, mono_Z_auth_own γ q n).
  Proof.
    unseal. intros p q. iSplit.
    - iIntros "[% [$ $]]". eauto.
    - iIntros "[[% H1] [% H2]]". iCombine "H1 H2" as "$". eauto.
  Qed.

Global Instancemono_Z_auth_own_as_fractional γ q n :
    AsFractional (mono_Z_auth_own γ q n) (λ q, mono_Z_auth_own γ q n) q.
  Proof. split; [auto|apply _]. Qed.

Lemma mono_Z_auth_own_agreeγ q1 q2 n1 n2 :
    mono_Z_auth_own γ q1 n1 -∗
    mono_Z_auth_own γ q2 n2 -∗
    ⌜(q1 + q2 ≤ 1)%Qp ∧ n1 = n2⌝.
  Proof.
    unseal. iIntros "[% H1] [% H2]".
    iDestruct (mono_nat_auth_own_agree with "H1 H2") as %?.
    iPureIntro. naive_solver lia.
  Qed.

Lemma mono_Z_auth_own_exclusiveγ n1 n2 :
    mono_Z_auth_own γ 1 n1 -∗ mono_Z_auth_own γ 1 n2 -∗ False.
  Proof.
    iIntros "H1 H2".
    by iDestruct (mono_Z_auth_own_agree with "H1 H2") as %[[] _].
  Qed.

Lemma mono_Z_lb_own_validγ q n m :
    mono_Z_auth_own γ q n -∗ mono_Z_lb_own γ m -∗ ⌜(q ≤ 1)%Qp ∧ m ≤ n⌝.
  Proof.
    unseal. iIntros "[% Hauth] [% Hlb]".
    iDestruct (mono_nat_lb_own_valid with "Hauth Hlb") as %Hvalid.
    iPureIntro. naive_solver lia.
  Qed.

Lemma mono_Z_lb_own_getγ q n :
    mono_Z_auth_own γ q n -∗ mono_Z_lb_own γ n.
  Proof. unseal. iIntros "[% ?]". iSplit; first done. by iApply mono_nat_lb_own_get. Qed.

Lemma mono_Z_lb_own_le{γ n} n' :
    n' ≤ n →
    0 ≤ n' →
    mono_Z_lb_own γ n -∗ mono_Z_lb_own γ n'.
  Proof.
    unseal. iIntros "% % [% ?]". iSplit; first done.
    iApply mono_nat_lb_own_le; last done. lia.
  Qed.

Lemma mono_Z_lb_own_0γ :
    ⊢ |==> mono_Z_lb_own γ 0.
  Proof. unseal. iMod mono_nat_lb_own_0 as "$". eauto. Qed.

Lemma mono_Z_own_allocn :
    0 ≤ n →
    ⊢ |==> ∃ γ, mono_Z_auth_own γ 1 n ∗ mono_Z_lb_own γ n.
  Proof.
    unseal. intros. iMod mono_nat_own_alloc as (γ) "[??]".
    iModIntro. iExists _. iFrame. eauto.
  Qed.

Lemma mono_Z_own_update{γ n} n' :
    n ≤ n' →
    mono_Z_auth_own γ 1 n ==∗ mono_Z_auth_own γ 1 n' ∗ mono_Z_lb_own γ n'.
  Proof.
    iIntros (?) "Hauth".
    iAssert (mono_Z_auth_own γ 1 n') with "[> Hauth]" as "Hauth".
    { unseal. iDestruct "Hauth" as "[% Hauth]".
      iMod (mono_nat_own_update with "Hauth") as "[$ _]"; auto with lia. }
    iModIntro. iSplit; [done|]. by iApply mono_Z_lb_own_get.
  Qed.