phanerozoic/Coq-Iris huggingface dataset:
(* iris_heap_lang/pretty.v *)
(* Note that this instance does not print function bodies and is thus not
injective (unlike most `pretty` instances). *)
Instance pretty_val : Pretty val
:=
  fix go v :=
    match v with
    | LitV l => "#" +:+ pretty l
    | RecV f x e =>
      match f with
      | BNamed f => "rec: " +:+ f +:+ " " +:+ pretty x +:+ " := <body>"
      | BAnon => "λ: " +:+ pretty x +:+ ", <body>"
      end
    | PairV v1 v2 => "(" +:+ go v1 +:+ ", " +:+ go v2 +:+ ")"
    | InjLV v => "inl (" +:+ go v +:+ ")"
    | InjRV v => "inr (" +:+ go v +:+ ")"
    end.
(* iris_heap_lang/pretty.v *)
Instance pretty_un_op : Pretty un_op
:=
  λ op, match op with
        | NegOp => "~"
        | MinusUnOp => "-"
        end.
(* iris_heap_lang/pretty.v *)
Instance pretty_bin_op : Pretty bin_op
:=
  λ op, match op with
        | PlusOp => "+"
        | MinusOp => "-"
        | MultOp => "*"
        | QuotOp => "`quot`"
        | RemOp => "`rem`"
        | AndOp => "&"
        | OrOp => "|"
        | XorOp => "`xor`"
        | ShiftLOp => "<<"
        | ShiftROp => ">>"
        | LeOp => "≤"
        | LtOp => "<"
        | EqOp => "="
        | OffsetOp => "+ₗ"
        end.
(* iris_heap_lang/primitive_laws.v *)
Class heapGS_gen hlc Σ
:= HeapGS {
  heapGS_invGS : invGS_gen hlc Σ;
  #[global] heapGS_gen_heapGS :: gen_heapGS loc (option val) Σ;
  #[global] heapGS_inv_heapGS :: inv_heapGS loc (option val) Σ;
  #[global] heapGS_proph_mapGS :: proph_mapGS proph_id (val * val) Σ;
  heapGS_step_name : gname;
  heapGS_step_cnt : mono_natG Σ;
}.
(* iris_heap_lang/primitive_laws.v *)
Notation heapGS
:= (heapGS_gen HasLc).
(* iris_heap_lang/primitive_laws.v *)
Definition steps_auth (n : nat) : iProp Σ
:=
    heapGS_step_name ↪●MN n.
(* iris_heap_lang/primitive_laws.v *)
Definition steps_lb (n : nat) : iProp Σ
:=
    heapGS_step_name ↪◯MN n.
(* iris_heap_lang/primitive_laws.v *)
Lemma steps_lb_0 :
    ⊢ |==> steps_lb 0.
Proof. by apply mono_nat_lb_own_0. Qed.
(* iris_heap_lang/primitive_laws.v *)
Lemma steps_lb_valid n m :
    steps_auth n -∗ steps_lb m -∗ ⌜m ≤ n⌝.
Proof.
    iIntros "Hauth Hlb".
    by iDestruct (mono_nat_auth_lb_own_valid with "Hauth Hlb") as %[_ Hle].
  Qed.
(* iris_heap_lang/primitive_laws.v *)
Lemma steps_lb_get n :
    steps_auth n -∗ steps_lb n.
Proof. apply mono_nat_lb_own_get. Qed.
(* iris_heap_lang/primitive_laws.v *)
Lemma steps_auth_update n n' :
    n ≤ n' → steps_auth n ==∗ steps_auth n' ∗ steps_lb n'.
Proof. intros Hle. by apply mono_nat_own_update. Qed.
(* iris_heap_lang/primitive_laws.v *)
Lemma steps_auth_update_S n :
    steps_auth n ==∗ steps_auth (S n).
Proof.
    iIntros "Hauth".
    iMod (mono_nat_own_update with "Hauth") as "[$ _]"; [lia|done].
  Qed.
(* iris_heap_lang/primitive_laws.v *)
Lemma steps_lb_le n n' :
    n' ≤ n → steps_lb n -∗ steps_lb n'.
Proof. intros Hle. by iApply mono_nat_lb_own_le. Qed.
(* iris_heap_lang/primitive_laws.v *)
Instance heapGS_irisGS `{!heapGS_gen hlc Σ} : irisGS_gen hlc heap_lang Σ := {
  iris_invGS := heapGS_invGS;
  state_interp σ step_cnt κs _ :=
    (gen_heap_interp σ.(heap) ∗ proph_map_interp κs σ.(used_proph_id) ∗
     steps_auth step_cnt)%I;
  fork_post _ := True%I;
  num_laters_per_step n := n;
}.
Next Obligation.
  iIntros (??? σ ns κs nt)  "/= ($ & $ & H)".
  by iMod (steps_auth_update_S with "H") as "$".
Qed.
(* iris_heap_lang/primitive_laws.v *)
(* Since we use an [option val] instance of [gen_heap], we need to add a
[Some]. We also seal the definition to avoid confusion between the version from
[gen_heap], and our version with [Some]. That also helps for scopes and
coercions. *)
Definition pointsto_def `{heapGS_gen hlc Σ}
    (l : loc) (dq : dfrac) (v : val) : iProp Σ
