Library zoo_saturn.ws_bdeque_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.auth_nat_max.
Require Import zoo.iris.base_logic.lib.auth_twins.
Require Import zoo.iris.base_logic.lib.excl.
Require Import zoo.iris.base_logic.lib.mono_list.
Require Import zoo.iris.base_logic.lib.twins.
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.ws_bdeque_1__code.
Require Import zoo_saturn.ws_bdeque_1__types.
Require Import zoo.options.

Implicit Type b : bool.
Implicit Type front front_cache back : nat.
Implicit Type id : prophet_id.
Implicit Type v : val.
Implicit Type us vs ws hist priv : list 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 WsBdeque1G Σ `{zoo۰G : !ZooG Σ} :=
  { #[local] ws_bdeque_1۰G۰prophet۰G :: ProphetMultiG Σ prophet_identifier
  ; #[local] ws_bdeque_1۰G۰model۰G :: AuthTwinsG Σ (leibnizO (list val)) suffix
  ; #[local] ws_bdeque_1۰G۰owner۰G :: TwinsG Σ (leibnizO (stability × nat))
  ; #[local] ws_bdeque_1۰G۰front۰G :: AuthNatMaxG Σ
  ; #[local] ws_bdeque_1۰G۰history۰G :: MonoListG Σ val
  ; #[local] ws_bdeque_1۰G۰winner۰G :: TwinsG Σ (natO × )
  }.

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

Module base.
  Section ws_bdeque_1۰G.
    Context `{ws_bdeque_1۰G : WsBdeque1G Σ}.

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

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

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

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

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

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

    #[local] Definition history۰auth' γ_history :=
      mono_list۰auth γ_history (DfracOwn 1).
    #[local] Definition history۰auth γ :=
      history۰auth' γ.(ws_bdeque_1۰name۰history).
    #[local] Definition history۰at γ :=
      mono_list۰at γ.(ws_bdeque_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' γ.(ws_bdeque_1۰name۰winner).
    #[local] Definition winner۰steal' γ_winner front P :=
      twins۰twin₂ γ_winner (front, Next P).
    #[local] Definition winner۰steal γ :=
      winner۰steal' γ.(ws_bdeque_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
      }> @ γ.(ws_bdeque_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 : iProp Σ :=
      stable = Stable
      front = back
      length hist = front
      winner γ.
    #[local] Instance : CustomIpat "inv۰state۰empty" :=
      " ( { {lazy}{>}% ; {lazy}% ; {>}-> ; -> } & {>;}-> & {>;}%Hhist{} & Hwinner ) ".
    #[local] Definition inv۰state۰nonempty γ stable front back hist vs prophs : iProp Σ :=
      stable = Stable
      front < back
      length hist = ˖front
      history۰at γ front (hd inhabitant vs)
      ( winner γ
       match prophs with
        | []
            False
        | id :: _
            winner۰pending₂ γ front id
        end
      ).
    #[local] Instance : CustomIpat "inv۰state۰nonempty" :=
      " ( { {lazy}{>}% ; {lazy}% ; {>}-> ; -> } & {>;}% & {>;}%Hhist{} & #Hhistory_at_front{} & Hwinner ) ".
    #[local] Definition inv۰state۰nonempty۰steal γ state stable front back hist vs prophs P : iProp Σ :=
      state = Nonempty
      stable = Stable
      front < back
      length hist = ˖front
      history۰at γ front (hd inhabitant vs)
      match prophs with
      | []
          False
      | id :: _
          winner۰pending₁ γ front P id
      end.
    #[local] Instance : CustomIpat "inv۰state۰nonempty۰steal" :=
      " ( {>;}-> & { {lazy}{>}% ; {lazy}% ; {>}-> ; -> } & {>;}% & {>;}%Hhist{} & #Hhistory_at_front{} & Hwinner ) ".
    #[local] Definition inv۰state۰emptyish γ stable front back hist priv : iProp Σ :=
       P,
      stable = Unstable
      front = back
      length hist = ˖front
      history۰at γ front (hd inhabitant priv)
      ( winner۰pop γ front P
       winner۰linearized γ front P
      ).
    #[local] Instance : CustomIpat "inv۰state۰emptyish" :=
      " ( %P_ & { {lazy}{>}% ; {lazy}% ; {>}-> ; -> } & {>;}-> & {>;}%Hhist{} & #Hhistory_at_front{} & Hwinner ) ".
    #[local] Definition inv۰state۰emptyish۰pop γ state stable front back hist priv P : iProp Σ :=
      state = Emptyish
      stable = Unstable
      front = back
      length hist = ˖front
      history۰at γ front (hd inhabitant priv)
      winner۰pop γ front P.
    #[local] Instance : CustomIpat "inv۰state۰emptyish۰pop" :=
      " ( {>;}-> & { {lazy}{>}% ; {lazy}% ; {>}-> ; -> } & {>;}-> & {>;}%Hhist{} & #Hhistory_at_front{} & Hwinner_pop ) ".
    #[local] Definition inv۰state۰emptyish۰steal γ state stable front back hist priv P : iProp Σ :=
      state = Emptyish
      stable = Unstable
      front = back
      length hist = ˖front
      history۰at γ front (hd inhabitant priv)
      winner۰linearized γ front P.
    #[local] Instance : CustomIpat "inv۰state۰emptyish۰steal" :=
      " ( {>;}-> & { {lazy}{>}% ; {lazy}% ; {>}-> ; -> } & {>;}-> & {>;}%Hhist{} & #Hhistory_at_front{} & (:winner۰linearized) ) ".
    #[local] Definition inv۰state۰superempty γ stable front back hist : iProp Σ :=
      stable = Unstable
      front = ˖back
      length hist = front
      winner γ.
    #[local] Instance : CustomIpat "inv۰state۰superempty" :=
      " ( { {lazy}{>}% ; {lazy}% ; {>}-> ; -> } & {>;}-> & {>;}%Hhist{} & Hwinner ) ".
    #[local] Definition inv۰state γ state stable front back hist vs priv prophs : iProp Σ :=
      match state with
      | Empty
          inv۰state۰empty γ stable front back hist
      | Nonempty
          inv۰state۰nonempty γ stable front back hist vs prophs
      | Emptyish
          inv۰state۰emptyish γ stable front back hist priv
      | Superempty
          inv۰state۰superempty γ stable front back hist
      end.

    #[local] Definition inv۰inner t γ : iProp Σ :=
       state stable front back hist vs priv pasts prophss,
      t.[front] #front
      t.[back] #back
      owner₂ γ stable back
      front۰auth γ front
      0 < front
      model₂ γ vs
      length vs = back - front
      array۰cslice γ.(ws_bdeque_1۰name۰data) γ.(ws_bdeque_1۰name۰capacity) front (DfracOwn (1/2)) (vs ++ priv)
      (length vs + length priv)%nat = γ.(ws_bdeque_1۰name۰capacity)
      history۰auth γ hist
      prophet_multi۰model prophet_identifier γ.(ws_bdeque_1۰name۰prophet) γ.(ws_bdeque_1۰name۰prophet_name) pasts prophss
       i, front i pasts i = []
      inv۰state γ state stable front back hist vs priv (prophss front).
    #[local] Instance : CustomIpat "inv۰inner" :=
      " ( %state{} & %stable{} & %front{} & %back{} & %hist{} & %vs{} & %priv{} & %pasts{} & %prophss{} & >Ht_front & >Ht_back & >Howner₂ & >Hfront_auth & >%Hfront{} & >Hmodel₂ & >%Hvs{} & >Hdata_cslice₁ & >%Hdata{} & >Hhistory_auth & >Hprophet_model & >%Hpasts{} & Hstate ) ".
    #[local] Definition inv' t γ : iProp Σ :=
      0 < γ.(ws_bdeque_1۰name۰capacity)
      t.[capacity] #γ.(ws_bdeque_1۰name۰capacity)
      t.[data] γ.(ws_bdeque_1۰name۰data)
      t.[proph] #γ.(ws_bdeque_1۰name۰prophet)
      inv γ.(ws_bdeque_1۰name۰inv) (inv۰inner t γ).
    #[local] Instance : CustomIpat "inv'" :=
      " ( %Hcapacity & #Ht_capacity & #Ht_data & #Ht_proph & #Hinv ) ".
    Definition ws_bdeque_1۰inv t γ ι cap : iProp Σ :=
      ι = γ.(ws_bdeque_1۰name۰inv)
      cap = γ.(ws_bdeque_1۰name۰capacity)
      inv' t γ.
    #[local] Instance : CustomIpat "inv" :=
      " ( -> & -> & (:inv') ) ".

    Definition ws_bdeque_1۰model γ vs : iProp Σ :=
      model₁ γ vs
      length vs γ.(ws_bdeque_1۰name۰capacity).
    #[local] Instance : CustomIpat "model" :=
      " ( Hmodel₁{_{}} & %Hvs{} ) ".

    Variant owner۰flag :=
      | OwnerNormal
      | OwnerPop.
    #[local] Definition owner۰1 flag t γ stable back ws front_cache i us : iProp Σ :=
      owner₁ γ stable back ws
      t.[front_cache] #front_cache
      front۰lb γ front_cache
      (if flag is OwnerPop then ˖back else back) front_cache + γ.(ws_bdeque_1۰name۰capacity)
      array۰cslice γ.(ws_bdeque_1۰name۰data) γ.(ws_bdeque_1۰name۰capacity) i (DfracOwn (1/2)) us
      length us = γ.(ws_bdeque_1۰name۰capacity).
    #[local] Instance : CustomIpat "owner۰1" :=
      " ( Howner₁{_{}} & Ht_front_cache{_{}} & { {!} _ ; #Hfront_lb_cache_{} ; #Hfront_lb_cache } & { {!} _ ; %Hfront_cache_{} ; %Hfront_cache } & Hdata_cslice₂{_{}} & { {!} _ ; %Hus{} ; %Hus } ) ".
    #[local] Definition owner۰2 :=
      owner۰1 OwnerNormal.
    #[local] Instance : CustomIpat "owner۰2" :=
      " (:owner۰1) ".
    Definition ws_bdeque_1۰owner t γ ws : iProp Σ :=
       back front_cache i us,
      owner۰2 t γ Stable back ws front_cache i us.
    #[local] Instance : CustomIpat "owner" :=
      " ( %back{} & %front_cache{_{}} & %i{} & %us{} & Howner{_{}} ) ".

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

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

    #[local] Lemma modelowneralloc :
       |==>
         γ_model γ_owner,
        model₁' γ_model []
        model₂' γ_model []
        owner₁' γ_owner γ_model Stable 1 []
        owner₂' γ_owner Stable 1.
    #[local] Lemma model₁valid γ stable back ws vs :
      owner₁ γ stable back 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 ws vs1 vs2 :
      owner₁ γ stable back ws -∗
      model₁ γ vs1 -∗
      model₂ γ vs2 -∗
        vs1 `suffix_of` ws
        vs1 = vs2.
    #[local] Lemma modelempty {γ stable back ws vs1 vs2} :
      owner₁ γ stable back ws -∗
      model₁ γ vs1 -∗
      model₂ γ vs2 ==∗
        owner₁ γ stable back []
        model₁ γ []
        model₂ γ [].
    #[local] Lemma modelpush {γ stable back ws vs1 vs2} v :
      owner₁ γ stable back ws -∗
      model₁ γ vs1 -∗
      model₂ γ vs2 ==∗
        owner₁ γ stable back (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 ws vs1 vs2 :
      owner₁ γ stable back ws -∗
      model₁ γ vs1 -∗
      model₂ γ vs2 ==∗
        owner₁ γ stable back (removelast vs1)
        model₁ γ (removelast vs1)
        model₂ γ (removelast vs1).
    #[local] Lemma modelpop' γ stable back ws vs1 v vs2 :
      owner₁ γ stable back ws -∗
      model₁ γ (vs1 ++ [v]) -∗
      model₂ γ vs2 ==∗
        owner₁ γ stable back vs1
        model₁ γ vs1
        model₂ γ vs1.

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

    #[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 ws_bdeque_1۰modelvalid t γ ι cap vs :
      ws_bdeque_1۰inv t γ ι cap -∗
      ws_bdeque_1۰model γ vs -∗
      length vs cap.
    Lemma ws_bdeque_1۰modelexclusive γ vs1 vs2 :
      ws_bdeque_1۰model γ vs1 -∗
      ws_bdeque_1۰model γ vs2 -∗
      False.

    #[local] Lemma owner۰2ーrebase {t γ stable back ws front_cache i1 us} i2 :
      0 < γ.(ws_bdeque_1۰name۰capacity)
      owner۰2 t γ stable back ws front_cache i1 us
         us,
        owner۰2 t γ stable back ws front_cache i2 us.

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

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

    Lemma ws_bdeque_1٠createspec ι (cap : Z) :
      (0 < cap)%Z
      {{{
        True
      }}}
        ws_bdeque_1٠create #cap
      {{{
        t γ
      , RET #t;
        meta_token t
        ws_bdeque_1۰inv t γ ι cap
        ws_bdeque_1۰model γ []
        ws_bdeque_1۰owner t γ []
      }}}.

    Lemma ws_bdeque_1٠capacityspec t γ ι cap :
      {{{
        ws_bdeque_1۰inv t γ ι cap
      }}}
        ws_bdeque_1٠capacity #t
      {{{
        RET #cap;
        True
      }}}.

    #[local] Lemma frontspec t γ :
      {{{
        inv' t γ
      }}}
        (#t).{front}
      {{{
        front
      , RET #front;
        front۰lb γ front
      }}}.
    #[local] Lemma frontspecownerStable t γ back ws :
      {{{
        inv' t γ
        owner₁ γ Stable back ws
      }}}
        (#t).{front}
      {{{
        front
      , RET #front;
        owner₁ γ Stable back ws
        front۰lb γ front
        front back
      }}}.
    #[local] Lemma frontspecownerUnstable t γ back ws :
      {{{
        inv' t γ
        owner₁ γ Unstable back ws
      }}}
        (#t).{front}
      {{{
        front
      , RET #front;
        owner₁ γ Unstable back ws
        front۰lb γ front
        front = back front = ˖back
      }}}.
    #[local] Lemma frontspecSuperempty t γ back ws front :
      back < front
      {{{
        inv' t γ
        owner₁ γ Unstable back ws
        front۰lb γ front
      }}}
        (#t).{front}
      {{{
        RET #front;
        owner₁ γ Unstable back 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 ws :
      {{{
        inv' t γ
        owner₁ γ stable back ws
      }}}
        (#t).{back}
      {{{
        RET #back;
        owner₁ γ stable back ws
      }}}.

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

    #[local] Lemma array٠unsafe_cgetspecloser t γ i :
      (0 i)%Z
      {{{
        inv' t γ
      }}}
        array٠unsafe_cget γ.(ws_bdeque_1۰name۰data) #i
      {{{
        v
      , RET v;
        True
      }}}.
    #[local] Lemma array٠unsafe_cgetspecwinner۰pop t γ front P v :
      {{{
        inv' t γ
        winner۰pop γ front P
        history۰at γ front v
      }}}
        array٠unsafe_cget γ.(ws_bdeque_1۰name۰data) #front
      {{{
        RET v;
        winner۰pop γ front P
      }}}.

    #[local] Lemma array٠unsafe_csetspecowner t γ back ws front_cache us front v :
      back < front + γ.(ws_bdeque_1۰name۰capacity)
      {{{
        inv' t γ
        owner۰2 t γ Stable back ws front_cache back us
        front۰lb γ front
      }}}
        array٠unsafe_cset γ.(ws_bdeque_1۰name۰data) #back v
      {{{
        RET ();
        owner۰2 t γ Stable back ws front_cache back (<[0 := v]> us)
      }}}.

    #[local] Lemma resolvespecloser₁ t γ front1 front2 id :
      front1 < front2
      {{{
        inv' t γ
        front۰lb γ front2
      }}}
        Resolve (CAS (#t).[front]%V #front1 #(front1 + 1)) #γ.(ws_bdeque_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 γ.(ws_bdeque_1۰name۰prophet_name) front prophs0
      }}}
        Resolve (CAS (#t).[front]%V #front #(front + 1)) #γ.(ws_bdeque_1۰name۰prophet) (#front, #id)%V
      {{{
        RET false;
        front۰lb γ ˖front
      }}}.
    #[local] Lemma resolvespecwinner۰pop t γ front P id :
      {{{
        inv' t γ
        winner۰pop γ front P
      }}}
        Resolve (CAS (#t).[front]%V #front #(front + 1)) #γ.(ws_bdeque_1۰name۰prophet) (#front, #id)%V
      {{{
        RET true;
         P
      }}}.
    #[local] Lemma resolvespecwinner۰steal t γ front P id :
      {{{
        inv' t γ
        winner۰steal γ front P
      }}}
        Resolve (CAS (#t).[front]%V #front #(front + 1)) #γ.(ws_bdeque_1۰name۰prophet) (#front, #id)%V
      {{{
        RET true;
        front۰lb γ ˖front
      }}}.
    #[local] Lemma resolvespecEmpty t γ back ws id :
      {{{
        inv' t γ
        owner₁ γ Stable back ws
        front۰lb γ back
      }}}
        Resolve (CAS (#t).[front]%V #back #(back + 1)) #γ.(ws_bdeque_1۰name۰prophet) (#back, #id)%V
      {{{
        RET true;
        owner₁ γ Unstable back ws
        front۰lb γ ˖back
      }}}.

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

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

    #[local] Definition push۰au t γ ws v Φ : iProp Σ :=
      AU <{
        ∃∃ vs,
        ws_bdeque_1۰model γ vs
      }> @ γ.(ws_bdeque_1۰name۰inv), <{
        ∀∀ b,
        b = bool_decide (length vs < γ.(ws_bdeque_1۰name۰capacity))
        vs `suffix_of` ws
        ws_bdeque_1۰model γ (if b then vs ++ [v] else vs)
      , COMM
        ws_bdeque_1۰owner t γ (if b then vs ++ [v] else ws) -∗
        Φ #b
      }>.
    Lemma ws_bdeque_1٠pushspec t γ ι cap ws v :
      <<<
        ws_bdeque_1۰inv t γ ι cap
        ws_bdeque_1۰owner t γ ws
      | ∀∀ vs,
        ws_bdeque_1۰model γ vs
      >>>
        ws_bdeque_1٠push #t v @ ι
      <<<
        ∃∃ b,
        b = bool_decide (length vs < cap)
        vs `suffix_of` ws
        ws_bdeque_1۰model γ (if b then vs ++ [v] else vs)
      | RET #b;
        ws_bdeque_1۰owner t γ (if b then vs ++ [v] else ws)
      >>>.

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

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

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

Require zoo_saturn.ws_bdeque_1__opaque.

Section ws_bdeque_1۰G.
  Context `{ws_bdeque_1۰G : WsBdeque1G Σ}.

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

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

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

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

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

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

  Lemma ws_bdeque_1۰modelvalid t ι cap vs :
    ws_bdeque_1۰inv t ι cap -∗
    ws_bdeque_1۰model t vs -∗
    length vs cap.
  Lemma ws_bdeque_1۰modelexclusive t vs1 vs2 :
    ws_bdeque_1۰model t vs1 -∗
    ws_bdeque_1۰model t vs2 -∗
    False.

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

  Lemma ws_bdeque_1٠createspec ι (cap : Z) :
    (0 < cap)%Z
    {{{
      True
    }}}
      ws_bdeque_1٠create #cap
    {{{
      t
    , RET t;
      ws_bdeque_1۰inv t ι cap
      ws_bdeque_1۰model t []
      ws_bdeque_1۰owner t []
    }}}.

  Lemma ws_bdeque_1٠capacityspec t ι cap :
    {{{
      ws_bdeque_1۰inv t ι cap
    }}}
      ws_bdeque_1٠capacity t
    {{{
      RET #cap;
      True
    }}}.

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

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

  Lemma ws_bdeque_1٠pushspec t ι cap ws v :
    <<<
      ws_bdeque_1۰inv t ι cap
      ws_bdeque_1۰owner t ws
    | ∀∀ vs,
      ws_bdeque_1۰model t vs
    >>>
      ws_bdeque_1٠push t v @ ι
    <<<
      ∃∃ b,
      b = bool_decide (length vs < cap)
      vs `suffix_of` ws
      ws_bdeque_1۰model t (if b then vs ++ [v] else vs)
    | RET #b;
      ws_bdeque_1۰owner t (if b then vs ++ [v] else ws)
    >>>.

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

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

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