Documentation

Complexitylib.Classes.FiniteCounting

Finite counting toolkit #

Exact cardinality lemmas for the finite sample space Fin T → Bool used by the probabilistic-machine semantics (NTM.acceptCount, NTM.acceptProb). These are the reusable combinatorial facts underlying randomized classes, amplification, and interactive proofs (roadmap track N2).

Main results #

Cardinality of the random-bit sample space #

The finite sample space of T random bits has 2 ^ T points. This is the denominator in NTM.acceptProb.

theorem Complexity.card_finArrowBool_add (a b : ) :
Fintype.card (Fin (a + b)Bool) = 2 ^ a * 2 ^ b

Splitting T = a + b random bits multiplies the point counts.

theorem Complexity.card_boolFunc (n : ) :
Fintype.card ((Fin nBool)Bool) = 2 ^ 2 ^ n

There are exactly 2 ^ (2 ^ n) Boolean functions on n bits — the number of distinct 2 ^ n-entry truth tables. The base fact for property density in natural-proofs arguments.

theorem Complexity.card_adjMatrix (n : ) :
Fintype.card (Fin nFin nBool) = 2 ^ (n * n)

There are exactly 2 ^ (n * n) directed graphs (adjacency matrices) on n vertices — the number of n × n Boolean matrices. The size datum behind adjacency-matrix encodings of graph languages such as CLIQUE.

def Complexity.adjMatrixEquivBitVec (n : ) :
(Fin nFin nBool) (Fin (n * n)Bool)

An n-vertex adjacency matrix biject with -bit strings by row-major serialization. Packaging this as an Equiv gives a canonical encode (adjMatrixEquivBitVec) and decode (.symm) that are inverse by construction — the codec behind graph-language encodings.