:=
  pointsto l dq (Some v).
(* iris_heap_lang/primitive_laws.v *)
Definition pointsto_unseal :
  @pointsto = @pointsto_def
:= pointsto_aux.(seal_eq).
(* iris_heap_lang/primitive_laws.v *)
Definition inv_pointsto_own (l : loc) (v : val) (I : val → Prop) : iProp Σ
:=
    inv_pointsto_own l (Some v) (from_option I False).
(* iris_heap_lang/primitive_laws.v *)
Definition inv_pointsto (l : loc) (I : val → Prop) : iProp Σ
:=
    inv_pointsto l (from_option I False).
(* iris_heap_lang/primitive_laws.v *)
Instance : Params (@inv_pointsto_own) 4
:= {}.
(* iris_heap_lang/primitive_laws.v *)
Instance : Params (@inv_pointsto) 3
:= {}.
(* iris_heap_lang/primitive_laws.v *)
Notation inv_heap_inv
:= (inv_heap_inv loc (option val)).
(* iris_heap_lang/primitive_laws.v *)
Notation "l ↦_ I v"
:= (inv_pointsto_own l v I%stdpp%type)
  (at level 20, I at level 9, format "l  ↦_ I  v") : bi_scope.
(* iris_heap_lang/primitive_laws.v *)
Lemma wp_lb_update s n E e Φ :
  TCEq (to_val e) None →
  steps_lb n -∗
  WP e @ s; E {{ v, steps_lb (S n) -∗ Φ v }} -∗
  WP e @ s; E {{ Φ }}.
Proof.
  (** TODO: We should try to use a generic lifting lemma (and avoid [wp_unfold])
  here, since this breaks the WP abstraction. *)
  rewrite !wp_unfold /wp_pre /=. iIntros (->) "Hlb Hwp".
  iIntros (σ1 ns κ κs m) "(Hσ & Hκ & Hsteps)".
  iDestruct (steps_lb_valid with "Hsteps Hlb") as %?.
  iMod ("Hwp" $! σ1 ns κ κs m with "[$Hσ $Hκ $Hsteps]") as "[%Hs Hwp]".
  iModIntro. iSplit; [done|].
  iIntros (e2 σ2 efs Hstep) "Hcred".
  iMod ("Hwp" with "[//] Hcred") as "Hwp".
  iIntros "!> !>". iMod "Hwp" as "Hwp". iIntros "!>".
  iApply (step_fupdN_wand with "Hwp").
  iIntros "Hwp". iMod "Hwp" as "(($ & $ & Hsteps)& Hwp & $)".
  iDestruct (steps_lb_get with "Hsteps") as "#HlbS".
  iDestruct (steps_lb_le _ (S n) with "HlbS") as "#HlbS'"; [lia|].
  iModIntro. iFrame "Hsteps".
  iApply (wp_wand with "Hwp"). iIntros (v) "HΦ". by iApply "HΦ".
Qed.
(* iris_heap_lang/primitive_laws.v *)
(* A stronger version of [twp_wp_step] to turn a TWP into a WP. Provided
[steps_lb n], this version gives us [S n] laters ([twp_wp_step] gives just one)
and [S n] later credits ([twp_wp_step] gives none). *)
Lemma twp_wp_step_lc s n E e P Φ :
  TCEq (to_val e) None →
  steps_lb n -∗
  ▷^(S n) P -∗
  WP e @ s; E [{ v, P -∗ £ (S n) ={E}=∗ Φ v }] -∗
  WP e @ s; E {{ Φ }}.
Proof.
  iIntros (?) "#Hlb HP Hwp".
  iApply (twp_wp_fupdN_strong (S n) _ _ _ _ (P ∗ £ (S n))); [done|]. iSplit.
  - iIntros (σ ns κ m) "(Hσ & Hκ & Hsteps)".
    iDestruct (steps_lb_valid with "[$] [$]") as %?.
    iApply fupd_mask_intro_discard; [set_solver|].
    iPureIntro. rewrite /num_laters_per_step /=. lia.
  - iSplitL "HP".
    + iIntros "!> ?". iApply step_fupdN_intro; [done|].
      iIntros "!> !>". by iFrame.
    + iApply (twp_wand with "Hwp").
      iIntros (v) "H [??]". by iApply ("H" with "[$]").
