Library zoo_saturn.queue_spmc

Require Import iris.base_logic.lib.ghost_map.

Require Import zoo.prelude.
Require Import zoo.common.relations.
Require Import zoo.common.countable.
Require Import zoo.iris.bi.big_op.
Require Import zoo.iris.base_logic.lib.mono_list.
Require Import zoo.iris.base_logic.lib.auth_nat_max.
Require Import zoo.iris.base_logic.lib.auth_twins.
Require Import zoo.iris.base_logic.lib.saved_pred.
Require Import zoo.base.
Require Import zoo_std.option.
Require Import zoo_std.xtchain.
Require Export zoo_saturn.queue_spmc__code.
Require Import zoo_saturn.queue_spmc__types.
Require Import zoo.options.

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

Class QueueSpmcG Σ `{zoo۰G : !ZooG Σ} :=
  { #[local] queue_spmc۰G۰history۰G :: MonoListG Σ location
  ; #[local] queue_spmc۰G۰front۰G :: AuthNatMaxG Σ
  ; #[local] queue_spmc۰G۰model۰G :: AuthTwinsG Σ (leibnizO (list val)) suffix
  ; #[local] queue_spmc۰G۰waiters۰G :: ghost_mapG Σ gname nat
  ; #[local] queue_spmc۰G۰saved_pred۰G :: SavedPredG Σ bool
  }.

Definition queue_spmc۰Σ :=
  #[mono_list۰Σ location
  ; auth_nat_max۰Σ
  ; auth_twins۰Σ (leibnizO (list val)) suffix
  ; ghost_mapΣ gname nat
  ; saved_pred۰Σ bool
  ].
#[global] Instance subGqueue_spmc۰Σ Σ `{zoo۰G : !ZooG Σ} :
  subG queue_spmc۰Σ Σ
  QueueSpmcG Σ.

