import QLF_Agency /-! # QLF_ClosureInference — a closure fixes what its completion must supply; conditioning on it is # the minimum-relative-entropy update Registry: [`Principles.md`](../Principles.md) C18 (active inference), C30 (entropy), P26; ladder rung 9 ([`Principle_Ladder.md`](../Principle_Ladder.md)). Proposal: Jim's 2026-11-30 post, which also noted that `QLF_FreeEnergy` proves only the binary identity `−D(δ ‖ U₂) = −log 2`. No axiom. §2–§3 are finite information theory (Gibbs' inequality and the chain identity for a conditioned distribution), proved here for any finite space rather than imported; their QLF content is the choice of `C` as the closure-compatible completions. * **§1 A history specifies its completion.** On every axis a completion supplies exactly the opposite of the history's net action (`closure_infers_complement`). The route is not fixed (`QLF.Manifestation.completion_not_unique`). * **§2 Gibbs.** For distributions `q`, `r` with `r` positive wherever `q` is, `D(q ‖ r) ≥ 0`, with equality only at `q = r` (`kl_nonneg`, `kl_eq_zero_iff`). * **§3 Conditioning on closure is the minimum-relative-entropy revision.** For a prior `p` and the set `C` of closure-compatible candidates, every `q` supported on `C` has `D(q ‖ p) = D(q ‖ p_C) − log p(C)` (`kl_cond_decomp`), so `D(q ‖ p) ≥ −log p(C)` with equality exactly at the conditioned prior `p_C` (`closure_conditioning_minimizes_kl`). Conditioning twice is conditioning once on the intersection (`inference_composes`). * **§4 Information gain.** Learning that a closure is compatible is worth `−log p(C)`: zero when it was certain, `n log 2` when the compatible mass is `2⁻ⁿ` (`closure_information_gain`). `n = 1` recovers the magnitude `log 2` of `QLF_FreeEnergy` (`binary_closure_information_special_case`). * **§5 Experiments as queries.** A prepared configuration `q` is answered by `r` iff `q ++ r` closes iff `A(r) = −A(q)` (`query_answer_iff_closure`); the answers are one class of the action abstraction, so a query fixes the class and never the route (`query_answers_form_abstraction`); a further record refines what can be told apart, strictly when it splits a class (`query_refinement_distinguishes_models`, elementary); answers compose (`query_composition`). That an apparatus is organized closures, with no observer variable, is A06 and holds by construction here (nothing in `countBalanced` names an observer). There is one closure network: query and answer are parts of it, never placed inside it. Open: a faithful encoding of a given apparatus, and a query arising from the network's own closures rather than from the modeller. **Exceptions and scope.** Certainty gives no information (`p(C) = 1`). The prior is an input: which measure the substrate supplies, and whether its dynamics realize this conditioning, including phases, is the open bridge (the measure question of #318). This is the minimum-relative-entropy update, not Friston's variational free energy, which adds a generative model this module does not have. That every closure is "elementary intelligence" is a reading, not a theorem. -/ namespace QLF.ClosureInference open QLF Real -- ============================================================================ -- §1 A history specifies its completion -- ============================================================================ /-- **The open history fixes the completion's net action, axis by axis.** -/ theorem closure_infers_complement {h c : List Twist} (hc : countBalanced (h ++ c)) : (c.count Twist.up : ℤ) - c.count Twist.down = -((h.count Twist.up : ℤ) - h.count Twist.down) ∧ (c.count Twist.left : ℤ) - c.count Twist.right = -((h.count Twist.left : ℤ) - h.count Twist.right) ∧ (c.count Twist.slash : ℤ) - c.count Twist.backslash = -((h.count Twist.slash : ℤ) - h.count Twist.backslash) ∧ (c.count Twist.plus : ℤ) - c.count Twist.minus = -((h.count Twist.plus : ℤ) - h.count Twist.minus) := by unfold countBalanced at hc simp only [List.count_append] at hc obtain ⟨h1, h2, h3, h4⟩ := hc refine ⟨?_, ?_, ?_, ?_⟩ <;> omega -- ============================================================================ -- §2 Gibbs -- ============================================================================ variable {Ω : Type*} [Fintype Ω] /-- Relative entropy `D(q ‖ r) = Σ q log (q / r)` on a finite space. -/ noncomputable def kl (q r : Ω → ℝ) : ℝ := ∑ k, q k * log (q k / r k) /-- The termwise form of `log x ≤ x − 1`. -/ theorem term_ge {q r : ℝ} (hq : 0 ≤ q) (hr : 0 ≤ r) (hqr : 0 < q → 0 < r) : q - r ≤ q * log (q / r) := by rcases hq.eq_or_lt with h0 | hpos · subst h0; simp; exact hr · have hr' := hqr hpos have hl := log_le_sub_one_of_pos (div_pos hr' hpos) rw [log_div hr'.ne' hpos.ne'] at hl rw [log_div hpos.ne' hr'.ne'] have := mul_le_mul_of_nonneg_left hl hpos.le have hq' : q * (r / q - 1) = r - q := by field_simp have e1 : q * (log q - log r) = -(q * (log r - log q)) := by ring rw [hq'] at this rw [e1]; linarith /-- Equality in `term_ge` forces `q = r`. -/ theorem term_eq {q r : ℝ} (hq : 0 ≤ q) (hr : 0 ≤ r) (hqr : 0 < q → 0 < r) (he : q - r = q * log (q / r)) : q = r := by rcases hq.eq_or_lt with h0 | hpos · subst h0; simp at he; linarith · have hr' := hqr hpos by_contra hne have hx : r / q ≠ 1 := by intro h1; apply hne; field_simp at h1; linarith have hl := log_lt_sub_one_of_pos (div_pos hr' hpos) hx rw [log_div hr'.ne' hpos.ne'] at hl rw [log_div hpos.ne' hr'.ne'] at he have := mul_lt_mul_of_pos_left hl hpos have hq' : q * (r / q - 1) = r - q := by field_simp have e1 : q * (log q - log r) = -(q * (log r - log q)) := by ring rw [hq'] at this rw [e1] at he; linarith /-- **Gibbs' inequality.** -/ theorem kl_nonneg {q r : Ω → ℝ} (hq : ∀ k, 0 ≤ q k) (hr : ∀ k, 0 ≤ r k) (hqr : ∀ k, 0 < q k → 0 < r k) (hsq : ∑ k, q k = 1) (hsr : ∑ k, r k ≤ 1) : 0 ≤ kl q r := by have : ∑ k, (q k - r k) ≤ kl q r := Finset.sum_le_sum fun k _ => term_ge (hq k) (hr k) (hqr k) rw [Finset.sum_sub_distrib, hsq] at this linarith /-- **Relative entropy vanishes only at equality.** -/ theorem kl_eq_zero_iff {q r : Ω → ℝ} (hq : ∀ k, 0 ≤ q k) (hr : ∀ k, 0 ≤ r k) (hqr : ∀ k, 0 < q k → 0 < r k) (hsq : ∑ k, q k = 1) (hsr : ∑ k, r k = 1) : kl q r = 0 ↔ q = r := by constructor · intro h0 have hsum : ∑ k, (q k * log (q k / r k) - (q k - r k)) = 0 := by rw [Finset.sum_sub_distrib, Finset.sum_sub_distrib, hsq, hsr] unfold kl at h0; linarith have hz := (Finset.sum_eq_zero_iff_of_nonneg fun k _ => sub_nonneg.2 (term_ge (hq k) (hr k) (hqr k))).1 hsum funext k exact term_eq (hq k) (hr k) (hqr k) (by linarith [hz k (Finset.mem_univ k)]) · rintro rfl unfold kl refine Finset.sum_eq_zero fun k _ => ?_ rcases (hq k).eq_or_lt with h | h · rw [← h]; simp · rw [div_self h.ne', log_one, mul_zero] -- ============================================================================ -- §3 Conditioning on closure -- ============================================================================ /-- The prior mass of the compatible set `C`. -/ def mass (p : Ω → ℝ) (C : Finset Ω) : ℝ := ∑ k ∈ C, p k open Classical in /-- The prior conditioned on `C`. -/ noncomputable def cond (p : Ω → ℝ) (C : Finset Ω) (k : Ω) : ℝ := if k ∈ C then p k / mass p C else 0 omit [Fintype Ω] in theorem cond_nonneg {p : Ω → ℝ} {C : Finset Ω} (hp : ∀ k, 0 ≤ p k) (hm : 0 < mass p C) (k : Ω) : 0 ≤ cond p C k := by unfold cond; split_ifs · exact div_nonneg (hp k) hm.le · exact le_rfl theorem sum_cond {p : Ω → ℝ} {C : Finset Ω} (hm : 0 < mass p C) : ∑ k, cond p C k = 1 := by classical unfold cond rw [← Finset.sum_filter, Finset.filter_mem_eq_inter, Finset.univ_inter, ← Finset.sum_div] exact div_self hm.ne' /-- **The chain identity for conditioning.** For `q` supported on `C`, with the prior positive on `C`: `D(q ‖ p) = D(q ‖ p_C) − log p(C) · Σ q`. -/ theorem kl_cond_decomp {p q : Ω → ℝ} {C : Finset Ω} (hp : ∀ k ∈ C, 0 < p k) (hq : ∀ k, k ∉ C → q k = 0) (hm : 0 < mass p C) : kl q p = kl q (cond p C) - log (mass p C) * ∑ k, q k := by unfold kl rw [Finset.mul_sum, ← Finset.sum_sub_distrib] refine Finset.sum_congr rfl fun k _ => ?_ by_cases hk : k ∈ C · by_cases hqk : q k = 0 · simp [hqk] · have hpk := hp k hk simp only [cond, hk, ite_true] rw [div_div_eq_mul_div, log_div (mul_ne_zero hqk hm.ne') hpk.ne', log_mul hqk hm.ne', log_div hqk hpk.ne'] ring · simp [hq k hk] /-- **Conditioning on closure is the unique minimum-relative-entropy revision.** Among distributions supported on the compatible set `C`, `D(q ‖ p) ≥ −log p(C)`, the conditioned prior attains it, and nothing else does. -/ theorem closure_conditioning_minimizes_kl {p : Ω → ℝ} {C : Finset Ω} (hp0 : ∀ k, 0 ≤ p k) (hp : ∀ k ∈ C, 0 < p k) (hm : 0 < mass p C) : kl (cond p C) p = -log (mass p C) ∧ ∀ q : Ω → ℝ, (∀ k, 0 ≤ q k) → ∑ k, q k = 1 → (∀ k, k ∉ C → q k = 0) → -log (mass p C) ≤ kl q p ∧ (kl q p = -log (mass p C) ↔ q = cond p C) := by have hc0 := cond_nonneg hp0 hm have hcs := sum_cond hm have hpos : ∀ q : Ω → ℝ, (∀ k, k ∉ C → q k = 0) → ∀ k, 0 < q k → 0 < cond p C k := by intro q hq k hk have hkC : k ∈ C := by by_contra h; rw [hq k h] at hk; exact lt_irrefl 0 hk simp only [cond, hkC, ite_true]; exact div_pos (hp k hkC) hm have hcsupp : ∀ k, k ∉ C → cond p C k = 0 := fun k hk => by simp [cond, hk] refine ⟨?_, fun q hq hsq hsupp => ?_⟩ · rw [kl_cond_decomp hp hcsupp hm, hcs, (kl_eq_zero_iff hc0 hc0 (fun _ h => h) hcs hcs).2 rfl] ring · rw [kl_cond_decomp hp hsupp hm, hsq, mul_one] have hnn := kl_nonneg hq hc0 (hpos q hsupp) hsq hcs.le refine ⟨by linarith, ?_⟩ rw [← kl_eq_zero_iff hq hc0 (hpos q hsupp) hsq hcs] constructor <;> intro h <;> linarith omit [Fintype Ω] in open Classical in /-- **Inference composes.** Conditioning on `C` and then on `D` is conditioning on `C ∩ D`. -/ theorem inference_composes {p : Ω → ℝ} {C D : Finset Ω} (hmC : 0 < mass p C) (hmCD : 0 < mass p (C ∩ D)) : cond (cond p C) D = cond p (C ∩ D) := by have hmass : mass (cond p C) D = mass p (C ∩ D) / mass p C := by have e : mass (cond p C) D = ∑ k ∈ D, if k ∈ C then p k / mass p C else 0 := rfl rw [e, Finset.sum_ite, Finset.sum_const_zero, add_zero, ← Finset.sum_div, Finset.filter_mem_eq_inter, Finset.inter_comm] rfl funext k have e : cond (cond p C) D k = if k ∈ D then cond p C k / mass (cond p C) D else 0 := rfl rw [e, hmass] have hmC' := hmC.ne' have hmCD' := hmCD.ne' by_cases hkD : k ∈ D <;> by_cases hkC : k ∈ C <;> simp [cond, hkD, hkC, Finset.mem_inter] field_simp -- ============================================================================ -- §4 Information gain -- ============================================================================ /-- **What learning compatibility is worth.** Zero when compatibility was certain; `n log 2` when the compatible mass is `2⁻ⁿ`. -/ theorem closure_information_gain (n : ℕ) : -log (1 : ℝ) = 0 ∧ -log ((1 : ℝ) / 2 ^ n) = n * log 2 := by refine ⟨by simp, ?_⟩ rw [one_div, log_inv, log_pow, neg_neg] /-- **The binary case.** Half the mass compatible: one bit, `log 2`, the magnitude of `QLF_FreeEnergy`'s per-closure `ΔF = −log 2`. -/ theorem binary_closure_information_special_case : -log ((1 : ℝ) / 2) = log 2 := by have := (closure_information_gain 1).2 simp only [pow_one, Nat.cast_one, one_mul] at this exact this -- ============================================================================ -- §5 Experiments as queries -- ============================================================================ /-- The signed action of a history, axis by axis: `(y, x, z, gauge)`. -/ def netAction (h : List Twist) : ℤ × ℤ × ℤ × ℤ := ((h.count Twist.up : ℤ) - h.count Twist.down, (h.count Twist.left : ℤ) - h.count Twist.right, (h.count Twist.slash : ℤ) - h.count Twist.backslash, (h.count Twist.plus : ℤ) - h.count Twist.minus) /-- **An answer is exactly a completion of the query's obligation.** A query `q` (a prepared configuration) is answered by `r` iff `q ++ r` closes iff `A(r) = −A(q)`. -/ theorem query_answer_iff_closure (q r : List Twist) : countBalanced (q ++ r) ↔ netAction r = -netAction q := by unfold countBalanced netAction simp only [List.count_append, Prod.neg_mk, Prod.mk.injEq] constructor · rintro ⟨h1, h2, h3, h4⟩; refine ⟨?_, ?_, ?_, ?_⟩ <;> omega · rintro ⟨h1, h2, h3, h4⟩; refine ⟨?_, ?_, ?_, ?_⟩ <;> omega /-- **A query's answers are one class of the action abstraction.** Given one answer `r₀`, the answers are exactly the histories the signed-action record cannot tell from it: the query fixes the class, never the route. -/ theorem query_answers_form_abstraction {q r₀ : List Twist} (h₀ : countBalanced (q ++ r₀)) (r : List Twist) : countBalanced (q ++ r) ↔ QLF.Abstraction.abstract netAction r = QLF.Abstraction.abstract netAction r₀ := by rw [QLF.Abstraction.abstraction_iff, query_answer_iff_closure, (query_answer_iff_closure q r₀).1 h₀] /-- **Asking more refines what can be told apart.** Joining a second record to the first never merges histories the first separated; it separates more exactly when the second record splits a class of the first. Elementary. -/ theorem query_refinement_distinguishes_models {H R S : Type*} (D₁ : H → R) (D₂ : H → S) : QLF.Abstraction.indistinguishable (fun x => (D₁ x, D₂ x)) ≤ QLF.Abstraction.indistinguishable D₁ ∧ ((∃ x y, D₁ x = D₁ y ∧ D₂ x ≠ D₂ y) → ¬ QLF.Abstraction.indistinguishable D₁ ≤ QLF.Abstraction.indistinguishable (fun x => (D₁ x, D₂ x))) := by refine ⟨fun x y h => congrArg Prod.fst h, ?_⟩ rintro ⟨x, y, h1, h2⟩ hle exact h2 (congrArg Prod.snd (hle h1)) /-- **Answers compose.** If `r` answers `q` and `r'` answers `q'`, then `r ++ r'` answers `q ++ q'`: two experiments run together are one experiment. -/ theorem query_composition {q r q' r' : List Twist} (h : countBalanced (q ++ r)) (h' : countBalanced (q' ++ r')) : countBalanced ((q ++ q') ++ (r ++ r')) := by unfold countBalanced at * simp only [List.count_append] at * omega end QLF.ClosureInference