Library zoo_saturn.inf_ws_deque_1

Require Import zoo.prelude.
Require Import zoo.common.countable.
Require Import zoo.common.function.
Require Import zoo.common.relations.
Require Import zoo.iris.base_logic.lib.excl.
Require Import zoo.iris.base_logic.lib.twins.
Require Import zoo.iris.base_logic.lib.auth_twins.
Require Import zoo.iris.base_logic.lib.auth_nat_max.
Require Import zoo.iris.base_logic.lib.mono_list.
Require Import zoo.base.
Require Import zoo.program_logic.prophet_identifier.
Require Import zoo.program_logic.prophet_multi.
Require Import zoo_std.option.
Require Export zoo_saturn.inf_ws_deque_1__code.
Require Import zoo_saturn.inf_ws_deque_1__types.
Require Import zoo.options.

Implicit Type front back : nat.
Implicit Type id : prophet_id.
Implicit Type v : val.
Implicit Type vs ws hist lhist : list val.
Implicit Type priv : nat val.
Implicit Type past prophs : list prophet_identifier.(prophet_typed۰type).
Implicit Type pasts prophss : nat list prophet_identifier.(prophet_typed۰type).

Variant state :=
  | Empty
  | Nonempty
  | Emptyish
  | Superempty.
Implicit Type state : state.

#[local] Instance stateinhabited : Inhabited state :=
  populate Empty.

Variant stability :=
  | Stable
  | Unstable.
Implicit Type stable : stability.

#[local] Instance stabilityinhabited : Inhabited stability :=
  populate Stable.

Class InfWsDeque1G Σ `{zoo۰G : !ZooG Σ} :=
  { #[local] inf_ws_deque_1۰G۰inf_array۰G :: InfArrayG Σ
  ; #[local] inf_ws_deque_1۰G۰prophet۰G :: ProphetMultiG Σ prophet_identifier
  ; #[local] inf_ws_deque_1۰G۰model۰G :: AuthTwinsG Σ (leibnizO (list val)) suffix
  ; #[local] inf_ws_deque_1۰G۰owner۰G :: TwinsG Σ (leibnizO (stability × nat × (nat val)))
  ; #[local] inf_ws_deque_1۰G۰front۰G :: AuthNatMaxG Σ
  ; #[local] inf_ws_deque_1۰G۰history۰G :: MonoListG Σ val
  ; #[local] inf_ws_deque_1۰G۰winner۰G :: TwinsG Σ (natO × )
  }.

Definition inf_ws_deque_1۰Σ :=
  #[inf_array۰Σ
  ; prophet_multi۰Σ prophet_identifier
  ; auth_twins۰Σ (leibnizO (list val)) suffix
  ; twins۰Σ (leibnizO (stability × nat × (nat val)))
  ; auth_nat_max۰Σ
  ; mono_list۰Σ val
  ; twins۰Σ (natO × )
  ].
#[global] Instance subGinf_ws_deque_1۰Σ Σ `{zoo۰G : !ZooG Σ} :
  subG inf_ws_deque_1۰Σ Σ
  InfWsDeque1G Σ .

