Ranvier.Heap.Collections
Heap collection-boundary primitives
This module lifts the four collection-boundary areas of Pure/Collections.v to HeapLang separation logic:
- Direct-observer edge attachment (
world_direct_link_spec), detachment (world_direct_unlink_spec), andForwardingEdge(forwarding_edge_moved_in_spec,forwarding_edge_mark_spec). - Active
OwnerLinkunlinking (owner_links_active_tail_unlink_specandowner_links_active_mid_unlink_spec) for per-key child scopes and reparenting. - Two-queue pass and effect scheduling (
schedule_pass_tq_specandschedule_effect_tq_spec). - Collection signal and row hooks (
write_signal_except_specandnotify_removed_spec), plusMemo.Adoptcompanion resource lifecycle (memo_adopt_specandmemo_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 :=
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").Definition schedule_effect_tq_code : val :=
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").ForwardingEdge heap representation and code (Core.fs lines 503–523)
Definition forwarding_edge_new_code : val :=
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 :=
Definition forwarding_edge_moved_in_code : val :=
λ: "fe" "_sourceSlot" "newHandle",
("fe" +ₗ #1) <- "newHandle".Definition forwarding_edge_mark_dirty_code : val :=
Definition forwarding_edge_mark_dirty_code : val :=
λ: "fe" "g",
let: "target" := !"fe" in
mark_dirty_code "g" "target".Definition forwarding_edge_mark_check_code : val :=
Definition forwarding_edge_mark_check_code : val :=
λ: "fe" "g",
let: "target" := !"fe" in
mark_check_code "g" "target".Memo.Adopt heap representation and code (Core.fs lines 4094, 4647–4651, 4683–4696)
Definition adopt_new_code : val :=
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 :=
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.Definition adopt_dispose_code : val :=
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.Section heap_collections.
References
Uses
- graphG
- Σ:1
- heapGS
1. Direct-observer edge boundary and ForwardingEdge
Direct-observer edge registration (sources_add_code) transitions is_world tbl w to is_world tbl (direct_link self s w) and preserves world_well_formed.
When a direct observer (Binding, FoldReads, FoldRow, ValueRow, or ForwardingEdge) with
source_cursor = length source_slots attaches to a distinct upstream source s, sources_add_code
updates the heap edge world to direct_link self s w while preserving world_well_formed.
Arguments
Theorem world_direct_link_spec (res : val) tbl (self s : node_id) (w : EdgeWorld) :
Theorem world_direct_link_spec (res : val) tbl (self s : node_id) (w : EdgeWorld) :
world_well_formed w ->
self ∈ dom tbl ->
s ∈ dom tbl ->
source_cursor (sources_of w self) = length (source_slots (sources_of w self)) ->
s ∉ fst <$> source_slots (sources_of w self) ->
{{{ resolver tbl res ∗ is_world tbl w }}}
sources_add_code res #(Z.of_nat self) #(Z.of_nat s)
{{{ RET #(); ⌜world_well_formed (direct_link self s w)⌝ ∗ is_world tbl (direct_link self s w) }}}.
Arguments
Qed
Proof.
intros Hwf Hself Hs Hcur Hnd.
destruct (direct_link_is_sources_add self s w Hwf Hcur Hnd) as [Hadd _].
destruct (direct_link_preserves_well_formed self s w Hwf Hnd) as (Hwf' & _).
iIntros (Φ) "Hpre HΦ".
wp_apply (sources_add_spec res tbl self s w (direct_link self s w) (proj1 Hwf) Hself Hs Hadd with "Hpre").
iIntros "[_ Hw]". iApply "HΦ". by iFrame.
Qed.References
Uses
- node_id
- source_slots
- source_cursor
- EdgeWorld
- sources_of
- world_well_formed
- direct_link
- direct_link_preserves_well_formed
11 more
- direct_link_is_sources_add
- sources_add_code
- resolver
- is_world
- sources_add_spec
- fst
- length
- proj1
- Z.of_nat
- heap_lang.val
- dom
Used by
Direct-observer detachment (sources_clear_code) transitions is_world tbl w to is_world tbl w' and preserves world_well_formed.
When a direct observer (Binding, FoldReads, FoldRow, or ValueRow) is disposed, sources_clear_code
removes every upstream edge, preserves world_well_formed, and leaves source_slots (sources_of w' self) = [].
Arguments
sources_clear.Theorem world_direct_unlink_spec (res : val) tbl (self : node_id) (w w' : EdgeWorld) :
Theorem world_direct_unlink_spec (res : val) tbl (self : node_id) (w w' : EdgeWorld) :
world_well_formed w ->
self ∈ dom tbl ->
sources_clear self w = Some w' ->
{{{ resolver tbl res ∗ is_world tbl w }}}
sources_clear_code res #(Z.of_nat self)
{{{ RET #();
⌜world_well_formed w' /\ source_slots (sources_of w' self) = [] /\
(forall src, self ∉ observer_view (observers_of w' src))⌝ ∗
is_world tbl w' }}}.
Arguments
sources_clear.Qed
Proof.
intros Hwf Hself Hclr.
destruct (direct_unlink_preserves_well_formed self w Hwf) as (w'' & Hclr' & Hwf' & Hempty & Hno_obs & _).
rewrite Hclr in Hclr'. injection Hclr' as <-.
iIntros (Φ) "Hpre HΦ".
wp_apply (sources_clear_spec res tbl self w w' (proj1 Hwf) Hself Hclr with "Hpre").
iIntros "[_ Hw]". iApply "HΦ". iFrame. iPureIntro. done.
Qed.References
Uses
10 more
- direct_unlink_preserves_well_formed
- sources_clear_code
- resolver
- is_world
- sources_clear_spec
- Some
- proj1
- Z.of_nat
- heap_lang.val
- dom
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.
References
Uses
- node_id
- nat
- heap_collections.Σ
- Z.of_nat
- iProp_solution.iProp
- loc
forwarding_edge_moved_in_code updates the stored handle in is_forwarding_edge when ObserverSet swap-removes another entry into newHandle.
Arguments
ForwardingEdge location.0 for ForwardingEdge).Theorem forwarding_edge_moved_in_spec (fe : loc) (target slot h h' : nat) :
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
ForwardingEdge location.0 for ForwardingEdge).Qed
Proof.
iIntros (Φ) "[H0 H1] HΦ".
wp_lam. wp_pures. wp_store. iModIntro.
iApply "HΦ". iFrame.
Qed.forwarding_edge_mark_dirty_code and forwarding_edge_mark_check_code delegate MarkDirty and MarkCheck directly to the target node in is_graph.
Arguments
ForwardingEdge location.foldMemo or outerMemo).Theorem forwarding_edge_mark_spec γ g (fe : loc) (target h fuel : nat) (s : State.state) :
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
ForwardingEdge location.foldMemo or outerMemo).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.2. Active OwnerLink unlinking for per-key child scopes
Definition link_detached (l : link) : link :=
Definition link_detached (l : link) : link :=
{| l_child := l_child l; l_owner := l_owner l; l_generation := None; l_acquired := l_acquired l |}.owner_links_unlink_code detaches an active tail OwnerLink (l_generation l = Some gen) in O(1) time.
Covers both the single-child case (entries = [(lk, l)]) and the multi-child tail case
(entries = E0 ++ [(lk_prev, l_prev); (lk, l)]), updating ol's head/tail pointers and clearing lk's
prev, next, and generation fields.
Arguments
Theorem owner_links_active_tail_unlink_spec (ol : loc) (o : owner_id) (lk : loc) (l : link) (gen acq : nat) :
Theorem owner_links_active_tail_unlink_spec (ol : loc) (o : owner_id) (lk : loc) (l : link) (gen acq : nat) :
l_generation l = Some gen ->
{{{ is_owner_links ol o [(lk, l)] gen acq }}}
owner_links_unlink_code #ol #lk
{{{ RET #(); is_owner_links ol o [] gen acq ∗ is_link_node lk None None (link_detached l) }}}.
Arguments
Qed
Proof.
intros Hgen.
iIntros (Φ) "(Hol0 & Hol1 & Hol2 & Hol3 & [(Hlk0 & Hlk1 & Hlk2 & Hlk3 & Hlk4) _]) HΦ".
rewrite /owner_links_unlink_code Hgen /=.
wp_lam. wp_pures. wp_load. wp_pures. wp_load. wp_pures.
rewrite bool_decide_eq_true_2 //. wp_pures.
wp_store. wp_load. wp_load. wp_pures.
wp_store. wp_store. wp_store. wp_store.
iModIntro. iApply "HΦ".
rewrite /is_owner_links /is_link_node /link_detached /=.
iFrame.
Qed.References
Uses
4 more
- Some
- nat
- loc
- bool_decide_eq_true_2
Used by
owner_links_unlink_code splices an active interior OwnerLink (l_generation l = Some gen) out of a doubly-linked chain in O(1) time.
When a projection key or reparented child scope is unlinked from the middle of a live parent's OwnerLink
list (prev = Some lk_prev, next = Some lk_next), owner_links_unlink_code redirects lk_prev.next to
Some lk_next and lk_next.prev to Some lk_prev, and clears lk's prev, next, and generation
fields without traversing the rest of the chain.
Arguments
Owner doubly-linked list header location.lk_prev's predecessor pointer.lk_next's successor pointer.ol.Theorem owner_links_active_mid_unlink_spec (ol lk_prev lk lk_next : loc)
Theorem owner_links_active_mid_unlink_spec (ol lk_prev lk lk_next : loc)
(prev_p next_n : option loc) (l_prev l l_next : link) (gen : nat) :
l_generation l = Some gen ->
{{{ (ol +ₗ 2) ↦ #(Z.of_nat gen) ∗
is_link_node lk_prev prev_p (Some lk) l_prev ∗
is_link_node lk (Some lk_prev) (Some lk_next) l ∗
is_link_node lk_next (Some lk) next_n l_next }}}
owner_links_unlink_code #ol #lk
{{{ RET #();
(ol +ₗ 2) ↦ #(Z.of_nat gen) ∗
is_link_node lk_prev prev_p (Some lk_next) l_prev ∗
is_link_node lk_next (Some lk_prev) next_n l_next ∗
is_link_node lk None None (link_detached l) }}}.
Arguments
Owner doubly-linked list header location.lk_prev's predecessor pointer.lk_next's successor pointer.ol.Qed
Proof.
intros Hgen.
iIntros (Φ) "(Hol2 & (Hp0 & Hp1 & Hp2 & Hp3 & Hp4) & (Hlk0 & Hlk1 & Hlk2 & Hlk3 & Hlk4) & (Hn0 & Hn1 & Hn2 & Hn3 & Hn4)) HΦ".
rewrite /owner_links_unlink_code Hgen /=.
wp_lam. wp_pures. wp_load. wp_pures. wp_load. wp_pures.
rewrite bool_decide_eq_true_2 //. wp_pures.
wp_store. wp_load. wp_load. wp_pures.
wp_store. wp_store. wp_store. wp_store.
iModIntro. iApply "HΦ".
rewrite /is_link_node /link_detached /=.
iFrame.
Qed.References
Uses
4 more
- option
- Z.of_nat
- loc
- bool_decide_eq_true_2
3. Two-queue pass and effect scheduling
Definition is_two_queue (tq : loc) (st : two_queue_state) (ps es : list node_id) : iProp Σ :=
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
schedule_pass_tq_code transitions is_two_queue along schedule_pass_tq and preserves tq_inv.
Arguments
SchedulePass).tq_pass_set st.tq_effect_set st.Theorem schedule_pass_tq_spec (tq : loc) (n : node_id) (st : two_queue_state) (ps es : list node_id) :
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
SchedulePass).tq_pass_set st.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
Uses
13 more
- vlist_snoc_spec
- vlist_mem_nat_spec
- schedule_pass_tq_code
- is_two_queue
- list
- nat
- Z.of_nat
- loc
- decide
- bool_decide_eq_false_2
- bool_decide_eq_true_2
- decide_False
- decide_True
Used by
schedule_effect_tq_code transitions is_two_queue along schedule_effect_tq and preserves tq_inv.
Arguments
Schedule).tq_pass_set st.tq_effect_set st.Theorem schedule_effect_tq_spec (tq : loc) (n : node_id) (st : two_queue_state) (ps es : list node_id) :
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
Schedule).tq_pass_set st.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
Uses
13 more
- vlist_snoc_spec
- vlist_mem_nat_spec
- schedule_effect_tq_code
- is_two_queue
- list
- nat
- Z.of_nat
- loc
- decide
- bool_decide_eq_false_2
- bool_decide_eq_true_2
- decide_False
- decide_True
Used by
4. Collection signal and row hooks
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
Some running or None).notify_dirty_except.n that takes marks.Theorem write_signal_except_spec γ g (fuel : nat) (n : node_id) (v : Z)
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
Some running or None).notify_dirty_except.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
Uses
Used by
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
notify_removed.notify_removed.n that takes marks.Theorem notify_removed_spec γ g (fuel : nat) (n : node_id) (s s1 : State.state) (m : node_id) :
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
notify_removed.notify_removed.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.4. Memo.Adopt companion resource lifecycle
Definition is_adopt (ad : loc) (st : adopt_state) : iProp Σ :=
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
- adopt_state
- ad_disposed
- ad_current
- ad_disposed_log
- list_val
- opt_nat_val
- heap_collections.Σ
- iProp_solution.iProp
1 more
- loc
adopt_step_code transitions is_adopt ad st to is_adopt ad (memo_adopt_step c st).
Arguments
Memo.Adopt.Theorem memo_adopt_spec (ad : loc) (c : nat) (st : adopt_state) :
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
Memo.Adopt.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
Uses
- adopt_state
- ad_disposed
- ad_current
- ad_disposed_log
- memo_adopt_step
- list_val_eq
- vlist_snoc_spec
- adopt_step_code
6 more
- is_adopt
- nat
- Z.of_nat
- loc
- decide_False
- decide_True
Used by
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
Memo.Dispose.Theorem memo_dispose_adopted_spec (ad : loc) (st : adopt_state) :
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
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
Uses
- adopt_state
- ad_disposed
- ad_current
- ad_disposed_log
- memo_dispose_adopted
- list_val_eq
- vlist_snoc_spec
- adopt_dispose_code
4 more
- is_adopt
- nat
- Z.of_nat
- loc
Used by
1-Based Fenwick Prefix Sums and Point Updates
Definition fenwick_sum_loop : val :=
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 :=
Definition fenwick_sum_code : val :=
λ: "arr" "i", fenwick_sum_loop "arr" "i" #0.Lemma fenwick_sum_loop_spec (arr : loc) (dq : dfrac) (tree : list Z) (i : nat) (acc : Z) :
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
Used by
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
0 for the empty prefix).Theorem fenwick_sum_spec (arr : loc) (dq : dfrac) (tree : list Z) (i : nat) :
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
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
Used by
Definition fenwick_add_loop : val :=
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 :=
Definition fenwick_add_code : val :=
λ: "arr" "n" "i" "delta", fenwick_add_loop "arr" "n" "i" "delta".Lemma fenwick_add_loop_spec (arr : loc) (tree : list Z) (n i : nat) (delta : Z) :
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
- nat_lsb
- nat_lsb_bounds
- z_land_opp_nat
- fenwick_add_go
- fenwick_add_pure
- fenwick_add_go_fuel_irrel
- fenwick_add_loop
- S
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
Used by
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
S n <= length tree).1 <= i).+1 or -1) added along the Fenwick ancestor chain.Theorem fenwick_add_spec (arr : loc) (tree : list Z) (n i : nat) (delta : Z) :
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
S n <= length tree).1 <= i).+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
Used by
7. Incremental `SortView` inverse-order array shift (Combinators.fs)
Definition inv_order_shift_loop : val :=
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 #().Definition inv_order_shift_code : 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".Lemma inv_order_shift_loop_spec
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
Used by
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 array.orderPos array.order before reinsertion.orderPos before reinsertion.p_idx.new_pos <= old_pos).old_pos to new_pos.Theorem inv_order_shift_spec
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 array.orderPos array.order before reinsertion.orderPos before reinsertion.p_idx.new_pos <= old_pos).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
- inv_order
- shift_right_steps
- reinsert_left_pure
- inv_order_to_hole
- inv_order_close_hole
- inv_order_shift_code
- inv_order_shift_loop_spec
- Some
8 more
- list
- nat
- Z.of_nat
- wp_store_offset
- loc
- list.lookup_lt_is_Some_2
- list.length_fmap
- list.list_fmap_insert
Used by
End heap_collections.