Module base.
  Section queue_spmc۰G.
    Context `{queue_spmc۰G : QueueSpmcG Σ}.

    Implicit Type t : location.

    Record metadata :=
      { metadata۰inv : namespace
      ; metadata۰history : gname
      ; metadata۰front : gname
      ; metadata۰model : auth_twins۰name
      ; metadata۰waiters : gname
      }.
    Implicit Type γ : metadata.

    #[global] Instance metadataeq_dec : EqDecision metadata :=
      ltac:(solve_decision).
    #[global] Instance metadatacountable :
      Countable metadata.

    #[local] Definition history۰auth' γ_history hist :=
      mono_list۰auth γ_history (DfracOwn (1/2)) hist.
    #[local] Definition history۰auth γ hist :=
      history۰auth' γ.(metadata۰history) hist.
    #[local] Definition history۰last' γ_history node : iProp Σ :=
       hist,
      mono_list۰auth γ_history (DfracOwn (1/2)) hist
      last hist = Some node.
    #[local] Instance : CustomIpat "history۰last" :=
      " ( %hist{} & Hauth{_{}} & %Hlast ) ".
    #[local] Definition history۰last γ :=
      history۰last' γ.(metadata۰history).
    #[local] Definition history۰at γ i node :=
      mono_list۰at γ.(metadata۰history) i node.

    #[local] Definition front۰auth' γ_front i :=
      auth_nat_max۰auth γ_front (DfracOwn 1) i.
    #[local] Definition front۰auth γ i :=
      front۰auth' γ.(metadata۰front) i.
    #[local] Definition front۰lb γ i :=
      auth_nat_max۰lb γ.(metadata۰front) i.

    #[local] Definition producer' γ_model ws :=
      auth_twins۰auth _ γ_model ws.
    #[local] Definition producer γ :=
      producer' γ.(metadata۰model).

    #[local] Definition model₁' γ_model vs :=
      auth_twins۰twin₁ _ γ_model vs.
    #[local] Definition model₁ γ :=
      model₁' γ.(metadata۰model).
    #[local] Definition model₂' γ_model vs :=
      auth_twins۰twin₂ _ γ_model vs.
    #[local] Definition model₂ γ :=
      model₂' γ.(metadata۰model).

    #[local] Definition waiters۰auth' γ_waiters waiters :=
      ghost_map_auth γ_waiters 1 waiters.
    #[local] Definition waiters۰auth γ waiters :=
      waiters۰auth' γ.(metadata۰waiters) waiters.
    #[local] Definition waiters۰at γ waiter i :=
      ghost_map_elem γ.(metadata۰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
      }> @ γ.(metadata۰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 vs waiters,
      hist = past ++ front :: nodes
      t.[front] #front
      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{} & %vs{} & %waiters{} & >%Hhist{} & >Ht_front & >Hhist & >Hnodes & >Hhistory_auth & >Hfront_auth & >Hmodel₂ & >Hwaiters_auth & Hwaiters ) ".
    #[local] Definition inv' t γ :=
      inv γ.(metadata۰inv) (inv۰inner t γ).
    Definition queue_spmc۰inv t γ ι : iProp Σ :=
      ι = γ.(metadata۰inv)
      inv' t γ.
    #[local] Instance : CustomIpat "inv" :=
      " ( -> & #Hinv ) ".

    Definition queue_spmc۰producer t γ ws : iProp Σ :=
       back,
      t.[back] #back
      back ↦ₕ Header §Node 2
      history۰last γ back
      producer γ ws.
    #[local] Instance : CustomIpat "producer" :=
      " ( %back{} & Ht_back{_{}} & #Hback{}_header & Hhistory_last{_{}} & Hproducer{_{}} ) ".

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

    #[global] Instance queue_spmc۰modeltimeless γ vs :
      Timeless (queue_spmc۰model γ vs).
    #[global] Instance queue_spmc۰producertimeless t γ ws :
      Timeless (queue_spmc۰producer t γ ws).

    #[global] Instance queue_spmc۰invpersistent t γ ι :
      Persistent (queue_spmc۰inv t γ ι).

    #[local] Lemma historyalloc front :
       |==>
         γ_history,
        history۰auth' γ_history [front]
        history۰last' γ_history front.
    #[local] Lemma history۰atget {γ hist} i node :
      hist !! i = Some node
      history۰auth γ hist
      history۰at γ i node.
    #[local] Lemma history۰atlookup γ hist i node :
      history۰auth γ hist -∗
      history۰at γ i node -∗
      hist !! i = Some node.
    #[local] Lemma historyauthlast γ hist node :
      history۰auth γ hist -∗
      history۰last γ node -∗
      last hist = Some node.
    #[local] Lemma historyupdate {γ hist node} node' :
      history۰auth γ hist -∗
      history۰last γ node ==∗
        history۰auth γ (hist ++ [node'])
        history۰last γ node'.
    Opaque history۰last'.

    #[local] Lemma frontalloc :
       |==>
         γ_front,
        front۰auth' γ_front 0.
    #[local] Lemma front۰lbget γ i :
      front۰auth γ i
      front۰lb γ i.
    #[local] Lemma front۰lbvalid γ i1 i2 :
      front۰auth γ i1 -∗
      front۰lb γ i2 -∗
      i2 i1.
    #[local] Lemma frontupdate {γ i} i' :
      i i'
      front۰auth γ i |==>
      front۰auth γ i'.

    #[local] Lemma producervalid γ ws vs :
      producer γ ws -∗
      model₁ γ vs -∗
      vs `suffix_of` ws.
    #[local] Lemma producerexclusive γ ws1 ws2 :
      producer γ ws1 -∗
      producer γ ws2 -∗
      False.

    #[local] Lemma modelproduceralloc :
       |==>
         γ_model,
        producer' γ_model []
        model₁' γ_model []
        model₂' γ_model [].
    #[local] Lemma model₁exclusive γ vs1 vs2 :
      model₁ γ vs1 -∗
      model₁ γ vs2 -∗
      False.
    #[local] Lemma modelagree γ vs1 vs2 :
      model₁ γ vs1 -∗
      model₂ γ vs2 -∗
      vs1 = vs2.
    #[local] Lemma modelpush {γ ws vs1 vs2} v :
      producer γ ws -∗
      model₁ γ vs1 -∗
      model₂ γ vs2 ==∗
        producer γ (vs1 ++ [v])
        model₁ γ (vs1 ++ [v])
        model₂ γ (vs1 ++ [v]).
    #[local] Lemma modelpop γ v vs1 vs2 :
      model₁ γ (v :: vs1) -∗
      model₂ γ vs2 ==∗
        model₁ γ vs1
        model₂ γ vs1.

    #[local] Lemma waitersalloc :
       |==>
         γ_waiters,
        waiters۰auth' γ_waiters .
    #[local] Lemma waitersinsert {γ waiters} i Ψ :
      waiters۰auth γ waiters |==>
         waiter,
        waiters۰auth γ (<[waiter := i]> waiters)
        saved_pred waiter Ψ
        waiters۰at γ waiter i.
    #[local] Lemma waitersdelete γ waiters waiter i :
      waiters۰auth γ waiters -∗
      waiters۰at γ waiter i ==∗
        waiters !! waiter = Some i
        waiters۰auth γ (delete waiter waiters).

    Lemma queue_spmc۰modelexclusive γ vs1 vs2 :
      queue_spmc۰model γ vs1 -∗
      queue_spmc۰model γ vs2 -∗
      False.

    Lemma queue_spmc۰producervalid t γ vs ws :
      queue_spmc۰producer t γ ws -∗
      queue_spmc۰model γ vs -∗
      vs `suffix_of` ws.
    Lemma queue_spmc۰producerexclusive t γ ws1 ws2 :
      queue_spmc۰producer t γ ws1 -∗
      queue_spmc۰producer t γ ws2 -∗
      False.

    Lemma queue_spmc٠createspec ι :
      {{{
        True
      }}}
        queue_spmc٠create ()
      {{{
        t γ
      , RET #t;
        meta_token t
        queue_spmc۰inv t γ ι
        queue_spmc۰model γ []
        queue_spmc۰producer t γ []
      }}}.

    #[local] Lemma frontspecstrong Ψ 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 frontspec t γ :
      {{{
        inv' t γ
      }}}
        (#t).{front}
      {{{
        front i
      , RET #front;
        node۰model γ front i true
      }}}.

    Variant operation :=
      | IsEmpty waiter (Ψ : bool iProp Σ)
      | Pop (Ψ : option val iProp Σ)
      | Other.
    Implicit Type 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
      }> @ γ.(metadata۰inv), <{
        model₁ γ (tail vs)
      , COMM
        Ψ (head vs)
      }>.
    #[local] Lemma nextspecaux 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' ˖i false
          match op with
          | IsEmpty waiter Ψ
              Ψ false
          | Pop Ψ
              pop۰au γ Ψ
          | Other
              True
          end
      }}}.
    #[local] Lemma nextspec t γ i node :
      {{{
        inv' t γ
        history۰at γ i node
      }}}
        (#node).{next}
      {{{
        res
      , RET res;
          res = §Null%V
         node',
          res = #node'
          node۰model γ node' ˖i false
      }}}.
    #[local] Lemma nextspecis_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' ˖i false
          Ψ false
      }}}.
    #[local] Lemma nextspecpop {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' ˖i false
          pop۰au γ Ψ
      }}}.

    Lemma queue_spmc٠is_emptyspec t γ ι :
      <<<
        queue_spmc۰inv t γ ι
      | ∀∀ vs,
        queue_spmc۰model γ vs
      >>>
        queue_spmc٠is_empty #t @ ι
      <<<
        queue_spmc۰model γ vs
      | RET #(bool_decide (vs = []%list));
        True
      >>>.

    Lemma queue_spmc٠pushspec t γ ι ws v :
      <<<
        queue_spmc۰inv t γ ι
        queue_spmc۰producer t γ ws
      | ∀∀ vs,
        queue_spmc۰model γ vs
      >>>
        queue_spmc٠push #t v @ ι
      <<<
        queue_spmc۰model γ (vs ++ [v])
      | RET ();
        queue_spmc۰producer t γ (vs ++ [v])
      >>>.

    #[local] Lemma queue_spmc٠popspecaux t γ :
      <<<
        inv' t γ
      | ∀∀ vs,
        model₁ γ vs
      >>>
        queue_spmc٠pop #t @ γ.(metadata۰inv)
      <<<
        model₁ γ (tail vs)
      | RET head vs;
        True
      >>>.
    Lemma queue_spmc٠popspec t γ ι :
      <<<
        queue_spmc۰inv t γ ι
      | ∀∀ vs,
        queue_spmc۰model γ vs
      >>>
        queue_spmc٠pop #t @ ι
      <<<
        queue_spmc۰model γ (tail vs)
      | RET head vs;
        True
      >>>.
  End queue_spmc۰G.

  #[global] Opaque queue_spmc۰inv.
  #[global] Opaque queue_spmc۰producer.
  #[global] Opaque queue_spmc۰model.
End base.

Require zoo_saturn.queue_spmc__opaque.

Section queue_spmc۰G.
  Context `{queue_spmc۰G : QueueSpmcG Σ}.

  Implicit Type 𝑡 : location.
  Implicit Type t : val.

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

  Definition queue_spmc۰producer t ws : iProp Σ :=
     𝑡 γ,
    t = #𝑡
    𝑡 γ
    base.queue_spmc۰producer 𝑡 γ ws.
  #[local] Instance : CustomIpat "producer" :=
    " ( %𝑡{} & %γ{} & {%Heq{};->} & #Hmeta{_{}} & Hproducer{_{}} ) ".

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

  #[global] Instance queue_spmc۰modeltimeless t vs :
    Timeless (queue_spmc۰model t vs).
  #[global] Instance queue_spmc۰producertimeless t ws :
    Timeless (queue_spmc۰producer t ws).

  #[global] Instance queue_spmc۰invpersistent t ι :
    Persistent (queue_spmc۰inv t ι).

  Lemma queue_spmc۰modelexclusive t vs1 vs2 :
    queue_spmc۰model t vs1 -∗
    queue_spmc۰model t vs2 -∗
    False.

  Lemma queue_spmc۰producervalid t vs ws :
    queue_spmc۰producer t ws -∗
    queue_spmc۰model t vs -∗
    vs `suffix_of` ws.
  Lemma queue_spmc۰producerexclusive t ws1 ws2 :
    queue_spmc۰producer t ws1 -∗
    queue_spmc۰producer t ws2 -∗
    False.

  Lemma queue_spmc٠createspec ι :
    {{{
      True
    }}}
      queue_spmc٠create ()
    {{{
      t
    , RET t;
      queue_spmc۰inv t ι
      queue_spmc۰model t []
      queue_spmc۰producer t []
    }}}.

  Lemma queue_spmc٠is_emptyspec t ι :
    <<<
      queue_spmc۰inv t ι
    | ∀∀ vs,
      queue_spmc۰model t vs
    >>>
      queue_spmc٠is_empty t @ ι
    <<<
      queue_spmc۰model t vs
    | RET #(bool_decide (vs = []%list));
      True
    >>>.

  Lemma queue_spmc٠pushspec t ι ws v :
    <<<
      queue_spmc۰inv t ι
      queue_spmc۰producer t ws
    | ∀∀ vs,
      queue_spmc۰model t vs
    >>>
      queue_spmc٠push t v @ ι
    <<<
      queue_spmc۰model t (vs ++ [v])
    | RET ();
      queue_spmc۰producer t (vs ++ [v])
    >>>.

  Lemma queue_spmc٠popspec t ι :
    <<<
      queue_spmc۰inv t ι
    | ∀∀ vs,
      queue_spmc۰model t vs
    >>>
      queue_spmc٠pop t @ ι
    <<<
      queue_spmc۰model t (tail vs)
    | RET head vs;
      True
    >>>.
End queue_spmc۰G.

#[global] Opaque queue_spmc۰inv.
#[global] Opaque queue_spmc۰producer.
#[global] Opaque queue_spmc۰model.