From 9b215edae427db1ef4ed5e868cf9dabfe5e75bc2 Mon Sep 17 00:00:00 2001 From: Marc Ilunga Date: Thu, 26 Nov 2026 12:18:54 +0100 Subject: [PATCH] Compare chronological labels under an invariant retained law --- REUSE.md | 1 + src/RandomSystems.lean | 1 + .../ChronologicalLabeling/Sampling.lean | 99 +++++++++++++++++++ src/Tests.lean | 1 + src/Tests/ChronologicalSampling.lean | 79 +++++++++++++++ 5 files changed, 181 insertions(+) create mode 100644 src/RandomSystems/Converter/ChronologicalLabeling/Sampling.lean create mode 100644 src/Tests/ChronologicalSampling.lean diff --git a/REUSE.md b/REUSE.md index 3047cc7..8260e5e 100644 --- a/REUSE.md +++ b/REUSE.md @@ -77,6 +77,7 @@ named `budget` argument. The object index below supplies owners for those declar | Extend distinct list-position labels to evaluated inputs | `List.exists_perm_apply_idxOf_eq` in `Probability.List.Permutation` | | Evaluate a completed adaptive invocation from its local trace | `Program.combine_eq_of_consistent_replies` in `RandomSystems.Converter.ProgramTrace` | | Certify a scheduled collection and attach it to retained state | `DSL.collectCalls_exchangesOf` in `RandomSystems.Typed.DSL.Collect`; `Program.combine_eq_of_collectCalls` in `CollectAttachment` | +| Compare chronological labels under an invariant retained-function law | `chronologicalLabelingProgram_mass_replies_eq_of_fTransform_eq` in `RandomSystems.Converter.ChronologicalLabeling.Sampling` | | Transport complete register converters through address embeddings | `registersDDC_mapCommand` in `Commons.Definitions.AbstractModel.RegisterDDC.AddressMap` | | Transport oracle bodies through state and outside-query maps | `Program.step_stateInputMap` in `RandomSystems.Converter.Program`; `DSL.callProg_map` in `RandomSystems.Typed.DSL.Programs` | | Rename register addresses while preserving contents | `AbstractModel.mapInput`, `mapCommand`, `registers_mapCommand` in `Commons.Definitions.AbstractModel` and `Registers.Laws` | diff --git a/src/RandomSystems.lean b/src/RandomSystems.lean index 8741827..e64f7a6 100644 --- a/src/RandomSystems.lean +++ b/src/RandomSystems.lean @@ -160,6 +160,7 @@ public import RandomSystems.Converter.ChronologicalLabeling.Laws public import RandomSystems.Converter.ProgramTrace public import RandomSystems.Typed.DSL.CollectAttachment +public import RandomSystems.Converter.ChronologicalLabeling.Sampling /-! diff --git a/src/RandomSystems/Converter/ChronologicalLabeling/Sampling.lean b/src/RandomSystems/Converter/ChronologicalLabeling/Sampling.lean new file mode 100644 index 0000000..be5c82d --- /dev/null +++ b/src/RandomSystems/Converter/ChronologicalLabeling/Sampling.lean @@ -0,0 +1,99 @@ +module + +public import RandomSystems.Converter.ChronologicalLabeling.Laws +public import RandomSystems.System.Replies +public import Probability.Distribution + +/-! +# Chronological labeling under a retained function law + +`chronologicalLabelingProgram_replies_iff_of_prefix_alignment` compares chronological and +evaluated inputs on one history. Its one-call replay law supplies the deterministic comparison. +`chronologicalLabelingProgram_mass_replies_eq_of_fTransform_eq` then transports this comparison +through any retained function law invariant under the selected input map. + +```text +outside a ── chronological label ── θ ── retained g ── reply + = evaluated input a ────────── retained g ── reply +``` +-/ + +@[expose] public section + +open Probability Probability.Distribution + +namespace SystemAlgebra + +-- `A` carries outside inputs, `B` the finite labels, and `C` the forwarded replies. +variable {A B C : Type} [BEq A] [Fintype B] + -- `labels` orders the label pool; exhausted fresh inputs use `fallback`. + (labels : Fin (Fintype.card B) ≃ B) (fallback : B) + -- `E` admits inside histories and `F` outside histories. + {E : List (Σ _ : Unit, B) → Prop} {F : List (Σ _ : Unit, A) → Prop} + -- `hE` and `hF` exclude empty histories and admit every nonempty prefix. + (hE : ¬ E [] ∧ ∀ {p h}, p <+: h → p ≠ [] → E h → E p) + (hF : ¬ F [] ∧ ∀ {p h}, p <+: h → p ≠ [] → F h → F p) + -- `domain` certifies the encoder's one-call action from `E` to `F`. + (domain : IsDDCFrom E F 1 + (DDC.ofProgramOn [] (chronologicalLabelingProgram (C := C) labels fallback) F)) + -- `θ` maps label inputs; `evaluate` maps the original outside inputs to inside inputs. + (θ : B → B) (evaluate : A → B) + -- `us` is the fixed outside history whose coefficient is compared. + (us : List (Σ _ : Unit, A)) + -- `aligned` identifies the label mapped by `θ` with `evaluate a` at admitted prefixes of `us`. + (aligned : ∀ vs a, vs ++ [⟨(), a⟩] <+: us → F (vs ++ [⟨(), a⟩]) → + θ (if h : (vs.map Sigma.snd ++ [a]).eraseDups.idxOf a < Fintype.card B then + labels ⟨(vs.map Sigma.snd ++ [a]).eraseDups.idxOf a, h⟩ else fallback) = evaluate a) + +include domain aligned + +/-- On the selected prefixes, input relabeling turns chronological replies into evaluation. -/ +lemma chronologicalLabelingProgram_replies_iff_of_prefix_alignment + -- `g` is retained across all calls; `ys` lists their candidate replies. + (g : B → C) (ys : List (Σ _ : Unit, C)) : + Replies (trim (apply + (DDC.ofProgramOn [] (chronologicalLabelingProgram labels fallback) F) + (DDS.ofFunction E hE (fun b => ⟨b.1, g (θ b.2)⟩)).1)) us ys ↔ + Replies (DDS.ofFunction F hF (fun a => ⟨a.1, g (evaluate a.2)⟩)).1 us ys := by + classical + -- Every completed reply depends only on its input prefix. + apply replies_congr_of_prefix + intro p hp + rcases List.eq_nil_or_concat p with rfl | ⟨vs, ⟨o, a⟩, rfl⟩ + · simp only [trim, reach_nil, ite_true, apply_eq, interconnect_nil] + exact (Part.eq_none_iff'.mpr (DDS.ofFunction F hF + (fun a => (⟨a.1, g (evaluate a.2)⟩ : Σ _ : Unit, C))).2.1).symm + · cases o + simp only [List.concat_eq_append] at hp ⊢ + -- The replay law keeps the original domain; alignment identifies its admitted reply. + rw [chronologicalLabelingProgram_apply_ofFunction_snoc labels fallback hE hF domain + (fun b => g (θ b)) vs a, DDS.ofFunction_snoc] + by_cases legal : F (vs ++ [⟨(), a⟩]) + · simp only [legal, ↓reduceIte, aligned vs a hp legal] + · simp only [legal, ↓reduceIte] + +/-- A retained function law invariant under `g ↦ g ∘ θ` has equal transcript coefficients. -/ +theorem chronologicalLabelingProgram_mass_replies_eq_of_fTransform_eq + -- `μ` assigns finite signed weights to functions retained across all calls. + (μ : Distribution (B → C)) + -- `invariant` says replacing `g` by `g ∘ θ` preserves this same retained law. + (invariant : fTransform (fun g => g ∘ θ) μ = μ) + -- `ys` is the fixed reply history, including any repeated replies. + (ys : List (Σ _ : Unit, C)) : + μ.mass (fun g => Replies (trim (apply + (DDC.ofProgramOn [] (chronologicalLabelingProgram labels fallback) F) + (DDS.ofFunction E hE (fun b => ⟨b.1, g b.2⟩)).1)) us ys) = + μ.mass (fun g => + Replies (DDS.ofFunction F hF (fun a => ⟨a.1, g (evaluate a.2)⟩)).1 us ys) := by + let event := fun g : B → C => Replies (trim (apply + (DDC.ofProgramOn [] (chronologicalLabelingProgram labels fallback) F) + (DDS.ofFunction E hE (fun b => ⟨b.1, g b.2⟩)).1)) us ys + -- Reindex the retained draw, then use deterministic prefix agreement at each function. + calc + μ.mass event = (fTransform (fun g => g ∘ θ) μ).mass event := by rw [invariant] + _ = μ.mass (fun g => event (g ∘ θ)) := mass_fTransform _ _ _ + _ = _ := mass_congr μ fun g => + chronologicalLabelingProgram_replies_iff_of_prefix_alignment + labels fallback hE hF domain θ evaluate us aligned g ys + +end SystemAlgebra diff --git a/src/Tests.lean b/src/Tests.lean index a269a76..45ffd61 100644 --- a/src/Tests.lean +++ b/src/Tests.lean @@ -235,6 +235,7 @@ public import Tests.RegisterAddressCompiler public import Tests.ProgramTrace public import Tests.CollectCallsTrace public import Tests.CollectAttachment +public import Tests.ChronologicalSampling public import Tests.ListPermutation diff --git a/src/Tests/ChronologicalSampling.lean b/src/Tests/ChronologicalSampling.lean new file mode 100644 index 0000000..879378b --- /dev/null +++ b/src/Tests/ChronologicalSampling.lean @@ -0,0 +1,79 @@ +module + +public import RandomSystems.Converter.ChronologicalLabeling.Sampling +public import Tests.ChronologicalLabeling + +/-! +# Retained sampling through chronological labels + +A nonuniform law chooses one constant function, retained across repeated queries. +Collapsing both label inputs to zero preserves that law. Its positive repeated-reply mass +and zero conflicting-reply mass exercise the coefficient theorem with a noninjective map. +-/ + +@[expose] public section + +namespace Tests.ChronologicalSampling + +open Probability Probability.Distribution SystemAlgebra Tests.ChronologicalLabeling + +/-- One retained function is constantly `42` with mass `1/3`, or constantly `7` with mass `2/3`. -/ +noncomputable def retainedLaw : Distribution (Fin 2 → ℕ) := + Finsupp.single (fun _ => 42) (1 / 3) + Finsupp.single (fun _ => 7) (2 / 3) + +/-- Collapsing both labels to `0` leaves either constant function unchanged. -/ +lemma retainedLaw_invariant : + fTransform (fun g : Fin 2 → ℕ => g ∘ fun _ => (0 : Fin 2)) retainedLaw = retainedLaw := by + rw [retainedLaw, fTransform_add, fTransform_single, fTransform_single] + rfl + +/-- The chosen input map is genuinely noninjective. -/ +example : ¬ Function.Injective (fun _ : Fin 2 => (0 : Fin 2)) := by + intro injective + have equal : (0 : Fin 2) = 1 := injective rfl + norm_num at equal + +-- `labels` may use either enumeration of the two-label pool. +variable (labels : Fin (Fintype.card (Fin 2)) ≃ Fin 2) + +/-- Three replies use one retained draw, so their joint mass is `1/3`. -/ +lemma repeated_reply_mass : + retainedLaw.mass (fun g => Replies (trim (apply + (DDC.ofProgramOn [] (chronologicalLabelingProgram labels 0) (outsideDomain 3)) + (DDS.ofFunction (insideDomain 3) (insideDomain_prefix 3) + (fun b => ⟨b.1, g b.2⟩)).1)) + [⟨(), 7⟩, ⟨(), 9⟩, ⟨(), 7⟩] [⟨(), 42⟩, ⟨(), 42⟩, ⟨(), 42⟩]) = 1 / 3 := by + classical + have coefficient := chronologicalLabelingProgram_mass_replies_eq_of_fTransform_eq + labels 0 (insideDomain_prefix 3) (outsideDomain_prefix 3) + (chronologicalLabelingProgram_isDDCFrom labels 0 3) + (fun _ => (0 : Fin 2)) (fun _ : ℕ => (0 : Fin 2)) + [⟨(), 7⟩, ⟨(), 9⟩, ⟨(), 7⟩] (fun _ _ _ _ => rfl) + retainedLaw retainedLaw_invariant [⟨(), 42⟩, ⟨(), 42⟩, ⟨(), 42⟩] + rw [coefficient] + norm_num [retainedLaw, mass_add, mass_single, DDS.replies_ofFunction_iff, outsideDomain] + +/-- Inconsistent replies on the repeated input cannot come from the retained draw. -/ +lemma conflicting_reply_mass : + retainedLaw.mass (fun g => Replies (trim (apply + (DDC.ofProgramOn [] (chronologicalLabelingProgram labels 0) (outsideDomain 3)) + (DDS.ofFunction (insideDomain 3) (insideDomain_prefix 3) + (fun b => ⟨b.1, g b.2⟩)).1)) + [⟨(), 7⟩, ⟨(), 9⟩, ⟨(), 7⟩] [⟨(), 42⟩, ⟨(), 42⟩, ⟨(), 7⟩]) = 0 := by + classical + have coefficient := chronologicalLabelingProgram_mass_replies_eq_of_fTransform_eq + labels 0 (insideDomain_prefix 3) (outsideDomain_prefix 3) + (chronologicalLabelingProgram_isDDCFrom labels 0 3) + (fun _ => (0 : Fin 2)) (fun _ : ℕ => (0 : Fin 2)) + [⟨(), 7⟩, ⟨(), 9⟩, ⟨(), 7⟩] (fun _ _ _ _ => rfl) + retainedLaw retainedLaw_invariant [⟨(), 42⟩, ⟨(), 42⟩, ⟨(), 7⟩] + rw [coefficient] + norm_num [retainedLaw, mass_add, mass_single, DDS.replies_ofFunction_iff, outsideDomain] + +#print axioms chronologicalLabelingProgram_replies_iff_of_prefix_alignment +#print axioms chronologicalLabelingProgram_mass_replies_eq_of_fTransform_eq +#print axioms retainedLaw_invariant +#print axioms repeated_reply_mass +#print axioms conflicting_reply_mass + +end Tests.ChronologicalSampling