Ranvier.Heap.Collections

Reading settings

Look

Mode

Definitions

Proofs

Imports

Arguments

References

References open

Saved in this browser.

ModulesGuarantees

Modules

Audit0Extract12

Base

Prelude9Syntax43

Reference

Semantics63Properties202

Pure

ObserverSet29SourceList104State16Edges12Marking117Engine103Check204Runs259Scheduler241Ownership49WholeGraph562Refinement465Witnesses244Optimised271Ledger6Collections302Concurrency141Extensions123Deltas68FastPaths435Combinators106MemoTable147MemoTableWitnesses62MemoTableFastPaths113

Heap

ObserverSet46SourceList89Ghost160Marking22Run286Scheduler7Ownership29Adequacy3Collections38Concurrency41Extensions17Deltas10MemoTable28

Conformance

Expected64Check119
On this page

On this page

Heap collection-boundary primitivesTwo-queue scheduler heap code (Core.fs lines 2155–2167, 3090–3215)schedule_pass_tq_codeschedule_effect_tq_codeForwardingEdge heap representation and code (Core.fs lines 503–523)forwarding_edge_new_codeforwarding_edge_moved_in_codeforwarding_edge_mark_dirty_codeforwarding_edge_mark_check_codeMemo.Adopt heap representation and code (Core.fs lines 4094, 4647–4651, 4683–4696)adopt_new_codeadopt_step_codeadopt_dispose_code1. Direct-observer edge boundary and ForwardingEdgeworld_direct_link_specworld_direct_unlink_specis_forwarding_edgeforwarding_edge_moved_in_specforwarding_edge_mark_spec2. Active OwnerLink unlinking for per-key child scopeslink_detachedowner_links_active_tail_unlink_specowner_links_active_mid_unlink_spec3. Two-queue pass and effect schedulingis_two_queueschedule_pass_tq_specschedule_effect_tq_spec4. Collection signal and row hookswrite_signal_except_specnotify_removed_spec4. Memo.Adopt companion resource lifecycleis_adoptmemo_adopt_specmemo_dispose_adopted_spec1-Based Fenwick Prefix Sums and Point Updatesfenwick_sum_loopfenwick_sum_codefenwick_sum_loop_specfenwick_sum_specfenwick_add_loopfenwick_add_codefenwick_add_loop_specfenwick_add_spec7. Incremental `SortView` inverse-order array shift (Combinators.fs)inv_order_shift_loopinv_order_shift_codeinv_order_shift_loop_specinv_order_shift_spec
This definition

Ranvier.Heap.Collections

Heap collection-boundary primitives

This module lifts the four collection-boundary areas of Pure/Collections.v to HeapLang separation logic:

  1. Direct-observer edge attachment (world_direct_link_spec), detachment (world_direct_unlink_spec), and ForwardingEdge (forwarding_edge_moved_in_spec, forwarding_edge_mark_spec).
  2. Active OwnerLink unlinking (owner_links_active_tail_unlink_spec and owner_links_active_mid_unlink_spec) for per-key child scopes and reparenting.
  3. Two-queue pass and effect scheduling (schedule_pass_tq_spec and schedule_effect_tq_spec).
  4. Collection signal and row hooks (write_signal_except_spec and notify_removed_spec), plus Memo.Adopt companion resource lifecycle (memo_adopt_spec and memo_dispose_adopted_spec).
8
From stdpp Require Import prelude gmap.
From Stdlib Require Import ZArith.
From Ranvier.Base Require Import Prelude Syntax.
From Ranvier.Pure Require Import ObserverSet SourceList State Edges Marking Engine Check Runs Scheduler Ownership Collections.
From iris.heap_lang Require Import proofmode notation.
From iris.heap_lang.lib Require Import array.
From Ranvier.Heap Require Import ObserverSet SourceList Ghost Marking Run Scheduler Ownership.
Open Scope nat_scope.

Two-queue scheduler heap code (Core.fs lines 2155–2167, 3090–3215)

