Ranvier.Pure.Combinators

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

Closed-window `*On` primitives, reactive combinators, unobserved `map`-chain fusion, and CE step dispatchcomb_readingresultis_unready_readingresult_to_readingproject_unreadyB16. Closed-window MemoOn settled-key cutoff and transient-unready preservationmemo_on_statememo_on_stepmemo_on_stepsmemo_on_steps_unready_preservesmemo_on_steps_appmemo_on_cutoff_eqB17. Closed-window AsyncOn six-policy flight preservationflight_policyflight_policy_eq_decis_deferring_policyasync_on_stateao_visibleasync_on_launchasync_on_stepfifo_recordfifo_drainasync_on_settleactive_overlay_flightasync_on_flight_preservation_eqB18. Unobserved map-chain fusion equivalence with per-stage cutoffschain_specunfused_chainfused_stagesfused_chaininit_unfused_chaininit_fused_stagesinit_fused_chainclear_fused_readyeval_fused_stageseval_fused_chainreading_movedeval_unfused_stageeval_unfused_chain_goeval_unfused_chainrun_fused_chainrun_unfused_chainunready_tagunready_tag_eq_dectag_to_readingopt_readyset_unfused_unreadystages_syncchain_syncinit_chain_syncclear_fused_ready_idempset_unfused_unready_idempreading_moved_tag_reflunfused_false_noopunfused_propagate_unreadyeval_fused_stages_eq_unfusedeval_fused_chain_step_syncrun_fused_chain_syncfused_map_chain_eq_unfusedB19. Static map2 and zip left-to-right status precedencecomb_bindeval_seq_let2eval_map2eval_zipmap2_zip_status_precedence_eqB20. Split bind inner-tick and transient-unready preservationsplit_bind_stateresolve_bound_innersplit_bind_stepsplit_bind_inner_tickssplit_bind_outer_stepssplit_bind_inner_ticks_preservessplit_bind_outer_unready_preservessplit_bind_outer_steps_appsplit_bind_inner_tick_eqB21. CE reentrant step dispatch and n-ary MergeSources precedenceB21a. Reentrant evalDepth step dispatchce_eval_statece_set_depthce_set_trackingce_alloc_nodece_restore_depthwith_eval_depthwith_suspended_runce_memo_stepuntracked_wrapce_bind_return_evalce_restore_depth_eqce_bind_return_eval_staticce_dispatch_and_nested_eval_eqB21b. n-ary right-nested MergeSources precedencesource_chainmerge_readingseval_merged_chainreading_bindeval_seq_chain_keval_seq_chaineq_speceq_chaineq_checksreading_bind_readyeval_seq_chain_k_mergedeq_chain_iff_checksmerge_sources_nary_precedence_eqB22. `AsyncMemo` settled-value cutoff (`#406`), comparer-throw containment, and 1.0 API capability/keyed symmetry (`#417`)async_settle_completedblind_async_settleasync_settled_value_cutoff_and_comparer_throwro_signalro_getro_try_readkeyed_live_try_getkeyed_try_read_existingreadonly_signal_and_keyed_try_symmetrystream_snapshot_delivery
This definition

Ranvier.Pure.Combinators

Closed-window `*On` primitives, reactive combinators, unobserved `map`-chain fusion, and CE step dispatch

This module formalizes and verifies the pure state machines and algebraic refinements (B16–B21) for Ranvier's closed-window *On split-phase computations (createMemoOn*, createAsyncOn*, createStreamOn*), static-source combinators (map, map2, zip, bind), single-consumer unobserved map-chain fusion (MapFusion), and the Ranvier.CE computation-expression steps that dispatch to them:

  1. B16 (memo_on_cutoff_eq): Two-phase (read, compute) surfaces Phase 1 CPending/CFailed readings while holding (mo_has_key, mo_last_key, mo_phase2_out, mo_scope_gen) intact, restores mo_phase2_out with 0 Phase 2 invocations and unchanged mo_scope_gen when Phase 1 settles to an eq_K-equal key (including after arbitrary transient CPending/CFailed excursions), and increments mo_scope_gen and mo_compute_runs only on initial or eq_K-unequal settled keys.
  2. B17 (async_on_flight_preservation_eq): Across all six FlightPolicy constructors (CancelPrevious, KeepLatest, Queue, FinishCurrent, Drop, DropWhilePending), settling on an eq_K-equal key clears any Phase 1 overlay and ao_owed_trailing while preserving ao_flight_gen, ao_queued, ao_scope_gen, and ao_phase2_slot; Phase 1 CPending/CFailed uniformly sets ao_phase1_overlay without mutating ao_has_key, ao_last_key, ao_flight_gen, ao_queued, ao_fifo, ao_phase2_slot, or ao_owed_trailing; unequal settled CReady k' while ao_queued <> 0 under FinishCurrent/Drop/DropWhilePending sets ao_queued = 2 without clobbering ao_last_key (with owed faulted settlement resetting ao_has_key := false to launch a fresh flight even on K1 -> K2 -> K1, and owed FinishCurrent RVal settlement holding ao_owed_trailing := true until CReady settles); background flight completion during an active Phase 1 unready overlay updates ao_phase2_slot and surfaces immediately once Phase 1 recovers to ao_last_key; and Queue drains out-of-order completions in FIFO start order.
  3. B18 (fused_map_chain_eq_unfused): Over heterogeneous $k$-stage map chains with arbitrary boolean stage cutoffs eq_i (no Leibniz equality requirement) and explicit state St (capturing allocation counters and heap-identity tokens), fused evaluation with per-stage cached ready values and unready cache resets produces the exact same (St * comb_reading B) and origin as sequential unfused Memo.map evaluation across CPending, upstream CFailed, stage throw at index i, intermediate cutoff early-exit at stage i, and arbitrary multi-step input traces.
  4. B19 (map2_zip_status_precedence_eq): Static two-input eval_map2 (both-ready fast path plus left-to-right unready precedence) and eval_zip match sequential monadic let! x = r1 in let! y = r2 in f x y across all 3 * 3 comb_reading pairs while preserving St, failure origin, and static two-slot upstream edges.
  5. B20 (split_bind_inner_tick_eq): Split bind invokes binder and discharges the previous outer scope if and only if outer settles to a new eq_K-unequal key, refreshes the bound inner source across arbitrary inner U transitions with 0 binder invocations and 0 scope discharges, and preserves the outer key, scope, and inner link across transient outer CPending/CFailed excursions recovering to an eq_K-equal key.
  6. B21a (ce_dispatch_and_nested_eval_eq): with_eval_depth and with_suspended_run return ce_depth to its entry value on every RVal/RThrow outcome; BindReturn at depth 0 constructs one static node and leaves its callbacks unevaluated, and at depth S d evaluates inline with a tracked Phase 1 read, an untracked callback, and zero node allocations of its own.
  7. B21b (merge_sources_nary_precedence_eq): Right-nested MergeSources over $n \ge 2$ sources matches the sequential let! chain with left-to-right CPending/CFailed precedence and exact origin, and its nested struct-tuple key cuts off iff every component predicate holds.
3
From stdpp Require Import prelude.
From Ranvier.Base Require Import Prelude.
Open Scope nat_scope.

Outcome of reading a reactive source or derived node, carrying the pending source id on suspension and both the exception id and ErrorOrigin node id on failure.

Inductive comb_reading (V : Type) : Type :=
  | CReady (v : V)
  | CPending (src : nat)
  | CFailed (ex : nat) (origin : nat).
References

Uses

  • nat
Arguments CReady {V} _.
Arguments CPending {V} _.
Arguments CFailed {V} _ _.

Outcome of a synchronous closed-window callback (compute, mapping, or binder).

Inductive result (V : Type) : Type :=
  | RVal (v : V)
  | RThrow (ex : nat).
References

Uses

  • nat
Arguments RVal {V} _.
Arguments RThrow {V} _.

True when a comb_reading is CPending or CFailed.

Definition is_unready_reading {V : Type} (r : comb_reading V) : bool :=
  match r with
  | CReady _ => false
  | CPending _ | CFailed _ _ => true
  end.
References

Converts a callback result on node self_id into a comb_reading attributing throws to self_id.

Definition result_to_reading {V : Type} (self_id : nat) (res : result V) : comb_reading V :=
  match res with
  | RVal v => CReady v
  | RThrow ex => CFailed ex self_id
  end.
References

Projects the unready reading across value types when is_unready_reading r = true.

Definition project_unready {A B : Type} (r : comb_reading A) (fallback : comb_reading B) : comb_reading B :=
  match r with
  | CReady _ => fallback
  | CPending src => CPending src
  | CFailed ex orig => CFailed ex orig
  end.
References

B16. Closed-window MemoOn settled-key cutoff and transient-unready preservation

Pure state of a two-phase createMemoOn / createMemoWithOn node.

Record memo_on_state (K U : Type) := {
  mo_has_key : bool;
  mo_last_key : K;
  mo_phase2_out : comb_reading U;
  mo_visible : comb_reading U;
  mo_scope_gen : nat;
  mo_compute_runs : nat;
}.
References

Uses

Arguments Build_memo_on_state {K U} _ _ _ _ _ _.
Arguments mo_has_key {K U} _.
Arguments mo_last_key {K U} _.
Arguments mo_phase2_out {K U} _.
Arguments mo_visible {K U} _.
Arguments mo_scope_gen {K U} _.
Arguments mo_compute_runs {K U} _.

Single-step transition of createMemoOn / createMemoWithOn on Phase 1 reading r1.

Definition memo_on_step {K U : Type}
    (self_id : nat) (eq_K : K -> K -> bool) (compute : K -> result U)
    (r1 : comb_reading K) (st : memo_on_state K U) : memo_on_state K U :=
  match r1 with
  | CPending src =>
      {| mo_has_key := mo_has_key st;
         mo_last_key := mo_last_key st;
         mo_phase2_out := mo_phase2_out st;
         mo_visible := CPending src;
         mo_scope_gen := mo_scope_gen st;
         mo_compute_runs := mo_compute_runs st |}
  | CFailed ex orig =>
      {| mo_has_key := mo_has_key st;
         mo_last_key := mo_last_key st;
         mo_phase2_out := mo_phase2_out st;
         mo_visible := CFailed ex orig;
         mo_scope_gen := mo_scope_gen st;
         mo_compute_runs := mo_compute_runs st |}
  | CReady k =>
      if mo_has_key st && eq_K (mo_last_key st) k then
        {| mo_has_key := true;
           mo_last_key := mo_last_key st;
           mo_phase2_out := mo_phase2_out st;
           mo_visible := mo_phase2_out st;
           mo_scope_gen := mo_scope_gen st;
           mo_compute_runs := mo_compute_runs st |}
      else
        let out := result_to_reading self_id (compute k) in
        {| mo_has_key := true;
           mo_last_key := k;
           mo_phase2_out := out;
           mo_visible := out;
           mo_scope_gen := S (mo_scope_gen st);
           mo_compute_runs := S (mo_compute_runs st) |}
  end.
References

Multi-step iteration of memo_on_step over a sequence of Phase 1 readings.

Fixpoint memo_on_steps {K U : Type}
    (self_id : nat) (eq_K : K -> K -> bool) (compute : K -> result U)
    (rs : list (comb_reading K)) (st : memo_on_state K U) : memo_on_state K U :=
  match rs with
  | [] => st
  | r :: rest => memo_on_steps self_id eq_K compute rest (memo_on_step self_id eq_K compute r st)
  end.
References
Lemma memo_on_steps_unready_preserves {K U : Type}
    (self_id : nat) (eq_K : K -> K -> bool) (compute : K -> result U)
    (us : list (comb_reading K)) (st : memo_on_state K U) :
  Forall (fun r => is_unready_reading r = true) us ->
  let st' := memo_on_steps self_id eq_K compute us st in
  mo_has_key st' = mo_has_key st /\
  mo_last_key st' = mo_last_key st /\
  mo_phase2_out st' = mo_phase2_out st /\
  mo_scope_gen st' = mo_scope_gen st /\
  mo_compute_runs st' = mo_compute_runs st.
Qed
Proof.
  revert st. induction us as [|r rest IH]; intros st Hall; simpl.
  - repeat split; reflexivity.
  - inversion Hall as [|? ? Hr Hrest]; subst.
    destruct r as [v|src|ex orig]; [simpl in Hr; discriminate| |];
      exact (IH _ Hrest).
Qed.
References
Lemma memo_on_steps_app {K U : Type}
    (self_id : nat) (eq_K : K -> K -> bool) (compute : K -> result U)
    (rs1 rs2 : list (comb_reading K)) (st : memo_on_state K U) :
  memo_on_steps self_id eq_K compute (rs1 ++ rs2) st =
  memo_on_steps self_id eq_K compute rs2 (memo_on_steps self_id eq_K compute rs1 st).
Qed
Proof.
  revert st. induction rs1 as [|r rest IH]; intros st; simpl; [reflexivity|apply IH].
Qed.
References
✓ No axioms

Two-phase memo_on_step surfaces Phase 1 CPending/CFailed while preserving (mo_has_key, mo_last_key, mo_phase2_out, mo_scope_gen), restores mo_phase2_out with zero Phase 2 runs on eq_K-equal settled keys (even after arbitrary unready excursions), and increments mo_scope_gen and mo_compute_runs only on initial or eq_K-unequal settled keys.

Matches createMemoOn, createMemoOnWithComparer, createMemoWithOn, and createOwningMemoOnWithComparer (Core.fs, Api.fs): Phase 1 unready readings never clobber the Phase 2 key or discharge its owned scope, equal settled keys restore the cached Phase 2 reading (CReady or Phase 2 CFailed), and unequal settled keys discharge the prior scope generation and invoke compute once.

Arguments

K : Type
Phase 1 key type.
U : Type
Phase 2 output value type.
self_id : nat
node id of the Memo, used as origin if compute throws.
eq_K : K -> K -> bool
boolean cutoff predicate over settled K keys.
compute : K -> result U
closed-window Phase 2 body.
single-step Phase 1 reading.
k : K
settled Phase 1 key.
us : list (comb_reading K)
transient Phase 1 unready excursion trace.
st : memo_on_state K U
state before the step or excursion.
Theorem memo_on_cutoff_eq (K U : Type)
    (self_id : nat) (eq_K : K -> K -> bool) (compute : K -> result U)
    (r1 : comb_reading K) (k : K) (us : list (comb_reading K)) (st : memo_on_state K U) :
  (is_unready_reading r1 = true ->
     let st' := memo_on_step self_id eq_K compute r1 st in
     mo_visible st' = project_unready r1 (mo_visible st) /\
     mo_has_key st' = mo_has_key st /\
     mo_last_key st' = mo_last_key st /\
     mo_phase2_out st' = mo_phase2_out st /\
     mo_scope_gen st' = mo_scope_gen st /\
     mo_compute_runs st' = mo_compute_runs st) /\
  (mo_has_key st = true -> eq_K (mo_last_key st) k = true ->
     Forall (fun r => is_unready_reading r = true) us ->
     let st' := memo_on_steps self_id eq_K compute (us ++ [CReady k]) st in
     mo_visible st' = mo_phase2_out st /\
     mo_phase2_out st' = mo_phase2_out st /\
     mo_has_key st' = true /\
     mo_last_key st' = mo_last_key st /\
     mo_scope_gen st' = mo_scope_gen st /\
     mo_compute_runs st' = mo_compute_runs st) /\
  (mo_has_key st = false \/ eq_K (mo_last_key st) k = false ->
     let st' := memo_on_step self_id eq_K compute (CReady k) st in
     mo_has_key st' = true /\
     mo_last_key st' = k /\
     mo_phase2_out st' = result_to_reading self_id (compute k) /\
     mo_visible st' = result_to_reading self_id (compute k) /\
     mo_scope_gen st' = S (mo_scope_gen st) /\
     mo_compute_runs st' = S (mo_compute_runs st)).

Arguments

K : Type
Phase 1 key type.
U : Type
Phase 2 output value type.
self_id : nat
node id of the Memo, used as origin if compute throws.
eq_K : K -> K -> bool
boolean cutoff predicate over settled K keys.
compute : K -> result U
closed-window Phase 2 body.
single-step Phase 1 reading.
k : K
settled Phase 1 key.
us : list (comb_reading K)
transient Phase 1 unready excursion trace.
st : memo_on_state K U
state before the step or excursion.
Qed
Proof.
  split_and!.
  - intros Hunr. destruct r1 as [v|src|ex orig]; [simpl in Hunr; discriminate| |];
      simpl; repeat split; reflexivity.
  - intros Hhas Heq Hall.
    rewrite memo_on_steps_app. simpl.
    pose proof (memo_on_steps_unready_preserves self_id eq_K compute us st Hall)
      as (Hhk & Hlk & Hp2 & Hsg & Hcr).
    unfold memo_on_step.
    rewrite Hhk, Hlk, Hhas, Heq. simpl.
    rewrite Hp2, Hsg, Hcr.
    repeat split; reflexivity.
  - intros Hnew. unfold memo_on_step.
    destruct Hnew as [Hno | Hneq].
    + rewrite Hno. simpl. repeat split; reflexivity.
    + rewrite Hneq, andb_false_r. repeat split; reflexivity.
