(* iris/algebra/lib/mono_Z.v *)
From iris.algebra Require Export auth
From iris.algebra Require Import numbers updates
From iris.prelude Require Import options

Definition mono_Z{SI: sidx} := auth (option max_ZR).

Definition mono_ZR{SI: sidx} := authR (optionUR max_ZR).

Definition mono_ZUR{SI: sidx} := authUR (optionUR max_ZR).

Definition mono_Z_auth{SI : sidx} (dq : dfrac) (n : Z) : mono_Z :=
  ●{dq} (Some (MaxZ n)) ⋅ ◯ (Some (MaxZ n)).

Definition mono_Z_lb{SI : sidx} (n : Z) : mono_Z := ◯ (Some (MaxZ n)).

Local OpenScope Z_scope.

Global Instancemono_Z_lb_core_id n : CoreId (◯MZ n).
  Proof. apply _. Qed.

Global Instancemono_Z_auth_core_id l : CoreId (●MZ□ l).
  Proof. apply _. Qed.

Lemma mono_Z_auth_dfrac_opdq1 dq2 n :
    ●MZ{dq1 ⋅ dq2} n ≡ ●MZ{dq1} n ⋅ ●MZ{dq2} n.
  Proof.
    rewrite /mono_Z_auth auth_auth_dfrac_op.
    rewrite (comm _ (●{dq2} _)) -!assoc (assoc _ (◯ _)).
    by rewrite -core_id_dup (comm _ (◯ _)).
  Qed.

Lemma mono_Z_lb_opn1 n2 :
    ◯MZ (n1 `max` n2) = ◯MZ n1 ⋅ ◯MZ n2.
  Proof. rewrite -auth_frag_op -Some_op max_Z_op //. Qed.

Lemma mono_Z_auth_lb_opdq n :
    ●MZ{dq} n ≡ ●MZ{dq} n ⋅ ◯MZ n.
  Proof.
    rewrite /mono_Z_auth /mono_Z_lb.
    rewrite -!assoc -auth_frag_op -Some_op max_Z_op.
    rewrite Z.max_id //.
  Qed.

Global Instancemono_Z_auth_dfrac_is_op dq dq1 dq2 n :
    IsOp dq dq1 dq2 → IsOp' (●MZ{dq} n) (●MZ{dq1} n) (●MZ{dq2} n).
  Proof. rewrite /IsOp' /IsOp=> ->. rewrite mono_Z_auth_dfrac_op //. Qed.

Global Instancemono_Z_lb_max_is_op n n1 n2 :
    IsOp (MaxZ n) (MaxZ n1) (MaxZ n2) → IsOp' (◯MZ n) (◯MZ n1) (◯MZ n2).
  Proof. rewrite /IsOp' /IsOp /mono_Z_lb=> ->. done. Qed.

Lemma mono_Z_lb_op_le_ln n' :
    n' ≤ n →
    ◯MZ n = ◯MZ n' ⋅ ◯MZ n.
  Proof. intros. rewrite -mono_Z_lb_op Z.max_r //. Qed.

Lemma mono_Z_auth_dfrac_validdq n : (✓ ●MZ{dq} n) ↔ ✓ dq.
  Proof.
    rewrite /mono_Z_auth auth_both_dfrac_valid_discrete /=. naive_solver.
  Qed.

Lemma mono_Z_auth_validn : ✓ ●MZ n.
  Proof. by apply auth_both_valid. Qed.

Lemma mono_Z_auth_dfrac_op_validdq1 dq2 n1 n2 :
    ✓ (●MZ{dq1} n1 ⋅ ●MZ{dq2} n2) ↔ ✓ (dq1 ⋅ dq2) ∧ n1 = n2.
  Proof.
    rewrite /mono_Z_auth (comm _ (●{dq2} _)) -!assoc (assoc _ (◯ _)).
    rewrite -auth_frag_op (comm _ (◯ _)) assoc. split.
    - move=> /cmra_valid_op_l /auth_auth_dfrac_op_valid. naive_solver.
    - intros [? ->]. rewrite -core_id_dup -auth_auth_dfrac_op.
      by apply auth_both_dfrac_valid_discrete.
  Qed.

Lemma mono_Z_auth_op_validn1 n2 :
    ✓ (●MZ n1 ⋅ ●MZ n2) ↔ False.
  Proof. rewrite mono_Z_auth_dfrac_op_valid. naive_solver. Qed.

Lemma mono_Z_both_dfrac_validdq n m :
    ✓ (●MZ{dq} n ⋅ ◯MZ m) ↔ ✓ dq ∧ m ≤ n.
  Proof.
    rewrite /mono_Z_auth /mono_Z_lb -assoc -auth_frag_op.
    rewrite auth_both_dfrac_valid_discrete Some_included_total max_Z_included /=.
    naive_solver lia.
  Qed.

Lemma mono_Z_both_validn m :
    ✓ (●MZ n ⋅ ◯MZ m) ↔ m ≤ n.
  Proof. rewrite mono_Z_both_dfrac_valid dfrac_valid_own. naive_solver. Qed.

Lemma mono_Z_lb_monon1 n2 : n1 ≤ n2 → ◯MZ n1 ≼ ◯MZ n2.
  Proof.
    intros. by apply auth_frag_mono, Some_included_total, max_Z_included.
  Qed.

Lemma mono_Z_includeddq n : ◯MZ n ≼ ●MZ{dq} n.
  Proof. apply: cmra_included_r. Qed.

Lemma mono_Z_update{n} n' :
    n ≤ n' → ●MZ n ~~> ●MZ n'.
  Proof.
    intros. rewrite /mono_Z_auth /mono_Z_lb.
    by apply auth_update, option_local_update, max_Z_local_update.
  Qed.

Lemma mono_Z_auth_persistn dq :
    ●MZ{dq} n ~~> ●MZ□ n.
  Proof.
    intros. rewrite /mono_Z_auth /mono_Z_lb.
    eapply cmra_update_op_proper; last done.
    eapply auth_update_auth_persist.
  Qed.

Lemma mono_Z_auth_unpersistn :
    ●MZ□ n ~~>: λ k, ∃ q, k = ●MZ{# q} n.
  Proof. eapply auth_updateP_both_unpersist. Qed.

Global TypeclassesOpaque mono_Z_auth mono_Z_lb.