Qed.
(* iris_heap_lang/primitive_laws.v *)
(* A version of [twp_wp_step_lc] that provides only a single later modality
(but still [S n] later credits). This version is tailored to lift total Texan
triples to a partial Texan triples with later credits, see e.g., [wp_alloc_lc]
below. *)
Lemma twp_wp_step_lc_texan s n E e P Φ :
  TCEq (to_val e) None →
  steps_lb n -∗
  ▷ P -∗
  WP e @ s; E [{ v, P -∗ £ (S n) ={E}=∗ Φ v }] -∗
  WP e @ s; E {{ Φ }}.
Proof.
  iIntros (?) "#Hlb HP Hwp".
  iApply (twp_wp_step_lc with "[$] [HP] Hwp"); auto.
Qed.
(* iris_heap_lang/primitive_laws.v *)
Lemma wp_step_fupdN_lb s n E1 E2 e P Φ :
  TCEq (to_val e) None →
  E2 ⊆ E1 →
  steps_lb n -∗
  (|={E1∖E2,∅}=> |={∅}▷=>^(S n) |={∅,E1∖E2}=> P) -∗
  WP e @ s; E2 {{ v, P ={E1}=∗ Φ v }} -∗
  WP e @ s; E1 {{ Φ }}.
Proof.
  iIntros (He HE) "Hlb HP Hwp".
  iApply wp_step_fupdN; [done|].
  iSplit; [|by iFrame].
  iIntros (σ ns κs nt) "(? & ? & Hsteps)".
  iDestruct (steps_lb_valid with "Hsteps Hlb") as %Hle.
  iApply fupd_mask_intro; [set_solver|].
  iIntros "_". iPureIntro. rewrite /num_laters_per_step /=. lia.
Qed.
(* iris_heap_lang/primitive_laws.v *)
(* Recursive functions: we do not use this lemmas as it is easier to use Löb
induction directly, but this demonstrates that we can state the expected
reasoning principle for recursive functions, without any visible ▷. *)
Lemma wp_rec_löb s E f x e Φ Ψ :
  □ ( □ (∀ v, Ψ v -∗ WP (rec: f x := e)%V v @ s; E {{ Φ }}) -∗
     ∀ v, Ψ v -∗ WP (subst' x v (subst' f (rec: f x := e) e)) @ s; E {{ Φ }}) -∗
  ∀ v, Ψ v -∗ WP (rec: f x := e)%V v @ s; E {{ Φ }}.
Proof.
  iIntros "#Hrec". iLöb as "IH". iIntros (v) "HΨ".
  iApply lifting.wp_pure_step_later; first done.
  iIntros "!> _". iApply ("Hrec" with "[] HΨ"). iIntros "!>" (w) "HΨ".
  iApply ("IH" with "HΨ").
Qed.
(* iris_heap_lang/primitive_laws.v *)
(* This slightly stronger version of the law allows a fancy update *after* stripping
the ▷ and *before* splitting the resources among the two threads. This is rarely ever
needed, but is technically stronger than [wp_fork] below. *)
Lemma wp_fork_fupd s E e Φ :
  ▷ (|={E}=> WP e @ s; ⊤ {{ _, True }} ∗ Φ (LitV LitUnit)) ⊢ WP Fork e @ s; E {{ Φ }}.
Proof.
  iIntros "HeΦ". iApply wp_lift_atomic_base_step; [done|].
  iIntros (σ1 ns κ κs nt) "(?&?&Hsteps) !>"; iSplit; first by eauto with base_step.
  iIntros "!>" (v2 σ2 efs Hstep) "_"; inv_base_step.
  iMod (steps_auth_update_S with "Hsteps") as "Hsteps".
  iMod "HeΦ" as "[He HΦ]".
  by iFrame.
Qed.
(* iris_heap_lang/primitive_laws.v *)
Lemma wp_fork s E e Φ :
  ▷ WP e @ s; ⊤ {{ _, True }} -∗ ▷ Φ (LitV LitUnit) -∗ WP Fork e @ s; E {{ Φ }}.
Proof.
  iIntros "He HΦ". iApply wp_fork_fupd. iNext. iModIntro. iFrame.
Qed.
(* iris_heap_lang/primitive_laws.v *)
Lemma twp_fork s E e Φ :
  WP e @ s; ⊤ [{ _, True }] -∗ Φ (LitV LitUnit) -∗ WP Fork e @ s; E [{ Φ }].
Proof.
  iIntros "He HΦ". iApply twp_lift_atomic_base_step; [done|].
  iIntros (σ1 ns κs nt) "(?&?&Hsteps) !>"; iSplit; first by eauto with base_step.
  iIntros (κ v2 σ2 efs Hstep); inv_base_step.
  iMod (steps_auth_update_S with "Hsteps") as "Hsteps".
  by iFrame.
