Library zoo_saturn.mpmc_queue_1

From iris.base_logic Require Import
  lib.ghost_map.

From zoo Require Import
  prelude.
From zoo.common Require Import
  countable.
From zoo.iris.bi Require Import
  big_op.
From zoo.iris.base_logic Require Import
  lib.mono_list
  lib.auth_nat_max
  lib.twins
  lib.saved_pred.
From zoo.language Require Import
  notations.
From zoo.diaframe Require Import
  diaframe.
From zoo_std Require Import
  option
  xtchain
  domain.
From zoo_saturn Require Export
  base
  mpmc_queue_1__code.
From zoo_saturn Require Import
  mpmc_queue_1__types.
From zoo Require Import
  options.

Implicit Types b : bool.
Implicit Types front node back new_back : location.
Implicit Types hist past nodes : list location.
Implicit Types v : val.
Implicit Types vs : list val.
Implicit Types waiter : gname.
Implicit Types waiters : gmap gname nat.

Class MpmcQueue1G Σ `{zoo_G : !ZooG Σ} :=
  { #[local] mpmc_queue_1_G_history_G :: MonoListG Σ location
  ; #[local] mpmc_queue_1_G_front_G :: AuthNatMaxG Σ
  ; #[local] mpmc_queue_1_G_model_G :: TwinsG Σ (leibnizO (list val))
  ; #[local] mpmc_queue_1_G_waiters_G :: ghost_mapG Σ gname nat
  ; #[local] mpmc_queue_1_G_saved_pred_G :: SavedPredG Σ bool
  }.

Definition mpmc_queue_1_Σ :=
  #[mono_list_Σ location
  ; auth_nat_max_Σ
  ; twins_Σ (leibnizO (list val))
  ; ghost_mapΣ gname nat
  ; saved_pred_Σ bool
  ].
#[global] Instance subG_mpmc_queue_1_Σ Σ `{zoo_G : !ZooG Σ} :
  subG mpmc_queue_1_Σ Σ
  MpmcQueue1G Σ.