Module base.
  Section inf_ws_deque_1۰G.
    Context `{inf_ws_deque_1۰G : InfWsDeque1G Σ}.

    Implicit Type t : location.
    Implicit Type P : iProp Σ.

    Record inf_ws_deque_1۰name :=
      { inf_ws_deque_1۰name۰data : val
      ; inf_ws_deque_1۰name۰inv : namespace
      ; inf_ws_deque_1۰name۰prophet : prophet_id
      ; inf_ws_deque_1۰name۰prophet_name : prophet_multi۰name
      ; inf_ws_deque_1۰name۰model : auth_twins۰name
      ; inf_ws_deque_1۰name۰owner : gname
      ; inf_ws_deque_1۰name۰front : gname
      ; inf_ws_deque_1۰name۰history : gname
      ; inf_ws_deque_1۰name۰winner : gname
      }.
    Implicit Type γ : inf_ws_deque_1۰name.

    #[global] Instance inf_ws_deque_1۰nameeq_dec : EqDecision inf_ws_deque_1۰name :=
      ltac:(solve_decision).
    #[global] Instance inf_ws_deque_1۰namecountable :
      Countable inf_ws_deque_1۰name.

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

    #[local] Definition owner₁' γ_owner γ_model stable back priv ws : iProp Σ :=
      twins۰twin₁ (twins۰G := inf_ws_deque_1۰G۰owner۰G) γ_owner (DfracOwn 1) (stable, back, priv)
      auth_twins۰auth _ γ_model ws.
    #[local] Definition owner₁ γ :=
      owner₁' γ.(inf_ws_deque_1۰name۰owner) γ.(inf_ws_deque_1۰name۰model).
    #[local] Instance : CustomIpat "owner₁" :=
      " ( Howner₁{_{}} & Hmodel_auth{_{}} ) ".
    #[local] Definition owner₂' γ_owner stable back priv :=
      twins۰twin₂ (twins۰G := inf_ws_deque_1۰G۰owner۰G) γ_owner (stable, back, priv).
    #[local] Definition owner₂ γ :=
      owner₂' γ.(inf_ws_deque_1۰name۰owner).

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

    #[local] Definition history۰auth' γ_history :=
      mono_list۰auth γ_history (DfracOwn 1).
    #[local] Definition history۰auth γ :=
      history۰auth' γ.(inf_ws_deque_1۰name۰history).
    #[local] Definition history۰at γ :=
      mono_list۰at γ.(inf_ws_deque_1۰name۰history).

    #[local] Definition winner۰pop' γ_winner front P : iProp Σ :=
      twins۰twin₁ γ_winner (DfracOwn 1) (front, Next P).
    #[local] Definition winner۰pop γ :=
      winner۰pop' γ.(inf_ws_deque_1۰name۰winner).
    #[local] Definition winner۰steal' γ_winner front P :=
      twins۰twin₂ γ_winner (front, Next P).
    #[local] Definition winner۰steal γ :=
      winner۰steal' γ.(inf_ws_deque_1۰name۰winner).
    #[local] Definition winner γ : iProp Σ :=
       front P1 P2,
      winner۰pop γ front P1
      winner۰steal γ front P2.
    #[local] Instance : CustomIpat "winner" :=
      " ( %front_winner & %P_winner_1 & %P_winner_2 & Hwinner_pop{_{}} & Hwinner_steal{_{}} ) ".

    #[local] Definition winner۰au γ front P : iProp Σ :=
      AU <{
        ∃∃ vs,
        model₁ γ vs
      }> @ γ.(inf_ws_deque_1۰name۰inv), <{
        ∀∀ v vs',
        vs = v :: vs'
        model₁ γ vs'
        history۰at γ front v
      , COMM
        P
      }>.
    #[local] Definition winner۰pending₁ γ front P id : iProp Σ :=
      winner۰steal γ front P
      identifier۰model id
      winner۰au γ front P.
    #[local] Instance : CustomIpat "winner۰pending₁" :=
      " ( Hwinner_steal{_{!}} & Hid{_{!}} & HP ) ".
    #[local] Definition winner۰pending₂ γ front id : iProp Σ :=
       P,
      winner۰pending₁ γ front P id.
    #[local] Instance : CustomIpat "winner۰pending₂" :=
      " ( %P{} & (:winner۰pending₁) ) ".
    #[local] Definition winner۰linearized γ front P : iProp Σ :=
      winner۰steal γ front P
      P.
    #[local] Instance : CustomIpat "winner۰linearized" :=
      " ( Hwinner_steal{_{!}} & HP ) ".

    #[local] Definition inv۰state۰empty γ stable front back hist lhist : iProp Σ :=
      stable = Stable
      front = back
      lhist = hist
      length hist = front
      winner γ.
    #[local] Instance : CustomIpat "inv۰state۰empty" :=
      " ( { {lazy}{>}% ; {lazy}% ; {>}-> ; -> } & {>;}-> & {>;}-> & {>;}%Hhist{} & Hwinner ) ".
    #[local] Definition inv۰state۰nonempty γ stable front back hist lhist vs prophs : iProp Σ :=
      stable = Stable
      front < back
      lhist = hist ++ take 1 vs
      length hist = front
      ( winner γ
       match prophs with
        | []
            False
        | id :: _
            winner۰pending₂ γ front id
        end
      ).
    #[local] Instance : CustomIpat "inv۰state۰nonempty" :=
      " ( { {lazy}{>}% ; {lazy}% ; {>}-> ; -> } & {>;}% & {>;}-> & {>;}%Hhist{} & Hwinner ) ".
    #[local] Definition inv۰state۰nonempty۰steal γ state stable front back hist lhist vs prophs P : iProp Σ :=
      state = Nonempty
      stable = Stable
      front < back
      lhist = hist ++ take 1 vs
      length hist = front
      match prophs with
      | []
          False
      | id :: _
          winner۰pending₁ γ front P id
      end.
    #[local] Instance : CustomIpat "inv۰state۰nonempty۰steal" :=
      " ( {>;}-> & { {lazy}{>}% ; {lazy}% ; {>}-> ; -> } & {>;}% & {>;}-> & {>;}%Hhist{} & (:winner۰pending₁) ) ".
    #[local] Definition inv۰state۰emptyish γ stable front back hist lhist : iProp Σ :=
       P,
      stable = Unstable
      front = back
      lhist = hist
      length hist = ˖front
      ( winner۰pop γ front P
       winner۰linearized γ front P
      ).
    #[local] Instance : CustomIpat "inv۰state۰emptyish" :=
      " ( %P_ & { {lazy}{>}% ; {lazy}% ; {>}-> ; -> } & {>;}-> & {>;}-> & {>;}%Hhist{} & Hwinner ) ".
    #[local] Definition inv۰state۰emptyish۰pop γ state stable front back hist lhist P : iProp Σ :=
      state = Emptyish
      stable = Unstable
      front = back
      lhist = hist
      length hist = ˖front
      winner۰pop γ front P.
    #[local] Instance : CustomIpat "inv۰state۰emptyish۰pop" :=
      " ( {>;}-> & { {lazy}{>}% ; {lazy}% ; {>}-> ; -> } & {>;}-> & {>;}-> & {>;}%Hhist{} & Hwinner_pop ) ".
    #[local] Definition inv۰state۰emptyish۰steal γ state stable front back hist lhist P : iProp Σ :=
      state = Emptyish
      stable = Unstable
      front = back
      lhist = hist
      length hist = ˖front
      winner۰linearized γ front P.
    #[local] Instance : CustomIpat "inv۰state۰emptyish۰steal" :=
      " ( {>;}-> & { {lazy}{>}% ; {lazy}% ; {>}-> ; -> } & {>;}-> & {>;}-> & {>;}%Hhist{} & (:winner۰linearized) ) ".
    #[local] Definition inv۰state۰superempty γ stable front back hist lhist : iProp Σ :=
      stable = Unstable
      front = ˖back
      lhist = hist
      length hist = front
      winner γ.
    #[local] Instance : CustomIpat "inv۰state۰superempty" :=
      " ( { {lazy}{>}% ; {lazy}% ; {>}-> ; -> } & {>;}-> & {>;}-> & {>;}%Hhist{} & Hwinner ) ".
    #[local] Definition inv۰state γ state stable front back hist lhist vs prophs : iProp Σ :=
      match state with
      | Empty
          inv۰state۰empty γ stable front back hist lhist
      | Nonempty
          inv۰state۰nonempty γ stable front back hist lhist vs prophs
      | Emptyish
          inv۰state۰emptyish γ stable front back hist lhist
      | Superempty
          inv۰state۰superempty γ stable front back hist lhist
      end.

    #[local] Definition inv۰inner t γ : iProp Σ :=
       state stable front back hist lhist vs priv pasts prophss,
      t.[front] #front
      t.[back] #back
      owner₂ γ stable back priv
      front۰auth γ front
      0 < front
      model₂ γ vs
      length vs = back - front
      inf_array۰model' γ.(inf_ws_deque_1۰name۰data) (hist ++ vs) priv
      history۰auth γ lhist
      prophet_multi۰model prophet_identifier γ.(inf_ws_deque_1۰name۰prophet) γ.(inf_ws_deque_1۰name۰prophet_name) pasts prophss
       i, front i pasts i = []
      inv۰state γ state stable front back hist lhist vs (prophss front).
    #[local] Instance : CustomIpat "inv۰inner" :=
      " ( %state{} & %stable{} & %front{} & %back{} & %hist{} & %lhist{} & %vs{} & %priv{} & %pasts{} & %prophss{} & >Ht_front & >Ht_back & >Howner₂ & >Hfront_auth & >%Hfront{} & >Hmodel₂ & >%Hvs{} & >Hdata_model & >Hhistory_auth & >Hprophet_model & >%Hpasts{} & Hstate ) ".
    #[local] Definition inv' t γ : iProp Σ :=
      t.[data] γ.(inf_ws_deque_1۰name۰data)
      t.[proph] #γ.(inf_ws_deque_1۰name۰prophet)
      inf_array۰inv γ.(inf_ws_deque_1۰name۰data)
      inv γ.(inf_ws_deque_1۰name۰inv) (inv۰inner t γ).
    #[local] Instance : CustomIpat "inv'" :=
      " ( #Ht_data & #Ht_proph & #Hdata_inv & #Hinv ) ".
    Definition inf_ws_deque_1۰inv t γ ι : iProp Σ :=
      ι = γ.(inf_ws_deque_1۰name۰inv)
      inv' t γ.
    #[local] Instance : CustomIpat "inv" :=
      " ( -> & (:inv') ) ".

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

    Definition inf_ws_deque_1۰owner γ ws : iProp Σ :=
       back priv,
      owner₁ γ Stable back priv ws.
    #[local] Instance : CustomIpat "owner" :=
      " ( %back{} & %priv{} & Howner₁{_{}} ) ".

    #[global] Instance inf_ws_deque_1۰modeltimeless γ vs :
      Timeless (inf_ws_deque_1۰model γ vs).
    #[global] Instance inf_ws_deque_1۰ownertimeless γ ws :
      Timeless (inf_ws_deque_1۰owner γ ws).

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

    #[local] Lemma modelowneralloc :
       |==>
         γ_model γ_owner,
        model₁' γ_model []
        model₂' γ_model []
        owner₁' γ_owner γ_model Stable 1 (λ _, ()%V) []
        owner₂' γ_owner Stable 1 (λ _, ()%V).
    #[local] Lemma model₁valid γ stable back priv ws vs :
      owner₁ γ stable back priv ws -∗
      model₁ γ vs -∗
      vs `suffix_of` ws.
    #[local] Lemma model₁exclusive γ vs1 vs2 :
      model₁ γ vs1 -∗
      model₁ γ vs2 -∗
      False.
    #[local] Lemma modelagree γ vs1 vs2 :
      model₁ γ vs1 -∗
      model₂ γ vs2 -∗
      vs1 = vs2.
    #[local] Lemma model۰owner₁agree γ stable back priv ws vs1 vs2 :
      owner₁ γ stable back priv ws -∗
      model₁ γ vs1 -∗
      model₂ γ vs2 -∗
        vs1 `suffix_of` ws
        vs1 = vs2.
    #[local] Lemma modelempty {γ stable back priv ws vs1 vs2} :
      owner₁ γ stable back priv ws -∗
      model₁ γ vs1 -∗
      model₂ γ vs2 ==∗
        owner₁ γ stable back priv []
        model₁ γ []
        model₂ γ [].
    #[local] Lemma modelpush {γ stable back priv ws vs1 vs2} v :
      owner₁ γ stable back priv ws -∗
      model₁ γ vs1 -∗
      model₂ γ vs2 ==∗
        owner₁ γ stable back priv (vs1 ++ [v])
        model₁ γ (vs1 ++ [v])
        model₂ γ (vs1 ++ [v]).
    #[local] Lemma modelsteal γ vs1 vs2 :
      model₁ γ vs1 -∗
      model₂ γ vs2 ==∗
        model₁ γ (tail vs1)
        model₂ γ (tail vs1).
    #[local] Lemma modelpop γ stable back priv ws vs1 vs2 :
      owner₁ γ stable back priv ws -∗
      model₁ γ vs1 -∗
      model₂ γ vs2 ==∗
        owner₁ γ stable back priv (removelast vs1)
        model₁ γ (removelast vs1)
        model₂ γ (removelast vs1).
    #[local] Lemma modelpop' γ stable back priv ws vs1 v vs2 :
      owner₁ γ stable back priv ws -∗
      model₁ γ (vs1 ++ [v]) -∗
      model₂ γ vs2 ==∗
        owner₁ γ stable back priv vs1
        model₁ γ vs1
        model₂ γ vs1.

    #[local] Lemma owner₁exclusive γ stable1 back1 priv1 ws1 stable2 back2 priv2 ws2 :
      owner₁ γ stable1 back1 priv1 ws1 -∗
      owner₁ γ stable2 back2 priv2 ws2 -∗
      False.
    #[local] Lemma owneragree γ stable1 back1 priv1 ws stable2 back2 priv2 :
      owner₁ γ stable1 back1 priv1 ws -∗
      owner₂ γ stable2 back2 priv2 -∗
        stable1 = stable2
        back1 = back2
        priv1 = priv2.
    #[local] Lemma owner₁update γ stable back priv ws vs :
      owner₁ γ stable back priv ws -∗
      model₁ γ vs -∗
      model₂ γ vs ==∗
        owner₁ γ stable back priv vs
        model₁ γ vs
        model₂ γ vs.
    #[local] Lemma ownerupdate {γ stable1 back1 priv1 ws stable2 back2 priv2} stable back priv :
      owner₁ γ stable1 back1 priv1 ws -∗
      owner₂ γ stable2 back2 priv2 ==∗
        owner₁ γ stable back priv ws
        owner₂ γ stable back priv.

    #[local] Lemma frontalloc :
       |==>
         γ_front,
        front۰auth' γ_front 1.
    #[local] Lemma front۰lbget γ front :
      front۰auth γ front
      front۰lb γ front.
    #[local] Lemma front۰lble {γ front} front' :
      front' front
      front۰lb γ front
      front۰lb γ front'.
    #[local] Lemma front۰lbvalid γ front1 front2 :
      front۰auth γ front1 -∗
      front۰lb γ front2 -∗
      front2 front1.
    #[local] Lemma frontupdate γ front :
      front۰auth γ front |==>
      front۰auth γ ˖front.

    #[local] Lemma historyalloc :
       |==>
         γ_hist,
        history۰auth' γ_hist [()%V].
    #[local] Lemma history۰atget {γ hist v} i :
      i = length hist
      history۰auth γ (hist ++ [v])
      history۰at γ i v.
    #[local] Lemma history۰atlookup γ hist i v :
      history۰auth γ hist -∗
      history۰at γ i v -∗
      hist !! i = Some v.
    #[local] Lemma history۰atagree γ i v1 v2 :
      history۰at γ i v1 -∗
      history۰at γ i v2 -∗
      v1 = v2.
    #[local] Lemma historyupdate {γ hist} i v :
      i = length hist
      history۰auth γ hist |==>
        history۰auth γ (hist ++ [v])
        history۰at γ i v.

    #[local] Lemma winneralloc :
       |==>
         γ_winner,
        winner۰pop' γ_winner 1 True
        winner۰steal' γ_winner 1 True.
    #[local] Lemma winner۰popexclusive γ front1 P1 front2 P2 :
      winner۰pop γ front1 P1 -∗
      winner۰pop γ front2 P2 -∗
      False.
    #[local] Lemma winner۰popexclusive' γ front P :
      winner۰pop γ front P -∗
      winner γ -∗
      False.
    #[local] Lemma winner۰stealexclusive γ front1 P1 front2 P2 :
      winner۰steal γ front1 P1 -∗
      winner۰steal γ front2 P2 -∗
      False.
    #[local] Lemma winner۰stealexclusive' γ front P :
      winner۰steal γ front P -∗
      winner γ -∗
      False.
    #[local] Lemma winneragree γ front1 P1 front2 P2 :
      winner۰pop γ front1 P1 -∗
      winner۰steal γ front2 P2 -∗
        front1 = front2
         (P1 P2).
    #[local] Lemma winnerupdate {γ front1 P1 front2 P2} front P :
      winner۰pop γ front1 P1 -∗
      winner۰steal γ front2 P2 ==∗
        winner۰pop γ front P
        winner۰steal γ front P.

    Opaque owner₁'.

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

    Lemma inf_ws_deque_1۰ownerexclusive γ ws1 ws2 :
      inf_ws_deque_1۰owner γ ws1 -∗
      inf_ws_deque_1۰owner γ ws2 -∗
      False.
    Lemma inf_ws_deque_1۰ownermodel γ ws vs :
      inf_ws_deque_1۰owner γ ws -∗
      inf_ws_deque_1۰model γ vs -∗
      vs `suffix_of` ws.

    #[local] Lemma inv۰stateStable γ state front back hist lhist vs prophs :
      length vs = back - front
      inv۰state γ state Stable front back hist lhist vs prophs
        state = Empty state = Nonempty
        front back
        length (hist ++ vs) = back.
    #[local] Lemma inv۰stateUnstable γ state front back hist lhist vs prophs :
      inv۰state γ state Unstable front back hist lhist vs prophs
        state = Emptyish state = Superempty
        front = back front = ˖back.
    #[local] Lemma inv۰stateNonempty γ state stable front back hist lhist vs prophs :
      front < back
      inv۰state γ state stable front back hist lhist vs prophs
      state = Nonempty.
    #[local] Lemma inv۰stateSuperempty γ state front back hist lhist vs prophs :
      back < front
      inv۰state γ state Unstable front back hist lhist vs prophs -∗
      state = Superempty.
    #[local] Lemma inv۰statewinner۰pop γ state stable front1 back hist lhist vs prophs front2 P :
      inv۰state γ state stable front1 back hist lhist vs prophs -∗
      winner۰pop γ front2 P -∗
         P_,
        front1 = front2
         (P P_)
        ( inv۰state۰nonempty۰steal γ state stable front2 back hist lhist vs prophs P_
         inv۰state۰emptyish۰steal γ state stable front2 back hist lhist P_
        )
        winner۰pop γ front2 P.
    #[local] Lemma inv۰statewinner۰steal γ state stable front1 back hist lhist vs prophs front2 P :
      inv۰state γ state stable front1 back hist lhist vs prophs -∗
      winner۰steal γ front2 P -∗
         P_,
        front1 = front2
         (P_ P)
        inv۰state۰emptyish۰pop γ state stable front2 back hist lhist P_
        winner۰steal γ front2 P.

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

    #[local] Lemma frontspec t γ :
      {{{
        inv' t γ
      }}}
        (#t).{front}
      {{{
        front
      , RET #front;
        front۰lb γ front
      }}}.
    #[local] Lemma frontspecownerStable t γ back priv ws :
      {{{
        inv' t γ
        owner₁ γ Stable back priv ws
      }}}
        (#t).{front}
      {{{
        front
      , RET #front;
        owner₁ γ Stable back priv ws
        front۰lb γ front
        front back
      }}}.
    #[local] Lemma frontspecownerUnstable t γ back priv ws :
      {{{
        inv' t γ
        owner₁ γ Unstable back priv ws
      }}}
        (#t).{front}
      {{{
        front
      , RET #front;
        owner₁ γ Unstable back priv ws
        front۰lb γ front
        front = back front = ˖back
      }}}.
    #[local] Lemma frontspecSuperempty t γ back priv ws front :
      back < front
      {{{
        inv' t γ
        owner₁ γ Unstable back priv ws
        front۰lb γ front
      }}}
        (#t).{front}
      {{{
        RET #front;
        owner₁ γ Unstable back priv ws
      }}}.
    #[local] Lemma frontspecwinner۰steal t γ front P :
      {{{
        inv' t γ
        winner۰steal γ front P
      }}}
        (#t).{front}
      {{{
        RET #front;
        winner۰steal γ front P
      }}}.

    #[local] Lemma backspec t γ stable back priv ws :
      {{{
        inv' t γ
        owner₁ γ stable back priv ws
      }}}
        (#t).{back}
      {{{
        RET #back;
        owner₁ γ stable back priv ws
      }}}.

    #[local] Lemma set_backspecSuperempty t γ back priv ws front (back' : Z) :
      back < front
      back' = ˖back
      {{{
        inv' t γ
        owner₁ γ Unstable back priv ws
        front۰lb γ front
      }}}
        #t <-{back} #back'
      {{{
        RET ();
        owner₁ γ Stable ˖back priv ws
      }}}.

    #[local] Lemma inf_array٠getspechistory t γ (i : nat) (i_ : Z) v :
      i_ = i
      {{{
        inv' t γ
        history۰at γ i v
      }}}
        inf_array٠get γ.(inf_ws_deque_1۰name۰data) #i_
      {{{
        RET v;
        True
      }}}.
    #[local] Lemma inf_array٠getspecowner t γ back (back_ : Z) priv ws v :
      back_ = back
      priv 0 = v
      {{{
        inv' t γ
        owner₁ γ Stable back priv ws
      }}}
        inf_array٠get γ.(inf_ws_deque_1۰name۰data) #back_
      {{{
        RET v;
        owner₁ γ Stable back priv ws
      }}}.

    #[local] Lemma inf_array٠setspecowner t γ back priv ws v :
      {{{
        inv' t γ
        owner₁ γ Stable back priv ws
      }}}
        inf_array٠set γ.(inf_ws_deque_1۰name۰data) #back v
      {{{
        RET ();
        owner₁ γ Stable back (<[0 := v]> priv) ws
      }}}.

    #[local] Lemma resolvespecloser₁ t γ front1 front2 id :
      front1 < front2
      {{{
        inv' t γ
        front۰lb γ front2
      }}}
        Resolve (CAS (#t).[front]%V #front1 #(front1 + 1)) #γ.(inf_ws_deque_1۰name۰prophet) (#front1, #id)%V
      {{{
        RET false;
        True
      }}}.
    #[local] Lemma resolvespecloser₂ t γ front id prophs0 :
      head prophs0 Some id
      {{{
        inv' t γ
        front۰lb γ front
        prophet_multi۰full prophet_identifier γ.(inf_ws_deque_1۰name۰prophet_name) front prophs0
      }}}
        Resolve (CAS (#t).[front]%V #front #(front + 1)) #γ.(inf_ws_deque_1۰name۰prophet) (#front, #id)%V
      {{{
        RET false;
        front۰lb γ ˖front
      }}}.
    #[local] Lemma resolvespecwinnerpop t γ front P id :
      {{{
        inv' t γ
        winner۰pop γ front P
      }}}
        Resolve (CAS (#t).[front]%V #front #(front + 1)) #γ.(inf_ws_deque_1۰name۰prophet) (#front, #id)%V
      {{{
        RET true;
         P
      }}}.
    #[local] Lemma resolvespecwinnersteal t γ front P id :
      {{{
        inv' t γ
        winner۰steal γ front P
      }}}
        Resolve (CAS (#t).[front]%V #front #(front + 1)) #γ.(inf_ws_deque_1۰name۰prophet) (#front, #id)%V
      {{{
        RET true;
        front۰lb γ ˖front
      }}}.
    #[local] Lemma resolvespecEmpty t γ back priv ws id :
      {{{
        inv' t γ
        owner₁ γ Stable back priv ws
        front۰lb γ back
      }}}
        Resolve (CAS (#t).[front]%V #back #(back + 1)) #γ.(inf_ws_deque_1۰name۰prophet) (#back, #id)%V
      {{{
        RET true;
        owner₁ γ Unstable back (priv S) ws
        front۰lb γ ˖back
        history۰at γ back (priv 0)
      }}}.

    Lemma inf_ws_deque_1٠sizespec t γ ι ws :
      <<<
        inf_ws_deque_1۰inv t γ ι
        inf_ws_deque_1۰owner γ ws
      | ∀∀ vs,
        inf_ws_deque_1۰model γ vs
      >>>
        inf_ws_deque_1٠size #t @ ι
      <<<
        vs `suffix_of` ws
        inf_ws_deque_1۰model γ vs
      | RET #(length vs);
        inf_ws_deque_1۰owner γ vs
      >>>.

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

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

    Lemma inf_ws_deque_1٠stealspec t γ ι :
      <<<
        inf_ws_deque_1۰inv t γ ι
      | ∀∀ vs,
        inf_ws_deque_1۰model γ vs
      >>>
        inf_ws_deque_1٠steal #t @ ι
      <<<
        inf_ws_deque_1۰model γ (tail vs)
      | RET head vs;
        True
      >>>.

    Variant pop۰state :=
      | PopNonempty v
      | PopEmptyishWinner v
      | PopEmptyishLoser
      | PopSuperempty.
    #[local] Lemma inf_ws_deque_1٠pop₁spec {t γ} (state : pop۰state) stable back (back_ : Z) priv ws id :
      back_ = back
      {{{
        inv' t γ
        owner₁ γ stable back priv ws
        match state with
        | PopNonempty v
            stable = Stable
            priv 0 = v
        | PopEmptyishWinner v
            stable = Unstable
            history۰at γ back v
            winner۰steal γ back inhabitant
        | PopEmptyishLoser
             id_winner prophs,
            stable = Unstable
            prophet_multi۰full prophet_identifier γ.(inf_ws_deque_1۰name۰prophet_name) back (id_winner :: prophs)
            head (id_winner :: prophs) Some id
        | PopSuperempty
             front,
            stable = Unstable
            front۰lb γ front
            front = ˖back
        end
      }}}
        inf_ws_deque_1٠pop₁ #t #id #back_
      {{{
        o back priv
      , RET o;
        owner₁ γ Stable back priv ws
        match state with
        | PopNonempty v
            o = Some v
        | PopEmptyishWinner v
            o = Some v
        | PopEmptyishLoser
            o = None
        | PopSuperempty
            o = None
        end
      }}}.
    Lemma inf_ws_deque_1٠popspec t γ ι ws :
      <<<
        inf_ws_deque_1۰inv t γ ι
        inf_ws_deque_1۰owner γ ws
      | ∀∀ vs,
        inf_ws_deque_1۰model γ vs
      >>>
        inf_ws_deque_1٠pop #t @ ι
      <<<
        ∃∃ o ws',
        vs `suffix_of` ws
        match o with
        | None
            vs = []
            ws' = []
            inf_ws_deque_1۰model γ []
        | Some v
             vs',
            vs = vs' ++ [v]
            ws' = vs'
            inf_ws_deque_1۰model γ vs'
        end
      | RET o;
        inf_ws_deque_1۰owner γ ws'
      >>>.
  End inf_ws_deque_1۰G.

  #[global] Opaque inf_ws_deque_1۰inv.
  #[global] Opaque inf_ws_deque_1۰model.
  #[global] Opaque inf_ws_deque_1۰owner.
End base.

Require zoo_saturn.inf_ws_deque_1__opaque.

Section inf_ws_deque_1۰G.
  Context `{inf_ws_deque_1۰G : InfWsDeque1G Σ}.

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

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

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

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

  #[global] Instance inf_ws_deque_1۰modeltimeless γ vs :
    Timeless (inf_ws_deque_1۰model γ vs).
  #[global] Instance inf_ws_deque_1۰ownertimeless γ ws :
    Timeless (inf_ws_deque_1۰owner γ ws).

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

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

  Lemma inf_ws_deque_1۰ownerexclusive t ws1 ws2 :
    inf_ws_deque_1۰owner t ws1 -∗
    inf_ws_deque_1۰owner t ws2 -∗
    False.
  Lemma inf_ws_deque_1ownermodel γ ws vs :
    inf_ws_deque_1۰owner γ ws -∗
    inf_ws_deque_1۰model γ vs -∗
    vs `suffix_of` ws.

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

  Lemma inf_ws_deque_1٠sizespec t ι ws :
    <<<
      inf_ws_deque_1۰inv t ι
      inf_ws_deque_1۰owner t ws
    | ∀∀ vs,
      inf_ws_deque_1۰model t vs
    >>>
      inf_ws_deque_1٠size t @ ι
    <<<
      vs `suffix_of` ws
      inf_ws_deque_1۰model t vs
    | RET #(length vs);
      inf_ws_deque_1۰owner t vs
    >>>.

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

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

  Lemma inf_ws_deque_1٠stealspec t ι :
    <<<
      inf_ws_deque_1۰inv t ι
    | ∀∀ vs,
      inf_ws_deque_1۰model t vs
    >>>
      inf_ws_deque_1٠steal t @ ι
    <<<
      inf_ws_deque_1۰model t (tail vs)
    | RET head vs;
      True
    >>>.

  Lemma inf_ws_deque_1٠popspec t ι ws :
    <<<
      inf_ws_deque_1۰inv t ι
      inf_ws_deque_1۰owner t ws
    | ∀∀ vs,
      inf_ws_deque_1۰model t vs
    >>>
      inf_ws_deque_1٠pop t @ ι
    <<<
      ∃∃ o ws',
      vs `suffix_of` ws
      match o with
      | None
          vs = []
          ws' = []
          inf_ws_deque_1۰model t []
      | Some v
           vs',
          vs = vs' ++ [v]
          ws' = vs'
          inf_ws_deque_1۰model t vs'
      end
    | RET o;
      inf_ws_deque_1۰owner t ws'
    >>>.
End inf_ws_deque_1۰G.

#[global] Opaque inf_ws_deque_1۰inv.
#[global] Opaque inf_ws_deque_1۰model.
#[global] Opaque inf_ws_deque_1۰owner.