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:
B16(memo_on_cutoff_eq): Two-phase(read, compute)surfaces Phase 1CPending/CFailedreadings while holding(mo_has_key, mo_last_key, mo_phase2_out, mo_scope_gen)intact, restoresmo_phase2_outwith0Phase 2 invocations and unchangedmo_scope_genwhen Phase 1 settles to aneq_K-equal key (including after arbitrary transientCPending/CFailedexcursions), and incrementsmo_scope_genandmo_compute_runsonly on initial oreq_K-unequal settled keys.B17(async_on_flight_preservation_eq): Across all sixFlightPolicyconstructors (CancelPrevious,KeepLatest,Queue,FinishCurrent,Drop,DropWhilePending), settling on aneq_K-equal key clears any Phase 1 overlay andao_owed_trailingwhile preservingao_flight_gen,ao_queued,ao_scope_gen, andao_phase2_slot; Phase 1CPending/CFaileduniformly setsao_phase1_overlaywithout mutatingao_has_key,ao_last_key,ao_flight_gen,ao_queued,ao_fifo,ao_phase2_slot, orao_owed_trailing; unequal settledCReady k'whileao_queued <> 0underFinishCurrent/Drop/DropWhilePendingsetsao_queued = 2without clobberingao_last_key(withowedfaulted settlement resettingao_has_key := falseto launch a fresh flight even onK1 -> K2 -> K1, andowedFinishCurrentRValsettlement holdingao_owed_trailing := trueuntilCReadysettles); background flight completion during an active Phase 1 unready overlay updatesao_phase2_slotand surfaces immediately once Phase 1 recovers toao_last_key; andQueuedrains out-of-order completions in FIFO start order.B18(fused_map_chain_eq_unfused): Over heterogeneous $k$-stagemapchains with arbitrary boolean stage cutoffseq_i(no Leibniz equality requirement) and explicit stateSt(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)andoriginas sequential unfusedMemo.mapevaluation acrossCPending, upstreamCFailed, stage throw at indexi, intermediate cutoff early-exit at stagei, and arbitrary multi-step input traces.B19(map2_zip_status_precedence_eq): Static two-inputeval_map2(both-ready fast path plus left-to-right unready precedence) andeval_zipmatch sequential monadiclet! x = r1 in let! y = r2 in f x yacross all3 * 3comb_readingpairs while preservingSt, failureorigin, and static two-slot upstream edges.B20(split_bind_inner_tick_eq): Splitbindinvokesbinderand discharges the previous outer scope if and only if outer settles to a neweq_K-unequal key, refreshes the bound inner source across arbitrary innerUtransitions with0binderinvocations and0scope discharges, and preserves the outer key, scope, and inner link across transient outerCPending/CFailedexcursions recovering to aneq_K-equal key.B21a(ce_dispatch_and_nested_eval_eq):with_eval_depthandwith_suspended_runreturnce_depthto its entry value on everyRVal/RThrowoutcome;BindReturnat depth0constructs one static node and leaves its callbacks unevaluated, and at depthS devaluates inline with a tracked Phase 1 read, an untracked callback, and zero node allocations of its own.B21b(merge_sources_nary_precedence_eq): Right-nestedMergeSourcesover $n \ge 2$ sources matches the sequentiallet!chain with left-to-rightCPending/CFailedprecedence and exactorigin, 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 :=
Inductive comb_reading (V : Type) : Type :=
| CReady (v : V)
| CPending (src : nat)
| CFailed (ex : nat) (origin : nat).References
Uses
- nat
Used by
- is_unready_reading
- result_to_reading
- project_unready
- memo_on_state
- memo_on_step
- memo_on_steps
- memo_on_steps_unready_preserves
- memo_on_steps_app
64 more
- memo_on_cutoff_eq
- async_on_state
- ao_visible
- async_on_step
- fifo_drain
- async_on_flight_preservation_eq
- unfused_chain
- fused_chain
- eval_fused_stages
- eval_fused_chain
- reading_moved
- eval_unfused_stage
- eval_unfused_chain_go
- eval_unfused_chain
- run_fused_chain
- run_unfused_chain
- tag_to_reading
- opt_ready
- stages_sync
- unfused_false_noop
- unfused_propagate_unready
- eval_fused_stages_eq_unfused
- eval_fused_chain_step_sync
- run_fused_chain_sync
- fused_map_chain_eq_unfused
- comb_bind
- eval_seq_let2
- eval_map2
- eval_zip
- map2_zip_status_precedence_eq
- split_bind_state
- resolve_bound_inner
- split_bind_step
- split_bind_inner_ticks
- split_bind_outer_steps
- split_bind_inner_ticks_preserves
- split_bind_outer_unready_preserves
- split_bind_outer_steps_app
- split_bind_inner_tick_eq
- ce_memo_step
- untracked_wrap
- ce_bind_return_eval
- ce_bind_return_eval_static
- ce_dispatch_and_nested_eval_eq
- source_chain
- merge_readings
- eval_merged_chain
- reading_bind
- eval_seq_chain_k
- eval_seq_chain
- reading_bind_ready
- eval_seq_chain_k_merged
- async_settle_completed
- blind_async_settle
- ro_try_read
- keyed_live_try_get
- keyed_try_read_existing
- readonly_signal_and_keyed_try_symmetry
- Ranvier.Pure.Combinators
- async_on_settle
- merge_sources_nary_precedence_eq
- async_settled_value_cutoff_and_comparer_throw
- async_on_launch
- active_overlay_flight
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 :=
Inductive result (V : Type) : Type :=
| RVal (v : V)
| RThrow (ex : nat).References
Uses
- nat
Used by
- result_to_reading
- memo_on_step
- memo_on_steps
- memo_on_steps_unready_preserves
- memo_on_steps_app
- memo_on_cutoff_eq
- async_on_state
- fifo_record
34 more
- fifo_drain
- async_on_settle
- async_on_flight_preservation_eq
- chain_spec
- unfused_chain
- fused_stages
- eval_unfused_stage
- stages_sync
- fused_map_chain_eq_unfused
- eval_seq_let2
- eval_map2
- map2_zip_status_precedence_eq
- split_bind_state
- resolve_bound_inner
- split_bind_step
- split_bind_inner_ticks
- split_bind_outer_steps
- split_bind_inner_ticks_preserves
- split_bind_outer_unready_preserves
- split_bind_outer_steps_app
- split_bind_inner_tick_eq
- ce_restore_depth
- with_eval_depth
- with_suspended_run
- untracked_wrap
- ce_restore_depth_eq
- ce_dispatch_and_nested_eval_eq
- merge_sources_nary_precedence_eq
- async_settle_completed
- async_settled_value_cutoff_and_comparer_throw
- readonly_signal_and_keyed_try_symmetry
- Ranvier.Pure.Combinators
- eval_fused_stages
- eval_zip
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 :=
Definition is_unready_reading {V : Type} (r : comb_reading V) : bool :=
match r with
| CReady _ => false
| CPending _ | CFailed _ _ => true
end.Converts a callback result on node self_id into a comb_reading attributing throws to self_id.
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 :=
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) := {
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;
}.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}
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.Multi-step iteration of memo_on_step over a sequence of Phase 1 readings.
Fixpoint memo_on_steps {K U : Type}
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
Uses
- comb_reading
- result
- memo_on_state
- memo_on_step
- bool
- list
- nat
Lemma memo_on_steps_unready_preserves {K U : Type}
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.Lemma memo_on_steps_app {K U : Type}
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.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 keys.Theorem memo_on_cutoff_eq (K U : Type)
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 keys.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
Uses
17 more
- mo_last_key
- mo_phase2_out
- mo_visible
- mo_scope_gen
- mo_compute_runs
- memo_on_step
- memo_on_steps
- memo_on_steps_unready_preserves
- memo_on_steps_app
- S
- bool
- false
- list
- nat
- true
- Forall
- andb_false_r
Used by
B17. Closed-window AsyncOn six-policy flight preservation
All six FlightPolicy constructors of Ranvier.FlightPolicy (Types.fs).
Inductive flight_policy : Type :=
Inductive flight_policy : Type :=
| CancelPrevious
| KeepLatest
| Queue
| FinishCurrent
| Drop
| DropWhilePending.Global Instance flight_policy_eq_dec : EqDecision flight_policy.
Defined
Proof. solve_decision. Defined.References
Uses
- flight_policy
- EqDecision
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 :=
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) := {
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;
}.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 :=
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
Uses
1 more
- Some
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 :=
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
Uses
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}
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
Uses
17 more
Records a settled flight outcome in Queue's start-ordered FIFO buffer.
Fixpoint fifo_record {U : Type} (gen : nat) (out : result U)
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.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)
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
- comb_reading
- result
- result_to_reading
- Some
- list
- nat
- option
- Nat.pred
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}
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
Uses
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 :=
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.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):
- Equal settled keys clear
ao_phase1_overlayandao_owed_trailingwhile preservingao_flight_gen,ao_queued,ao_scope_gen,ao_last_key, andao_phase2_slot. - Phase 1
CPending/CFailedreadings uniformly setao_phase1_overlayacross all six policies without mutatingao_has_key,ao_last_key,ao_flight_gen,ao_queued,ao_fifo,ao_phase2_slot, orao_owed_trailing. - Unequal settled
CReady k'during an active deferring flight (ao_queued <> 0underFinishCurrent/Drop/DropWhilePending) setsao_queued = 2while retainingao_last_keyandao_flight_gen. - Under
active_overlay_flight st(active in-flight state across all six policies, covering bothCPendingandCFailedPhase 1 overlays), background settlement updatesao_phase2_slotbehind the overlay and surfaces immediately with zero new launches once Phase 1 settles back to aneq_K-equal key. - When an
owed(ao_queued = 2) flight faults underFinishCurrentorDropWhilePending,async_on_settleresetsao_has_key = falsesoasync_on_stepon anyCReady k(evenk = ao_last_key) launches a fresh flight (ao_queued = 1,ao_flight_gen = S gen); when anowedFinishCurrentflight completes withRVal u,ao_phase2_slotstoresCReady uwhileao_owed_trailing = trueholdsao_visible = CPending ao_self_iduntilasync_on_steponeq_K-equalCReady kclearsao_owed_trailingand surfacesCReady uwith zero new launches. - Under
Queue, out-of-order completionsg2theng1(g1 <> g2) holdao_phase2_slotuntilg1arrives and applyout1thenout2in start order.
Arguments
K keys.owed RThrow ex faulted drop.g1.g2.Queue.Queue.async_on_state before the step.Theorem async_on_flight_preservation_eq (K U : Type)
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 keys.owed RThrow ex faulted drop.g1.g2.Queue.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
Uses
39 more
- project_unready
- Queue
- FinishCurrent
- DropWhilePending
- is_deferring_policy
- async_on_state
- ao_policy
- ao_self_id
- ao_has_key
- ao_last_key
- ao_flight_gen
- ao_queued
- ao_flying
- ao_scope_gen
- ao_phase2_slot
- ao_phase1_overlay
- ao_fifo
- ao_owed_trailing
- ao_visible
- async_on_launch
- async_on_step
- fifo_record
- fifo_drain
- async_on_settle
- active_overlay_flight
- None
- S
- Some
- bool
- false
- nat
- negb
- true
- Nat.eqb
- Nat.eqb_eq
- Nat.eqb_neq
- Nat.eqb_refl
- Nat.pred
- andb_false_r
Used by
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 :=
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.Unfused sequential Memo.map chain state: each stage holds its own cached option (comb_reading M).
Inductive unfused_chain (St : Type) : Type -> Type -> Type :=
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.Fused MapFusion stage spine: each stage stores only its last settled ready value option M.
Inductive fused_stages (St : Type) : Type -> Type -> Type :=
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.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) := {
Record fused_chain (St A B : Type) := {
fc_stages : fused_stages St A B;
fc_tail_out : option (comb_reading B);
}.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 :=
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 :=
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.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 :=
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
Uses
- chain_spec
- fused_chain
- init_fused_stages
- None
- Build_fused_chain
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 :=
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.Direct fused pipeline evaluation with per-stage cutoff early-exit and stage-attributed throws.
Fixpoint eval_fused_stages {St A B : Type}
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
Uses
6 more
- clear_fused_ready
- None
- Some
- option
- A0:252
- B0:253
Evaluates a fused MapFusion chain over root reading r_in.
Definition eval_fused_chain {St A B : Type}
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
Uses
3 more
- eval_fused_stages
- Some
- Build_fused_chain
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)
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
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}
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
Uses
4 more
- bool
- false
- nat
- option
Used by
Sequential unfused evaluation threading (st, up_moved, r_mid) across every Memo.map node.
Fixpoint eval_unfused_chain_go {St A B : Type}
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.Evaluates an unfused Memo.map chain from a root reading r_in.
Definition eval_unfused_chain {St A B : Type}
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.Multi-step trace evaluation of eval_fused_chain over an input list.
Fixpoint run_fused_chain {St A B : Type}
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}
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 :=
Inductive unready_tag : Type :=
| UTPending (src : nat)
| UTFailed (ex : nat) (orig : nat).Global Instance unready_tag_eq_dec : EqDecision unready_tag.
Defined
Proof. solve_decision. Defined.References
Uses
- unready_tag
- EqDecision
Definition tag_to_reading {V : Type} (t : unready_tag) : comb_reading V :=
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.Definition opt_ready {V : Type} (r : option (comb_reading V)) : option V :=
Definition opt_ready {V : Type} (r : option (comb_reading V)) : option V :=
match r with
| Some (CReady v) => Some v
| _ => None
end.Fixpoint set_unfused_unready {St A B : Type} (t : unready_tag) (uc : unfused_chain St A B)
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.Simulation relation linking unfused_chain and (fused_stages, tail_out) across all runs.
Inductive stages_sync {St : Type} :
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).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 :=
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).Lemma init_chain_sync {St A B : Type} (spec : chain_spec St A B) :
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.Lemma clear_fused_ready_idemp {St A B : Type} (fs : fused_stages St A B) :
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) :
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.Lemma reading_moved_tag_refl {V : Type} (eq_V : V -> V -> bool) (t : unready_tag) :
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.Lemma unfused_false_noop {St A B : Type}
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
Uses
3 more
- false
- option
- eq_refl
Lemma unfused_propagate_unready {St A B : Type}
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
Uses
- comb_reading
- CPending
- CFailed
- unfused_chain
- fused_stages
- clear_fused_ready
- reading_moved
- eval_unfused_chain_go
20 more
- unready_tag
- UTPending
- UTFailed
- tag_to_reading
- set_unfused_unready
- stages_sync
- Sync_Single
- Sync_Cons_Unready
- clear_fused_ready_idemp
- set_unfused_unready_idemp
- reading_moved_tag_refl
- unfused_false_noop
- Some
- false
- option
- true
- eq_refl
- Nat.eqb
- Nat.eqb_eq
- decide
Lemma eval_fused_stages_eq_unfused {St A B : Type}
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
Uses
19 more
- UTFailed
- stages_sync
- Sync_Single
- Sync_Cons_Unready
- Sync_Cons_Ready
- clear_fused_ready_idemp
- set_unfused_unready_idemp
- unfused_false_noop
- unfused_propagate_unready
- None
- Some
- negb
- option
- true
- eq_refl
- Nat.eqb
- Nat.eqb_eq
- Nat.eqb_refl
- decide
Used by
Lemma eval_fused_chain_step_sync {St A B : Type}
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.Lemma run_fused_chain_sync {St A B : Type}
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.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):
- From any synchronized state
chain_sync uc fc, single-stepeval_fused_chainandeval_unfused_chainreturn identical(St, comb_reading B)and preservechain_syncacrossCPending, upstreamCFailed, stage throws, intermediate cutoffs, and fullCReadypropagation. - Over any input trace
inputsstarting frominit_fused_chain specandinit_unfused_chain spec,run_fused_chainandrun_unfused_chainyield identical finalSt(allocation/identity state) and output reading sequences. - On an intermediate cutoff (
eq_m old_m v_mid = true) or stage throw (RThrow ex),eval_fused_chainstops at that stage without invoking downstreamreststeps, preservingcached_tailor attributingCFailed ex idto stageid.
Arguments
(node_id, eq_i, step_i) stages.fc.uc.St state.node_id of an inspected head stage.Theorem fused_map_chain_eq_unfused (St A M B : Type)
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
(node_id, eq_i, step_i) stages.fc.uc.St state.node_id of an inspected head stage.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
Uses
Used by
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}
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.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}
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))).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}
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
Uses
4 more
- list
- nat
- negb
- true
Static eval_zip pairing two comb_reading inputs via eval_map2.
Definition eval_zip {A B : Type}
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).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
f.source1.source2.St state before evaluation.source1.source2.Theorem map2_zip_status_precedence_eq (St A B C : Type)
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
f.source1.source2.St state before evaluation.source1.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
Uses
5 more
- nat
- snd
- true
- tt
- unit
Used by
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) := {
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;
}.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}
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.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}
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
Uses
Evaluates a sequence of inner U environment ticks while outer settles to CReady k.
Fixpoint split_bind_inner_ticks {K InnerId U : Type}
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
Uses
Evaluates a sequence of outer comb_reading K transitions against a fixed inner environment inner.
Fixpoint split_bind_outer_steps {K InnerId U : Type}
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.Lemma split_bind_inner_ticks_preserves {K InnerId U : Type}
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
Uses
14 more
- sb_bound
- sb_scope_gen
- sb_discharges
- sb_binder_runs
- sb_visible
- split_bind_step
- split_bind_inner_ticks
- Some
- app
- bool
- list
- nat
- true
- eq_refl
Used by
Lemma split_bind_outer_unready_preserves {K InnerId U : Type}
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
Uses
9 more
- sb_scope_gen
- sb_discharges
- sb_binder_runs
- split_bind_outer_steps
- bool
- list
- nat
- true
- Forall
Used by
Lemma split_bind_outer_steps_app {K InnerId U : Type}
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
Uses
Used by
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):
sb_binder_runsincrements if and only ifr_outer = CReady kwithsb_has_key = falseoreq_K sb_last_key k = false; otherwisesb_binder_runsis unchanged.sb_dischargesincrements if and only ifr_outer = CReady kwithsb_has_key = trueandeq_K sb_last_key k = false; otherwisesb_dischargesis unchanged.- Across any sequence of inner
Utransitionsinners ++ [inner_last]while outer settles to aneq_K-equal keyk,split_bind_inner_ticksupdatessb_visibletoinner_last inner_idwith zerobinderruns and zero scope discharges. - Across any sequence
usof transient outerCPending/CFailedreadings followed byCReady kwitheq_K sb_last_key k = true,split_bind_outer_stepspreservessb_bound,sb_last_key,sb_binder_runs, andsb_discharges, resumingresolve_bound_innerdirectly.
Arguments
Memo<'U> nodes returned by binder.K values.result InnerId.InnerId nodes.U ticks.U tick environments.sb_last_key.inner_id was bound.(tok, result InnerId) outcome preserved across outer unready excursions.split_bind_state before the step or trace.Theorem split_bind_inner_tick_eq (K InnerId U : Type)
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
Memo<'U> nodes returned by binder.K values.result InnerId.InnerId nodes.U ticks.U tick environments.sb_last_key.inner_id was bound.(tok, result InnerId) outcome preserved across outer unready excursions.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
Uses
Used by
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 := {
Record ce_eval_state := {
ce_depth : nat;
ce_tracking : bool;
ce_nodes_created : nat;
}.Definition ce_set_depth (d : nat) (st : ce_eval_state) : ce_eval_state :=
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 |}.Definition ce_set_tracking (t : bool) (st : ce_eval_state) : ce_eval_state :=
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 :=
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) |}.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)
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}
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}
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)).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 :=
Inductive ce_memo_step (U : Type) : Type :=
| OfFactory (node : nat)
| OfInline (r : comb_reading U).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.
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}
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).Lemma ce_restore_depth_eq {A : Type} (d : nat) (st' : ce_eval_state) (res : result A) :
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}
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.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):
with_eval_depth(CeRuntime.CreateDynamicMemo) runsbodyat depthS dandwith_suspended_run(CeRuntime.SuspendNested/InternalCeState.SuspendForNestedRun) runs it at depth0; both end at the entry depthdwith the body's outcomeres, tracking flag, and node count, forRVal vandRThrow exalike viaInternalCeState.ExitEval.- After a dynamic evaluation entered at depth
0ends with any outcome, including a Phase 1NotReadyExceptionor an exception thrown insidematch!, the next top-levelMemoBuilder.BindReturndispatches statically. - At depth
0,ce_bind_return_evalreturnsOfFactory n(MemoStatic) for the fresh noden = ce_nodes_created stand increments the node count by one. The result is independent ofreadandf. - At depth
S d,readruns withce_tracking = true. An unready Phase 1 reading ends the step with its exactCPending srcorCFailed ex origand the entry state, leavingfunevaluated. A ready reading runsfat depthS dwithce_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 off. - A synchronous
CPending srcraised by the untrackedfsurfaces asCFailed (wrap_ex src) self_id. CeRuntime.SuspendNestedaroundMemoRunner.EvalBound(with_suspended_runaroundce_bind_return_eval) constructs one node viaCeRuntime.ProbeBoundand returns to the entry depth, at the top level and for a nestedmemo { ... }inside anowningPhase 2 callback alike.
Arguments
f.body.with_eval_depth or with_suspended_run.origin of a failure raised by f.InvalidOperationException wrapping a NotReadyException on a source.return.S d in the inline-evaluation clauses.Theorem ce_dispatch_and_nested_eval_eq (A U R : Type)
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
f.body.with_eval_depth or with_suspended_run.origin of a failure raised by f.InvalidOperationException wrapping a NotReadyException on a source.return.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
Uses
Used by
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 :=
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).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)
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.Reading of the nested struct tuple of a merged source chain.
Fixpoint eval_merged_chain {T : Type} (sc : source_chain T) : comb_reading T :=
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)
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))).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)
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 :=
Definition eval_seq_chain {T : Type} (sc : source_chain T) : comb_reading T :=
eval_seq_chain_k sc CReady.Component equality predicates of a right-nested struct tuple.
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 :=
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
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 :=
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
Lemma reading_bind_ready {A : Type} (r : comb_reading A) :
Lemma reading_bind_ready {A : Type} (r : comb_reading A) :
reading_bind r CReady = r.
Qed
Proof. destruct r; reflexivity. Qed.Lemma eval_seq_chain_k_merged {T R : Type} (sc : source_chain T) (k : T -> comb_reading R) :
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.Lemma eq_chain_iff_checks {T : Type} (eqs : eq_spec T) (k1 k2 : T) :
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
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:
eval_merged_chain sc = eval_seq_chain sc: the merged reading is the nested tuple of all $n$ ready values, or else the exactCPending srcorCFailed ex originof the leftmost unready source.eq_chain eqs k1 k2 = trueiff every entry ofeq_checks eqs k1 k2istrue.- On a
Memo.CreateOnnode (memo_on_step,B16) undereq_chain eqswith no settled key, a run that settlessctoCReady k1followed by a run that settlessc'toCReady k2keepsmo_compute_runsunchanged on the second run iffeq_chain eqs k1 k2 = true.
Arguments
Memo.CreateOn node.Theorem merge_sources_nary_precedence_eq (T U : Type)
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
Memo.CreateOn node.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
Uses
Used by
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_readyholds iffPublished = trueand the node was visibly ready (LaunchFromReady = truefor an inline completion during launch, orstatus = Status.Noneoutside launch), i.e. no intervening visiblePendingorErrortransition occurred.cmpis the node's value comparer (GraphOptions.Equalityor explicitcomparer), which may returnRVal true,RVal false, or throwRThrow ex. Returns(next_visible, woke)wherewoke = trueiffWake ()(Tracer.Moved+ observer notification) fires.
Definition async_settle_completed {U : Type}
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}
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).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)
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
Uses
5 more
- bool
- false
- nat
- snd
- true
Used by
Read-only capability projection over a Signal value (ReadOnlySignal<'T> in Core.fs / Api.fs).
Record ro_signal (T : Type) := {
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.Definition ro_try_read {T : Type} (ro : ro_signal T) : comb_reading T := CReady (ro_underlying ro).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)
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
- comb_reading
- None
- Some
- bool
- fst
- list
- option
- find
Definition keyed_try_read_existing {K V : Type} (eq_K : K -> K -> bool)
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
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)
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.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) :
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.