Definition schedule_pass_tq_code : val :=
  λ: "tq" "n",
    let: "ps" := !("tq" +ₗ #4) in
    if: vlist_mem_nat_code "n" "ps" then #()
    else
      "tq" <- vlist_snoc_code !"tq" "n";;
      ("tq" +ₗ #4) <- SOME ("n", "ps").
References
Definition schedule_effect_tq_code : val :=
  λ: "tq" "n",
    let: "es" := !("tq" +ₗ #5) in
    if: vlist_mem_nat_code "n" "es" then #()
    else
      ("tq" +ₗ #2) <- vlist_snoc_code !("tq" +ₗ #2) "n";;
      ("tq" +ₗ #5) <- SOME ("n", "es").
References

ForwardingEdge heap representation and code (Core.fs lines 503–523)

Definition forwarding_edge_new_code : val :=
  λ: "target" "h",
    let: "fe" := AllocN #2 #0 in
    "fe" <- "target";;
    ("fe" +ₗ #1) <- "h";;
    "fe".
References

Uses

  • heap_lang.AllocN
  • heap_lang.val
Definition forwarding_edge_moved_in_code : val :=
  λ: "fe" "_sourceSlot" "newHandle",
    ("fe" +ₗ #1) <- "newHandle".
References

Uses

  • heap_lang.val
Definition forwarding_edge_mark_dirty_code : val :=
  λ: "fe" "g",
    let: "target" := !"fe" in
    mark_dirty_code "g" "target".
References

Uses

Definition forwarding_edge_mark_check_code : val :=
  λ: "fe" "g",
    let: "target" := !"fe" in
    mark_check_code "g" "target".
References

Uses

Memo.Adopt heap representation and code (Core.fs lines 4094, 4647–4651, 4683–4696)

Definition adopt_new_code : val :=
  λ: <>,
    let: "ad" := AllocN #3 #0 in
    "ad" <- #false;;
    ("ad" +ₗ #1) <- NONE;;
    ("ad" +ₗ #2) <- NONE;;
    "ad".
References

Uses

  • false
  • heap_lang.AllocN
  • heap_lang.val
  • NONE
Definition adopt_step_code : val :=
  λ: "ad" "c",
    if: !"ad" then
      ("ad" +ₗ #2) <- vlist_snoc_code !("ad" +ₗ #2) "c"
    else
      match: !("ad" +ₗ #1) with
        NONE => ("ad" +ₗ #1) <- SOME "c"
      | SOME "prev" =>
          if: "prev" = "c" then #()
          else
            ("ad" +ₗ #1) <- SOME "c";;
            ("ad" +ₗ #2) <- vlist_snoc_code !("ad" +ₗ #2) "prev"
      end.
References

Uses

Definition adopt_dispose_code : val :=
  λ: "ad",
    if: !"ad" then #()
    else
      "ad" <- #true;;
      match: !("ad" +ₗ #1) with
        NONE => #()
      | SOME "c" =>
          ("ad" +ₗ #1) <- NONE;;
          ("ad" +ₗ #2) <- vlist_snoc_code !("ad" +ₗ #2) "c"
      end.
References

Uses

Section heap_collections.
Context `{!heapGS Σ, !graphG Σ}.
References

Uses

1. Direct-observer edge boundary and ForwardingEdge

Heap representation of a ForwardingEdge (Core.fs lines 503–523): stores the downstream target node id and the current observer-set handle h in the upstream source's ObserverSet.

Definition is_forwarding_edge (fe : loc) (target : node_id) (h : nat) : iProp Σ :=
  fe ↦ #(Z.of_nat target) ∗
  (fe +ₗ 1) ↦ #(Z.of_nat h).
References

Uses

  • node_id
  • nat
  • heap_collections.Σ
  • Z.of_nat
  • iProp_solution.iProp
  • loc
✓ No axioms

forwarding_edge_moved_in_code updates the stored handle in is_forwarding_edge when ObserverSet swap-removes another entry into newHandle.

Arguments

fe : loc
the ForwardingEdge location.
target : nat
the target node id forwarded to.
slot : nat
the unused source-slot parameter (0 for ForwardingEdge).
h : nat
the previous observer-set handle.
h' : nat
the new observer-set handle after swap-remove.
Theorem forwarding_edge_moved_in_spec (fe : loc) (target slot h h' : nat) :
  {{{ is_forwarding_edge fe target h }}}
    forwarding_edge_moved_in_code #fe #(Z.of_nat slot) #(Z.of_nat h')
  {{{ RET #(); is_forwarding_edge fe target h' }}}.

Arguments

fe : loc
the ForwardingEdge location.
target : nat
the target node id forwarded to.
slot : nat
the unused source-slot parameter (0 for ForwardingEdge).
h : nat
the previous observer-set handle.
h' : nat
the new observer-set handle after swap-remove.
Qed
Proof.
  iIntros (Φ) "[H0 H1] HΦ".
  wp_lam. wp_pures. wp_store. iModIntro.
  iApply "HΦ". iFrame.
Qed.
References
✓ No axioms

forwarding_edge_mark_dirty_code and forwarding_edge_mark_check_code delegate MarkDirty and MarkCheck directly to the target node in is_graph.

Arguments

γ : iprop.gname
the ghost name.
g : lang.heap_lang.val
the graph value.
fe : loc
the ForwardingEdge location.
target : nat
the target node id (foldMemo or outerMemo).
h : nat
the observer-set handle.
fuel : nat
the marking fuel.
the graph state before marking.
Theorem forwarding_edge_mark_spec γ g (fe : loc) (target h fuel : nat) (s : State.state) :
  (forall s',
     mark_dirty fuel target s = Some s' ->
     {{{ is_forwarding_edge fe target h ∗ is_graph γ g s }}}
       forwarding_edge_mark_dirty_code #fe g
     {{{ RET #(); is_forwarding_edge fe target h ∗ is_graph γ g s' }}}) /\
  (forall s',
     mark_check fuel target s = Some s' ->
     {{{ is_forwarding_edge fe target h ∗ is_graph γ g s }}}
       forwarding_edge_mark_check_code #fe g
     {{{ RET #(); is_forwarding_edge fe target h ∗ is_graph γ g s' }}}).

Arguments

γ : iprop.gname
the ghost name.
g : lang.heap_lang.val
the graph value.
fe : loc
the ForwardingEdge location.
target : nat
the target node id (foldMemo or outerMemo).
h : nat
the observer-set handle.
fuel : nat
the marking fuel.
the graph state before marking.
Qed
Proof.
  split.
  - intros s' Hmd. iIntros (Φ) "[[H0 H1] Hg] HΦ".
    wp_lam. wp_pures. wp_load. wp_pures.
    wp_apply (mark_dirty_spec γ g fuel target s s' Hmd with "Hg").
    iIntros "Hg". iApply "HΦ". iFrame.
  - intros s' Hmc. iIntros (Φ) "[[H0 H1] Hg] HΦ".
    wp_lam. wp_pures. wp_load. wp_pures.
    wp_apply (mark_check_spec γ g fuel target s s' Hmc with "Hg").
    iIntros "Hg". iApply "HΦ". iFrame.
Qed.
References

3. Two-queue pass and effect scheduling

Definition is_two_queue (tq : loc) (st : two_queue_state) (ps es : list node_id) : iProp Σ :=
  ⌜list_to_set ps = tq_pass_set st /\ list_to_set es = tq_effect_set st⌝ ∗
  tq ↦ list_val (tq_passes st) ∗
  (tq +ₗ 1) ↦ #(Z.of_nat (tq_pass_head st)) ∗
  (tq +ₗ 2) ↦ list_val (tq_queue st) ∗
  (tq +ₗ 3) ↦ #(Z.of_nat (tq_queue_head st)) ∗
  (tq +ₗ 4) ↦ list_val ps ∗
  (tq +ₗ 5) ↦ list_val es.
References

Uses

7 more
  • list_val
  • list
  • heap_collections.Σ
  • Z.of_nat
  • iProp_solution.iProp
  • loc
  • list_to_set
✓ No axioms

schedule_pass_tq_code transitions is_two_queue along schedule_pass_tq and preserves tq_inv.

Arguments

tq : loc
the two-queue scheduler location.
the pass node id scheduled (SchedulePass).
the two-queue state before scheduling.
ps : list node_id
the list representation of tq_pass_set st.
es : list node_id
the list representation of tq_effect_set st.
Theorem schedule_pass_tq_spec (tq : loc) (n : node_id) (st : two_queue_state) (ps es : list node_id) :
  tq_inv st ->
  {{{ is_two_queue tq st ps es }}}
    schedule_pass_tq_code #tq #(Z.of_nat n)
  {{{ ps', RET #(); ⌜tq_inv (schedule_pass_tq n st)⌝ ∗ is_two_queue tq (schedule_pass_tq n st) ps' es }}}.

Arguments

tq : loc
the two-queue scheduler location.
the pass node id scheduled (SchedulePass).
the two-queue state before scheduling.
ps : list node_id
the list representation of tq_pass_set st.
es : list node_id
the list representation of tq_effect_set st.
Qed
Proof.
  intros Hinv.
  destruct (pass_queue_once n st Hinv) as [Hinv' _].
  iIntros (Φ) "([%Hps %Hes] & H0 & H1 & H2 & H3 & H4 & H5) HΦ".
  rewrite /schedule_pass_tq_code.
  wp_lam. wp_pures. wp_load. wp_pures.
  wp_apply (vlist_mem_nat_spec n ps); [done|]. iIntros "_".
  destruct (decide (n ∈ tq_pass_set st)) as [Hin|Hnin].
  - assert (Hin_ps : n ∈ ps) by set_solver.
    rewrite (bool_decide_eq_true_2 (n ∈ ps) Hin_ps). wp_pures. iModIntro.
    iApply ("HΦ" $! ps). iSplit; [done|].
    rewrite /schedule_pass_tq decide_True // /is_two_queue. iFrame. done.
  - assert (Hnin_ps : n ∉ ps) by set_solver.
    rewrite (bool_decide_eq_false_2 (n ∈ ps) Hnin_ps). wp_pures.
    wp_load. rewrite list_val_eq.
    wp_apply (vlist_snoc_spec (fun k : nat => #(Z.of_nat k)) (tq_passes st) n); [done|].
    iIntros "_". wp_store. wp_store. iModIntro.
    iApply ("HΦ" $! (n :: ps)). iSplit; [done|].
    rewrite /schedule_pass_tq decide_False // /is_two_queue /= (list_val_eq (_ ++ _)). iFrame.
    iPureIntro. split; [set_solver|done].
Qed.
References
✓ No axioms

Arguments

tq : loc
the two-queue scheduler location.
the effect node id scheduled (Schedule).
the two-queue state before scheduling.
ps : list node_id
the list representation of tq_pass_set st.
es : list node_id
the list representation of tq_effect_set st.
Theorem schedule_effect_tq_spec (tq : loc) (n : node_id) (st : two_queue_state) (ps es : list node_id) :
  tq_inv st ->
  {{{ is_two_queue tq st ps es }}}
    schedule_effect_tq_code #tq #(Z.of_nat n)
  {{{ es', RET #(); ⌜tq_inv (schedule_effect_tq n st)⌝ ∗ is_two_queue tq (schedule_effect_tq n st) ps es' }}}.

Arguments

tq : loc
the two-queue scheduler location.
the effect node id scheduled (Schedule).
the two-queue state before scheduling.
ps : list node_id
the list representation of tq_pass_set st.
es : list node_id
the list representation of tq_effect_set st.
Qed
Proof.
  intros Hinv.
  destruct (pass_queue_once n st Hinv) as [_ Hinv'].
  iIntros (Φ) "([%Hps %Hes] & H0 & H1 & H2 & H3 & H4 & H5) HΦ".
  rewrite /schedule_effect_tq_code.
  wp_lam. wp_pures. wp_load. wp_pures.
  wp_apply (vlist_mem_nat_spec n es); [done|]. iIntros "_".
  destruct (decide (n ∈ tq_effect_set st)) as [Hin|Hnin].
  - assert (Hin_es : n ∈ es) by set_solver.
    rewrite (bool_decide_eq_true_2 (n ∈ es) Hin_es). wp_pures. iModIntro.
    iApply ("HΦ" $! es). iSplit; [done|].
    rewrite /schedule_effect_tq decide_True // /is_two_queue. iFrame. done.
  - assert (Hnin_es : n ∉ es) by set_solver.
    rewrite (bool_decide_eq_false_2 (n ∈ es) Hnin_es). wp_pures.
    wp_load. rewrite (list_val_eq (tq_queue st)).
    wp_apply (vlist_snoc_spec (fun k : nat => #(Z.of_nat k)) (tq_queue st) n); [done|].
    iIntros "_". wp_store. wp_store. iModIntro.
    iApply ("HΦ" $! (n :: es)). iSplit; [done|].
    rewrite /schedule_effect_tq decide_False // /is_two_queue /= (list_val_eq (tq_queue st ++ [n])). iFrame.
    iPureIntro. split; [done|set_solver].
Qed.
References

4. Collection signal and row hooks

✓ No axioms

notify_dirty_except_code establishes the collection WriteExcept postcondition (ends_dirty s1 m) on the heap graph.

Combines notify_dirty_except_spec with write_signal_except_wakes_others: after updating signal n's value and ghost publication, running notify_dirty_except_code transitions is_graph to s1 where every observer m (with r <> Some m or n ∈ read_prefix (g_edges s) m) satisfies ends_dirty s1 m.

Arguments

γ : iprop.gname
the ghost name.
g : lang.heap_lang.val
the graph value.
fuel : nat
the marking fuel.
the signal node id written.
v : Z
the new value written.
r : option node_id
the excluded computation (Some running or None).
d : node
the signal node in s.
the state before the write.
the state after notify_dirty_except.
an observer of n that takes marks.
Theorem write_signal_except_spec γ g (fuel : nat) (n : node_id) (v : Z)
    (r : option node_id) (d : node) (s s1 : State.state) (m : node_id) :
  g_nodes s !! n = Some d ->
  n_kind d = KSignal ->
  m ∈ observer_view (observers_of (g_edges s) n) ->
  takes_marks s m ->
  (r ≠ Some m \/ n ∈ read_prefix (g_edges s) m) ->
  notify_dirty_except fuel n r (ghost_publish n (set_node n (node_with_value v d) s)) = Some s1 ->
  {{{ is_graph γ g (ghost_publish n (set_node n (node_with_value v d) s)) }}}
    notify_dirty_except_code g #(Z.of_nat n) (opt_nat_val r)
  {{{ RET #(); ⌜ends_dirty s1 m⌝ ∗ is_graph γ g s1 }}}.

Arguments

γ : iprop.gname
the ghost name.
g : lang.heap_lang.val
the graph value.
fuel : nat
the marking fuel.
the signal node id written.
v : Z
the new value written.
r : option node_id
the excluded computation (Some running or None).
d : node
the signal node in s.
the state before the write.
the state after notify_dirty_except.
an observer of n that takes marks.
Qed
Proof.
  intros Hd Hk Hin Ht Hcond Hnot.
  destruct (write_signal_except_wakes_others fuel n v r d s s1 m Hd Hk Hin Ht Hcond Hnot) as [Hdirty _].
  iIntros (Φ) "Hg HΦ".
  wp_apply (notify_dirty_except_spec γ g fuel n r _ s1 Hnot with "Hg").
  iIntros "Hg". iApply "HΦ". by iFrame.
Qed.
References
✓ No axioms

notify_dirty_code establishes the Memo.NotifyRemoved postcondition (ends_dirty s1 m) on the heap graph before a removed key's row memo is disposed.

Arguments

γ : iprop.gname
the ghost name.
g : lang.heap_lang.val
the graph value.
fuel : nat
the marking fuel.
the retiring row memo node id.
the state before notify_removed.
the state after notify_removed.
an observer of n that takes marks.
Theorem notify_removed_spec γ g (fuel : nat) (n : node_id) (s s1 : State.state) (m : node_id) :
  m ∈ observer_view (observers_of (g_edges s) n) ->
  takes_marks s m ->
  notify_removed fuel n s = Some (s1, tt) ->
  {{{ is_graph γ g s }}}
    notify_dirty_code g #(Z.of_nat n)
  {{{ RET #(); ⌜ends_dirty s1 m⌝ ∗ is_graph γ g s1 }}}.

Arguments

γ : iprop.gname
the ghost name.
g : lang.heap_lang.val
the graph value.
fuel : nat
the marking fuel.
the retiring row memo node id.
the state before notify_removed.
the state after notify_removed.
an observer of n that takes marks.
Qed
Proof.
  intros Hin Ht Hnr.
  pose proof (notify_removed_wakes_observers fuel n s s1 m Hin Ht Hnr) as Hdirty.
  unfold notify_removed, m_step in Hnr.
  destruct (notify_dirty fuel n s) as [s'|] eqn:E; [|done].
  injection Hnr as <-.
  iIntros (Φ) "Hg HΦ".
  wp_apply (notify_dirty_spec γ g fuel n s s' E with "Hg").
  iIntros "Hg". iApply "HΦ". by iFrame.
Qed.
References

4. Memo.Adopt companion resource lifecycle

Definition is_adopt (ad : loc) (st : adopt_state) : iProp Σ :=
  ad ↦ #(ad_disposed st) ∗
  (ad +ₗ 1) ↦ opt_nat_val (ad_current st) ∗
  (ad +ₗ 2) ↦ list_val (ad_disposed_log st).
References

Uses

1 more
  • loc
✓ No axioms

adopt_step_code transitions is_adopt ad st to is_adopt ad (memo_adopt_step c st).

Arguments

ad : loc
the adopt state location.
c : nat
the companion id passed to Memo.Adopt.
the adopt state before the call.
Theorem memo_adopt_spec (ad : loc) (c : nat) (st : adopt_state) :
  {{{ is_adopt ad st }}}
    adopt_step_code #ad #(Z.of_nat c)
  {{{ RET #(); is_adopt ad (memo_adopt_step c st) }}}.

Arguments

ad : loc
the adopt state location.
c : nat
the companion id passed to Memo.Adopt.
the adopt state before the call.
Qed
Proof.
  iIntros (Φ) "(H0 & H1 & H2) HΦ".
  rewrite /adopt_step_code /memo_adopt_step.
  wp_lam. wp_pures. wp_load.
  destruct (ad_disposed st) eqn:Ed; wp_pures.
  - wp_load. rewrite list_val_eq.
    wp_apply (vlist_snoc_spec (fun n : nat => #(Z.of_nat n)) (ad_disposed_log st) c); [done|].
    iIntros "_". wp_store. iModIntro. iApply "HΦ".
    rewrite /is_adopt /= list_val_eq. iFrame.
  - wp_load. destruct (ad_current st) as [prev|] eqn:Ec; simpl; wp_pures.
    + case_bool_decide as Heq; wp_pures.
      * assert (prev = c) as -> by (injection Heq; lia).
        rewrite decide_True //. iModIntro. iApply "HΦ".
        rewrite /is_adopt Ed Ec. iFrame.
      * assert (prev ≠ c) by (intros ->; done).
        rewrite decide_False //.
        wp_store. wp_load. rewrite list_val_eq.
        wp_apply (vlist_snoc_spec (fun n : nat => #(Z.of_nat n)) (ad_disposed_log st) prev); [done|].
        iIntros "_". wp_store. iModIntro. iApply "HΦ".
        rewrite /is_adopt /= list_val_eq. iFrame.
    + wp_store. iModIntro. iApply "HΦ". rewrite /is_adopt /=. iFrame.
Qed.
References
✓ No axioms

adopt_dispose_code transitions is_adopt ad st to is_adopt ad (memo_dispose_adopted st) and establishes adopt_companion_disposed_once when st = adopt_many cs empty_adopt_state.

Arguments

ad : loc
the adopt state location.
the adopt state before Memo.Dispose.
Theorem memo_dispose_adopted_spec (ad : loc) (st : adopt_state) :
  {{{ is_adopt ad st }}}
    adopt_dispose_code #ad
  {{{ RET #(); is_adopt ad (memo_dispose_adopted st) }}}.

Arguments

ad : loc
the adopt state location.
the adopt state before Memo.Dispose.
Qed
Proof.
  iIntros (Φ) "(H0 & H1 & H2) HΦ".
  rewrite /adopt_dispose_code /memo_dispose_adopted.
  wp_lam. wp_pures. wp_load.
  destruct (ad_disposed st) eqn:Ed; wp_pures.
  - iModIntro. iApply "HΦ". rewrite /is_adopt Ed. iFrame.
  - wp_store. wp_load.
    destruct (ad_current st) as [c|] eqn:Ec; simpl; wp_pures.
    + wp_store. wp_load. rewrite list_val_eq.
      wp_apply (vlist_snoc_spec (fun n : nat => #(Z.of_nat n)) (ad_disposed_log st) c); [done|].
      iIntros "_". wp_store. iModIntro. iApply "HΦ".
      rewrite /is_adopt /= list_val_eq. iFrame.
    + iModIntro. iApply "HΦ". rewrite /is_adopt /=. iFrame.
Qed.
References

1-Based Fenwick Prefix Sums and Point Updates

Definition fenwick_sum_loop : val :=
  rec: "loop" "arr" "i" "acc" :=
    if: #0 < "i" then
      let: "v" := !("arr" +ₗ "i") in
      let: "lsb" := BinOp AndOp "i" (- "i") in
      "loop" "arr" ("i" - "lsb") ("acc" + "v")
    else "acc".
References

Uses

  • heap_lang.AndOp
  • heap_lang.BinOp
  • heap_lang.val
Definition fenwick_sum_code : val :=
  λ: "arr" "i", fenwick_sum_loop "arr" "i" #0.
References

Uses

Lemma fenwick_sum_loop_spec (arr : loc) (dq : dfrac) (tree : list Z) (i : nat) (acc : Z) :
  (i < length tree)%nat ->
  {{{ arr ↦∗{dq} ((fun z : Z => #z) <$> tree) }}}
    fenwick_sum_loop #arr #(Z.of_nat i) #acc
  {{{ RET #(fenwick_sum_go i tree i acc); arr ↦∗{dq} ((fun z : Z => #z) <$> tree) }}}.
Qed
Proof.
  revert acc. induction i as [i IH] using lt_wf_ind.
  intros acc Hlen Φ. iIntros "Hpre HΦ".
  wp_rec. wp_pures.
  destruct (decide (1 <= i)%nat) as [Hi|Hni].
  - rewrite bool_decide_eq_true_2; [|lia]. wp_pures.
    destruct (lookup_lt_is_Some_2 tree i Hlen) as [v Hv].
    wp_apply (wp_load_offset with "Hpre"); [by rewrite list_lookup_fmap Hv|].
    iIntros "Hpre". wp_pures.
    rewrite (z_land_opp_nat i Hi).
    destruct (nat_lsb_bounds i Hi) as [Hpos Hle].
    replace (Z.of_nat i - Z.of_nat (nat_lsb i))%Z with (Z.of_nat (i - nat_lsb i)) by lia.
    wp_apply (IH (i - nat_lsb i) ltac:(lia) (acc + v)%Z ltac:(lia) with "Hpre").
    iIntros "Hpre".
    destruct i as [|i']; [lia|]. cbn [fenwick_sum_go].
    destruct (decide (1 <= S i')%nat) as [_|]; [|lia].
    rewrite Hv /=.
    erewrite fenwick_sum_go_fuel_irrel; [iApply "HΦ"; iExact "Hpre" | lia | lia].
  - assert (i = 0)%nat as -> by lia.
    rewrite bool_decide_eq_false_2; [|lia]. wp_pures.
    cbn [fenwick_sum_go]. iApply "HΦ". iExact "Hpre".
Qed.
References

Uses

13 more
  • list
  • nat
  • Z
  • lt_wf_ind
  • Z.of_nat
  • dfrac
  • wp_load_offset
  • loc
  • decide
  • bool_decide_eq_false_2
  • bool_decide_eq_true_2
  • list.lookup_lt_is_Some_2
  • list.list_lookup_fmap
✓ No axioms

1-based HeapLang Fenwick prefix sum matches pure fenwick_sum_pure.

Walking down i via two's-complement step i - (i &&& -i) over the fractional heap array arr ↦∗{dq} tree returns fenwick_sum_pure tree i without mutating the Fenwick tree.

Arguments

arr : loc
base heap location of the 1-based Fenwick array.
dq : dfrac
fractional ownership held during the read-only prefix query.
tree : list Z
pure integer model of the Fenwick array entries.
i : nat
1-based prefix index to accumulate (0 for the empty prefix).
Theorem fenwick_sum_spec (arr : loc) (dq : dfrac) (tree : list Z) (i : nat) :
  (i < length tree)%nat ->
  {{{ arr ↦∗{dq} ((fun z : Z => #z) <$> tree) }}}
    fenwick_sum_code #arr #(Z.of_nat i)
  {{{ RET #(fenwick_sum_pure tree i); arr ↦∗{dq} ((fun z : Z => #z) <$> tree) }}}.

Arguments

arr : loc
base heap location of the 1-based Fenwick array.
dq : dfrac
fractional ownership held during the read-only prefix query.
tree : list Z
pure integer model of the Fenwick array entries.
i : nat
1-based prefix index to accumulate (0 for the empty prefix).
Qed
Proof.
  intros Hlen Φ. iIntros "Hpre HΦ".
  wp_lam. wp_pures.
  wp_apply (fenwick_sum_loop_spec with "Hpre"); [exact Hlen|].
  iExact "HΦ".
Qed.
References

Uses

2 more
  • dfrac
  • loc
Definition fenwick_add_loop : val :=
  rec: "loop" "arr" "n" "i" "delta" :=
    if: "i" ≤ "n" then
      let: "v" := !("arr" +ₗ "i") in
      ("arr" +ₗ "i") <- ("v" + "delta") ;;
      let: "lsb" := BinOp AndOp "i" (- "i") in
      "loop" "arr" "n" ("i" + "lsb") "delta"
    else #().
References

Uses

  • heap_lang.AndOp
  • heap_lang.BinOp
  • heap_lang.val
Definition fenwick_add_code : val :=
  λ: "arr" "n" "i" "delta", fenwick_add_loop "arr" "n" "i" "delta".
References

Uses

Lemma fenwick_add_loop_spec (arr : loc) (tree : list Z) (n i : nat) (delta : Z) :
  (1 <= i)%nat -> (S n <= length tree)%nat ->
  {{{ arr ↦∗ ((fun z : Z => #z) <$> tree) }}}
    fenwick_add_loop #arr #(Z.of_nat n) #(Z.of_nat i) #delta
  {{{ RET #(); arr ↦∗ ((fun z : Z => #z) <$> fenwick_add_pure tree n i delta) }}}.
Qed
Proof.
  remember (S n - i)%nat as rem eqn:Hrem. revert tree i Hrem.
  induction rem as [rem IH] using lt_wf_ind.
  intros tree i Hrem Hi Hlen Φ. iIntros "Hpre HΦ".
  wp_rec. wp_pures.
  destruct (decide (i <= n)%nat) as [Hle|Hgt].
  - rewrite bool_decide_eq_true_2; [|lia]. wp_pures.
    assert (Hil : (i < length tree)%nat) by lia.
    destruct (lookup_lt_is_Some_2 tree i Hil) as [v Hv].
    wp_apply (wp_load_offset with "Hpre"); [by rewrite list_lookup_fmap Hv|].
    iIntros "Hpre". wp_pures.
    wp_apply (wp_store_offset with "Hpre"); [rewrite list_lookup_fmap Hv; eauto|].
    iIntros "Hpre". rewrite -list_fmap_insert. wp_pures.
    rewrite (z_land_opp_nat i Hi).
    destruct (nat_lsb_bounds i Hi) as [Hpos _].
    replace (Z.of_nat i + Z.of_nat (nat_lsb i))%Z with (Z.of_nat (i + nat_lsb i)) by lia.
    set (tree' := <[i := (v + delta)%Z]> tree).
    assert (Hlen' : (S n <= length tree')%nat) by (subst tree'; rewrite length_insert; lia).
    wp_apply (IH (S n - (i + nat_lsb i))%nat ltac:(lia) tree' (i + nat_lsb i)%nat eq_refl ltac:(lia) Hlen' with "Hpre").
    iIntros "Hpre". unfold fenwick_add_pure.
    destruct (S n - i)%nat as [|f'] eqn:Hf; [lia|]. cbn [fenwick_add_go].
    destruct (decide (1 <= i /\ i <= n)%nat) as [_|]; [|lia].
    rewrite Hv. fold tree'.
    erewrite (fenwick_add_go_fuel_irrel (S n - (i + nat_lsb i)) f');
      [iApply "HΦ"; iExact "Hpre" | lia | lia | lia].
  - rewrite bool_decide_eq_false_2; [|lia]. wp_pures.
    unfold fenwick_add_pure. replace (S n - i)%nat with 0%nat by lia.
    cbn [fenwick_add_go]. iApply "HΦ". iExact "Hpre".
Qed.
References

Uses

17 more
  • length
  • list
  • nat
  • eq_refl
  • Z
  • lt_wf_ind
  • Z.of_nat
  • wp_load_offset
  • wp_store_offset
  • loc
  • decide
  • bool_decide_eq_false_2
  • bool_decide_eq_true_2
  • list.length_insert
  • list.lookup_lt_is_Some_2
  • list.list_fmap_insert
  • list.list_lookup_fmap
✓ No axioms

1-based HeapLang Fenwick point update matches pure fenwick_add_pure.

Climbing i via i + (i &&& -i) up to n adds delta at every covered Fenwick bucket in arr and leaves arr holding fenwick_add_pure tree n i delta.

Arguments

arr : loc
base heap location of the 1-based Fenwick array.
tree : list Z
initial integer model of the Fenwick array entries.
n : nat
logical 1-based capacity upper bound (S n <= length tree).
i : nat
starting 1-based index being updated (1 <= i).
delta : Z
integer delta (+1 or -1) added along the Fenwick ancestor chain.
Theorem fenwick_add_spec (arr : loc) (tree : list Z) (n i : nat) (delta : Z) :
  (1 <= i)%nat -> (S n <= length tree)%nat ->
  {{{ arr ↦∗ ((fun z : Z => #z) <$> tree) }}}
    fenwick_add_code #arr #(Z.of_nat n) #(Z.of_nat i) #delta
  {{{ RET #(); arr ↦∗ ((fun z : Z => #z) <$> fenwick_add_pure tree n i delta) }}}.

Arguments

arr : loc
base heap location of the 1-based Fenwick array.
tree : list Z
initial integer model of the Fenwick array entries.
n : nat
logical 1-based capacity upper bound (S n <= length tree).
i : nat
starting 1-based index being updated (1 <= i).
delta : Z
integer delta (+1 or -1) added along the Fenwick ancestor chain.
Qed
Proof.
  intros Hi Hlen Φ. iIntros "Hpre HΦ".
  wp_lam. wp_pures.
  wp_apply (fenwick_add_loop_spec with "Hpre"); [exact Hi|exact Hlen|].
  iExact "HΦ".
Qed.
References

Uses

2 more
  • Z.of_nat
  • loc

7. Incremental `SortView` inverse-order array shift (Combinators.fs)

Definition inv_order_shift_loop : val :=
  rec: "loop" "order_arr" "pos_arr" "k" "new_pos" :=
    if: "new_pos" < "k" then
      let: "km1" := "k" - #1 in
      let: "moved" := !("order_arr" +ₗ "km1") in
      ("order_arr" +ₗ "k") <- "moved" ;;
      ("pos_arr" +ₗ "moved") <- "k" ;;
      "loop" "order_arr" "pos_arr" "km1" "new_pos"
    else #().
References

Uses

  • heap_lang.val
Definition inv_order_shift_code : val :=
  λ: "order_arr" "pos_arr" "old_pos" "new_pos" "p_idx",
    inv_order_shift_loop "order_arr" "pos_arr" "old_pos" "new_pos" ;;
    ("order_arr" +ₗ "new_pos") <- "p_idx" ;;
    ("pos_arr" +ₗ "p_idx") <- "new_pos".
References

Uses

Lemma inv_order_shift_loop_spec
  (order_arr pos_arr : loc) (k new_pos free_p : nat) (order order_pos : list nat) :
  (new_pos <= k)%nat ->
  inv_order_hole k free_p order order_pos ->
  {{{ order_arr ↦∗ ((fun n : nat => #(Z.of_nat n)) <$> order) ∗
      pos_arr ↦∗ ((fun n : nat => #(Z.of_nat n)) <$> order_pos) }}}
    inv_order_shift_loop #order_arr #pos_arr #(Z.of_nat k) #(Z.of_nat new_pos)
  {{{ RET #();
      let '(order1, order_pos1) := shift_right_steps (k - new_pos)%nat k order order_pos in
      order_arr ↦∗ ((fun n : nat => #(Z.of_nat n)) <$> order1) ∗
      pos_arr ↦∗ ((fun n : nat => #(Z.of_nat n)) <$> order_pos1) ∗
      ⌜inv_order_hole new_pos free_p order1 order_pos1⌝ }}}.
Qed
Proof.
  remember (k - new_pos)%nat as rem eqn:Hrem.
  revert k order order_pos Hrem.
  induction rem as [|rem' IH]; intros k order order_pos Hrem Hle Hinv Φ; iIntros "[Ho Hp] HΦ".
  - wp_rec. wp_pures.
    rewrite bool_decide_eq_false_2; [|lia]. wp_pures.
    assert (Hk : k = new_pos) by lia. subst k.
    cbn [shift_right_steps]. iApply "HΦ". iFrame. iPureIntro. exact Hinv.
  - wp_rec. wp_pures.
    rewrite bool_decide_eq_true_2; [|lia]. wp_pures.
    replace (Z.of_nat k - 1)%Z with (Z.of_nat (k - 1)) by lia.
    destruct Hinv as (Hlen & Hk_lt & Hfree_lt & Hfw & Hbw).
    assert (Hkm1_lt : (k - 1 < length order)%nat) by lia.
    destruct (lookup_lt_is_Some_2 order (k - 1)%nat Hkm1_lt) as [moved Hmoved].
    wp_apply (wp_load_offset with "Ho"); [by rewrite list_lookup_fmap Hmoved|].
    iIntros "Ho". wp_pures.
    wp_apply (wp_store_offset with "Ho"); [apply lookup_lt_is_Some_2; rewrite length_fmap; lia|].
    iIntros "Ho". rewrite -list_fmap_insert. wp_pures.
    pose proof (Hfw (k - 1)%nat moved ltac:(lia) Hmoved) as [_ Hmoved_pos].
    wp_apply (wp_store_offset with "Hp"); [rewrite list_lookup_fmap Hmoved_pos; eauto|].
    iIntros "Hp". rewrite -list_fmap_insert. wp_pures.
    pose proof (inv_order_hole_step k free_p moved order order_pos ltac:(lia) (conj Hlen (conj Hk_lt (conj Hfree_lt (conj Hfw Hbw)))) Hmoved) as Hstep.
    wp_apply (IH (k - 1)%nat (<[k := moved]> order) (<[moved := k]> order_pos) ltac:(lia) ltac:(lia) Hstep with "[$Ho $Hp]").
    destruct (shift_right_steps rem' (k - 1)%nat (<[k := moved]> order) (<[moved := k]> order_pos)) as [o1 op1] eqn:Hsr1.
    iIntros "(Ho & Hp & %Hinv1)".
    cbn [shift_right_steps]. rewrite Hmoved Hsr1.
    iApply "HΦ". iFrame. iPureIntro. exact Hinv1.
Qed.
References

Uses

10 more
  • Z.of_nat
  • wp_load_offset
  • wp_store_offset
  • loc
  • bool_decide_eq_false_2
  • bool_decide_eq_true_2
  • list.lookup_lt_is_Some_2
  • list.length_fmap
  • list.list_fmap_insert
  • list.list_lookup_fmap
✓ No axioms

HeapLang inv_order_shift_code refines reinsert_left_pure and preserves inv_order.

Executes SortView's leftward reinsertion right-shift loop across heap arrays order_arr and pos_arr, then writes p_idx and new_pos, leaving both heap arrays equal to reinsert_left_pure with inv_order restored.

Arguments

order_arr : loc
base heap location of the output-position order array.
pos_arr : loc
base heap location of the inverse orderPos array.
order : list nat
pure model of order before reinsertion.
order_pos : list nat
pure model of orderPos before reinsertion.
old_pos : nat
prior output position holding p_idx.
new_pos : nat
target output position (new_pos <= old_pos).
p_idx : nat
placed-slot index moved leftward from old_pos to new_pos.
Theorem inv_order_shift_spec
  (order_arr pos_arr : loc) (order order_pos : list nat) (old_pos new_pos p_idx : nat) :
  inv_order order order_pos ->
  (new_pos <= old_pos)%nat ->
  order !! old_pos = Some p_idx ->
  {{{ order_arr ↦∗ ((fun n : nat => #(Z.of_nat n)) <$> order) ∗
      pos_arr ↦∗ ((fun n : nat => #(Z.of_nat n)) <$> order_pos) }}}
    inv_order_shift_code #order_arr #pos_arr #(Z.of_nat old_pos) #(Z.of_nat new_pos) #(Z.of_nat p_idx)
  {{{ RET #();
      let '(order', order_pos') := reinsert_left_pure old_pos new_pos p_idx order order_pos in
      order_arr ↦∗ ((fun n : nat => #(Z.of_nat n)) <$> order') ∗
      pos_arr ↦∗ ((fun n : nat => #(Z.of_nat n)) <$> order_pos') ∗
      ⌜inv_order order' order_pos'⌝ }}}.

Arguments

order_arr : loc
base heap location of the output-position order array.
pos_arr : loc
base heap location of the inverse orderPos array.
order : list nat
pure model of order before reinsertion.
order_pos : list nat
pure model of orderPos before reinsertion.
old_pos : nat
prior output position holding p_idx.
new_pos : nat
target output position (new_pos <= old_pos).
p_idx : nat
placed-slot index moved leftward from old_pos to new_pos.
Qed
Proof.
  intros Hinv Hle Hold Φ. iIntros "[Ho Hp] HΦ".
  wp_lam. wp_pures.
  wp_apply (inv_order_shift_loop_spec order_arr pos_arr old_pos new_pos p_idx order order_pos Hle (inv_order_to_hole order order_pos old_pos p_idx Hinv Hold) with "[$Ho $Hp]").
  destruct (shift_right_steps (old_pos - new_pos)%nat old_pos order order_pos) as [order1 order_pos1] eqn:Hsr.
  iIntros "(Ho & Hp & %Hhole)". wp_pures.
  destruct (inv_order_close_hole new_pos p_idx order1 order_pos1 Hhole) as [Hinv' _].
  destruct Hhole as (_ & Hnp_lt & Hp_lt & _).
  wp_apply (wp_store_offset with "Ho"); [apply lookup_lt_is_Some_2; rewrite length_fmap; lia|].
  iIntros "Ho". rewrite -list_fmap_insert. wp_pures.
  wp_apply (wp_store_offset with "Hp"); [apply lookup_lt_is_Some_2; rewrite length_fmap; lia|].
  iIntros "Hp". rewrite -list_fmap_insert.
  unfold reinsert_left_pure. rewrite Hsr.
  iApply "HΦ". iFrame. iPureIntro. exact Hinv'.
Qed.
References

Uses

8 more
  • list
  • nat
  • Z.of_nat
  • wp_store_offset
  • loc
  • list.lookup_lt_is_Some_2
  • list.length_fmap
  • list.list_fmap_insert
End heap_collections.