Module base.
  Section mpmc_queue_1_G.
    Context `{mpmc_queue_1_G : MpmcQueue1G Σ}.

    Implicit Types t : location.

    Record mpmc_queue_1_name :=
    { mpmc_queue_1_name_inv : namespace
    ; mpmc_queue_1_name_history : gname
    ; mpmc_queue_1_name_front : gname
    ; mpmc_queue_1_name_model : gname
    ; mpmc_queue_1_name_waiters : gname
    }.
    Implicit Type γ : mpmc_queue_1_name.

    #[global] Instance mpmc_queue_1_name_eq_dec : EqDecision mpmc_queue_1_name :=
      ltac:(solve_decision).
    #[global] Instance mpmc_queue_1_name_countable :
      Countable mpmc_queue_1_name.

    #[local] Definition history_auth' γ_history hist :=
      mono_list_auth γ_history (DfracOwn 1) hist.
    #[local] Definition history_auth γ hist :=
      history_auth' γ.(mpmc_queue_1_name_history) hist.
    #[local] Definition history_at γ i node :=
      mono_list_at γ.(mpmc_queue_1_name_history) i node.

    #[local] Definition front_auth' γ_front i :=
      auth_nat_max_auth γ_front (DfracOwn 1) i.
    #[local] Definition front_auth γ i :=
      front_auth' γ.(mpmc_queue_1_name_front) i.
    #[local] Definition front_lb γ i :=
      auth_nat_max_lb γ.(mpmc_queue_1_name_front) i.

    #[local] Definition model₁' γ_model vs :=
      twins_twin1 γ_model (DfracOwn 1) vs.
    #[local] Definition model₁ γ vs :=
      model₁' γ.(mpmc_queue_1_name_model) vs.
    #[local] Definition model₂' γ_model vs :=
      twins_twin2 γ_model vs.
    #[local] Definition model₂ γ vs :=
      model₂' γ.(mpmc_queue_1_name_model) vs.

    #[local] Definition waiters_auth' γ_waiters waiters :=
      ghost_map_auth γ_waiters 1 waiters.
    #[local] Definition waiters_auth γ waiters :=
      waiters_auth' γ.(mpmc_queue_1_name_waiters) waiters.
    #[local] Definition waiters_at γ waiter i :=
      ghost_map_elem γ.(mpmc_queue_1_name_waiters) waiter (DfracOwn 1) i.

    #[local] Definition node_model γ node i b : iProp Σ :=
      node ↦ₕ Header §Node 2
      history_at γ i node
      if b then front_lb γ i else True%I.
    #[local] Instance : CustomIpat "node_model" :=
      " ( #H{}_header & #Hhistory_at_{} & {{front}#Hfront_lb_{};_} ) ".

    #[local] Definition waiter_au γ (Ψ : bool iProp Σ) : iProp Σ :=
      AU <{
        ∃∃ vs,
        model₁ γ vs
      }> @ γ.(mpmc_queue_1_name_inv), <{
        model₁ γ vs
      , COMM
        Ψ (bool_decide (vs = []))
      }>.
    #[local] Definition waiter_model γ past waiter i : iProp Σ :=
       Ψ,
      saved_pred waiter Ψ
      if decide (i < length past) then
        Ψ false
      else
        waiter_au γ Ψ.

    #[local] Definition inv_inner t γ : iProp Σ :=
       hist past front nodes back vs waiters,
      hist = past ++ front :: nodes
      back hist
      t.[front] #front
      t.[back] #back
      xtchain (Header §Node 2) (DfracOwn 1) hist §Null
      ([∗ list] node; v nodes; vs, node.[data] v)
      history_auth γ hist
      front_auth γ (length past)
      model₂ γ vs
      waiters_auth γ waiters
      ([∗ map] waiter i waiters, waiter_model γ past waiter i).
    #[local] Instance : CustomIpat "inv_inner" :=
      " ( %hist{} & %past{} & %front{} & %nodes{} & %back{} & %vs{} & %waiters{} & >%Hhist{} & >%Hback{} & >Ht_front & >Ht_back & >Hhist & >Hnodes & >Hhistory_auth & >Hfront_auth & >Hmodel₂ & >Hwaiters_auth & Hwaiters ) ".
    #[local] Definition inv' t γ :=
      inv γ.(mpmc_queue_1_name_inv) (inv_inner t γ).
    Definition mpmc_queue_1_inv t γ ι : iProp Σ :=
      ι = γ.(mpmc_queue_1_name_inv)
      inv' t γ.
    #[local] Instance : CustomIpat "inv" :=
      " ( -> & #Hinv ) ".

    Definition mpmc_queue_1_model :=
      model₁.
    #[local] Instance : CustomIpat "model" :=
      " Hmodel₁{_{}} ".

    #[global] Instance mpmc_queue_1_model_timeless γ vs :
      Timeless (mpmc_queue_1_model γ vs).

    #[global] Instance mpmc_queue_1_inv_persistent t γ ι :
      Persistent (mpmc_queue_1_inv t γ ι).

    #[local] Lemma history_alloc front :
       |==>
         γ_history,
        history_auth' γ_history [front].
    #[local] Lemma history_at_get {γ hist} i node :
      hist !! i = Some node
      history_auth γ hist
      history_at γ i node.
    #[local] Lemma history_at_lookup γ hist i node :
      history_auth γ hist -∗
      history_at γ i node -∗
      hist !! i = Some node.
    #[local] Lemma history_update {γ hist} node :
      history_auth γ hist |==>
        history_auth γ (hist ++ [node])
        history_at γ (length hist) node.

    #[local] Lemma front_alloc :
       |==>
         γ_front,
        front_auth' γ_front 0.
    #[local] Lemma front_lb_get γ i :
      front_auth γ i
      front_lb γ i.
    #[local] Lemma front_lb_valid γ i1 i2 :
      front_auth γ i1 -∗
      front_lb γ i2 -∗
      i2 i1.
    #[local] Lemma front_update {γ i} i' :
      i i'
      front_auth γ i |==>
      front_auth γ i'.

    #[local] Lemma model_alloc :
       |==>
         γ_model,
        model₁' γ_model []
        model₂' γ_model [].
    #[local] Lemma model₁_exclusive γ vs1 vs2 :
      model₁ γ vs1 -∗
      model₁ γ vs2 -∗
      False.
    #[local] Lemma model_agree γ vs1 vs2 :
      model₁ γ vs1 -∗
      model₂ γ vs2 -∗
      vs1 = vs2.
    #[local] Lemma model_update {γ vs1 vs2} vs :
      model₁ γ vs1 -∗
      model₂ γ vs2 ==∗
        model₁ γ vs
        model₂ γ vs.

    #[local] Lemma waiters_alloc :
       |==>
         γ_waiters,
        waiters_auth' γ_waiters .
    #[local] Lemma waiters_insert {γ waiters} i Ψ :
      waiters_auth γ waiters |==>
         waiter,
        waiters_auth γ (<[waiter := i]> waiters)
        saved_pred waiter Ψ
        waiters_at γ waiter i.
    #[local] Lemma waiters_delete γ waiters waiter i :
      waiters_auth γ waiters -∗
      waiters_at γ waiter i ==∗
        waiters !! waiter = Some i
        waiters_auth γ (delete waiter waiters).

    Lemma mpmc_queue_1_model_exclusive γ vs1 vs2 :
      mpmc_queue_1_model γ vs1 -∗
      mpmc_queue_1_model γ vs2 -∗
      False.

    Lemma mpmc_queue_1٠create𑁒spec ι :
      {{{
        True
      }}}
        mpmc_queue_1٠create ()
      {{{
        t γ
      , RET #t;
        meta_token t
        mpmc_queue_1_inv t γ ι
        mpmc_queue_1_model γ []
      }}}.

    #[local] Lemma front𑁒spec_strong Ψ t γ :
      {{{
        inv' t γ
        if Ψ is Some Ψ then
          waiter_au γ Ψ
        else
          True
      }}}
        (#t).{front}
      {{{
        front i
      , RET #front;
        node_model γ front i true
        if Ψ is Some Ψ then
           waiter,
          saved_pred waiter Ψ
          waiters_at γ waiter i
        else
          True
      }}}.
    #[local] Lemma front𑁒spec t γ :
      {{{
        inv' t γ
      }}}
        (#t).{front}
      {{{
        front i
      , RET #front;
        node_model γ front i true
      }}}.

    #[local] Lemma back𑁒spec t γ :
      {{{
        inv' t γ
      }}}
        (#t).{back}
      {{{
        back i
      , RET #back;
        node_model γ back i false
      }}}.

    Variant operation :=
      | IsEmpty waiter (Ψ : bool iProp Σ)
      | Pop (Ψ : option val iProp Σ)
      | Other.
    Implicit Types op : operation.
    Variant operation' :=
      | IsEmpty'
      | Pop'
      | Other'.
    #[local] Instance operation'_eq_dec : EqDecision operation' :=
      ltac:(solve_decision).
    #[local] Coercion operation_to_operation' op :=
      match op with
      | IsEmpty _ _
          IsEmpty'
      | Pop _
          Pop'
      | Other
          Other'
      end.
    #[local] Definition pop_au γ (Ψ : option val iProp Σ) : iProp Σ :=
      AU <{
        ∃∃ vs,
        model₁ γ vs
      }> @ γ.(mpmc_queue_1_name_inv), <{
        model₁ γ (tail vs)
      , COMM
        Ψ (head vs)
      }>.
    #[local] Lemma next𑁒spec_aux op t γ i node :
      {{{
        inv' t γ
        history_at γ i node
        ( if decide (op = Other' :> operation') then True else
            front_lb γ i
        )
        match op with
        | IsEmpty waiter Ψ
            saved_pred waiter Ψ
            waiters_at γ waiter i
            £ 1
        | Pop Ψ
            pop_au γ Ψ
        | Other
            True
        end
      }}}
        (#node).{next}
      {{{
        res
      , RET res;
          res = §Null%V
          match op with
          | IsEmpty waiter Ψ
              Ψ true
          | Pop Ψ
              Ψ None
          | Other
              True
          end
         node',
          res = #node'
          node_model γ node' (S i) false
          match op with
          | IsEmpty waiter Ψ
              Ψ false
          | Pop Ψ
              pop_au γ Ψ
          | Other
              True
          end
      }}}.
    #[local] Lemma next𑁒spec {t γ i} node :
      {{{
        inv' t γ
        history_at γ i node
      }}}
        (#node).{next}
      {{{
        res
      , RET res;
          res = §Null%V
         node',
          res = #node'
          node_model γ node' (S i) false
      }}}.
    #[local] Lemma next𑁒spec_is_empty {t γ i node} waiter Ψ :
      {{{
        inv' t γ
        history_at γ i node
        front_lb γ i
        saved_pred waiter Ψ
        waiters_at γ waiter i
        £ 1
      }}}
        (#node).{next}
      {{{
        res
      , RET res;
          res = §Null%V
          Ψ true
         node',
          res = #node'
          node_model γ node' (S i) false
          Ψ false
      }}}.
    #[local] Lemma next𑁒spec_pop {t γ i node} Ψ :
      {{{
        inv' t γ
        history_at γ i node
        front_lb γ i
        pop_au γ Ψ
      }}}
        (#node).{next}
      {{{
        res
      , RET res;
          res = §Null%V
          Ψ None
         node',
          res = #node'
          node_model γ node' (S i) false
          pop_au γ Ψ
      }}}.

    Lemma mpmc_queue_1٠is_empty𑁒spec t γ ι :
      <<<
        mpmc_queue_1_inv t γ ι
      | ∀∀ vs,
        mpmc_queue_1_model γ vs
      >>>
        mpmc_queue_1٠is_empty #t @ ι
      <<<
        mpmc_queue_1_model γ vs
      | RET #(bool_decide (vs = []%list));
        £ 1
      >>>.
    Lemma mpmc_queue_1٠is_empty𑁒spec' t γ ι :
      {{{
        mpmc_queue_1_inv t γ ι
      }}}
        mpmc_queue_1٠is_empty #t
      {{{
        b
      , RET #b;
        True
      }}}.

    #[local] Lemma mpmc_queue_1٠push₀𑁒spec t γ i node new_back v :
      <<<
        inv' t γ
        node_model γ node i false
        new_back ↦ₕ Header §Node 2
        new_back.[next] §Null
        new_back.[data] v
      | ∀∀ vs,
        mpmc_queue_1_model γ vs
      >>>
        mpmc_queue_1٠push₀ #node #new_back @ γ.(mpmc_queue_1_name_inv)
      <<<
        mpmc_queue_1_model γ (vs ++ [v])
      | RET ();
         j,
        history_at γ j new_back
      >>>.

    #[local] Lemma mpmc_queue_1٠fix_back𑁒spec t γ i back j new_back :
      {{{
        inv' t γ
        history_at γ i back
        node_model γ new_back j false
      }}}
        mpmc_queue_1٠fix_back #t #back #new_back
      {{{
        RET ();
        True
      }}}.

    Lemma mpmc_queue_1٠push𑁒spec t γ ι v :
      <<<
        mpmc_queue_1_inv t γ ι
      | ∀∀ vs,
        mpmc_queue_1_model γ vs
      >>>
        mpmc_queue_1٠push #t v @ ι
      <<<
        mpmc_queue_1_model γ (vs ++ [v])
      | RET ();
        £ 1
      >>>.

    Lemma mpmc_queue_1٠pop𑁒spec t γ ι :
      <<<
        mpmc_queue_1_inv t γ ι
      | ∀∀ vs,
        mpmc_queue_1_model γ vs
      >>>
        mpmc_queue_1٠pop #t @ ι
      <<<
        mpmc_queue_1_model γ (tail vs)
      | RET head vs;
        £ 1
      >>>.
  End mpmc_queue_1_G.

  #[global] Opaque mpmc_queue_1_inv.
  #[global] Opaque mpmc_queue_1_model.
End base.

From zoo_saturn Require
  mpmc_queue_1__opaque.

Section mpmc_queue_1_G.
  Context `{mpmc_queue_1_G : MpmcQueue1G Σ}.

  Implicit Types 𝑡 : location.
  Implicit Types t : val.

  Definition mpmc_queue_1_inv t ι : iProp Σ :=
     𝑡 γ,
    t = #𝑡
    meta 𝑡 nroot γ
    base.mpmc_queue_1_inv 𝑡 γ ι.
  #[local] Instance : CustomIpat "inv" :=
    " ( %𝑡{} & %γ{} & {%Heq{};->} & #Hmeta{_{}} & Hinv{_{}} ) ".

  Definition mpmc_queue_1_model t vs : iProp Σ :=
     𝑡 γ,
    t = #𝑡
    meta 𝑡 nroot γ
    base.mpmc_queue_1_model γ vs.
  #[local] Instance : CustomIpat "model" :=
    " ( %𝑡{} & %γ{} & {%Heq{};->} & #Hmeta{_{}} & Hmodel{_{}} ) ".

  #[global] Instance mpmc_queue_1_model_timeless t vs :
    Timeless (mpmc_queue_1_model t vs).

  #[global] Instance mpmc_queue_1_inv_persistent t ι :
    Persistent (mpmc_queue_1_inv t ι).

  Lemma mpmc_queue_1_model_exclusive t vs1 vs2 :
    mpmc_queue_1_model t vs1 -∗
    mpmc_queue_1_model t vs2 -∗
    False.

  Lemma mpmc_queue_1٠create𑁒spec ι :
    {{{
      True
    }}}
      mpmc_queue_1٠create ()
    {{{
      t
    , RET t;
      mpmc_queue_1_inv t ι
      mpmc_queue_1_model t []
    }}}.

  Lemma mpmc_queue_1٠is_empty𑁒spec t ι :
    <<<
      mpmc_queue_1_inv t ι
    | ∀∀ vs,
      mpmc_queue_1_model t vs
    >>>
      mpmc_queue_1٠is_empty t @ ι
    <<<
      mpmc_queue_1_model t vs
    | RET #(bool_decide (vs = []%list));
      £ 1
    >>>.
  Lemma mpmc_queue_1٠is_empty𑁒spec' t ι :
    {{{
      mpmc_queue_1_inv t ι
    }}}
      mpmc_queue_1٠is_empty t
    {{{
      b
    , RET #b;
      True
    }}}.

  Lemma mpmc_queue_1٠push𑁒spec t ι v :
    <<<
      mpmc_queue_1_inv t ι
    | ∀∀ vs,
      mpmc_queue_1_model t vs
    >>>
      mpmc_queue_1٠push t v @ ι
    <<<
      mpmc_queue_1_model t (vs ++ [v])
    | RET ();
      £ 1
    >>>.

  Lemma mpmc_queue_1٠pop𑁒spec t ι :
    <<<
      mpmc_queue_1_inv t ι
    | ∀∀ vs,
      mpmc_queue_1_model t vs
    >>>
      mpmc_queue_1٠pop t @ ι
    <<<
      mpmc_queue_1_model t (tail vs)
    | RET head vs;
      £ 1
    >>>.