Qed.
References

B17. Closed-window AsyncOn six-policy flight preservation

All six FlightPolicy constructors of Ranvier.FlightPolicy (Types.fs).

Inductive flight_policy : Type :=
  | CancelPrevious
  | KeepLatest
  | Queue
  | FinishCurrent
  | Drop
  | DropWhilePending.
References
Global Instance flight_policy_eq_dec : EqDecision flight_policy.
Defined
Proof. solve_decision. Defined.
References

Uses

Policies whose active flights defer or discard intervening changes via queued = 2 (AsyncMemo.StartOnFrom in Core.fs).

Definition is_deferring_policy (p : flight_policy) : bool :=
  match p with
  | FinishCurrent | Drop | DropWhilePending => true
  | CancelPrevious | KeepLatest | Queue => false
  end.
References

Pure state of a two-phase AsyncMemo *On node across all six flight policies.

Record async_on_state (K U : Type) := {
  ao_policy : flight_policy;
  ao_self_id : nat;
  ao_has_key : bool;
  ao_last_key : K;
  ao_flight_gen : nat;
  ao_queued : nat;
  ao_flying : nat;
  ao_scope_gen : nat;
  ao_phase2_slot : comb_reading U;
  ao_phase1_overlay : option (comb_reading U);
  ao_fifo : list (nat * option (result U));
  ao_owed_trailing : bool;
}.
References

Uses

Arguments Build_async_on_state {K U} _ _ _ _ _ _ _ _ _ _ _ _.
Arguments ao_policy {K U} _.
Arguments ao_self_id {K U} _.
Arguments ao_has_key {K U} _.
Arguments ao_last_key {K U} _.
Arguments ao_flight_gen {K U} _.
Arguments ao_queued {K U} _.
Arguments ao_flying {K U} _.
Arguments ao_scope_gen {K U} _.
Arguments ao_phase2_slot {K U} _.
Arguments ao_phase1_overlay {K U} _.
Arguments ao_fifo {K U} _.
Arguments ao_owed_trailing {K U} _.

Externally visible reading of an AsyncMemo *On node: Phase 1 overlay when active, CPending ao_self_id while an owed FinishCurrent trailing run is pending (AsyncMemo.SyncVisibleOn in Core.fs), otherwise Phase 2's slot.

Definition ao_visible {K U : Type} (st : async_on_state K U) : comb_reading U :=
  match ao_phase1_overlay st with
  | Some r1 => r1
  | None =>
      if ao_owed_trailing st then CPending (ao_self_id st)
      else ao_phase2_slot st
  end.
References

Launches a new untracked Phase 2 flight for settled key k, advancing ao_flight_gen and ao_scope_gen and clearing ao_phase1_overlay and ao_owed_trailing.

Definition async_on_launch {K U : Type} (k : K) (st : async_on_state K U) : async_on_state K U :=
  let next_gen := S (ao_flight_gen st) in
  let '(next_queued, next_flying, next_fifo) :=
    match ao_policy st with
    | CancelPrevious => (0, 1, [])
    | KeepLatest => (0, S (ao_flying st), [])
    | Queue => (S (ao_queued st), 0, ao_fifo st ++ [(next_gen, None)])
    | FinishCurrent | Drop | DropWhilePending => (1, 0, [])
    end in
  {| ao_policy := ao_policy st;
     ao_self_id := ao_self_id st;
     ao_has_key := true;
     ao_last_key := k;
     ao_flight_gen := next_gen;
     ao_queued := next_queued;
     ao_flying := next_flying;
     ao_scope_gen := S (ao_scope_gen st);
     ao_phase2_slot := CPending (ao_self_id st);
     ao_phase1_overlay := None;
     ao_fifo := next_fifo;
     ao_owed_trailing := false |}.
References

Phase 1 refresh step of createAsyncOn* across all six FlightPolicy constructors: Phase 1 CPending and CFailed uniformly set ao_phase1_overlay without mutating Phase 2 flight state.

Definition async_on_step {K U : Type}
    (eq_K : K -> K -> bool) (r1 : comb_reading K) (st : async_on_state K U)
    : async_on_state K U :=
  match r1 with
  | CReady k =>
      if ao_has_key st && eq_K (ao_last_key st) k then
        {| ao_policy := ao_policy st;
           ao_self_id := ao_self_id st;
           ao_has_key := ao_has_key st;
           ao_last_key := ao_last_key st;
           ao_flight_gen := ao_flight_gen st;
           ao_queued := ao_queued st;
           ao_flying := ao_flying st;
           ao_scope_gen := ao_scope_gen st;
           ao_phase2_slot := ao_phase2_slot st;
           ao_phase1_overlay := None;
           ao_fifo := ao_fifo st;
           ao_owed_trailing := false |}
      else if is_deferring_policy (ao_policy st) && negb (Nat.eqb (ao_queued st) 0) then
        {| ao_policy := ao_policy st;
           ao_self_id := ao_self_id st;
           ao_has_key := ao_has_key st;
           ao_last_key := ao_last_key st;
           ao_flight_gen := ao_flight_gen st;
           ao_queued := 2;
           ao_flying := ao_flying st;
           ao_scope_gen := ao_scope_gen st;
           ao_phase2_slot := ao_phase2_slot st;
           ao_phase1_overlay := None;
           ao_fifo := ao_fifo st;
           ao_owed_trailing := ao_owed_trailing st |}
      else
        async_on_launch k st
  | CPending src =>
      {| ao_policy := ao_policy st;
         ao_self_id := ao_self_id st;
         ao_has_key := ao_has_key st;
         ao_last_key := ao_last_key st;
         ao_flight_gen := ao_flight_gen st;
         ao_queued := ao_queued st;
         ao_flying := ao_flying st;
         ao_scope_gen := ao_scope_gen st;
         ao_phase2_slot := ao_phase2_slot st;
         ao_phase1_overlay := Some (CPending src);
         ao_fifo := ao_fifo st;
         ao_owed_trailing := ao_owed_trailing st |}
  | CFailed ex orig =>
      {| ao_policy := ao_policy st;
         ao_self_id := ao_self_id st;
         ao_has_key := ao_has_key st;
         ao_last_key := ao_last_key st;
         ao_flight_gen := ao_flight_gen st;
         ao_queued := ao_queued st;
         ao_flying := ao_flying st;
         ao_scope_gen := ao_scope_gen st;
         ao_phase2_slot := ao_phase2_slot st;
         ao_phase1_overlay := Some (CFailed ex orig);
         ao_fifo := ao_fifo st;
         ao_owed_trailing := ao_owed_trailing st |}
  end.
References

Records a settled flight outcome in Queue's start-ordered FIFO buffer.

Fixpoint fifo_record {U : Type} (gen : nat) (out : result U)
    (q : list (nat * option (result U))) : list (nat * option (result U)) :=
  match q with
  | [] => []
  | (g, cur) :: rest =>
      if Nat.eqb g gen then (g, Some out) :: rest
      else (g, cur) :: fifo_record gen out rest
  end.
References

Uses

  • result
  • Some
  • list
  • nat
  • option
  • Nat.eqb

Drains consecutive settled head entries under FlightPolicy.Queue (AsyncMemo.ApplyQueued in Core.fs).

Fixpoint fifo_drain {U : Type} (self_id : nat) (queued : nat) (slot : comb_reading U)
    (q : list (nat * option (result U)))
    : nat * comb_reading U * list (nat * option (result U)) :=
  match q with
  | (_, Some out) :: rest =>
      fifo_drain self_id (Nat.pred queued) (result_to_reading self_id out) rest
  | _ => (queued, slot, q)
  end.
References

Uses

Background Phase 2 flight completion (AsyncMemo.ApplyResult / AsyncMemo.ApplyQueued in Core.fs), updating ao_phase2_slot without clearing any active Phase 1 unready overlay, resetting ao_has_key := false when an owed (ao_queued = 2) faulted flight is dropped under FinishCurrent or DropWhilePending, and setting ao_owed_trailing := true when an owed FinishCurrent flight completes with RVal.

Definition async_on_settle {K U : Type}
    (gen : nat) (out : result U) (st : async_on_state K U) : async_on_state K U :=
  let settled := result_to_reading (ao_self_id st) out in
  match ao_policy st with
  | CancelPrevious | KeepLatest =>
      let next_slot :=
        if Nat.eqb gen (ao_flight_gen st) then settled else ao_phase2_slot st in
      {| ao_policy := ao_policy st;
         ao_self_id := ao_self_id st;
         ao_has_key := ao_has_key st;
         ao_last_key := ao_last_key st;
         ao_flight_gen := ao_flight_gen st;
         ao_queued := ao_queued st;
         ao_flying := Nat.pred (ao_flying st);
         ao_scope_gen := ao_scope_gen st;
         ao_phase2_slot := next_slot;
         ao_phase1_overlay := ao_phase1_overlay st;
         ao_fifo := ao_fifo st;
         ao_owed_trailing := ao_owed_trailing st |}
  | Queue =>
      let '(q', slot', fifo') :=
        fifo_drain (ao_self_id st) (ao_queued st) (ao_phase2_slot st)
          (fifo_record gen out (ao_fifo st)) in
      {| ao_policy := Queue;
         ao_self_id := ao_self_id st;
         ao_has_key := ao_has_key st;
         ao_last_key := ao_last_key st;
         ao_flight_gen := ao_flight_gen st;
         ao_queued := q';
         ao_flying := ao_flying st;
         ao_scope_gen := ao_scope_gen st;
         ao_phase2_slot := slot';
         ao_phase1_overlay := ao_phase1_overlay st;
         ao_fifo := fifo';
         ao_owed_trailing := ao_owed_trailing st |}
  | FinishCurrent =>
      let owed := Nat.eqb (ao_queued st) 2 in
      let '(next_has_key, next_slot, next_owed_trailing) :=
        if Nat.eqb gen (ao_flight_gen st) then
          if owed then
            match out with
            | RVal u => (ao_has_key st, CReady u, true)
            | RThrow _ => (false, CPending (ao_self_id st), false)
            end
          else (ao_has_key st, settled, false)
        else (ao_has_key st, ao_phase2_slot st, ao_owed_trailing st) in
      {| ao_policy := FinishCurrent;
         ao_self_id := ao_self_id st;
         ao_has_key := next_has_key;
         ao_last_key := ao_last_key st;
         ao_flight_gen := ao_flight_gen st;
         ao_queued := 0;
         ao_flying := ao_flying st;
         ao_scope_gen := ao_scope_gen st;
         ao_phase2_slot := next_slot;
         ao_phase1_overlay := ao_phase1_overlay st;
         ao_fifo := ao_fifo st;
         ao_owed_trailing := next_owed_trailing |}
  | Drop =>
      let next_slot :=
        if Nat.eqb gen (ao_flight_gen st) then settled else ao_phase2_slot st in
      {| ao_policy := Drop;
         ao_self_id := ao_self_id st;
         ao_has_key := ao_has_key st;
         ao_last_key := ao_last_key st;
         ao_flight_gen := ao_flight_gen st;
         ao_queued := 0;
         ao_flying := ao_flying st;
         ao_scope_gen := ao_scope_gen st;
         ao_phase2_slot := next_slot;
         ao_phase1_overlay := ao_phase1_overlay st;
         ao_fifo := ao_fifo st;
         ao_owed_trailing := ao_owed_trailing st |}
  | DropWhilePending =>
      let owed :=
        match out with
        | RVal _ => false
        | RThrow _ => Nat.eqb (ao_queued st) 2
        end in
      let '(next_has_key, next_slot) :=
        if Nat.eqb gen (ao_flight_gen st) then
          if owed then (false, CPending (ao_self_id st))
          else (ao_has_key st, settled)
        else (ao_has_key st, ao_phase2_slot st) in
      {| ao_policy := DropWhilePending;
         ao_self_id := ao_self_id st;
         ao_has_key := next_has_key;
         ao_last_key := ao_last_key st;
         ao_flight_gen := ao_flight_gen st;
         ao_queued := 0;
         ao_flying := ao_flying st;
         ao_scope_gen := ao_scope_gen st;
         ao_phase2_slot := next_slot;
         ao_phase1_overlay := ao_phase1_overlay st;
         ao_fifo := ao_fifo st;
         ao_owed_trailing := ao_owed_trailing st |}
  end.
References

Genuinely active in-flight precondition across all six FlightPolicy modes when Phase 1 enters an unready overlay.

Definition active_overlay_flight {K U : Type} (st : async_on_state K U) : Prop :=
  ao_owed_trailing st = false /\
  match ao_policy st with
  | CancelPrevious | KeepLatest =>
      ao_flying st > 0 /\ ao_phase2_slot st = CPending (ao_self_id st)
  | Queue =>
      ao_queued st = 1 /\
      ao_fifo st = [(ao_flight_gen st, None)] /\
      ao_phase2_slot st = CPending (ao_self_id st)
  | FinishCurrent | Drop | DropWhilePending =>
      ao_queued st = 1 /\ ao_phase2_slot st = CPending (ao_self_id st)
  end.
References
✓ No axioms

Across all six FlightPolicy constructors, async_on_step preserves Phase 2 state on eq_K-equal settled keys and Phase 1 CPending/CFailed overlays, sets ao_queued = 2 strictly on unequal settled keys while ao_queued <> 0 under FinishCurrent/Drop/DropWhilePending (resetting ao_has_key = false on owed faulted drops and holding ao_owed_trailing = true on owed FinishCurrent RVal completions), surfaces background completions across all six active policies once Phase 1 recovers, and drains Queue in FIFO start order.

Certifies the two-phase AsyncMemo *On engine (createAsyncOn, createAsyncWithOn, createAsyncOnWithPolicy, createOwningAsyncOnWithPolicy, AsyncMemo.bind*, and AsyncSource.bind):

  1. Equal settled keys clear ao_phase1_overlay and ao_owed_trailing while preserving ao_flight_gen, ao_queued, ao_scope_gen, ao_last_key, and ao_phase2_slot.
  2. Phase 1 CPending/CFailed readings uniformly set ao_phase1_overlay across all six policies without mutating ao_has_key, ao_last_key, ao_flight_gen, ao_queued, ao_fifo, ao_phase2_slot, or ao_owed_trailing.
  3. Unequal settled CReady k' during an active deferring flight (ao_queued <> 0 under FinishCurrent/Drop/DropWhilePending) sets ao_queued = 2 while retaining ao_last_key and ao_flight_gen.
  4. Under active_overlay_flight st (active in-flight state across all six policies, covering both CPending and CFailed Phase 1 overlays), background settlement updates ao_phase2_slot behind the overlay and surfaces immediately with zero new launches once Phase 1 settles back to an eq_K-equal key.
  5. When an owed (ao_queued = 2) flight faults under FinishCurrent or DropWhilePending, async_on_settle resets ao_has_key = false so async_on_step on any CReady k (even k = ao_last_key) launches a fresh flight (ao_queued = 1, ao_flight_gen = S gen); when an owed FinishCurrent flight completes with RVal u, ao_phase2_slot stores CReady u while ao_owed_trailing = true holds ao_visible = CPending ao_self_id until async_on_step on eq_K-equal CReady k clears ao_owed_trailing and surfaces CReady u with zero new launches.
  6. Under Queue, out-of-order completions g2 then g1 (g1 <> g2) hold ao_phase2_slot until g1 arrives and apply out1 then out2 in start order.

Arguments

