diff --git a/formal/README.md b/formal/README.md index f56e9c5d5..ae1712d5b 100644 --- a/formal/README.md +++ b/formal/README.md @@ -14,7 +14,7 @@ cd .. python formal/gate.py --execute ``` -The source-only command does not run Lean. The execute command fails unless the pinned project builds, `leanchecker` rechecks the package, all 20 declarations have transitive axiom reports restricted to propext/Classical.choice/Quot.sound, and five executable negative controls are rejected. It writes logs and a receipt in `.lake/formal-evidence/`; the workflow uploads them even on failure. A receipt from arbitrary input is not trusted evidence: use the successful read-only workflow and checked Git commit. +The source-only command does not run Lean. The execute command fails unless the pinned project builds, `leanchecker` rechecks the package, all 29 declarations have transitive axiom reports restricted to propext/Classical.choice/Quot.sound, and five executable negative controls are rejected. It writes logs and a receipt in `.lake/formal-evidence/`; the workflow uploads them even on failure. A receipt from arbitrary input is not trusted evidence: use the successful read-only workflow and checked Git commit. Sources: [scope](SCOPE.md), [glossary](GLOSSARY.md), [manifest](manifest.json), [blueprint source](blueprint/src/content.tex). The original GP-FOR-192 bytes are preserved under originals/*.lean.txt; separately named V2 companions carry the documented compiler-only repairs in [COMPATIBILITY.md](COMPATIBILITY.md). No historical Status.lean or claimed status is imported. [Alignment contract](SCOPE.md#review-contract) remains distinct from kernel evidence. @@ -26,8 +26,12 @@ Sources: [scope](SCOPE.md), [glossary](GLOSSARY.md), [manifest](manifest.json), The additive [MeasureBridge](MEASURE_BRIDGE.md) contributes five same-law measure-theoretic/conditional-transfer declarations and two scalar counterexamples. It supplies the actual Cauchy–Schwarz numerator step, while the concrete moment estimate, lower normalizer and input event tail remain explicit premises. The original 13 scalar declarations, toolchain and gate are unchanged. The two counterexamples are audited declarations, not extra rejection runs. +## Constructed weighted probability law + +The additive [WeightedLaw](WEIGHTED_LAW.md) contributes nine declarations. It constructs the normalized nonnegative density measure, proves total mass one and the event-ratio identity, and applies the retained transfer bounds to its actual event probabilities. It also proves absolute continuity, almost-everywhere invariance, and zero/unit-weight controls. The original 20 theorem statements and proofs are unchanged; concrete Palm-model identification and field estimates remain separate. + ## Next agents -Read the current source-bound PR and exact head first; main #95 is historical rollout context. A non-OpenAI alignment reviewer should inspect all assumptions, the source correspondence and the explicit nonclaims. An independent engineering reviewer should attack omitted targets, imports carrying custom axioms, stale scope/hash/review records and altered toolchains. A probability formalizer should next construct the normalized reweighted probability measure and then discharge a concrete same-law moment/lower-normalizer interface. Do not import GP214's upper bound as the lower bound. The MeasureBridge source is supplied but has no self-awarded kernel or alignment status. Coordinate before editing; a review request is not a claim that a reviewer is active. +Read the current source-bound PR and exact head first; main #95 is historical rollout context. A non-OpenAI alignment reviewer should inspect all assumptions, the source correspondence and the explicit nonclaims. An independent engineering reviewer should attack omitted targets, imports carrying custom axioms, stale scope/hash/review records and altered toolchains. A probability formalizer should next identify the constructed weighted law with the concrete typed Palm model and discharge its same-law moment/lower-normalizer interface. Do not import GP214's upper bound as the lower bound. The MeasureBridge source is supplied but has no self-awarded kernel or alignment status. Coordinate before editing; a review request is not a claim that a reviewer is active. Blueprint declaration links are checked against the target inventory and compiled declarations, not by semantic equivalence. The full plasTeX/LaTeX website is not installed in this pilot. Optional independent checkers or AI proof generators may be added with pinned versions/budgets; their availability and execution must be separately recorded. Never treat AlphaProof as an assumed publicly callable CI service or a generator's success as an independent alignment review. diff --git a/formal/ResearchFormalCoreR1.lean b/formal/ResearchFormalCoreR1.lean index 772d991c0..9c1547f7a 100644 --- a/formal/ResearchFormalCoreR1.lean +++ b/formal/ResearchFormalCoreR1.lean @@ -1,3 +1,4 @@ import ResearchFormalCoreR1.AlgebraV2 import ResearchFormalCoreR1.ProbabilityCompanionsV2 import ResearchFormalCoreR1.MeasureBridge +import ResearchFormalCoreR1.WeightedLaw diff --git a/formal/ResearchFormalCoreR1/WeightedLaw.lean b/formal/ResearchFormalCoreR1/WeightedLaw.lean new file mode 100644 index 000000000..b51ca969a --- /dev/null +++ b/formal/ResearchFormalCoreR1/WeightedLaw.lean @@ -0,0 +1,127 @@ +import ResearchFormalCoreR1.MeasureBridge + +/-! +# A normalized nonnegative weighted probability law + +This supplies the probability-law construction deliberately excluded from +MeasureBridge. The concrete field, conditioning, moment, normalizer and tail +estimates are not constructed. See ../WEIGHTED_LAW.md for exact scope. +-/ + +namespace ResearchFormalCoreR1 + +open MeasureTheory +open scoped ENNReal + +set_option autoImplicit false + +variable {Ω : Type*} [MeasurableSpace Ω] + +/-- A measure for any W; it is a probability measure only under the hypotheses +proved below. `ofReal` clips negative values, so a.e. nonnegativity is essential +when identifying its density integral with the signed Bochner integral. -/ +noncomputable def weightedLaw (μ : Measure Ω) (W : Ω → ℝ) : Measure Ω := + (ENNReal.ofReal (∫ x, W x ∂μ))⁻¹ • + μ.withDensity (fun x => ENNReal.ofReal (W x)) + +/-- Measurable-event mass before taking real values. Genuine integrability and +nonnegativity identify the density integral with the Bochner set integral. -/ +theorem weightedLaw_apply + (μ : Measure Ω) (W : Ω → ℝ) (A : Set Ω) + (hW : Integrable W μ) (hW0 : 0 ≤ᵐ[μ] W) (hA : MeasurableSet A) : + weightedLaw μ W A = (ENNReal.ofReal (∫ x, W x ∂μ))⁻¹ * + ENNReal.ofReal (∫ x in A, W x ∂μ) := by + simp only [weightedLaw, Measure.smul_apply, smul_eq_mul, withDensity_apply _ hA] + rw [← ofReal_integral_eq_lintegral_ofReal hW.integrableOn (ae_restrict_of_ae hW0)] + +/-- Total mass one follows from a positive finite normalizer. This theorem does +not assume that the measure it constructs is already a probability measure. -/ +theorem weightedLaw_isProbabilityMeasure + (μ : Measure Ω) (W : Ω → ℝ) + (hW : Integrable W μ) (hW0 : 0 ≤ᵐ[μ] W) + (hz : 0 < ∫ x, W x ∂μ) : IsProbabilityMeasure (weightedLaw μ W) := by + constructor + rw [weightedLaw_apply μ W Set.univ hW hW0 MeasurableSet.univ] + simp only [Measure.restrict_univ] + exact ENNReal.inv_mul_cancel (ne_of_gt (ENNReal.ofReal_pos.mpr hz)) + ENNReal.ofReal_ne_top + +/-- The real event probability is the same-law integral ratio. The positive +normalizer is retained explicitly for its intended probability interpretation. -/ +theorem weightedLaw_event_real + (μ : Measure Ω) (W : Ω → ℝ) (A : Set Ω) + (hW : Integrable W μ) (hW0 : 0 ≤ᵐ[μ] W) (hA : MeasurableSet A) + (hz : 0 < ∫ x, W x ∂μ) : + (weightedLaw μ W A).toReal = (∫ x in A, W x ∂μ) / (∫ x, W x ∂μ) := by + rw [weightedLaw_apply μ W A hW hW0 hA, ENNReal.toReal_mul, + ENNReal.toReal_inv, ENNReal.toReal_ofReal hz.le, + ENNReal.toReal_ofReal (integral_nonneg_of_ae (ae_restrict_of_ae hW0))] + ring + +/-- A null event under the original law is also null under the weighted law. +No independence or strict positivity of W is required for absolute continuity. -/ +theorem weightedLaw_absolutelyContinuous + (μ : Measure Ω) (W : Ω → ℝ) : weightedLaw μ W ≪ μ := by + intro A hA + have hzero := (withDensity_absolutelyContinuous μ (fun x => ENNReal.ofReal (W x))) hA + simp only [weightedLaw, Measure.smul_apply, smul_eq_mul, hzero, mul_zero] + +/-- Changing W on a null set changes neither its normalizer nor the law. -/ +theorem weightedLaw_congr_ae + (μ : Measure Ω) (W V : Ω → ℝ) (h : W =ᵐ[μ] V) : + weightedLaw μ W = weightedLaw μ V := by + have hd : (fun x => ENNReal.ofReal (W x)) =ᵐ[μ] + (fun x => ENNReal.ofReal (V x)) := + h.mono (fun _ hx => congrArg ENNReal.ofReal hx) + unfold weightedLaw + rw [integral_congr_ae h, withDensity_congr_ae hd] + +/-- The zero weight gives the zero measure, not a probability law. This exposes +why normalization positivity cannot be removed from the construction theorem. -/ +theorem weightedLaw_zero (μ : Measure Ω) : + weightedLaw μ (fun _ : Ω => (0 : ℝ)) = 0 := by + simp [weightedLaw] + +/-- A unit weight on a probability space recovers the original probability law. -/ +theorem weightedLaw_one (μ : Measure Ω) [IsProbabilityMeasure μ] : + weightedLaw μ (fun _ : Ω => (1 : ℝ)) = μ := by + simp [weightedLaw] + +/-- P02-LM-008 for the actual constructed measure, including its probability +property. The concrete moment and lower-normalizer estimates remain premises. -/ +theorem p02_lm008_probability_transfer + (μ : Measure Ω) [IsProbabilityMeasure μ] (W : Ω → ℝ) (A : Set Ω) + (hW : MemLp W 2 μ) (hW0 : 0 ≤ᵐ[μ] W) (hA : MeasurableSet A) + (r cW cZ : ℝ) (hr : 0 < r) (hcW : 0 ≤ cW) (hcZ : 0 < cZ) + (hsecond : (∫ x, W x ^ 2 ∂μ) ≤ cW * r ^ 4) + (hlower : cZ * r ^ 2 ≤ ∫ x, W x ∂μ) : + IsProbabilityMeasure (weightedLaw μ W) ∧ + (weightedLaw μ W A).toReal ≤ (Real.sqrt cW / cZ) * Real.sqrt ((μ A).toReal) := by + have hi : Integrable W μ := hW.integrable (by norm_num) + have hz : 0 < ∫ x, W x ∂μ := + lt_of_lt_of_le (mul_pos hcZ (sq_pos_of_pos hr)) hlower + refine ⟨weightedLaw_isProbabilityMeasure μ W hi hW0 hz, ?_⟩ + rw [weightedLaw_event_real μ W A hi hW0 hA hz] + exact p02_lm008_measure_transfer μ W A hW hW0 hA r cW cZ hr hcW hcZ hsecond hlower + +/-- The supplied eighth-order event bound transfers to the constructed law's +cubic bound. Neither the event bound nor uniformity in r is proved here. -/ +theorem p02_lm009_probability_transfer_r8_to_r3 + (μ : Measure Ω) [IsProbabilityMeasure μ] (W : Ω → ℝ) (A : Set Ω) + (hW : MemLp W 2 μ) (hW0 : 0 ≤ᵐ[μ] W) (hA : MeasurableSet A) + (r cW cZ K : ℝ) (hr : 0 < r) (hr1 : r ≤ 1) + (hcW : 0 ≤ cW) (hcZ : 0 < cZ) (hK : 0 ≤ K) + (hsecond : (∫ x, W x ^ 2 ∂μ) ≤ cW * r ^ 4) + (hlower : cZ * r ^ 2 ≤ ∫ x, W x ∂μ) + (htail : (μ A).toReal ≤ K * r ^ 8) : + IsProbabilityMeasure (weightedLaw μ W) ∧ + (weightedLaw μ W A).toReal ≤ ((Real.sqrt cW / cZ) * Real.sqrt K) * r ^ 3 := by + have hi : Integrable W μ := hW.integrable (by norm_num) + have hz : 0 < ∫ x, W x ∂μ := + lt_of_lt_of_le (mul_pos hcZ (sq_pos_of_pos hr)) hlower + refine ⟨weightedLaw_isProbabilityMeasure μ W hi hW0 hz, ?_⟩ + rw [weightedLaw_event_real μ W A hi hW0 hA hz] + exact p02_lm009_measure_transfer_r8_to_r3 μ W A hW hW0 hA r cW cZ K + hr hr1 hcW hcZ hK hsecond hlower htail + +end ResearchFormalCoreR1 diff --git a/formal/SCOPE.md b/formal/SCOPE.md index 014919e8e..7fc346b8d 100644 --- a/formal/SCOPE.md +++ b/formal/SCOPE.md @@ -18,7 +18,7 @@ GP214's upper bound Z_r <= 15 r^2 cannot discharge the required lower bound cZ r ## Additive measure-theoretic bridge (seven new declarations) -The package now registers 20 declarations: the original 13 above and seven in +This retained layer consists of 20 declarations: the original 13 above and seven in `ResearchFormalCoreR1/MeasureBridge.lean`. The exact statement mapping and retained hypotheses are in the hash-bound [bridge scope](MEASURE_BRIDGE.md). @@ -37,6 +37,24 @@ the gate and its five executable rejection controls remain unchanged. The two new counterexamples are included in the normal target/axiom inventory. The changed manifest and scope require a fresh separately authenticated alignment review. +## Additive weighted probability law (nine further declarations) + +The total inventory is now 29 theorem targets. [WEIGHTED_LAW.md](WEIGHTED_LAW.md) +is the hash-bound exact scope for the new `WeightedLaw.lean` module. It consumes +all earlier formal source unchanged and supplies the abstract PT1 construction +excluded by MeasureBridge: an integrable a.e.-nonnegative weight with positive +normalizer defines a probability measure, whose real mass on measurable events +is the same-law integral ratio. Null sets and a.e.-equal weights are respected; +zero and unit weights are checked. The two final declarations compose this +constructed measure with the existing conditional square-root and cubic bounds. + +This construction does not identify a concrete Gaussian/typed Palm law or prove +its moment, lower-normalizer, event-tail, uniformity or persistence hypotheses. +The previous scope exclusions describe their respective modules; the abstract +construction alone is added here. The executable gate and five negative controls +are unchanged, and all nine new targets enter its transitive axiom inventory. +No existing review is silently rebound to the changed manifest or scope. + ## Review contract The source manifest is an evidence sidecar, not another scientific register. A trusted successful workflow establishes kernel evidence only for these target declarations and their displayed hypotheses, relative to Lean's kernel/standard foundations/toolchain. Independent review must compare each Lean statement and definition to this scope note and the source actually cited. Review author, provider, family and agent must be explicit. Validate an authenticated record with `python formal/gate.py --alignment review.json`; then the existing controlling gate must decide whether its wider requirements are met. The validator does not authenticate a review merely because a JSON string names a reviewer. diff --git a/formal/WEIGHTED_LAW.md b/formal/WEIGHTED_LAW.md new file mode 100644 index 000000000..ab1ece70a --- /dev/null +++ b/formal/WEIGHTED_LAW.md @@ -0,0 +1,80 @@ +# Constructed weighted probability law + +Scientific effect: NONE. Dylan Roy — delegated AI work; actual performer OpenAI / +GPT-6 Astra Pro, 2026-10-05. This is an additive successor to Math-#272, not a +rewrite or acceptance of its proof. Independent statement alignment is pending. + +## Design and exact source correspondence + +The source is P02-LM-008 (LCR-CAP-024-v1.0), §§2–3 and PT1 in §4, at +`d6g8k5htny-coder/Math-`, commit +`22876edaaec054d3ab8b0b16668ce3ff8cbf8c73`, path +`imports/hardening_ebedb780/P02-LM-008/proof.md.export.txt`. + +The consumed formal bridge is Math-#272 at +`270167900b71539d0e2135131a3a96f1f04289f6`, module +`formal/ResearchFormalCoreR1/MeasureBridge.lean`, SHA-256 +`28ac130db490c3c3e2fbfabf3ac5be43c854ab1468bf573bf5a8c0dd2c50955f`. +It is preserved byte-for-byte. The dependency is a kernel-checked candidate, +not a claim that #272 is merged or independently aligned. + +For one measure μ and real function W, the definition is + + weightedLaw μ W = ofReal(∫ W dμ)⁻¹ • μ.withDensity (ofReal ∘ W). + +This is a measure by construction, including countable additivity. Its probability +property is proved rather than assumed, under W integrable, W ≥ 0 almost everywhere, +and ∫ W dμ > 0. No finiteness or probability hypothesis on μ is needed for this +construction theorem. The applications additionally take μ to be a probability +measure and derive integrability from `MemLp W 2 μ`. + +## Nine new audited declarations + +| Suffix in ResearchFormalCoreR1 | Exact coverage | +|---|---| +| weightedLaw_apply | Measurable-event ENNReal mass equals inverse ofReal normalizer times ofReal set integral. | +| weightedLaw_isProbabilityMeasure | Integrability, a.e. nonnegativity and positive normalizer imply total mass one. | +| weightedLaw_event_real | Under those conditions, real event mass is (∫_A W dμ)/(∫ W dμ). | +| weightedLaw_absolutelyContinuous | Original-law null sets remain null; no independence or W > 0 assumption. | +| weightedLaw_congr_ae | Null-set changes of the weight leave the entire measure unchanged. | +| weightedLaw_zero | The zero weight yields zero measure, exposing the need for positive normalization. | +| weightedLaw_one | Unit weight on a probability space recovers μ. | +| p02_lm008_probability_transfer | Constructs a probability law and proves its event bound from the same moment/lower-normalizer assumptions as #272. | +| p02_lm009_probability_transfer_r8_to_r3 | Constructs that law and composes the supplied eighth-order event bound with the cubic transfer. | + +The new definition is consumed transitively by these audited targets. The package +now has 29 theorem targets; the original 20 theorem statements and proof bytes are +unchanged. The older bridge scope's PT1 exclusion describes that earlier module; +this new module fills the abstract probability-law construction only. + +## Essential hypotheses and nonclaims + +`ofReal` discards negative values. Without a.e. nonnegativity its density integral +need not equal the Bochner normalizer: weights (-1,3) on two equal atoms give +normalizer 1 but clipped normalized total mass 3/2. A zero normalizer gives no +probability theorem. Real integral hypotheses never replace integrability with +an inequality on Lean's totalized integral of a divergent function. + +This does not identify the constructed law with the program's concrete typed +Palm/conditioned field law. Concrete model correspondence, moment bounds, lower +normalizers, event tails, constant uniformity, GT5, P0.2, and the parent persistence +theorem remain separate obligations. No new hypothesis independence is asserted. +Historical analytical reviews are not inherited by these new formal statements. + +## Verification and handoff + +Lean 4.34.1, mathlib d13f23b723b8a846827a245b89c10fc7d3f11612, the original gate, +all five executable rejection controls, and the hosted workflow are unchanged. +The same gate must build, recheck, extract types, and audit all 29 targets. +The two prior scalar counterexamples remain target propositions, not rejection runs. + +Thirteen focused Python tests include 208 exact rational event cases, null atoms, +a.e.-equal representatives, constant and rescaled weights, finite partitions, +zero-normalizer and signed-weight failures. These are finite falsification/wiring +checks, not a proof of countable additivity or a substitute for the Lean kernel. +The prior bridge-inventory test now checks its exact retained 13:20 slice; this +successor checks the complete 20:29 extension and total 29, so no target is dropped. + +Review work on #272 stays isolated from this successor. Any helper must report its +actual model/provider/runtime and claimed scope. A dispatch, automated code review, +or successful build does not supply independent semantic alignment by itself. diff --git a/formal/manifest.json b/formal/manifest.json index 8bb1c8959..cf8894a2d 100644 --- a/formal/manifest.json +++ b/formal/manifest.json @@ -22,7 +22,8 @@ "source_modules": [ "ResearchFormalCoreR1/AlgebraV2.lean", "ResearchFormalCoreR1/ProbabilityCompanionsV2.lean", - "ResearchFormalCoreR1/MeasureBridge.lean" + "ResearchFormalCoreR1/MeasureBridge.lean", + "ResearchFormalCoreR1/WeightedLaw.lean" ], "targets": [ "ResearchFormalCoreR1.ec005_fold_gap", @@ -44,17 +45,26 @@ "ResearchFormalCoreR1.p02_lm008_measure_transfer", "ResearchFormalCoreR1.p02_lm009_measure_transfer_r8_to_r3", "ResearchFormalCoreR1.p02_lm008_sqrt_counterexample", - "ResearchFormalCoreR1.p02_lm008_upper_normalizer_counterexample" + "ResearchFormalCoreR1.p02_lm008_upper_normalizer_counterexample", + "ResearchFormalCoreR1.weightedLaw_apply", + "ResearchFormalCoreR1.weightedLaw_isProbabilityMeasure", + "ResearchFormalCoreR1.weightedLaw_event_real", + "ResearchFormalCoreR1.weightedLaw_absolutelyContinuous", + "ResearchFormalCoreR1.weightedLaw_congr_ae", + "ResearchFormalCoreR1.weightedLaw_zero", + "ResearchFormalCoreR1.weightedLaw_one", + "ResearchFormalCoreR1.p02_lm008_probability_transfer", + "ResearchFormalCoreR1.p02_lm009_probability_transfer_r8_to_r3" ], "files": { ".gitignore": "b34af236d1df9e39ed4f98fca51702530ce76bdee4e1a69512f6caed933c3374", "COMPATIBILITY.md": "33721aad32f82131a6956a06b58c5bc41f5d28f26efe840a99a4d51ad60cf247", "GLOSSARY.md": "6bdda2ce054915efc26e8db462dbe8fd9d0c1db30e02328857bebc6568e05ce9", - "README.md": "6f6f5d235d913b3fdcbfa6c08f739ca34629da7d02d86fb6abcb0684f5ba46f1", + "README.md": "638d8d9e8354462f5e309d6b249def1f0ed0b38ca83a36521106ab5c69bfca81", "ResearchFormalCoreR1/AlgebraV2.lean": "4480708263c40a2f7f03f12ef7e15f0eb4f8193c2dccbb253921a7ec251863c3", "ResearchFormalCoreR1/ProbabilityCompanionsV2.lean": "338452a0c2ba24d016059354d543cc995b8cb29c9d4a41d98bb93499f5f8cb8f", - "ResearchFormalCoreR1.lean": "d091b1dc7c7d7551040092781ac80afc827109330fdccfb4e1381879df7526e5", - "SCOPE.md": "e303550bdd71aa969703fd33dc9808383db13b08072437577d192a7f149a3edb", + "ResearchFormalCoreR1.lean": "b6077abc3522043216eface2da88a1d0f7a2c54a7519229b6f5d1ceb67bbb4a9", + "SCOPE.md": "e8ef291bb7daa334812451a675788b4ee902c0974d9bcd3197407b6b56b63548", "blueprint/src/content.tex": "e00ee78344ed27c097bccb8c6360f6bb86a223ab06319a407de2dd0104fd2e28", "evidence/initial-build-failure.log": "43393d2e2b3148a2453e3092a1ff04e257dbd4a6dc97eb2853a8bc0451002591", "gate.py": "4f14a78bbc6e9b7f929648b13ad3a9ae467256db4be9792e3667680e697a5c94", @@ -66,6 +76,9 @@ "tests/test_gate.py": "5df769901bbf53a7a434c2794c8685242ecfdc32f15d72fae8c2030dff7a9aa2", "ResearchFormalCoreR1/MeasureBridge.lean": "28ac130db490c3c3e2fbfabf3ac5be43c854ab1468bf573bf5a8c0dd2c50955f", "MEASURE_BRIDGE.md": "bdb6c06f359f17d493d6057adbed61edb20236b7a3a3ca4880ae0b33d8a7200e", - "tests/test_measure_bridge.py": "4df86de602d7beffb988b398fe8090b3bb11d0a3dde44685edd584f4864ebe25" + "tests/test_measure_bridge.py": "0e54b2ed7c2e197967180993b4c0c1ece1e63576ad34fd74ac232a4d507628a2", + "ResearchFormalCoreR1/WeightedLaw.lean": "f31dfd2cd048cc0c0446c54c46d89c9cdd5d7bb9d76e9d8d16b7c034f351d736", + "WEIGHTED_LAW.md": "a530dc957923c4c9111b734d8fcc0f4cd3cdba45b08480cfc2e241ecefc5fba4", + "tests/test_weighted_law.py": "3367e505664cbcd1626b175f1c211d74241803113dbca909e2e71fae38776785" } } diff --git a/formal/tests/test_measure_bridge.py b/formal/tests/test_measure_bridge.py index 76eaf915a..6769c97d5 100644 --- a/formal/tests/test_measure_bridge.py +++ b/formal/tests/test_measure_bridge.py @@ -39,8 +39,8 @@ def test_bridge_module_present(self): def test_exact_new_inventory(self): manifest = json.loads((ROOT/'manifest.json').read_text()) targets = ['ResearchFormalCoreR1.'+name for name in NAMES] - self.assertEqual(manifest['targets'][13:], targets) - self.assertEqual(len(manifest['targets']), 20) + self.assertEqual(manifest['targets'][13:20], targets) + self.assertGreaterEqual(len(manifest['targets']), 20) self.assertIn(MODULE, manifest['source_modules']) def test_module_and_scope_bound(self): diff --git a/formal/tests/test_weighted_law.py b/formal/tests/test_weighted_law.py new file mode 100644 index 000000000..1045f0af3 --- /dev/null +++ b/formal/tests/test_weighted_law.py @@ -0,0 +1,133 @@ +"""Exact finite-model falsification and source wiring, not Lean execution.""" +from fractions import Fraction as F +import hashlib +import itertools +import json +from pathlib import Path +import re +import unittest + +ROOT = Path(__file__).resolve().parents[1] +MODULE = 'ResearchFormalCoreR1/WeightedLaw.lean' +NAMES = [ + 'weightedLaw_apply', 'weightedLaw_isProbabilityMeasure', + 'weightedLaw_event_real', 'weightedLaw_absolutelyContinuous', + 'weightedLaw_congr_ae', 'weightedLaw_zero', 'weightedLaw_one', + 'p02_lm008_probability_transfer', 'p02_lm009_probability_transfer_r8_to_r3', +] + + +def normalized_atoms(probabilities, weights): + """Independent rational finite model; nonnegativity is almost everywhere.""" + if len(probabilities) != len(weights) or sum(probabilities, F(0)) != 1: + raise ValueError('not a probability vector') + if any(p < 0 for p in probabilities): + raise ValueError('negative probability') + if any(p > 0 and w < 0 for p, w in zip(probabilities, weights)): + raise ValueError('weight negative on positive mass') + z = sum((p*w for p, w in zip(probabilities, weights)), F(0)) + if z <= 0: + raise ValueError('normalizer must be positive') + return tuple(p*max(w, F(0))/z for p, w in zip(probabilities, weights)) + + +class WeightedLawSourceTests(unittest.TestCase): + def test_module_and_declarations(self): + self.assertTrue((ROOT/MODULE).is_file(), 'weighted-law module not supplied') + source = (ROOT/MODULE).read_text() + self.assertEqual(re.findall(r'^theorem\s+([A-Za-z_][A-Za-z0-9_]*)', source, re.M), NAMES) + self.assertIn('noncomputable def weightedLaw', source) + + def test_exact_target_inventory(self): + m = json.loads((ROOT/'manifest.json').read_text()) + self.assertEqual(m['targets'][20:], ['ResearchFormalCoreR1.'+n for n in NAMES]) + self.assertEqual(len(m['targets']), 29) + + def test_bound_source_scope_and_tests(self): + m = json.loads((ROOT/'manifest.json').read_text()) + for path in (MODULE, 'WEIGHTED_LAW.md', 'tests/test_weighted_law.py'): + self.assertIn(path, m['files']) + self.assertEqual(hashlib.sha256((ROOT/path).read_bytes()).hexdigest(), m['files'][path]) + + def test_root_import(self): + self.assertIn('import ResearchFormalCoreR1.WeightedLaw\n', + (ROOT/'ResearchFormalCoreR1.lean').read_text()) + + def test_parent_proofs_and_execution_controls_retained(self): + m = json.loads((ROOT/'manifest.json').read_text()) + pins = { + 'ResearchFormalCoreR1/MeasureBridge.lean': '28ac130db490c3c3e2fbfabf3ac5be43c854ab1468bf573bf5a8c0dd2c50955f', + 'gate.py': '4f14a78bbc6e9b7f929648b13ad3a9ae467256db4be9792e3667680e697a5c94', + 'lean-toolchain': 'd5edba4e4b8faad9c1baeadb265716d20d03be4d1a2647dc5e35b0c0325bea7b', + } + for path, digest in pins.items(): + self.assertEqual(m['files'][path], digest) + self.assertEqual(m['alignment_status'], 'PENDING_INDEPENDENT_REVIEW') + self.assertEqual(m['scientific_effect'], 'NONE') + + +class WeightedLawFiniteModelTests(unittest.TestCase): + def test_all_event_ratios_and_transfer(self): + probabilities = (F(1, 4), F(1, 4), F(1, 2)) + cases = 0 + for weights in itertools.product((F(0), F(1, 2), F(3)), repeat=3): + z = sum(p*w for p, w in zip(probabilities, weights)) + if z == 0: + continue + atoms = normalized_atoms(probabilities, weights) + self.assertEqual(sum(atoms), 1) + second = sum(p*w*w for p, w in zip(probabilities, weights)) + for event in itertools.product((False, True), repeat=3): + q = sum(p for p, a in zip(probabilities, event) if a) + n = sum(p*w for p, w, a in zip(probabilities, weights, event) if a) + actual = sum(p for p, a in zip(atoms, event) if a) + self.assertEqual(actual, n/z) + self.assertGreaterEqual(actual, 0) + self.assertLessEqual(actual, 1) + self.assertLessEqual(actual**2, second*q/z**2) + cases += 1 + self.assertEqual(cases, 208) + + def test_constant_positive_weights(self): + p = (F(1, 4), F(3, 4)) + for a in (F(1, 7), F(1), F(5)): + self.assertEqual(normalized_atoms(p, (a, a)), p) + + def test_zero_normalizer_rejected(self): + with self.assertRaisesRegex(ValueError, 'normalizer'): + normalized_atoms((F(1, 2), F(1, 2)), (F(0), F(0))) + + def test_signed_weight_rejected_and_clipping_is_not_normalization(self): + p, w = (F(1, 2), F(1, 2)), (F(-1), F(3)) + with self.assertRaisesRegex(ValueError, 'negative'): + normalized_atoms(p, w) + z = sum(a*b for a, b in zip(p, w)) + self.assertEqual(z, 1) + self.assertEqual(sum(a*max(b, F(0))/z for a, b in zip(p, w)), F(3, 2)) + + def test_ae_representatives_and_null_events(self): + p = (F(0), F(1, 4), F(3, 4)) + first = normalized_atoms(p, (F(-100), F(4), F(0))) + second = normalized_atoms(p, (F(100), F(4), F(0))) + self.assertEqual(first, second) + self.assertEqual(first, (F(0), F(1), F(0))) + + def test_positive_rescaling(self): + p, w = (F(1, 3), F(2, 3)), (F(2), F(5)) + for a in (F(1, 7), F(9)): + self.assertEqual(normalized_atoms(p, w), normalized_atoms(p, tuple(a*x for x in w))) + + def test_omitting_normalizer_changes_total_mass(self): + p, w = (F(1, 2), F(1, 2)), (F(2), F(2)) + self.assertEqual(sum(a*b for a, b in zip(p, w)), 2) + self.assertEqual(sum(normalized_atoms(p, w)), 1) + + def test_countable_additivity_finite_partition_witness(self): + p, w = (F(1, 4), F(1, 4), F(1, 2)), (F(1), F(2), F(3)) + atoms = normalized_atoms(p, w) + self.assertEqual(sum(atoms[:2]) + atoms[2], 1) + self.assertEqual(sum(atoms[1:]), 1-atoms[0]) + + +if __name__ == '__main__': + unittest.main()