Qed.
(* iris_heap_lang/primitive_laws.v *)
(* We need to adjust the [gen_heap] and [gen_inv_heap] lemmas because of our
value type being [option val] (and sealing). *)
Instance pointsto_timeless l dq v : Timeless (l ↦{dq} v).
Proof. rewrite pointsto_unseal. apply _. Qed.
(* iris_heap_lang/primitive_laws.v *)
Instance pointsto_fractional l v : Fractional (λ q, l ↦{#q} v)%I.
Proof. rewrite pointsto_unseal. apply _. Qed.
(* iris_heap_lang/primitive_laws.v *)
Instance pointsto_as_fractional l q v :
  AsFractional (l ↦{#q} v) (λ q, l ↦{#q} v)%I q.
Proof. rewrite pointsto_unseal. apply _. Qed.
(* iris_heap_lang/primitive_laws.v *)
Instance pointsto_persistent l v : Persistent (l ↦□ v).
Proof. rewrite pointsto_unseal. apply _. Qed.
(* iris_heap_lang/primitive_laws.v *)
Instance pointsto_combine_sep_gives l dq1 dq2 v1 v2 : 
  CombineSepGives (l ↦{dq1} v1) (l ↦{dq2} v2) ⌜✓ (dq1 ⋅ dq2) ∧ v1 = v2⌝.
Proof.
  rewrite pointsto_unseal /CombineSepGives. iIntros "[H1 H2]".
  iCombine "H1 H2" gives %[? [=->]]. eauto.
Qed.
(* iris_heap_lang/primitive_laws.v *)
Lemma pointsto_combine l dq1 dq2 v1 v2 :
  l ↦{dq1} v1 -∗ l ↦{dq2} v2 -∗ l ↦{dq1 ⋅ dq2} v1 ∗ ⌜v1 = v2⌝.
Proof.
  rewrite pointsto_unseal.
  iIntros "Hl1 Hl2". by iCombine "Hl1 Hl2" as "$" gives %[_ [= ->]].
Qed.
(* iris_heap_lang/primitive_laws.v *)
Instance pointsto_combine_as l dq1 dq2 v1 v2 :
  CombineSepAs (l ↦{dq1} v1) (l ↦{dq2} v2) (l ↦{dq1 ⋅ dq2} v1) | 60.
(* higher cost than the Fractional instance, which kicks in for #qs *)
Proof.
  rewrite /CombineSepAs. iIntros "[H1 H2]".
  iDestruct (pointsto_combine with "H1 H2") as "[$ _]".
Qed.
(* iris_heap_lang/primitive_laws.v *)
Lemma pointsto_valid l dq v : l ↦{dq} v -∗ ⌜✓ dq⌝.
Proof. rewrite pointsto_unseal. apply pointsto_valid. Qed.
(* iris_heap_lang/primitive_laws.v *)
Lemma pointsto_valid_2 l dq1 dq2 v1 v2 :
  l ↦{dq1} v1 -∗ l ↦{dq2} v2 -∗ ⌜✓ (dq1 ⋅ dq2) ∧ v1 = v2⌝.
Proof. iIntros "H1 H2". iCombine "H1 H2" gives %[? [= ?]]. done. Qed.
(* iris_heap_lang/primitive_laws.v *)
Lemma pointsto_agree l dq1 dq2 v1 v2 : l ↦{dq1} v1 -∗ l ↦{dq2} v2 -∗ ⌜v1 = v2⌝.
Proof. iIntros "H1 H2". iCombine "H1 H2" gives %[_ [= ?]]. done. Qed.
(* iris_heap_lang/primitive_laws.v *)
Lemma pointsto_frac_ne l1 l2 dq1 dq2 v1 v2 :
  ¬ ✓(dq1 ⋅ dq2) → l1 ↦{dq1} v1 -∗ l2 ↦{dq2} v2 -∗ ⌜l1 ≠ l2⌝.
Proof. rewrite pointsto_unseal. apply pointsto_frac_ne. Qed.
(* iris_heap_lang/primitive_laws.v *)
Lemma pointsto_ne l1 l2 dq2 v1 v2 : l1 ↦ v1 -∗ l2 ↦{dq2} v2 -∗ ⌜l1 ≠ l2⌝.
Proof. rewrite pointsto_unseal. apply pointsto_ne. Qed.
(* iris_heap_lang/primitive_laws.v *)
Lemma pointsto_persist l dq v : l ↦{dq} v ==∗ l ↦□ v.
Proof. rewrite pointsto_unseal. apply pointsto_persist. Qed.
(* iris_heap_lang/primitive_laws.v *)
Lemma pointsto_unpersist l v : l ↦□ v ==∗ ∃ q, l ↦{#q} v.
Proof. rewrite pointsto_unseal. apply pointsto_unpersist. Qed.
(* iris_heap_lang/primitive_laws.v *)
Instance frame_pointsto p l v q1 q2 q :
  FrameFractionalQp q1 q2 q →
  Frame p (l ↦{#q1} v) (l ↦{#q2} v) (l ↦{#q} v) | 5.
Proof. apply: frame_fractional. Qed.
(* iris_heap_lang/primitive_laws.v *)
Instance inv_pointsto_own_proper l v :
  Proper (pointwise_relation _ iff ==> (≡)) (inv_pointsto_own l v).
Proof.
  intros I1 I2 HI. rewrite /inv_pointsto_own. f_equiv=>-[w|]; last done.
  simpl. apply HI.
Qed.
(* iris_heap_lang/primitive_laws.v *)
Instance inv_pointsto_proper l :
  Proper (pointwise_relation _ iff ==> (≡)) (inv_pointsto l).
Proof.
  intros I1 I2 HI. rewrite /inv_pointsto. f_equiv=>-[w|]; last done.
  simpl. apply HI.
Qed.
(* iris_heap_lang/primitive_laws.v *)
Lemma make_inv_pointsto l v (I : val → Prop) E :
  ↑inv_heapN ⊆ E →
  I v →
  inv_heap_inv -∗ l ↦ v ={E}=∗ l ↦_I v.
Proof.
  iIntros (??) "#HI Hl". rewrite pointsto_unseal.
  iApply make_inv_pointsto; done.
Qed.
(* iris_heap_lang/primitive_laws.v *)
Lemma inv_pointsto_own_inv l v I : l ↦_I v -∗ l ↦_I □.
Proof. apply inv_pointsto_own_inv. Qed.
(* iris_heap_lang/primitive_laws.v *)
Lemma inv_pointsto_own_acc_strong E :
  ↑inv_heapN ⊆ E →
  inv_heap_inv ={E, E ∖ ↑inv_heapN}=∗ ∀ l v I, l ↦_I v -∗
    (⌜I v⌝ ∗ l ↦ v ∗ (∀ w, ⌜I w ⌝ -∗ l ↦ w ==∗
      inv_pointsto_own l w I ∗ |={E ∖ ↑inv_heapN, E}=> True)).
Proof.
  iIntros (?) "#Hinv". rewrite pointsto_unseal.
  iMod (inv_pointsto_own_acc_strong with "Hinv") as "Hacc"; first done.
  iIntros "!>" (l v I) "Hl". iDestruct ("Hacc" with "Hl") as "(% & Hl & Hclose)".
  iFrame "%∗". iIntros (w) "% Hl". iApply "Hclose"; done.
Qed.
(* iris_heap_lang/primitive_laws.v *)
Lemma inv_pointsto_own_acc E l v I:
  ↑inv_heapN ⊆ E →
  inv_heap_inv -∗ l ↦_I v ={E, E ∖ ↑inv_heapN}=∗
    (⌜I v⌝ ∗ l ↦ v ∗ (∀ w, ⌜I w ⌝ -∗ l ↦ w ={E ∖ ↑inv_heapN, E}=∗ l ↦_I w)).
Proof.
  iIntros (?) "#Hinv Hl". rewrite pointsto_unseal.
  iMod (inv_pointsto_own_acc with "Hinv Hl") as "(% & Hl & Hclose)"; first done.
  iFrame "%∗". iIntros "!>" (w) "% Hl". iApply "Hclose"; done.
Qed.
(* iris_heap_lang/primitive_laws.v *)
Lemma inv_pointsto_acc l I E :
  ↑inv_heapN ⊆ E →
  inv_heap_inv -∗ l ↦_I □ ={E, E ∖ ↑inv_heapN}=∗
    ∃ v, ⌜I v⌝ ∗ l ↦ v ∗ (l ↦ v ={E ∖ ↑inv_heapN, E}=∗ True).
Proof.
  iIntros (?) "#Hinv Hl". rewrite pointsto_unseal.
  iMod (inv_pointsto_acc with "Hinv Hl") as ([v|]) "(% & Hl & Hclose)"; [done| |done].
  iIntros "!>". iExists (v). iFrame "%∗".
Qed.
(* iris_heap_lang/primitive_laws.v *)
(* The usable rules for [allocN] stated in terms of the [array] proposition
are derived in te file [array]. *)
Lemma heap_array_to_seq_meta l vs (n : nat) :
  length vs = n →
  ([∗ map] l' ↦ _ ∈ heap_array l vs, meta_token l' ⊤) -∗
  [∗ list] i ∈ seq 0 n, meta_token (l +ₗ (i : nat)) ⊤.
Proof.
  iIntros (<-) "Hvs". iInduction vs as [|v vs IH] forall (l)=> //=.
  rewrite big_opM_union; last first.
  { apply map_disjoint_spec=> l' v1 v2 /lookup_singleton_Some [-> _].
    intros (j&w&?&Hjl&?&?)%heap_array_lookup.
    rewrite Loc.add_assoc -{1}[l']Loc.add_0 in Hjl. simplify_eq; lia. }
  rewrite Loc.add_0 -fmap_S_seq big_sepL_fmap.
  setoid_rewrite Nat2Z.inj_succ. setoid_rewrite <-Z.add_1_l.
  setoid_rewrite <-Loc.add_assoc.
  rewrite big_opM_singleton; iDestruct "Hvs" as "[$ Hvs]". by iApply "IH".
Qed.
(* iris_heap_lang/primitive_laws.v *)
Lemma heap_array_to_seq_pointsto l v (n : nat) :
  ([∗ map] l' ↦ ov ∈ heap_array l (replicate n v), gen_heap.pointsto l' (DfracOwn 1) ov) -∗
  [∗ list] i ∈ seq 0 n, (l +ₗ (i : nat)) ↦ v.
Proof.
  iIntros "Hvs". iInduction n as [|n IH] forall (l); simpl.
  { done. }
  rewrite big_opM_union; last first.
  { apply map_disjoint_spec=> l' v1 v2 /lookup_singleton_Some [-> _].
    intros (j&w&?&Hjl&_)%heap_array_lookup.
    rewrite Loc.add_assoc -{1}[l']Loc.add_0 in Hjl. simplify_eq; lia. }
  rewrite Loc.add_0 -fmap_S_seq big_sepL_fmap.
  setoid_rewrite Nat2Z.inj_succ. setoid_rewrite <-Z.add_1_l.
  setoid_rewrite <-Loc.add_assoc.
  rewrite big_opM_singleton pointsto_unseal.
  iDestruct "Hvs" as "[$ Hvs]". by iApply "IH".
Qed.
(* iris_heap_lang/primitive_laws.v *)
Lemma twp_allocN_seq s E v n :
  (0 < n)%Z →
  [[{ True }]] AllocN (Val $ LitV $ LitInt $ n) (Val v) @ s; E
  [[{ l, RET LitV (LitLoc l); [∗ list] i ∈ seq 0 (Z.to_nat n),
      (l +ₗ (i : nat)) ↦ v ∗ meta_token (l +ₗ (i : nat)) ⊤ }]].
Proof.
  iIntros (Hn Φ) "_ HΦ". iApply twp_lift_atomic_base_step_no_fork; first done.
  iIntros (σ1 ns κs nt) "(Hσ & Hκs & Hsteps) !>".
  iSplit; first by destruct n; auto with lia base_step.
  iIntros (κ v2 σ2 efs Hstep); inv_base_step.
  iMod (gen_heap_alloc_big _ (heap_array _ (replicate (Z.to_nat n) v)) with "Hσ")
    as "(Hσ & Hl & Hm)".
  { apply heap_array_map_disjoint.
    rewrite length_replicate Z2Nat.id; auto with lia. }
  iMod (steps_auth_update_S with "Hsteps") as "Hsteps".
  iModIntro; do 2 (iSplit; first done). iFrame "Hσ Hκs Hsteps". iApply "HΦ".
  iApply big_sepL_sep. iSplitL "Hl".
  - by iApply heap_array_to_seq_pointsto.
  - iApply (heap_array_to_seq_meta with "Hm"). by rewrite length_replicate.
Qed.
(* iris_heap_lang/primitive_laws.v *)
Lemma wp_allocN_seq s E v n :
  (0 < n)%Z →
  {{{ True }}} AllocN (Val $ LitV $ LitInt $ n) (Val v) @ s; E
  {{{ l, RET LitV (LitLoc l); [∗ list] i ∈ seq 0 (Z.to_nat n),
      (l +ₗ (i : nat)) ↦ v ∗ meta_token (l +ₗ (i : nat)) ⊤ }}}.
Proof.
  iIntros (Hn Φ) "_ HΦ". iApply (twp_wp_step with "HΦ").
  iApply twp_allocN_seq; [by auto..|]; iIntros (l) "H HΦ". by iApply "HΦ".
Qed.
(* iris_heap_lang/primitive_laws.v *)
Lemma wp_allocN_seq_lc s E v n n' :
  (0 < n)%Z →
  {{{ steps_lb n' }}} AllocN (Val $ LitV $ LitInt $ n) (Val v) @ s; E
  {{{ l, RET LitV (LitLoc l); ([∗ list] i ∈ seq 0 (Z.to_nat n),
      (l +ₗ (i : nat)) ↦ v ∗ meta_token (l +ₗ (i : nat)) ⊤) ∗ £ (S n') }}}.
Proof.
  iIntros (Hn Φ) "#? HΦ". iApply (twp_wp_step_lc_texan with "[$] HΦ").
  iApply twp_allocN_seq; [by auto..|]; iIntros (l) "H HΦ ? !>".
  iApply "HΦ"; iFrame.
Qed.
(* iris_heap_lang/primitive_laws.v *)
Lemma twp_alloc s E v :
  [[{ True }]]
    Alloc (Val v) @ s; E
  [[{ l, RET LitV (LitLoc l); l ↦ v ∗ meta_token l ⊤ }]].
Proof.
  iIntros (Φ) "_ HΦ". iApply twp_allocN_seq; [auto with lia..|].
  iIntros (l) "/= (? & _)". rewrite Loc.add_0. iApply "HΦ"; iFrame.
Qed.
(* iris_heap_lang/primitive_laws.v *)
Lemma wp_alloc s E v :
  {{{ True }}}
    Alloc (Val v) @ s; E
  {{{ l, RET LitV (LitLoc l); l ↦ v ∗ meta_token l ⊤ }}}.
Proof.
  iIntros (Φ) "_ HΦ". iApply (twp_wp_step with "HΦ").
  iApply twp_alloc; [by auto..|]; iIntros (l) "H HΦ". by iApply "HΦ".
Qed.
(* iris_heap_lang/primitive_laws.v *)
Lemma wp_alloc_lc s E v n :
  {{{ steps_lb n }}}
    Alloc (Val v) @ s; E
  {{{ l, RET LitV (LitLoc l); l ↦ v ∗ meta_token l ⊤ ∗ £ (S n) }}}.
Proof.
  iIntros (Φ) "#? HΦ". iApply (twp_wp_step_lc_texan with "[$] HΦ").
  iApply twp_alloc; [by auto..|]; iIntros (l) "[??] HΦ ? !>".
  iApply "HΦ"; iFrame.
Qed.
(* iris_heap_lang/primitive_laws.v *)
Lemma twp_free s E l v :
  [[{ l ↦ v }]] Free (Val $ LitV $ LitLoc l) @ s; E
  [[{ RET LitV LitUnit; True }]].
Proof.
  iIntros (Φ) "Hl HΦ". iApply twp_lift_atomic_base_step_no_fork; first done.
  iIntros (σ1 ns κs nt) "(Hσ & Hκs & Hsteps) !>".
  rewrite pointsto_unseal. iDestruct (gen_heap_valid with "Hσ Hl") as %?.
  iSplit; first by eauto with base_step.
  iIntros (κ v2 σ2 efs Hstep); inv_base_step.
  iMod (gen_heap_update with "Hσ Hl") as "[$ Hl]".
  iMod (steps_auth_update_S with "Hsteps") as "Hsteps".
  iModIntro. iSplit; first done. iSplit; first done. iFrame. by iApply "HΦ".
Qed.
(* iris_heap_lang/primitive_laws.v *)
Lemma wp_free s E l v :
  {{{ ▷ l ↦ v }}} Free (Val $ LitV (LitLoc l)) @ s; E
  {{{ RET LitV LitUnit; True }}}.
Proof.
  iIntros (Φ) ">H HΦ". iApply (twp_wp_step with "HΦ").
  iApply (twp_free with "H"); [by auto..|]; iIntros "H HΦ". by iApply "HΦ".
Qed.
(* iris_heap_lang/primitive_laws.v *)
Lemma wp_free_lc s E l v n :
  {{{ steps_lb n ∗ ▷ l ↦ v }}} Free (Val $ LitV (LitLoc l)) @ s; E
  {{{ RET LitV LitUnit; £ (S n) }}}.
Proof.
  iIntros (Φ) "[#? >H] HΦ". iApply (twp_wp_step_lc_texan with "[$] HΦ").
  iApply (twp_free with "H"); [by auto..|]; iIntros "H HΦ ? !>". by iApply "HΦ".
Qed.
(* iris_heap_lang/primitive_laws.v *)
Lemma twp_load s E l dq v :
  [[{ l ↦{dq} v }]] Load (Val $ LitV $ LitLoc l) @ s; E [[{ RET v; l ↦{dq} v }]].
Proof.
  iIntros (Φ) "Hl HΦ". iApply twp_lift_atomic_base_step_no_fork; first done.
  iIntros (σ1 ns κs nt) "(Hσ & Hκs & Hsteps) !>".
  rewrite pointsto_unseal. iDestruct (gen_heap_valid with "Hσ Hl") as %?.
  iSplit; first by eauto with base_step.
  iIntros (κ v2 σ2 efs Hstep); inv_base_step.
  iMod (steps_auth_update_S with "Hsteps") as "Hsteps".
  iModIntro. iSplit; [done|]. iSplit; [done|]. iFrame.
  by iApply "HΦ".
Qed.
(* iris_heap_lang/primitive_laws.v *)
Lemma wp_load s E l dq v :
  {{{ ▷ l ↦{dq} v }}} Load (Val $ LitV $ LitLoc l) @ s; E {{{ RET v; l ↦{dq} v }}}.
Proof.
  iIntros (Φ) ">H HΦ". iApply (twp_wp_step with "HΦ").
  iApply (twp_load with "H"). iIntros "H HΦ". by iApply "HΦ".
Qed.
(* iris_heap_lang/primitive_laws.v *)
Lemma wp_load_lc s E l dq v n :
  {{{ steps_lb n ∗ ▷ l ↦{dq} v }}}
    Load (Val $ LitV $ LitLoc l) @ s; E
  {{{ RET v; l ↦{dq} v ∗ £ (S n) }}}.
Proof.
  iIntros (Φ) "[#? >H] HΦ". iApply (twp_wp_step_lc_texan with "[$] HΦ").
  iApply (twp_load with "H"). iIntros "H HΦ ? !>". iApply "HΦ"; iFrame.
Qed.
(* iris_heap_lang/primitive_laws.v *)
Lemma twp_store s E l v' v :
  [[{ l ↦ v' }]] Store (Val $ LitV $ LitLoc l) (Val v) @ s; E
  [[{ RET LitV LitUnit; l ↦ v }]].
Proof.
  iIntros (Φ) "Hl HΦ". iApply twp_lift_atomic_base_step_no_fork; first done.
  iIntros (σ1 ns κs nt) "(Hσ & Hκs & Hsteps) !>".
  rewrite pointsto_unseal. iDestruct (gen_heap_valid with "Hσ Hl") as %?.
  iSplit; first by eauto with base_step.
  iIntros (κ v2 σ2 efs Hstep); inv_base_step.
  iMod (steps_auth_update_S with "Hsteps") as "Hsteps".
  iMod (gen_heap_update with "Hσ Hl") as "[$ Hl]".
  iModIntro. iSplit; first done. iSplit; first done. iFrame. by iApply "HΦ".
Qed.
(* iris_heap_lang/primitive_laws.v *)
Lemma wp_store s E l v' v :
  {{{ ▷ l ↦ v' }}} Store (Val $ LitV (LitLoc l)) (Val v) @ s; E
  {{{ RET LitV LitUnit; l ↦ v }}}.
Proof.
  iIntros (Φ) ">H HΦ". iApply (twp_wp_step with "HΦ").
  iApply (twp_store with "H"); [by auto..|]; iIntros "H HΦ". by iApply "HΦ".
Qed.
(* iris_heap_lang/primitive_laws.v *)
Lemma wp_store_lc s E l v' v n :
  {{{ steps_lb n ∗ ▷ l ↦ v' }}} Store (Val $ LitV (LitLoc l)) (Val v) @ s; E
  {{{ RET LitV LitUnit; l ↦ v ∗ £ (S n) }}}.
Proof.
  iIntros (Φ) "[#? >H] HΦ". iApply (twp_wp_step_lc_texan with "[$] HΦ").
  iApply (twp_store with "H"); [by auto..|]; iIntros "H HΦ ? !>".
  iApply "HΦ"; iFrame.
Qed.
(* iris_heap_lang/primitive_laws.v *)
Lemma twp_xchg s E l v' v :
  [[{ l ↦ v' }]] Xchg (Val $ LitV $ LitLoc l) (Val v) @ s; E
  [[{ RET v'; l ↦ v }]].
Proof.
  iIntros (Φ) "Hl HΦ". iApply twp_lift_atomic_base_step_no_fork; first done.
  iIntros (σ1 ns κs nt) "(Hσ & Hκs & Hsteps) !>".
  rewrite pointsto_unseal. iDestruct (gen_heap_valid with "Hσ Hl") as %?.
  iSplit; first by eauto with base_step.
  iIntros (κ v2 σ2 efs Hstep); inv_base_step.
  iMod (steps_auth_update_S with "Hsteps") as "Hsteps".
  iMod (gen_heap_update with "Hσ Hl") as "[$ Hl]".
  iModIntro. iSplit; first done. iSplit; first done. iFrame. by iApply "HΦ".
Qed.
(* iris_heap_lang/primitive_laws.v *)
Lemma wp_xchg s E l v' v :
  {{{ ▷ l ↦ v' }}} Xchg (Val $ LitV (LitLoc l)) (Val v) @ s; E
  {{{ RET v'; l ↦ v }}}.
Proof.
  iIntros (Φ) ">H HΦ". iApply (twp_wp_step with "HΦ").
  iApply (twp_xchg with "H"); [by auto..|]. iIntros "H HΦ". by iApply "HΦ".
Qed.
(* iris_heap_lang/primitive_laws.v *)
Lemma wp_xchg_lc s E l v' v n :
  {{{ steps_lb n ∗ ▷ l ↦ v' }}} Xchg (Val $ LitV (LitLoc l)) (Val v) @ s; E
  {{{ RET v'; l ↦ v ∗ £ (S n) }}}.
Proof.
  iIntros (Φ) "[#? >H] HΦ". iApply (twp_wp_step_lc_texan with "[$] HΦ").
  iApply (twp_xchg with "H"); [by auto..|]. iIntros "H HΦ ? !>".
  iApply "HΦ"; iFrame.
Qed.