← Back to Pull requests

feat(Circuit): strengthen Shannon's bound and drop finiteness assumptions #1130

Open
SamuelSchlesinger opened this pull request with 156 lines diff across 3 files
View on GitHub
SamuelSchlesinger SamuelSchlesinger commented on Oct 13, 2026 03:49 UTC
Author

Shows that almost all functions need more than qⁿ/n gates. Shannon.tendsto_card_computableFunctions_div says that, over a finite carrier with q ≥ 2 elements and any finite signature of arity at most two, the fraction of functions on n inputs computable with qⁿ/n gates tends to zero, and Boolean.Shannon.tendsto_card_computableFunctions_div specializes it to De Morgan circuits. The existing counting argument already gives this once the exponent gap is taken one larger, which bounds the count by exp(-s) · q^(qⁿ) at s = ⌊qⁿ/n⌋.

Shannon.exists_hard_function no longer assumes [Finite σ.Op]: it takes its hard function from the binary full basis, which simulates every signature of arity at most two gate by gate. fullInterpretation_isComplete and the IsComplete instance for full bases no longer assume [Finite U]: on an infinite carrier, a bijection U × U ≃ U lets binary gates pair up the inputs into one element, and one more gate decodes it and applies the function. Both only drop instance arguments, so no caller changes.

Assisted by Claude.

Diff: 156 lines (+118 / -38) across 3 files Reviewer: ctchou Review duration: 58669m
All Vouch review confidence checks passed
Residual risk 0.07 = Change Risk (0.43) × (1 − Review Confidence (0.85)). Review depth and reviewer attention state are adequate.
Model 1 · Change Risk (XGBoost)
Score: 0.43 · 156 lines diff across 3 files
0.43
Model 2 · Review Depth (DistilBERT)
Depth Score: 0.89 · 5 substantive comment(s)
0.89
Model 3 · Review Timing & Attention
Session adequacy: 100% · Reviewer familiarity: 50%
84%
Bedrock Claude 3 · Risk Narration
(disconnected)
Groq
Groq Risk Narration
AWS Bedrock is currently disconnected. Generate high-confidence PR triage narration on-demand using Groq.
Model 1 · Change Risk Attribution
SHAP Weights
Diff Size diff_size
0.303
Real PR Conversation Timeline
7 items
ctchou ctchou commented on code on Oct 23, 2026 21:38 UTC
Code Review
Cslib/Computability/Circuit/Boolean/Shannon.lean:1
@@ -11,15 +11,26 @@ public import Cslib.Computability.Circuit.Shannon
 /-!
 # Shannon's lower bound for De Morgan circuits
 
-This is the De Morgan specialization of `Cslib.Circuits.Shannon.exists_hard_function`,
-stated for circuits and for circuit complexity. Together with Lupanov's construction, it
-gives the asymptotically sharp gate count `2ⁿ/n`.
+This is the De Morgan specialization of `Cslib.Circuits.Shannon`: almost all Boolean functions
+on `n` inputs require more than `2ⁿ/n` gates, so for large `n` some function does, stated for
+circuits and for circuit complexity. Together with Lupanov's construction, it gives the
+asymptotically sharp gate count `2ⁿ/n`.
 -/
 
 public section
 
 namespace Cslib.Circuits.Boolean.Shannon
 
+open Filter Topology
+
+/-- Almost all Boolean functions on `n` inputs require more than `2ⁿ/n` De Morgan gates: the
+fraction computable within that budget tends to zero. -/
+theorem tendsto_card_computableFunctions_div :
+    Tendsto (fun n : ℕ => ((computableFunctions interpretation n (2 ^ n / n)).card : ℝ) /
+      2 ^ 2 ^ n) atTop (𝓝 0) := by

The numbers 2 ^ n / n and 2 ^ 2 ^ n should be given names conveying the significance. Also, do you have theorems about that? For example, do you have a theorem saying that the number of all possible boolean functions with n inputs is 2 ^ 2 ^ n?

Nitpick: I think the (fun n => ...) expression should be put on a single line if possible.

ctchou ctchou commented on code on Oct 23, 2026 22:26 UTC
Code Review
Cslib/Computability/Circuit/Shannon.lean:1
@@ -110,37 +116,62 @@ private theorem eventually_card_lt [Finite σ.Op] [Fintype U] [Nontrivial U]
     simpa [Real.log_pow, Nat.cast_sub htn] using h
   have hsize : (n : ℝ) * s ≤ (q : ℝ) ^ n := by
     exact_mod_cast (Nat.mul_div_le (q ^ n) n)