K : Type
Phase 1 key type.
U : Type
Phase 2 async result type.
eq_K : K -> K -> bool
boolean cutoff predicate over settled K keys.
k : K
settled Phase 1 key.
k' : K
unequal settled Phase 1 key arriving during an active deferring flight.
r_unr : comb_reading K
Phase 1 unready reading (CPending or CFailed).
u : U
settled RVal u payload for owed FinishCurrent completion.
ex : nat
exception id for owed RThrow ex faulted drop.
out : result U
background flight completion outcome during a Phase 1 overlay.
out1 : result U
outcome of earlier queued flight g1.
out2 : result U
outcome of later queued flight g2.
g1 : nat
generation of first queued flight under Queue.
g2 : nat
generation of second queued flight under Queue.
async_on_state before the step.
Theorem async_on_flight_preservation_eq (K U : Type)
    (eq_K : K -> K -> bool) (k k' : K)
    (r_unr : comb_reading K)
    (u : U) (ex : nat)
    (out out1 out2 : result U) (g1 g2 : nat)
    (st : async_on_state K U) :
  (ao_has_key st = true -> eq_K (ao_last_key st) k = true ->
     let st' := async_on_step eq_K (CReady k) st in
     ao_flight_gen st' = ao_flight_gen st /\
     ao_queued st' = ao_queued st /\
     ao_scope_gen st' = ao_scope_gen st /\
     ao_last_key st' = ao_last_key st /\
     ao_phase2_slot st' = ao_phase2_slot st /\
     ao_phase1_overlay st' = None /\
     ao_owed_trailing st' = false /\
     ao_visible st' = ao_phase2_slot st) /\
  (is_unready_reading r_unr = true ->
     let st' := async_on_step eq_K r_unr st in
     ao_phase1_overlay st' = Some (project_unready r_unr (ao_phase2_slot st)) /\
     ao_visible st' = project_unready r_unr (ao_phase2_slot st) /\
     ao_has_key st' = ao_has_key st /\
     ao_last_key st' = ao_last_key st /\
     ao_flight_gen st' = ao_flight_gen st /\
     ao_queued st' = ao_queued st /\
     ao_fifo st' = ao_fifo st /\
     ao_phase2_slot st' = ao_phase2_slot st /\
     ao_owed_trailing st' = ao_owed_trailing st) /\
  (is_deferring_policy (ao_policy st) = true -> ao_queued st <> 0 ->
     (ao_has_key st = false \/ eq_K (ao_last_key st) k' = false) ->
     let st' := async_on_step eq_K (CReady k') st in
     ao_queued st' = 2 /\
     ao_last_key st' = ao_last_key st /\
     ao_has_key st' = ao_has_key st /\
     ao_flight_gen st' = ao_flight_gen st /\
     ao_scope_gen st' = ao_scope_gen st /\
     ao_phase2_slot st' = ao_phase2_slot st /\
     ao_phase1_overlay st' = None /\
     ao_owed_trailing st' = ao_owed_trailing st) /\
  (ao_has_key st = true -> eq_K (ao_last_key st) k = true ->
     is_unready_reading r_unr = true ->
     active_overlay_flight st ->
     let st_ov := async_on_step eq_K r_unr st in
     let st_bg := async_on_settle (ao_flight_gen st) out st_ov in
     let st_rec := async_on_step eq_K (CReady k) st_bg in
     ao_visible st_bg = project_unready r_unr (ao_phase2_slot st) /\
     ao_phase2_slot st_bg = result_to_reading (ao_self_id st) out /\
     ao_visible st_rec = result_to_reading (ao_self_id st) out /\
     ao_flight_gen st_rec = ao_flight_gen st /\
     ao_scope_gen st_rec = ao_scope_gen st /\
     ao_last_key st_rec = ao_last_key st) /\
  (((ao_policy st = FinishCurrent \/ ao_policy st = DropWhilePending) ->
     ao_queued st = 2 ->
     let st_drop := async_on_settle (ao_flight_gen st) (RThrow ex) st in
     let st_re := async_on_step eq_K (CReady k) st_drop in
     ao_has_key st_drop = false /\
     ao_queued st_drop = 0 /\
     ao_has_key st_re = true /\
     ao_last_key st_re = k /\
     ao_queued st_re = 1 /\
     ao_flight_gen st_re = S (ao_flight_gen st) /\
     ao_scope_gen st_re = S (ao_scope_gen st)) /\
   (ao_policy st = FinishCurrent ->
     ao_has_key st = true -> eq_K (ao_last_key st) k = true ->
     ao_phase1_overlay st = None -> ao_queued st = 2 ->
     let st_hold := async_on_settle (ao_flight_gen st) (RVal u) st in
     let st_done := async_on_step eq_K (CReady k) st_hold in
     ao_phase2_slot st_hold = CReady u /\
     ao_owed_trailing st_hold = true /\
     ao_visible st_hold = CPending (ao_self_id st) /\
     ao_owed_trailing st_done = false /\
     ao_visible st_done = CReady u /\
     ao_flight_gen st_done = ao_flight_gen st /\
     ao_queued st_done = 0)) /\
  (ao_policy st = Queue -> g1 <> g2 ->
     ao_queued st = 2 -> ao_fifo st = [(g1, None); (g2, None)] ->
     let st_early := async_on_settle g2 out2 st in
     let st_both := async_on_settle g1 out1 st_early in
     ao_phase2_slot st_early = ao_phase2_slot st /\
     ao_queued st_early = 2 /\
     ao_queued st_both = 0 /\
     ao_fifo st_both = [] /\
     ao_phase2_slot st_both = result_to_reading (ao_self_id st) out2).

Arguments

K : Type
Phase 1 key type.
U : Type
Phase 2 async result type.
eq_K : K -> K -> bool
boolean cutoff predicate over settled K keys.
k : K
settled Phase 1 key.
k' : K
unequal settled Phase 1 key arriving during an active deferring flight.
r_unr : comb_reading K
Phase 1 unready reading (CPending or CFailed).
u : U
settled RVal u payload for owed FinishCurrent completion.
ex : nat
exception id for owed RThrow ex faulted drop.
out : result U
background flight completion outcome during a Phase 1 overlay.
out1 : result U
outcome of earlier queued flight g1.
out2 : result U
outcome of later queued flight g2.
g1 : nat
generation of first queued flight under Queue.
g2 : nat
generation of second queued flight under Queue.
async_on_state before the step.
Qed
Proof.
  split_and!.
  - intros Hhas Heq.
    destruct (ao_policy st); unfold async_on_step, ao_visible;
      rewrite Hhas, Heq; simpl; repeat split; reflexivity.
  - intros Hunr.
    destruct r_unr as [v|src|ex0 orig0]; [simpl in Hunr; discriminate| |];
      unfold async_on_step, ao_visible; simpl; repeat split; reflexivity.
  - intros Hdef Hq Hnew.
    assert (Hq_nz : negb (Nat.eqb (ao_queued st) 0) = true).
    { destruct (Nat.eqb (ao_queued st) 0) eqn:Eq0; [|reflexivity].
      apply Nat.eqb_eq in Eq0. contradiction. }
    unfold async_on_step.
    destruct Hnew as [Hno | Hneq];
      [rewrite Hno | rewrite Hneq, andb_false_r];
      rewrite Hdef, Hq_nz; simpl; repeat split; reflexivity.
  - intros Hhas Heq Hunr [Hot Hact].
    assert (Hov :
      async_on_step eq_K r_unr st =
      {| ao_policy := ao_policy st;
         ao_self_id := ao_self_id st;
         ao_has_key := ao_has_key st;
         ao_last_key := ao_last_key st;
         ao_flight_gen := ao_flight_gen st;
         ao_queued := ao_queued st;
         ao_flying := ao_flying st;
         ao_scope_gen := ao_scope_gen st;
         ao_phase2_slot := ao_phase2_slot st;
         ao_phase1_overlay := Some (project_unready r_unr (ao_phase2_slot st));
         ao_fifo := ao_fifo st;
         ao_owed_trailing := false |})
      by (destruct r_unr as [v|src|ex0 orig0]; [simpl in Hunr; discriminate| |];
          unfold async_on_step; rewrite Hot; reflexivity).
    cbv zeta; rewrite Hov.
    destruct (ao_policy st) eqn:Epol; simpl in Hact.
    + cbn [async_on_settle ao_policy ao_flight_gen ao_self_id ao_has_key ao_last_key ao_queued ao_flying ao_scope_gen ao_phase2_slot ao_phase1_overlay ao_fifo ao_owed_trailing].
      rewrite Nat.eqb_refl.
      cbn [async_on_step ao_visible ao_has_key ao_last_key ao_phase1_overlay ao_phase2_slot ao_flight_gen ao_scope_gen ao_owed_trailing].
      rewrite Hhas, Heq; simpl; repeat split; reflexivity.
    + cbn [async_on_settle ao_policy ao_flight_gen ao_self_id ao_has_key ao_last_key ao_queued ao_flying ao_scope_gen ao_phase2_slot ao_phase1_overlay ao_fifo ao_owed_trailing].
      rewrite Nat.eqb_refl.
      cbn [async_on_step ao_visible ao_has_key ao_last_key ao_phase1_overlay ao_phase2_slot ao_flight_gen ao_scope_gen ao_owed_trailing].
      rewrite Hhas, Heq; simpl; repeat split; reflexivity.
    + destruct Hact as (Hq1 & Hfifo & _).
      rewrite Hq1, Hfifo.
      cbn [async_on_settle fifo_record ao_policy ao_flight_gen ao_self_id ao_has_key ao_last_key ao_queued ao_flying ao_scope_gen ao_phase2_slot ao_phase1_overlay ao_fifo ao_owed_trailing].
      rewrite Nat.eqb_refl; cbn [fifo_drain Nat.pred].
      cbn [async_on_step ao_visible ao_has_key ao_last_key ao_phase1_overlay ao_phase2_slot ao_flight_gen ao_scope_gen ao_owed_trailing].
      rewrite Hhas, Heq; simpl; repeat split; reflexivity.
    + destruct Hact as (Hq1 & _).
      rewrite Hq1.
      cbn [async_on_settle ao_policy ao_flight_gen ao_self_id ao_has_key ao_last_key ao_queued ao_flying ao_scope_gen ao_phase2_slot ao_phase1_overlay ao_fifo ao_owed_trailing].
      rewrite Nat.eqb_refl; simpl.
      cbn [async_on_step ao_visible ao_has_key ao_last_key ao_phase1_overlay ao_phase2_slot ao_flight_gen ao_scope_gen ao_owed_trailing].
      rewrite Hhas, Heq; simpl; repeat split; reflexivity.
    + destruct Hact as (Hq1 & _).
      rewrite Hq1.
      cbn [async_on_settle ao_policy ao_flight_gen ao_self_id ao_has_key ao_last_key ao_queued ao_flying ao_scope_gen ao_phase2_slot ao_phase1_overlay ao_fifo ao_owed_trailing].
      rewrite Nat.eqb_refl; simpl.
      cbn [async_on_step ao_visible ao_has_key ao_last_key ao_phase1_overlay ao_phase2_slot ao_flight_gen ao_scope_gen ao_owed_trailing].
      rewrite Hhas, Heq; simpl; repeat split; reflexivity.
    + destruct Hact as (Hq1 & _).
      rewrite Hq1.
      cbn [async_on_settle ao_policy ao_flight_gen ao_self_id ao_has_key ao_last_key ao_queued ao_flying ao_scope_gen ao_phase2_slot ao_phase1_overlay ao_fifo ao_owed_trailing].
      rewrite Nat.eqb_refl; destruct out; simpl;
      cbn [async_on_step ao_visible ao_has_key ao_last_key ao_phase1_overlay ao_phase2_slot ao_flight_gen ao_scope_gen ao_owed_trailing];
      rewrite Hhas, Heq; simpl; repeat split; reflexivity.
  - intros Hpol Hq2. cbv zeta.
    destruct Hpol as [Hfc | Hdwp].
    + unfold async_on_settle; rewrite Hfc, Hq2, Nat.eqb_refl; simpl.
      unfold async_on_step, async_on_launch; simpl.
      repeat split; reflexivity.
    + unfold async_on_settle; rewrite Hdwp, Hq2, Nat.eqb_refl; simpl.
      unfold async_on_step, async_on_launch; simpl.
      repeat split; reflexivity.
  - intros Hfc Hhas Heq Hov0 Hq2. cbv zeta.
    unfold async_on_settle; rewrite Hfc, Hq2, Hov0, Nat.eqb_refl; simpl.
    unfold async_on_step, ao_visible; simpl.
    rewrite Hhas, Heq; simpl.
    repeat split; reflexivity.
  - intros Hpol Hg12 Hq2 Hfifo.
    cbv zeta. unfold async_on_settle.
    rewrite Hpol, Hq2, Hfifo. simpl.
    assert (Hne12 : Nat.eqb g1 g2 = false) by (apply Nat.eqb_neq; exact Hg12).
    rewrite Hne12, Nat.eqb_refl. simpl.
    rewrite Nat.eqb_refl. simpl.
    repeat split; reflexivity.
Qed.
References

B18. Unobserved map-chain fusion equivalence with per-stage cutoffs

Declarative heterogeneous $k$-stage map specification from A to B over state St.

Inductive chain_spec (St : Type) : Type -> Type -> Type :=
  | CS_Single :
      forall {A B : Type}
        (cs_id : nat)
        (cs_eq : B -> B -> bool)
        (cs_step : St -> A -> St * result B),
        chain_spec St A B
  | CS_Cons :
      forall {A M B : Type}
        (cs_id : nat)
        (cs_eq : M -> M -> bool)
        (cs_step : St -> A -> St * result M),
        chain_spec St M B ->
        chain_spec St A B.
References

Uses

Unfused sequential Memo.map chain state: each stage holds its own cached option (comb_reading M).

Inductive unfused_chain (St : Type) : Type -> Type -> Type :=
  | UC_Single :
      forall {A B : Type}
        (uc_id : nat)
        (uc_eq : B -> B -> bool)
        (uc_cached : option (comb_reading B))
        (uc_step : St -> A -> St * result B),
        unfused_chain St A B
  | UC_Cons :
      forall {A M B : Type}
        (uc_id : nat)
        (uc_eq : M -> M -> bool)
        (uc_cached : option (comb_reading M))
        (uc_step : St -> A -> St * result M),
        unfused_chain St M B ->
        unfused_chain St A B.
References

Uses

Fused MapFusion stage spine: each stage stores only its last settled ready value option M.

Inductive fused_stages (St : Type) : Type -> Type -> Type :=
  | FS_Single :
      forall {A B : Type}
        (fs_id : nat)
        (fs_eq : B -> B -> bool)
        (fs_ready : option B)
        (fs_step : St -> A -> St * result B),
        fused_stages St A B
  | FS_Cons :
      forall {A M B : Type}
        (fs_id : nat)
        (fs_eq : M -> M -> bool)
        (fs_ready : option M)
        (fs_step : St -> A -> St * result M),
        fused_stages St M B ->
        fused_stages St A B.
References

Uses

Arguments CS_Single {St A B} _ _ _.
Arguments CS_Cons {St A M B} _ _ _ _.
Arguments UC_Single {St A B} _ _ _ _.
Arguments UC_Cons {St A M B} _ _ _ _ _.
Arguments FS_Single {St A B} _ _ _ _.
Arguments FS_Cons {St A M B} _ _ _ _ _.

Complete state of a fused MapFusion chain: stage ready-value slots plus the tail's cached reading.

Record fused_chain (St A B : Type) := {
  fc_stages : fused_stages St A B;
  fc_tail_out : option (comb_reading B);
}.
References
Arguments Build_fused_chain {St A B} _ _.
Arguments fc_stages {St A B} _.
Arguments fc_tail_out {St A B} _.

Compiles a chain_spec into its initial unpulled unfused Memo.map chain.

Fixpoint init_unfused_chain {St A B : Type} (spec : chain_spec St A B) : unfused_chain St A B :=
  match spec with
  | CS_Single id eq_b step => UC_Single id eq_b None step
  | CS_Cons id eq_m step rest => UC_Cons id eq_m None step (init_unfused_chain rest)
  end.
References

Compiles a chain_spec into its initial unpulled fused_stages spine.

Fixpoint init_fused_stages {St A B : Type} (spec : chain_spec St A B) : fused_stages St A B :=
  match spec with
  | CS_Single id eq_b step => FS_Single id eq_b None step
  | CS_Cons id eq_m step rest => FS_Cons id eq_m None step (init_fused_stages rest)
  end.
References

Compiles a chain_spec into its initial unpulled fused_chain.

Definition init_fused_chain {St A B : Type} (spec : chain_spec St A B) : fused_chain St A B :=
  Build_fused_chain (init_fused_stages spec) None.
References

Clears fs_ready slots along fused_stages when an upstream or intermediate stage becomes unready.

Fixpoint clear_fused_ready {St A B : Type} (fs : fused_stages St A B) : fused_stages St A B :=
  match fs with
  | FS_Single id eq_b _ step => FS_Single id eq_b None step
  | FS_Cons id eq_m _ step rest => FS_Cons id eq_m None step (clear_fused_ready rest)
  end.
References

Direct fused pipeline evaluation with per-stage cutoff early-exit and stage-attributed throws.

Fixpoint eval_fused_stages {St A B : Type}
    (st : St) (v_in : A) (prev_tail : option (comb_reading B))
    (fs : fused_stages St A B)
    : St * comb_reading B * fused_stages St A B :=
  match fs in fused_stages _ A0 B0
    return A0 -> option (comb_reading B0) -> St * comb_reading B0 * fused_stages St A0 B0
  with
  | FS_Single id eq_b _ step =>
      fun v0 _ =>
        let '(st', res) := step st v0 in
        match res with
        | RVal v_out =>
            (st', CReady v_out, FS_Single id eq_b (Some v_out) step)
        | RThrow ex =>
            (st', CFailed ex id, FS_Single id eq_b None step)
        end
  | FS_Cons id eq_m prev_m step rest =>
      fun v0 tail0 =>
        let '(st', res) := step st v0 in
        match res with
        | RThrow ex =>
            (st', CFailed ex id, FS_Cons id eq_m None step (clear_fused_ready rest))
        | RVal v_mid =>
            match prev_m, tail0 with
            | Some old_m, Some cached_tail =>
                if eq_m old_m v_mid then
                  (st', cached_tail, FS_Cons id eq_m (Some v_mid) step rest)
                else
                  let '(st'', out, rest') := eval_fused_stages st' v_mid tail0 rest in
                  (st'', out, FS_Cons id eq_m (Some v_mid) step rest')
            | _, _ =>
                let '(st'', out, rest') := eval_fused_stages st' v_mid tail0 rest in
                (st'', out, FS_Cons id eq_m (Some v_mid) step rest')
            end
        end
  end v_in prev_tail.
References

Evaluates a fused MapFusion chain over root reading r_in.

Definition eval_fused_chain {St A B : Type}
    (st : St) (r_in : comb_reading A) (fc : fused_chain St A B)
    : St * comb_reading B * fused_chain St A B :=
  match r_in with
  | CPending src =>
      let out := CPending src in
      (st, out, Build_fused_chain (clear_fused_ready (fc_stages fc)) (Some out))
  | CFailed ex orig =>
      let out := CFailed ex orig in
      (st, out, Build_fused_chain (clear_fused_ready (fc_stages fc)) (Some out))
  | CReady v_in =>
      let '(st', out, stages') :=
        eval_fused_stages st v_in (fc_tail_out fc) (fc_stages fc) in
      (st', out, Build_fused_chain stages' (Some out))
  end.
References

Stage movement test matching Memo.Publish (Core.fs): compares status, failure (ex, origin), or ready values via eq_V.

Definition reading_moved {V : Type} (eq_V : V -> V -> bool)
    (prev : option (comb_reading V)) (next : comb_reading V) : bool :=
  match prev, next with
  | None, _ => true
  | Some (CReady v0), CReady v1 => negb (eq_V v0 v1)
  | Some (CPending s0), CPending s1 => negb (Nat.eqb s0 s1)
  | Some (CFailed e0 o0), CFailed e1 o1 => negb (Nat.eqb e0 e1 && Nat.eqb o0 o1)
  | Some _, _ => true
  end.
References

Uses

3 more
  • option
  • true
  • Nat.eqb

Evaluates a single unfused Memo.map stage: resolves Check -> Clean without running step when up_moved = false.

Definition eval_unfused_stage {St A M : Type}
    (id : nat) (eq_m : M -> M -> bool) (prev : option (comb_reading M))
    (step : St -> A -> St * result M)
    (st : St) (up_moved : bool) (r_in : comb_reading A)
    : St * bool * comb_reading M * option (comb_reading M) :=
  match up_moved, prev with
  | false, Some cached => (st, false, cached, Some cached)
  | _, _ =>
      let '(st', out) :=
        match r_in with
        | CPending src => (st, CPending src)
        | CFailed ex orig => (st, CFailed ex orig)
        | CReady v_in =>
            let '(st1, res) := step st v_in in
            (st1, result_to_reading id res)
        end in
      (st', reading_moved eq_m prev out, out, Some out)
  end.
References

Sequential unfused evaluation threading (st, up_moved, r_mid) across every Memo.map node.

Fixpoint eval_unfused_chain_go {St A B : Type}
    (st : St) (up_moved : bool) (r_in : comb_reading A) (uc : unfused_chain St A B)
    : St * comb_reading B * unfused_chain St A B :=
  match uc in unfused_chain _ A0 B0
    return comb_reading A0 -> St * comb_reading B0 * unfused_chain St A0 B0
  with
  | UC_Single id eq_b prev step =>
      fun r0 =>
        let '(st', _, out, next_prev) :=
          eval_unfused_stage id eq_b prev step st up_moved r0 in
        (st', out, UC_Single id eq_b next_prev step)
  | UC_Cons id eq_m prev step rest =>
      fun r0 =>
        let '(st', moved, out_m, next_prev) :=
          eval_unfused_stage id eq_m prev step st up_moved r0 in
        let '(st'', out_b, rest') :=
          eval_unfused_chain_go st' moved out_m rest in
        (st'', out_b, UC_Cons id eq_m next_prev step rest')
  end r_in.
References

Evaluates an unfused Memo.map chain from a root reading r_in.

Definition eval_unfused_chain {St A B : Type}
    (st : St) (r_in : comb_reading A) (uc : unfused_chain St A B)
    : St * comb_reading B * unfused_chain St A B :=
  eval_unfused_chain_go st true r_in uc.
References

Multi-step trace evaluation of eval_fused_chain over an input list.

Fixpoint run_fused_chain {St A B : Type}
    (st : St) (inputs : list (comb_reading A)) (fc : fused_chain St A B)
    : St * list (comb_reading B) * fused_chain St A B :=
  match inputs with
  | [] => (st, [], fc)
  | r :: rest =>
      let '(st1, out1, fc1) := eval_fused_chain st r fc in
      let '(st2, outs, fc2) := run_fused_chain st1 rest fc1 in
      (st2, out1 :: outs, fc2)
  end.
References

Multi-step trace evaluation of eval_unfused_chain over an input list.

Fixpoint run_unfused_chain {St A B : Type}
    (st : St) (inputs : list (comb_reading A)) (uc : unfused_chain St A B)
    : St * list (comb_reading B) * unfused_chain St A B :=
  match inputs with
  | [] => (st, [], uc)
  | r :: rest =>
      let '(st1, out1, uc1) := eval_unfused_chain st r uc in
      let '(st2, outs, uc2) := run_unfused_chain st1 rest uc1 in
      (st2, out1 :: outs, uc2)
  end.
References

Type-erased unready tag for synchronizing unready states across heterogeneous stage types.

Inductive unready_tag : Type :=
  | UTPending (src : nat)
  | UTFailed (ex : nat) (orig : nat).
References

Uses

  • nat
Global Instance unready_tag_eq_dec : EqDecision unready_tag.
Defined
Proof. solve_decision. Defined.
References

Uses

Definition tag_to_reading {V : Type} (t : unready_tag) : comb_reading V :=
  match t with
  | UTPending src => CPending src
  | UTFailed ex orig => CFailed ex orig
  end.
References
Definition opt_ready {V : Type} (r : option (comb_reading V)) : option V :=
  match r with
  | Some (CReady v) => Some v
  | _ => None
  end.
References

Uses

Fixpoint set_unfused_unready {St A B : Type} (t : unready_tag) (uc : unfused_chain St A B)
    : unfused_chain St A B :=
  match uc with
  | UC_Single id eq_b _ step => UC_Single id eq_b (Some (tag_to_reading t)) step
  | UC_Cons id eq_m _ step rest => UC_Cons id eq_m (Some (tag_to_reading t)) step (set_unfused_unready t rest)
  end.
References

Simulation relation linking unfused_chain and (fused_stages, tail_out) across all runs.

Inductive stages_sync {St : Type} :
    forall {A B : Type}, unfused_chain St A B -> fused_stages St A B -> option (comb_reading B) -> Prop :=
  | Sync_Single :
      forall {A B : Type} (id : nat) (eq_b : B -> B -> bool)
        (prev : option (comb_reading B)) (step : St -> A -> St * result B),
        stages_sync (UC_Single id eq_b prev step) (FS_Single id eq_b (opt_ready prev) step) prev
  | Sync_Cons_None :
      forall {A M B : Type} (id : nat) (eq_m : M -> M -> bool)
        (step : St -> A -> St * result M)
        (uc_rest : unfused_chain St M B) (fs_rest : fused_stages St M B),
        stages_sync uc_rest fs_rest None ->
        stages_sync (UC_Cons id eq_m None step uc_rest) (FS_Cons id eq_m None step fs_rest) None
  | Sync_Cons_Unready :
      forall {A M B : Type} (id : nat) (eq_m : M -> M -> bool)
        (step : St -> A -> St * result M) (t : unready_tag)
        (uc_rest : unfused_chain St M B) (fs_rest : fused_stages St M B),
        uc_rest = set_unfused_unready t uc_rest ->
        fs_rest = clear_fused_ready fs_rest ->
        stages_sync uc_rest fs_rest (Some (tag_to_reading t)) ->
        stages_sync
          (UC_Cons id eq_m (Some (tag_to_reading t)) step uc_rest)
          (FS_Cons id eq_m None step fs_rest)
          (Some (tag_to_reading t))
  | Sync_Cons_Ready :
      forall {A M B : Type} (id : nat) (eq_m : M -> M -> bool) (v_m : M) (r_tail : comb_reading B)
        (step : St -> A -> St * result M)
        (uc_rest : unfused_chain St M B) (fs_rest : fused_stages St M B),
        stages_sync uc_rest fs_rest (Some r_tail) ->
        stages_sync
          (UC_Cons id eq_m (Some (CReady v_m)) step uc_rest)
          (FS_Cons id eq_m (Some v_m) step fs_rest)
          (Some r_tail).
References

Chain-level bisimulation invariant between unfused_chain and fused_chain.

Definition chain_sync {St A B : Type} (uc : unfused_chain St A B) (fc : fused_chain St A B) : Prop :=
  stages_sync uc (fc_stages fc) (fc_tail_out fc).
References
Lemma init_chain_sync {St A B : Type} (spec : chain_spec St A B) :
  chain_sync (init_unfused_chain spec) (init_fused_chain spec).
Qed
Proof.
  unfold chain_sync, init_fused_chain; simpl.
  induction spec as [A B id eq_b step | A M B id eq_m step rest IH]; simpl.
  - exact (Sync_Single id eq_b None step).
  - exact (Sync_Cons_None id eq_m step _ _ IH).
Qed.
References
Lemma clear_fused_ready_idemp {St A B : Type} (fs : fused_stages St A B) :
  clear_fused_ready (clear_fused_ready fs) = clear_fused_ready fs.
Qed
Proof.
  induction fs as [A B id eq_b rdy step | A M B id eq_m rdy step rest IH]; simpl;
    [reflexivity | rewrite IH; reflexivity].
Qed.
References
Lemma set_unfused_unready_idemp {St A B : Type} (t : unready_tag) (uc : unfused_chain St A B) :
  set_unfused_unready t (set_unfused_unready t uc) = set_unfused_unready t uc.
Qed
Proof.
  induction uc as [A B id eq_b prev step | A M B id eq_m prev step rest IH]; simpl;
    [reflexivity | rewrite IH; reflexivity].
Qed.
References
Lemma reading_moved_tag_refl {V : Type} (eq_V : V -> V -> bool) (t : unready_tag) :
  reading_moved eq_V (Some (tag_to_reading t)) (tag_to_reading t) = false.
Qed
Proof.
  destruct t as [src | ex orig]; simpl.
  - rewrite Nat.eqb_refl. reflexivity.
  - rewrite !Nat.eqb_refl. reflexivity.
Qed.
References

Uses

Lemma unfused_false_noop {St A B : Type}
    (uc : unfused_chain St A B) (fs : fused_stages St A B) (tail_opt : option (comb_reading B)) :
  stages_sync uc fs tail_opt ->
  forall (r_tail : comb_reading B) (st : St) (r_in : comb_reading A),
    tail_opt = Some r_tail ->
    eval_unfused_chain_go st false r_in uc = (st, r_tail, uc).
Qed
Proof.
  intros Hsync.
  induction Hsync as
    [ A B id eq_b prev step
    | A M B id eq_m step uc_rest fs_rest Hrest IH
    | A M B id eq_m step t uc_rest fs_rest Huc Hfs Hrest IH
    | A M B id eq_m v_m r_b step uc_rest fs_rest Hrest IH ];
    intros r_tail st r_in Htail.
  - subst prev. reflexivity.
  - discriminate Htail.
  - injection Htail as <-. simpl.
    rewrite (IH (tag_to_reading t) st (tag_to_reading t) eq_refl). reflexivity.
  - injection Htail as <-. simpl.
    rewrite (IH r_b st (CReady v_m) eq_refl). reflexivity.
Qed.
References
Lemma unfused_propagate_unready {St A B : Type}
    (uc : unfused_chain St A B) (fs : fused_stages St A B) (tail_opt : option (comb_reading B)) :
  stages_sync uc fs tail_opt ->
  forall (t : unready_tag) (st : St),
    eval_unfused_chain_go st true (tag_to_reading t) uc =
      (st, tag_to_reading t, set_unfused_unready t uc) /\
    stages_sync (set_unfused_unready t uc) (clear_fused_ready fs) (Some (tag_to_reading t)).
Qed
Proof.
  intros Hsync.
  induction Hsync as
    [ A B id eq_b prev step
    | A M B id eq_m step uc_rest fs_rest Hrest IH
    | A M B id eq_m step t0 uc_rest fs_rest Huc Hfs Hrest IH
    | A M B id eq_m v_m r_b step uc_rest fs_rest Hrest IH ];
    intros t st.
  - simpl. split; [destruct t; reflexivity|].
    pose proof (Sync_Single id eq_b (Some (tag_to_reading t)) step) as Hs.
    destruct t; simpl in Hs |- *; exact Hs.
  - destruct (IH t st) as [Heval Hsync'].
    simpl. destruct t as [s1 | e1 o1]; simpl in Heval |- *; rewrite Heval;
      [ (split; [reflexivity|]);
        apply (Sync_Cons_Unready id eq_m step (UTPending s1) _ _);
          [ symmetry; apply set_unfused_unready_idemp
          | symmetry; apply clear_fused_ready_idemp
          | exact Hsync' ]
      | (split; [reflexivity|]);
        apply (Sync_Cons_Unready id eq_m step (UTFailed e1 o1) _ _);
          [ symmetry; apply set_unfused_unready_idemp
          | symmetry; apply clear_fused_ready_idemp
          | exact Hsync' ] ].
  - destruct (decide (t0 = t)) as [<- | Hne].
    + simpl.
      assert (Hmv : reading_moved eq_m (Some (tag_to_reading t0)) (tag_to_reading t0) = false)
        by apply reading_moved_tag_refl.
      destruct t0 as [s0 | e0 o0]; simpl in Hmv |- *; rewrite Hmv;
        [ rewrite (unfused_false_noop uc_rest fs_rest _ Hrest (CPending s0) st (CPending s0) eq_refl);
          rewrite <- Huc, <- Hfs;
          (split; [reflexivity|]);
          exact (Sync_Cons_Unready id eq_m step (UTPending s0) uc_rest fs_rest Huc Hfs Hrest)
        | rewrite (unfused_false_noop uc_rest fs_rest _ Hrest (CFailed e0 o0) st (CFailed e0 o0) eq_refl);
          rewrite <- Huc, <- Hfs;
          (split; [reflexivity|]);
          exact (Sync_Cons_Unready id eq_m step (UTFailed e0 o0) uc_rest fs_rest Huc Hfs Hrest) ].
    + assert (Hmv : reading_moved eq_m (Some (tag_to_reading t0)) (tag_to_reading t) = true).
      { destruct t0 as [s0 | e0 o0], t as [s1 | e1 o1]; simpl; try reflexivity.
        - destruct (Nat.eqb s0 s1) eqn:Es; [|reflexivity].
          apply Nat.eqb_eq in Es. subst. contradiction.
        - destruct (Nat.eqb e0 e1) eqn:Ee; simpl; [|reflexivity].
          destruct (Nat.eqb o0 o1) eqn:Eo; simpl; [|reflexivity].
          apply Nat.eqb_eq in Ee; apply Nat.eqb_eq in Eo; subst; contradiction. }
      destruct (IH t st) as [Heval Hsync'].
      destruct t as [s1 | e1 o1]; simpl in Hmv, Heval |- *; rewrite Hmv, Heval;
        [ (split; [reflexivity|]);
          apply (Sync_Cons_Unready id eq_m step (UTPending s1) _ _);
            [ symmetry; apply set_unfused_unready_idemp
            | symmetry; apply clear_fused_ready_idemp
            | exact Hsync' ]
        | (split; [reflexivity|]);
          apply (Sync_Cons_Unready id eq_m step (UTFailed e1 o1) _ _);
            [ symmetry; apply set_unfused_unready_idemp
            | symmetry; apply clear_fused_ready_idemp
            | exact Hsync' ] ].
  - destruct (IH t st) as [Heval Hsync'].
    destruct t as [s1 | e1 o1]; simpl in Heval |- *; rewrite Heval;
      [ (split; [reflexivity|]);
        apply (Sync_Cons_Unready id eq_m step (UTPending s1) _ _);
          [ symmetry; apply set_unfused_unready_idemp
          | symmetry; apply clear_fused_ready_idemp
          | exact Hsync' ]
      | (split; [reflexivity|]);
        apply (Sync_Cons_Unready id eq_m step (UTFailed e1 o1) _ _);
          [ symmetry; apply set_unfused_unready_idemp
          | symmetry; apply clear_fused_ready_idemp
          | exact Hsync' ] ].
Qed.
References
Lemma eval_fused_stages_eq_unfused {St A B : Type}
    (uc : unfused_chain St A B) (fs : fused_stages St A B) (tail_opt : option (comb_reading B)) :
  stages_sync uc fs tail_opt ->
  forall (st : St) (v_in : A),
    let '(st_f, out_f, fs') := eval_fused_stages st v_in tail_opt fs in
    let '(st_u, out_u, uc') := eval_unfused_chain_go st true (CReady v_in) uc in
    st_f = st_u /\ out_f = out_u /\ stages_sync uc' fs' (Some out_f).
Qed
Proof.
  intros Hsync.
  induction Hsync as
    [ A B id eq_b prev step
    | A M B id eq_m step uc_rest fs_rest Hrest IH
    | A M B id eq_m step t0 uc_rest fs_rest Huc Hfs Hrest IH
    | A M B id eq_m v_m r_b step uc_rest fs_rest Hrest IH ];
    intros st v_in; simpl.
  - destruct (step st v_in) as [st' [v_out | ex]]; simpl.
    + repeat split; [exact (Sync_Single id eq_b (Some (CReady v_out)) step)].
    + repeat split; [exact (Sync_Single id eq_b (Some (CFailed ex id)) step)].
  - destruct (step st v_in) as [st' [v_mid | ex]]; simpl.
    + specialize (IH st' v_mid).
      destruct (eval_fused_stages st' v_mid None fs_rest) as [[st_f out_f] fs_rest'].
      destruct (eval_unfused_chain_go st' true (CReady v_mid) uc_rest) as [[st_u out_u] uc_rest'].
      destruct IH as (-> & -> & Hsync').
      repeat split. exact (Sync_Cons_Ready id eq_m v_mid out_u step uc_rest' fs_rest' Hsync').
    + destruct (unfused_propagate_unready uc_rest fs_rest None Hrest (UTFailed ex id) st')
        as [Heval Hsync'].
      simpl in Heval. rewrite Heval.
      repeat split.
      apply (Sync_Cons_Unready id eq_m step (UTFailed ex id) _ _);
        [ symmetry; apply set_unfused_unready_idemp
        | symmetry; apply clear_fused_ready_idemp
        | exact Hsync' ].
  - destruct (step st v_in) as [st' [v_mid | ex]]; simpl.
    + destruct t0 as [s0 | e0 o0]; simpl;
        specialize (IH st' v_mid); simpl in IH;
        [ destruct (eval_fused_stages st' v_mid (Some (CPending s0)) fs_rest) as [[st_f out_f] fs_rest']
        | destruct (eval_fused_stages st' v_mid (Some (CFailed e0 o0)) fs_rest) as [[st_f out_f] fs_rest'] ];
        destruct (eval_unfused_chain_go st' true (CReady v_mid) uc_rest) as [[st_u out_u] uc_rest'];
        destruct IH as (-> & -> & Hsync');
        repeat split; exact (Sync_Cons_Ready id eq_m v_mid out_u step uc_rest' fs_rest' Hsync').
    + destruct (decide (t0 = UTFailed ex id)) as [-> | Hne].
      * simpl. rewrite !Nat.eqb_refl. simpl.
        rewrite (unfused_false_noop uc_rest fs_rest _ Hrest (CFailed ex id) st' (CFailed ex id) eq_refl).
        rewrite <- Hfs.
        repeat split.
        exact (Sync_Cons_Unready id eq_m step (UTFailed ex id) uc_rest fs_rest Huc Hfs Hrest).
      * destruct (unfused_propagate_unready uc_rest fs_rest _ Hrest (UTFailed ex id) st')
          as [Heval Hsync'].
        simpl in Heval.
        destruct t0 as [s0 | e0 o0]; simpl.
        -- rewrite Heval. repeat split.
           apply (Sync_Cons_Unready id eq_m step (UTFailed ex id) _ _);
             [ symmetry; apply set_unfused_unready_idemp
             | symmetry; apply clear_fused_ready_idemp
             | exact Hsync' ].
        -- assert (Hneq_b : negb (Nat.eqb e0 ex && Nat.eqb o0 id) = true).
           { destruct (Nat.eqb e0 ex) eqn:Ee; simpl; [|reflexivity].
             destruct (Nat.eqb o0 id) eqn:Eo; simpl; [|reflexivity].
             apply Nat.eqb_eq in Ee; apply Nat.eqb_eq in Eo; subst; contradiction. }
           rewrite Hneq_b, Heval. repeat split.
           apply (Sync_Cons_Unready id eq_m step (UTFailed ex id) _ _);
             [ symmetry; apply set_unfused_unready_idemp
             | symmetry; apply clear_fused_ready_idemp
             | exact Hsync' ].
  - destruct (step st v_in) as [st' [v_mid | ex]]; simpl.
    + destruct (eq_m v_m v_mid) eqn:Eeq; simpl.
      * rewrite (unfused_false_noop uc_rest fs_rest _ Hrest r_b st' (CReady v_mid) eq_refl).
        repeat split. exact (Sync_Cons_Ready id eq_m v_mid r_b step uc_rest fs_rest Hrest).
      * specialize (IH st' v_mid).
        destruct (eval_fused_stages st' v_mid (Some r_b) fs_rest) as [[st_f out_f] fs_rest'].
        destruct (eval_unfused_chain_go st' true (CReady v_mid) uc_rest) as [[st_u out_u] uc_rest'].
        destruct IH as (-> & -> & Hsync').
        repeat split. exact (Sync_Cons_Ready id eq_m v_mid out_u step uc_rest' fs_rest' Hsync').
    + destruct (unfused_propagate_unready uc_rest fs_rest _ Hrest (UTFailed ex id) st')
        as [Heval Hsync'].
      simpl in Heval. rewrite Heval.
      repeat split.
      apply (Sync_Cons_Unready id eq_m step (UTFailed ex id) _ _);
        [ symmetry; apply set_unfused_unready_idemp
        | symmetry; apply clear_fused_ready_idemp
        | exact Hsync' ].
Qed.
References
Lemma eval_fused_chain_step_sync {St A B : Type}
    (uc : unfused_chain St A B) (fc : fused_chain St A B)
    (st : St) (r_in : comb_reading A) :
  chain_sync uc fc ->
  let '(st_f, out_f, fc') := eval_fused_chain st r_in fc in
  let '(st_u, out_u, uc') := eval_unfused_chain st r_in uc in
  st_f = st_u /\ out_f = out_u /\ chain_sync uc' fc'.
Qed
Proof.
  intros Hsync. unfold chain_sync in *.
  destruct r_in as [v_in | src | ex orig].
  - unfold eval_fused_chain, eval_unfused_chain.
    pose proof (eval_fused_stages_eq_unfused uc (fc_stages fc) (fc_tail_out fc) Hsync st v_in) as Hstep.
    destruct (eval_fused_stages st v_in (fc_tail_out fc) (fc_stages fc)) as [[st_f out_f] fs'].
    destruct (eval_unfused_chain_go st true (CReady v_in) uc) as [[st_u out_u] uc'].
    exact Hstep.
  - unfold eval_fused_chain, eval_unfused_chain.
    destruct (unfused_propagate_unready uc (fc_stages fc) (fc_tail_out fc) Hsync (UTPending src) st)
      as [Heval Hsync'].
    simpl in Heval. rewrite Heval.
    repeat split; exact Hsync'.
  - unfold eval_fused_chain, eval_unfused_chain.
    destruct (unfused_propagate_unready uc (fc_stages fc) (fc_tail_out fc) Hsync (UTFailed ex orig) st)
      as [Heval Hsync'].
    simpl in Heval. rewrite Heval.
    repeat split; exact Hsync'.
Qed.
References
Lemma run_fused_chain_sync {St A B : Type}
    (inputs : list (comb_reading A))
    (uc : unfused_chain St A B) (fc : fused_chain St A B) (st : St) :
  chain_sync uc fc ->
  let '(st_f, outs_f, fc') := run_fused_chain st inputs fc in
  let '(st_u, outs_u, uc') := run_unfused_chain st inputs uc in
  st_f = st_u /\ outs_f = outs_u /\ chain_sync uc' fc'.
Qed
Proof.
  revert uc fc st.
  induction inputs as [|r rest IH]; intros uc fc st Hsync; simpl.
  - repeat split; exact Hsync.
  - pose proof (eval_fused_chain_step_sync uc fc st r Hsync) as H1.
    destruct (eval_fused_chain st r fc) as [[st_f1 out_f1] fc1].
    destruct (eval_unfused_chain st r uc) as [[st_u1 out_u1] uc1].
    destruct H1 as (<- & <- & Hsync1).
    specialize (IH uc1 fc1 st_f1 Hsync1).
    destruct (run_fused_chain st_f1 rest fc1) as [[st_f2 outs_f] fc2].
    destruct (run_unfused_chain st_f1 rest uc1) as [[st_u2 outs_u] uc2].
    destruct IH as (<- & <- & Hsync2).
    repeat split; exact Hsync2.
Qed.
References
✓ No axioms

Fused eval_fused_chain produces the exact same (St * comb_reading B), origin, and multi-step trace as sequential unfused eval_unfused_chain over arbitrary boolean stage cutoffs eq_i without Leibniz equality, early-exiting on intermediate stage cutoffs and attributing stage throws to the throwing stage's node_id.

Certifies MapFusion (FusedSignalMap1--4, FusedSourceMap1--4, FusedChainMap in Core.fs) across Signal.map, Memo.map, AsyncMemo.map, AsyncSource.map, and StreamMemo.map (Core.fs, Streams.fs, Api.fs):

  1. From any synchronized state chain_sync uc fc, single-step eval_fused_chain and eval_unfused_chain return identical (St, comb_reading B) and preserve chain_sync across CPending, upstream CFailed, stage throws, intermediate cutoffs, and full CReady propagation.
  2. Over any input trace inputs starting from init_fused_chain spec and init_unfused_chain spec, run_fused_chain and run_unfused_chain yield identical final St (allocation/identity state) and output reading sequences.
  3. On an intermediate cutoff (eq_m old_m v_mid = true) or stage throw (RThrow ex), eval_fused_chain stops at that stage without invoking downstream rest steps, preserving cached_tail or attributing CFailed ex id to stage id.

Arguments

St : Type
explicit heap/allocation state threaded through stage callbacks.
A : Type
root input value type.
M : Type
intermediate stage output type for direct cutoff/throw inspection.
B : Type
tail output value type.
spec : chain_spec St A B
declarative heterogeneous chain of (node_id, eq_i, step_i) stages.
uc : unfused_chain St A B
unfused chain state synchronized with fc.
fc : fused_chain St A B
fused chain state synchronized with uc.
st : St
starting St state.
r_in : comb_reading A
single-step root reading.
inputs : list (comb_reading A)
multi-step sequence of root readings.
id : nat
node_id of an inspected head stage.
eq_m : M -> M -> bool
boolean cutoff predicate of the head stage.
old_m : M
cached ready value of the head stage.
cached_tail : comb_reading B
cached tail reading returned on head-stage cutoff.
step_m : St -> A -> St * result M
step callback of the head stage.
fs_rest : fused_stages St M B
downstream fused stages skipped on cutoff or throw.
v_in : A
ready root input value for head-stage cutoff/throw inspection.
Theorem fused_map_chain_eq_unfused (St A M B : Type)
    (spec : chain_spec St A B)
    (uc : unfused_chain St A B) (fc : fused_chain St A B)
    (st : St) (r_in : comb_reading A) (inputs : list (comb_reading A))
    (id : nat) (eq_m : M -> M -> bool) (old_m : M) (cached_tail : comb_reading B)
    (step_m : St -> A -> St * result M) (fs_rest : fused_stages St M B) (v_in : A) :
  (chain_sync uc fc ->
     let '(st_f, out_f, fc') := eval_fused_chain st r_in fc in
     let '(st_u, out_u, uc') := eval_unfused_chain st r_in uc in
     st_f = st_u /\ out_f = out_u /\ chain_sync uc' fc') /\
  (let '(st_f, outs_f, _) := run_fused_chain st inputs (init_fused_chain spec) in
   let '(st_u, outs_u, _) := run_unfused_chain st inputs (init_unfused_chain spec) in
   st_f = st_u /\ outs_f = outs_u) /\
  (forall st1 v_mid,
     step_m st v_in = (st1, RVal v_mid) ->
     eq_m old_m v_mid = true ->
     eval_fused_chain st (CReady v_in)
       (Build_fused_chain (FS_Cons id eq_m (Some old_m) step_m fs_rest) (Some cached_tail)) =
     (st1, cached_tail,
      Build_fused_chain (FS_Cons id eq_m (Some v_mid) step_m fs_rest) (Some cached_tail))) /\
  (forall st1 ex,
     step_m st v_in = (st1, RThrow ex) ->
     eval_fused_chain st (CReady v_in)
       (Build_fused_chain (FS_Cons id eq_m (Some old_m) step_m fs_rest) (Some cached_tail)) =
     (st1, CFailed ex id,
      Build_fused_chain (FS_Cons id eq_m None step_m (clear_fused_ready fs_rest))
        (Some (CFailed ex id)))).

Arguments

St : Type
explicit heap/allocation state threaded through stage callbacks.
A : Type
root input value type.
M : Type
intermediate stage output type for direct cutoff/throw inspection.
B : Type
tail output value type.
spec : chain_spec St A B
declarative heterogeneous chain of (node_id, eq_i, step_i) stages.
uc : unfused_chain St A B
unfused chain state synchronized with fc.
fc : fused_chain St A B
fused chain state synchronized with uc.
st : St
starting St state.
r_in : comb_reading A
single-step root reading.
inputs : list (comb_reading A)
multi-step sequence of root readings.
id : nat
node_id of an inspected head stage.
eq_m : M -> M -> bool
boolean cutoff predicate of the head stage.
old_m : M
cached ready value of the head stage.
cached_tail : comb_reading B
cached tail reading returned on head-stage cutoff.
step_m : St -> A -> St * result M
step callback of the head stage.
fs_rest : fused_stages St M B
downstream fused stages skipped on cutoff or throw.
v_in : A
ready root input value for head-stage cutoff/throw inspection.
Qed
Proof.
  split_and!.
  - exact (eval_fused_chain_step_sync uc fc st r_in).
  - pose proof (run_fused_chain_sync inputs (init_unfused_chain spec) (init_fused_chain spec) st
                  (init_chain_sync spec)) as Hrun.
    destruct (run_fused_chain st inputs (init_fused_chain spec)) as [[st_f outs_f] fc'].
    destruct (run_unfused_chain st inputs (init_unfused_chain spec)) as [[st_u outs_u] uc'].
    destruct Hrun as (-> & -> & _). split; reflexivity.
  - intros st1 v_mid Hstep Heq.
    unfold eval_fused_chain; simpl. rewrite Hstep, Heq. reflexivity.
  - intros st1 ex Hstep.
    unfold eval_fused_chain; simpl. rewrite Hstep. reflexivity.
Qed.
References

B19. Static map2 and zip left-to-right status precedence

Monadic Value bind over comb_reading: short-circuits on CPending or CFailed preserving origin.

Definition comb_bind {St A B : Type}
    (r : comb_reading A) (st : St) (k : St -> A -> St * comb_reading B)
    : St * comb_reading B :=
  match r with
  | CReady x => k st x
  | CPending src => (st, CPending src)
  | CFailed ex orig => (st, CFailed ex orig)
  end.
References

Reference sequential dynamic body let! x = r1 in let! y = r2 in f x y on node self_id.

Definition eval_seq_let2 {St A B C : Type}
    (self_id : nat) (f : St -> A -> B -> St * result C)
    (st : St) (r1 : comb_reading A) (r2 : comb_reading B)
    : St * comb_reading C :=
  comb_bind r1 st (fun st1 x =>
    comb_bind r2 st1 (fun st2 y =>
      let '(st3, res) := f st2 x y in
      (st3, result_to_reading self_id res))).
References

Static two-source eval_map2 matching Core.fs: fast both-ready path plus slow left-to-right unready precedence, keeping static edges [id1; id2].

Definition eval_map2 {St A B C : Type}
    (self_id id1 id2 : nat) (f : St -> A -> B -> St * result C)
    (st : St) (r1 : comb_reading A) (r2 : comb_reading B)
    : St * comb_reading C * list nat :=
  let edges := [id1; id2] in
  match negb (is_unready_reading r1) && negb (is_unready_reading r2) with
  | true =>
      match r1, r2 with
      | CReady x, CReady y =>
          let '(st', res) := f st x y in
          (st', result_to_reading self_id res, edges)
      | _, _ => (st, CPending self_id, edges)
      end
  | false =>
      match r1 with
      | CPending src1 => (st, CPending src1, edges)
      | CFailed ex1 orig1 => (st, CFailed ex1 orig1, edges)
      | CReady _ =>
          match r2 with
          | CPending src2 => (st, CPending src2, edges)
          | CFailed ex2 orig2 => (st, CFailed ex2 orig2, edges)
          | CReady _ => (st, CPending self_id, edges)
          end
      end
  end.
References

Static eval_zip pairing two comb_reading inputs via eval_map2.

Definition eval_zip {A B : Type}
    (self_id id1 id2 : nat) (r1 : comb_reading A) (r2 : comb_reading B)
    : comb_reading (A * B) * list nat :=
  let '(_, out, edges) :=
    eval_map2 self_id id1 id2 (fun (_ : unit) x y => (tt, RVal (x, y))) tt r1 r2 in
  (out, edges).
References

Uses

✓ No axioms

Static eval_map2 and eval_zip match sequential let! x = r1 in let! y = r2 in f x y across all 3 * 3 comb_reading pairs, preserving St, failure origin, and static [id1; id2] upstream edges.

Certifies map2 and zip across Signal, Memo, AsyncMemo, AsyncSource, StreamMemo, and RanvierExtensions.Zip: the fast both-ready check combined with slow left-to-right unready resolution yields the exact same (St * comb_reading C) and origin as monadic comb_bind evaluation across all nine constructor combinations, skips f (St unchanged) whenever either input is unready, and retains both static upstream edges [id1; id2] without dynamic re-tracking churn.

Arguments

St : Type
explicit state threaded through f.
A : Type
first source value type.
B : Type
second source value type.
C : Type
combined output value type.
self_id : nat
node id of the map2 / zip Memo.
id1 : nat
upstream node id of source1.
id2 : nat
upstream node id of source2.
f : St -> A -> B -> St * result C
closed-window binary mapping function.
st : St
initial St state before evaluation.
reading of source1.
reading of source2.
Theorem map2_zip_status_precedence_eq (St A B C : Type)
    (self_id id1 id2 : nat) (f : St -> A -> B -> St * result C)
    (st : St) (r1 : comb_reading A) (r2 : comb_reading B) :
  let '(st_m, out_m, edges_m) := eval_map2 self_id id1 id2 f st r1 r2 in
  let '(st_s, out_s) := eval_seq_let2 self_id f st r1 r2 in
  st_m = st_s /\
  out_m = out_s /\
  edges_m = [id1; id2] /\
  (is_unready_reading r1 = true \/ is_unready_reading r2 = true -> st_m = st) /\
  eval_zip self_id id1 id2 r1 r2 =
    (snd (eval_seq_let2 self_id (fun (_ : unit) x y => (tt, RVal (x, y))) tt r1 r2), [id1; id2]).

Arguments

St : Type
explicit state threaded through f.
A : Type
first source value type.
B : Type
second source value type.
C : Type
combined output value type.
self_id : nat
node id of the map2 / zip Memo.
id1 : nat
upstream node id of source1.
id2 : nat
upstream node id of source2.
f : St -> A -> B -> St * result C
closed-window binary mapping function.
st : St
initial St state before evaluation.
reading of source1.
reading of source2.
Qed
Proof.
  unfold eval_map2, eval_seq_let2, comb_bind, eval_zip.
  destruct r1 as [x | s1 | e1 o1], r2 as [y | s2 | e2 o2]; simpl.
  - destruct (f st x y) as [st' [v | ex]]; simpl;
      (split_and!; [reflexivity | reflexivity | reflexivity | intros [H1 | H2]; discriminate | reflexivity]).
  - split_and!; try reflexivity; intros _; reflexivity.
  - split_and!; try reflexivity; intros _; reflexivity.
  - split_and!; try reflexivity; intros _; reflexivity.
  - split_and!; try reflexivity; intros _; reflexivity.
  - split_and!; try reflexivity; intros _; reflexivity.
  - split_and!; try reflexivity; intros _; reflexivity.
  - split_and!; try reflexivity; intros _; reflexivity.
  - split_and!; try reflexivity; intros _; reflexivity.
Qed.
References

B20. Split bind inner-tick and transient-unready preservation

Pure state of a split Signal.bind / Memo.bind node tracking outer key, inner link, binder runs, and outer scope discharges.

Record split_bind_state (K InnerId U : Type) := {
  sb_self_id : nat;
  sb_has_key : bool;
  sb_last_key : K;
  sb_bound : option (nat * result InnerId);
  sb_scope_gen : nat;
  sb_discharges : nat;
  sb_binder_runs : nat;
  sb_visible : comb_reading U;
}.
References

Uses

Arguments Build_split_bind_state {K InnerId U} _ _ _ _ _ _ _ _.
Arguments sb_self_id {K InnerId U} _.
Arguments sb_has_key {K InnerId U} _.
Arguments sb_last_key {K InnerId U} _.
Arguments sb_bound {K InnerId U} _.
Arguments sb_scope_gen {K InnerId U} _.
Arguments sb_discharges {K InnerId U} _.
Arguments sb_binder_runs {K InnerId U} _.
Arguments sb_visible {K InnerId U} _.

Resolves the current inner reading from a bound (alloc_tok, result InnerId) outcome.

Definition resolve_bound_inner {InnerId U : Type}
    (self_id : nat) (inner : InnerId -> comb_reading U)
    (b : option (nat * result InnerId)) (fallback : comb_reading U) : comb_reading U :=
  match b with
  | None => fallback
  | Some (_, RVal id) => inner id
  | Some (_, RThrow ex) => CFailed ex self_id
  end.
References

Uses

1 more
  • option

Single-step evaluation of Signal.bind / Memo.bind over outer r_outer, binder : K -> nat * result InnerId, and inner : InnerId -> comb_reading U.

Definition split_bind_step {K InnerId U : Type}
    (eq_K : K -> K -> bool)
    (binder : K -> nat * result InnerId)
    (inner : InnerId -> comb_reading U)
    (r_outer : comb_reading K)
    (st : split_bind_state K InnerId U)
    : split_bind_state K InnerId U :=
  match r_outer with
  | CPending src =>
      {| sb_self_id := sb_self_id st;
         sb_has_key := sb_has_key st;
         sb_last_key := sb_last_key st;
         sb_bound := sb_bound st;
         sb_scope_gen := sb_scope_gen st;
         sb_discharges := sb_discharges st;
         sb_binder_runs := sb_binder_runs st;
         sb_visible := CPending src |}
  | CFailed ex orig =>
      {| sb_self_id := sb_self_id st;
         sb_has_key := sb_has_key st;
         sb_last_key := sb_last_key st;
         sb_bound := sb_bound st;
         sb_scope_gen := sb_scope_gen st;
         sb_discharges := sb_discharges st;
         sb_binder_runs := sb_binder_runs st;
         sb_visible := CFailed ex orig |}
  | CReady k =>
      if sb_has_key st && eq_K (sb_last_key st) k then
        {| sb_self_id := sb_self_id st;
           sb_has_key := true;
           sb_last_key := sb_last_key st;
           sb_bound := sb_bound st;
           sb_scope_gen := sb_scope_gen st;
           sb_discharges := sb_discharges st;
           sb_binder_runs := sb_binder_runs st;
           sb_visible := resolve_bound_inner (sb_self_id st) inner (sb_bound st) (sb_visible st) |}
      else
        let b_out := binder k in
        let next_discharges :=
          if sb_has_key st then S (sb_discharges st) else sb_discharges st in
        {| sb_self_id := sb_self_id st;
           sb_has_key := true;
           sb_last_key := k;
           sb_bound := Some b_out;
           sb_scope_gen := S (sb_scope_gen st);
           sb_discharges := next_discharges;
           sb_binder_runs := S (sb_binder_runs st);
           sb_visible :=
             match snd b_out with
             | RVal id => inner id
             | RThrow ex => CFailed ex (sb_self_id st)
             end |}
  end.
References

Evaluates a sequence of inner U environment ticks while outer settles to CReady k.

Fixpoint split_bind_inner_ticks {K InnerId U : Type}
    (eq_K : K -> K -> bool)
    (binder : K -> nat * result InnerId)
    (k : K)
    (inners : list (InnerId -> comb_reading U))
    (st : split_bind_state K InnerId U)
    : split_bind_state K InnerId U :=
  match inners with
  | [] => st
  | inner_i :: rest =>
      split_bind_inner_ticks eq_K binder k rest
        (split_bind_step eq_K binder inner_i (CReady k) st)
  end.
References

Evaluates a sequence of outer comb_reading K transitions against a fixed inner environment inner.

Fixpoint split_bind_outer_steps {K InnerId U : Type}
    (eq_K : K -> K -> bool)
    (binder : K -> nat * result InnerId)
    (inner : InnerId -> comb_reading U)
    (outers : list (comb_reading K))
    (st : split_bind_state K InnerId U)
    : split_bind_state K InnerId U :=
  match outers with
  | [] => st
  | r :: rest =>
      split_bind_outer_steps eq_K binder inner rest
        (split_bind_step eq_K binder inner r st)
  end.
References
Lemma split_bind_inner_ticks_preserves {K InnerId U : Type}
    (eq_K : K -> K -> bool)
    (binder : K -> nat * result InnerId)
    (k : K) (tok : nat) (inner_id : InnerId)
    (inners : list (InnerId -> comb_reading U))
    (inner_last : InnerId -> comb_reading U)
    (st : split_bind_state K InnerId U) :
  sb_has_key st = true ->
  eq_K (sb_last_key st) k = true ->
  sb_bound st = Some (tok, RVal inner_id) ->
  let st' := split_bind_inner_ticks eq_K binder k (inners ++ [inner_last]) st in
  sb_binder_runs st' = sb_binder_runs st /\
  sb_scope_gen st' = sb_scope_gen st /\
  sb_discharges st' = sb_discharges st /\
  sb_last_key st' = sb_last_key st /\
  sb_bound st' = Some (tok, RVal inner_id) /\
  sb_visible st' = inner_last inner_id.
Qed
Proof.
  revert st.
  induction inners as [|inner_i rest IH]; intros st Hhas Heq Hbnd;
    cbn [split_bind_inner_ticks app].
  - unfold split_bind_step. rewrite Hhas, Heq, Hbnd. simpl.
    repeat split; reflexivity.
  - assert (Hstep :
      split_bind_step eq_K binder inner_i (CReady k) st =
      {| sb_self_id := sb_self_id st;
         sb_has_key := true;
         sb_last_key := sb_last_key st;
         sb_bound := Some (tok, RVal inner_id);
         sb_scope_gen := sb_scope_gen st;
         sb_discharges := sb_discharges st;
         sb_binder_runs := sb_binder_runs st;
         sb_visible := inner_i inner_id |}).
    { unfold split_bind_step. rewrite Hhas, Heq, Hbnd. simpl. reflexivity. }
    cbv zeta. rewrite Hstep.
    exact (IH {| sb_self_id := sb_self_id st;
                 sb_has_key := true;
                 sb_last_key := sb_last_key st;
                 sb_bound := Some (tok, RVal inner_id);
                 sb_scope_gen := sb_scope_gen st;
                 sb_discharges := sb_discharges st;
                 sb_binder_runs := sb_binder_runs st;
                 sb_visible := inner_i inner_id |}
              eq_refl Heq eq_refl).
Qed.
References
Lemma split_bind_outer_unready_preserves {K InnerId U : Type}
    (eq_K : K -> K -> bool)
    (binder : K -> nat * result InnerId)
    (inner : InnerId -> comb_reading U)
    (us : list (comb_reading K))
    (st : split_bind_state K InnerId U) :
  Forall (fun r => is_unready_reading r = true) us ->
  let st' := split_bind_outer_steps eq_K binder inner us st in
  sb_self_id st' = sb_self_id st /\
  sb_has_key st' = sb_has_key st /\
  sb_last_key st' = sb_last_key st /\
  sb_bound st' = sb_bound st /\
  sb_scope_gen st' = sb_scope_gen st /\
  sb_discharges st' = sb_discharges st /\
  sb_binder_runs st' = sb_binder_runs st.
Qed
Proof.
  revert st. induction us as [|r rest IH]; intros st Hall; simpl.
  - repeat split; reflexivity.
  - inversion Hall as [|? ? Hr Hrest]; subst.
    destruct r as [v | src | ex orig]; [simpl in Hr; discriminate| |];
      exact (IH _ Hrest).
Qed.
References
Lemma split_bind_outer_steps_app {K InnerId U : Type}
    (eq_K : K -> K -> bool)
    (binder : K -> nat * result InnerId)
    (inner : InnerId -> comb_reading U)
    (rs1 rs2 : list (comb_reading K))
    (st : split_bind_state K InnerId U) :
  split_bind_outer_steps eq_K binder inner (rs1 ++ rs2) st =
  split_bind_outer_steps eq_K binder inner rs2
    (split_bind_outer_steps eq_K binder inner rs1 st).
Qed
Proof.
  revert st. induction rs1 as [|r rest IH]; intros st; simpl; [reflexivity | apply IH].
Qed.
References
✓ No axioms

Split split_bind_step invokes binder iff outer settles to an initial or eq_K-unequal key, discharges the prior outer scope iff sb_has_key = true and outer settles to an eq_K-unequal key, and performs zero binder calls or scope discharges across inner U transitions and transient outer CPending/CFailed excursions recovering to an eq_K-equal key.

Certifies Signal.bind, Memo.bind (SplitBindState in Core.fs, Api.fs), and RanvierExtensions.SelectMany (Ranvier.CSharp/Extensions.fs):

  1. sb_binder_runs increments if and only if r_outer = CReady k with sb_has_key = false or eq_K sb_last_key k = false; otherwise sb_binder_runs is unchanged.
  2. sb_discharges increments if and only if r_outer = CReady k with sb_has_key = true and eq_K sb_last_key k = false; otherwise sb_discharges is unchanged.
  3. Across any sequence of inner U transitions inners ++ [inner_last] while outer settles to an eq_K-equal key k, split_bind_inner_ticks updates sb_visible to inner_last inner_id with zero binder runs and zero scope discharges.
  4. Across any sequence us of transient outer CPending/CFailed readings followed by CReady k with eq_K sb_last_key k = true, split_bind_outer_steps preserves sb_bound, sb_last_key, sb_binder_runs, and sb_discharges, resuming resolve_bound_inner directly.

Arguments

K : Type
outer reactive value type.
InnerId : Type
identifier type of inner Memo<'U> nodes returned by binder.
U : Type
inner reactive value type.
eq_K : K -> K -> bool
boolean cutoff predicate over settled outer K values.
binder : K -> nat * result InnerId
closed-window outer binder returning an allocation token and result InnerId.
inner : InnerId -> comb_reading U
reading lookup for inner InnerId nodes.
inner_last : InnerId -> comb_reading U
final inner reading lookup after a sequence of inner U ticks.
inners : list (InnerId -> comb_reading U)
intermediate inner U tick environments.
r_outer : comb_reading K
single-step outer reading.
k : K
settled outer key matching sb_last_key.
tok : nat
scope/allocation token recorded when inner_id was bound.
inner_id : InnerId
currently linked inner node id.
b_out : nat * result InnerId
bound (tok, result InnerId) outcome preserved across outer unready excursions.
us : list (comb_reading K)
transient outer CPending/CFailed excursion trace.
st : split_bind_state K InnerId U
split_bind_state before the step or trace.
Theorem split_bind_inner_tick_eq (K InnerId U : Type)
    (eq_K : K -> K -> bool)
    (binder : K -> nat * result InnerId)
    (inner inner_last : InnerId -> comb_reading U)
    (inners : list (InnerId -> comb_reading U))
    (r_outer : comb_reading K) (k : K) (tok : nat) (inner_id : InnerId)
    (b_out : nat * result InnerId)
    (us : list (comb_reading K))
    (st : split_bind_state K InnerId U) :
  (let st' := split_bind_step eq_K binder inner r_outer st in
   (sb_binder_runs st' = S (sb_binder_runs st) <->
      exists k', r_outer = CReady k' /\ (sb_has_key st = false \/ eq_K (sb_last_key st) k' = false)) /\
   (sb_binder_runs st' = sb_binder_runs st \/ sb_binder_runs st' = S (sb_binder_runs st)) /\
   (sb_discharges st' = S (sb_discharges st) <->
      exists k', r_outer = CReady k' /\ sb_has_key st = true /\ eq_K (sb_last_key st) k' = false) /\
   (sb_discharges st' = sb_discharges st \/ sb_discharges st' = S (sb_discharges st))) /\
  (sb_has_key st = true -> eq_K (sb_last_key st) k = true ->
     sb_bound st = Some (tok, RVal inner_id) ->
     let st' := split_bind_inner_ticks eq_K binder k (inners ++ [inner_last]) st in
     sb_binder_runs st' = sb_binder_runs st /\
     sb_scope_gen st' = sb_scope_gen st /\
     sb_discharges st' = sb_discharges st /\
     sb_last_key st' = sb_last_key st /\
     sb_bound st' = Some (tok, RVal inner_id) /\
     sb_visible st' = inner_last inner_id) /\
  (sb_has_key st = true -> eq_K (sb_last_key st) k = true ->
     sb_bound st = Some b_out ->
     Forall (fun r => is_unready_reading r = true) us ->
     let st' := split_bind_outer_steps eq_K binder inner (us ++ [CReady k]) st in
     sb_binder_runs st' = sb_binder_runs st /\
     sb_scope_gen st' = sb_scope_gen st /\
     sb_discharges st' = sb_discharges st /\
     sb_last_key st' = sb_last_key st /\
     sb_bound st' = Some b_out /\
     sb_visible st' =
       match snd b_out with
       | RVal id => inner id
       | RThrow ex => CFailed ex (sb_self_id st)
       end).

Arguments

K : Type
outer reactive value type.
InnerId : Type
identifier type of inner Memo<'U> nodes returned by binder.
U : Type
inner reactive value type.
eq_K : K -> K -> bool
boolean cutoff predicate over settled outer K values.
binder : K -> nat * result InnerId
closed-window outer binder returning an allocation token and result InnerId.
inner : InnerId -> comb_reading U
reading lookup for inner InnerId nodes.
inner_last : InnerId -> comb_reading U
final inner reading lookup after a sequence of inner U ticks.
inners : list (InnerId -> comb_reading U)
intermediate inner U tick environments.
r_outer : comb_reading K
single-step outer reading.
k : K
settled outer key matching sb_last_key.
tok : nat
scope/allocation token recorded when inner_id was bound.
inner_id : InnerId
currently linked inner node id.
b_out : nat * result InnerId
bound (tok, result InnerId) outcome preserved across outer unready excursions.
us : list (comb_reading K)
transient outer CPending/CFailed excursion trace.
st : split_bind_state K InnerId U
split_bind_state before the step or trace.
Qed
Proof.
  split_and!.
  - cbv zeta. split_and!.
    + split.
      * destruct r_outer as [k' | src | ex orig]; simpl.
        -- destruct (sb_has_key st) eqn:Ehk; simpl.
           ++ destruct (eq_K (sb_last_key st) k') eqn:Eeq; simpl.
              ** intros Hbad; lia.
              ** intros _. exists k'. split; [reflexivity | right; exact Eeq].
           ++ intros _. exists k'. split; [reflexivity | left; reflexivity].
        -- intros Hbad; lia.
        -- intros Hbad; lia.
      * intros (k' & -> & Hnew). unfold split_bind_step.
        destruct Hnew as [Hno | Hneq];
          [rewrite Hno; reflexivity | rewrite Hneq, andb_false_r; reflexivity].
    + destruct r_outer as [k' | src | ex orig]; simpl;
        [destruct (sb_has_key st && eq_K (sb_last_key st) k'); simpl; [left | right]; reflexivity
        | left; reflexivity
        | left; reflexivity].
    + split.
      * destruct r_outer as [k' | src | ex orig]; simpl.
        -- destruct (sb_has_key st) eqn:Ehk; simpl.
           ++ destruct (eq_K (sb_last_key st) k') eqn:Eeq; simpl.
              ** intros Hbad; lia.
              ** intros _. exists k'. split_and!; [reflexivity | reflexivity | exact Eeq].
           ++ intros Hbad; lia.
        -- intros Hbad; lia.
        -- intros Hbad; lia.
      * intros (k' & -> & Hhk & Hneq). unfold split_bind_step.
        rewrite Hhk, Hneq. simpl. reflexivity.
    + destruct r_outer as [k' | src | ex orig]; simpl;
        [destruct (sb_has_key st) eqn:Ehk; simpl;
          [destruct (eq_K (sb_last_key st) k'); simpl; [left | right]; reflexivity
          | left; reflexivity]
        | left; reflexivity
        | left; reflexivity].
  - exact (split_bind_inner_ticks_preserves eq_K binder k tok inner_id inners inner_last st).
  - intros Hhas Heq Hb_out Hall.
    rewrite split_bind_outer_steps_app. simpl.
    pose proof (split_bind_outer_unready_preserves eq_K binder inner us st Hall)
      as (Hsid & Hhk & Hlk & Hbnd & Hsg & Hdis & Hbr).
    cbv zeta.
    set (st_u := split_bind_outer_steps eq_K binder inner us st) in *.
    unfold split_bind_step.
    rewrite Hhk, Hlk, Hhas, Heq. simpl.
    rewrite Hbr, Hsg, Hdis, Hbnd, Hsid, Hb_out.
    destruct b_out as [t [id | ex]]; simpl; repeat split; reflexivity.
Qed.
References

B21. CE reentrant step dispatch and n-ary MergeSources precedence

B21a. Reentrant evalDepth step dispatch

State of a CE step evaluation on one thread: InternalCeState.evalDepth (ce_depth), whether reads register dependency edges on the running node (ce_tracking), and the number of nodes constructed (ce_nodes_created).

Record ce_eval_state := {
  ce_depth : nat;
  ce_tracking : bool;
  ce_nodes_created : nat;
}.
References

Uses

  • bool
  • nat
Definition ce_set_depth (d : nat) (st : ce_eval_state) : ce_eval_state :=
  {| ce_depth := d; ce_tracking := ce_tracking st; ce_nodes_created := ce_nodes_created st |}.
References
Definition ce_set_tracking (t : bool) (st : ce_eval_state) : ce_eval_state :=
  {| ce_depth := ce_depth st; ce_tracking := t; ce_nodes_created := ce_nodes_created st |}.
References
Definition ce_alloc_node (st : ce_eval_state) : ce_eval_state :=
  {| ce_depth := ce_depth st; ce_tracking := ce_tracking st;
     ce_nodes_created := S (ce_nodes_created st) |}.
References

finally (InternalCeState.ExitEval d) (CeRuntime.CreateDynamicMemo / CeRuntime.SuspendNested): sets ce_depth := d and returns the outcome unchanged, on the RVal v return and the RThrow ex unwind alike.

Definition ce_restore_depth {A : Type} (d : nat) (out : ce_eval_state * result A)
    : ce_eval_state * result A :=
  match out with
  | (st', RVal v) => (ce_set_depth d st', RVal v)
  | (st', RThrow ex) => (ce_set_depth d st', RThrow ex)
  end.
References

CeRuntime.CreateDynamicMemo (backed by MemoDynamic) around a CE body: body runs at depth S d for entry depth d, followed by ce_restore_depth d.

Definition with_eval_depth {A : Type}
    (body : ce_eval_state -> ce_eval_state * result A) (st : ce_eval_state)
    : ce_eval_state * result A :=
  let d := ce_depth st in
  ce_restore_depth d (body (ce_set_depth (S d) st)).
References

CeRuntime.SuspendNested (InternalCeState.SuspendForNestedRun) around MemoRunner.EvalBound or a nested memo block inside an owning Phase 2 callback: body runs at depth 0, followed by ce_restore_depth d for entry depth d.

Definition with_suspended_run {A : Type}
    (body : ce_eval_state -> ce_eval_state * result A) (st : ce_eval_state)
    : ce_eval_state * result A :=
  let d := ce_depth st in
  ce_restore_depth d (body (ce_set_depth 0 st)).
References

MemoStep<'Phase, 'Prev, 'U>: OfFactory n corresponds to MemoStatic (static node n, constructed by MemoRunner.EvalBound), and OfInline r corresponds to MemoInline (reading r, computed inline on the enclosing node alongside MemoDynamic).

Inductive ce_memo_step (U : Type) : Type :=
  | OfFactory (node : nat)
  | OfInline (r : comb_reading U).
References
Arguments OfFactory {U} _.
Arguments OfInline {U} _.

UntrackedPhase.wrapPending over the raw outcome of an untracked callback: a synchronous NotReadyException on src becomes RThrow (wrap_ex src), the InvalidOperationException wrapping it, and any other throw keeps its exception id.

Definition untracked_wrap {U : Type} (wrap_ex : nat -> nat) (raw : comb_reading U) : result U :=
  match raw with
  | CReady u => RVal u
  | CPending src => RThrow (wrap_ex src)
  | CFailed ex _ => RThrow ex
  end.
References

BindReturn(src, f) of a CE step on node self_id. At depth 0 it constructs one static node and leaves read and f unevaluated. At a nonzero depth it reads src with tracking on, runs f with tracking off (EnterUntrackedPure / RunUntracked), and restores the entry tracking flag.

Definition ce_bind_return_eval {A U : Type}
    (self_id : nat) (wrap_ex : nat -> nat)
    (read : ce_eval_state -> comb_reading A)
    (f : ce_eval_state -> A -> ce_eval_state * comb_reading U)
    (st : ce_eval_state) : ce_eval_state * ce_memo_step U :=
  if Nat.eqb (ce_depth st) 0 then
    (ce_alloc_node st, OfFactory (ce_nodes_created st))
  else
    let '(st', out) :=
      comb_bind (read (ce_set_tracking true st)) st (fun st1 x =>
        let '(st_f, raw) := f (ce_set_tracking false st1) x in
        (ce_set_tracking (ce_tracking st1) st_f,
         result_to_reading self_id (untracked_wrap wrap_ex raw))) in
    (st', OfInline out).
References
Lemma ce_restore_depth_eq {A : Type} (d : nat) (st' : ce_eval_state) (res : result A) :
  ce_restore_depth d (st', res) = (ce_set_depth d st', res).
Qed
Proof. destruct res; reflexivity. Qed.
References
Lemma ce_bind_return_eval_static {A U : Type}
    (self_id : nat) (wrap_ex : nat -> nat)
    (read : ce_eval_state -> comb_reading A)
    (f : ce_eval_state -> A -> ce_eval_state * comb_reading U)
    (st : ce_eval_state) :
  ce_depth st = 0 ->
  ce_bind_return_eval self_id wrap_ex read f st =
    (ce_alloc_node st, OfFactory (ce_nodes_created st)).
Qed
Proof. intros Hd. unfold ce_bind_return_eval. rewrite Hd. reflexivity. Qed.
References
✓ No axioms

with_eval_depth and with_suspended_run return ce_depth to its entry value on every RVal/RThrow outcome, MemoBuilder.BindReturn at depth 0 constructs one static node (MemoStatic) and leaves read and f unevaluated, and at depth S d it evaluates inline (MemoInline) with a tracked Phase 1 read, an untracked f, and zero node allocations of its own.

Certifies the reentrant evalDepth dispatch of Ranvier.CE (InternalCeState, MemoStep<'Phase, 'Prev, 'U>, and MemoRunner.EvalBound / CeRuntime.ProbeBound):

  1. with_eval_depth (CeRuntime.CreateDynamicMemo) runs body at depth S d and with_suspended_run (CeRuntime.SuspendNested / InternalCeState.SuspendForNestedRun) runs it at depth 0; both end at the entry depth d with the body's outcome res, tracking flag, and node count, for RVal v and RThrow ex alike via InternalCeState.ExitEval.
  2. After a dynamic evaluation entered at depth 0 ends with any outcome, including a Phase 1 NotReadyException or an exception thrown inside match!, the next top-level MemoBuilder.BindReturn dispatches statically.
  3. At depth 0, ce_bind_return_eval returns OfFactory n (MemoStatic) for the fresh node n = ce_nodes_created st and increments the node count by one. The result is independent of read and f.
  4. At depth S d, read runs with ce_tracking = true. An unready Phase 1 reading ends the step with its exact CPending src or CFailed ex orig and the entry state, leaving f unevaluated. A ready reading runs f at depth S d with ce_tracking = false (EnterUntrackedPure / RunUntrackedPureArg) and the entry node count; the step then restores the entry tracking flag and takes its depth and node count from the result state of f.
  5. A synchronous CPending src raised by the untracked f surfaces as CFailed (wrap_ex src) self_id.
  6. CeRuntime.SuspendNested around MemoRunner.EvalBound (with_suspended_run around ce_bind_return_eval) constructs one node via CeRuntime.ProbeBound and returns to the entry depth, at the top level and for a nested memo { ... } inside an owning Phase 2 callback alike.

Arguments

A : Type
Phase 1 source value type.
U : Type
output type of f.
R : Type
outcome type of body.
CE body evaluated under with_eval_depth or with_suspended_run.
self_id : nat
node id of the enclosing node, the origin of a failure raised by f.
wrap_ex : nat -> nat
exception id of the InvalidOperationException wrapping a NotReadyException on a source.
Phase 1 tracked read of the bound source.
closed-window mapping callback of return.
CE state at entry.
d : nat
the entry depth is S d in the inline-evaluation clauses.
Theorem ce_dispatch_and_nested_eval_eq (A U R : Type)
    (body : ce_eval_state -> ce_eval_state * result R)
    (self_id : nat) (wrap_ex : nat -> nat)
    (read : ce_eval_state -> comb_reading A)
    (f : ce_eval_state -> A -> ce_eval_state * comb_reading U)
    (st : ce_eval_state) (d : nat) :
  (forall st' res,
     body (ce_set_depth (S (ce_depth st)) st) = (st', res) ->
     with_eval_depth body st = (ce_set_depth (ce_depth st) st', res)) /\
  (forall st' res,
     body (ce_set_depth 0 st) = (st', res) ->
     with_suspended_run body st = (ce_set_depth (ce_depth st) st', res)) /\
  (ce_depth st = 0 ->
     let st1 := fst (with_eval_depth body st) in
     ce_bind_return_eval self_id wrap_ex read f st1 =
       (ce_alloc_node st1, OfFactory (ce_nodes_created st1))) /\
  (ce_depth st = 0 ->
     ce_bind_return_eval self_id wrap_ex read f st =
       (ce_alloc_node st, OfFactory (ce_nodes_created st))) /\
  (ce_depth st = S d ->
     let st_read := {| ce_depth := S d; ce_tracking := true;
                       ce_nodes_created := ce_nodes_created st |} in
     let st_call := {| ce_depth := S d; ce_tracking := false;
                       ce_nodes_created := ce_nodes_created st |} in
     (forall src, read st_read = CPending src ->
        ce_bind_return_eval self_id wrap_ex read f st = (st, OfInline (CPending src))) /\
     (forall ex orig, read st_read = CFailed ex orig ->
        ce_bind_return_eval self_id wrap_ex read f st = (st, OfInline (CFailed ex orig))) /\
     (forall x st_f raw, read st_read = CReady x -> f st_call x = (st_f, raw) ->
        ce_bind_return_eval self_id wrap_ex read f st =
          ({| ce_depth := ce_depth st_f; ce_tracking := ce_tracking st;
              ce_nodes_created := ce_nodes_created st_f |},
           OfInline (result_to_reading self_id (untracked_wrap wrap_ex raw)))) /\
     (forall x st_f src, read st_read = CReady x -> f st_call x = (st_f, CPending src) ->
        snd (ce_bind_return_eval self_id wrap_ex read f st) =
          OfInline (CFailed (wrap_ex src) self_id))) /\
  with_suspended_run
    (fun s => let '(s', step) := ce_bind_return_eval self_id wrap_ex read f s in (s', RVal step)) st =
    (ce_alloc_node st, RVal (OfFactory (ce_nodes_created st))).

Arguments

A : Type
Phase 1 source value type.
U : Type
output type of f.
R : Type
outcome type of body.
CE body evaluated under with_eval_depth or with_suspended_run.
self_id : nat
node id of the enclosing node, the origin of a failure raised by f.
wrap_ex : nat -> nat
exception id of the InvalidOperationException wrapping a NotReadyException on a source.
Phase 1 tracked read of the bound source.
closed-window mapping callback of return.
CE state at entry.
d : nat
the entry depth is S d in the inline-evaluation clauses.
Qed
Proof.
  split_and!.
  - intros st' res Hbody. unfold with_eval_depth. cbv zeta.
    rewrite Hbody. apply ce_restore_depth_eq.
  - intros st' res Hbody. unfold with_suspended_run. cbv zeta.
    rewrite Hbody. apply ce_restore_depth_eq.
  - intros Hd0. cbv zeta. apply ce_bind_return_eval_static.
    unfold with_eval_depth. cbv zeta.
    destruct (body (ce_set_depth (S (ce_depth st)) st)) as [st' res].
    rewrite ce_restore_depth_eq. simpl. exact Hd0.
  - apply ce_bind_return_eval_static.
  - intros Hd. cbv zeta.
    assert (Hread : ce_set_tracking true st =
              {| ce_depth := S d; ce_tracking := true; ce_nodes_created := ce_nodes_created st |})
      by (unfold ce_set_tracking; rewrite Hd; reflexivity).
    assert (Hcall : ce_set_tracking false st =
              {| ce_depth := S d; ce_tracking := false; ce_nodes_created := ce_nodes_created st |})
      by (unfold ce_set_tracking; rewrite Hd; reflexivity).
    rewrite <- Hread, <- Hcall.
    assert (Hinline : forall x st_f raw,
              read (ce_set_tracking true st) = CReady x ->
              f (ce_set_tracking false st) x = (st_f, raw) ->
              ce_bind_return_eval self_id wrap_ex read f st =
                ({| ce_depth := ce_depth st_f; ce_tracking := ce_tracking st;
                    ce_nodes_created := ce_nodes_created st_f |},
                 OfInline (result_to_reading self_id (untracked_wrap wrap_ex raw)))).
    { intros x st_f raw Hr Hf. unfold ce_bind_return_eval. rewrite Hd. cbn [Nat.eqb].
      rewrite Hr. cbn [comb_bind]. rewrite Hf. reflexivity. }
    split_and!.
    + intros src Hr. unfold ce_bind_return_eval. rewrite Hd. cbn [Nat.eqb].
      rewrite Hr. reflexivity.
    + intros ex orig Hr. unfold ce_bind_return_eval. rewrite Hd. cbn [Nat.eqb].
      rewrite Hr. reflexivity.
    + exact Hinline.
    + intros x st_f src Hr Hf. rewrite (Hinline x st_f (CPending src) Hr Hf). reflexivity.
  - unfold with_suspended_run. cbv beta zeta.
    rewrite ce_bind_return_eval_static by reflexivity.
    reflexivity.
Qed.
References

B21b. n-ary right-nested MergeSources precedence

Right-nested MergeSources(r_1, MergeSources(r_2, ...)) over $n \ge 2$ source readings: SLast2 r1 r2 is the innermost pair and SCons r rest prepends source r.

Inductive source_chain : Type -> Type :=
  | SLast2 : forall {A B : Type}, comb_reading A -> comb_reading B -> source_chain (A * B)
  | SCons : forall {A B : Type}, comb_reading A -> source_chain B -> source_chain (A * B).
References
Arguments SLast2 {A B} _ _.
Arguments SCons {A B} _ _.

Reading of MergeSources(r1, r2) (MergedSignals, MergedMemos, MergedReactive): the pair of both values when both are ready, otherwise the leftmost unready reading.

Definition merge_readings {A B : Type} (r1 : comb_reading A) (r2 : comb_reading B)
    : comb_reading (A * B) :=
  match r1, r2 with
  | CReady a, CReady b => CReady (a, b)
  | CPending src, _ => CPending src
  | CFailed ex orig, _ => CFailed ex orig
  | CReady _, CPending src => CPending src
  | CReady _, CFailed ex orig => CFailed ex orig
  end.
References

Reading of the nested struct tuple of a merged source chain.

Fixpoint eval_merged_chain {T : Type} (sc : source_chain T) : comb_reading T :=
  match sc in source_chain T0 return comb_reading T0 with
  | SLast2 r1 r2 => merge_readings r1 r2
  | SCons r1 rest => merge_readings r1 (eval_merged_chain rest)
  end.
References

comb_bind on a reading alone, threading state unit.

Definition reading_bind {A B : Type} (r : comb_reading A) (k : A -> comb_reading B)
    : comb_reading B :=
  snd (comb_bind r tt (fun _ a => (tt, k a))).
References

Sequential let! x_1 = r_1 in ... let! x_n = r_n in k (x_1, (..., x_n)) over a source chain.

Fixpoint eval_seq_chain_k {T R : Type} (sc : source_chain T)
    : (T -> comb_reading R) -> comb_reading R :=
  match sc in source_chain T0 return (T0 -> comb_reading R) -> comb_reading R with
  | SLast2 r1 r2 => fun k => reading_bind r1 (fun a => reading_bind r2 (fun b => k (a, b)))
  | SCons r1 rest => fun k => reading_bind r1 (fun a => eval_seq_chain_k rest (fun t => k (a, t)))
  end.
References

Reference sequential body let! x_1 = r_1 in ... let! x_n = r_n in return (x_1, (..., x_n)).

Definition eval_seq_chain {T : Type} (sc : source_chain T) : comb_reading T :=
  eval_seq_chain_k sc CReady.
References

Component equality predicates of a right-nested struct tuple.

Inductive eq_spec : Type -> Type :=
  | EqLast2 : forall {A B : Type}, (A -> A -> bool) -> (B -> B -> bool) -> eq_spec (A * B)
  | EqCons : forall {A B : Type}, (A -> A -> bool) -> eq_spec B -> eq_spec (A * B).
References

Uses

  • bool
Arguments EqLast2 {A B} _ _.
Arguments EqCons {A B} _ _.

Settled-key cutoff on a nested struct tuple: the left-to-right && of its component predicates.

Fixpoint eq_chain {T : Type} (eqs : eq_spec T) : T -> T -> bool :=
  match eqs in eq_spec T0 return T0 -> T0 -> bool with
  | EqLast2 eq_a eq_b => fun k1 k2 => eq_a (fst k1) (fst k2) && eq_b (snd k1) (snd k2)
  | EqCons eq_a rest => fun k1 k2 => eq_a (fst k1) (fst k2) && eq_chain rest (snd k1) (snd k2)
  end.
References

Uses

Each component predicate's result on two nested struct tuples, in component order.

Fixpoint eq_checks {T : Type} (eqs : eq_spec T) : T -> T -> list bool :=
  match eqs in eq_spec T0 return T0 -> T0 -> list bool with
  | EqLast2 eq_a eq_b => fun k1 k2 => [eq_a (fst k1) (fst k2); eq_b (snd k1) (snd k2)]
  | EqCons eq_a rest => fun k1 k2 => eq_a (fst k1) (fst k2) :: eq_checks rest (snd k1) (snd k2)
  end.
References

Uses

Lemma reading_bind_ready {A : Type} (r : comb_reading A) :
  reading_bind r CReady = r.
Qed
Proof. destruct r; reflexivity. Qed.
References
Lemma eval_seq_chain_k_merged {T R : Type} (sc : source_chain T) (k : T -> comb_reading R) :
  eval_seq_chain_k sc k = reading_bind (eval_merged_chain sc) k.
Qed
Proof.
  revert R k.
  induction sc as [A B r1 r2 | A B r1 rest IH]; intros R k; simpl.
  - destruct r1 as [a | s1 | e1 o1], r2 as [b | s2 | e2 o2]; reflexivity.
  - destruct r1 as [a | s1 | e1 o1]; [| reflexivity | reflexivity].
    cbn [reading_bind comb_bind snd merge_readings].
    rewrite IH. destruct (eval_merged_chain rest) as [t | s | e o]; reflexivity.
Qed.
References
Lemma eq_chain_iff_checks {T : Type} (eqs : eq_spec T) (k1 k2 : T) :
  eq_chain eqs k1 k2 = true <-> Forall (fun b => b = true) (eq_checks eqs k1 k2).
Qed
Proof.
  revert k1 k2.
  induction eqs as [A B eq_a eq_b | A B eq_a rest IH]; intros k1 k2; simpl.
  - rewrite andb_true_iff. split.
    + intros [Ha Hb]. repeat constructor; assumption.
    + intros Hall. inversion Hall as [|? ? Ha Hrest]; subst.
      inversion Hrest as [|? ? Hb _]; subst. split; assumption.
  - rewrite andb_true_iff, IH. split.
    + intros [Ha Hrest]. constructor; assumption.
    + intros Hall. inversion Hall as [|? ? Ha Hrest]; subst. split; assumption.
Qed.
References

Uses

✓ No axioms

Right-nested MergeSources over $n \ge 2$ sources reads as the sequential let! chain, with left-to-right CPending/CFailed precedence and exact origin, and its nested struct-tuple key cuts off iff every component predicate holds.

Certifies let! ... and! ... over $n \ge 2$ sources in Ranvier.CE (MergeSources returning MergedSignals, MergedMemos, or MergedReactive, nested to the right), generalizing the two-source precedence of B19:

  1. eval_merged_chain sc = eval_seq_chain sc: the merged reading is the nested tuple of all $n$ ready values, or else the exact CPending src or CFailed ex origin of the leftmost unready source.
  2. eq_chain eqs k1 k2 = true iff every entry of eq_checks eqs k1 k2 is true.
  3. On a Memo.CreateOn node (memo_on_step, B16) under eq_chain eqs with no settled key, a run that settles sc to CReady k1 followed by a run that settles sc' to CReady k2 keeps mo_compute_runs unchanged on the second run iff eq_chain eqs k1 k2 = true.

Arguments

T : Type
nested struct-tuple type of the merged sources.
U : Type
Phase 2 output type.
merged source chain of the first run.
sc' : source_chain T
merged source chain of the second run.
eqs : eq_spec T
component equality predicates.
self_id : nat
node id of the Memo.CreateOn node.
compute : T -> result U
closed-window Phase 2 body.
st : memo_on_state T U
node state before the first run.
k1 : T
settled key of the first run.
k2 : T
settled key of the second run.
Theorem merge_sources_nary_precedence_eq (T U : Type)
    (sc sc' : source_chain T) (eqs : eq_spec T)
    (self_id : nat) (compute : T -> result U) (st : memo_on_state T U) (k1 k2 : T) :
  eval_merged_chain sc = eval_seq_chain sc /\
  (eq_chain eqs k1 k2 = true <-> Forall (fun b => b = true) (eq_checks eqs k1 k2)) /\
  (mo_has_key st = false ->
     eval_merged_chain sc = CReady k1 -> eval_merged_chain sc' = CReady k2 ->
     let st1 := memo_on_step self_id (eq_chain eqs) compute (eval_merged_chain sc) st in
     let st2 := memo_on_step self_id (eq_chain eqs) compute (eval_merged_chain sc') st1 in
     mo_compute_runs st2 = mo_compute_runs st1 <-> eq_chain eqs k1 k2 = true).

Arguments

T : Type
nested struct-tuple type of the merged sources.
U : Type
Phase 2 output type.
merged source chain of the first run.
sc' : source_chain T
merged source chain of the second run.
eqs : eq_spec T
component equality predicates.
self_id : nat
node id of the Memo.CreateOn node.
compute : T -> result U
closed-window Phase 2 body.
st : memo_on_state T U
node state before the first run.
k1 : T
settled key of the first run.
k2 : T
settled key of the second run.
Qed
Proof.
  split_and!.
  - unfold eval_seq_chain. rewrite eval_seq_chain_k_merged. symmetry. apply reading_bind_ready.
  - apply eq_chain_iff_checks.
  - intros Hno H1 H2. cbv zeta. rewrite H1, H2.
    assert (Hst1 : memo_on_step self_id (eq_chain eqs) compute (CReady k1) st =
      {| mo_has_key := true;
         mo_last_key := k1;
         mo_phase2_out := result_to_reading self_id (compute k1);
         mo_visible := result_to_reading self_id (compute k1);
         mo_scope_gen := S (mo_scope_gen st);
         mo_compute_runs := S (mo_compute_runs st) |})
      by (unfold memo_on_step; rewrite Hno; reflexivity).
    rewrite Hst1. unfold memo_on_step. cbn [mo_has_key mo_last_key mo_compute_runs andb].
    destruct (eq_chain eqs k1 k2); cbn [mo_compute_runs];
      split; intros Hc; [reflexivity | reflexivity | lia | discriminate].
Qed.
References

B22. `AsyncMemo` settled-value cutoff (`#406`), comparer-throw containment, and 1.0 API capability/keyed symmetry (`#417`)

Models AsyncMemo.ApplyResult (src/Ranvier/Core.fs:16170–16215, 16270–16299) when a current flight completes with v:

  • was_ready holds iff Published = true and the node was visibly ready (LaunchFromReady = true for an inline completion during launch, or status = Status.None outside launch), i.e. no intervening visible Pending or Error transition occurred.
  • cmp is the node's value comparer (GraphOptions.Equality or explicit comparer), which may return RVal true, RVal false, or throw RThrow ex. Returns (next_visible, woke) where woke = true iff Wake () (Tracer.Moved + observer notification) fires.
Definition async_settle_completed {U : Type}
    (self_id : nat) (cmp : U -> U -> result bool)
    (was_ready : bool) (prev_v v : U) : comb_reading U * bool :=
  if was_ready then
    match cmp prev_v v with
    | RVal true => (CReady v, false)
    | RVal false => (CReady v, true)
    | RThrow ex => (CFailed ex self_id, true)
    end
  else
    (CReady v, true).
References

Buggy alternative that compares against prev_v whenever published = true, ignoring an intervening CPending transition.

Definition blind_async_settle {U : Type}
    (eq_U : U -> U -> bool) (published : bool) (prev_v v : U) : comb_reading U * bool :=
  if published && eq_U prev_v v then (CReady v, false) else (CReady v, true).
References

Uses

✓ No axioms

AsyncMemo settled-value cutoff suppresses Wake iff was_ready = true and cmp prev_v v = RVal true, wakes observers after any intervening Pending/Error state (was_ready = false), and contains throwing comparers as CFailed ex self_id; whereas a was_ready-blind cutoff loses wakeups after Ready -> Pending -> Ready.

Theorem async_settled_value_cutoff_and_comparer_throw (U : Type)
    (self_id : nat) (cmp : U -> U -> result bool) (prev_v v : U) (ex : nat) :
  (cmp prev_v v = RVal true ->
     async_settle_completed self_id cmp true prev_v v = (CReady v, false)) /\
  (cmp prev_v v = RVal false ->
     async_settle_completed self_id cmp true prev_v v = (CReady v, true)) /\
  (cmp prev_v v = RThrow ex ->
     async_settle_completed self_id cmp true prev_v v = (CFailed ex self_id, true)) /\
  (async_settle_completed self_id cmp false prev_v v = (CReady v, true)) /\
  (forall (eq_U : U -> U -> bool),
     eq_U prev_v prev_v = true ->
     snd (blind_async_settle eq_U true prev_v prev_v) = false /\
     CPending self_id <> CReady prev_v).
Qed
Proof.
  split_and!.
  - intros Heq. unfold async_settle_completed. rewrite Heq. reflexivity.
  - intros Hneq. unfold async_settle_completed. rewrite Hneq. reflexivity.
  - intros Hth. unfold async_settle_completed. rewrite Hth. reflexivity.
  - reflexivity.
  - intros eq_U Hrefl. unfold blind_async_settle. rewrite Hrefl. simpl.
    split; [reflexivity | discriminate].
Qed.
References

Read-only capability projection over a Signal value (ReadOnlySignal<'T> in Core.fs / Api.fs).

Record ro_signal (T : Type) := {
  ro_underlying : T;
}.
References
Arguments Build_ro_signal {T} _.
Arguments ro_underlying {T} _.
Definition ro_get {T : Type} (ro : ro_signal T) : T := ro_underlying ro.
References
Definition ro_try_read {T : Type} (ro : ro_signal T) : comb_reading T := CReady (ro_underlying ro).
References

Open-domain keyed table state (Lookup / MemoTable) mapping allocated live keys to their comb_reading V.

Definition keyed_live_try_get {K V : Type} (eq_K : K -> K -> bool)
    (slots : list (K * comb_reading V)) (k : K) : option (comb_reading V) :=
  match List.find (fun p => eq_K (fst p) k) slots with
  | None => None
  | Some (_, r) => Some r
  end.
References

Uses

Definition keyed_try_read_existing {K V : Type} (eq_K : K -> K -> bool)
    (slots : list (K * comb_reading V)) (k : K) : comb_reading (option V) :=
  match keyed_live_try_get eq_K slots k with
  | None => CReady None
  | Some (CReady v) => CReady (Some v)
  | Some (CPending src) => CPending src
  | Some (CFailed ex orig) => CFailed ex orig
  end.
References
✓ No axioms

ReadOnlySignal<'T> preserves underlying Signal reads and Signal.map composition, while TryGet / tryAt on open-domain Lookup and MemoTable inspects only live slots (None / CReady None when absent without allocating a slot) and forwards Ready / Pending / Failed on live slots.

Theorem readonly_signal_and_keyed_try_symmetry (K T U : Type)
    (s_val : T) (f : T -> result U) (self_id : nat)
    (eq_K : K -> K -> bool) (slots : list (K * comb_reading U)) (k : K) :
  let ro := Build_ro_signal s_val in
  ro_get ro = s_val /\
  ro_try_read ro = CReady s_val /\
  reading_bind (ro_try_read ro) (fun x => result_to_reading self_id (f x)) =
    result_to_reading self_id (f s_val) /\
  (List.find (fun p => eq_K (fst p) k) slots = None ->
     keyed_live_try_get eq_K slots k = None /\
     keyed_try_read_existing eq_K slots k = CReady None).
Qed
Proof.
  cbv zeta. split_and!.
  - reflexivity.
  - reflexivity.
  - reflexivity.
  - intros Hnone. unfold keyed_live_try_get, keyed_try_read_existing.
    unfold keyed_live_try_get. rewrite Hnone. split; reflexivity.
Qed.
References
✓ No axioms

Publishing a nonempty stream snapshot uses the accumulator paired with its folded count. The count and captured state are observed under the same stream lock.

Theorem stream_snapshot_delivery {S : Type} received (folded : nat) (state cached : S) :
  (let count := if received : bool then folded else 0 in
   let captured := if received then state else cached in
   if Nat.eqb count 0 then None else Some (captured, count)) =
  (if received && negb (Nat.eqb folded 0) then Some (state, folded) else None).
Qed
Proof. destruct received, folded; reflexivity. Qed.
References

Uses

  • None
  • Some
  • bool
  • nat
  • negb
  • Nat.eqb