Equations
Instances For

    Block projection and concatenation #

    def Complexity.blockEquiv (a b : ) :
    (Fin (a + b)Bool) (Fin aBool) × (Fin bBool)

    A length-a + b random string is equivalently its length-a prefix paired with its length-b suffix. The forward map is the pair of projections; the inverse is concatenation. Packaging this as an Equiv proves the projection and concatenation maps are mutually inverse.

    Equations
    Instances For
      def Complexity.blockFst (a b : ) (w : Fin (a + b)Bool) :
      Fin aBool

      The prefix projection of a length-a + b random string.

      Equations
      Instances For
        def Complexity.blockSnd (a b : ) (w : Fin (a + b)Bool) :
        Fin bBool

        The suffix projection of a length-a + b random string.

        Equations
        Instances For
          @[simp]
          theorem Complexity.blockSnd_apply (a b : ) (w : Fin (a + b)Bool) (i : Fin b) :
          blockSnd a b w i = w (Fin.natAdd a i)

          Suffix projection is restriction along the canonical shifted inclusion.

          def Complexity.blockAppend (a b : ) (u : Fin aBool) (v : Fin bBool) :
          Fin (a + b)Bool

          Concatenate a length-a prefix and length-b suffix into one random string.

          Equations
          Instances For
            @[simp]
            theorem Complexity.blockFst_append (a b : ) (u : Fin aBool) (v : Fin bBool) :
            blockFst a b (blockAppend a b u v) = u
            @[simp]
            theorem Complexity.blockSnd_append (a b : ) (u : Fin aBool) (v : Fin bBool) :
            blockSnd a b (blockAppend a b u v) = v
            @[simp]
            theorem Complexity.blockAppend_fst_snd (a b : ) (w : Fin (a + b)Bool) :
            blockAppend a b (blockFst a b w) (blockSnd a b w) = w
            theorem Complexity.card_filter_block {a b : } (P : (Fin aBool)Prop) (Q : (Fin bBool)Prop) [DecidablePred P] [DecidablePred Q] :
            {w : Fin (a + b)Bool | P (blockFst a b w) Q (blockSnd a b w)}.card = (Finset.filter P Finset.univ).card * (Finset.filter Q Finset.univ).card

            Block independence (exact counting form). The number of length-a + b seeds whose prefix satisfies P and suffix satisfies Q is the product of the two individual counts. Because blockEquiv is a bijection onto the product sample space, the joint event factors — the exact-count statement of independence across blocks, and the combinatorial heart of relating a repeated machine's acceptance to its single-run acceptance (amplification, roadmap N2/M2).

            Finite union bound #

            theorem Complexity.card_filter_exists_le {ι : Type u_1} {α : Type u_2} [Fintype α] [DecidableEq α] (s : Finset ι) (p : ιαProp) [(i : ι) → DecidablePred (p i)] :
            {x : α | is, p i x}.card is, (Finset.filter (p i) Finset.univ).card

            The finite union bound: the number of sample points satisfying some predicate p i for i in a finite index set s is at most the sum over s of the per-predicate counts. The workhorse behind failure-probability bounds over many bad events.

            theorem Complexity.exists_good_seed {S : Type u_1} {ι : Type u_2} [Fintype S] [DecidableEq S] (inputs : Finset ι) (bad : ιFinset S) (h : iinputs, (bad i).card < Fintype.card S) :
            ∃ (s : S), iinputs, sbad i

            The probabilistic method: a perfect seed exists. If the total number of "bad" seeds summed over all inputs is strictly less than the number of seeds, then some single seed is good for every input simultaneously. This is the machine-independent counting core of BPP ⊆ P/poly (hardwiring one random string that works for all 2ⁿ inputs of a given length).

            Majority #

            def Complexity.popCount {k : } (f : Fin kBool) :

            The number of true positions of a Boolean vector.

            Equations
            Instances For

              Batteries' fold-based Boolean count agrees with the finite-set support count used by the randomized-counting layer.

              def Complexity.boolFunEquivFinset (α : Type u_1) [Fintype α] [DecidableEq α] :
              (αBool) Finset α

              Boolean-valued functions are equivalent to their true supports.

              Equations
              Instances For
                @[simp]
                theorem Complexity.mem_boolFunEquivFinset {α : Type u_1} [Fintype α] [DecidableEq α] (f : αBool) (i : α) :
                theorem Complexity.card_filter_popCount_eq (k r : ) :
                {f : Fin kBool | popCount f = r}.card = k.choose r

                Exactly k.choose r Boolean vectors of length k have r true entries.

                theorem Complexity.card_filter_popCount_mem (k : ) (s : Finset ) :
                {f : Fin kBool | popCount f s}.card = rs, k.choose r

                The number of vectors whose popCount lies in a finite set is the corresponding sum of binomial coefficients.

                Weighted event counts across blocks #

                def Complexity.blocksEquiv (k T : ) :
                (Fin (k * T)Bool) (Fin kFin TBool)

                A length-k * T seed is equivalently a sequence of k blocks of length T. The product equivalence fixes the row-major schedule consumed by repeated randomized computations.

                Equations
                Instances For
                  @[simp]
                  theorem Complexity.blocksEquiv_apply (k T : ) (w : Fin (k * T)Bool) (i : Fin k) (t : Fin T) :
                  (blocksEquiv k T) w i t = w (finProdFinEquiv (i, t))
                  @[simp]
                  theorem Complexity.blocksEquiv_symm_apply (k T : ) (f : Fin kFin TBool) (p : Fin (k * T)) :

                  Fixed-time repetition schedules #

                  def Complexity.repeatStrideIndexEquiv (k T : ) :
                  Fin (k * T) Fin (k * (T + 2)) Fin (k * (2 * T + 2))

                  Split stride positions into simulation slots and administrative slots.

                  Equations
                  • One or more equations did not get rendered due to their size.
                  Instances For
                    def Complexity.repeatStrideSeedEquiv (k T : ) :
                    (Fin (k * (2 * T + 2))Bool) (Fin (k * T)Bool) × (Fin (k * (T + 2))Bool)

                    Split a full stride seed into simulation and administrative choices.

                    Equations
                    Instances For
                      def Complexity.repeatStrideRandomSeed (k T : ) (w : Fin (k * (2 * T + 2))Bool) :
                      Fin (k * T)Bool

                      Project a repetition stride onto its k * T genuine simulation choices.

                      Equations
                      Instances For
                        def Complexity.repeatRandomSeed (k T : ) (w : Fin (2 + k * (2 * T + 2))Bool) :
                        Fin (k * T)Bool

                        Extract the k * T genuinely random simulation choices from a repetition trace of length 2 + k * (2 * T + 2). The first two choices and the last T + 2 choices of every repetition stride are administrative and ignored.

                        Equations
                        Instances For
                          theorem Complexity.repeatRandomSeed_apply (k T : ) (w : Fin (2 + k * (2 * T + 2))Bool) (i : Fin k) (t : Fin T) :

                          The compact seed's (i,t) entry is the choice at simulation offset t in repetition stride i, after the two initial setup choices.

                          theorem Complexity.card_repeatRandomSeed_fiber (k T : ) (seed : Fin (k * T)Bool) :
                          {w : Fin (2 + k * (2 * T + 2))Bool | repeatRandomSeed k T w = seed}.card = 2 ^ (2 + k * (T + 2))

                          Every compact repetition seed has exactly one freely chosen assignment to each administrative slot in its full-seed fiber.

                          theorem Complexity.card_filter_of_constant_fibers {total compact ignored : } (randomSeed : (Fin totalBool)Fin compactBool) (Accept : (Fin totalBool)Prop) (Good : (Fin compactBool)Prop) [DecidablePred Accept] [DecidablePred Good] (hfactor : ∀ (w : Fin totalBool), Accept w Good (randomSeed w)) (hfiber : ∀ (seed : Fin compactBool), {w : Fin totalBool | randomSeed w = seed}.card = 2 ^ ignored) :

                          If a finite Boolean-seed projection has constant fibers, then every event that factors through the projection gains exactly that common fiber factor.

                          theorem Complexity.card_filter_repeatRandomSeed (k T : ) (P : (Fin (k * T)Bool)Prop) [DecidablePred P] :
                          {w : Fin (2 + k * (2 * T + 2))Bool | P (repeatRandomSeed k T w)}.card = (Finset.filter P Finset.univ).card * 2 ^ (2 + k * (T + 2))

                          An event depending only on the compact repetition seed has its count multiplied by the number of ignored administrative choice assignments.

                          def Complexity.blockEventCount {k T : } (E : Finset (Fin TBool)) (w : Fin (k * T)Bool) :

                          The number of blocks of a long seed that lie in the event E.

                          Equations
                          Instances For
                            theorem Complexity.blockEventCount_add_compl {k T : } (E : Finset (Fin TBool)) (w : Fin (k * T)Bool) :

                            Every block lies in exactly one of an event and its complement.

                            theorem Complexity.card_blockEventCount_eq {T : } (E : Finset (Fin TBool)) (k j : ) :
                            {w : Fin (k * T)Bool | blockEventCount E w = j}.card = k.choose j * E.card ^ j * (2 ^ T - E.card) ^ (k - j)

                            Weighted binomial count in the machine seed space. Exactly the stated binomial number of long seeds have j blocks in E; the two weights are the counts of successful and unsuccessful T-bit blocks.

                            def Complexity.blockMajority {k T : } (E : Finset (Fin TBool)) (w : Fin (k * T)Bool) :

                            Strict majority of the blocks of a long seed lying in the event E.

                            Equations
                            Instances For
                              theorem Complexity.blockMajority_compl_of_odd {k T : } (hk : Odd k) (E : Finset (Fin TBool)) (w : Fin (k * T)Bool) :

                              At an odd number of blocks, complementing the event flips the strict majority verdict.

                              theorem Complexity.blockMajority_eq_false_iff {k T : } (E : Finset (Fin TBool)) (w : Fin (k * T)Bool) :
                              theorem Complexity.card_blockMajority_eq_false (T r : ) (E : Finset (Fin TBool)) :
                              {w : Fin ((2 * r + 1) * T)Bool | blockMajority E w = false}.card = jFinset.range (r + 1), (2 * r + 1).choose j * E.card ^ j * (2 ^ T - E.card) ^ (2 * r + 1 - j)

                              Exact odd-majority failure count. For 2r + 1 independent T-bit blocks, failure means at most r blocks lie in E; each summand is the corresponding weighted binomial fiber.

                              def Complexity.majority {k : } (f : Fin kBool) :

                              The strict majority vote of a Boolean vector: true iff more than half the positions are true.

                              Equations
                              Instances For
                                theorem Complexity.popCount_le {k : } (f : Fin kBool) :

                                popCount is bounded by the length.

                                theorem Complexity.card_majority_eq_true (k : ) :
                                {f : Fin kBool | majority f = true}.card = rFinset.Icc (k / 2 + 1) k, k.choose r

                                The exact binomial upper-tail count for strict-majority success.

                                theorem Complexity.card_majority_eq_false (k : ) :
                                {f : Fin kBool | majority f = false}.card = rFinset.range (k / 2 + 1), k.choose r

                                The exact binomial lower-tail count for strict-majority failure, including ties when the vector length is even.

                                theorem Complexity.card_majority_ne_true (k : ) :
                                {f : Fin kBool | majority f true}.card = rFinset.range (k / 2 + 1), k.choose r

                                Failure phrased as not returning true has the same exact lower-tail count.

                                theorem Complexity.popCount_add_popCount_not {k : } (f : Fin kBool) :
                                (popCount f + popCount fun (i : Fin k) => !f i) = k

                                A position is either true or true-after-negation, never both: the two popCounts sum to the length.

                                theorem Complexity.popCount_not {k : } (f : Fin kBool) :
                                (popCount fun (i : Fin k) => !f i) = k - popCount f

                                popCount of the pointwise negation is the complementary count.

                                theorem Complexity.majority_not_of_odd {k : } (hk : Odd k) (f : Fin kBool) :
                                (majority fun (i : Fin k) => !f i) = !majority f

                                Antisymmetry of majority under negation at odd length. When the length k is odd there are no ties, so negating every bit flips the majority vote.