-  have hexponent : (s : ℝ) * Real.log s + C * s <
-      (q : ℝ) ^ n * Real.log q := by
+  have hexponent : (s : ℝ) * Real.log s + C * s ≤ -(s : ℝ) + (q : ℝ) ^ n * Real.log q := by
     nlinarith only [mul_le_mul_of_nonneg_left hlog hs.le,
       mul_le_mul_of_nonneg_right hsize hlogq.le, mul_lt_mul_of_pos_right hgap hs]
-  have hcount : ((computableFunctions I n s).card : ℝ) < (q : ℝ) ^ (q ^ n : ℕ) := by
-    calc
-      _ ≤ Real.exp ((s : ℝ) * Real.log s + C * s) := hC n s hn
-      _ < Real.exp ((q : ℝ) ^ n * Real.log q) := Real.exp_lt_exp.mpr hexponent
-      _ = (q : ℝ) ^ (q ^ n : ℕ) := by
-        rw [show (q : ℝ) ^ n = ((q ^ n : ℕ) : ℝ) by norm_cast,
-          Real.exp_nat_mul, Real.exp_log (zero_lt_one.trans hqR)]
-  exact_mod_cast hcount
+  calc
+    _ ≤ Real.exp ((s : ℝ) * Real.log s + C * s) := hC n s hn
+    _ ≤ Real.exp (-(s : ℝ) + (q : ℝ) ^ n * Real.log q) := Real.exp_le_exp.mpr hexponent
+    _ = Real.exp (-(s : ℝ)) * (q : ℝ) ^ (q ^ n : ℕ) := by
+      rw [Real.exp_add, show (q : ℝ) ^ n = ((q ^ n : ℕ) : ℝ) by norm_cast,
+        Real.exp_nat_mul, Real.exp_log (zero_lt_one.trans hqR)]
+
+/-- Almost all functions on `n` inputs require more than `|U|ⁿ/n` gates over a fixed finite
+signature whose operations have arity at most two: the fraction of functions computable within
+that budget tends to zero. -/
+theorem tendsto_card_computableFunctions_div [Finite σ.Op] [Finite U] [Nontrivial U]
+    (I : Interpretation σ U) (arity_le : ∀ op, σ.Arity op ≤ 2) :
+    Tendsto (fun n : ℕ => ((computableFunctions I n (Nat.card U ^ n / n)).card : ℝ) /
+      (Nat.card U : ℝ) ^ (Nat.card U ^ n)) atTop (𝓝 0) := by

The numbers Nat.card U : ℝ) ^ (Nat.card U ^ n) and Nat.card U ^ n / n should be given names conveying their significance. Do you have a theorem saying that the former is the number of all possible functions with n inputs?

Nitpick: I think the (fun n => ...) expression should be put on a single line if possible. At least the line break should happen after =>, not after /.

ctchou ctchou commented on code on Oct 23, 2026 22:51 UTC
Code Review
Cslib/Computability/Circuit/Full.lean:1
@@ -140,15 +143,50 @@ private theorem fullInterpretation_isComplete_two [Finite U] :
       obtain ⟨cost, hcost⟩ := exists_full_synthesis_finset f (Classical.choice h) Finset.univ
       simpa using hcost.exists_circuit.imp fun _ hc => hc.1
 
-/-- Every full basis of arity at least two is complete on a finite carrier. -/
-theorem fullInterpretation_isComplete [Finite U] (hk : 2 ≤ k) :
+private theorem fullInterpretation_isComplete_two_of_infinite [Infinite U] :
+    (fullInterpretation (k := 2) (Carrier := U)).IsComplete where

I don't understand why you want to worry about the case [Infinite U]. The Shannon bound doesn't even make sense in that case, right? Also, doesn't this theorem require a signature with an infinite number of operation symbols? In any case, this result seems completely unrelated to the rest of this PR.

ctchou ctchou requested changes on Oct 23, 2026 22:53 UTC
Changes Requested Reviewer

In the main commit message, shouldn't "arity at most two" be "arity at least two"?

open Nat when appropriate, so that you can write Card or Nat.Card.

SamuelSchlesinger SamuelSchlesinger commented on code on Nov 12, 2026 03:34 UTC
Code Review
Cslib/Computability/Circuit/Full.lean:1
@@ -140,15 +143,50 @@ private theorem fullInterpretation_isComplete_two [Finite U] :
       obtain ⟨cost, hcost⟩ := exists_full_synthesis_finset f (Classical.choice h) Finset.univ
       simpa using hcost.exists_circuit.imp fun _ hc => hc.1
 