End mpmc_queue_1_G.

#[global] Opaque mpmc_queue_1_inv.
#[global] Opaque mpmc_queue_1_model.

Section mpmc_queue_1_G.
  Context `{mpmc_queue_1_G : MpmcQueue1G Σ}.
  Context τ `{!iType (iProp Σ) τ}.

  #[local] Definition itype_inner t : iProp Σ :=
     vs,
    mpmc_queue_1_model t vs
    [∗ list] v vs, τ v.
  #[local] Instance : CustomIpat "itype_inner" :=
    " ( %vs & >Hmodel & #Hvs ) ".
  Definition itype_mpmc_queue_1 t : iProp Σ :=
    mpmc_queue_1_inv t (nroot.@"1")
    inv (nroot.@"2") (itype_inner t).
  #[local] Instance : CustomIpat "itype" :=
    " ( #Hinv1 & #Hinv2 ) ".

  #[global] Instance itype_mpmc_queue_1_itype :
    iType _ itype_mpmc_queue_1.

  Lemma mpmc_queue_1٠create𑁒type :
    {{{
      True
    }}}
      mpmc_queue_1٠create ()
    {{{
      t
    , RET t;
      itype_mpmc_queue_1 t
    }}}.

  Lemma mpmc_queue_1٠is_empty𑁒type t :
    {{{
      itype_mpmc_queue_1 t
    }}}
      mpmc_queue_1٠is_empty t
    {{{
      b
    , RET #b;
      True
    }}}.

  Lemma mpmc_queue_1٠push𑁒type t v :
    {{{
      itype_mpmc_queue_1 t
      τ v
    }}}
      mpmc_queue_1٠push t v
    {{{
      RET ();
      True
    }}}.

  Lemma mpmc_queue_1٠pop𑁒type t :
    {{{
      itype_mpmc_queue_1 t
    }}}
      mpmc_queue_1٠pop t
    {{{
      o
    , RET o;
      itype_option τ o
    }}}.
End mpmc_queue_1_G.

#[global] Opaque itype_mpmc_queue_1.