Ranvier.Pure.FastPaths

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

Stage B fast-path refinement theorems (pure tier)0. Fuel monotonicity and state invariants across Enginebind_monohosted_monountracked_monodetached_monowalk_marks_monomark_check_monomark_dirty_mononotify_dirty_mononotify_dirty_except_monom_step_mark_dirty_monom_step_mark_check_monom_step_notify_dirty_monom_step_notify_dirty_except_monomark_reader_monoengine_mono_atmono_stepengine_mono_allsame_depthssame_depths_reflsame_depths_transdepths_kept_atbind_depthsbind_depths_ofmarks_same_depthshosted_depthsuntracked_depthsdetached_depthslog_label_depthsmark_reader_depthsengine_depths_keptB1. Flushing implies no flush owed and Deferring equivalenceflushing_implies_no_flush_oweddeferring_b1b1_execb1_run_topb1_mono_atb1_mono_allb1_depths_kept_atb1_depths_keptb1_state_relb1_state_rel_reflb1_state_rel_eqb1_rel_fieldsb1_rel_updateb1_rel_simpleb1_rel_set_nodeb1_rel_with_pull_Sb1_rel_with_pull_S_leftb1_rel_with_pull_predrequest_flush_flushingsettle_flush_flushingb1_request_flush_flushingb1_settle_flush_flushingsim_fwd_stepsim_bwd_stepsim_bindsim_pure_bindb1_rel_hostedb1_rel_hosted_bwdb1_rel_untrackedb1_rel_untracked_bwdb1_rel_detachedb1_rel_detached_bwdb1_rel_destructb1_rel_pure_stepb1_rel_pure_step_bwdset_node_with_pullenqueue_with_pullwalk_marks_with_pullmark_check_with_pullmark_dirty_with_pullnotify_dirty_with_pullnotify_dirty_except_with_pullb1_fwd_bindb1_fwd_bind_ofb1_bwd_bindb1_bwd_bind_ofb1_fwd_bind_pure_lb1_bwd_bind_pure_lb1_fwd_bind_pure_l_ofb1_bwd_bind_pure_l_ofb1_hosted_fwdb1_hosted_bwdb1_untracked_fwdb1_untracked_bwdb1_detached_fwdb1_detached_bwdb1_mono_taceng_mono_tacb1_pure_solveb1_pure_solve_bwdb1_pure_m_returnb1_pure_m_return_bwdb1_pure_get_nodeb1_pure_get_node_bwdb1_pure_modify_nodeb1_pure_modify_node_bwdb1_pure_get_ownerb1_pure_get_owner_bwdb1_pure_edges_optb1_pure_edges_opt_bwdb1_pure_current_ownerb1_pure_current_owner_bwdb1_pure_note_host_readb1_pure_note_host_read_bwdb1_pure_trackb1_pure_track_bwdb1_pure_mark_readerb1_pure_mark_reader_bwdb1_pure_announceb1_pure_announce_bwdb1_pure_unlinkb1_pure_unlink_bwdb1_pure_record_errorb1_pure_record_error_bwdb1_pure_log_labelb1_pure_log_label_bwdb1_pure_mark_dirtyb1_pure_mark_dirty_bwdb1_pure_notify_dirtyb1_pure_notify_dirty_bwdb1_pure_notify_dirty_exceptb1_pure_notify_dirty_except_bwdb1_fwd_atb1_pure_modifyb1_pure_modify_bwdb1_pure_next_idb1_pure_next_id_bwdwrap_ex_Supd_pull_tacb1_pure_cmp_throwsb1_pure_cmp_throws_bwdb1_mod_fwdb1_mod_bwdb1_pure_fwdb1_pure_bwdb1_fwd_retb1_bwd_retb1_fwd_allb1_bwd_atb1_bwd_alldeferring_with_flushing_eqB2. Disposed empty sources & Track slot-0 / slot-1 cursor-bump fast pathsset_cursoredge_match_emptyedge_match_beginslot_at_none_niledge_match_addsources_add_keeps_emptysources_end_run_keeps_emptysources_clear_keeps_emptyb2_invb2_inv_initmark_prim_b2_invmarks_b2_invb2_inv_any_markb2_bind_ofb2_m_returnb2_m_modifyb2_get_nodeb2_get_ownerb2_modify_nodeb2_add_nodeb2_current_ownerb2_trackb2_mark_readerb2_announceb2_unlinkb2_record_errorb2_begin_runb2_end_or_clearb2_hostedb2_untrackedb2_detachedb2_dispose_prefixb2_next_idb2_note_host_readb2_log_labelb2_pure_tacb2_inv_atb2_inv_alldisposed_empty_sourcestrack_slot0_fast_eqtrack_slot1_fast_eqtrack_slot_fast_eqB3: Marking fast paths — leaf clean memo, moved single-check memo, iterative check spinenotify_with_nilnotify_with_singletonleaf_clean_memo_mark_dirty_eqnotify_moved_single_check_memo_eqiter_mark_check_chainiterative_mark_check_chain_eqB4: Explicit-stack iterative single-memo `Check`-chain refreshb4_bind_assocb4_bind_ret_lb4_bind_extsingle_memo_check_slotrefresh_after_srciter_unwind_oneiter_unwinditer_descenditer_chain_refreshrefresh_after_src_monoiter_unwind_monosingle_memo_check_slot_invdeferring_with_checkingresolve_walk_0_stepb4_bind_argrefresh_single_slot_unfoldb4_bind_push_checkingrefresh_under_4_noneiter_descend_fwditer_descend_bwditerative_chain_refresh_eqB5: Queue equivalence and idle-graph fused `S -> E` / `S -> M -> E` executionqueue_relqueue_rel_reflqueue_rel_symqueue_rel_reset_eqqueue_rel_drained_eqqueue_rel_live_queuequeue_rel_head_lookupqueue_rel_fieldsqueue_rel_popqueue_rel_enqueuequeue_rel_set_nodequeue_rel_effect_markedqrel_walk_marksqrel_mark_checkqrel_mark_dirtyqrel_notify_dirtyqrel_notify_dirty_exceptqrel_bind_ofqrel_m_returnqrel_m_modifyqrel_get_nodeqrel_modify_nodeqrel_get_ownerqrel_upd_tacqrel_current_ownerqrel_next_idqrel_note_host_readqrel_log_labelqrel_trackqrel_mark_readerqrel_announceqrel_unlinkqrel_record_errorqrel_edges_optqrel_hostedqrel_untrackedqrel_detachedqrel_pure_tacb1_qrel_atb1_qrel_allb1_drain_resetsdrain_resetsb1_write_signal_eqb1_run_effect_eqb1_drain_eqfused_single_signal_effectnode_clean_unqueued_marked_dirtym_bind_get_nodem_bind_modify_nodem_bind_modifym_bind_stepm_bind_getif_true_eqif_false_eqbool_decide_nat_0reset_queue_set_nodereset_queue_enqueuereset_queue_headreset_queue_flush_owedreset_queue_flushingset_node_with_flushingset_node_with_flush_owedqueue_rel_fused_signalqueue_rel_fused_memofused_single_signal_effect_eqfused_single_memo_effectfused_single_memo_effect_tail_monofused_single_memo_effect_flush_eqB6: Published ready-memo outcome cutoff refinementoutcome_movedready_value_movedmemo_published_ready_cutoff_eqB7. Bit-tagged edge handles and observer slotsN_lt_pow2_of_bitsN_testbit_lt_pow2edge_signal_bitedge_memo_bitedge_idx_maskedge_tag_maskencode_edge_handledecode_edge_sigdecode_edge_memodecode_edge_idxretag_edge_idxedge_handle_tag_losslessobs_node_slot_bitobs_slot_maskencode_observer_slotdecode_obs_is_nodedecode_obs_slotobserver_slot_tag_losslesstagged_observer_setempty_tagged_observer_setuntag_observer_settagged_observers_remove_atTaggedEdgeWorlduntag_source_listuntag_worldtobservers_oftsources_oftslot_attset_observerstset_sourcestset_handletmoved_handletagged_remove_edgetagged_sources_remove_slotuntag_observers_ofuntag_sources_ofuntag_set_observerslist_fmap_alteruntag_set_handletake_fmapuntag_observers_remove_atdecode_edge_idx_retagtagged_swap_remove_preserves_edge_matchB8. Packed Node.Flags bijection and single-word guard refinementnode_flags_unpackednf_wfnf_bool_bitpack_node_flagsunpack_node_flagsN_testbit_bool_bitN_testbit_shiftl_exactN_testbit_shiftl_ltN_testbit_1kill_pack_bitsN_testbit_pack_bit_exactpacked_node_flags_bijectionpacked_idle_effect_maskpacked_idle_effect_valpacked_clean_leaf_memo_maskpacked_clean_leaf_memo_valpacked_ready_published_memo_maskpacked_ready_published_memo_valpacked_guard_refines_unpackedB9. Leaf-memo cutoff skip and custom-comparer fused flushref_ready_cutoff_stepleaf_no_cutoff_stepleaf_memo_no_cutoff_eqwrite_signal_cmpfused_single_signal_effect_cmpcustom_comparer_fused_flush_eqB9b: Idle first-run effect fusion (`EffectNode.Start` / `EffectOn.Start`)fused_first_run_effectidle_first_run_effect_fusion_eqambient_opact_entryeager_ambientinit_eager_ambientstep_eagerresolve_eagerlazy_ambientinit_lazy_ambientstep_lazyresolve_ambientambient_wfrun_eager_tracerun_lazy_tracelazy_stack_invlazy_stack_inv_boundsambient_sim_relForall2_filter_actsstep_ambient_simambient_sim_resolveambient_lazy_epoch_trace_eqB11. Sole-observer TryPromoteTop & direct-return chain steptry_promote_top_effect_eqchain_moved_direct_stepchain_moved_ref_stepchain_direct_moved_step_eqB12. Deferring pull entry refinement (AsyncMemo / Boundary / StreamMemo)b1_deferringb1_batch_kept_atbind_bdmarks_bdbind_bd_hostedbind_bd_untrackedbind_bd_detachedbd_state_eqbd_primbd_solveb1_batch_keptb2_pull_relb2_pull_rel_reflb2_pull_rel_symb2_pull_rel_with_pull_Sb2_pull_rel_with_pull_S_rightb2_pull_rel_with_pull_predb2_pure_stepb2_pure_modifyb2p_bind_ofb2_bind_pure_l_ofb2p_hostedb2p_untrackedb2p_detachedb2_pure_solveb2_pure_m_returnb2_pure_failb2_pure_get_nodeb2_pure_modify_nodeb2_pure_get_ownerb2_pure_edges_optb2_pure_current_ownerb2_pure_note_host_readb2_pure_trackb2_pure_mark_readerb2_pure_announceb2_pure_unlinkb2_pure_record_errorb2_pure_log_labelb2_pure_next_idb2_pure_mark_dirtyb2_pure_notify_dirtyb2_pure_notify_dirty_exceptb2_pure_cmp_throwsb2_upd_tacb2_pure_add_nodeb2_pure_fwdb2_retb2_mono_tacb2_wrap_ex_Sb2_pull_rel_fieldsb2_sim_atb2_sim_alldeferring_pull_refines_enter_pullB13. Timed now-reuse non-immediate fire & Stamp non-throwing version equalitytimed_capture_now_reuse_no_immediate_firestamp_version_relstamp_version_eqstamp_version_eq_decstamp_version_eq_equivalenceend_run_matched_cursor
This definition

Ranvier.Pure.FastPaths

Stage B fast-path refinement theorems (pure tier)

This module formalizes and verifies the Stage B fast-path refinement theorems in Pure/:

  1. Theorem B1 (flushing_implies_no_flush_owed, deferring_with_flushing_eq): g_flushing = true implies g_flush_owed = false under sched_inv, and adding g_flushing to deferring yields identical states, values, and traces across all programs and pulls.
  2. Theorem B2 (disposed_empty_sources, track_slot0_fast_eq, track_slot1_fast_eq, track_slot_fast_eq): reachable disposed computations always have empty source_slots (even on the running stack), refining slot-0 and slot-1 hits in track to a direct cursor assignment.
  3. Theorem B3 (leaf_clean_memo_mark_dirty_eq, notify_moved_single_check_memo_eq, iterative_mark_check_chain_eq): leaf clean memo direct dirty write, sole Check memo promotion, and tail-recursive single-memo mark_check chain traversal.
  4. Theorem B4 (iterative_chain_refresh_eq): explicit-stack iterative single-memo Check-chain refresh with LIFO g_checking frames and repeat-check unwinding equals refresh on all states.
  5. Theorem B5 (fused_single_signal_effect_eq, fused_single_memo_effect_flush_eq): idle-graph fused S -> E and S -> M -> E execution equals write_signal with bit-for-bit identical final states and traces.
  6. Theorem B6 (memo_published_ready_cutoff_eq): ready-published memo outcome comparison simplifies to value comparison without status/failure checks.
5
From stdpp Require Import prelude gmap.
From Stdlib Require Import ZArith NArith.
From Ranvier.Base Require Import Prelude Syntax.
From Ranvier.Pure Require Import ObserverSet SourceList State Edges Marking Engine Check Runs Scheduler Ownership Concurrency.
Open Scope nat_scope.

0. Fuel monotonicity and state invariants across Engine

Lemma bind_mono {A B} (m1 m2 : M A) (k1 k2 : A -> M B) s r :
  (forall s1 a, m1 s = Some (s1, a) -> m2 s = Some (s1, a)) ->
  (forall a s1 r1, m1 s = Some (s1, a) -> k1 a s1 = Some r1 -> k2 a s1 = Some r1) ->
  m_bind m1 k1 s = Some r -> m_bind m2 k2 s = Some r.
Qed
Proof.
  intros Hm Hk Hb. unfold m_bind in *.
  destruct (m1 s) as [[s1 a]|] eqn:E; [|discriminate].
  rewrite (Hm _ _ eq_refl). by eapply Hk.
Qed.
References

Uses

Lemma hosted_mono {A} n (b1 b2 : M A) s r :
  (forall s0 r0, b1 s0 = Some r0 -> b2 s0 = Some r0) ->
  hosted n b1 s = Some r -> hosted n b2 s = Some r.
Qed
Proof.
  intros Hb H. unfold hosted in *.
  eapply bind_mono; [intros ?? E; exact E| |exact H].
  intros [] s1 r1 _.
  eapply bind_mono; [intros ?? E; by apply Hb|intros a s2 r2 _; exact id].
Qed.
References
Lemma untracked_mono {A} (b1 b2 : M A) s r :
  (forall s0 r0, b1 s0 = Some r0 -> b2 s0 = Some r0) ->
  untracked b1 s = Some r -> untracked b2 s = Some r.
Qed
Proof.
  intros Hb H. unfold untracked in *.
  eapply bind_mono; [intros ?? E; exact E| |exact H].
  intros [] s1 r1 _.
  eapply bind_mono; [intros ?? E; by apply Hb|intros a s2 r2 _; exact id].
Qed.
References
Lemma detached_mono o (b1 b2 : M unit) s r :
  (forall s0 r0, b1 s0 = Some r0 -> b2 s0 = Some r0) ->
  detached o b1 s = Some r -> detached o b2 s = Some r.
Qed
Proof.
  intros Hb H. unfold detached in *.
  eapply bind_mono; [intros ?? E; exact E| |exact H].
  intros [] s1 r1 _.
  eapply bind_mono; [intros ?? E; by apply Hb|intros [] s2 r2 _; exact id].
Qed.
References

Uses

Lemma walk_marks_mono mark1 mark2 src k s s' :
  (forall c s0 s1, mark1 c s0 = Some s1 -> mark2 c s0 = Some s1) ->
  walk_marks mark1 src k s = Some s' -> walk_marks mark2 src k s = Some s'.
Qed
Proof.
  intros Hm. revert s. induction k as [|i IH]; intros s Hw; simpl in *; [exact Hw|].
  destruct (decide _); [by apply IH|].
  destruct (observer_items (observers_of (g_edges s) src) !! i) as [[c ?]|]; [|discriminate].
  destruct (mark1 c s) as [s1|] eqn:E; simpl in *; [|discriminate].
  rewrite (Hm _ _ _ E). simpl. by apply IH.
Qed.
References
Lemma mark_check_mono f f' n s s' :
  f <= f' -> mark_check f n s = Some s' -> mark_check f' n s = Some s'.
Qed
Proof.
  revert f' n s s'. induction f as [|f IH]; intros f' n s s' Hle Hm; simpl in *; [discriminate|].
  destruct f' as [|f']; [lia|]. simpl.
  destruct (g_nodes s !! n) as [d|]; [|exact Hm].
  destruct (n_kind d); [exact Hm| |exact Hm].
  destruct (decide (n_freshness d = Clean)).
  - eapply walk_marks_mono; [|exact Hm]. intros c s0 s1. apply IH. lia.
  - destruct (announce_due s d); [|exact Hm].
    eapply walk_marks_mono; [|exact Hm]. intros c s0 s1. apply IH. lia.
Qed.
References
Lemma mark_dirty_mono f f' n s s' :
  f <= f' -> mark_dirty f n s = Some s' -> mark_dirty f' n s = Some s'.
Qed
Proof.
  intros Hle Hm. destruct f as [|f]; simpl in *; [discriminate|].
  destruct f' as [|f']; [lia|]. simpl.
  destruct (g_nodes s !! n) as [d|]; [|exact Hm].
  destruct (n_kind d); [exact Hm| |exact Hm].
  destruct (decide (n_freshness d = Clean)).
  - eapply walk_marks_mono; [|exact Hm]. intros c s0 s1. apply mark_check_mono. lia.
  - destruct (announce_due s d); [|exact Hm].
    eapply walk_marks_mono; [|exact Hm]. intros c s0 s1. apply mark_check_mono. lia.
Qed.
References
Lemma notify_dirty_mono f f' src s s' :
  f <= f' -> notify_dirty f src s = Some s' -> notify_dirty f' src s = Some s'.
Qed
Proof.
  intros Hle Hn. unfold notify_dirty, notify_with in *.
  eapply walk_marks_mono; [|exact Hn]. intros c s0 s1. by apply mark_dirty_mono.
Qed.
References
Lemma notify_dirty_except_mono f f' src r s s' :
  f <= f' -> notify_dirty_except f src r s = Some s' -> notify_dirty_except f' src r s = Some s'.
Qed
Proof.
  intros Hle Hn. unfold notify_dirty_except, notify_with in *.
  eapply walk_marks_mono; [|exact Hn]. intros c s0 s1.
  unfold mark_except. destruct (decide _); [exact id|by apply mark_dirty_mono].
Qed.
References
Lemma m_step_mark_dirty_mono f1 f2 n s r :
  f1 <= f2 -> m_step (mark_dirty f1 n) s = Some r -> m_step (mark_dirty f2 n) s = Some r.
Qed
Proof.
  intros Hle H. unfold m_step in H |- *.
  destruct (mark_dirty f1 n s) eqn:Em; simplify_eq.
  erewrite mark_dirty_mono; [reflexivity|exact Hle|exact Em].
Qed.
References
Lemma m_step_mark_check_mono f1 f2 n s r :
  f1 <= f2 -> m_step (mark_check f1 n) s = Some r -> m_step (mark_check f2 n) s = Some r.
Qed
Proof.
  intros Hle H. unfold m_step in H |- *.
  destruct (mark_check f1 n s) eqn:Em; simplify_eq.
  erewrite mark_check_mono; [reflexivity|exact Hle|exact Em].
Qed.
References
Lemma m_step_notify_dirty_mono f1 f2 src s r :
  f1 <= f2 -> m_step (notify_dirty f1 src) s = Some r -> m_step (notify_dirty f2 src) s = Some r.
Qed
Proof.
  intros Hle H. unfold m_step in H |- *.
  destruct (notify_dirty f1 src s) eqn:Em; simplify_eq.
  erewrite notify_dirty_mono; [reflexivity|exact Hle|exact Em].
Qed.
References
Lemma m_step_notify_dirty_except_mono f1 f2 src cur s r :
  f1 <= f2 -> m_step (notify_dirty_except f1 src cur) s = Some r -> m_step (notify_dirty_except f2 src cur) s = Some r.
Qed
Proof.
  intros Hle H. unfold m_step in H |- *.
  destruct (notify_dirty_except f1 src cur s) eqn:Em; simplify_eq.
  erewrite notify_dirty_except_mono; [reflexivity|exact Hle|exact Em].
Qed.
References
Lemma mark_reader_mono f1 f2 s r :
  f1 <= f2 -> mark_reader f1 s = Some r -> mark_reader f2 s = Some r.
Qed
Proof.
  intros Hle H. unfold mark_reader in H |- *.
  destruct (g_current s), (f_reader_mark (g_flags s)); try exact H.
  destruct (mark_check f1 _ s) eqn:Em; simplify_eq.
  erewrite mark_check_mono; [reflexivity|exact Hle|exact Em].
Qed.
References
Record engine_mono_at (f : nat) : Prop := {
  Mexec : forall f' e c s r, f <= f' -> exec f e c s = Some r -> exec f' e c s = Some r;
  Mread : forall f' n b s r, f <= f' -> read f n b s = Some r -> read f' n b s = Some r;
  Mread_node : forall f' n s r, f <= f' -> read_node f n s = Some r -> read_node f' n s = Some r;
  Mupdate : forall f' n s r, f <= f' -> update_if_necessary f n s = Some r -> update_if_necessary f' n s = Some r;
  Mrop : forall f' n s r, f <= f' -> refresh_or_pull f n s = Some r -> refresh_or_pull f' n s = Some r;
  Mpull : forall f' n s r, f <= f' -> pull f n s = Some r -> pull f' n s = Some r;
  Mrefresh : forall f' n s r, f <= f' -> refresh f n s = Some r -> refresh f' n s = Some r;
  Mwalk : forall f' n i s r, f <= f' -> resolve_walk f n i s = Some r -> resolve_walk f' n i s = Some r;
  Mrecompute : forall f' n s r, f <= f' -> recompute f n s = Some r -> recompute f' n s = Some r;
  Mrun_memo : forall f' n s r, f <= f' -> run_memo f n s = Some r -> run_memo f' n s = Some r;
  Mexecute : forall f' n s r, f <= f' -> execute_effect f n s = Some r -> execute_effect f' n s = Some r;
  Mrun_effect : forall f' n s r, f <= f' -> run_effect f n s = Some r -> run_effect f' n s = Some r;
  Mbody : forall f' n s r, f <= f' -> run_effect_body f n s = Some r -> run_effect_body f' n s = Some r;
  Mdischarge : forall f' o s r, f <= f' -> discharge f o s = Some r -> discharge f' o s = Some r;
  Mretire : forall f' o s r, f <= f' -> retire f o s = Some r -> retire f' o s = Some r;
  Mdispose_scope : forall f' o s r, f <= f' -> dispose_scope f o s = Some r -> dispose_scope f' o s = Some r;
  Mteardown : forall f' o ls cs s r, f <= f' -> teardown f o ls cs s = Some r -> teardown f' o ls cs s = Some r;
  Mcleanup : forall f' o b e s r, f <= f' -> run_cleanup f o b e s = Some r -> run_cleanup f' o b e s = Some r;
  Mrelease : forall f' n s r, f <= f' -> release f n s = Some r -> release f' n s = Some r;
  Mdispose : forall f' n s r, f <= f' -> dispose_node f n s = Some r -> dispose_node f' n s = Some r;
  Mattach : forall f' n o s r, f <= f' -> attach f n o s = Some r -> attach f' n o s = Some r;
  Mwrite : forall f' n v s r, f <= f' -> write_signal f n v s = Some r -> write_signal f' n v s = Some r;
  Mrequest : forall f' s r, f <= f' -> request_flush f s = Some r -> request_flush f' s = Some r;
  Msettle : forall f' s r, f <= f' -> settle_flush f s = Some r -> settle_flush f' s = Some r;
  Mflush : forall f' s r, f <= f' -> run_flush f s = Some r -> run_flush f' s = Some r;
  Mdrain : forall f' s r, f <= f' -> drain f s = Some r -> drain f' s = Some r
}.
References
Local Ltac mono_step :=
  repeat match goal with
  | |- m_bind _ _ _ = Some _ => eapply bind_mono; [intros ?? ?; repeat mono_step|intros ??? ?]
  | |- hosted _ _ _ = Some _ => eapply hosted_mono; [intros ?? ?; repeat mono_step|]
  | |- untracked _ _ = Some _ => eapply untracked_mono; [intros ?? ?; repeat mono_step|]
  | |- detached _ _ _ = Some _ => eapply detached_mono; [intros ?? ?; repeat mono_step|]
  | H : m_step _ ?s = Some (?s', tt) |- m_step _ ?s = Some (?s', tt) =>
      unfold m_step in H |- *; destruct (_ ?s) eqn:?E; simplify_eq;
      first [erewrite mark_dirty_mono; [reflexivity|lia|exact E]
            |erewrite mark_check_mono; [reflexivity|lia|exact E]
            |erewrite notify_dirty_mono; [reflexivity|lia|exact E]
            |erewrite notify_dirty_except_mono; [reflexivity|lia|exact E]
            |idtac]
  | |- context[match ?x with _ => _ end] => destruct x eqn:?
  | |- (if ?b then _ else _) _ = Some _ => destruct b eqn:?
  end; try (eauto with lia; fail).
References
Lemma engine_mono_all f : engine_mono_at f.
Qed
Proof.
  induction f as [|f IH].
  - split; intros; try discriminate.
  - assert (Hle : forall f', S f <= f' -> exists g, f' = S g /\ f <= g).
    { intros [|g] H; [lia|]. exists g. split; [reflexivity|lia]. }
    split.
    + intros f' e c s r Hle' H. destruct (Hle _ Hle') as (g & -> & Hg).
      cbn [exec] in H |- *.
      destruct c; repeat match goal with
      | |- context [on_node] => unfold on_node in H |- *
      | |- context [on_value] => unfold on_value in H |- *
      | |- context [match eval_expr ?e ?p ?x with _ => _ end] => destruct (eval_expr e p x) as [[|]|]; try discriminate
      | |- (if decide _ then _ else stuck) _ = Some _ => destruct (decide _); try discriminate
      end; try exact H; try (by apply (Mexec _ IH)); try (destruct (decide _); by apply (Mexec _ IH)); revert H.
      * eapply bind_mono; [intros ??; by apply (Mread _ IH)|intros o ?? _].
        eapply bind_mono; [intros ?? E; exact E|intros [] ?? _; destruct o; [by apply (Mexec _ IH)|exact id]].
      * eapply bind_mono; [intros ??; by apply (Mread _ IH)|intros o ?? _].
        eapply bind_mono; [intros ?? E; exact E|intros [] ?? _; destruct o; by apply (Mexec _ IH)].
      * eapply bind_mono; [intros ??; by apply (Mread _ IH)|intros o ?? _].
        eapply bind_mono; [intros ?? E; exact E|intros [] ?? _; destruct o; [by apply (Mexec _ IH)|exact id]].
      * eapply bind_mono; [intros ??; by apply (Mwrite _ IH)|intros [] ?? _; by apply (Mexec _ IH)].
      * eapply bind_mono; [intros ?? E; exact E|intros ??? _].
        eapply bind_mono; [intros ?? E; exact E|intros ??? _].
        eapply bind_mono; [intros ?? E; exact E|intros [o|] ?? _; [|exact id]].
        eapply bind_mono; [intros ??; by apply (Mattach _ IH)|intros [] ?? _; by apply (Mexec _ IH)].
      * eapply bind_mono; [intros ?? E; exact E|intros ??? _].
        eapply bind_mono; [intros ?? E; exact E|intros ??? _].
        eapply bind_mono; [intros ?? E; exact E|intros [o|] ?? _; [|exact id]].
        eapply bind_mono; [intros ??; by apply (Mattach _ IH)|intros [] ?? _].
        eapply bind_mono; [|intros [] ?? _].
        { intros ?? E. unfold m_step in E |- *. destruct (mark_dirty f _ _) eqn:Em; simplify_eq.
          erewrite mark_dirty_mono; [reflexivity|exact Hg|exact Em]. }
        eapply bind_mono; [intros ??; by apply (Mrequest _ IH)|intros [] ?? _; by apply (Mexec _ IH)].
      * eapply bind_mono; [intros ?? E; exact E|intros [o|] ?? _; [|exact id]].
        eapply bind_mono; [intros ?? E; exact E|intros x ?? _].
        eapply bind_mono; [intros ?? E; exact E|intros [] ?? _].
        eapply bind_mono; [|intros [] ?? _; by apply (Mexec _ IH)].
        intros ??; destruct (o_disposed x); [|exact id].
        eapply bind_mono; [intros ?? E; eapply detached_mono; [intros ??; by apply (Mcleanup _ IH)|exact E]|intros [] ?? _; by apply (Mrequest _ IH)].
      * eapply bind_mono; [intros ?? E; exact E|intros d ?? _].
        destruct (n_kind d); try discriminate;
          (eapply bind_mono; [intros ??; by apply (Mdispose _ IH)|intros [] ?? _];
           eapply bind_mono; [intros ?? E; exact E|intros [] ?? _; by apply (Mexec _ IH)]).
      * eapply bind_mono; [intros ?? E; exact E|intros [] ?? _].
        eapply bind_mono; [intros ??; by apply (Mexec _ IH)|intros [|] ?? _];
          (eapply bind_mono; [intros ?? E; exact E|intros [] ?? _]);
          [eapply bind_mono; [intros ??; by apply (Mrequest _ IH)|intros [] ?? _; by apply (Mexec _ IH)]|exact id].
      * eapply bind_mono; [intros ?? E; exact E|intros ??? _].
        eapply bind_mono; [intros ?? E; exact E|intros [] ?? _; by apply (Mexec _ IH)].
    + intros f' n b s r Hle' H. destruct (Hle _ Hle') as (g & -> & Hg).
      cbn [read] in H |- *. destruct b; [by apply (Mread_node _ IH)|eapply untracked_mono; [intros ??; by apply (Mread_node _ IH)|exact H]].
    + intros f' n s r Hle' H. destruct (Hle _ Hle') as (g & -> & Hg).
      cbn [read_node] in H |- *. revert H.
      eapply bind_mono; [intros ?? E; exact E|intros d ?? _].
      destruct (n_kind d); [exact id| |exact id].
      eapply bind_mono; [|intros d' ?? _; exact id].
      intros ??; destruct (stale_live d); [|exact id].
      eapply bind_mono; [intros ??; by apply (Mrop _ IH)|intros d' ?? _].
      eapply bind_mono; [|intros [] ?? _; exact id].
      intros ??; eapply bind_mono; [intros ?? E; exact E|intros [] ?? _].
      eapply bind_mono; [intros ?? E; exact E|intros d'' ?? _].
      destruct (stale_live d''); [|exact id].
      intros E; eapply mark_reader_mono; [exact Hg|exact E].
    + intros f' n s r Hle' H. destruct (Hle _ Hle') as (g & -> & Hg).
      cbn [update_if_necessary] in H |- *. revert H.
      eapply bind_mono; [intros ?? E; exact E|intros d ?? _].
      destruct (n_kind d); [exact id| |exact id].
      destruct (stale_live d); [|exact id].
      eapply bind_mono; [intros ??; by apply (Mrop _ IH)|intros ??? _; exact id].
    + intros f' n s r Hle' H. destruct (Hle _ Hle') as (g & -> & Hg).
      cbn [refresh_or_pull] in H |- *. revert H.
      eapply bind_mono; [intros ?? E; exact E|intros st0 ?? _].
      destruct (deferring st0); [|by apply (Mpull _ IH)].
      eapply bind_mono; [intros ??; by apply (Mrefresh _ IH)|intros ??? _; exact id].
    + intros f' n s r Hle' H. destruct (Hle _ Hle') as (g & -> & Hg).
      cbn [pull] in H |- *. revert H.
      eapply bind_mono; [intros ?? E; exact E|intros [] ?? _].
      eapply bind_mono; [intros ??; by apply (Mrefresh _ IH)|intros [] ?? _].
      eapply bind_mono; [intros ?? E; exact E|intros d ?? _].
      eapply bind_mono; [intros ?? E; exact E|intros [] ?? _].
      eapply bind_mono; [intros ?? E; exact E|intros st0 ?? _].
      eapply bind_mono; [|intros ??? _; exact id].
      intros ??; destruct (_ && _); [by apply (Msettle _ IH)|exact id].
    + intros f' n s r Hle' H. destruct (Hle _ Hle') as (g & -> & Hg).
      cbn [refresh] in H |- *. revert H.
      eapply bind_mono; [intros ?? E; exact E|intros st0 ?? _].
      destruct (decide (n ∈ g_checking st0)).
      { intros E; eapply m_step_mark_dirty_mono; [exact Hg|exact E]. }
      destruct (decide (n ∈ g_running st0)); [exact id|].
      eapply bind_mono; [intros ?? E; exact E|intros d ?? _].
      eapply bind_mono; [|intros [] ?? _].
      { intros ??; destruct (decide (n_freshness d = Check)); [|exact id].
        eapply bind_mono; [intros ?? E; exact E|intros [] ?? _].
        eapply bind_mono; [intros ??; by apply (Mwalk _ IH)|intros [] ?? _].
        eapply bind_mono; [intros ?? E; exact E|intros [] ?? _].
        eapply bind_mono; [intros ?? E; exact E|intros d' ?? _].
        destruct (decide (n_freshness d' = Check)); [|exact id].
        eapply bind_mono; [intros ?? E; exact E|intros [] ?? _; exact id]. }
      eapply bind_mono; [intros ?? E; exact E|intros d' ?? _].
      destruct (decide (n_freshness d' = Dirty)); [by apply (Mrecompute _ IH)|exact id].
    + intros f' n i s r Hle' H. destruct (Hle _ Hle') as (g & -> & Hg).
      cbn [resolve_walk] in H |- *. revert H.
      eapply bind_mono; [intros ?? E; exact E|intros d ?? _].
      eapply bind_mono; [intros ?? E; exact E|intros st0 ?? _].
      destruct (decide (n_freshness d = Check)); [|exact id].
      destruct (source_slots (sources_of (g_edges st0) n) !! i) as [[src _]|].
      * eapply bind_mono; [intros ??; by apply (Mupdate _ IH)|intros [] ?? _].
        eapply bind_mono; [intros ?? E; exact E|intros d' ?? _].
        eapply bind_mono; [intros ?? E; exact E|intros d_src ?? _].
        eapply bind_mono; [intros ?? E; exact E|intros st1 ?? _].
        eapply bind_mono; [|intros [] ?? _; by apply (Mwalk _ IH)].
        intros st2 a0; destruct (decide _); [|exact id]. intros E; eapply m_step_mark_dirty_mono; [exact Hg|exact E].
      * destruct (slot_holds stale_live st0 n); [by apply (Mwalk _ IH)|exact id].
    + intros f' n s r Hle' H. destruct (Hle _ Hle') as (g & -> & Hg).
      cbn [recompute] in H |- *. revert H.
      eapply bind_mono; [intros ?? E; exact E|intros d ?? _].
      destruct (n_scope d) as [o|]; [|by apply (Mrun_memo _ IH)].
      eapply bind_mono; [intros ?? E; exact E|intros [] ?? _].
      eapply bind_mono; [intros ?? E; exact E|intros [] ?? _].
      eapply bind_mono; [intros ??; by apply (Mdischarge _ IH)|intros [] ?? _].
      eapply bind_mono; [intros ?? E; exact E|intros d' ?? _].
      destruct (n_disposed d'); [exact id|].
      destruct (decide (n_runs d' = n_runs d)); [by apply (Mrun_memo _ IH)|].
      destruct (decide (n_freshness d' = Dirty)); [by apply (Mrecompute _ IH)|exact id].
    + intros f' n s r Hle' H. destruct (Hle _ Hle') as (g & -> & Hg).
      cbn [run_memo] in H |- *. revert H.
      eapply bind_mono; [intros ?? E; exact E|intros d ?? _].
      eapply bind_mono; [intros ?? E; exact E|intros [] ?? _].
      eapply bind_mono; [intros ?? E; exact E|intros [] ?? _].
      eapply bind_mono; [intros ?? E; exact E|intros [] ?? _].
      eapply bind_mono; [intros ?? E; exact E|intros [] ?? _].
      eapply bind_mono; [intros ?? E; eapply hosted_mono; [intros ??; by apply (Mexec _ IH)|exact E]|intros o ?? _].
      eapply bind_mono; [intros ?? E; exact E|intros d1 ?? _].
      eapply bind_mono; [intros ?? E; exact E|intros [] ?? _].
      eapply bind_mono; [intros ?? E; exact E|intros [] ?? _].
      eapply bind_mono; [intros ?? E; exact E|intros [] ?? _].
      eapply bind_mono; [intros ?? E; exact E|intros [] ?? _].
      eapply bind_mono; [intros ?? E; exact E|intros d2 ?? _].
      eapply bind_mono; [intros ?? E; exact E|intros moved ?? _].
      eapply bind_mono; [intros ?? E; exact E|intros st0 ?? _].
      eapply bind_mono; [intros ?? E; exact E|intros [] ?? _].
      eapply bind_mono; [|intros [] ?? _; exact id].
      intros ??; destruct moved; [|exact id].
      eapply bind_mono; [|intros [] ?? _; exact id].
      intros st2 a0 E. eapply m_step_notify_dirty_except_mono; [exact Hg|exact E].
    + intros f' n s r Hle' H. destruct (Hle _ Hle') as (g & -> & Hg).
      cbn [execute_effect] in H |- *. revert H.
      eapply bind_mono; [intros ?? E; exact E|intros d ?? _].
      destruct (n_disposed d); [exact id|].
      eapply bind_mono; [|intros [] ?? _].
      { intros ??; destruct (decide (n_freshness d = Check)); [|exact id].
        eapply bind_mono; [intros ??; by apply (Mwalk _ IH)|intros [] ?? _].
        eapply bind_mono; [intros ?? E; exact E|intros d' ?? _].
        destruct (decide (n_freshness d' = Check)); [|exact id].
        eapply bind_mono; [intros ?? E; exact E|intros [] ?? _; exact id]. }
      eapply bind_mono; [intros ?? E; exact E|intros d' ?? _].
      eapply bind_mono; [intros ?? E; exact E|intros [] ?? _].
      destruct (decide (n_freshness d' = Dirty)); [by apply (Mrun_effect _ IH)|exact id].
    + intros f' n s r Hle' H. destruct (Hle _ Hle') as (g & -> & Hg).
      cbn [run_effect] in H |- *. revert H.
      eapply bind_mono; [intros ?? E; exact E|intros [] ?? _].
      eapply bind_mono; [intros ?? E; exact E|intros [] ?? _].
      eapply bind_mono; [intros ?? E; exact E|intros [] ?? _].
      eapply bind_mono; [intros ?? E; exact E|intros d ?? _].
      eapply bind_mono; [|intros [] ?? _].
      { intros ??; destruct (n_scope d) as [o|]; [|exact id].
        eapply bind_mono; [intros ?? E; exact E|intros [] ?? _].
        eapply bind_mono; [intros ??; by apply (Mdischarge _ IH)|intros [] ?? _; exact id]. }
      eapply bind_mono; [intros ?? E; exact E|intros d' ?? _].
      eapply bind_mono; [|intros [] ?? _; exact id].
      intros ??; destruct (n_disposed d'); [exact id|by apply (Mbody _ IH)].
    + intros f' n s r Hle' H. destruct (Hle _ Hle') as (g & -> & Hg).
      cbn [run_effect_body] in H |- *. revert H.
      eapply bind_mono; [intros ?? E; exact E|intros d ?? _].
      eapply bind_mono; [intros ?? E; exact E|intros [] ?? _].
      eapply bind_mono; [intros ?? E; exact E|intros [] ?? _].
      eapply bind_mono; [intros ?? E; eapply hosted_mono; [intros ??; by apply (Mexec _ IH)|exact E]|intros o ?? _].
      eapply bind_mono; [intros ?? E; exact E|intros d' ?? _].
      eapply bind_mono; [intros ?? E; exact E|intros [] ?? _; exact id].
    + intros f' o s r Hle' H. destruct (Hle _ Hle') as (g & -> & Hg).
      cbn [discharge] in H |- *. revert H.
      eapply bind_mono; [intros ?? E; eapply detached_mono; [intros ??; by apply (Mdispose_scope _ IH)|exact E]|intros [] ?? _; exact id].
    + intros f' o s r Hle' H. destruct (Hle _ Hle') as (g & -> & Hg).
      cbn [retire] in H |- *. revert H.
      eapply bind_mono; [|intros [] ?? _].
      { intros ?? E0. eapply detached_mono; [|exact E0]. intros st0 r0.
        eapply bind_mono; [intros ?? E; exact E|intros x ?? _].
        destruct (o_disposed x); [exact id|].
        eapply bind_mono; [intros ?? E; exact E|intros [] ?? _; by apply (Mdispose_scope _ IH)]. }
      eapply bind_mono; [intros ?? E; exact E|intros st0 ?? _].
      destruct (live_queue st0); [by apply (Mrequest _ IH)|exact id].
    + intros f' o s r Hle' H. destruct (Hle _ Hle') as (g & -> & Hg).
      cbn [dispose_scope] in H |- *. revert H.
      eapply bind_mono; [intros ?? E; exact E|intros x ?? _].
      eapply bind_mono; [intros ?? E; exact E|intros [] ?? _; by apply (Mteardown _ IH)].
    + intros f' o ls cs s r Hle' H. destruct (Hle _ Hle') as (g & -> & Hg).
      cbn [teardown] in H |- *. revert H.
      destruct ls as [|l ls'], cs as [|[[b e] a] cs']; [exact id|..].
      * eapply bind_mono; [intros ??; by apply (Mcleanup _ IH)|intros [] ?? _; by apply (Mteardown _ IH)].
      * eapply bind_mono; [intros ??; by apply (Mrelease _ IH)|intros [] ?? _; by apply (Mteardown _ IH)].
      * destruct (decide (a < l_acquired l)).
        -- eapply bind_mono; [intros ??; by apply (Mrelease _ IH)|intros [] ?? _; by apply (Mteardown _ IH)].
        -- eapply bind_mono; [intros ??; by apply (Mcleanup _ IH)|intros [] ?? _; by apply (Mteardown _ IH)].
    + intros f' o b e s r Hle' H. destruct (Hle _ Hle') as (g & -> & Hg).
      cbn [run_cleanup] in H |- *. revert H.
      eapply bind_mono; [intros ?? E; exact E|intros x ?? _].
      eapply bind_mono; [intros ?? E; exact E|intros [] ?? _].
      eapply bind_mono; [intros ??; by apply (Mexec _ IH)|intros [|] ?? _; exact id].
    + intros f' n s r Hle' H. destruct (Hle _ Hle') as (g & -> & Hg).
      cbn [release] in H |- *. revert H.
      eapply bind_mono; [intros ?? E; exact E|intros [] ?? _; by apply (Mdispose _ IH)].
    + intros f' n s r Hle' H. destruct (Hle _ Hle') as (g & -> & Hg).
      cbn [dispose_node] in H |- *. revert H.
      eapply bind_mono; [intros ?? E; exact E|intros d ?? _].
      destruct (n_disposed d); [exact id|].
      eapply bind_mono; [intros ?? E; exact E|intros [] ?? _].
      eapply bind_mono; [intros ?? E; exact E|intros [] ?? _].
      eapply bind_mono; [intros ?? E; exact E|intros [] ?? _].
      destruct (n_scope d); [by apply (Mretire _ IH)|exact id].
    + intros f' n o s r Hle' H. destruct (Hle _ Hle') as (g & -> & Hg).
      cbn [attach] in H |- *. revert H.
      eapply bind_mono; [intros ?? E; exact E|intros x ?? _].
      destruct (o_disposed x); [|exact id].
      eapply bind_mono; [intros ??; by apply (Mrelease _ IH)|intros [] ?? _; exact id].
    + intros f' n v s r Hle' H. destruct (Hle _ Hle') as (g & -> & Hg).
      cbn [write_signal] in H |- *. revert H.
      eapply bind_mono; [intros ?? E; exact E|intros d ?? _].
      destruct (n_kind d); [|exact id|exact id].
      destruct (decide (n_value d = v)); [exact id|].
      eapply bind_mono; [intros ?? E; exact E|intros [] ?? _].
      eapply bind_mono; [intros ?? E; exact E|intros [] ?? _].
      eapply bind_mono; [|intros [] ?? _; by apply (Mrequest _ IH)].
      intros st2 a0 E. eapply m_step_notify_dirty_mono; [exact Hg|exact E].
    + intros f' s r Hle' H. destruct (Hle _ Hle') as (g & -> & Hg).
      cbn [request_flush] in H |- *. revert H.
      eapply bind_mono; [intros ?? E; exact E|intros st0 ?? _].
      destruct (_ && _); [|exact id].
      destruct (outside_bodies st0); [by apply (Mflush _ IH)|exact id].
    + intros f' s r Hle' H. destruct (Hle _ Hle') as (g & -> & Hg).
      cbn [settle_flush] in H |- *. revert H.
      eapply bind_mono; [intros ?? E; exact E|intros st0 ?? _].
      destruct (_ && _); [by apply (Mflush _ IH)|exact id].
    + intros f' s r Hle' H. destruct (Hle _ Hle') as (g & -> & Hg).
      cbn [run_flush] in H |- *. revert H.
      eapply bind_mono; [intros ?? E; exact E|intros st0 ?? _].
      destruct (g_flushing st0); [exact id|].
      eapply bind_mono; [intros ?? E; exact E|intros [] ?? _].
      eapply bind_mono; [intros ??; by apply (Mdrain _ IH)|intros [] ?? _; exact id].
    + intros f' s r Hle' H. destruct (Hle _ Hle') as (g & -> & Hg).
      cbn [drain] in H |- *. revert H.
      eapply bind_mono; [intros ?? E; exact E|intros st0 ?? _].
      destruct (g_queue st0 !! g_queue_head st0); [|exact id].
      eapply bind_mono; [intros ?? E; exact E|intros [] ?? _].
      eapply bind_mono; [intros ??; by apply (Mexecute _ IH)|intros [] ?? _; by apply (Mdrain _ IH)].
Qed.
References
Definition same_depths (s s' : state) : Prop :=
  g_pull_depth s' = g_pull_depth s /\ g_flushing s' = g_flushing s.
References
Lemma same_depths_refl s : same_depths s s.
Qed
Proof. split; reflexivity. Qed.
References
Lemma same_depths_trans s1 s2 s3 : same_depths s1 s2 -> same_depths s2 s3 -> same_depths s1 s3.
Qed
Proof. intros [A B] [C D]. split; congruence. Qed.
References
Record depths_kept_at (f : nat) : Prop := {
  Dexec : forall e c s s' r, exec f e c s = Some (s', r) -> same_depths s s';
  Dread : forall n b s s' r, read f n b s = Some (s', r) -> same_depths s s';
  Dread_node : forall n s s' r, read_node f n s = Some (s', r) -> same_depths s s';
  Dupdate : forall n s s' r, update_if_necessary f n s = Some (s', r) -> same_depths s s';
  Drop : forall n s s' r, refresh_or_pull f n s = Some (s', r) -> same_depths s s';
  Dpull : forall n s s' r, pull f n s = Some (s', r) -> same_depths s s';
  Drefresh : forall n s s' r, refresh f n s = Some (s', r) -> same_depths s s';
  Dwalk : forall n i s s' r, resolve_walk f n i s = Some (s', r) -> same_depths s s';
  Drecompute : forall n s s' r, recompute f n s = Some (s', r) -> same_depths s s';
  Drun_memo : forall n s s' r, run_memo f n s = Some (s', r) -> same_depths s s';
  Dexecute : forall n s s' r, execute_effect f n s = Some (s', r) -> same_depths s s';
  Drun_effect : forall n s s' r, run_effect f n s = Some (s', r) -> same_depths s s';
  Dbody : forall n s s' r, run_effect_body f n s = Some (s', r) -> same_depths s s';
  Ddischarge : forall o s s' r, discharge f o s = Some (s', r) -> same_depths s s';
  Dretire : forall o s s' r, retire f o s = Some (s', r) -> same_depths s s';
  Ddispose_scope : forall o s s' r, dispose_scope f o s = Some (s', r) -> same_depths s s';
  Dteardown : forall o ls cs s s' r, teardown f o ls cs s = Some (s', r) -> same_depths s s';
  Dcleanup : forall o b e s s' r, run_cleanup f o b e s = Some (s', r) -> same_depths s s';
  Drelease : forall n s s' r, release f n s = Some (s', r) -> same_depths s s';
  Ddispose : forall n s s' r, dispose_node f n s = Some (s', r) -> same_depths s s';
  Dattach : forall n o s s' r, attach f n o s = Some (s', r) -> same_depths s s';
  Dwrite : forall n v s s' r, write_signal f n v s = Some (s', r) -> same_depths s s';
  Drequest : forall s s' r, request_flush f s = Some (s', r) -> same_depths s s';
  Dsettle : forall s s' r, settle_flush f s = Some (s', r) -> same_depths s s';
  Dflush : forall s s' r, run_flush f s = Some (s', r) -> same_depths s s';
  Ddrain : forall s s' r, drain f s = Some (s', r) -> same_depths s s'
}.
References
Lemma bind_depths {A B} (m : M A) (k : A -> M B) s s' b :
  (forall s1 a, m s = Some (s1, a) -> same_depths s s1) ->
  (forall a s1, m s = Some (s1, a) -> k a s1 = Some (s', b) -> same_depths s1 s') ->
  m_bind m k s = Some (s', b) -> same_depths s s'.
Qed
Proof.
  intros Hm Hk Hb. unfold m_bind in Hb.
  destruct (m s) as [[s1 a]|] eqn:E; [|discriminate].
  eapply same_depths_trans; [by eapply Hm|by eapply Hk].
Qed.
References
Lemma bind_depths_of {A B} {m : M A} {k : A -> M B} {s s' b}
    (Hb : m_bind m k s = Some (s', b)) :
  (forall s1 a, m s = Some (s1, a) -> same_depths s s1) ->
  (forall a s1, m s = Some (s1, a) -> k a s1 = Some (s', b) -> same_depths s1 s') ->
  same_depths s s'.
Qed
Proof. intros Hm Hk. exact (bind_depths m k s s' b Hm Hk Hb). Qed.
References
Lemma marks_same_depths s s' : rtc mark_prim s s' -> same_depths s s'.
Qed
Proof.
  induction 1 as [s|s1 s2 s3 Hp _ IH]; [apply same_depths_refl|].
  eapply same_depths_trans; [|exact IH]. destruct Hp; split; reflexivity.
Qed.
References
Lemma hosted_depths {A} n (b : M A) s s' r :
  (forall s0 s1 a, b s0 = Some (s1, a) -> same_depths s0 s1) ->
  hosted n b s = Some (s', r) -> same_depths s s'.
Qed
Proof.
  intros Hb H. unfold hosted in H.
  eapply (bind_depths_of H); [intros ?? E; unfold m_modify in E; simplify_eq; split; reflexivity| ].
  intros [] s1 E1. eapply bind_depths; [intros ?? E2; by eapply Hb|].
  intros a s2 E2 H3. unfold m_bind, m_modify, m_return in H3. simplify_eq. split; reflexivity.
Qed.
References
Lemma untracked_depths {A} (b : M A) s s' r :
  (forall s0 s1 a, b s0 = Some (s1, a) -> same_depths s0 s1) ->
  untracked b s = Some (s', r) -> same_depths s s'.
Qed
Proof.
  intros Hb H. unfold untracked in H.
  eapply (bind_depths_of H); [intros ?? E; unfold m_modify in E; simplify_eq; split; reflexivity| ].
  intros [] s1 E1. eapply bind_depths; [intros ?? E2; by eapply Hb|].
  intros a s2 E2 H3. unfold m_bind, m_modify, m_return in H3. simplify_eq. split; reflexivity.
Qed.
References
Lemma detached_depths o (b : M unit) s s' r :
  (forall s0 s1 a, b s0 = Some (s1, a) -> same_depths s0 s1) ->
  detached o b s = Some (s', r) -> same_depths s s'.
Qed
Proof.
  intros Hb H. unfold detached in H.
  eapply (bind_depths_of H); [intros ?? E; unfold m_modify in E; simplify_eq; split; reflexivity| ].
  intros [] s1 E1. eapply bind_depths; [intros ?? E2; by eapply Hb|].
  intros [] s2 E2 H3. unfold m_bind, m_modify in H3. simplify_eq. split; reflexivity.
Qed.
References
Lemma log_label_depths s s' a : log_label s = Some (s', a) -> same_depths s s'.
Qed
Proof.
  intro H. unfold log_label in H.
  destruct (g_current s), (g_current_owner s); simplify_eq; apply same_depths_refl.
Qed.
References
Lemma mark_reader_depths f s s' a : mark_reader f s = Some (s', a) -> same_depths s s'.
Qed
Proof.
  intro H. unfold mark_reader in H.
  destruct (g_current s), (f_reader_mark (g_flags s)); try (simplify_eq; apply same_depths_refl).
  destruct (mark_check f _ s) eqn:Em; simplify_eq. by eapply marks_same_depths, mark_check_rtc.
Qed.
References
Lemma engine_depths_kept f : depths_kept_at f.
Qed
Proof.
  induction f as [|f IH].
  - split; intros; discriminate.
  - assert (Htrack : forall n s s' a, track n s = Some (s', a) -> same_depths s s').
    { intros n s s' a Ht. unfold track in Ht. destruct (live_current s); [|simplify_eq; apply same_depths_refl].
      destruct (sources_add _ _ _); [|discriminate]. simplify_eq. unfold ghost_read. destruct (g_nodes (with_edges _ _) !! n); split; reflexivity. }
    assert (Hcur : forall s s' a, current_owner s = Some (s', a) -> same_depths s s').
    { intros s s' a Hc. unfold current_owner in Hc.
      destruct (g_current_owner s), (g_current s); try discriminate; try (simplify_eq; apply same_depths_refl).
      destruct (g_nodes s !! _); [|discriminate]. destruct (n_scope _); [simplify_eq; split; reflexivity|].
      destruct (match n_kind _, n_mode _ with KMemo, Pure => true | _, _ => false end); simplify_eq; split; reflexivity. }
    assert (Hunlink : forall l s s' a, unlink l s = Some (s', a) -> same_depths s s').
    { intros l s s' a Hu. unfold unlink, m_bind, get_owner, put_owner, m_modify, m_return in Hu.
      destruct (g_owners s !! l_owner l); [|simplify_eq]. destruct (decide _); simplify_eq; split; reflexivity. }
    assert (Hrec : forall o y s s' a, record_error o y s = Some (s', a) -> same_depths s s').
    { intros o y s s' a Hr. unfold record_error, m_bind, get_owner, put_owner, m_modify in Hr.
      destruct (g_owners s !! o) as [x|]; [|simplify_eq]. destruct (o_sink x) as [p|]; [destruct (g_owners s !! p)|]; simplify_eq; split; reflexivity. }
    assert (Hghost : forall n s s' a, (let* d3 := get_node n in if decide (n_freshness d3 = Clean) then m_return tt else announce n) s = Some (s', a) -> same_depths s s').
    { intros n s s' a Hg. unfold m_bind, get_node, announce, modify_node, m_return in Hg.
      destruct (g_nodes s !! n); [|discriminate]. destruct (decide _); [simplify_eq; split; reflexivity|].
      destruct (f_unannounced _); [destruct (g_nodes s !! n); simplify_eq; split; reflexivity|simplify_eq; split; reflexivity]. }
    split.
    + intros e c s s' r H. cbn [exec] in H.
      destruct c; repeat match goal with
      | H0 : context [on_node] |- _ => unfold on_node in H0
      | H0 : context [on_value] |- _ => unfold on_value in H0
      | H0 : context [match eval_expr ?e ?p ?x with _ => _ end] |- _ => destruct (eval_expr e p x) as [[|]|]; try discriminate
      | H0 : (if decide _ then _ else stuck) _ = Some _ |- _ => destruct (decide _); try discriminate
      end; try (unfold m_return in H; simplify_eq; apply same_depths_refl); try (simplify_eq; apply same_depths_refl).
      * eapply (Dexec _ IH); exact H.
      * destruct (decide _); eapply (Dexec _ IH); exact H.
      * eapply (bind_depths_of H); [intros ??; intro H__d; eapply (Dread _ IH); exact H__d| ].
        intros o s1 E1. eapply bind_depths; [intros ?? E2; unfold note_host_read in E2; destruct (g_current s1), (g_current_owner s1); try destruct (decide _); unfold emit, m_modify in E2; simplify_eq; split; reflexivity|].
        intros [] s2 E2 H3. destruct o; [eapply (Dexec _ IH); exact H3|unfold m_return in H3; simplify_eq; apply same_depths_refl].
      * eapply (bind_depths_of H); [intros ??; intro H__d; eapply (Dread _ IH); exact H__d| ].
        intros o s1 E1. eapply bind_depths; [intros ?? E2; unfold note_host_read in E2; destruct (g_current s1), (g_current_owner s1); try destruct (decide _); unfold emit, m_modify in E2; simplify_eq; split; reflexivity|].
        intros [] s2 E2 H3. destruct o; eapply (Dexec _ IH); exact H3.
      * eapply (bind_depths_of H); [intros ??; intro H__d; eapply (Dread _ IH); exact H__d| ].
        intros o s1 E1. eapply bind_depths; [intros ?? E2; unfold note_host_read in E2; destruct (g_current s1), (g_current_owner s1); try destruct (decide _); unfold emit, m_modify in E2; simplify_eq; split; reflexivity|].
        intros [] s2 E2 H3. destruct o; [eapply (Dexec _ IH); exact H3|unfold m_return in H3; simplify_eq; apply same_depths_refl].
      * eapply (bind_depths_of H); [intros ??; intro H__d; eapply (Dwrite _ IH); exact H__d|intros [] ?? H__d; eapply (Dexec _ IH); exact H__d].
      * eapply (bind_depths_of H); [intros ?? E; unfold next_id in E; simplify_eq; split; reflexivity|intros ???; eapply bind_depths; [intros ?? E; unfold add_node, m_modify, ghost_publish in E; destruct (g_nodes _ !! _); simplify_eq; split; reflexivity|intros [] ?? H__d; eapply (Dexec _ IH); exact H__d]].
      * eapply (bind_depths_of H); [intros ?? E; unfold next_id in E; simplify_eq; split; reflexivity|intros ???; eapply bind_depths; [intros ?? E; unfold add_node, m_modify, ghost_publish in E; destruct (g_nodes _ !! _); simplify_eq; split; reflexivity|intros [] ??; eapply bind_depths; [intros ??; apply Hcur|intros [o|] ??; [eapply bind_depths; [intros ??; intro H__d; eapply (Dattach _ IH); exact H__d|intros [] ?? H__d; eapply (Dexec _ IH); exact H__d]|intros Hm; unfold m_return in Hm; simplify_eq; apply same_depths_refl]]]].
      * eapply (bind_depths_of H); [intros ?? E; unfold next_id in E; simplify_eq; split; reflexivity|intros ???; eapply bind_depths; [intros ?? E; unfold add_node, m_modify, ghost_publish in E; destruct (g_nodes _ !! _); simplify_eq; split; reflexivity|intros [] ??; eapply bind_depths; [intros ??; apply Hcur|intros [o|] ??; [|intros Hm; unfold m_return in Hm; simplify_eq; apply same_depths_refl]]]].
        eapply bind_depths; [intros ??; intro H__d; eapply (Dattach _ IH); exact H__d|intros [] ??].
        eapply bind_depths; [intros ?? E; unfold m_step in E; destruct (mark_dirty f _ _) eqn:Em; simplify_eq; by eapply marks_same_depths, mark_dirty_rtc|intros [] ??].
        eapply bind_depths; [intros ??; intro H__d; eapply (Drequest _ IH); exact H__d|intros [] ?? H__d; eapply (Dexec _ IH); exact H__d].
      * eapply (bind_depths_of H); [intros ??; apply Hcur|intros [o|] ??; [|intros Hm; unfold m_return in Hm; simplify_eq; apply same_depths_refl]].
        eapply bind_depths; [intros ?? E; unfold get_owner in E; destruct (g_owners _ !! o); simplify_eq; apply same_depths_refl|intros x ??].
        eapply bind_depths; [intros ?? E; unfold m_modify in E; simplify_eq; split; reflexivity|intros [] ??].
        eapply bind_depths; [|intros [] ?? H__d; eapply (Dexec _ IH); exact H__d].
        intros ??; destruct (o_disposed x); [|intros E; unfold put_owner, m_modify in E; simplify_eq; split; reflexivity].
        eapply bind_depths; [intros ??; eapply detached_depths; intros ???; intro H__d; eapply (Dcleanup _ IH); exact H__d|intros [] ?? H__d; eapply (Drequest _ IH); exact H__d].
      * eapply (bind_depths_of H); [intros ?? E; unfold get_node in E; destruct (g_nodes _ !! _); simplify_eq; apply same_depths_refl|intros d ?? ].
        destruct (n_kind d); try discriminate;
          (eapply bind_depths; [intros ??; intro H__d; eapply (Ddispose _ IH); exact H__d|intros [] ??];
           eapply bind_depths; [intros ?? E; destruct (n_disposed d); unfold emit, m_modify, m_return in E; simplify_eq; split; reflexivity|intros [] ?? H__d; eapply (Dexec _ IH); exact H__d]).
      * eapply (bind_depths_of H); [intros ?? E; unfold m_modify in E; simplify_eq; split; reflexivity|intros [] ?? ].
        eapply bind_depths; [intros ??; intro H__d; eapply (Dexec _ IH); exact H__d|intros o ??].
        eapply bind_depths; [intros ?? E; unfold m_modify in E; simplify_eq; split; reflexivity|intros [] ??].
        destruct o; [eapply bind_depths; [intros ??; intro H__d; eapply (Drequest _ IH); exact H__d|intros [] ?? H__d; eapply (Dexec _ IH); exact H__d]|intros Hm; unfold m_return in Hm; simplify_eq; apply same_depths_refl].
      * eapply (bind_depths_of H); [intros ??; apply log_label_depths|intros ??? ].
        eapply bind_depths; [intros ?? E; unfold emit, m_modify in E; simplify_eq; split; reflexivity|intros [] ?? H__d; eapply (Dexec _ IH); exact H__d].
    + intros n b s s' r H. cbn [read] in H. destruct b; [by eapply (Dread_node _ IH)|eapply untracked_depths; [intros ???; intro H__d; eapply (Dread_node _ IH); exact H__d|exact H]].
    + intros n s s' r H. cbn [read_node] in H.
      eapply (bind_depths_of H); [intros ?? E; unfold get_node in E; destruct (g_nodes _ !! n); simplify_eq; apply same_depths_refl|intros d ?? ].
      destruct (n_kind d); [eapply bind_depths; [intros ??; apply Htrack|intros [] ?? E; unfold m_return in E; simplify_eq; apply same_depths_refl]| |discriminate].
      eapply bind_depths; [|intros d' ?? E; destruct (n_status d'); unfold m_return in E; simplify_eq; apply same_depths_refl].
      intros ??; destruct (stale_live d).
      * eapply bind_depths; [intros ??; intro H__d; eapply (Drop _ IH); exact H__d|intros d' ??].
        eapply bind_depths; [|intros [] ?? E; unfold m_return in E; simplify_eq; apply same_depths_refl].
        intros ??; eapply bind_depths; [intros ??; apply Htrack|intros [] ??].
        eapply bind_depths; [intros ?? E; unfold get_node in E; destruct (g_nodes _ !! n); simplify_eq; apply same_depths_refl|intros d'' ??].
        destruct (stale_live d''); [|intros E; unfold m_return in E; simplify_eq; apply same_depths_refl].
        apply mark_reader_depths.
      * eapply bind_depths; [intros ??; apply Htrack|intros [] ?? E; unfold get_node in E; destruct (g_nodes _ !! n); simplify_eq; apply same_depths_refl].
    + intros n s s' r H. cbn [update_if_necessary] in H.
      eapply (bind_depths_of H); [intros ?? E; unfold get_node in E; destruct (g_nodes _ !! n); simplify_eq; apply same_depths_refl|intros d ?? ].
      destruct (n_kind d); [intros E; unfold m_return in E; simplify_eq; apply same_depths_refl| |intros E; unfold m_return in E; simplify_eq; apply same_depths_refl].
      destruct (stale_live d); [|intros E; unfold m_return in E; simplify_eq; apply same_depths_refl].
      eapply bind_depths; [intros ??; intro H__d; eapply (Drop _ IH); exact H__d|intros ??? E; unfold m_return in E; simplify_eq; apply same_depths_refl].
    + intros n s s' r H. cbn [refresh_or_pull] in H.
      eapply (bind_depths_of H); [intros ?? E; unfold m_get in E; simplify_eq; apply same_depths_refl|intros s0 ?? ].
      destruct (deferring s0); [|by apply (Dpull _ IH)].
      eapply bind_depths; [intros ??; intro H__d; eapply (Drefresh _ IH); exact H__d|intros [] ?? E; unfold get_node in E; destruct (g_nodes _ !! n); simplify_eq; apply same_depths_refl].
    + intros n s s' r H. cbn [pull] in H.
      eapply bind_inv in H as (s0 & [] & E0 & H). unfold m_modify in E0; simplify_eq.
      eapply bind_inv in H as (s1 & [] & Er & H).
      eapply bind_inv in H as (s2 & d & Eg & H). unfold get_node in Eg. destruct (g_nodes s1 !! n); [|discriminate]. injection Eg as <- <-.
      eapply bind_inv in H as (s3 & [] & E3 & H). unfold m_modify in E3; simplify_eq.
      rewrite bind_get in H.
      eapply bind_inv in H as (s4 & [] & Es & H). unfold m_return in H; simplify_eq.
      pose proof (Drefresh _ IH n _ _ _ Er) as [Hp1 Hf1].
      set (s3 := with_pull_depth (pred (g_pull_depth s1)) s1) in *.
      assert (Hp3 : g_pull_depth s3 = g_pull_depth s) by (unfold s3; simpl; rewrite Hp1; simpl; lia).
      assert (Hf3 : g_flushing s3 = g_flushing s) by (unfold s3; simpl; exact Hf1).
      destruct (bool_decide (g_pull_depth s3 = 0) && g_flush_owed s3).
      * pose proof (Dsettle _ IH _ _ _ Es) as [Hp4 Hf4]. split; congruence.
      * unfold m_return in Es; simplify_eq. split; assumption.
    + intros n s s' r H. cbn [refresh] in H.
      eapply (bind_depths_of H); [intros ?? E; unfold m_get in E; simplify_eq; apply same_depths_refl|intros s0 ?? ].
      destruct (decide (n ∈ g_checking s0)).
      { intros E. unfold m_step in E. destruct (mark_dirty f n _) eqn:Em; simplify_eq. by eapply marks_same_depths, mark_dirty_rtc. }
      destruct (decide (n ∈ g_running s0)); [intros E; unfold m_return in E; simplify_eq; apply same_depths_refl|].
      eapply bind_depths; [intros ?? E; unfold get_node in E; destruct (g_nodes _ !! n); simplify_eq; apply same_depths_refl|intros d ??].
      eapply bind_depths; [|intros [] ??].
      { intros ??; destruct (decide (n_freshness d = Check)); [|intros E; unfold m_return in E; simplify_eq; apply same_depths_refl].
        eapply bind_depths; [intros ?? E; unfold push_checking, m_modify in E; simplify_eq; split; reflexivity|intros [] ??].
        eapply bind_depths; [intros ??; intro H__d; eapply (Dwalk _ IH); exact H__d|intros [] ??].
        eapply bind_depths; [intros ?? E; unfold pop_checking, m_modify in E; simplify_eq; split; reflexivity|intros [] ??].
        eapply bind_depths; [intros ?? E; unfold get_node in E; destruct (g_nodes _ !! n); simplify_eq; apply same_depths_refl|intros d' ??].
        destruct (decide (n_freshness d' = Check)); [|intros E; unfold m_return in E; simplify_eq; apply same_depths_refl].
        eapply bind_depths; [intros ?? E; unfold m_modify in E; simplify_eq; split; reflexivity|intros [] ?? E; unfold modify_node in E; destruct (g_nodes _ !! n); simplify_eq; split; reflexivity]. }
      eapply bind_depths; [intros ?? E; unfold get_node in E; destruct (g_nodes _ !! n); simplify_eq; apply same_depths_refl|intros d' ??].
      destruct (decide (n_freshness d' = Dirty)); [by apply (Drecompute _ IH)|intros E; unfold m_return in E; simplify_eq; apply same_depths_refl].
    + intros n i s s' r H. cbn [resolve_walk] in H.
      eapply (bind_depths_of H); [intros ?? E; unfold get_node in E; destruct (g_nodes _ !! n); simplify_eq; apply same_depths_refl|intros d ?? ].
      eapply bind_depths; [intros ?? E; unfold m_get in E; simplify_eq; apply same_depths_refl|intros st0 ??].
      destruct (decide (n_freshness d = Check)); [|intros E; unfold m_return in E; simplify_eq; apply same_depths_refl].
      destruct (source_slots (sources_of (g_edges st0) n) !! i) as [[src _]|].
      * eapply bind_depths; [intros ??; intro H__d; eapply (Dupdate _ IH); exact H__d|intros [] ??].
        eapply bind_depths; [intros ?? E; unfold get_node in E; destruct (g_nodes _ !! n); simplify_eq; apply same_depths_refl|intros d' ??].
        eapply bind_depths; [intros ?? E; unfold get_node in E; destruct (g_nodes _ !! src); simplify_eq; apply same_depths_refl|intros d_src ??].
        eapply bind_depths; [intros ?? E; unfold m_get in E; simplify_eq; apply same_depths_refl|intros st1 ??].
        eapply bind_depths; [|intros [] ?? H__d; eapply (Dwalk _ IH); exact H__d].
        intros st_ [] E; destruct (decide _); [|unfold m_return in E; simplify_eq; apply same_depths_refl].
        unfold m_step in E. destruct (mark_dirty f n _) eqn:Em; simplify_eq. by eapply marks_same_depths, mark_dirty_rtc.
      * destruct (slot_holds stale_live st0 n); [by apply (Dwalk _ IH)|intros E; unfold m_return in E; simplify_eq; apply same_depths_refl].
    + intros n s s' r H. cbn [recompute] in H.
      eapply (bind_depths_of H); [intros ?? E; unfold get_node in E; destruct (g_nodes _ !! n); simplify_eq; apply same_depths_refl|intros d ?? ].
      destruct (n_scope d) as [o|]; [|by apply (Drun_memo _ IH)].
      eapply bind_depths; [intros ?? E; unfold modify_node in E; destruct (g_nodes _ !! n); simplify_eq; split; reflexivity|intros [] ??].
      eapply bind_depths; [intros ?? E; unfold m_modify in E; simplify_eq; split; reflexivity|intros [] ??].
      eapply bind_depths; [intros ??; intro H__d; eapply (Ddischarge _ IH); exact H__d|intros [] ??].
      eapply bind_depths; [intros ?? E; unfold get_node in E; destruct (g_nodes _ !! n); simplify_eq; apply same_depths_refl|intros d' ??].
      destruct (n_disposed d').
      { destruct (decide (n_freshness d' = Clean)); [intros E; unfold modify_node in E; destruct (g_nodes _ !! n); simplify_eq; split; reflexivity|intros E; unfold m_return in E; simplify_eq; apply same_depths_refl]. }
      destruct (decide (n_runs d' = n_runs d)); [by apply (Drun_memo _ IH)|].
      destruct (decide (n_freshness d' = Dirty)); [by apply (Drecompute _ IH)|intros E; unfold m_return in E; simplify_eq; apply same_depths_refl].
    + intros n s s' r H. cbn [run_memo] in H.
      eapply (bind_depths_of H); [intros ?? E; unfold get_node in E; destruct (g_nodes _ !! n); simplify_eq; apply same_depths_refl|intros d ?? ].
      eapply bind_depths; [intros ?? E; unfold modify_node in E; destruct (g_nodes _ !! n); simplify_eq; split; reflexivity|intros [] ??].
      eapply bind_depths; [intros ?? E; unfold edges, m_modify in E; simplify_eq; split; reflexivity|intros [] ??].
      eapply bind_depths; [intros ?? E; unfold m_modify in E; simplify_eq; split; reflexivity|intros [] ??].
      eapply bind_depths; [intros ?? E; unfold push_running, m_modify in E; simplify_eq; split; reflexivity|intros [] ??].
      eapply bind_depths; [intros ??; eapply hosted_depths; intros ???; intro H__d; eapply (Dexec _ IH); exact H__d|intros o ??].
      eapply bind_depths; [intros ?? E; unfold get_node in E; destruct (g_nodes _ !! n); simplify_eq; apply same_depths_refl|intros d1 ??].
      eapply bind_depths; [intros ?? E; destruct (if n_violated d1 then Fail pure_scope_failure else o); unfold modify_node, m_return in E; try destruct (g_nodes _ !! n); simplify_eq; split; reflexivity|intros [] ??].
      eapply bind_depths; [intros ?? E; unfold edges_opt in E; destruct (_ (g_edges _)); simplify_eq; split; reflexivity|intros [] ??].
      eapply bind_depths; [intros ?? E; unfold pop_running, m_modify in E; simplify_eq; split; reflexivity|intros [] ??].
      eapply bind_depths; [intros ?? E; destruct (if n_violated d1 then Fail pure_scope_failure else o); unfold modify_node, m_return in E; try destruct (g_nodes _ !! n); simplify_eq; split; reflexivity|intros [] ??].
      eapply bind_depths; [intros ?? E; unfold get_node in E; destruct (g_nodes _ !! n); simplify_eq; apply same_depths_refl|intros d2 ??].
      eapply bind_depths; [intros ?? E; destruct (decide _); [unfold m_return in E; simplify_eq; apply same_depths_refl|destruct (n_comparer d2); unfold m_bind, modify_node, m_return in E; try destruct (g_nodes _ !! n); simplify_eq; split; reflexivity]|intros mv ??].
      eapply bind_depths; [intros ?? E; unfold m_get in E; simplify_eq; apply same_depths_refl|intros st0 ??].
      eapply bind_depths; [intros ?? E; unfold m_modify, ghost_run_end in E; destruct (g_nodes _ !! n); simplify_eq; split; reflexivity|intros [] ??].
      eapply bind_depths; [|intros [] ??; intro Hg_; eapply Hghost; exact Hg_].
      intros ??; destruct mv; [|intros E; unfold m_return in E; simplify_eq; apply same_depths_refl].
      eapply bind_depths; [intros st_ [] E; unfold m_step in E; destruct (notify_dirty_except f n (g_current st0) _) eqn:Em; simplify_eq; by eapply marks_same_depths, (any_mark_rtc f n (g_current st0)), or_intror, or_intror, or_intror, or_intror|intros [] ??].
      eapply bind_depths; [intros ?? E; unfold m_get in E; simplify_eq; apply same_depths_refl|intros st1 ??].
      destruct (_ && _); [intros E; unfold m_modify in E; simplify_eq; split; reflexivity|intros E; unfold m_return in E; simplify_eq; apply same_depths_refl].
    + intros n s s' r H. cbn [execute_effect] in H.
      eapply (bind_depths_of H); [intros ?? E; unfold get_node in E; destruct (g_nodes _ !! n); simplify_eq; apply same_depths_refl|intros d ?? ].
      destruct (n_disposed d); [intros E; unfold modify_node in E; destruct (g_nodes _ !! n); simplify_eq; split; reflexivity|].
      eapply bind_depths; [|intros [] ??].
      { intros ??; destruct (decide (n_freshness d = Check)); [|intros E; unfold m_return in E; simplify_eq; apply same_depths_refl].
        eapply bind_depths; [intros ??; intro H__d; eapply (Dwalk _ IH); exact H__d|intros [] ??].
        eapply bind_depths; [intros ?? E; unfold get_node in E; destruct (g_nodes _ !! n); simplify_eq; apply same_depths_refl|intros d' ??].
        destruct (decide (n_freshness d' = Check)); [|intros E; unfold m_return in E; simplify_eq; apply same_depths_refl].
        eapply bind_depths; [intros ?? E; unfold m_modify in E; simplify_eq; split; reflexivity|intros [] ?? E; unfold modify_node in E; destruct (g_nodes _ !! n); simplify_eq; split; reflexivity]. }
      eapply bind_depths; [intros ?? E; unfold get_node in E; destruct (g_nodes _ !! n); simplify_eq; apply same_depths_refl|intros d' ??].
      eapply bind_depths; [intros ?? E; unfold modify_node in E; destruct (g_nodes _ !! n); simplify_eq; split; reflexivity|intros [] ??].
      destruct (decide (n_freshness d' = Dirty)); [by apply (Drun_effect _ IH)|intros E; unfold m_return in E; simplify_eq; apply same_depths_refl].
    + intros n s s' r H. cbn [run_effect] in H.
      eapply (bind_depths_of H); [intros ?? E; unfold push_running, m_modify in E; simplify_eq; split; reflexivity|intros [] ?? ].
      eapply bind_depths; [intros ?? E; unfold edges, m_modify in E; simplify_eq; split; reflexivity|intros [] ??].
      eapply bind_depths; [intros ?? E; unfold m_modify in E; simplify_eq; split; reflexivity|intros [] ??].
      eapply bind_depths; [intros ?? E; unfold get_node in E; destruct (g_nodes _ !! n); simplify_eq; apply same_depths_refl|intros d ??].
      eapply bind_depths; [|intros [] ??].
      { intros ??; destruct (n_scope d) as [o|]; [|intros E; unfold m_return in E; simplify_eq; apply same_depths_refl].
        eapply bind_depths; [intros ?? E; unfold modify_node in E; destruct (g_nodes _ !! n); simplify_eq; split; reflexivity|intros [] ??].
        eapply bind_depths; [intros ??; intro H__d; eapply (Ddischarge _ IH); exact H__d|intros [] ?? E; unfold modify_node in E; destruct (g_nodes _ !! n); simplify_eq; split; reflexivity]. }
      eapply bind_depths; [intros ?? E; unfold get_node in E; destruct (g_nodes _ !! n); simplify_eq; apply same_depths_refl|intros d' ??].
      eapply bind_depths; [|intros [] ?? E; unfold pop_running, m_modify in E; simplify_eq; split; reflexivity].
      intros ??; destruct (n_disposed d'); [intros E; unfold m_return in E; simplify_eq; apply same_depths_refl|by apply (Dbody _ IH)].
    + intros n s s' r H. cbn [run_effect_body] in H.
      eapply (bind_depths_of H); [intros ?? E; unfold get_node in E; destruct (g_nodes _ !! n); simplify_eq; apply same_depths_refl|intros d ?? ].
      eapply bind_depths; [intros ?? E; unfold modify_node in E; destruct (g_nodes _ !! n); simplify_eq; split; reflexivity|intros [] ??].
      eapply bind_depths; [intros ?? E; unfold emit, m_modify in E; simplify_eq; split; reflexivity|intros [] ??].
      eapply bind_depths; [intros ??; eapply hosted_depths; intros ???; intro H__d; eapply (Dexec _ IH); exact H__d|intros o ??].
      eapply bind_depths; [intros ?? E; unfold get_node in E; destruct (g_nodes _ !! n); simplify_eq; apply same_depths_refl|intros d' ??].
      eapply bind_depths; [intros ?? E; unfold edges_opt in E; destruct (_ (g_edges _)); simplify_eq; split; reflexivity|intros [] ??].
      destruct o; [intros E; unfold m_return in E; simplify_eq; apply same_depths_refl|].
      eapply bind_depths; [intros ?? E; unfold modify_node in E; destruct (g_nodes _ !! n); simplify_eq; split; reflexivity|intros [] ?? E; unfold emit, m_modify in E; simplify_eq; split; reflexivity].
    + intros o s s' r H. cbn [discharge] in H.
      eapply (bind_depths_of H); [intros ??; eapply detached_depths; intros ???; intro H__d; eapply (Ddispose_scope _ IH); exact H__d|intros [] ?? ].
      eapply bind_depths; [intros ?? E; unfold m_get in E; simplify_eq; apply same_depths_refl|intros st0 ??].
      destruct (_ && _); [intros E; unfold m_modify in E; simplify_eq; split; reflexivity|intros E; unfold m_return in E; simplify_eq; apply same_depths_refl].
    + intros o s s' r H. cbn [retire] in H.
      eapply (bind_depths_of H); [|intros [] ?? ].
      { intros ?? E. eapply detached_depths; [|exact E]. intros ???.
        eapply bind_depths; [intros ?? E1; unfold get_owner in E1; destruct (g_owners _ !! o); simplify_eq; apply same_depths_refl|intros x ??].
        destruct (o_disposed x); [intros E1; unfold m_return in E1; simplify_eq; apply same_depths_refl|].
        eapply bind_depths; [intros ?? E1; unfold put_owner, m_modify in E1; simplify_eq; split; reflexivity|intros [] ?? H__d; eapply (Ddispose_scope _ IH); exact H__d]. }
      eapply bind_depths; [intros ?? E; unfold m_get in E; simplify_eq; apply same_depths_refl|intros st0 ??].
      destruct (live_queue st0); [by apply (Drequest _ IH)|intros E; unfold m_return in E; simplify_eq; apply same_depths_refl].
    + intros o s s' r H. cbn [dispose_scope] in H.
      eapply (bind_depths_of H); [intros ?? E; unfold get_owner in E; destruct (g_owners _ !! o); simplify_eq; apply same_depths_refl|intros x ?? ].
      eapply bind_depths; [intros ?? E; unfold put_owner, m_modify in E; simplify_eq; split; reflexivity|intros [] ?? H__d; eapply (Dteardown _ IH); exact H__d].
    + intros o ls cs s s' r H. cbn [teardown] in H.
      destruct ls as [|l ls'], cs as [|[[b e] a] cs'].
      * unfold m_return in H. simplify_eq. apply same_depths_refl.
      * eapply (bind_depths_of H); [intros ??; intro H__d; eapply (Dcleanup _ IH); exact H__d|intros [] ?? H__d; eapply (Dteardown _ IH); exact H__d].
      * eapply (bind_depths_of H); [intros ??; intro H__d; eapply (Drelease _ IH); exact H__d|intros [] ?? H__d; eapply (Dteardown _ IH); exact H__d].
      * destruct (decide _); [eapply (bind_depths_of H); [intros ??; intro H__d; eapply (Drelease _ IH); exact H__d|intros [] ?? H__d; eapply (Dteardown _ IH); exact H__d]|eapply (bind_depths_of H); [intros ??; intro H__d; eapply (Dcleanup _ IH); exact H__d|intros [] ?? H__d; eapply (Dteardown _ IH); exact H__d]].
    + intros o b e s s' r H. cbn [run_cleanup] in H.
      eapply (bind_depths_of H); [intros ?? E; unfold get_owner in E; destruct (g_owners _ !! o); simplify_eq; apply same_depths_refl|intros x ?? ].
      eapply bind_depths; [intros ?? E; unfold emit, m_modify in E; simplify_eq; split; reflexivity|intros [] ??].
      eapply bind_depths; [intros ??; intro H__d; eapply (Dexec _ IH); exact H__d|intros [|] ??].
      * intros E; unfold m_return in E; simplify_eq; apply same_depths_refl.
      * eapply bind_depths; [intros ??; apply Hrec|intros [] ?? E; unfold emit, m_modify in E; simplify_eq; split; reflexivity].
    + intros n s s' r H. cbn [release] in H.
      eapply (bind_depths_of H); [intros ?? E; unfold modify_node in E; destruct (g_nodes _ !! n); simplify_eq; split; reflexivity|intros [] ?? H__d; eapply (Ddispose _ IH); exact H__d].
    + intros n s s' r H. cbn [dispose_node] in H.
      eapply (bind_depths_of H); [intros ?? E; unfold get_node in E; destruct (g_nodes _ !! n); simplify_eq; apply same_depths_refl|intros d ?? ].
      destruct (n_disposed d); [intros E; unfold m_return in E; simplify_eq; apply same_depths_refl|].
      eapply bind_depths; [intros ?? E; unfold modify_node in E; destruct (g_nodes _ !! n); simplify_eq; split; reflexivity|intros [] ??].
      eapply bind_depths; [intros ??; destruct (n_link d); [apply Hunlink|intros E; unfold m_return in E; simplify_eq; apply same_depths_refl]|intros [] ??].
      eapply bind_depths; [intros ?? E; unfold edges_opt in E; destruct (sources_clear _ _); simplify_eq; split; reflexivity|intros [] ??].
      destruct (n_scope d); [by apply (Dretire _ IH)|intros E; unfold m_return in E; simplify_eq; apply same_depths_refl].
    + intros n o s s' r H. cbn [attach] in H.
      eapply (bind_depths_of H); [intros ?? E; unfold get_owner in E; destruct (g_owners _ !! o); simplify_eq; apply same_depths_refl|intros x ?? ].
      destruct (o_disposed x).
      * eapply bind_depths; [intros ??; intro H__d; eapply (Drelease _ IH); exact H__d|intros [] ?? E; unfold modify_node in E; destruct (g_nodes _ !! n); simplify_eq; split; reflexivity].
      * eapply bind_depths; [intros ?? E; unfold put_owner, m_modify in E; simplify_eq; split; reflexivity|intros [] ?? E; unfold modify_node in E; destruct (g_nodes _ !! n); simplify_eq; split; reflexivity].
    + intros n v s s' r H. cbn [write_signal] in H.
      eapply (bind_depths_of H); [intros ?? E; unfold get_node in E; destruct (g_nodes _ !! n); simplify_eq; apply same_depths_refl|intros d ?? ].
      destruct (n_kind d); [|discriminate|discriminate].
      destruct (decide (n_value d = v)); [intros E; unfold m_return in E; simplify_eq; apply same_depths_refl|].
      eapply bind_depths; [intros ?? E; unfold modify_node in E; destruct (g_nodes _ !! n); simplify_eq; split; reflexivity|intros [] ??].
      eapply bind_depths; [intros ?? E; unfold m_modify, ghost_publish in E; destruct (g_nodes _ !! n); simplify_eq; split; reflexivity|intros [] ??].
      eapply bind_depths; [intros ?? E; unfold m_step in E; destruct (notify_dirty f n _) eqn:Em; simplify_eq; by eapply marks_same_depths, (any_mark_rtc f n None), or_intror, or_intror, or_intror, or_introl|intros [] ?? H__d; eapply (Drequest _ IH); exact H__d].
    + intros s s' r H. cbn [request_flush] in H.
      eapply (bind_depths_of H); [intros ?? E; unfold m_get in E; simplify_eq; apply same_depths_refl|intros st0 ?? ].
      destruct (_ && _); [|intros E; unfold m_return in E; simplify_eq; apply same_depths_refl].
      destruct (outside_bodies st0); [by apply (Dflush _ IH)|].
      destruct (g_flushing st0); [intros E; unfold m_return in E; simplify_eq; apply same_depths_refl|intros E; unfold m_modify in E; simplify_eq; split; reflexivity].
    + intros s s' r H. cbn [settle_flush] in H.
      eapply (bind_depths_of H); [intros ?? E; unfold m_get in E; simplify_eq; apply same_depths_refl|intros st0 ?? ].
      destruct (_ && _); [by apply (Dflush _ IH)|intros E; unfold m_return in E; simplify_eq; apply same_depths_refl].
    + intros s s' r H. cbn [run_flush] in H.
      rewrite bind_get in H.
      destruct (g_flushing s) eqn:Ef; [unfold m_return in H; simplify_eq; apply same_depths_refl|].
      eapply bind_inv in H as (st1 & [] & E1 & H). unfold m_modify in E1; simplify_eq.
      eapply bind_inv in H as (s2 & [] & Ed & H). unfold m_modify in H; simplify_eq.
      pose proof (Ddrain _ IH _ _ _ Ed) as [Hp2 _]. split; [exact Hp2|simpl; symmetry; exact Ef].
    + intros s s' r H. cbn [drain] in H.
      eapply (bind_depths_of H); [intros ?? E; unfold m_get in E; simplify_eq; apply same_depths_refl|intros st0 ?? ].
      destruct (g_queue st0 !! g_queue_head st0); [|intros E; unfold m_modify in E; simplify_eq; split; reflexivity].
      eapply bind_depths; [intros ?? E; unfold m_modify in E; simplify_eq; split; reflexivity|intros [] ??].
      eapply bind_depths; [intros ??; intro H__d; eapply (Dexecute _ IH); exact H__d|intros [] ?? H__d; eapply (Ddrain _ IH); exact H__d].
Qed.
References

B1. Flushing implies no flush owed and Deferring equivalence

✓ No axioms

Whenever g_flushing is true under sched_inv (and hence after any run_top from init), g_flush_owed is false.

run_flush clears g_flush_owed to false before setting g_flushing := true, and request_flush, run_memo, and discharge only set g_flush_owed := true when g_flushing = false. Consequently sc_owed in sched_inv rules out any owed flush while a flush is running.

Arguments

x
optional in-progress effect for sched_inv.
s
any state satisfying sched_inv x s.
f : nat
fuel for run_top.
fl : State.flags
initial graph flags.
c : cmd
whole program executed from init fl.
s' : State.state
final state after run_top.
r : outcome
outcome of run_top.
Theorem flushing_implies_no_flush_owed f fl c s' r :
  (forall x s, sched_inv x s -> g_flushing s = true -> g_flush_owed s = false) /\
  (run_top f c (init fl) = Some (s', r) -> g_flushing s' = true -> g_flush_owed s' = false).

Arguments

x
optional in-progress effect for sched_inv.
s
any state satisfying sched_inv x s.
f : nat
fuel for run_top.
fl : State.flags
initial graph flags.
c : cmd
whole program executed from init fl.
s' : State.state
final state after run_top.
r : outcome
outcome of run_top.
Qed
Proof.
  assert (Hinv : forall x s, sched_inv x s -> g_flushing s = true -> g_flush_owed s = false).
  { intros x s (_ & [_ Howed _] & _) Hfl.
    destruct (g_flush_owed s) eqn:Eo; [|reflexivity].
    pose proof (Howed eq_refl). congruence. }
  split; [exact Hinv|].
  intros Hrun Hfl.
  exact (Hinv None s' (sched_inv_run_top f c _ s' r (sched_inv_init fl) Hrun) Hfl).
Qed.
References

Graph.Deferring with g_flushing included: defers nested flushes when a flush is active, a pull is active, or a computation body is current.

Definition deferring_b1 (s : state) : bool := g_flushing s || deferring s.
References

Engine interpreter identical to `Engine.v` except `b1_refresh_or_pull` uses `deferring_b1`.

Fixpoint b1_exec (fuel : nat) (e : env) (c : cmd) {struct fuel} : M outcome :=
  match fuel with
  | 0 => stuck
  | S f =>
    match c with
    | CRet x => on_value e x (fun v => m_return (Ok v))
    | CThrow y => m_return (Fail y)
    | CLet x v k => on_value e v (fun z => b1_exec f ((x, z) :: e) k)
    | CIf v a b => on_value e v (fun z => if decide (z = 0%Z) then b1_exec f e b else b1_exec f e a)
    | CRead x n k =>
      on_node e n (fun t =>
        let* o := b1_read f t true in
        let* _ := note_host_read t o in
        match o with Ok v => b1_exec f ((x, v) :: e) k | Fail y => m_return (Fail y) end)
    | CTryRead x ok n k =>
      on_node e n (fun t =>
        let* o := b1_read f t true in
        let* _ := note_host_read t o in
        match o with
        | Ok v => b1_exec f ((ok, 1%Z) :: (x, v) :: e) k
        | Fail y => b1_exec f ((ok, 0%Z) :: (x, y) :: e) k
        end)
    | CReadUntracked x n k =>
      on_node e n (fun t =>
        let* o := b1_read f t false in
        let* _ := note_host_read t o in
        match o with Ok v => b1_exec f ((x, v) :: e) k | Fail y => m_return (Fail y) end)
    | CWrite n v k =>
      on_node e n (fun t => on_value e v (fun z => let* _ := b1_write_signal f t z in b1_exec f e k))
    | CSignal x v k =>
      on_value e v (fun z =>
        let* n := next_id in
        let* _ := add_node n (signal_node z) in
        b1_exec f ((x, Z.of_nat n) :: e) k)
    | CMemo x m q b k =>
      let* n := next_id in
      let e' := (x, Z.of_nat n) :: e in
      let* _ := add_node n (memo_node m q b e') in
      let* owner := current_owner in
      match owner with
      | None => m_return (Fail pure_scope_failure)
      | Some o => let* _ := b1_attach f n o in b1_exec f e' k
      end
    | CEffect x b k =>
      let* n := next_id in
      let e' := (x, Z.of_nat n) :: e in
      let* _ := add_node n (effect_node b e') in
      let* owner := current_owner in
      match owner with
      | None => m_return (Fail pure_scope_failure)
      | Some o =>
        let* _ := b1_attach f n o in
        let* _ := m_step (mark_dirty f n) in
        let* _ := b1_request_flush f in
        b1_exec f e' k
      end
    | CCleanup b k =>
      let* owner := current_owner in
      match owner with
      | None => m_return (Fail pure_scope_failure)
      | Some o =>
        let* x := get_owner o in
        let* _ := m_modify (ghost_registered (o_host x)) in
        let* _ :=
          if o_disposed x then let* _ := detached o (b1_run_cleanup f o b e) in b1_request_flush f
          else put_owner o (owner_with_cleanup b e x) in
        b1_exec f e k
      end
    | CDispose n k =>
      on_node e n (fun t =>
        let* d := get_node t in
        match n_kind d with
        | KSignal => stuck
        | _ =>
          let* _ := b1_dispose_node f t in
          let* _ := if n_disposed d then m_return tt else emit (EvDispose t) in
          b1_exec f e k
        end)
    | CBatch b k =>
      let* _ := m_modify (fun s => with_batch_depth (S (g_batch_depth s)) s) in
      let* r := b1_exec f e b in
      let* _ := m_modify (fun s => with_batch_depth (pred (g_batch_depth s)) s) in
      match r with
      | Ok _ => let* _ := b1_request_flush f in b1_exec f e k
      | Fail y => m_return (Fail y)
      end
    | CLog v k =>
      on_value e v (fun z => let* l := log_label in let* _ := emit (EvLog l z) in b1_exec f e k)
    end
  end

with b1_read (fuel : nat) (n : node_id) (tracked : bool) {struct fuel} : M outcome :=
  match fuel with
  | 0 => stuck
  | S f => if tracked then b1_read_node f n else untracked (b1_read_node f n)
  end

with b1_read_node (fuel : nat) (n : node_id) {struct fuel} : M outcome :=
  match fuel with
  | 0 => stuck
  | S f =>
    let* d := get_node n in
    match n_kind d with
    | KSignal => let* _ := track n in m_return (Ok (n_value d))
    | KEffect => stuck
    | KMemo =>
      let* d' :=
        if stale_live d then
          let* d' := b1_refresh_or_pull f n in
          let* _ :=
            let* _ := track n in
            let* d'' := get_node n in
            if stale_live d'' then mark_reader f else m_return tt in
          m_return d'
        else let* _ := track n in get_node n in
      match n_status d' with
      | StError => m_return (Fail (default 0%Z (n_failure d')))
      | _ => m_return (Ok (n_value d'))
      end
    end
  end

with b1_update_if_necessary (fuel : nat) (n : node_id) {struct fuel} : M unit :=
  match fuel with
  | 0 => stuck
  | S f =>
    let* d := get_node n in
    match n_kind d with
    | KMemo => if stale_live d then let* _ := b1_refresh_or_pull f n in m_return tt else m_return tt
    | _ => m_return tt
    end
  end

with b1_refresh_or_pull (fuel : nat) (n : node_id) {struct fuel} : M node :=
  match fuel with
  | 0 => stuck
  | S f =>
    let* s := m_get in
    if deferring_b1 s then let* _ := b1_refresh f n in get_node n else b1_pull f n
  end

with b1_pull (fuel : nat) (n : node_id) {struct fuel} : M node :=
  match fuel with
  | 0 => stuck
  | S f =>
    let* _ := m_modify (fun s => with_pull_depth (S (g_pull_depth s)) s) in
    let* _ := b1_refresh f n in
    let* d := get_node n in
    let* _ := m_modify (fun s => with_pull_depth (pred (g_pull_depth s)) s) in
    let* s := m_get in
    let* _ := if bool_decide (g_pull_depth s = 0%nat) && g_flush_owed s then b1_settle_flush f else m_return tt in
    m_return d
  end

with b1_refresh (fuel : nat) (n : node_id) {struct fuel} : M unit :=
  match fuel with
  | 0 => stuck
  | S f =>
    let* s0 := m_get in
    if decide (n ∈ g_checking s0) then m_step (mark_dirty f n)
    else if decide (n ∈ g_running s0) then m_return tt
    else
      let* d := get_node n in
      let* _ :=
        if decide (n_freshness d = Check) then
          let* _ := push_checking n in
          let* _ := b1_resolve_walk f n 0 in
          let* _ := pop_checking in
          let* d' := get_node n in
          if decide (n_freshness d' = Check) then
            let* _ := m_modify (ghost_resolved n) in
            modify_node n (fun x => node_with_unannounced false (node_with_freshness Clean x))
          else m_return tt
        else m_return tt in
      let* d' := get_node n in
      if decide (n_freshness d' = Dirty) then b1_recompute f n else m_return tt
  end

with b1_resolve_walk (fuel : nat) (n : node_id) (i : nat) {struct fuel} : M unit :=
  match fuel with
  | 0 => stuck
  | S f =>
    let* d := get_node n in
    let* s := m_get in
    if decide (n_freshness d = Check) then
      match source_slots (sources_of (g_edges s) n) !! i with
      | Some (src, _) =>
        let* _ := b1_update_if_necessary f src in
        let* d' := get_node n in
        let* d_src := get_node src in
        let* s1 := m_get in
        let* _ :=
          if decide (n_freshness d' = Check /\ stale_live d_src = true /\ (src ∈ g_running s1 \/ src ∈ g_checking s1))
          then m_step (mark_dirty f n) else m_return tt in
        b1_resolve_walk f n (S i)
      | None => if slot_holds stale_live s n then b1_resolve_walk f n 0 else m_return tt
      end
    else m_return tt
  end

with b1_recompute (fuel : nat) (n : node_id) {struct fuel} : M unit :=
  match fuel with
  | 0 => stuck
  | S f =>
    let* d := get_node n in
    match n_scope d with
    | None => b1_run_memo f n
    | Some o =>
      let* _ := modify_node n (node_with_freshness Clean) in
      let* _ := m_modify (ghost_dirty_cleared n (n_runs d)) in
      let* _ := b1_discharge f o in
      let* d' := get_node n in
      if n_disposed d' then
        (if decide (n_freshness d' = Clean) then modify_node n (node_with_unannounced false) else m_return tt)
      else if decide (n_runs d' = n_runs d) then b1_run_memo f n
      else if decide (n_freshness d' = Dirty) then b1_recompute f n
      else m_return tt
    end
  end

with b1_run_memo (fuel : nat) (n : node_id) {struct fuel} : M unit :=
  match fuel with
  | 0 => stuck
  | S f =>
    let* d := get_node n in
    let previous := n_value d in
    let previous_status := n_status d in
    let previous_failure := n_failure d in
    let* _ := modify_node n (fun x =>
      node_with_runs (S (n_runs x)) (node_with_failure None (node_with_status StNone
        (node_with_unannounced false (node_with_freshness Clean x))))) in
    let* _ := edges (sources_begin_run n) in
    let* _ := m_modify (ghost_run_start n) in
    let* _ := push_running n in
    let body_env := if n_published d then (prev_var, previous) :: n_env d else without_prev (n_env d) in
    let* r := hosted n (b1_exec f body_env (n_body d)) in
    let* d1 := get_node n in
    let result := if n_violated d1 then Fail pure_scope_failure else r in
    let* _ :=
      match result with
      | Ok v => modify_node n (fun x => node_with_published true (node_with_value v x))
      | Fail _ => m_return tt
      end in
    let* _ := edges_opt (if n_disposed d1 then sources_clear n else sources_end_run n) in
    let* _ := pop_running in
    let* _ :=
      match result with
      | Ok _ => m_return tt
      | Fail y => modify_node n (fun x => node_with_violated false (node_with_status StError (node_with_failure (Some y) x)))
      end in
    let* d2 := get_node n in
    let* moved :=
      if decide (n_status d2 <> previous_status \/ n_failure d2 <> previous_failure) then m_return true
      else
        match n_comparer d2 with
        | CmpEq => m_return (bool_decide (n_value d2 <> previous))
        | CmpNever => m_return true
        | CmpThrows y =>
          let* _ := modify_node n (fun x => node_with_status StError (node_with_failure (Some y) (node_with_value previous x))) in
          m_return true
        end in
    let* s := m_get in
    let* _ := m_modify (ghost_run_end n (g_current s) moved) in
    let* _ :=
      if moved then
        let* _ := m_step (notify_dirty_except f n (g_current s)) in
        let* s' := m_get in
        if bool_decide (g_batch_depth s' = 0%nat) && negb (g_flushing s') && live_queue s'
        then m_modify (with_flush_owed true) else m_return tt
      else m_return tt in
    let* d3 := get_node n in
    if decide (n_freshness d3 = Clean) then m_return tt else announce n
  end

with b1_execute_effect (fuel : nat) (n : node_id) {struct fuel} : M unit :=
  match fuel with
  | 0 => stuck
  | S f =>
    let* d := get_node n in
    if n_disposed d then modify_node n (node_with_queued false)
    else
      let* _ :=
        if decide (n_freshness d = Check) then
          let* _ := b1_resolve_walk f n 0 in
          let* d' := get_node n in
          if decide (n_freshness d' = Check) then
            let* _ := m_modify (ghost_resolved n) in modify_node n (node_with_freshness Clean)
          else m_return tt
        else m_return tt in
      let* d' := get_node n in
      let* _ := modify_node n (fun x => node_with_freshness Clean (node_with_queued false x)) in
      if decide (n_freshness d' = Dirty) then b1_run_effect f n else m_return tt
  end

with b1_run_effect (fuel : nat) (n : node_id) {struct fuel} : M unit :=
  match fuel with
  | 0 => stuck
  | S f =>
    let* _ := push_running n in
    let* _ := edges (sources_begin_run n) in
    let* _ := m_modify (ghost_effect_start n) in
    let* d := get_node n in
    let* _ :=
      match n_scope d with
      | Some o =>
        let* _ := modify_node n (node_with_in_cleanup true) in
        let* _ := b1_discharge f o in
        modify_node n (node_with_in_cleanup false)
      | None => m_return tt
      end in
    let* d' := get_node n in
    let* _ := if n_disposed d' then m_return tt else b1_run_effect_body f n in
    pop_running
  end

with b1_run_effect_body (fuel : nat) (n : node_id) {struct fuel} : M unit :=
  match fuel with
  | 0 => stuck
  | S f =>
    let* d := get_node n in
    let* _ := modify_node n (fun x => node_with_runs (S (n_runs x)) (node_with_failure None (node_with_status StNone x))) in
    let* _ := emit (EvRun n) in
    let* r := hosted n (b1_exec f (n_env d) (n_body d)) in
    let* d' := get_node n in
    let* _ := edges_opt (if n_disposed d' then sources_clear n else sources_end_run n) in
    match r with
    | Ok _ => m_return tt
    | Fail y =>
      let* _ := modify_node n (fun x => node_with_status StError (node_with_failure (Some y) x)) in
      emit (EvFail n y)
    end
  end

with b1_discharge (fuel : nat) (o : owner_id) {struct fuel} : M unit :=
  match fuel with
  | 0 => stuck
  | S f =>
    let* _ := detached o (b1_dispose_scope f o) in
    let* s := m_get in
    if bool_decide (g_batch_depth s = 0%nat) && negb (g_flushing s) && live_queue s
    then m_modify (with_flush_owed true) else m_return tt
  end

with b1_retire (fuel : nat) (o : owner_id) {struct fuel} : M unit :=
  match fuel with
  | 0 => stuck
  | S f =>
    let* _ := detached o (
      let* x := get_owner o in
      if o_disposed x then m_return tt
      else let* _ := put_owner o (owner_retired x) in b1_dispose_scope f o) in
    let* s := m_get in
    if live_queue s then b1_request_flush f else m_return tt
  end

with b1_dispose_scope (fuel : nat) (o : owner_id) {struct fuel} : M unit :=
  match fuel with
  | 0 => stuck
  | S f =>
    let* x := get_owner o in
    let* _ := put_owner o (owner_detached x) in
    b1_teardown f o (rev (o_children x)) (owner_cleanups x)
  end

with b1_teardown (fuel : nat) (o : owner_id) (ls : list link) (cs : list (cmd * env * nat)) {struct fuel} : M unit :=
  match fuel with
  | 0 => stuck
  | S f =>
    match ls, cs with
    | [], [] => m_return tt
    | l :: ls', [] => let* _ := b1_release f (l_child l) in b1_teardown f o ls' []
    | [], (b, e, _) :: cs' => let* _ := b1_run_cleanup f o b e in b1_teardown f o [] cs'
    | l :: ls', (b, e, a) :: cs' =>
      if decide (a < l_acquired l)%nat then
        let* _ := b1_release f (l_child l) in b1_teardown f o ls' cs
      else let* _ := b1_run_cleanup f o b e in b1_teardown f o ls cs'
    end
  end

with b1_run_cleanup (fuel : nat) (o : owner_id) (b : cmd) (e : env) {struct fuel} : M unit :=
  match fuel with
  | 0 => stuck
  | S f =>
    let* x := get_owner o in
    let* _ := emit (EvCleanup (o_host x)) in
    let* r := b1_exec f e b in
    match r with
    | Ok _ => m_return tt
    | Fail y => let* _ := record_error o y in emit (EvFail (o_host x) y)
    end
  end

with b1_release (fuel : nat) (n : node_id) {struct fuel} : M unit :=
  match fuel with
  | 0 => stuck
  | S f => let* _ := modify_node n (node_with_link None) in b1_dispose_node f n
  end

with b1_dispose_node (fuel : nat) (n : node_id) {struct fuel} : M unit :=
  match fuel with
  | 0 => stuck
  | S f =>
    let* d := get_node n in
    if n_disposed d then m_return tt
    else
      let* _ := modify_node n (fun x => node_with_link None (node_with_disposed true x)) in
      let* _ := match n_link d with Some l => unlink l | None => m_return tt end in
      let* _ := edges_opt (sources_clear n) in
      match n_scope d with Some o => b1_retire f o | None => m_return tt end
  end

with b1_attach (fuel : nat) (n : node_id) (o : owner_id) {struct fuel} : M unit :=
  match fuel with
  | 0 => stuck
  | S f =>
    let* x := get_owner o in
    if o_disposed x then
      let* _ := b1_release f n in
      modify_node n (node_with_link (Some {| l_child := n; l_owner := o; l_generation := None; l_acquired := 0 |}))
    else
      let l := {| l_child := n; l_owner := o; l_generation := Some (o_generation x); l_acquired := o_acquisitions x |} in
      let* _ := put_owner o (owner_with_child l x) in
      modify_node n (node_with_link (Some l))
  end

with b1_write_signal (fuel : nat) (n : node_id) (v : val) {struct fuel} : M unit :=
  match fuel with
  | 0 => stuck
  | S f =>
    let* d := get_node n in
    match n_kind d with
    | KSignal =>
      if decide (n_value d = v) then m_return tt
      else
        let* _ := modify_node n (node_with_value v) in
        let* _ := m_modify (ghost_publish n) in
        let* _ := m_step (notify_dirty f n) in
        b1_request_flush f
    | _ => stuck
    end
  end

with b1_request_flush (fuel : nat) {struct fuel} : M unit :=
  match fuel with
  | 0 => stuck
  | S f =>
    let* s := m_get in
    if live_queue s && bool_decide (g_batch_depth s = 0%nat) then
      if outside_bodies s then b1_run_flush f
      else if g_flushing s then m_return tt
      else m_modify (with_flush_owed true)
    else m_return tt
  end

with b1_settle_flush (fuel : nat) {struct fuel} : M unit :=
  match fuel with
  | 0 => stuck
  | S f =>
    let* s := m_get in
    if g_flush_owed s && bool_decide (g_batch_depth s = 0%nat) && outside_bodies s then b1_run_flush f else m_return tt
  end

with b1_run_flush (fuel : nat) {struct fuel} : M unit :=
  match fuel with
  | 0 => stuck
  | S f =>
    let* s := m_get in
    if g_flushing s then m_return tt
    else
      let* _ := m_modify (fun s => with_flush_owed false (with_flushing true s)) in
      let* _ := b1_drain f in
      m_modify (with_flushing false)
  end

with b1_drain (fuel : nat) {struct fuel} : M unit :=
  match fuel with
  | 0 => stuck
  | S f =>
    let* s := m_get in
    match g_queue s !! g_queue_head s with
    | Some n =>
      let* _ := m_modify (with_queue_head (S (g_queue_head s))) in
      let* _ := b1_execute_effect f n in
      b1_drain f
    | None => m_modify (fun s => with_queue [] (with_queue_head 0 s))
    end
  end.
References

Uses

165 more
Definition b1_run_top (fuel : nat) (c : cmd) : M outcome := b1_exec fuel [] c.
References
Record b1_mono_at (f : nat) : Prop := {
  B1Mexec : forall f' e c s r, f <= f' -> b1_exec f e c s = Some r -> b1_exec f' e c s = Some r;
  B1Mread : forall f' n b s r, f <= f' -> b1_read f n b s = Some r -> b1_read f' n b s = Some r;
  B1Mread_node : forall f' n s r, f <= f' -> b1_read_node f n s = Some r -> b1_read_node f' n s = Some r;
  B1Mupdate : forall f' n s r, f <= f' -> b1_update_if_necessary f n s = Some r -> b1_update_if_necessary f' n s = Some r;
  B1Mrop : forall f' n s r, f <= f' -> b1_refresh_or_pull f n s = Some r -> b1_refresh_or_pull f' n s = Some r;
  B1Mpull : forall f' n s r, f <= f' -> b1_pull f n s = Some r -> b1_pull f' n s = Some r;
  B1Mrefresh : forall f' n s r, f <= f' -> b1_refresh f n s = Some r -> b1_refresh f' n s = Some r;
  B1Mwalk : forall f' n i s r, f <= f' -> b1_resolve_walk f n i s = Some r -> b1_resolve_walk f' n i s = Some r;
  B1Mrecompute : forall f' n s r, f <= f' -> b1_recompute f n s = Some r -> b1_recompute f' n s = Some r;
  B1Mrun_memo : forall f' n s r, f <= f' -> b1_run_memo f n s = Some r -> b1_run_memo f' n s = Some r;
  B1Mexecute : forall f' n s r, f <= f' -> b1_execute_effect f n s = Some r -> b1_execute_effect f' n s = Some r;
  B1Mrun_effect : forall f' n s r, f <= f' -> b1_run_effect f n s = Some r -> b1_run_effect f' n s = Some r;
  B1Mbody : forall f' n s r, f <= f' -> b1_run_effect_body f n s = Some r -> b1_run_effect_body f' n s = Some r;
  B1Mdischarge : forall f' o s r, f <= f' -> b1_discharge f o s = Some r -> b1_discharge f' o s = Some r;
  B1Mretire : forall f' o s r, f <= f' -> b1_retire f o s = Some r -> b1_retire f' o s = Some r;
  B1Mdispose_scope : forall f' o s r, f <= f' -> b1_dispose_scope f o s = Some r -> b1_dispose_scope f' o s = Some r;
  B1Mteardown : forall f' o ls cs s r, f <= f' -> b1_teardown f o ls cs s = Some r -> b1_teardown f' o ls cs s = Some r;
  B1Mcleanup : forall f' o b e s r, f <= f' -> b1_run_cleanup f o b e s = Some r -> b1_run_cleanup f' o b e s = Some r;
  B1Mrelease : forall f' n s r, f <= f' -> b1_release f n s = Some r -> b1_release f' n s = Some r;
  B1Mdispose : forall f' n s r, f <= f' -> b1_dispose_node f n s = Some r -> b1_dispose_node f' n s = Some r;
  B1Mattach : forall f' n o s r, f <= f' -> b1_attach f n o s = Some r -> b1_attach f' n o s = Some r;
  B1Mwrite : forall f' n v s r, f <= f' -> b1_write_signal f n v s = Some r -> b1_write_signal f' n v s = Some r;
  B1Mrequest : forall f' s r, f <= f' -> b1_request_flush f s = Some r -> b1_request_flush f' s = Some r;
  B1Msettle : forall f' s r, f <= f' -> b1_settle_flush f s = Some r -> b1_settle_flush f' s = Some r;
  B1Mflush : forall f' s r, f <= f' -> b1_run_flush f s = Some r -> b1_run_flush f' s = Some r;
  B1Mdrain : forall f' s r, f <= f' -> b1_drain f s = Some r -> b1_drain f' s = Some r
}.
References
Lemma b1_mono_all f : b1_mono_at f.
Qed
Proof.
  induction f as [|f IH].
  - split; intros; discriminate.
  - assert (Hle : forall f', S f <= f' -> exists g, f' = S g /\ f <= g).
    { intros [|g] H; [lia|]. exists g. split; [reflexivity|lia]. }
    split.
    + intros f' e c s r Hle' H. destruct (Hle _ Hle') as (g & -> & Hg).
      cbn [b1_exec] in H |- *.
      destruct c; repeat match goal with
      | |- context [on_node] => unfold on_node in H |- *
      | |- context [on_value] => unfold on_value in H |- *
      | |- context [match eval_expr ?e ?p ?x with _ => _ end] => destruct (eval_expr e p x) as [[|]|]; try discriminate
      | |- (if decide _ then _ else stuck) _ = Some _ => destruct (decide _); try discriminate
      end; try exact H; try (by apply (B1Mexec _ IH)); try (destruct (decide _); by apply (B1Mexec _ IH)); revert H.
      * eapply bind_mono; [intros ??; by apply (B1Mread _ IH)|intros o ?? _].
        eapply bind_mono; [intros ?? E; exact E|intros [] ?? _; destruct o; [by apply (B1Mexec _ IH)|exact id]].
      * eapply bind_mono; [intros ??; by apply (B1Mread _ IH)|intros o ?? _].
        eapply bind_mono; [intros ?? E; exact E|intros [] ?? _; destruct o; by apply (B1Mexec _ IH)].
      * eapply bind_mono; [intros ??; by apply (B1Mread _ IH)|intros o ?? _].
        eapply bind_mono; [intros ?? E; exact E|intros [] ?? _; destruct o; [by apply (B1Mexec _ IH)|exact id]].
      * eapply bind_mono; [intros ??; by apply (B1Mwrite _ IH)|intros [] ?? _; by apply (B1Mexec _ IH)].
      * eapply bind_mono; [intros ?? E; exact E|intros ??? _].
        eapply bind_mono; [intros ?? E; exact E|intros ??? _].
        eapply bind_mono; [intros ?? E; exact E|intros [o|] ?? _; [|exact id]].
        eapply bind_mono; [intros ??; by apply (B1Mattach _ IH)|intros [] ?? _; by apply (B1Mexec _ IH)].
      * eapply bind_mono; [intros ?? E; exact E|intros ??? _].
        eapply bind_mono; [intros ?? E; exact E|intros ??? _].
        eapply bind_mono; [intros ?? E; exact E|intros [o|] ?? _; [|exact id]].
        eapply bind_mono; [intros ??; by apply (B1Mattach _ IH)|intros [] ?? _].
        eapply bind_mono; [|intros [] ?? _].
        { intros ?? E. unfold m_step in E |- *. destruct (mark_dirty f _ _) eqn:Em; simplify_eq.
          erewrite mark_dirty_mono; [reflexivity|exact Hg|exact Em]. }
        eapply bind_mono; [intros ??; by apply (B1Mrequest _ IH)|intros [] ?? _; by apply (B1Mexec _ IH)].
      * eapply bind_mono; [intros ?? E; exact E|intros [o|] ?? _; [|exact id]].
        eapply bind_mono; [intros ?? E; exact E|intros x ?? _].
        eapply bind_mono; [intros ?? E; exact E|intros [] ?? _].
        eapply bind_mono; [|intros [] ?? _; by apply (B1Mexec _ IH)].
        intros ??; destruct (o_disposed x); [|exact id].
        eapply bind_mono; [intros ?? E; eapply detached_mono; [intros ??; by apply (B1Mcleanup _ IH)|exact E]|intros [] ?? _; by apply (B1Mrequest _ IH)].
      * eapply bind_mono; [intros ?? E; exact E|intros d ?? _].
        destruct (n_kind d); try discriminate;
          (eapply bind_mono; [intros ??; by apply (B1Mdispose _ IH)|intros [] ?? _];
           eapply bind_mono; [intros ?? E; exact E|intros [] ?? _; by apply (B1Mexec _ IH)]).
      * eapply bind_mono; [intros ?? E; exact E|intros [] ?? _].
        eapply bind_mono; [intros ??; by apply (B1Mexec _ IH)|intros [|] ?? _];
          (eapply bind_mono; [intros ?? E; exact E|intros [] ?? _]);
          [eapply bind_mono; [intros ??; by apply (B1Mrequest _ IH)|intros [] ?? _; by apply (B1Mexec _ IH)]|exact id].
      * eapply bind_mono; [intros ?? E; exact E|intros ??? _].
        eapply bind_mono; [intros ?? E; exact E|intros [] ?? _; by apply (B1Mexec _ IH)].
    + intros f' n b s r Hle' H. destruct (Hle _ Hle') as (g & -> & Hg).
      cbn [b1_read] in H |- *. destruct b; [by apply (B1Mread_node _ IH)|eapply untracked_mono; [intros ??; by apply (B1Mread_node _ IH)|exact H]].
    + intros f' n s r Hle' H. destruct (Hle _ Hle') as (g & -> & Hg).
      cbn [b1_read_node] in H |- *. revert H.
      eapply bind_mono; [intros ?? E; exact E|intros d ?? _].
      destruct (n_kind d); [exact id| |exact id].
      eapply bind_mono; [|intros d' ?? _; exact id].
      intros ??; destruct (stale_live d); [|exact id].
      eapply bind_mono; [intros ??; by apply (B1Mrop _ IH)|intros d' ?? _].
      eapply bind_mono; [|intros [] ?? _; exact id].
      intros ??; eapply bind_mono; [intros ?? E; exact E|intros [] ?? _].
      eapply bind_mono; [intros ?? E; exact E|intros d'' ?? _].
      destruct (stale_live d''); [|exact id].
      intros E; eapply mark_reader_mono; [exact Hg|exact E].
    + intros f' n s r Hle' H. destruct (Hle _ Hle') as (g & -> & Hg).
      cbn [b1_update_if_necessary] in H |- *. revert H.
      eapply bind_mono; [intros ?? E; exact E|intros d ?? _].
      destruct (n_kind d); [exact id| |exact id].
      destruct (stale_live d); [|exact id].
      eapply bind_mono; [intros ??; by apply (B1Mrop _ IH)|intros ??? _; exact id].
    + intros f' n s r Hle' H. destruct (Hle _ Hle') as (g & -> & Hg).
      cbn [b1_refresh_or_pull] in H |- *. revert H.
      eapply bind_mono; [intros ?? E; exact E|intros st0 ?? _].
      destruct (deferring_b1 st0); [|by apply (B1Mpull _ IH)].
      eapply bind_mono; [intros ??; by apply (B1Mrefresh _ IH)|intros ??? _; exact id].
    + intros f' n s r Hle' H. destruct (Hle _ Hle') as (g & -> & Hg).
      cbn [b1_pull] in H |- *. revert H.
      eapply bind_mono; [intros ?? E; exact E|intros [] ?? _].
      eapply bind_mono; [intros ??; by apply (B1Mrefresh _ IH)|intros [] ?? _].
      eapply bind_mono; [intros ?? E; exact E|intros d ?? _].
      eapply bind_mono; [intros ?? E; exact E|intros [] ?? _].
      eapply bind_mono; [intros ?? E; exact E|intros st0 ?? _].
      eapply bind_mono; [|intros ??? _; exact id].
      intros ??; destruct (_ && _); [by apply (B1Msettle _ IH)|exact id].
    + intros f' n s r Hle' H. destruct (Hle _ Hle') as (g & -> & Hg).
      cbn [b1_refresh] in H |- *. revert H.
      eapply bind_mono; [intros ?? E; exact E|intros st0 ?? _].
      destruct (decide (n ∈ g_checking st0)).
      { intros E; eapply m_step_mark_dirty_mono; [exact Hg|exact E]. }
      destruct (decide (n ∈ g_running st0)); [exact id|].
      eapply bind_mono; [intros ?? E; exact E|intros d ?? _].
      eapply bind_mono; [|intros [] ?? _].
      { intros ??; destruct (decide (n_freshness d = Check)); [|exact id].
        eapply bind_mono; [intros ?? E; exact E|intros [] ?? _].
        eapply bind_mono; [intros ??; by apply (B1Mwalk _ IH)|intros [] ?? _].
        eapply bind_mono; [intros ?? E; exact E|intros [] ?? _].
        eapply bind_mono; [intros ?? E; exact E|intros d' ?? _].
        destruct (decide (n_freshness d' = Check)); [|exact id].
        eapply bind_mono; [intros ?? E; exact E|intros [] ?? _; exact id]. }
      eapply bind_mono; [intros ?? E; exact E|intros d' ?? _].
      destruct (decide (n_freshness d' = Dirty)); [by apply (B1Mrecompute _ IH)|exact id].
    + intros f' n i s r Hle' H. destruct (Hle _ Hle') as (g & -> & Hg).
      cbn [b1_resolve_walk] in H |- *. revert H.
      eapply bind_mono; [intros ?? E; exact E|intros d ?? _].
      eapply bind_mono; [intros ?? E; exact E|intros st0 ?? _].
      destruct (decide (n_freshness d = Check)); [|exact id].
      destruct (source_slots (sources_of (g_edges st0) n) !! i) as [[src _]|].
      * eapply bind_mono; [intros ??; by apply (B1Mupdate _ IH)|intros [] ?? _].
        eapply bind_mono; [intros ?? E; exact E|intros d' ?? _].
        eapply bind_mono; [intros ?? E; exact E|intros d_src ?? _].
        eapply bind_mono; [intros ?? E; exact E|intros st1 ?? _].
        eapply bind_mono; [|intros [] ?? _; by apply (B1Mwalk _ IH)].
        intros st2 a0; destruct (decide _); [|exact id]. intros E; eapply m_step_mark_dirty_mono; [exact Hg|exact E].
      * destruct (slot_holds stale_live st0 n); [by apply (B1Mwalk _ IH)|exact id].
    + intros f' n s r Hle' H. destruct (Hle _ Hle') as (g & -> & Hg).
      cbn [b1_recompute] in H |- *. revert H.
      eapply bind_mono; [intros ?? E; exact E|intros d ?? _].
      destruct (n_scope d) as [o|]; [|by apply (B1Mrun_memo _ IH)].
      eapply bind_mono; [intros ?? E; exact E|intros [] ?? _].
      eapply bind_mono; [intros ?? E; exact E|intros [] ?? _].
      eapply bind_mono; [intros ??; by apply (B1Mdischarge _ IH)|intros [] ?? _].
      eapply bind_mono; [intros ?? E; exact E|intros d' ?? _].
      destruct (n_disposed d'); [exact id|].
      destruct (decide (n_runs d' = n_runs d)); [by apply (B1Mrun_memo _ IH)|].
      destruct (decide (n_freshness d' = Dirty)); [by apply (B1Mrecompute _ IH)|exact id].
    + intros f' n s r Hle' H. destruct (Hle _ Hle') as (g & -> & Hg).
      cbn [b1_run_memo] in H |- *. revert H.
      eapply bind_mono; [intros ?? E; exact E|intros d ?? _].
      eapply bind_mono; [intros ?? E; exact E|intros [] ?? _].
      eapply bind_mono; [intros ?? E; exact E|intros [] ?? _].
      eapply bind_mono; [intros ?? E; exact E|intros [] ?? _].
      eapply bind_mono; [intros ?? E; exact E|intros [] ?? _].
      eapply bind_mono; [intros ?? E; eapply hosted_mono; [intros ??; by apply (B1Mexec _ IH)|exact E]|intros o ?? _].
      eapply bind_mono; [intros ?? E; exact E|intros d1 ?? _].
      eapply bind_mono; [intros ?? E; exact E|intros [] ?? _].
      eapply bind_mono; [intros ?? E; exact E|intros [] ?? _].
      eapply bind_mono; [intros ?? E; exact E|intros [] ?? _].
      eapply bind_mono; [intros ?? E; exact E|intros [] ?? _].
      eapply bind_mono; [intros ?? E; exact E|intros d2 ?? _].
      eapply bind_mono; [intros ?? E; exact E|intros moved ?? _].
      eapply bind_mono; [intros ?? E; exact E|intros st0 ?? _].
      eapply bind_mono; [intros ?? E; exact E|intros [] ?? _].
      eapply bind_mono; [|intros [] ?? _; exact id].
      intros ??; destruct moved; [|exact id].
      eapply bind_mono; [|intros [] ?? _; exact id].
      intros st2 a0 E. eapply m_step_notify_dirty_except_mono; [exact Hg|exact E].
    + intros f' n s r Hle' H. destruct (Hle _ Hle') as (g & -> & Hg).
      cbn [b1_execute_effect] in H |- *. revert H.
      eapply bind_mono; [intros ?? E; exact E|intros d ?? _].
      destruct (n_disposed d); [exact id|].
      eapply bind_mono; [|intros [] ?? _].
      { intros ??; destruct (decide (n_freshness d = Check)); [|exact id].
        eapply bind_mono; [intros ??; by apply (B1Mwalk _ IH)|intros [] ?? _].
        eapply bind_mono; [intros ?? E; exact E|intros d' ?? _].
        destruct (decide (n_freshness d' = Check)); [|exact id].
        eapply bind_mono; [intros ?? E; exact E|intros [] ?? _; exact id]. }
      eapply bind_mono; [intros ?? E; exact E|intros d' ?? _].
      eapply bind_mono; [intros ?? E; exact E|intros [] ?? _].
      destruct (decide (n_freshness d' = Dirty)); [by apply (B1Mrun_effect _ IH)|exact id].
    + intros f' n s r Hle' H. destruct (Hle _ Hle') as (g & -> & Hg).
      cbn [b1_run_effect] in H |- *. revert H.
      eapply bind_mono; [intros ?? E; exact E|intros [] ?? _].
      eapply bind_mono; [intros ?? E; exact E|intros [] ?? _].
      eapply bind_mono; [intros ?? E; exact E|intros [] ?? _].
      eapply bind_mono; [intros ?? E; exact E|intros d ?? _].
      eapply bind_mono; [|intros [] ?? _].
      { intros ??; destruct (n_scope d) as [o|]; [|exact id].
        eapply bind_mono; [intros ?? E; exact E|intros [] ?? _].
        eapply bind_mono; [intros ??; by apply (B1Mdischarge _ IH)|intros [] ?? _; exact id]. }
      eapply bind_mono; [intros ?? E; exact E|intros d' ?? _].
      eapply bind_mono; [|intros [] ?? _; exact id].
      intros ??; destruct (n_disposed d'); [exact id|by apply (B1Mbody _ IH)].
    + intros f' n s r Hle' H. destruct (Hle _ Hle') as (g & -> & Hg).
      cbn [b1_run_effect_body] in H |- *. revert H.
      eapply bind_mono; [intros ?? E; exact E|intros d ?? _].
      eapply bind_mono; [intros ?? E; exact E|intros [] ?? _].
      eapply bind_mono; [intros ?? E; exact E|intros [] ?? _].
      eapply bind_mono; [intros ?? E; eapply hosted_mono; [intros ??; by apply (B1Mexec _ IH)|exact E]|intros o ?? _].
      eapply bind_mono; [intros ?? E; exact E|intros d' ?? _].
      eapply bind_mono; [intros ?? E; exact E|intros [] ?? _; exact id].
    + intros f' o s r Hle' H. destruct (Hle _ Hle') as (g & -> & Hg).
      cbn [b1_discharge] in H |- *. revert H.
      eapply bind_mono; [intros ?? E; eapply detached_mono; [intros ??; by apply (B1Mdispose_scope _ IH)|exact E]|intros [] ?? _; exact id].
    + intros f' o s r Hle' H. destruct (Hle _ Hle') as (g & -> & Hg).
      cbn [b1_retire] in H |- *. revert H.
      eapply bind_mono; [|intros [] ?? _].
      { intros ?? E0. eapply detached_mono; [|exact E0]. intros st0 r0.
        eapply bind_mono; [intros ?? E; exact E|intros x ?? _].
        destruct (o_disposed x); [exact id|].
        eapply bind_mono; [intros ?? E; exact E|intros [] ?? _; by apply (B1Mdispose_scope _ IH)]. }
      eapply bind_mono; [intros ?? E; exact E|intros st0 ?? _].
      destruct (live_queue st0); [by apply (B1Mrequest _ IH)|exact id].
    + intros f' o s r Hle' H. destruct (Hle _ Hle') as (g & -> & Hg).
      cbn [b1_dispose_scope] in H |- *. revert H.
      eapply bind_mono; [intros ?? E; exact E|intros x ?? _].
      eapply bind_mono; [intros ?? E; exact E|intros [] ?? _; by apply (B1Mteardown _ IH)].
    + intros f' o ls cs s r Hle' H. destruct (Hle _ Hle') as (g & -> & Hg).
      cbn [b1_teardown] in H |- *. revert H.
      destruct ls as [|l ls'], cs as [|[[b e] a] cs']; [exact id|..].
      * eapply bind_mono; [intros ??; by apply (B1Mcleanup _ IH)|intros [] ?? _; by apply (B1Mteardown _ IH)].
      * eapply bind_mono; [intros ??; by apply (B1Mrelease _ IH)|intros [] ?? _; by apply (B1Mteardown _ IH)].
      * destruct (decide (a < l_acquired l)).
        -- eapply bind_mono; [intros ??; by apply (B1Mrelease _ IH)|intros [] ?? _; by apply (B1Mteardown _ IH)].
        -- eapply bind_mono; [intros ??; by apply (B1Mcleanup _ IH)|intros [] ?? _; by apply (B1Mteardown _ IH)].
    + intros f' o b e s r Hle' H. destruct (Hle _ Hle') as (g & -> & Hg).
      cbn [b1_run_cleanup] in H |- *. revert H.
      eapply bind_mono; [intros ?? E; exact E|intros x ?? _].
      eapply bind_mono; [intros ?? E; exact E|intros [] ?? _].
      eapply bind_mono; [intros ??; by apply (B1Mexec _ IH)|intros [|] ?? _; exact id].
    + intros f' n s r Hle' H. destruct (Hle _ Hle') as (g & -> & Hg).
      cbn [b1_release] in H |- *. revert H.
      eapply bind_mono; [intros ?? E; exact E|intros [] ?? _; by apply (B1Mdispose _ IH)].
    + intros f' n s r Hle' H. destruct (Hle _ Hle') as (g & -> & Hg).
      cbn [b1_dispose_node] in H |- *. revert H.
      eapply bind_mono; [intros ?? E; exact E|intros d ?? _].
      destruct (n_disposed d); [exact id|].
      eapply bind_mono; [intros ?? E; exact E|intros [] ?? _].
      eapply bind_mono; [intros ?? E; exact E|intros [] ?? _].
      eapply bind_mono; [intros ?? E; exact E|intros [] ?? _].
      destruct (n_scope d); [by apply (B1Mretire _ IH)|exact id].
    + intros f' n o s r Hle' H. destruct (Hle _ Hle') as (g & -> & Hg).
      cbn [b1_attach] in H |- *. revert H.
      eapply bind_mono; [intros ?? E; exact E|intros x ?? _].
      destruct (o_disposed x); [|exact id].
      eapply bind_mono; [intros ??; by apply (B1Mrelease _ IH)|intros [] ?? _; exact id].
    + intros f' n v s r Hle' H. destruct (Hle _ Hle') as (g & -> & Hg).
      cbn [b1_write_signal] in H |- *. revert H.
      eapply bind_mono; [intros ?? E; exact E|intros d ?? _].
      destruct (n_kind d); [|exact id|exact id].
      destruct (decide (n_value d = v)); [exact id|].
      eapply bind_mono; [intros ?? E; exact E|intros [] ?? _].
      eapply bind_mono; [intros ?? E; exact E|intros [] ?? _].
      eapply bind_mono; [|intros [] ?? _; by apply (B1Mrequest _ IH)].
      intros st2 a0 E. eapply m_step_notify_dirty_mono; [exact Hg|exact E].
    + intros f' s r Hle' H. destruct (Hle _ Hle') as (g & -> & Hg).
      cbn [b1_request_flush] in H |- *. revert H.
      eapply bind_mono; [intros ?? E; exact E|intros st0 ?? _].
      destruct (_ && _); [|exact id].
      destruct (outside_bodies st0); [by apply (B1Mflush _ IH)|exact id].
    + intros f' s r Hle' H. destruct (Hle _ Hle') as (g & -> & Hg).
      cbn [b1_settle_flush] in H |- *. revert H.
      eapply bind_mono; [intros ?? E; exact E|intros st0 ?? _].
      destruct (_ && _); [by apply (B1Mflush _ IH)|exact id].
    + intros f' s r Hle' H. destruct (Hle _ Hle') as (g & -> & Hg).
      cbn [b1_run_flush] in H |- *. revert H.
      eapply bind_mono; [intros ?? E; exact E|intros st0 ?? _].
      destruct (g_flushing st0); [exact id|].
      eapply bind_mono; [intros ?? E; exact E|intros [] ?? _].
      eapply bind_mono; [intros ??; by apply (B1Mdrain _ IH)|intros [] ?? _; exact id].
    + intros f' s r Hle' H. destruct (Hle _ Hle') as (g & -> & Hg).
      cbn [b1_drain] in H |- *. revert H.
      eapply bind_mono; [intros ?? E; exact E|intros st0 ?? _].
      destruct (g_queue st0 !! g_queue_head st0); [|exact id].
      eapply bind_mono; [intros ?? E; unfold m_modify in *; exact E|intros [] ?? _].
      eapply bind_mono; [intros ??; by apply (B1Mexecute _ IH)|intros [] ?? _; by apply (B1Mdrain _ IH)].
Qed.
References
Record b1_depths_kept_at (f : nat) : Prop := {
  B1Dexec : forall e c s s' r, b1_exec f e c s = Some (s', r) -> same_depths s s';
  B1Dread : forall n b s s' r, b1_read f n b s = Some (s', r) -> same_depths s s';
  B1Dread_node : forall n s s' r, b1_read_node f n s = Some (s', r) -> same_depths s s';
  B1Dupdate : forall n s s' r, b1_update_if_necessary f n s = Some (s', r) -> same_depths s s';
  B1Drop : forall n s s' r, b1_refresh_or_pull f n s = Some (s', r) -> same_depths s s';
  B1Dpull : forall n s s' r, b1_pull f n s = Some (s', r) -> same_depths s s';
  B1Drefresh : forall n s s' r, b1_refresh f n s = Some (s', r) -> same_depths s s';
  B1Dwalk : forall n i s s' r, b1_resolve_walk f n i s = Some (s', r) -> same_depths s s';
  B1Drecompute : forall n s s' r, b1_recompute f n s = Some (s', r) -> same_depths s s';
  B1Drun_memo : forall n s s' r, b1_run_memo f n s = Some (s', r) -> same_depths s s';
  B1Dexecute : forall n s s' r, b1_execute_effect f n s = Some (s', r) -> same_depths s s';
  B1Drun_effect : forall n s s' r, b1_run_effect f n s = Some (s', r) -> same_depths s s';
  B1Dbody : forall n s s' r, b1_run_effect_body f n s = Some (s', r) -> same_depths s s';
  B1Ddischarge : forall o s s' r, b1_discharge f o s = Some (s', r) -> same_depths s s';
  B1Dretire : forall o s s' r, b1_retire f o s = Some (s', r) -> same_depths s s';
  B1Ddispose_scope : forall o s s' r, b1_dispose_scope f o s = Some (s', r) -> same_depths s s';
  B1Dteardown : forall o ls cs s s' r, b1_teardown f o ls cs s = Some (s', r) -> same_depths s s';
  B1Dcleanup : forall o b e s s' r, b1_run_cleanup f o b e s = Some (s', r) -> same_depths s s';
  B1Drelease : forall n s s' r, b1_release f n s = Some (s', r) -> same_depths s s';
  B1Ddispose : forall n s s' r, b1_dispose_node f n s = Some (s', r) -> same_depths s s';
  B1Dattach : forall n o s s' r, b1_attach f n o s = Some (s', r) -> same_depths s s';
  B1Dwrite : forall n v s s' r, b1_write_signal f n v s = Some (s', r) -> same_depths s s';
  B1Drequest : forall s s' r, b1_request_flush f s = Some (s', r) -> same_depths s s';
  B1Dsettle : forall s s' r, b1_settle_flush f s = Some (s', r) -> same_depths s s';
  B1Dflush : forall s s' r, b1_run_flush f s = Some (s', r) -> same_depths s s';
  B1Ddrain : forall s s' r, b1_drain f s = Some (s', r) -> same_depths s s'
}.
References
Lemma b1_depths_kept f : b1_depths_kept_at f.
Qed
Proof.
  induction f as [|f IH].
  - split; intros; discriminate.
  - assert (Htrack : forall n s s' a, track n s = Some (s', a) -> same_depths s s').
    { intros n s s' a Ht. unfold track in Ht. destruct (live_current s); [|simplify_eq; apply same_depths_refl].
      destruct (sources_add _ _ _); [|discriminate]. simplify_eq. unfold ghost_read. destruct (g_nodes (with_edges _ _) !! n); split; reflexivity. }
    assert (Hcur : forall s s' a, current_owner s = Some (s', a) -> same_depths s s').
    { intros s s' a Hc. unfold current_owner in Hc.
      destruct (g_current_owner s), (g_current s); try discriminate; try (simplify_eq; apply same_depths_refl).
      destruct (g_nodes s !! _); [|discriminate]. destruct (n_scope _); [simplify_eq; split; reflexivity|].
      destruct (match n_kind _, n_mode _ with KMemo, Pure => true | _, _ => false end); simplify_eq; split; reflexivity. }
    assert (Hunlink : forall l s s' a, unlink l s = Some (s', a) -> same_depths s s').
    { intros l s s' a Hu. unfold unlink, m_bind, get_owner, put_owner, m_modify, m_return in Hu.
      destruct (g_owners s !! l_owner l); [|simplify_eq]. destruct (decide _); simplify_eq; split; reflexivity. }
    assert (Hrec : forall o y s s' a, record_error o y s = Some (s', a) -> same_depths s s').
    { intros o y s s' a Hr. unfold record_error, m_bind, get_owner, put_owner, m_modify in Hr.
      destruct (g_owners s !! o) as [x|]; [|simplify_eq]. destruct (o_sink x) as [p|]; [destruct (g_owners s !! p)|]; simplify_eq; split; reflexivity. }
    assert (Hghost : forall n s s' a, (let* d3 := get_node n in if decide (n_freshness d3 = Clean) then m_return tt else announce n) s = Some (s', a) -> same_depths s s').
    { intros n s s' a Hg. unfold m_bind, get_node, announce, modify_node, m_return in Hg.
      destruct (g_nodes s !! n); [|discriminate]. destruct (decide _); [simplify_eq; split; reflexivity|].
      destruct (f_unannounced _); [destruct (g_nodes s !! n); simplify_eq; split; reflexivity|simplify_eq; split; reflexivity]. }
    split.
    + intros e c s s' r H. cbn [b1_exec] in H.
      destruct c; repeat match goal with
      | H0 : context [on_node] |- _ => unfold on_node in H0
      | H0 : context [on_value] |- _ => unfold on_value in H0
      | H0 : context [match eval_expr ?e ?p ?x with _ => _ end] |- _ => destruct (eval_expr e p x) as [[|]|]; try discriminate
      | H0 : (if decide _ then _ else stuck) _ = Some _ |- _ => destruct (decide _); try discriminate
      end; try (unfold m_return in H; simplify_eq; apply same_depths_refl); try (simplify_eq; apply same_depths_refl).
      * eapply (B1Dexec _ IH); exact H.
      * destruct (decide _); eapply (B1Dexec _ IH); exact H.
      * eapply (bind_depths_of H); [intros ??; intro H__d; eapply (B1Dread _ IH); exact H__d| ].
        intros o s1 E1. eapply bind_depths; [intros ?? E2; unfold note_host_read in E2; destruct (g_current s1), (g_current_owner s1); try destruct (decide _); unfold emit, m_modify in E2; simplify_eq; split; reflexivity|].
        intros [] s2 E2 H3. destruct o; [eapply (B1Dexec _ IH); exact H3|unfold m_return in H3; simplify_eq; apply same_depths_refl].
      * eapply (bind_depths_of H); [intros ??; intro H__d; eapply (B1Dread _ IH); exact H__d| ].
        intros o s1 E1. eapply bind_depths; [intros ?? E2; unfold note_host_read in E2; destruct (g_current s1), (g_current_owner s1); try destruct (decide _); unfold emit, m_modify in E2; simplify_eq; split; reflexivity|].
        intros [] s2 E2 H3. destruct o; eapply (B1Dexec _ IH); exact H3.
      * eapply (bind_depths_of H); [intros ??; intro H__d; eapply (B1Dread _ IH); exact H__d| ].
        intros o s1 E1. eapply bind_depths; [intros ?? E2; unfold note_host_read in E2; destruct (g_current s1), (g_current_owner s1); try destruct (decide _); unfold emit, m_modify in E2; simplify_eq; split; reflexivity|].
        intros [] s2 E2 H3. destruct o; [eapply (B1Dexec _ IH); exact H3|unfold m_return in H3; simplify_eq; apply same_depths_refl].
      * eapply (bind_depths_of H); [intros ??; intro H__d; eapply (B1Dwrite _ IH); exact H__d|intros [] ?? H__d; eapply (B1Dexec _ IH); exact H__d].
      * eapply (bind_depths_of H); [intros ?? E; unfold next_id in E; simplify_eq; split; reflexivity|intros ???; eapply bind_depths; [intros ?? E; unfold add_node, m_modify, ghost_publish in E; destruct (g_nodes _ !! _); simplify_eq; split; reflexivity|intros [] ?? H__d; eapply (B1Dexec _ IH); exact H__d]].
      * eapply (bind_depths_of H); [intros ?? E; unfold next_id in E; simplify_eq; split; reflexivity|intros ???; eapply bind_depths; [intros ?? E; unfold add_node, m_modify, ghost_publish in E; destruct (g_nodes _ !! _); simplify_eq; split; reflexivity|intros [] ??; eapply bind_depths; [intros ??; apply Hcur|intros [o|] ??; [eapply bind_depths; [intros ??; intro H__d; eapply (B1Dattach _ IH); exact H__d|intros [] ?? H__d; eapply (B1Dexec _ IH); exact H__d]|intros Hm; unfold m_return in Hm; simplify_eq; apply same_depths_refl]]]].
      * eapply (bind_depths_of H); [intros ?? E; unfold next_id in E; simplify_eq; split; reflexivity|intros ???; eapply bind_depths; [intros ?? E; unfold add_node, m_modify, ghost_publish in E; destruct (g_nodes _ !! _); simplify_eq; split; reflexivity|intros [] ??; eapply bind_depths; [intros ??; apply Hcur|intros [o|] ??; [|intros Hm; unfold m_return in Hm; simplify_eq; apply same_depths_refl]]]].
        eapply bind_depths; [intros ??; intro H__d; eapply (B1Dattach _ IH); exact H__d|intros [] ??].
        eapply bind_depths; [intros ?? E; unfold m_step in E; destruct (mark_dirty f _ _) eqn:Em; simplify_eq; by eapply marks_same_depths, mark_dirty_rtc|intros [] ??].
        eapply bind_depths; [intros ??; intro H__d; eapply (B1Drequest _ IH); exact H__d|intros [] ?? H__d; eapply (B1Dexec _ IH); exact H__d].
      * eapply (bind_depths_of H); [intros ??; apply Hcur|intros [o|] ??; [|intros Hm; unfold m_return in Hm; simplify_eq; apply same_depths_refl]].
        eapply bind_depths; [intros ?? E; unfold get_owner in E; destruct (g_owners _ !! o); simplify_eq; apply same_depths_refl|intros x ??].
        eapply bind_depths; [intros ?? E; unfold m_modify in E; simplify_eq; split; reflexivity|intros [] ??].
        eapply bind_depths; [|intros [] ?? H__d; eapply (B1Dexec _ IH); exact H__d].
        intros ??; destruct (o_disposed x); [|intros E; unfold put_owner, m_modify in E; simplify_eq; split; reflexivity].
        eapply bind_depths; [intros ??; eapply detached_depths; intros ???; intro H__d; eapply (B1Dcleanup _ IH); exact H__d|intros [] ?? H__d; eapply (B1Drequest _ IH); exact H__d].
      * eapply (bind_depths_of H); [intros ?? E; unfold get_node in E; destruct (g_nodes _ !! _); simplify_eq; apply same_depths_refl|intros d ?? ].
        destruct (n_kind d); try discriminate;
          (eapply bind_depths; [intros ??; intro H__d; eapply (B1Ddispose _ IH); exact H__d|intros [] ??];
           eapply bind_depths; [intros ?? E; destruct (n_disposed d); unfold emit, m_modify, m_return in E; simplify_eq; split; reflexivity|intros [] ?? H__d; eapply (B1Dexec _ IH); exact H__d]).
      * eapply (bind_depths_of H); [intros ?? E; unfold m_modify in E; simplify_eq; split; reflexivity|intros [] ?? ].
        eapply bind_depths; [intros ??; intro H__d; eapply (B1Dexec _ IH); exact H__d|intros o ??].
        eapply bind_depths; [intros ?? E; unfold m_modify in E; simplify_eq; split; reflexivity|intros [] ??].
        destruct o; [eapply bind_depths; [intros ??; intro H__d; eapply (B1Drequest _ IH); exact H__d|intros [] ?? H__d; eapply (B1Dexec _ IH); exact H__d]|intros Hm; unfold m_return in Hm; simplify_eq; apply same_depths_refl].
      * eapply (bind_depths_of H); [intros ??; apply log_label_depths|intros ??? ].
        eapply bind_depths; [intros ?? E; unfold emit, m_modify in E; simplify_eq; split; reflexivity|intros [] ?? H__d; eapply (B1Dexec _ IH); exact H__d].
    + intros n b s s' r H. cbn [b1_read] in H. destruct b; [by eapply (B1Dread_node _ IH)|eapply untracked_depths; [intros ???; intro H__d; eapply (B1Dread_node _ IH); exact H__d|exact H]].
    + intros n s s' r H. cbn [b1_read_node] in H.
      eapply (bind_depths_of H); [intros ?? E; unfold get_node in E; destruct (g_nodes _ !! n); simplify_eq; apply same_depths_refl|intros d ?? ].
      destruct (n_kind d); [eapply bind_depths; [intros ??; apply Htrack|intros [] ?? E; unfold m_return in E; simplify_eq; apply same_depths_refl]| |discriminate].
      eapply bind_depths; [|intros d' ?? E; destruct (n_status d'); unfold m_return in E; simplify_eq; apply same_depths_refl].
      intros ??; destruct (stale_live d).
      * eapply bind_depths; [intros ??; intro H__d; eapply (B1Drop _ IH); exact H__d|intros d' ??].
        eapply bind_depths; [|intros [] ?? E; unfold m_return in E; simplify_eq; apply same_depths_refl].
        intros ??; eapply bind_depths; [intros ??; apply Htrack|intros [] ??].
        eapply bind_depths; [intros ?? E; unfold get_node in E; destruct (g_nodes _ !! n); simplify_eq; apply same_depths_refl|intros d'' ??].
        destruct (stale_live d''); [|intros E; unfold m_return in E; simplify_eq; apply same_depths_refl].
        apply mark_reader_depths.
      * eapply bind_depths; [intros ??; apply Htrack|intros [] ?? E; unfold get_node in E; destruct (g_nodes _ !! n); simplify_eq; apply same_depths_refl].
    + intros n s s' r H. cbn [b1_update_if_necessary] in H.
      eapply (bind_depths_of H); [intros ?? E; unfold get_node in E; destruct (g_nodes _ !! n); simplify_eq; apply same_depths_refl|intros d ?? ].
      destruct (n_kind d); [intros E; unfold m_return in E; simplify_eq; apply same_depths_refl| |intros E; unfold m_return in E; simplify_eq; apply same_depths_refl].
      destruct (stale_live d); [|intros E; unfold m_return in E; simplify_eq; apply same_depths_refl].
      eapply bind_depths; [intros ??; intro H__d; eapply (B1Drop _ IH); exact H__d|intros ??? E; unfold m_return in E; simplify_eq; apply same_depths_refl].
    + intros n s s' r H. cbn [b1_refresh_or_pull] in H.
      eapply (bind_depths_of H); [intros ?? E; unfold m_get in E; simplify_eq; apply same_depths_refl|intros s0 ?? ].
      destruct (deferring_b1 s0); [|by apply (B1Dpull _ IH)].
      eapply bind_depths; [intros ??; intro H__d; eapply (B1Drefresh _ IH); exact H__d|intros [] ?? E; unfold get_node in E; destruct (g_nodes _ !! n); simplify_eq; apply same_depths_refl].
    + intros n s s' r H. cbn [b1_pull] in H.
      eapply bind_inv in H as (s0 & [] & E0 & H). unfold m_modify in E0; simplify_eq.
      eapply bind_inv in H as (s1 & [] & Er & H).
      eapply bind_inv in H as (s2 & d & Eg & H). unfold get_node in Eg. destruct (g_nodes s1 !! n); [|discriminate]. injection Eg as <- <-.
      eapply bind_inv in H as (s3 & [] & E3 & H). unfold m_modify in E3; simplify_eq.
      rewrite bind_get in H.
      eapply bind_inv in H as (s4 & [] & Es & H). unfold m_return in H; simplify_eq.
      pose proof (B1Drefresh _ IH n _ _ _ Er) as [Hp1 Hf1].
      set (s3 := with_pull_depth (pred (g_pull_depth s1)) s1) in *.
      assert (Hp3 : g_pull_depth s3 = g_pull_depth s) by (unfold s3; simpl; rewrite Hp1; simpl; lia).
      assert (Hf3 : g_flushing s3 = g_flushing s) by (unfold s3; simpl; exact Hf1).
      destruct (bool_decide (g_pull_depth s3 = 0) && g_flush_owed s3).
      * pose proof (B1Dsettle _ IH _ _ _ Es) as [Hp4 Hf4]. split; congruence.
      * unfold m_return in Es; simplify_eq. split; assumption.
    + intros n s s' r H. cbn [b1_refresh] in H.
      eapply (bind_depths_of H); [intros ?? E; unfold m_get in E; simplify_eq; apply same_depths_refl|intros s0 ?? ].
      destruct (decide (n ∈ g_checking s0)).
      { intros E. unfold m_step in E. destruct (mark_dirty f n _) eqn:Em; simplify_eq. by eapply marks_same_depths, mark_dirty_rtc. }
      destruct (decide (n ∈ g_running s0)); [intros E; unfold m_return in E; simplify_eq; apply same_depths_refl|].
      eapply bind_depths; [intros ?? E; unfold get_node in E; destruct (g_nodes _ !! n); simplify_eq; apply same_depths_refl|intros d ??].
      eapply bind_depths; [|intros [] ??].
      { intros ??; destruct (decide (n_freshness d = Check)); [|intros E; unfold m_return in E; simplify_eq; apply same_depths_refl].
        eapply bind_depths; [intros ?? E; unfold push_checking, m_modify in E; simplify_eq; split; reflexivity|intros [] ??].
        eapply bind_depths; [intros ??; intro H__d; eapply (B1Dwalk _ IH); exact H__d|intros [] ??].
        eapply bind_depths; [intros ?? E; unfold pop_checking, m_modify in E; simplify_eq; split; reflexivity|intros [] ??].
        eapply bind_depths; [intros ?? E; unfold get_node in E; destruct (g_nodes _ !! n); simplify_eq; apply same_depths_refl|intros d' ??].
        destruct (decide (n_freshness d' = Check)); [|intros E; unfold m_return in E; simplify_eq; apply same_depths_refl].
        eapply bind_depths; [intros ?? E; unfold m_modify in E; simplify_eq; split; reflexivity|intros [] ?? E; unfold modify_node in E; destruct (g_nodes _ !! n); simplify_eq; split; reflexivity]. }
      eapply bind_depths; [intros ?? E; unfold get_node in E; destruct (g_nodes _ !! n); simplify_eq; apply same_depths_refl|intros d' ??].
      destruct (decide (n_freshness d' = Dirty)); [by apply (B1Drecompute _ IH)|intros E; unfold m_return in E; simplify_eq; apply same_depths_refl].
    + intros n i s s' r H. cbn [b1_resolve_walk] in H.
      eapply (bind_depths_of H); [intros ?? E; unfold get_node in E; destruct (g_nodes _ !! n); simplify_eq; apply same_depths_refl|intros d ?? ].
      eapply bind_depths; [intros ?? E; unfold m_get in E; simplify_eq; apply same_depths_refl|intros st0 ??].
      destruct (decide (n_freshness d = Check)); [|intros E; unfold m_return in E; simplify_eq; apply same_depths_refl].
      destruct (source_slots (sources_of (g_edges st0) n) !! i) as [[src _]|].
      * eapply bind_depths; [intros ??; intro H__d; eapply (B1Dupdate _ IH); exact H__d|intros [] ??].
        eapply bind_depths; [intros ?? E; unfold get_node in E; destruct (g_nodes _ !! n); simplify_eq; apply same_depths_refl|intros d' ??].
        eapply bind_depths; [intros ?? E; unfold get_node in E; destruct (g_nodes _ !! src); simplify_eq; apply same_depths_refl|intros d_src ??].
        eapply bind_depths; [intros ?? E; unfold m_get in E; simplify_eq; apply same_depths_refl|intros st1 ??].
        eapply bind_depths; [|intros [] ?? H__d; eapply (B1Dwalk _ IH); exact H__d].
        intros st_ [] E; destruct (decide _); [|unfold m_return in E; simplify_eq; apply same_depths_refl].
        unfold m_step in E. destruct (mark_dirty f n _) eqn:Em; simplify_eq. by eapply marks_same_depths, mark_dirty_rtc.
      * destruct (slot_holds stale_live st0 n); [by apply (B1Dwalk _ IH)|intros E; unfold m_return in E; simplify_eq; apply same_depths_refl].
    + intros n s s' r H. cbn [b1_recompute] in H.
      eapply (bind_depths_of H); [intros ?? E; unfold get_node in E; destruct (g_nodes _ !! n); simplify_eq; apply same_depths_refl|intros d ?? ].
      destruct (n_scope d) as [o|]; [|by apply (B1Drun_memo _ IH)].
      eapply bind_depths; [intros ?? E; unfold modify_node in E; destruct (g_nodes _ !! n); simplify_eq; split; reflexivity|intros [] ??].
      eapply bind_depths; [intros ?? E; unfold m_modify in E; simplify_eq; split; reflexivity|intros [] ??].
      eapply bind_depths; [intros ??; intro H__d; eapply (B1Ddischarge _ IH); exact H__d|intros [] ??].
      eapply bind_depths; [intros ?? E; unfold get_node in E; destruct (g_nodes _ !! n); simplify_eq; apply same_depths_refl|intros d' ??].
      destruct (n_disposed d').
      { destruct (decide (n_freshness d' = Clean)); [intros E; unfold modify_node in E; destruct (g_nodes _ !! n); simplify_eq; split; reflexivity|intros E; unfold m_return in E; simplify_eq; apply same_depths_refl]. }
      destruct (decide (n_runs d' = n_runs d)); [by apply (B1Drun_memo _ IH)|].
      destruct (decide (n_freshness d' = Dirty)); [by apply (B1Drecompute _ IH)|intros E; unfold m_return in E; simplify_eq; apply same_depths_refl].
    + intros n s s' r H. cbn [b1_run_memo] in H.
      eapply (bind_depths_of H); [intros ?? E; unfold get_node in E; destruct (g_nodes _ !! n); simplify_eq; apply same_depths_refl|intros d ?? ].
      eapply bind_depths; [intros ?? E; unfold modify_node in E; destruct (g_nodes _ !! n); simplify_eq; split; reflexivity|intros [] ??].
      eapply bind_depths; [intros ?? E; unfold edges, m_modify in E; simplify_eq; split; reflexivity|intros [] ??].
      eapply bind_depths; [intros ?? E; unfold m_modify in E; simplify_eq; split; reflexivity|intros [] ??].
      eapply bind_depths; [intros ?? E; unfold push_running, m_modify in E; simplify_eq; split; reflexivity|intros [] ??].
      eapply bind_depths; [intros ??; eapply hosted_depths; intros ???; intro H__d; eapply (B1Dexec _ IH); exact H__d|intros o ??].
      eapply bind_depths; [intros ?? E; unfold get_node in E; destruct (g_nodes _ !! n); simplify_eq; apply same_depths_refl|intros d1 ??].
      eapply bind_depths; [intros ?? E; destruct (if n_violated d1 then Fail pure_scope_failure else o); unfold modify_node, m_return in E; try destruct (g_nodes _ !! n); simplify_eq; split; reflexivity|intros [] ??].
      eapply bind_depths; [intros ?? E; unfold edges_opt in E; destruct (_ (g_edges _)); simplify_eq; split; reflexivity|intros [] ??].
      eapply bind_depths; [intros ?? E; unfold pop_running, m_modify in E; simplify_eq; split; reflexivity|intros [] ??].
      eapply bind_depths; [intros ?? E; destruct (if n_violated d1 then Fail pure_scope_failure else o); unfold modify_node, m_return in E; try destruct (g_nodes _ !! n); simplify_eq; split; reflexivity|intros [] ??].
      eapply bind_depths; [intros ?? E; unfold get_node in E; destruct (g_nodes _ !! n); simplify_eq; apply same_depths_refl|intros d2 ??].
      eapply bind_depths; [intros ?? E; destruct (decide _); [unfold m_return in E; simplify_eq; apply same_depths_refl|destruct (n_comparer d2); unfold m_bind, modify_node, m_return in E; try destruct (g_nodes _ !! n); simplify_eq; split; reflexivity]|intros mv ??].
      eapply bind_depths; [intros ?? E; unfold m_get in E; simplify_eq; apply same_depths_refl|intros st0 ??].
      eapply bind_depths; [intros ?? E; unfold m_modify, ghost_run_end in E; destruct (g_nodes _ !! n); simplify_eq; split; reflexivity|intros [] ??].
      eapply bind_depths; [|intros [] ??; intro Hg_; eapply Hghost; exact Hg_].
      intros ??; destruct mv; [|intros E; unfold m_return in E; simplify_eq; apply same_depths_refl].
      eapply bind_depths; [intros st_ [] E; unfold m_step in E; destruct (notify_dirty_except f n (g_current st0) _) eqn:Em; simplify_eq; by eapply marks_same_depths, (any_mark_rtc f n (g_current st0)), or_intror, or_intror, or_intror, or_intror|intros [] ??].
      eapply bind_depths; [intros ?? E; unfold m_get in E; simplify_eq; apply same_depths_refl|intros st1 ??].
      destruct (_ && _); [intros E; unfold m_modify in E; simplify_eq; split; reflexivity|intros E; unfold m_return in E; simplify_eq; apply same_depths_refl].
    + intros n s s' r H. cbn [b1_execute_effect] in H.
      eapply (bind_depths_of H); [intros ?? E; unfold get_node in E; destruct (g_nodes _ !! n); simplify_eq; apply same_depths_refl|intros d ?? ].
      destruct (n_disposed d); [intros E; unfold modify_node in E; destruct (g_nodes _ !! n); simplify_eq; split; reflexivity|].
      eapply bind_depths; [|intros [] ??].
      { intros ??; destruct (decide (n_freshness d = Check)); [|intros E; unfold m_return in E; simplify_eq; apply same_depths_refl].
        eapply bind_depths; [intros ??; intro H__d; eapply (B1Dwalk _ IH); exact H__d|intros [] ??].
        eapply bind_depths; [intros ?? E; unfold get_node in E; destruct (g_nodes _ !! n); simplify_eq; apply same_depths_refl|intros d' ??].
        destruct (decide (n_freshness d' = Check)); [|intros E; unfold m_return in E; simplify_eq; apply same_depths_refl].
        eapply bind_depths; [intros ?? E; unfold m_modify in E; simplify_eq; split; reflexivity|intros [] ?? E; unfold modify_node in E; destruct (g_nodes _ !! n); simplify_eq; split; reflexivity]. }
      eapply bind_depths; [intros ?? E; unfold get_node in E; destruct (g_nodes _ !! n); simplify_eq; apply same_depths_refl|intros d' ??].
      eapply bind_depths; [intros ?? E; unfold modify_node in E; destruct (g_nodes _ !! n); simplify_eq; split; reflexivity|intros [] ??].
      destruct (decide (n_freshness d' = Dirty)); [by apply (B1Drun_effect _ IH)|intros E; unfold m_return in E; simplify_eq; apply same_depths_refl].
    + intros n s s' r H. cbn [b1_run_effect] in H.
      eapply (bind_depths_of H); [intros ?? E; unfold push_running, m_modify in E; simplify_eq; split; reflexivity|intros [] ?? ].
      eapply bind_depths; [intros ?? E; unfold edges, m_modify in E; simplify_eq; split; reflexivity|intros [] ??].
      eapply bind_depths; [intros ?? E; unfold m_modify in E; simplify_eq; split; reflexivity|intros [] ??].
      eapply bind_depths; [intros ?? E; unfold get_node in E; destruct (g_nodes _ !! n); simplify_eq; apply same_depths_refl|intros d ??].
      eapply bind_depths; [|intros [] ??].
      { intros ??; destruct (n_scope d) as [o|]; [|intros E; unfold m_return in E; simplify_eq; apply same_depths_refl].
        eapply bind_depths; [intros ?? E; unfold modify_node in E; destruct (g_nodes _ !! n); simplify_eq; split; reflexivity|intros [] ??].
        eapply bind_depths; [intros ??; intro H__d; eapply (B1Ddischarge _ IH); exact H__d|intros [] ?? E; unfold modify_node in E; destruct (g_nodes _ !! n); simplify_eq; split; reflexivity]. }
      eapply bind_depths; [intros ?? E; unfold get_node in E; destruct (g_nodes _ !! n); simplify_eq; apply same_depths_refl|intros d' ??].
      eapply bind_depths; [|intros [] ?? E; unfold pop_running, m_modify in E; simplify_eq; split; reflexivity].
      intros ??; destruct (n_disposed d'); [intros E; unfold m_return in E; simplify_eq; apply same_depths_refl|by apply (B1Dbody _ IH)].
    + intros n s s' r H. cbn [b1_run_effect_body] in H.
      eapply (bind_depths_of H); [intros ?? E; unfold get_node in E; destruct (g_nodes _ !! n); simplify_eq; apply same_depths_refl|intros d ?? ].
      eapply bind_depths; [intros ?? E; unfold modify_node in E; destruct (g_nodes _ !! n); simplify_eq; split; reflexivity|intros [] ??].
      eapply bind_depths; [intros ?? E; unfold emit, m_modify in E; simplify_eq; split; reflexivity|intros [] ??].
      eapply bind_depths; [intros ??; eapply hosted_depths; intros ???; intro H__d; eapply (B1Dexec _ IH); exact H__d|intros o ??].
      eapply bind_depths; [intros ?? E; unfold get_node in E; destruct (g_nodes _ !! n); simplify_eq; apply same_depths_refl|intros d' ??].
      eapply bind_depths; [intros ?? E; unfold edges_opt in E; destruct (_ (g_edges _)); simplify_eq; split; reflexivity|intros [] ??].
      destruct o; [intros E; unfold m_return in E; simplify_eq; apply same_depths_refl|].
      eapply bind_depths; [intros ?? E; unfold modify_node in E; destruct (g_nodes _ !! n); simplify_eq; split; reflexivity|intros [] ?? E; unfold emit, m_modify in E; simplify_eq; split; reflexivity].
    + intros o s s' r H. cbn [b1_discharge] in H.
      eapply (bind_depths_of H); [intros ??; eapply detached_depths; intros ???; intro H__d; eapply (B1Ddispose_scope _ IH); exact H__d|intros [] ?? ].
      eapply bind_depths; [intros ?? E; unfold m_get in E; simplify_eq; apply same_depths_refl|intros st0 ??].
      destruct (_ && _); [intros E; unfold m_modify in E; simplify_eq; split; reflexivity|intros E; unfold m_return in E; simplify_eq; apply same_depths_refl].
    + intros o s s' r H. cbn [b1_retire] in H.
      eapply (bind_depths_of H); [|intros [] ?? ].
      { intros ?? E. eapply detached_depths; [|exact E]. intros ???.
        eapply bind_depths; [intros ?? E1; unfold get_owner in E1; destruct (g_owners _ !! o); simplify_eq; apply same_depths_refl|intros x ??].
        destruct (o_disposed x); [intros E1; unfold m_return in E1; simplify_eq; apply same_depths_refl|].
        eapply bind_depths; [intros ?? E1; unfold put_owner, m_modify in E1; simplify_eq; split; reflexivity|intros [] ?? H__d; eapply (B1Ddispose_scope _ IH); exact H__d]. }
      eapply bind_depths; [intros ?? E; unfold m_get in E; simplify_eq; apply same_depths_refl|intros st0 ??].
      destruct (live_queue st0); [by apply (B1Drequest _ IH)|intros E; unfold m_return in E; simplify_eq; apply same_depths_refl].
    + intros o s s' r H. cbn [b1_dispose_scope] in H.
      eapply (bind_depths_of H); [intros ?? E; unfold get_owner in E; destruct (g_owners _ !! o); simplify_eq; apply same_depths_refl|intros x ?? ].
      eapply bind_depths; [intros ?? E; unfold put_owner, m_modify in E; simplify_eq; split; reflexivity|intros [] ?? H__d; eapply (B1Dteardown _ IH); exact H__d].
    + intros o ls cs s s' r H. cbn [b1_teardown] in H.
      destruct ls as [|l ls'], cs as [|[[b e] a] cs'].
      * unfold m_return in H. simplify_eq. apply same_depths_refl.
      * eapply (bind_depths_of H); [intros ??; intro H__d; eapply (B1Dcleanup _ IH); exact H__d|intros [] ?? H__d; eapply (B1Dteardown _ IH); exact H__d].
      * eapply (bind_depths_of H); [intros ??; intro H__d; eapply (B1Drelease _ IH); exact H__d|intros [] ?? H__d; eapply (B1Dteardown _ IH); exact H__d].
      * destruct (decide _); [eapply (bind_depths_of H); [intros ??; intro H__d; eapply (B1Drelease _ IH); exact H__d|intros [] ?? H__d; eapply (B1Dteardown _ IH); exact H__d]|eapply (bind_depths_of H); [intros ??; intro H__d; eapply (B1Dcleanup _ IH); exact H__d|intros [] ?? H__d; eapply (B1Dteardown _ IH); exact H__d]].
    + intros o b e s s' r H. cbn [b1_run_cleanup] in H.
      eapply (bind_depths_of H); [intros ?? E; unfold get_owner in E; destruct (g_owners _ !! o); simplify_eq; apply same_depths_refl|intros x ?? ].
      eapply bind_depths; [intros ?? E; unfold emit, m_modify in E; simplify_eq; split; reflexivity|intros [] ??].
      eapply bind_depths; [intros ??; intro H__d; eapply (B1Dexec _ IH); exact H__d|intros [|] ??].
      * intros E; unfold m_return in E; simplify_eq; apply same_depths_refl.
      * eapply bind_depths; [intros ??; apply Hrec|intros [] ?? E; unfold emit, m_modify in E; simplify_eq; split; reflexivity].
    + intros n s s' r H. cbn [b1_release] in H.
      eapply (bind_depths_of H); [intros ?? E; unfold modify_node in E; destruct (g_nodes _ !! n); simplify_eq; split; reflexivity|intros [] ?? H__d; eapply (B1Ddispose _ IH); exact H__d].
    + intros n s s' r H. cbn [b1_dispose_node] in H.
      eapply (bind_depths_of H); [intros ?? E; unfold get_node in E; destruct (g_nodes _ !! n); simplify_eq; apply same_depths_refl|intros d ?? ].
      destruct (n_disposed d); [intros E; unfold m_return in E; simplify_eq; apply same_depths_refl|].
      eapply bind_depths; [intros ?? E; unfold modify_node in E; destruct (g_nodes _ !! n); simplify_eq; split; reflexivity|intros [] ??].
      eapply bind_depths; [intros ??; destruct (n_link d); [apply Hunlink|intros E; unfold m_return in E; simplify_eq; apply same_depths_refl]|intros [] ??].
      eapply bind_depths; [intros ?? E; unfold edges_opt in E; destruct (sources_clear _ _); simplify_eq; split; reflexivity|intros [] ??].
      destruct (n_scope d); [by apply (B1Dretire _ IH)|intros E; unfold m_return in E; simplify_eq; apply same_depths_refl].
    + intros n o s s' r H. cbn [b1_attach] in H.
      eapply (bind_depths_of H); [intros ?? E; unfold get_owner in E; destruct (g_owners _ !! o); simplify_eq; apply same_depths_refl|intros x ?? ].
      destruct (o_disposed x).
      * eapply bind_depths; [intros ??; intro H__d; eapply (B1Drelease _ IH); exact H__d|intros [] ?? E; unfold modify_node in E; destruct (g_nodes _ !! n); simplify_eq; split; reflexivity].
      * eapply bind_depths; [intros ?? E; unfold put_owner, m_modify in E; simplify_eq; split; reflexivity|intros [] ?? E; unfold modify_node in E; destruct (g_nodes _ !! n); simplify_eq; split; reflexivity].
    + intros n v s s' r H. cbn [b1_write_signal] in H.
      eapply (bind_depths_of H); [intros ?? E; unfold get_node in E; destruct (g_nodes _ !! n); simplify_eq; apply same_depths_refl|intros d ?? ].
      destruct (n_kind d); [|discriminate|discriminate].
      destruct (decide (n_value d = v)); [intros E; unfold m_return in E; simplify_eq; apply same_depths_refl|].
      eapply bind_depths; [intros ?? E; unfold modify_node in E; destruct (g_nodes _ !! n); simplify_eq; split; reflexivity|intros [] ??].
      eapply bind_depths; [intros ?? E; unfold m_modify, ghost_publish in E; destruct (g_nodes _ !! n); simplify_eq; split; reflexivity|intros [] ??].
      eapply bind_depths; [intros ?? E; unfold m_step in E; destruct (notify_dirty f n _) eqn:Em; simplify_eq; by eapply marks_same_depths, (any_mark_rtc f n None), or_intror, or_intror, or_intror, or_introl|intros [] ?? H__d; eapply (B1Drequest _ IH); exact H__d].
    + intros s s' r H. cbn [b1_request_flush] in H.
      eapply (bind_depths_of H); [intros ?? E; unfold m_get in E; simplify_eq; apply same_depths_refl|intros st0 ?? ].
      destruct (_ && _); [|intros E; unfold m_return in E; simplify_eq; apply same_depths_refl].
      destruct (outside_bodies st0); [by apply (B1Dflush _ IH)|].
      destruct (g_flushing st0); [intros E; unfold m_return in E; simplify_eq; apply same_depths_refl|intros E; unfold m_modify in E; simplify_eq; split; reflexivity].
    + intros s s' r H. cbn [b1_settle_flush] in H.
      eapply (bind_depths_of H); [intros ?? E; unfold m_get in E; simplify_eq; apply same_depths_refl|intros st0 ?? ].
      destruct (_ && _); [by apply (B1Dflush _ IH)|intros E; unfold m_return in E; simplify_eq; apply same_depths_refl].
    + intros s s' r H. cbn [b1_run_flush] in H.
      rewrite bind_get in H.
      destruct (g_flushing s) eqn:Ef; [unfold m_return in H; simplify_eq; apply same_depths_refl|].
      eapply bind_inv in H as (st1 & [] & E1 & H). unfold m_modify in E1; simplify_eq.
      eapply bind_inv in H as (s2 & [] & Ed & H). unfold m_modify in H; simplify_eq.
      pose proof (B1Ddrain _ IH _ _ _ Ed) as [Hp2 _]. split; [exact Hp2|simpl; symmetry; exact Ef].
    + intros s s' r H. cbn [b1_drain] in H.
      eapply (bind_depths_of H); [intros ?? E; unfold m_get in E; simplify_eq; apply same_depths_refl|intros st0 ?? ].
      destruct (g_queue st0 !! g_queue_head st0); [|intros E; unfold m_modify in E; simplify_eq; split; reflexivity].
      eapply bind_depths; [intros ?? E; unfold m_modify in E; simplify_eq; split; reflexivity|intros [] ??].
      eapply bind_depths; [intros ??; intro H__d; eapply (B1Dexecute _ IH); exact H__d|intros [] ?? H__d; eapply (B1Ddrain _ IH); exact H__d].
Qed.
References

Uses

150 more
Definition b1_state_rel (s sb : state) : Prop :=
  with_pull_depth 0 s = with_pull_depth 0 sb /\
  (g_flushing sb = false -> g_pull_depth s = g_pull_depth sb) /\
  (g_flushing sb = true -> g_pull_depth sb <= g_pull_depth s).
References
Lemma b1_state_rel_refl s : b1_state_rel s s.
Qed
Proof. split; [reflexivity|split; auto]. Qed.
References
Lemma b1_state_rel_eq s sb :
  b1_state_rel s sb -> g_pull_depth s = g_pull_depth sb -> s = sb.
Qed
Proof.
  intros [Hw _] Hpd.
  destruct s, sb; simpl in *.
  unfold with_pull_depth in Hw. injection Hw. intros. subst. reflexivity.
Qed.
References
Lemma b1_rel_fields s sb :
  b1_state_rel s sb ->
  g_flags s = g_flags sb /\ g_edges s = g_edges sb /\ g_nodes s = g_nodes sb /\
  g_owners s = g_owners sb /\ g_queue s = g_queue sb /\ g_queue_head s = g_queue_head sb /\
  g_flushing s = g_flushing sb /\ g_flush_owed s = g_flush_owed sb /\
  g_batch_depth s = g_batch_depth sb /\ g_current s = g_current sb /\
  g_untracked s = g_untracked sb /\ g_current_owner s = g_current_owner sb /\
  g_next_id s = g_next_id sb /\ g_trace s = g_trace sb /\ g_running s = g_running sb /\
  g_checking s = g_checking sb /\ g_ghost s = g_ghost sb /\ g_registered s = g_registered sb.
Qed
Proof.
  intros [Hw _]. unfold with_pull_depth in Hw. destruct s, sb; simpl in *.
  injection Hw; intros; repeat split; assumption.
Qed.
References
Lemma b1_rel_update (upd : state -> state) s sb :
  (forall st, g_pull_depth (upd st) = g_pull_depth st /\
              g_flushing (upd st) = g_flushing st /\
              with_pull_depth 0 (upd st) = upd (with_pull_depth 0 st)) ->
  b1_state_rel s sb -> b1_state_rel (upd s) (upd sb).
Qed
Proof.
  intros Hupd [Hw [Hf Ht]].
  destruct (Hupd s) as [Hp1 [Hfl1 Hw1]].
  destruct (Hupd sb) as [Hp2 [Hfl2 Hw2]].
  split; [congruence|split].
  - rewrite Hfl2, Hp1, Hp2. exact Hf.
  - rewrite Hfl2, Hp1, Hp2. exact Ht.
Qed.
References
Local Ltac b1_rel_simple :=
  match goal with
  | H : b1_state_rel ?s ?sb |- b1_state_rel _ _ =>
    let Hw := fresh "Hw" in let Hf := fresh "Hf" in let Ht := fresh "Ht" in
    destruct H as [Hw [Hf Ht]];
    split; [unfold with_pull_depth in *; destruct s, sb; simpl in *; injection Hw; intros; subst; reflexivity|simpl; split; assumption]
  end.
References
Lemma b1_rel_set_node n d s sb :
  b1_state_rel s sb -> b1_state_rel (set_node n d s) (set_node n d sb).
Qed
Proof. intro H. b1_rel_simple. Qed.
References
Lemma b1_rel_with_pull_S s sb :
  b1_state_rel s sb -> b1_state_rel (with_pull_depth (S (g_pull_depth s)) s) (with_pull_depth (S (g_pull_depth sb)) sb).
Qed
Proof.
  intros [Hw [Hf Ht]]. split; [unfold with_pull_depth in *; destruct s, sb; simpl in *; injection Hw; intros; subst; reflexivity|].
  simpl. split; [intros H; rewrite (Hf H); reflexivity|intros H; pose proof (Ht H); lia].
Qed.
References
Lemma b1_rel_with_pull_S_left s sb :
  b1_state_rel s sb -> g_flushing sb = true ->
  b1_state_rel (with_pull_depth (S (g_pull_depth s)) s) sb.
Qed
Proof.
  intros [Hw [Hf Ht]] Hfl. split.
  - rewrite <- Hw. unfold with_pull_depth. destruct s; reflexivity.
  - simpl. split; [intro H; congruence|intro H; pose proof (Ht H); lia].
Qed.
References
Lemma b1_rel_with_pull_pred s sb :
  b1_state_rel s sb -> b1_state_rel (with_pull_depth (pred (g_pull_depth s)) s) (with_pull_depth (pred (g_pull_depth sb)) sb).
Qed
Proof.
  intros [Hw [Hf Ht]]. split; [unfold with_pull_depth in *; destruct s, sb; simpl in *; injection Hw; intros; subst; reflexivity|].
  simpl. split; [intros H; rewrite (Hf H); reflexivity|intros H; pose proof (Ht H); lia].
Qed.
References
Lemma request_flush_flushing f s s' a :
  g_flushing s = true -> request_flush f s = Some (s', a) -> s' = s /\ a = tt.
Qed
Proof.
  intros Hf H. destruct f as [|f]; [discriminate|].
  cbn [request_flush] in H. rewrite bind_get in H.
  destruct (_ && _); [|unfold m_return in H; simplify_eq; done].
  destruct (outside_bodies s).
  - destruct f as [|f]; [discriminate|]. cbn [run_flush] in H. rewrite bind_get, Hf in H.
    unfold m_return in H. by simplify_eq.
  - rewrite Hf in H. unfold m_return in H. by simplify_eq.
Qed.
References

Used by

Lemma settle_flush_flushing f s s' a :
  g_flushing s = true -> settle_flush f s = Some (s', a) -> s' = s /\ a = tt.
Qed
Proof.
  intros Hf H. destruct f as [|f]; [discriminate|].
  cbn [settle_flush] in H. rewrite bind_get in H.
  destruct (_ && _); [|unfold m_return in H; simplify_eq; done].
  destruct f as [|f]; [discriminate|]. cbn [run_flush] in H. rewrite bind_get, Hf in H.
  unfold m_return in H. by simplify_eq.
Qed.
References

Used by

Lemma b1_request_flush_flushing f s s' a :
  g_flushing s = true -> b1_request_flush f s = Some (s', a) -> s' = s /\ a = tt.
Qed
Proof.
  intros Hf H. destruct f as [|f]; [discriminate|].
  cbn [b1_request_flush] in H. rewrite bind_get in H.
  destruct (_ && _); [|unfold m_return in H; simplify_eq; done].
  destruct (outside_bodies s).
  - destruct f as [|f]; [discriminate|]. cbn [b1_run_flush] in H. rewrite bind_get, Hf in H.
    unfold m_return in H. by simplify_eq.
  - rewrite Hf in H. unfold m_return in H. by simplify_eq.
Qed.
References

Used by

Lemma b1_settle_flush_flushing f s s' a :
  g_flushing s = true -> b1_settle_flush f s = Some (s', a) -> s' = s /\ a = tt.
Qed
Proof.
  intros Hf H. destruct f as [|f]; [discriminate|].
  cbn [b1_settle_flush] in H. rewrite bind_get in H.
  destruct (_ && _); [|unfold m_return in H; simplify_eq; done].
  destruct f as [|f]; [discriminate|]. cbn [b1_run_flush] in H. rewrite bind_get, Hf in H.
  unfold m_return in H. by simplify_eq.
Qed.
References

Used by

Definition sim_fwd_step {A} (m mb : nat -> M A) : Prop :=
  forall f s sb s' a, b1_state_rel s sb -> m f s = Some (s', a) ->
    exists fb sb', mb fb sb = Some (sb', a) /\ b1_state_rel s' sb'.
References

Uses

Definition sim_bwd_step {A} (m mb : nat -> M A) : Prop :=
  forall fb sb sb' a s, b1_state_rel s sb -> mb fb sb = Some (sb', a) ->
    exists f s', m f s = Some (s', a) /\ b1_state_rel s' sb'.
References

Uses

Lemma sim_bind {A B} (m1 mb1 : nat -> M A) (k1 kb1 : A -> nat -> M B)
    (mono1 : forall f1 f2 s r, f1 <= f2 -> mb1 f1 s = Some r -> mb1 f2 s = Some r)
    (mono2 : forall a f1 f2 s r, f1 <= f2 -> kb1 a f1 s = Some r -> kb1 a f2 s = Some r) :
  sim_fwd_step m1 mb1 ->
  (forall a, sim_fwd_step (k1 a) (kb1 a)) ->
  forall f s sb s' b, b1_state_rel s sb -> m_bind (m1 f) (fun a => k1 a f) s = Some (s', b) ->
    exists fb sb', m_bind (mb1 fb) (fun a => kb1 a fb) sb = Some (sb', b) /\ b1_state_rel s' sb'.
Qed
Proof.
  intros H1 H2 f s sb s' b Hrel Hb.
  unfold m_bind in Hb. destruct (m1 f s) as [[s1 a]|] eqn:E1; [|discriminate].
  destruct (H1 f s sb s1 a Hrel E1) as (fb1 & sb1 & Emb1 & Hrel1).
  destruct (H2 a f s1 sb1 s' b Hrel1 Hb) as (fb2 & sb' & Ekb2 & Hrel2).
  exists (Nat.max fb1 fb2), sb'. split; [|exact Hrel2].
  unfold m_bind.
  rewrite (mono1 fb1 (Nat.max fb1 fb2) sb (sb1, a) ltac:(lia) Emb1).
  exact (mono2 a fb2 (Nat.max fb1 fb2) sb1 (sb', b) ltac:(lia) Ekb2).
Qed.
References
Lemma sim_pure_bind {A B} (m : M A) (k1 kb1 : A -> nat -> M B)
    (Hm : forall s sb s1 a, b1_state_rel s sb -> m s = Some (s1, a) ->
      exists sb1, m sb = Some (sb1, a) /\ b1_state_rel s1 sb1) :
  (forall a, sim_fwd_step (k1 a) (kb1 a)) ->
  forall f s sb s' b, b1_state_rel s sb -> m_bind m (fun a => k1 a f) s = Some (s', b) ->
    exists fb sb', m_bind m (fun a => kb1 a fb) sb = Some (sb', b) /\ b1_state_rel s' sb'.
Qed
Proof.
  intros Hk f s sb s' b Hrel Hb.
  unfold m_bind in Hb. destruct (m s) as [[s1 a]|] eqn:E1; [|discriminate].
  destruct (Hm s sb s1 a Hrel E1) as (sb1 & Emb1 & Hrel1).
  destruct (Hk a f s1 sb1 s' b Hrel1 Hb) as (fb & sb' & Ekb & Hrel2).
  exists fb, sb'. split; [|exact Hrel2]. unfold m_bind. by rewrite Emb1.
Qed.
References
Lemma b1_rel_hosted {A} n (b bb : nat -> M A)
    (Hsim : sim_fwd_step b bb) :
  sim_fwd_step (fun f => hosted n (b f)) (fun fb => hosted n (bb fb)).
Qed
Proof.
  intros f s sb s' a Hrel H. unfold hosted in H.
  eapply bind_inv in H as (s1 & [] & E1 & H). unfold m_modify in E1. simplify_eq.
  eapply bind_inv in H as (s2 & a' & E2 & H).
  unfold m_bind, m_modify, m_return in H. simplify_eq.
  assert (Hrel1 : b1_state_rel (with_current_owner None (with_current (Some n) s))
                               (with_current_owner None (with_current (Some n) sb))).
  { b1_rel_simple. }
  destruct (Hsim f _ _ _ _ Hrel1 E2) as (fb & sb2 & Ebb & Hrel2).
  exists fb, (with_current_owner (g_current_owner sb) (with_current (g_current sb) sb2)).
  split.
  - unfold hosted, m_bind, m_modify, m_return. rewrite Ebb. reflexivity.
  - destruct (b1_rel_fields _ _ Hrel) as (_&_&_&_&_&_&_&_&_&Hc&_&Ho&_).
    destruct Hrel2 as [Hw2 [Hf2 Ht2]]. split.
    + unfold with_pull_depth in *. destruct s2, sb2; simpl in *. injection Hw2; intros; subst.
      by rewrite Hc, Ho.
    + simpl. split; assumption.
Qed.
References
Lemma b1_rel_hosted_bwd {A} n (b bb : nat -> M A)
    (Hsim : sim_bwd_step b bb) :
  sim_bwd_step (fun f => hosted n (b f)) (fun fb => hosted n (bb fb)).
Qed
Proof.
  intros fb sb sb' a s Hrel H. unfold hosted in H.
  eapply bind_inv in H as (sb1 & [] & E1 & H). unfold m_modify in E1. simplify_eq.
  eapply bind_inv in H as (sb2 & a' & E2 & H).
  unfold m_bind, m_modify, m_return in H. simplify_eq.
  assert (Hrel1 : b1_state_rel (with_current_owner None (with_current (Some n) s))
                               (with_current_owner None (with_current (Some n) sb))).
  { b1_rel_simple. }
  destruct (Hsim fb _ _ _ _ Hrel1 E2) as (f & s2 & Eb & Hrel2).
  exists f, (with_current_owner (g_current_owner s) (with_current (g_current s) s2)).
  split.
  - unfold hosted, m_bind, m_modify, m_return. rewrite Eb. reflexivity.
  - destruct (b1_rel_fields _ _ Hrel) as (_&_&_&_&_&_&_&_&_&Hc&_&Ho&_).
    destruct Hrel2 as [Hw2 [Hf2 Ht2]]. split.
    + unfold with_pull_depth in *. destruct s2, sb2; simpl in *. injection Hw2; intros; subst.
      by rewrite Hc, Ho.
    + simpl. split; assumption.
Qed.
References
Lemma b1_rel_untracked {A} (b bb : nat -> M A)
    (Hsim : sim_fwd_step b bb) :
  sim_fwd_step (fun f => untracked (b f)) (fun fb => untracked (bb fb)).
Qed
Proof.
  intros f s sb s' a Hrel H. unfold untracked in H.
  eapply bind_inv in H as (s1 & [] & E1 & H). unfold m_modify in E1. simplify_eq.
  eapply bind_inv in H as (s2 & a' & E2 & H).
  unfold m_bind, m_modify, m_return in H. simplify_eq.
  destruct (b1_rel_fields _ _ Hrel) as (_&_&_&_&_&_&_&_&_&Hc&Hu&_).
  assert (Hflag : match g_current s with Some _ => true | None => g_untracked s end =
                  match g_current sb with Some _ => true | None => g_untracked sb end) by (by rewrite Hc, Hu).
  assert (Hrel1 : b1_state_rel (with_untracked (match g_current s with Some _ => true | None => g_untracked s end) (with_current None s))
                               (with_untracked (match g_current sb with Some _ => true | None => g_untracked sb end) (with_current None sb))).
  { rewrite Hflag. b1_rel_simple. }
  destruct (Hsim f _ _ _ _ Hrel1 E2) as (fb & sb2 & Ebb & Hrel2).
  exists fb, (with_untracked (g_untracked sb) (with_current (g_current sb) sb2)).
  split.
  - unfold untracked, m_bind, m_modify, m_return. rewrite Ebb. reflexivity.
  - destruct Hrel2 as [Hw2 [Hf2 Ht2]]. split.
    + unfold with_pull_depth in *. destruct s2, sb2; simpl in *. injection Hw2; intros; subst.
      by rewrite Hc, Hu.
    + simpl. split; assumption.
Qed.
References
Lemma b1_rel_untracked_bwd {A} (b bb : nat -> M A)
    (Hsim : sim_bwd_step b bb) :
  sim_bwd_step (fun f => untracked (b f)) (fun fb => untracked (bb fb)).
Qed
Proof.
  intros fb sb sb' a s Hrel H. unfold untracked in H.
  eapply bind_inv in H as (sb1 & [] & E1 & H). unfold m_modify in E1. simplify_eq.
  eapply bind_inv in H as (sb2 & a' & E2 & H).
  unfold m_bind, m_modify, m_return in H. simplify_eq.
  destruct (b1_rel_fields _ _ Hrel) as (_&_&_&_&_&_&_&_&_&Hc&Hu&_).
  assert (Hflag : match g_current s with Some _ => true | None => g_untracked s end =
                  match g_current sb with Some _ => true | None => g_untracked sb end) by (by rewrite Hc, Hu).
  assert (Hrel1 : b1_state_rel (with_untracked (match g_current s with Some _ => true | None => g_untracked s end) (with_current None s))
                               (with_untracked (match g_current sb with Some _ => true | None => g_untracked sb end) (with_current None sb))).
  { rewrite Hflag. b1_rel_simple. }
  destruct (Hsim fb _ _ _ _ Hrel1 E2) as (f & s2 & Eb & Hrel2).
  exists f, (with_untracked (g_untracked s) (with_current (g_current s) s2)).
  split.
  - unfold untracked, m_bind, m_modify, m_return. rewrite Eb. reflexivity.
  - destruct Hrel2 as [Hw2 [Hf2 Ht2]]. split.
    + unfold with_pull_depth in *. destruct s2, sb2; simpl in *. injection Hw2; intros; subst.
      by rewrite Hc, Hu.
    + simpl. split; assumption.
Qed.
References
Lemma b1_rel_detached o (b bb : nat -> M unit)
    (Hsim : sim_fwd_step b bb) :
  sim_fwd_step (fun f => detached o (b f)) (fun fb => detached o (bb fb)).
Qed
Proof.
  intros f s sb s' [] Hrel H. unfold detached in H.
  eapply bind_inv in H as (s1 & [] & E1 & H). unfold m_modify in E1. simplify_eq.
  eapply bind_inv in H as (s2 & [] & E2 & H). unfold m_modify in H. simplify_eq.
  assert (Hrel1 : b1_state_rel (with_batch_depth (S (g_batch_depth s)) (with_current_owner (Some o) (with_current None s)))
                               (with_batch_depth (S (g_batch_depth sb)) (with_current_owner (Some o) (with_current None sb)))).
  { destruct (b1_rel_fields _ _ Hrel) as (_&_&_&_&_&_&_&_&Hbd&_). rewrite Hbd. b1_rel_simple. }
  destruct (Hsim f _ _ _ _ Hrel1 E2) as (fb & sb2 & Ebb & Hrel2).
  exists fb, (with_batch_depth (pred (g_batch_depth sb2)) (with_current_owner (g_current_owner sb) (with_current (g_current sb) sb2))).
  split.
  - unfold detached, m_bind, m_modify. rewrite Ebb. reflexivity.
  - destruct (b1_rel_fields _ _ Hrel) as (_&_&_&_&_&_&_&_&_&Hc&_&Ho&_).
    destruct Hrel2 as [Hw2 [Hf2 Ht2]]. split.
    + unfold with_pull_depth in *. destruct s2, sb2; simpl in *. injection Hw2; intros; subst.
      by rewrite Hc, Ho.
    + simpl. split; assumption.
Qed.
References
Lemma b1_rel_detached_bwd o (b bb : nat -> M unit)
    (Hsim : sim_bwd_step b bb) :
  sim_bwd_step (fun f => detached o (b f)) (fun fb => detached o (bb fb)).
Qed
Proof.
  intros fb sb sb' [] s Hrel H. unfold detached in H.
  eapply bind_inv in H as (sb1 & [] & E1 & H). unfold m_modify in E1. simplify_eq.
  eapply bind_inv in H as (sb2 & [] & E2 & H). unfold m_modify in H. simplify_eq.
  assert (Hrel1 : b1_state_rel (with_batch_depth (S (g_batch_depth s)) (with_current_owner (Some o) (with_current None s)))
                               (with_batch_depth (S (g_batch_depth sb)) (with_current_owner (Some o) (with_current None sb)))).
  { destruct (b1_rel_fields _ _ Hrel) as (_&_&_&_&_&_&_&_&Hbd&_). rewrite Hbd. b1_rel_simple. }
  destruct (Hsim fb _ _ _ _ Hrel1 E2) as (f & s2 & Eb & Hrel2).
  exists f, (with_batch_depth (pred (g_batch_depth s2)) (with_current_owner (g_current_owner s) (with_current (g_current s) s2))).
  split.
  - unfold detached, m_bind, m_modify. rewrite Eb. reflexivity.
  - destruct (b1_rel_fields _ _ Hrel) as (_&_&_&_&_&_&_&_&_&Hc&_&Ho&_).
    destruct Hrel2 as [Hw2 [Hf2 Ht2]]. split.
    + unfold with_pull_depth in *. destruct s2, sb2; simpl in *. injection Hw2; intros; subst.
      by rewrite Hc, Ho.
    + simpl. split; assumption.
Qed.
References
Lemma b1_rel_destruct s sb :
  b1_state_rel s sb ->
  sb = with_pull_depth (g_pull_depth sb) s /\
  s = with_pull_depth (g_pull_depth s) sb /\
  g_flushing s = g_flushing sb /\
  (g_flushing sb = false -> g_pull_depth s = g_pull_depth sb) /\
  (g_flushing sb = true -> g_pull_depth sb <= g_pull_depth s).
Qed
Proof.
  intros [Hw [Hf Ht]].
  assert (Hsb : sb = with_pull_depth (g_pull_depth sb) s).
  { unfold with_pull_depth in *. destruct s, sb; simpl in *. injection Hw; intros; subst; reflexivity. }
  assert (Hs : s = with_pull_depth (g_pull_depth s) sb).
  { unfold with_pull_depth in *. destruct s, sb; simpl in *. injection Hw; intros; subst; reflexivity. }
  repeat split; try assumption.
  destruct (b1_rel_fields _ _ (conj Hw (conj Hf Ht))) as (_&_&_&_&_&_&Hfl&_). exact Hfl.
Qed.
References
Lemma b1_rel_pure_step {A} (m : M A)
    (Hwp : forall p s, m (with_pull_depth p s) = option_map (fun '(s', a) => (with_pull_depth p s', a)) (m s))
    (Hdp : forall s s' a, m s = Some (s', a) -> g_flushing s' = g_flushing s /\ g_pull_depth s' = g_pull_depth s)
    s sb s' a :
  b1_state_rel s sb -> m s = Some (s', a) ->
  exists sb', m sb = Some (sb', a) /\ b1_state_rel s' sb'.
Qed
Proof.
  intros Hrel Hm.
  destruct (b1_rel_destruct _ _ Hrel) as (Hsb & _ & Hfl & Hf & Ht).
  exists (with_pull_depth (g_pull_depth sb) s').
  rewrite Hsb, Hwp, Hm. simpl.
  destruct (Hdp _ _ _ Hm) as [Hfl' Hpd'].
  split; [reflexivity|].
  split; [unfold with_pull_depth; destruct s'; reflexivity|].
  simpl. split.
  - intro H. rewrite Hfl', Hfl in H. rewrite Hpd'. exact (Hf H).
  - intro H. rewrite Hfl', Hfl in H. rewrite Hpd'. exact (Ht H).
Qed.
References
Lemma b1_rel_pure_step_bwd {A} (m : M A)
    (Hwp : forall p s, m (with_pull_depth p s) = option_map (fun '(s', a) => (with_pull_depth p s', a)) (m s))
    (Hdp : forall s s' a, m s = Some (s', a) -> g_flushing s' = g_flushing s /\ g_pull_depth s' = g_pull_depth s)
    s sb sb' a :
  b1_state_rel s sb -> m sb = Some (sb', a) ->
  exists s', m s = Some (s', a) /\ b1_state_rel s' sb'.
Qed
Proof.
  intros Hrel Hm.
  destruct (b1_rel_destruct _ _ Hrel) as (_ & Hs & Hfl & Hf & Ht).
  exists (with_pull_depth (g_pull_depth s) sb').
  rewrite Hs, Hwp, Hm. simpl.
  destruct (Hdp _ _ _ Hm) as [Hfl' Hpd'].
  split; [reflexivity|].
  split; [unfold with_pull_depth; destruct sb'; reflexivity|].
  simpl. split.
  - intro H. rewrite Hfl' in H. rewrite Hpd'. exact (Hf H).
  - intro H. rewrite Hfl' in H. rewrite Hpd'. exact (Ht H).
Qed.
References
Lemma set_node_with_pull n d p s : set_node n d (with_pull_depth p s) = with_pull_depth p (set_node n d s).
Qed
Proof. destruct s; reflexivity. Qed.
References
Lemma enqueue_with_pull n p s : enqueue n (with_pull_depth p s) = with_pull_depth p (enqueue n s).
Qed
Proof. destruct s; reflexivity. Qed.
References
Lemma walk_marks_with_pull mark src k p s :
  (forall c p0 s0, mark c (with_pull_depth p0 s0) = option_map (with_pull_depth p0) (mark c s0)) ->
  walk_marks mark src k (with_pull_depth p s) = option_map (with_pull_depth p) (walk_marks mark src k s).
Qed
Proof.
  intros Hm. revert s. induction k as [|k IH]; intros s; simpl; [reflexivity|].
  destruct (decide _); [apply IH|].
  destruct (observer_items (observers_of (g_edges s) src) !! k) as [[c _]|]; [|reflexivity].
  rewrite Hm. destruct (mark c s) as [s1|]; [simpl; apply IH|reflexivity].
Qed.
References
Lemma mark_check_with_pull f n p s :
  mark_check f n (with_pull_depth p s) = option_map (with_pull_depth p) (mark_check f n s).
Qed
Proof.
  revert p n s. induction f as [|f IH]; intros p n s; simpl; [reflexivity|].
  destruct (g_nodes s !! n) as [d|]; [|reflexivity].
  destruct (n_kind d); [reflexivity| |].
  - destruct (decide _).
    + unfold notify_with; simpl. rewrite set_node_with_pull. apply walk_marks_with_pull. intros; apply IH.
    + unfold announce_due; simpl. destruct (f_unannounced (g_flags s) && n_unannounced d); [|reflexivity].
      unfold notify_with; simpl. rewrite set_node_with_pull. apply walk_marks_with_pull. intros; apply IH.
  - simpl. unfold effect_marked.
    destruct (n_disposed d || n_in_cleanup d); [reflexivity|].
    destruct (n_queued d); rewrite set_node_with_pull; [reflexivity|].
    by rewrite enqueue_with_pull.
Qed.
References
Lemma mark_dirty_with_pull f n p s :
  mark_dirty f n (with_pull_depth p s) = option_map (with_pull_depth p) (mark_dirty f n s).
Qed
Proof.
  destruct f as [|f]; simpl; [reflexivity|].
  destruct (g_nodes s !! n) as [d|]; [|reflexivity].
  destruct (n_kind d); [reflexivity| |].
  - destruct (decide _).
    + unfold notify_with; simpl. rewrite set_node_with_pull. apply walk_marks_with_pull. intros; apply mark_check_with_pull.
    + unfold announce_due; simpl. destruct (f_unannounced (g_flags s) && n_unannounced d).
      * unfold notify_with; simpl. rewrite set_node_with_pull. apply walk_marks_with_pull. intros; apply mark_check_with_pull.
      * destruct (decide _); [reflexivity|]. by rewrite set_node_with_pull.
  - simpl. unfold effect_marked.
    destruct (n_disposed d || n_in_cleanup d); [reflexivity|].
    destruct (n_queued d); rewrite set_node_with_pull; [reflexivity|].
    by rewrite enqueue_with_pull.
Qed.
References
Lemma notify_dirty_with_pull f src p s :
  notify_dirty f src (with_pull_depth p s) = option_map (with_pull_depth p) (notify_dirty f src s).
Qed
Proof.
  unfold notify_dirty, notify_with; simpl.
  apply walk_marks_with_pull. intros; apply mark_dirty_with_pull.
Qed.
References
Lemma notify_dirty_except_with_pull f src cur p s :
  notify_dirty_except f src cur (with_pull_depth p s) = option_map (with_pull_depth p) (notify_dirty_except f src cur s).
Qed
Proof.
  unfold notify_dirty_except, notify_with; simpl.
  apply walk_marks_with_pull.
  intros c p0 s0. unfold mark_except; simpl.
  destruct (decide _); [reflexivity|apply mark_dirty_with_pull].
Qed.
References
Lemma b1_fwd_bind {A B} (m : M A) (mb : nat -> M A) (k : A -> M B) (kb : A -> nat -> M B) s sb s' b :
  (forall f1 f2 s0 r0, f1 <= f2 -> mb f1 s0 = Some r0 -> mb f2 s0 = Some r0) ->
  (forall a f1 f2 s0 r0, f1 <= f2 -> kb a f1 s0 = Some r0 -> kb a f2 s0 = Some r0) ->
  (forall s1 a, m s = Some (s1, a) ->
    exists fb1 sb1, mb fb1 sb = Some (sb1, a) /\ b1_state_rel s1 sb1) ->
  (forall a s1 sb1, m s = Some (s1, a) -> b1_state_rel s1 sb1 -> k a s1 = Some (s', b) ->
    exists fb2 sb', kb a fb2 sb1 = Some (sb', b) /\ b1_state_rel s' sb') ->
  m_bind m k s = Some (s', b) ->
  exists fb sb', m_bind (mb fb) (fun a => kb a fb) sb = Some (sb', b) /\ b1_state_rel s' sb'.
Qed
Proof.
  intros Hm1 Hk1 H1 H2 Hb.
  unfold m_bind in Hb. destruct (m s) as [[s1 a]|] eqn:E1; [|discriminate].
  destruct (H1 _ _ eq_refl) as (fb1 & sb1 & Emb1 & Hrel1).
  destruct (H2 _ _ _ eq_refl Hrel1 Hb) as (fb2 & sb' & Ekb2 & Hrel2).
  exists (Nat.max fb1 fb2), sb'. split; [|exact Hrel2].
  unfold m_bind.
  rewrite (Hm1 fb1 (Nat.max fb1 fb2) sb (sb1, a) ltac:(lia) Emb1).
  exact (Hk1 a fb2 (Nat.max fb1 fb2) sb1 (sb', b) ltac:(lia) Ekb2).
Qed.
References

Uses

Lemma b1_fwd_bind_of {A B m k s s' b} (Hb : m_bind m k s = Some (s', b))
    (mb : nat -> M A) (kb : A -> nat -> M B) sb :
  (forall f1 f2 s0 r0, f1 <= f2 -> mb f1 s0 = Some r0 -> mb f2 s0 = Some r0) ->
  (forall a f1 f2 s0 r0, f1 <= f2 -> kb a f1 s0 = Some r0 -> kb a f2 s0 = Some r0) ->
  (forall s1 a, m s = Some (s1, a) ->
    exists fb1 sb1, mb fb1 sb = Some (sb1, a) /\ b1_state_rel s1 sb1) ->
  (forall a s1 sb1, m s = Some (s1, a) -> b1_state_rel s1 sb1 -> k a s1 = Some (s', b) ->
    exists fb2 sb', kb a fb2 sb1 = Some (sb', b) /\ b1_state_rel s' sb') ->
  exists fb sb', m_bind (mb fb) (fun a => kb a fb) sb = Some (sb', b) /\ b1_state_rel s' sb'.
Qed
Proof. intros Hm1 Hk1 H1 H2. exact (b1_fwd_bind m mb k kb s sb s' b Hm1 Hk1 H1 H2 Hb). Qed.
References

Used by

Lemma b1_bwd_bind {A B} (m : nat -> M A) (mb : M A) (k : A -> nat -> M B) (kb : A -> M B) s sb sb' b :
  (forall f1 f2 s0 r0, f1 <= f2 -> m f1 s0 = Some r0 -> m f2 s0 = Some r0) ->
  (forall a f1 f2 s0 r0, f1 <= f2 -> k a f1 s0 = Some r0 -> k a f2 s0 = Some r0) ->
  (forall sb1 a, mb sb = Some (sb1, a) ->
    exists f1 s1, m f1 s = Some (s1, a) /\ b1_state_rel s1 sb1) ->
  (forall a sb1 s1, mb sb = Some (sb1, a) -> b1_state_rel s1 sb1 -> kb a sb1 = Some (sb', b) ->
    exists f2 s', k a f2 s1 = Some (s', b) /\ b1_state_rel s' sb') ->
  m_bind mb kb sb = Some (sb', b) ->
  exists f s', m_bind (m f) (fun a => k a f) s = Some (s', b) /\ b1_state_rel s' sb'.
Qed
Proof.
  intros Hm1 Hk1 H1 H2 Hb.
  unfold m_bind in Hb. destruct (mb sb) as [[sb1 a]|] eqn:E1; [|discriminate].
  destruct (H1 _ _ eq_refl) as (f1 & s1 & Em1 & Hrel1).
  destruct (H2 _ _ _ eq_refl Hrel1 Hb) as (f2 & s' & Ek2 & Hrel2).
  exists (Nat.max f1 f2), s'. split; [|exact Hrel2].
  unfold m_bind.
  rewrite (Hm1 f1 (Nat.max f1 f2) s (s1, a) ltac:(lia) Em1).
  exact (Hk1 a f2 (Nat.max f1 f2) s1 (s', b) ltac:(lia) Ek2).
Qed.
References

Uses

Lemma b1_bwd_bind_of {A B mb kb sb sb' b} (Hb : m_bind mb kb sb = Some (sb', b))
    (m : nat -> M A) (k : A -> nat -> M B) s :
  (forall f1 f2 s0 r0, f1 <= f2 -> m f1 s0 = Some r0 -> m f2 s0 = Some r0) ->
  (forall a f1 f2 s0 r0, f1 <= f2 -> k a f1 s0 = Some r0 -> k a f2 s0 = Some r0) ->
  (forall sb1 a, mb sb = Some (sb1, a) ->
    exists f1 s1, m f1 s = Some (s1, a) /\ b1_state_rel s1 sb1) ->
  (forall a sb1 s1, mb sb = Some (sb1, a) -> b1_state_rel s1 sb1 -> kb a sb1 = Some (sb', b) ->
    exists f2 s', k a f2 s1 = Some (s', b) /\ b1_state_rel s' sb') ->
  exists f s', m_bind (m f) (fun a => k a f) s = Some (s', b) /\ b1_state_rel s' sb'.
Qed
Proof. intros Hm1 Hk1 H1 H2. exact (b1_bwd_bind m mb k kb s sb sb' b Hm1 Hk1 H1 H2 Hb). Qed.
References

Used by

Lemma b1_fwd_bind_pure_l {A B} (m : M A) (k : A -> M B) (kb : A -> nat -> M B) s sb s' b :
  b1_state_rel s sb ->
  (forall s0 sb0 s1 a, b1_state_rel s0 sb0 -> m s0 = Some (s1, a) ->
    exists sb1, m sb0 = Some (sb1, a) /\ b1_state_rel s1 sb1) ->
  (forall a s1 sb1, m s = Some (s1, a) -> m sb = Some (sb1, a) -> b1_state_rel s1 sb1 ->
    k a s1 = Some (s', b) -> exists fb sb', kb a fb sb1 = Some (sb', b) /\ b1_state_rel s' sb') ->
  m_bind m k s = Some (s', b) ->
  exists fb sb', m_bind m (fun a => kb a fb) sb = Some (sb', b) /\ b1_state_rel s' sb'.
Qed
Proof.
  intros Hrel Hm Hk Hb.
  unfold m_bind in Hb. destruct (m s) as [[s1 a]|] eqn:E1; [|discriminate].
  destruct (Hm _ _ _ _ Hrel E1) as (sb1 & Emb1 & Hrel1).
  destruct (Hk _ _ _ eq_refl Emb1 Hrel1 Hb) as (fb & sb' & Ekb & Hrel2).
  exists fb, sb'. split; [|exact Hrel2]. unfold m_bind. by rewrite Emb1.
Qed.
References

Uses

Lemma b1_bwd_bind_pure_l {A B} (m : M A) (k : A -> nat -> M B) (kb : A -> M B) s sb sb' b :
  b1_state_rel s sb ->
  (forall s0 sb0 sb1 a, b1_state_rel s0 sb0 -> m sb0 = Some (sb1, a) ->
    exists s1, m s0 = Some (s1, a) /\ b1_state_rel s1 sb1) ->
  (forall a sb1 s1, m sb = Some (sb1, a) -> m s = Some (s1, a) -> b1_state_rel s1 sb1 ->
    kb a sb1 = Some (sb', b) -> exists f s', k a f s1 = Some (s', b) /\ b1_state_rel s' sb') ->
  m_bind m kb sb = Some (sb', b) ->
  exists f s', m_bind m (fun a => k a f) s = Some (s', b) /\ b1_state_rel s' sb'.
Qed
Proof.
  intros Hrel Hm Hk Hb.
  unfold m_bind in Hb. destruct (m sb) as [[sb1 a]|] eqn:E1; [|discriminate].
  destruct (Hm _ _ _ _ Hrel E1) as (s1 & Em1 & Hrel1).
  destruct (Hk _ _ _ eq_refl Em1 Hrel1 Hb) as (f & s' & Ek & Hrel2).
  exists f, s'. split; [|exact Hrel2]. unfold m_bind. by rewrite Em1.
Qed.
References

Uses

Lemma b1_fwd_bind_pure_l_of {A B m k s sb s' b}
    (Hrel : b1_state_rel s sb) (Hb : m_bind m k s = Some (s', b)) (kb : A -> nat -> M B) :
  (forall s0 sb0 s1 a, b1_state_rel s0 sb0 -> m s0 = Some (s1, a) ->
    exists sb1, m sb0 = Some (sb1, a) /\ b1_state_rel s1 sb1) ->
  (forall a s1 sb1, m s = Some (s1, a) -> m sb = Some (sb1, a) -> b1_state_rel s1 sb1 ->
    k a s1 = Some (s', b) -> exists fb sb', kb a fb sb1 = Some (sb', b) /\ b1_state_rel s' sb') ->
  exists fb sb', m_bind m (fun a => kb a fb) sb = Some (sb', b) /\ b1_state_rel s' sb'.
Qed
Proof. intros Hm Hk. exact (b1_fwd_bind_pure_l m k kb s sb s' b Hrel Hm Hk Hb). Qed.
References

Used by

Lemma b1_bwd_bind_pure_l_of {A B m kb sb s sb' b}
    (Hrel : b1_state_rel s sb) (Hb : m_bind m kb sb = Some (sb', b)) (k : A -> nat -> M B) :
  (forall s0 sb0 sb1 a, b1_state_rel s0 sb0 -> m sb0 = Some (sb1, a) ->
    exists s1, m s0 = Some (s1, a) /\ b1_state_rel s1 sb1) ->
  (forall a sb1 s1, m sb = Some (sb1, a) -> m s = Some (s1, a) -> b1_state_rel s1 sb1 ->
    kb a sb1 = Some (sb', b) -> exists f s', k a f s1 = Some (s', b) /\ b1_state_rel s' sb') ->
  exists f s', m_bind m (fun a => k a f) s = Some (s', b) /\ b1_state_rel s' sb'.
Qed
Proof. intros Hm Hk. exact (b1_bwd_bind_pure_l m k kb s sb sb' b Hrel Hm Hk Hb). Qed.
References

Used by

Lemma b1_hosted_fwd {A} n (b : M A) (bb : nat -> M A) s sb s' a :
  b1_state_rel s sb ->
  (forall s0 sb0, b1_state_rel s0 sb0 -> forall s1 a1, b s0 = Some (s1, a1) ->
    exists fb sb1, bb fb sb0 = Some (sb1, a1) /\ b1_state_rel s1 sb1) ->
  hosted n b s = Some (s', a) ->
  exists fb sb', hosted n (bb fb) sb = Some (sb', a) /\ b1_state_rel s' sb'.
Qed
Proof.
  intros Hrel Hb H. unfold hosted in H.
  eapply bind_inv in H as (s1 & [] & E1 & H). unfold m_modify in E1. simplify_eq.
  eapply bind_inv in H as (s2 & a' & E2 & H).
  unfold m_bind, m_modify, m_return in H. simplify_eq.
  assert (Hrel1 : b1_state_rel (with_current_owner None (with_current (Some n) s))
                               (with_current_owner None (with_current (Some n) sb))) by b1_rel_simple.
  destruct (Hb _ _ Hrel1 _ _ E2) as (fb & sb2 & Ebb & Hrel2).
  exists fb, (with_current_owner (g_current_owner sb) (with_current (g_current sb) sb2)).
  split.
  - unfold hosted, m_bind, m_modify, m_return. rewrite Ebb. reflexivity.
  - destruct (b1_rel_fields _ _ Hrel) as (_&_&_&_&_&_&_&_&_&Hc&_&Ho&_).
    destruct Hrel2 as [Hw2 [Hf2 Ht2]]. split.
    + unfold with_pull_depth in *. destruct s2, sb2; simpl in *. injection Hw2; intros; subst.
      by rewrite Hc, Ho.
    + simpl. split; assumption.
Qed.
References

Used by

Lemma b1_hosted_bwd {A} n (b : nat -> M A) (bb : M A) s sb sb' a :
  b1_state_rel s sb ->
  (forall s0 sb0, b1_state_rel s0 sb0 -> forall sb1 a1, bb sb0 = Some (sb1, a1) ->
    exists f s1, b f s0 = Some (s1, a1) /\ b1_state_rel s1 sb1) ->
  hosted n bb sb = Some (sb', a) ->
  exists f s', hosted n (b f) s = Some (s', a) /\ b1_state_rel s' sb'.
Qed
Proof.
  intros Hrel Hb H. unfold hosted in H.
  eapply bind_inv in H as (sb1 & [] & E1 & H). unfold m_modify in E1. simplify_eq.
  eapply bind_inv in H as (sb2 & a' & E2 & H).
  unfold m_bind, m_modify, m_return in H. simplify_eq.
  assert (Hrel1 : b1_state_rel (with_current_owner None (with_current (Some n) s))
                               (with_current_owner None (with_current (Some n) sb))) by b1_rel_simple.
  destruct (Hb _ _ Hrel1 _ _ E2) as (f & s2 & Eb & Hrel2).
  exists f, (with_current_owner (g_current_owner s) (with_current (g_current s) s2)).
  split.
  - unfold hosted, m_bind, m_modify, m_return. rewrite Eb. reflexivity.
  - destruct (b1_rel_fields _ _ Hrel) as (_&_&_&_&_&_&_&_&_&Hc&_&Ho&_).
    destruct Hrel2 as [Hw2 [Hf2 Ht2]]. split.
    + unfold with_pull_depth in *. destruct s2, sb2; simpl in *. injection Hw2; intros; subst.
      by rewrite Hc, Ho.
    + simpl. split; assumption.
Qed.
References

Used by

Lemma b1_untracked_fwd {A} (b : M A) (bb : nat -> M A) s sb s' a :
  b1_state_rel s sb ->
  (forall s0 sb0, b1_state_rel s0 sb0 -> forall s1 a1, b s0 = Some (s1, a1) ->
    exists fb sb1, bb fb sb0 = Some (sb1, a1) /\ b1_state_rel s1 sb1) ->
  untracked b s = Some (s', a) ->
  exists fb sb', untracked (bb fb) sb = Some (sb', a) /\ b1_state_rel s' sb'.
Qed
Proof.
  intros Hrel Hb H. unfold untracked in H.
  eapply bind_inv in H as (s1 & [] & E1 & H). unfold m_modify in E1. simplify_eq.
  eapply bind_inv in H as (s2 & a' & E2 & H).
  unfold m_bind, m_modify, m_return in H. simplify_eq.
  destruct (b1_rel_fields _ _ Hrel) as (_&_&_&_&_&_&_&_&_&Hc&Hu&_).
  assert (Hflag : match g_current s with Some _ => true | None => g_untracked s end =
                  match g_current sb with Some _ => true | None => g_untracked sb end) by (by rewrite Hc, Hu).
  assert (Hrel1 : b1_state_rel (with_untracked (match g_current s with Some _ => true | None => g_untracked s end) (with_current None s))
                               (with_untracked (match g_current sb with Some _ => true | None => g_untracked sb end) (with_current None sb))).
  { rewrite Hflag. b1_rel_simple. }
  destruct (Hb _ _ Hrel1 _ _ E2) as (fb & sb2 & Ebb & Hrel2).
  exists fb, (with_untracked (g_untracked sb) (with_current (g_current sb) sb2)).
  split.
  - unfold untracked, m_bind, m_modify, m_return. rewrite Ebb. reflexivity.
  - destruct Hrel2 as [Hw2 [Hf2 Ht2]]. split.
    + unfold with_pull_depth in *. destruct s2, sb2; simpl in *. injection Hw2; intros; subst.
      by rewrite Hc, Hu.
    + simpl. split; assumption.
Qed.
References

Used by

Lemma b1_untracked_bwd {A} (b : nat -> M A) (bb : M A) s sb sb' a :
  b1_state_rel s sb ->
  (forall s0 sb0, b1_state_rel s0 sb0 -> forall sb1 a1, bb sb0 = Some (sb1, a1) ->
    exists f s1, b f s0 = Some (s1, a1) /\ b1_state_rel s1 sb1) ->
  untracked bb sb = Some (sb', a) ->
  exists f s', untracked (b f) s = Some (s', a) /\ b1_state_rel s' sb'.
Qed
Proof.
  intros Hrel Hb H. unfold untracked in H.
  eapply bind_inv in H as (sb1 & [] & E1 & H). unfold m_modify in E1. simplify_eq.
  eapply bind_inv in H as (sb2 & a' & E2 & H).
  unfold m_bind, m_modify, m_return in H. simplify_eq.
  destruct (b1_rel_fields _ _ Hrel) as (_&_&_&_&_&_&_&_&_&Hc&Hu&_).
  assert (Hflag : match g_current s with Some _ => true | None => g_untracked s end =
                  match g_current sb with Some _ => true | None => g_untracked sb end) by (by rewrite Hc, Hu).
  assert (Hrel1 : b1_state_rel (with_untracked (match g_current s with Some _ => true | None => g_untracked s end) (with_current None s))
                               (with_untracked (match g_current sb with Some _ => true | None => g_untracked sb end) (with_current None sb))).
  { rewrite Hflag. b1_rel_simple. }
  destruct (Hb _ _ Hrel1 _ _ E2) as (f & s2 & Eb & Hrel2).
  exists f, (with_untracked (g_untracked s) (with_current (g_current s) s2)).
  split.
  - unfold untracked, m_bind, m_modify, m_return. rewrite Eb. reflexivity.
  - destruct Hrel2 as [Hw2 [Hf2 Ht2]]. split.
    + unfold with_pull_depth in *. destruct s2, sb2; simpl in *. injection Hw2; intros; subst.
      by rewrite Hc, Hu.
    + simpl. split; assumption.
Qed.
References

Used by

Lemma b1_detached_fwd o (b : M unit) (bb : nat -> M unit) s sb s' u :
  b1_state_rel s sb ->
  (forall s0 sb0, b1_state_rel s0 sb0 -> forall s1 a1, b s0 = Some (s1, a1) ->
    exists fb sb1, bb fb sb0 = Some (sb1, a1) /\ b1_state_rel s1 sb1) ->
  detached o b s = Some (s', u) ->
  exists fb sb', detached o (bb fb) sb = Some (sb', u) /\ b1_state_rel s' sb'.
Qed
Proof.
  intros Hrel Hb H. destruct u. unfold detached in H.
  eapply bind_inv in H as (s1 & [] & E1 & H). unfold m_modify in E1. simplify_eq.
  eapply bind_inv in H as (s2 & [] & E2 & H). unfold m_modify in H. simplify_eq.
  assert (Hrel1 : b1_state_rel (with_batch_depth (S (g_batch_depth s)) (with_current_owner (Some o) (with_current None s)))
                               (with_batch_depth (S (g_batch_depth sb)) (with_current_owner (Some o) (with_current None sb)))).
  { destruct (b1_rel_fields _ _ Hrel) as (_&_&_&_&_&_&_&_&Hbd&_). rewrite Hbd. b1_rel_simple. }
  destruct (Hb _ _ Hrel1 _ _ E2) as (fb & sb2 & Ebb & Hrel2).
  exists fb, (with_batch_depth (pred (g_batch_depth sb2)) (with_current_owner (g_current_owner sb) (with_current (g_current sb) sb2))).
  split.
  - unfold detached, m_bind, m_modify. rewrite Ebb. reflexivity.
  - destruct (b1_rel_fields _ _ Hrel) as (_&_&_&_&_&_&_&_&_&Hc&_&Ho&_).
    destruct Hrel2 as [Hw2 [Hf2 Ht2]]. split.
    + unfold with_pull_depth in *. destruct s2, sb2; simpl in *. injection Hw2; intros; subst.
      by rewrite Hc, Ho.
    + simpl. split; assumption.
Qed.
References

Used by

Lemma b1_detached_bwd o (b : nat -> M unit) (bb : M unit) s sb sb' u :
  b1_state_rel s sb ->
  (forall s0 sb0, b1_state_rel s0 sb0 -> forall sb1 a1, bb sb0 = Some (sb1, a1) ->
    exists f s1, b f s0 = Some (s1, a1) /\ b1_state_rel s1 sb1) ->
  detached o bb sb = Some (sb', u) ->
  exists f s', detached o (b f) s = Some (s', u) /\ b1_state_rel s' sb'.
Qed
Proof.
  intros Hrel Hb H. destruct u. unfold detached in H.
  eapply bind_inv in H as (sb1 & [] & E1 & H). unfold m_modify in E1. simplify_eq.
  eapply bind_inv in H as (sb2 & [] & E2 & H). unfold m_modify in H. simplify_eq.
  assert (Hrel1 : b1_state_rel (with_batch_depth (S (g_batch_depth s)) (with_current_owner (Some o) (with_current None s)))
                               (with_batch_depth (S (g_batch_depth sb)) (with_current_owner (Some o) (with_current None sb)))).
  { destruct (b1_rel_fields _ _ Hrel) as (_&_&_&_&_&_&_&_&Hbd&_). rewrite Hbd. b1_rel_simple. }
  destruct (Hb _ _ Hrel1 _ _ E2) as (f & s2 & Eb & Hrel2).
  exists f, (with_batch_depth (pred (g_batch_depth s2)) (with_current_owner (g_current_owner s) (with_current (g_current s) s2))).
  split.
  - unfold detached, m_bind, m_modify. rewrite Eb. reflexivity.
  - destruct (b1_rel_fields _ _ Hrel) as (_&_&_&_&_&_&_&_&_&Hc&_&Ho&_).
    destruct Hrel2 as [Hw2 [Hf2 Ht2]]. split.
    + unfold with_pull_depth in *. destruct s2, sb2; simpl in *. injection Hw2; intros; subst.
      by rewrite Hc, Ho.
    + simpl. split; assumption.
Qed.
References

Used by

Generic monotonicity tactics for `b1_*` and `Engine` steps.

Local Ltac b1_mono_tac :=
  intros;
  repeat (cbv beta in *; match goal with
  | H0 : m_bind ?m1 ?k1 ?s = Some ?r |- m_bind _ _ ?s = Some ?r =>
    eapply bind_mono; [..|exact H0]; [intros ?? ?|intros ???? ?]
  | H0 : hosted ?n ?b1 ?s = Some ?r |- hosted ?n _ ?s = Some ?r =>
    eapply hosted_mono; [..|exact H0]; intros ???
  | H0 : untracked ?b1 ?s = Some ?r |- untracked _ ?s = Some ?r =>
    eapply untracked_mono; [..|exact H0]; intros ???
  | H0 : detached ?o ?b1 ?s = Some ?r |- detached ?o _ ?s = Some ?r =>
    eapply detached_mono; [..|exact H0]; intros ???
  | |- (if ?b then _ else _) _ = Some _ => destruct b
  | |- match ?x with _ => _ end _ = Some _ => destruct x
  | H : ?f1 <= ?f2 |- b1_exec ?f2 _ _ _ = Some _ => by eapply (B1Mexec _ (b1_mono_all f1))
  | H : ?f1 <= ?f2 |- b1_read ?f2 _ _ _ = Some _ => by eapply (B1Mread _ (b1_mono_all f1))
  | H : ?f1 <= ?f2 |- b1_read_node ?f2 _ _ = Some _ => by eapply (B1Mread_node _ (b1_mono_all f1))
  | H : ?f1 <= ?f2 |- b1_update_if_necessary ?f2 _ _ = Some _ => by eapply (B1Mupdate _ (b1_mono_all f1))
  | H : ?f1 <= ?f2 |- b1_refresh_or_pull ?f2 _ _ = Some _ => by eapply (B1Mrop _ (b1_mono_all f1))
  | H : ?f1 <= ?f2 |- b1_pull ?f2 _ _ = Some _ => by eapply (B1Mpull _ (b1_mono_all f1))
  | H : ?f1 <= ?f2 |- b1_refresh ?f2 _ _ = Some _ => by eapply (B1Mrefresh _ (b1_mono_all f1))
  | H : ?f1 <= ?f2 |- b1_resolve_walk ?f2 _ _ _ = Some _ => by eapply (B1Mwalk _ (b1_mono_all f1))
  | H : ?f1 <= ?f2 |- b1_recompute ?f2 _ _ = Some _ => by eapply (B1Mrecompute _ (b1_mono_all f1))
  | H : ?f1 <= ?f2 |- b1_run_memo ?f2 _ _ = Some _ => by eapply (B1Mrun_memo _ (b1_mono_all f1))
  | H : ?f1 <= ?f2 |- b1_execute_effect ?f2 _ _ = Some _ => by eapply (B1Mexecute _ (b1_mono_all f1))
  | H : ?f1 <= ?f2 |- b1_run_effect ?f2 _ _ = Some _ => by eapply (B1Mrun_effect _ (b1_mono_all f1))
  | H : ?f1 <= ?f2 |- b1_run_effect_body ?f2 _ _ = Some _ => by eapply (B1Mbody _ (b1_mono_all f1))
  | H : ?f1 <= ?f2 |- b1_discharge ?f2 _ _ = Some _ => by eapply (B1Mdischarge _ (b1_mono_all f1))
  | H : ?f1 <= ?f2 |- b1_retire ?f2 _ _ = Some _ => by eapply (B1Mretire _ (b1_mono_all f1))
  | H : ?f1 <= ?f2 |- b1_dispose_scope ?f2 _ _ = Some _ => by eapply (B1Mdispose_scope _ (b1_mono_all f1))
  | H : ?f1 <= ?f2 |- b1_teardown ?f2 _ _ _ _ = Some _ => by eapply (B1Mteardown _ (b1_mono_all f1))
  | H : ?f1 <= ?f2 |- b1_run_cleanup ?f2 _ _ _ _ = Some _ => by eapply (B1Mcleanup _ (b1_mono_all f1))
  | H : ?f1 <= ?f2 |- b1_release ?f2 _ _ = Some _ => by eapply (B1Mrelease _ (b1_mono_all f1))
  | H : ?f1 <= ?f2 |- b1_dispose_node ?f2 _ _ = Some _ => by eapply (B1Mdispose _ (b1_mono_all f1))
  | H : ?f1 <= ?f2 |- b1_attach ?f2 _ _ _ = Some _ => by eapply (B1Mattach _ (b1_mono_all f1))
  | H : ?f1 <= ?f2 |- b1_write_signal ?f2 _ _ _ = Some _ => by eapply (B1Mwrite _ (b1_mono_all f1))
  | H : ?f1 <= ?f2 |- b1_request_flush ?f2 _ = Some _ => by eapply (B1Mrequest _ (b1_mono_all f1))
  | H : ?f1 <= ?f2 |- b1_settle_flush ?f2 _ = Some _ => by eapply (B1Msettle _ (b1_mono_all f1))
  | H : ?f1 <= ?f2 |- b1_run_flush ?f2 _ = Some _ => by eapply (B1Mflush _ (b1_mono_all f1))
  | H : ?f1 <= ?f2 |- b1_drain ?f2 _ = Some _ => by eapply (B1Mdrain _ (b1_mono_all f1))
  | H : ?f1 <= ?f2 |- mark_reader ?f2 _ = Some _ => by eapply mark_reader_mono
  | H : ?f1 <= ?f2 |- m_step (mark_dirty ?f2 _) _ = Some _ => by eapply m_step_mark_dirty_mono
  | H : ?f1 <= ?f2 |- m_step (mark_check ?f2 _) _ = Some _ => by eapply m_step_mark_check_mono
  | H : ?f1 <= ?f2 |- m_step (notify_dirty ?f2 _) _ = Some _ => by eapply m_step_notify_dirty_mono
  | H : ?f1 <= ?f2 |- m_step (notify_dirty_except ?f2 _ _) _ = Some _ => by eapply m_step_notify_dirty_except_mono
  | H0 : ?m ?s = Some ?r |- ?m ?s = Some ?r => exact H0
  end).
References
Local Ltac eng_mono_tac :=
  intros;
  repeat (cbv beta in *; match goal with
  | H0 : m_bind ?m1 ?k1 ?s = Some ?r |- m_bind _ _ ?s = Some ?r =>
    eapply bind_mono; [..|exact H0]; [intros ?? ?|intros ???? ?]
  | H0 : hosted ?n ?b1 ?s = Some ?r |- hosted ?n _ ?s = Some ?r =>
    eapply hosted_mono; [..|exact H0]; intros ???
  | H0 : untracked ?b1 ?s = Some ?r |- untracked _ ?s = Some ?r =>
    eapply untracked_mono; [..|exact H0]; intros ???
  | H0 : detached ?o ?b1 ?s = Some ?r |- detached ?o _ ?s = Some ?r =>
    eapply detached_mono; [..|exact H0]; intros ???
  | |- (if ?b then _ else _) _ = Some _ => destruct b
  | |- match ?x with _ => _ end _ = Some _ => destruct x
  | H : ?f1 <= ?f2 |- exec ?f2 _ _ _ = Some _ => by eapply (Mexec _ (engine_mono_all f1))
  | H : ?f1 <= ?f2 |- read ?f2 _ _ _ = Some _ => by eapply (Mread _ (engine_mono_all f1))
  | H : ?f1 <= ?f2 |- read_node ?f2 _ _ = Some _ => by eapply (Mread_node _ (engine_mono_all f1))
  | H : ?f1 <= ?f2 |- update_if_necessary ?f2 _ _ = Some _ => by eapply (Mupdate _ (engine_mono_all f1))
  | H : ?f1 <= ?f2 |- refresh_or_pull ?f2 _ _ = Some _ => by eapply (Mrop _ (engine_mono_all f1))
  | H : ?f1 <= ?f2 |- pull ?f2 _ _ = Some _ => by eapply (Mpull _ (engine_mono_all f1))
  | H : ?f1 <= ?f2 |- refresh ?f2 _ _ = Some _ => by eapply (Mrefresh _ (engine_mono_all f1))
  | H : ?f1 <= ?f2 |- resolve_walk ?f2 _ _ _ = Some _ => by eapply (Mwalk _ (engine_mono_all f1))
  | H : ?f1 <= ?f2 |- recompute ?f2 _ _ = Some _ => by eapply (Mrecompute _ (engine_mono_all f1))
  | H : ?f1 <= ?f2 |- run_memo ?f2 _ _ = Some _ => by eapply (Mrun_memo _ (engine_mono_all f1))
  | H : ?f1 <= ?f2 |- execute_effect ?f2 _ _ = Some _ => by eapply (Mexecute _ (engine_mono_all f1))
  | H : ?f1 <= ?f2 |- run_effect ?f2 _ _ = Some _ => by eapply (Mrun_effect _ (engine_mono_all f1))
  | H : ?f1 <= ?f2 |- run_effect_body ?f2 _ _ = Some _ => by eapply (Mbody _ (engine_mono_all f1))
  | H : ?f1 <= ?f2 |- discharge ?f2 _ _ = Some _ => by eapply (Mdischarge _ (engine_mono_all f1))
  | H : ?f1 <= ?f2 |- retire ?f2 _ _ = Some _ => by eapply (Mretire _ (engine_mono_all f1))
  | H : ?f1 <= ?f2 |- dispose_scope ?f2 _ _ = Some _ => by eapply (Mdispose_scope _ (engine_mono_all f1))
  | H : ?f1 <= ?f2 |- teardown ?f2 _ _ _ _ = Some _ => by eapply (Mteardown _ (engine_mono_all f1))
  | H : ?f1 <= ?f2 |- run_cleanup ?f2 _ _ _ _ = Some _ => by eapply (Mcleanup _ (engine_mono_all f1))
  | H : ?f1 <= ?f2 |- release ?f2 _ _ = Some _ => by eapply (Mrelease _ (engine_mono_all f1))
  | H : ?f1 <= ?f2 |- dispose_node ?f2 _ _ = Some _ => by eapply (Mdispose _ (engine_mono_all f1))
  | H : ?f1 <= ?f2 |- attach ?f2 _ _ _ = Some _ => by eapply (Mattach _ (engine_mono_all f1))
  | H : ?f1 <= ?f2 |- write_signal ?f2 _ _ _ = Some _ => by eapply (Mwrite _ (engine_mono_all f1))
  | H : ?f1 <= ?f2 |- request_flush ?f2 _ = Some _ => by eapply (Mrequest _ (engine_mono_all f1))
  | H : ?f1 <= ?f2 |- settle_flush ?f2 _ = Some _ => by eapply (Msettle _ (engine_mono_all f1))
  | H : ?f1 <= ?f2 |- run_flush ?f2 _ = Some _ => by eapply (Mflush _ (engine_mono_all f1))
  | H : ?f1 <= ?f2 |- drain ?f2 _ = Some _ => by eapply (Mdrain _ (engine_mono_all f1))
  | H : ?f1 <= ?f2 |- mark_reader ?f2 _ = Some _ => by eapply mark_reader_mono
  | H : ?f1 <= ?f2 |- m_step (mark_dirty ?f2 _) _ = Some _ => by eapply m_step_mark_dirty_mono
  | H : ?f1 <= ?f2 |- m_step (mark_check ?f2 _) _ = Some _ => by eapply m_step_mark_check_mono
  | H : ?f1 <= ?f2 |- m_step (notify_dirty ?f2 _) _ = Some _ => by eapply m_step_notify_dirty_mono
  | H : ?f1 <= ?f2 |- m_step (notify_dirty_except ?f2 _ _) _ = Some _ => by eapply m_step_notify_dirty_except_mono
  | H0 : ?m ?s = Some ?r |- ?m ?s = Some ?r => exact H0
  end).
References
Local Ltac b1_pure_solve :=
  intros s0 sb0 s1 a Hrel0 Hm; eapply b1_rel_pure_step; [..|exact Hrel0|exact Hm];
  [intros p st; destruct st; try reflexivity
  |intros st st' a0 Hst; destruct st; try (simplify_eq; split; reflexivity)].
References
Local Ltac b1_pure_solve_bwd :=
  intros s0 sb0 sb1 a Hrel0 Hm; eapply b1_rel_pure_step_bwd; [..|exact Hrel0|exact Hm];
  [intros p st; destruct st; try reflexivity
  |intros st st' a0 Hst; destruct st; try (simplify_eq; split; reflexivity)].
References
Lemma b1_pure_m_return {A} (a : A) s0 sb0 s1 a1 :
  b1_state_rel s0 sb0 -> m_return a s0 = Some (s1, a1) ->
  exists sb1, m_return a sb0 = Some (sb1, a1) /\ b1_state_rel s1 sb1.
Qed
Proof. intros Hrel H. unfold m_return in H |- *. simplify_eq. eauto. Qed.
References
Lemma b1_pure_m_return_bwd {A} (a : A) s0 sb0 sb1 a1 :
  b1_state_rel s0 sb0 -> m_return a sb0 = Some (sb1, a1) ->
  exists s1, m_return a s0 = Some (s1, a1) /\ b1_state_rel s1 sb1.
Qed
Proof. intros Hrel H. unfold m_return in H |- *. simplify_eq. eauto. Qed.
References
Lemma b1_pure_get_node n s0 sb0 s1 d :
  b1_state_rel s0 sb0 -> get_node n s0 = Some (s1, d) ->
  exists sb1, get_node n sb0 = Some (sb1, d) /\ b1_state_rel s1 sb1.
Qed
Proof.
  eapply b1_rel_pure_step; [intros p []; unfold get_node; simpl; destruct (g_nodes !! n); reflexivity|intros ??? H; unfold get_node in H; destruct (g_nodes _ !! n); simplify_eq; split; reflexivity].
Qed.
References
Lemma b1_pure_get_node_bwd n s0 sb0 sb1 d :
  b1_state_rel s0 sb0 -> get_node n sb0 = Some (sb1, d) ->
  exists s1, get_node n s0 = Some (s1, d) /\ b1_state_rel s1 sb1.
Qed
Proof.
  eapply b1_rel_pure_step_bwd; [intros p []; unfold get_node; simpl; destruct (g_nodes !! n); reflexivity|intros ??? H; unfold get_node in H; destruct (g_nodes _ !! n); simplify_eq; split; reflexivity].
Qed.
References
Lemma b1_pure_modify_node n u s0 sb0 s1 r :
  b1_state_rel s0 sb0 -> modify_node n u s0 = Some (s1, r) ->
  exists sb1, modify_node n u sb0 = Some (sb1, r) /\ b1_state_rel s1 sb1.
Qed
Proof.
  eapply b1_rel_pure_step; [intros p []; unfold modify_node; simpl; destruct (g_nodes !! n); reflexivity|intros ??? H; unfold modify_node in H; destruct (g_nodes _ !! n); simplify_eq; split; reflexivity].
Qed.
References
Lemma b1_pure_modify_node_bwd n u s0 sb0 sb1 r :
  b1_state_rel s0 sb0 -> modify_node n u sb0 = Some (sb1, r) ->
  exists s1, modify_node n u s0 = Some (s1, r) /\ b1_state_rel s1 sb1.
Qed
Proof.
  eapply b1_rel_pure_step_bwd; [intros p []; unfold modify_node; simpl; destruct (g_nodes !! n); reflexivity|intros ??? H; unfold modify_node in H; destruct (g_nodes _ !! n); simplify_eq; split; reflexivity].
Qed.
References
Lemma b1_pure_get_owner o s0 sb0 s1 x :
  b1_state_rel s0 sb0 -> get_owner o s0 = Some (s1, x) ->
  exists sb1, get_owner o sb0 = Some (sb1, x) /\ b1_state_rel s1 sb1.
Qed
Proof.
  eapply b1_rel_pure_step; [intros p []; unfold get_owner; simpl; destruct (g_owners !! o); reflexivity|intros ??? H; unfold get_owner in H; destruct (g_owners _ !! o); simplify_eq; split; reflexivity].
Qed.
References
Lemma b1_pure_get_owner_bwd o s0 sb0 sb1 x :
  b1_state_rel s0 sb0 -> get_owner o sb0 = Some (sb1, x) ->
  exists s1, get_owner o s0 = Some (s1, x) /\ b1_state_rel s1 sb1.
Qed
Proof.
  eapply b1_rel_pure_step_bwd; [intros p []; unfold get_owner; simpl; destruct (g_owners !! o); reflexivity|intros ??? H; unfold get_owner in H; destruct (g_owners _ !! o); simplify_eq; split; reflexivity].
Qed.
References
Lemma b1_pure_edges_opt u s0 sb0 s1 r :
  b1_state_rel s0 sb0 -> edges_opt u s0 = Some (s1, r) ->
  exists sb1, edges_opt u sb0 = Some (sb1, r) /\ b1_state_rel s1 sb1.
Qed
Proof.
  eapply b1_rel_pure_step; [intros p []; unfold edges_opt; simpl; destruct (u _); reflexivity|intros ??? H; unfold edges_opt in H; destruct (u _); simplify_eq; split; reflexivity].
Qed.
References
Lemma b1_pure_edges_opt_bwd u s0 sb0 sb1 r :
  b1_state_rel s0 sb0 -> edges_opt u sb0 = Some (sb1, r) ->
  exists s1, edges_opt u s0 = Some (s1, r) /\ b1_state_rel s1 sb1.
Qed
Proof.
  eapply b1_rel_pure_step_bwd; [intros p []; unfold edges_opt; simpl; destruct (u _); reflexivity|intros ??? H; unfold edges_opt in H; destruct (u _); simplify_eq; split; reflexivity].
Qed.
References
Lemma b1_pure_current_owner s0 sb0 s1 r :
  b1_state_rel s0 sb0 -> current_owner s0 = Some (s1, r) ->
  exists sb1, current_owner sb0 = Some (sb1, r) /\ b1_state_rel s1 sb1.
Qed
Proof.
  eapply b1_rel_pure_step.
  - intros p []. unfold current_owner. simpl.
    destruct g_current_owner, g_current; try reflexivity.
    destruct (g_nodes !! n) as [d|]; [|reflexivity].
    destruct (n_scope d); [reflexivity|].
    destruct (match n_kind d, n_mode d with KMemo, Pure => true | _, _ => false end); reflexivity.
  - intros s s' a H. unfold current_owner in H.
    destruct (g_current_owner s), (g_current s); simplify_eq; try (split; reflexivity).
    destruct (g_nodes s !! _) as [d|]; [|simplify_eq].
    destruct (n_scope d); simplify_eq; try (split; reflexivity).
    destruct (match n_kind d, n_mode d with KMemo, Pure => true | _, _ => false end); simplify_eq; split; reflexivity.
Qed.
References
Lemma b1_pure_current_owner_bwd s0 sb0 sb1 r :
  b1_state_rel s0 sb0 -> current_owner sb0 = Some (sb1, r) ->
  exists s1, current_owner s0 = Some (s1, r) /\ b1_state_rel s1 sb1.
Qed
Proof.
  eapply b1_rel_pure_step_bwd.
  - intros p []. unfold current_owner. simpl.
    destruct g_current_owner, g_current; try reflexivity.
    destruct (g_nodes !! n) as [d|]; [|reflexivity].
    destruct (n_scope d); [reflexivity|].
    destruct (match n_kind d, n_mode d with KMemo, Pure => true | _, _ => false end); reflexivity.
  - intros s s' a H. unfold current_owner in H.
    destruct (g_current_owner s), (g_current s); simplify_eq; try (split; reflexivity).
    destruct (g_nodes s !! _) as [d|]; [|simplify_eq].
    destruct (n_scope d); simplify_eq; try (split; reflexivity).
    destruct (match n_kind d, n_mode d with KMemo, Pure => true | _, _ => false end); simplify_eq; split; reflexivity.
Qed.
References
Lemma b1_pure_note_host_read n o s0 sb0 s1 r :
  b1_state_rel s0 sb0 -> note_host_read n o s0 = Some (s1, r) ->
  exists sb1, note_host_read n o sb0 = Some (sb1, r) /\ b1_state_rel s1 sb1.
Qed
Proof.
  eapply b1_rel_pure_step.
  - intros p []. unfold note_host_read, emit, m_modify. simpl.
    destruct g_current, g_current_owner; try reflexivity.
    destruct (decide _); reflexivity.
  - intros s s' a H. unfold note_host_read, emit, m_modify in H.
    destruct (g_current s), (g_current_owner s); simplify_eq; try (split; reflexivity).
    destruct (decide _); simplify_eq; split; reflexivity.
Qed.
References
Lemma b1_pure_note_host_read_bwd n o s0 sb0 sb1 r :
  b1_state_rel s0 sb0 -> note_host_read n o sb0 = Some (sb1, r) ->
  exists s1, note_host_read n o s0 = Some (s1, r) /\ b1_state_rel s1 sb1.
Qed
Proof.
  eapply b1_rel_pure_step_bwd.
  - intros p []. unfold note_host_read, emit, m_modify. simpl.
    destruct g_current, g_current_owner; try reflexivity.
    destruct (decide _); reflexivity.
  - intros s s' a H. unfold note_host_read, emit, m_modify in H.
    destruct (g_current s), (g_current_owner s); simplify_eq; try (split; reflexivity).
    destruct (decide _); simplify_eq; split; reflexivity.
Qed.
References
Lemma b1_pure_track n s0 sb0 s1 r :
  b1_state_rel s0 sb0 -> track n s0 = Some (s1, r) ->
  exists sb1, track n sb0 = Some (sb1, r) /\ b1_state_rel s1 sb1.
Qed
Proof.
  eapply b1_rel_pure_step.
  - intros p []. unfold track, live_current. simpl.
    destruct g_current; [|reflexivity].
    destruct (g_nodes !! n0) as [d|]; [|reflexivity].
    destruct (n_disposed d); [reflexivity|].
    destruct (sources_add _ _ _); [|reflexivity].
    unfold ghost_read; simpl. destruct (g_nodes !! n); reflexivity.
  - intros s s' a H. unfold track, live_current in H.
    destruct (g_current s); simplify_eq; try (split; reflexivity).
    destruct (g_nodes s !! _) as [d|]; [|simplify_eq; split; reflexivity].
    destruct (n_disposed d); simplify_eq; [split; reflexivity|].
    destruct (sources_add _ _ _); simplify_eq; unfold ghost_read; simpl; try destruct (g_nodes s !! n); split; reflexivity.
Qed.
References
Lemma b1_pure_track_bwd n s0 sb0 sb1 r :
  b1_state_rel s0 sb0 -> track n sb0 = Some (sb1, r) ->
  exists s1, track n s0 = Some (s1, r) /\ b1_state_rel s1 sb1.
Qed
Proof.
  eapply b1_rel_pure_step_bwd.
  - intros p []. unfold track, live_current. simpl.
    destruct g_current; [|reflexivity].
    destruct (g_nodes !! n0) as [d|]; [|reflexivity].
    destruct (n_disposed d); [reflexivity|].
    destruct (sources_add _ _ _); [|reflexivity].
    unfold ghost_read; simpl. destruct (g_nodes !! n); reflexivity.
  - intros s s' a H. unfold track, live_current in H.
    destruct (g_current s); simplify_eq; try (split; reflexivity).
    destruct (g_nodes s !! _) as [d|]; [|simplify_eq; split; reflexivity].
    destruct (n_disposed d); simplify_eq; [split; reflexivity|].
    destruct (sources_add _ _ _); simplify_eq; unfold ghost_read; simpl; try destruct (g_nodes s !! n); split; reflexivity.
Qed.
References
Lemma b1_pure_mark_reader f s0 sb0 s1 r :
  b1_state_rel s0 sb0 -> mark_reader f s0 = Some (s1, r) ->
  exists sb1, mark_reader f sb0 = Some (sb1, r) /\ b1_state_rel s1 sb1.
Qed
Proof.
  eapply b1_rel_pure_step.
  - intros p s. unfold mark_reader; simpl.
    destruct (g_current s), (f_reader_mark (g_flags s)); try reflexivity.
    rewrite mark_check_with_pull. destruct (mark_check _ _ _); reflexivity.
  - intros s s' a H. unfold mark_reader in H.
    destruct (g_current s), (f_reader_mark (g_flags s)); simplify_eq; try (split; reflexivity).
    destruct (mark_check f _ s) eqn:Em; simplify_eq.
    edestruct marks_same_depths as [-> ->]; [eapply (any_mark_rtc f _ None), or_introl, Em|split; reflexivity].
Qed.
References
Lemma b1_pure_mark_reader_bwd f s0 sb0 sb1 r :
  b1_state_rel s0 sb0 -> mark_reader f sb0 = Some (sb1, r) ->
  exists s1, mark_reader f s0 = Some (s1, r) /\ b1_state_rel s1 sb1.
Qed
Proof.
  eapply b1_rel_pure_step_bwd.
  - intros p s. unfold mark_reader; simpl.
    destruct (g_current s), (f_reader_mark (g_flags s)); try reflexivity.
    rewrite mark_check_with_pull. destruct (mark_check _ _ _); reflexivity.
  - intros s s' a H. unfold mark_reader in H.
    destruct (g_current s), (f_reader_mark (g_flags s)); simplify_eq; try (split; reflexivity).
    destruct (mark_check f _ s) eqn:Em; simplify_eq.
    edestruct marks_same_depths as [-> ->]; [eapply (any_mark_rtc f _ None), or_introl, Em|split; reflexivity].
Qed.
References
Lemma b1_pure_announce n s0 sb0 s1 r :
  b1_state_rel s0 sb0 -> announce n s0 = Some (s1, r) ->
  exists sb1, announce n sb0 = Some (sb1, r) /\ b1_state_rel s1 sb1.
Qed
Proof.
  eapply b1_rel_pure_step.
  - intros p []. unfold announce, modify_node. simpl.
    destruct (f_unannounced g_flags); [destruct (g_nodes !! n); reflexivity|reflexivity].
  - intros ??? H. unfold announce, modify_node in H.
    destruct (f_unannounced _); [destruct (g_nodes _ !! n); simplify_eq; split; reflexivity|simplify_eq; split; reflexivity].
Qed.
References
Lemma b1_pure_announce_bwd n s0 sb0 sb1 r :
  b1_state_rel s0 sb0 -> announce n sb0 = Some (sb1, r) ->
  exists s1, announce n s0 = Some (s1, r) /\ b1_state_rel s1 sb1.
Qed
Proof.
  eapply b1_rel_pure_step_bwd.
  - intros p []. unfold announce, modify_node. simpl.
    destruct (f_unannounced g_flags); [destruct (g_nodes !! n); reflexivity|reflexivity].
  - intros ??? H. unfold announce, modify_node in H.
    destruct (f_unannounced _); [destruct (g_nodes _ !! n); simplify_eq; split; reflexivity|simplify_eq; split; reflexivity].
Qed.
References
Lemma b1_pure_record_error o y s0 sb0 s1 r :
  b1_state_rel s0 sb0 -> record_error o y s0 = Some (s1, r) ->
  exists sb1, record_error o y sb0 = Some (sb1, r) /\ b1_state_rel s1 sb1.
Qed
Proof.
  eapply b1_rel_pure_step.
  - intros p []. unfold record_error, m_bind, get_owner, put_owner, m_modify. simpl.
    destruct (g_owners !! o) as [x|]; simpl; [|reflexivity].
    destruct (o_sink x) as [os|]; simpl; [destruct (g_owners !! os); simpl; reflexivity|reflexivity].
  - intros ??? H. unfold record_error, m_bind, get_owner, put_owner, m_modify in H.
    destruct (g_owners _ !! o) as [x|]; [|simplify_eq].
    destruct (o_sink x) as [os|]; [destruct (g_owners _ !! os); simplify_eq; split; reflexivity|simplify_eq; split; reflexivity].
Qed.
References
Lemma b1_pure_record_error_bwd o y s0 sb0 sb1 r :
  b1_state_rel s0 sb0 -> record_error o y sb0 = Some (sb1, r) ->
  exists s1, record_error o y s0 = Some (s1, r) /\ b1_state_rel s1 sb1.
Qed
Proof.
  eapply b1_rel_pure_step_bwd.
  - intros p []. unfold record_error, m_bind, get_owner, put_owner, m_modify. simpl.
    destruct (g_owners !! o) as [x|]; simpl; [|reflexivity].
    destruct (o_sink x) as [os|]; simpl; [destruct (g_owners !! os); simpl; reflexivity|reflexivity].
  - intros ??? H. unfold record_error, m_bind, get_owner, put_owner, m_modify in H.
    destruct (g_owners _ !! o) as [x|]; [|simplify_eq].
    destruct (o_sink x) as [os|]; [destruct (g_owners _ !! os); simplify_eq; split; reflexivity|simplify_eq; split; reflexivity].
Qed.
References
Lemma b1_pure_log_label s0 sb0 s1 r :
  b1_state_rel s0 sb0 -> log_label s0 = Some (s1, r) ->
  exists sb1, log_label sb0 = Some (sb1, r) /\ b1_state_rel s1 sb1.
Qed
Proof.
  eapply b1_rel_pure_step.
  - intros p []. unfold log_label. simpl. destruct g_current, g_current_owner; reflexivity.
  - intros s s' a H. unfold log_label in H. destruct (g_current s), (g_current_owner s); simplify_eq; split; reflexivity.
Qed.
References
Lemma b1_pure_log_label_bwd s0 sb0 sb1 r :
  b1_state_rel s0 sb0 -> log_label sb0 = Some (sb1, r) ->
  exists s1, log_label s0 = Some (s1, r) /\ b1_state_rel s1 sb1.
Qed
Proof.
  eapply b1_rel_pure_step_bwd.
  - intros p []. unfold log_label. simpl. destruct g_current, g_current_owner; reflexivity.
  - intros s s' a H. unfold log_label in H. destruct (g_current s), (g_current_owner s); simplify_eq; split; reflexivity.
Qed.
References
Lemma b1_pure_mark_dirty f n s0 sb0 s1 r :
  b1_state_rel s0 sb0 -> m_step (mark_dirty f n) s0 = Some (s1, r) ->
  exists sb1, m_step (mark_dirty f n) sb0 = Some (sb1, r) /\ b1_state_rel s1 sb1.
Qed
Proof.
  eapply b1_rel_pure_step.
  - intros p s. unfold m_step. rewrite mark_dirty_with_pull. destruct (mark_dirty _ _ _); reflexivity.
  - intros ??? H. unfold m_step in H. destruct (mark_dirty _ _ _) eqn:Em; simplify_eq.
    edestruct marks_same_depths as [-> ->]; [eapply (any_mark_rtc f n None), or_intror, or_introl, Em|split; reflexivity].
Qed.
References
Lemma b1_pure_mark_dirty_bwd f n s0 sb0 sb1 r :
  b1_state_rel s0 sb0 -> m_step (mark_dirty f n) sb0 = Some (sb1, r) ->
  exists s1, m_step (mark_dirty f n) s0 = Some (s1, r) /\ b1_state_rel s1 sb1.
Qed
Proof.
  eapply b1_rel_pure_step_bwd.
  - intros p s. unfold m_step. rewrite mark_dirty_with_pull. destruct (mark_dirty _ _ _); reflexivity.
  - intros ??? H. unfold m_step in H. destruct (mark_dirty _ _ _) eqn:Em; simplify_eq.
    edestruct marks_same_depths as [-> ->]; [eapply (any_mark_rtc f n None), or_intror, or_introl, Em|split; reflexivity].
Qed.
References
Lemma b1_pure_notify_dirty f src s0 sb0 s1 r :
  b1_state_rel s0 sb0 -> m_step (notify_dirty f src) s0 = Some (s1, r) ->
  exists sb1, m_step (notify_dirty f src) sb0 = Some (sb1, r) /\ b1_state_rel s1 sb1.
Qed
Proof.
  eapply b1_rel_pure_step.
  - intros p s. unfold m_step. rewrite notify_dirty_with_pull. destruct (notify_dirty _ _ _); reflexivity.
  - intros ??? H. unfold m_step in H. destruct (notify_dirty _ _ _) eqn:Em; simplify_eq.
    edestruct marks_same_depths as [-> ->]; [eapply (any_mark_rtc f src None), or_intror, or_intror, or_intror, or_introl, Em|split; reflexivity].
Qed.
References
Lemma b1_pure_notify_dirty_bwd f src s0 sb0 sb1 r :
  b1_state_rel s0 sb0 -> m_step (notify_dirty f src) sb0 = Some (sb1, r) ->
  exists s1, m_step (notify_dirty f src) s0 = Some (s1, r) /\ b1_state_rel s1 sb1.
Qed
Proof.
  eapply b1_rel_pure_step_bwd.
  - intros p s. unfold m_step. rewrite notify_dirty_with_pull. destruct (notify_dirty _ _ _); reflexivity.
  - intros ??? H. unfold m_step in H. destruct (notify_dirty _ _ _) eqn:Em; simplify_eq.
    edestruct marks_same_depths as [-> ->]; [eapply (any_mark_rtc f src None), or_intror, or_intror, or_intror, or_introl, Em|split; reflexivity].
Qed.
References
Lemma b1_pure_notify_dirty_except f src cur s0 sb0 s1 r :
  b1_state_rel s0 sb0 -> m_step (notify_dirty_except f src cur) s0 = Some (s1, r) ->
  exists sb1, m_step (notify_dirty_except f src cur) sb0 = Some (sb1, r) /\ b1_state_rel s1 sb1.
Qed
Proof.
  eapply b1_rel_pure_step.
  - intros p s. unfold m_step. rewrite notify_dirty_except_with_pull. destruct (notify_dirty_except _ _ _ _); reflexivity.
  - intros ??? H. unfold m_step in H. destruct (notify_dirty_except _ _ _ _) eqn:Em; simplify_eq.
    edestruct marks_same_depths as [-> ->]; [eapply (any_mark_rtc f src cur), or_intror, or_intror, or_intror, or_intror, Em|split; reflexivity].
Qed.
References
Lemma b1_pure_notify_dirty_except_bwd f src cur s0 sb0 sb1 r :
  b1_state_rel s0 sb0 -> m_step (notify_dirty_except f src cur) sb0 = Some (sb1, r) ->
  exists s1, m_step (notify_dirty_except f src cur) s0 = Some (s1, r) /\ b1_state_rel s1 sb1.
Qed
Proof.
  eapply b1_rel_pure_step_bwd.
  - intros p s. unfold m_step. rewrite notify_dirty_except_with_pull. destruct (notify_dirty_except _ _ _ _); reflexivity.
  - intros ??? H. unfold m_step in H. destruct (notify_dirty_except _ _ _ _) eqn:Em; simplify_eq.
    edestruct marks_same_depths as [-> ->]; [eapply (any_mark_rtc f src cur), or_intror, or_intror, or_intror, or_intror, Em|split; reflexivity].
Qed.
References
Record b1_fwd_at (f : nat) : Prop := {
  Fexec1 : forall e c s sb s' r, b1_state_rel s sb -> exec f e c s = Some (s', r) ->
    exists fb sb', b1_exec fb e c sb = Some (sb', r) /\ b1_state_rel s' sb';
  Fread1 : forall n b s sb s' r, b1_state_rel s sb -> read f n b s = Some (s', r) ->
    exists fb sb', b1_read fb n b sb = Some (sb', r) /\ b1_state_rel s' sb';
  Fread_node1 : forall n s sb s' r, b1_state_rel s sb -> read_node f n s = Some (s', r) ->
    exists fb sb', b1_read_node fb n sb = Some (sb', r) /\ b1_state_rel s' sb';
  Fupdate1 : forall n s sb s' r, b1_state_rel s sb -> update_if_necessary f n s = Some (s', r) ->
    exists fb sb', b1_update_if_necessary fb n sb = Some (sb', r) /\ b1_state_rel s' sb';
  Frop1 : forall n s sb s' r, b1_state_rel s sb -> refresh_or_pull f n s = Some (s', r) ->
    exists fb sb', b1_refresh_or_pull fb n sb = Some (sb', r) /\ b1_state_rel s' sb';
  Fpull1 : forall n s sb s' r, b1_state_rel s sb -> pull f n s = Some (s', r) ->
    exists fb sb', b1_pull fb n sb = Some (sb', r) /\ b1_state_rel s' sb';
  Frefresh1 : forall n s sb s' r, b1_state_rel s sb -> refresh f n s = Some (s', r) ->
    exists fb sb', b1_refresh fb n sb = Some (sb', r) /\ b1_state_rel s' sb';
  Fwalk1 : forall n i s sb s' r, b1_state_rel s sb -> resolve_walk f n i s = Some (s', r) ->
    exists fb sb', b1_resolve_walk fb n i sb = Some (sb', r) /\ b1_state_rel s' sb';
  Frecompute1 : forall n s sb s' r, b1_state_rel s sb -> recompute f n s = Some (s', r) ->
    exists fb sb', b1_recompute fb n sb = Some (sb', r) /\ b1_state_rel s' sb';
  Frun_memo1 : forall n s sb s' r, b1_state_rel s sb -> run_memo f n s = Some (s', r) ->
    exists fb sb', b1_run_memo fb n sb = Some (sb', r) /\ b1_state_rel s' sb';
  Fexecute1 : forall n s sb s' r, b1_state_rel s sb -> execute_effect f n s = Some (s', r) ->
    exists fb sb', b1_execute_effect fb n sb = Some (sb', r) /\ b1_state_rel s' sb';
  Frun_effect1 : forall n s sb s' r, b1_state_rel s sb -> run_effect f n s = Some (s', r) ->
    exists fb sb', b1_run_effect fb n sb = Some (sb', r) /\ b1_state_rel s' sb';
  Fbody1 : forall n s sb s' r, b1_state_rel s sb -> run_effect_body f n s = Some (s', r) ->
    exists fb sb', b1_run_effect_body fb n sb = Some (sb', r) /\ b1_state_rel s' sb';
  Fdischarge1 : forall o s sb s' r, b1_state_rel s sb -> discharge f o s = Some (s', r) ->
    exists fb sb', b1_discharge fb o sb = Some (sb', r) /\ b1_state_rel s' sb';
  Fretire1 : forall o s sb s' r, b1_state_rel s sb -> retire f o s = Some (s', r) ->
    exists fb sb', b1_retire fb o sb = Some (sb', r) /\ b1_state_rel s' sb';
  Fdispose_scope1 : forall o s sb s' r, b1_state_rel s sb -> dispose_scope f o s = Some (s', r) ->
    exists fb sb', b1_dispose_scope fb o sb = Some (sb', r) /\ b1_state_rel s' sb';
  Fteardown1 : forall o ls cs s sb s' r, b1_state_rel s sb -> teardown f o ls cs s = Some (s', r) ->
    exists fb sb', b1_teardown fb o ls cs sb = Some (sb', r) /\ b1_state_rel s' sb';
  Fcleanup1 : forall o b e s sb s' r, b1_state_rel s sb -> run_cleanup f o b e s = Some (s', r) ->
    exists fb sb', b1_run_cleanup fb o b e sb = Some (sb', r) /\ b1_state_rel s' sb';
  Frelease1 : forall n s sb s' r, b1_state_rel s sb -> release f n s = Some (s', r) ->
    exists fb sb', b1_release fb n sb = Some (sb', r) /\ b1_state_rel s' sb';
  Fdispose1 : forall n s sb s' r, b1_state_rel s sb -> dispose_node f n s = Some (s', r) ->
    exists fb sb', b1_dispose_node fb n sb = Some (sb', r) /\ b1_state_rel s' sb';
  Fattach1 : forall n o s sb s' r, b1_state_rel s sb -> attach f n o s = Some (s', r) ->
    exists fb sb', b1_attach fb n o sb = Some (sb', r) /\ b1_state_rel s' sb';
  Fwrite1 : forall n v s sb s' r, b1_state_rel s sb -> write_signal f n v s = Some (s', r) ->
    exists fb sb', b1_write_signal fb n v sb = Some (sb', r) /\ b1_state_rel s' sb';
  Frequest1 : forall s sb s' r, b1_state_rel s sb -> request_flush f s = Some (s', r) ->
    exists fb sb', b1_request_flush fb sb = Some (sb', r) /\ b1_state_rel s' sb';
  Fsettle1 : forall s sb s' r, b1_state_rel s sb -> settle_flush f s = Some (s', r) ->
    exists fb sb', b1_settle_flush fb sb = Some (sb', r) /\ b1_state_rel s' sb';
  Fflush1 : forall s sb s' r, b1_state_rel s sb -> run_flush f s = Some (s', r) ->
    exists fb sb', b1_run_flush fb sb = Some (sb', r) /\ b1_state_rel s' sb';
  Fdrain1 : forall s sb s' r, b1_state_rel s sb -> drain f s = Some (s', r) ->
    exists fb sb', b1_drain fb sb = Some (sb', r) /\ b1_state_rel s' sb'
}.
References
Lemma b1_pure_modify upd
    (Hupd : forall p s, upd (with_pull_depth p s) = with_pull_depth p (upd s) /\
                        g_flushing (upd s) = g_flushing s /\ g_pull_depth (upd s) = g_pull_depth s)
    s0 sb0 s1 r :
  b1_state_rel s0 sb0 -> m_modify upd s0 = Some (s1, r) ->
  exists sb1, m_modify upd sb0 = Some (sb1, r) /\ b1_state_rel s1 sb1.
Qed
Proof.
  eapply b1_rel_pure_step.
  - intros p s. unfold m_modify. destruct (Hupd p s) as [Hw _]. by rewrite Hw.
  - intros s s' [] H. unfold m_modify in H. simplify_eq. by destruct (Hupd 0 s) as [_ ?].
Qed.
References
Lemma b1_pure_modify_bwd upd
    (Hupd : forall p s, upd (with_pull_depth p s) = with_pull_depth p (upd s) /\
                        g_flushing (upd s) = g_flushing s /\ g_pull_depth (upd s) = g_pull_depth s)
    s0 sb0 sb1 r :
  b1_state_rel s0 sb0 -> m_modify upd sb0 = Some (sb1, r) ->
  exists s1, m_modify upd s0 = Some (s1, r) /\ b1_state_rel s1 sb1.
Qed
Proof.
  eapply b1_rel_pure_step_bwd.
  - intros p s. unfold m_modify. destruct (Hupd p s) as [Hw _]. by rewrite Hw.
  - intros s s' [] H. unfold m_modify in H. simplify_eq. by destruct (Hupd 0 s) as [_ ?].
Qed.
References
Lemma b1_pure_next_id s0 sb0 s1 r :
  b1_state_rel s0 sb0 -> next_id s0 = Some (s1, r) ->
  exists sb1, next_id sb0 = Some (sb1, r) /\ b1_state_rel s1 sb1.
Qed
Proof.
  eapply b1_rel_pure_step; [intros p []; unfold next_id; reflexivity|intros ??? H; unfold next_id in H; simplify_eq; split; reflexivity].
Qed.
References
Lemma b1_pure_next_id_bwd s0 sb0 sb1 r :
  b1_state_rel s0 sb0 -> next_id sb0 = Some (sb1, r) ->
  exists s1, next_id s0 = Some (s1, r) /\ b1_state_rel s1 sb1.
Qed
Proof.
  eapply b1_rel_pure_step_bwd; [intros p []; unfold next_id; reflexivity|intros ??? H; unfold next_id in H; simplify_eq; split; reflexivity].
Qed.
References
Lemma wrap_ex_S (P : nat -> Prop) : (exists f, P (S f)) -> exists f, P f.
Qed
Proof. intros [f H]. exists (S f). exact H. Qed.
References

Uses

  • S
  • nat
Local Ltac upd_pull_tac :=
  let st := fresh "st" in
  intros ?p st;
  unfold ghost_publish, ghost_run_end, ghost_run_start, ghost_effect_start,
    ghost_dirty_cleared, ghost_resolved, ghost_registered, ghost_read,
    run_end_unnoticed, slot_holds, add_if, set_node,
    with_batch_depth, with_flush_owed, with_queue_head, with_queue,
    with_owners, with_edges, with_trace, with_running, with_checking,
    with_ghost, with_registered, with_flushing,
    with_current, with_current_owner, with_untracked, with_next_id;
  destruct st; simpl;
  repeat match goal with
  | |- context [match ?m !! ?k with _ => _ end] => destruct (m !! k)
  end;
  repeat split; reflexivity.
References
Lemma b1_pure_cmp_throws {A} n u (v : A) s0 sb0 s1 a :
  b1_state_rel s0 sb0 -> (let* _ := modify_node n u in m_return v) s0 = Some (s1, a) ->
  exists sb1, (let* _ := modify_node n u in m_return v) sb0 = Some (sb1, a) /\ b1_state_rel s1 sb1.
Qed
Proof.
  eapply b1_rel_pure_step.
  - intros p []. unfold m_bind, modify_node, m_return. simpl. destruct (g_nodes !! n); reflexivity.
  - intros ??? H. unfold m_bind, modify_node, m_return in H. destruct (g_nodes _ !! n); simplify_eq; split; reflexivity.
Qed.
References
Lemma b1_pure_cmp_throws_bwd {A} n u (v : A) s0 sb0 sb1 a :
  b1_state_rel s0 sb0 -> (let* _ := modify_node n u in m_return v) sb0 = Some (sb1, a) ->
  exists s1, (let* _ := modify_node n u in m_return v) s0 = Some (s1, a) /\ b1_state_rel s1 sb1.
Qed
Proof.
  eapply b1_rel_pure_step_bwd.
  - intros p []. unfold m_bind, modify_node, m_return. simpl. destruct (g_nodes !! n); reflexivity.
  - intros ??? H. unfold m_bind, modify_node, m_return in H. destruct (g_nodes _ !! n); simplify_eq; split; reflexivity.
Qed.
References
Local Ltac b1_mod_fwd :=
  intros;
  try unfold add_node, put_owner, emit, edges, push_running, pop_running, push_checking, pop_checking in *;
  eapply b1_pure_modify; [| |eassumption]; [|eassumption]; upd_pull_tac.
References
Local Ltac b1_mod_bwd :=
  intros;
  try unfold add_node, put_owner, emit, edges, push_running, pop_running, push_checking, pop_checking in *;
  eapply b1_pure_modify_bwd; [| |eassumption]; [|eassumption]; upd_pull_tac.
References
Local Ltac b1_pure_fwd :=
  try unfold add_node, put_owner, emit, edges, push_running, pop_running, push_checking, pop_checking;
  first
    [ eapply b1_pure_m_return
    | eapply b1_pure_cmp_throws
    | eapply b1_pure_get_node
    | eapply b1_pure_modify_node
    | eapply b1_pure_get_owner
    | eapply b1_pure_edges_opt
    | eapply b1_pure_current_owner
    | eapply b1_pure_note_host_read
    | eapply b1_pure_track
    | eapply b1_pure_mark_reader
    | eapply b1_pure_announce
    | eapply b1_pure_unlink
    | eapply b1_pure_record_error
    | eapply b1_pure_log_label
    | eapply b1_pure_mark_dirty
    | eapply b1_pure_notify_dirty
    | eapply b1_pure_notify_dirty_except
    | eapply b1_pure_next_id
    | b1_mod_fwd
    ].
References
References
Lemma b1_fwd_ret {A} (a : A) s sb s' r :
  b1_state_rel s sb -> m_return a s = Some (s', r) ->
  exists (fb : nat) sb', m_return a sb = Some (sb', r) /\ b1_state_rel s' sb'.
Qed
Proof. intros Hr H. unfold m_return in H |- *. simplify_eq. exists 0%nat, sb. auto. Qed.
References

Used by

Lemma b1_bwd_ret {A} (a : A) s sb sb' r :
  b1_state_rel s sb -> m_return a sb = Some (sb', r) ->
  exists (f : nat) s', m_return a s = Some (s', r) /\ b1_state_rel s' sb'.
Qed
Proof. intros Hr H. unfold m_return in H |- *. simplify_eq. exists 0%nat, s. auto. Qed.
References

Used by

Lemma b1_fwd_all f : b1_fwd_at f.
Qed
Proof.
  induction f as [|f IH].
  - split; intros; discriminate.
  - split.
    + intros e c s sb s' r Hrel H.
      apply wrap_ex_S. cbn [b1_exec]. cbn [exec] in H.
      destruct c; repeat match goal with
      | H0 : context [on_node] |- _ => unfold on_node in H0 |- *
      | H0 : context [on_value] |- _ => unfold on_value in H0 |- *
      | H0 : context [match eval_expr ?e ?p ?x with _ => _ end] |- _ => destruct (eval_expr e p x) as [[|]|]; try discriminate
      | H0 : (if decide _ then _ else stuck) _ = Some _ |- _ => destruct (decide _); try discriminate
      end; try (unfold m_return in H; simplify_eq; exists 0%nat, sb; split; [reflexivity|exact Hrel]);
      try (simplify_eq; exists 0%nat, sb; split; [reflexivity|exact Hrel]).
      * by eapply (Fexec1 _ IH).
      * destruct (decide _); by eapply (Fexec1 _ IH).
      * eapply (b1_fwd_bind_of H); [intros ????; b1_mono_tac|intros ?????; b1_mono_tac|intros ???; by eapply (Fread1 _ IH)|intros o ?? _ Hrel1 Hk].
        eapply (b1_fwd_bind_pure_l_of Hrel1 Hk _); [b1_pure_fwd|intros [] ?? _ _ Hrel2 Hk2].
        destruct o; [by eapply (Fexec1 _ IH)|by eapply b1_fwd_ret].
      * eapply (b1_fwd_bind_of H); [intros ????; b1_mono_tac|intros ?????; b1_mono_tac|intros ???; by eapply (Fread1 _ IH)|intros o ?? _ Hrel1 Hk].
        eapply (b1_fwd_bind_pure_l_of Hrel1 Hk _); [b1_pure_fwd|intros [] ?? _ _ Hrel2 Hk2].
        destruct o; by eapply (Fexec1 _ IH).
      * eapply (b1_fwd_bind_of H); [intros ????; b1_mono_tac|intros ?????; b1_mono_tac|intros ???; by eapply (Fread1 _ IH)|intros o ?? _ Hrel1 Hk].
        eapply (b1_fwd_bind_pure_l_of Hrel1 Hk _); [b1_pure_fwd|intros [] ?? _ _ Hrel2 Hk2].
        destruct o; [by eapply (Fexec1 _ IH)|by eapply b1_fwd_ret].
      * eapply (b1_fwd_bind_of H); [intros ????; b1_mono_tac|intros ?????; b1_mono_tac|intros ???; by eapply (Fwrite1 _ IH)|intros [] ?? _ Hrel1 Hk].
        by eapply (Fexec1 _ IH).
      * eapply (b1_fwd_bind_pure_l_of Hrel H _); [b1_pure_fwd|intros ?? sb1 _ _ Hrel1 Hk].
        eapply (b1_fwd_bind_pure_l_of Hrel1 Hk _); [b1_pure_fwd|intros ?? sb2 _ _ Hrel2 Hk2].
        by eapply (Fexec1 _ IH).
      * eapply (b1_fwd_bind_pure_l_of Hrel H _); [b1_pure_fwd|intros ?? sb1 _ _ Hrel1 Hk].
        eapply (b1_fwd_bind_pure_l_of Hrel1 Hk _); [b1_pure_fwd|intros ?? sb2 _ _ Hrel2 Hk2].
        eapply (b1_fwd_bind_pure_l_of Hrel2 Hk2 _); [b1_pure_fwd|intros [o|] ?? _ _ Hrel3 Hk3; [|by eapply b1_fwd_ret]].
        eapply (b1_fwd_bind_of Hk3); [intros ????; b1_mono_tac|intros ?????; b1_mono_tac|intros ???; by eapply (Fattach1 _ IH)|intros [] ?? _ Hrel4 Hk4].
        by eapply (Fexec1 _ IH).
      * eapply (b1_fwd_bind_pure_l_of Hrel H _); [b1_pure_fwd|intros ?? sb1 _ _ Hrel1 Hk].
        eapply (b1_fwd_bind_pure_l_of Hrel1 Hk _); [b1_pure_fwd|intros ?? sb2 _ _ Hrel2 Hk2].
        eapply (b1_fwd_bind_pure_l_of Hrel2 Hk2 _); [b1_pure_fwd|intros [o|] ?? _ _ Hrel3 Hk3; [|by eapply b1_fwd_ret]].
        eapply (b1_fwd_bind_of Hk3); [intros ????; b1_mono_tac|intros ?????; b1_mono_tac|intros ???; by eapply (Fattach1 _ IH)|intros [] ?? _ Hrel4 Hk4].
        eapply (b1_fwd_bind_of Hk4); [intros ????; b1_mono_tac|intros ?????; b1_mono_tac|intros ???; exists f; by eapply b1_pure_mark_dirty|intros [] ?? _ Hrel5 Hk5].
        eapply (b1_fwd_bind_of Hk5); [intros ????; b1_mono_tac|intros ?????; b1_mono_tac|intros ???; by eapply (Frequest1 _ IH)|intros [] ?? _ Hrel6 Hk6].
        by eapply (Fexec1 _ IH).
      * eapply (b1_fwd_bind_pure_l_of Hrel H _); [b1_pure_fwd|intros [o|] ?? _ _ Hrel1 Hk; [|by eapply b1_fwd_ret]].
        eapply (b1_fwd_bind_pure_l_of Hrel1 Hk _); [b1_pure_fwd|intros x ?? _ _ Hrel2 Hk2].
        eapply (b1_fwd_bind_pure_l_of Hrel2 Hk2 _); [b1_pure_fwd|intros [] ?? _ _ Hrel3 Hk3].
        destruct (o_disposed x).
        -- eapply (b1_fwd_bind_of Hk3); [intros ????; b1_mono_tac|intros ?????; b1_mono_tac| |intros [] ?? _ Hrel4 Hk4].
           ++ intros s1' [] Hm. eapply (b1_fwd_bind_of Hm); [intros ????; b1_mono_tac|intros ?????; b1_mono_tac| |intros [] ?? _ Hrel4' Hk4'].
              ** intros s2' [] Hd. by eapply b1_detached_fwd; [exact Hrel3|intros ??? Hrel0 ??; by eapply (Fcleanup1 _ IH)|exact Hd].
              ** by eapply (Frequest1 _ IH).
           ++ by eapply (Fexec1 _ IH).
        -- eapply (b1_fwd_bind_pure_l_of Hrel3 Hk3 _); [b1_pure_fwd|intros [] ?? _ _ Hrel4 Hk4].
           by eapply (Fexec1 _ IH).
      * eapply (b1_fwd_bind_pure_l_of Hrel H _); [b1_pure_fwd|intros d ?? _ _ Hrel1 Hk].
        destruct (n_kind d); [discriminate| |];
          (eapply (b1_fwd_bind_of Hk); [intros ????; b1_mono_tac|intros ?????; b1_mono_tac|intros ???; by eapply (Fdispose1 _ IH)|intros [] ?? _ Hrel2 Hk2];
           eapply (b1_fwd_bind_pure_l_of Hrel2 Hk2 _); [destruct (n_disposed d); b1_pure_fwd|intros [] ?? _ _ Hrel3 Hk3];
           by eapply (Fexec1 _ IH)).
      * eapply (b1_fwd_bind_pure_l_of Hrel H _); [b1_pure_fwd|intros [] ?? _ _ Hrel1 Hk].
        eapply (b1_fwd_bind_of Hk); [intros ????; b1_mono_tac|intros ?????; b1_mono_tac|intros ???; by eapply (Fexec1 _ IH)|intros r0 ?? _ Hrel2 Hk2].
        eapply (b1_fwd_bind_pure_l_of Hrel2 Hk2 _); [b1_pure_fwd|intros [] ?? _ _ Hrel3 Hk3].
        destruct r0 as [|].
        -- eapply (b1_fwd_bind_of Hk3); [intros ????; b1_mono_tac|intros ?????; b1_mono_tac|intros ???; by eapply (Frequest1 _ IH)|intros [] ?? _ Hrel4 Hk4].
           by eapply (Fexec1 _ IH).
        -- by eapply b1_fwd_ret.
      * eapply (b1_fwd_bind_pure_l_of Hrel H _); [b1_pure_fwd|intros ?? sb1 _ _ Hrel1 Hk].
        eapply (b1_fwd_bind_pure_l_of Hrel1 Hk _); [b1_pure_fwd|intros [] ?? _ _ Hrel2 Hk2].
        by eapply (Fexec1 _ IH).
    + intros n b s sb s' r Hrel H. apply wrap_ex_S. cbn [b1_read]. cbn [read] in H.
      destruct b; [by eapply (Fread_node1 _ IH)|eapply b1_untracked_fwd; [exact Hrel|intros ??? Hrel0 ??; by eapply (Fread_node1 _ IH)|exact H]].
    + intros n s sb s' r Hrel H. apply wrap_ex_S. cbn [b1_read_node]. cbn [read_node] in H.
      eapply (b1_fwd_bind_pure_l_of Hrel H _); [b1_pure_fwd|intros d ?? _ _ Hrel1 Hk].
      destruct (n_kind d); [| |discriminate].
      * eapply (b1_fwd_bind_pure_l_of Hrel1 Hk _); [b1_pure_fwd|intros [] ?? _ _ Hrel2 Hk2].
        by eapply b1_fwd_ret.
      * eapply (b1_fwd_bind_of Hk); [intros ????; b1_mono_tac|intros ?????; b1_mono_tac| |intros d' ?? _ Hrel2 Hk2].
        -- intros s1a d'a Hm. destruct (stale_live d).
           ++ eapply (b1_fwd_bind_of Hm); [intros ????; b1_mono_tac|intros ?????; b1_mono_tac|intros ???; by eapply (Frop1 _ IH)|intros d'0 ?? _ Hrel2a Hk2a].
              eapply (b1_fwd_bind_of Hk2a); [intros ????; b1_mono_tac|intros ?????; b1_mono_tac| |intros [] ?? _ Hrel3a Hk3a].
              ** intros s2a [] Hm2.
                 eapply (b1_fwd_bind_pure_l_of Hrel2a Hm2 _); [b1_pure_fwd|intros [] ?? _ _ Hrel3b Hk3b].
                 eapply (b1_fwd_bind_pure_l_of Hrel3b Hk3b _); [b1_pure_fwd|intros d'' ?? _ _ Hrel4b Hk4b].
                 destruct (stale_live d''); [destruct (b1_pure_mark_reader _ _ _ _ _ Hrel4b Hk4b) as (sb' & E & Hr); exists f, sb'; exact (conj E Hr)|by eapply b1_fwd_ret].
              ** by eapply b1_fwd_ret.
           ++ eapply (b1_fwd_bind_pure_l_of Hrel1 Hm _); [b1_pure_fwd|intros [] ?? _ _ Hrel2b Hk2b].
              exists 0%nat; by eapply b1_pure_get_node.
        -- destruct (n_status d'); by eapply b1_fwd_ret.
    + intros n s sb s' r Hrel H. apply wrap_ex_S. cbn [b1_update_if_necessary]. cbn [update_if_necessary] in H.
      eapply (b1_fwd_bind_pure_l_of Hrel H _); [b1_pure_fwd|intros d ?? _ _ Hrel1 Hk].
      destruct (n_kind d); [by eapply b1_fwd_ret| |by eapply b1_fwd_ret].
      destruct (stale_live d); [|by eapply b1_fwd_ret].
      eapply (b1_fwd_bind_of Hk); [intros ????; b1_mono_tac|intros ?????; b1_mono_tac|intros ???; by eapply (Frop1 _ IH)|intros ??? _ Hrel2 Hk2].
      by eapply b1_fwd_ret.
    + intros n s sb s' r Hrel H. apply wrap_ex_S. cbn [b1_refresh_or_pull]. cbn [refresh_or_pull] in H.
      rewrite bind_get in H. setoid_rewrite bind_get.
      destruct (g_flushing sb) eqn:Efl.
      * unfold deferring_b1. rewrite Efl. simpl.
        destruct (deferring s) eqn:Edef.
        -- eapply (b1_fwd_bind_of H); [intros ????; b1_mono_tac|intros ?????; b1_mono_tac|intros ???; by eapply (Frefresh1 _ IH)|intros [] ?? _ Hrel1 Hk].
           destruct (b1_pure_get_node _ _ _ _ _ Hrel1 Hk) as (sb' & E & Hr); exists 0%nat, sb'; exact (conj E Hr).
        -- destruct f as [|f']; [discriminate|]. cbn [pull] in H.
           eapply bind_inv in H as (s1 & [] & E1 & H). unfold m_modify in E1. simplify_eq.
           eapply bind_inv in H as (s2 & [] & E2 & H).
           eapply bind_inv in H as (s3 & d & E3 & H). unfold get_node in E3.
           destruct (g_nodes s2 !! n) as [d0|] eqn:En; [|discriminate]. simplify_eq.
           eapply bind_inv in H as (s4 & [] & E4 & H). unfold m_modify in E4. simplify_eq.
           rewrite bind_get in H.
           eapply bind_inv in H as (s5 & [] & E5 & H). unfold m_return in H. simplify_eq.
           assert (Hrel1 : b1_state_rel (with_pull_depth (S (g_pull_depth s)) s) sb)
             by (by apply b1_rel_with_pull_S_left).
           pose proof (Mrefresh _ (engine_mono_all f') (S f') n (with_pull_depth (S (g_pull_depth s)) s) (s3, ()) ltac:(lia) E2) as E2'.
           destruct (Frefresh1 _ IH n _ _ _ _ Hrel1 E2') as (fb & sb2 & Eb2 & Hrel2).
           destruct (engine_depths_kept f') as [_ _ _ _ _ _ Dref _].
           pose proof (Dref n _ _ _ E2) as [Hp2 Hfl2].
           destruct (b1_depths_kept fb) as [_ _ _ _ _ _ B1Dref _].
           pose proof (B1Dref n _ _ _ Eb2) as [Hpb2 Hflb2].
           assert (Hfls4 : g_flushing (with_pull_depth (pred (g_pull_depth s3)) s3) = true).
           { simpl. rewrite Hfl2. simpl. destruct (b1_rel_destruct _ _ Hrel) as (_&_&Hf&_). congruence. }
           assert (Hs' : s' = with_pull_depth (pred (g_pull_depth s3)) s3).
           { destruct (_ && _); [by destruct (settle_flush_flushing _ _ _ _ Hfls4 E5)|unfold m_return in E5; by simplify_eq]. }
           subst s'.
           assert (Hrel5 : b1_state_rel (with_pull_depth (pred (g_pull_depth s3)) s3) sb2).
           { destruct (b1_rel_destruct _ _ Hrel) as (_&_&Hfs&_&Hts).
             destruct Hrel2 as [Hw2 [Hf2 Ht2]]. split.
             - rewrite <- Hw2. unfold with_pull_depth. destruct s3; reflexivity.
             - simpl. split; [intro; congruence|]. intro Hfl0. rewrite Hp2. simpl.
               pose proof (Hts Efl) as Hle. rewrite Hpb2. lia. }
           assert (Eget0 : get_node n (with_pull_depth (pred (g_pull_depth s3)) s3) = Some (with_pull_depth (pred (g_pull_depth s3)) s3, r))
             by (unfold get_node, with_pull_depth; simpl; by rewrite En).
           destruct (b1_pure_get_node _ _ _ _ _ Hrel5 Eget0) as (sb' & Eget & Hrel').
           exists fb, sb'. split; [|exact Hrel'].
           unfold m_bind. rewrite Eb2. simpl. exact Eget.
      * destruct (b1_rel_destruct _ _ Hrel) as (_ & _ & Hfs & Hfp & _).
        assert (Hs : s = sb) by (apply b1_state_rel_eq; [exact Hrel|auto]). subst sb.
        unfold deferring_b1. rewrite Efl. simpl.
        destruct (deferring s).
        -- eapply (b1_fwd_bind_of H); [intros ????; b1_mono_tac|intros ?????; b1_mono_tac|intros ???; by eapply (Frefresh1 _ IH)|intros [] ?? _ Hrel1 Hk].
           exists 0%nat; by eapply b1_pure_get_node.
        -- by eapply (Fpull1 _ IH).
    + intros n s sb s' r Hrel H. apply wrap_ex_S. cbn [b1_pull]. cbn [pull] in H.
      eapply (b1_fwd_bind_pure_l_of Hrel H _);
        [intros sa0 sba0 sa1 [] Hr0 Hm; unfold m_modify in Hm; simplify_eq; eexists; split; [unfold m_modify; reflexivity|by apply b1_rel_with_pull_S]|intros [] ?? _ _ Hrel1 Hk].
      eapply (b1_fwd_bind_of Hk); [intros ????; b1_mono_tac|intros ?????; b1_mono_tac|intros ???; by eapply (Frefresh1 _ IH)|intros [] ?? _ Hrel2 Hk2].
      eapply (b1_fwd_bind_pure_l_of Hrel2 Hk2 _); [b1_pure_fwd|intros d ?? _ _ Hrel3 Hk3].
      eapply (b1_fwd_bind_pure_l_of Hrel3 Hk3 _);
        [intros s_p2 sb_p2 s_p3 [] Hr0 Hm; unfold m_modify in Hm; simplify_eq; eexists; split; [unfold m_modify; reflexivity|by apply b1_rel_with_pull_pred]|intros [] s3 sb4 _ _ Hrel4 Hk4].
      rewrite bind_get in Hk4. setoid_rewrite bind_get.
      eapply (b1_fwd_bind_of Hk4); [intros ????; b1_mono_tac|intros ?????; b1_mono_tac| |intros [] ?? _ Hrel5 Hk5; by eapply b1_fwd_ret].
      intros s_q a Hm.
      destruct (g_flushing s3) eqn:Efl.
      * destruct (b1_rel_destruct _ _ Hrel4) as (_&_&Hfl&_).
        assert (Hflb : g_flushing sb4 = true) by congruence.
        assert (Hs1 : s_q = s3 /\ a = tt).
        { destruct (bool_decide (g_pull_depth s3 = 0) && g_flush_owed s3).
          - by eapply settle_flush_flushing.
          - unfold m_return in Hm. by simplify_eq. }
        destruct Hs1 as [-> ->].
        exists 2%nat, sb4. split; [|exact Hrel4].
        destruct (bool_decide (g_pull_depth sb4 = 0) && g_flush_owed sb4); [|reflexivity].
        cbn [b1_settle_flush]. rewrite bind_get.
        destruct (g_flush_owed sb4 && bool_decide (g_batch_depth sb4 = 0) && outside_bodies sb4); [|reflexivity].
        cbn [b1_run_flush]. rewrite bind_get, Hflb. reflexivity.
      * destruct (b1_rel_destruct _ _ Hrel4) as (_&_&Hfl&Hfp&_).
        assert (Hsb4 : s3 = sb4) by (apply b1_state_rel_eq; [exact Hrel4|apply Hfp; congruence]).
        subst sb4.
        destruct (bool_decide (g_pull_depth s3 = 0) && g_flush_owed s3); [by eapply (Fsettle1 _ IH)|by eapply b1_fwd_ret].
    + intros n s sb s' r Hrel H. apply wrap_ex_S. cbn [b1_refresh]. cbn [refresh] in H.
      rewrite bind_get in H. setoid_rewrite bind_get.
      destruct (b1_rel_fields _ _ Hrel) as (_&_&_&_&_&_&_&_&_&_&_&_&_&_&Hrun&Hchk&_).
      destruct (decide (n ∈ g_checking s)), (decide (n ∈ g_checking sb)); try (exfalso; rewrite <- Hchk in *; contradiction).
      { unfold m_step in H |- *. destruct (mark_dirty f n s) eqn:Em; simplify_eq.
        destruct (b1_pure_mark_dirty f n s sb s' tt Hrel ltac:(unfold m_step; by rewrite Em)) as (sb1 & Em1 & Hrel1).
        unfold m_step in Em1. destruct (mark_dirty f n sb) eqn:Emb; simplify_eq.
        exists f, sb1. split; [by rewrite Emb|exact Hrel1]. }
      destruct (decide (n ∈ g_running s)), (decide (n ∈ g_running sb)); try (exfalso; rewrite <- Hrun in *; contradiction); [by eapply b1_fwd_ret|].
      eapply (b1_fwd_bind_pure_l_of Hrel H _); [b1_pure_fwd|intros d ?? _ _ Hrel1 Hk].
      eapply (b1_fwd_bind_of Hk); [intros ????; b1_mono_tac|intros ?????; b1_mono_tac| |intros [] ?? _ Hrel2 Hk2].
      * intros s_ref [] Hm. destruct (decide (n_freshness d = Check)); [|by eapply b1_fwd_ret].
        eapply (b1_fwd_bind_pure_l_of Hrel1 Hm _); [b1_pure_fwd|intros [] ?? _ _ Hrel2a Hk2a].
        eapply (b1_fwd_bind_of Hk2a); [intros ????; b1_mono_tac|intros ?????; b1_mono_tac|intros ???; by eapply (Fwalk1 _ IH)|intros [] ?? _ Hrel3a Hk3a].
        eapply (b1_fwd_bind_pure_l_of Hrel3a Hk3a _); [b1_pure_fwd|intros [] ?? _ _ Hrel4a Hk4a].
        eapply (b1_fwd_bind_pure_l_of Hrel4a Hk4a _); [b1_pure_fwd|intros d' ?? _ _ Hrel5a Hk5a].
        destruct (decide (n_freshness d' = Check)); [|by eapply b1_fwd_ret].
        eapply (b1_fwd_bind_pure_l_of Hrel5a Hk5a _); [b1_pure_fwd|intros [] ?? _ _ Hrel6a Hk6a].
        exists 0%nat; by eapply b1_pure_modify_node.
      * eapply (b1_fwd_bind_pure_l_of Hrel2 Hk2 _); [b1_pure_fwd|intros d' ?? _ _ Hrel3 Hk3].
        destruct (decide (n_freshness d' = Dirty)); [by eapply (Frecompute1 _ IH)|by eapply b1_fwd_ret].
    + intros n i s sb s' r Hrel H. apply wrap_ex_S. cbn [b1_resolve_walk]. cbn [resolve_walk] in H.
      eapply (b1_fwd_bind_pure_l_of Hrel H _); [b1_pure_fwd|intros d s1 sb1 _ _ Hrel1 Hk].
      rewrite bind_get in Hk. setoid_rewrite bind_get.
      destruct (decide (n_freshness d = Check)); [|by eapply b1_fwd_ret].
      destruct (b1_rel_fields _ _ Hrel1) as (_&He1&_).
      destruct (source_slots (sources_of (g_edges s1) n) !! i) as [[src ?]|] eqn:Es1,
               (source_slots (sources_of (g_edges sb1) n) !! i) as [[srcb ?]|] eqn:Esb1;
        try (exfalso; rewrite <- He1, Es1 in Esb1; discriminate).
      * assert (srcb = src) by (rewrite <- He1, Es1 in Esb1; congruence); subst srcb.
        eapply (b1_fwd_bind_of Hk); [intros ????; b1_mono_tac|intros ?????; b1_mono_tac|intros ???; by eapply (Fupdate1 _ IH)|intros [] ?? _ Hrel2 Hk2].
        eapply (b1_fwd_bind_pure_l_of Hrel2 Hk2 _); [b1_pure_fwd|intros d' ?? _ _ Hrel3 Hk3].
        eapply (b1_fwd_bind_pure_l_of Hrel3 Hk3 _); [b1_pure_fwd|intros d_src s4 sb4 _ _ Hrel4 Hk4].
        rewrite bind_get in Hk4. setoid_rewrite bind_get.
        destruct (b1_rel_fields _ _ Hrel4) as (_&_&_&_&_&_&_&_&_&_&_&_&_&_&Hrun4&Hchk4&_).
        eapply (b1_fwd_bind_of Hk4); [intros ????; b1_mono_tac|intros ?????; b1_mono_tac| |intros [] ?? _ Hrel5 Hk5; by eapply (Fwalk1 _ IH)].
        intros s5 [] Hm.
        destruct (decide (n_freshness d' = Check /\ stale_live d_src = true /\ (src ∈ g_running s4 \/ src ∈ g_checking s4))) as [Hd|Hd];
          destruct (decide (n_freshness d' = Check /\ stale_live d_src = true /\ (src ∈ g_running sb4 \/ src ∈ g_checking sb4))) as [Hdb|Hdb];
          try (exfalso; rewrite <- Hrun4, <- Hchk4 in *; tauto).
        -- unfold m_step in Hm |- *. destruct (mark_dirty f n s4) eqn:Em; simplify_eq.
           destruct (b1_pure_mark_dirty f n s4 sb4 s5 tt Hrel4 ltac:(unfold m_step; by rewrite Em)) as (sb5 & Em5 & Hrel5).
           unfold m_step in Em5. destruct (mark_dirty f n sb4) eqn:Emb; simplify_eq.
           exists f, sb5. split; [by rewrite Emb|exact Hrel5].
        -- by eapply b1_fwd_ret.
      * assert (Hsh : slot_holds stale_live s1 n = slot_holds stale_live sb1 n).
        { destruct (b1_rel_fields _ _ Hrel1) as (_&He'&Hn'&_). unfold slot_holds. by rewrite He', Hn'. }
        destruct (slot_holds stale_live s1 n) eqn:Esh1, (slot_holds stale_live sb1 n) eqn:Esh2;
          try (exfalso; congruence); [by eapply (Fwalk1 _ IH)|by eapply b1_fwd_ret].
    + intros n s sb s' r Hrel H. apply wrap_ex_S. cbn [b1_recompute]. cbn [recompute] in H.
      eapply (b1_fwd_bind_pure_l_of Hrel H _); [b1_pure_fwd|intros d ?? _ _ Hrel1 Hk].
      destruct (n_scope d) as [o|]; [|by eapply (Frun_memo1 _ IH)].
      eapply (b1_fwd_bind_pure_l_of Hrel1 Hk _); [b1_pure_fwd|intros [] ?? _ _ Hrel2 Hk2].
      eapply (b1_fwd_bind_pure_l_of Hrel2 Hk2 _); [b1_pure_fwd|intros [] ?? _ _ Hrel3 Hk3].
      eapply (b1_fwd_bind_of Hk3); [intros ????; b1_mono_tac|intros ?????; b1_mono_tac|intros ???; by eapply (Fdischarge1 _ IH)|intros [] ?? _ Hrel4 Hk4].
      eapply (b1_fwd_bind_pure_l_of Hrel4 Hk4 _); [b1_pure_fwd|intros d' ?? _ _ Hrel5 Hk5].
      destruct (n_disposed d').
      * destruct (decide (n_freshness d' = Clean)); [exists 0%nat; by eapply b1_pure_modify_node|by eapply b1_fwd_ret].
      * destruct (decide (n_runs d' = n_runs d)); [by eapply (Frun_memo1 _ IH)|].
        destruct (decide (n_freshness d' = Dirty)); [by eapply (Frecompute1 _ IH)|by eapply b1_fwd_ret].
    + intros n s sb s' r Hrel H. apply wrap_ex_S. cbn [b1_run_memo]. cbn [run_memo] in H.
      eapply (b1_fwd_bind_pure_l_of Hrel H _); [b1_pure_fwd|intros d ?? _ _ Hrel1 Hk].
      eapply (b1_fwd_bind_pure_l_of Hrel1 Hk _); [b1_pure_fwd|intros [] ?? _ _ Hrel2 Hk2].
      eapply (b1_fwd_bind_pure_l_of Hrel2 Hk2 _); [b1_pure_fwd|intros [] ?? _ _ Hrel3 Hk3].
      eapply (b1_fwd_bind_pure_l_of Hrel3 Hk3 _); [b1_pure_fwd|intros [] ?? _ _ Hrel4 Hk4].
      eapply (b1_fwd_bind_pure_l_of Hrel4 Hk4 _); [b1_pure_fwd|intros [] ?? _ _ Hrel5 Hk5].
      eapply (b1_fwd_bind_of Hk5); [intros ????; b1_mono_tac|intros ?????; b1_mono_tac| |intros r0 ?? _ Hrel6 Hk6].
      { intros s6 r0a Hm. by eapply b1_hosted_fwd; [exact Hrel5|intros ??? Hrel0 ??; by eapply (Fexec1 _ IH)|exact Hm]. }
      eapply (b1_fwd_bind_pure_l_of Hrel6 Hk6 _); [b1_pure_fwd|intros d1 ?? _ _ Hrel7 Hk7].
      eapply (b1_fwd_bind_pure_l_of Hrel7 Hk7 _); [destruct (if n_violated d1 then Fail pure_scope_failure else r0); b1_pure_fwd|intros [] ?? _ _ Hrel8 Hk8].
      eapply (b1_fwd_bind_pure_l_of Hrel8 Hk8 _); [b1_pure_fwd|intros [] ?? _ _ Hrel9 Hk9].
      eapply (b1_fwd_bind_pure_l_of Hrel9 Hk9 _); [b1_pure_fwd|intros [] ?? _ _ Hrel10 Hk10].
      eapply (b1_fwd_bind_pure_l_of Hrel10 Hk10 _); [destruct (if n_violated d1 then Fail pure_scope_failure else r0); b1_pure_fwd|intros [] ?? _ _ Hrel11 Hk11].
      eapply (b1_fwd_bind_pure_l_of Hrel11 Hk11 _); [b1_pure_fwd|intros d2 ?? _ _ Hrel12 Hk12].
      eapply (b1_fwd_bind_pure_l_of Hrel12 Hk12 _);
        [destruct (decide _); [b1_pure_fwd|destruct (n_comparer d2); b1_pure_fwd]|intros moved s13 sb13 _ _ Hrel13 Hk13].
      rewrite bind_get in Hk13. setoid_rewrite bind_get.
      destruct (b1_rel_fields _ _ Hrel13) as (_&_&_&_&_&_&_&_&_&Hc13&_).
      destruct (g_current s13) as [c13|] eqn:Ecs1, (g_current sb13) as [cb13|] eqn:Ecs2;
        try (exfalso; rewrite Hc13 in Ecs1; congruence);
        [assert (cb13 = c13) by congruence; subst cb13|];
        (eapply (b1_fwd_bind_pure_l_of Hrel13 Hk13 _); [b1_pure_fwd|intros [] ?? _ _ Hrel14 Hk14];
         eapply (b1_fwd_bind_of Hk14); [intros ????; b1_mono_tac|intros ?????; b1_mono_tac| |intros [] ?? _ Hrel15 Hk15];
         [intros s15 [] Hm; destruct moved; [|by eapply b1_fwd_ret];
          eapply (b1_fwd_bind_of Hm); [intros ????; b1_mono_tac|intros ?????; b1_mono_tac|intros ???; exists f; by eapply b1_pure_notify_dirty_except|intros [] s16 sb16 _ Hrel16 Hk16];
          rewrite bind_get in Hk16; setoid_rewrite bind_get;
          destruct (b1_rel_fields _ _ Hrel16) as (_&_&_&_&Hq16&Hqh16&Hfl16&_&Hbd16&_);
          assert (Hcond : bool_decide (g_batch_depth s16 = 0%nat) && negb (g_flushing s16) && live_queue s16 =
                          bool_decide (g_batch_depth sb16 = 0%nat) && negb (g_flushing sb16) && live_queue sb16)
            by (unfold live_queue; by rewrite Hbd16, Hfl16, Hq16, Hqh16);
          destruct (bool_decide (g_batch_depth s16 = 0%nat) && negb (g_flushing s16) && live_queue s16) eqn:Ec1,
                   (bool_decide (g_batch_depth sb16 = 0%nat) && negb (g_flushing sb16) && live_queue sb16) eqn:Ec2;
            try (exfalso; congruence); [exists 0%nat; b1_mod_fwd|by eapply b1_fwd_ret]
         |eapply (b1_fwd_bind_pure_l_of Hrel15 Hk15 _); [b1_pure_fwd|intros d3 ?? _ _ Hrel16 Hk16];
          destruct (decide (n_freshness d3 = Clean)); [by eapply b1_fwd_ret|exists 0%nat; by eapply b1_pure_announce]]).
    + intros n s sb s' r Hrel H. apply wrap_ex_S. cbn [b1_execute_effect]. cbn [execute_effect] in H.
      eapply (b1_fwd_bind_pure_l_of Hrel H _); [b1_pure_fwd|intros d ?? _ _ Hrel1 Hk].
      destruct (n_disposed d); [exists 0%nat; by eapply b1_pure_modify_node|].
      eapply (b1_fwd_bind_of Hk); [intros ????; b1_mono_tac|intros ?????; b1_mono_tac| |intros [] ?? _ Hrel2 Hk2].
      * intros s2 [] Hm. destruct (decide (n_freshness d = Check)); [|by eapply b1_fwd_ret].
        eapply (b1_fwd_bind_of Hm); [intros ????; b1_mono_tac|intros ?????; b1_mono_tac|intros ???; by eapply (Fwalk1 _ IH)|intros [] ?? _ Hrel3 Hk3].
        eapply (b1_fwd_bind_pure_l_of Hrel3 Hk3 _); [b1_pure_fwd|intros d' ?? _ _ Hrel4 Hk4].
        destruct (decide (n_freshness d' = Check)); [|by eapply b1_fwd_ret].
        eapply (b1_fwd_bind_pure_l_of Hrel4 Hk4 _); [b1_pure_fwd|intros [] ?? _ _ Hrel5 Hk5].
        exists 0%nat; by eapply b1_pure_modify_node.
      * eapply (b1_fwd_bind_pure_l_of Hrel2 Hk2 _); [b1_pure_fwd|intros d' ?? _ _ Hrel3 Hk3].
        eapply (b1_fwd_bind_pure_l_of Hrel3 Hk3 _); [b1_pure_fwd|intros [] ?? _ _ Hrel4 Hk4].
        destruct (decide (n_freshness d' = Dirty)); [by eapply (Frun_effect1 _ IH)|by eapply b1_fwd_ret].
    + intros n s sb s' r Hrel H. apply wrap_ex_S. cbn [b1_run_effect]. cbn [run_effect] in H.
      eapply (b1_fwd_bind_pure_l_of Hrel H _); [b1_pure_fwd|intros [] ?? _ _ Hrel1 Hk].
      eapply (b1_fwd_bind_pure_l_of Hrel1 Hk _); [b1_pure_fwd|intros [] ?? _ _ Hrel2 Hk2].
      eapply (b1_fwd_bind_pure_l_of Hrel2 Hk2 _); [b1_pure_fwd|intros [] ?? _ _ Hrel3 Hk3].
      eapply (b1_fwd_bind_pure_l_of Hrel3 Hk3 _); [b1_pure_fwd|intros d ?? _ _ Hrel4 Hk4].
      eapply (b1_fwd_bind_of Hk4); [intros ????; b1_mono_tac|intros ?????; b1_mono_tac| |intros [] ?? _ Hrel5 Hk5].
      * intros s5 [] Hm. destruct (n_scope d) as [o|]; [|by eapply b1_fwd_ret].
        eapply (b1_fwd_bind_pure_l_of Hrel4 Hm _); [b1_pure_fwd|intros [] ?? _ _ Hrel5a Hk5a].
        eapply (b1_fwd_bind_of Hk5a); [intros ????; b1_mono_tac|intros ?????; b1_mono_tac|intros ???; by eapply (Fdischarge1 _ IH)|intros [] ?? _ Hrel6a Hk6a].
        exists 0%nat; by eapply b1_pure_modify_node.
      * eapply (b1_fwd_bind_pure_l_of Hrel5 Hk5 _); [b1_pure_fwd|intros d' ?? _ _ Hrel6 Hk6].
        eapply (b1_fwd_bind_of Hk6); [intros ????; b1_mono_tac|intros ?????; b1_mono_tac| |intros [] ?? _ Hrel7 Hk7; exists 0%nat; b1_mod_fwd].
        intros s7 [] Hm. destruct (n_disposed d'); [by eapply b1_fwd_ret|by eapply (Fbody1 _ IH)].
    + intros n s sb s' r Hrel H. apply wrap_ex_S. cbn [b1_run_effect_body]. cbn [run_effect_body] in H.
      eapply (b1_fwd_bind_pure_l_of Hrel H _); [b1_pure_fwd|intros d ?? _ _ Hrel1 Hk].
      eapply (b1_fwd_bind_pure_l_of Hrel1 Hk _); [b1_pure_fwd|intros [] ?? _ _ Hrel2 Hk2].
      eapply (b1_fwd_bind_pure_l_of Hrel2 Hk2 _); [b1_pure_fwd|intros [] ?? _ _ Hrel3 Hk3].
      eapply (b1_fwd_bind_of Hk3); [intros ????; b1_mono_tac|intros ?????; b1_mono_tac| |intros r0 ?? _ Hrel4 Hk4].
      { intros s4 r0a Hm. by eapply b1_hosted_fwd; [exact Hrel3|intros ??? Hrel0 ??; by eapply (Fexec1 _ IH)|exact Hm]. }
      eapply (b1_fwd_bind_pure_l_of Hrel4 Hk4 _); [b1_pure_fwd|intros d' ?? _ _ Hrel5 Hk5].
      eapply (b1_fwd_bind_pure_l_of Hrel5 Hk5 _); [b1_pure_fwd|intros [] ?? _ _ Hrel6 Hk6].
      destruct r0; [by eapply b1_fwd_ret|].
      eapply (b1_fwd_bind_pure_l_of Hrel6 Hk6 _); [b1_pure_fwd|intros [] ?? _ _ Hrel7 Hk7].
      exists 0%nat; b1_mod_fwd.
    + intros o s sb s' r Hrel H. apply wrap_ex_S. cbn [b1_discharge]. cbn [discharge] in H.
      eapply (b1_fwd_bind_of H); [intros ????; b1_mono_tac|intros ?????; b1_mono_tac| |intros [] s1 sb1 _ Hrel1 Hk].
      * intros s1 [] Hm. by eapply b1_detached_fwd; [exact Hrel|intros ??? Hrel0 ??; by eapply (Fdispose_scope1 _ IH)|exact Hm].
      * rewrite bind_get in Hk. setoid_rewrite bind_get.
        destruct (b1_rel_fields _ _ Hrel1) as (_&_&_&_&Hq1&Hqh1&Hfl1&_&Hbd1&_).
        assert (Hcond : bool_decide (g_batch_depth s1 = 0%nat) && negb (g_flushing s1) && live_queue s1 =
                        bool_decide (g_batch_depth sb1 = 0%nat) && negb (g_flushing sb1) && live_queue sb1)
          by (unfold live_queue; by rewrite Hbd1, Hfl1, Hq1, Hqh1).
        destruct (bool_decide (g_batch_depth s1 = 0%nat) && negb (g_flushing s1) && live_queue s1) eqn:Ec1,
                 (bool_decide (g_batch_depth sb1 = 0%nat) && negb (g_flushing sb1) && live_queue sb1) eqn:Ec2;
          try (exfalso; congruence); [exists 0%nat; b1_mod_fwd|by eapply b1_fwd_ret].
    + intros o s sb s' r Hrel H. apply wrap_ex_S. cbn [b1_retire]. cbn [retire] in H.
      eapply (b1_fwd_bind_of H); [intros ????; b1_mono_tac|intros ?????; b1_mono_tac| |intros [] s1 sb1 _ Hrel1 Hk].
      * intros s1' [] Hm. eapply b1_detached_fwd; [exact Hrel| |exact Hm].
        intros s0 sb0 Hrel0 s1'' r' Hm0.
        eapply (b1_fwd_bind_pure_l_of Hrel0 Hm0 _); [b1_pure_fwd|intros x ?? _ _ Hrel2 Hk2].
        destruct (o_disposed x); [by eapply b1_fwd_ret|].
        eapply (b1_fwd_bind_pure_l_of Hrel2 Hk2 _); [b1_pure_fwd|intros [] ?? _ _ Hrel3 Hk3].
        by eapply (Fdispose_scope1 _ IH).
      * rewrite bind_get in Hk. setoid_rewrite bind_get.
        destruct (b1_rel_fields _ _ Hrel1) as (_&_&_&_&Hq1&Hqh1&_).
        assert (Hlq : live_queue s1 = live_queue sb1) by (unfold live_queue; by rewrite Hq1, Hqh1).
        destruct (live_queue s1) eqn:El1, (live_queue sb1) eqn:El2;
          try (exfalso; congruence); [by eapply (Frequest1 _ IH)|by eapply b1_fwd_ret].
    + intros o s sb s' r Hrel H. apply wrap_ex_S. cbn [b1_dispose_scope]. cbn [dispose_scope] in H.
      eapply (b1_fwd_bind_pure_l_of Hrel H _); [b1_pure_fwd|intros x ?? _ _ Hrel1 Hk].
      eapply (b1_fwd_bind_pure_l_of Hrel1 Hk _); [b1_pure_fwd|intros [] ?? _ _ Hrel2 Hk2].
      by eapply (Fteardown1 _ IH).
    + intros o ls cs s sb s' r Hrel H. apply wrap_ex_S. cbn [b1_teardown]. cbn [teardown] in H.
      destruct ls as [|l ls'], cs as [|[[b e] a] cs']; [by eapply b1_fwd_ret|..].
      * eapply (b1_fwd_bind_of H); [intros ????; b1_mono_tac|intros ?????; b1_mono_tac|intros ???; by eapply (Fcleanup1 _ IH)|intros [] ?? _ Hrel1 Hk].
        by eapply (Fteardown1 _ IH).
      * eapply (b1_fwd_bind_of H); [intros ????; b1_mono_tac|intros ?????; b1_mono_tac|intros ???; by eapply (Frelease1 _ IH)|intros [] ?? _ Hrel1 Hk].
        by eapply (Fteardown1 _ IH).
      * destruct (decide (a < l_acquired l)).
        -- eapply (b1_fwd_bind_of H); [intros ????; b1_mono_tac|intros ?????; b1_mono_tac|intros ???; by eapply (Frelease1 _ IH)|intros [] ?? _ Hrel1 Hk].
           by eapply (Fteardown1 _ IH).
        -- eapply (b1_fwd_bind_of H); [intros ????; b1_mono_tac|intros ?????; b1_mono_tac|intros ???; by eapply (Fcleanup1 _ IH)|intros [] ?? _ Hrel1 Hk].
           by eapply (Fteardown1 _ IH).
    + intros o b e s sb s' r Hrel H. apply wrap_ex_S. cbn [b1_run_cleanup]. cbn [run_cleanup] in H.
      eapply (b1_fwd_bind_pure_l_of Hrel H _); [b1_pure_fwd|intros x ?? _ _ Hrel1 Hk].
      eapply (b1_fwd_bind_pure_l_of Hrel1 Hk _); [b1_pure_fwd|intros [] ?? _ _ Hrel2 Hk2].
      eapply (b1_fwd_bind_of Hk2); [intros ????; b1_mono_tac|intros ?????; b1_mono_tac|intros ???; by eapply (Fexec1 _ IH)|intros r0 ?? _ Hrel3 Hk3].
      destruct r0; [by eapply b1_fwd_ret|].
      eapply (b1_fwd_bind_pure_l_of Hrel3 Hk3 _); [b1_pure_fwd|intros [] ?? _ _ Hrel4 Hk4].
      exists 0%nat; b1_mod_fwd.
    + intros n s sb s' r Hrel H. apply wrap_ex_S. cbn [b1_release]. cbn [release] in H.
      eapply (b1_fwd_bind_pure_l_of Hrel H _); [b1_pure_fwd|intros [] ?? _ _ Hrel1 Hk].
      by eapply (Fdispose1 _ IH).
    + intros n s sb s' r Hrel H. apply wrap_ex_S. cbn [b1_dispose_node]. cbn [dispose_node] in H.
      eapply (b1_fwd_bind_pure_l_of Hrel H _); [b1_pure_fwd|intros d ?? _ _ Hrel1 Hk].
      destruct (n_disposed d); [by eapply b1_fwd_ret|].
      eapply (b1_fwd_bind_pure_l_of Hrel1 Hk _); [b1_pure_fwd|intros [] ?? _ _ Hrel2 Hk2].
      eapply (b1_fwd_bind_pure_l_of Hrel2 Hk2 _); [destruct (n_link d); b1_pure_fwd|intros [] ?? _ _ Hrel3 Hk3].
      eapply (b1_fwd_bind_pure_l_of Hrel3 Hk3 _); [b1_pure_fwd|intros [] ?? _ _ Hrel4 Hk4].
      destruct (n_scope d); [by eapply (Fretire1 _ IH)|by eapply b1_fwd_ret].
    + intros n o s sb s' r Hrel H. apply wrap_ex_S. cbn [b1_attach]. cbn [attach] in H.
      eapply (b1_fwd_bind_pure_l_of Hrel H _); [b1_pure_fwd|intros x ?? _ _ Hrel1 Hk].
      destruct (o_disposed x).
      * eapply (b1_fwd_bind_of Hk); [intros ????; b1_mono_tac|intros ?????; b1_mono_tac|intros ???; by eapply (Frelease1 _ IH)|intros [] ?? _ Hrel2 Hk2].
        exists 0%nat; by eapply b1_pure_modify_node.
      * eapply (b1_fwd_bind_pure_l_of Hrel1 Hk _); [b1_pure_fwd|intros [] ?? _ _ Hrel2 Hk2].
        exists 0%nat; by eapply b1_pure_modify_node.
    + intros n v s sb s' r Hrel H. apply wrap_ex_S. cbn [b1_write_signal]. cbn [write_signal] in H.
      eapply (b1_fwd_bind_pure_l_of Hrel H _); [b1_pure_fwd|intros d ?? _ _ Hrel1 Hk].
      destruct (n_kind d); [|discriminate|discriminate].
      destruct (decide (n_value d = v)); [by eapply b1_fwd_ret|].
      eapply (b1_fwd_bind_pure_l_of Hrel1 Hk _); [b1_pure_fwd|intros [] ?? _ _ Hrel2 Hk2].
      eapply (b1_fwd_bind_pure_l_of Hrel2 Hk2 _); [b1_pure_fwd|intros [] ?? _ _ Hrel3 Hk3].
      eapply (b1_fwd_bind_of Hk3); [intros ????; b1_mono_tac|intros ?????; b1_mono_tac|intros ???; exists f; by eapply b1_pure_notify_dirty|intros [] ?? _ Hrel4 Hk4].
      by eapply (Frequest1 _ IH).
    + intros s sb s' r Hrel H.
      destruct (g_flushing sb) eqn:Efl.
      * destruct (b1_rel_destruct _ _ Hrel) as (_&_&Hfl&_).
        assert (Hfls : g_flushing s = true) by congruence.
        destruct (request_flush_flushing _ _ _ _ Hfls H) as [-> ->].
        exists 2%nat, sb. split; [|exact Hrel].
        cbn [b1_request_flush]. rewrite bind_get.
        destruct (live_queue sb && bool_decide (g_batch_depth sb = 0)); [|reflexivity].
        destruct (outside_bodies sb); [|by rewrite Efl].
        cbn [b1_run_flush]. rewrite bind_get, Efl. reflexivity.
      * destruct (b1_rel_destruct _ _ Hrel) as (_&_&_&Hfp&_).
        assert (Hs : s = sb) by (apply b1_state_rel_eq; [exact Hrel|auto]). subst sb.
        apply wrap_ex_S. cbn [b1_request_flush]. cbn [request_flush] in H.
        rewrite bind_get in H. setoid_rewrite bind_get.
        destruct (live_queue s && bool_decide (g_batch_depth s = 0));
          [|unfold m_return in H; simplify_eq; exists 0%nat, s'; split; [reflexivity|apply b1_state_rel_refl]].
        destruct (outside_bodies s); [by eapply (Fflush1 _ IH)|].
        rewrite Efl in H. unfold m_modify in H; simplify_eq.
        exists 0%nat, (with_flush_owed true s); split; [rewrite Efl; reflexivity|apply b1_state_rel_refl].
    + intros s sb s' r Hrel H.
      destruct (g_flushing sb) eqn:Efl.
      * destruct (b1_rel_destruct _ _ Hrel) as (_&_&Hfl&_).
        assert (Hfls : g_flushing s = true) by congruence.
        destruct (settle_flush_flushing _ _ _ _ Hfls H) as [-> ->].
        exists 2%nat, sb. split; [|exact Hrel].
        cbn [b1_settle_flush]. rewrite bind_get.
        destruct (g_flush_owed sb && bool_decide (g_batch_depth sb = 0) && outside_bodies sb); [|reflexivity].
        cbn [b1_run_flush]. rewrite bind_get, Efl. reflexivity.
      * destruct (b1_rel_destruct _ _ Hrel) as (_&_&_&Hfp&_).
        assert (Hs : s = sb) by (apply b1_state_rel_eq; [exact Hrel|auto]). subst sb.
        apply wrap_ex_S. cbn [b1_settle_flush]. cbn [settle_flush] in H.
        rewrite bind_get in H. setoid_rewrite bind_get.
        destruct (g_flush_owed s && bool_decide (g_batch_depth s = 0) && outside_bodies s);
          [by eapply (Fflush1 _ IH)|unfold m_return in H; simplify_eq; exists 0%nat, s'; split; [reflexivity|apply b1_state_rel_refl]].
    + intros s sb s' r Hrel H.
      destruct (g_flushing sb) eqn:Efl.
      * destruct (b1_rel_destruct _ _ Hrel) as (_&_&Hfl&_).
        cbn [run_flush] in H. rewrite bind_get in H.
        assert (Hsfl : g_flushing s = true) by congruence. rewrite Hsfl in H.
        unfold m_return in H. simplify_eq.
        exists 1%nat, sb. split; [cbn [b1_run_flush]; rewrite bind_get, Efl; reflexivity|exact Hrel].
      * destruct (b1_rel_destruct _ _ Hrel) as (_&_&_&Hfp&_).
        assert (Hs : s = sb) by (apply b1_state_rel_eq; [exact Hrel|auto]). subst sb.
        cbn [run_flush] in H. rewrite bind_get, Efl in H.
        unfold m_bind, m_modify in H.
        set (s1 := with_flush_owed false (with_flushing true s)) in H.
        destruct (drain f s1) as [[s2 []]|] eqn:Ed; simpl in H; [|discriminate].
        simplify_eq.
        destruct (Fdrain1 _ IH _ _ _ _ (b1_state_rel_refl s1) Ed) as (fb & sb2 & Ebd & Hrel2).
        destruct (engine_depths_kept f) as [_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ Ddrain].
        destruct (b1_depths_kept fb) as [_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ B1Ddrain].
        pose proof (Ddrain _ _ _ Ed) as [Hp2 _].
        pose proof (B1Ddrain _ _ _ Ebd) as [Hpb2 _].
        assert (Hs2 : s2 = sb2) by (apply b1_state_rel_eq; [exact Hrel2|congruence]). subst sb2.
        exists (S fb), (with_flushing false s2). split.
        -- cbn [b1_run_flush]. unfold m_bind, m_get, m_modify. rewrite Efl. fold s1. rewrite Ebd. reflexivity.
        -- apply b1_state_rel_refl.
    + intros s sb s' r Hrel H. apply wrap_ex_S. cbn [b1_drain]. cbn [drain] in H.
      rewrite bind_get in H. setoid_rewrite bind_get.
      destruct (b1_rel_fields _ _ Hrel) as (_&_&_&_&Hq&Hqh&_).
      destruct (g_queue s !! g_queue_head s) as [n|] eqn:Eq1,
               (g_queue sb !! g_queue_head sb) as [nb|] eqn:Eq2;
        try (exfalso; rewrite <- Hq, <- Hqh, Eq1 in Eq2; discriminate).
      * assert (nb = n) by (rewrite <- Hq, <- Hqh, Eq1 in Eq2; congruence); subst nb.
        eapply bind_inv in H as (s1 & [] & E1 & H). unfold m_modify in E1; simplify_eq.
        eapply bind_inv in H as (s2 & [] & E2 & E3).
        assert (Hrel1 : b1_state_rel (with_queue_head (S (g_queue_head s)) s) (with_queue_head (S (g_queue_head sb)) sb))
          by (rewrite Hqh; b1_rel_simple).
        destruct (Fexecute1 _ IH n _ _ _ _ Hrel1 E2) as (f1 & sb2 & Eb2 & Hrel2).
        destruct (Fdrain1 _ IH _ _ _ _ Hrel2 E3) as (f2 & sb3 & Eb3 & Hrel3).
        exists (Nat.max f1 f2), sb3. split; [|exact Hrel3].
        unfold m_bind, m_modify.
        rewrite (B1Mexecute _ (b1_mono_all f1) (Nat.max f1 f2) _ _ _ (Nat.le_max_l f1 f2) Eb2).
        exact (B1Mdrain _ (b1_mono_all f2) (Nat.max f1 f2) _ _ (Nat.le_max_r f1 f2) Eb3).
      * exists 0%nat; eapply b1_pure_modify; [|exact Hrel|exact H]; upd_pull_tac.
Qed.
References

Uses

169 more
Record b1_bwd_at (fb : nat) : Prop := {
  Bexec1 : forall e c s sb sb' r, b1_state_rel s sb -> b1_exec fb e c sb = Some (sb', r) ->
    exists f s', exec f e c s = Some (s', r) /\ b1_state_rel s' sb';
  Bread1 : forall n b s sb sb' r, b1_state_rel s sb -> b1_read fb n b sb = Some (sb', r) ->
    exists f s', read f n b s = Some (s', r) /\ b1_state_rel s' sb';
  Bread_node1 : forall n s sb sb' r, b1_state_rel s sb -> b1_read_node fb n sb = Some (sb', r) ->
    exists f s', read_node f n s = Some (s', r) /\ b1_state_rel s' sb';
  Bupdate1 : forall n s sb sb' r, b1_state_rel s sb -> b1_update_if_necessary fb n sb = Some (sb', r) ->
    exists f s', update_if_necessary f n s = Some (s', r) /\ b1_state_rel s' sb';
  Brop1 : forall n s sb sb' r, b1_state_rel s sb -> b1_refresh_or_pull fb n sb = Some (sb', r) ->
    exists f s', refresh_or_pull f n s = Some (s', r) /\ b1_state_rel s' sb';
  Bpull1 : forall n s sb sb' r, b1_state_rel s sb -> b1_pull fb n sb = Some (sb', r) ->
    exists f s', pull f n s = Some (s', r) /\ b1_state_rel s' sb';
  Brefresh1 : forall n s sb sb' r, b1_state_rel s sb -> b1_refresh fb n sb = Some (sb', r) ->
    exists f s', refresh f n s = Some (s', r) /\ b1_state_rel s' sb';
  Bwalk1 : forall n i s sb sb' r, b1_state_rel s sb -> b1_resolve_walk fb n i sb = Some (sb', r) ->
    exists f s', resolve_walk f n i s = Some (s', r) /\ b1_state_rel s' sb';
  Brecompute1 : forall n s sb sb' r, b1_state_rel s sb -> b1_recompute fb n sb = Some (sb', r) ->
    exists f s', recompute f n s = Some (s', r) /\ b1_state_rel s' sb';
  Brun_memo1 : forall n s sb sb' r, b1_state_rel s sb -> b1_run_memo fb n sb = Some (sb', r) ->
    exists f s', run_memo f n s = Some (s', r) /\ b1_state_rel s' sb';
  Bexecute1 : forall n s sb sb' r, b1_state_rel s sb -> b1_execute_effect fb n sb = Some (sb', r) ->
    exists f s', execute_effect f n s = Some (s', r) /\ b1_state_rel s' sb';
  Brun_effect1 : forall n s sb sb' r, b1_state_rel s sb -> b1_run_effect fb n sb = Some (sb', r) ->
    exists f s', run_effect f n s = Some (s', r) /\ b1_state_rel s' sb';
  Bbody1 : forall n s sb sb' r, b1_state_rel s sb -> b1_run_effect_body fb n sb = Some (sb', r) ->
    exists f s', run_effect_body f n s = Some (s', r) /\ b1_state_rel s' sb';
  Bdischarge1 : forall o s sb sb' r, b1_state_rel s sb -> b1_discharge fb o sb = Some (sb', r) ->
    exists f s', discharge f o s = Some (s', r) /\ b1_state_rel s' sb';
  Bretire1 : forall o s sb sb' r, b1_state_rel s sb -> b1_retire fb o sb = Some (sb', r) ->
    exists f s', retire f o s = Some (s', r) /\ b1_state_rel s' sb';
  Bdispose_scope1 : forall o s sb sb' r, b1_state_rel s sb -> b1_dispose_scope fb o sb = Some (sb', r) ->
    exists f s', dispose_scope f o s = Some (s', r) /\ b1_state_rel s' sb';
  Bteardown1 : forall o ls cs s sb sb' r, b1_state_rel s sb -> b1_teardown fb o ls cs sb = Some (sb', r) ->
    exists f s', teardown f o ls cs s = Some (s', r) /\ b1_state_rel s' sb';
  Bcleanup1 : forall o b e s sb sb' r, b1_state_rel s sb -> b1_run_cleanup fb o b e sb = Some (sb', r) ->
    exists f s', run_cleanup f o b e s = Some (s', r) /\ b1_state_rel s' sb';
  Brelease1 : forall n s sb sb' r, b1_state_rel s sb -> b1_release fb n sb = Some (sb', r) ->
    exists f s', release f n s = Some (s', r) /\ b1_state_rel s' sb';
  Bdispose1 : forall n s sb sb' r, b1_state_rel s sb -> b1_dispose_node fb n sb = Some (sb', r) ->
    exists f s', dispose_node f n s = Some (s', r) /\ b1_state_rel s' sb';
  Battach1 : forall n o s sb sb' r, b1_state_rel s sb -> b1_attach fb n o sb = Some (sb', r) ->
    exists f s', attach f n o s = Some (s', r) /\ b1_state_rel s' sb';
  Bwrite1 : forall n v s sb sb' r, b1_state_rel s sb -> b1_write_signal fb n v sb = Some (sb', r) ->
    exists f s', write_signal f n v s = Some (s', r) /\ b1_state_rel s' sb';
  Brequest1 : forall s sb sb' r, b1_state_rel s sb -> b1_request_flush fb sb = Some (sb', r) ->
    exists f s', request_flush f s = Some (s', r) /\ b1_state_rel s' sb';
  Bsettle1 : forall s sb sb' r, b1_state_rel s sb -> b1_settle_flush fb sb = Some (sb', r) ->
    exists f s', settle_flush f s = Some (s', r) /\ b1_state_rel s' sb';
  Bflush1 : forall s sb sb' r, b1_state_rel s sb -> b1_run_flush fb sb = Some (sb', r) ->
    exists f s', run_flush f s = Some (s', r) /\ b1_state_rel s' sb';
  Bdrain1 : forall s sb sb' r, b1_state_rel s sb -> b1_drain fb sb = Some (sb', r) ->
    exists f s', drain f s = Some (s', r) /\ b1_state_rel s' sb'
}.
References
Lemma b1_bwd_all fb : b1_bwd_at fb.
Qed
Proof.
  induction fb as [|fb IH].
  - split; intros; discriminate.
  - split.
    + intros e c s sb sb' r Hrel H.
      apply wrap_ex_S. cbn [exec]. cbn [b1_exec] in H.
      destruct c; repeat match goal with
      | H0 : context [on_node] |- _ => unfold on_node in H0 |- *
      | H0 : context [on_value] |- _ => unfold on_value in H0 |- *
      | H0 : context [match eval_expr ?e ?p ?x with _ => _ end] |- _ => destruct (eval_expr e p x) as [[|]|]; try discriminate
      | H0 : (if decide _ then _ else stuck) _ = Some _ |- _ => destruct (decide _); try discriminate
      end; try (unfold m_return in H; simplify_eq; exists 0%nat, s; split; [reflexivity|exact Hrel]);
      try (simplify_eq; exists 0%nat, s; split; [reflexivity|exact Hrel]).
      * by eapply (Bexec1 _ IH).
      * destruct (decide _); by eapply (Bexec1 _ IH).
      * eapply (b1_bwd_bind_of H); [intros ????; eng_mono_tac|intros ?????; eng_mono_tac|intros ???; by eapply (Bread1 _ IH)|intros o ?? _ Hrel1 Hk].
        eapply (b1_bwd_bind_pure_l_of Hrel1 Hk _); [b1_pure_bwd|intros [] ?? _ _ Hrel2 Hk2].
        destruct o; [by eapply (Bexec1 _ IH)|by eapply b1_bwd_ret].
      * eapply (b1_bwd_bind_of H); [intros ????; eng_mono_tac|intros ?????; eng_mono_tac|intros ???; by eapply (Bread1 _ IH)|intros o ?? _ Hrel1 Hk].
        eapply (b1_bwd_bind_pure_l_of Hrel1 Hk _); [b1_pure_bwd|intros [] ?? _ _ Hrel2 Hk2].
        destruct o; by eapply (Bexec1 _ IH).
      * eapply (b1_bwd_bind_of H); [intros ????; eng_mono_tac|intros ?????; eng_mono_tac|intros ???; by eapply (Bread1 _ IH)|intros o ?? _ Hrel1 Hk].
        eapply (b1_bwd_bind_pure_l_of Hrel1 Hk _); [b1_pure_bwd|intros [] ?? _ _ Hrel2 Hk2].
        destruct o; [by eapply (Bexec1 _ IH)|by eapply b1_bwd_ret].
      * eapply (b1_bwd_bind_of H); [intros ????; eng_mono_tac|intros ?????; eng_mono_tac|intros ???; by eapply (Bwrite1 _ IH)|intros [] ?? _ Hrel1 Hk].
        by eapply (Bexec1 _ IH).
      * eapply (b1_bwd_bind_pure_l_of Hrel H _); [b1_pure_bwd|intros ?? s1 _ _ Hrel1 Hk].
        eapply (b1_bwd_bind_pure_l_of Hrel1 Hk _); [b1_pure_bwd|intros ?? s2 _ _ Hrel2 Hk2].
        by eapply (Bexec1 _ IH).
      * eapply (b1_bwd_bind_pure_l_of Hrel H _); [b1_pure_bwd|intros ?? s1 _ _ Hrel1 Hk].
        eapply (b1_bwd_bind_pure_l_of Hrel1 Hk _); [b1_pure_bwd|intros ?? s2 _ _ Hrel2 Hk2].
        eapply (b1_bwd_bind_pure_l_of Hrel2 Hk2 _); [b1_pure_bwd|intros [o|] ?? _ _ Hrel3 Hk3; [|by eapply b1_bwd_ret]].
        eapply (b1_bwd_bind_of Hk3); [intros ????; eng_mono_tac|intros ?????; eng_mono_tac|intros ???; by eapply (Battach1 _ IH)|intros [] ?? _ Hrel4 Hk4].
        by eapply (Bexec1 _ IH).
      * eapply (b1_bwd_bind_pure_l_of Hrel H _); [b1_pure_bwd|intros ?? s1 _ _ Hrel1 Hk].
        eapply (b1_bwd_bind_pure_l_of Hrel1 Hk _); [b1_pure_bwd|intros ?? s2 _ _ Hrel2 Hk2].
        eapply (b1_bwd_bind_pure_l_of Hrel2 Hk2 _); [b1_pure_bwd|intros [o|] ?? _ _ Hrel3 Hk3; [|by eapply b1_bwd_ret]].
        eapply (b1_bwd_bind_of Hk3); [intros ????; eng_mono_tac|intros ?????; eng_mono_tac|intros ???; by eapply (Battach1 _ IH)|intros [] ?? _ Hrel4 Hk4].
        eapply (b1_bwd_bind_of Hk4); [intros ????; eng_mono_tac|intros ?????; eng_mono_tac|intros ???; exists fb; by eapply b1_pure_mark_dirty_bwd|intros [] ?? _ Hrel5 Hk5].
        eapply (b1_bwd_bind_of Hk5); [intros ????; eng_mono_tac|intros ?????; eng_mono_tac|intros ???; by eapply (Brequest1 _ IH)|intros [] ?? _ Hrel6 Hk6].
        by eapply (Bexec1 _ IH).
      * eapply (b1_bwd_bind_pure_l_of Hrel H _); [b1_pure_bwd|intros [o|] ?? _ _ Hrel1 Hk; [|by eapply b1_bwd_ret]].
        eapply (b1_bwd_bind_pure_l_of Hrel1 Hk _); [b1_pure_bwd|intros x ?? _ _ Hrel2 Hk2].
        eapply (b1_bwd_bind_pure_l_of Hrel2 Hk2 _); [b1_pure_bwd|intros [] ?? _ _ Hrel3 Hk3].
        destruct (o_disposed x).
        -- eapply (b1_bwd_bind_of Hk3); [intros ????; eng_mono_tac|intros ?????; eng_mono_tac| |intros [] ?? _ Hrel4 Hk4].
           ++ intros sb1' [] Hm. eapply (b1_bwd_bind_of Hm); [intros ????; eng_mono_tac|intros ?????; eng_mono_tac| |intros [] ?? _ Hrel4' Hk4'].
              ** intros sb2' [] Hd. by eapply b1_detached_bwd; [exact Hrel3|intros ??? Hrel0 ??; by eapply (Bcleanup1 _ IH)|exact Hd].
              ** by eapply (Brequest1 _ IH).
           ++ by eapply (Bexec1 _ IH).
        -- eapply (b1_bwd_bind_pure_l_of Hrel3 Hk3 _); [b1_pure_bwd|intros [] ?? _ _ Hrel4 Hk4].
           by eapply (Bexec1 _ IH).
      * eapply (b1_bwd_bind_pure_l_of Hrel H _); [b1_pure_bwd|intros d ?? _ _ Hrel1 Hk].
        destruct (n_kind d); [discriminate| |];
          (eapply (b1_bwd_bind_of Hk); [intros ????; eng_mono_tac|intros ?????; eng_mono_tac|intros ???; by eapply (Bdispose1 _ IH)|intros [] ?? _ Hrel2 Hk2];
           eapply (b1_bwd_bind_pure_l_of Hrel2 Hk2 _); [destruct (n_disposed d); b1_pure_bwd|intros [] ?? _ _ Hrel3 Hk3];
           by eapply (Bexec1 _ IH)).
      * eapply (b1_bwd_bind_pure_l_of Hrel H _); [b1_pure_bwd|intros [] ?? _ _ Hrel1 Hk].
        eapply (b1_bwd_bind_of Hk); [intros ????; eng_mono_tac|intros ?????; eng_mono_tac|intros ???; by eapply (Bexec1 _ IH)|intros r0 ?? _ Hrel2 Hk2].
        eapply (b1_bwd_bind_pure_l_of Hrel2 Hk2 _); [b1_pure_bwd|intros [] ?? _ _ Hrel3 Hk3].
        destruct r0 as [|].
        -- eapply (b1_bwd_bind_of Hk3); [intros ????; eng_mono_tac|intros ?????; eng_mono_tac|intros ???; by eapply (Brequest1 _ IH)|intros [] ?? _ Hrel4 Hk4].
           by eapply (Bexec1 _ IH).
        -- by eapply b1_bwd_ret.
      * eapply (b1_bwd_bind_pure_l_of Hrel H _); [b1_pure_bwd|intros ?? s1 _ _ Hrel1 Hk].
        eapply (b1_bwd_bind_pure_l_of Hrel1 Hk _); [b1_pure_bwd|intros [] ?? _ _ Hrel2 Hk2].
        by eapply (Bexec1 _ IH).
    + intros n b s sb sb' r Hrel H. apply wrap_ex_S. cbn [read]. cbn [b1_read] in H.
      destruct b; [by eapply (Bread_node1 _ IH)|eapply b1_untracked_bwd; [exact Hrel|intros ??? Hrel0 ??; by eapply (Bread_node1 _ IH)|exact H]].
    + intros n s sb sb' r Hrel H. apply wrap_ex_S. cbn [read_node]. cbn [b1_read_node] in H.
      eapply (b1_bwd_bind_pure_l_of Hrel H _); [b1_pure_bwd|intros d ?? _ _ Hrel1 Hk].
      destruct (n_kind d); [| |discriminate].
      * eapply (b1_bwd_bind_pure_l_of Hrel1 Hk _); [b1_pure_bwd|intros [] ?? _ _ Hrel2 Hk2].
        by eapply b1_bwd_ret.
      * eapply (b1_bwd_bind_of Hk); [intros ????; eng_mono_tac|intros ?????; eng_mono_tac| |intros d' ?? _ Hrel2 Hk2].
        -- intros sb1a d'a Hm. destruct (stale_live d).
           ++ eapply (b1_bwd_bind_of Hm); [intros ????; eng_mono_tac|intros ?????; eng_mono_tac|intros ???; by eapply (Brop1 _ IH)|intros d'0 ?? _ Hrel2a Hk2a].
              eapply (b1_bwd_bind_of Hk2a); [intros ????; eng_mono_tac|intros ?????; eng_mono_tac| |intros [] ?? _ Hrel3a Hk3a].
              ** intros sb2a [] Hm2.
                 eapply (b1_bwd_bind_pure_l_of Hrel2a Hm2 _); [b1_pure_bwd|intros [] ?? _ _ Hrel3b Hk3b].
                 eapply (b1_bwd_bind_pure_l_of Hrel3b Hk3b _); [b1_pure_bwd|intros d'' ?? _ _ Hrel4b Hk4b].
                 destruct (stale_live d''); [destruct (b1_pure_mark_reader_bwd _ _ _ _ _ Hrel4b Hk4b) as (s' & E & Hr); exists fb, s'; exact (conj E Hr)|by eapply b1_bwd_ret].
              ** by eapply b1_bwd_ret.
           ++ eapply (b1_bwd_bind_pure_l_of Hrel1 Hm _); [b1_pure_bwd|intros [] ?? _ _ Hrel2b Hk2b].
              exists 0%nat; by eapply b1_pure_get_node_bwd.
        -- destruct (n_status d'); by eapply b1_bwd_ret.
    + intros n s sb sb' r Hrel H. apply wrap_ex_S. cbn [update_if_necessary]. cbn [b1_update_if_necessary] in H.
      eapply (b1_bwd_bind_pure_l_of Hrel H _); [b1_pure_bwd|intros d ?? _ _ Hrel1 Hk].
      destruct (n_kind d); [by eapply b1_bwd_ret| |by eapply b1_bwd_ret].
      destruct (stale_live d); [|by eapply b1_bwd_ret].
      eapply (b1_bwd_bind_of Hk); [intros ????; eng_mono_tac|intros ?????; eng_mono_tac|intros ???; by eapply (Brop1 _ IH)|intros ??? _ Hrel2 Hk2].
      by eapply b1_bwd_ret.
    + intros n s sb sb' r Hrel H. apply wrap_ex_S. cbn [refresh_or_pull]. cbn [b1_refresh_or_pull] in H.
      rewrite bind_get in H. setoid_rewrite bind_get.
      destruct (g_flushing sb) eqn:Efl.
      * unfold deferring_b1 in H. rewrite Efl in H. simpl in H.
        destruct (deferring s) eqn:Edef.
        -- eapply (b1_bwd_bind_of H); [intros ????; eng_mono_tac|intros ?????; eng_mono_tac|intros ???; by eapply (Brefresh1 _ IH)|intros [] ?? _ Hrel1 Hk].
           destruct (b1_pure_get_node_bwd _ _ _ _ _ Hrel1 Hk) as (s' & E & Hr); exists 0%nat, s'; exact (conj E Hr).
        -- eapply bind_inv in H as (sb2 & [] & Eb2 & H). unfold get_node in H.
           destruct (g_nodes sb2 !! n) as [d0|] eqn:En; simplify_eq.
           assert (Hrel1 : b1_state_rel (with_pull_depth (S (g_pull_depth s)) s) sb)
             by (by apply b1_rel_with_pull_S_left).
           destruct (Brefresh1 _ IH n _ _ _ _ Hrel1 Eb2) as (f1 & s2 & E2 & Hrel2).
           set (f2 := Nat.max f1 2).
           pose proof (Mrefresh _ (engine_mono_all f1) f2 n _ _ (Nat.le_max_l f1 2) E2) as E2'.
           destruct (engine_depths_kept f2) as [_ _ _ _ _ _ Dref _].
           pose proof (Dref n _ _ _ E2') as [Hp2 Hfl2].
           destruct (b1_depths_kept fb) as [_ _ _ _ _ _ B1Dref _].
           pose proof (B1Dref n _ _ _ Eb2) as [Hpb2 Hflb2].
           set (s3 := with_pull_depth (pred (g_pull_depth s2)) s2).
           assert (Hfls3 : g_flushing s3 = true).
           { unfold s3. simpl. rewrite Hfl2. simpl. destruct (b1_rel_destruct _ _ Hrel) as (_&_&Hf&_). congruence. }
           assert (Hsettle : (if bool_decide (g_pull_depth s3 = 0%nat) && g_flush_owed s3 then settle_flush f2 else m_return tt) s3 = Some (s3, tt)).
           { destruct (bool_decide (g_pull_depth s3 = 0) && g_flush_owed s3); [|reflexivity].
             assert (Hf2 : f2 >= 2) by (unfold f2; lia).
             destruct f2 as [|[|k]]; [lia|lia|].
             cbn [settle_flush]. rewrite bind_get.
             destruct (g_flush_owed s3 && bool_decide (g_batch_depth s3 = 0) && outside_bodies s3); [|reflexivity].
             cbn [run_flush]. rewrite bind_get, Hfls3. reflexivity. }
           assert (Hrel3 : b1_state_rel s3 sb').
           { unfold s3. destruct (b1_rel_destruct _ _ Hrel) as (_&_&Hfs&_&Hts).
             destruct Hrel2 as [Hw2 [Hf2 Ht2]]. split.
             - rewrite <- Hw2. unfold with_pull_depth. destruct s2; reflexivity.
             - simpl. split; [intro; congruence|]. intro Hfl0. rewrite Hp2. simpl.
               pose proof (Hts Efl) as Hle. rewrite Hpb2. lia. }
           destruct (b1_rel_fields _ _ Hrel2) as (_&_&Hn2&_).
           exists (S f2), s3. split; [|exact Hrel3].
           cbn [pull]. unfold m_bind, m_modify, m_get, get_node.
           rewrite E2', Hn2, En.
           change ((if bool_decide (g_pull_depth (with_pull_depth (pred (g_pull_depth s2)) s2) = 0%nat) && g_flush_owed (with_pull_depth (pred (g_pull_depth s2)) s2) then settle_flush f2 else m_return tt) (with_pull_depth (pred (g_pull_depth s2)) s2))
             with ((if bool_decide (g_pull_depth s3 = 0%nat) && g_flush_owed s3 then settle_flush f2 else m_return tt) s3).
           rewrite Hsettle. reflexivity.
      * destruct (b1_rel_destruct _ _ Hrel) as (_ & _ & Hfs & Hfp & _).
        assert (Hs : s = sb) by (apply b1_state_rel_eq; [exact Hrel|auto]). subst sb.
        unfold deferring_b1 in H. rewrite Efl in H. simpl in H.
        destruct (deferring s).
        -- eapply (b1_bwd_bind_of H); [intros ????; eng_mono_tac|intros ?????; eng_mono_tac|intros ???; by eapply (Brefresh1 _ IH)|intros [] ?? _ Hrel1 Hk].
           exists 0%nat; by eapply b1_pure_get_node_bwd.
        -- by eapply (Bpull1 _ IH).
    + intros n s sb sb' r Hrel H. apply wrap_ex_S. cbn [pull]. cbn [b1_pull] in H.
      eapply (b1_bwd_bind_pure_l_of Hrel H _);
        [intros sa0 sba0 sba1 [] Hr0 Hm; unfold m_modify in Hm; simplify_eq; eexists; split; [unfold m_modify; reflexivity|by apply b1_rel_with_pull_S]|intros [] ?? _ _ Hrel1 Hk].
      eapply (b1_bwd_bind_of Hk); [intros ????; eng_mono_tac|intros ?????; eng_mono_tac|intros ???; by eapply (Brefresh1 _ IH)|intros [] ?? _ Hrel2 Hk2].
      eapply (b1_bwd_bind_pure_l_of Hrel2 Hk2 _); [b1_pure_bwd|intros d ?? _ _ Hrel3 Hk3].
      eapply (b1_bwd_bind_pure_l_of Hrel3 Hk3 _);
        [intros s_p2 sb_p2 s_p3 [] Hr0 Hm; unfold m_modify in Hm; simplify_eq; eexists; split; [unfold m_modify; reflexivity|by apply b1_rel_with_pull_pred]|intros [] sb3 s3 _ _ Hrel4 Hk4].
      rewrite bind_get in Hk4. setoid_rewrite bind_get.
      eapply (b1_bwd_bind_of Hk4); [intros ????; eng_mono_tac|intros ?????; eng_mono_tac| |intros [] ?? _ Hrel5 Hk5; by eapply b1_bwd_ret].
      intros sb_q a Hm.
      destruct (g_flushing sb3) eqn:Efl.
      * destruct (b1_rel_destruct _ _ Hrel4) as (_&_&Hfl&_).
        assert (Hfls : g_flushing s3 = true) by congruence.
        assert (Hsb1 : sb_q = sb3 /\ a = tt).
        { destruct (bool_decide (g_pull_depth sb3 = 0) && g_flush_owed sb3).
          - by eapply b1_settle_flush_flushing.
          - unfold m_return in Hm. by simplify_eq. }
        destruct Hsb1 as [-> ->].
        exists 2%nat, s3. split; [|exact Hrel4].
        destruct (bool_decide (g_pull_depth s3 = 0) && g_flush_owed s3); [|reflexivity].
        cbn [settle_flush]. rewrite bind_get.
        destruct (g_flush_owed s3 && bool_decide (g_batch_depth s3 = 0) && outside_bodies s3); [|reflexivity].
        cbn [run_flush]. rewrite bind_get, Hfls. reflexivity.
      * destruct (b1_rel_destruct _ _ Hrel4) as (_&_&_&Hfp&_).
        assert (Hsb4 : s3 = sb3) by (apply b1_state_rel_eq; [exact Hrel4|apply Hfp; congruence]).
        subst sb3.
        destruct (bool_decide (g_pull_depth s3 = 0) && g_flush_owed s3); [by eapply (Bsettle1 _ IH)|by eapply b1_bwd_ret].
    + intros n s sb sb' r Hrel H. apply wrap_ex_S. cbn [refresh]. cbn [b1_refresh] in H.
      rewrite bind_get in H. setoid_rewrite bind_get.
      destruct (b1_rel_fields _ _ Hrel) as (_&_&_&_&_&_&_&_&_&_&_&_&_&_&Hrun&Hchk&_).
      destruct (decide (n ∈ g_checking sb)), (decide (n ∈ g_checking s)); try (exfalso; rewrite Hchk in *; contradiction).
      { unfold m_step in H |- *. destruct (mark_dirty fb n sb) eqn:Em; simplify_eq.
        destruct (b1_pure_mark_dirty_bwd fb n s sb sb' tt Hrel ltac:(unfold m_step; by rewrite Em)) as (s1 & Em1 & Hrel1).
        unfold m_step in Em1. destruct (mark_dirty fb n s) eqn:Ems; simplify_eq.
        exists fb, s1. split; [by rewrite Ems|exact Hrel1]. }
      destruct (decide (n ∈ g_running sb)), (decide (n ∈ g_running s)); try (exfalso; rewrite Hrun in *; contradiction); [by eapply b1_bwd_ret|].
      eapply (b1_bwd_bind_pure_l_of Hrel H _); [b1_pure_bwd|intros d ?? _ _ Hrel1 Hk].
      eapply (b1_bwd_bind_of Hk); [intros ????; eng_mono_tac|intros ?????; eng_mono_tac| |intros [] ?? _ Hrel2 Hk2].
      * intros sb_ref [] Hm. destruct (decide (n_freshness d = Check)); [|by eapply b1_bwd_ret].
        eapply (b1_bwd_bind_pure_l_of Hrel1 Hm _); [b1_pure_bwd|intros [] ?? _ _ Hrel2a Hk2a].
        eapply (b1_bwd_bind_of Hk2a); [intros ????; eng_mono_tac|intros ?????; eng_mono_tac|intros ???; by eapply (Bwalk1 _ IH)|intros [] ?? _ Hrel3a Hk3a].
        eapply (b1_bwd_bind_pure_l_of Hrel3a Hk3a _); [b1_pure_bwd|intros [] ?? _ _ Hrel4a Hk4a].
        eapply (b1_bwd_bind_pure_l_of Hrel4a Hk4a _); [b1_pure_bwd|intros d' ?? _ _ Hrel5a Hk5a].
        destruct (decide (n_freshness d' = Check)); [|by eapply b1_bwd_ret].
        eapply (b1_bwd_bind_pure_l_of Hrel5a Hk5a _); [b1_pure_bwd|intros [] ?? _ _ Hrel6a Hk6a].
        exists 0%nat; by eapply b1_pure_modify_node_bwd.
      * eapply (b1_bwd_bind_pure_l_of Hrel2 Hk2 _); [b1_pure_bwd|intros d' ?? _ _ Hrel3 Hk3].
        destruct (decide (n_freshness d' = Dirty)); [by eapply (Brecompute1 _ IH)|by eapply b1_bwd_ret].
    + intros n i s sb sb' r Hrel H. apply wrap_ex_S. cbn [resolve_walk]. cbn [b1_resolve_walk] in H.
      eapply (b1_bwd_bind_pure_l_of Hrel H _); [b1_pure_bwd|intros d sb1 s1 _ _ Hrel1 Hk].
      rewrite bind_get in Hk. setoid_rewrite bind_get.
      destruct (decide (n_freshness d = Check)); [|by eapply b1_bwd_ret].
      destruct (b1_rel_fields _ _ Hrel1) as (_&He1&_).
      destruct (source_slots (sources_of (g_edges sb1) n) !! i) as [[src ?]|] eqn:Esb1,
               (source_slots (sources_of (g_edges s1) n) !! i) as [[srcs ?]|] eqn:Es1;
        try (exfalso; rewrite He1, Esb1 in Es1; discriminate).
      * assert (srcs = src) by (rewrite He1, Esb1 in Es1; congruence); subst srcs.
        eapply (b1_bwd_bind_of Hk); [intros ????; eng_mono_tac|intros ?????; eng_mono_tac|intros ???; by eapply (Bupdate1 _ IH)|intros [] ?? _ Hrel2 Hk2].
        eapply (b1_bwd_bind_pure_l_of Hrel2 Hk2 _); [b1_pure_bwd|intros d' ?? _ _ Hrel3 Hk3].
        eapply (b1_bwd_bind_pure_l_of Hrel3 Hk3 _); [b1_pure_bwd|intros d_src sb4 s4 _ _ Hrel4 Hk4].
        rewrite bind_get in Hk4. setoid_rewrite bind_get.
        destruct (b1_rel_fields _ _ Hrel4) as (_&_&_&_&_&_&_&_&_&_&_&_&_&_&Hrun4&Hchk4&_).
        eapply (b1_bwd_bind_of Hk4); [intros ????; eng_mono_tac|intros ?????; eng_mono_tac| |intros [] ?? _ Hrel5 Hk5; by eapply (Bwalk1 _ IH)].
        intros sb5 [] Hm.
        destruct (decide (n_freshness d' = Check /\ stale_live d_src = true /\ (src ∈ g_running sb4 \/ src ∈ g_checking sb4))) as [Hdb|Hdb];
          destruct (decide (n_freshness d' = Check /\ stale_live d_src = true /\ (src ∈ g_running s4 \/ src ∈ g_checking s4))) as [Hd|Hd];
          try (exfalso; rewrite Hrun4, Hchk4 in *; tauto).
        -- unfold m_step in Hm |- *. destruct (mark_dirty fb n sb4) eqn:Em; simplify_eq.
           destruct (b1_pure_mark_dirty_bwd fb n s4 sb4 sb5 tt Hrel4 ltac:(unfold m_step; by rewrite Em)) as (s5 & Em5 & Hrel5).
           unfold m_step in Em5. destruct (mark_dirty fb n s4) eqn:Ems; simplify_eq.
           exists fb, s5. split; [by rewrite Ems|exact Hrel5].
        -- by eapply b1_bwd_ret.
      * assert (Hsh : slot_holds stale_live s1 n = slot_holds stale_live sb1 n).
        { destruct (b1_rel_fields _ _ Hrel1) as (_&He'&Hn'&_). unfold slot_holds. by rewrite He', Hn'. }
        destruct (slot_holds stale_live sb1 n) eqn:Esh1, (slot_holds stale_live s1 n) eqn:Esh2;
          try (exfalso; congruence); [by eapply (Bwalk1 _ IH)|by eapply b1_bwd_ret].
    + intros n s sb sb' r Hrel H. apply wrap_ex_S. cbn [recompute]. cbn [b1_recompute] in H.
      eapply (b1_bwd_bind_pure_l_of Hrel H _); [b1_pure_bwd|intros d ?? _ _ Hrel1 Hk].
      destruct (n_scope d) as [o|]; [|by eapply (Brun_memo1 _ IH)].
      eapply (b1_bwd_bind_pure_l_of Hrel1 Hk _); [b1_pure_bwd|intros [] ?? _ _ Hrel2 Hk2].
      eapply (b1_bwd_bind_pure_l_of Hrel2 Hk2 _); [b1_pure_bwd|intros [] ?? _ _ Hrel3 Hk3].
      eapply (b1_bwd_bind_of Hk3); [intros ????; eng_mono_tac|intros ?????; eng_mono_tac|intros ???; by eapply (Bdischarge1 _ IH)|intros [] ?? _ Hrel4 Hk4].
      eapply (b1_bwd_bind_pure_l_of Hrel4 Hk4 _); [b1_pure_bwd|intros d' ?? _ _ Hrel5 Hk5].
      destruct (n_disposed d').
      * destruct (decide (n_freshness d' = Clean)); [exists 0%nat; by eapply b1_pure_modify_node_bwd|by eapply b1_bwd_ret].
      * destruct (decide (n_runs d' = n_runs d)); [by eapply (Brun_memo1 _ IH)|].
        destruct (decide (n_freshness d' = Dirty)); [by eapply (Brecompute1 _ IH)|by eapply b1_bwd_ret].
    + intros n s sb sb' r Hrel H. apply wrap_ex_S. cbn [run_memo]. cbn [b1_run_memo] in H.
      eapply (b1_bwd_bind_pure_l_of Hrel H _); [b1_pure_bwd|intros d ?? _ _ Hrel1 Hk].
      eapply (b1_bwd_bind_pure_l_of Hrel1 Hk _); [b1_pure_bwd|intros [] ?? _ _ Hrel2 Hk2].
      eapply (b1_bwd_bind_pure_l_of Hrel2 Hk2 _); [b1_pure_bwd|intros [] ?? _ _ Hrel3 Hk3].
      eapply (b1_bwd_bind_pure_l_of Hrel3 Hk3 _); [b1_pure_bwd|intros [] ?? _ _ Hrel4 Hk4].
      eapply (b1_bwd_bind_pure_l_of Hrel4 Hk4 _); [b1_pure_bwd|intros [] ?? _ _ Hrel5 Hk5].
      eapply (b1_bwd_bind_of Hk5); [intros ????; eng_mono_tac|intros ?????; eng_mono_tac| |intros r0 ?? _ Hrel6 Hk6].
      { intros sb6 r0a Hm. by eapply b1_hosted_bwd; [exact Hrel5|intros ??? Hrel0 ??; by eapply (Bexec1 _ IH)|exact Hm]. }
      eapply (b1_bwd_bind_pure_l_of Hrel6 Hk6 _); [b1_pure_bwd|intros d1 ?? _ _ Hrel7 Hk7].
      eapply (b1_bwd_bind_pure_l_of Hrel7 Hk7 _); [destruct (if n_violated d1 then Fail pure_scope_failure else r0); b1_pure_bwd|intros [] ?? _ _ Hrel8 Hk8].
      eapply (b1_bwd_bind_pure_l_of Hrel8 Hk8 _); [b1_pure_bwd|intros [] ?? _ _ Hrel9 Hk9].
      eapply (b1_bwd_bind_pure_l_of Hrel9 Hk9 _); [b1_pure_bwd|intros [] ?? _ _ Hrel10 Hk10].
      eapply (b1_bwd_bind_pure_l_of Hrel10 Hk10 _); [destruct (if n_violated d1 then Fail pure_scope_failure else r0); b1_pure_bwd|intros [] ?? _ _ Hrel11 Hk11].
      eapply (b1_bwd_bind_pure_l_of Hrel11 Hk11 _); [b1_pure_bwd|intros d2 ?? _ _ Hrel12 Hk12].
      eapply (b1_bwd_bind_pure_l_of Hrel12 Hk12 _);
        [destruct (decide _); [b1_pure_bwd|destruct (n_comparer d2); b1_pure_bwd]|intros moved sb13 s13 _ _ Hrel13 Hk13].
      rewrite bind_get in Hk13. setoid_rewrite bind_get.
      destruct (b1_rel_fields _ _ Hrel13) as (_&_&_&_&_&_&_&_&_&Hc13&_).
      destruct (g_current sb13) as [cb13|] eqn:Ecs1, (g_current s13) as [c13|] eqn:Ecs2;
        try (exfalso; rewrite Hc13 in Ecs2; congruence);
        [assert (c13 = cb13) by congruence; subst c13|];
        (eapply (b1_bwd_bind_pure_l_of Hrel13 Hk13 _); [b1_pure_bwd|intros [] ?? _ _ Hrel14 Hk14];
         eapply (b1_bwd_bind_of Hk14); [intros ????; eng_mono_tac|intros ?????; eng_mono_tac| |intros [] ?? _ Hrel15 Hk15];
         [intros sb15 [] Hm; destruct moved; [|by eapply b1_bwd_ret];
          eapply (b1_bwd_bind_of Hm); [intros ????; eng_mono_tac|intros ?????; eng_mono_tac|intros ???; exists fb; by eapply b1_pure_notify_dirty_except_bwd|intros [] sb16 s16 _ Hrel16 Hk16];
          rewrite bind_get in Hk16; setoid_rewrite bind_get;
          destruct (b1_rel_fields _ _ Hrel16) as (_&_&_&_&Hq16&Hqh16&Hfl16&_&Hbd16&_);
          assert (Hcond : bool_decide (g_batch_depth s16 = 0%nat) && negb (g_flushing s16) && live_queue s16 =
                          bool_decide (g_batch_depth sb16 = 0%nat) && negb (g_flushing sb16) && live_queue sb16)
            by (unfold live_queue; by rewrite Hbd16, Hfl16, Hq16, Hqh16);
          destruct (bool_decide (g_batch_depth sb16 = 0%nat) && negb (g_flushing sb16) && live_queue sb16) eqn:Ec1,
                   (bool_decide (g_batch_depth s16 = 0%nat) && negb (g_flushing s16) && live_queue s16) eqn:Ec2;
            try (exfalso; congruence); [exists 0%nat; b1_mod_bwd|by eapply b1_bwd_ret]
         |eapply (b1_bwd_bind_pure_l_of Hrel15 Hk15 _); [b1_pure_bwd|intros d3 ?? _ _ Hrel16 Hk16];
          destruct (decide (n_freshness d3 = Clean)); [by eapply b1_bwd_ret|exists 0%nat; by eapply b1_pure_announce_bwd]]).
    + intros n s sb sb' r Hrel H. apply wrap_ex_S. cbn [execute_effect]. cbn [b1_execute_effect] in H.
      eapply (b1_bwd_bind_pure_l_of Hrel H _); [b1_pure_bwd|intros d ?? _ _ Hrel1 Hk].
      destruct (n_disposed d); [exists 0%nat; by eapply b1_pure_modify_node_bwd|].
      eapply (b1_bwd_bind_of Hk); [intros ????; eng_mono_tac|intros ?????; eng_mono_tac| |intros [] ?? _ Hrel2 Hk2].
      * intros sb2 [] Hm. destruct (decide (n_freshness d = Check)); [|by eapply b1_bwd_ret].
        eapply (b1_bwd_bind_of Hm); [intros ????; eng_mono_tac|intros ?????; eng_mono_tac|intros ???; by eapply (Bwalk1 _ IH)|intros [] ?? _ Hrel3 Hk3].
        eapply (b1_bwd_bind_pure_l_of Hrel3 Hk3 _); [b1_pure_bwd|intros d' ?? _ _ Hrel4 Hk4].
        destruct (decide (n_freshness d' = Check)); [|by eapply b1_bwd_ret].
        eapply (b1_bwd_bind_pure_l_of Hrel4 Hk4 _); [b1_pure_bwd|intros [] ?? _ _ Hrel5 Hk5].
        exists 0%nat; by eapply b1_pure_modify_node_bwd.
      * eapply (b1_bwd_bind_pure_l_of Hrel2 Hk2 _); [b1_pure_bwd|intros d' ?? _ _ Hrel3 Hk3].
        eapply (b1_bwd_bind_pure_l_of Hrel3 Hk3 _); [b1_pure_bwd|intros [] ?? _ _ Hrel4 Hk4].
        destruct (decide (n_freshness d' = Dirty)); [by eapply (Brun_effect1 _ IH)|by eapply b1_bwd_ret].
    + intros n s sb sb' r Hrel H. apply wrap_ex_S. cbn [run_effect]. cbn [b1_run_effect] in H.
      eapply (b1_bwd_bind_pure_l_of Hrel H _); [b1_pure_bwd|intros [] ?? _ _ Hrel1 Hk].
      eapply (b1_bwd_bind_pure_l_of Hrel1 Hk _); [b1_pure_bwd|intros [] ?? _ _ Hrel2 Hk2].
      eapply (b1_bwd_bind_pure_l_of Hrel2 Hk2 _); [b1_pure_bwd|intros [] ?? _ _ Hrel3 Hk3].
      eapply (b1_bwd_bind_pure_l_of Hrel3 Hk3 _); [b1_pure_bwd|intros d ?? _ _ Hrel4 Hk4].
      eapply (b1_bwd_bind_of Hk4); [intros ????; eng_mono_tac|intros ?????; eng_mono_tac| |intros [] ?? _ Hrel5 Hk5].
      * intros sb5 [] Hm. destruct (n_scope d) as [o|]; [|by eapply b1_bwd_ret].
        eapply (b1_bwd_bind_pure_l_of Hrel4 Hm _); [b1_pure_bwd|intros [] ?? _ _ Hrel5a Hk5a].
        eapply (b1_bwd_bind_of Hk5a); [intros ????; eng_mono_tac|intros ?????; eng_mono_tac|intros ???; by eapply (Bdischarge1 _ IH)|intros [] ?? _ Hrel6a Hk6a].
        exists 0%nat; by eapply b1_pure_modify_node_bwd.
      * eapply (b1_bwd_bind_pure_l_of Hrel5 Hk5 _); [b1_pure_bwd|intros d' ?? _ _ Hrel6 Hk6].
        eapply (b1_bwd_bind_of Hk6); [intros ????; eng_mono_tac|intros ?????; eng_mono_tac| |intros [] ?? _ Hrel7 Hk7; exists 0%nat; b1_mod_bwd].
        intros sb7 [] Hm. destruct (n_disposed d'); [by eapply b1_bwd_ret|by eapply (Bbody1 _ IH)].
    + intros n s sb sb' r Hrel H. apply wrap_ex_S. cbn [run_effect_body]. cbn [b1_run_effect_body] in H.
      eapply (b1_bwd_bind_pure_l_of Hrel H _); [b1_pure_bwd|intros d ?? _ _ Hrel1 Hk].
      eapply (b1_bwd_bind_pure_l_of Hrel1 Hk _); [b1_pure_bwd|intros [] ?? _ _ Hrel2 Hk2].
      eapply (b1_bwd_bind_pure_l_of Hrel2 Hk2 _); [b1_pure_bwd|intros [] ?? _ _ Hrel3 Hk3].
      eapply (b1_bwd_bind_of Hk3); [intros ????; eng_mono_tac|intros ?????; eng_mono_tac| |intros r0 ?? _ Hrel4 Hk4].
      { intros sb4 r0a Hm. by eapply b1_hosted_bwd; [exact Hrel3|intros ??? Hrel0 ??; by eapply (Bexec1 _ IH)|exact Hm]. }
      eapply (b1_bwd_bind_pure_l_of Hrel4 Hk4 _); [b1_pure_bwd|intros d' ?? _ _ Hrel5 Hk5].
      eapply (b1_bwd_bind_pure_l_of Hrel5 Hk5 _); [b1_pure_bwd|intros [] ?? _ _ Hrel6 Hk6].
      destruct r0; [by eapply b1_bwd_ret|].
      eapply (b1_bwd_bind_pure_l_of Hrel6 Hk6 _); [b1_pure_bwd|intros [] ?? _ _ Hrel7 Hk7].
      exists 0%nat; b1_mod_bwd.
    + intros o s sb sb' r Hrel H. apply wrap_ex_S. cbn [discharge]. cbn [b1_discharge] in H.
      eapply (b1_bwd_bind_of H); [intros ????; eng_mono_tac|intros ?????; eng_mono_tac| |intros [] sb1 s1 _ Hrel1 Hk].
      * intros sb1 [] Hm. by eapply b1_detached_bwd; [exact Hrel|intros ??? Hrel0 ??; by eapply (Bdispose_scope1 _ IH)|exact Hm].
      * rewrite bind_get in Hk. setoid_rewrite bind_get.
        destruct (b1_rel_fields _ _ Hrel1) as (_&_&_&_&Hq1&Hqh1&Hfl1&_&Hbd1&_).
        assert (Hcond : bool_decide (g_batch_depth s1 = 0%nat) && negb (g_flushing s1) && live_queue s1 =
                        bool_decide (g_batch_depth sb1 = 0%nat) && negb (g_flushing sb1) && live_queue sb1)
          by (unfold live_queue; by rewrite Hbd1, Hfl1, Hq1, Hqh1).
        destruct (bool_decide (g_batch_depth sb1 = 0%nat) && negb (g_flushing sb1) && live_queue sb1) eqn:Ec1,
                 (bool_decide (g_batch_depth s1 = 0%nat) && negb (g_flushing s1) && live_queue s1) eqn:Ec2;
          try (exfalso; congruence); [exists 0%nat; b1_mod_bwd|by eapply b1_bwd_ret].
    + intros o s sb sb' r Hrel H. apply wrap_ex_S. cbn [retire]. cbn [b1_retire] in H.
      eapply (b1_bwd_bind_of H); [intros ????; eng_mono_tac|intros ?????; eng_mono_tac| |intros [] sb1 s1 _ Hrel1 Hk].
      * intros sb1' [] Hm. eapply b1_detached_bwd; [exact Hrel| |exact Hm].
        intros s0 sb0 Hrel0 sb1'' r' Hm0.
        eapply (b1_bwd_bind_pure_l_of Hrel0 Hm0 _); [b1_pure_bwd|intros x ?? _ _ Hrel2 Hk2].
        destruct (o_disposed x); [by eapply b1_bwd_ret|].
        eapply (b1_bwd_bind_pure_l_of Hrel2 Hk2 _); [b1_pure_bwd|intros [] ?? _ _ Hrel3 Hk3].
        by eapply (Bdispose_scope1 _ IH).
      * rewrite bind_get in Hk. setoid_rewrite bind_get.
        destruct (b1_rel_fields _ _ Hrel1) as (_&_&_&_&Hq1&Hqh1&_).
        assert (Hlq : live_queue s1 = live_queue sb1) by (unfold live_queue; by rewrite Hq1, Hqh1).
        destruct (live_queue sb1) eqn:El1, (live_queue s1) eqn:El2;
          try (exfalso; congruence); [by eapply (Brequest1 _ IH)|by eapply b1_bwd_ret].
    + intros o s sb sb' r Hrel H. apply wrap_ex_S. cbn [dispose_scope]. cbn [b1_dispose_scope] in H.
      eapply (b1_bwd_bind_pure_l_of Hrel H _); [b1_pure_bwd|intros x ?? _ _ Hrel1 Hk].
      eapply (b1_bwd_bind_pure_l_of Hrel1 Hk _); [b1_pure_bwd|intros [] ?? _ _ Hrel2 Hk2].
      by eapply (Bteardown1 _ IH).
    + intros o ls cs s sb sb' r Hrel H. apply wrap_ex_S. cbn [teardown]. cbn [b1_teardown] in H.
      destruct ls as [|l ls'], cs as [|[[b e] a] cs']; [by eapply b1_bwd_ret|..].
      * eapply (b1_bwd_bind_of H); [intros ????; eng_mono_tac|intros ?????; eng_mono_tac|intros ???; by eapply (Bcleanup1 _ IH)|intros [] ?? _ Hrel1 Hk].
        by eapply (Bteardown1 _ IH).
      * eapply (b1_bwd_bind_of H); [intros ????; eng_mono_tac|intros ?????; eng_mono_tac|intros ???; by eapply (Brelease1 _ IH)|intros [] ?? _ Hrel1 Hk].
        by eapply (Bteardown1 _ IH).
      * destruct (decide (a < l_acquired l)).
        -- eapply (b1_bwd_bind_of H); [intros ????; eng_mono_tac|intros ?????; eng_mono_tac|intros ???; by eapply (Brelease1 _ IH)|intros [] ?? _ Hrel1 Hk].
           by eapply (Bteardown1 _ IH).
        -- eapply (b1_bwd_bind_of H); [intros ????; eng_mono_tac|intros ?????; eng_mono_tac|intros ???; by eapply (Bcleanup1 _ IH)|intros [] ?? _ Hrel1 Hk].
           by eapply (Bteardown1 _ IH).
    + intros o b e s sb sb' r Hrel H. apply wrap_ex_S. cbn [run_cleanup]. cbn [b1_run_cleanup] in H.
      eapply (b1_bwd_bind_pure_l_of Hrel H _); [b1_pure_bwd|intros x ?? _ _ Hrel1 Hk].
      eapply (b1_bwd_bind_pure_l_of Hrel1 Hk _); [b1_pure_bwd|intros [] ?? _ _ Hrel2 Hk2].
      eapply (b1_bwd_bind_of Hk2); [intros ????; eng_mono_tac|intros ?????; eng_mono_tac|intros ???; by eapply (Bexec1 _ IH)|intros r0 ?? _ Hrel3 Hk3].
      destruct r0; [by eapply b1_bwd_ret|].
      eapply (b1_bwd_bind_pure_l_of Hrel3 Hk3 _); [b1_pure_bwd|intros [] ?? _ _ Hrel4 Hk4].
      exists 0%nat; b1_mod_bwd.
    + intros n s sb sb' r Hrel H. apply wrap_ex_S. cbn [release]. cbn [b1_release] in H.
      eapply (b1_bwd_bind_pure_l_of Hrel H _); [b1_pure_bwd|intros [] ?? _ _ Hrel1 Hk].
      by eapply (Bdispose1 _ IH).
    + intros n s sb sb' r Hrel H. apply wrap_ex_S. cbn [dispose_node]. cbn [b1_dispose_node] in H.
      eapply (b1_bwd_bind_pure_l_of Hrel H _); [b1_pure_bwd|intros d ?? _ _ Hrel1 Hk].
      destruct (n_disposed d); [by eapply b1_bwd_ret|].
      eapply (b1_bwd_bind_pure_l_of Hrel1 Hk _); [b1_pure_bwd|intros [] ?? _ _ Hrel2 Hk2].
      eapply (b1_bwd_bind_pure_l_of Hrel2 Hk2 _); [destruct (n_link d); b1_pure_bwd|intros [] ?? _ _ Hrel3 Hk3].
      eapply (b1_bwd_bind_pure_l_of Hrel3 Hk3 _); [b1_pure_bwd|intros [] ?? _ _ Hrel4 Hk4].
      destruct (n_scope d); [by eapply (Bretire1 _ IH)|by eapply b1_bwd_ret].
    + intros n o s sb sb' r Hrel H. apply wrap_ex_S. cbn [attach]. cbn [b1_attach] in H.
      eapply (b1_bwd_bind_pure_l_of Hrel H _); [b1_pure_bwd|intros x ?? _ _ Hrel1 Hk].
      destruct (o_disposed x).
      * eapply (b1_bwd_bind_of Hk); [intros ????; eng_mono_tac|intros ?????; eng_mono_tac|intros ???; by eapply (Brelease1 _ IH)|intros [] ?? _ Hrel2 Hk2].
        exists 0%nat; by eapply b1_pure_modify_node_bwd.
      * eapply (b1_bwd_bind_pure_l_of Hrel1 Hk _); [b1_pure_bwd|intros [] ?? _ _ Hrel2 Hk2].
        exists 0%nat; by eapply b1_pure_modify_node_bwd.
    + intros n v s sb sb' r Hrel H. apply wrap_ex_S. cbn [write_signal]. cbn [b1_write_signal] in H.
      eapply (b1_bwd_bind_pure_l_of Hrel H _); [b1_pure_bwd|intros d ?? _ _ Hrel1 Hk].
      destruct (n_kind d); [|discriminate|discriminate].
      destruct (decide (n_value d = v)); [by eapply b1_bwd_ret|].
      eapply (b1_bwd_bind_pure_l_of Hrel1 Hk _); [b1_pure_bwd|intros [] ?? _ _ Hrel2 Hk2].
      eapply (b1_bwd_bind_pure_l_of Hrel2 Hk2 _); [b1_pure_bwd|intros [] ?? _ _ Hrel3 Hk3].
      eapply (b1_bwd_bind_of Hk3); [intros ????; eng_mono_tac|intros ?????; eng_mono_tac|intros ???; exists fb; by eapply b1_pure_notify_dirty_bwd|intros [] ?? _ Hrel4 Hk4].
      by eapply (Brequest1 _ IH).
    + intros s sb sb' r Hrel H.
      destruct (g_flushing sb) eqn:Efl.
      * destruct (b1_rel_destruct _ _ Hrel) as (_&_&Hfl&_).
        destruct (b1_request_flush_flushing _ _ _ _ Efl H) as [-> ->].
        exists 2%nat, s. split; [|exact Hrel].
        cbn [request_flush]. rewrite bind_get.
        destruct (live_queue s && bool_decide (g_batch_depth s = 0)); [|reflexivity].
        destruct (outside_bodies s); [|by rewrite Hfl, Efl].
        cbn [run_flush]. rewrite bind_get, Hfl, Efl. reflexivity.
      * destruct (b1_rel_destruct _ _ Hrel) as (_&_&_&Hfp&_).
        assert (Hs : s = sb) by (apply b1_state_rel_eq; [exact Hrel|auto]). subst sb.
        apply wrap_ex_S. cbn [request_flush]. cbn [b1_request_flush] in H.
        rewrite bind_get in H. setoid_rewrite bind_get.
        destruct (live_queue s && bool_decide (g_batch_depth s = 0));
          [|unfold m_return in H; simplify_eq; exists 0%nat, sb'; split; [reflexivity|apply b1_state_rel_refl]].
        destruct (outside_bodies s); [by eapply (Bflush1 _ IH)|].
        rewrite Efl in H. unfold m_modify in H; simplify_eq.
        exists 0%nat, (with_flush_owed true s); split; [rewrite Efl; reflexivity|apply b1_state_rel_refl].
    + intros s sb sb' r Hrel H.
      destruct (g_flushing sb) eqn:Efl.
      * destruct (b1_rel_destruct _ _ Hrel) as (_&_&Hfl&_).
        destruct (b1_settle_flush_flushing _ _ _ _ Efl H) as [-> ->].
        exists 2%nat, s. split; [|exact Hrel].
        cbn [settle_flush]. rewrite bind_get.
        destruct (g_flush_owed s && bool_decide (g_batch_depth s = 0) && outside_bodies s); [|reflexivity].
        cbn [run_flush]. rewrite bind_get, Hfl, Efl. reflexivity.
      * destruct (b1_rel_destruct _ _ Hrel) as (_&_&_&Hfp&_).
        assert (Hs : s = sb) by (apply b1_state_rel_eq; [exact Hrel|auto]). subst sb.
        apply wrap_ex_S. cbn [settle_flush]. cbn [b1_settle_flush] in H.
        rewrite bind_get in H. setoid_rewrite bind_get.
        destruct (g_flush_owed s && bool_decide (g_batch_depth s = 0) && outside_bodies s);
          [by eapply (Bflush1 _ IH)|unfold m_return in H; simplify_eq; exists 0%nat, sb'; split; [reflexivity|apply b1_state_rel_refl]].
    + intros s sb sb' r Hrel H.
      destruct (g_flushing sb) eqn:Efl.
      * destruct (b1_rel_destruct _ _ Hrel) as (_&_&Hfl&_).
        cbn [b1_run_flush] in H. rewrite bind_get, Efl in H.
        unfold m_return in H. simplify_eq.
        exists 1%nat, s. split; [cbn [run_flush]; rewrite bind_get, Hfl, Efl; reflexivity|exact Hrel].
      * destruct (b1_rel_destruct _ _ Hrel) as (_&_&_&Hfp&_).
        assert (Hs : s = sb) by (apply b1_state_rel_eq; [exact Hrel|auto]). subst sb.
        cbn [b1_run_flush] in H. rewrite bind_get, Efl in H.
        unfold m_bind, m_modify in H.
        set (s1 := with_flush_owed false (with_flushing true s)) in H.
        destruct (b1_drain fb s1) as [[sb2 []]|] eqn:Ed; simpl in H; [|discriminate].
        simplify_eq.
        destruct (Bdrain1 _ IH _ _ _ _ (b1_state_rel_refl s1) Ed) as (f & s2 & Ebd & Hrel2).
        destruct (engine_depths_kept f) as [_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ Ddrain].
        destruct (b1_depths_kept fb) as [_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ B1Ddrain].
        pose proof (Ddrain _ _ _ Ebd) as [Hp2 _].
        pose proof (B1Ddrain _ _ _ Ed) as [Hpb2 _].
        assert (Hs2 : s2 = sb2) by (apply b1_state_rel_eq; [exact Hrel2|congruence]). subst sb2.
        exists (S f), (with_flushing false s2). split.
        -- cbn [run_flush]. unfold m_bind, m_get, m_modify. rewrite Efl. fold s1. rewrite Ebd. reflexivity.
        -- apply b1_state_rel_refl.
    + intros s sb sb' r Hrel H. apply wrap_ex_S. cbn [drain]. cbn [b1_drain] in H.
      rewrite bind_get in H. setoid_rewrite bind_get.
      destruct (b1_rel_fields _ _ Hrel) as (_&_&_&_&Hq&Hqh&_).
      destruct (g_queue sb !! g_queue_head sb) as [n|] eqn:Eq1,
               (g_queue s !! g_queue_head s) as [ns|] eqn:Eq2;
        try (exfalso; rewrite Hq, Hqh, Eq1 in Eq2; discriminate).
      * assert (ns = n) by (rewrite Hq, Hqh, Eq1 in Eq2; congruence); subst ns.
        eapply bind_inv in H as (sb1 & [] & E1 & H). unfold m_modify in E1; simplify_eq.
        eapply bind_inv in H as (sb2 & [] & E2 & E3).
        assert (Hrel1 : b1_state_rel (with_queue_head (S (g_queue_head s)) s) (with_queue_head (S (g_queue_head sb)) sb))
          by (rewrite Hqh; b1_rel_simple).
        destruct (Bexecute1 _ IH n _ _ _ _ Hrel1 E2) as (f1 & s2 & Eb2 & Hrel2).
        destruct (Bdrain1 _ IH _ _ _ _ Hrel2 E3) as (f2 & s3 & Eb3 & Hrel3).
        exists (Nat.max f1 f2), s3. split; [|exact Hrel3].
        unfold m_bind, m_modify.
        rewrite (Mexecute _ (engine_mono_all f1) (Nat.max f1 f2) _ _ _ (Nat.le_max_l f1 f2) Eb2).
        exact (Mdrain _ (engine_mono_all f2) (Nat.max f1 f2) _ _ (Nat.le_max_r f1 f2) Eb3).
      * exists 0%nat; eapply b1_pure_modify_bwd; [|exact Hrel|exact H]; upd_pull_tac.
Qed.
References

Uses

168 more
✓ No axioms

Including g_flushing in deferring (deferring_b1) preserves exact program outcomes, states, and traces, and makes pull identical to refresh when g_flushing = true, g_pull_depth = 0, and g_current = None.

Every engine primitive except refresh_or_pull is identical between Engine and Engine_B1, and whenever g_flushing = true, any pull leaves g_flushing = true, restores g_pull_depth, and skips settle_flush. Consequently whole-program execution from any non-flushing state (including init fl) and single-node pulls during a flush reach identical final states (hence identical g_trace).

Arguments

c : cmd
program command executed by run_top / b1_run_top.
s : State.state
starting state.
n : node_id
target node for pull / refresh.
s' : State.state
final state after execution.
r : outcome
command outcome (Ok or Fail).
d : State.node
read node snapshot returned by pull or get_node.
Theorem deferring_with_flushing_eq c s n s' r d :
  (g_flushing s = false ->
    ((exists f, run_top f c s = Some (s', r)) <-> (exists fb, b1_run_top fb c s = Some (s', r)))) /\
  (g_flushing s = true -> g_pull_depth s = 0 -> g_current s = None ->
    ((exists f, pull f n s = Some (s', d)) <->
     (exists fb, (let* _ := b1_refresh fb n in get_node n) s = Some (s', d))) /\
    ((exists f, pull f n s = Some (s', d)) <->
     (exists f', (let* _ := refresh f' n in get_node n) s = Some (s', d)))).

Arguments

c : cmd
program command executed by run_top / b1_run_top.
s : State.state
starting state.
n : node_id
target node for pull / refresh.
s' : State.state
final state after execution.
r : outcome
command outcome (Ok or Fail).
d : State.node
read node snapshot returned by pull or get_node.
Qed
Proof.
  split.
  - intros Hfl. split.
    + intros [f Hf].
      destruct (Fexec1 _ (b1_fwd_all f) [] c s s s' r (b1_state_rel_refl s) Hf) as (fb & sb' & Efb & Hrel).
      destruct (engine_depths_kept f) as [Dexec _].
      destruct (b1_depths_kept fb) as [B1Dexec _].
      pose proof (Dexec [] c s s' r Hf) as [Hp1 _].
      pose proof (B1Dexec [] c s sb' r Efb) as [Hp2 _].
      assert (s' = sb') by (apply b1_state_rel_eq; [exact Hrel|congruence]).
      subst sb'. exists fb. exact Efb.
    + intros [fb Hfb].
      destruct (Bexec1 _ (b1_bwd_all fb) [] c s s s' r (b1_state_rel_refl s) Hfb) as (f & s0' & Ef & Hrel).
      destruct (engine_depths_kept f) as [Dexec _].
      destruct (b1_depths_kept fb) as [B1Dexec _].
      pose proof (Dexec [] c s s0' r Ef) as [Hp1 _].
      pose proof (B1Dexec [] c s s' r Hfb) as [Hp2 _].
      assert (s0' = s') by (apply b1_state_rel_eq; [exact Hrel|congruence]).
      subst s0'. exists f. exact Ef.
  - intros Hfl Hpd Hcur.
    assert (Hmid : (exists f, pull f n s = Some (s', d)) <->
                   (exists fb, (let* _ := b1_refresh fb n in get_node n) s = Some (s', d))).
    { split.
      - intros [f Hf].
        assert (Hrop : refresh_or_pull (S f) n s = Some (s', d)).
        { cbn [refresh_or_pull]. rewrite bind_get. unfold deferring. rewrite Hcur, Hpd. simpl. exact Hf. }
        destruct (Frop1 _ (b1_fwd_all (S f)) n s s s' d (b1_state_rel_refl s) Hrop) as (fb & sb' & Efb & Hrel).
        destruct fb as [|fb']; [discriminate|].
        cbn [b1_refresh_or_pull] in Efb. rewrite bind_get in Efb.
        unfold deferring_b1 in Efb. rewrite Hfl in Efb. simpl in Efb.
        eapply bind_inv in Efb as (sb1 & [] & Eb1 & Eg). unfold get_node in Eg.
        destruct (g_nodes sb1 !! n) eqn:En; [|discriminate]. simplify_eq.
        destruct (engine_depths_kept f) as [_ _ _ _ _ Dpull _].
        pose proof (Dpull n s s' d Hf) as [Hp1 _].
        destruct (b1_depths_kept fb') as [_ _ _ _ _ _ B1Dref _].
        pose proof (B1Dref n s sb' tt Eb1) as [Hp2 _].
        assert (s' = sb') by (apply b1_state_rel_eq; [exact Hrel|congruence]).
        subst sb'. exists fb'. unfold m_bind. rewrite Eb1. simpl. unfold get_node.
        by rewrite En.
      - intros [fb Hfb].
        eapply bind_inv in Hfb as (sb1 & [] & Eb1 & Eg). unfold get_node in Eg.
        destruct (g_nodes sb1 !! n) eqn:En; [|discriminate]. simplify_eq.
        assert (Hbrop : b1_refresh_or_pull (S fb) n s = Some (s', d)).
        { cbn [b1_refresh_or_pull]. rewrite bind_get. unfold deferring_b1. rewrite Hfl. simpl.
          unfold m_bind. rewrite Eb1. simpl. unfold get_node. by rewrite En. }
        destruct (Brop1 _ (b1_bwd_all (S fb)) n s s s' d (b1_state_rel_refl s) Hbrop) as (f & s0' & Ef & Hrel).
        destruct f as [|f']; [discriminate|].
        cbn [refresh_or_pull] in Ef. rewrite bind_get in Ef.
        unfold deferring in Ef. rewrite Hcur, Hpd in Ef. simpl in Ef.
        destruct (engine_depths_kept f') as [_ _ _ _ _ Dpull _].
        pose proof (Dpull n s s0' d Ef) as [Hp1 _].
        destruct (b1_depths_kept fb) as [_ _ _ _ _ _ B1Dref _].
        pose proof (B1Dref n s s' tt Eb1) as [Hp2 _].
        assert (s0' = s') by (apply b1_state_rel_eq; [exact Hrel|congruence]).
        subst s0'. exists f'. exact Ef. }
    split; [exact Hmid|].
    rewrite Hmid. split.
    + intros [fb Hfb].
      eapply bind_inv in Hfb as (s1 & [] & Eb1 & Eg). unfold get_node in Eg.
      destruct (g_nodes s1 !! n) eqn:En; [|discriminate]. simplify_eq.
      destruct (Brefresh1 _ (b1_bwd_all fb) n s s s' tt (b1_state_rel_refl s) Eb1) as (f' & s0' & Ef & Hrel).
      destruct (engine_depths_kept f') as [_ _ _ _ _ _ Dref _].
      pose proof (Dref n s s0' tt Ef) as [Hp1 _].
      destruct (b1_depths_kept fb) as [_ _ _ _ _ _ B1Dref _].
      pose proof (B1Dref n s s' tt Eb1) as [Hp2 _].
      assert (s0' = s') by (apply b1_state_rel_eq; [exact Hrel|congruence]). subst s0'.
      exists f'. unfold m_bind. rewrite Ef. simpl. unfold get_node. by rewrite En.
    + intros [f' Hf'].
      eapply bind_inv in Hf' as (s1 & [] & Ef1 & Eg). unfold get_node in Eg.
      destruct (g_nodes s1 !! n) eqn:En; [|discriminate]. simplify_eq.
      destruct (Frefresh1 _ (b1_fwd_all f') n s s s' tt (b1_state_rel_refl s) Ef1) as (fb & sb' & Eb & Hrel).
      destruct (engine_depths_kept f') as [_ _ _ _ _ _ Dref _].
      pose proof (Dref n s s' tt Ef1) as [Hp1 _].
      destruct (b1_depths_kept fb) as [_ _ _ _ _ _ B1Dref _].
      pose proof (B1Dref n s sb' tt Eb) as [Hp2 _].
      assert (s' = sb') by (apply b1_state_rel_eq; [exact Hrel|congruence]). subst sb'.
      exists fb. unfold m_bind. rewrite Eb. simpl. unfold get_node. by rewrite En.
Qed.
References

B2. Disposed empty sources & Track slot-0 / slot-1 cursor-bump fast paths

Definition set_cursor (cur : nat) (self : node_id) (w : EdgeWorld) : EdgeWorld :=
  let l := sources_of w self in
  set_sources self {| source_slots := source_slots l; source_cursor := cur |} w.
References
Lemma edge_match_empty : edge_match empty_world.
Qed
Proof.
  intros c i s h. unfold slot_at, sources_of, observers_of, empty_world. simpl.
  rewrite !lookup_empty. simpl. rewrite !lookup_nil. split; discriminate.
Qed.
References
Lemma edge_match_begin c w :
  edge_match w -> edge_match (sources_begin_run c w).
Qed
Proof.
  intros Hm c' i s h. unfold sources_begin_run.
  rewrite slot_at_set_sources, observers_of_set_sources.
  destruct (decide (c' = c)) as [->|]; apply Hm.
Qed.
References
Lemma slot_at_none_nil w c :
  (forall i, fst <$> slot_at w c i = None) -> source_slots (sources_of w c) = [].
Qed
Proof.
  intros H. destruct (source_slots (sources_of w c)) as [|p ps] eqn:Es; [reflexivity|].
  specialize (H 0%nat). unfold slot_at in H. rewrite Es in H. discriminate.
Qed.
References
Lemma edge_match_add c src w w' :
  edge_match w -> sources_add c src w = Some w' -> edge_match w'.
Qed
Proof.
  intros Hm Hadd. unfold sources_add in Hadd.
  destruct (decide _).
  - simplify_eq. intros c' i s h.
    rewrite slot_at_set_sources, observers_of_set_sources.
    destruct (decide (c' = c)) as [->|]; apply Hm.
  - destruct (decide _); [by simplify_eq|].
    destruct (sources_end_run c w) as [w1|] eqn:Eend; [|discriminate].
    simpl in Hadd. simplify_eq.
    destruct (sources_end_run_ok c w Hm) as (w1' & E1 & Hm1 & _).
    rewrite Eend in E1. injection E1 as <-.
    by apply edge_match_link.
Qed.
References

Used by

Lemma sources_add_keeps_empty c src w w' c' :
  edge_match w -> c' <> c ->
  source_slots (sources_of w c') = [] ->
  sources_add c src w = Some w' ->
  source_slots (sources_of w' c') = [].
Qed
Proof.
  intros Hm Hne Hempty Hadd. unfold sources_add in Hadd.
  destruct (decide _).
  - simplify_eq. rewrite sources_of_set_sources, decide_False by exact Hne. exact Hempty.
  - destruct (decide _); [simplify_eq; exact Hempty|].
    destruct (sources_end_run c w) as [w1|] eqn:Eend; [|discriminate].
    simpl in Hadd. simplify_eq.
    destruct (sources_end_run_ok c w Hm) as (w1' & E1 & _ & (_&_&Hother&_)).
    rewrite Eend in E1. injection E1 as <-.
    assert (H1empty : source_slots (sources_of w1 c') = []).
    { apply slot_at_none_nil. intros i. destruct (Hother c' i Hne) as [-> _].
      unfold slot_at. rewrite Hempty, lookup_nil. reflexivity. }
    unfold sources_link, sources_append.
    repeat match goal with
    | |- context [if decide _ then _ else _] => destruct (decide _)
    | |- context [match observers_try_add _ _ _ with _ => _ end] => destruct (observers_try_add _ _ _) as [? []]
    end; rewrite sources_of_set_observers, sources_of_set_sources, decide_False by exact Hne; exact H1empty.
Qed.
References

Used by

Lemma sources_end_run_keeps_empty n w w' c :
  edge_match w ->
  source_slots (sources_of w c) = [] ->
  sources_end_run n w = Some w' ->
  source_slots (sources_of w' c) = [].
Qed
Proof.
  intros Hm Hempty Hend.
  destruct (sources_end_run_ok n w Hm) as (w1 & E1 & _ & (_&Hnone&Hother&_)).
  rewrite Hend in E1. injection E1 as <-.
  destruct (decide (c = n)) as [->|Hne].
  - unfold sources_end_run in Hend.
    destruct (decide (source_cursor (sources_of w n) < length (source_slots (sources_of w n)))) as [Hl|Hl].
    + rewrite Hempty in Hl. simpl in Hl. lia.
    + by simplify_eq.
  - apply slot_at_none_nil. intros i. destruct (Hother c i Hne) as [-> _].
    unfold slot_at. rewrite Hempty, lookup_nil. reflexivity.
Qed.
References
Lemma sources_clear_keeps_empty n w w' c :
  edge_match w ->
  (c = n \/ source_slots (sources_of w c) = []) ->
  sources_clear n w = Some w' ->
  source_slots (sources_of w' c) = [].
Qed
Proof.
  intros Hm Hc Hclr.
  destruct (sources_clear_ok n w Hm) as (w1 & E1 & _ & Hnempty & _ & Hother & _).
  rewrite Hclr in E1. injection E1 as <-.
  destruct (decide (c = n)) as [->|Hne]; [exact Hnempty|].
  destruct Hc as [Heq|Hempty]; [contradiction|].
  apply slot_at_none_nil. intros i. destruct (Hother c i Hne) as [-> _].
  unfold slot_at. rewrite Hempty, lookup_nil. reflexivity.
Qed.
References
Definition b2_inv (s : state) : Prop :=
  edge_match (g_edges s) /\
  forall c d, g_nodes s !! c = Some d -> n_disposed d = true ->
    source_slots (sources_of (g_edges s) c) = [].
References
Lemma b2_inv_init fl : b2_inv (init fl).
Qed
Proof.
  split; [exact edge_match_empty|].
  intros c d Hd. unfold init in Hd. simpl in Hd. by rewrite lookup_empty in Hd.
Qed.
References
Lemma mark_prim_b2_inv s s' :
  mark_prim s s' -> b2_inv s -> b2_inv s'.
Qed
Proof.
  intros Hp [Hm Hdisp].
  assert (He : g_edges s' = g_edges s) by (by apply prim_edges).
  split; [by rewrite He|].
  intros c d Hd Hdz. rewrite He.
  destruct Hp as [s0 n d0 f u Hnd Hk Hf Hu|s0 n d0 f Hnd Hk Hq Hf|s0 n d0 f Hnd Hk Hq Hf]; simpl in *.
  all: destruct (decide (c = n)) as [->|Hne];
       [rewrite lookup_insert_eq in Hd; simplify_eq; eapply Hdisp; [exact Hnd|exact Hdz]
       |rewrite lookup_insert_ne in Hd by congruence; eapply Hdisp; [exact Hd|exact Hdz]].
Qed.
References

Uses

Lemma marks_b2_inv s s' :
  rtc mark_prim s s' -> b2_inv s -> b2_inv s'.
Qed
Proof. induction 1; [exact id|intros Hb; apply IHrtc; by eapply mark_prim_b2_inv]. Qed.
References
Lemma b2_inv_any_mark f n r s s' :
  any_mark f n r s s' -> b2_inv s -> b2_inv s'.
Qed
Proof. intros H. eapply marks_b2_inv, (any_mark_rtc f n r _ _ H). Qed.
References
Lemma b2_bind_of {A B : Type} {m : M A} {k : A -> M B} {s s' : state} {b : B}
    (Hb : m_bind m k s = Some (s', b)) :
  (forall s1 a, b2_inv s -> m s = Some (s1, a) -> b2_inv s1) ->
  (forall a s1, m s = Some (s1, a) -> b2_inv s1 -> k a s1 = Some (s', b) -> b2_inv s') ->
  b2_inv s -> b2_inv s'.
Qed
Proof.
  intros H1 H2 Hs. unfold m_bind in Hb. destruct (m s) as [[s1 a]|] eqn:Em; [|discriminate].
  eapply (H2 a s1 eq_refl); [by eapply H1|exact Hb].
Qed.
References

Uses

Lemma b2_m_return {A} (a : A) s s' r :
  m_return a s = Some (s', r) -> b2_inv s -> b2_inv s'.
Qed
Proof. intros H Hs. unfold m_return in H. by simplify_eq. Qed.
References
Lemma b2_m_modify upd s s' r :
  m_modify upd s = Some (s', r) -> b2_inv s ->
  (forall st, g_nodes (upd st) = g_nodes st /\ g_edges (upd st) = g_edges st) ->
  b2_inv s'.
Qed
Proof.
  intros H [Hm Hd] Hupd. unfold m_modify in H. simplify_eq.
  destruct (Hupd s) as [Hn He]. split; [by rewrite He|].
  intros c d Hc Hdz. rewrite He. eapply Hd; [rewrite <- Hn; exact Hc|exact Hdz].
Qed.
References
Lemma b2_get_node n s s' d :
  get_node n s = Some (s', d) -> b2_inv s -> b2_inv s'.
Qed
Proof. intros H Hs. unfold get_node in H. destruct (g_nodes s !! n); simplify_eq; exact Hs. Qed.
References
Lemma b2_get_owner o s s' x :
  get_owner o s = Some (s', x) -> b2_inv s -> b2_inv s'.
Qed
Proof. intros H Hs. unfold get_owner in H. destruct (g_owners s !! o); simplify_eq; exact Hs. Qed.
References
Lemma b2_modify_node n u s s' r :
  modify_node n u s = Some (s', r) -> b2_inv s ->
  (forall x, n_disposed (u x) = n_disposed x) ->
  b2_inv s'.
Qed
Proof.
  intros H [Hm Hd] Hu. unfold modify_node in H.
  destruct (g_nodes s !! n) as [d0|] eqn:En; [|discriminate].
  simplify_eq. split; [exact Hm|].
  intros c d Hc Hdz. simpl in *.
  destruct (decide (c = n)) as [->|Hne].
  - rewrite lookup_insert_eq in Hc. simplify_eq. rewrite Hu in Hdz. eapply Hd; [exact En|exact Hdz].
  - rewrite lookup_insert_ne in Hc by congruence. eapply Hd; [exact Hc|exact Hdz].
Qed.
References

Uses

Lemma b2_add_node n d s s' r :
  add_node n d s = Some (s', r) -> b2_inv s -> n_disposed d = false -> b2_inv s'.
Qed
Proof.
  intros H [Hm Hd] Hnd. unfold add_node, m_modify in H. simplify_eq.
  unfold ghost_publish, set_node. simpl.
  destruct (<[n := d]> (g_nodes s) !! n) eqn:E;
    (split; simpl; [exact Hm|];
     intros c d0 Hc Hdz;
     destruct (decide (c = n)) as [->|Hne];
     [rewrite lookup_insert_eq in Hc; simplify_eq; congruence
     |rewrite lookup_insert_ne in Hc by congruence; eapply Hd; [exact Hc|exact Hdz]]).
Qed.
References

Uses

4 more
  • false
  • decide
  • lookup_insert_eq
  • lookup_insert_ne
Lemma b2_current_owner s s' r :
  current_owner s = Some (s', r) -> b2_inv s -> b2_inv s'.
Qed
Proof.
  intros H [Hm Hd]. unfold current_owner in H.
  destruct (g_current_owner s), (g_current s); simplify_eq; try (split; assumption).
  destruct (g_nodes s !! n) as [d0|] eqn:En; [|discriminate].
  destruct (n_scope d0); simplify_eq; try (split; assumption).
  destruct (match n_kind d0, n_mode d0 with KMemo, Pure => true | _, _ => false end); simplify_eq;
    (split; simpl; [exact Hm|];
     intros c d Hc Hdz; destruct (decide (c = n)) as [->|Hne];
     [rewrite lookup_insert_eq in Hc; simplify_eq; eapply Hd; [exact En|exact Hdz]
     |rewrite lookup_insert_ne in Hc by congruence; eapply Hd; [exact Hc|exact Hdz]]).
Qed.
References

Uses

8 more
Lemma b2_track n s s' r :
  track n s = Some (s', r) -> b2_inv s -> b2_inv s'.
Qed
Proof.
  intros H [Hm Hd]. unfold track, live_current in H.
  destruct (g_current s) as [c0|]; [|simplify_eq; split; assumption].
  destruct (g_nodes s !! c0) as [dc|] eqn:Edc; [|simplify_eq; split; assumption].
  destruct (n_disposed dc) eqn:Edcz; [simplify_eq; split; assumption|].
  destruct (sources_add c0 n (g_edges s)) as [w'|] eqn:Eadd; [|discriminate].
  simplify_eq. unfold ghost_read; simpl.
  destruct (g_nodes s !! n) as [dn|]; simpl;
    (split; [by eapply edge_match_add|];
     intros c d Hc Hdz; simpl in Hc;
     destruct (decide (c = c0)) as [->|Hne];
     [rewrite Edc in Hc; simplify_eq; congruence
     |eapply sources_add_keeps_empty; [exact Hm|exact Hne|eapply Hd; [exact Hc|exact Hdz]|exact Eadd]]).
Qed.
References
Lemma b2_mark_reader f s s' r :
  mark_reader f s = Some (s', r) -> b2_inv s -> b2_inv s'.
Qed
Proof.
  intros H Hb. unfold mark_reader in H.
  destruct (g_current s), (f_reader_mark (g_flags s)); simplify_eq; try exact Hb.
  destruct (mark_check f _ s) eqn:Em; simplify_eq.
  eapply (b2_inv_any_mark _ _ None); [left; exact Em|exact Hb].
Qed.
References
Lemma b2_announce n s s' r :
  announce n s = Some (s', r) -> b2_inv s -> b2_inv s'.
Qed
Proof.
  intros H Hb. unfold announce in H. destruct (f_unannounced (g_flags s)); [|by simplify_eq].
  eapply b2_modify_node; [exact H|exact Hb|done].
Qed.
References
Lemma b2_record_error o y s s' r :
  record_error o y s = Some (s', r) -> b2_inv s -> b2_inv s'.
Qed
Proof.
  intros H Hb. unfold record_error in H. eapply (b2_bind_of H); [intros ?? Hs Hg; by eapply b2_get_owner| |exact Hb].
  intros x s1 _ Hs1 H1. destruct (o_sink x).
  - eapply (b2_bind_of H1); [intros ?? Hs Hg; by eapply b2_get_owner| |exact Hs1].
    intros y0 s2 _ Hs2 H2. eapply b2_m_modify; [exact H2|exact Hs2|]. intro st; split; reflexivity.
  - eapply b2_m_modify; [exact H1|exact Hs1|]. intro st; split; reflexivity.
Qed.
References
Lemma b2_begin_run n s s' r :
  edges (sources_begin_run n) s = Some (s', r) -> b2_inv s -> b2_inv s'.
Qed
Proof.
  intros H [Hm Hd]. unfold edges, m_modify in H. simplify_eq.
  split; simpl; [by apply edge_match_begin|].
  intros c d Hc Hdz. unfold sources_begin_run. rewrite sources_of_set_sources.
  destruct (decide (c = n)) as [->|]; eapply Hd; [exact Hc|exact Hdz|exact Hc|exact Hdz].
Qed.
References
Lemma b2_end_or_clear n (b : bool) s s' r :
  edges_opt (if b then sources_clear n else sources_end_run n) s = Some (s', r) -> b2_inv s -> b2_inv s'.
Qed
Proof.
  intros H [Hm Hd]. unfold edges_opt in H.
  destruct b.
  - destruct (sources_clear n (g_edges s)) as [w'|] eqn:E; [|discriminate].
    simplify_eq. destruct (sources_clear_ok n _ Hm) as (w1 & E1 & Hm1 & Hempty & _ & _).
    rewrite E in E1. injection E1 as <-. split; [exact Hm1|].
    intros c d Hc Hdz. simpl. destruct (decide (c = n)) as [->|Hne]; [exact Hempty|].
    eapply sources_clear_keeps_empty; [exact Hm|right; eapply Hd; [exact Hc|exact Hdz]|exact E].
  - destruct (sources_end_run n (g_edges s)) as [w'|] eqn:E; [|discriminate].
    simplify_eq. destruct (sources_end_run_ok n _ Hm) as (w1 & E1 & Hm1 & _).
    rewrite E in E1. injection E1 as <-. split; [exact Hm1|].
    intros c d Hc Hdz. simpl. eapply sources_end_run_keeps_empty; [exact Hm|eapply Hd; [exact Hc|exact Hdz]|exact E].
Qed.
References
Lemma b2_hosted {A} n (b : M A) s s' r :
  hosted n b s = Some (s', r) ->
  (forall s0 s1 a, b s0 = Some (s1, a) -> b2_inv s0 -> b2_inv s1) ->
  b2_inv s -> b2_inv s'.
Qed
Proof.
  intros H Hb Hs. unfold hosted in H.
  eapply (b2_bind_of H); [intros ?? Hs0 Hm; eapply b2_m_modify; [exact Hm|exact Hs0|]; intro st; split; reflexivity| |exact Hs].
  intros [] s1 _ Hs1 Hk1.
  eapply (b2_bind_of Hk1); [intros ?? Hs0 Hm; by eapply Hb| |exact Hs1].
  intros a s2 _ Hs2 Hk2.
  eapply (b2_bind_of Hk2); [intros ?? Hs0 Hm; eapply b2_m_modify; [exact Hm|exact Hs0|]; intro st; split; reflexivity| |exact Hs2].
  intros [] s3 _ Hs3 Hm. by eapply b2_m_return.
Qed.
References

Used by

Lemma b2_untracked {A} (b : M A) s s' r :
  untracked b s = Some (s', r) ->
  (forall s0 s1 a, b s0 = Some (s1, a) -> b2_inv s0 -> b2_inv s1) ->
  b2_inv s -> b2_inv s'.
Qed
Proof.
  intros H Hb Hs. unfold untracked in H.
  eapply (b2_bind_of H); [intros ?? Hs0 Hm; eapply b2_m_modify; [exact Hm|exact Hs0|]; intro st; split; reflexivity| |exact Hs].
  intros [] s1 _ Hs1 Hk1.
  eapply (b2_bind_of Hk1); [intros ?? Hs0 Hm; by eapply Hb| |exact Hs1].
  intros a s2 _ Hs2 Hk2.
  eapply (b2_bind_of Hk2); [intros ?? Hs0 Hm; eapply b2_m_modify; [exact Hm|exact Hs0|]; intro st; split; reflexivity| |exact Hs2].
  intros [] s3 _ Hs3 Hm. by eapply b2_m_return.
Qed.
References

Used by

Lemma b2_detached o b s s' r :
  detached o b s = Some (s', r) ->
  (forall s0 s1 a, b s0 = Some (s1, a) -> b2_inv s0 -> b2_inv s1) ->
  b2_inv s -> b2_inv s'.
Qed
Proof.
  intros H Hb Hs. unfold detached in H.
  eapply (b2_bind_of H); [intros ?? Hs0 Hm; eapply b2_m_modify; [exact Hm|exact Hs0|]; intro st; split; reflexivity| |exact Hs].
  intros [] s1 _ Hs1 Hk1.
  eapply (b2_bind_of Hk1); [intros ?? Hs0 Hm; by eapply Hb| |exact Hs1].
  intros [] s2 _ Hs2 Hm. eapply b2_m_modify; [exact Hm|exact Hs2|]. intro st; split; reflexivity.
Qed.
References

Used by

Lemma b2_dispose_prefix n d s s1 s2 s3 :
  b2_inv s ->
  modify_node n (fun x => node_with_link None (node_with_disposed true x)) s = Some (s1, tt) ->
  match n_link d with Some l => unlink l | None => m_return tt end s1 = Some (s2, tt) ->
  edges_opt (sources_clear n) s2 = Some (s3, tt) ->
  b2_inv s3.
Qed
Proof.
  intros [Hm Hd] E1 E2 E3.
  unfold modify_node in E1. destruct (g_nodes s !! n) as [d0|] eqn:En; [|discriminate]. simplify_eq.
  assert (Hn2 : g_nodes s2 = <[n := node_with_link None (node_with_disposed true d0)]> (g_nodes s))
    by (destruct (n_link d); [unfold unlink in E2; eapply bind_inv in E2 as (sx & x & Ex & Eu); destruct (decide _); unfold get_owner, put_owner, m_modify, m_return in *; destruct (g_owners _ !! _); simplify_eq; reflexivity|unfold m_return in E2; by simplify_eq]).
  assert (He2 : g_edges s2 = g_edges s)
    by (destruct (n_link d); [unfold unlink in E2; eapply bind_inv in E2 as (sx & x & Ex & Eu); destruct (decide _); unfold get_owner, put_owner, m_modify, m_return in *; destruct (g_owners _ !! _); simplify_eq; reflexivity|unfold m_return in E2; by simplify_eq]).
  unfold edges_opt in E3. destruct (sources_clear n (g_edges s2)) as [w'|] eqn:Eclr; [|discriminate].
  simplify_eq. rewrite He2 in Eclr.
  destruct (sources_clear_ok n _ Hm) as (w1 & Eclr1 & Hm1 & Hempty & _).
  rewrite Eclr in Eclr1. injection Eclr1 as <-.
  split; [exact Hm1|].
  intros c dc Hc Hdz. simpl in *. rewrite Hn2 in Hc.
  destruct (decide (c = n)) as [->|Hne]; [exact Hempty|].
  rewrite lookup_insert_ne in Hc by congruence.
  eapply sources_clear_keeps_empty; [exact Hm|right; eapply Hd; [exact Hc|exact Hdz]|exact Eclr].
Qed.
References

Used by

Lemma b2_next_id s s' r :
  next_id s = Some (s', r) -> b2_inv s -> b2_inv s'.
Qed
Proof. intros H Hb. unfold next_id in H. by simplify_eq. Qed.
References
Lemma b2_note_host_read n o s s' r :
  note_host_read n o s = Some (s', r) -> b2_inv s -> b2_inv s'.
Qed
Proof.
  intros H Hb. unfold note_host_read in H.
  destruct (g_current s), (g_current_owner s); simplify_eq; try exact Hb.
  destruct (decide _); [unfold emit, m_modify in H; by simplify_eq|by simplify_eq].
Qed.
References
Lemma b2_log_label s s' r :
  log_label s = Some (s', r) -> b2_inv s -> b2_inv s'.
Qed
Proof.
  intros H Hb. unfold log_label in H.
  destruct (g_current s), (g_current_owner s); by simplify_eq.
Qed.
References
Local Ltac b2_pure_tac :=
  try unfold add_node, put_owner, emit, edges, push_running, pop_running,
    push_checking, pop_checking, m_step in *;
  first
    [ by eapply b2_m_return
    | by eapply b2_get_node
    | by eapply b2_get_owner
    | by eapply b2_current_owner
    | by eapply b2_next_id
    | by eapply b2_note_host_read
    | by eapply b2_log_label
    | by eapply b2_track
    | by eapply b2_mark_reader
    | by eapply b2_announce
    | by eapply b2_unlink
    | by eapply b2_record_error
    | by eapply b2_begin_run
    | by eapply b2_end_or_clear
    | by (eapply b2_add_node; [eassumption|eassumption|reflexivity])
    | by (eapply b2_modify_node; [eassumption|eassumption|intro; reflexivity])
    | by (eapply b2_m_modify; [eassumption|eassumption|let st := fresh "st" in intro st; unfold ghost_publish, ghost_run_end, ghost_run_start, ghost_effect_start, ghost_dirty_cleared, ghost_resolved, ghost_registered, run_end_unnoticed, slot_holds, add_if, set_node; destruct st; simpl; repeat match goal with | |- context [match ?m !! ?k with _ => _ end] => destruct (m !! k) end; split; reflexivity])
    ].
References
Record b2_inv_at (f : nat) : Prop := {
  B2exec : forall e c s s' r, exec f e c s = Some (s', r) -> b2_inv s -> b2_inv s';
  B2read : forall n b s s' r, read f n b s = Some (s', r) -> b2_inv s -> b2_inv s';
  B2read_node : forall n s s' r, read_node f n s = Some (s', r) -> b2_inv s -> b2_inv s';
  B2update : forall n s s' r, update_if_necessary f n s = Some (s', r) -> b2_inv s -> b2_inv s';
  B2rop : forall n s s' r, refresh_or_pull f n s = Some (s', r) -> b2_inv s -> b2_inv s';
  B2pull : forall n s s' r, pull f n s = Some (s', r) -> b2_inv s -> b2_inv s';
  B2refresh : forall n s s' r, refresh f n s = Some (s', r) -> b2_inv s -> b2_inv s';
  B2walk : forall n i s s' r, resolve_walk f n i s = Some (s', r) -> b2_inv s -> b2_inv s';
  B2recompute : forall n s s' r, recompute f n s = Some (s', r) -> b2_inv s -> b2_inv s';
  B2run_memo : forall n s s' r, run_memo f n s = Some (s', r) -> b2_inv s -> b2_inv s';
  B2execute : forall n s s' r, execute_effect f n s = Some (s', r) -> b2_inv s -> b2_inv s';
  B2run_effect : forall n s s' r, run_effect f n s = Some (s', r) -> b2_inv s -> b2_inv s';
  B2body : forall n s s' r, run_effect_body f n s = Some (s', r) -> b2_inv s -> b2_inv s';
  B2discharge : forall o s s' r, discharge f o s = Some (s', r) -> b2_inv s -> b2_inv s';
  B2retire : forall o s s' r, retire f o s = Some (s', r) -> b2_inv s -> b2_inv s';
  B2dispose_scope : forall o s s' r, dispose_scope f o s = Some (s', r) -> b2_inv s -> b2_inv s';
  B2teardown : forall o ls cs s s' r, teardown f o ls cs s = Some (s', r) -> b2_inv s -> b2_inv s';
  B2cleanup : forall o b e s s' r, run_cleanup f o b e s = Some (s', r) -> b2_inv s -> b2_inv s';
  B2release : forall n s s' r, release f n s = Some (s', r) -> b2_inv s -> b2_inv s';
  B2dispose : forall n s s' r, dispose_node f n s = Some (s', r) -> b2_inv s -> b2_inv s';
  B2attach : forall n o s s' r, attach f n o s = Some (s', r) -> b2_inv s -> b2_inv s';
  B2write : forall n v s s' r, write_signal f n v s = Some (s', r) -> b2_inv s -> b2_inv s';
  B2request : forall s s' r, request_flush f s = Some (s', r) -> b2_inv s -> b2_inv s';
  B2settle : forall s s' r, settle_flush f s = Some (s', r) -> b2_inv s -> b2_inv s';
  B2flush : forall s s' r, run_flush f s = Some (s', r) -> b2_inv s -> b2_inv s';
  B2drain : forall s s' r, drain f s = Some (s', r) -> b2_inv s -> b2_inv s'
}.
References
Lemma b2_inv_all f : b2_inv_at f.
Qed
Proof.
  induction f as [|f IH].
  - split; intros; discriminate.
  - split.
    + intros e c s s' r H Hs. cbn [exec] in H.
      destruct c; repeat match goal with
      | H0 : context [on_node] |- _ => unfold on_node in H0
      | H0 : context [on_value] |- _ => unfold on_value in H0
      | H0 : context [match eval_expr ?e ?p ?x with _ => _ end] |- _ => destruct (eval_expr e p x) as [[|]|]; try discriminate
      | H0 : (if decide _ then _ else stuck) _ = Some _ |- _ => destruct (decide _); try discriminate
      end; try (eapply b2_m_return; [exact H|exact Hs]); try (simplify_eq; exact Hs).
      * by eapply (B2exec _ IH).
      * destruct (decide _); by eapply (B2exec _ IH).
      * eapply (b2_bind_of H); [intros ????; by eapply (B2read _ IH)|intros o s1 _ Hs1 Hk|exact Hs].
        eapply (b2_bind_of Hk); [intros ????; b2_pure_tac|intros [] s2 _ Hs2 Hk2|exact Hs1].
        destruct o; [by eapply (B2exec _ IH)|by eapply b2_m_return].
      * eapply (b2_bind_of H); [intros ????; by eapply (B2read _ IH)|intros o s1 _ Hs1 Hk|exact Hs].
        eapply (b2_bind_of Hk); [intros ????; b2_pure_tac|intros [] s2 _ Hs2 Hk2|exact Hs1].
        destruct o; by eapply (B2exec _ IH).
      * eapply (b2_bind_of H); [intros ????; by eapply (B2read _ IH)|intros o s1 _ Hs1 Hk|exact Hs].
        eapply (b2_bind_of Hk); [intros ????; b2_pure_tac|intros [] s2 _ Hs2 Hk2|exact Hs1].
        destruct o; [by eapply (B2exec _ IH)|by eapply b2_m_return].
      * eapply (b2_bind_of H); [intros ????; by eapply (B2write _ IH)|intros [] s1 _ Hs1 Hk; by eapply (B2exec _ IH)|exact Hs].
      * eapply (b2_bind_of H); [intros ????; b2_pure_tac|intros ?? _ Hs1 Hk|exact Hs].
        eapply (b2_bind_of Hk); [intros ????; b2_pure_tac|intros [] ? _ Hs2 Hk2; by eapply (B2exec _ IH)|exact Hs1].
      * eapply (b2_bind_of H); [intros ????; b2_pure_tac|intros ?? _ Hs1 Hk|exact Hs].
        eapply (b2_bind_of Hk); [intros ????; b2_pure_tac|intros [] ? _ Hs2 Hk2|exact Hs1].
        eapply (b2_bind_of Hk2); [intros ????; b2_pure_tac|intros [o|] ? _ Hs3 Hk3; [|by eapply b2_m_return]|exact Hs2].
        eapply (b2_bind_of Hk3); [intros ????; by eapply (B2attach _ IH)|intros [] ? _ Hs4 Hk4; by eapply (B2exec _ IH)|exact Hs3].
      * eapply (b2_bind_of H); [intros ????; b2_pure_tac|intros ?? _ Hs1 Hk|exact Hs].
        eapply (b2_bind_of Hk); [intros ????; b2_pure_tac|intros [] ? _ Hs2 Hk2|exact Hs1].
        eapply (b2_bind_of Hk2); [intros ????; b2_pure_tac|intros [o|] ? _ Hs3 Hk3; [|by eapply b2_m_return]|exact Hs2].
        eapply (b2_bind_of Hk3); [intros ????; by eapply (B2attach _ IH)|intros [] ? _ Hs4 Hk4|exact Hs3].
        eapply (b2_bind_of Hk4); [intros ?? Hs_ Hm_; unfold m_step in Hm_; destruct (mark_dirty f _ _) eqn:Em; simplify_eq; eapply (b2_inv_any_mark _ _ None); [right; left; exact Em|exact Hs_]|intros [] ? _ Hs5 Hk5|exact Hs4].
        eapply (b2_bind_of Hk5); [intros ????; by eapply (B2request _ IH)|intros [] ? _ Hs6 Hk6; by eapply (B2exec _ IH)|exact Hs5].
      * eapply (b2_bind_of H); [intros ????; b2_pure_tac|intros [o|] ? _ Hs1 Hk; [|by eapply b2_m_return]|exact Hs].
        eapply (b2_bind_of Hk); [intros ????; b2_pure_tac|intros x ? _ Hs2 Hk2|exact Hs1].
        eapply (b2_bind_of Hk2); [intros ????; b2_pure_tac|intros [] ? _ Hs3 Hk3|exact Hs2].
        destruct (o_disposed x).
        -- eapply (b2_bind_of Hk3); [|intros [] ? _ Hs4 Hk4; by eapply (B2exec _ IH)|exact Hs3].
           intros s4 [] _ Hm. eapply (b2_bind_of Hm); [|intros [] ? _ Hs5 Hreq; by eapply (B2request _ IH)|exact Hs3].
           intros s5 [] _ Hd. by eapply b2_detached; [exact Hd|intros ?????; by eapply (B2cleanup _ IH)|exact Hs3].
        -- eapply (b2_bind_of Hk3); [intros ????; b2_pure_tac|intros [] ? _ Hs4 Hk4; by eapply (B2exec _ IH)|exact Hs3].
      * eapply (b2_bind_of H); [intros ????; b2_pure_tac|intros d ? _ Hs1 Hk|exact Hs].
        destruct (n_kind d); [discriminate| |];
          (eapply (b2_bind_of Hk); [intros ????; by eapply (B2dispose _ IH)|intros [] ? _ Hs2 Hk2|exact Hs1];
           eapply (b2_bind_of Hk2); [intros ????; destruct (n_disposed d); b2_pure_tac|intros [] ? _ Hs3 Hk3; by eapply (B2exec _ IH)|exact Hs2]).
      * eapply (b2_bind_of H); [intros ????; b2_pure_tac|intros [] ? _ Hs1 Hk|exact Hs].
        eapply (b2_bind_of Hk); [intros ????; by eapply (B2exec _ IH)|intros r0 ? _ Hs2 Hk2|exact Hs1].
        eapply (b2_bind_of Hk2); [intros ????; b2_pure_tac|intros [] ? _ Hs3 Hk3|exact Hs2].
        destruct r0 as [|]; [|by eapply b2_m_return].
        eapply (b2_bind_of Hk3); [intros ????; by eapply (B2request _ IH)|intros [] ? _ Hs4 Hk4; by eapply (B2exec _ IH)|exact Hs3].
      * eapply (b2_bind_of H); [intros ????; b2_pure_tac|intros ?? _ Hs1 Hk|exact Hs].
        eapply (b2_bind_of Hk); [intros ????; b2_pure_tac|intros [] ? _ Hs2 Hk2; by eapply (B2exec _ IH)|exact Hs1].
    + intros n b s s' r H Hs. cbn [read] in H.
      destruct b; [by eapply (B2read_node _ IH)|eapply b2_untracked; [exact H|intros ?????; by eapply (B2read_node _ IH)|exact Hs]].
    + intros n s s' r H Hs. cbn [read_node] in H.
      eapply (b2_bind_of H); [intros ????; b2_pure_tac|intros d ? _ Hs1 Hk|exact Hs].
      destruct (n_kind d); [| |discriminate].
      * eapply (b2_bind_of Hk); [intros ????; b2_pure_tac|intros [] ? _ Hs2 Hk2; by eapply b2_m_return|exact Hs1].
      * eapply (b2_bind_of Hk); [|intros d' ? _ Hs2 Hk2; destruct (n_status d'); by eapply b2_m_return|exact Hs1].
        intros s2 d' _ Hm. destruct (stale_live d).
        -- eapply (b2_bind_of Hm); [intros ????; by eapply (B2rop _ IH)|intros d0 ? _ Hs2 Hk2|exact Hs1].
           eapply (b2_bind_of Hk2); [|intros [] ? _ Hs3 Hk3; by eapply b2_m_return|exact Hs2].
           intros s3 [] _ Hm2.
           eapply (b2_bind_of Hm2); [intros ????; b2_pure_tac|intros [] ? _ Hs3 Hk3|exact Hs2].
           eapply (b2_bind_of Hk3); [intros ????; b2_pure_tac|intros d'' ? _ Hs4 Hk4|exact Hs3].
           destruct (stale_live d''); [b2_pure_tac|by eapply b2_m_return].
        -- eapply (b2_bind_of Hm); [intros ????; b2_pure_tac|intros [] ? _ Hs2 Hk2; b2_pure_tac|exact Hs1].
    + intros n s s' r H Hs. cbn [update_if_necessary] in H.
      eapply (b2_bind_of H); [intros ????; b2_pure_tac|intros d ? _ Hs1 Hk|exact Hs].
      destruct (n_kind d); [by eapply b2_m_return| |by eapply b2_m_return].
      destruct (stale_live d); [|by eapply b2_m_return].
      eapply (b2_bind_of Hk); [intros ????; by eapply (B2rop _ IH)|intros ?? _ Hs2 Hk2; by eapply b2_m_return|exact Hs1].
    + intros n s s' r H Hs. cbn [refresh_or_pull] in H. rewrite bind_get in H.
      destruct (deferring s); [|by eapply (B2pull _ IH)].
      eapply (b2_bind_of H); [intros ????; by eapply (B2refresh _ IH)|intros [] ? _ Hs1 Hk; b2_pure_tac|exact Hs].
    + intros n s s' r H Hs. cbn [pull] in H.
      eapply (b2_bind_of H); [intros ????; b2_pure_tac|intros [] ? _ Hs1 Hk|exact Hs].
      eapply (b2_bind_of Hk); [intros ????; by eapply (B2refresh _ IH)|intros [] ? _ Hs2 Hk2|exact Hs1].
      eapply (b2_bind_of Hk2); [intros ????; b2_pure_tac|intros d ? _ Hs3 Hk3|exact Hs2].
      eapply (b2_bind_of Hk3); [intros ????; b2_pure_tac|intros [] ? _ Hs4 Hk4|exact Hs3].
      rewrite bind_get in Hk4.
      eapply (b2_bind_of Hk4); [intros s5 ? Hs4' Hm; destruct (bool_decide _ && _); [by eapply (B2settle _ IH)|by eapply b2_m_return]|intros [] ? _ Hs5 Hk5; by eapply b2_m_return|exact Hs4].
    + intros n s s' r H Hs. cbn [refresh] in H. rewrite bind_get in H.
      destruct (decide (n ∈ g_checking s)).
      { unfold m_step in H. destruct (mark_dirty f n s) eqn:Em; simplify_eq. eapply (b2_inv_any_mark _ _ None); [right; left; exact Em|exact Hs]. }
      destruct (decide (n ∈ g_running s)); [by eapply b2_m_return|].
      eapply (b2_bind_of H); [intros ????; b2_pure_tac|intros d ? _ Hs1 Hk|exact Hs].
      eapply (b2_bind_of Hk); [|intros [] ? _ Hs2 Hk2; eapply (b2_bind_of Hk2); [intros ????; b2_pure_tac|intros d' ? _ Hs3 Hk3; destruct (decide (n_freshness d' = Dirty)); [by eapply (B2recompute _ IH)|by eapply b2_m_return]|exact Hs2]|exact Hs1].
      intros s2 [] _ Hm. destruct (decide (n_freshness d = Check)); [|by eapply b2_m_return].
      eapply (b2_bind_of Hm); [intros ????; b2_pure_tac|intros [] ? _ Hs2 Hk2|exact Hs1].
      eapply (b2_bind_of Hk2); [intros ????; by eapply (B2walk _ IH)|intros [] ? _ Hs3 Hk3|exact Hs2].
      eapply (b2_bind_of Hk3); [intros ????; b2_pure_tac|intros [] ? _ Hs4 Hk4|exact Hs3].
      eapply (b2_bind_of Hk4); [intros ????; b2_pure_tac|intros d' ? _ Hs5 Hk5|exact Hs4].
      destruct (decide (n_freshness d' = Check)); [|by eapply b2_m_return].
      eapply (b2_bind_of Hk5); [intros ????; b2_pure_tac|intros [] ? _ Hs6 Hk6; b2_pure_tac|exact Hs5].
    + intros n i s s' r H Hs. cbn [resolve_walk] in H.
      eapply (b2_bind_of H); [intros ????; b2_pure_tac|intros d ? _ Hs1 Hk|exact Hs].
      rewrite bind_get in Hk. destruct (decide (n_freshness d = Check)); [|by eapply b2_m_return].
      destruct (source_slots (sources_of (g_edges s1) n) !! i) as [[src _]|].
      * eapply (b2_bind_of Hk); [intros ????; by eapply (B2update _ IH)|intros [] ? _ Hs2 Hk2|exact Hs1].
        eapply (b2_bind_of Hk2); [intros ????; b2_pure_tac|intros d' ? _ Hs3 Hk3|exact Hs2].
        eapply (b2_bind_of Hk3); [intros ????; b2_pure_tac|intros d_src ? _ Hs4 Hk4|exact Hs3].
        rewrite bind_get in Hk4.
        eapply (b2_bind_of Hk4); [|intros [] ? _ Hs5 Hk5; by eapply (B2walk _ IH)|exact Hs4].
        intros s5 [] _ Hm. destruct (decide _); [|by eapply b2_m_return].
        unfold m_step in Hm. destruct (mark_dirty f n _) eqn:Em; simplify_eq.
        eapply (b2_inv_any_mark _ _ None); [right; left; exact Em|exact Hs4].
      * destruct (slot_holds stale_live s1 n); [by eapply (B2walk _ IH)|by eapply b2_m_return].
    + intros n s s' r H Hs. cbn [recompute] in H.
      eapply (b2_bind_of H); [intros ????; b2_pure_tac|intros d ? _ Hs1 Hk|exact Hs].
      destruct (n_scope d) as [o|]; [|by eapply (B2run_memo _ IH)].
      eapply (b2_bind_of Hk); [intros ????; b2_pure_tac|intros [] ? _ Hs2 Hk2|exact Hs1].
      eapply (b2_bind_of Hk2); [intros ????; b2_pure_tac|intros [] ? _ Hs3 Hk3|exact Hs2].
      eapply (b2_bind_of Hk3); [intros ????; by eapply (B2discharge _ IH)|intros [] ? _ Hs4 Hk4|exact Hs3].
      eapply (b2_bind_of Hk4); [intros ????; b2_pure_tac|intros d' ? _ Hs5 Hk5|exact Hs4].
      destruct (n_disposed d').
      * destruct (decide (n_freshness d' = Clean)); [b2_pure_tac|by eapply b2_m_return].
      * destruct (decide (n_runs d' = n_runs d)); [by eapply (B2run_memo _ IH)|].
        destruct (decide (n_freshness d' = Dirty)); [by eapply (B2recompute _ IH)|by eapply b2_m_return].
    + intros n s s' r H Hs. cbn [run_memo] in H.
      eapply (b2_bind_of H); [intros ????; b2_pure_tac|intros d ? _ Hs1 Hk|exact Hs].
      eapply (b2_bind_of Hk); [intros ????; b2_pure_tac|intros [] ? _ Hs2 Hk2|exact Hs1].
      eapply (b2_bind_of Hk2); [intros ????; b2_pure_tac|intros [] ? _ Hs3 Hk3|exact Hs2].
      eapply (b2_bind_of Hk3); [intros ????; b2_pure_tac|intros [] ? _ Hs4 Hk4|exact Hs3].
      eapply (b2_bind_of Hk4); [intros ????; b2_pure_tac|intros [] ? _ Hs5 Hk5|exact Hs4].
      eapply (b2_bind_of Hk5); [intros ????; eapply b2_hosted; [eassumption|intros ?????; by eapply (B2exec _ IH)|eassumption]|intros r0 ? _ Hs6 Hk6|exact Hs5].
      eapply (b2_bind_of Hk6); [intros ????; b2_pure_tac|intros d1 ? _ Hs7 Hk7|exact Hs6].
      eapply (b2_bind_of Hk7); [intros ????; destruct (if n_violated d1 then Fail pure_scope_failure else r0); b2_pure_tac|intros [] ? _ Hs8 Hk8|exact Hs7].
      eapply (b2_bind_of Hk8); [intros ????; b2_pure_tac|intros [] ? _ Hs9 Hk9|exact Hs8].
      eapply (b2_bind_of Hk9); [intros ????; b2_pure_tac|intros [] ? _ Hs10 Hk10|exact Hs9].
      eapply (b2_bind_of Hk10); [intros ????; destruct (if n_violated d1 then Fail pure_scope_failure else r0); b2_pure_tac|intros [] ? _ Hs11 Hk11|exact Hs10].
      eapply (b2_bind_of Hk11); [intros ????; b2_pure_tac|intros d2 ? _ Hs12 Hk12|exact Hs11].
      eapply (b2_bind_of Hk12); [intros s13 moved Hs12' Hcmp; destruct (decide _); [by eapply b2_m_return|destruct (n_comparer d2); [by eapply b2_m_return|by eapply b2_m_return|eapply (b2_bind_of Hcmp); [intros ????; b2_pure_tac|intros [] ? _ Hs_ Hm_; by eapply b2_m_return|exact Hs12']]]|intros moved ? _ Hs13 Hk13|exact Hs12].
      rewrite bind_get in Hk13.
      eapply (b2_bind_of Hk13); [intros ????; b2_pure_tac|intros [] ? _ Hs14 Hk14|exact Hs13].
      eapply (b2_bind_of Hk14); [|intros [] ? _ Hs15 Hk15; eapply (b2_bind_of Hk15); [intros ????; b2_pure_tac|intros d3 ? _ Hs16 Hk16; destruct (decide (n_freshness d3 = Clean)); [by eapply b2_m_return|b2_pure_tac]|exact Hs15]|exact Hs14].
      intros s15 [] _ Hm. destruct moved; [|by eapply b2_m_return].
      eapply (b2_bind_of Hm); [intros ?? Hs_ Hm_; unfold m_step in Hm_; destruct (notify_dirty_except f n _ _) eqn:Em; simplify_eq; eapply b2_inv_any_mark; [do 4 right; exact Em|exact Hs_]|intros [] s16 _ Hs16 Hk16; rewrite bind_get in Hk16; destruct (bool_decide _ && _ && _); [b2_pure_tac|by eapply b2_m_return]|exact Hs14].
    + intros n s s' r H Hs. cbn [execute_effect] in H.
      eapply (b2_bind_of H); [intros ????; b2_pure_tac|intros d ? _ Hs1 Hk|exact Hs].
      destruct (n_disposed d); [b2_pure_tac|].
      eapply (b2_bind_of Hk); [|intros [] ? _ Hs2 Hk2|exact Hs1].
      * intros s2 [] _ Hm. destruct (decide (n_freshness d = Check)); [|by eapply b2_m_return].
        eapply (b2_bind_of Hm); [intros ????; by eapply (B2walk _ IH)|intros [] ? _ Hs3 Hk3|exact Hs1].
        eapply (b2_bind_of Hk3); [intros ????; b2_pure_tac|intros d' ? _ Hs4 Hk4|exact Hs3].
        destruct (decide (n_freshness d' = Check)); [|by eapply b2_m_return].
        eapply (b2_bind_of Hk4); [intros ????; b2_pure_tac|intros [] ? _ Hs5 Hk5; b2_pure_tac|exact Hs4].
      * eapply (b2_bind_of Hk2); [intros ????; b2_pure_tac|intros d' ? _ Hs3 Hk3|exact Hs2].
        eapply (b2_bind_of Hk3); [intros ????; b2_pure_tac|intros [] ? _ Hs4 Hk4; destruct (decide (n_freshness d' = Dirty)); [by eapply (B2run_effect _ IH)|by eapply b2_m_return]|exact Hs3].
    + intros n s s' r H Hs. cbn [run_effect] in H.
      eapply (b2_bind_of H); [intros ????; b2_pure_tac|intros [] ? _ Hs1 Hk|exact Hs].
      eapply (b2_bind_of Hk); [intros ????; b2_pure_tac|intros [] ? _ Hs2 Hk2|exact Hs1].
      eapply (b2_bind_of Hk2); [intros ????; b2_pure_tac|intros [] ? _ Hs3 Hk3|exact Hs2].
      eapply (b2_bind_of Hk3); [intros ????; b2_pure_tac|intros d ? _ Hs4 Hk4|exact Hs3].
      eapply (b2_bind_of Hk4); [|intros [] ? _ Hs5 Hk5|exact Hs4].
      * intros s5 [] _ Hm. destruct (n_scope d) as [o|]; [|by eapply b2_m_return].
        eapply (b2_bind_of Hm); [intros ????; b2_pure_tac|intros [] ? _ Hs5 Hk5|exact Hs4].
        eapply (b2_bind_of Hk5); [intros ????; by eapply (B2discharge _ IH)|intros [] ? _ Hs6 Hk6; b2_pure_tac|exact Hs5].
      * eapply (b2_bind_of Hk5); [intros ????; b2_pure_tac|intros d' ? _ Hs6 Hk6|exact Hs5].
        eapply (b2_bind_of Hk6); [intros s7 [] _ Hm; destruct (n_disposed d'); [by eapply b2_m_return|by eapply (B2body _ IH)]|intros [] ? _ Hs7 Hk7; b2_pure_tac|exact Hs6].
    + intros n s s' r H Hs. cbn [run_effect_body] in H.
      eapply (b2_bind_of H); [intros ????; b2_pure_tac|intros d ? _ Hs1 Hk|exact Hs].
      eapply (b2_bind_of Hk); [intros ????; b2_pure_tac|intros [] ? _ Hs2 Hk2|exact Hs1].
      eapply (b2_bind_of Hk2); [intros ????; b2_pure_tac|intros [] ? _ Hs3 Hk3|exact Hs2].
      eapply (b2_bind_of Hk3); [intros ????; eapply b2_hosted; [eassumption|intros ?????; by eapply (B2exec _ IH)|eassumption]|intros r0 ? _ Hs4 Hk4|exact Hs3].
      eapply (b2_bind_of Hk4); [intros ????; b2_pure_tac|intros d' ? _ Hs5 Hk5|exact Hs4].
      eapply (b2_bind_of Hk5); [intros ????; b2_pure_tac|intros [] ? _ Hs6 Hk6; destruct r0; [by eapply b2_m_return|eapply (b2_bind_of Hk6); [intros ????; b2_pure_tac|intros [] ? _ Hs7 Hk7; b2_pure_tac|exact Hs6]]|exact Hs5].
    + intros o s s' r H Hs. cbn [discharge] in H.
      eapply (b2_bind_of H); [intros ????; eapply b2_detached; [eassumption|intros ?????; by eapply (B2dispose_scope _ IH)|eassumption]|intros [] ? _ Hs1 Hk; rewrite bind_get in Hk; destruct (bool_decide _ && _ && _); [b2_pure_tac|by eapply b2_m_return]|exact Hs].
    + intros o s s' r H Hs. cbn [retire] in H.
      eapply (b2_bind_of H); [|intros [] ? _ Hs1 Hk; rewrite bind_get in Hk; destruct (live_queue s1); [by eapply (B2request _ IH)|by eapply b2_m_return]|exact Hs].
      intros s1 [] _ Hm. eapply b2_detached; [exact Hm| |exact Hs].
      intros s0 s2 r2 Hm0 Hs0.
      eapply (b2_bind_of Hm0); [intros ????; b2_pure_tac|intros x ? _ Hs2 Hk2; destruct (o_disposed x); [by eapply b2_m_return|eapply (b2_bind_of Hk2); [intros ????; b2_pure_tac|intros [] ? _ Hs3 Hk3; by eapply (B2dispose_scope _ IH)|exact Hs2]]|exact Hs0].
    + intros o s s' r H Hs. cbn [dispose_scope] in H.
      eapply (b2_bind_of H); [intros ????; b2_pure_tac|intros x ? _ Hs1 Hk|exact Hs].
      eapply (b2_bind_of Hk); [intros ????; b2_pure_tac|intros [] ? _ Hs2 Hk2; by eapply (B2teardown _ IH)|exact Hs1].
    + intros o ls cs s s' r H Hs. cbn [teardown] in H.
      destruct ls as [|l ls'], cs as [|[[b e] a] cs']; [by eapply b2_m_return|..].
      * eapply (b2_bind_of H); [intros ????; by eapply (B2cleanup _ IH)|intros [] ? _ Hs1 Hk; by eapply (B2teardown _ IH)|exact Hs].
      * eapply (b2_bind_of H); [intros ????; by eapply (B2release _ IH)|intros [] ? _ Hs1 Hk; by eapply (B2teardown _ IH)|exact Hs].
      * destruct (decide (a < l_acquired l)).
        -- eapply (b2_bind_of H); [intros ????; by eapply (B2release _ IH)|intros [] ? _ Hs1 Hk; by eapply (B2teardown _ IH)|exact Hs].
        -- eapply (b2_bind_of H); [intros ????; by eapply (B2cleanup _ IH)|intros [] ? _ Hs1 Hk; by eapply (B2teardown _ IH)|exact Hs].
    + intros o b e s s' r H Hs. cbn [run_cleanup] in H.
      eapply (b2_bind_of H); [intros ????; b2_pure_tac|intros x ? _ Hs1 Hk|exact Hs].
      eapply (b2_bind_of Hk); [intros ????; b2_pure_tac|intros [] ? _ Hs2 Hk2|exact Hs1].
      eapply (b2_bind_of Hk2); [intros ????; by eapply (B2exec _ IH)|intros r0 ? _ Hs3 Hk3|exact Hs2].
      destruct r0; [by eapply b2_m_return|].
      eapply (b2_bind_of Hk3); [intros ????; b2_pure_tac|intros [] ? _ Hs4 Hk4; b2_pure_tac|exact Hs3].
    + intros n s s' r H Hs. cbn [release] in H.
      eapply (b2_bind_of H); [intros ????; b2_pure_tac|intros [] ? _ Hs1 Hk; by eapply (B2dispose _ IH)|exact Hs].
    + intros n s s' r H Hs. cbn [dispose_node] in H.
      eapply (b2_bind_of H); [intros ????; b2_pure_tac|intros d s1 _ Hs1 Hk|exact Hs].
      destruct (n_disposed d); [by eapply b2_m_return|].
      eapply bind_inv in Hk as (s1' & [] & E1' & Hk').
      eapply bind_inv in Hk' as (s2' & [] & E2' & Hk'').
      eapply bind_inv in Hk'' as (s3' & [] & E3' & Hk''').
      assert (Hs3' : b2_inv s3') by (eapply b2_dispose_prefix; [exact Hs1|exact E1'|exact E2'|exact E3']).
      destruct (n_scope d); [by eapply (B2retire _ IH)|by eapply b2_m_return].
    + intros n o s s' r H Hs. cbn [attach] in H.
      eapply (b2_bind_of H); [intros ????; b2_pure_tac|intros x ? _ Hs1 Hk|exact Hs].
      destruct (o_disposed x).
      * eapply (b2_bind_of Hk); [intros ????; by eapply (B2release _ IH)|intros [] ? _ Hs2 Hk2; b2_pure_tac|exact Hs1].
      * eapply (b2_bind_of Hk); [intros ????; b2_pure_tac|intros [] ? _ Hs2 Hk2; b2_pure_tac|exact Hs1].
    + intros n v s s' r H Hs. cbn [write_signal] in H.
      eapply (b2_bind_of H); [intros ????; b2_pure_tac|intros d ? _ Hs1 Hk|exact Hs].
      destruct (n_kind d); [|discriminate|discriminate].
      destruct (decide (n_value d = v)); [by eapply b2_m_return|].
      eapply (b2_bind_of Hk); [intros ????; b2_pure_tac|intros [] ? _ Hs2 Hk2|exact Hs1].
      eapply (b2_bind_of Hk2); [intros ????; b2_pure_tac|intros [] ? _ Hs3 Hk3|exact Hs2].
      eapply (b2_bind_of Hk3); [intros ?? Hs_ Hm_; unfold m_step in Hm_; destruct (notify_dirty f n _) eqn:Em; simplify_eq; eapply (b2_inv_any_mark _ _ None); [do 3 right; left; exact Em|exact Hs_]|intros [] ? _ Hs4 Hk4; by eapply (B2request _ IH)|exact Hs3].
    + intros s s' r H Hs. cbn [request_flush] in H. rewrite bind_get in H.
      destruct (live_queue s && bool_decide (g_batch_depth s = 0)); [|by eapply b2_m_return].
      destruct (outside_bodies s); [by eapply (B2flush _ IH)|].
      destruct (g_flushing s); [by eapply b2_m_return|b2_pure_tac].
    + intros s s' r H Hs. cbn [settle_flush] in H. rewrite bind_get in H.
      destruct (g_flush_owed s && bool_decide (g_batch_depth s = 0) && outside_bodies s); [by eapply (B2flush _ IH)|by eapply b2_m_return].
    + intros s s' r H Hs. cbn [run_flush] in H. rewrite bind_get in H.
      destruct (g_flushing s); [by eapply b2_m_return|].
      eapply (b2_bind_of H); [intros ????; b2_pure_tac|intros [] ? _ Hs1 Hk|exact Hs].
      eapply (b2_bind_of Hk); [intros ????; by eapply (B2drain _ IH)|intros [] ? _ Hs2 Hk2; b2_pure_tac|exact Hs1].
    + intros s s' r H Hs. cbn [drain] in H. rewrite bind_get in H.
      destruct (g_queue s !! g_queue_head s).
      * eapply (b2_bind_of H); [intros ????; b2_pure_tac|intros [] ? _ Hs1 Hk|exact Hs].
        eapply (b2_bind_of Hk); [intros ????; by eapply (B2execute _ IH)|intros [] ? _ Hs2 Hk2; by eapply (B2drain _ IH)|exact Hs1].
      * b2_pure_tac.
Qed.
References
✓ No axioms

Every disposed computation reachable from a new graph holds empty source_slots at all points (including on the running stack), and off-stack disposed nodes have no edges in either map.

Strengthens [Ownership.disposed_no_edges] to hold even during an in-flight computation (dispose_node clears sources via sources_clear and #318 live_current blocks track on a disposed current node), guaranteeing that reading any slot k during track witnesses n_disposed d = false.

Arguments

f : nat
fuel bound.
fl : State.flags
initial flags.
c : cmd
program command executed from init fl.
s' : State.state
reachable intermediate or final state (b2_inv preserved across all 26 engine functions).
r : outcome
command outcome.
n : node_id
disposed node id.
d : State.node
node record at n.
k
slot index observed in sources_of.
p
slot pair (src, handle).
Theorem disposed_empty_sources f fl c s' r n d :
  run_top f c (init fl) = Some (s', r) ->
  g_nodes s' !! n = Some d -> n_disposed d = true ->
  source_slots (sources_of (g_edges s') n) = [] /\
  (n ∉ g_running s' ->
    (forall src, ~ edge s' src n) /\
    (forall src, n ∉ observer_view (observers_of (g_edges s') src))) /\
  (forall k p, source_slots (sources_of (g_edges s') n) !! k = Some p -> False).

Arguments

f : nat
fuel bound.
fl : State.flags
initial flags.
c : cmd
program command executed from init fl.
s' : State.state
reachable intermediate or final state (b2_inv preserved across all 26 engine functions).
r : outcome
command outcome.
n : node_id
disposed node id.
d : State.node
node record at n.
k
slot index observed in sources_of.
p
slot pair (src, handle).
Qed
Proof.
  intros Hrun Hn Hdz.
  pose proof (B2exec _ (b2_inv_all f) [] c _ _ _ Hrun (b2_inv_init fl)) as [Hm Hd].
  pose proof (Hd n d Hn Hdz) as Hslots.
  split_and!; [exact Hslots| |intros k p Hp; rewrite Hslots, lookup_nil in Hp; discriminate].
  intros Hnr. split.
  - intros src (i & h & Hsl & _). unfold slot_at in Hsl. rewrite Hslots in Hsl. discriminate.
  - intros src Hin. apply view_elem in Hin as (h & i & Hobs).
    pose proof (proj2 (Hm n i src h) Hobs) as Hsl.
    unfold slot_at in Hsl. rewrite Hslots, lookup_nil in Hsl. discriminate.
Qed.
References
✓ No axioms

When source_cursor = 0 and slot 0 matches src, track refines to n_disposed = false and setting source_cursor := 1.

Refines Graph.Track + SourceList.Add under b2_inv (and for any state reachable from init or satisfying engine_inv + on-stack cleared disposed slots): the presence of slot 0 rules out disposal without an explicit branch, #318 makes live_current s = Some c, and sources_add c src hits slot 0 and sets source_cursor := 1.

Arguments

s : State.state
current engine state satisfying b2_inv s.
c : node_id
g_current s computation id.
d : State.node
node record at c.
src : node_id
source id read.
h : option nat
handle stored at slot 0.
Theorem track_slot0_fast_eq s c d src h :
  b2_inv s ->
  g_current s = Some c ->
  g_nodes s !! c = Some d ->
  source_cursor (sources_of (g_edges s) c) = 0 ->
  source_slots (sources_of (g_edges s) c) !! 0 = Some (src, h) ->
  n_disposed d = false /\
  live_current s = Some c /\
  sources_add c src (g_edges s) = Some (set_cursor 1 c (g_edges s)) /\
  track src s = Some (ghost_read c src (read_prefix (g_edges s) c) (with_edges (set_cursor 1 c (g_edges s)) s), tt).

Arguments

s : State.state
current engine state satisfying b2_inv s.
c : node_id
g_current s computation id.
d : State.node
node record at c.
src : node_id
source id read.
h : option nat
handle stored at slot 0.
Qed
Proof.
  intros [_ Hd] Hcur Hc Hcursor Hslot0.
  assert (Hdz : n_disposed d = false).
  { destruct (n_disposed d) eqn:E; [|reflexivity].
    rewrite (Hd c d Hc E), lookup_nil in Hslot0. discriminate. }
  assert (Hlive : live_current s = Some c)
    by (unfold live_current; by rewrite Hcur, Hc, Hdz).
  assert (Hadd : sources_add c src (g_edges s) = Some (set_cursor 1 c (g_edges s))).
  { unfold sources_add, set_cursor. rewrite Hcursor, Hslot0. simpl.
    by rewrite decide_True. }
  split_and!; [exact Hdz|exact Hlive|exact Hadd|].
  unfold track. by rewrite Hlive, Hadd.
Qed.
References
✓ No axioms

When source_cursor = 1 and slot 1 matches src, track refines to n_disposed = false and setting source_cursor := 2.

Refines Graph.Track + SourceList.Add under b2_inv: reading slot 1 establishes n_disposed d = false (disposed_empty_sources), so live_current s = Some c and sources_add c src bumps source_cursor from 1 to 2.

Arguments

s : State.state
current engine state satisfying b2_inv s.
c : node_id
g_current s computation id.
d : State.node
node record at c.
src : node_id
source id read.
h : option nat
handle stored at slot 1.
Theorem track_slot1_fast_eq s c d src h :
  b2_inv s ->
  g_current s = Some c ->
  g_nodes s !! c = Some d ->
  source_cursor (sources_of (g_edges s) c) = 1 ->
  source_slots (sources_of (g_edges s) c) !! 1 = Some (src, h) ->
  n_disposed d = false /\
  live_current s = Some c /\
  sources_add c src (g_edges s) = Some (set_cursor 2 c (g_edges s)) /\
  track src s = Some (ghost_read c src (read_prefix (g_edges s) c) (with_edges (set_cursor 2 c (g_edges s)) s), tt).

Arguments

s : State.state
current engine state satisfying b2_inv s.
c : node_id
g_current s computation id.
d : State.node
node record at c.
src : node_id
source id read.
h : option nat
handle stored at slot 1.
Qed
Proof.
  intros [_ Hd] Hcur Hc Hcursor Hslot1.
  assert (Hdz : n_disposed d = false).
  { destruct (n_disposed d) eqn:E; [|reflexivity].
    rewrite (Hd c d Hc E), lookup_nil in Hslot1. discriminate. }
  assert (Hlive : live_current s = Some c)
    by (unfold live_current; by rewrite Hcur, Hc, Hdz).
  assert (Hadd : sources_add c src (g_edges s) = Some (set_cursor 2 c (g_edges s))).
  { unfold sources_add, set_cursor. rewrite Hcursor, Hslot1. simpl.
    by rewrite decide_True. }
  split_and!; [exact Hdz|exact Hlive|exact Hadd|].
  unfold track. by rewrite Hlive, Hadd.
Qed.
References
✓ No axioms

When slot k at the cursor matches src, track refines to n_disposed = false and setting source_cursor := k + 1, for every k.

Refines the Graph.TrackNodeRest hit at slots 2 and up (cur < count and rest[cur - 2].Source == source) under b2_inv: a slot at the cursor rules out disposal (disposed_empty_sources), so the hit needs no IsDisposed test, and sources_add c src advances source_cursor from k to k + 1. track_slot0_fast_eq and track_slot1_fast_eq are its cases k = 0 and k = 1.

Arguments

s : State.state
current engine state satisfying b2_inv s.
c : node_id
g_current s computation id.
d : State.node
node record at c.
src : node_id
source id read.
h : option nat
handle stored at slot k.
k : nat
the cursor.
Theorem track_slot_fast_eq s c d src h k :
  b2_inv s ->
  g_current s = Some c ->
  g_nodes s !! c = Some d ->
  source_cursor (sources_of (g_edges s) c) = k ->
  source_slots (sources_of (g_edges s) c) !! k = Some (src, h) ->
  n_disposed d = false /\
  live_current s = Some c /\
  sources_add c src (g_edges s) = Some (set_cursor (S k) c (g_edges s)) /\
  track src s = Some (ghost_read c src (read_prefix (g_edges s) c) (with_edges (set_cursor (S k) c (g_edges s)) s), tt).

Arguments

s : State.state
current engine state satisfying b2_inv s.
c : node_id
g_current s computation id.
d : State.node
node record at c.
src : node_id
source id read.
h : option nat
handle stored at slot k.
k : nat
the cursor.
Qed
Proof.
  intros [_ Hd] Hcur Hc Hcursor Hslot.
  assert (Hdz : n_disposed d = false).
  { destruct (n_disposed d) eqn:E; [|reflexivity].
    rewrite (Hd c d Hc E), lookup_nil in Hslot. discriminate. }
  assert (Hlive : live_current s = Some c)
    by (unfold live_current; by rewrite Hcur, Hc, Hdz).
  assert (Hadd : sources_add c src (g_edges s) = Some (set_cursor (S k) c (g_edges s))).
  { unfold sources_add, set_cursor. rewrite Hcursor, Hslot. simpl.
    by rewrite decide_True. }
  split_and!; [exact Hdz|exact Hlive|exact Hadd|].
  unfold track. by rewrite Hlive, Hadd.
Qed.
References

B3: Marking fast paths — leaf clean memo, moved single-check memo, iterative check spine

Lemma notify_with_nil mark src s :
  observer_items (observers_of (g_edges s) src) = [] ->
  notify_with mark src s = Some s.
Qed
Proof.
  intros Hobs. unfold notify_with. rewrite Hobs. reflexivity.
Qed.
References
Lemma notify_with_singleton mark src c slot s :
  observer_items (observers_of (g_edges s) src) = [(c, slot)] ->
  notify_with mark src s = mark c s.
Qed
Proof.
  intros Hobs. unfold notify_with. rewrite Hobs. simpl.
  rewrite Hobs. destruct (decide _); [simpl in *; lia|].
  simpl. destruct (mark c s); reflexivity.
Qed.
References
✓ No axioms

Marking a Clean leaf memo dirty updates n_freshness := Dirty directly while preserving n_unannounced and g_edges.

Refines the leaf-clean fast path in NodeOps.MarkDirty: because observer_items of m is empty, notify_with (mark_check f) terminates immediately without touching g_edges or n_unannounced.

Arguments

f : nat
remaining recursion fuel.
m : node_id
leaf memo node id.
d : State.node
memo record at m before marking.
s : State.state
engine state before marking.
Theorem leaf_clean_memo_mark_dirty_eq f m d s :
  g_nodes s !! m = Some d ->
  n_kind d = KMemo ->
  n_freshness d = Clean ->
  observer_items (observers_of (g_edges s) m) = [] ->
  let d' := node_marked Dirty (n_unannounced d) (n_queued d) d in
  let s' := set_node m d' s in
  mark_dirty (S f) m s = Some s' /\
  n_unannounced d' = n_unannounced d /\
  g_edges s' = g_edges s.

Arguments

f : nat
remaining recursion fuel.
m : node_id
leaf memo node id.
d : State.node
memo record at m before marking.
s : State.state
engine state before marking.
Qed
Proof.
  intros Hm Hk Hc Hobs. simpl. rewrite Hm, Hk, decide_True by exact Hc.
  rewrite notify_with_nil; [|simpl; exact Hobs].
  repeat split; reflexivity.
Qed.
References
✓ No axioms

notify_dirty_except on a source with sole observer [m] (KMemo, Check, n_unannounced = false) promotes m directly to Dirty.

Refines NotifyMoved / NotifyMovedTop fast path (r = None): since m is already Check and unflagged for deferred announcement, mark_dirty updates n_freshness m := Dirty in one write without propagating mark_check downstream.

Arguments

f : nat
remaining recursion fuel.
src : node_id
moved source node id.
m : node_id
sole memo observer id.
slot : nat
edge slot index stored on m.
dm : State.node
node record at m.
s : State.state
engine state before notification.
Theorem notify_moved_single_check_memo_eq f src m slot dm s :
  observer_items (observers_of (g_edges s) src) = [(m, slot)] ->
  g_nodes s !! m = Some dm ->
  n_kind dm = KMemo ->
  n_freshness dm = Check ->
  n_unannounced dm = false ->
  notify_dirty_except (S (S f)) src None s =
    Some (set_node m (node_marked Dirty false (n_queued dm) dm) s).

Arguments

f : nat
remaining recursion fuel.
src : node_id
moved source node id.
m : node_id
sole memo observer id.
slot : nat
edge slot index stored on m.
dm : State.node
node record at m.
s : State.state
engine state before notification.
Qed
Proof.
  intros Hobs Hm Hk Hc Hu. unfold notify_dirty_except.
  erewrite notify_with_singleton; [|exact Hobs].
  unfold mark_except. rewrite decide_False by (intros [H _]; discriminate).
  simpl. rewrite Hm, Hk, decide_False by congruence.
  unfold announce_due. rewrite Hu, andb_false_r.
  rewrite decide_False by congruence.
  reflexivity.
Qed.
References

Tail-recursive single-memo `Check` spine walk mirroring `NotifyCheckSpine`, falling back to `notify_check` on fan-out > 1 or non-memo observers.

Fixpoint iter_mark_check_chain (fuel : nat) (n : node_id) (s : state) {struct fuel} : option state :=
  match fuel with
  | 0 => None
  | S f =>
    match g_nodes s !! n with
    | Some d =>
      match n_kind d with
      | KMemo =>
        if bool_decide (n_freshness d = Clean) || announce_due s d then
          let nf := if decide (n_freshness d = Clean) then Check else n_freshness d in
          let nu := if decide (n_freshness d = Clean) then n_unannounced d else false in
          let s1 := set_node n (node_marked nf nu (n_queued d) d) s in
          match observer_items (observers_of (g_edges s1) n) with
          | [] => Some s1
          | [(next, _)] =>
            match g_nodes s1 !! next with
            | Some dn =>
              match n_kind dn with
              | KMemo => iter_mark_check_chain f next s1
              | _ => notify_check f n s1
              end
            | None => notify_check f n s1
            end
          | _ => notify_check f n s1
          end
        else Some s
      | KEffect =>
        Some (effect_marked (if decide (n_freshness d = Clean) then Check else n_freshness d) n d s)
      | KSignal => Some s
      end
    | None => Some s
    end
  end.
References
✓ No axioms

The iterative single-memo spine walk iter_mark_check_chain is extensionally equal to mark_check on all inputs and states.

Refines NotifyCheckSpine: tail-recursing down single KMemo observer edges and falling back to notify_check on empty, non-memo, or multi-observer edges produces the exact same option state as recursive mark_check (and in particular along any clean single-memo chain).

Arguments

fuel : nat
fuel bound.
n : node_id
starting node id.
s : State.state
starting engine state.
Theorem iterative_mark_check_chain_eq fuel n s :
  iter_mark_check_chain fuel n s = mark_check fuel n s.

Arguments

fuel : nat
fuel bound.
n : node_id
starting node id.
s : State.state
starting engine state.
Qed
Proof.
  revert n s. induction fuel as [|f IH]; intros n s; simpl; [reflexivity|].
  destruct (g_nodes s !! n) as [d|]; [|reflexivity].
  destruct (n_kind d) eqn:Hk; [reflexivity| |reflexivity].
  destruct (decide (n_freshness d = Clean)) as [Hc|Hc].
  - rewrite bool_decide_true by exact Hc. simpl.
    destruct (observer_items (observers_of (g_edges s) n)) as [|[next slot] [|p2 rest]] eqn:Hobs.
    + symmetry. apply notify_with_nil. simpl. exact Hobs.
    + destruct (<[n := node_marked Check (n_unannounced d) (n_queued d) d]> (g_nodes s) !! next) as [dn|];
        [|by unfold notify_check].
      destruct (n_kind dn); [by unfold notify_check| |by unfold notify_check].
      rewrite IH. symmetry. eapply notify_with_singleton. simpl. exact Hobs.
    + by unfold notify_check.
  - rewrite bool_decide_false by exact Hc. simpl.
    destruct (announce_due s d) eqn:Hann; [|reflexivity].
    destruct (observer_items (observers_of (g_edges s) n)) as [|[next slot] [|p2 rest]] eqn:Hobs.
    + symmetry. apply notify_with_nil. simpl. exact Hobs.
    + destruct (<[n := node_marked (n_freshness d) false (n_queued d) d]> (g_nodes s) !! next) as [dn|];
        [|by unfold notify_check].
      destruct (n_kind dn); [by unfold notify_check| |by unfold notify_check].
      rewrite IH. symmetry. eapply notify_with_singleton. simpl. exact Hobs.
    + by unfold notify_check.
Qed.
References

B4: Explicit-stack iterative single-memo `Check`-chain refresh

Lemma b4_bind_assoc {A B C} (m : M A) (k1 : A -> M B) (k2 : B -> M C) s :
  m_bind (m_bind m k1) k2 s = m_bind m (fun a => m_bind (k1 a) k2) s.
Qed
Proof.
  unfold m_bind. destruct (m s) as [[s1 a]|]; reflexivity.
Qed.
References
Lemma b4_bind_ret_l {A B} (a : A) (k : A -> M B) s :
  m_bind (m_return a) k s = k a s.
Qed
Proof. reflexivity. Qed.
References
Lemma b4_bind_ext {A B} (m : M A) (k1 k2 : A -> M B) s :
  (forall a s1, k1 a s1 = k2 a s1) ->
  m_bind m k1 s = m_bind m k2 s.
Qed
Proof.
  intros Hk. unfold m_bind. destruct (m s) as [[s1 a]|]; [apply Hk|reflexivity].
Qed.
References

Single-memo `Check` descent guard matching `RefreshChain`'s linear spine check.

Definition single_memo_check_slot (s : state) (cur : node_id) : option node_id :=
  if deferring s then
    if decide (cur ∉ g_checking s /\ cur ∉ g_running s) then
      match g_nodes s !! cur with
      | Some d =>
        if decide (n_freshness d = Check) then
          match source_slots (sources_of (g_edges s) cur) with
          | [(src, _)] =>
            if decide (src <> cur /\ src ∉ g_checking s /\ src ∉ g_running s) then
              match g_nodes s !! src with
              | Some d_src =>
                match n_kind d_src with
                | KMemo =>
                  if decide (n_freshness d_src = Check /\ n_disposed d_src = false)
                  then Some src else None
                | _ => None
                end
              | None => None
              end
            else None
          | _ => None
          end
        else None
      | None => None
      end
    else None
  else None.
References
Definition refresh_after_src (f_md f_rw f_rec : nat) (cur src : node_id) : M unit :=
  let* _ := get_node src in
  let* d' := get_node cur in
  let* d_src' := get_node src in
  let* s1 := m_get in
  let* _ :=
    if decide (n_freshness d' = Check /\ stale_live d_src' = true /\ (src ∈ g_running s1 \/ src ∈ g_checking s1))
    then m_step (mark_dirty f_md cur) else m_return tt in
  let* _ := resolve_walk f_rw cur 1 in
  let* _ := pop_checking in
  let* d'' := get_node cur in
  let* _ :=
    if decide (n_freshness d'' = Check)
    then let* _ := m_modify (ghost_resolved cur) in
         modify_node cur (fun x => node_with_unannounced false (node_with_freshness Clean x))
    else m_return tt in
  let* d''' := get_node cur in
  if decide (n_freshness d''' = Dirty) then recompute f_rec cur else m_return tt.
References
Definition iter_unwind_one (fuel : nat) (cur src : node_id) : M unit :=
  refresh_after_src fuel fuel fuel cur src.
References
Fixpoint iter_unwind (fuel : nat) (stk : list (node_id * node_id)) {struct fuel} : M unit :=
  match fuel with
  | 0 => stuck
  | S f =>
    match stk with
    | [] => m_return tt
    | (cur, src) :: rest =>
      let* _ := iter_unwind_one f cur src in
      iter_unwind f rest
    end
  end.
References
Fixpoint iter_descend (fuel : nat) (stk : list (node_id * node_id)) (cur : node_id) {struct fuel} : M unit :=
  match fuel with
  | 0 => stuck
  | S f =>
    let* s := m_get in
    match single_memo_check_slot s cur with
    | Some src =>
      let* _ := push_checking cur in
      iter_descend f ((cur, src) :: stk) src
    | None =>
      let* _ := refresh f cur in
      iter_unwind f stk
    end
  end.
References
Definition iter_chain_refresh (fuel : nat) (n : node_id) : M unit :=
  iter_descend fuel [] n.
References
Lemma refresh_after_src_mono f1 f1' f2 f2' f3 f3' cur src s r :
  f1 <= f1' -> f2 <= f2' -> f3 <= f3' ->
  refresh_after_src f1 f2 f3 cur src s = Some r ->
  refresh_after_src f1' f2' f3' cur src s = Some r.
Qed
Proof.
  intros H1 H2 H3 H. unfold refresh_after_src in H |- *. revert H.
  eapply bind_mono; [intros ?? E; exact E|intros ??? _].
  eapply bind_mono; [intros ?? E; exact E|intros ??? _].
  eapply bind_mono; [intros ?? E; exact E|intros ??? _].
  eapply bind_mono; [intros ?? E; exact E|intros ??? _].
  eapply bind_mono; [|intros [] ?? _].
  { intros ?? E. destruct (decide _); [by eapply m_step_mark_dirty_mono|exact E]. }
  eapply bind_mono; [intros ??; by eapply (Mwalk _ (engine_mono_all f2))|intros [] ?? _].
  eapply bind_mono; [intros ?? E; exact E|intros ??? _].
  eapply bind_mono; [intros ?? E; exact E|intros d'' ?? _].
  eapply bind_mono; [intros ?? E; exact E|intros [] ?? _].
  eapply bind_mono; [intros ?? E; exact E|intros d''' ?? _].
  destruct (decide (n_freshness d''' = Dirty)); [by eapply (Mrecompute _ (engine_mono_all f3))|exact id].
Qed.
References
Lemma iter_unwind_mono f f' stk s r :
  f <= f' ->
  iter_unwind f stk s = Some r ->
  iter_unwind f' stk s = Some r.
Qed
Proof.
  revert f' stk s r. induction f as [|f IH]; intros f' stk s r Hle H.
  - cbn in H. discriminate.
  - destruct f' as [|f']; [lia|].
    assert (Hle' : f <= f') by lia.
    cbn [iter_unwind] in H |- *. destruct stk as [|[cur src] rest]; [exact H|].
    revert H. eapply bind_mono.
    + intros ?? E. eapply refresh_after_src_mono; [exact Hle'|exact Hle'|exact Hle'|exact E].
    + intros [] ?? _ E. eapply IH; [exact Hle'|exact E].
Qed.
References
Lemma single_memo_check_slot_inv s cur src :
  single_memo_check_slot s cur = Some src ->
  exists d h d_src,
    deferring s = true /\
    cur ∉ g_checking s /\ cur ∉ g_running s /\
    g_nodes s !! cur = Some d /\ n_freshness d = Check /\
    source_slots (sources_of (g_edges s) cur) = [(src, h)] /\
    src <> cur /\ src ∉ g_checking s /\ src ∉ g_running s /\
    g_nodes s !! src = Some d_src /\ n_kind d_src = KMemo /\
    n_freshness d_src = Check /\ n_disposed d_src = false.
Qed
Proof.
  unfold single_memo_check_slot.
  destruct (deferring s) eqn:Edef; [|discriminate].
  destruct (decide (cur ∉ g_checking s /\ cur ∉ g_running s)) as [[Hc1 Hc2]|]; [|discriminate].
  destruct (g_nodes s !! cur) as [d|] eqn:Ecur; [|discriminate].
  destruct (decide (n_freshness d = Check)) as [Hfc|]; [|discriminate].
  destruct (source_slots (sources_of (g_edges s) cur)) as [|[s0 h] [|p2 rest]] eqn:Esl; try discriminate.
  destruct (decide (s0 <> cur /\ s0 ∉ g_checking s /\ s0 ∉ g_running s)) as [[Hne [Hs1 Hs2]]|]; [|discriminate].
  destruct (g_nodes s !! s0) as [d_src|] eqn:Esrc; [|discriminate].
  destruct (n_kind d_src) eqn:Hk; try discriminate.
  destruct (decide (n_freshness d_src = Check /\ n_disposed d_src = false)) as [[Hfs Hdz]|]; [|discriminate].
  intros [= <-]. exists d, h, d_src. repeat split; assumption.
Qed.
References
Lemma deferring_with_checking ch s :
  deferring (with_checking ch s) = deferring s.
Qed
Proof. reflexivity. Qed.
References
Lemma resolve_walk_0_step f cur src s d h :
  g_nodes s !! cur = Some d ->
  n_freshness d = Check ->
  source_slots (sources_of (g_edges s) cur) = [(src, h)] ->
  resolve_walk (S f) cur 0 s =
  (let* _ := update_if_necessary f src in
   let* d' := get_node cur in
   let* d_src := get_node src in
   let* s1 := m_get in
   let* _ :=
     if decide (n_freshness d' = Check /\ stale_live d_src = true /\ (src ∈ g_running s1 \/ src ∈ g_checking s1))
     then m_step (mark_dirty f cur) else m_return tt in
   resolve_walk f cur 1) s.
Qed
Proof.
  intros Hcur Hfc Hsl. cbn [resolve_walk].
  unfold get_node at 1. unfold m_bind at 1. rewrite Hcur.
  rewrite bind_get, decide_True by exact Hfc.
  simpl. rewrite Hsl. reflexivity.
Qed.
References
Lemma b4_bind_arg {A B} (m1 m2 : M A) (k : A -> M B) s :
  m1 s = m2 s -> m_bind m1 k s = m_bind m2 k s.
Qed
Proof. intros Hm. unfold m_bind. rewrite Hm. reflexivity. Qed.
References
Lemma refresh_single_slot_unfold f cur src s :
  single_memo_check_slot s cur = Some src ->
  refresh (S (S (S (S f)))) cur s =
  m_bind (refresh f src)
    (fun _ => refresh_after_src (S (S f)) (S (S f)) (S (S (S f))) cur src)
    (with_checking (cur :: g_checking s) s).
Qed
Proof.
  intros Hslot.
  destruct (single_memo_check_slot_inv _ _ _ Hslot)
    as (d & h & d_src & Hdef & Hc1 & Hc2 & Hcur & Hfc & Hsl & _ & _ & _ & Hsrc & Hks & Hfs & Hdz).
  cbn [refresh]. rewrite bind_get.
  rewrite decide_False by exact Hc1.
  rewrite decide_False by exact Hc2.
  unfold get_node at 1. unfold m_bind at 1. rewrite Hcur.
  rewrite decide_True by exact Hfc.
  rewrite b4_bind_assoc. unfold push_checking, m_modify, m_bind at 1. simpl.
  set (s1 := with_checking (cur :: g_checking s) s).
  rewrite b4_bind_assoc.
  transitivity
    (m_bind (resolve_walk (S (S (S f))) cur 0)
       (fun _ =>
          let* _ := pop_checking in
          let* d'' := get_node cur in
          let* _ :=
            if decide (n_freshness d'' = Check)
            then let* _ := m_modify (ghost_resolved cur) in
                 modify_node cur (fun x => node_with_unannounced false (node_with_freshness Clean x))
            else m_return tt in
          let* d''' := get_node cur in
          if decide (n_freshness d''' = Dirty) then recompute (S (S (S f))) cur else m_return tt)
       s1).
  { apply b4_bind_ext. intros [] st. rewrite ?b4_bind_assoc. apply b4_bind_ext. intros [] st'.
    rewrite ?b4_bind_assoc. apply b4_bind_ext. intros d'' st''. reflexivity. }
  erewrite b4_bind_arg; [|erewrite resolve_walk_0_step; [reflexivity|simpl; exact Hcur|exact Hfc|simpl; exact Hsl]].
  rewrite b4_bind_assoc.
  cbn [update_if_necessary]. rewrite b4_bind_assoc.
  unfold get_node at 1. unfold m_bind at 1. simpl. rewrite Hsrc. simpl.
  rewrite Hks.
  assert (Hstl : stale_live d_src = true) by (unfold stale_live; by rewrite Hfs, Hdz).
  rewrite Hstl. rewrite b4_bind_assoc.
  cbn [refresh_or_pull]. rewrite ?b4_bind_assoc, bind_get.
  unfold s1 at 1. rewrite deferring_with_checking, Hdef.
  rewrite ?b4_bind_assoc.
  apply b4_bind_ext. intros [] st.
  unfold refresh_after_src.
  apply b4_bind_ext. intros _ st0.
  rewrite b4_bind_ret_l.
  rewrite ?b4_bind_assoc. apply b4_bind_ext. intros d' st1.
  rewrite ?b4_bind_assoc. apply b4_bind_ext. intros ds' st2.
  rewrite ?b4_bind_assoc. apply b4_bind_ext. intros s2 st3.
  rewrite ?b4_bind_assoc. apply b4_bind_ext. intros [] st4.
  reflexivity.
Qed.
References
Lemma b4_bind_push_checking {A} cur (k : unit -> M A) s :
  m_bind (push_checking cur) k s = k tt (with_checking (cur :: g_checking s) s).
Qed
Proof. reflexivity. Qed.
References
Lemma refresh_under_4_none f1 cur src s :
  f1 < 4 ->
  single_memo_check_slot s cur = Some src ->
  refresh f1 cur s = None.
Qed
Proof.
  intros Hlt Hslot.
  destruct (single_memo_check_slot_inv _ _ _ Hslot)
    as (d & h & d_src & Hdef & Hc1 & Hc2 & Hcur & Hfc & Hsl & _ & _ & _ & Hsrc & Hks & Hfs & Hdz).
  assert (Hstl : stale_live d_src = true) by (unfold stale_live; by rewrite Hfs, Hdz).
  destruct f1 as [|[|[|[|f']]]]; [reflexivity| | | |lia].
  - cbn [refresh]. rewrite bind_get, decide_False, decide_False by assumption.
    unfold get_node at 1. unfold m_bind at 1. rewrite Hcur.
    rewrite decide_True by exact Hfc. rewrite b4_bind_assoc.
    rewrite b4_bind_push_checking. reflexivity.
  - cbn [refresh]. rewrite bind_get, decide_False, decide_False by assumption.
    unfold get_node at 1. unfold m_bind at 1. rewrite Hcur.
    rewrite decide_True by exact Hfc. rewrite b4_bind_assoc.
    rewrite b4_bind_push_checking, b4_bind_assoc.
    erewrite b4_bind_arg; [|erewrite resolve_walk_0_step; [reflexivity|simpl; exact Hcur|exact Hfc|simpl; exact Hsl]].
    rewrite b4_bind_assoc. cbn [update_if_necessary]. reflexivity.
  - cbn [refresh]. rewrite bind_get, decide_False, decide_False by assumption.
    unfold get_node at 1. unfold m_bind at 1. rewrite Hcur.
    rewrite decide_True by exact Hfc. rewrite b4_bind_assoc.
    rewrite b4_bind_push_checking, b4_bind_assoc.
    erewrite b4_bind_arg; [|erewrite resolve_walk_0_step; [reflexivity|simpl; exact Hcur|exact Hfc|simpl; exact Hsl]].
    rewrite b4_bind_assoc. cbn [update_if_necessary]. rewrite b4_bind_assoc.
    unfold get_node at 1. unfold m_bind at 1. simpl. rewrite Hsrc. simpl.
    rewrite Hks, Hstl. rewrite b4_bind_assoc. cbn [refresh_or_pull]. reflexivity.
Qed.
References
Lemma iter_descend_fwd f stk n s s' :
  iter_descend f stk n s = Some (s', tt) ->
  exists f1 f2, m_bind (refresh f1 n) (fun _ => iter_unwind f2 stk) s = Some (s', tt).
Qed
Proof.
  revert stk n s s'. induction f as [|f IH]; intros stk n s s' H.
  - cbn in H. discriminate.
  - cbn [iter_descend] in H. rewrite bind_get in H.
    destruct (single_memo_check_slot s n) as [src|] eqn:Eslot.
    + unfold m_bind, push_checking, m_modify in H. simpl in H.
      destruct (IH _ _ _ _ H) as (f1 & f2 & Hm).
      unfold m_bind in Hm. destruct (refresh f1 src (with_checking (n :: g_checking s) s)) as [[s2 []]|] eqn:Er1; [|discriminate].
      destruct f2 as [|f2']; [cbn in Hm; discriminate|].
      cbn [iter_unwind] in Hm. unfold m_bind in Hm.
      destruct (iter_unwind_one f2' n src s2) as [[s3 []]|] eqn:Eu1; [|discriminate].
      set (m := max f1 f2').
      assert (Er' : refresh m src (with_checking (n :: g_checking s) s) = Some (s2, tt))
        by (eapply (Mrefresh _ (engine_mono_all f1)); [lia|exact Er1]).
      assert (Eu' : refresh_after_src (S (S m)) (S (S m)) (S (S (S m))) n src s2 = Some (s3, tt))
        by (eapply (refresh_after_src_mono f2' _ f2' _ f2' _); [lia|lia|lia|exact Eu1]).
      assert (Href : refresh (S (S (S (S m)))) n s = Some (s3, tt)).
      { erewrite refresh_single_slot_unfold; [|exact Eslot].
        unfold m_bind. rewrite Er'. exact Eu'. }
      exists (S (S (S (S m)))), f2'.
      unfold m_bind. rewrite Href. exact Hm.
    + exists f, f. exact H.
Qed.
References
Lemma iter_descend_bwd f1 f2 stk n s s' :
  m_bind (refresh f1 n) (fun _ => iter_unwind f2 stk) s = Some (s', tt) ->
  exists f, iter_descend f stk n s = Some (s', tt).
Qed
Proof.
  revert f2 stk n s s'. induction f1 as [f1 IH] using lt_wf_ind; intros f2 stk n s s' H.
  destruct (single_memo_check_slot s n) as [src|] eqn:Eslot.
  - destruct f1 as [|[|[|[|f1']]]].
    1,2,3,4: unfold m_bind in H; erewrite (refresh_under_4_none _ n src s) in H; [discriminate|lia|exact Eslot].
    unfold m_bind in H. destruct (refresh (S (S (S (S f1')))) n s) as [[s2 []]|] eqn:Href; [|discriminate].
    erewrite refresh_single_slot_unfold in Href by exact Eslot.
    unfold m_bind in Href. destruct (refresh f1' src (with_checking (n :: g_checking s) s)) as [[sm []]|] eqn:Er'; [|discriminate].
    set (fu := max (S (S (S f1'))) f2).
    assert (Eu1 : iter_unwind_one fu n src sm = Some (s2, tt))
      by (eapply (refresh_after_src_mono (S (S f1')) _ (S (S f1')) _ (S (S (S f1'))) _); [lia|lia|lia|exact Href]).
    assert (Eu2 : iter_unwind fu stk s2 = Some (s', tt))
      by (eapply (iter_unwind_mono f2); [lia|exact H]).
    assert (Hm' : m_bind (refresh f1' src) (fun _ => iter_unwind (S fu) ((n, src) :: stk)) (with_checking (n :: g_checking s) s) = Some (s', tt)).
    { unfold m_bind. rewrite Er'. cbn [iter_unwind]. unfold m_bind. rewrite Eu1. exact Eu2. }
    destruct (IH f1' ltac:(lia) (S fu) ((n, src) :: stk) src (with_checking (n :: g_checking s) s) s' Hm') as [f_desc Hdesc].
    exists (S f_desc). cbn [iter_descend]. rewrite bind_get, Eslot.
    unfold m_bind, push_checking, m_modify. simpl. exact Hdesc.
  - unfold m_bind in H. destruct (refresh f1 n s) as [[s1 []]|] eqn:Er1; [|discriminate].
    set (fm := max f1 f2).
    assert (Er' : refresh fm n s = Some (s1, tt)) by (eapply (Mrefresh _ (engine_mono_all f1)); [lia|exact Er1]).
    assert (Eu' : iter_unwind fm stk s1 = Some (s', tt)) by (eapply (iter_unwind_mono f2); [lia|exact H]).
    exists (S fm). cbn [iter_descend]. rewrite bind_get, Eslot.
    unfold m_bind. rewrite Er'. exact Eu'.
Qed.
References
✓ No axioms

The explicit-stack iterative Check-chain refresh iter_chain_refresh produces the exact same states as recursive refresh.

Refines RefreshChain: descending down single-memo Check edges while pushing g_checking and checkStack frames and unwinding in LIFO order with the repeat check, resolve_walk ... 1, and recompute agrees extensionally with refresh on every state.

Arguments

n : node_id
target memo node id.
s : State.state
initial engine state.
s' : State.state
resulting engine state.
Theorem iterative_chain_refresh_eq n s s' :
  (exists f, iter_chain_refresh f n s = Some (s', tt)) <->
  (exists f, refresh f n s = Some (s', tt)).

Arguments

n : node_id
target memo node id.
s : State.state
initial engine state.
s' : State.state
resulting engine state.
Qed
Proof.
  split.
  - intros [f H]. destruct (iter_descend_fwd _ _ _ _ _ H) as (f1 & f2 & Hm).
    unfold m_bind in Hm. destruct (refresh f1 n s) as [[s1 []]|] eqn:Er1; [|discriminate].
    destruct f2 as [|f2']; [cbn in Hm; discriminate|].
    cbn [iter_unwind] in Hm. unfold m_return in Hm. simplify_eq. by exists f1.
  - intros [f H]. apply (iter_descend_bwd f 1 [] n s s').
    unfold m_bind. rewrite H. reflexivity.
Qed.
References

B5: Queue equivalence and idle-graph fused `S -> E` / `S -> M -> E` execution

Definition queue_rel (s1 s2 : state) : Prop :=
  with_queue_head 0 (with_queue [] s1) = with_queue_head 0 (with_queue [] s2) /\
  pending s1 = pending s2 /\
  g_queue_head s1 <= length (g_queue s1) /\
  g_queue_head s2 <= length (g_queue s2).
References
Lemma queue_rel_refl s :
  g_queue_head s <= length (g_queue s) ->
  queue_rel s s.
Qed
Proof. intros Hle. repeat split; assumption. Qed.
References
Lemma queue_rel_sym s1 s2 : queue_rel s1 s2 -> queue_rel s2 s1.
Qed
Proof. intros (Hw & Hp & H1 & H2). repeat split; congruence || assumption. Qed.
References
Lemma queue_rel_reset_eq s1 s2 :
  queue_rel s1 s2 ->
  with_queue [] (with_queue_head 0 s1) = with_queue [] (with_queue_head 0 s2).
Qed
Proof.
  intros (Hw & _). destruct s1, s2; simpl in *; simplify_eq; reflexivity.
Qed.
References
Lemma queue_rel_drained_eq s1 s2 :
  queue_rel s1 s2 ->
  g_queue s1 = [] -> g_queue_head s1 = 0 ->
  g_queue s2 = [] -> g_queue_head s2 = 0 ->
  s1 = s2.
Qed
Proof.
  intros (Hw & _) Eq1 Eh1 Eq2 Eh2.
  destruct s1, s2; simpl in *; subst; simplify_eq; reflexivity.
Qed.
References
Lemma queue_rel_live_queue s1 s2 :
  queue_rel s1 s2 -> live_queue s1 = live_queue s2.
Qed
Proof.
  intros (_ & Hp & H1 & H2). unfold live_queue, pending in *.
  pose proof (f_equal length Hp) as Hlen. rewrite !length_drop in Hlen.
  destruct (bool_decide (g_queue_head s1 < length (g_queue s1))) eqn:E1,
           (bool_decide (g_queue_head s2 < length (g_queue s2))) eqn:E2; try reflexivity.
  - apply bool_decide_eq_true in E1. apply bool_decide_eq_false in E2. lia.
  - apply bool_decide_eq_false in E1. apply bool_decide_eq_true in E2. lia.
Qed.
References

Uses

3 more
  • bool_decide_eq_false
  • bool_decide_eq_true
  • list.length_drop
Lemma queue_rel_head_lookup s1 s2 :
  queue_rel s1 s2 -> g_queue s1 !! g_queue_head s1 = g_queue s2 !! g_queue_head s2.
Qed
Proof.
  intros (_ & Hp & _). unfold pending in Hp.
  pose proof (f_equal (fun l => l !! 0) Hp) as H0.
  rewrite !lookup_drop, !Nat.add_0_r in H0. exact H0.
Qed.
References

Uses

Lemma queue_rel_fields s1 s2 :
  queue_rel s1 s2 ->
  g_nodes s1 = g_nodes s2 /\
  g_edges s1 = g_edges s2 /\
  g_owners s1 = g_owners s2 /\
  g_flags s1 = g_flags s2 /\
  g_flushing s1 = g_flushing s2 /\
  g_flush_owed s1 = g_flush_owed s2 /\
  g_batch_depth s1 = g_batch_depth s2 /\
  g_pull_depth s1 = g_pull_depth s2 /\
  g_current s1 = g_current s2 /\
  g_untracked s1 = g_untracked s2 /\
  g_current_owner s1 = g_current_owner s2 /\
  g_running s1 = g_running s2 /\
  g_checking s1 = g_checking s2 /\
  live_queue s1 = live_queue s2 /\
  outside_bodies s1 = outside_bodies s2 /\
  deferring_b1 s1 = deferring_b1 s2 /\
  g_queue s1 !! g_queue_head s1 = g_queue s2 !! g_queue_head s2.
Qed
Proof.
  intros Hrel.
  pose proof (queue_rel_live_queue _ _ Hrel) as Hlq.
  pose proof (queue_rel_head_lookup _ _ Hrel) as Hhd.
  destruct Hrel as (Hw & _).
  destruct s1, s2; simpl in *; simplify_eq.
  unfold outside_bodies, deferring_b1 in *; simpl in *.
  repeat split; assumption || reflexivity.
Qed.
References
Lemma queue_rel_pop s1 s2 n :
  queue_rel s1 s2 ->
  g_queue s1 !! g_queue_head s1 = Some n ->
  queue_rel (with_queue_head (S (g_queue_head s1)) s1)
            (with_queue_head (S (g_queue_head s2)) s2).
Qed
Proof.
  intros Hrel Hn1.
  pose proof (queue_rel_head_lookup _ _ Hrel) as Hhd.
  rewrite Hn1 in Hhd. symmetry in Hhd.
  destruct Hrel as (Hw & Hp & H1 & H2).
  pose proof (lookup_lt_Some _ _ _ Hn1) as Hlt1.
  pose proof (lookup_lt_Some _ _ _ Hhd) as Hlt2.
  unfold queue_rel, pending in *. simpl in *.
  repeat split; [destruct s1, s2; simpl in *; simplify_eq; reflexivity| |lia|lia].
  rewrite (drop_S _ n (g_queue_head s1) Hn1) in Hp.
  rewrite (drop_S _ n (g_queue_head s2) Hhd) in Hp.
  by inversion Hp.
Qed.
References
Lemma queue_rel_enqueue n s1 s2 :
  queue_rel s1 s2 -> queue_rel (enqueue n s1) (enqueue n s2).
Qed
Proof.
  intros (Hw & Hp & H1 & H2). unfold queue_rel, pending, enqueue in *; simpl in *.
  repeat split; [destruct s1, s2; simpl in *; simplify_eq; reflexivity|..].
  - rewrite !drop_app_le by assumption. congruence.
  - rewrite length_app; simpl; lia.
  - rewrite length_app; simpl; lia.
Qed.
References

Uses

Lemma queue_rel_set_node n d s1 s2 :
  queue_rel s1 s2 -> queue_rel (set_node n d s1) (set_node n d s2).
Qed
Proof.
  intros (Hw & Hp & H1 & H2). unfold pending, set_node in *.
  destruct s1, s2; simpl in *; simplify_eq. by repeat split.
Qed.
References
Lemma queue_rel_effect_marked f n d s1 s2 :
  queue_rel s1 s2 -> queue_rel (effect_marked f n d s1) (effect_marked f n d s2).
Qed
Proof.
  intros Hrel. unfold effect_marked.
  destruct (n_disposed d || n_in_cleanup d); [exact Hrel|].
  destruct (n_queued d); [by apply queue_rel_set_node|apply queue_rel_enqueue, queue_rel_set_node; exact Hrel].
Qed.
References
Lemma qrel_walk_marks m1 m2 src k s1 s2 s1' :
  (forall c x y x', queue_rel x y -> m1 c x = Some x' -> exists y', m2 c y = Some y' /\ queue_rel x' y') ->
  queue_rel s1 s2 ->
  walk_marks m1 src k s1 = Some s1' ->
  exists s2', walk_marks m2 src k s2 = Some s2' /\ queue_rel s1' s2'.
Qed
Proof.
  intros Hm. revert s1 s2 s1'. induction k as [|k IH]; intros s1 s2 s1' Hrel H.
  - cbn in H |- *. simplify_eq. by exists s2.
  - cbn [walk_marks] in H |- *.
    destruct (queue_rel_fields _ _ Hrel) as (_ & He & _).
    rewrite <- He.
    destruct (decide (length (observer_items (observers_of (g_edges s1) src)) <= k));
      [by eapply IH|].
    destruct (observer_items (observers_of (g_edges s1) src) !! k) as [[c slot]|]; [|discriminate].
    unfold m_bind, option_bind in *.
    destruct (m1 c s1) as [sx1|] eqn:Em1; [|discriminate].
    destruct (Hm c s1 s2 sx1 Hrel Em1) as (sx2 & Em2 & Hrel1).
    rewrite Em2. simpl in *. by eapply IH.
Qed.
References
Lemma qrel_mark_check f n s1 s2 s1' :
  queue_rel s1 s2 ->
  mark_check f n s1 = Some s1' ->
  exists s2', mark_check f n s2 = Some s2' /\ queue_rel s1' s2'.
Qed
Proof.
  revert n s1 s2 s1'. induction f as [|f IH]; intros n s1 s2 s1' Hrel H.
  - cbn in H. discriminate.
  - cbn [mark_check] in H |- *.
    destruct (queue_rel_fields _ _ Hrel) as (Hn & He & Ho & Hfl & _).
    rewrite <- Hn.
    destruct (g_nodes s1 !! n) as [d|]; [|simplify_eq; by exists s2].
    destruct (n_kind d); [simplify_eq; by exists s2| |].
    + destruct (decide (n_freshness d = Clean)).
      * unfold notify_with in H |- *. simpl in H |- *. rewrite <- He.
        eapply qrel_walk_marks; [intros c x y x' Hxy Hm; by eapply IH| |exact H].
        by apply queue_rel_set_node.
      * assert (Hann : announce_due s1 d = announce_due s2 d)
          by (unfold announce_due; by rewrite Hfl).
        rewrite <- Hann. destruct (announce_due s1 d).
        -- unfold notify_with in H |- *. simpl in H |- *. rewrite <- He.
           eapply qrel_walk_marks; [intros c x y x' Hxy Hm; by eapply IH| |exact H].
           by apply queue_rel_set_node.
        -- simplify_eq. by exists s2.
    + simplify_eq. eexists; split; [reflexivity|].
      by apply queue_rel_effect_marked.
Qed.
References
Lemma qrel_mark_dirty f n s1 s2 s1' :
  queue_rel s1 s2 ->
  mark_dirty f n s1 = Some s1' ->
  exists s2', mark_dirty f n s2 = Some s2' /\ queue_rel s1' s2'.
Qed
Proof.
  intros Hrel H. destruct f as [|f]; [cbn in H; discriminate|].
  cbn [mark_dirty] in H |- *.
  destruct (queue_rel_fields _ _ Hrel) as (Hn & He & Ho & Hfl & _).
  rewrite <- Hn.
  destruct (g_nodes s1 !! n) as [d|]; [|simplify_eq; by exists s2].
  destruct (n_kind d); [simplify_eq; by exists s2| |].
  - destruct (decide (n_freshness d = Clean)).
    + unfold notify_with in H |- *. simpl in H |- *. rewrite <- He.
      eapply qrel_walk_marks; [intros c x y x' Hxy Hm; by eapply qrel_mark_check| |exact H].
      by apply queue_rel_set_node.
    + assert (Hann : announce_due s1 d = announce_due s2 d) by (unfold announce_due; by rewrite Hfl).
      rewrite <- Hann. destruct (announce_due s1 d).
      * unfold notify_with in H |- *. simpl in H |- *. rewrite <- He.
        eapply qrel_walk_marks; [intros c x y x' Hxy Hm; by eapply qrel_mark_check| |exact H].
        by apply queue_rel_set_node.
      * destruct (decide (n_freshness d = Dirty)); simplify_eq; [by exists s2|].
        eexists; split; [reflexivity|by apply queue_rel_set_node].
  - simplify_eq. eexists; split; [reflexivity|by apply queue_rel_effect_marked].
Qed.
References
Lemma qrel_notify_dirty f n s1 s2 s1' :
  queue_rel s1 s2 ->
  notify_dirty f n s1 = Some s1' ->
  exists s2', notify_dirty f n s2 = Some s2' /\ queue_rel s1' s2'.
Qed
Proof.
  intros Hrel H. unfold notify_dirty, notify_with in H |- *.
  destruct (queue_rel_fields _ _ Hrel) as (_ & He & _).
  rewrite <- He.
  eapply qrel_walk_marks; [intros c x y x' Hxy Hm; by eapply qrel_mark_dirty|exact Hrel|exact H].
Qed.
References
Lemma qrel_notify_dirty_except f n r s1 s2 s1' :
  queue_rel s1 s2 ->
  notify_dirty_except f n r s1 = Some s1' ->
  exists s2', notify_dirty_except f n r s2 = Some s2' /\ queue_rel s1' s2'.
Qed
Proof.
  intros Hrel H. unfold notify_dirty_except, notify_with in H |- *.
  destruct (queue_rel_fields _ _ Hrel) as (_ & He & _).
  rewrite <- He.
  eapply qrel_walk_marks; [|exact Hrel|exact H].
  intros c x y x' Hxy Hm. unfold mark_except in Hm |- *.
  destruct (queue_rel_fields _ _ Hxy) as (_ & Hey & _).
  rewrite <- Hey.
  destruct (decide _); [simplify_eq; by exists y|by eapply qrel_mark_dirty].
Qed.
References
Lemma qrel_bind_of {A B} {m : M A} {k : A -> M B} {s1 s2 s1' : state} {b : B}
    (Hb : m_bind m k s1 = Some (s1', b)) (Hrel : queue_rel s1 s2) :
  (forall sb1 a, m s1 = Some (sb1, a) -> queue_rel s1 s2 ->
    exists sb2, m s2 = Some (sb2, a) /\ queue_rel sb1 sb2) ->
  (forall a sb1 sb2, queue_rel sb1 sb2 -> k a sb1 = Some (s1', b) ->
    exists s2', k a sb2 = Some (s2', b) /\ queue_rel s1' s2') ->
  exists s2', m_bind m k s2 = Some (s2', b) /\ queue_rel s1' s2'.
Qed
Proof.
  intros Hm Hk. unfold m_bind in *.
  destruct (m s1) as [[sb1 a]|] eqn:Em1; [|discriminate].
  destruct (Hm _ _ eq_refl Hrel) as (sb2 & Em2 & Hrel1).
  rewrite Em2. eapply Hk; [exact Hrel1|exact Hb].
Qed.
References

Uses

Lemma qrel_m_return {A} (a : A) s1 s2 s1' r :
  m_return a s1 = Some (s1', r) -> queue_rel s1 s2 ->
  exists s2', m_return a s2 = Some (s2', r) /\ queue_rel s1' s2'.
Qed
Proof. intros H Hrel. unfold m_return in *. simplify_eq. by exists s2. Qed.
References
Lemma qrel_m_modify upd s1 s2 s1' r :
  m_modify upd s1 = Some (s1', r) -> queue_rel s1 s2 ->
  (forall x y, queue_rel x y -> queue_rel (upd x) (upd y)) ->
  exists s2', m_modify upd s2 = Some (s2', r) /\ queue_rel s1' s2'.
Qed
Proof.
  intros H Hrel Hupd. unfold m_modify in *. simplify_eq.
  eexists; split; [reflexivity|by apply Hupd].
Qed.
References
Lemma qrel_get_node n s1 s2 s1' d :
  get_node n s1 = Some (s1', d) -> queue_rel s1 s2 ->
  exists s2', get_node n s2 = Some (s2', d) /\ queue_rel s1' s2'.
Qed
Proof.
  intros H Hrel. destruct (queue_rel_fields _ _ Hrel) as (Hn & _).
  unfold get_node in *. rewrite <- Hn.
  destruct (g_nodes s1 !! n); [simplify_eq; by exists s2|discriminate].
Qed.
References
Lemma qrel_modify_node n u s1 s2 s1' r :
  modify_node n u s1 = Some (s1', r) -> queue_rel s1 s2 ->
  exists s2', modify_node n u s2 = Some (s2', r) /\ queue_rel s1' s2'.
Qed
Proof.
  intros H Hrel. destruct (queue_rel_fields _ _ Hrel) as (Hn & _).
  unfold modify_node in *. rewrite <- Hn.
  destruct (g_nodes s1 !! n); [|discriminate].
  simplify_eq. eexists; split; [reflexivity|by apply queue_rel_set_node].
Qed.
References
Lemma qrel_get_owner o s1 s2 s1' x :
  get_owner o s1 = Some (s1', x) -> queue_rel s1 s2 ->
  exists s2', get_owner o s2 = Some (s2', x) /\ queue_rel s1' s2'.
Qed
Proof.
  intros H Hrel. destruct (queue_rel_fields _ _ Hrel) as (_ & _ & Ho & _).
  unfold get_owner in *. rewrite <- Ho.
  destruct (g_owners s1 !! o); [simplify_eq; by exists s2|discriminate].
Qed.
References
Local Ltac qrel_upd_tac :=
  let x := fresh "x" in let y := fresh "y" in let Hxy := fresh "Hxy" in
  intros x y Hxy; destruct Hxy as (?&?&?&?);
  unfold queue_rel, pending,
    set_node, ghost_read, ghost_run_start, ghost_effect_start, ghost_publish,
    ghost_run_end, ghost_dirty_cleared, ghost_resolved, ghost_registered,
    run_end_unnoticed, slot_holds, add_if in *;
  destruct x, y; simpl in *; simplify_eq;
  repeat match goal with
  | |- context [match ?m !! ?k with _ => _ end] => destruct (m !! k)
  end;
  simpl in *;
  repeat split; try assumption; try reflexivity.
References
Lemma qrel_current_owner s1 s2 s1' r :
  current_owner s1 = Some (s1', r) -> queue_rel s1 s2 ->
  exists s2', current_owner s2 = Some (s2', r) /\ queue_rel s1' s2'.
Qed
Proof.
  intros H Hrel. destruct Hrel as (Hw & Hp & H1 & H2).
  unfold current_owner, pending in *.
  destruct s1, s2; simpl in *; simplify_eq.
  destruct g_current_owner0, g_current0; simplify_eq; try (eexists; split; [reflexivity|by repeat split]).
  destruct (g_nodes0 !! n); [|discriminate].
  destruct (n_scope n0); simplify_eq; try (eexists; split; [reflexivity|by repeat split]).
  destruct (match n_kind n0, n_mode n0 with KMemo, Pure => true | _, _ => false end); simplify_eq;
    (eexists; split; [reflexivity|by repeat split]).
Qed.
References
Lemma qrel_next_id s1 s2 s1' r :
  next_id s1 = Some (s1', r) -> queue_rel s1 s2 ->
  exists s2', next_id s2 = Some (s2', r) /\ queue_rel s1' s2'.
Qed
Proof.
  intros H (Hw & Hp & H1 & H2). unfold next_id, pending in *.
  destruct s1, s2; simpl in *; simplify_eq.
  eexists; split; [reflexivity|by repeat split].
Qed.
References
Lemma qrel_note_host_read n o s1 s2 s1' r :
  note_host_read n o s1 = Some (s1', r) -> queue_rel s1 s2 ->
  exists s2', note_host_read n o s2 = Some (s2', r) /\ queue_rel s1' s2'.
Qed
Proof.
  intros H (Hw & Hp & H1 & H2). unfold note_host_read, emit, m_modify, pending in *.
  destruct s1, s2; simpl in *; simplify_eq.
  destruct g_current0, g_current_owner0; simplify_eq; try (eexists; split; [reflexivity|by repeat split]).
  destruct (decide _); simplify_eq; (eexists; split; [reflexivity|by repeat split]).
Qed.
References
Lemma qrel_log_label s1 s2 s1' r :
  log_label s1 = Some (s1', r) -> queue_rel s1 s2 ->
  exists s2', log_label s2 = Some (s2', r) /\ queue_rel s1' s2'.
Qed
Proof.
  intros H (Hw & Hp & H1 & H2). unfold log_label, pending in *.
  destruct s1, s2; simpl in *; simplify_eq.
  destruct g_current0, g_current_owner0; simplify_eq; (eexists; split; [reflexivity|by repeat split]).
Qed.
References
Lemma qrel_track n s1 s2 s1' r :
  track n s1 = Some (s1', r) -> queue_rel s1 s2 ->
  exists s2', track n s2 = Some (s2', r) /\ queue_rel s1' s2'.
Qed
Proof.
  intros H (Hw & Hp & H1 & H2). unfold track, live_current, ghost_read, pending in *.
  destruct s1, s2; simpl in *; simplify_eq.
  destruct g_current0; simplify_eq; try (eexists; split; [reflexivity|by repeat split]).
  destruct (g_nodes0 !! n0); simplify_eq; try (eexists; split; [reflexivity|by repeat split]).
  destruct (n_disposed n1); simplify_eq; try (eexists; split; [reflexivity|by repeat split]).
  destruct (sources_add n0 n g_edges0); [|discriminate]. simplify_eq.
  destruct (g_nodes0 !! n); (eexists; split; [reflexivity|by repeat split]).
Qed.
References
Lemma qrel_mark_reader f s1 s2 s1' r :
  mark_reader f s1 = Some (s1', r) -> queue_rel s1 s2 ->
  exists s2', mark_reader f s2 = Some (s2', r) /\ queue_rel s1' s2'.
Qed
Proof.
  intros H Hrel.
  destruct (queue_rel_fields _ _ Hrel) as (_ & _ & _ & Hfl & _ & _ & _ & _ & Hcur & _).
  unfold mark_reader in *. rewrite <- Hcur, <- Hfl.
  destruct (g_current s1), (f_reader_mark (g_flags s1)); simplify_eq; try (by exists s2).
  destruct (mark_check f n s1) eqn:Em; simplify_eq.
  destruct (qrel_mark_check _ _ _ _ _ Hrel Em) as (s2' & Em2 & Hrel').
  rewrite Em2. by exists s2'.
Qed.
References
Lemma qrel_announce n s1 s2 s1' r :
  announce n s1 = Some (s1', r) -> queue_rel s1 s2 ->
  exists s2', announce n s2 = Some (s2', r) /\ queue_rel s1' s2'.
Qed
Proof.
  intros H Hrel.
  destruct (queue_rel_fields _ _ Hrel) as (_ & _ & _ & Hfl & _).
  unfold announce in *. rewrite <- Hfl.
  destruct (f_unannounced (g_flags s1)); [by eapply qrel_modify_node|simplify_eq; by exists s2].
Qed.
References
Lemma qrel_record_error o y s1 s2 s1' r :
  record_error o y s1 = Some (s1', r) -> queue_rel s1 s2 ->
  exists s2', record_error o y s2 = Some (s2', r) /\ queue_rel s1' s2'.
Qed
Proof.
  intros H Hrel. unfold record_error in *.
  eapply (qrel_bind_of H Hrel); [intros ?? Hm Hxy; by eapply qrel_get_owner|].
  intros x sb1 sb2 Hxy Hk. destruct (o_sink x).
  - eapply (qrel_bind_of Hk Hxy); [intros ?? Hm Hxy'; by eapply qrel_get_owner|].
    intros y0 sb1' sb2' Hxy' Hk'. eapply qrel_m_modify; [exact Hk'|exact Hxy'|qrel_upd_tac].
  - eapply qrel_m_modify; [exact Hk|exact Hxy|qrel_upd_tac].
Qed.
References
Lemma qrel_edges_opt u s1 s2 s1' r :
  edges_opt u s1 = Some (s1', r) -> queue_rel s1 s2 ->
  exists s2', edges_opt u s2 = Some (s2', r) /\ queue_rel s1' s2'.
Qed
Proof.
  intros H (Hw & Hp & H1 & H2). unfold edges_opt, pending in *.
  destruct s1, s2; simpl in *; simplify_eq.
  destruct (u g_edges0); [|discriminate]. simplify_eq.
  eexists; split; [reflexivity|by repeat split].
Qed.
References
Lemma qrel_hosted {A} n (b : M A) s1 s2 s1' r :
  hosted n b s1 = Some (s1', r) -> queue_rel s1 s2 ->
  (forall x1 x2 x1' a, b x1 = Some (x1', a) -> queue_rel x1 x2 ->
    exists x2', b x2 = Some (x2', a) /\ queue_rel x1' x2') ->
  exists s2', hosted n b s2 = Some (s2', r) /\ queue_rel s1' s2'.
Qed
Proof.
  intros H Hrel Hb. unfold hosted in *.
  destruct (queue_rel_fields _ _ Hrel) as (_&_&_&_&_&_&_&_&Hcur&_&Hcown&_).
  rewrite <- Hcur, <- Hcown.
  eapply (qrel_bind_of H Hrel); [intros ?? Hm Hxy; eapply qrel_m_modify; [exact Hm|exact Hxy|qrel_upd_tac]|].
  intros [] sb1 sb2 Hxy Hk.
  eapply (qrel_bind_of Hk Hxy); [intros ?? Hm' Hxy'; by eapply Hb|].
  intros a sb1' sb2' Hxy' Hk'.
  eapply (qrel_bind_of Hk' Hxy'); [intros ?? Hm Hxy''; eapply qrel_m_modify; [exact Hm|exact Hxy''|qrel_upd_tac]|].
  intros [] sb1'' sb2'' Hxy'' Hk''. by eapply qrel_m_return.
Qed.
References
Lemma qrel_untracked {A} (b : M A) s1 s2 s1' r :
  untracked b s1 = Some (s1', r) -> queue_rel s1 s2 ->
  (forall x1 x2 x1' a, b x1 = Some (x1', a) -> queue_rel x1 x2 ->
    exists x2', b x2 = Some (x2', a) /\ queue_rel x1' x2') ->
  exists s2', untracked b s2 = Some (s2', r) /\ queue_rel s1' s2'.
Qed
Proof.
  intros H Hrel Hb. unfold untracked in *.
  destruct (queue_rel_fields _ _ Hrel) as (_&_&_&_&_&_&_&_&Hcur&Hunt&_).
  rewrite <- Hcur, <- Hunt.
  eapply (qrel_bind_of H Hrel); [intros ?? Hm Hxy; eapply qrel_m_modify; [exact Hm|exact Hxy|qrel_upd_tac]|].
  intros [] sb1 sb2 Hxy Hk.
  eapply (qrel_bind_of Hk Hxy); [intros ?? Hm' Hxy'; by eapply Hb|].
  intros a sb1' sb2' Hxy' Hk'.
  eapply (qrel_bind_of Hk' Hxy'); [intros ?? Hm Hxy''; eapply qrel_m_modify; [exact Hm|exact Hxy''|qrel_upd_tac]|].
  intros [] sb1'' sb2'' Hxy'' Hk''. by eapply qrel_m_return.
Qed.
References
Lemma qrel_detached o b s1 s2 s1' r :
  detached o b s1 = Some (s1', r) -> queue_rel s1 s2 ->
  (forall x1 x2 x1' a, b x1 = Some (x1', a) -> queue_rel x1 x2 ->
    exists x2', b x2 = Some (x2', a) /\ queue_rel x1' x2') ->
  exists s2', detached o b s2 = Some (s2', r) /\ queue_rel s1' s2'.
Qed
Proof.
  intros H Hrel Hb. unfold detached in *.
  destruct (queue_rel_fields _ _ Hrel) as (_&_&_&_&_&_&_&_&Hcur&_&Hcown&_).
  rewrite <- Hcur, <- Hcown.
  eapply (qrel_bind_of H Hrel); [intros ?? Hm Hxy; eapply qrel_m_modify; [exact Hm|exact Hxy|qrel_upd_tac]|].
  intros [] sb1 sb2 Hxy Hk.
  eapply (qrel_bind_of Hk Hxy); [intros ?? Hm' Hxy'; by eapply Hb|].
  intros [] sb1' sb2' Hxy' Hk'.
  eapply qrel_m_modify; [exact Hk'|exact Hxy'|qrel_upd_tac].
Qed.
References
Local Ltac qrel_pure_tac :=
  try unfold add_node, put_owner, emit, edges, push_running, pop_running,
    push_checking, pop_checking, m_step in *;
  first
    [ by eapply qrel_m_return
    | by eapply qrel_get_node
    | by eapply qrel_modify_node
    | by eapply qrel_get_owner
    | by eapply qrel_current_owner
    | by eapply qrel_next_id
    | by eapply qrel_note_host_read
    | by eapply qrel_log_label
    | by eapply qrel_track
    | by eapply qrel_mark_reader
    | by eapply qrel_announce
    | by eapply qrel_unlink
    | by eapply qrel_record_error
    | by eapply qrel_edges_opt
    | by (eapply qrel_m_modify; [eassumption|eassumption|qrel_upd_tac])
    ].
References
Record b1_qrel_at (f : nat) : Prop := {
  B1Qexec : forall e c s1 s2 s1' r, b1_exec f e c s1 = Some (s1', r) -> queue_rel s1 s2 ->
    exists s2', b1_exec f e c s2 = Some (s2', r) /\ queue_rel s1' s2';
  B1Qread : forall n b s1 s2 s1' r, b1_read f n b s1 = Some (s1', r) -> queue_rel s1 s2 ->
    exists s2', b1_read f n b s2 = Some (s2', r) /\ queue_rel s1' s2';
  B1Qread_node : forall n s1 s2 s1' r, b1_read_node f n s1 = Some (s1', r) -> queue_rel s1 s2 ->
    exists s2', b1_read_node f n s2 = Some (s2', r) /\ queue_rel s1' s2';
  B1Qupdate : forall n s1 s2 s1' r, b1_update_if_necessary f n s1 = Some (s1', r) -> queue_rel s1 s2 ->
    exists s2', b1_update_if_necessary f n s2 = Some (s2', r) /\ queue_rel s1' s2';
  B1Qrop : forall n s1 s2 s1' r, b1_refresh_or_pull f n s1 = Some (s1', r) -> queue_rel s1 s2 ->
    exists s2', b1_refresh_or_pull f n s2 = Some (s2', r) /\ queue_rel s1' s2';
  B1Qpull : forall n s1 s2 s1' r, b1_pull f n s1 = Some (s1', r) -> queue_rel s1 s2 ->
    exists s2', b1_pull f n s2 = Some (s2', r) /\ queue_rel s1' s2';
  B1Qrefresh : forall n s1 s2 s1' r, b1_refresh f n s1 = Some (s1', r) -> queue_rel s1 s2 ->
    exists s2', b1_refresh f n s2 = Some (s2', r) /\ queue_rel s1' s2';
  B1Qwalk : forall n i s1 s2 s1' r, b1_resolve_walk f n i s1 = Some (s1', r) -> queue_rel s1 s2 ->
    exists s2', b1_resolve_walk f n i s2 = Some (s2', r) /\ queue_rel s1' s2';
  B1Qrecompute : forall n s1 s2 s1' r, b1_recompute f n s1 = Some (s1', r) -> queue_rel s1 s2 ->
    exists s2', b1_recompute f n s2 = Some (s2', r) /\ queue_rel s1' s2';
  B1Qrun_memo : forall n s1 s2 s1' r, b1_run_memo f n s1 = Some (s1', r) -> queue_rel s1 s2 ->
    exists s2', b1_run_memo f n s2 = Some (s2', r) /\ queue_rel s1' s2';
  B1Qexecute : forall n s1 s2 s1' r, b1_execute_effect f n s1 = Some (s1', r) -> queue_rel s1 s2 ->
    exists s2', b1_execute_effect f n s2 = Some (s2', r) /\ queue_rel s1' s2';
  B1Qrun_effect : forall n s1 s2 s1' r, b1_run_effect f n s1 = Some (s1', r) -> queue_rel s1 s2 ->
    exists s2', b1_run_effect f n s2 = Some (s2', r) /\ queue_rel s1' s2';
  B1Qbody : forall n s1 s2 s1' r, b1_run_effect_body f n s1 = Some (s1', r) -> queue_rel s1 s2 ->
    exists s2', b1_run_effect_body f n s2 = Some (s2', r) /\ queue_rel s1' s2';
  B1Qdischarge : forall o s1 s2 s1' r, b1_discharge f o s1 = Some (s1', r) -> queue_rel s1 s2 ->
    exists s2', b1_discharge f o s2 = Some (s2', r) /\ queue_rel s1' s2';
  B1Qretire : forall o s1 s2 s1' r, b1_retire f o s1 = Some (s1', r) -> queue_rel s1 s2 ->
    exists s2', b1_retire f o s2 = Some (s2', r) /\ queue_rel s1' s2';
  B1Qdispose_scope : forall o s1 s2 s1' r, b1_dispose_scope f o s1 = Some (s1', r) -> queue_rel s1 s2 ->
    exists s2', b1_dispose_scope f o s2 = Some (s2', r) /\ queue_rel s1' s2';
  B1Qteardown : forall o ls cs s1 s2 s1' r, b1_teardown f o ls cs s1 = Some (s1', r) -> queue_rel s1 s2 ->
    exists s2', b1_teardown f o ls cs s2 = Some (s2', r) /\ queue_rel s1' s2';
  B1Qcleanup : forall o b e s1 s2 s1' r, b1_run_cleanup f o b e s1 = Some (s1', r) -> queue_rel s1 s2 ->
    exists s2', b1_run_cleanup f o b e s2 = Some (s2', r) /\ queue_rel s1' s2';
  B1Qrelease : forall n s1 s2 s1' r, b1_release f n s1 = Some (s1', r) -> queue_rel s1 s2 ->
    exists s2', b1_release f n s2 = Some (s2', r) /\ queue_rel s1' s2';
  B1Qdispose : forall n s1 s2 s1' r, b1_dispose_node f n s1 = Some (s1', r) -> queue_rel s1 s2 ->
    exists s2', b1_dispose_node f n s2 = Some (s2', r) /\ queue_rel s1' s2';
  B1Qattach : forall n o s1 s2 s1' r, b1_attach f n o s1 = Some (s1', r) -> queue_rel s1 s2 ->
    exists s2', b1_attach f n o s2 = Some (s2', r) /\ queue_rel s1' s2';
  B1Qwrite : forall n v s1 s2 s1' r, b1_write_signal f n v s1 = Some (s1', r) -> queue_rel s1 s2 ->
    exists s2', b1_write_signal f n v s2 = Some (s2', r) /\ queue_rel s1' s2';
  B1Qrequest : forall s1 s2 s1' r, b1_request_flush f s1 = Some (s1', r) -> queue_rel s1 s2 ->
    exists s2', b1_request_flush f s2 = Some (s2', r) /\ queue_rel s1' s2';
  B1Qsettle : forall s1 s2 s1' r, b1_settle_flush f s1 = Some (s1', r) -> queue_rel s1 s2 ->
    exists s2', b1_settle_flush f s2 = Some (s2', r) /\ queue_rel s1' s2';
  B1Qflush : forall s1 s2 s1' r, b1_run_flush f s1 = Some (s1', r) -> queue_rel s1 s2 ->
    exists s2', b1_run_flush f s2 = Some (s2', r) /\ queue_rel s1' s2';
  B1Qdrain : forall s1 s2 s1' r, b1_drain f s1 = Some (s1', r) -> queue_rel s1 s2 ->
    exists s2', b1_drain f s2 = Some (s2', r) /\ queue_rel s1' s2'
}.
References
Lemma b1_qrel_all f : b1_qrel_at f.
Qed
Proof.
  induction f as [|f IH].
  - split; intros; discriminate.
  - split.
    + intros e c s1 s2 s1' r H Hrel. cbn [b1_exec] in H |- *.
      destruct c; repeat match goal with
      | H0 : context [on_node] |- _ => unfold on_node in H0 |- *
      | H0 : context [on_value] |- _ => unfold on_value in H0 |- *
      | H0 : context [match eval_expr ?e ?p ?x with _ => _ end] |- _ => destruct (eval_expr e p x) as [[|]|]; try discriminate
      | H0 : (if decide _ then _ else stuck) _ = Some _ |- _ => destruct (decide _); try discriminate
      end; try (eapply qrel_m_return; [exact H|exact Hrel]); try (simplify_eq; by exists s2).
      * by eapply (B1Qexec _ IH).
      * destruct (decide _); by eapply (B1Qexec _ IH).
      * eapply (qrel_bind_of H Hrel); [intros ????; by eapply (B1Qread _ IH)|intros o sb1 sb2 Hrel1 Hk].
        eapply (qrel_bind_of Hk Hrel1); [intros ????; qrel_pure_tac|intros [] sb1' sb2' Hrel2 Hk2].
        destruct o; [by eapply (B1Qexec _ IH)|by eapply qrel_m_return].
      * eapply (qrel_bind_of H Hrel); [intros ????; by eapply (B1Qread _ IH)|intros o sb1 sb2 Hrel1 Hk].
        eapply (qrel_bind_of Hk Hrel1); [intros ????; qrel_pure_tac|intros [] sb1' sb2' Hrel2 Hk2].
        destruct o; by eapply (B1Qexec _ IH).
      * eapply (qrel_bind_of H Hrel); [intros ????; by eapply (B1Qread _ IH)|intros o sb1 sb2 Hrel1 Hk].
        eapply (qrel_bind_of Hk Hrel1); [intros ????; qrel_pure_tac|intros [] sb1' sb2' Hrel2 Hk2].
        destruct o; [by eapply (B1Qexec _ IH)|by eapply qrel_m_return].
      * eapply (qrel_bind_of H Hrel); [intros ????; by eapply (B1Qwrite _ IH)|intros [] sb1 sb2 Hrel1 Hk; by eapply (B1Qexec _ IH)].
      * eapply (qrel_bind_of H Hrel); [intros ????; qrel_pure_tac|intros ?? sb2 Hrel1 Hk].
        eapply (qrel_bind_of Hk Hrel1); [intros ????; qrel_pure_tac|intros [] ? sb2' Hrel2 Hk2; by eapply (B1Qexec _ IH)].
      * eapply (qrel_bind_of H Hrel); [intros ????; qrel_pure_tac|intros ?? sb2 Hrel1 Hk].
        eapply (qrel_bind_of Hk Hrel1); [intros ????; qrel_pure_tac|intros [] ? sb2' Hrel2 Hk2].
        eapply (qrel_bind_of Hk2 Hrel2); [intros ????; qrel_pure_tac|intros [o|] ? sb2'' Hrel3 Hk3; [|by eapply qrel_m_return]].
        eapply (qrel_bind_of Hk3 Hrel3); [intros ????; by eapply (B1Qattach _ IH)|intros [] ? sb2''' Hrel4 Hk4; by eapply (B1Qexec _ IH)].
      * eapply (qrel_bind_of H Hrel); [intros ????; qrel_pure_tac|intros ?? sb2 Hrel1 Hk].
        eapply (qrel_bind_of Hk Hrel1); [intros ????; qrel_pure_tac|intros [] ? sb2' Hrel2 Hk2].
        eapply (qrel_bind_of Hk2 Hrel2); [intros ????; qrel_pure_tac|intros [o|] ? sb2'' Hrel3 Hk3; [|by eapply qrel_m_return]].
        eapply (qrel_bind_of Hk3 Hrel3); [intros ????; by eapply (B1Qattach _ IH)|intros [] ? sb2''' Hrel4 Hk4].
        eapply (qrel_bind_of Hk4 Hrel4); [intros ?? Hm Hxy; unfold m_step in *; destruct (mark_dirty f _ _) eqn:Em; simplify_eq; destruct (qrel_mark_dirty _ _ _ _ _ Hxy Em) as (y' & Em2 & Hxy'); rewrite Em2; by exists y'|intros [] ? sb2'''' Hrel5 Hk5].
        eapply (qrel_bind_of Hk5 Hrel5); [intros ????; by eapply (B1Qrequest _ IH)|intros [] ? sb2''''' Hrel6 Hk6; by eapply (B1Qexec _ IH)].
      * eapply (qrel_bind_of H Hrel); [intros ????; qrel_pure_tac|intros [o|] ? sb2 Hrel1 Hk; [|by eapply qrel_m_return]].
        eapply (qrel_bind_of Hk Hrel1); [intros ????; qrel_pure_tac|intros x ? sb2' Hrel2 Hk2].
        eapply (qrel_bind_of Hk2 Hrel2); [intros ????; qrel_pure_tac|intros [] ? sb2'' Hrel3 Hk3].
        destruct (o_disposed x).
        -- eapply (qrel_bind_of Hk3 Hrel3); [|intros [] ? sb2''' Hrel4 Hk4; by eapply (B1Qexec _ IH)].
           intros s4 [] Hm Hxy. eapply (qrel_bind_of Hm Hxy); [|intros [] ? sb2'''' Hrel5 Hreq; by eapply (B1Qrequest _ IH)].
           intros s5 [] Hd Hxy'. by eapply qrel_detached; [exact Hd|exact Hxy'|intros ????; by eapply (B1Qcleanup _ IH)].
        -- eapply (qrel_bind_of Hk3 Hrel3); [intros ????; qrel_pure_tac|intros [] ? sb2''' Hrel4 Hk4; by eapply (B1Qexec _ IH)].
      * eapply (qrel_bind_of H Hrel); [intros ????; qrel_pure_tac|intros d ? sb2 Hrel1 Hk].
        destruct (n_kind d); [discriminate| |];
          (eapply (qrel_bind_of Hk Hrel1); [intros ????; by eapply (B1Qdispose _ IH)|intros [] ? sb2' Hrel2 Hk2];
           eapply (qrel_bind_of Hk2 Hrel2); [intros ????; destruct (n_disposed d); qrel_pure_tac|intros [] ? sb2'' Hrel3 Hk3; by eapply (B1Qexec _ IH)]).
      * eapply (qrel_bind_of H Hrel); [intros ????; qrel_pure_tac|intros [] ? sb2 Hrel1 Hk].
        eapply (qrel_bind_of Hk Hrel1); [intros ????; by eapply (B1Qexec _ IH)|intros r0 ? sb2' Hrel2 Hk2].
        eapply (qrel_bind_of Hk2 Hrel2); [intros ????; qrel_pure_tac|intros [] ? sb2'' Hrel3 Hk3].
        destruct r0 as [|]; [|by eapply qrel_m_return].
        eapply (qrel_bind_of Hk3 Hrel3); [intros ????; by eapply (B1Qrequest _ IH)|intros [] ? sb2''' Hrel4 Hk4; by eapply (B1Qexec _ IH)].
      * eapply (qrel_bind_of H Hrel); [intros ????; qrel_pure_tac|intros ?? sb2 Hrel1 Hk].
        eapply (qrel_bind_of Hk Hrel1); [intros ????; qrel_pure_tac|intros [] ? sb2' Hrel2 Hk2; by eapply (B1Qexec _ IH)].
    + intros n b s1 s2 s1' r H Hrel. cbn [b1_read] in H |- *.
      destruct b; [by eapply (B1Qread_node _ IH)|eapply qrel_untracked; [exact H|exact Hrel|intros ????; by eapply (B1Qread_node _ IH)]].
    + intros n s1 s2 s1' r H Hrel. cbn [b1_read_node] in H |- *.
      eapply (qrel_bind_of H Hrel); [intros ????; qrel_pure_tac|intros d ? sb2 Hrel1 Hk].
      destruct (n_kind d); [| |discriminate].
      * eapply (qrel_bind_of Hk Hrel1); [intros ????; qrel_pure_tac|intros [] ? sb2' Hrel2 Hk2; by eapply qrel_m_return].
      * eapply (qrel_bind_of Hk Hrel1); [|intros d' ? sb2' Hrel2 Hk2; destruct (n_status d'); by eapply qrel_m_return].
        intros sb1' d' Hm Hxy. destruct (stale_live d).
        -- eapply (qrel_bind_of Hm Hxy); [intros ????; by eapply (B1Qrop _ IH)|intros d0 ? sb2' Hrel2 Hk2].
           eapply (qrel_bind_of Hk2 Hrel2); [|intros [] ? sb2'' Hrel3 Hk3; by eapply qrel_m_return].
           intros sb1'' [] Hm2 Hxy'.
           eapply (qrel_bind_of Hm2 Hxy'); [intros ????; qrel_pure_tac|intros [] ? sb2'' Hrel3 Hk3].
           eapply (qrel_bind_of Hk3 Hrel3); [intros ????; qrel_pure_tac|intros d'' ? sb2''' Hrel4 Hk4].
           destruct (stale_live d''); [qrel_pure_tac|by eapply qrel_m_return].
        -- eapply (qrel_bind_of Hm Hxy); [intros ????; qrel_pure_tac|intros [] ? sb2' Hrel2 Hk2; qrel_pure_tac].
    + intros n s1 s2 s1' r H Hrel. cbn [b1_update_if_necessary] in H |- *.
      eapply (qrel_bind_of H Hrel); [intros ????; qrel_pure_tac|intros d ? sb2 Hrel1 Hk].
      destruct (n_kind d); [by eapply qrel_m_return| |by eapply qrel_m_return].
      destruct (stale_live d); [|by eapply qrel_m_return].
      eapply (qrel_bind_of Hk Hrel1); [intros ????; by eapply (B1Qrop _ IH)|intros ?? sb2' Hrel2 Hk2; by eapply qrel_m_return].
    + intros n s1 s2 s1' r H Hrel. cbn [b1_refresh_or_pull] in H |- *. rewrite !bind_get in H |- *.
      destruct (queue_rel_fields _ _ Hrel) as (_&_&_&_&_&_&_&_&_&_&_&_&_&_&_&Hdef&_).
      rewrite <- Hdef.
      destruct (deferring_b1 s1); [|by eapply (B1Qpull _ IH)].
      eapply (qrel_bind_of H Hrel); [intros ????; by eapply (B1Qrefresh _ IH)|intros [] ? sb2 Hrel1 Hk; qrel_pure_tac].
    + intros n s1 s2 s1' r H Hrel. cbn [b1_pull] in H |- *.
      eapply (qrel_bind_of H Hrel); [intros ????; qrel_pure_tac|intros [] ? sb2 Hrel1 Hk].
      eapply (qrel_bind_of Hk Hrel1); [intros ????; by eapply (B1Qrefresh _ IH)|intros [] ? sb2' Hrel2 Hk2].
      eapply (qrel_bind_of Hk2 Hrel2); [intros ????; qrel_pure_tac|intros d ? sb2'' Hrel3 Hk3].
      eapply (qrel_bind_of Hk3 Hrel3); [intros ????; qrel_pure_tac|intros [] ? sb2''' Hrel4 Hk4].
      rewrite !bind_get in Hk4 |- *.
      destruct (queue_rel_fields _ _ Hrel4) as (_&_&_&_&_&Hfo&_&Hpd&_).
      rewrite <- Hfo, <- Hpd.
      eapply (qrel_bind_of Hk4 Hrel4); [|intros [] ? sb2'''' Hrel5 Hk5; by eapply qrel_m_return].
      intros s5 ? Hm Hxy. destruct (bool_decide _ && _); [by eapply (B1Qsettle _ IH)|by eapply qrel_m_return].
    + intros n s1 s2 s1' r H Hrel. cbn [b1_refresh] in H |- *. rewrite !bind_get in H |- *.
      destruct (queue_rel_fields _ _ Hrel) as (_&_&_&_&_&_&_&_&_&_&_&Hrun&Hchk&_).
      rewrite <- Hchk, <- Hrun.
      destruct (decide (n ∈ g_checking s1)).
      { unfold m_step in *. destruct (mark_dirty f n s1) eqn:Em; simplify_eq.
        destruct (qrel_mark_dirty _ _ _ _ _ Hrel Em) as (y' & Em2 & Hxy'). rewrite Em2. by exists y'. }
      destruct (decide (n ∈ g_running s1)); [by eapply qrel_m_return|].
      eapply (qrel_bind_of H Hrel); [intros ????; qrel_pure_tac|intros d ? sb2 Hrel1 Hk].
      eapply (qrel_bind_of Hk Hrel1); [|intros [] ? sb2' Hrel2 Hk2; eapply (qrel_bind_of Hk2 Hrel2); [intros ????; qrel_pure_tac|intros d' ? sb2'' Hrel3 Hk3; destruct (decide (n_freshness d' = Dirty)); [by eapply (B1Qrecompute _ IH)|by eapply qrel_m_return]]].
      intros s2' [] Hm Hxy. destruct (decide (n_freshness d = Check)); [|by eapply qrel_m_return].
      eapply (qrel_bind_of Hm Hxy); [intros ????; qrel_pure_tac|intros [] ? sb2' Hrel2 Hk2].
      eapply (qrel_bind_of Hk2 Hrel2); [intros ????; by eapply (B1Qwalk _ IH)|intros [] ? sb2'' Hrel3 Hk3].
      eapply (qrel_bind_of Hk3 Hrel3); [intros ????; qrel_pure_tac|intros [] ? sb2''' Hrel4 Hk4].
      eapply (qrel_bind_of Hk4 Hrel4); [intros ????; qrel_pure_tac|intros d' ? sb2'''' Hrel5 Hk5].
      destruct (decide (n_freshness d' = Check)); [|by eapply qrel_m_return].
      eapply (qrel_bind_of Hk5 Hrel5); [intros ????; qrel_pure_tac|intros [] ? sb2''''' Hrel6 Hk6; qrel_pure_tac].
    + intros n i s1 s2 s1' r H Hrel. cbn [b1_resolve_walk] in H |- *.
      eapply (qrel_bind_of H Hrel); [intros ????; qrel_pure_tac|intros d sb1 sb2 Hrel1 Hk].
      rewrite !bind_get in Hk |- *.
      destruct (queue_rel_fields _ _ Hrel1) as (Hn1&He1&_). rewrite <- He1.
      destruct (decide (n_freshness d = Check)); [|by eapply qrel_m_return].
      destruct (source_slots (sources_of (g_edges sb1) n) !! i) as [[src _]|].
      * eapply (qrel_bind_of Hk Hrel1); [intros ????; by eapply (B1Qupdate _ IH)|intros [] ? sb2' Hrel2 Hk2].
        eapply (qrel_bind_of Hk2 Hrel2); [intros ????; qrel_pure_tac|intros d' ? sb2'' Hrel3 Hk3].
        eapply (qrel_bind_of Hk3 Hrel3); [intros ????; qrel_pure_tac|intros d_src ? sb2''' Hrel4 Hk4].
        rewrite !bind_get in Hk4 |- *.
        destruct (queue_rel_fields _ _ Hrel4) as (_&_&_&_&_&_&_&_&_&_&_&Hrun&Hchk&_).
        rewrite <- Hrun, <- Hchk.
        eapply (qrel_bind_of Hk4 Hrel4); [|intros [] ? sb2'''' Hrel5 Hk5; by eapply (B1Qwalk _ IH)].
        intros s5 [] Hm Hxy. destruct (decide _); [|by eapply qrel_m_return].
        unfold m_step in *. destruct (mark_dirty f n _) eqn:Em; simplify_eq.
        destruct (qrel_mark_dirty _ _ _ _ _ Hxy Em) as (y' & Em2 & Hxy'). rewrite Em2. by exists y'.
      * assert (Hsh : slot_holds stale_live sb1 n = slot_holds stale_live sb2 n)
          by (unfold slot_holds; by rewrite Hn1, He1).
        rewrite <- Hsh. destruct (slot_holds stale_live sb1 n); [by eapply (B1Qwalk _ IH)|by eapply qrel_m_return].
    + intros n s1 s2 s1' r H Hrel. cbn [b1_recompute] in H |- *.
      eapply (qrel_bind_of H Hrel); [intros ????; qrel_pure_tac|intros d ? sb2 Hrel1 Hk].
      destruct (n_scope d) as [o|]; [|by eapply (B1Qrun_memo _ IH)].
      eapply (qrel_bind_of Hk Hrel1); [intros ????; qrel_pure_tac|intros [] ? sb2' Hrel2 Hk2].
      eapply (qrel_bind_of Hk2 Hrel2); [intros ????; qrel_pure_tac|intros [] ? sb2'' Hrel3 Hk3].
      eapply (qrel_bind_of Hk3 Hrel3); [intros ????; by eapply (B1Qdischarge _ IH)|intros [] ? sb2''' Hrel4 Hk4].
      eapply (qrel_bind_of Hk4 Hrel4); [intros ????; qrel_pure_tac|intros d' ? sb2'''' Hrel5 Hk5].
      destruct (n_disposed d').
      * destruct (decide (n_freshness d' = Clean)); [qrel_pure_tac|by eapply qrel_m_return].
      * destruct (decide (n_runs d' = n_runs d)); [by eapply (B1Qrun_memo _ IH)|].
        destruct (decide (n_freshness d' = Dirty)); [by eapply (B1Qrecompute _ IH)|by eapply qrel_m_return].
    + intros n s1 s2 s1' r H Hrel. cbn [b1_run_memo] in H |- *.
      eapply (qrel_bind_of H Hrel); [intros ????; qrel_pure_tac|intros d ? sb2 Hrel1 Hk].
      eapply (qrel_bind_of Hk Hrel1); [intros ????; qrel_pure_tac|intros [] ? sb2' Hrel2 Hk2].
      eapply (qrel_bind_of Hk2 Hrel2); [intros ????; qrel_pure_tac|intros [] ? sb2'' Hrel3 Hk3].
      eapply (qrel_bind_of Hk3 Hrel3); [intros ????; qrel_pure_tac|intros [] ? sb2''' Hrel4 Hk4].
      eapply (qrel_bind_of Hk4 Hrel4); [intros ????; qrel_pure_tac|intros [] ? sb2'''' Hrel5 Hk5].
      eapply (qrel_bind_of Hk5 Hrel5); [intros ?? Hm Hxy; eapply qrel_hosted; [exact Hm|exact Hxy|intros ????; by eapply (B1Qexec _ IH)]|intros r0 ? sb2''''' Hrel6 Hk6].
      eapply (qrel_bind_of Hk6 Hrel6); [intros ????; qrel_pure_tac|intros d1 ? sb2_7 Hrel7 Hk7].
      eapply (qrel_bind_of Hk7 Hrel7); [intros ????; destruct (if n_violated d1 then Fail pure_scope_failure else r0); qrel_pure_tac|intros [] ? sb2_8 Hrel8 Hk8].
      eapply (qrel_bind_of Hk8 Hrel8); [intros ????; qrel_pure_tac|intros [] ? sb2_9 Hrel9 Hk9].
      eapply (qrel_bind_of Hk9 Hrel9); [intros ????; qrel_pure_tac|intros [] ? sb2_10 Hrel10 Hk10].
      eapply (qrel_bind_of Hk10 Hrel10); [intros ????; destruct (if n_violated d1 then Fail pure_scope_failure else r0); qrel_pure_tac|intros [] ? sb2_11 Hrel11 Hk11].
      eapply (qrel_bind_of Hk11 Hrel11); [intros ????; qrel_pure_tac|intros d2 ? sb2_12 Hrel12 Hk12].
      eapply (qrel_bind_of Hk12 Hrel12); [intros ?? Hm Hxy; destruct (decide _); [by eapply qrel_m_return|destruct (n_comparer d2); [by eapply qrel_m_return|by eapply qrel_m_return|eapply (qrel_bind_of Hm Hxy); [intros ????; qrel_pure_tac|intros [] ? sb2_x Hxy' Hk'; by eapply qrel_m_return]]]|intros moved ? sb2_13 Hrel13 Hk13].
      rewrite !bind_get in Hk13 |- *.
      destruct (queue_rel_fields _ _ Hrel13) as (_&_&_&_&_&_&_&_&Hcur&_).
      rewrite <- Hcur.
      eapply (qrel_bind_of Hk13 Hrel13); [intros ?? Hm Hxy; eapply qrel_m_modify; [exact Hm|exact Hxy|qrel_upd_tac]|intros [] ? sb2_14 Hrel14 Hk14].
      eapply (qrel_bind_of Hk14 Hrel14); [|intros [] ? sb2_15 Hrel15 Hk15; eapply (qrel_bind_of Hk15 Hrel15); [intros ????; qrel_pure_tac|intros d3 ? sb2_16 Hrel16 Hk16; destruct (decide (n_freshness d3 = Clean)); [by eapply qrel_m_return|qrel_pure_tac]]].
      intros s15 [] Hm Hxy. destruct moved; [|by eapply qrel_m_return].
      eapply (qrel_bind_of Hm Hxy); [intros ?? Hm' Hxy'; unfold m_step in *; destruct (notify_dirty_except f n _ _) eqn:Em; simplify_eq; destruct (qrel_notify_dirty_except _ _ _ _ _ _ Hxy' Em) as (y' & Em2 & Hxy''); rewrite Em2; by exists y'|].
      intros [] sb16 sb2_16 Hrel16 Hk16. rewrite !bind_get in Hk16 |- *.
      destruct (queue_rel_fields _ _ Hrel16) as (_&_&_&_&Hfl&_&Hbd&_&_&_&_&_&_&Hlq&_).
      rewrite <- Hbd, <- Hfl, <- Hlq.
      destruct (bool_decide _ && _ && _); [qrel_pure_tac|by eapply qrel_m_return].
    + intros n s1 s2 s1' r H Hrel. cbn [b1_execute_effect] in H |- *.
      eapply (qrel_bind_of H Hrel); [intros ????; qrel_pure_tac|intros d ? sb2 Hrel1 Hk].
      destruct (n_disposed d); [qrel_pure_tac|].
      eapply (qrel_bind_of Hk Hrel1); [|intros [] ? sb2' Hrel2 Hk2].
      * intros s2' [] Hm Hxy. destruct (decide (n_freshness d = Check)); [|by eapply qrel_m_return].
        eapply (qrel_bind_of Hm Hxy); [intros ????; by eapply (B1Qwalk _ IH)|intros [] ? sb2' Hrel2 Hk2].
        eapply (qrel_bind_of Hk2 Hrel2); [intros ????; qrel_pure_tac|intros d' ? sb2'' Hrel3 Hk3].
        destruct (decide (n_freshness d' = Check)); [|by eapply qrel_m_return].
        eapply (qrel_bind_of Hk3 Hrel3); [intros ????; qrel_pure_tac|intros [] ? sb2''' Hrel4 Hk4; qrel_pure_tac].
      * eapply (qrel_bind_of Hk2 Hrel2); [intros ????; qrel_pure_tac|intros d' ? sb2'' Hrel3 Hk3].
        eapply (qrel_bind_of Hk3 Hrel3); [intros ????; qrel_pure_tac|intros [] ? sb2''' Hrel4 Hk4; destruct (decide (n_freshness d' = Dirty)); [by eapply (B1Qrun_effect _ IH)|by eapply qrel_m_return]].
    + intros n s1 s2 s1' r H Hrel. cbn [b1_run_effect] in H |- *.
      eapply (qrel_bind_of H Hrel); [intros ????; qrel_pure_tac|intros [] ? sb2 Hrel1 Hk].
      eapply (qrel_bind_of Hk Hrel1); [intros ????; qrel_pure_tac|intros [] ? sb2' Hrel2 Hk2].
      eapply (qrel_bind_of Hk2 Hrel2); [intros ????; qrel_pure_tac|intros [] ? sb2'' Hrel3 Hk3].
      eapply (qrel_bind_of Hk3 Hrel3); [intros ????; qrel_pure_tac|intros d ? sb2''' Hrel4 Hk4].
      eapply (qrel_bind_of Hk4 Hrel4); [|intros [] ? sb2'''' Hrel5 Hk5].
      * intros s5 [] Hm Hxy. destruct (n_scope d) as [o|]; [|by eapply qrel_m_return].
        eapply (qrel_bind_of Hm Hxy); [intros ????; qrel_pure_tac|intros [] ? sb2'''' Hrel5 Hk5].
        eapply (qrel_bind_of Hk5 Hrel5); [intros ????; by eapply (B1Qdischarge _ IH)|intros [] ? sb2''''' Hrel6 Hk6; qrel_pure_tac].
      * eapply (qrel_bind_of Hk5 Hrel5); [intros ????; qrel_pure_tac|intros d' ? sb2''''' Hrel6 Hk6].
        eapply (qrel_bind_of Hk6 Hrel6); [intros s7 [] Hm Hxy; destruct (n_disposed d'); [by eapply qrel_m_return|by eapply (B1Qbody _ IH)]|intros [] ? sb2_7 Hrel7 Hk7; qrel_pure_tac].
    + intros n s1 s2 s1' r H Hrel. cbn [b1_run_effect_body] in H |- *.
      eapply (qrel_bind_of H Hrel); [intros ????; qrel_pure_tac|intros d ? sb2 Hrel1 Hk].
      eapply (qrel_bind_of Hk Hrel1); [intros ????; qrel_pure_tac|intros [] ? sb2' Hrel2 Hk2].
      eapply (qrel_bind_of Hk2 Hrel2); [intros ????; qrel_pure_tac|intros [] ? sb2'' Hrel3 Hk3].
      eapply (qrel_bind_of Hk3 Hrel3); [intros ?? Hm Hxy; eapply qrel_hosted; [exact Hm|exact Hxy|intros ????; by eapply (B1Qexec _ IH)]|intros r0 ? sb2''' Hrel4 Hk4].
      eapply (qrel_bind_of Hk4 Hrel4); [intros ????; qrel_pure_tac|intros d' ? sb2'''' Hrel5 Hk5].
      eapply (qrel_bind_of Hk5 Hrel5); [intros ????; qrel_pure_tac|intros [] ? sb2''''' Hrel6 Hk6; destruct r0; [by eapply qrel_m_return|eapply (qrel_bind_of Hk6 Hrel6); [intros ????; qrel_pure_tac|intros [] ? sb2_7 Hrel7 Hk7; qrel_pure_tac]]].
    + intros o s1 s2 s1' r H Hrel. cbn [b1_discharge] in H |- *.
      eapply (qrel_bind_of H Hrel); [intros ?? Hm Hxy; eapply qrel_detached; [exact Hm|exact Hxy|intros ????; by eapply (B1Qdispose_scope _ IH)]|].
      intros [] sb1 sb2 Hrel1 Hk. rewrite !bind_get in Hk |- *.
      destruct (queue_rel_fields _ _ Hrel1) as (_&_&_&_&Hfl&_&Hbd&_&_&_&_&_&_&Hlq&_).
      rewrite <- Hbd, <- Hfl, <- Hlq.
      destruct (bool_decide _ && _ && _); [qrel_pure_tac|by eapply qrel_m_return].
    + intros o s1 s2 s1' r H Hrel. cbn [b1_retire] in H |- *.
      eapply (qrel_bind_of H Hrel); [|intros [] sb1 sb2 Hrel1 Hk; rewrite !bind_get in Hk |- *; destruct (queue_rel_fields _ _ Hrel1) as (_&_&_&_&_&_&_&_&_&_&_&_&_&Hlq&_); rewrite <- Hlq; destruct (live_queue sb1); [by eapply (B1Qrequest _ IH)|by eapply qrel_m_return]].
      intros s1'0 [] Hm Hxy. eapply qrel_detached; [exact Hm|exact Hxy|].
      intros x0 x2 x1' r2 Hm0 Hxy0.
      eapply (qrel_bind_of Hm0 Hxy0); [intros ????; qrel_pure_tac|intros x ? sb2 Hrel2 Hk2].
      destruct (o_disposed x); [by eapply qrel_m_return|eapply (qrel_bind_of Hk2 Hrel2); [intros ????; qrel_pure_tac|intros [] ? sb2' Hrel3 Hk3; by eapply (B1Qdispose_scope _ IH)]].
    + intros o s1 s2 s1' r H Hrel. cbn [b1_dispose_scope] in H |- *.
      eapply (qrel_bind_of H Hrel); [intros ????; qrel_pure_tac|intros x ? sb2 Hrel1 Hk].
      eapply (qrel_bind_of Hk Hrel1); [intros ????; qrel_pure_tac|intros [] ? sb2' Hrel2 Hk2; by eapply (B1Qteardown _ IH)].
    + intros o ls cs s1 s2 s1' r H Hrel. cbn [b1_teardown] in H |- *.
      destruct ls as [|l ls'], cs as [|[[b e] a] cs']; [by eapply qrel_m_return|..].
      * eapply (qrel_bind_of H Hrel); [intros ????; by eapply (B1Qcleanup _ IH)|intros [] ? sb2 Hrel1 Hk; by eapply (B1Qteardown _ IH)].
      * eapply (qrel_bind_of H Hrel); [intros ????; by eapply (B1Qrelease _ IH)|intros [] ? sb2 Hrel1 Hk; by eapply (B1Qteardown _ IH)].
      * destruct (decide (a < l_acquired l)).
        -- eapply (qrel_bind_of H Hrel); [intros ????; by eapply (B1Qrelease _ IH)|intros [] ? sb2 Hrel1 Hk; by eapply (B1Qteardown _ IH)].
        -- eapply (qrel_bind_of H Hrel); [intros ????; by eapply (B1Qcleanup _ IH)|intros [] ? sb2 Hrel1 Hk; by eapply (B1Qteardown _ IH)].
    + intros o b e s1 s2 s1' r H Hrel. cbn [b1_run_cleanup] in H |- *.
      eapply (qrel_bind_of H Hrel); [intros ????; qrel_pure_tac|intros x ? sb2 Hrel1 Hk].
      eapply (qrel_bind_of Hk Hrel1); [intros ????; qrel_pure_tac|intros [] ? sb2' Hrel2 Hk2].
      eapply (qrel_bind_of Hk2 Hrel2); [intros ????; by eapply (B1Qexec _ IH)|intros r0 ? sb2'' Hrel3 Hk3].
      destruct r0; [by eapply qrel_m_return|].
      eapply (qrel_bind_of Hk3 Hrel3); [intros ????; qrel_pure_tac|intros [] ? sb2''' Hrel4 Hk4; qrel_pure_tac].
    + intros n s1 s2 s1' r H Hrel. cbn [b1_release] in H |- *.
      eapply (qrel_bind_of H Hrel); [intros ????; qrel_pure_tac|intros [] ? sb2 Hrel1 Hk; by eapply (B1Qdispose _ IH)].
    + intros n s1 s2 s1' r H Hrel. cbn [b1_dispose_node] in H |- *.
      eapply (qrel_bind_of H Hrel); [intros ????; qrel_pure_tac|intros d ? sb2 Hrel1 Hk].
      destruct (n_disposed d); [by eapply qrel_m_return|].
      eapply (qrel_bind_of Hk Hrel1); [intros ????; qrel_pure_tac|intros [] ? sb2' Hrel2 Hk2].
      eapply (qrel_bind_of Hk2 Hrel2); [intros ????; destruct (n_link d); qrel_pure_tac|intros [] ? sb2'' Hrel3 Hk3].
      eapply (qrel_bind_of Hk3 Hrel3); [intros ????; qrel_pure_tac|intros [] ? sb2''' Hrel4 Hk4].
      destruct (n_scope d); [by eapply (B1Qretire _ IH)|by eapply qrel_m_return].
    + intros n o s1 s2 s1' r H Hrel. cbn [b1_attach] in H |- *.
      eapply (qrel_bind_of H Hrel); [intros ????; qrel_pure_tac|intros x ? sb2 Hrel1 Hk].
      destruct (o_disposed x).
      * eapply (qrel_bind_of Hk Hrel1); [intros ????; by eapply (B1Qrelease _ IH)|intros [] ? sb2' Hrel2 Hk2; qrel_pure_tac].
      * eapply (qrel_bind_of Hk Hrel1); [intros ????; qrel_pure_tac|intros [] ? sb2' Hrel2 Hk2; qrel_pure_tac].
    + intros n v s1 s2 s1' r H Hrel. cbn [b1_write_signal] in H |- *.
      eapply (qrel_bind_of H Hrel); [intros ????; qrel_pure_tac|intros d ? sb2 Hrel1 Hk].
      destruct (n_kind d); [|discriminate|discriminate].
      destruct (decide (n_value d = v)); [by eapply qrel_m_return|].
      eapply (qrel_bind_of Hk Hrel1); [intros ????; qrel_pure_tac|intros [] ? sb2' Hrel2 Hk2].
      eapply (qrel_bind_of Hk2 Hrel2); [intros ????; qrel_pure_tac|intros [] ? sb2'' Hrel3 Hk3].
      eapply (qrel_bind_of Hk3 Hrel3); [intros ?? Hm Hxy; unfold m_step in *; destruct (notify_dirty f n _) eqn:Em; simplify_eq; destruct (qrel_notify_dirty _ _ _ _ _ Hxy Em) as (y' & Em2 & Hxy'); rewrite Em2; by exists y'|intros [] ? sb2''' Hrel4 Hk4; by eapply (B1Qrequest _ IH)].
    + intros s1 s2 s1' r H Hrel. cbn [b1_request_flush] in H |- *. rewrite !bind_get in H |- *.
      destruct (queue_rel_fields _ _ Hrel) as (_&_&_&_&Hfl&_&Hbd&_&_&_&_&_&_&Hlq&Hob&_).
      rewrite <- Hlq, <- Hbd, <- Hob, <- Hfl.
      destruct (live_queue s1 && bool_decide (g_batch_depth s1 = 0)); [|by eapply qrel_m_return].
      destruct (outside_bodies s1); [by eapply (B1Qflush _ IH)|].
      destruct (g_flushing s1); [by eapply qrel_m_return|qrel_pure_tac].
    + intros s1 s2 s1' r H Hrel. cbn [b1_settle_flush] in H |- *. rewrite !bind_get in H |- *.
      destruct (queue_rel_fields _ _ Hrel) as (_&_&_&_&_&Hfo&Hbd&_&_&_&_&_&_&_&Hob&_).
      rewrite <- Hfo, <- Hbd, <- Hob.
      destruct (g_flush_owed s1 && bool_decide (g_batch_depth s1 = 0) && outside_bodies s1); [by eapply (B1Qflush _ IH)|by eapply qrel_m_return].
    + intros s1 s2 s1' r H Hrel. cbn [b1_run_flush] in H |- *. rewrite !bind_get in H |- *.
      destruct (queue_rel_fields _ _ Hrel) as (_&_&_&_&Hfl&_). rewrite <- Hfl.
      destruct (g_flushing s1); [by eapply qrel_m_return|].
      eapply (qrel_bind_of H Hrel); [intros ????; qrel_pure_tac|intros [] ? sb2 Hrel1 Hk].
      eapply (qrel_bind_of Hk Hrel1); [intros ????; by eapply (B1Qdrain _ IH)|intros [] ? sb2' Hrel2 Hk2; qrel_pure_tac].
    + intros s1 s2 s1' r H Hrel. cbn [b1_drain] in H |- *. rewrite !bind_get in H |- *.
      destruct (queue_rel_fields _ _ Hrel) as (_&_&_&_&_&_&_&_&_&_&_&_&_&_&_&_&Hhd).
      rewrite <- Hhd.
      destruct (g_queue s1 !! g_queue_head s1) as [n|] eqn:En.
      * eapply bind_inv in H as (sb1 & [] & Em & Hk). unfold m_modify in Em; simplify_eq.
        assert (Hrel1 : queue_rel (with_queue_head (S (g_queue_head s1)) s1)
                                  (with_queue_head (S (g_queue_head s2)) s2))
          by by eapply queue_rel_pop.
        eapply bind_inv in Hk as (sb1' & [] & Hexe & Hdr).
        destruct (B1Qexecute _ IH _ _ _ _ _ Hexe Hrel1) as (sb2' & Hexe2 & Hrel2).
        destruct (B1Qdrain _ IH _ _ _ _ Hdr Hrel2) as (s2' & Hdr2 & Hrel3).
        exists s2'; split; [|exact Hrel3].
        unfold m_bind at 1, m_modify at 1; simpl.
        unfold m_bind at 1; rewrite Hexe2; simpl; exact Hdr2.
      * unfold m_modify in *. simplify_eq.
        eexists; split; [reflexivity|].
        rewrite <- (queue_rel_reset_eq _ _ Hrel).
        apply queue_rel_refl. simpl. lia.
Qed.
References
Lemma b1_drain_resets f s s' :
  b1_drain f s = Some (s', tt) ->
  g_queue s' = [] /\ g_queue_head s' = 0.
Qed
Proof.
  revert s s'. induction f as [|f IH]; intros s s' H; [cbn in H; discriminate|].
  cbn [b1_drain] in H. rewrite bind_get in H.
  destruct (g_queue s !! g_queue_head s) as [n|].
  - eapply bind_inv in H as (s1 & [] & _ & H1).
    eapply bind_inv in H1 as (s2 & [] & _ & H2).
    by eapply IH.
  - unfold m_modify in H. simplify_eq. split; reflexivity.
Qed.
References
Lemma drain_resets f s s' :
  drain f s = Some (s', tt) ->
  g_queue s' = [] /\ g_queue_head s' = 0.
Qed
Proof.
  revert s s'. induction f as [|f IH]; intros s s' H; [cbn in H; discriminate|].
  cbn [drain] in H. rewrite bind_get in H.
  destruct (g_queue s !! g_queue_head s) as [n|].
  - eapply bind_inv in H as (s1 & [] & _ & H1).
    eapply bind_inv in H1 as (s2 & [] & _ & H2).
    by eapply IH.
  - unfold m_modify in H. simplify_eq. split; reflexivity.
Qed.
References
Lemma b1_write_signal_eq n v s s' :
  (exists f, write_signal f n v s = Some (s', tt)) <->
  (exists fb, b1_write_signal fb n v s = Some (s', tt)).
Qed
Proof.
  split.
  - intros [f Hf].
    destruct (Fwrite1 _ (b1_fwd_all f) n v s s s' tt (b1_state_rel_refl s) Hf) as (fb & sb' & Efb & Hrel).
    destruct (engine_depths_kept f) as [_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ Dwr _ _ _ _].
    destruct (b1_depths_kept fb) as [_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ B1Dwr _ _ _ _].
    pose proof (Dwr n v s s' tt Hf) as [Hp1 _].
    pose proof (B1Dwr n v s sb' tt Efb) as [Hp2 _].
    assert (s' = sb') by (apply b1_state_rel_eq; [exact Hrel|congruence]).
    subst sb'. by exists fb.
  - intros [fb Hfb].
    destruct (Bwrite1 _ (b1_bwd_all fb) n v s s s' tt (b1_state_rel_refl s) Hfb) as (f & s0' & Ef & Hrel).
    destruct (engine_depths_kept f) as [_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ Dwr _ _ _ _].
    destruct (b1_depths_kept fb) as [_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ B1Dwr _ _ _ _].
    pose proof (Dwr n v s s0' tt Ef) as [Hp1 _].
    pose proof (B1Dwr n v s s' tt Hfb) as [Hp2 _].
    assert (s0' = s') by (apply b1_state_rel_eq; [exact Hrel|congruence]).
    subst s0'. by exists f.
Qed.
References
Lemma b1_run_effect_eq n s s' :
  (exists f, run_effect f n s = Some (s', tt)) <->
  (exists fb, b1_run_effect fb n s = Some (s', tt)).
Qed
Proof.
  split.
  - intros [f Hf].
    destruct (Frun_effect1 _ (b1_fwd_all f) n s s s' tt (b1_state_rel_refl s) Hf) as (fb & sb' & Efb & Hrel).
    destruct (engine_depths_kept f) as [_ _ _ _ _ _ _ _ _ _ _ Dre _ _ _ _ _ _ _ _ _ _ _ _ _ _].
    destruct (b1_depths_kept fb) as [_ _ _ _ _ _ _ _ _ _ _ B1Dre _ _ _ _ _ _ _ _ _ _ _ _ _ _].
    pose proof (Dre n s s' tt Hf) as [Hp1 _].
    pose proof (B1Dre n s sb' tt Efb) as [Hp2 _].
    assert (s' = sb') by (apply b1_state_rel_eq; [exact Hrel|congruence]).
    subst sb'. by exists fb.
  - intros [fb Hfb].
    destruct (Brun_effect1 _ (b1_bwd_all fb) n s s s' tt (b1_state_rel_refl s) Hfb) as (f & s0' & Ef & Hrel).
    destruct (engine_depths_kept f) as [_ _ _ _ _ _ _ _ _ _ _ Dre _ _ _ _ _ _ _ _ _ _ _ _ _ _].
    destruct (b1_depths_kept fb) as [_ _ _ _ _ _ _ _ _ _ _ B1Dre _ _ _ _ _ _ _ _ _ _ _ _ _ _].
    pose proof (Dre n s s0' tt Ef) as [Hp1 _].
    pose proof (B1Dre n s s' tt Hfb) as [Hp2 _].
    assert (s0' = s') by (apply b1_state_rel_eq; [exact Hrel|congruence]).
    subst s0'. by exists f.
Qed.
References
Lemma b1_drain_eq s s' :
  (exists f, drain f s = Some (s', tt)) <->
  (exists fb, b1_drain fb s = Some (s', tt)).
Qed
Proof.
  split.
  - intros [f Hf].
    destruct (Fdrain1 _ (b1_fwd_all f) s s s' tt (b1_state_rel_refl s) Hf) as (fb & sb' & Efb & Hrel).
    destruct (engine_depths_kept f) as [_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ Ddr].
    destruct (b1_depths_kept fb) as [_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ B1Ddr].
    pose proof (Ddr s s' tt Hf) as [Hp1 _].
    pose proof (B1Ddr s sb' tt Efb) as [Hp2 _].
    assert (s' = sb') by (apply b1_state_rel_eq; [exact Hrel|congruence]).
    subst sb'. by exists fb.
  - intros [fb Hfb].
    destruct (Bdrain1 _ (b1_bwd_all fb) s s s' tt (b1_state_rel_refl s) Hfb) as (f & s0' & Ef & Hrel).
    destruct (engine_depths_kept f) as [_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ Ddr].
    destruct (b1_depths_kept fb) as [_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ B1Ddr].
    pose proof (Ddr s s0' tt Ef) as [Hp1 _].
    pose proof (B1Ddr s s' tt Hfb) as [Hp2 _].
    assert (s0' = s') by (apply b1_state_rel_eq; [exact Hrel|congruence]).
    subst s0'. by exists f.
Qed.
References

Idle-graph fused execution of a signal whose sole observer is an idle effect (`S -> E`).

Definition fused_single_signal_effect (f : nat) (src eff : node_id) (v : val) : M unit :=
  let* d := get_node src in
  match n_kind d with
  | KSignal =>
    if decide (n_value d = v) then m_return tt
    else
      let* _ := modify_node src (node_with_value v) in
      let* _ := m_modify (ghost_publish src) in
      let* _ := m_modify (fun st => with_flush_owed false (with_flushing true st)) in
      let* _ := modify_node eff (fun x => node_with_freshness Clean (node_with_queued false x)) in
      let* _ := run_effect f eff in
      let* _ := drain f in
      m_modify (with_flushing false)
  | _ => stuck
  end.
References
Lemma node_clean_unqueued_marked_dirty de :
  node_with_freshness Clean (node_with_queued false (node_marked Dirty (n_unannounced de) true de)) =
  node_with_freshness Clean (node_with_queued false de).
Qed
Proof.
  destruct de; reflexivity.
Qed.
References
Lemma m_bind_get_node {A} n (k : node -> M A) d s :
  g_nodes s !! n = Some d -> (let* x := get_node n in k x) s = k d s.
Qed
Proof. intros H. unfold m_bind, get_node. by rewrite H. Qed.
References
Lemma m_bind_modify_node {A} n u (k : M A) d s :
  g_nodes s !! n = Some d -> (let* _ := modify_node n u in k) s = k (set_node n (u d) s).
Qed
Proof. intros H. unfold m_bind, modify_node. by rewrite H. Qed.
References
Lemma m_bind_modify {A} u (k : M A) s :
  (let* _ := m_modify u in k) s = k (u s).
Qed
Proof. reflexivity. Qed.
References
Lemma m_bind_step {A} step (k : M A) s s' :
  step s = Some s' -> (let* _ := m_step step in k) s = k s'.
Qed
Proof. intros H. unfold m_bind, m_step. by rewrite H. Qed.
References
Lemma m_bind_get {A} (k : state -> M A) s :
  (let* s0 := m_get in k s0) s = k s s.
Qed
Proof. reflexivity. Qed.
References
Lemma if_true_eq {A} (x y : A) : (if true then x else y) = x.
Qed
Proof. reflexivity. Qed.
References

Uses

  • true
Lemma if_false_eq {A} (x y : A) : (if false then x else y) = y.
Qed
Proof. reflexivity. Qed.
References

Uses

  • false
Lemma bool_decide_nat_0 : bool_decide (0%nat = 0%nat) = true.
Qed
Proof. by apply bool_decide_eq_true. Qed.
References

Uses

  • true
  • bool_decide
  • bool_decide_eq_true
Lemma reset_queue_set_node n d s :
  with_queue_head 0 (with_queue [] (set_node n d s)) = set_node n d (with_queue_head 0 (with_queue [] s)).
Qed
Proof. destruct s; reflexivity. Qed.
References
Lemma reset_queue_enqueue n s :
  with_queue_head 0 (with_queue [] (enqueue n s)) = with_queue_head 0 (with_queue [] s).
Qed
Proof. destruct s; reflexivity. Qed.
References
Lemma reset_queue_head h s :
  with_queue_head 0 (with_queue [] (with_queue_head h s)) = with_queue_head 0 (with_queue [] s).
Qed
Proof. destruct s; reflexivity. Qed.
References
Lemma reset_queue_flush_owed b s :
  with_queue_head 0 (with_queue [] (with_flush_owed b s)) = with_flush_owed b (with_queue_head 0 (with_queue [] s)).
Qed
Proof. destruct s; reflexivity. Qed.
References
Lemma reset_queue_flushing b s :
  with_queue_head 0 (with_queue [] (with_flushing b s)) = with_flushing b (with_queue_head 0 (with_queue [] s)).
Qed
Proof. destruct s; reflexivity. Qed.
References
Lemma set_node_with_flushing n d b s :
  set_node n d (with_flushing b s) = with_flushing b (set_node n d s).
Qed
Proof. destruct s; reflexivity. Qed.
References
Lemma set_node_with_flush_owed n d b s :
  set_node n d (with_flush_owed b s) = with_flush_owed b (set_node n d s).
Qed
Proof. destruct s; reflexivity. Qed.
References
Lemma queue_rel_fused_signal (x : state) eff d_eff :
  g_queue_head x = length (g_queue x) ->
  queue_rel
    (set_node eff (node_with_freshness Clean (node_with_queued false d_eff))
       (with_flush_owed false (with_flushing true x)))
    (set_node eff (node_with_freshness Clean (node_with_queued false (node_marked Dirty (n_unannounced d_eff) true d_eff)))
       (with_queue_head (S (g_queue_head (enqueue eff (set_node eff (node_marked Dirty (n_unannounced d_eff) true d_eff) x))))
          (with_flush_owed false (with_flushing true (enqueue eff (set_node eff (node_marked Dirty (n_unannounced d_eff) true d_eff) x)))))).
Qed
Proof.
  intros Hqhd. rewrite node_clean_unqueued_marked_dirty.
  unfold queue_rel, pending. repeat split.
  - rewrite !reset_queue_set_node, !reset_queue_head, !reset_queue_flush_owed, !reset_queue_flushing, !reset_queue_enqueue, !reset_queue_set_node.
    rewrite <- set_node_with_flushing, <- set_node_with_flush_owed, set_node_twice.
    reflexivity.
  - cbn [g_queue g_queue_head enqueue with_queue with_queue_head with_flush_owed with_flushing set_node].
    rewrite Hqhd, drop_all.
    replace (S (length (g_queue x))) with (length (g_queue x ++ [eff])) by (rewrite length_app; simpl; lia).
    by rewrite drop_all.
  - cbn [g_queue g_queue_head with_flush_owed with_flushing set_node]. lia.
  - cbn [g_queue g_queue_head enqueue with_queue with_queue_head with_flush_owed with_flushing set_node].
    rewrite length_app; simpl; lia.
Qed.
References
Lemma queue_rel_fused_memo (x : state) memo eff dm' de' :
  g_queue_head x = length (g_queue x) ->
  queue_rel
    (with_flush_owed false (with_flushing true (set_node eff de' (set_node memo dm' x))))
    (with_queue_head (S (g_queue_head (enqueue eff (set_node eff de' (set_node memo dm' x)))))
       (with_flush_owed false (with_flushing true (enqueue eff (set_node eff de' (set_node memo dm' x)))))).
Qed
Proof.
  intros Hqhd. unfold queue_rel, pending. split; [reflexivity|repeat split].
  - cbn [g_queue g_queue_head enqueue with_queue with_queue_head with_flush_owed with_flushing set_node].
    rewrite Hqhd, drop_all.
    replace (S (length (g_queue x))) with (length (g_queue x ++ [eff])) by (rewrite length_app; simpl; lia).
    by rewrite drop_all.
  - cbn [g_queue g_queue_head with_flush_owed with_flushing set_node]. lia.
  - cbn [g_queue g_queue_head enqueue with_queue with_queue_head with_flush_owed with_flushing set_node].
    rewrite length_app; simpl; lia.
Qed.
References
✓ No axioms

On an idle graph, fused S -> E execution without intermediate enqueue/dequeue produces the exact same final state and trace as write_signal.

Refines the idle S -> E branch of NotifySingleFromIdleSignal: setting g_flushing := true, clearing eff to Clean / n_queued := false, running run_effect eff, and draining any reentrant effects gives the identical state s' (g_trace, g_nodes, g_queue) as write_signal.

Arguments

src : node_id
signal node id.
d_src : State.node
signal node record in s.
eff : node_id
sole observer effect node id.
slot : nat
observer edge slot on eff.
de : State.node
effect node record in s.
v : val
new value written to src.
s : State.state
idle starting engine state.
s' : State.state
final engine state after flush completes.
Theorem fused_single_signal_effect_eq src d_src eff slot de v s s' :
  g_nodes s !! src = Some d_src ->
  n_kind d_src = KSignal ->
  observer_items (observers_of (g_edges s) src) = [(eff, slot)] ->
  g_nodes s !! eff = Some de ->
  n_kind de = KEffect ->
  n_disposed de = false ->
  n_in_cleanup de = false ->
  n_queued de = false ->
  g_batch_depth s = 0 ->
  g_pull_depth s = 0 ->
  g_flushing s = false ->
  g_current s = None ->
  g_untracked s = false ->
  g_flush_owed s = false ->
  pending s = [] ->
  g_queue_head s <= length (g_queue s) ->
  (exists f, fused_single_signal_effect f src eff v s = Some (s', tt)) <->
  (exists f, write_signal f src v s = Some (s', tt)).

Arguments

src : node_id
signal node id.
d_src : State.node
signal node record in s.
eff : node_id
sole observer effect node id.
slot : nat
observer edge slot on eff.
de : State.node
effect node record in s.
v : val
new value written to src.
s : State.state
idle starting engine state.
s' : State.state
final engine state after flush completes.
Qed
Proof.
  intros Hsrc Hks Hobs Heff Hke Hde Hclean Hnq Hbd Hpd Hfl Hcur Hunt Hfo Hpend Hqle.
  assert (Hne : eff <> src) by (intros ->; rewrite Hsrc in Heff; simplify_eq; congruence).
  assert (Hqhd : g_queue_head s = length (g_queue s)).
  { unfold pending in Hpend. pose proof (f_equal length Hpend) as Hlen. rewrite length_drop in Hlen. simpl in Hlen. lia. }
  rewrite b1_write_signal_eq.
  destruct (decide (n_value d_src = v)) as [Heqv|Hnev].
  - split; intros [f Hf].
    + unfold fused_single_signal_effect in Hf. unfold get_node, m_bind at 1 in Hf.
      rewrite Hsrc, Hks, decide_True in Hf by exact Heqv. unfold m_return in Hf. simplify_eq.
      exists 1. cbn [b1_write_signal]. unfold get_node, m_bind at 1.
      rewrite Hsrc, Hks, decide_True by reflexivity. reflexivity.
    + destruct f as [|f]; [cbn in Hf; discriminate|].
      cbn [b1_write_signal] in Hf. unfold get_node, m_bind at 1 in Hf.
      rewrite Hsrc, Hks, decide_True in Hf by exact Heqv. unfold m_return in Hf. simplify_eq.
      exists 0. unfold fused_single_signal_effect. unfold get_node, m_bind at 1.
      rewrite Hsrc, Hks, decide_True by reflexivity. reflexivity.
  - set (sa := ghost_publish src (set_node src (node_with_value v d_src) s)).
    assert (Ha_nodes : g_nodes sa = <[src := node_with_value v d_src]> (g_nodes s))
      by (unfold sa, ghost_publish; destruct (g_nodes (set_node src (node_with_value v d_src) s) !! src); reflexivity).
    assert (Ha_eff : g_nodes sa !! eff = Some de) by (rewrite Ha_nodes, lookup_insert_ne by congruence; exact Heff).
    assert (Ha_edges : g_edges sa = g_edges s)
      by (unfold sa, ghost_publish; destruct (g_nodes (set_node src (node_with_value v d_src) s) !! src); reflexivity).
    assert (Ha_q : g_queue sa = g_queue s)
      by (unfold sa, ghost_publish; destruct (g_nodes (set_node src (node_with_value v d_src) s) !! src); reflexivity).
    assert (Ha_qhd : g_queue_head sa = g_queue_head s)
      by (unfold sa, ghost_publish; destruct (g_nodes (set_node src (node_with_value v d_src) s) !! src); reflexivity).
    assert (Ha_bd : g_batch_depth sa = g_batch_depth s)
      by (unfold sa, ghost_publish; destruct (g_nodes (set_node src (node_with_value v d_src) s) !! src); reflexivity).
    assert (Ha_pd : g_pull_depth sa = g_pull_depth s)
      by (unfold sa, ghost_publish; destruct (g_nodes (set_node src (node_with_value v d_src) s) !! src); reflexivity).
    assert (Ha_fl : g_flushing sa = g_flushing s)
      by (unfold sa, ghost_publish; destruct (g_nodes (set_node src (node_with_value v d_src) s) !! src); reflexivity).
    assert (Ha_cur : g_current sa = g_current s)
      by (unfold sa, ghost_publish; destruct (g_nodes (set_node src (node_with_value v d_src) s) !! src); reflexivity).
    assert (Ha_unt : g_untracked sa = g_untracked s)
      by (unfold sa, ghost_publish; destruct (g_nodes (set_node src (node_with_value v d_src) s) !! src); reflexivity).
    assert (Ha_obs : observer_items (observers_of (g_edges sa) src) = [(eff, slot)]) by (rewrite Ha_edges; exact Hobs).
    assert (Hpre : forall f,
      fused_single_signal_effect f src eff v s =
      (let* _ := run_effect f eff in let* _ := drain f in m_modify (with_flushing false))
        (set_node eff (node_with_freshness Clean (node_with_queued false de))
           (with_flush_owed false (with_flushing true sa)))).
    { intros f. unfold fused_single_signal_effect.
      rewrite (m_bind_get_node _ _ _ _ Hsrc).
      rewrite Hks, decide_False by exact Hnev.
      rewrite (m_bind_modify_node _ _ _ _ _ Hsrc).
      rewrite m_bind_modify. fold sa.
      rewrite m_bind_modify.
      assert (Hwf_eff : g_nodes (with_flush_owed false (with_flushing true sa)) !! eff = Some de) by exact Ha_eff.
      rewrite (m_bind_modify_node _ _ _ _ _ Hwf_eff).
      reflexivity. }
    set (sf0 := set_node eff (node_with_freshness Clean (node_with_queued false de))
                  (with_flush_owed false (with_flushing true sa))).
    assert (Hmark : forall f,
      notify_dirty (S f) src sa =
      Some (enqueue eff (set_node eff (node_marked Dirty (n_unannounced de) true de) sa))).
    { intros f. unfold notify_dirty. erewrite notify_with_singleton; [|exact Ha_obs].
      cbn [mark_dirty]. rewrite Ha_eff, Hke.
      unfold effect_marked. rewrite Hde, Hclean, Hnq. reflexivity. }
    set (sa_enq := enqueue eff (set_node eff (node_marked Dirty (n_unannounced de) true de) sa)).
    set (sw0 := with_queue_head (S (g_queue_head sa_enq)) (with_flush_owed false (with_flushing true sa_enq))).
    assert (Hrel_0 : queue_rel sf0 (set_node eff (node_with_freshness Clean (node_with_queued false (node_marked Dirty (n_unannounced de) true de))) sw0))
      by (unfold sf0, sw0, sa_enq; apply queue_rel_fused_signal; rewrite Ha_qhd, Ha_q; exact Hqhd).
    assert (Hlq_enq : live_queue sa_enq = true)
      by (unfold live_queue, sa_enq, enqueue, set_node; simpl; rewrite Ha_q, Ha_qhd, Hqhd; apply bool_decide_eq_true; rewrite length_app; simpl; lia).
    assert (Hbd_enq : g_batch_depth sa_enq = 0)
      by (unfold sa_enq, enqueue, set_node; simpl; rewrite Ha_bd; exact Hbd).
    assert (Hob_enq : outside_bodies sa_enq = true)
      by (unfold outside_bodies, sa_enq, enqueue, set_node; simpl; by rewrite Ha_pd, Ha_cur, Ha_unt, Hpd, Hcur, Hunt).
    assert (Hfl_enq : g_flushing sa_enq = false)
      by (unfold sa_enq, enqueue, set_node; simpl; rewrite Ha_fl; exact Hfl).
    assert (Hlu_enq : (g_queue sa ++ [eff]) !! g_queue_head sa = Some eff)
      by (rewrite Ha_q, Ha_qhd, Hqhd, lookup_app_r, Nat.sub_diag by lia; reflexivity).
    assert (Hsw0_eff : g_nodes sw0 !! eff = Some (node_marked Dirty (n_unannounced de) true de))
      by (unfold sw0, sa_enq, enqueue, set_node; simpl; rewrite lookup_insert_eq; reflexivity).
    assert (Hwrite_prefix : forall k,
      b1_write_signal (5 + k) src v s =
      (let* _ := b1_run_effect k eff in
       let* _ := b1_drain (S k) in
       m_modify (with_flushing false))
        (set_node eff (node_with_freshness Clean (node_with_queued false (node_marked Dirty (n_unannounced de) true de))) sw0)).
    { intros k. change (5 + k) with (S (S (S (S (S k))))).
      cbn [b1_write_signal].
      rewrite (m_bind_get_node _ _ _ _ Hsrc). rewrite Hks, decide_False by exact Hnev.
      rewrite (m_bind_modify_node _ _ _ _ _ Hsrc). rewrite m_bind_modify. fold sa.
      rewrite (m_bind_step _ _ _ _ (Hmark (S (S (S k))))).
      cbn [b1_request_flush]. rewrite m_bind_get. fold sa_enq.
      rewrite Hlq_enq, Hbd_enq, Hob_enq, bool_decide_nat_0. cbn [andb].
      cbn [b1_run_flush]. rewrite m_bind_get, Hfl_enq, m_bind_modify.
      cbn [b1_drain]. rewrite ?b4_bind_assoc, m_bind_get.
      change (g_queue (with_flush_owed false (with_flushing true sa_enq)) !!
              g_queue_head (with_flush_owed false (with_flushing true sa_enq)))
        with ((g_queue sa ++ [eff]) !! g_queue_head sa).
      rewrite Hlu_enq, ?b4_bind_assoc, m_bind_modify.
      change (g_queue_head (with_flush_owed false (with_flushing true sa_enq))) with (g_queue_head sa_enq).
      fold sw0.
      cbn [b1_execute_effect]. rewrite ?b4_bind_assoc, (m_bind_get_node _ _ _ _ Hsw0_eff).
      change (n_disposed (node_marked Dirty (n_unannounced de) true de)) with (n_disposed de).
      rewrite Hde, decide_False by discriminate.
      rewrite ?b4_bind_assoc, b4_bind_ret_l, ?b4_bind_assoc.
      rewrite (m_bind_get_node _ _ _ _ Hsw0_eff), ?b4_bind_assoc.
      rewrite (m_bind_modify_node _ _ _ _ _ Hsw0_eff), ?b4_bind_assoc.
      change (n_freshness (node_marked Dirty (n_unannounced de) true de)) with Dirty.
      rewrite decide_True by reflexivity.
      reflexivity. }
    split.
    + intros [f Hf]. rewrite Hpre in Hf.
      eapply bind_inv in Hf as (s_re & [] & Hre & Hd).
      eapply bind_inv in Hd as (s_dr & [] & Hdr & Hfl_end).
      unfold m_modify in Hfl_end. simplify_eq.
      assert (Hre_ex : exists fb1, b1_run_effect fb1 eff sf0 = Some (s_re, tt))
        by (rewrite <- b1_run_effect_eq; exists f; exact Hre).
      destruct Hre_ex as [fb1 Hre1].
      assert (Hdr_ex : exists fb2, b1_drain fb2 s_re = Some (s_dr, tt))
        by (rewrite <- b1_drain_eq; exists f; exact Hdr).
      destruct Hdr_ex as [fb2 Hdr2].
      set (fb := max fb1 fb2).
      assert (Hre' : b1_run_effect fb eff sf0 = Some (s_re, tt))
        by (eapply (B1Mrun_effect _ (b1_mono_all fb1)); [lia|exact Hre1]).
      assert (Hdr' : b1_drain fb s_re = Some (s_dr, tt))
        by (eapply (B1Mdrain _ (b1_mono_all fb2)); [lia|exact Hdr2]).
      destruct (B1Qrun_effect _ (b1_qrel_all fb) eff _ _ _ _ Hre' Hrel_0) as (sw1 & Hre_w & Hrel1).
      destruct (B1Qdrain _ (b1_qrel_all fb) _ _ _ _ Hdr' Hrel1) as (sw2 & Hdr_w & Hrel2).
      destruct (b1_drain_resets _ _ _ Hdr') as [Hq1 Hh1].
      destruct (b1_drain_resets _ _ _ Hdr_w) as [Hq2 Hh2].
      assert (Heq : s_dr = sw2) by (apply queue_rel_drained_eq; assumption).
      subst sw2.
      exists (5 + fb). rewrite Hwrite_prefix.
      unfold m_bind at 1. rewrite Hre_w.
      assert (Hdr_w' : b1_drain (S fb) sw1 = Some (s_dr, tt))
        by (eapply (B1Mdrain _ (b1_mono_all fb)); [lia|exact Hdr_w]).
      cbn [option_bind]. unfold m_bind at 1. rewrite Hdr_w'. cbn [option_bind]. reflexivity.
    + intros [f Hf].
      assert (Hunder5 : f < 5 -> b1_write_signal f src v s = None).
      { intros Hlt. destruct f as [|[|[|[|[|f']]]]]; try lia.
        - reflexivity.
        - cbn [b1_write_signal]. rewrite (m_bind_get_node _ _ _ _ Hsrc).
          rewrite Hks, decide_False by exact Hnev.
          rewrite (m_bind_modify_node _ _ _ _ _ Hsrc). rewrite m_bind_modify. fold sa.
          unfold m_bind, m_step, notify_dirty. erewrite notify_with_singleton by exact Ha_obs.
          cbn [walk_marks mark_dirty]. reflexivity.
        - cbn [b1_write_signal]. rewrite (m_bind_get_node _ _ _ _ Hsrc).
          rewrite Hks, decide_False by exact Hnev.
          rewrite (m_bind_modify_node _ _ _ _ _ Hsrc). rewrite m_bind_modify. fold sa.
          rewrite (m_bind_step _ _ _ _ (Hmark 0)).
          cbn [b1_request_flush]. rewrite m_bind_get. fold sa_enq.
          rewrite Hlq_enq, Hbd_enq, Hob_enq, bool_decide_nat_0. cbn [andb].
          reflexivity.
        - cbn [b1_write_signal]. rewrite (m_bind_get_node _ _ _ _ Hsrc).
          rewrite Hks, decide_False by exact Hnev.
          rewrite (m_bind_modify_node _ _ _ _ _ Hsrc). rewrite m_bind_modify. fold sa.
          rewrite (m_bind_step _ _ _ _ (Hmark 1)).
          cbn [b1_request_flush]. rewrite m_bind_get. fold sa_enq.
          rewrite Hlq_enq, Hbd_enq, Hob_enq, bool_decide_nat_0. cbn [andb].
          cbn [b1_run_flush]. rewrite m_bind_get, Hfl_enq, m_bind_modify.
          reflexivity.
        - cbn [b1_write_signal]. rewrite (m_bind_get_node _ _ _ _ Hsrc).
          rewrite Hks, decide_False by exact Hnev.
          rewrite (m_bind_modify_node _ _ _ _ _ Hsrc). rewrite m_bind_modify. fold sa.
          rewrite (m_bind_step _ _ _ _ (Hmark 2)).
          cbn [b1_request_flush]. rewrite m_bind_get. fold sa_enq.
          rewrite Hlq_enq, Hbd_enq, Hob_enq, bool_decide_nat_0. cbn [andb].
          cbn [b1_run_flush]. rewrite m_bind_get, Hfl_enq, m_bind_modify.
          cbn [b1_drain]. rewrite ?b4_bind_assoc, m_bind_get.
          change (g_queue (with_flush_owed false (with_flushing true sa_enq)) !!
                  g_queue_head (with_flush_owed false (with_flushing true sa_enq)))
            with ((g_queue sa ++ [eff]) !! g_queue_head sa).
          rewrite Hlu_enq, ?b4_bind_assoc, m_bind_modify. reflexivity. }
      destruct f as [|[|[|[|[|f']]]]]; [rewrite Hunder5 in Hf by lia; discriminate..|].
      replace (S (S (S (S (S f'))))) with (5 + f') in Hf by lia.
      rewrite Hwrite_prefix in Hf.
      eapply bind_inv in Hf as (sw1 & [] & Hre_w & Hrest).
      eapply bind_inv in Hrest as (sw2 & [] & Hdr_w & Hfl_end).
      unfold m_modify in Hfl_end. simplify_eq.
      pose proof (queue_rel_sym _ _ Hrel_0) as Hrel_0'.
      destruct (B1Qrun_effect _ (b1_qrel_all f') eff _ _ _ _ Hre_w Hrel_0') as (sf1 & Hre_f1 & Hrel1).
      destruct (B1Qdrain _ (b1_qrel_all (S f')) _ _ _ _ Hdr_w Hrel1) as (sf2 & Hdr_f1 & Hrel2).
      destruct (b1_drain_resets _ _ _ Hdr_w) as [Hq1 Hh1].
      destruct (b1_drain_resets _ _ _ Hdr_f1) as [Hq2 Hh2].
      assert (Heq : sw2 = sf2) by (apply queue_rel_drained_eq; assumption).
      subst sf2.
      assert (Hre_eng : exists fe1, run_effect fe1 eff sf0 = Some (sf1, tt))
        by (rewrite b1_run_effect_eq; by exists f').
      assert (Hdr_eng : exists fe2, drain fe2 sf1 = Some (sw2, tt))
        by (rewrite b1_drain_eq; by exists (S f')).
      destruct Hre_eng as [fe1 Hre_e], Hdr_eng as [fe2 Hdr_e].
      set (fe := max fe1 fe2).
      assert (Hre_e' : run_effect fe eff sf0 = Some (sf1, tt))
        by (eapply (Mrun_effect _ (engine_mono_all fe1)); [lia|exact Hre_e]).
      assert (Hdr_e' : drain fe sf1 = Some (sw2, tt))
        by (eapply (Mdrain _ (engine_mono_all fe2)); [lia|exact Hdr_e]).
      exists fe. rewrite Hpre. fold sf0. unfold m_bind at 1. rewrite Hre_e'.
      cbn [option_bind]. unfold m_bind at 1. rewrite Hdr_e'. cbn [option_bind]. reflexivity.
Qed.
References

Idle-graph fused execution of `S -> M -> E`, setting `E` to `Check` + `queued := true` and `g_flushing := true` while refreshing `M` inline via `b1_refresh`, then settling/running `E` and draining any newly queued effects.

References
Lemma fused_single_memo_effect_tail_mono f1 f2 memo eff :
  f1 <= f2 ->
  forall st r,
    (let* _ := b1_refresh f1 memo in
     let* _ := get_node memo in
     let* d' := get_node eff in
     let* d_m := get_node memo in
     let* s1 := m_get in
     let* _ :=
       if decide (n_freshness d' = Check /\ stale_live d_m = true /\ (memo ∈ g_running s1 \/ memo ∈ g_checking s1))
       then m_step (mark_dirty f1 eff) else m_return tt in
     let* _ := b1_resolve_walk f1 eff 1 in
     let* d'' := get_node eff in
     let* _ :=
       if decide (n_freshness d'' = Check)
       then let* _ := m_modify (ghost_resolved eff) in modify_node eff (node_with_freshness Clean)
       else m_return tt in
     let* d''' := get_node eff in
     let* _ := modify_node eff (fun x => node_with_freshness Clean (node_with_queued false x)) in
     let* _ := if decide (n_freshness d''' = Dirty) then b1_run_effect f1 eff else m_return tt in
     let* _ := b1_drain f1 in
     m_modify (with_flushing false)) st = Some r ->
    (let* _ := b1_refresh f2 memo in
     let* _ := get_node memo in
     let* d' := get_node eff in
     let* d_m := get_node memo in
     let* s1 := m_get in
     let* _ :=
       if decide (n_freshness d' = Check /\ stale_live d_m = true /\ (memo ∈ g_running s1 \/ memo ∈ g_checking s1))
       then m_step (mark_dirty f2 eff) else m_return tt in
     let* _ := b1_resolve_walk f2 eff 1 in
     let* d'' := get_node eff in
     let* _ :=
       if decide (n_freshness d'' = Check)
       then let* _ := m_modify (ghost_resolved eff) in modify_node eff (node_with_freshness Clean)
       else m_return tt in
     let* d''' := get_node eff in
     let* _ := modify_node eff (fun x => node_with_freshness Clean (node_with_queued false x)) in
     let* _ := if decide (n_freshness d''' = Dirty) then b1_run_effect f2 eff else m_return tt in
     let* _ := b1_drain f2 in
     m_modify (with_flushing false)) st = Some r.
Qed
Proof.
  intros Hle st r H. revert H.
  eapply bind_mono; [intros ??; by eapply (B1Mrefresh _ (b1_mono_all f1))|intros [] ?? _].
  eapply bind_mono; [intros ?? E; exact E|intros ??? _].
  eapply bind_mono; [intros ?? E; exact E|intros d' ?? _].
  eapply bind_mono; [intros ?? E; exact E|intros dm ?? _].
  eapply bind_mono; [intros ?? E; exact E|intros st1 ?? _].
  eapply bind_mono; [intros ?? E; destruct (decide _); [by eapply m_step_mark_dirty_mono|exact E]|intros [] ?? _].
  eapply bind_mono; [intros ??; by eapply (B1Mwalk _ (b1_mono_all f1))|intros [] ?? _].
  eapply bind_mono; [intros ?? E; exact E|intros d'' ?? _].
  eapply bind_mono; [intros ?? E; exact E|intros [] ?? _].
  eapply bind_mono; [intros ?? E; exact E|intros d''' ?? _].
  eapply bind_mono; [intros ?? E; exact E|intros [] ?? _].
  eapply bind_mono; [intros ?? E; destruct (decide (n_freshness d''' = Dirty)); [by eapply (B1Mrun_effect _ (b1_mono_all f1))|exact E]|intros [] ?? _].
  eapply bind_mono; [intros ??; by eapply (B1Mdrain _ (b1_mono_all f1))|intros [] ?? _; exact id].
Qed.
References
✓ No axioms

On an idle graph, fused S -> M -> E execution with E marked Check/queued and g_flushing = true yields the exact same final state and trace as write_signal.

Refines the idle S -> M -> E branch of NotifySingleFromIdleSignal together with Section B1 (flushing in Deferring): refreshing M inline via b1_refresh under g_flushing = true and then checking/running E and draining reentrant effects produces the identical final state and trace as write_signal.

Arguments

src : node_id
signal node id.
d_src : State.node
signal node record in s.
memo : node_id
sole memo observer id.
slot_m : nat
observer edge slot on memo.
dm : State.node
memo node record in s.
eff : node_id
sole effect observer id of memo.
slot_e : nat
observer edge slot on eff.
de : State.node
effect node record in s.
h_e : option nat
handle of slot 0 in sources_of eff.
v : val
new value written to src.
s : State.state
idle starting engine state.
s' : State.state
final engine state after flush completes.
Theorem fused_single_memo_effect_flush_eq src d_src memo slot_m dm eff slot_e de h_e v s s' :
  g_nodes s !! src = Some d_src ->
  n_kind d_src = KSignal ->
  observer_items (observers_of (g_edges s) src) = [(memo, slot_m)] ->
  g_nodes s !! memo = Some dm ->
  n_kind dm = KMemo ->
  n_freshness dm = Clean ->
  n_disposed dm = false ->
  observer_items (observers_of (g_edges s) memo) = [(eff, slot_e)] ->
  g_nodes s !! eff = Some de ->
  n_kind de = KEffect ->
  n_freshness de = Clean ->
  n_disposed de = false ->
  n_in_cleanup de = false ->
  n_queued de = false ->
  source_slots (sources_of (g_edges s) eff) !! 0 = Some (memo, h_e) ->
  g_batch_depth s = 0 ->
  g_pull_depth s = 0 ->
  g_flushing s = false ->
  g_current s = None ->
  g_untracked s = false ->
  g_flush_owed s = false ->
  pending s = [] ->
  g_queue_head s <= length (g_queue s) ->
  (exists f, fused_single_memo_effect f src memo eff v s = Some (s', tt)) <->
  (exists f, write_signal f src v s = Some (s', tt)).

Arguments

src : node_id
signal node id.
d_src : State.node
signal node record in s.
memo : node_id
sole memo observer id.
slot_m : nat
observer edge slot on memo.
dm : State.node
memo node record in s.
eff : node_id
sole effect observer id of memo.
slot_e : nat
observer edge slot on eff.
de : State.node
effect node record in s.
h_e : option nat
handle of slot 0 in sources_of eff.
v : val
new value written to src.
s : State.state
idle starting engine state.
s' : State.state
final engine state after flush completes.
Qed
Proof.
  intros Hsrc Hks Hobs_m Hmemo Hkm Hcm Hdzm Hobs_e Heff Hke Hce Hdze Hclean Hnq Hsl0
         Hbd Hpd Hfl Hcur Hunt Hfo Hpend Hqle.
  assert (Hsm : memo <> src) by (intros ->; rewrite Hsrc in Hmemo; simplify_eq; congruence).
  assert (Hse : eff <> src) by (intros ->; rewrite Hsrc in Heff; simplify_eq; congruence).
  assert (Hme : eff <> memo) by (intros ->; rewrite Hmemo in Heff; simplify_eq; congruence).
  assert (Hqhd : g_queue_head s = length (g_queue s)).
  { unfold pending in Hpend. pose proof (f_equal length Hpend) as Hlen. rewrite length_drop in Hlen. simpl in Hlen. lia. }
  rewrite b1_write_signal_eq.
  destruct (decide (n_value d_src = v)) as [Heqv|Hnev].
  - split; intros [f Hf].
    + unfold fused_single_memo_effect in Hf. unfold get_node, m_bind at 1 in Hf.
      rewrite Hsrc, Hks, decide_True in Hf by exact Heqv. unfold m_return in Hf. simplify_eq.
      exists 1. cbn [b1_write_signal]. unfold get_node, m_bind at 1.
      rewrite Hsrc, Hks, decide_True by reflexivity. reflexivity.
    + destruct f as [|f]; [cbn in Hf; discriminate|].
      cbn [b1_write_signal] in Hf. unfold get_node, m_bind at 1 in Hf.
      rewrite Hsrc, Hks, decide_True in Hf by exact Heqv. unfold m_return in Hf. simplify_eq.
      exists 0. unfold fused_single_memo_effect. unfold get_node, m_bind at 1.
      rewrite Hsrc, Hks, decide_True by reflexivity. reflexivity.
  - set (sa := ghost_publish src (set_node src (node_with_value v d_src) s)).
    assert (Ha_nodes : g_nodes sa = <[src := node_with_value v d_src]> (g_nodes s))
      by (unfold sa, ghost_publish; destruct (g_nodes (set_node src (node_with_value v d_src) s) !! src); reflexivity).
    assert (Ha_memo : g_nodes sa !! memo = Some dm) by (rewrite Ha_nodes, lookup_insert_ne by congruence; exact Hmemo).
    assert (Ha_eff : g_nodes sa !! eff = Some de) by (rewrite Ha_nodes, lookup_insert_ne by congruence; exact Heff).
    assert (Ha_edges : g_edges sa = g_edges s)
      by (unfold sa, ghost_publish; destruct (g_nodes (set_node src (node_with_value v d_src) s) !! src); reflexivity).
    assert (Ha_q : g_queue sa = g_queue s)
      by (unfold sa, ghost_publish; destruct (g_nodes (set_node src (node_with_value v d_src) s) !! src); reflexivity).
    assert (Ha_qhd : g_queue_head sa = g_queue_head s)
      by (unfold sa, ghost_publish; destruct (g_nodes (set_node src (node_with_value v d_src) s) !! src); reflexivity).
    assert (Ha_bd : g_batch_depth sa = g_batch_depth s)
      by (unfold sa, ghost_publish; destruct (g_nodes (set_node src (node_with_value v d_src) s) !! src); reflexivity).
    assert (Ha_pd : g_pull_depth sa = g_pull_depth s)
      by (unfold sa, ghost_publish; destruct (g_nodes (set_node src (node_with_value v d_src) s) !! src); reflexivity).
    assert (Ha_fl : g_flushing sa = g_flushing s)
      by (unfold sa, ghost_publish; destruct (g_nodes (set_node src (node_with_value v d_src) s) !! src); reflexivity).
    assert (Ha_cur : g_current sa = g_current s)
      by (unfold sa, ghost_publish; destruct (g_nodes (set_node src (node_with_value v d_src) s) !! src); reflexivity).
    assert (Ha_unt : g_untracked sa = g_untracked s)
      by (unfold sa, ghost_publish; destruct (g_nodes (set_node src (node_with_value v d_src) s) !! src); reflexivity).
    assert (Ha_obs_m : observer_items (observers_of (g_edges sa) src) = [(memo, slot_m)])
      by (rewrite Ha_edges; exact Hobs_m).
    assert (Ha_obs_e : observer_items (observers_of (g_edges (set_node memo (node_marked Dirty (n_unannounced dm) (n_queued dm) dm) sa)) memo) = [(eff, slot_e)])
      by (unfold set_node; simpl; rewrite Ha_edges; exact Hobs_e).
    assert (Hmark : forall f,
      notify_dirty (S (S f)) src sa =
      Some (enqueue eff (set_node eff (node_marked Check (n_unannounced de) true de)
               (set_node memo (node_marked Dirty (n_unannounced dm) (n_queued dm) dm) sa)))).
    { intros f. unfold notify_dirty. erewrite notify_with_singleton; [|exact Ha_obs_m].
      cbn [mark_dirty]. rewrite Ha_memo, Hkm, decide_True by exact Hcm.
      erewrite notify_with_singleton; [|exact Ha_obs_e].
      cbn [mark_check]. unfold set_node at 1. simpl.
      rewrite lookup_insert_ne by congruence. rewrite Ha_eff, Hke.
      unfold effect_marked. rewrite Hdze, Hclean, Hnq, decide_True by exact Hce. reflexivity. }
    set (dm' := node_marked Dirty (n_unannounced dm) (n_queued dm) dm).
    set (de' := node_marked Check (n_unannounced de) true de).
    set (sa_enq := enqueue eff (set_node eff de' (set_node memo dm' sa))).
    set (sf0 := with_flush_owed false (with_flushing true (set_node eff de' (set_node memo dm' sa)))).
    set (sw0 := with_queue_head (S (g_queue_head sa_enq)) (with_flush_owed false (with_flushing true sa_enq))).
    assert (Hpre_f : forall f,
      fused_single_memo_effect f src memo eff v s =
      (let* _ := b1_refresh f memo in
       let* _ := get_node memo in
       let* d' := get_node eff in
       let* d_m := get_node memo in
       let* s1 := m_get in
       let* _ :=
         if decide (n_freshness d' = Check /\ stale_live d_m = true /\ (memo ∈ g_running s1 \/ memo ∈ g_checking s1))
         then m_step (mark_dirty f eff) else m_return tt in
       let* _ := b1_resolve_walk f eff 1 in
       let* d'' := get_node eff in
       let* _ :=
         if decide (n_freshness d'' = Check)
         then let* _ := m_modify (ghost_resolved eff) in modify_node eff (node_with_freshness Clean)
         else m_return tt in
       let* d''' := get_node eff in
       let* _ := modify_node eff (fun x => node_with_freshness Clean (node_with_queued false x)) in
       let* _ := if decide (n_freshness d''' = Dirty) then b1_run_effect f eff else m_return tt in
       let* _ := b1_drain f in
       m_modify (with_flushing false)) sf0).
    { intros f. unfold fused_single_memo_effect.
      rewrite (m_bind_get_node _ _ _ _ Hsrc). rewrite Hks, decide_False by exact Hnev.
      rewrite (m_bind_modify_node _ _ _ _ _ Hsrc). rewrite m_bind_modify. fold sa.
      rewrite (m_bind_modify_node _ _ _ _ _ Ha_memo).
      assert (Hmem_eff : g_nodes (set_node memo (node_marked Dirty (n_unannounced dm) (n_queued dm) dm) sa) !! eff = Some de)
        by (unfold set_node; simpl; rewrite lookup_insert_ne by congruence; exact Ha_eff).
      rewrite (m_bind_modify_node _ _ _ _ _ Hmem_eff).
      fold dm' de'. rewrite m_bind_modify. reflexivity. }
    assert (Hrel_0 : queue_rel sf0 sw0)
      by (unfold sf0, sw0, sa_enq; apply queue_rel_fused_memo; rewrite Ha_qhd, Ha_q; exact Hqhd).
    assert (Hstl_dm : stale_live dm' = true) by (unfold stale_live, dm'; simpl; by rewrite Hdzm).
    assert (Hsw0_memo : g_nodes sw0 !! memo = Some dm')
      by (unfold sw0, sa_enq, enqueue, set_node; simpl; rewrite Ha_nodes, lookup_insert_ne by congruence; rewrite lookup_insert_eq; reflexivity).
    assert (Hsw0_eff : g_nodes sw0 !! eff = Some de')
      by (unfold sw0, sa_enq, enqueue, set_node; simpl; rewrite lookup_insert_eq; reflexivity).
    assert (Hsw0_sl : source_slots (sources_of (g_edges sw0) eff) !! 0 = Some (memo, h_e))
      by (unfold sw0, sa_enq, enqueue, set_node; simpl; rewrite Ha_edges; exact Hsl0).
    assert (Hsw0_def : deferring_b1 sw0 = true) by (unfold deferring_b1, sw0; simpl; reflexivity).
    assert (Hlq_enq : live_queue sa_enq = true)
      by (unfold live_queue, sa_enq, enqueue, set_node; simpl; rewrite Ha_q, Ha_qhd, Hqhd; apply bool_decide_eq_true; rewrite length_app; simpl; lia).
    assert (Hbd_enq : g_batch_depth sa_enq = 0)
      by (unfold sa_enq, enqueue, set_node; simpl; rewrite Ha_bd; exact Hbd).
    assert (Hob_enq : outside_bodies sa_enq = true)
      by (unfold outside_bodies, sa_enq, enqueue, set_node; simpl; by rewrite Ha_pd, Ha_cur, Ha_unt, Hpd, Hcur, Hunt).
    assert (Hfl_enq : g_flushing sa_enq = false)
      by (unfold sa_enq, enqueue, set_node; simpl; rewrite Ha_fl; exact Hfl).
    assert (Hlu_enq : (g_queue sa ++ [eff]) !! g_queue_head sa = Some eff)
      by (rewrite Ha_q, Ha_qhd, Hqhd, lookup_app_r, Nat.sub_diag by lia; reflexivity).
    assert (Hstep8 : forall k,
      b1_write_signal (8 + k) src v s =
      (let* _ := b1_refresh k memo in
       let* _ := get_node memo in
       let* _ := m_return tt in
       let* d' := get_node eff in
       let* d_m := get_node memo in
       let* s1 := m_get in
       let* _ :=
         if decide (n_freshness d' = Check /\ stale_live d_m = true /\ (memo ∈ g_running s1 \/ memo ∈ g_checking s1))
         then m_step (mark_dirty (2 + k) eff) else m_return tt in
       let* _ := b1_resolve_walk (2 + k) eff 1 in
       let* d'' := get_node eff in
       let* _ :=
         if decide (n_freshness d'' = Check)
         then let* _ := m_modify (ghost_resolved eff) in modify_node eff (node_with_freshness Clean)
         else m_return tt in
       let* d''' := get_node eff in
       let* _ := modify_node eff (fun x => node_with_freshness Clean (node_with_queued false x)) in
       let* _ := if decide (n_freshness d''' = Dirty) then b1_run_effect (3 + k) eff else m_return tt in
       let* _ := b1_drain (4 + k) in
       m_modify (with_flushing false)) sw0).
    { intros k. change (8 + k) with (S (S (S (S (S (S (S (S k)))))))).
      cbn [b1_write_signal].
      rewrite (m_bind_get_node _ _ _ _ Hsrc). rewrite Hks, decide_False by exact Hnev.
      rewrite (m_bind_modify_node _ _ _ _ _ Hsrc). rewrite m_bind_modify. fold sa.
      change (S (S (S (S (S (S (S k))))))) with (S (S (5 + k))).
      rewrite (m_bind_step _ _ _ _ (Hmark (5 + k))).
      cbn [b1_request_flush]. rewrite m_bind_get. fold dm' de'. fold sa_enq.
      rewrite Hlq_enq, Hbd_enq, Hob_enq, bool_decide_nat_0. cbn [andb].
      cbn [b1_run_flush]. rewrite m_bind_get, Hfl_enq, m_bind_modify.
      change (5 + k) with (S (4 + k)).
      cbn [b1_drain]. rewrite ?b4_bind_assoc, m_bind_get.
      change (g_queue (with_flush_owed false (with_flushing true sa_enq)) !!
              g_queue_head (with_flush_owed false (with_flushing true sa_enq)))
        with ((g_queue sa ++ [eff]) !! g_queue_head sa).
      rewrite Hlu_enq, ?b4_bind_assoc, m_bind_modify.
      change (g_queue_head (with_flush_owed false (with_flushing true sa_enq))) with (g_queue_head sa_enq).
      fold sw0.
      change (4 + k) with (S (3 + k)) at 1.
      cbn [b1_execute_effect]. rewrite ?b4_bind_assoc.
      rewrite (m_bind_get_node _ _ _ _ Hsw0_eff), ?b4_bind_assoc.
      change (n_disposed de') with (n_disposed de).
      change (n_freshness de') with Check.
      rewrite Hdze, decide_True by reflexivity.
      rewrite ?b4_bind_assoc.
      change (3 + k) with (S (2 + k)) at 1.
      cbn [b1_resolve_walk]. rewrite ?b4_bind_assoc.
      rewrite (m_bind_get_node _ _ _ _ Hsw0_eff), ?b4_bind_assoc.
      rewrite m_bind_get.
      change (n_freshness de') with Check.
      rewrite decide_True by reflexivity.
      rewrite Hsw0_sl, ?b4_bind_assoc.
      change (2 + k) with (S (1 + k)) at 1.
      cbn [b1_update_if_necessary]. rewrite ?b4_bind_assoc.
      rewrite (m_bind_get_node _ _ _ _ Hsw0_memo).
      change (n_kind dm') with (n_kind dm).
      rewrite Hkm, Hstl_dm, ?b4_bind_assoc.
      change (1 + k) with (S k).
      cbn [b1_refresh_or_pull]. rewrite ?b4_bind_assoc.
      rewrite m_bind_get, Hsw0_def, ?b4_bind_assoc.
      repeat (first [ rewrite b4_bind_assoc | apply b4_bind_ext; intros ? ? ]).
      reflexivity. }
    split.
    + intros [f Hf]. rewrite Hpre_f in Hf.
      eapply bind_inv in Hf as (sf1 & [] & Href & Hrest).
      eapply bind_inv in Hrest as (sf1' & d_m0 & Hgetm & Hrest).
      eapply bind_inv in Hrest as (sf2 & d' & Hget_d' & Hrest).
      eapply bind_inv in Hrest as (sf3 & d_m & Hget_dm & Hrest).
      eapply bind_inv in Hrest as (sf4 & s1_ & Hget_s1 & Hrest).
      eapply bind_inv in Hrest as (sf5 & [] & Hmd & Hrest).
      eapply bind_inv in Hrest as (sf6 & [] & Hrw & Hrest).
      eapply bind_inv in Hrest as (sf7 & d'' & Hget_d'' & Hrest).
      eapply bind_inv in Hrest as (sf8 & [] & Hres & Hrest).
      eapply bind_inv in Hrest as (sf9 & d''' & Hget_d''' & Hrest).
      eapply bind_inv in Hrest as (sf10 & [] & Hclr & Hrest).
      eapply bind_inv in Hrest as (sf11 & [] & Hre & Hrest).
      eapply bind_inv in Hrest as (sf12 & [] & Hdr & Hfl_end).
      unfold m_modify in Hfl_end. simplify_eq.
      assert (Href' : b1_refresh f memo sf0 = Some (sf1, tt)) by exact Href.
      destruct (B1Qrefresh _ (b1_qrel_all f) memo _ _ _ _ Href' Hrel_0) as (sw1 & Href_w & Hrel1).
      destruct (qrel_get_node memo _ _ _ _ Hgetm Hrel1) as (sw1' & Hgetm_w & Hrel1').
      destruct (qrel_get_node eff _ _ _ _ Hget_d' Hrel1') as (sw2 & Hget_d'_w & Hrel2).
      destruct (qrel_get_node memo _ _ _ _ Hget_dm Hrel2) as (sw3 & Hget_dm_w & Hrel3).
      unfold m_get in Hget_s1. injection Hget_s1 as <- <-.
      assert (Hmd_w : exists sw5,
        (if decide (n_freshness d' = Check /\ stale_live d_m = true /\ (memo ∈ g_running sw3 \/ memo ∈ g_checking sw3))
         then m_step (mark_dirty (2 + f) eff) else m_return tt) sw3 = Some (sw5, tt) /\
        queue_rel sf5 sw5).
      { destruct (queue_rel_fields _ _ Hrel3) as (_&_&_&_&_&_&_&_&_&_&_&Hrun&Hchk&_).
        rewrite <- Hrun, <- Hchk.
        destruct (decide _); [|unfold m_return in Hmd; simplify_eq; by exists sw3].
        unfold m_step in Hmd |- *. destruct (mark_dirty f eff sf3) eqn:Em; simplify_eq.
        assert (Em' : mark_dirty (2 + f) eff sf3 = Some sf5) by (eapply (mark_dirty_mono f); [lia|exact Em]).
        destruct (qrel_mark_dirty _ _ _ _ _ Hrel3 Em') as (sw5 & Em2 & Hrel5). rewrite Em2. by exists sw5. }
      destruct Hmd_w as (sw5 & Hmd_w & Hrel5).
      assert (Hrw' : b1_resolve_walk (2 + f) eff 1 sf5 = Some (sf6, tt))
        by (eapply (B1Mwalk _ (b1_mono_all f)); [lia|exact Hrw]).
      destruct (B1Qwalk _ (b1_qrel_all (2 + f)) eff 1 _ _ _ _ Hrw' Hrel5) as (sw6 & Hrw_w & Hrel6).
      destruct (qrel_get_node eff _ _ _ _ Hget_d'' Hrel6) as (sw7 & Hget_d''_w & Hrel7).
      assert (Hres_w : exists sw8,
        (if decide (n_freshness d'' = Check)
         then let* _ := m_modify (ghost_resolved eff) in modify_node eff (node_with_freshness Clean)
         else m_return tt) sw7 = Some (sw8, tt) /\ queue_rel sf8 sw8).
      { destruct (decide (n_freshness d'' = Check)); [|unfold m_return in Hres; simplify_eq; by exists sw7].
        eapply (qrel_bind_of Hres Hrel7); [intros ????; qrel_pure_tac|intros [] ????; qrel_pure_tac]. }
      destruct Hres_w as (sw8 & Hres_w & Hrel8).
      destruct (qrel_get_node eff _ _ _ _ Hget_d''' Hrel8) as (sw9 & Hget_d'''_w & Hrel9).
      destruct (qrel_modify_node eff (fun x => node_with_freshness Clean (node_with_queued false x)) _ _ _ _ Hclr Hrel9) as (sw10 & Hclr_w & Hrel10).
      assert (Hre_w : exists sw11,
        (if decide (n_freshness d''' = Dirty) then b1_run_effect (3 + f) eff else m_return tt) sw10 = Some (sw11, tt) /\
        queue_rel sf11 sw11).
      { destruct (decide (n_freshness d''' = Dirty)); [|unfold m_return in Hre; simplify_eq; by exists sw10].
        assert (Hre' : b1_run_effect (3 + f) eff sf10 = Some (sf11, tt))
          by (eapply (B1Mrun_effect _ (b1_mono_all f)); [lia|exact Hre]).
        by eapply (B1Qrun_effect _ (b1_qrel_all _)). }
      destruct Hre_w as (sw11 & Hre_w & Hrel11).
      assert (Hdr' : b1_drain (4 + f) sf11 = Some (sf12, tt))
        by (eapply (B1Mdrain _ (b1_mono_all f)); [lia|exact Hdr]).
      destruct (B1Qdrain _ (b1_qrel_all _) _ _ _ _ Hdr' Hrel11) as (sw12 & Hdr_w & Hrel12).
      destruct (b1_drain_resets _ _ _ Hdr') as [Hq1 Hh1].
      destruct (b1_drain_resets _ _ _ Hdr_w) as [Hq2 Hh2].
      assert (Heq : sf12 = sw12) by (apply queue_rel_drained_eq; assumption).
      subst sw12.
      exists (8 + f). rewrite Hstep8.
      unfold m_bind at 1. rewrite Href_w. cbn [option_bind].
      unfold m_bind at 1. rewrite Hgetm_w. cbn [option_bind].
      rewrite b4_bind_ret_l.
      unfold m_bind at 1. rewrite Hget_d'_w. cbn [option_bind].
      unfold m_bind at 1. rewrite Hget_dm_w. cbn [option_bind].
      rewrite m_bind_get.
      unfold m_bind at 1. rewrite Hmd_w. cbn [option_bind].
      unfold m_bind at 1. rewrite Hrw_w. cbn [option_bind].
      unfold m_bind at 1. rewrite Hget_d''_w. cbn [option_bind].
      unfold m_bind at 1. rewrite Hres_w. cbn [option_bind].
      unfold m_bind at 1. rewrite Hget_d'''_w. cbn [option_bind].
      unfold m_bind at 1. rewrite Hclr_w. cbn [option_bind].
      unfold m_bind at 1. rewrite Hre_w. cbn [option_bind].
      unfold m_bind at 1. rewrite Hdr_w. cbn [option_bind].
      reflexivity.
    + intros [f Hf].
      assert (Hunder9 : f < 9 -> b1_write_signal f src v s = None).
      { intros Hlt.
        destruct f as [|[|[|[|[|[|[|[|[|f']]]]]]]]]; try lia.
        - reflexivity.
        - cbn [b1_write_signal]. rewrite (m_bind_get_node _ _ _ _ Hsrc). rewrite Hks, decide_False by exact Hnev.
          rewrite (m_bind_modify_node _ _ _ _ _ Hsrc). rewrite m_bind_modify. fold sa.
          unfold m_bind, m_step, notify_dirty. erewrite notify_with_singleton by exact Ha_obs_m.
          cbn [walk_marks mark_dirty]. reflexivity.
        - cbn [b1_write_signal]. rewrite (m_bind_get_node _ _ _ _ Hsrc). rewrite Hks, decide_False by exact Hnev.
          rewrite (m_bind_modify_node _ _ _ _ _ Hsrc). rewrite m_bind_modify. fold sa.
          unfold m_bind, m_step, notify_dirty. erewrite notify_with_singleton by exact Ha_obs_m.
          cbn [walk_marks mark_dirty]. rewrite Ha_memo, Hkm, decide_True by exact Hcm.
          erewrite notify_with_singleton by exact Ha_obs_e.
          cbn [walk_marks mark_check]. reflexivity.
        - cbn [b1_write_signal]. rewrite (m_bind_get_node _ _ _ _ Hsrc). rewrite Hks, decide_False by exact Hnev.
          rewrite (m_bind_modify_node _ _ _ _ _ Hsrc). rewrite m_bind_modify. fold sa.
          rewrite (m_bind_step _ _ _ _ (Hmark 0)).
          cbn [b1_request_flush]. rewrite m_bind_get. fold dm' de'. fold sa_enq.
          rewrite Hlq_enq, Hbd_enq, Hob_enq, bool_decide_nat_0. cbn [andb].
          cbn [b1_run_flush]. rewrite m_bind_get, Hfl_enq, m_bind_modify. reflexivity.
        - cbn [b1_write_signal]. rewrite (m_bind_get_node _ _ _ _ Hsrc). rewrite Hks, decide_False by exact Hnev.
          rewrite (m_bind_modify_node _ _ _ _ _ Hsrc). rewrite m_bind_modify. fold sa.
          rewrite (m_bind_step _ _ _ _ (Hmark 1)).
          cbn [b1_request_flush]. rewrite m_bind_get. fold dm' de'. fold sa_enq.
          rewrite Hlq_enq, Hbd_enq, Hob_enq, bool_decide_nat_0. cbn [andb].
          cbn [b1_run_flush]. rewrite m_bind_get, Hfl_enq, m_bind_modify.
          cbn [b1_drain]. rewrite ?b4_bind_assoc, m_bind_get.
          change (g_queue (with_flush_owed false (with_flushing true sa_enq)) !!
                  g_queue_head (with_flush_owed false (with_flushing true sa_enq)))
            with ((g_queue sa ++ [eff]) !! g_queue_head sa).
          rewrite Hlu_enq, ?b4_bind_assoc, m_bind_modify. reflexivity.
        - cbn [b1_write_signal]. rewrite (m_bind_get_node _ _ _ _ Hsrc). rewrite Hks, decide_False by exact Hnev.
          rewrite (m_bind_modify_node _ _ _ _ _ Hsrc). rewrite m_bind_modify. fold sa.
          rewrite (m_bind_step _ _ _ _ (Hmark 2)).
          cbn [b1_request_flush]. rewrite m_bind_get. fold dm' de'. fold sa_enq.
          rewrite Hlq_enq, Hbd_enq, Hob_enq, bool_decide_nat_0. cbn [andb].
          cbn [b1_run_flush]. rewrite m_bind_get, Hfl_enq, m_bind_modify.
          cbn [b1_drain]. rewrite ?b4_bind_assoc, m_bind_get.
          change (g_queue (with_flush_owed false (with_flushing true sa_enq)) !!
                  g_queue_head (with_flush_owed false (with_flushing true sa_enq)))
            with ((g_queue sa ++ [eff]) !! g_queue_head sa).
          rewrite Hlu_enq, ?b4_bind_assoc, m_bind_modify.
      change (g_queue_head (with_flush_owed false (with_flushing true sa_enq))) with (g_queue_head sa_enq).
      fold sw0.
          cbn [b1_execute_effect]. rewrite ?b4_bind_assoc.
          rewrite (m_bind_get_node _ _ _ _ Hsw0_eff), ?b4_bind_assoc.
          change (n_disposed de') with (n_disposed de).
          change (n_freshness de') with Check.
          rewrite Hdze, decide_True by reflexivity.
          rewrite ?b4_bind_assoc. reflexivity.
        - cbn [b1_write_signal]. rewrite (m_bind_get_node _ _ _ _ Hsrc). rewrite Hks, decide_False by exact Hnev.
          rewrite (m_bind_modify_node _ _ _ _ _ Hsrc). rewrite m_bind_modify. fold sa.
          rewrite (m_bind_step _ _ _ _ (Hmark 3)).
          cbn [b1_request_flush]. rewrite m_bind_get. fold dm' de'. fold sa_enq.
          rewrite Hlq_enq, Hbd_enq, Hob_enq, bool_decide_nat_0. cbn [andb].
          cbn [b1_run_flush]. rewrite m_bind_get, Hfl_enq, m_bind_modify.
          cbn [b1_drain]. rewrite ?b4_bind_assoc, m_bind_get.
          change (g_queue (with_flush_owed false (with_flushing true sa_enq)) !!
                  g_queue_head (with_flush_owed false (with_flushing true sa_enq)))
            with ((g_queue sa ++ [eff]) !! g_queue_head sa).
          rewrite Hlu_enq, ?b4_bind_assoc, m_bind_modify.
      change (g_queue_head (with_flush_owed false (with_flushing true sa_enq))) with (g_queue_head sa_enq).
      fold sw0.
          cbn [b1_execute_effect]. rewrite ?b4_bind_assoc.
          rewrite (m_bind_get_node _ _ _ _ Hsw0_eff), ?b4_bind_assoc.
          change (n_disposed de') with (n_disposed de).
          change (n_freshness de') with Check.
          rewrite Hdze, decide_True by reflexivity.
          rewrite ?b4_bind_assoc.
          cbn [b1_resolve_walk]. rewrite ?b4_bind_assoc.
          rewrite (m_bind_get_node _ _ _ _ Hsw0_eff), ?b4_bind_assoc.
          rewrite m_bind_get. change (n_freshness de') with Check.
          rewrite decide_True by reflexivity. rewrite Hsw0_sl, ?b4_bind_assoc.
          reflexivity.
        - cbn [b1_write_signal]. rewrite (m_bind_get_node _ _ _ _ Hsrc). rewrite Hks, decide_False by exact Hnev.
          rewrite (m_bind_modify_node _ _ _ _ _ Hsrc). rewrite m_bind_modify. fold sa.
          rewrite (m_bind_step _ _ _ _ (Hmark 4)).
          cbn [b1_request_flush]. rewrite m_bind_get. fold dm' de'. fold sa_enq.
          rewrite Hlq_enq, Hbd_enq, Hob_enq, bool_decide_nat_0. cbn [andb].
          cbn [b1_run_flush]. rewrite m_bind_get, Hfl_enq, m_bind_modify.
          cbn [b1_drain]. rewrite ?b4_bind_assoc, m_bind_get.
          change (g_queue (with_flush_owed false (with_flushing true sa_enq)) !!
                  g_queue_head (with_flush_owed false (with_flushing true sa_enq)))
            with ((g_queue sa ++ [eff]) !! g_queue_head sa).
          rewrite Hlu_enq, ?b4_bind_assoc, m_bind_modify.
      change (g_queue_head (with_flush_owed false (with_flushing true sa_enq))) with (g_queue_head sa_enq).
      fold sw0.
          cbn [b1_execute_effect]. rewrite ?b4_bind_assoc.
          rewrite (m_bind_get_node _ _ _ _ Hsw0_eff), ?b4_bind_assoc.
          change (n_disposed de') with (n_disposed de).
          change (n_freshness de') with Check.
          rewrite Hdze, decide_True by reflexivity.
          rewrite ?b4_bind_assoc.
          cbn [b1_resolve_walk]. rewrite ?b4_bind_assoc.
          rewrite (m_bind_get_node _ _ _ _ Hsw0_eff), ?b4_bind_assoc.
          rewrite m_bind_get. change (n_freshness de') with Check.
          rewrite decide_True by reflexivity. rewrite Hsw0_sl, ?b4_bind_assoc.
          cbn [b1_update_if_necessary]. rewrite ?b4_bind_assoc.
          rewrite (m_bind_get_node _ _ _ _ Hsw0_memo).
          change (n_kind dm') with (n_kind dm).
          rewrite Hkm, Hstl_dm, ?b4_bind_assoc.
          reflexivity.
        - change 8 with (8 + 0). rewrite Hstep8. reflexivity. }
      destruct f as [|[|[|[|[|[|[|[|[|f']]]]]]]]];
        [rewrite Hunder9 in Hf by lia; discriminate..|].
      replace (S (S (S (S (S (S (S (S (S f'))))))))) with (8 + S f') in Hf by lia.
      rewrite Hstep8 in Hf.
      eapply bind_inv in Hf as (sw1 & [] & Href_w & Hrest).
      eapply bind_inv in Hrest as (sw1' & d_m0 & Hgetm_w & Hrest).
      rewrite b4_bind_ret_l in Hrest.
      eapply bind_inv in Hrest as (sw2 & d' & Hget_d'_w & Hrest).
      eapply bind_inv in Hrest as (sw3 & d_m & Hget_dm_w & Hrest).
      eapply bind_inv in Hrest as (sw4 & s1_ & Hget_s1_w & Hrest).
      eapply bind_inv in Hrest as (sw5 & [] & Hmd_w & Hrest).
      eapply bind_inv in Hrest as (sw6 & [] & Hrw_w & Hrest).
      eapply bind_inv in Hrest as (sw7 & d'' & Hget_d''_w & Hrest).
      eapply bind_inv in Hrest as (sw8 & [] & Hres_w & Hrest).
      eapply bind_inv in Hrest as (sw9 & d''' & Hget_d'''_w & Hrest).
      eapply bind_inv in Hrest as (sw10 & [] & Hclr_w & Hrest).
      eapply bind_inv in Hrest as (sw11 & [] & Hre_w & Hrest).
      eapply bind_inv in Hrest as (sw12 & [] & Hdr_w & Hfl_end).
      unfold m_modify in Hfl_end. simplify_eq.
      pose proof (queue_rel_sym _ _ Hrel_0) as Hrel_0'.
      set (fmax := S (S (S (S (S (S f')))))).
      assert (Href_w' : b1_refresh fmax memo sw0 = Some (sw1, tt))
        by (eapply (B1Mrefresh _ (b1_mono_all (S f'))); [lia|exact Href_w]).
      destruct (B1Qrefresh _ (b1_qrel_all fmax) memo _ _ _ _ Href_w' Hrel_0') as (sf1 & Href_f & Hrel1).
      destruct (qrel_get_node memo _ _ _ _ Hgetm_w Hrel1) as (sf1' & Hgetm_f & Hrel1').
      destruct (qrel_get_node eff _ _ _ _ Hget_d'_w Hrel1') as (sf2 & Hget_d'_f & Hrel2).
      destruct (qrel_get_node memo _ _ _ _ Hget_dm_w Hrel2) as (sf3 & Hget_dm_f & Hrel3).
      unfold m_get in Hget_s1_w. injection Hget_s1_w as <- <-.
      assert (Hmd_f : exists sf5,
        (if decide (n_freshness d' = Check /\ stale_live d_m = true /\ (memo ∈ g_running sf3 \/ memo ∈ g_checking sf3))
         then m_step (mark_dirty fmax eff) else m_return tt) sf3 = Some (sf5, tt) /\
        queue_rel sw5 sf5).
      { destruct (queue_rel_fields _ _ Hrel3) as (_&_&_&_&_&_&_&_&_&_&_&Hrun&Hchk&_).
        rewrite <- Hrun, <- Hchk.
        destruct (decide _); [|unfold m_return in Hmd_w; simplify_eq; by exists sf3].
        unfold m_step in Hmd_w |- *. destruct (mark_dirty (2 + S f') eff sw3) eqn:Em; simplify_eq.
        assert (Em' : mark_dirty fmax eff sw3 = Some sw5) by (eapply (mark_dirty_mono (2 + S f')); [lia|exact Em]).
        destruct (qrel_mark_dirty _ _ _ _ _ Hrel3 Em') as (sf5 & Em2 & Hrel5). rewrite Em2. by exists sf5. }
      destruct Hmd_f as (sf5 & Hmd_f & Hrel5).
      assert (Hrw_w' : b1_resolve_walk fmax eff 1 sw5 = Some (sw6, tt))
        by (eapply (B1Mwalk _ (b1_mono_all (2 + S f'))); [lia|exact Hrw_w]).
      destruct (B1Qwalk _ (b1_qrel_all fmax) eff 1 _ _ _ _ Hrw_w' Hrel5) as (sf6 & Hrw_f & Hrel6).
      destruct (qrel_get_node eff _ _ _ _ Hget_d''_w Hrel6) as (sf7 & Hget_d''_f & Hrel7).
      assert (Hres_f : exists sf8,
        (if decide (n_freshness d'' = Check)
         then let* _ := m_modify (ghost_resolved eff) in modify_node eff (node_with_freshness Clean)
         else m_return tt) sf7 = Some (sf8, tt) /\ queue_rel sw8 sf8).
      { destruct (decide (n_freshness d'' = Check)); [|unfold m_return in Hres_w; simplify_eq; by exists sf7].
        eapply (qrel_bind_of Hres_w Hrel7); [intros ????; qrel_pure_tac|intros [] ????; qrel_pure_tac]. }
      destruct Hres_f as (sf8 & Hres_f & Hrel8).
      destruct (qrel_get_node eff _ _ _ _ Hget_d'''_w Hrel8) as (sf9 & Hget_d'''_f & Hrel9).
      destruct (qrel_modify_node eff (fun x => node_with_freshness Clean (node_with_queued false x)) _ _ _ _ Hclr_w Hrel9) as (sf10 & Hclr_f & Hrel10).
      assert (Hre_f : exists sf11,
        (if decide (n_freshness d''' = Dirty) then b1_run_effect fmax eff else m_return tt) sf10 = Some (sf11, tt) /\
        queue_rel sw11 sf11).
      { destruct (decide (n_freshness d''' = Dirty)); [|unfold m_return in Hre_w; simplify_eq; by exists sf10].
        assert (Hre_w' : b1_run_effect fmax eff sw10 = Some (sw11, tt))
          by (eapply (B1Mrun_effect _ (b1_mono_all (3 + S f'))); [lia|exact Hre_w]).
        by eapply (B1Qrun_effect _ (b1_qrel_all fmax)). }
      destruct Hre_f as (sf11 & Hre_f & Hrel11).
      assert (Hdr_w' : b1_drain fmax sw11 = Some (sw12, tt))
        by (eapply (B1Mdrain _ (b1_mono_all (4 + S f'))); [lia|exact Hdr_w]).
      destruct (B1Qdrain _ (b1_qrel_all fmax) _ _ _ _ Hdr_w' Hrel11) as (sf12 & Hdr_f & Hrel12).
      destruct (b1_drain_resets _ _ _ Hdr_w') as [Hq1 Hh1].
      destruct (b1_drain_resets _ _ _ Hdr_f) as [Hq2 Hh2].
      assert (Heq : sw12 = sf12) by (apply queue_rel_drained_eq; assumption).
      subst sf12.
      exists fmax. rewrite Hpre_f. try fold sf0.
      unfold m_bind at 1. rewrite Href_f. cbn [option_bind].
      unfold m_bind at 1. rewrite Hgetm_f. cbn [option_bind].
      unfold m_bind at 1. rewrite Hget_d'_f. cbn [option_bind].
      unfold m_bind at 1. rewrite Hget_dm_f. cbn [option_bind].
      rewrite m_bind_get.
      unfold m_bind at 1. rewrite Hmd_f. cbn [option_bind].
      unfold m_bind at 1. rewrite Hrw_f. cbn [option_bind].
      unfold m_bind at 1. rewrite Hget_d''_f. cbn [option_bind].
      unfold m_bind at 1. rewrite Hres_f. cbn [option_bind].
      unfold m_bind at 1. rewrite Hget_d'''_f. cbn [option_bind].
      unfold m_bind at 1. rewrite Hclr_f. cbn [option_bind].
      unfold m_bind at 1. rewrite Hre_f. cbn [option_bind].
      unfold m_bind at 1. rewrite Hdr_f. cbn [option_bind].
      reflexivity.
Qed.
References

B6: Published ready-memo outcome cutoff refinement

Definition outcome_moved
    (prev_st : status) (prev_fail : option val) (prev_val : val)
    (next_st : status) (next_fail : option val) (next_val : val)
    (cmp : comparer) (n : node_id) : M bool :=
  if decide (next_st <> prev_st \/ next_fail <> prev_fail) then m_return true
  else
    match cmp with
    | CmpEq => m_return (bool_decide (next_val <> prev_val))
    | CmpNever => m_return true
    | CmpThrows y =>
      let* _ := modify_node n (fun x =>
        node_with_status StError (node_with_failure (Some y) (node_with_value prev_val x))) in
      m_return true
    end.
References
Definition ready_value_moved
    (prev_val next_val : val) (cmp : comparer) (n : node_id) : M bool :=
  match cmp with
  | CmpEq => m_return (bool_decide (next_val <> prev_val))
  | CmpNever => m_return true
  | CmpThrows y =>
    let* _ := modify_node n (fun x =>
      node_with_status StError (node_with_failure (Some y) (node_with_value prev_val x))) in
    m_return true
  end.
References
✓ No axioms

When previous and next memo states are both ready (StNone, failure = None), outcome_moved simplifies directly to value comparison without status or failure checks.

Refines RunTopCore / Memo.Recompute steady-state cutoff on ready-published memos: the status/failure difference guard is identically false, reducing outcome_moved to ready_value_moved (and for CmpEq, to bool_decide (next_val <> prev_val)).

Arguments

prev_st : State.status
previous memo status.
prev_fail : option val
previous memo failure.
prev_val : val
previous memo value.
next_st : State.status
newly computed memo status.
next_fail : option val
newly computed memo failure.
next_val : val
newly computed memo value.
cmp : comparer
memo equality comparer.
n : node_id
memo node id.
s : State.state
engine state.
Theorem memo_published_ready_cutoff_eq
    prev_st prev_fail prev_val next_st next_fail next_val cmp n s :
  prev_st = StNone ->
  prev_fail = None ->
  next_st = StNone ->
  next_fail = None ->
  outcome_moved prev_st prev_fail prev_val next_st next_fail next_val cmp n s =
    ready_value_moved prev_val next_val cmp n s /\
  (cmp = CmpEq ->
    outcome_moved prev_st prev_fail prev_val next_st next_fail next_val cmp n s =
      Some (s, bool_decide (next_val <> prev_val))).

Arguments

prev_st : State.status
previous memo status.
prev_fail : option val
previous memo failure.
prev_val : val
previous memo value.
next_st : State.status
newly computed memo status.
next_fail : option val
newly computed memo failure.
next_val : val
newly computed memo value.
cmp : comparer
memo equality comparer.
n : node_id
memo node id.
s : State.state
engine state.
Qed
Proof.
  intros -> -> -> ->. unfold outcome_moved, ready_value_moved.
  rewrite decide_False by (intros [H|H]; exact (H eq_refl)).
  split; [reflexivity|].
  intros ->. reflexivity.
Qed.
References

Uses

1 more
  • decide_False

B7. Bit-tagged edge handles and observer slots

Lemma N_lt_pow2_of_bits (x n : N) :
  (forall k, (n <= k)%N -> N.testbit x k = false) -> (x < 2 ^ n)%N.
Qed
Proof.
  intros Hbits.
  assert (Heq : N.land x (N.ones n) = x).
  { apply N.bits_inj. intros k. rewrite N.land_spec.
    destruct (N.ltb_spec0 k n) as [Hk|Hk].
    - rewrite N.ones_spec_low; [apply andb_true_r|exact Hk].
    - rewrite N.ones_spec_high by lia. rewrite Hbits; [apply andb_false_l|lia]. }
  rewrite <- Heq, N.land_ones.
  apply N.mod_upper_bound, N.pow_nonzero. discriminate.
Qed.
References

Uses

  • false
  • N
  • andb_false_l
  • andb_true_r
  • N.bits_inj
  • N.land
  • N.land_ones
  • N.land_spec
7 more
  • N.ltb_spec0
  • N.mod_upper_bound
  • N.ones
  • N.ones_spec_high
  • N.ones_spec_low
  • N.pow_nonzero
  • N.testbit
Lemma N_testbit_lt_pow2 (x n k : N) :
  (x < 2 ^ n)%N -> (n <= k)%N -> N.testbit x k = false.
Qed
Proof.
  intros Hlt Hk.
  rewrite <- (N.mod_small x (2 ^ n)%N Hlt).
  apply N.mod_pow2_bits_high. exact Hk.
Qed.
References

Uses

  • false
  • N
  • N.mod_pow2_bits_high
  • N.mod_small
  • N.testbit
Definition edge_signal_bit : N := (2 ^ 29)%N.
References

Uses

  • N
Definition edge_memo_bit : N := (2 ^ 28)%N.
References

Uses

  • N
Definition edge_idx_mask : N := N.ones 28.
References

Uses

  • N
  • N.ones
Definition edge_tag_mask : N := N.lor edge_signal_bit edge_memo_bit.
References
Definition encode_edge_handle (tag_sig tag_memo : bool) (idx : N) : N :=
  N.lor (if tag_sig then edge_signal_bit else 0%N)
        (N.lor (if tag_memo then edge_memo_bit else 0%N) idx).
References
Definition decode_edge_sig (w : N) : bool := N.testbit w 29.
References

Uses

  • bool
  • N
  • N.testbit
Definition decode_edge_memo (w : N) : bool := N.testbit w 28.
References

Uses

  • bool
  • N
  • N.testbit
Definition decode_edge_idx (w : N) : N := N.land w edge_idx_mask.
References

Uses

Definition retag_edge_idx (w new_idx : N) : N :=
  N.lor new_idx (N.land w edge_tag_mask).
References

Uses

✓ No axioms

Bit-tagging edge handles with SignalBit = 2^29 and MemoBit = 2^28 leaves all idx < 2^28 lossless, stays within 31-bit Smi bounds (< 2^30), preserves mutual exclusion of signal/memo tags, and makes index-only swap updates preserve tag bits.

Arguments

tag_sig : bool
signal tag bit placed at bit 29.
tag_memo : bool
memo tag bit placed at bit 28.
idx : N
edge index below 2^28.
new_idx : N
replacement edge index below 2^28.
Theorem edge_handle_tag_lossless
    (tag_sig tag_memo : bool) (idx new_idx : N) :
  (idx < 2 ^ 28)%N ->
  (new_idx < 2 ^ 28)%N ->
  ~ (tag_sig = true /\ tag_memo = true) ->
  let w := encode_edge_handle tag_sig tag_memo idx in
  decode_edge_sig w = tag_sig /\
  decode_edge_memo w = tag_memo /\
  decode_edge_idx w = idx /\
  (w < 2 ^ 30)%N /\
  ~ (decode_edge_sig w = true /\ decode_edge_memo w = true) /\
  retag_edge_idx w new_idx = encode_edge_handle tag_sig tag_memo new_idx.

Arguments

tag_sig : bool
signal tag bit placed at bit 29.
tag_memo : bool
memo tag bit placed at bit 28.
idx : N
edge index below 2^28.
new_idx : N
replacement edge index below 2^28.
Qed
Proof.
  intros Hidx Hnew Hmut.
  cbv zeta.
  assert (Hsig : decode_edge_sig (encode_edge_handle tag_sig tag_memo idx) = tag_sig).
  { unfold decode_edge_sig, encode_edge_handle, edge_signal_bit, edge_memo_bit.
    repeat rewrite N.lor_spec.
    rewrite (N_testbit_lt_pow2 idx 28 29 Hidx) by lia.
    destruct tag_sig, tag_memo;
      try rewrite N.bits_0;
      try rewrite N.pow2_bits_true;
      try rewrite N.pow2_bits_false by lia;
      reflexivity. }
  assert (Hmemo : decode_edge_memo (encode_edge_handle tag_sig tag_memo idx) = tag_memo).
  { unfold decode_edge_memo, encode_edge_handle, edge_signal_bit, edge_memo_bit.
    repeat rewrite N.lor_spec.
    rewrite (N_testbit_lt_pow2 idx 28 28 Hidx) by lia.
    destruct tag_sig, tag_memo;
      try rewrite N.bits_0;
      try rewrite N.pow2_bits_true;
      try rewrite N.pow2_bits_false by lia;
      reflexivity. }
  assert (Hdidx : decode_edge_idx (encode_edge_handle tag_sig tag_memo idx) = idx).
  { unfold decode_edge_idx, encode_edge_handle, edge_signal_bit, edge_memo_bit, edge_idx_mask.
    apply N.bits_inj. intros k.
    rewrite N.land_spec. repeat rewrite N.lor_spec.
    destruct (N.ltb_spec0 k 28) as [Hk|Hk].
    - rewrite N.ones_spec_low by exact Hk.
      destruct tag_sig, tag_memo;
        try rewrite N.bits_0;
        try rewrite (N.pow2_bits_false 29 k) by lia;
        try rewrite (N.pow2_bits_false 28 k) by lia;
        simpl; apply andb_true_r.
    - rewrite N.ones_spec_high by lia.
      rewrite andb_false_r. symmetry.
      exact (N_testbit_lt_pow2 idx 28 k Hidx ltac:(lia)). }
  assert (Hlt : (encode_edge_handle tag_sig tag_memo idx < 2 ^ 30)%N).
  { apply N_lt_pow2_of_bits. intros k Hk.
    unfold encode_edge_handle, edge_signal_bit, edge_memo_bit.
    repeat rewrite N.lor_spec.
    rewrite (N_testbit_lt_pow2 idx 28 k Hidx) by lia.
    destruct tag_sig, tag_memo;
      repeat rewrite N.bits_0;
      repeat rewrite N.pow2_bits_false by lia;
      reflexivity. }
  assert (Hmut' : ~ (decode_edge_sig (encode_edge_handle tag_sig tag_memo idx) = true /\
                     decode_edge_memo (encode_edge_handle tag_sig tag_memo idx) = true)).
  { rewrite Hsig, Hmemo. exact Hmut. }
  assert (Hretag : retag_edge_idx (encode_edge_handle tag_sig tag_memo idx) new_idx =
                   encode_edge_handle tag_sig tag_memo new_idx).
  { unfold retag_edge_idx, encode_edge_handle, edge_tag_mask, edge_signal_bit, edge_memo_bit.
    apply N.bits_inj. intros k.
    repeat rewrite N.lor_spec, ?N.land_spec, ?N.lor_spec.
    destruct (N.ltb_spec0 k 28) as [Hk|Hk].
    - destruct tag_sig, tag_memo;
        repeat rewrite (N.pow2_bits_false 29 k) by lia;
        repeat rewrite (N.pow2_bits_false 28 k) by lia;
        repeat rewrite N.bits_0;
        destruct (N.testbit new_idx k), (N.testbit idx k); reflexivity.
    - destruct tag_sig, tag_memo;
        rewrite (N_testbit_lt_pow2 idx 28 k Hidx ltac:(lia));
        rewrite (N_testbit_lt_pow2 new_idx 28 k Hnew ltac:(lia));
        destruct (N.eqb_spec k 29), (N.eqb_spec k 28); subst; try lia;
        repeat rewrite (N.pow2_bits_false 28 29) by lia;
        repeat rewrite (N.pow2_bits_false 29 28) by lia;
        repeat rewrite (N.pow2_bits_false 29 k) by lia;
        repeat rewrite (N.pow2_bits_false 28 k) by lia;
        try rewrite N.pow2_bits_true;
        repeat rewrite N.bits_0;
        reflexivity. }
  repeat split; assumption.
Qed.
References

Uses

19 more
Definition obs_node_slot_bit : N := (2 ^ 29)%N.
References

Uses

  • N
Definition obs_slot_mask : N := N.ones 29.
References

Uses

  • N
  • N.ones
Definition encode_observer_slot (is_node : bool) (slot : N) : N :=
  N.lor (if is_node then obs_node_slot_bit else 0%N) slot.
References

Uses

Definition decode_obs_is_node (w : N) : bool := N.testbit w 29.
References

Uses

  • bool
  • N
  • N.testbit
Definition decode_obs_slot (w : N) : N := N.land w obs_slot_mask.
References

Uses

✓ No axioms

Tagging an observer slot index < 2^29 with NodeSlotBit = 2^29 encodes both the is_node bit and slot losslessly within 30 bits (< 2^30, matching EdgeHandle.NodeSlotBit = 0x20000000 and SlotMask = 0x1FFFFFFF in Core.fs:417,421).

Arguments

is_node : bool
true when observer slot holds a Node, false for non-node observer handles.
slot : N
observer slot index below 2^29.
Theorem observer_slot_tag_lossless (is_node : bool) (slot : N) :
  (slot < 2 ^ 29)%N ->
  let w := encode_observer_slot is_node slot in
  decode_obs_is_node w = is_node /\
  decode_obs_slot w = slot /\
  (w < 2 ^ 30)%N.

Arguments

is_node : bool
true when observer slot holds a Node, false for non-node observer handles.
slot : N
observer slot index below 2^29.
Qed
Proof.
  intros Hslot. cbv zeta.
  split.
  - unfold decode_obs_is_node, encode_observer_slot, obs_node_slot_bit.
    rewrite N.lor_spec, (N_testbit_lt_pow2 slot 29 29 Hslot) by lia.
    destruct is_node; [rewrite N.pow2_bits_true|rewrite N.bits_0]; reflexivity.
  - split.
    + unfold decode_obs_slot, encode_observer_slot, obs_node_slot_bit, obs_slot_mask.
      apply N.bits_inj. intros k.
      rewrite N.land_spec, N.lor_spec.
      destruct (N.ltb_spec0 k 29) as [Hk|Hk].
      * rewrite N.ones_spec_low by exact Hk.
        destruct is_node; [rewrite (N.pow2_bits_false 29 k) by lia|rewrite N.bits_0];
        simpl; apply andb_true_r.
      * rewrite N.ones_spec_high by lia.
        rewrite andb_false_r. symmetry.
        exact (N_testbit_lt_pow2 slot 29 k Hslot ltac:(lia)).
    + apply N_lt_pow2_of_bits. intros k Hk.
      unfold encode_observer_slot, obs_node_slot_bit.
      rewrite N.lor_spec, (N_testbit_lt_pow2 slot 29 k Hslot) by lia.
      destruct is_node; [repeat rewrite N.pow2_bits_false by lia|repeat rewrite N.bits_0]; reflexivity.
Qed.
References

Uses

12 more
  • N
  • andb_false_r
  • andb_true_r
  • N.bits_0
  • N.bits_inj
  • N.land_spec
  • N.lor_spec
  • N.ltb_spec0
  • N.ones_spec_high
  • N.ones_spec_low
  • N.pow2_bits_false
  • N.pow2_bits_true
Record tagged_observer_set := {
  tobserver_items : list (node_id * N);
  tobserver_latest : nat
}.
References

Uses

Definition empty_tagged_observer_set : tagged_observer_set :=
  {| tobserver_items := []; tobserver_latest := 0 |}.
References
Definition untag_observer_set (o : tagged_observer_set) : observer_set :=
  {| observer_items := (fun '(c, s) => (c, N.to_nat (decode_obs_slot s))) <$> tobserver_items o;
     observer_latest := tobserver_latest o |}.
References
Definition tagged_observers_remove_at (c : node_id) (th : N) (o : tagged_observer_set)
    : option (tagged_observer_set * option (node_id * N * nat)) :=
  let items := tobserver_items o in
  let last := pred (length items) in
  let h := N.to_nat (decode_edge_idx th) in
  match items !! h with
  | Some (c', _) =>
    if decide (c' = c) then
      if decide (h = last) then
        Some ({| tobserver_items := take last items; tobserver_latest := tobserver_latest o |}, None)
      else
        match items !! last with
        | Some (m, ms) =>
          Some ({| tobserver_items := <[h := (m, ms)]> (take last items);
                   tobserver_latest := if decide (tobserver_latest o = last) then h else tobserver_latest o |},
                Some (m, ms, h))
        | None => None
        end
    else None
  | None => None
  end.
References

Uses

7 more
  • nat
  • option
  • pred
  • N
  • N.to_nat
  • decide
  • list.take
Record TaggedEdgeWorld := {
  tw_observers : gmap node_id tagged_observer_set;
  tw_sources : gmap node_id (list (node_id * option N) * nat)
}.
References

Uses

Definition untag_source_list (p : list (node_id * option N) * nat) : source_list :=
  {| source_slots :=
       (fun '(s, mh) => (s, (fun th => N.to_nat (decode_edge_idx th)) <$> (mh : option N)))
         <$> (p.1 : list (node_id * option N));
     source_cursor := p.2 |}.
References
Definition untag_world (tw : TaggedEdgeWorld) : EdgeWorld :=
  {| world_observers := untag_observer_set <$> tw_observers tw;
     world_sources := untag_source_list <$> tw_sources tw |}.
References
Definition tobservers_of (tw : TaggedEdgeWorld) (s : node_id) : tagged_observer_set :=
  default empty_tagged_observer_set (tw_observers tw !! s).
References
Definition tsources_of (tw : TaggedEdgeWorld) (c : node_id) : list (node_id * option N) * nat :=
  default ([], 0) (tw_sources tw !! c).
References

Uses

Definition tslot_at (tw : TaggedEdgeWorld) (c : node_id) (i : nat) : option (node_id * option N) :=
  (tsources_of tw c).1 !! i.
References
Definition tset_observers (s : node_id) (o : tagged_observer_set) (tw : TaggedEdgeWorld) : TaggedEdgeWorld :=
  {| tw_observers := <[s := o]> (tw_observers tw); tw_sources := tw_sources tw |}.
References
Definition tset_sources (c : node_id) (l : list (node_id * option N) * nat) (tw : TaggedEdgeWorld) : TaggedEdgeWorld :=
  {| tw_observers := tw_observers tw; tw_sources := <[c := l]> (tw_sources tw) |}.
References
Definition tset_handle (c : node_id) (i : nat) (mth : option N) (tw : TaggedEdgeWorld) : TaggedEdgeWorld :=
  let '(slots, cur) := tsources_of tw c in
  tset_sources c (alter (fun '(s, _) => (s, mth)) i slots, cur) tw.
References
Definition tmoved_handle (c : node_id) (i : nat) (h : nat) (tw : TaggedEdgeWorld) : option TaggedEdgeWorld :=
  match tslot_at tw c i with
  | Some (_, Some th) =>
    Some (tset_handle c i (Some (retag_edge_idx th (N.of_nat h))) tw)
  | _ => None
  end.
References
Definition tagged_remove_edge (self s : node_id) (th : N) (tw : TaggedEdgeWorld) : option TaggedEdgeWorld :=
  match tagged_observers_remove_at self th (tobservers_of tw s) with
  | None => None
  | Some (o', None) => Some (tset_observers s o' tw)
  | Some (o', Some (m, ms, h)) =>
    tmoved_handle m (N.to_nat (decode_obs_slot ms)) h (tset_observers s o' tw)
  end.
References
Definition tagged_sources_remove_slot (self : node_id) (i : nat) (tw : TaggedEdgeWorld) : option TaggedEdgeWorld :=
  match tslot_at tw self i with
  | Some (s, Some th) => tset_handle self i None <$> tagged_remove_edge self s th tw
  | _ => None
  end.
References
Lemma untag_observers_of tw s :
  observers_of (untag_world tw) s = untag_observer_set (tobservers_of tw s).
Qed
Proof.
  unfold observers_of, tobservers_of, untag_world. simpl.
  rewrite lookup_fmap.
  destruct (tw_observers tw !! s); reflexivity.
Qed.
References
Lemma untag_sources_of tw c :
  sources_of (untag_world tw) c = untag_source_list (tsources_of tw c).
Qed
Proof.
  unfold sources_of, tsources_of, untag_world. simpl.
  rewrite lookup_fmap.
  destruct (tw_sources tw !! c); reflexivity.
Qed.
References
Lemma untag_set_observers s o tw :
  untag_world (tset_observers s o tw) = set_observers s (untag_observer_set o) (untag_world tw).
Qed
Proof.
  unfold untag_world, tset_observers, set_observers. simpl.
  f_equal. rewrite fmap_insert. reflexivity.
Qed.
References
Lemma list_fmap_alter {A B} (f : A -> B) (g1 : A -> A) (g2 : B -> B) (i : nat) (l : list A) :
  (forall a, f (g1 a) = g2 (f a)) ->
  f <$> alter g1 i l = alter g2 i (f <$> l).
Qed
Proof.
  intros Hfg. revert i.
  induction l as [|a l IH]; intros [|i]; simpl; [reflexivity|reflexivity| |].
  - rewrite Hfg. reflexivity.
  - f_equal. apply IH.
Qed.
References

Uses

  • list
  • nat
  • alter
Lemma untag_set_handle c i mth tw :
  untag_world (tset_handle c i mth tw) =
  set_handle c i ((fun th => N.to_nat (decode_edge_idx th)) <$> mth) (untag_world tw).
Qed
Proof.
  unfold tset_handle, set_handle.
  pose proof (untag_sources_of tw c) as Hsrc.
  destruct (tsources_of tw c) as [slots cur] eqn:Ec.
  unfold untag_world at 1, tset_sources, set_sources. simpl.
  rewrite Hsrc. unfold untag_source_list at 2. simpl.
  f_equal. rewrite fmap_insert. f_equal. f_equal.
  unfold untag_source_list. simpl. f_equal.
  apply list_fmap_alter. intros [s' ?]. destruct mth; reflexivity.
Qed.
References
Lemma take_fmap {A B} (f : A -> B) n (l : list A) :
  take n (f <$> l) = f <$> take n l.
Qed
Proof. apply firstn_map. Qed.
References

Uses

  • list
  • firstn_map
  • list.take
Lemma untag_observers_remove_at c th o o' mres :
  tagged_observers_remove_at c th o = Some (o', mres) ->
  observers_remove_at c (N.to_nat (decode_edge_idx th)) (untag_observer_set o) =
  Some (untag_observer_set o', (fun '(m, ms, h) => (m, N.to_nat (decode_obs_slot ms), h)) <$> mres).
Qed
Proof.
  intros Hrem.
  unfold tagged_observers_remove_at in Hrem.
  unfold observers_remove_at, untag_observer_set in *. simpl.
  rewrite length_fmap, !list_lookup_fmap.
  set (h := N.to_nat (decode_edge_idx th)) in *.
  destruct (tobserver_items o !! h) as [[c' cs]|] eqn:Eh; [|discriminate].
  simpl. destruct (decide (c' = c)); [|discriminate].
  destruct (decide (h = pred (length (tobserver_items o)))) as [Hlast|Hnlast].
  - simplify_eq. simpl. rewrite take_fmap. reflexivity.
  - destruct (tobserver_items o !! pred (length (tobserver_items o))) as [[m ms]|] eqn:Elast; [|discriminate].
    simplify_eq. simpl.
    rewrite take_fmap, list_fmap_insert. reflexivity.
Qed.
References

Uses

7 more
  • length
  • pred
  • N.to_nat
  • decide
  • list.length_fmap
  • list.list_fmap_insert
  • list.list_lookup_fmap
Lemma decode_edge_idx_retag (th : N) (h : nat) :
  (N.of_nat h < 2 ^ 28)%N ->
  decode_edge_idx (retag_edge_idx th (N.of_nat h)) = N.of_nat h /\
  decode_edge_sig (retag_edge_idx th (N.of_nat h)) = decode_edge_sig th /\
  decode_edge_memo (retag_edge_idx th (N.of_nat h)) = decode_edge_memo th.
Qed
Proof.
  intros Hh.
  repeat split.
  - unfold decode_edge_idx, retag_edge_idx, edge_idx_mask, edge_tag_mask, edge_signal_bit, edge_memo_bit.
    apply N.bits_inj. intros k.
    rewrite !N.land_spec, !N.lor_spec, N.land_spec.
    destruct (N.ltb_spec0 k 28) as [Hk|Hk].
    + rewrite N.ones_spec_low by exact Hk.
      rewrite N.lor_spec, (N.pow2_bits_false 29 k) by lia.
      rewrite (N.pow2_bits_false 28 k) by lia.
      simpl. rewrite !andb_false_r, orb_false_r, andb_true_r. reflexivity.
    + rewrite N.ones_spec_high by lia.
      rewrite andb_false_r. symmetry.
      exact (N_testbit_lt_pow2 (N.of_nat h) 28 k Hh ltac:(lia)).
  - unfold decode_edge_sig, retag_edge_idx, edge_tag_mask, edge_signal_bit, edge_memo_bit.
    rewrite N.lor_spec, N.land_spec, N.lor_spec.
    rewrite (N_testbit_lt_pow2 (N.of_nat h) 28 29 Hh) by lia.
    rewrite N.pow2_bits_true, (N.pow2_bits_false 28 29) by lia.
    simpl. rewrite andb_true_r. reflexivity.
  - unfold decode_edge_memo, retag_edge_idx, edge_tag_mask, edge_signal_bit, edge_memo_bit.
    rewrite N.lor_spec, N.land_spec, N.lor_spec.
    rewrite (N_testbit_lt_pow2 (N.of_nat h) 28 28 Hh) by lia.
    rewrite N.pow2_bits_true, (N.pow2_bits_false 29 28) by lia.
    simpl. rewrite andb_true_r. reflexivity.
Qed.
References

Uses

15 more
  • retag_edge_idx
  • nat
  • N
  • andb_false_r
  • andb_true_r
  • orb_false_r
  • N.bits_inj
  • N.land_spec
  • N.lor_spec
  • N.ltb_spec0
  • N.of_nat
  • N.ones_spec_high
  • N.ones_spec_low
  • N.pow2_bits_false
  • N.pow2_bits_true
✓ No axioms

Tag-preserving ObserverList swap-remove refines untagged sources_remove_slot, preserves edge_match on untag_world, and preserves the swapped peer's is_node and source-handle tag bits.

Arguments

self : node_id
computation node removing its source slot i.
i : nat
slot index removed on self.
observed source node referenced by slot i.
th : N
tagged handle stored at self's slot i.
well-matched starting TaggedEdgeWorld.
Theorem tagged_swap_remove_preserves_edge_match
    (self : node_id) (i : nat) (s : node_id) (th : N) (tw : TaggedEdgeWorld) :
  edge_match (untag_world tw) ->
  tslot_at tw self i = Some (s, Some th) ->
  (exists tw',
    tagged_sources_remove_slot self i tw = Some tw' /\
    sources_remove_slot self i (untag_world tw) = Some (untag_world tw') /\
    edge_match (untag_world tw') /\
    (forall o' m ms_tagged h_sw th_sw,
      (N.of_nat h_sw < 2 ^ 28)%N ->
      tagged_observers_remove_at self th (tobservers_of tw s) =
        Some (o', Some (m, ms_tagged, h_sw)) ->
      tslot_at tw m (N.to_nat (decode_obs_slot ms_tagged)) = Some (s, Some th_sw) ->
      tobserver_items (tobservers_of tw' s) !! h_sw = Some (m, ms_tagged) /\
      (exists th', tslot_at tw' m (N.to_nat (decode_obs_slot ms_tagged)) = Some (s, Some th') /\
        decode_edge_idx th' = N.of_nat h_sw /\
        decode_edge_sig th' = decode_edge_sig th_sw /\
        decode_edge_memo th' = decode_edge_memo th_sw))).

Arguments

self : node_id
computation node removing its source slot i.
i : nat
slot index removed on self.
observed source node referenced by slot i.
th : N
tagged handle stored at self's slot i.
well-matched starting TaggedEdgeWorld.
Qed
Proof.
  intros Hm Hslot.
  assert (Huslot : slot_at (untag_world tw) self i = Some (s, Some (N.to_nat (decode_edge_idx th)))).
  { unfold slot_at. rewrite untag_sources_of. unfold tslot_at in Hslot.
    unfold untag_source_list. simpl. rewrite list_lookup_fmap, Hslot. reflexivity. }
  destruct (remove_step self i s (N.to_nat (decode_edge_idx th)) (untag_world tw) Hm Huslot)
    as (w' & Hrem & Hm' & _).
  unfold tagged_sources_remove_slot. rewrite Hslot.
  unfold sources_remove_slot in Hrem. rewrite Huslot in Hrem.
  unfold tagged_remove_edge.
  unfold remove_edge in Hrem.
  assert (Hh : observer_items (observers_of (untag_world tw) s) !! N.to_nat (decode_edge_idx th) = Some (self, i))
    by (apply Hm; exact Huslot).
  rewrite untag_observers_of in Hh.
  unfold untag_observer_set in Hh. simpl in Hh.
  rewrite list_lookup_fmap in Hh.
  destruct (tobserver_items (tobservers_of tw s) !! N.to_nat (decode_edge_idx th)) as [[c' cs]|] eqn:Eth;
    [|discriminate].
  simpl in Hh. injection Hh as Hc' _. subst c'.
  destruct (decide (N.to_nat (decode_edge_idx th) = pred (length (tobserver_items (tobservers_of tw s)))))
    as [Hlast|Hnlast].
  - pose (o' := {| tobserver_items := take (pred (length (tobserver_items (tobservers_of tw s)))) (tobserver_items (tobservers_of tw s));
                   tobserver_latest := tobserver_latest (tobservers_of tw s) |}).
    assert (Ho' : tagged_observers_remove_at self th (tobservers_of tw s) = Some (o', None)).
    { unfold tagged_observers_remove_at. rewrite Eth, decide_True by reflexivity.
      rewrite decide_True by exact Hlast. reflexivity. }
    pose proof (untag_observers_remove_at _ _ _ _ _ Ho') as Huo'.
    rewrite <- untag_observers_of in Huo'. rewrite Huo' in Hrem. simpl in Hrem.
    injection Hrem as <-.
    exists (tset_handle self i None (tset_observers s o' tw)).
    split; [|split; [|split]].
    + unfold tagged_remove_edge. rewrite Ho'. reflexivity.
    + rewrite untag_set_handle, untag_set_observers.
      unfold sources_remove_slot, remove_edge. rewrite Huslot, Huo'. reflexivity.
    + rewrite untag_set_handle, untag_set_observers. exact Hm'.
    + intros o'' m ms_tagged h_sw th_sw _ Hsw _.
      rewrite Ho' in Hsw. discriminate.
  - set (last := pred (length (tobserver_items (tobservers_of tw s)))) in *.
    assert (Hlast_lt : N.to_nat (decode_edge_idx th) < last).
    { pose proof (lookup_lt_Some _ _ _ Eth). lia. }
    assert (Hlen_last : last < length (tobserver_items (tobservers_of tw s))).
    { pose proof (lookup_lt_Some _ _ _ Eth). lia. }
    destruct (lookup_lt_is_Some_2 _ _ Hlen_last) as [[m ms] Elast].
    set (h := N.to_nat (decode_edge_idx th)) in *.
    pose (o' := {| tobserver_items := <[h := (m, ms)]> (take last (tobserver_items (tobservers_of tw s)));
                   tobserver_latest := if decide (tobserver_latest (tobservers_of tw s) = last) then h else tobserver_latest (tobservers_of tw s) |}).
    assert (Ho' : tagged_observers_remove_at self th (tobservers_of tw s) = Some (o', Some (m, ms, h))).
    { unfold tagged_observers_remove_at. fold h last. rewrite Eth, decide_True by reflexivity.
      rewrite decide_False by exact Hnlast. rewrite Elast. reflexivity. }
    pose proof (untag_observers_remove_at _ _ _ _ _ Ho') as Huo'.
    fold h in Huo'.
    rewrite <- untag_observers_of in Huo'. rewrite Huo' in Hrem. simpl in Hrem.
    assert (Hm_obs : observer_items (observers_of (untag_world tw) s) !! last = Some (m, N.to_nat (decode_obs_slot ms))).
    { rewrite untag_observers_of. unfold untag_observer_set. simpl.
      rewrite list_lookup_fmap, Elast. reflexivity. }
    assert (Hm_src : slot_at (untag_world tw) m (N.to_nat (decode_obs_slot ms)) = Some (s, Some last))
      by (apply Hm; exact Hm_obs).
    assert (Hmslot : exists th_m, tslot_at tw m (N.to_nat (decode_obs_slot ms)) = Some (s, Some th_m)).
    { unfold slot_at in Hm_src. rewrite untag_sources_of in Hm_src.
      unfold untag_source_list in Hm_src. simpl in Hm_src. rewrite list_lookup_fmap in Hm_src.
      unfold tslot_at. destruct ((tsources_of tw m).1 !! N.to_nat (decode_obs_slot ms)) as [[sm mthm]|]; [|discriminate].
      destruct mthm as [thm|]; simpl in Hm_src; simplify_eq. eexists. reflexivity. }
    destruct Hmslot as [th_m Htslot_m].
    assert (Hne_pair : ~ (self = m /\ i = N.to_nat (decode_obs_slot ms))).
    { intros [-> Heqi]. rewrite <- Heqi in Hm_src. rewrite Huslot in Hm_src. simplify_eq. }
    assert (Htslot_m' : tslot_at (tset_observers s o' tw) m (N.to_nat (decode_obs_slot ms)) = Some (s, Some th_m))
      by (unfold tslot_at, tsources_of, tset_observers; simpl; exact Htslot_m).
    set (tw1 := tset_handle m (N.to_nat (decode_obs_slot ms)) (Some (retag_edge_idx th_m (N.of_nat h))) (tset_observers s o' tw)).
    pose (tw' := tset_handle self i None tw1).
    exists tw'.
    assert (Hh_lt28 : (N.of_nat h < 2 ^ 28)%N).
    { unfold h. rewrite N2Nat.id. unfold decode_edge_idx, edge_idx_mask.
      rewrite N.land_ones. apply N.mod_upper_bound, N.pow_nonzero. discriminate. }
    assert (Hunwrap : untag_world tw' = set_handle self i None (set_handle m (N.to_nat (decode_obs_slot ms)) (Some h) (set_observers s (untag_observer_set o') (untag_world tw)))).
    { unfold tw', tw1. rewrite !untag_set_handle, untag_set_observers.
      simpl. destruct (decode_edge_idx_retag th_m h Hh_lt28) as [Hd _].
      rewrite Hd, Nat2N.id. reflexivity. }
    split; [|split; [|split]].
    + unfold tagged_remove_edge. rewrite Ho'. unfold tmoved_handle. rewrite Htslot_m'. reflexivity.
    + rewrite Hunwrap. unfold sources_remove_slot, remove_edge. rewrite Huslot, Huo'. reflexivity.
    + rewrite Hunwrap. injection Hrem as Hrem'. unfold sources_moved_in in Hrem'.
      rewrite <- Hrem' in Hm'. exact Hm'.
    + intros o'' m0 ms0 h_sw th_sw Hh_bound Hsw Hslot0.
      rewrite Ho' in Hsw. injection Hsw as <- Hm0 Hms0 <-. subst m0 ms0.
      assert (Hobs_tw' : tw_observers tw' = <[s := o']> (tw_observers tw)).
      { assert (Hobs_th : forall k idx v w, tw_observers (tset_handle k idx v w) = tw_observers w).
        { intros k0 i0 v0 w0. unfold tset_handle, tsources_of, tset_sources.
          destruct (default ([], 0) (tw_sources w0 !! k0)) as [? ?]. reflexivity. }
        unfold tw', tw1. rewrite !Hobs_th. reflexivity. }
      split.
      * unfold tobservers_of. rewrite Hobs_tw', lookup_insert_eq. unfold o'; simpl.
        rewrite list_lookup_insert_eq by (rewrite length_take; lia). reflexivity.
      * rewrite Hslot0 in Htslot_m. injection Htslot_m as Hth_eq. subst th_m.
        destruct (decode_edge_idx_retag th_sw h Hh_lt28) as (Hdi & Hds & Hdm).
        assert (Hsrc_th : forall k idx v w,
          tw_sources (tset_handle k idx v w) =
          <[k := (alter (fun '(s', _) => (s', v)) idx (tsources_of w k).1, (tsources_of w k).2)]> (tw_sources w)).
        { intros k0 i0 v0 w0. unfold tset_handle, tsources_of, tset_sources.
          destruct (default ([], 0) (tw_sources w0 !! k0)) as [? ?]. reflexivity. }
        assert (Hsrc1 : tw_sources tw1 =
          <[m := (alter (fun '(s', _) => (s', Some (retag_edge_idx th_sw (N.of_nat h)))) (N.to_nat (decode_obs_slot ms)) (tsources_of tw m).1, (tsources_of tw m).2)]> (tw_sources tw))
          by (unfold tw1; rewrite Hsrc_th; reflexivity).
        assert (Hsrc' : tw_sources tw' =
          <[self := (alter (fun '(s', _) => (s', None)) i (tsources_of tw1 self).1, (tsources_of tw1 self).2)]> (tw_sources tw1))
          by (unfold tw'; rewrite Hsrc_th; reflexivity).
        unfold tslot_at, tsources_of. rewrite Hsrc'.
        unfold tslot_at in Hslot0.
        destruct (decide (self = m)) as [<-|Hnem].
        -- assert (Hnei : i <> N.to_nat (decode_obs_slot ms))
             by (intro Htmp; apply Hne_pair; split; [reflexivity|exact Htmp]).
           rewrite lookup_insert_eq. simpl.
           unfold tsources_of at 1. rewrite Hsrc1, lookup_insert_eq. simpl.
           rewrite list_lookup_alter_ne by exact Hnei.
           rewrite list_lookup_alter_eq, Hslot0. simpl.
           eexists; split; [reflexivity|split; [exact Hdi|split; [exact Hds|exact Hdm]]].
        -- rewrite lookup_insert_ne by exact Hnem.
           rewrite Hsrc1, lookup_insert_eq. simpl.
           rewrite list_lookup_alter_eq, Hslot0. simpl.
           eexists; split; [reflexivity|split; [exact Hdi|split; [exact Hds|exact Hdm]]].
Qed.
References

B8. Packed Node.Flags bijection and single-word guard refinement

Record node_flags_unpacked := {
  nf_kind : N;
  nf_freshness : N;
  nf_status : N;
  nf_active : N;
  nf_disposed : bool;
  nf_unannounced : bool;
  nf_queued : bool;
  nf_in_cleanup : bool;
  nf_publishes : bool;
  nf_violated : bool;
  nf_published : bool;
  nf_safe_eq : bool;
  nf_has_acted : bool;
  nf_acting : bool
}.
References

Uses

  • bool
  • N
Definition nf_wf (nf : node_flags_unpacked) : Prop :=
  (nf_kind nf < 4)%N /\
  (nf_freshness nf < 4)%N /\
  (nf_status nf < 16)%N /\
  (nf_active nf < 4)%N.
References
Definition nf_bool_bit (b : bool) (pos : N) : N :=
  if b then N.shiftl 1%N pos else 0%N.
References

Uses

  • bool
  • N
  • N.shiftl
Definition pack_node_flags (nf : node_flags_unpacked) : N :=
  N.lor (nf_kind nf)
    (N.lor (N.shiftl (nf_freshness nf) 2)
      (N.lor (N.shiftl (nf_status nf) 4)
        (N.lor (N.shiftl (nf_active nf) 8)
          (N.lor (nf_bool_bit (nf_disposed nf) 10)
            (N.lor (nf_bool_bit (nf_unannounced nf) 11)
              (N.lor (nf_bool_bit (nf_queued nf) 12)
                (N.lor (nf_bool_bit (nf_in_cleanup nf) 13)
                  (N.lor (nf_bool_bit (nf_publishes nf) 14)
                    (N.lor (nf_bool_bit (nf_violated nf) 15)
                      (N.lor (nf_bool_bit (nf_published nf) 16)
                        (N.lor (nf_bool_bit (nf_safe_eq nf) 17)
                          (N.lor (nf_bool_bit (nf_has_acted nf) 18)
                                 (nf_bool_bit (nf_acting nf) 19))))))))))))).
References
Definition unpack_node_flags (w : N) : node_flags_unpacked := {|
  nf_kind := N.land w (N.ones 2);
  nf_freshness := N.land (N.shiftr w 2) (N.ones 2);
  nf_status := N.land (N.shiftr w 4) (N.ones 4);
  nf_active := N.land (N.shiftr w 8) (N.ones 2);
  nf_disposed := N.testbit w 10;
  nf_unannounced := N.testbit w 11;
  nf_queued := N.testbit w 12;
  nf_in_cleanup := N.testbit w 13;
  nf_publishes := N.testbit w 14;
  nf_violated := N.testbit w 15;
  nf_published := N.testbit w 16;
  nf_safe_eq := N.testbit w 17;
  nf_has_acted := N.testbit w 18;
  nf_acting := N.testbit w 19
|}.
References
Lemma N_testbit_bool_bit b pos k :
  N.testbit (nf_bool_bit b pos) k = if b then N.testbit (N.shiftl 1%N pos) k else false.
Qed
Proof. destruct b; simpl; reflexivity. Qed.
References

Uses

Lemma N_testbit_shiftl_exact a pos k :
  (pos <= k)%N -> N.testbit (N.shiftl a pos) k = N.testbit a (k - pos).
Qed
Proof. intros H. apply N.shiftl_spec_high; lia. Qed.
References

Uses

  • N.shiftl
  • N.shiftl_spec_high
  • N.testbit
Lemma N_testbit_shiftl_lt a pos k :
  (k < pos)%N -> N.testbit (N.shiftl a pos) k = false.
Qed
Proof. intros H. apply N.shiftl_spec_low; exact H. Qed.
References

Uses

  • false
  • N.shiftl
  • N.shiftl_spec_low
  • N.testbit
Lemma N_testbit_1 k : N.testbit 1%N k = (k =? 0)%N.
Qed
Proof. replace 1%N with (2 ^ 0)%N by reflexivity. destruct (N.eqb_spec k 0) as [->|H].
  - apply N.pow2_bits_true.
  - apply N.pow2_bits_false. lia.
Qed.
References

Uses

  • N.eqb_spec
  • N.pow2_bits_false
  • N.pow2_bits_true
  • N.testbit
Local Ltac kill_pack_bits Hk0 Hf0 Hs0 Ha0 Hif_false :=
  repeat match goal with
  | |- context [N.testbit (N.shiftl ?a ?p) ?kk] =>
    first
      [ rewrite (N_testbit_shiftl_lt a p kk) by lia
      | rewrite (N_testbit_shiftl_exact a p kk) by lia ]
  end;
  rewrite ?N_testbit_1;
  repeat match goal with
  | |- context [N.testbit (nf_kind _) ?kk] =>
    first [rewrite (Hk0 kk) by lia | idtac]
  | |- context [N.testbit (nf_freshness _) ?kk] =>
    first [rewrite (Hf0 kk) by lia | idtac]
  | |- context [N.testbit (nf_status _) ?kk] =>
    first [rewrite (Hs0 kk) by lia | idtac]
  | |- context [N.testbit (nf_active _) ?kk] =>
    first [rewrite (Ha0 kk) by lia | idtac]
  end;
  repeat match goal with
  | |- context [(?x =? ?y)%N] =>
    destruct (N.eqb_spec x y); try lia
  end;
  rewrite ?Hif_false;
  repeat match goal with
  | |- context [if ?b then _ else _] => destruct b
  end;
  rewrite ?Bool.orb_false_l, ?Bool.orb_false_r;
  reflexivity.
References
Lemma N_testbit_pack_bit_exact nf k :
  nf_wf nf ->
  ((k < 2)%N -> N.testbit (pack_node_flags nf) k = N.testbit (nf_kind nf) k) /\
  ((2 <= k)%N -> (k < 4)%N -> N.testbit (pack_node_flags nf) k = N.testbit (nf_freshness nf) (k - 2)) /\
  ((4 <= k)%N -> (k < 8)%N -> N.testbit (pack_node_flags nf) k = N.testbit (nf_status nf) (k - 4)) /\
  ((8 <= k)%N -> (k < 10)%N -> N.testbit (pack_node_flags nf) k = N.testbit (nf_active nf) (k - 8)) /\
  (k = 10%N -> N.testbit (pack_node_flags nf) k = nf_disposed nf) /\
  (k = 11%N -> N.testbit (pack_node_flags nf) k = nf_unannounced nf) /\
  (k = 12%N -> N.testbit (pack_node_flags nf) k = nf_queued nf) /\
  (k = 13%N -> N.testbit (pack_node_flags nf) k = nf_in_cleanup nf) /\
  (k = 14%N -> N.testbit (pack_node_flags nf) k = nf_publishes nf) /\
  (k = 15%N -> N.testbit (pack_node_flags nf) k = nf_violated nf) /\
  (k = 16%N -> N.testbit (pack_node_flags nf) k = nf_published nf) /\
  (k = 17%N -> N.testbit (pack_node_flags nf) k = nf_safe_eq nf) /\
  (k = 18%N -> N.testbit (pack_node_flags nf) k = nf_has_acted nf) /\
  (k = 19%N -> N.testbit (pack_node_flags nf) k = nf_acting nf) /\
  ((20 <= k)%N -> N.testbit (pack_node_flags nf) k = false).
Qed
Proof.
  intros (Hk & Hf & Hs & Ha).
  change 4%N with (2 ^ 2)%N in Hk, Hf, Ha.
  change 16%N with (2 ^ 4)%N in Hs.
  assert (Hk0 : forall m, (2 <= m)%N -> N.testbit (nf_kind nf) m = false) by (intros; eapply N_testbit_lt_pow2; eassumption).
  assert (Hf0 : forall m, (2 <= m)%N -> N.testbit (nf_freshness nf) m = false) by (intros; eapply N_testbit_lt_pow2; eassumption).
  assert (Hs0 : forall m, (4 <= m)%N -> N.testbit (nf_status nf) m = false) by (intros; eapply N_testbit_lt_pow2; eassumption).
  assert (Ha0 : forall m, (2 <= m)%N -> N.testbit (nf_active nf) m = false) by (intros; eapply N_testbit_lt_pow2; eassumption).
  unfold pack_node_flags.
  repeat rewrite N.lor_spec.
  repeat rewrite N_testbit_bool_bit.
  assert (Hif_false : forall b : bool, (if b then false else false) = false) by (intros []; reflexivity).
  repeat split; intros;
    try subst k;
    kill_pack_bits Hk0 Hf0 Hs0 Ha0 Hif_false.
Qed.
References
✓ No axioms

Packing Kind (2b), Freshness (2b), Status (4b), Active (2b), and all 10 boolean node flags (Disposed, Unannounced, Queued, InCleanup, Publishes, Violated, Published, SafeEq, HasActed, Acting) into a 31-bit Smi word (< 2^31) is a lossless exact bijection on well-formed unpacked flag records.

Arguments

well-formed unpacked node_flags_unpacked record whose multi-bit fields fit their bit widths (Kind < 4, Freshness < 4, Status < 16, Active < 4).
Theorem packed_node_flags_bijection (nf : node_flags_unpacked) :
  nf_wf nf ->
  let w := pack_node_flags nf in
  (w < 2 ^ 31)%N /\ unpack_node_flags w = nf.

Arguments

well-formed unpacked node_flags_unpacked record whose multi-bit fields fit their bit widths (Kind < 4, Freshness < 4, Status < 16, Active < 4).
Qed
Proof.
  intros Hwf. pose proof Hwf as (Hk & Hf & Hs & Ha).
  change 4%N with (2 ^ 2)%N in Hk, Hf, Ha.
  change 16%N with (2 ^ 4)%N in Hs.
  cbv zeta. split.
  - apply N_lt_pow2_of_bits. intros k Hk31.
    destruct (N_testbit_pack_bit_exact nf k Hwf) as (_&_&_&_&_&_&_&_&_&_&_&_&_&_&Hhi).
    apply Hhi. lia.
  - unfold unpack_node_flags.
    destruct nf as [k0 f0 s0 a0 b10 b11 b12 b13 b14 b15 b16 b17 b18 b19].
    simpl in *.
    assert (Ek : N.land (pack_node_flags {| nf_kind := k0; nf_freshness := f0; nf_status := s0; nf_active := a0;
      nf_disposed := b10; nf_unannounced := b11; nf_queued := b12; nf_in_cleanup := b13;
      nf_publishes := b14; nf_violated := b15; nf_published := b16; nf_safe_eq := b17;
      nf_has_acted := b18; nf_acting := b19 |}) (N.ones 2) = k0).
    { apply N.bits_inj. intros j. rewrite N.land_spec.
      destruct (N.ltb_spec0 j 2) as [Hj|Hj].
      - rewrite N.ones_spec_low by exact Hj. rewrite andb_true_r.
        destruct (N_testbit_pack_bit_exact _ j Hwf) as [H0 _]. rewrite H0 by exact Hj. reflexivity.
      - rewrite N.ones_spec_high by lia. rewrite andb_false_r. symmetry.
        eapply N_testbit_lt_pow2; [exact Hk|lia]. }
    assert (Ef : N.land (N.shiftr (pack_node_flags {| nf_kind := k0; nf_freshness := f0; nf_status := s0; nf_active := a0;
      nf_disposed := b10; nf_unannounced := b11; nf_queued := b12; nf_in_cleanup := b13;
      nf_publishes := b14; nf_violated := b15; nf_published := b16; nf_safe_eq := b17;
      nf_has_acted := b18; nf_acting := b19 |}) 2) (N.ones 2) = f0).
    { apply N.bits_inj. intros j. rewrite N.land_spec, N.shiftr_spec by lia.
      destruct (N.ltb_spec0 j 2) as [Hj|Hj].
      - rewrite N.ones_spec_low by exact Hj. rewrite andb_true_r.
        destruct (N_testbit_pack_bit_exact _ (j + 2) Hwf) as (_ & H2 & _).
        rewrite H2 by lia. simpl. f_equal. lia.
      - rewrite N.ones_spec_high by lia. rewrite andb_false_r. symmetry.
        eapply N_testbit_lt_pow2; [exact Hf|lia]. }
    assert (Es : N.land (N.shiftr (pack_node_flags {| nf_kind := k0; nf_freshness := f0; nf_status := s0; nf_active := a0;
      nf_disposed := b10; nf_unannounced := b11; nf_queued := b12; nf_in_cleanup := b13;
      nf_publishes := b14; nf_violated := b15; nf_published := b16; nf_safe_eq := b17;
      nf_has_acted := b18; nf_acting := b19 |}) 4) (N.ones 4) = s0).
    { apply N.bits_inj. intros j. rewrite N.land_spec, N.shiftr_spec by lia.
      destruct (N.ltb_spec0 j 4) as [Hj|Hj].
      - rewrite N.ones_spec_low by exact Hj. rewrite andb_true_r.
        destruct (N_testbit_pack_bit_exact _ (j + 4) Hwf) as (_ & _ & H4 & _).
        rewrite H4 by lia. simpl. f_equal. lia.
      - rewrite N.ones_spec_high by lia. rewrite andb_false_r. symmetry.
        eapply N_testbit_lt_pow2; [exact Hs|lia]. }
    assert (Ea : N.land (N.shiftr (pack_node_flags {| nf_kind := k0; nf_freshness := f0; nf_status := s0; nf_active := a0;
      nf_disposed := b10; nf_unannounced := b11; nf_queued := b12; nf_in_cleanup := b13;
      nf_publishes := b14; nf_violated := b15; nf_published := b16; nf_safe_eq := b17;
      nf_has_acted := b18; nf_acting := b19 |}) 8) (N.ones 2) = a0).
    { apply N.bits_inj. intros j. rewrite N.land_spec, N.shiftr_spec by lia.
      destruct (N.ltb_spec0 j 2) as [Hj|Hj].
      - rewrite N.ones_spec_low by exact Hj. rewrite andb_true_r.
        destruct (N_testbit_pack_bit_exact _ (j + 8) Hwf) as (_ & _ & _ & H8 & _).
        rewrite H8 by lia. simpl. f_equal. lia.
      - rewrite N.ones_spec_high by lia. rewrite andb_false_r. symmetry.
        eapply N_testbit_lt_pow2; [exact Ha|lia]. }
    destruct (N_testbit_pack_bit_exact {| nf_kind := k0; nf_freshness := f0; nf_status := s0; nf_active := a0;
      nf_disposed := b10; nf_unannounced := b11; nf_queued := b12; nf_in_cleanup := b13;
      nf_publishes := b14; nf_violated := b15; nf_published := b16; nf_safe_eq := b17;
      nf_has_acted := b18; nf_acting := b19 |} 10 Hwf) as (_&_&_&_&H10&_).
    destruct (N_testbit_pack_bit_exact {| nf_kind := k0; nf_freshness := f0; nf_status := s0; nf_active := a0;
      nf_disposed := b10; nf_unannounced := b11; nf_queued := b12; nf_in_cleanup := b13;
      nf_publishes := b14; nf_violated := b15; nf_published := b16; nf_safe_eq := b17;
      nf_has_acted := b18; nf_acting := b19 |} 11 Hwf) as (_&_&_&_&_&H11&_).
    destruct (N_testbit_pack_bit_exact {| nf_kind := k0; nf_freshness := f0; nf_status := s0; nf_active := a0;
      nf_disposed := b10; nf_unannounced := b11; nf_queued := b12; nf_in_cleanup := b13;
      nf_publishes := b14; nf_violated := b15; nf_published := b16; nf_safe_eq := b17;
      nf_has_acted := b18; nf_acting := b19 |} 12 Hwf) as (_&_&_&_&_&_&H12&_).
    destruct (N_testbit_pack_bit_exact {| nf_kind := k0; nf_freshness := f0; nf_status := s0; nf_active := a0;
      nf_disposed := b10; nf_unannounced := b11; nf_queued := b12; nf_in_cleanup := b13;
      nf_publishes := b14; nf_violated := b15; nf_published := b16; nf_safe_eq := b17;
      nf_has_acted := b18; nf_acting := b19 |} 13 Hwf) as (_&_&_&_&_&_&_&H13&_).
    destruct (N_testbit_pack_bit_exact {| nf_kind := k0; nf_freshness := f0; nf_status := s0; nf_active := a0;
      nf_disposed := b10; nf_unannounced := b11; nf_queued := b12; nf_in_cleanup := b13;
      nf_publishes := b14; nf_violated := b15; nf_published := b16; nf_safe_eq := b17;
      nf_has_acted := b18; nf_acting := b19 |} 14 Hwf) as (_&_&_&_&_&_&_&_&H14&_).
    destruct (N_testbit_pack_bit_exact {| nf_kind := k0; nf_freshness := f0; nf_status := s0; nf_active := a0;
      nf_disposed := b10; nf_unannounced := b11; nf_queued := b12; nf_in_cleanup := b13;
      nf_publishes := b14; nf_violated := b15; nf_published := b16; nf_safe_eq := b17;
      nf_has_acted := b18; nf_acting := b19 |} 15 Hwf) as (_&_&_&_&_&_&_&_&_&H15&_).
    destruct (N_testbit_pack_bit_exact {| nf_kind := k0; nf_freshness := f0; nf_status := s0; nf_active := a0;
      nf_disposed := b10; nf_unannounced := b11; nf_queued := b12; nf_in_cleanup := b13;
      nf_publishes := b14; nf_violated := b15; nf_published := b16; nf_safe_eq := b17;
      nf_has_acted := b18; nf_acting := b19 |} 16 Hwf) as (_&_&_&_&_&_&_&_&_&_&H16&_).
    destruct (N_testbit_pack_bit_exact {| nf_kind := k0; nf_freshness := f0; nf_status := s0; nf_active := a0;
      nf_disposed := b10; nf_unannounced := b11; nf_queued := b12; nf_in_cleanup := b13;
      nf_publishes := b14; nf_violated := b15; nf_published := b16; nf_safe_eq := b17;
      nf_has_acted := b18; nf_acting := b19 |} 17 Hwf) as (_&_&_&_&_&_&_&_&_&_&_&H17&_).
    destruct (N_testbit_pack_bit_exact {| nf_kind := k0; nf_freshness := f0; nf_status := s0; nf_active := a0;
      nf_disposed := b10; nf_unannounced := b11; nf_queued := b12; nf_in_cleanup := b13;
      nf_publishes := b14; nf_violated := b15; nf_published := b16; nf_safe_eq := b17;
      nf_has_acted := b18; nf_acting := b19 |} 18 Hwf) as (_&_&_&_&_&_&_&_&_&_&_&_&H18&_).
    destruct (N_testbit_pack_bit_exact {| nf_kind := k0; nf_freshness := f0; nf_status := s0; nf_active := a0;
      nf_disposed := b10; nf_unannounced := b11; nf_queued := b12; nf_in_cleanup := b13;
      nf_publishes := b14; nf_violated := b15; nf_published := b16; nf_safe_eq := b17;
      nf_has_acted := b18; nf_acting := b19 |} 19 Hwf) as (_&_&_&_&_&_&_&_&_&_&_&_&_&H19&_).
    rewrite Ek, Ef, Es, Ea.
    rewrite H10, H11, H12, H13, H14, H15, H16, H17, H18, H19 by reflexivity.
    reflexivity.
Qed.
References
Definition packed_idle_effect_mask : N :=
  N.lor (N.ones 4) (N.lor (N.shiftl 1%N 10) (N.lor (N.shiftl 1%N 12) (N.lor (N.shiftl 1%N 13) (N.shiftl 1%N 14)))).
References

Uses

  • N
  • N.lor
  • N.ones
  • N.shiftl
Definition packed_idle_effect_val : N := 1%N.
References

Uses

  • N
Definition packed_clean_leaf_memo_mask : N :=
  N.lor (N.ones 4) (N.shiftl 1%N 10).
References

Uses

  • N
  • N.lor
  • N.ones
  • N.shiftl
Definition packed_clean_leaf_memo_val : N := 0%N.
References

Uses

  • N
Definition packed_ready_published_memo_mask : N :=
  N.lor (N.shiftl (N.ones 4) 4)
    (N.lor (N.shiftl (N.ones 2) 8)
      (N.lor (N.shiftl 1%N 10) (N.lor (N.shiftl 1%N 16) (N.shiftl 1%N 17)))).
References

Uses

  • N
  • N.lor
  • N.ones
  • N.shiftl
Definition packed_ready_published_memo_val : N :=
  N.lor (N.shiftl 1%N 16) (N.shiftl 1%N 17).
References

Uses

  • N
  • N.lor
  • N.shiftl
✓ No axioms

Single-word mask-and-compare guards on packed 31-bit Node.Flags are logically equivalent to the unpacked multi-field conjunctions for idle effects (0x740F, Core.fs:340), clean leaf memos (0x40F), and ready published safe-eq memos (0x307F0 / 0x30000, Core.fs:324,328).

Arguments

well-formed unpacked node_flags_unpacked record whose bit-packed representation is tested.
Theorem packed_guard_refines_unpacked (nf : node_flags_unpacked) :
  nf_wf nf ->
  let w := pack_node_flags nf in
  ((N.land w packed_idle_effect_mask =? packed_idle_effect_val)%N = true <->
    nf_kind nf = 1%N /\ nf_freshness nf = 0%N /\
    nf_disposed nf = false /\ nf_queued nf = false /\
    nf_in_cleanup nf = false /\ nf_publishes nf = false) /\
  ((N.land w packed_clean_leaf_memo_mask =? packed_clean_leaf_memo_val)%N = true <->
    nf_kind nf = 0%N /\ nf_freshness nf = 0%N /\ nf_disposed nf = false) /\
  ((N.land w packed_ready_published_memo_mask =? packed_ready_published_memo_val)%N = true <->
    nf_status nf = 0%N /\ nf_active nf = 0%N /\
    nf_disposed nf = false /\ nf_published nf = true /\ nf_safe_eq nf = true).

Arguments

well-formed unpacked node_flags_unpacked record whose bit-packed representation is tested.
Qed
Proof.
  intros Hwf. cbv zeta.
  pose proof (packed_node_flags_bijection nf Hwf) as [_ Hunp].
  rewrite !N.eqb_eq.
  assert (Hbit_eq : forall a b : N, a = b <-> forall k, N.testbit a k = N.testbit b k).
  { intros a b; split; [intros ->; reflexivity|apply N.bits_inj]. }
  assert (Hm1_0 : N.land packed_idle_effect_mask (N.ones 2) = N.ones 2) by reflexivity.
  assert (Hm1_2 : N.land (N.shiftr packed_idle_effect_mask 2) (N.ones 2) = N.ones 2) by reflexivity.
  assert (Hm2_0 : N.land packed_clean_leaf_memo_mask (N.ones 2) = N.ones 2) by reflexivity.
  assert (Hm2_2 : N.land (N.shiftr packed_clean_leaf_memo_mask 2) (N.ones 2) = N.ones 2) by reflexivity.
  assert (Hm3_4 : N.land (N.shiftr packed_ready_published_memo_mask 4) (N.ones 4) = N.ones 4) by reflexivity.
  assert (Hm3_8 : N.land (N.shiftr packed_ready_published_memo_mask 8) (N.ones 2) = N.ones 2) by reflexivity.
  assert (Hunp_shiftr : forall m n pos width,
    N.land (N.shiftr (N.land n m) pos) (N.ones width) =
    N.land (N.land (N.shiftr n pos) (N.ones width)) (N.land (N.shiftr m pos) (N.ones width))).
  { intros m n pos width. apply N.bits_inj. intros j.
    rewrite !N.land_spec, !N.shiftr_spec by lia. rewrite !N.land_spec.
    destruct (N.testbit n (j + pos)), (N.testbit m (j + pos)), (N.testbit (N.ones width) j); reflexivity. }
  split; [split | split; [split | split]].
  - intros Heq. repeat split.
    + pose proof (f_equal (fun u => N.land u (N.ones 2)) Heq) as Hk.
      cbv beta in Hk. rewrite <- N.land_assoc, Hm1_0 in Hk.
      assert (Hk' : nf_kind (unpack_node_flags (pack_node_flags nf)) = 1%N)
        by (unfold unpack_node_flags; simpl; rewrite Hk; reflexivity).
      rewrite Hunp in Hk'. exact Hk'.
    + pose proof (f_equal (fun u => N.land (N.shiftr u 2) (N.ones 2)) Heq) as Hf.
      cbv beta in Hf. rewrite Hunp_shiftr, Hm1_2 in Hf.
      assert (Hf' : nf_freshness (unpack_node_flags (pack_node_flags nf)) = 0%N).
      { unfold unpack_node_flags; simpl.
        rewrite <- N.land_assoc in Hf.
        replace (N.land (N.ones 2) (N.ones 2)) with (N.ones 2) in Hf by reflexivity.
        rewrite Hf. reflexivity. }
      rewrite Hunp in Hf'. exact Hf'.
    + pose proof (proj1 (Hbit_eq _ _) Heq 10%N) as B10.
      rewrite N.land_spec in B10.
      change (N.testbit packed_idle_effect_mask 10) with true in B10.
      change (N.testbit packed_idle_effect_val 10) with false in B10.
      rewrite andb_true_r in B10.
      assert (Hd : nf_disposed (unpack_node_flags (pack_node_flags nf)) = false) by (exact B10).
      rewrite Hunp in Hd. exact Hd.
    + pose proof (proj1 (Hbit_eq _ _) Heq 12%N) as B12.
      rewrite N.land_spec in B12.
      change (N.testbit packed_idle_effect_mask 12) with true in B12.
      change (N.testbit packed_idle_effect_val 12) with false in B12.
      rewrite andb_true_r in B12.
      assert (Hq : nf_queued (unpack_node_flags (pack_node_flags nf)) = false) by (exact B12).
      rewrite Hunp in Hq. exact Hq.
    + pose proof (proj1 (Hbit_eq _ _) Heq 13%N) as B13.
      rewrite N.land_spec in B13.
      change (N.testbit packed_idle_effect_mask 13) with true in B13.
      change (N.testbit packed_idle_effect_val 13) with false in B13.
      rewrite andb_true_r in B13.
      assert (Hc : nf_in_cleanup (unpack_node_flags (pack_node_flags nf)) = false) by (exact B13).
      rewrite Hunp in Hc. exact Hc.
    + pose proof (proj1 (Hbit_eq _ _) Heq 14%N) as B14.
      rewrite N.land_spec in B14.
      change (N.testbit packed_idle_effect_mask 14) with true in B14.
      change (N.testbit packed_idle_effect_val 14) with false in B14.
      rewrite andb_true_r in B14.
      assert (Hp : nf_publishes (unpack_node_flags (pack_node_flags nf)) = false) by (exact B14).
      rewrite Hunp in Hp. exact Hp.
  - intros (Hk & Hf & Hd & Hq & Hc & Hpub).
    apply N.bits_inj. intros k. rewrite N.land_spec.
    destruct (N.ltb_spec0 k 20) as [Hk20|Hk20].
    + destruct (N_testbit_pack_bit_exact nf k Hwf)
        as (P0 & P2 & P4 & P8 & P10 & P11 & P12 & P13 & P14 & P15 & P16 & P17 & P18 & P19 & _).
      assert (Hkcases : k = 0%N \/ k = 1%N \/ k = 2%N \/ k = 3%N \/
                        k = 4%N \/ k = 5%N \/ k = 6%N \/ k = 7%N \/
                        k = 8%N \/ k = 9%N \/ k = 10%N \/ k = 11%N \/
                        k = 12%N \/ k = 13%N \/ k = 14%N \/ k = 15%N \/
                        k = 16%N \/ k = 17%N \/ k = 18%N \/ k = 19%N) by lia.
      repeat destruct Hkcases as [->|Hkcases];
        try (rewrite P0 by lia; rewrite Hk; reflexivity);
        try (rewrite P2 by lia; rewrite Hf; reflexivity);
        try (rewrite P10 by reflexivity; rewrite Hd; reflexivity);
        try (rewrite P12 by reflexivity; rewrite Hq; reflexivity);
        try (rewrite P13 by reflexivity; rewrite Hc; reflexivity);
        try (rewrite P14 by reflexivity; rewrite Hpub; reflexivity);
        try (change (N.testbit packed_idle_effect_mask _) with false; rewrite andb_false_r; reflexivity).
      subst k. rewrite P19 by reflexivity. change (N.testbit packed_idle_effect_mask 19) with false.
      rewrite andb_false_r. reflexivity.
    + destruct (N_testbit_pack_bit_exact nf k Hwf) as (_&_&_&_&_&_&_&_&_&_&_&_&_&_&Hhi).
      rewrite Hhi by lia. rewrite andb_false_l.
      symmetry. eapply N_testbit_lt_pow2; [instantiate (1 := 20%N); vm_compute; reflexivity|lia].
  - intros Heq. repeat split.
    + pose proof (f_equal (fun u => N.land u (N.ones 2)) Heq) as Hk.
      cbv beta in Hk. rewrite <- N.land_assoc, Hm2_0 in Hk.
      assert (Hk' : nf_kind (unpack_node_flags (pack_node_flags nf)) = 0%N)
        by (unfold unpack_node_flags; simpl; rewrite Hk; reflexivity).
      rewrite Hunp in Hk'. exact Hk'.
    + pose proof (f_equal (fun u => N.land (N.shiftr u 2) (N.ones 2)) Heq) as Hf.
      cbv beta in Hf. rewrite Hunp_shiftr, Hm2_2 in Hf.
      assert (Hf' : nf_freshness (unpack_node_flags (pack_node_flags nf)) = 0%N).
      { unfold unpack_node_flags; simpl.
        rewrite <- N.land_assoc in Hf.
        replace (N.land (N.ones 2) (N.ones 2)) with (N.ones 2) in Hf by reflexivity.
        rewrite Hf. reflexivity. }
      rewrite Hunp in Hf'. exact Hf'.
    + pose proof (proj1 (Hbit_eq _ _) Heq 10%N) as B10.
      rewrite N.land_spec in B10.
      change (N.testbit packed_clean_leaf_memo_mask 10) with true in B10.
      change (N.testbit packed_clean_leaf_memo_val 10) with false in B10.
      rewrite andb_true_r in B10.
      assert (Hd : nf_disposed (unpack_node_flags (pack_node_flags nf)) = false) by (exact B10).
      rewrite Hunp in Hd. exact Hd.
  - intros (Hk & Hf & Hd).
    apply N.bits_inj. intros k. rewrite N.land_spec.
    destruct (N.ltb_spec0 k 20) as [Hk20|Hk20].
    + destruct (N_testbit_pack_bit_exact nf k Hwf)
        as (P0 & P2 & _ & _ & P10 & _).
      assert (Hkcases : k = 0%N \/ k = 1%N \/ k = 2%N \/ k = 3%N \/
                        k = 4%N \/ k = 5%N \/ k = 6%N \/ k = 7%N \/
                        k = 8%N \/ k = 9%N \/ k = 10%N \/ k = 11%N \/
                        k = 12%N \/ k = 13%N \/ k = 14%N \/ k = 15%N \/
                        k = 16%N \/ k = 17%N \/ k = 18%N \/ k = 19%N) by lia.
      repeat destruct Hkcases as [->|Hkcases];
        try (rewrite P0 by lia; rewrite Hk; reflexivity);
        try (rewrite P2 by lia; rewrite Hf; reflexivity);
        try (rewrite P10 by reflexivity; rewrite Hd; reflexivity);
        try (change (N.testbit packed_clean_leaf_memo_mask _) with false; rewrite andb_false_r; reflexivity).
      subst k. change (N.testbit packed_clean_leaf_memo_mask 19) with false. rewrite andb_false_r. reflexivity.
    + destruct (N_testbit_pack_bit_exact nf k Hwf) as (_&_&_&_&_&_&_&_&_&_&_&_&_&_&Hhi).
      rewrite Hhi by lia. rewrite andb_false_l.
      symmetry. apply N.bits_0.
  - intros Heq. repeat split.
    + pose proof (f_equal (fun u => N.land (N.shiftr u 4) (N.ones 4)) Heq) as Hs.
      cbv beta in Hs. rewrite Hunp_shiftr, Hm3_4 in Hs.
      assert (Hs' : nf_status (unpack_node_flags (pack_node_flags nf)) = 0%N).
      { unfold unpack_node_flags; simpl.
        rewrite <- N.land_assoc in Hs.
        replace (N.land (N.ones 4) (N.ones 4)) with (N.ones 4) in Hs by reflexivity.
        rewrite Hs. reflexivity. }
      rewrite Hunp in Hs'. exact Hs'.
    + pose proof (f_equal (fun u => N.land (N.shiftr u 8) (N.ones 2)) Heq) as Ha.
      cbv beta in Ha. rewrite Hunp_shiftr, Hm3_8 in Ha.
      assert (Ha' : nf_active (unpack_node_flags (pack_node_flags nf)) = 0%N).
      { unfold unpack_node_flags; simpl.
        rewrite <- N.land_assoc in Ha.
        replace (N.land (N.ones 2) (N.ones 2)) with (N.ones 2) in Ha by reflexivity.
        rewrite Ha. reflexivity. }
      rewrite Hunp in Ha'. exact Ha'.
    + pose proof (proj1 (Hbit_eq _ _) Heq 10%N) as B10.
      rewrite N.land_spec in B10.
      change (N.testbit packed_ready_published_memo_mask 10) with true in B10.
      change (N.testbit packed_ready_published_memo_val 10) with false in B10.
      rewrite andb_true_r in B10.
      assert (Hd : nf_disposed (unpack_node_flags (pack_node_flags nf)) = false) by (exact B10).
      rewrite Hunp in Hd. exact Hd.
    + pose proof (proj1 (Hbit_eq _ _) Heq 16%N) as B16.
      rewrite N.land_spec in B16.
      change (N.testbit packed_ready_published_memo_mask 16) with true in B16.
      change (N.testbit packed_ready_published_memo_val 16) with true in B16.
      rewrite andb_true_r in B16.
      assert (Hp : nf_published (unpack_node_flags (pack_node_flags nf)) = true) by (exact B16).
      rewrite Hunp in Hp. exact Hp.
    + pose proof (proj1 (Hbit_eq _ _) Heq 17%N) as B17.
      rewrite N.land_spec in B17.
      change (N.testbit packed_ready_published_memo_mask 17) with true in B17.
      change (N.testbit packed_ready_published_memo_val 17) with true in B17.
      rewrite andb_true_r in B17.
      assert (Hseq : nf_safe_eq (unpack_node_flags (pack_node_flags nf)) = true) by (exact B17).
      rewrite Hunp in Hseq. exact Hseq.
  - intros (Hs & Ha & Hd & Hp & Hseq).
    apply N.bits_inj. intros k. rewrite N.land_spec.
    destruct (N.ltb_spec0 k 20) as [Hk20|Hk20].
    + destruct (N_testbit_pack_bit_exact nf k Hwf)
        as (_ & _ & P4 & P8 & P10 & _ & _ & _ & _ & _ & P16 & P17 & _).
      assert (Hkcases : k = 0%N \/ k = 1%N \/ k = 2%N \/ k = 3%N \/
                        k = 4%N \/ k = 5%N \/ k = 6%N \/ k = 7%N \/
                        k = 8%N \/ k = 9%N \/ k = 10%N \/ k = 11%N \/
                        k = 12%N \/ k = 13%N \/ k = 14%N \/ k = 15%N \/
                        k = 16%N \/ k = 17%N \/ k = 18%N \/ k = 19%N) by lia.
      repeat destruct Hkcases as [->|Hkcases];
        try (rewrite P4 by lia; rewrite Hs; reflexivity);
        try (rewrite P8 by lia; rewrite Ha; reflexivity);
        try (rewrite P10 by reflexivity; rewrite Hd; reflexivity);
        try (rewrite P16 by reflexivity; rewrite Hp; reflexivity);
        try (rewrite P17 by reflexivity; rewrite Hseq; reflexivity);
        try (change (N.testbit packed_ready_published_memo_mask _) with false; rewrite andb_false_r; reflexivity).
      subst k. change (N.testbit packed_ready_published_memo_mask 19) with false. rewrite andb_false_r. reflexivity.
    + destruct (N_testbit_pack_bit_exact nf k Hwf) as (_&_&_&_&_&_&_&_&_&_&_&_&_&_&Hhi).
      rewrite Hhi by lia. rewrite andb_false_l.
      symmetry. eapply N_testbit_lt_pow2; [instantiate (1 := 20%N); vm_compute; reflexivity|lia].
Qed.
References

B9. Leaf-memo cutoff skip and custom-comparer fused flush

Definition ref_ready_cutoff_step
    (f : nat) (prev_val next_val : val) (cmp : comparer) (n : node_id) : M unit :=
  let* moved := ready_value_moved prev_val next_val cmp n in
  let* s0 := m_get in
  let* _ := m_modify (ghost_run_end n (g_current s0) moved) in
  if moved then
    let* _ := m_step (notify_dirty_except f n (g_current s0)) in
    let* s' := m_get in
    if bool_decide (g_batch_depth s' = 0%nat) && negb (g_flushing s') && live_queue s'
    then m_modify (with_flush_owed true) else m_return tt
  else m_return tt.
References
Definition leaf_no_cutoff_step (n : node_id) : M unit :=
  let* s0 := m_get in
  m_modify (ghost_run_end n (g_current s0) false).
References
✓ No axioms

For a ready-published memo n with zero observers (observer_items = []) and a non-throwing comparer (cmp <> CmpThrows _), skipping equality evaluation (moved := false, omitting notify_dirty_except) yields the exact same engine state s' as ready_value_moved plus conditional notify_dirty_except whenever no un-owed queued flush transition triggers.

Arguments

f : nat
marking fuel bound.
prev_val : val
previously published memo value.
next_val : val
newly produced memo value.
cmp : comparer
memo cutoff comparer (non-throwing).
leaf memo node id.
r : option node_id
optional current computation passed to notify_dirty_except.
s : state
starting engine state with empty observers on n.
Theorem leaf_memo_no_cutoff_eq
    (f : nat) (prev_val next_val : val) (cmp : comparer) (n : node_id)
    (r : option node_id) (s : state) :
  observer_items (observers_of (g_edges s) n) = [] ->
  (forall y, cmp <> CmpThrows y) ->
  notify_dirty_except f n r s = Some s /\
  ((bool_decide (g_batch_depth s = 0%nat) && negb (g_flushing s) && live_queue s && negb (g_flush_owed s) = false) ->
   ref_ready_cutoff_step f prev_val next_val cmp n s = leaf_no_cutoff_step n s).

Arguments

f : nat
marking fuel bound.
prev_val : val
previously published memo value.
next_val : val
newly produced memo value.
cmp : comparer
memo cutoff comparer (non-throwing).
leaf memo node id.
r : option node_id
optional current computation passed to notify_dirty_except.
s : state
starting engine state with empty observers on n.
Qed
Proof.
  intros Hobs Hnt.
  assert (Hnde : forall cur st,
    observer_items (observers_of (g_edges st) n) = [] ->
    notify_dirty_except f n cur st = Some st).
  { intros cur st Hst. unfold notify_dirty_except. apply notify_with_nil. exact Hst. }
  split; [exact (Hnde r s Hobs)|].
  intros Hno_owe.
  unfold ref_ready_cutoff_step, leaf_no_cutoff_step, ready_value_moved.
  assert (Hcmp : exists b : bool,
    match cmp with
    | CmpEq => m_return (bool_decide (next_val <> prev_val))
    | CmpNever => m_return true
    | CmpThrows y =>
      let* _ := modify_node n (fun x => node_with_status StError (node_with_failure (Some y) (node_with_value prev_val x))) in
      m_return true
    end s = Some (s, b)).
  { destruct cmp as [| |y]; eauto. exfalso. exact (Hnt y eq_refl). }
  destruct Hcmp as [moved Hmoved].
  unfold m_bind at 1. rewrite Hmoved.
  rewrite !m_bind_get, !m_bind_modify.
  assert (Hgh : ghost_run_end n (g_current s) moved s = ghost_run_end n (g_current s) false s).
  { unfold ghost_run_end. destruct (g_nodes s !! n) as [dn|]; [|reflexivity].
    f_equal. f_equal. unfold run_end_unnoticed.
    assert (Hempty_view : observer_view (observers_of (g_edges s) n) = []).
    { unfold observer_view. rewrite Hobs. reflexivity. }
    rewrite Hempty_view. simpl.
    destruct moved, (decide (gh_published (g_ghost s) !! n = Some (read_view_of dn))); reflexivity. }
  destruct moved; [|reflexivity].
  set (sg := ghost_run_end n (g_current s) true s).
  assert (Hsg_obs : observer_items (observers_of (g_edges sg) n) = []).
  { unfold sg, ghost_run_end. destruct (g_nodes s !! n); exact Hobs. }
  rewrite m_bind_step with (s' := sg) by (exact (Hnde (g_current s) sg Hsg_obs)).
  rewrite m_bind_get.
  assert (Hsg_fields : g_batch_depth sg = g_batch_depth s /\ g_flushing sg = g_flushing s /\
                       live_queue sg = live_queue s /\ g_flush_owed sg = g_flush_owed s).
  { unfold sg, ghost_run_end, live_queue. destruct (g_nodes s !! n); repeat split; reflexivity. }
  destruct Hsg_fields as (Hbd & Hfl & Hlq & Hfo).
  rewrite Hbd, Hfl, Hlq.
  destruct (bool_decide (g_batch_depth s = 0%nat) && negb (g_flushing s) && live_queue s) eqn:Hcond.
  - simpl in Hno_owe. unfold m_modify, sg. rewrite Hgh.
    destruct (g_flush_owed s) eqn:Efo; [|discriminate].
    unfold ghost_run_end, with_flush_owed.
    destruct (g_nodes s !! n), s; simpl in *; rewrite Efo; reflexivity.
  - unfold m_return, sg. rewrite Hgh. reflexivity.
Qed.
References
Definition write_signal_cmp (cmp_eq : val -> val -> bool) (fuel : nat) (n : node_id) (v : val) : M unit :=
  match fuel with
  | 0 => stuck
  | S f =>
    let* d := get_node n in
    match n_kind d with
    | KSignal =>
      if cmp_eq (n_value d) v then m_return tt
      else
        let* _ := modify_node n (node_with_value v) in
        let* _ := m_modify (ghost_publish n) in
        let* _ := m_step (notify_dirty f n) in
        request_flush f
    | _ => stuck
    end
  end.
References
Definition fused_single_signal_effect_cmp
    (cmp_eq : val -> val -> bool) (f : nat) (src eff : node_id) (v : val) : M unit :=
  let* d := get_node src in
  match n_kind d with
  | KSignal =>
    if cmp_eq (n_value d) v then m_return tt
    else
      let* _ := modify_node src (node_with_value v) in
      let* _ := m_modify (ghost_publish src) in
      let* _ := m_modify (fun s => with_flush_owed false (with_flushing true s)) in
      let* _ := modify_node eff (fun x => node_with_freshness Clean (node_with_queued false x)) in
      let* _ := run_effect f eff in
      let* _ := drain f in
      m_modify (with_flushing false)
  | _ => stuck
  end.
References
✓ No axioms

When writing v to an idle-graph signal with a single observer effect eff using a non-throwing custom comparer cmp_eq evaluating unequal (cmp_eq (n_value d_src) v = false), fused S -> E execution equals write_signal_cmp (and matches write_signal when cmp_eq is reflexive).

Arguments

cmp_eq : val -> val -> bool
pure boolean custom cutoff comparer.
src : node_id
signal node id.
d_src : State.node
signal node snapshot in s.
eff : node_id
sole observer effect node id.
slot : nat
edge slot of eff on src.
de : State.node
effect node snapshot in s.
v : val
new value written to src.
s : State.state
idle starting engine state.
s' : State.state
final engine state after flush completes.
Theorem custom_comparer_fused_flush_eq
    (cmp_eq : val -> val -> bool) src d_src eff slot de v s s' :
  g_nodes s !! src = Some d_src ->
  n_kind d_src = KSignal ->
  cmp_eq (n_value d_src) v = false ->
  observer_items (observers_of (g_edges s) src) = [(eff, slot)] ->
  g_nodes s !! eff = Some de ->
  n_kind de = KEffect ->
  n_disposed de = false ->
  n_in_cleanup de = false ->
  n_queued de = false ->
  g_batch_depth s = 0 ->
  g_pull_depth s = 0 ->
  g_flushing s = false ->
  g_current s = None ->
  g_untracked s = false ->
  g_flush_owed s = false ->
  pending s = [] ->
  g_queue_head s <= length (g_queue s) ->
  ((exists f, fused_single_signal_effect_cmp cmp_eq f src eff v s = Some (s', tt)) <->
   (exists f, write_signal_cmp cmp_eq f src v s = Some (s', tt))) /\
  ((forall x, cmp_eq x x = true) ->
   ((exists f, write_signal_cmp cmp_eq f src v s = Some (s', tt)) <->
    (exists f, write_signal f src v s = Some (s', tt)))).

Arguments

cmp_eq : val -> val -> bool
pure boolean custom cutoff comparer.
src : node_id
signal node id.
d_src : State.node
signal node snapshot in s.
eff : node_id
sole observer effect node id.
slot : nat
edge slot of eff on src.
de : State.node
effect node snapshot in s.
v : val
new value written to src.
s : State.state
idle starting engine state.
s' : State.state
final engine state after flush completes.
Qed
Proof.
  intros Hsrc Hks Hneq Hobs Heff Hke Hde Hclean Hnq Hbd Hpd Hfl Hcur Hunt Hfo Hpend Hqle.
  assert (Hne : src <> eff) by (intros ->; rewrite Hsrc in Heff; simplify_eq; congruence).
  set (d_diff := node_with_value (v + 1)%Z d_src).
  set (s_diff := set_node src d_diff s).
  assert (Hsdiff_src : g_nodes s_diff !! src = Some d_diff) by (unfold s_diff, set_node; simpl; apply lookup_insert_eq).
  assert (Hsdiff_eff : g_nodes s_diff !! eff = Some de) by (unfold s_diff, set_node; simpl; rewrite lookup_insert_ne by exact Hne; exact Heff).
  assert (Hstep_ws : forall f, write_signal_cmp cmp_eq (S f) src v s = write_signal (S f) src v s_diff).
  { intros f. cbn [write_signal_cmp write_signal].
    rewrite (m_bind_get_node _ _ _ _ Hsrc), (m_bind_get_node _ _ _ _ Hsdiff_src).
    unfold d_diff; simpl. rewrite Hks, Hneq.
    rewrite decide_False by lia.
    rewrite (m_bind_modify_node _ _ _ _ _ Hsrc), (m_bind_modify_node _ _ _ _ _ Hsdiff_src).
    rewrite !m_bind_modify.
    assert (Hpub_eq :
      ghost_publish src (set_node src (node_with_value v d_src) s) =
      ghost_publish src (set_node src (node_with_value v (node_with_value (v + 1)%Z d_src)) s_diff)).
    { unfold ghost_publish, s_diff, set_node. simpl.
      replace (node_with_value v (node_with_value (v + 1)%Z d_src)) with (node_with_value v d_src)
        by (unfold node_with_value; destruct d_src; reflexivity).
      rewrite insert_insert_eq, !lookup_insert_eq. reflexivity. }
    rewrite Hpub_eq. reflexivity. }
  assert (Hstep_fused : forall f, fused_single_signal_effect_cmp cmp_eq f src eff v s = fused_single_signal_effect f src eff v s_diff).
  { intros f. unfold fused_single_signal_effect_cmp, fused_single_signal_effect.
    rewrite (m_bind_get_node _ _ _ _ Hsrc), (m_bind_get_node _ _ _ _ Hsdiff_src).
    unfold d_diff; simpl. rewrite Hks, Hneq.
    rewrite decide_False by lia.
    rewrite (m_bind_modify_node _ _ _ _ _ Hsrc), (m_bind_modify_node _ _ _ _ _ Hsdiff_src).
    rewrite !m_bind_modify.
    assert (Hpub_eq :
      ghost_publish src (set_node src (node_with_value v d_src) s) =
      ghost_publish src (set_node src (node_with_value v (node_with_value (v + 1)%Z d_src)) s_diff)).
    { unfold ghost_publish, s_diff, set_node. simpl.
      replace (node_with_value v (node_with_value (v + 1)%Z d_src)) with (node_with_value v d_src)
        by (unfold node_with_value; destruct d_src; reflexivity).
      rewrite insert_insert_eq, !lookup_insert_eq. reflexivity. }
    rewrite Hpub_eq. reflexivity. }
  pose proof (fused_single_signal_effect_eq src d_diff eff slot de v s_diff s'
    Hsdiff_src Hks Hobs Hsdiff_eff Hke Hde Hclean Hnq Hbd Hpd Hfl Hcur Hunt Hfo Hpend Hqle) as Heq_diff.
  split.
  - split; intros [f Hf].
    + rewrite Hstep_fused in Hf.
      destruct Heq_diff as [Hfwd _]. destruct (Hfwd (ex_intro _ f Hf)) as [fw Hfw].
      destruct fw as [|fw']; [discriminate|]. exists (S fw'). rewrite Hstep_ws. exact Hfw.
    + destruct f as [|f']; [discriminate|]. rewrite Hstep_ws in Hf.
      destruct Heq_diff as [_ Hbwd]. destruct (Hbwd (ex_intro _ (S f') Hf)) as [ff Hff].
      exists ff. rewrite Hstep_fused. exact Hff.
  - intros Hrefl.
    assert (Hne_val : n_value d_src <> v).
    { intro Heqv. rewrite Heqv, Hrefl in Hneq. discriminate. }
    assert (Hws_same : forall f, write_signal_cmp cmp_eq (S f) src v s = write_signal (S f) src v s).
    { intros f. cbn [write_signal_cmp write_signal].
      rewrite (m_bind_get_node _ _ _ _ Hsrc), (m_bind_get_node _ _ _ _ Hsrc).
      rewrite Hks, Hneq, decide_False by exact Hne_val. reflexivity. }
    split; intros [f Hf]; destruct f as [|f']; try discriminate.
    + exists (S f'). rewrite <- Hws_same. exact Hf.
    + exists (S f'). rewrite Hws_same. exact Hf.
Qed.
References

B9b: Idle first-run effect fusion (`EffectNode.Start` / `EffectOn.Start`)

Definition fused_first_run_effect (f : nat) (eff : node_id) : M unit :=
  let* _ := m_modify (fun s => with_flush_owed false (with_flushing true s)) in
  let* _ := run_effect f eff in
  let* _ := drain f in
  m_modify (with_flushing false).
References
✓ No axioms

When a newly attached Clean, unqueued, non-disposed effect eff is triggered for its first run on an idle graph with empty pending queue, mark_dirty followed by request_flush is observationally and state-identical to opening the flush window directly, running eff, draining any newly enqueued work, and closing flushing (EffectNode.Start / EffectOn.Start, Core.fs:8478,13125).

Arguments

eff : node_id
newly attached effect node id.
de : State.node
descriptor of eff in the idle starting state s.
s : State.state
idle starting engine state with empty pending queue.
s' : State.state
final engine state after flush completes.
Theorem idle_first_run_effect_fusion_eq eff de s s' :
  g_nodes s !! eff = Some de ->
  n_kind de = KEffect ->
  n_freshness de = Clean ->
  n_disposed de = false ->
  n_in_cleanup de = false ->
  n_queued de = false ->
  g_batch_depth s = 0 ->
  g_pull_depth s = 0 ->
  g_flushing s = false ->
  g_current s = None ->
  g_untracked s = false ->
  g_flush_owed s = false ->
  pending s = [] ->
  g_queue_head s <= length (g_queue s) ->
  (exists f,
    (let* _ := m_step (mark_dirty f eff) in request_flush f) s = Some (s', tt)) <->
  (exists f, fused_first_run_effect f eff s = Some (s', tt)).

Arguments

eff : node_id
newly attached effect node id.
de : State.node
descriptor of eff in the idle starting state s.
s : State.state
idle starting engine state with empty pending queue.
s' : State.state
final engine state after flush completes.
Qed
Proof.
  intros Heff Hke Hcf Hde Hclean Hnq Hbd Hpd Hfl Hcur Hunt Hfo Hpend Hqle.
  assert (Hqhd : g_queue_head s = length (g_queue s)).
  { unfold pending in Hpend. pose proof (f_equal length Hpend) as Hlen.
    rewrite length_drop in Hlen. simpl in Hlen. lia. }
  assert (Hde_id :
    node_with_freshness Clean (node_with_queued false (node_marked Dirty (n_unannounced de) true de)) = de).
  { rewrite node_clean_unqueued_marked_dirty. destruct de; simpl in *; subst; reflexivity. }
  set (s1 := enqueue eff (set_node eff (node_marked Dirty (n_unannounced de) true de) s)).
  set (s2 := with_flush_owed false (with_flushing true s1)).
  set (s3 := with_queue_head (S (g_queue_head s)) s2).
  set (s_pre := with_queue_head (S (g_queue_head s))
                  (with_flush_owed false (with_flushing true (enqueue eff s)))).
  set (sw0 := with_flush_owed false (with_flushing true s)).
  assert (Hs_pre_eq : set_node eff de s3 = s_pre).
  { unfold s_pre, s3, s2, s1, enqueue, set_node; simpl.
    rewrite insert_insert_eq, (insert_id _ _ _ Heff).
    destruct s; reflexivity. }
  assert (Hs3_eff : g_nodes s3 !! eff = Some (node_marked Dirty (n_unannounced de) true de)).
  { unfold s3, s2, s1, enqueue, set_node; simpl. apply lookup_insert_eq. }
  assert (Hrel : queue_rel s_pre sw0).
  { unfold queue_rel, pending, s_pre, sw0, enqueue; simpl.
    split; [destruct s; reflexivity|].
    split; [rewrite drop_ge; [symmetry; exact Hpend|rewrite length_app; simpl; lia]|].
    rewrite length_app; simpl. split; [lia|exact Hqle]. }
  assert (Hmark : forall f',
    m_step (mark_dirty (S f') eff) s = Some (s1, tt)).
  { intros f'. unfold m_step. cbn [mark_dirty].
    rewrite Heff, Hke. unfold effect_marked.
    rewrite Hde, Hclean, Hnq. reflexivity. }
  assert (Hreq : forall f',
    request_flush (S f') s1 = run_flush f' s1).
  { intros f'. cbn [request_flush]. rewrite bind_get.
    assert (Hlq : live_queue s1 = true).
    { unfold live_queue, s1, enqueue; simpl.
      rewrite length_app, Hqhd; simpl. apply bool_decide_true; lia. }
    assert (Hob : outside_bodies s1 = true).
    { unfold outside_bodies, s1, enqueue, set_node; simpl.
      rewrite Hpd, Hcur, Hunt, bool_decide_true by reflexivity. reflexivity. }
    assert (Hsbd : bool_decide (g_batch_depth s1 = 0%nat) = true).
    { unfold s1, enqueue, set_node; simpl. rewrite Hbd. apply bool_decide_true; reflexivity. }
    rewrite Hlq, Hsbd, Hob. reflexivity. }
  assert (Hrf : forall f',
    run_flush (S f') s1 =
    (let* _ := drain f' in m_modify (with_flushing false)) s2).
  { intros f'. cbn [run_flush]. rewrite bind_get.
    assert (Hsfl : g_flushing s1 = false) by exact Hfl.
    rewrite Hsfl. unfold m_bind at 1, m_modify. reflexivity. }
  assert (Hdr : forall f',
    drain (S f') s2 =
    (let* _ := execute_effect f' eff in drain f') s3).
  { intros f'. cbn [drain]. rewrite bind_get.
    assert (Hluq : g_queue s2 !! g_queue_head s2 = Some eff).
    { unfold s2, s1, enqueue, set_node; simpl.
      rewrite Hqhd, lookup_app_r, Nat.sub_diag by lia. reflexivity. }
    rewrite Hluq. unfold m_bind at 1, m_modify. reflexivity. }
  assert (Hexe : forall k,
    execute_effect (S k) eff s3 = run_effect k eff s_pre).
  { intros k. cbn [execute_effect].
    unfold m_bind at 1, get_node at 1. rewrite Hs3_eff.
    simpl. rewrite Hde. simpl.
    unfold m_bind at 1, m_return at 1. simpl.
    unfold m_bind at 1, get_node at 1. rewrite Hs3_eff. simpl.
    unfold m_bind at 1, modify_node at 1. rewrite Hs3_eff. simpl.
    rewrite Hde_id, Hs_pre_eq.
    reflexivity. }
  assert (Hunder4 : forall f,
    f < 4 ->
    (let* _ := m_step (mark_dirty f eff) in request_flush f) s = None).
  { intros f Hf. destruct f as [|[|[|[|k]]]]; try lia.
    - reflexivity.
    - unfold m_bind at 1. rewrite Hmark, Hreq. reflexivity.
    - unfold m_bind at 1. rewrite Hmark, Hreq, Hrf. reflexivity.
    - unfold m_bind at 1. rewrite Hmark, Hreq, Hrf.
      unfold m_bind at 1. rewrite Hdr. reflexivity. }
  assert (Hpre : forall k,
    (let* _ := m_step (mark_dirty (S (S (S (S k)))) eff) in
     request_flush (S (S (S (S k))))) s =
    (let* _ := run_effect k eff in
     let* _ := drain (S k) in
     m_modify (with_flushing false)) s_pre).
  { intros k. unfold m_bind at 1.
    rewrite Hmark, Hreq, Hrf.
    unfold m_bind at 1. rewrite Hdr.
    unfold m_bind. rewrite Hexe.
    destruct (run_effect k eff s_pre) as [[sf []]|]; [|reflexivity].
    destruct (drain (S k) sf) as [[sf' []]|]; reflexivity. }
  split.
  - intros [f Hf].
    destruct f as [|[|[|[|k]]]].
    + rewrite Hunder4 in Hf by lia. discriminate.
    + rewrite Hunder4 in Hf by lia. discriminate.
    + rewrite Hunder4 in Hf by lia. discriminate.
    + rewrite Hunder4 in Hf by lia. discriminate.
    + rewrite Hpre in Hf.
      eapply bind_inv in Hf as (sf1 & [] & Hre & Hf).
      eapply bind_inv in Hf as (sf2 & [] & Hdr_f & Hf).
      unfold m_modify in Hf. inversion Hf as [Hs']; clear Hf.
      assert (Hre_b : exists fb, b1_run_effect fb eff s_pre = Some (sf1, tt))
        by (apply b1_run_effect_eq; exists k; exact Hre).
      destruct Hre_b as [fb1 Hre_b].
      destruct (B1Qrun_effect _ (b1_qrel_all fb1) eff _ _ _ _ Hre_b Hrel)
        as (sw1 & Hre_w & Hrel1).
      assert (Hre_e : exists f_re, run_effect f_re eff sw0 = Some (sw1, tt))
        by (apply b1_run_effect_eq; exists fb1; exact Hre_w).
      destruct Hre_e as [f_re Hre_e].
      assert (Hdr_b : exists fb, b1_drain fb sf1 = Some (sf2, tt))
        by (apply b1_drain_eq; exists (S k); exact Hdr_f).
      destruct Hdr_b as [fb2 Hdr_b].
      destruct (B1Qdrain _ (b1_qrel_all fb2) _ _ _ _ Hdr_b Hrel1)
        as (sw2 & Hdr_w & Hrel2).
      destruct (drain_resets _ _ _ Hdr_f) as [Hq_sf2 Hh_sf2].
      destruct (b1_drain_resets _ _ _ Hdr_w) as [Hq_sw2 Hh_sw2].
      assert (Heq2 : sf2 = sw2) by (eapply queue_rel_drained_eq; eassumption).
      rewrite <- Heq2 in Hdr_w.
      assert (Hdr_e : exists f_dr, drain f_dr sw1 = Some (sf2, tt))
        by (apply b1_drain_eq; exists fb2; exact Hdr_w).
      destruct Hdr_e as [f_dr Hdr_e].
      exists (Nat.max f_re f_dr).
      change (fused_first_run_effect (Nat.max f_re f_dr) eff s) with
        ((let* _ := run_effect (Nat.max f_re f_dr) eff in
          let* _ := drain (Nat.max f_re f_dr) in
          m_modify (with_flushing false)) sw0).
      assert (Hre_m : run_effect (Nat.max f_re f_dr) eff sw0 = Some (sw1, tt))
        by (eapply (Mrun_effect _ (engine_mono_all f_re)); [lia|exact Hre_e]).
      assert (Hdr_m : drain (Nat.max f_re f_dr) sw1 = Some (sf2, tt))
        by (eapply (Mdrain _ (engine_mono_all f_dr)); [lia|exact Hdr_e]).
      unfold m_bind at 1. rewrite Hre_m. cbv iota.
      unfold m_bind at 1. rewrite Hdr_m. cbv iota.
      unfold m_modify. rewrite Hs'. reflexivity.
  - intros [f Hf].
    change (fused_first_run_effect f eff s) with
      ((let* _ := run_effect f eff in
        let* _ := drain f in
        m_modify (with_flushing false)) sw0) in Hf.
    eapply bind_inv in Hf as (sw1 & [] & Hre & Hf).
    eapply bind_inv in Hf as (sw2 & [] & Hdr_f & Hf).
    unfold m_modify in Hf. inversion Hf as [Hs']; clear Hf.
    pose proof (queue_rel_sym _ _ Hrel) as Hrel'.
    assert (Hre_b : exists fb, b1_run_effect fb eff sw0 = Some (sw1, tt))
      by (apply b1_run_effect_eq; exists f; exact Hre).
    destruct Hre_b as [fb1 Hre_b].
    destruct (B1Qrun_effect _ (b1_qrel_all fb1) eff _ _ _ _ Hre_b Hrel')
      as (sf1 & Hre_f & Hrel1).
    assert (Hre_e : exists f_re, run_effect f_re eff s_pre = Some (sf1, tt))
      by (apply b1_run_effect_eq; exists fb1; exact Hre_f).
    destruct Hre_e as [f_re Hre_e].
    assert (Hdr_b : exists fb, b1_drain fb sw1 = Some (sw2, tt))
      by (apply b1_drain_eq; exists f; exact Hdr_f).
    destruct Hdr_b as [fb2 Hdr_b].
    destruct (B1Qdrain _ (b1_qrel_all fb2) _ _ _ _ Hdr_b Hrel1)
      as (sf2 & Hdr_w & Hrel2).
    destruct (drain_resets _ _ _ Hdr_f) as [Hq_sw2 Hh_sw2].
    destruct (b1_drain_resets _ _ _ Hdr_w) as [Hq_sf2 Hh_sf2].
    assert (Heq2 : sw2 = sf2) by (eapply queue_rel_drained_eq; eassumption).
    rewrite <- Heq2 in Hdr_w.
    assert (Hdr_e : exists f_dr, drain f_dr sf1 = Some (sw2, tt))
      by (apply b1_drain_eq; exists fb2; exact Hdr_w).
    destruct Hdr_e as [f_dr Hdr_e].
    exists (S (S (S (S (Nat.max f_re f_dr))))).
    rewrite Hpre.
    assert (Hre_m : run_effect (Nat.max f_re f_dr) eff s_pre = Some (sf1, tt))
      by (eapply (Mrun_effect _ (engine_mono_all f_re)); [lia|exact Hre_e]).
    assert (Hdr_m : drain (S (Nat.max f_re f_dr)) sf1 = Some (sw2, tt))
      by (eapply (Mdrain _ (engine_mono_all f_dr)); [lia|exact Hdr_e]).
    unfold m_bind at 1. rewrite Hre_m. cbv iota.
    unfold m_bind at 1. rewrite Hdr_m. cbv iota.
    unfold m_modify. rewrite Hs'. reflexivity.
Qed.
References
Inductive ambient_op :=
  | AEnter (g : nat)
  | ALeave
  | AActivate (g : nat)
  | ADeactivate (id : nat).
References

Uses

  • nat
Record act_entry := {
  ae_id : nat;
  ae_graph : nat;
  ae_clock : nat
}.
References

Uses

  • nat
Record eager_ambient := {
  ea_clock_ctr : nat;
  ea_host_depth : nat;
  ea_host_epoch : nat;
  ea_epoch_stack : list nat;
  ea_hosting : option nat;
  ea_host_stack : list (option nat);
  ea_acts : list act_entry;
  ea_next_id : nat
}.
References

Uses

Definition init_eager_ambient : eager_ambient := {|
  ea_clock_ctr := 0;
  ea_host_depth := 0;
  ea_host_epoch := 0;
  ea_epoch_stack := [];
  ea_hosting := None;
  ea_host_stack := [];
  ea_acts := [];
  ea_next_id := 0
|}.
References
Definition step_eager (op : ambient_op) (st : eager_ambient) : eager_ambient :=
  match op with
  | AEnter g =>
    let c := S (ea_clock_ctr st) in
    if decide (ea_host_depth st = 0) then
      {| ea_clock_ctr := c; ea_host_depth := 1; ea_host_epoch := c;
         ea_epoch_stack := ea_epoch_stack st; ea_hosting := Some g;
         ea_host_stack := ea_hosting st :: ea_host_stack st;
         ea_acts := ea_acts st; ea_next_id := ea_next_id st |}
    else
      {| ea_clock_ctr := c; ea_host_depth := S (ea_host_depth st); ea_host_epoch := c;
         ea_epoch_stack := ea_host_epoch st :: ea_epoch_stack st; ea_hosting := Some g;
         ea_host_stack := ea_hosting st :: ea_host_stack st;
         ea_acts := ea_acts st; ea_next_id := ea_next_id st |}
  | ALeave =>
    match ea_host_depth st with
    | 0 => st
    | 1 =>
      {| ea_clock_ctr := ea_clock_ctr st; ea_host_depth := 0; ea_host_epoch := 0;
         ea_epoch_stack := ea_epoch_stack st; ea_hosting := hd None (ea_host_stack st);
         ea_host_stack := tl (ea_host_stack st);
         ea_acts := ea_acts st; ea_next_id := ea_next_id st |}
    | S (S _ as next_d) =>
      {| ea_clock_ctr := ea_clock_ctr st; ea_host_depth := next_d;
         ea_host_epoch := hd 0 (ea_epoch_stack st);
         ea_epoch_stack := tl (ea_epoch_stack st);
         ea_hosting := hd None (ea_host_stack st);
         ea_host_stack := tl (ea_host_stack st);
         ea_acts := ea_acts st; ea_next_id := ea_next_id st |}
    end
  | AActivate g =>
    let c := S (ea_clock_ctr st) in
    {| ea_clock_ctr := c; ea_host_depth := ea_host_depth st;
       ea_host_epoch := ea_host_epoch st; ea_epoch_stack := ea_epoch_stack st;
       ea_hosting := ea_hosting st; ea_host_stack := ea_host_stack st;
       ea_acts := {| ae_id := ea_next_id st; ae_graph := g; ae_clock := c |} :: ea_acts st;
       ea_next_id := S (ea_next_id st) |}
  | ADeactivate id =>
    {| ea_clock_ctr := ea_clock_ctr st; ea_host_depth := ea_host_depth st;
       ea_host_epoch := ea_host_epoch st; ea_epoch_stack := ea_epoch_stack st;
       ea_hosting := ea_hosting st; ea_host_stack := ea_host_stack st;
       ea_acts := List.filter (fun a => negb (Nat.eqb (ae_id a) id)) (ea_acts st);
       ea_next_id := ea_next_id st |}
  end.
References
Definition resolve_eager (st : eager_ambient) : option nat :=
  let act_top := match ea_acts st with a :: _ => Some (ae_graph a) | [] => None end in
  if Nat.ltb 0 (ea_host_depth st) then
    if existsb (fun a => Nat.ltb (ea_host_epoch st) (ae_clock a)) (ea_acts st)
    then act_top else ea_hosting st
  else act_top.
References
Record lazy_ambient := {
  la_clock : nat;
  la_host_depth : nat;
  la_host_epoch : nat;
  la_epoch_depth : nat;
  la_epoch_stack : list (nat * nat);
  la_hosting : option nat;
  la_host_stack : list (option nat);
  la_acts : list act_entry;
  la_next_id : nat
}.
References

Uses

Definition init_lazy_ambient : lazy_ambient := {|
  la_clock := 0;
  la_host_depth := 0;
  la_host_epoch := 0;
  la_epoch_depth := 0;
  la_epoch_stack := [];
  la_hosting := None;
  la_host_stack := [];
  la_acts := [];
  la_next_id := 0
|}.
References
Definition step_lazy (op : ambient_op) (st : lazy_ambient) : lazy_ambient :=
  match op with
  | AEnter g =>
    if decide (la_host_depth st = 0) then
      {| la_clock := la_clock st; la_host_depth := 1;
         la_host_epoch := la_clock st; la_epoch_depth := 0;
         la_epoch_stack := la_epoch_stack st; la_hosting := Some g;
         la_host_stack := la_hosting st :: la_host_stack st;
         la_acts := la_acts st; la_next_id := la_next_id st |}
    else if decide (la_host_epoch st = la_clock st) then
      {| la_clock := la_clock st; la_host_depth := S (la_host_depth st);
         la_host_epoch := la_host_epoch st; la_epoch_depth := la_epoch_depth st;
         la_epoch_stack := la_epoch_stack st; la_hosting := Some g;
         la_host_stack := la_hosting st :: la_host_stack st;
         la_acts := la_acts st; la_next_id := la_next_id st |}
    else
      {| la_clock := la_clock st; la_host_depth := S (la_host_depth st);
         la_host_epoch := la_clock st; la_epoch_depth := S (la_host_depth st);
         la_epoch_stack := (la_host_epoch st, la_epoch_depth st) :: la_epoch_stack st;
         la_hosting := Some g;
         la_host_stack := la_hosting st :: la_host_stack st;
         la_acts := la_acts st; la_next_id := la_next_id st |}
  | ALeave =>
    match la_host_depth st with
    | 0 => st
    | 1 =>
      {| la_clock := la_clock st; la_host_depth := 0;
         la_host_epoch := 0; la_epoch_depth := 0;
         la_epoch_stack := la_epoch_stack st;
         la_hosting := hd None (la_host_stack st);
         la_host_stack := tl (la_host_stack st);
         la_acts := la_acts st; la_next_id := la_next_id st |}
    | S (S _ as next_d) =>
      if decide (la_host_depth st = la_epoch_depth st) then
        let '(pe, pd) := hd (0, 0) (la_epoch_stack st) in
        {| la_clock := la_clock st; la_host_depth := next_d;
           la_host_epoch := pe; la_epoch_depth := pd;
           la_epoch_stack := tl (la_epoch_stack st);
           la_hosting := hd None (la_host_stack st);
           la_host_stack := tl (la_host_stack st);
           la_acts := la_acts st; la_next_id := la_next_id st |}
      else
        {| la_clock := la_clock st; la_host_depth := next_d;
           la_host_epoch := la_host_epoch st; la_epoch_depth := la_epoch_depth st;
           la_epoch_stack := la_epoch_stack st;
           la_hosting := hd None (la_host_stack st);
           la_host_stack := tl (la_host_stack st);
           la_acts := la_acts st; la_next_id := la_next_id st |}
    end
  | AActivate g =>
    let c := S (la_clock st) in
    {| la_clock := c; la_host_depth := la_host_depth st;
       la_host_epoch := la_host_epoch st; la_epoch_depth := la_epoch_depth st;
       la_epoch_stack := la_epoch_stack st;
       la_hosting := la_hosting st; la_host_stack := la_host_stack st;
       la_acts := {| ae_id := la_next_id st; ae_graph := g; ae_clock := c |} :: la_acts st;
       la_next_id := S (la_next_id st) |}
  | ADeactivate id =>
    {| la_clock := la_clock st; la_host_depth := la_host_depth st;
       la_host_epoch := la_host_epoch st; la_epoch_depth := la_epoch_depth st;
       la_epoch_stack := la_epoch_stack st;
       la_hosting := la_hosting st; la_host_stack := la_host_stack st;
       la_acts := List.filter (fun a => negb (Nat.eqb (ae_id a) id)) (la_acts st);
       la_next_id := la_next_id st |}
  end.
References
Definition resolve_ambient (st : lazy_ambient) : option nat :=
  let act_top := match la_acts st with a :: _ => Some (ae_graph a) | [] => None end in
  if Nat.ltb 0 (la_host_depth st) then
    if existsb (fun a => Nat.ltb (la_host_epoch st) (ae_clock a)) (la_acts st)
    then act_top else la_hosting st
  else act_top.
References
Fixpoint ambient_wf (d : nat) (tr : list ambient_op) : Prop :=
  match tr with
  | [] => True
  | op :: rest =>
    match op with
    | AEnter _ => ambient_wf (S d) rest
    | ALeave => 0 < d /\ ambient_wf (pred d) rest
    | AActivate _ => ambient_wf d rest
    | ADeactivate _ => ambient_wf d rest
    end
  end.
References
Fixpoint run_eager_trace (st : eager_ambient) (tr : list ambient_op) : eager_ambient :=
  match tr with
  | [] => st
  | op :: rest => run_eager_trace (step_eager op st) rest
  end.
References
Fixpoint run_lazy_trace (st : lazy_ambient) (tr : list ambient_op) : lazy_ambient :=
  match tr with
  | [] => st
  | op :: rest => run_lazy_trace (step_lazy op st) rest
  end.
References
Fixpoint lazy_stack_inv
    (cs : list nat) (depth epoch edepth : nat) (stk : list (nat * nat)) : Prop :=
  match cs with
  | [] => depth = 0 /\ epoch = 0 /\ edepth = 0 /\ stk = []
  | [c] => depth = 1 /\ epoch = c /\ edepth = 0 /\ stk = []
  | c1 :: ((c2 :: _) as tail) =>
    depth = S (length tail) /\
    epoch = c1 /\
    if decide (c1 = c2) then
      lazy_stack_inv tail (length tail) epoch edepth stk
    else
      edepth = S (length tail) /\
      exists ed' stk', stk = (c2, ed') :: stk' /\ lazy_stack_inv tail (length tail) c2 ed' stk'
  end.
References

Uses

  • S
  • length
  • list
  • nat
  • decide
Lemma lazy_stack_inv_bounds cs depth epoch edepth stk :
  lazy_stack_inv cs depth epoch edepth stk ->
  depth = length cs /\ edepth <= depth.
Qed
Proof.
  revert depth epoch edepth stk.
  induction cs as [|c1 [|c2 tail] IH]; intros depth epoch edepth stk H.
  - destruct H as (-> & -> & -> & ->). simpl. lia.
  - destruct H as (-> & -> & -> & ->). simpl. lia.
  - destruct H as (Hdep & Hep & Hbr).
    destruct (decide (c1 = c2)) as [Heq|Hne].
    + destruct (IH _ _ _ _ Hbr) as [Hlen Hle]. simpl in *. lia.
    + destruct Hbr as (-> & ed' & stk' & -> & Htail). simpl in *. lia.
Qed.
References

Uses

Definition ambient_sim_rel (epochs : list (nat * nat)) (es : eager_ambient) (ls : lazy_ambient) : Prop :=
  ea_host_depth es = length epochs /\
  la_host_depth ls = length epochs /\
  ea_hosting es = la_hosting ls /\
  ea_host_stack es = la_host_stack ls /\
  ea_next_id es = la_next_id ls /\
  (match epochs with
   | [] => ea_host_epoch es = 0 /\ ea_epoch_stack es = []
   | (ee, _) :: rest => ea_host_epoch es = ee /\ ea_epoch_stack es = map fst rest
   end) /\
  lazy_stack_inv (map snd epochs) (la_host_depth ls) (la_host_epoch ls) (la_epoch_depth ls) (la_epoch_stack ls) /\
  Forall (fun '(ee, le) => ee <= ea_clock_ctr es /\ le <= la_clock ls) epochs /\
  Forall2 (fun ea la =>
    ae_id ea = ae_id la /\
    ae_graph ea = ae_graph la /\
    ae_clock ea <= ea_clock_ctr es /\
    ae_clock la <= la_clock ls /\
    Forall (fun '(ee, le) => ee < ae_clock ea <-> le < ae_clock la) epochs)
    (ea_acts es) (la_acts ls).
References
Lemma Forall2_filter_acts epochs ec lc (eas las : list act_entry) id :
  Forall2 (fun ea la =>
    ae_id ea = ae_id la /\ ae_graph ea = ae_graph la /\
    ae_clock ea <= ec /\ ae_clock la <= lc /\
    Forall (fun '(ee, le) => ee < ae_clock ea <-> le < ae_clock la) epochs)
    eas las ->
  Forall2 (fun ea la =>
    ae_id ea = ae_id la /\ ae_graph ea = ae_graph la /\
    ae_clock ea <= ec /\ ae_clock la <= lc /\
    Forall (fun '(ee, le) => ee < ae_clock ea <-> le < ae_clock la) epochs)
    (List.filter (fun a => negb (Nat.eqb (ae_id a) id)) eas)
    (List.filter (fun a => negb (Nat.eqb (ae_id a) id)) las).
Qed
Proof.
  intros H. induction H as [|ea la eas' las' (Hid & Hrem) _ IH]; simpl; [constructor|].
  rewrite Hid. destruct (negb (Nat.eqb (ae_id la) id)); [constructor; [|exact IH]; split; assumption|exact IH].
Qed.
References

Uses

2 more
  • Forall2
  • filter
Lemma step_ambient_sim epochs es ls op :
  ambient_sim_rel epochs es ls ->
  (op = ALeave -> 0 < length epochs) ->
  exists epochs',
    ambient_sim_rel epochs' (step_eager op es) (step_lazy op ls) /\
    (match op with
     | AEnter _ => length epochs' = S (length epochs)
     | ALeave => length epochs' = pred (length epochs)
     | _ => length epochs' = length epochs
     end).
Qed
Proof.
  intros (Hed & Hld & Hhost & Hstk & Hnid & Hea & Hlz & Hep & Hacts) Hwf.
  destruct op as [g| |g|id].
  - exists ((S (ea_clock_ctr es), la_clock ls) :: epochs).
    split; [|reflexivity].
    unfold step_eager, step_lazy.
    destruct epochs as [|[ee0 le0] rest].
    + simpl in Hed, Hld. rewrite decide_True by exact Hed. rewrite decide_True by exact Hld.
      destruct Hea as [_ ->]. unfold lazy_stack_inv in Hlz. destruct Hlz as (_ & _ & _ & ->).
      repeat split; simpl; try assumption; try reflexivity.
      * rewrite Hhost, Hstk. reflexivity.
      * constructor; [lia|constructor].
      * eapply Forall2_impl; [exact Hacts|].
        intros ea la (Hid & Hgr & Hec & Hlc & Hfa).
        repeat split; try assumption; try lia.
        constructor; [split; intro; lia|exact Hfa].
    + simpl in Hed, Hld. rewrite decide_False by (intro; lia). rewrite decide_False by (intro; lia).
      destruct Hea as [Hee Hestk].
      assert (Hlz_head : la_host_epoch ls = le0).
      { change (map snd ((ee0, le0) :: rest)) with (le0 :: map snd rest) in Hlz.
        destruct rest as [|[ee1 le1] rest']; simpl in Hlz; [exact (proj1 (proj2 Hlz))|exact (proj1 (proj2 Hlz))]. }
      rewrite Hlz_head.
      destruct (decide (le0 = la_clock ls)) as [Hsame|Hdiff]; repeat split; simpl; rewrite ?length_map; try assumption; try lia.
      * rewrite Hhost, Hstk. reflexivity.
      * rewrite Hee, Hestk. reflexivity.
      * fold (map snd rest). rewrite decide_True by (symmetry; exact Hsame).
        destruct rest as [|[ee1 le1] rest']; simpl in *.
        -- rewrite Hlz_head in Hlz. destruct Hlz as (H1 & H2 & H3 & H4).
           repeat split; assumption || reflexivity.
        -- rewrite ?length_map in Hlz |- *.
           destruct Hlz as (Hdep' & Hep' & Hbr').
           repeat split; try assumption; try reflexivity.
           rewrite Hlz_head in Hbr'. exact Hbr'.
      * constructor; [lia|]. eapply Forall_impl; [exact Hep|]. intros [ee le] [? ?]; lia.
      * eapply Forall2_impl; [exact Hacts|].
        intros ea la (Hid & Hgr & Hec & Hlc & Hfa).
        repeat split; try assumption; try lia.
        constructor; [split; intro; lia|exact Hfa].
      * rewrite Hhost, Hstk. reflexivity.
      * rewrite Hee, Hestk. reflexivity.
      * fold (map snd rest). rewrite decide_False by (intro Heqc; apply Hdiff; symmetry; exact Heqc).
        split; [lia|].
        exists (la_epoch_depth ls), (la_epoch_stack ls).
        split; [reflexivity|].
        destruct rest as [|[ee1 le1] rest']; simpl in *.
        -- rewrite Hlz_head in Hlz. destruct Hlz as (H1 & H2 & H3 & H4).
           repeat split; assumption || reflexivity.
        -- rewrite ?length_map in Hlz |- *.
           destruct Hlz as (Hdep' & Hep' & Hbr').
           repeat split; try assumption; try reflexivity.
           rewrite Hlz_head in Hbr'. exact Hbr'.
      * constructor; [lia|]. eapply Forall_impl; [exact Hep|]. intros [ee le] [? ?]; lia.
      * eapply Forall2_impl; [exact Hacts|].
        intros ea la (Hid & Hgr & Hec & Hlc & Hfa).
        repeat split; try assumption; try lia.
        constructor; [split; intro; lia|exact Hfa].
  - specialize (Hwf eq_refl).
    destruct epochs as [|[ee0 le0] [|[ee1 le1] rest]]; simpl in *; [lia| |].
    + exists []. split; [|reflexivity].
      rewrite Hed, Hld. destruct Hea as [_ ->].
      destruct Hlz as (_ & _ & _ & ->).
      repeat split; simpl; try assumption; try reflexivity; try constructor.
      * rewrite Hstk. reflexivity.
      * rewrite Hstk. reflexivity.
      * eapply Forall2_impl; [exact Hacts|].
        intros ea la (Hid & Hgr & Hec & Hlc & Hfa).
        inversion Hfa; subst. repeat split; assumption.
    + exists ((ee1, le1) :: rest). split; [|reflexivity].
      rewrite Hed, Hld. destruct Hea as [_ ->].
      change (map snd ((ee0, le0) :: (ee1, le1) :: rest)) with (le0 :: le1 :: map snd rest) in Hlz.
      destruct Hlz as (Hdep & Hep0 & Hbr).
      assert (Hdep_val : la_host_depth ls = S (S (length rest))) by lia.
      destruct (decide (le0 = le1)) as [Hsame|Hdiff].
      * assert (Hbr_fold : lazy_stack_inv (le1 :: map snd rest) (S (length (map snd rest))) (la_host_epoch ls) (la_epoch_depth ls) (la_epoch_stack ls))
          by exact Hbr.
        destruct (lazy_stack_inv_bounds _ _ _ _ _ Hbr_fold) as [Hlen Hle].
        rewrite length_map in Hlen.
        rewrite decide_False by lia.
        repeat split; simpl; try assumption; try reflexivity.
        -- rewrite Hstk. reflexivity.
        -- rewrite Hstk. reflexivity.
        -- rewrite ?length_map in Hbr |- *. exact Hbr.
        -- inversion Hep; assumption.
        -- eapply Forall2_impl; [exact Hacts|].
           intros ea la (Hid & Hgr & Hec & Hlc & Hfa).
           inversion Hfa; subst. repeat split; assumption.
      * destruct Hbr as (Hedp & ed' & stk' & -> & Htail).
        rewrite decide_True by (simpl in Hedp; rewrite ?length_map in Hedp; lia).
        repeat split; simpl; try assumption; try reflexivity.
        -- rewrite Hstk. reflexivity.
        -- rewrite Hstk. reflexivity.
        -- rewrite ?length_map in Htail |- *. exact Htail.
        -- inversion Hep; assumption.
        -- eapply Forall2_impl; [exact Hacts|].
           intros ea la (Hid & Hgr & Hec & Hlc & Hfa).
           inversion Hfa; subst. repeat split; assumption.
  - exists epochs. split; [|reflexivity].
    repeat split; simpl; try assumption; try reflexivity; try lia.
    + eapply Forall_impl; [exact Hep|]. intros [ee le] [? ?]; lia.
    + constructor.
      * repeat split; simpl; try assumption; try lia.
        eapply Forall_impl; [exact Hep|]. intros [ee le] [? ?]; split; intro; lia.
      * eapply Forall2_impl; [exact Hacts|].
        intros ea la (Hid & Hgr & Hec & Hlc & Hfa).
        repeat split; try assumption; try lia.
  - exists epochs. split; [|reflexivity].
    repeat split; simpl; try assumption; try reflexivity.
    apply Forall2_filter_acts. exact Hacts.
Qed.
References

Uses

20 more
Lemma ambient_sim_resolve epochs es ls :
  ambient_sim_rel epochs es ls ->
  resolve_ambient ls = resolve_eager es.
Qed
Proof.
  intros (Hed & Hld & Hhost & _ & _ & Hea & Hlz & _ & Hacts).
  unfold resolve_ambient, resolve_eager.
  rewrite Hed, Hld.
  assert (Htop : (match la_acts ls with a :: _ => Some (ae_graph a) | [] => None end) =
                 (match ea_acts es with a :: _ => Some (ae_graph a) | [] => None end)).
  { destruct Hacts as [|ea la eas las (Hid & Hgr & _)]; [reflexivity|rewrite Hgr; reflexivity]. }
  destruct epochs as [|[ee0 le0] rest]; simpl; [exact Htop|].
  destruct Hea as [-> _].
  assert (Hle0 : la_host_epoch ls = le0).
  { change (map snd ((ee0, le0) :: rest)) with (le0 :: map snd rest) in Hlz.
    destruct rest as [|[ee1 le1] rest']; simpl in Hlz; exact (proj1 (proj2 Hlz)). }
  rewrite Hle0.
  assert (Hex : existsb (fun a => (le0 <? ae_clock a)) (la_acts ls) =
                existsb (fun a => (ee0 <? ae_clock a)) (ea_acts es)).
  { clear Htop. induction Hacts as [|ea la eas las (_ & _ & _ & _ & Hfa) _ IH]; simpl; [reflexivity|].
    apply Forall_inv in Hfa as H0. simpl in H0.
    assert (Hcmp : (le0 <? ae_clock la) = (ee0 <? ae_clock ea)).
    { destruct (Nat.ltb_spec0 le0 (ae_clock la)) as [Hl|Hl], (Nat.ltb_spec0 ee0 (ae_clock ea)) as [He|He];
        try reflexivity; exfalso;
        [apply He; apply H0; exact Hl|apply Hl; apply H0; exact He]. }
    rewrite Hcmp, IH. reflexivity. }
  rewrite Hex, Htop, Hhost. reflexivity.
Qed.
References

Uses

9 more
  • None
  • Some
  • snd
  • proj1
  • proj2
  • map
  • Nat.ltb_spec0
  • Forall_inv
  • existsb
✓ No axioms

Across every well-bracketed ambient_op trace, the lazy-epoch AmbientCell state machine (Clock incremented only on AActivate, in-place epoch update at HostDepth = 0 with EpochDepth = 0 sentinel, sparse push at HostDepth > 0 iff HostEpoch <> Clock, pop iff HostDepth = EpochDepth) resolves the exact same active ambient graph as the eager per-frame reference model.

Arguments

tr : list ambient_op
well-bracketed sequence of AEnter, ALeave, AActivate, and ADeactivate operations starting at depth 0.
Theorem ambient_lazy_epoch_trace_eq (tr : list ambient_op) :
  ambient_wf 0 tr ->
  resolve_ambient (run_lazy_trace init_lazy_ambient tr) =
  resolve_eager (run_eager_trace init_eager_ambient tr).

Arguments

tr : list ambient_op
well-bracketed sequence of AEnter, ALeave, AActivate, and ADeactivate operations starting at depth 0.
Qed
Proof.
  assert (Hgen : forall t epochs es ls,
    ambient_wf (length epochs) t ->
    ambient_sim_rel epochs es ls ->
    resolve_ambient (run_lazy_trace ls t) = resolve_eager (run_eager_trace es t)).
  { induction t as [|op rest IH]; intros epochs es ls Hwf Hsim; simpl.
    - exact (ambient_sim_resolve epochs es ls Hsim).
    - assert (Hleave_pos : op = ALeave -> 0 < length epochs).
      { intros ->. exact (proj1 Hwf). }
      destruct (step_ambient_sim epochs es ls op Hsim Hleave_pos) as (epochs' & Hsim' & Hlen').
      eapply IH; [|exact Hsim'].
      destruct op; simpl in Hwf; rewrite Hlen'; try exact Hwf; exact (proj2 Hwf). }
  intros Hwf. eapply Hgen with (epochs := []).
  - exact Hwf.
  - repeat split; constructor.
Qed.
References

B11. Sole-observer TryPromoteTop & direct-return chain step

✓ No axioms

When memo m has a sole observer e (KEffect, Check, already n_queued = true, not disposed or in cleanup) and r <> Some e, notify_dirty_except equals directly setting e's freshness to Dirty (TryPromoteTop).

Arguments

f : nat
fuel bound.
m : node_id
parent memo node id.
e : node_id
sole observer effect node id.
slot : nat
edge slot of e on m.
de : State.node
effect node record in s.
r : option node_id
optional current computation (r <> Some e).
s : State.state
starting engine state.
Theorem try_promote_top_effect_eq f m e slot de r s :
  observer_items (observers_of (g_edges s) m) = [(e, slot)] ->
  g_nodes s !! e = Some de ->
  n_kind de = KEffect ->
  n_freshness de = Check ->
  n_queued de = true ->
  n_disposed de = false ->
  n_in_cleanup de = false ->
  r <> Some e ->
  notify_dirty_except (S f) m r s = Some (set_node e (node_with_freshness Dirty de) s).

Arguments

f : nat
fuel bound.
m : node_id
parent memo node id.
e : node_id
sole observer effect node id.
slot : nat
edge slot of e on m.
de : State.node
effect node record in s.
r : option node_id
optional current computation (r <> Some e).
s : State.state
starting engine state.
Qed
Proof.
  intros Hobs He Hke Hfr Hq Hdisp Hcln Hne.
  unfold notify_dirty_except.
  erewrite notify_with_singleton; [|exact Hobs].
  unfold mark_except.
  rewrite decide_False by (intros [Heq _]; contradiction).
  cbn [mark_dirty]. rewrite He, Hke.
  unfold effect_marked. rewrite Hdisp, Hcln, Hq. simpl.
  f_equal. f_equal. unfold node_marked, node_with_freshness.
  destruct de; simpl in *; subst; reflexivity.
Qed.
References
Definition chain_moved_direct_step (moved : bool) (c : node_id) : M unit :=
  if moved then
    let* dc := get_node c in
    modify_node c (fun _ => node_with_freshness Dirty dc)
  else m_return tt.
References
Definition chain_moved_ref_step (f : nat) (moved : bool) (m : node_id) (s : state) : option state :=
  if moved then notify_dirty_except (S (S f)) m None s else Some s.
References

Uses

1 more
  • option
✓ No axioms

On a single-memo chain link where child c is the sole observer of parent m (KMemo, Check, n_unannounced = false), branching directly on moved : bool returned from m's recomputation matches notify_dirty_except followed by checking c's Freshness = Dirty.

Arguments

f : nat
marking fuel bound beyond the two outer mark_dirty/mark_check frames.
moved : bool
whether parent memo m moved.
m : node_id
parent memo node id.
c : node_id
sole observer child memo node id.
slot : nat
edge slot of c on m.
dc : State.node
child memo snapshot in s.
s : State.state
starting engine state.
s' : State.state
resulting engine state.
Theorem chain_direct_moved_step_eq f moved m c slot dc s s' :
  observer_items (observers_of (g_edges s) m) = [(c, slot)] ->
  g_nodes s !! c = Some dc ->
  n_kind dc = KMemo ->
  n_freshness dc = Check ->
  n_unannounced dc = false ->
  m_step (chain_moved_ref_step f moved m) s = Some (s', tt) <->
  chain_moved_direct_step moved c s = Some (s', tt) /\
  (exists dc', g_nodes s' !! c = Some dc' /\ bool_decide (n_freshness dc' = Dirty) = moved).

Arguments

f : nat
marking fuel bound beyond the two outer mark_dirty/mark_check frames.
moved : bool
whether parent memo m moved.
m : node_id
parent memo node id.
c : node_id
sole observer child memo node id.
slot : nat
edge slot of c on m.
dc : State.node
child memo snapshot in s.
s : State.state
starting engine state.
s' : State.state
resulting engine state.
Qed
Proof.
  intros Hobs Hc Hkm Hfr Huna.
  destruct moved.
  - unfold m_step, chain_moved_ref_step, chain_moved_direct_step.
    erewrite notify_moved_single_check_memo_eq by eassumption.
    assert (Hnode_eq : node_marked Dirty false (n_queued dc) dc = node_with_freshness Dirty dc)
      by (unfold node_marked, node_with_freshness; destruct dc; simpl in *; subst; reflexivity).
    rewrite Hnode_eq.
    rewrite (m_bind_get_node _ _ _ _ Hc).
    unfold modify_node. rewrite Hc.
    split.
    + intros H; simplify_eq. split; [reflexivity|].
      eexists; split; [unfold set_node; simpl; apply lookup_insert_eq|].
      unfold node_with_freshness; simpl; first [reflexivity|apply bool_decide_eq_true_2; reflexivity].
    + intros [H _]. simplify_eq. reflexivity.
  - unfold m_step, chain_moved_ref_step, chain_moved_direct_step, m_return.
    split.
    + intros H; simplify_eq. split; [reflexivity|].
      exists dc; split; [exact Hc|rewrite Hfr; first [reflexivity|apply bool_decide_eq_false_2; discriminate]].
    + intros [H _]. simplify_eq. reflexivity.
Qed.
References

B12. Deferring pull entry refinement (AsyncMemo / Boundary / StreamMemo)

Definition b1_deferring (s : state) : bool :=
  g_flushing s || bool_decide (0 < g_pull_depth s) || bool_decide (0 < g_batch_depth s).
References
Record b1_batch_kept_at (f : nat) : Prop := {
  B1BKexec : forall e c s s' r, b1_exec f e c s = Some (s', r) -> g_batch_depth s' = g_batch_depth s;
  B1BKread : forall n b s s' r, b1_read f n b s = Some (s', r) -> g_batch_depth s' = g_batch_depth s;
  B1BKread_node : forall n s s' r, b1_read_node f n s = Some (s', r) -> g_batch_depth s' = g_batch_depth s;
  B1BKupdate : forall n s s' r, b1_update_if_necessary f n s = Some (s', r) -> g_batch_depth s' = g_batch_depth s;
  B1BKrop : forall n s s' r, b1_refresh_or_pull f n s = Some (s', r) -> g_batch_depth s' = g_batch_depth s;
  B1BKpull : forall n s s' r, b1_pull f n s = Some (s', r) -> g_batch_depth s' = g_batch_depth s;
  B1BKrefresh : forall n s s' r, b1_refresh f n s = Some (s', r) -> g_batch_depth s' = g_batch_depth s;
  B1BKwalk : forall n i s s' r, b1_resolve_walk f n i s = Some (s', r) -> g_batch_depth s' = g_batch_depth s;
  B1BKrecompute : forall n s s' r, b1_recompute f n s = Some (s', r) -> g_batch_depth s' = g_batch_depth s;
  B1BKrun_memo : forall n s s' r, b1_run_memo f n s = Some (s', r) -> g_batch_depth s' = g_batch_depth s;
  B1BKexecute : forall n s s' r, b1_execute_effect f n s = Some (s', r) -> g_batch_depth s' = g_batch_depth s;
  B1BKrun_effect : forall n s s' r, b1_run_effect f n s = Some (s', r) -> g_batch_depth s' = g_batch_depth s;
  B1BKbody : forall n s s' r, b1_run_effect_body f n s = Some (s', r) -> g_batch_depth s' = g_batch_depth s;
  B1BKdischarge : forall o s s' r, b1_discharge f o s = Some (s', r) -> g_batch_depth s' = g_batch_depth s;
  B1BKretire : forall o s s' r, b1_retire f o s = Some (s', r) -> g_batch_depth s' = g_batch_depth s;
  B1BKdispose_scope : forall o s s' r, b1_dispose_scope f o s = Some (s', r) -> g_batch_depth s' = g_batch_depth s;
  B1BKteardown : forall o ls cs s s' r, b1_teardown f o ls cs s = Some (s', r) -> g_batch_depth s' = g_batch_depth s;
  B1BKcleanup : forall o b e s s' r, b1_run_cleanup f o b e s = Some (s', r) -> g_batch_depth s' = g_batch_depth s;
  B1BKrelease : forall n s s' r, b1_release f n s = Some (s', r) -> g_batch_depth s' = g_batch_depth s;
  B1BKdispose : forall n s s' r, b1_dispose_node f n s = Some (s', r) -> g_batch_depth s' = g_batch_depth s;
  B1BKattach : forall n o s s' r, b1_attach f n o s = Some (s', r) -> g_batch_depth s' = g_batch_depth s;
  B1BKwrite : forall n v s s' r, b1_write_signal f n v s = Some (s', r) -> g_batch_depth s' = g_batch_depth s;
  B1BKrequest : forall s s' r, b1_request_flush f s = Some (s', r) -> g_batch_depth s' = g_batch_depth s;
  B1BKsettle : forall s s' r, b1_settle_flush f s = Some (s', r) -> g_batch_depth s' = g_batch_depth s;
  B1BKflush : forall s s' r, b1_run_flush f s = Some (s', r) -> g_batch_depth s' = g_batch_depth s;
  B1BKdrain : forall s s' r, b1_drain f s = Some (s', r) -> g_batch_depth s' = g_batch_depth s
}.
References
Lemma bind_bd {A B} (m : M A) (k : A -> M B) s s' b :
  (forall s1 a, m s = Some (s1, a) -> g_batch_depth s1 = g_batch_depth s) ->
  (forall a s1, m s = Some (s1, a) -> k a s1 = Some (s', b) -> g_batch_depth s' = g_batch_depth s1) ->
  m_bind m k s = Some (s', b) -> g_batch_depth s' = g_batch_depth s.
Qed
Proof.
  intros Hm Hk H. unfold m_bind in H. destruct (m s) as [[s1 a]|] eqn:E; [|discriminate].
  rewrite (Hk a s1 eq_refl H). exact (Hm s1 a eq_refl).
Qed.
References

Uses

Used by

Lemma marks_bd s s' : rtc mark_prim s s' -> g_batch_depth s' = g_batch_depth s.
Qed
Proof.
  intros H. induction H; [reflexivity|]. rewrite IHrtc.
  destruct H; unfold set_node, enqueue; reflexivity.
Qed.
References
Lemma bind_bd_hosted {A} n (b : M A) s s' r :
  (forall s0 s1 r0, b s0 = Some (s1, r0) -> g_batch_depth s1 = g_batch_depth s0) ->
  hosted n b s = Some (s', r) -> g_batch_depth s' = g_batch_depth s.
Qed
Proof.
  intros Hb H. unfold hosted in H.
  eapply bind_inv in H as (s1 & [] & E1 & H). unfold m_modify in E1; simplify_eq.
  eapply bind_inv in H as (s2 & r2 & E2 & H).
  unfold m_bind, m_modify, m_return in H; simplify_eq.
  simpl. exact (Hb _ _ _ E2).
Qed.
References

Used by

Lemma bind_bd_untracked {A} (b : M A) s s' r :
  (forall s0 s1 r0, b s0 = Some (s1, r0) -> g_batch_depth s1 = g_batch_depth s0) ->
  untracked b s = Some (s', r) -> g_batch_depth s' = g_batch_depth s.
Qed
Proof.
  intros Hb H. unfold untracked in H.
  eapply bind_inv in H as (s1 & [] & E1 & H). unfold m_modify in E1; simplify_eq.
  eapply bind_inv in H as (s2 & r2 & E2 & H).
  unfold m_bind, m_modify, m_return in H; simplify_eq.
  simpl. exact (Hb _ _ _ E2).
Qed.
References

Used by

Lemma bind_bd_detached o (b : M unit) s s' r :
  (forall s0 s1 r0, b s0 = Some (s1, r0) -> g_batch_depth s1 = g_batch_depth s0) ->
  detached o b s = Some (s', r) -> g_batch_depth s' = g_batch_depth s.
Qed
Proof.
  intros Hb H. unfold detached in H.
  eapply bind_inv in H as (s1 & [] & E1 & H). unfold m_modify in E1; simplify_eq.
  eapply bind_inv in H as (s2 & [] & E2 & H).
  unfold m_modify in H; simplify_eq.
  simpl. rewrite (Hb _ _ _ E2). reflexivity.
Qed.
References

Used by

References
Local Ltac bd_prim :=
  match goal with
  | H : m_return _ _ = Some _ |- _ => unfold m_return in H; simplify_eq; reflexivity
  | H : stuck _ = Some _ |- _ => discriminate
  | H : Some _ = Some _ |- _ => simplify_eq; reflexivity
  | H : None = Some _ |- _ => discriminate
  | H : m_get _ = Some _ |- _ => unfold m_get in H; simplify_eq; reflexivity
  | H : get_node _ _ = Some _ |- _ => unfold get_node in H; repeat match type of H with context [match ?x with _ => _ end] => destruct x end; simplify_eq; reflexivity
  | H : modify_node _ _ _ = Some _ |- _ => unfold modify_node in H; repeat match type of H with context [match ?x with _ => _ end] => destruct x end; simplify_eq; reflexivity
  | H : get_owner _ _ = Some _ |- _ => unfold get_owner in H; repeat match type of H with context [match ?x with _ => _ end] => destruct x end; simplify_eq; reflexivity
  | H : put_owner _ _ _ = Some _ |- _ => unfold put_owner, m_modify in H; simplify_eq; reflexivity
  | H : edges _ _ = Some _ |- _ => unfold edges, m_modify in H; simplify_eq; reflexivity
  | H : edges_opt _ _ = Some _ |- _ => unfold edges_opt in H; match type of H with context [match ?x with _ => _ end] => destruct x end; simplify_eq; reflexivity
  | H : push_running _ _ = Some _ |- _ => unfold push_running, m_modify in H; simplify_eq; reflexivity
  | H : pop_running _ = Some _ |- _ => unfold pop_running, m_modify in H; simplify_eq; reflexivity
  | H : push_checking _ _ = Some _ |- _ => unfold push_checking, m_modify in H; simplify_eq; reflexivity
  | H : pop_checking _ = Some _ |- _ => unfold pop_checking, m_modify in H; simplify_eq; reflexivity
  | H : emit _ _ = Some _ |- _ => unfold emit, m_modify in H; simplify_eq; reflexivity
  | H : announce _ _ = Some _ |- _ => unfold announce, modify_node in H; repeat match type of H with context [match ?x with _ => _ end] => destruct x end; simplify_eq; reflexivity
  | H : track _ _ = Some _ |- _ => unfold track in H; repeat match type of H with context [match ?x with _ => _ end] => destruct x end; simplify_eq; bd_state_eq
  | H : mark_reader _ _ = Some _ |- _ =>
    unfold mark_reader in H;
    match type of H with context [g_current ?s0] => destruct (g_current s0) end;
    [match type of H with context [f_reader_mark ?fl] => destruct (f_reader_mark fl) end;
     [match type of H with context [mark_check ?ff ?nn ?ss] =>
        destruct (mark_check ff nn ss) eqn:Em;
        [simplify_eq; eapply marks_bd, mark_check_rtc; exact Em|discriminate]
      end|simplify_eq; reflexivity]
    |simplify_eq; reflexivity]
  | H : log_label _ = Some _ |- _ => unfold log_label in H; repeat match type of H with context [match ?x with _ => _ end] => destruct x end; simplify_eq; reflexivity
  | H : note_host_read _ _ _ = Some _ |- _ => unfold note_host_read, emit, m_modify in H; repeat match type of H with context [match ?x with _ => _ end] => destruct x end; simplify_eq; reflexivity
  | H : current_owner _ = Some _ |- _ => unfold current_owner in H; repeat match type of H with context [match ?x with _ => _ end] => destruct x end; simplify_eq; reflexivity
  | H : next_id _ = Some _ |- _ => unfold next_id in H; simplify_eq; reflexivity
  | H : add_node _ _ _ = Some _ |- _ => unfold add_node, m_modify in H; simplify_eq; bd_state_eq
  | H : m_step (mark_dirty _ _) _ = Some _ |- _ => unfold m_step in H; destruct (mark_dirty _ _ _) eqn:Em; simplify_eq; eapply marks_bd, mark_dirty_rtc; exact Em
  | H : m_step (notify_dirty _ _) _ = Some _ |- _ => unfold m_step in H; destruct (notify_dirty _ _ _) eqn:Em; simplify_eq; eapply marks_bd, (any_mark_rtc _ _ None); do 3 right; left; exact Em
  | H : m_step (notify_dirty_except _ _ _) _ = Some _ |- _ => unfold m_step in H; destruct (notify_dirty_except _ _ _ _) eqn:Em; simplify_eq; eapply marks_bd, (any_mark_rtc _ _ _); do 4 right; exact Em
  | H : m_modify ?upd _ = Some _ |- _ => unfold m_modify in H; simplify_eq; bd_state_eq
  end.
References
Local Ltac bd_solve IH :=
  repeat (
    cbv beta in *;
    try bd_prim;
    match goal with
    | H : on_node _ _ _ _ = Some _ |- _ => unfold on_node in H
    | H : unlink _ _ = Some _ |- _ => unfold unlink in H
    | H : record_error _ _ _ = Some _ |- _ => unfold record_error in H
    | H : on_value _ _ _ _ = Some _ |- _ =>
      let o := fresh "o" in
      unfold on_value in H;
      match type of H with context [eval_expr ?ee ?pp ?xx] => destruct (eval_expr ee pp xx) as [o|] end;
      [destruct o; [|simplify_eq; reflexivity]|discriminate]
    | H : m_bind _ _ _ = Some _ |- _ =>
      eapply bind_bd in H; [exact H|intros ?? ?|intros ?? _ ?]
    | H : hosted _ _ _ = Some _ |- _ =>
      eapply bind_bd_hosted in H; [exact H|intros ??? ?]
    | H : untracked _ _ = Some _ |- _ =>
      eapply bind_bd_untracked in H; [exact H|intros ??? ?]
    | H : detached _ _ _ = Some _ |- _ =>
      eapply bind_bd_detached in H; [exact H|intros ??? ?]
    | H : b1_exec _ _ _ _ = Some _ |- _ => exact (B1BKexec _ IH _ _ _ _ _ H)
    | H : b1_read _ _ _ _ = Some _ |- _ => exact (B1BKread _ IH _ _ _ _ _ H)
    | H : b1_read_node _ _ _ = Some _ |- _ => exact (B1BKread_node _ IH _ _ _ _ H)
    | H : b1_update_if_necessary _ _ _ = Some _ |- _ => exact (B1BKupdate _ IH _ _ _ _ H)
    | H : b1_refresh_or_pull _ _ _ = Some _ |- _ => exact (B1BKrop _ IH _ _ _ _ H)
    | H : b1_pull _ _ _ = Some _ |- _ => exact (B1BKpull _ IH _ _ _ _ H)
    | H : b1_refresh _ _ _ = Some _ |- _ => exact (B1BKrefresh _ IH _ _ _ _ H)
    | H : b1_resolve_walk _ _ _ _ = Some _ |- _ => exact (B1BKwalk _ IH _ _ _ _ _ H)
    | H : b1_recompute _ _ _ = Some _ |- _ => exact (B1BKrecompute _ IH _ _ _ _ H)
    | H : b1_run_memo _ _ _ = Some _ |- _ => exact (B1BKrun_memo _ IH _ _ _ _ H)
    | H : b1_execute_effect _ _ _ = Some _ |- _ => exact (B1BKexecute _ IH _ _ _ _ H)
    | H : b1_run_effect _ _ _ = Some _ |- _ => exact (B1BKrun_effect _ IH _ _ _ _ H)
    | H : b1_run_effect_body _ _ _ = Some _ |- _ => exact (B1BKbody _ IH _ _ _ _ H)
    | H : b1_discharge _ _ _ = Some _ |- _ => exact (B1BKdischarge _ IH _ _ _ _ H)
    | H : b1_retire _ _ _ = Some _ |- _ => exact (B1BKretire _ IH _ _ _ _ H)
    | H : b1_dispose_scope _ _ _ = Some _ |- _ => exact (B1BKdispose_scope _ IH _ _ _ _ H)
    | H : b1_teardown _ _ _ _ _ = Some _ |- _ => exact (B1BKteardown _ IH _ _ _ _ _ _ H)
    | H : b1_run_cleanup _ _ _ _ _ = Some _ |- _ => exact (B1BKcleanup _ IH _ _ _ _ _ _ H)
    | H : b1_release _ _ _ = Some _ |- _ => exact (B1BKrelease _ IH _ _ _ _ H)
    | H : b1_dispose_node _ _ _ = Some _ |- _ => exact (B1BKdispose _ IH _ _ _ _ H)
    | H : b1_attach _ _ _ _ = Some _ |- _ => exact (B1BKattach _ IH _ _ _ _ _ H)
    | H : b1_write_signal _ _ _ _ = Some _ |- _ => exact (B1BKwrite _ IH _ _ _ _ _ H)
    | H : b1_request_flush _ _ = Some _ |- _ => exact (B1BKrequest _ IH _ _ _ H)
    | H : b1_settle_flush _ _ = Some _ |- _ => exact (B1BKsettle _ IH _ _ _ H)
    | H : b1_run_flush _ _ = Some _ |- _ => exact (B1BKflush _ IH _ _ _ H)
    | H : b1_drain _ _ = Some _ |- _ => exact (B1BKdrain _ IH _ _ _ H)
    | H : (if ?b then _ else _) _ = Some _ |- _ => destruct b
    | H : (if ?b then _ else _) = Some _ |- _ => destruct b
    | H : (match ?x with _ => _ end) _ = Some _ |- _ => destruct x
    | H : (match ?x with _ => _ end) = Some _ |- _ => destruct x
    end).
References
Lemma b1_batch_kept f : b1_batch_kept_at f.
Qed
Proof.
  induction f as [|f IH]; [split; intros; discriminate|].
  split.
  - intros e c s s' r H. cbn [b1_exec] in H.
    destruct c; try solve [bd_solve IH].
    eapply bind_inv in H as (sa1 & [] & E1 & H). unfold m_modify in E1; simplify_eq.
    eapply bind_inv in H as (sa2 & rb & E2 & H).
    apply (B1BKexec _ IH) in E2. simpl in E2.
    eapply bind_inv in H as (sa3 & [] & E3 & H). unfold m_modify in E3; simplify_eq.
    assert (Hbd3 : g_batch_depth (with_batch_depth (pred (g_batch_depth sa2)) sa2) = g_batch_depth s)
      by (simpl; rewrite E2; reflexivity).
    destruct rb;
      [transitivity (g_batch_depth (with_batch_depth (pred (g_batch_depth sa2)) sa2));
       [bd_solve IH|exact Hbd3]
      |unfold m_return in H; simplify_eq; exact Hbd3].
  - intros n b s s' r H. cbn [b1_read] in H. destruct b; bd_solve IH.
  - intros n s s' r H. cbn [b1_read_node] in H. bd_solve IH.
  - intros n s s' r H. cbn [b1_update_if_necessary] in H. bd_solve IH.
  - intros n s s' r H. cbn [b1_refresh_or_pull] in H. bd_solve IH.
  - intros n s s' r H. cbn [b1_pull] in H. bd_solve IH.
  - intros n s s' r H. cbn [b1_refresh] in H. bd_solve IH.
  - intros n i s s' r H. cbn [b1_resolve_walk] in H. bd_solve IH.
  - intros n s s' r H. cbn [b1_recompute] in H. bd_solve IH.
  - intros n s s' r H. cbn [b1_run_memo] in H. bd_solve IH.
  - intros n s s' r H. cbn [b1_execute_effect] in H. bd_solve IH.
  - intros n s s' r H. cbn [b1_run_effect] in H. bd_solve IH.
  - intros n s s' r H. cbn [b1_run_effect_body] in H. bd_solve IH.
  - intros o s s' r H. cbn [b1_discharge] in H. bd_solve IH.
  - intros o s s' r H. cbn [b1_retire] in H. bd_solve IH.
  - intros o s s' r H. cbn [b1_dispose_scope] in H. bd_solve IH.
  - intros o ls cs s s' r H. cbn [b1_teardown] in H. destruct ls, cs; bd_solve IH.
  - intros o b e s s' r H. cbn [b1_run_cleanup] in H. bd_solve IH.
  - intros n s s' r H. cbn [b1_release] in H. bd_solve IH.
  - intros n s s' r H. cbn [b1_dispose_node] in H. bd_solve IH.
  - intros n o s s' r H. cbn [b1_attach] in H. bd_solve IH.
  - intros n v s s' r H. cbn [b1_write_signal] in H. bd_solve IH.
  - intros s s' r H. cbn [b1_request_flush] in H. bd_solve IH.
  - intros s s' r H. cbn [b1_settle_flush] in H. bd_solve IH.
  - intros s s' r H. cbn [b1_run_flush] in H. bd_solve IH.
  - intros s s' r H. cbn [b1_drain] in H. bd_solve IH.
Qed.
References
Definition b2_pull_rel (s1 s2 : state) : Prop :=
  with_pull_depth (g_pull_depth s2) s1 = s2 /\
  ((g_flushing s1 = false /\ g_batch_depth s1 = 0%nat) ->
    (0 < g_pull_depth s1 <-> 0 < g_pull_depth s2)).
References
Lemma b2_pull_rel_refl s : b2_pull_rel s s.
Qed
Proof. split; [unfold with_pull_depth; destruct s; reflexivity|intros _; split; auto]. Qed.
References
Lemma b2_pull_rel_sym s1 s2 : b2_pull_rel s1 s2 -> b2_pull_rel s2 s1.
Qed
Proof.
  intros [Hw Hiff].
  assert (Hfl : g_flushing s2 = g_flushing s1 /\ g_batch_depth s2 = g_batch_depth s1).
  { rewrite <- Hw. split; reflexivity. }
  split.
  - rewrite <- Hw. unfold with_pull_depth; destruct s1; reflexivity.
  - intros [Hfl2 Hbd2]. rewrite (proj1 Hfl) in Hfl2. rewrite (proj2 Hfl) in Hbd2.
    symmetry. exact (Hiff (conj Hfl2 Hbd2)).
Qed.
References
Lemma b2_pull_rel_with_pull_S s1 s2 :
  b2_pull_rel s1 s2 -> b2_pull_rel (with_pull_depth (S (g_pull_depth s1)) s1) (with_pull_depth (S (g_pull_depth s2)) s2).
Qed
Proof.
  intros [Hw _]. split.
  - rewrite <- Hw. unfold with_pull_depth. destruct s1; reflexivity.
  - intros _. split; intros _; simpl; lia.
Qed.
References

Used by

Lemma b2_pull_rel_with_pull_S_right s1 s2 :
  b2_pull_rel s1 s2 ->
  (g_flushing s1 = true \/ 0 < g_batch_depth s1 \/ 0 < g_pull_depth s1) ->
  b2_pull_rel s1 (with_pull_depth (S (g_pull_depth s2)) s2).
Qed
Proof.
  intros [Hw Hiff] Hdef. split.
  - rewrite <- Hw. unfold with_pull_depth. destruct s1; reflexivity.
  - intros [Hfl Hbd]. destruct Hdef as [H|[H|H]]; try congruence; [lia|].
    split; intros _; simpl; [lia|exact H].
Qed.
References
Lemma b2_pull_rel_with_pull_pred s1 s2 :
  b2_pull_rel s1 s2 ->
  ((g_flushing s1 = false /\ g_batch_depth s1 = 0%nat) -> (1 < g_pull_depth s1 <-> 1 < g_pull_depth s2)) ->
  b2_pull_rel (with_pull_depth (pred (g_pull_depth s1)) s1) (with_pull_depth (pred (g_pull_depth s2)) s2).
Qed
Proof.
  intros [Hw _] Hlt. split.
  - rewrite <- Hw. unfold with_pull_depth. destruct s1; reflexivity.
  - intros [Hfl Hbd]. simpl in *. specialize (Hlt (conj Hfl Hbd)). lia.
Qed.
References
Lemma b2_pure_step {A} (m : M A)
    (Hwp : forall p s, m (with_pull_depth p s) = option_map (fun '(s', a) => (with_pull_depth p s', a)) (m s))
    (Hdp : forall s s' a, m s = Some (s', a) -> g_flushing s' = g_flushing s /\ g_batch_depth s' = g_batch_depth s /\ g_pull_depth s' = g_pull_depth s)
    s1 s2 s1' a :
  b2_pull_rel s1 s2 -> m s1 = Some (s1', a) ->
  exists s2', m s2 = Some (s2', a) /\ b2_pull_rel s1' s2'.
Qed
Proof.
  intros [Hw Hiff] Hm.
  exists (with_pull_depth (g_pull_depth s2) s1').
  rewrite <- Hw, Hwp, Hm. simpl.
  destruct (Hdp _ _ _ Hm) as (Hfl & Hbd & Hpd).
  split; [reflexivity|].
  split; [unfold with_pull_depth; destruct s1'; reflexivity|].
  intros [Hfl' Hbd']. rewrite Hfl, Hbd, Hpd in *. exact (Hiff (conj Hfl' Hbd')).
Qed.
References
Lemma b2_pure_modify upd
    (Hwp : forall p s, upd (with_pull_depth p s) = with_pull_depth p (upd s))
    (Hdp : forall s, g_flushing (upd s) = g_flushing s /\ g_batch_depth (upd s) = g_batch_depth s /\ g_pull_depth (upd s) = g_pull_depth s)
    s1 s2 s1' r :
  b2_pull_rel s1 s2 -> m_modify upd s1 = Some (s1', r) ->
  exists s2', m_modify upd s2 = Some (s2', r) /\ b2_pull_rel s1' s2'.
Qed
Proof.
  intros Hrel Hm. unfold m_modify in Hm; simplify_eq.
  eapply (b2_pure_step (m_modify upd)); [intros p s; unfold m_modify; rewrite Hwp; reflexivity|intros ??? H; unfold m_modify in H; simplify_eq; apply Hdp|exact Hrel|unfold m_modify; reflexivity].
Qed.
References
Existing Class b2_pull_rel.
#[local] Hint Extern 0 (b2_pull_rel _ _) => eassumption : typeclass_instances.
Lemma b2p_bind_of {A B} {m1 : M A} {k1 : A -> M B} {m2 : nat -> M A} {k2 : A -> nat -> M B}
    {s1 s2 s1' : state} {b : B}
    (Hb : m_bind m1 k1 s1 = Some (s1', b))
    `{Hrel : b2_pull_rel s1 s2} :
  (forall f0 f' s0 r0, f0 <= f' -> m2 f0 s0 = Some r0 -> m2 f' s0 = Some r0) ->
  (forall a f0 f' s0 r0, f0 <= f' -> k2 a f0 s0 = Some r0 -> k2 a f' s0 = Some r0) ->
  (forall sa0 sb0 sa1 a, b2_pull_rel sa0 sb0 -> m1 sa0 = Some (sa1, a) ->
    exists f2 sb1, m2 f2 sb0 = Some (sb1, a) /\ b2_pull_rel sa1 sb1) ->
  (forall a sa1 sb1, b2_pull_rel sa1 sb1 -> k1 a sa1 = Some (s1', b) ->
    exists f2 s2', k2 a f2 sb1 = Some (s2', b) /\ b2_pull_rel s1' s2') ->
  exists f2 s2', m_bind (m2 f2) (fun a => k2 a f2) s2 = Some (s2', b) /\ b2_pull_rel s1' s2'.
Qed
Proof.
  intros Hm_mono Hk_mono Hm Hk.
  eapply bind_inv in Hb as (sa1 & a & E1 & E2).
  destruct (Hm _ _ _ _ Hrel E1) as (fm & sb1 & Em & Hrel1).
  destruct (Hk _ _ _ Hrel1 E2) as (fk & s2' & Ek & Hrel').
  exists (Nat.max fm fk), s2'. split; [|exact Hrel'].
  unfold m_bind. rewrite (Hm_mono _ _ _ _ (Nat.le_max_l fm fk) Em).
  exact (Hk_mono _ _ _ _ _ (Nat.le_max_r fm fk) Ek).
Qed.
References

Uses

2 more
  • Nat.le_max_r
  • Nat.max

Used by

Lemma b2_bind_pure_l_of {A B} {m : M A} {k : A -> M B} {s1 s2 s1' : state} {b : B}
    (Hrel : b2_pull_rel s1 s2) (Hb : m_bind m k s1 = Some (s1', b))
    (k2 : A -> nat -> M B) :
  (forall sa0 sb0 sa1 a, b2_pull_rel sa0 sb0 -> m sa0 = Some (sa1, a) ->
    exists sb1, m sb0 = Some (sb1, a) /\ b2_pull_rel sa1 sb1) ->
  (forall a sa1 sb1, b2_pull_rel sa1 sb1 -> k a sa1 = Some (s1', b) ->
    exists f2 s2', k2 a f2 sb1 = Some (s2', b) /\ b2_pull_rel s1' s2') ->
  exists f2 s2', m_bind m (fun a => k2 a f2) s2 = Some (s2', b) /\ b2_pull_rel s1' s2'.
Qed
Proof.
  intros Hm Hk.
  unfold m_bind in Hb. destruct (m s1) as [[sa1 a]|] eqn:Em1; [|discriminate].
  destruct (Hm _ _ _ _ Hrel Em1) as (sb1 & Em2 & Hrel1).
  destruct (Hk _ _ _ Hrel1 Hb) as (f2 & s2' & Ek & Hrel').
  exists f2, s2'. split; [|exact Hrel']. unfold m_bind. rewrite Em2. exact Ek.
Qed.
References

Used by

Lemma b2p_hosted {A} n (m1 : M A) (b2 : nat -> M A) s1 s2 s1' a :
  b2_pull_rel s1 s2 ->
  (forall sa sb sa' r, b2_pull_rel sa sb -> m1 sa = Some (sa', r) -> True) ->
  (forall sa sb sa' r, b2_pull_rel sa sb -> m1 sa = Some (sa', r) ->
    exists f2 sb', b2 f2 sb = Some (sb', r) /\ b2_pull_rel sa' sb') ->
  hosted n m1 s1 = Some (s1', a) ->
  exists f2 s2', hosted n (b2 f2) s2 = Some (s2', a) /\ b2_pull_rel s1' s2'.
Qed
Proof.
  intros Hrel _ Hb H. unfold hosted in H.
  eapply bind_inv in H as (sa1 & [] & E1 & H). unfold m_modify in E1; simplify_eq.
  eapply bind_inv in H as (sa2 & a2 & E2 & H). unfold m_bind, m_modify, m_return in H; simplify_eq.
  assert (Hrel1 : b2_pull_rel (with_current_owner None (with_current (Some n) s1))
                              (with_current_owner None (with_current (Some n) s2))).
  { destruct Hrel as [Hw Hiff]. split; [rewrite <- Hw; unfold with_pull_depth; destruct s1; reflexivity|].
    simpl. exact Hiff. }
  destruct (Hb _ _ _ _ Hrel1 E2) as (f2 & sb2 & Eb2 & [Hw2 Hiff2]).
  exists f2, (with_current_owner (g_current_owner s2) (with_current (g_current s2) sb2)).
  split.
  - unfold hosted, m_bind, m_modify, m_return. rewrite Eb2. reflexivity.
  - destruct Hrel as [Hw Hiff]. split.
    + rewrite <- Hw2, <- Hw. unfold with_pull_depth. destruct s1, sa2; reflexivity.
    + simpl. intros [Hfl Hbd].
      assert (Hcur_s2 : g_current s2 = g_current s1 /\ g_current_owner s2 = g_current_owner s1)
        by (rewrite <- Hw; split; reflexivity).
      exact (Hiff2 (conj Hfl Hbd)).
Qed.
References

Used by

Lemma b2p_untracked {A} (m1 : M A) (b2 : nat -> M A) s1 s2 s1' a :
  b2_pull_rel s1 s2 ->
  (forall sa sb sa' r, b2_pull_rel sa sb -> m1 sa = Some (sa', r) ->
    exists f2 sb', b2 f2 sb = Some (sb', r) /\ b2_pull_rel sa' sb') ->
  untracked m1 s1 = Some (s1', a) ->
  exists f2 s2', untracked (b2 f2) s2 = Some (s2', a) /\ b2_pull_rel s1' s2'.
Qed
Proof.
  intros Hrel Hb H. unfold untracked in H.
  eapply bind_inv in H as (sa1 & [] & E1 & H). unfold m_modify in E1; simplify_eq.
  eapply bind_inv in H as (sa2 & a2 & E2 & H). unfold m_bind, m_modify, m_return in H; simplify_eq.
  destruct Hrel as [Hw Hiff].
  assert (Hflag : (match g_current s2 with Some _ => true | None => g_untracked s2 end) =
                  (match g_current s1 with Some _ => true | None => g_untracked s1 end))
    by (rewrite <- Hw; reflexivity).
  assert (Hrel1 : b2_pull_rel (with_untracked (match g_current s1 with Some _ => true | None => g_untracked s1 end) (with_current None s1))
                              (with_untracked (match g_current s2 with Some _ => true | None => g_untracked s2 end) (with_current None s2))).
  { rewrite Hflag. split; [rewrite <- Hw; unfold with_pull_depth; destruct s1; reflexivity|].
    simpl. exact Hiff. }
  destruct (Hb _ _ _ _ Hrel1 E2) as (f2 & sb2 & Eb2 & [Hw2 Hiff2]).
  exists f2, (with_untracked (g_untracked s2) (with_current (g_current s2) sb2)).
  split.
  - unfold untracked, m_bind, m_modify, m_return. rewrite Eb2. reflexivity.
  - split.
    + rewrite <- Hw2, <- Hw. unfold with_pull_depth. destruct s1, sa2; reflexivity.
    + simpl. exact Hiff2.
Qed.
References

Used by

Lemma b2p_detached o (m1 : M unit) (b2 : nat -> M unit) s1 s2 s1' a :
  b2_pull_rel s1 s2 ->
  (forall sa sb sa' r, b2_pull_rel sa sb -> m1 sa = Some (sa', r) ->
    exists f2 sb', b2 f2 sb = Some (sb', r) /\ b2_pull_rel sa' sb') ->
  (forall sa sa' r, m1 sa = Some (sa', r) -> g_batch_depth sa' = g_batch_depth sa) ->
  (forall sa sa' r, m1 sa = Some (sa', r) -> g_pull_depth sa' = g_pull_depth sa) ->
  (forall f sa sa' r, b2 f sa = Some (sa', r) -> g_flushing sa' = g_flushing sa /\ g_pull_depth sa' = g_pull_depth sa) ->
  detached o m1 s1 = Some (s1', a) ->
  exists f2 s2', detached o (b2 f2) s2 = Some (s2', a) /\ b2_pull_rel s1' s2'.
Qed
Proof.
  intros Hrel Hb Hbd1 Hpd1 Hdp2 H. unfold detached in H. destruct a.
  eapply bind_inv in H as (sa1 & [] & E1 & H). unfold m_modify in E1; simplify_eq.
  eapply bind_inv in H as (sa2 & [] & E2 & H). unfold m_modify in H; simplify_eq.
  destruct Hrel as [Hw Hiff].
  assert (Hrel1 : b2_pull_rel (with_batch_depth (S (g_batch_depth s1)) (with_current_owner (Some o) (with_current None s1)))
                              (with_batch_depth (S (g_batch_depth s2)) (with_current_owner (Some o) (with_current None s2)))).
  { split; [rewrite <- Hw; unfold with_pull_depth; destruct s1; reflexivity|simpl; lia]. }
  destruct (Hb _ _ _ _ Hrel1 E2) as (f2 & sb2 & Eb2 & [Hw2 Hiff2]).
  exists f2, (with_batch_depth (pred (g_batch_depth sb2)) (with_current_owner (g_current_owner s2) (with_current (g_current s2) sb2))).
  split.
  - unfold detached, m_bind, m_modify. rewrite Eb2. reflexivity.
  - split.
    + rewrite <- Hw2, <- Hw. unfold with_pull_depth. destruct s1, sa2; reflexivity.
    + destruct (Hdp2 f2 _ _ _ Eb2) as [Hfl2 Hpd2]. simpl in Hfl2, Hpd2.
      assert (Hfl1 : g_flushing sa2 = g_flushing s2) by (rewrite <- Hw2 in Hfl2; exact Hfl2).
      assert (Hfl_s2 : g_flushing s2 = g_flushing s1) by (rewrite <- Hw; reflexivity).
      assert (Hbd_s2 : g_batch_depth s2 = g_batch_depth s1) by (rewrite <- Hw; reflexivity).
      assert (Hbd1' : g_batch_depth sa2 = S (g_batch_depth s1)) by (apply Hbd1 in E2; exact E2).
      assert (Hpd1' : g_pull_depth sa2 = g_pull_depth s1) by (apply Hpd1 in E2; exact E2).
      rewrite <- Hw2. simpl. rewrite Hbd1'. simpl.
      intros [Hfl' Hbd']. rewrite Hfl1, Hfl_s2 in Hfl'.
      rewrite Hpd1', Hpd2. exact (Hiff (conj Hfl' Hbd')).
Qed.
References

Used by

Local Ltac b2_pure_solve :=
  let Hrel := fresh "Hrel" in let H := fresh "H" in
  intros Hrel H; destruct Hrel as [Hw Hiff];
  unfold with_pull_depth in Hw;
  match type of Hw with _ = ?sb => destruct sb end;
  match type of Hw with ?lhs = _ => match lhs with context [State.g_flags ?sa] => destruct sa end end;
  unfold with_pull_depth in *;
  simpl in *; injection Hw; intros; subst;
  repeat match type of H with
  | context [match ?x with _ => _ end] => destruct x eqn:?
  end;
  simplify_eq; try (eexists; split; [reflexivity|split; [reflexivity|simpl; exact Hiff]]).
References
Lemma b2_pure_m_return {A} (a : A) s0 sb0 s1 a1 :
  b2_pull_rel s0 sb0 -> m_return a s0 = Some (s1, a1) ->
  exists sb1, m_return a sb0 = Some (sb1, a1) /\ b2_pull_rel s1 sb1.
Qed
Proof. unfold m_return. b2_pure_solve. Qed.
References
Lemma b2_pure_fail {A} (s0 sb0 s1 : state) (r1 r2 : A) :
  b2_pull_rel s0 sb0 -> Some (s0, r1) = Some (s1, r2) ->
  exists sb1, Some (sb0, r1) = Some (sb1, r2) /\ b2_pull_rel s1 sb1.
Qed
Proof. intros Hrel H. simplify_eq. exists sb0. split; [reflexivity|exact Hrel]. Qed.
References
Lemma b2_pure_get_node n s0 sb0 s1 d :
  b2_pull_rel s0 sb0 -> get_node n s0 = Some (s1, d) ->
  exists sb1, get_node n sb0 = Some (sb1, d) /\ b2_pull_rel s1 sb1.
Qed
Proof. unfold get_node. b2_pure_solve. Qed.
References
Lemma b2_pure_modify_node n u s0 sb0 s1 r :
  b2_pull_rel s0 sb0 -> modify_node n u s0 = Some (s1, r) ->
  exists sb1, modify_node n u sb0 = Some (sb1, r) /\ b2_pull_rel s1 sb1.
Qed
Proof. unfold modify_node, set_node. b2_pure_solve. Qed.
References
Lemma b2_pure_get_owner o s0 sb0 s1 x :
  b2_pull_rel s0 sb0 -> get_owner o s0 = Some (s1, x) ->
  exists sb1, get_owner o sb0 = Some (sb1, x) /\ b2_pull_rel s1 sb1.
Qed
Proof. unfold get_owner. b2_pure_solve. Qed.
References
Lemma b2_pure_edges_opt u s0 sb0 s1 r :
  b2_pull_rel s0 sb0 -> edges_opt u s0 = Some (s1, r) ->
  exists sb1, edges_opt u sb0 = Some (sb1, r) /\ b2_pull_rel s1 sb1.
Qed
Proof. unfold edges_opt. b2_pure_solve. Qed.
References
Lemma b2_pure_current_owner s0 sb0 s1 r :
  b2_pull_rel s0 sb0 -> current_owner s0 = Some (s1, r) ->
  exists sb1, current_owner sb0 = Some (sb1, r) /\ b2_pull_rel s1 sb1.
Qed
Proof.
  unfold current_owner, set_node.
  intros [Hw Hiff] H. unfold with_pull_depth in Hw.
  destruct s0, sb0; simpl in *; injection Hw; intros; subst.
  destruct g_current_owner0, g_current0; simplify_eq;
    try (eexists; split; [reflexivity|split; [reflexivity|simpl; exact Hiff]]).
  destruct (g_nodes0 !! n); [|discriminate].
  destruct (n_scope n0); simplify_eq;
    try (eexists; split; [reflexivity|split; [reflexivity|simpl; exact Hiff]]).
  destruct (n_kind n0), (n_mode n0); simplify_eq;
    (eexists; split; [reflexivity|split; [reflexivity|simpl; exact Hiff]]).
Qed.
References
Lemma b2_pure_note_host_read n o s0 sb0 s1 r :
  b2_pull_rel s0 sb0 -> note_host_read n o s0 = Some (s1, r) ->
  exists sb1, note_host_read n o sb0 = Some (sb1, r) /\ b2_pull_rel s1 sb1.
Qed
Proof.
  unfold note_host_read, emit, m_modify.
  intros [Hw Hiff] H. unfold with_pull_depth in Hw.
  destruct s0, sb0; simpl in *; injection Hw; intros; subst.
  destruct g_current0, g_current_owner0; simplify_eq;
    try (eexists; split; [reflexivity|split; [reflexivity|simpl; exact Hiff]]).
  destruct (decide _); simplify_eq; (eexists; split; [reflexivity|split; [reflexivity|simpl; exact Hiff]]).
Qed.
References
Lemma b2_pure_track n s0 sb0 s1 r :
  b2_pull_rel s0 sb0 -> track n s0 = Some (s1, r) ->
  exists sb1, track n sb0 = Some (sb1, r) /\ b2_pull_rel s1 sb1.
Qed
Proof.
  unfold track, live_current, ghost_read, set_node.
  intros [Hw Hiff] H. unfold with_pull_depth in Hw.
  destruct s0, sb0; simpl in *; injection Hw; intros; subst.
  destruct g_current0; simplify_eq; try (eexists; split; [reflexivity|split; [reflexivity|simpl; exact Hiff]]).
  destruct (g_nodes0 !! n0); simplify_eq; try (eexists; split; [reflexivity|split; [reflexivity|simpl; exact Hiff]]).
  destruct (n_disposed n1); simplify_eq; try (eexists; split; [reflexivity|split; [reflexivity|simpl; exact Hiff]]).
  destruct (sources_add n0 n g_edges0); simplify_eq.
  destruct (g_nodes0 !! n);
    (eexists; split; [reflexivity|split; [reflexivity|simpl; exact Hiff]]).
Qed.
References
Lemma b2_pure_mark_reader f s0 sb0 s1 r :
  b2_pull_rel s0 sb0 -> mark_reader f s0 = Some (s1, r) ->
  exists sb1, mark_reader f sb0 = Some (sb1, r) /\ b2_pull_rel s1 sb1.
Qed
Proof.
  intros [Hw Hiff] H.
  eapply (b2_pure_step (mark_reader f)); [| |exact (conj Hw Hiff)|exact H].
  - intros p s. unfold mark_reader. destruct (g_current s) eqn:Ec; simpl; [rewrite Ec|rewrite Ec; reflexivity].
    destruct (f_reader_mark (g_flags s)); [|reflexivity].
    rewrite mark_check_with_pull. destruct (mark_check f n s); reflexivity.
  - intros s s' a Hm. unfold mark_reader in Hm.
    destruct (g_current s); [|simplify_eq; repeat split].
    destruct (f_reader_mark (g_flags s)); [|simplify_eq; repeat split].
    destruct (mark_check f n s) eqn:Em; [|discriminate]. simplify_eq.
    destruct (marks_same_depths _ _ (mark_check_rtc f n _ _ Em)) as [Hp Hf].
    pose proof (marks_bd _ _ (mark_check_rtc f n _ _ Em)) as Hbd.
    auto.
Qed.
References
Lemma b2_pure_announce n s0 sb0 s1 r :
  b2_pull_rel s0 sb0 -> announce n s0 = Some (s1, r) ->
  exists sb1, announce n sb0 = Some (sb1, r) /\ b2_pull_rel s1 sb1.
Qed
Proof. unfold announce, modify_node, set_node. b2_pure_solve. Qed.
References
Lemma b2_pure_record_error o y s0 sb0 s1 r :
  b2_pull_rel s0 sb0 -> record_error o y s0 = Some (s1, r) ->
  exists sb1, record_error o y sb0 = Some (sb1, r) /\ b2_pull_rel s1 sb1.
Qed
Proof.
  unfold record_error, m_bind, get_owner, put_owner, m_modify.
  intros [Hw Hiff] H. unfold with_pull_depth in Hw.
  destruct s0, sb0; simpl in *; injection Hw; intros; subst.
  destruct (g_owners0 !! o); [|discriminate]. simpl in *.
  destruct (o_sink o0); [|simplify_eq; (eexists; split; [reflexivity|split; [reflexivity|simpl; exact Hiff]])].
  simpl in *. destruct (g_owners0 !! o1); simplify_eq.
  eexists; split; [reflexivity|split; [reflexivity|simpl; exact Hiff]].
Qed.
References
Lemma b2_pure_log_label s0 sb0 s1 r :
  b2_pull_rel s0 sb0 -> log_label s0 = Some (s1, r) ->
  exists sb1, log_label sb0 = Some (sb1, r) /\ b2_pull_rel s1 sb1.
Qed
Proof. unfold log_label. b2_pure_solve. Qed.
References
Lemma b2_pure_next_id s0 sb0 s1 r :
  b2_pull_rel s0 sb0 -> next_id s0 = Some (s1, r) ->
  exists sb1, next_id sb0 = Some (sb1, r) /\ b2_pull_rel s1 sb1.
Qed
Proof. unfold next_id. b2_pure_solve. Qed.
References
Lemma b2_pure_mark_dirty f n s0 sb0 s1 r :
  b2_pull_rel s0 sb0 -> m_step (mark_dirty f n) s0 = Some (s1, r) ->
  exists sb1, m_step (mark_dirty f n) sb0 = Some (sb1, r) /\ b2_pull_rel s1 sb1.
Qed
Proof.
  intros Hrel H.
  eapply (b2_pure_step (m_step (mark_dirty f n))); [| |exact Hrel|exact H].
  - intros p s. unfold m_step. rewrite mark_dirty_with_pull. destruct (mark_dirty f n s); reflexivity.
  - intros s s' a Hm. unfold m_step in Hm. destruct (mark_dirty f n s) eqn:Em; [|discriminate].
    simplify_eq. destruct (marks_same_depths _ _ (mark_dirty_rtc f n _ _ Em)) as [Hp Hf].
    pose proof (marks_bd _ _ (mark_dirty_rtc f n _ _ Em)) as Hbd. auto.
Qed.
References
Lemma b2_pure_notify_dirty f src s0 sb0 s1 r :
  b2_pull_rel s0 sb0 -> m_step (notify_dirty f src) s0 = Some (s1, r) ->
  exists sb1, m_step (notify_dirty f src) sb0 = Some (sb1, r) /\ b2_pull_rel s1 sb1.
Qed
Proof.
  intros Hrel H.
  eapply (b2_pure_step (m_step (notify_dirty f src))); [| |exact Hrel|exact H].
  - intros p s. unfold m_step. rewrite notify_dirty_with_pull. destruct (notify_dirty f src s); reflexivity.
  - intros s s' a Hm. unfold m_step in Hm. destruct (notify_dirty f src s) eqn:Em; [|discriminate].
    simplify_eq. destruct (marks_same_depths _ _ (any_mark_rtc f src None _ _ (or_intror (or_intror (or_intror (or_introl Em)))))) as [Hp Hf].
    pose proof (marks_bd _ _ (any_mark_rtc f src None _ _ (or_intror (or_intror (or_intror (or_introl Em)))))) as Hbd. auto.
Qed.
References
Lemma b2_pure_notify_dirty_except f src cur s0 sb0 s1 r :
  b2_pull_rel s0 sb0 -> m_step (notify_dirty_except f src cur) s0 = Some (s1, r) ->
  exists sb1, m_step (notify_dirty_except f src cur) sb0 = Some (sb1, r) /\ b2_pull_rel s1 sb1.
Qed
Proof.
  intros Hrel H.
  eapply (b2_pure_step (m_step (notify_dirty_except f src cur))); [| |exact Hrel|exact H].
  - intros p s. unfold m_step. rewrite notify_dirty_except_with_pull. destruct (notify_dirty_except f src cur s); reflexivity.
  - intros s s' a Hm. unfold m_step in Hm. destruct (notify_dirty_except f src cur s) eqn:Em; [|discriminate].
    simplify_eq. destruct (marks_same_depths _ _ (any_mark_rtc f src cur _ _ (or_intror (or_intror (or_intror (or_intror Em)))))) as [Hp Hf].
    pose proof (marks_bd _ _ (any_mark_rtc f src cur _ _ (or_intror (or_intror (or_intror (or_intror Em)))))) as Hbd. auto.
Qed.
References
Lemma b2_pure_cmp_throws {A} n u (v : A) s0 sb0 s1 a :
  b2_pull_rel s0 sb0 ->
  (let* _ := modify_node n u in m_return v) s0 = Some (s1, a) ->
  exists sb1, (let* _ := modify_node n u in m_return v) sb0 = Some (sb1, a) /\ b2_pull_rel s1 sb1.
Qed
Proof.
  unfold m_bind, modify_node, set_node, m_return.
  intros [Hw Hiff] H. unfold with_pull_depth in Hw.
  destruct s0, sb0; simpl in *; injection Hw; intros; subst.
  destruct (g_nodes0 !! n); simplify_eq;
    (eexists; split; [reflexivity|split; [reflexivity|simpl; exact Hiff]]).
Qed.
References
Local Ltac b2_upd_tac :=
  unfold with_pull_depth,
    with_flush_owed, with_flushing, with_queue, with_queue_head,
    with_owners, with_edges, with_trace, with_running, with_checking,
    with_registered, with_ghost, with_current, with_current_owner, with_untracked,
    with_skip, with_epoch, with_stamps,
    push_running, pop_running, push_checking, pop_checking, emit, edges, put_owner,
    ghost_read, ghost_run_start, ghost_effect_start, ghost_publish,
    ghost_run_end, ghost_dirty_cleared, ghost_resolved, ghost_registered,
    run_end_unnoticed, slot_holds, add_if, set_node;
  repeat match goal with |- forall s, _ => let s := fresh "s" in intro s; destruct s; simpl end;
  repeat match goal with |- context [match ?m !! ?k with _ => _ end] => destruct (m !! k) end;
  simpl; repeat split; reflexivity.
References
Lemma b2_pure_add_node n d s0 sb0 s1 r :
  b2_pull_rel s0 sb0 -> add_node n d s0 = Some (s1, r) ->
  exists sb1, add_node n d sb0 = Some (sb1, r) /\ b2_pull_rel s1 sb1.
Qed
Proof.
  unfold add_node. intros Hrel H.
  eapply b2_pure_modify;
    [intros p s; unfold ghost_publish, set_node; destruct s; simpl; rewrite !lookup_insert_eq; reflexivity
    |intro s; unfold ghost_publish, set_node; destruct s; simpl; rewrite lookup_insert_eq; repeat split; reflexivity
    |exact Hrel|exact H].
Qed.
References
Local Ltac b2_pure_fwd :=
  intros;
  first
    [ by eapply b2_pure_m_return
    | by eapply b2_pure_fail
    | by eapply b2_pure_cmp_throws
    | by eapply b2_pure_get_node
    | by eapply b2_pure_modify_node
    | by eapply b2_pure_get_owner
    | by eapply b2_pure_edges_opt
    | by eapply b2_pure_current_owner
    | by eapply b2_pure_note_host_read
    | by eapply b2_pure_track
    | by eapply b2_pure_mark_reader
    | by eapply b2_pure_announce
    | by eapply b2_pure_unlink
    | by eapply b2_pure_record_error
    | by eapply b2_pure_log_label
    | by eapply b2_pure_mark_dirty
    | by eapply b2_pure_notify_dirty
    | by eapply b2_pure_notify_dirty_except
    | by eapply b2_pure_next_id
    | by eapply b2_pure_add_node
    | by (eapply b2_pure_modify; [intros p; b2_upd_tac | b2_upd_tac | eassumption | eassumption]) ].
References
Local Ltac b2_ret := intros; eexists 0%nat; b2_pure_fwd.
Local Ltac b2_mono_tac :=
  intros;
  repeat (cbv beta in *; match goal with
  | H0 : m_bind ?m1 ?k1 ?s = Some ?r |- m_bind _ _ ?s = Some ?r =>
    eapply bind_mono; [..|exact H0]; [intros ?? ?|intros ???? ?]
  | H0 : hosted ?n ?b1 ?s = Some ?r |- hosted ?n _ ?s = Some ?r =>
    eapply hosted_mono; [..|exact H0]; intros ???
  | H0 : untracked ?b1 ?s = Some ?r |- untracked _ ?s = Some ?r =>
    eapply untracked_mono; [..|exact H0]; intros ???
  | H0 : detached ?o ?b1 ?s = Some ?r |- detached ?o _ ?s = Some ?r =>
    eapply detached_mono; [..|exact H0]; intros ???
  | H : ?f1 <= ?f2 |- b1_exec ?f2 _ _ _ = Some _ => by eapply (B1Mexec _ (b1_mono_all f1))
  | H : ?f1 <= ?f2 |- b1_read ?f2 _ _ _ = Some _ => by eapply (B1Mread _ (b1_mono_all f1))
  | H : ?f1 <= ?f2 |- b1_read_node ?f2 _ _ = Some _ => by eapply (B1Mread_node _ (b1_mono_all f1))
  | H : ?f1 <= ?f2 |- b1_update_if_necessary ?f2 _ _ = Some _ => by eapply (B1Mupdate _ (b1_mono_all f1))
  | H : ?f1 <= ?f2 |- b1_refresh_or_pull ?f2 _ _ = Some _ => by eapply (B1Mrop _ (b1_mono_all f1))
  | H : ?f1 <= ?f2 |- b1_pull ?f2 _ _ = Some _ => by eapply (B1Mpull _ (b1_mono_all f1))
  | H : ?f1 <= ?f2 |- b1_refresh ?f2 _ _ = Some _ => by eapply (B1Mrefresh _ (b1_mono_all f1))
  | H : ?f1 <= ?f2 |- b1_resolve_walk ?f2 _ _ _ = Some _ => by eapply (B1Mwalk _ (b1_mono_all f1))
  | H : ?f1 <= ?f2 |- b1_recompute ?f2 _ _ = Some _ => by eapply (B1Mrecompute _ (b1_mono_all f1))
  | H : ?f1 <= ?f2 |- b1_run_memo ?f2 _ _ = Some _ => by eapply (B1Mrun_memo _ (b1_mono_all f1))
  | H : ?f1 <= ?f2 |- b1_execute_effect ?f2 _ _ = Some _ => by eapply (B1Mexecute _ (b1_mono_all f1))
  | H : ?f1 <= ?f2 |- b1_run_effect ?f2 _ _ = Some _ => by eapply (B1Mrun_effect _ (b1_mono_all f1))
  | H : ?f1 <= ?f2 |- b1_run_effect_body ?f2 _ _ = Some _ => by eapply (B1Mbody _ (b1_mono_all f1))
  | H : ?f1 <= ?f2 |- b1_discharge ?f2 _ _ = Some _ => by eapply (B1Mdischarge _ (b1_mono_all f1))
  | H : ?f1 <= ?f2 |- b1_retire ?f2 _ _ = Some _ => by eapply (B1Mretire _ (b1_mono_all f1))
  | H : ?f1 <= ?f2 |- b1_dispose_scope ?f2 _ _ = Some _ => by eapply (B1Mdispose_scope _ (b1_mono_all f1))
  | H : ?f1 <= ?f2 |- b1_teardown ?f2 _ _ _ _ = Some _ => by eapply (B1Mteardown _ (b1_mono_all f1))
  | H : ?f1 <= ?f2 |- b1_run_cleanup ?f2 _ _ _ _ = Some _ => by eapply (B1Mcleanup _ (b1_mono_all f1))
  | H : ?f1 <= ?f2 |- b1_release ?f2 _ _ = Some _ => by eapply (B1Mrelease _ (b1_mono_all f1))
  | H : ?f1 <= ?f2 |- b1_dispose_node ?f2 _ _ = Some _ => by eapply (B1Mdispose _ (b1_mono_all f1))
  | H : ?f1 <= ?f2 |- b1_attach ?f2 _ _ _ = Some _ => by eapply (B1Mattach _ (b1_mono_all f1))
  | H : ?f1 <= ?f2 |- b1_write_signal ?f2 _ _ _ = Some _ => by eapply (B1Mwrite _ (b1_mono_all f1))
  | H : ?f1 <= ?f2 |- b1_request_flush ?f2 _ = Some _ => by eapply (B1Mrequest _ (b1_mono_all f1))
  | H : ?f1 <= ?f2 |- b1_settle_flush ?f2 _ = Some _ => by eapply (B1Msettle _ (b1_mono_all f1))
  | H : ?f1 <= ?f2 |- b1_run_flush ?f2 _ = Some _ => by eapply (B1Mflush _ (b1_mono_all f1))
  | H : ?f1 <= ?f2 |- b1_drain ?f2 _ = Some _ => by eapply (B1Mdrain _ (b1_mono_all f1))
  | H : ?f1 <= ?f2 |- m_step (mark_dirty ?f2 _) _ = Some _ => by eapply m_step_mark_dirty_mono
  | H : ?f1 <= ?f2 |- m_step (notify_dirty ?f2 _) _ = Some _ => by eapply m_step_notify_dirty_mono
  | H : ?f1 <= ?f2 |- m_step (notify_dirty_except ?f2 _ _) _ = Some _ => by eapply m_step_notify_dirty_except_mono
  | H : ?f1 <= ?f2 |- mark_reader ?f2 _ = Some _ => by eapply mark_reader_mono
  | |- (if ?b then _ else _) _ = Some _ => destruct b
  | |- (if ?b then _ else _) = Some _ => destruct b
  | |- (match ?x with _ => _ end) _ = Some _ => destruct x
  | |- (match ?x with _ => _ end) = Some _ => destruct x
  | |- _ = Some _ => assumption
  end);
  match goal with
  | |- _ = Some _ => eassumption
  | _ => idtac
  end.
References
Local Ltac b2_wrap_ex_S :=
  match goal with
  | |- @ex nat ?P =>
    cut (exists f' : nat, P (S f'));
    [let f' := fresh "f'" in let H := fresh "H" in intros [f' H]; exists (S f'); exact H | cbv beta]
  end.
References

Uses

  • S
  • nat
  • ex
Lemma b2_pull_rel_fields s1 s2 :
  b2_pull_rel s1 s2 ->
  g_nodes s2 = g_nodes s1 /\
  g_edges s2 = g_edges s1 /\
  g_owners s2 = g_owners s1 /\
  g_flags s2 = g_flags s1 /\
  g_queue s2 = g_queue s1 /\
  g_queue_head s2 = g_queue_head s1 /\
  g_flushing s2 = g_flushing s1 /\
  g_flush_owed s2 = g_flush_owed s1 /\
  g_batch_depth s2 = g_batch_depth s1 /\
  g_current s2 = g_current s1 /\
  g_untracked s2 = g_untracked s1 /\
  g_current_owner s2 = g_current_owner s1 /\
  g_running s2 = g_running s1 /\
  g_checking s2 = g_checking s1.
Qed
Proof.
  intros [Hw _]. unfold with_pull_depth in Hw.
  destruct s1, s2; simpl in *; injection Hw; intros; subst; repeat split; reflexivity.
Qed.
References

Used by

Record b2_sim_at (f : nat) : Prop := {
  B2Sexec : forall e c s1 s2 s1' r, b2_pull_rel s1 s2 -> b1_exec f e c s1 = Some (s1', r) -> exists f2 s2', b1_exec f2 e c s2 = Some (s2', r) /\ b2_pull_rel s1' s2';
  B2Sread : forall n b s1 s2 s1' r, b2_pull_rel s1 s2 -> b1_read f n b s1 = Some (s1', r) -> exists f2 s2', b1_read f2 n b s2 = Some (s2', r) /\ b2_pull_rel s1' s2';
  B2Sread_node : forall n s1 s2 s1' r, b2_pull_rel s1 s2 -> b1_read_node f n s1 = Some (s1', r) -> exists f2 s2', b1_read_node f2 n s2 = Some (s2', r) /\ b2_pull_rel s1' s2';
  B2Supdate : forall n s1 s2 s1' r, b2_pull_rel s1 s2 -> b1_update_if_necessary f n s1 = Some (s1', r) -> exists f2 s2', b1_update_if_necessary f2 n s2 = Some (s2', r) /\ b2_pull_rel s1' s2';
  B2Srop : forall n s1 s2 s1' r, b2_pull_rel s1 s2 -> b1_refresh_or_pull f n s1 = Some (s1', r) -> exists f2 s2', b1_refresh_or_pull f2 n s2 = Some (s2', r) /\ b2_pull_rel s1' s2';
  B2Spull : forall n s1 s2 s1' r, b2_pull_rel s1 s2 -> b1_pull f n s1 = Some (s1', r) -> exists f2 s2', b1_pull f2 n s2 = Some (s2', r) /\ b2_pull_rel s1' s2';
  B2Srefresh : forall n s1 s2 s1' r, b2_pull_rel s1 s2 -> b1_refresh f n s1 = Some (s1', r) -> exists f2 s2', b1_refresh f2 n s2 = Some (s2', r) /\ b2_pull_rel s1' s2';
  B2Swalk : forall n i s1 s2 s1' r, b2_pull_rel s1 s2 -> b1_resolve_walk f n i s1 = Some (s1', r) -> exists f2 s2', b1_resolve_walk f2 n i s2 = Some (s2', r) /\ b2_pull_rel s1' s2';
  B2Srecompute : forall n s1 s2 s1' r, b2_pull_rel s1 s2 -> b1_recompute f n s1 = Some (s1', r) -> exists f2 s2', b1_recompute f2 n s2 = Some (s2', r) /\ b2_pull_rel s1' s2';
  B2Srun_memo : forall n s1 s2 s1' r, b2_pull_rel s1 s2 -> b1_run_memo f n s1 = Some (s1', r) -> exists f2 s2', b1_run_memo f2 n s2 = Some (s2', r) /\ b2_pull_rel s1' s2';
  B2Sexecute : forall n s1 s2 s1' r, b2_pull_rel s1 s2 -> b1_execute_effect f n s1 = Some (s1', r) -> exists f2 s2', b1_execute_effect f2 n s2 = Some (s2', r) /\ b2_pull_rel s1' s2';
  B2Srun_effect : forall n s1 s2 s1' r, b2_pull_rel s1 s2 -> b1_run_effect f n s1 = Some (s1', r) -> exists f2 s2', b1_run_effect f2 n s2 = Some (s2', r) /\ b2_pull_rel s1' s2';
  B2Sbody : forall n s1 s2 s1' r, b2_pull_rel s1 s2 -> b1_run_effect_body f n s1 = Some (s1', r) -> exists f2 s2', b1_run_effect_body f2 n s2 = Some (s2', r) /\ b2_pull_rel s1' s2';
  B2Sdischarge : forall o s1 s2 s1' r, b2_pull_rel s1 s2 -> b1_discharge f o s1 = Some (s1', r) -> exists f2 s2', b1_discharge f2 o s2 = Some (s2', r) /\ b2_pull_rel s1' s2';
  B2Sretire : forall o s1 s2 s1' r, b2_pull_rel s1 s2 -> b1_retire f o s1 = Some (s1', r) -> exists f2 s2', b1_retire f2 o s2 = Some (s2', r) /\ b2_pull_rel s1' s2';
  B2Sdispose_scope : forall o s1 s2 s1' r, b2_pull_rel s1 s2 -> b1_dispose_scope f o s1 = Some (s1', r) -> exists f2 s2', b1_dispose_scope f2 o s2 = Some (s2', r) /\ b2_pull_rel s1' s2';
  B2Steardown : forall o ls cs s1 s2 s1' r, b2_pull_rel s1 s2 -> b1_teardown f o ls cs s1 = Some (s1', r) -> exists f2 s2', b1_teardown f2 o ls cs s2 = Some (s2', r) /\ b2_pull_rel s1' s2';
  B2Scleanup : forall o b e s1 s2 s1' r, b2_pull_rel s1 s2 -> b1_run_cleanup f o b e s1 = Some (s1', r) -> exists f2 s2', b1_run_cleanup f2 o b e s2 = Some (s2', r) /\ b2_pull_rel s1' s2';
  B2Srelease : forall n s1 s2 s1' r, b2_pull_rel s1 s2 -> b1_release f n s1 = Some (s1', r) -> exists f2 s2', b1_release f2 n s2 = Some (s2', r) /\ b2_pull_rel s1' s2';
  B2Sdispose : forall n s1 s2 s1' r, b2_pull_rel s1 s2 -> b1_dispose_node f n s1 = Some (s1', r) -> exists f2 s2', b1_dispose_node f2 n s2 = Some (s2', r) /\ b2_pull_rel s1' s2';
  B2Sattach : forall n o s1 s2 s1' r, b2_pull_rel s1 s2 -> b1_attach f n o s1 = Some (s1', r) -> exists f2 s2', b1_attach f2 n o s2 = Some (s2', r) /\ b2_pull_rel s1' s2';
  B2Swrite : forall n v s1 s2 s1' r, b2_pull_rel s1 s2 -> b1_write_signal f n v s1 = Some (s1', r) -> exists f2 s2', b1_write_signal f2 n v s2 = Some (s2', r) /\ b2_pull_rel s1' s2';
  B2Srequest : forall s1 s2 s1' r, b2_pull_rel s1 s2 -> b1_request_flush f s1 = Some (s1', r) -> exists f2 s2', b1_request_flush f2 s2 = Some (s2', r) /\ b2_pull_rel s1' s2';
  B2Ssettle : forall s1 s2 s1' r, b2_pull_rel s1 s2 -> b1_settle_flush f s1 = Some (s1', r) -> exists f2 s2', b1_settle_flush f2 s2 = Some (s2', r) /\ b2_pull_rel s1' s2';
  B2Sflush : forall s1 s2 s1' r, b2_pull_rel s1 s2 -> b1_run_flush f s1 = Some (s1', r) -> exists f2 s2', b1_run_flush f2 s2 = Some (s2', r) /\ b2_pull_rel s1' s2';
  B2Sdrain : forall s1 s2 s1' r, b2_pull_rel s1 s2 -> b1_drain f s1 = Some (s1', r) -> exists f2 s2', b1_drain f2 s2 = Some (s2', r) /\ b2_pull_rel s1' s2'
}.
References
Lemma b2_sim_all f : b2_sim_at f.
Qed
Proof.
  induction f as [|f IH]; [split; intros; discriminate|].
  split.
  - intros e c s1 s2 s1' r Hrel H. b2_wrap_ex_S. cbn [b1_exec] in H |- *.
    destruct (b2_pull_rel_fields _ _ Hrel) as (_&_&_&_&_&_&Hfl&_&Hbd&_).
    destruct c.
    + exists 0%nat. unfold on_value in H |- *.
      destruct (eval_expr e (env_lookup e prev_var) e0) as [[|]|]; [| |discriminate]; b2_pure_fwd.
    + b2_ret.
    + unfold on_value in H |- *. destruct (eval_expr e (env_lookup e prev_var) e0) as [[|]|];
        [by eapply (B2Sexec _ IH)|b2_ret|discriminate].
    + unfold on_value in H |- *. destruct (eval_expr e (env_lookup e prev_var) e0) as [[|]|];
        [destruct (decide _); by eapply (B2Sexec _ IH)|b2_ret|discriminate].
    + unfold on_node, on_value in H |- *.
      destruct (eval_expr e (env_lookup e prev_var) n) as [[|]|]; try discriminate; [destruct (decide _); [|discriminate]|b2_ret].
      eapply (b2p_bind_of H); [intros ????; b2_mono_tac|intros ?????; b2_mono_tac|intros ????; by eapply (B2Sread _ IH)|intros o ?? Hrel1 Hk].
      eapply (b2_bind_pure_l_of Hrel1 Hk); [b2_pure_fwd|intros [] ?? Hrel2 Hk2].
      destruct o; [by eapply (B2Sexec _ IH)|b2_ret].
    + unfold on_node, on_value in H |- *.
      destruct (eval_expr e (env_lookup e prev_var) n) as [[|]|]; try discriminate; [destruct (decide _); [|discriminate]|b2_ret].
      eapply (b2p_bind_of H); [intros ????; b2_mono_tac|intros ?????; b2_mono_tac|intros ????; by eapply (B2Sread _ IH)|intros o ?? Hrel1 Hk].
      eapply (b2_bind_pure_l_of Hrel1 Hk); [b2_pure_fwd|intros [] ?? Hrel2 Hk2].
      destruct o; by eapply (B2Sexec _ IH).
    + unfold on_node, on_value in H |- *.
      destruct (eval_expr e (env_lookup e prev_var) n) as [[|]|]; try discriminate; [destruct (decide _); [|discriminate]|b2_ret].
      eapply (b2p_bind_of H); [intros ????; b2_mono_tac|intros ?????; b2_mono_tac|intros ????; by eapply (B2Sread _ IH)|intros o ?? Hrel1 Hk].
      eapply (b2_bind_pure_l_of Hrel1 Hk); [b2_pure_fwd|intros [] ?? Hrel2 Hk2].
      destruct o; [by eapply (B2Sexec _ IH)|b2_ret].
    + unfold on_node, on_value in H |- *.
      destruct (eval_expr e (env_lookup e prev_var) n) as [[|]|]; try discriminate; [destruct (decide _); [|discriminate]|b2_ret].
      destruct (eval_expr e (env_lookup e prev_var) e0) as [[|]|]; try discriminate; [|b2_ret].
      eapply (b2p_bind_of H); [intros ????; b2_mono_tac|intros ?????; b2_mono_tac|intros ????; by eapply (B2Swrite _ IH)|intros [] ?? Hrel1 Hk].
      by eapply (B2Sexec _ IH).
    + unfold on_value in H |- *. destruct (eval_expr e (env_lookup e prev_var) e0) as [[|]|]; try discriminate; [|b2_ret].
      eapply (b2_bind_pure_l_of Hrel H); [b2_pure_fwd|intros n ?? Hrel1 Hk].
      eapply (b2_bind_pure_l_of Hrel1 Hk); [b2_pure_fwd|intros [] ?? Hrel2 Hk2].
      by eapply (B2Sexec _ IH).
    + eapply (b2_bind_pure_l_of Hrel H); [b2_pure_fwd|intros n ?? Hrel1 Hk].
      eapply (b2_bind_pure_l_of Hrel1 Hk); [b2_pure_fwd|intros [] ?? Hrel2 Hk2].
      eapply (b2_bind_pure_l_of Hrel2 Hk2); [b2_pure_fwd|intros own ?? Hrel3 Hk3].
      destruct own; [|b2_ret].
      eapply (b2p_bind_of Hk3); [intros ????; b2_mono_tac|intros ?????; b2_mono_tac|intros ????; by eapply (B2Sattach _ IH)|intros [] ?? Hrel4 Hk4].
      by eapply (B2Sexec _ IH).
    + eapply (b2_bind_pure_l_of Hrel H); [b2_pure_fwd|intros n ?? Hrel1 Hk].
      eapply (b2_bind_pure_l_of Hrel1 Hk); [b2_pure_fwd|intros [] ?? Hrel2 Hk2].
      eapply (b2_bind_pure_l_of Hrel2 Hk2); [b2_pure_fwd|intros own ?? Hrel3 Hk3].
      destruct own; [|b2_ret].
      eapply (b2p_bind_of Hk3); [intros ????; b2_mono_tac|intros ?????; b2_mono_tac|intros ????; by eapply (B2Sattach _ IH)|intros [] ?? Hrel4 Hk4].
      eapply (b2p_bind_of Hk4); [intros ????; b2_mono_tac|intros ?????; b2_mono_tac|intros ????; exists f; b2_pure_fwd|intros [] ?? Hrel5 Hk5].
      eapply (b2p_bind_of Hk5); [intros ????; b2_mono_tac|intros ?????; b2_mono_tac|intros ????; by eapply (B2Srequest _ IH)|intros [] ?? Hrel6 Hk6].
      by eapply (B2Sexec _ IH).
    + eapply (b2_bind_pure_l_of Hrel H); [b2_pure_fwd|intros own ?? Hrel1 Hk].
      destruct own; [|b2_ret].
      eapply (b2_bind_pure_l_of Hrel1 Hk); [b2_pure_fwd|intros x ?? Hrel2 Hk2].
      eapply (b2_bind_pure_l_of Hrel2 Hk2); [b2_pure_fwd|intros [] ?? Hrel3 Hk3].
      eapply (b2p_bind_of Hk3); [intros ????; b2_mono_tac|intros ?????; b2_mono_tac| |intros [] ?? Hrel4 Hk4; by eapply (B2Sexec _ IH)].
      intros sa sb sa' r_ Hrel_a Hm. destruct (o_disposed x).
      * eapply (b2p_bind_of Hm); [intros ????; b2_mono_tac|intros ?????; b2_mono_tac| |intros [] ?? Hrel_b Hkb; by eapply (B2Srequest _ IH)].
        intros sa_d sb_d sa_d' r0_ Hrel_d Hm_d. eapply b2p_detached; [exact Hrel_d| | | | |exact Hm_d].
        -- intros ????; by eapply (B2Scleanup _ IH).
        -- intros ??? E; apply (B1BKcleanup f (b1_batch_kept f)) in E; exact E.
        -- intros ??? E; apply (B1Dcleanup f (b1_depths_kept f)) in E; exact (proj1 E).
        -- intros f0 ??? E; apply (B1Dcleanup f0 (b1_depths_kept f0)) in E as [Hpd Hfl_]; exact (conj Hfl_ Hpd).
      * exists 0%nat; b2_pure_fwd.
    + unfold on_node, on_value in H |- *.
      destruct (eval_expr e (env_lookup e prev_var) n) as [[|]|]; try discriminate; [destruct (decide _); [|discriminate]|b2_ret].
      eapply (b2_bind_pure_l_of Hrel H); [b2_pure_fwd|intros d ?? Hrel1 Hk].
      destruct (n_kind d); [discriminate| |];
      (eapply (b2p_bind_of Hk); [intros ????; b2_mono_tac|intros ?????; b2_mono_tac|intros ????; by eapply (B2Sdispose _ IH)|intros [] ?? Hrel2 Hk2];
       eapply (b2_bind_pure_l_of Hrel2 Hk2); [destruct (n_disposed d); b2_pure_fwd|intros [] ?? Hrel3 Hk3];
       by eapply (B2Sexec _ IH)).
    + eapply bind_inv in H as (sa1 & [] & E1 & H). unfold m_modify in E1; simplify_eq.
      eapply bind_inv in H as (sa2 & rb & E2 & H).
      eapply bind_inv in H as (sa3 & [] & E3 & H). unfold m_modify in E3; simplify_eq.
      assert (Hrel1 : b2_pull_rel (with_batch_depth (S (g_batch_depth s1)) s1) (with_batch_depth (S (g_batch_depth s2)) s2)).
      { destruct Hrel as [Hw _]. split; [rewrite <- Hw; unfold with_pull_depth; destruct s1; reflexivity|simpl; lia]. }
      destruct (B2Sexec _ IH _ _ _ _ _ _ Hrel1 E2) as (f2 & sb2 & Eb2 & [Hw2 _]).
      assert (Hbd_sa2 : g_batch_depth sa2 = S (g_batch_depth s1)) by (apply (B1BKexec _ (b1_batch_kept f)) in E2; exact E2).
      destruct (B1Dexec _ (b1_depths_kept f) _ _ _ _ _ E2) as [Hpd_sa2 Hfl_sa2]. simpl in Hpd_sa2, Hfl_sa2.
      destruct (B1Dexec _ (b1_depths_kept f2) _ _ _ _ _ Eb2) as [Hpd_sb2 _]. simpl in Hpd_sb2.
      assert (Hrel3 : b2_pull_rel (with_batch_depth (pred (g_batch_depth sa2)) sa2) (with_batch_depth (pred (g_batch_depth sb2)) sb2)).
      { split; [rewrite <- Hw2; unfold with_pull_depth; destruct sa2; reflexivity|].
        simpl. rewrite Hbd_sa2, Hpd_sa2, Hpd_sb2, Hfl_sa2. simpl.
        destruct Hrel as [_ Hiff]. exact Hiff. }
      destruct rb as [|].
      * assert (Hk : (let* _ := b1_request_flush f in b1_exec f e c2) (with_batch_depth (pred (g_batch_depth sa2)) sa2) = Some (s1', r)) by exact H.
        assert (Htmp : exists f3 s2',
          (let* _ := b1_request_flush f3 in b1_exec f3 e c2) (with_batch_depth (pred (g_batch_depth sb2)) sb2) = Some (s2', r) /\
          b2_pull_rel s1' s2').
        { eapply (b2p_bind_of Hk); [intros ????; b2_mono_tac|intros ?????; b2_mono_tac|intros ????; by eapply (B2Srequest _ IH)|intros [] ?? Hrel4 Hk4; by eapply (B2Sexec _ IH)]. }
        destruct Htmp as (f3 & s2' & Ek3 & Hrel').
        exists (Nat.max f2 f3), s2'. split; [|exact Hrel'].
        unfold m_bind, m_modify. rewrite (B1Mexec _ (b1_mono_all f2) (Nat.max f2 f3) _ _ _ _ (Nat.le_max_l f2 f3) Eb2).
        eapply bind_inv in Ek3 as (sb4 & [] & Eq1 & Eq2).
        rewrite (B1Mrequest _ (b1_mono_all f3) (Nat.max f2 f3) _ _ (Nat.le_max_r f2 f3) Eq1).
        exact (B1Mexec _ (b1_mono_all f3) (Nat.max f2 f3) _ _ _ _ (Nat.le_max_r f2 f3) Eq2).
      * unfold m_return in H; simplify_eq.
        exists f2, (with_batch_depth (pred (g_batch_depth sb2)) sb2). split; [|exact Hrel3].
        unfold m_bind, m_modify. rewrite Eb2. reflexivity.
    + unfold on_value in H |- *. destruct (eval_expr e (env_lookup e prev_var) e0) as [[|]|]; try discriminate; [|b2_ret].
      eapply (b2_bind_pure_l_of Hrel H); [b2_pure_fwd|intros l ?? Hrel1 Hk].
      eapply (b2_bind_pure_l_of Hrel1 Hk); [b2_pure_fwd|intros [] ?? Hrel2 Hk2].
      by eapply (B2Sexec _ IH).
  - intros n b s1 s2 s1' r Hrel H. b2_wrap_ex_S. cbn [b1_read] in H |- *.
    destruct b; [by eapply (B2Sread_node _ IH) | eapply b2p_untracked; [exact Hrel|intros ????; by eapply (B2Sread_node _ IH)|exact H]].
  - (* B2Sread_node *)
    intros n s1 s2 s1' r Hrel H. b2_wrap_ex_S. cbn [b1_read_node] in H |- *.
    eapply (b2_bind_pure_l_of Hrel H); [b2_pure_fwd|intros d ?? Hrel1 Hk].
    destruct (n_kind d); [| |discriminate].
    + eapply (b2_bind_pure_l_of Hrel1 Hk); [b2_pure_fwd|intros [] ?? Hrel2 Hk2]. b2_ret.
    + eapply (b2p_bind_of Hk); [intros ????; b2_mono_tac|intros ?????; b2_mono_tac| |intros d' ?? Hrel2 Hk2; destruct (n_status d'); b2_ret].
      intros sa sb sa' r_ Hrel_a Hm. destruct (stale_live d).
      * eapply (b2p_bind_of Hm); [intros ????; b2_mono_tac|intros ?????; b2_mono_tac|intros ????; by eapply (B2Srop _ IH)|intros d'0 ?? Hrel2a Hk2a].
        eapply (b2p_bind_of Hk2a); [intros ????; b2_mono_tac|intros ?????; b2_mono_tac| |intros [] ?? Hrel3a Hk3a; b2_ret].
        intros sa2 sb2 sa2' r2_ Hrel2a' Hm2. eapply (b2_bind_pure_l_of Hrel2a' Hm2); [b2_pure_fwd|intros [] ?? Hrel3b Hk3b].
        eapply (b2_bind_pure_l_of Hrel3b Hk3b); [b2_pure_fwd|intros d'' ?? Hrel4b Hk4b].
        destruct (stale_live d''); [exists f; by b2_pure_fwd|b2_ret].
      * eapply (b2_bind_pure_l_of Hrel_a Hm); [b2_pure_fwd|intros [] ?? Hrel2b Hk2b].
        exists 0%nat. by b2_pure_fwd.
  - (* B2Supdate *)
    intros n s1 s2 s1' r Hrel H. b2_wrap_ex_S. cbn [b1_update_if_necessary] in H |- *.
    eapply (b2_bind_pure_l_of Hrel H); [b2_pure_fwd|intros d ?? Hrel1 Hk].
    destruct (n_kind d); [b2_ret| |b2_ret].
    destruct (stale_live d); [|b2_ret].
    eapply (b2p_bind_of Hk); [intros ????; b2_mono_tac|intros ?????; b2_mono_tac|intros ????; by eapply (B2Srop _ IH)|intros ??? Hrel2 Hk2].
    b2_ret.
  - (* B2Srop *)
    intros n s1 s2 s1' r Hrel H. b2_wrap_ex_S. cbn [b1_refresh_or_pull] in H |- *.
    rewrite bind_get in H. setoid_rewrite bind_get.
    destruct (b2_pull_rel_fields _ _ Hrel) as (_&_&_&_&_&_&Hfl&_&Hbd&Hcur&_).
    unfold deferring_b1, deferring in *. rewrite Hfl, Hcur in *.
    destruct (g_flushing s1 || match g_current s1 with Some _ => true | None => false end) eqn:Hearly.
    + (* Both take b1_refresh; get_node *)
      destruct (g_flushing s1) eqn:Efl; [|destruct (g_current s1) eqn:Ec; [|discriminate]]; simpl in H |- *.
      * eapply (b2p_bind_of H); [intros ????; b2_mono_tac|intros ?????; b2_mono_tac|intros ????; by eapply (B2Srefresh _ IH)|intros [] ?? Hrel1 Hk].
        exists 0%nat; by b2_pure_fwd.
      * eapply (b2p_bind_of H); [intros ????; b2_mono_tac|intros ?????; b2_mono_tac|intros ????; by eapply (B2Srefresh _ IH)|intros [] ?? Hrel1 Hk].
        exists 0%nat; by b2_pure_fwd.
    + destruct (g_flushing s1) eqn:Efl; [discriminate|].
      destruct (g_current s1) eqn:Ec; [discriminate|].
      simpl in H |- *.
      destruct (bool_decide (0 < g_pull_depth s1)) eqn:Ep1, (bool_decide (0 < g_pull_depth s2)) eqn:Ep2.
      * eapply (b2p_bind_of H); [intros ????; b2_mono_tac|intros ?????; b2_mono_tac|intros ????; by eapply (B2Srefresh _ IH)|intros [] ?? Hrel1 Hk].
        exists 0%nat; by b2_pure_fwd.
      * (* Ep1 = true, Ep2 = false -> 0 < g_batch_depth s1 = g_batch_depth s2 *)
        assert (Hbd_pos : 0 < g_batch_depth s1).
        { destruct (Nat.eq_dec (g_batch_depth s1) 0) as [H0|]; [|lia].
          destruct Hrel as [_ Hiff]. specialize (Hiff (conj Efl H0)).
          rewrite bool_decide_eq_true in Ep1. rewrite bool_decide_eq_false in Ep2. lia. }
        eapply bind_inv in H as (sa1 & [] & E1 & Eg). unfold get_node in Eg.
        destruct (g_nodes sa1 !! n) as [dn|] eqn:En; [|discriminate]. simplify_eq.
        set (s2_inc := with_pull_depth (S (g_pull_depth s2)) s2).
        assert (Hrel_inc : b2_pull_rel s1 s2_inc).
        { apply b2_pull_rel_with_pull_S_right; [exact Hrel|right; left; exact Hbd_pos]. }
        destruct (B2Srefresh _ IH n _ _ _ _ Hrel_inc E1) as (f2 & sb1 & Eb1 & Hrel1).
        pose proof (B1BKrefresh _ (b1_batch_kept f) _ _ _ _ E1) as Hbd_sa1.
        pose proof (B1Drefresh _ (b1_depths_kept f) _ _ _ _ E1) as [Hpd_sa1 Hfl_sa1].
        pose proof (B1BKrefresh _ (b1_batch_kept f2) _ _ _ _ Eb1) as Hbd_sb1.
        pose proof (B1Drefresh _ (b1_depths_kept f2) _ _ _ _ Eb1) as [Hpd_sb1 Hfl_sb1].
        assert (Hf2' := B1Mrefresh _ (b1_mono_all f2) (Nat.max f2 1) n s2_inc (sb1, tt) (Nat.le_max_l f2 1) Eb1).
        set (sb2 := with_pull_depth (pred (g_pull_depth sb1)) sb1).
        exists (S (Nat.max f2 1)), sb2.
        split.
        -- cbn [b1_pull]. unfold m_bind, m_modify, get_node.
           fold s2_inc. rewrite Hf2'. simpl.
           destruct (b2_pull_rel_fields _ _ Hrel1) as (Hns & _). rewrite Hns, En. simpl.
           fold sb2.
           destruct (_ && _); [|reflexivity].
           replace (Nat.max f2 1) with (S (pred (Nat.max f2 1))) by lia.
           cbn [b1_settle_flush]. rewrite bind_get.
           assert (Hbd_sb2 : 0 < g_batch_depth sb2).
           { unfold sb2. simpl. rewrite Hbd_sb1. simpl. rewrite Hbd. exact Hbd_pos. }
           rewrite (bool_decide_eq_false_2 (g_batch_depth sb2 = 0%nat)) by lia.
           rewrite andb_false_r, andb_false_l. reflexivity.
        -- destruct Hrel1 as [Hw1 _]. split.
           ++ unfold sb2, with_pull_depth in *. rewrite <- Hw1. destruct s1'; reflexivity.
           ++ intros [Hfl1 Hbd1]. rewrite Hbd_sa1 in Hbd1. lia.
      * (* Ep1 = false, Ep2 = true -> 0 < g_batch_depth s1 = g_batch_depth s2 *)
        assert (Hbd_pos : 0 < g_batch_depth s1).
        { destruct (Nat.eq_dec (g_batch_depth s1) 0) as [H0|]; [|lia].
          destruct Hrel as [_ Hiff]. specialize (Hiff (conj Efl H0)).
          rewrite bool_decide_eq_false in Ep1. rewrite bool_decide_eq_true in Ep2. lia. }
        destruct f as [|f']; [discriminate|].
        cbn [b1_pull] in H.
        eapply bind_inv in H as (sa0 & [] & E0 & H). unfold m_modify in E0; simplify_eq.
        eapply bind_inv in H as (sa1 & [] & E1 & H).
        eapply bind_inv in H as (sa2 & d & E2 & H). unfold get_node in E2.
        destruct (g_nodes sa1 !! n) eqn:En; [|discriminate]. simplify_eq. rename sa2 into sa1.
        eapply bind_inv in H as (sa3 & [] & E3 & H). unfold m_modify in E3; simplify_eq.
        rewrite bind_get in H.
        eapply bind_inv in H as (sa4 & [] & E4 & H). unfold m_return in H; simplify_eq.
        pose proof (B1BKrefresh _ (b1_batch_kept f') _ _ _ _ E1) as Hbd_sa1. simpl in Hbd_sa1.
        assert (Hsa4 : s1' = with_pull_depth (pred (g_pull_depth sa1)) sa1).
        { destruct (_ && _); [|unfold m_return in E4; by simplify_eq].
          destruct f'; [discriminate|]. cbn [b1_settle_flush] in E4. rewrite bind_get in E4.
          rewrite (bool_decide_eq_false_2 (g_batch_depth (with_pull_depth (pred (g_pull_depth sa1)) sa1) = 0%nat)) in E4 by (simpl; lia).
          rewrite andb_false_r, andb_false_l in E4. unfold m_return in E4; by simplify_eq. }
        subst s1'.
        assert (Hrel_inc : b2_pull_rel (with_pull_depth (S (g_pull_depth s1)) s1) s2).
        { apply b2_pull_rel_sym. apply b2_pull_rel_with_pull_S_right; [by apply b2_pull_rel_sym|].
          rewrite <- Hfl, <- Hbd in *. right; left; exact Hbd_pos. }
        pose proof (B1Mrefresh _ (b1_mono_all f') (S f') n _ _ (ltac:(lia)) E1) as E1'.
        destruct (B2Srefresh _ IH n _ _ _ _ Hrel_inc E1') as (f2 & sb1 & Eb1 & Hrel1).
        exists f2, sb1. split.
        -- unfold m_bind. rewrite Eb1. simpl. unfold get_node.
           destruct (b2_pull_rel_fields _ _ Hrel1) as (Hns & _). rewrite Hns, En. reflexivity.
        -- destruct Hrel1 as [Hw1 _]. split; [rewrite <- Hw1; unfold with_pull_depth; destruct sa1; reflexivity|].
           simpl. intros [_ Hbd1]. rewrite Hbd_sa1 in Hbd1. lia.
      * by eapply (B2Spull _ IH).
  - (* B2Spull *)
    intros n s1 s2 s1' r Hrel H. b2_wrap_ex_S. cbn [b1_pull] in H |- *.
    eapply bind_inv in H as (sa0 & [] & E0 & H). unfold m_modify in E0; simplify_eq.
    eapply bind_inv in H as (sa1 & [] & E1 & H).
    eapply bind_inv in H as (sa2 & d & E2 & H). unfold get_node in E2.
    destruct (g_nodes sa1 !! n) eqn:En; [|discriminate]. simplify_eq. rename sa2 into sa1.
    eapply bind_inv in H as (sa3 & [] & E3 & H). unfold m_modify in E3; simplify_eq.
    rewrite bind_get in H.
    eapply bind_inv in H as (sa4 & [] & E4 & H). unfold m_return in H; simplify_eq.
    assert (Hrel_inc : b2_pull_rel (with_pull_depth (S (g_pull_depth s1)) s1) (with_pull_depth (S (g_pull_depth s2)) s2))
      by (by apply b2_pull_rel_with_pull_S).
    destruct (B2Srefresh _ IH n _ _ _ _ Hrel_inc E1) as (f2 & sb1 & Eb1 & Hrel1).
    pose proof (B1Drefresh _ (b1_depths_kept f) _ _ _ _ E1) as [Hpd1 Hfl1]. simpl in Hpd1, Hfl1.
    pose proof (B1BKrefresh _ (b1_batch_kept f) _ _ _ _ E1) as Hbd1. simpl in Hbd1.
    pose proof (B1Drefresh _ (b1_depths_kept f2) _ _ _ _ Eb1) as [Hpd2 Hfl2]. simpl in Hpd2, Hfl2.
    pose proof (B1BKrefresh _ (b1_batch_kept f2) _ _ _ _ Eb1) as Hbd2. simpl in Hbd2.
    set (sa_dec := with_pull_depth (pred (g_pull_depth sa1)) sa1).
    set (sb_dec := with_pull_depth (pred (g_pull_depth sb1)) sb1).
    assert (Hrel_dec : b2_pull_rel sa_dec sb_dec).
    { destruct Hrel1 as [Hw1 _]. split; [unfold sa_dec, sb_dec, with_pull_depth in *; rewrite <- Hw1; destruct sa1; reflexivity|].
      unfold sa_dec, sb_dec. simpl. rewrite Hpd1, Hpd2, Hfl1, Hbd1.
      simpl. destruct Hrel as [_ Hiff]. exact Hiff. }
    destruct (b2_pull_rel_fields _ _ Hrel_dec) as (_&_&_&_&_&_&Hfl_dec&Hfo&Hbd_dec&_).
    destruct (b2_pull_rel_fields _ _ Hrel1) as (Hns1&_).
    destruct (g_flushing sa_dec) eqn:Efl; [|destruct (Nat.eq_dec (g_batch_depth sa_dec) 0) as [Hbd0|Hbdpos]].
    + (* Flushing: settle_flush is no-op on both sides *)
      assert (Hsa4 : s1' = sa_dec).
      { fold sa_dec in E4. destruct (bool_decide (g_pull_depth sa_dec = 0) && g_flush_owed sa_dec); [|unfold m_return in E4; by simplify_eq].
        destruct f; [discriminate|]. cbn [b1_settle_flush] in E4. rewrite bind_get in E4.
        destruct (g_flush_owed sa_dec && bool_decide (g_batch_depth sa_dec = 0) && outside_bodies sa_dec);
          [|unfold m_return in E4; by simplify_eq].
        destruct f; [discriminate|]. cbn [b1_run_flush] in E4. rewrite bind_get, Efl in E4.
        unfold m_return in E4; by simplify_eq. }
      subst s1'.
      exists (Nat.max f2 2), sb_dec.
      assert (Ef2m := B1Mrefresh _ (b1_mono_all f2) (Nat.max f2 2) n _ _ (Nat.le_max_l f2 2) Eb1).
      split; [|exact Hrel_dec].
      unfold m_bind, m_modify, get_node. rewrite Ef2m. simpl. rewrite Hns1, En. simpl.
      fold sb_dec.
      match goal with |- context[if ?c then _ else _] => destruct c end; [|reflexivity].
      replace (Nat.max f2 2) with (S (S (Nat.max f2 2 - 2))) by lia.
      cbn [b1_settle_flush b1_run_flush]. rewrite bind_get.
      destruct (g_flush_owed sb_dec && bool_decide (g_batch_depth sb_dec = 0) && outside_bodies sb_dec);
        [rewrite bind_get, Hfl_dec; reflexivity|reflexivity].
    + (* g_flushing = false /\ g_batch_depth = 0 -> pull_depth = 0 agrees *)
      assert (Hp_eq : bool_decide (g_pull_depth sa_dec = 0) = bool_decide (g_pull_depth sb_dec = 0)).
      { destruct Hrel_dec as [_ Hiff]. specialize (Hiff (conj Efl Hbd0)).
        destruct (bool_decide_reflect (g_pull_depth sa_dec = 0)), (bool_decide_reflect (g_pull_depth sb_dec = 0)); try reflexivity; exfalso; lia. }
      fold sa_dec in E4. rewrite Hp_eq, <- Hfo in E4.
      destruct (bool_decide (g_pull_depth sb_dec = 0) && g_flush_owed sb_dec) eqn:Econd.
      * destruct (B2Ssettle _ IH _ _ _ _ Hrel_dec E4) as (f3 & sb_fin & Eset & Hrel_fin).
        exists (Nat.max f2 f3), sb_fin. split; [|exact Hrel_fin].
        unfold m_bind, m_modify, get_node.
        rewrite (B1Mrefresh _ (b1_mono_all f2) (Nat.max f2 f3) n _ _ (Nat.le_max_l f2 f3) Eb1). simpl.
        rewrite Hns1, En. simpl. fold sb_dec. unfold sb_dec in Econd; simpl in Econd. rewrite Econd.
        rewrite (B1Msettle _ (b1_mono_all f3) (Nat.max f2 f3) _ _ (Nat.le_max_r f2 f3) Eset). reflexivity.
      * unfold m_return in E4; simplify_eq.
        exists f2, sb_dec. split; [|exact Hrel_dec].
        unfold m_bind, m_modify, get_node. rewrite Eb1. simpl. rewrite Hns1, En. simpl.
        fold sb_dec. unfold sb_dec in Econd; simpl in Econd. rewrite Econd. reflexivity.
    + (* g_batch_depth > 0 -> settle_flush is no-op on both sides *)
      assert (Hsa4 : s1' = sa_dec).
      { fold sa_dec in E4. destruct (bool_decide (g_pull_depth sa_dec = 0) && g_flush_owed sa_dec); [|unfold m_return in E4; by simplify_eq].
        destruct f; [discriminate|]. cbn [b1_settle_flush] in E4. rewrite bind_get in E4.
        rewrite (bool_decide_eq_false_2 (g_batch_depth sa_dec = 0%nat)) in E4 by lia.
        rewrite andb_false_r, andb_false_l in E4. unfold m_return in E4; by simplify_eq. }
      subst s1'.
      exists (Nat.max f2 1), sb_dec. split; [|exact Hrel_dec].
      unfold m_bind, m_modify, get_node.
      rewrite (B1Mrefresh _ (b1_mono_all f2) (Nat.max f2 1) n _ _ (Nat.le_max_l f2 1) Eb1). simpl.
      rewrite Hns1, En. simpl. fold sb_dec.
      match goal with |- context[if ?c then _ else _] => destruct c end; [|reflexivity].
      replace (Nat.max f2 1) with (S (Nat.max f2 1 - 1)) by lia.
      cbn [b1_settle_flush]. rewrite bind_get, Hbd_dec.
      rewrite (bool_decide_eq_false_2 (g_batch_depth sa_dec = 0%nat)) by lia.
      rewrite andb_false_r, andb_false_l. reflexivity.
  - (* B2Srefresh *)
    intros n s1 s2 s1' r Hrel H. b2_wrap_ex_S. cbn [b1_refresh] in H |- *.
    destruct (b2_pull_rel_fields _ _ Hrel) as (_&_&_&_&_&_&_&_&_&_&_&_).
    rewrite bind_get in H. setoid_rewrite bind_get.
    destruct (b2_pull_rel_fields _ _ Hrel) as (Hn&_).
    (* checking & running fields agree via with_pull_depth *)
    assert (Hcr : g_checking s2 = g_checking s1 /\ g_running s2 = g_running s1).
    { destruct Hrel as [Hw _]. unfold with_pull_depth in Hw. destruct s1, s2; simpl in *; injection Hw; intros; subst; auto. }
    rewrite (proj1 Hcr), (proj2 Hcr).
    destruct (decide (n ∈ g_checking s1)); [exists f; b2_pure_fwd|].
    destruct (decide (n ∈ g_running s1)); [b2_ret|].
    eapply (b2_bind_pure_l_of Hrel H); [b2_pure_fwd|intros d ?? Hrel1 Hk].
    eapply (b2p_bind_of Hk); [intros ????; b2_mono_tac|intros ?????; b2_mono_tac| |intros [] ?? Hrel2 Hk2].
    + intros sa sb sa' r_ Hrel_a Hm. destruct (decide (n_freshness d = Check)); [|b2_ret].
      eapply (b2_bind_pure_l_of Hrel_a Hm); [b2_pure_fwd|intros [] ?? Hrel2a Hk2a].
      eapply (b2p_bind_of Hk2a); [intros ????; b2_mono_tac|intros ?????; b2_mono_tac|intros ????; by eapply (B2Swalk _ IH)|intros [] ?? Hrel3a Hk3a].
      eapply (b2_bind_pure_l_of Hrel3a Hk3a); [b2_pure_fwd|intros [] ?? Hrel4a Hk4a].
      eapply (b2_bind_pure_l_of Hrel4a Hk4a); [b2_pure_fwd|intros d' ?? Hrel5a Hk5a].
      destruct (decide (n_freshness d' = Check)).
      * eapply (b2_bind_pure_l_of Hrel5a Hk5a); [b2_pure_fwd|intros [] ?? Hrel6a Hk6a].
        exists 0%nat; b2_pure_fwd.
      * b2_ret.
    + eapply (b2_bind_pure_l_of Hrel2 Hk2); [b2_pure_fwd|intros d' ?? Hrel3 Hk3].
      destruct (decide (n_freshness d' = Dirty)); [by eapply (B2Srecompute _ IH)|b2_ret].
  - (* B2Swalk *)
    intros n i s1 s2 s1' r Hrel H. b2_wrap_ex_S. cbn [b1_resolve_walk] in H |- *.
    eapply (b2_bind_pure_l_of Hrel H); [b2_pure_fwd|intros d ?? Hrel1 Hk].
    rewrite bind_get in Hk. setoid_rewrite bind_get.
    destruct (decide (n_freshness d = Check)); [|b2_ret].
    destruct (b2_pull_rel_fields _ _ Hrel1) as (_&He&_). rewrite He.
    destruct (source_slots (sources_of (g_edges sa1) n) !! i) as [[src ?]|].
    + eapply (b2p_bind_of Hk); [intros ????; b2_mono_tac|intros ?????; b2_mono_tac|intros ????; by eapply (B2Supdate _ IH)|intros [] ?? Hrel2 Hk2].
      eapply (b2_bind_pure_l_of Hrel2 Hk2); [b2_pure_fwd|intros d' ?? Hrel3 Hk3].
      eapply (b2_bind_pure_l_of Hrel3 Hk3); [b2_pure_fwd|intros d_src ?? Hrel4 Hk4].
      rewrite bind_get in Hk4. setoid_rewrite bind_get.
      destruct (b2_pull_rel_fields _ _ Hrel4) as (_&_&_&_&_&_&_&_&_&_&_&_&Hrun4&Hchk4).
      rewrite Hrun4, Hchk4.
      eapply (b2p_bind_of Hk4); [intros ????; b2_mono_tac|intros ?????; b2_mono_tac| |intros [] ?? Hrel5 Hk5; by eapply (B2Swalk _ IH)].
      intros sa sb sa' r_ Hrel_a Hm. destruct (decide _); [exists f; b2_pure_fwd|b2_ret].
    + destruct (b2_pull_rel_fields _ _ Hrel1) as (Hn1&_).
      assert (Hsh : slot_holds stale_live sb1 n = slot_holds stale_live sa1 n).
      { unfold slot_holds. rewrite He, Hn1. reflexivity. }
      rewrite Hsh.
      destruct (slot_holds stale_live sa1 n); [by eapply (B2Swalk _ IH)|b2_ret].
  - (* B2Srecompute *)
    intros n s1 s2 s1' r Hrel H. b2_wrap_ex_S. cbn [b1_recompute] in H |- *.
    eapply (b2_bind_pure_l_of Hrel H); [b2_pure_fwd|intros d ?? Hrel1 Hk].
    destruct (n_scope d) as [o|]; [|by eapply (B2Srun_memo _ IH)].
    eapply (b2_bind_pure_l_of Hrel1 Hk); [b2_pure_fwd|intros [] ?? Hrel2 Hk2].
    eapply (b2_bind_pure_l_of Hrel2 Hk2); [b2_pure_fwd|intros [] ?? Hrel3 Hk3].
    eapply (b2p_bind_of Hk3); [intros ????; b2_mono_tac|intros ?????; b2_mono_tac|intros ????; by eapply (B2Sdischarge _ IH)|intros [] ?? Hrel4 Hk4].
    eapply (b2_bind_pure_l_of Hrel4 Hk4); [b2_pure_fwd|intros d' ?? Hrel5 Hk5].
    destruct (n_disposed d').
    + destruct (decide (n_freshness d' = Clean)); [exists 0%nat; b2_pure_fwd|b2_ret].
    + destruct (decide (n_runs d' = n_runs d)); [by eapply (B2Srun_memo _ IH)|].
      destruct (decide (n_freshness d' = Dirty)); [by eapply (B2Srecompute _ IH)|b2_ret].
  - (* B2Srun_memo *)
    intros n s1 s2 s1' r Hrel H. b2_wrap_ex_S. cbn [b1_run_memo] in H |- *.
    eapply (b2_bind_pure_l_of Hrel H); [b2_pure_fwd|intros d ?? Hrel1 Hk].
    eapply (b2_bind_pure_l_of Hrel1 Hk); [b2_pure_fwd|intros [] ?? Hrel2 Hk2].
    eapply (b2_bind_pure_l_of Hrel2 Hk2); [b2_pure_fwd|intros [] ?? Hrel3 Hk3].
    eapply (b2_bind_pure_l_of Hrel3 Hk3); [b2_pure_fwd|intros [] ?? Hrel4 Hk4].
    eapply (b2_bind_pure_l_of Hrel4 Hk4); [b2_pure_fwd|intros [] ?? Hrel5 Hk5].
    eapply (b2p_bind_of Hk5); [intros ????; b2_mono_tac|intros ?????; b2_mono_tac| |intros ro ?? Hrel6 Hk6].
    + intros sa sb sa' r_ Hrel_a Hm. eapply b2p_hosted; [exact Hrel_a|intros; exact I|intros ????; by eapply (B2Sexec _ IH)|exact Hm].
    + eapply (b2_bind_pure_l_of Hrel6 Hk6); [b2_pure_fwd|intros d1 ?? Hrel7 Hk7].
      eapply (b2_bind_pure_l_of Hrel7 Hk7); [destruct (if n_violated d1 then Fail pure_scope_failure else ro); b2_pure_fwd|intros [] ?? Hrel8 Hk8].
      eapply (b2_bind_pure_l_of Hrel8 Hk8); [b2_pure_fwd|intros [] ?? Hrel9 Hk9].
      eapply (b2_bind_pure_l_of Hrel9 Hk9); [b2_pure_fwd|intros [] ?? Hrel10 Hk10].
      eapply (b2_bind_pure_l_of Hrel10 Hk10); [destruct (if n_violated d1 then Fail pure_scope_failure else ro); b2_pure_fwd|intros [] ?? Hrel11 Hk11].
      eapply (b2_bind_pure_l_of Hrel11 Hk11); [b2_pure_fwd|intros d2 ?? Hrel12 Hk12].
      eapply (b2_bind_pure_l_of Hrel12 Hk12); [destruct (decide _); [b2_pure_fwd|destruct (n_comparer d2); b2_pure_fwd]|intros moved ?? Hrel13 Hk13].
      rewrite bind_get in Hk13. setoid_rewrite bind_get.
      destruct (b2_pull_rel_fields _ _ Hrel13) as (_&_&_&_&_&_&_&_&_&Hcur&_).
      rewrite Hcur.
      eapply (b2_bind_pure_l_of Hrel13 Hk13); [b2_pure_fwd|intros [] ?? Hrel14 Hk14].
      eapply (b2p_bind_of Hk14); [intros ????; b2_mono_tac|intros ?????; b2_mono_tac| |intros [] ?? Hrel15 Hk15].
      * intros sa sb sa' r_ Hrel_a Hm. destruct moved; [|b2_ret].
        eapply (b2p_bind_of Hm); [intros ????; b2_mono_tac|intros ?????; b2_mono_tac|intros ??????; exists f; b2_pure_fwd|intros [] sa_b sb_b Hrel_b Hkb].
        rewrite bind_get in Hkb. setoid_rewrite bind_get.
        destruct (b2_pull_rel_fields _ _ Hrel_b) as (_&_&_&_&Hq&Hqh&Hfl&_&Hbd&_).
        assert (Hlq : live_queue sb_b = live_queue sa_b) by (unfold live_queue; rewrite Hq, Hqh; reflexivity).
        rewrite Hbd, Hfl, Hlq.
        destruct (bool_decide (g_batch_depth sa_b = 0) && negb (g_flushing sa_b) && live_queue sa_b); [exists 0%nat; b2_pure_fwd|b2_ret].
      * eapply (b2_bind_pure_l_of Hrel15 Hk15); [b2_pure_fwd|intros d3 ?? Hrel16 Hk16].
        destruct (decide (n_freshness d3 = Clean)); [b2_ret|exists 0%nat; b2_pure_fwd].
  - (* B2Sexecute *)
    intros n s1 s2 s1' r Hrel H. b2_wrap_ex_S. cbn [b1_execute_effect] in H |- *.
    eapply (b2_bind_pure_l_of Hrel H); [b2_pure_fwd|intros d ?? Hrel1 Hk].
    destruct (n_disposed d); [exists 0%nat; b2_pure_fwd|].
    eapply (b2p_bind_of Hk); [intros ????; b2_mono_tac|intros ?????; b2_mono_tac| |intros [] ?? Hrel2 Hk2].
    + intros sa sb sa' r_ Hrel_a Hm. destruct (decide (n_freshness d = Check)); [|b2_ret].
      eapply (b2p_bind_of Hm); [intros ????; b2_mono_tac|intros ?????; b2_mono_tac|intros ????; by eapply (B2Swalk _ IH)|intros [] ?? Hrel_b Hkb].
      eapply (b2_bind_pure_l_of Hrel_b Hkb); [b2_pure_fwd|intros d' ?? Hrel_c Hkc].
      destruct (decide (n_freshness d' = Check)); [|b2_ret].
      eapply (b2_bind_pure_l_of Hrel_c Hkc); [b2_pure_fwd|intros [] ?? Hrel_d Hkd].
      exists 0%nat; b2_pure_fwd.
    + eapply (b2_bind_pure_l_of Hrel2 Hk2); [b2_pure_fwd|intros d' ?? Hrel3 Hk3].
      eapply (b2_bind_pure_l_of Hrel3 Hk3); [b2_pure_fwd|intros [] ?? Hrel4 Hk4].
      destruct (decide (n_freshness d' = Dirty)); [by eapply (B2Srun_effect _ IH)|b2_ret].
  - (* B2Srun_effect *)
    intros n s1 s2 s1' r Hrel H. b2_wrap_ex_S. cbn [b1_run_effect] in H |- *.
    eapply (b2_bind_pure_l_of Hrel H); [b2_pure_fwd|intros [] ?? Hrel1 Hk].
    eapply (b2_bind_pure_l_of Hrel1 Hk); [b2_pure_fwd|intros [] ?? Hrel2 Hk2].
    eapply (b2_bind_pure_l_of Hrel2 Hk2); [b2_pure_fwd|intros [] ?? Hrel3 Hk3].
    eapply (b2_bind_pure_l_of Hrel3 Hk3); [b2_pure_fwd|intros d ?? Hrel4 Hk4].
    eapply (b2p_bind_of Hk4); [intros ????; b2_mono_tac|intros ?????; b2_mono_tac| |intros [] ?? Hrel5 Hk5].
    + intros sa sb sa' r_ Hrel_a Hm. destruct (n_scope d) as [o|]; [|b2_ret].
      eapply (b2_bind_pure_l_of Hrel_a Hm); [b2_pure_fwd|intros [] ?? Hrel_b Hkb].
      eapply (b2p_bind_of Hkb); [intros ????; b2_mono_tac|intros ?????; b2_mono_tac|intros ????; by eapply (B2Sdischarge _ IH)|intros [] ?? Hrel_c Hkc].
      exists 0%nat; b2_pure_fwd.
    + eapply (b2_bind_pure_l_of Hrel5 Hk5); [b2_pure_fwd|intros d' ?? Hrel6 Hk6].
      eapply (b2p_bind_of Hk6); [intros ????; b2_mono_tac|intros ?????; b2_mono_tac| |intros [] ?? Hrel7 Hk7; exists 0%nat; b2_pure_fwd].
      intros sa sb sa' r_ Hrel_a Hm. destruct (n_disposed d'); [b2_ret|by eapply (B2Sbody _ IH)].
  - (* B2Sbody *)
    intros n s1 s2 s1' r Hrel H. b2_wrap_ex_S. cbn [b1_run_effect_body] in H |- *.
    eapply (b2_bind_pure_l_of Hrel H); [b2_pure_fwd|intros d ?? Hrel1 Hk].
    eapply (b2_bind_pure_l_of Hrel1 Hk); [b2_pure_fwd|intros [] ?? Hrel2 Hk2].
    eapply (b2_bind_pure_l_of Hrel2 Hk2); [b2_pure_fwd|intros [] ?? Hrel3 Hk3].
    eapply (b2p_bind_of Hk3); [intros ????; b2_mono_tac|intros ?????; b2_mono_tac| |intros ro ?? Hrel4 Hk4].
    + intros sa sb sa' r_ Hrel_a Hm. eapply b2p_hosted; [exact Hrel_a|intros; exact I|intros ????; by eapply (B2Sexec _ IH)|exact Hm].
    + eapply (b2_bind_pure_l_of Hrel4 Hk4); [b2_pure_fwd|intros d' ?? Hrel5 Hk5].
      eapply (b2_bind_pure_l_of Hrel5 Hk5); [b2_pure_fwd|intros [] ?? Hrel6 Hk6].
      destruct ro; [b2_ret|].
      eapply (b2_bind_pure_l_of Hrel6 Hk6); [b2_pure_fwd|intros [] ?? Hrel7 Hk7].
      exists 0%nat; b2_pure_fwd.
  - (* B2Sdischarge *)
    intros o s1 s2 s1' r Hrel H. b2_wrap_ex_S. cbn [b1_discharge] in H |- *.
    eapply (b2p_bind_of H); [intros ????; b2_mono_tac|intros ?????; b2_mono_tac| |intros [] sa_1 sb_1 Hrel1 Hk].
    + intros sa sb sa' r_ Hrel_a Hm. eapply b2p_detached; [exact Hrel_a| | | | |exact Hm].
      * intros ????; by eapply (B2Sdispose_scope _ IH).
      * intros ??? E; exact (B1BKdispose_scope _ (b1_batch_kept f) _ _ _ _ E).
      * intros ??? E; exact (proj1 (B1Ddispose_scope _ (b1_depths_kept f) _ _ _ _ E)).
      * intros f0 ??? E; apply (B1Ddispose_scope f0 (b1_depths_kept f0)) in E as [Hpd Hfl_]; exact (conj Hfl_ Hpd).
    + rewrite bind_get in Hk. setoid_rewrite bind_get.
      destruct (b2_pull_rel_fields _ _ Hrel1) as (_&_&_&_&Hq&Hqh&Hfl&_&Hbd&_).
      assert (Hlq : live_queue sb_1 = live_queue sa_1) by (unfold live_queue; rewrite Hq, Hqh; reflexivity).
      rewrite Hbd, Hfl, Hlq.
      destruct (bool_decide (g_batch_depth sa_1 = 0) && negb (g_flushing sa_1) && live_queue sa_1); [exists 0%nat; b2_pure_fwd|b2_ret].
  - (* B2Sretire *)
    intros o s1 s2 s1' r Hrel H. b2_wrap_ex_S. cbn [b1_retire] in H |- *.
    eapply (b2p_bind_of H); [intros ????; b2_mono_tac|intros ?????; b2_mono_tac| |intros [] sa_1 sb_1 Hrel1 Hk].
    + intros sa sb sa' r_ Hrel_a Hm. eapply b2p_detached; [exact Hrel_a| | | | |exact Hm].
      * intros sa0 sb0 sa0' r0 Hrel0 Hm0.
        eapply (b2_bind_pure_l_of Hrel0 Hm0); [b2_pure_fwd|intros x ?? Hrel_x Hkx].
        destruct (o_disposed x); [b2_ret|].
        eapply (b2_bind_pure_l_of Hrel_x Hkx); [b2_pure_fwd|intros [] ?? Hrel_y Hky].
        by eapply (B2Sdispose_scope _ IH).
      * intros sa0 sa0' r0 Hm0.
        eapply bind_inv in Hm0 as (sa_x & x & E_x & Hm0).
        assert (Hbd_x : g_batch_depth sa_x = g_batch_depth sa0) by (unfold get_owner in E_x; destruct (g_owners sa0 !! o); simplify_eq; reflexivity).
        destruct (o_disposed x); [unfold m_return in Hm0; simplify_eq; exact Hbd_x|].
        eapply bind_inv in Hm0 as (sa_y & [] & E_y & Hm0). unfold put_owner, m_modify in E_y; simplify_eq.
        apply (B1BKdispose_scope _ (b1_batch_kept f)) in Hm0. simpl in Hm0. congruence.
      * intros sa0 sa0' r0 Hm0.
        eapply bind_inv in Hm0 as (sa_x & x & E_x & Hm0).
        assert (Hpd_x : g_pull_depth sa_x = g_pull_depth sa0) by (unfold get_owner in E_x; destruct (g_owners sa0 !! o); simplify_eq; reflexivity).
        destruct (o_disposed x); [unfold m_return in Hm0; simplify_eq; exact Hpd_x|].
        eapply bind_inv in Hm0 as (sa_y & [] & E_y & Hm0). unfold put_owner, m_modify in E_y; simplify_eq.
        destruct (B1Ddispose_scope _ (b1_depths_kept f) _ _ _ _ Hm0) as [Hpd_y _]. simpl in Hpd_y. congruence.
      * intros f0 sa0 sa0' r0 Hm0.
        eapply bind_inv in Hm0 as (sa_x & x & E_x & Hm0).
        assert (Hdp_x : g_flushing sa_x = g_flushing sa0 /\ g_pull_depth sa_x = g_pull_depth sa0)
          by (unfold get_owner in E_x; destruct (g_owners sa0 !! o); simplify_eq; auto).
        destruct (o_disposed x); [unfold m_return in Hm0; simplify_eq; exact Hdp_x|].
        eapply bind_inv in Hm0 as (sa_y & [] & E_y & Hm0). unfold put_owner, m_modify in E_y; simplify_eq.
        destruct (B1Ddispose_scope _ (b1_depths_kept f0) _ _ _ _ Hm0) as [Hpd_y Hfl_y]. destruct Hdp_x as [Hfl_x Hpd_x]. simpl in Hpd_y, Hfl_y. split; congruence.
    + rewrite bind_get in Hk. setoid_rewrite bind_get.
      destruct (b2_pull_rel_fields _ _ Hrel1) as (_&_&_&_&Hq&Hqh&_).
      assert (Hlq : live_queue sb_1 = live_queue sa_1) by (unfold live_queue; rewrite Hq, Hqh; reflexivity).
      rewrite Hlq. destruct (live_queue sa_1); [by eapply (B2Srequest _ IH)|b2_ret].
  - (* B2Sdispose_scope *)
    intros o s1 s2 s1' r Hrel H. b2_wrap_ex_S. cbn [b1_dispose_scope] in H |- *.
    eapply (b2_bind_pure_l_of Hrel H); [b2_pure_fwd|intros x ?? Hrel1 Hk].
    eapply (b2_bind_pure_l_of Hrel1 Hk); [b2_pure_fwd|intros [] ?? Hrel2 Hk2].
    by eapply (B2Steardown _ IH).
  - (* B2Steardown *)
    intros o ls cs s1 s2 s1' r Hrel H. b2_wrap_ex_S. cbn [b1_teardown] in H |- *.
    destruct ls as [|l ls'], cs as [|[[b e] a] cs'].
    + b2_ret.
    + eapply (b2p_bind_of H); [intros ????; b2_mono_tac|intros ?????; b2_mono_tac|intros ????; by eapply (B2Scleanup _ IH)|intros [] ?? Hrel1 Hk].
      by eapply (B2Steardown _ IH).
    + eapply (b2p_bind_of H); [intros ????; b2_mono_tac|intros ?????; b2_mono_tac|intros ????; by eapply (B2Srelease _ IH)|intros [] ?? Hrel1 Hk].
      by eapply (B2Steardown _ IH).
    + destruct (decide (a < l_acquired l)%nat);
      (eapply (b2p_bind_of H); [intros ????; b2_mono_tac|intros ?????; b2_mono_tac|intros ????; first [by eapply (B2Srelease _ IH)|by eapply (B2Scleanup _ IH)]|intros [] ?? Hrel1 Hk];
       by eapply (B2Steardown _ IH)).
  - (* B2Scleanup *)
    intros o b e s1 s2 s1' r Hrel H. b2_wrap_ex_S. cbn [b1_run_cleanup] in H |- *.
    eapply (b2_bind_pure_l_of Hrel H); [b2_pure_fwd|intros x ?? Hrel1 Hk].
    eapply (b2_bind_pure_l_of Hrel1 Hk); [b2_pure_fwd|intros [] ?? Hrel2 Hk2].
    eapply (b2p_bind_of Hk2); [intros ????; b2_mono_tac|intros ?????; b2_mono_tac|intros ????; by eapply (B2Sexec _ IH)|intros rc ?? Hrel3 Hk3].
    destruct rc; [b2_ret|].
    eapply (b2_bind_pure_l_of Hrel3 Hk3); [b2_pure_fwd|intros [] ?? Hrel4 Hk4].
    exists 0%nat; b2_pure_fwd.
  - (* B2Srelease *)
    intros n s1 s2 s1' r Hrel H. b2_wrap_ex_S. cbn [b1_release] in H |- *.
    eapply (b2_bind_pure_l_of Hrel H); [b2_pure_fwd|intros [] ?? Hrel1 Hk].
    by eapply (B2Sdispose _ IH).
  - (* B2Sdispose *)
    intros n s1 s2 s1' r Hrel H. b2_wrap_ex_S. cbn [b1_dispose_node] in H |- *.
    eapply (b2_bind_pure_l_of Hrel H); [b2_pure_fwd|intros d ?? Hrel1 Hk].
    destruct (n_disposed d); [b2_ret|].
    eapply (b2_bind_pure_l_of Hrel1 Hk); [b2_pure_fwd|intros [] ?? Hrel2 Hk2].
    eapply (b2_bind_pure_l_of Hrel2 Hk2); [destruct (n_link d); b2_pure_fwd|intros [] ?? Hrel3 Hk3].
    eapply (b2_bind_pure_l_of Hrel3 Hk3); [b2_pure_fwd|intros [] ?? Hrel4 Hk4].
    destruct (n_scope d); [by eapply (B2Sretire _ IH)|b2_ret].
  - (* B2Sattach *)
    intros n o s1 s2 s1' r Hrel H. b2_wrap_ex_S. cbn [b1_attach] in H |- *.
    eapply (b2_bind_pure_l_of Hrel H); [b2_pure_fwd|intros x ?? Hrel1 Hk].
    destruct (o_disposed x).
    + eapply (b2p_bind_of Hk); [intros ????; b2_mono_tac|intros ?????; b2_mono_tac|intros ????; by eapply (B2Srelease _ IH)|intros [] ?? Hrel2 Hk2].
      exists 0%nat; b2_pure_fwd.
    + eapply (b2_bind_pure_l_of Hrel1 Hk); [b2_pure_fwd|intros [] ?? Hrel2 Hk2].
      exists 0%nat; b2_pure_fwd.
  - (* B2Swrite *)
    intros n v s1 s2 s1' r Hrel H. b2_wrap_ex_S. cbn [b1_write_signal] in H |- *.
    eapply (b2_bind_pure_l_of Hrel H); [b2_pure_fwd|intros d ?? Hrel1 Hk].
    destruct (n_kind d); [|discriminate|discriminate].
    destruct (decide (n_value d = v)); [b2_ret|].
    eapply (b2_bind_pure_l_of Hrel1 Hk); [b2_pure_fwd|intros [] ?? Hrel2 Hk2].
    eapply (b2_bind_pure_l_of Hrel2 Hk2); [b2_pure_fwd|intros [] ?? Hrel3 Hk3].
    eapply (b2p_bind_of Hk3); [intros ????; b2_mono_tac|intros ?????; b2_mono_tac|intros ??????; exists f; b2_pure_fwd|intros [] ?? Hrel4 Hk4].
    by eapply (B2Srequest _ IH).
  - (* B2Srequest *)
    intros s1 s2 s1' r Hrel H. b2_wrap_ex_S. cbn [b1_request_flush] in H |- *.
    rewrite bind_get in H. setoid_rewrite bind_get.
    destruct (b2_pull_rel_fields _ _ Hrel) as (_&_&_&_&Hq&Hqh&Hfl&_&Hbd&Hcur&Hunt&_).
    assert (Hlq : live_queue s2 = live_queue s1) by (unfold live_queue; rewrite Hq, Hqh; reflexivity).
    rewrite Hlq, Hbd.
    destruct (live_queue s1 && bool_decide (g_batch_depth s1 = 0%nat)) eqn:Econd; [|unfold m_return in H; simplify_eq; by exists 0%nat, s2].
    apply andb_true_iff in Econd as [_ Hbd0]. apply bool_decide_eq_true in Hbd0.
    destruct (g_flushing s1) eqn:Efl.
    + assert (Hs1' : s1' = s1 /\ r = tt).
      { destruct (outside_bodies s1).
        - destruct f; [discriminate|]. cbn [b1_run_flush] in H. rewrite bind_get, Efl in H.
          unfold m_return in H; simplify_eq; auto.
        - unfold m_return in H; simplify_eq; auto. }
      destruct Hs1' as [-> ->].
      exists 2%nat, s2. split; [|exact Hrel].
      cbn [b1_run_flush].
      destruct (outside_bodies s2); [rewrite bind_get, Hfl; reflexivity|rewrite Hfl; reflexivity].
    + assert (Hob : outside_bodies s2 = outside_bodies s1).
      { unfold outside_bodies. rewrite Hcur, Hunt.
        destruct Hrel as [_ Hiff]. specialize (Hiff (conj Efl Hbd0)).
        destruct (bool_decide_reflect (g_pull_depth s1 = 0%nat)), (bool_decide_reflect (g_pull_depth s2 = 0%nat)); try reflexivity; exfalso; lia. }
      rewrite Hob. destruct (outside_bodies s1); [by eapply (B2Sflush _ IH)|rewrite Hfl; b2_ret].
  - (* B2Ssettle *)
    intros s1 s2 s1' r Hrel H. b2_wrap_ex_S. cbn [b1_settle_flush] in H |- *.
    rewrite bind_get in H. setoid_rewrite bind_get.
    destruct (b2_pull_rel_fields _ _ Hrel) as (_&_&_&_&_&_&Hfl&Hfo&Hbd&Hcur&Hunt&_).
    destruct (g_flushing s1) eqn:Efl.
    + assert (Hs1' : s1' = s1 /\ r = tt).
      { destruct (g_flush_owed s1 && bool_decide (g_batch_depth s1 = 0%nat) && outside_bodies s1);
          [|unfold m_return in H; simplify_eq; auto].
        destruct f; [discriminate|]. cbn [b1_run_flush] in H. rewrite bind_get, Efl in H.
        unfold m_return in H; simplify_eq; auto. }
      destruct Hs1' as [-> ->].
      exists 2%nat, s2. split; [|exact Hrel].
      cbn [b1_run_flush]. rewrite Hfo, Hbd.
      destruct (g_flush_owed s1 && bool_decide (g_batch_depth s1 = 0%nat) && outside_bodies s2);
        [rewrite bind_get, Hfl; reflexivity|reflexivity].
    + destruct (Nat.eq_dec (g_batch_depth s1) 0) as [Hbd0|Hbdpos].
      * assert (Hob : outside_bodies s2 = outside_bodies s1).
        { unfold outside_bodies. rewrite Hcur, Hunt.
          destruct Hrel as [_ Hiff]. specialize (Hiff (conj Efl Hbd0)).
          destruct (bool_decide_reflect (g_pull_depth s1 = 0%nat)), (bool_decide_reflect (g_pull_depth s2 = 0%nat)); try reflexivity; exfalso; lia. }
        rewrite Hfo, Hbd, Hob.
        destruct (g_flush_owed s1 && bool_decide (g_batch_depth s1 = 0%nat) && outside_bodies s1);
          [by eapply (B2Sflush _ IH)|b2_ret].
      * rewrite (bool_decide_eq_false_2 (g_batch_depth s1 = 0%nat)) in H by exact Hbdpos.
        rewrite andb_false_r, andb_false_l in H. unfold m_return in H; inversion H; subst s1' r; clear H.
        exists 1%nat, s2. split; [|exact Hrel].
        rewrite Hbd.
        rewrite (bool_decide_eq_false_2 (g_batch_depth s1 = 0%nat)) by exact Hbdpos.
        rewrite andb_false_r, andb_false_l. reflexivity.
  - (* B2Sflush *)
    intros s1 s2 s1' r Hrel H. b2_wrap_ex_S. cbn [b1_run_flush] in H |- *.
    rewrite bind_get in H. setoid_rewrite bind_get.
    destruct (b2_pull_rel_fields _ _ Hrel) as (_&_&_&_&_&_&Hfl&_&Hbd&_).
    rewrite Hfl. destruct (g_flushing s1) eqn:Efl; [b2_ret|].
    eapply bind_inv in H as (sa1 & [] & E1 & H). unfold m_modify in E1; simplify_eq.
    eapply bind_inv in H as (sa2 & [] & E2 & H). unfold m_modify in H; simplify_eq.
    assert (Hrel1 : b2_pull_rel (with_flush_owed false (with_flushing true s1)) (with_flush_owed false (with_flushing true s2))).
    { destruct Hrel as [Hw _]. split; [rewrite <- Hw; unfold with_pull_depth; destruct s1; reflexivity|simpl; intro Hbad; destruct Hbad as [? _]; discriminate]. }
    destruct (B2Sdrain _ IH _ _ _ _ Hrel1 E2) as (f2 & sb2 & Ed2 & [Hw2 _]).
    pose proof (B1Ddrain _ (b1_depths_kept f) _ _ _ E2) as [Hpd1 Hfl1]. simpl in Hpd1, Hfl1.
    pose proof (B1BKdrain _ (b1_batch_kept f) _ _ _ E2) as Hbd1. simpl in Hbd1.
    pose proof (B1Ddrain _ (b1_depths_kept f2) _ _ _ Ed2) as [Hpd2 Hfl2]. simpl in Hpd2, Hfl2.
    exists f2, (with_flushing false sb2).
    split.
    + unfold m_bind, m_modify. rewrite Ed2. reflexivity.
    + split; [rewrite <- Hw2; unfold with_pull_depth; destruct sa2; reflexivity|].
      simpl. rewrite Hpd1, Hpd2, Hbd1. intros [_ Hbd0].
      destruct Hrel as [_ Hiff]. exact (Hiff (conj Efl Hbd0)).
  - (* B2Sdrain *)
    intros s1 s2 s1' r Hrel H. b2_wrap_ex_S. cbn [b1_drain] in H |- *.
    rewrite bind_get in H. setoid_rewrite bind_get.
    destruct (b2_pull_rel_fields _ _ Hrel) as (_&_&_&_&Hq&Hqh&_).
    rewrite Hq, Hqh.
    destruct (g_queue s1 !! g_queue_head s1) as [n|].
    + eapply (b2_bind_pure_l_of Hrel H);
        [intros sa0 sb0 sa1 [] [Hw0 Hiff0] Hm0; unfold m_modify in Hm0; simplify_eq;
         exists (with_queue_head (S (g_queue_head s1)) sb0); split; [unfold m_modify; reflexivity|];
         split; [rewrite <- Hw0; unfold with_pull_depth; destruct sa0; reflexivity|exact Hiff0]
        |intros [] ?? Hrel1 Hk].
      eapply (b2p_bind_of Hk); [intros ????; b2_mono_tac|intros ?????; b2_mono_tac|intros ????; by eapply (B2Sexecute _ IH)|intros [] ?? Hrel2 Hk2].
      by eapply (B2Sdrain _ IH).
    + exists 0%nat. b2_pure_fwd.
Qed.
References

Uses

153 more
✓ No axioms

Under sched_inv, whenever b1_deferring s = true (g_pull_depth s > 0 \/ g_batch_depth s > 0 \/ g_flushing s = true), skipping EnterPull/ExitPullDeferSettle (b1_refresh) refines b1_pull with identical final engine state s' and result (d or tt) for matching fuel.

Arguments

x : option node_id
enclosing scheduled node identifier (if any) in sched_inv.
n : node_id
target memo or boundary node id being refreshed/pulled.
s : State.state
deferring starting engine state satisfying sched_inv x s and b1_deferring s = true.
s' : State.state
resulting engine state after refresh or pull.
d : State.node
read node snapshot at n after completion.
Theorem deferring_pull_refines_enter_pull x n s s' d :
  sched_inv x s ->
  b1_deferring s = true ->
  ((exists fp, b1_pull fp n s = Some (s', d)) <->
   (exists fr, (let* _ := b1_refresh fr n in get_node n) s = Some (s', d))) /\
  ((exists fp, (let* _ := b1_pull fp n in m_return tt) s = Some (s', tt)) <->
   (exists fr, (let* _ := b1_refresh fr n in let* _ := get_node n in m_return tt) s = Some (s', tt))) /\
  (g_nodes s' !! n = Some d ->
   ((exists fp, (let* _ := b1_pull fp n in m_return tt) s = Some (s', tt)) <->
    (exists fr, b1_refresh fr n s = Some (s', tt)))).

Arguments

x : option node_id
enclosing scheduled node identifier (if any) in sched_inv.
n : node_id
target memo or boundary node id being refreshed/pulled.
s : State.state
deferring starting engine state satisfying sched_inv x s and b1_deferring s = true.
s' : State.state
resulting engine state after refresh or pull.
d : State.node
read node snapshot at n after completion.
Qed
Proof.
  intros _ Hdef.
  assert (Hdisj : g_flushing s = true \/ 0 < g_batch_depth s \/ 0 < g_pull_depth s).
  { unfold b1_deferring in Hdef.
    repeat match type of Hdef with (_ || _ = true) => apply orb_true_iff in Hdef as [Hdef|Hdef] end;
      try rewrite bool_decide_eq_true in *; auto. }
  assert (Hrel_lr : b2_pull_rel s (with_pull_depth (S (g_pull_depth s)) s)).
  { apply b2_pull_rel_with_pull_S_right; [apply b2_pull_rel_refl|exact Hdisj]. }
  assert (Hrel_rl : b2_pull_rel (with_pull_depth (S (g_pull_depth s)) s) s)
    by (apply b2_pull_rel_sym; exact Hrel_lr).
  assert (Hpull_body : forall fp s_out d_out,
    b1_pull fp n s = Some (s_out, d_out) ->
    exists fp', b1_refresh fp' n (with_pull_depth (S (g_pull_depth s)) s) = Some (with_pull_depth (S (g_pull_depth s)) s_out, tt) /\
                g_nodes s_out !! n = Some d_out).
  { intros [|fp'] s_out d_out Hpull; [discriminate|].
    cbn [b1_pull] in Hpull.
    eapply bind_inv in Hpull as (s1 & [] & E1 & Hpull). unfold m_modify in E1; simplify_eq.
    eapply bind_inv in Hpull as (s2 & [] & E2 & Hpull).
    eapply bind_inv in Hpull as (s3 & d3 & E3 & Hpull). unfold get_node in E3.
    destruct (g_nodes s2 !! n) eqn:En; [|discriminate]. simplify_eq. rename s3 into s2.
    eapply bind_inv in Hpull as (s4 & [] & E4 & Hpull). unfold m_modify in E4; simplify_eq.
    rewrite bind_get in Hpull.
    eapply bind_inv in Hpull as (s5 & [] & E5 & Hpull). unfold m_return in Hpull; simplify_eq.
    pose proof (B1Drefresh _ (b1_depths_kept fp') _ _ _ _ E2) as [Hpd2 Hfl2]. simpl in Hpd2, Hfl2.
    pose proof (B1BKrefresh _ (b1_batch_kept fp') _ _ _ _ E2) as Hbd2. simpl in Hbd2.
    set (s_dec := with_pull_depth (pred (g_pull_depth s2)) s2) in *.
    assert (Hs5 : s_out = s_dec).
    { fold s_dec in E5. destruct (bool_decide (g_pull_depth s_dec = 0) && g_flush_owed s_dec) eqn:Econd;
        [|unfold m_return in E5; by simplify_eq].
      destruct fp'; [discriminate|]. cbn [b1_settle_flush] in E5. rewrite bind_get in E5.
      destruct Hdisj as [Hfl|[Hbd|Hpd]].
      - assert (Hfl_dec : g_flushing s_dec = true) by (unfold s_dec; simpl; congruence).
        destruct (g_flush_owed s_dec && bool_decide (g_batch_depth s_dec = 0) && outside_bodies s_dec);
          [|unfold m_return in E5; by simplify_eq].
        destruct fp'; [discriminate|]. cbn [b1_run_flush] in E5. rewrite bind_get, Hfl_dec in E5.
        unfold m_return in E5; by simplify_eq.
      - assert (Hbd_dec : 0 < g_batch_depth s_dec) by (unfold s_dec; simpl; lia).
        rewrite (bool_decide_eq_false_2 (g_batch_depth s_dec = 0%nat)) in E5 by lia.
        rewrite andb_false_r, andb_false_l in E5. unfold m_return in E5; by simplify_eq.
      - exfalso. unfold s_dec in Econd. simpl in Econd. rewrite Hpd2 in Econd. simpl in Econd.
        rewrite (bool_decide_eq_false_2 (g_pull_depth s = 0%nat)) in Econd by lia. discriminate. }
    subst s_out. exists fp'. split.
    - replace (with_pull_depth (S (g_pull_depth s)) s_dec) with s2; [exact E2|].
      unfold s_dec, with_pull_depth. destruct s2; simpl in *; f_equal; lia.
    - unfold s_dec; simpl. exact En. }
  assert (Hfwd_unit : forall d_out,
    (exists fp, b1_pull fp n s = Some (s', d_out)) ->
    exists fr, b1_refresh fr n s = Some (s', tt) /\ g_nodes s' !! n = Some d_out).
  { intros d_out [fp Ep].
    destruct (Hpull_body fp s' d_out Ep) as (fp' & Eref & Hn).
    destruct (B2Srefresh _ (b2_sim_all fp') n _ _ _ _ Hrel_rl Eref) as (fr & sr & Er & [Hw _]).
    pose proof (B1Drefresh _ (b1_depths_kept fr) _ _ _ _ Er) as [Hpdr _].
    pose proof (B1Dpull _ (b1_depths_kept fp) _ _ _ _ Ep) as [Hpdp _].
    assert (Hs' : sr = s').
    { rewrite <- Hw. unfold with_pull_depth. destruct s'; simpl in *; f_equal; lia. }
    subst sr. exists fr. split; assumption. }
  assert (Hbwd_unit :
    (exists fr, b1_refresh fr n s = Some (s', tt)) ->
    forall d0, g_nodes s' !! n = Some d0 ->
    exists fp, b1_pull fp n s = Some (s', d0)).
  { intros [fr Er] d0 Hd0.
    destruct (B2Srefresh _ (b2_sim_all fr) n _ _ _ _ Hrel_lr Er) as (fp' & s2 & E2 & [Hw _]).
    pose proof (B1Drefresh _ (b1_depths_kept fr) _ _ _ _ Er) as [Hpd1 Hfl1].
    pose proof (B1BKrefresh _ (b1_batch_kept fr) _ _ _ _ Er) as Hbd1.
    pose proof (B1Drefresh _ (b1_depths_kept fp') _ _ _ _ E2) as [Hpd2 Hfl2]. simpl in Hpd2, Hfl2.
    pose proof (B1BKrefresh _ (b1_batch_kept fp') _ _ _ _ E2) as Hbd2. simpl in Hbd2.
    assert (Hsdec : with_pull_depth (pred (g_pull_depth s2)) s2 = s').
    { rewrite <- Hw. unfold with_pull_depth. destruct s'; simpl in *; f_equal; lia. }
    exists (S (Nat.max fp' 2)). cbn [b1_pull]. unfold m_bind, m_modify.
    rewrite (B1Mrefresh _ (b1_mono_all fp') (Nat.max fp' 2) n _ _ (Nat.le_max_l fp' 2) E2). simpl.
    unfold get_node.
    assert (Hs2n : g_nodes s2 !! n = Some d0) by (rewrite <- Hsdec in Hd0; exact Hd0).
    rewrite Hs2n. simpl.
    assert (pred (g_pull_depth s2) = g_pull_depth s' /\ g_flush_owed s2 = g_flush_owed s')
      as [Hgp Hgf] by (rewrite <- Hsdec; split; reflexivity).
    rewrite Hsdec, Hgp, Hgf.
    destruct (bool_decide (g_pull_depth s' = 0) && g_flush_owed s') eqn:Econd; [|reflexivity].
    replace (Nat.max fp' 2) with (S (S (Nat.max fp' 2 - 2))) by lia.
    cbn [b1_settle_flush]. rewrite bind_get.
    destruct Hdisj as [Hfl|[Hbd|Hpd]].
    - cbn [b1_run_flush].
      destruct (g_flush_owed s' && bool_decide (g_batch_depth s' = 0) && outside_bodies s');
        [rewrite bind_get, Hfl1, Hfl; reflexivity|reflexivity].
    - rewrite (bool_decide_eq_false_2 (g_batch_depth s' = 0%nat)) by lia.
      rewrite andb_false_r, andb_false_l. reflexivity.
    - exfalso. rewrite Hpd1 in Econd.
      rewrite (bool_decide_eq_false_2 (g_pull_depth s = 0%nat)) in Econd by lia. discriminate. }
  assert (Hnode_eq :
    (exists fp, b1_pull fp n s = Some (s', d)) <->
    (exists fr, (let* _ := b1_refresh fr n in get_node n) s = Some (s', d))).
  { split.
    - intros Hpull. destruct (Hfwd_unit d Hpull) as (fr & Er & Hn).
      exists fr. unfold m_bind. rewrite Er. simpl. unfold get_node. rewrite Hn. reflexivity.
    - intros [fr Href]. eapply bind_inv in Href as (s1 & [] & Er & Eg).
      unfold get_node in Eg. destruct (g_nodes s1 !! n) eqn:En; [|discriminate]. simplify_eq.
      exact (Hbwd_unit (ex_intro _ fr Er) d En). }
  split; [exact Hnode_eq|split; [split|intro Hd; split]].
  - intros [fp Hpull]. eapply bind_inv in Hpull as (s_out & d_out & Ep & Hm).
    unfold m_return in Hm; simplify_eq.
    destruct (Hfwd_unit d_out (ex_intro _ fp Ep)) as (fr & Er & Hn).
    exists fr. unfold m_bind, get_node, m_return. rewrite Er. simpl. rewrite Hn. reflexivity.
  - intros [fr Href]. eapply bind_inv in Href as (s1 & [] & Er & Hk).
    eapply bind_inv in Hk as (s2 & d2 & Eg & Hm).
    unfold get_node in Eg. destruct (g_nodes s1 !! n) eqn:En; [|discriminate]. simplify_eq.
    unfold m_return in Hm; simplify_eq.
    destruct (Hbwd_unit (ex_intro _ fr Er) d2 En) as [fp Ep].
    exists fp. unfold m_bind, m_return. rewrite Ep. reflexivity.
  - intros [fp Hpull]. eapply bind_inv in Hpull as (s_out & d_out & Ep & Hm).
    unfold m_return in Hm; simplify_eq.
    destruct (Hfwd_unit d_out (ex_intro _ fp Ep)) as (fr & Er & _).
    exists fr. exact Er.
  - intros Href. destruct (Hbwd_unit Href d Hd) as [fp Ep].
    exists fp. unfold m_bind, m_return. rewrite Ep. reflexivity.
Qed.
References

B13. Timed now-reuse non-immediate fire & Stamp non-throwing version equality

✓ No axioms

Reusing the captured now timestamp for a strictly positive delay ms > 0 guarantees both deadline = now + ms lies strictly in the future ((now <? now + ms)%Z = true /\ ~ (now >= now + ms)%Z) and the immediate elapsed-time test now - startedAt (now - now = 0) is strictly below ms ((now - now <? ms)%Z = true /\ ~ (now - now >= ms)%Z), so a freshly armed Timed node cannot immediately fire on the same tick.

Arguments

now : Z
current millisecond clock reading at arming.
ms : Z
strictly positive delay duration in milliseconds.
Theorem timed_capture_now_reuse_no_immediate_fire (now ms : Z) :
  (ms > 0)%Z ->
  ((now <? now + ms)%Z = true /\ ~ (now >= now + ms)%Z) /\
  ((now - now <? ms)%Z = true /\ ~ (now - now >= ms)%Z).

Arguments

now : Z
current millisecond clock reading at arming.
ms : Z
strictly positive delay duration in milliseconds.
Qed
Proof.
  intros Hms. split; split; [apply Z.ltb_lt| |apply Z.ltb_lt|]; lia.
Qed.
References

Uses

  • true
  • Z
  • Z.ltb_lt
Definition stamp_version_rel {A : Type} (x y : Z * A) : Prop := x.1 = y.1.
References

Uses

  • Z
Definition stamp_version_eq {A : Type} (x y : Z * A) : bool := Z.eqb x.1 y.1.
References

Uses

  • bool
  • Z
  • Z.eqb
Definition stamp_version_eq_dec {A : Type} (x y : Z * A) :
  {stamp_version_rel x y} + {~ stamp_version_rel x y} := Z.eq_dec x.1 y.1.
References

Uses

✓ No axioms

Version-only equality on (ver, payload) stamp pairs is an equivalence relation (Equivalence), decidable, reflected by Z.eqb on version numbers, and non-throwing (CmpEq on ver matches ready_value_moved with zero error transition).

Arguments

A : Type
payload type ignored by the version-only stamp comparer.
Theorem stamp_version_eq_equivalence (A : Type) :
  Equivalence (@stamp_version_rel A) /\
  (forall x y : Z * A, stamp_version_rel x y \/ ~ stamp_version_rel x y) /\
  (forall x y : Z * A, stamp_version_eq x y = true <-> stamp_version_rel x y) /\
  (forall (x y : Z * A) n s,
    ready_value_moved x.1 y.1 CmpEq n s = Some (s, negb (stamp_version_eq x y)) /\
    (forall err, CmpEq <> CmpThrows err)).

Arguments

A : Type
payload type ignored by the version-only stamp comparer.
Qed
Proof.
  split; [|split; [|split]].
  - split.
    + intros x. reflexivity.
    + intros x y H. symmetry. exact H.
    + intros x y z Hxy Hyz. transitivity y.1; assumption.
  - intros x y. destruct (Z.eq_dec x.1 y.1) as [H|H]; [left|right]; exact H.
  - intros x y. unfold stamp_version_eq, stamp_version_rel. apply Z.eqb_eq.
  - intros x y n s. split.
    + unfold ready_value_moved, stamp_version_eq.
      f_equal. f_equal.
      destruct (Z.eqb_spec x.1 y.1) as [Heq|Hne].
      * rewrite bool_decide_eq_false_2 by (intro H; apply H; symmetry; exact Heq). reflexivity.
      * rewrite bool_decide_eq_true_2 by (intro H; apply Hne; symmetry; exact H). reflexivity.
    + intros err H. discriminate.
Qed.
References

Uses

7 more
  • true
  • Z
  • Z.eq_dec
  • Z.eqb_eq
  • Z.eqb_spec
  • bool_decide_eq_false_2
  • bool_decide_eq_true_2
✓ No axioms

A completed source scan leaves the edge world unchanged. MemoOn Phase 1 skips finalization when every slot matched in place.

Theorem end_run_matched_cursor self w :
  source_cursor (sources_of w self) = length (source_slots (sources_of w self)) ->
  sources_end_run self w = Some w.
Qed
Proof. intro H. unfold sources_end_run. rewrite decide_False by lia. reflexivity. Qed.
References