-/-- Every full basis of arity at least two is complete on a finite carrier. -/
-theorem fullInterpretation_isComplete [Finite U] (hk : 2 ≤ k) :
+private theorem fullInterpretation_isComplete_two_of_infinite [Infinite U] :
+    (fullInterpretation (k := 2) (Carrier := U)).IsComplete where

Good call, will remove this.

SamuelSchlesinger SamuelSchlesinger commented on code on Nov 12, 2026 03:34 UTC
Code Review
Cslib/Computability/Circuit/Shannon.lean:1
@@ -110,37 +116,62 @@ private theorem eventually_card_lt [Finite σ.Op] [Fintype U] [Nontrivial U]
     simpa [Real.log_pow, Nat.cast_sub htn] using h
   have hsize : (n : ℝ) * s ≤ (q : ℝ) ^ n := by
     exact_mod_cast (Nat.mul_div_le (q ^ n) n)
-  have hexponent : (s : ℝ) * Real.log s + C * s <
-      (q : ℝ) ^ n * Real.log q := by
+  have hexponent : (s : ℝ) * Real.log s + C * s ≤ -(s : ℝ) + (q : ℝ) ^ n * Real.log q := by
     nlinarith only [mul_le_mul_of_nonneg_left hlog hs.le,
       mul_le_mul_of_nonneg_right hsize hlogq.le, mul_lt_mul_of_pos_right hgap hs]
-  have hcount : ((computableFunctions I n s).card : ℝ) < (q : ℝ) ^ (q ^ n : ℕ) := by
-    calc
-      _ ≤ Real.exp ((s : ℝ) * Real.log s + C * s) := hC n s hn
-      _ < Real.exp ((q : ℝ) ^ n * Real.log q) := Real.exp_lt_exp.mpr hexponent
-      _ = (q : ℝ) ^ (q ^ n : ℕ) := by
-        rw [show (q : ℝ) ^ n = ((q ^ n : ℕ) : ℝ) by norm_cast,
-          Real.exp_nat_mul, Real.exp_log (zero_lt_one.trans hqR)]
-  exact_mod_cast hcount
+  calc
+    _ ≤ Real.exp ((s : ℝ) * Real.log s + C * s) := hC n s hn
+    _ ≤ Real.exp (-(s : ℝ) + (q : ℝ) ^ n * Real.log q) := Real.exp_le_exp.mpr hexponent
+    _ = Real.exp (-(s : ℝ)) * (q : ℝ) ^ (q ^ n : ℕ) := by
+      rw [Real.exp_add, show (q : ℝ) ^ n = ((q ^ n : ℕ) : ℝ) by norm_cast,
+        Real.exp_nat_mul, Real.exp_log (zero_lt_one.trans hqR)]
+
+/-- Almost all functions on `n` inputs require more than `|U|ⁿ/n` gates over a fixed finite
+signature whose operations have arity at most two: the fraction of functions computable within
+that budget tends to zero. -/
+theorem tendsto_card_computableFunctions_div [Finite σ.Op] [Finite U] [Nontrivial U]
+    (I : Interpretation σ U) (arity_le : ∀ op, σ.Arity op ≤ 2) :
+    Tendsto (fun n : ℕ => ((computableFunctions I n (Nat.card U ^ n / n)).card : ℝ) /
+      (Nat.card U : ℝ) ^ (Nat.card U ^ n)) atTop (𝓝 0) := by

Do you have a theorem saying that the former is the number of all possible functions with n inputs?

Not sure if that is in the tree already, I'll check and confirm.

Reviewers
Assignees
SamuelSchlesinger SamuelSchlesinger
Labels
risk: low confidence: 84.9% reviewed
Vouch Risk Breakdown
AI-powered review risk analysis
Change Risk
Change Risk · Model 1 (XGBoost)
Predicted defect probability (0.00–1.00) based on diff churn, modified sensitive files, and author risk history.
Score: 43.2% (MEDIUM) · Higher = larger blast radius
43.2%
Review Confidence
Review Confidence · Models 2 & 3
Scrutiny score (0.00–1.00) combining DistilBERT semantic review depth with reviewer familiarity and session dwell time.
Score: 84.9% · Lower = rushed or shallow review
84.9%
Residual Risk
Residual Risk · Core Equation
Residual = Change × (1 − Confidence)
Unmitigated risk surviving review. ≥ 0.65 triggers an automated re-queue to protect production.
Score: 6.5% · Safe to merge
6.5%