arXiv:2610.36196cs.LOOct 2026

Manuscript Offprint

arXiv:2610.36196

count-catcher: Lean Theorems from Model Counting Certificates

Johannes K. Fichte, Stefan Szeider

Abstract

We present count-catcher, a tool that turns a CPOG certificate of a count by the exact model counter d4 into a Lean theorem that a CNF formula defined in Lean has exactly N models over V variables. With an encoding lemma, this theorem becomes a theorem about the number of combinatorial objects. A CPOG certificate consists of a Boolean circuit equivalent to the formula and a proof of equivalence. count-catcher checks in Lean that the formula implies the circuit (forward check) and the circuit implies the formula (reverse check). For the forward check, irat-catcher streams a refutation through the LRAT checker of Lean's standard library. The reverse check and the validity check of the circuit are computations proved correct in Lean. Lean runs all three checks as compiled code, so the theorem depends on Lean's kernel, compiler and runtime, as does CPOG's verified checker. We combine the theorem with results from Mathlib, for instance with Burnside's lemma to count graphs up to isomorphism. We prove the number of Latin squares of order 8 as a weighted sum of hundreds of certified counts from about eight terabytes of certificates. We turn about half of the 2022 Model Counting Competition formulas into theorems, with the three checks taking about a quarter of the time of CPOG's verified checker. We found that CPOG's unverified checker reported wrong counts, and removed its verified checker's dependence on two unproved lemmas. End to end, a theorem costs about the same as a checked count.

1 Introduction

There are exactly 108,776,032,459,082,956,800 Latin squares of order 8. Numbers like this are established by computation. A common approach is to encode the objects as models of a propositional formula and run an exact model counter. For the Latin squares, the formula has one variable per cell and symbol, and its clauses ensure that every row and column forms a permutation. In Lean, we state the number using the following definition of a Latin square:

def IsLatin {n : N} (L : Fin n → Fin n → Fin n) : Prop := (∀ i, Function.Bijective (L i)) ∧ ∀ j, Function.Bijective fun i => L i j theorem latin8_all : Nat.card {L : Fin 8 → Fin 8 → Fin 8 // IsLatin L} = 108776032459082956800

The first thing we need to prove is an encoding lemma that ensures that the Latin squares are in bijection with the models of the formula. The user provides this lemma. The second thing is the count theorem, that a CNF formula F given as a Lean term has exactly N models over a universe of V variables that contains the variables of F. The following is the soundness theorem of our checker:

theorem checkAll_sound (h : checkAll V F pogStr lratStr N = true) : Fintype.card {a : Fin V → Bool // F.eval (ext a) = true} = N

The listing shows that the hypothesis h says that the checker accepts a certificate, given as the strings pogStr and lratStr, and the part after the colon is the count theorem. So once the checker has accepted, the count theorem follows from the soundness theorem. Here, ext a extends the assignment a of the universe by false on all other variables. From the encoding lemma and the count theorem, we obtain an end-to-end theorem in which the number is verified inside Lean from the definition of the objects to the final number. How to establish such a count theorem at the scale of terabytes of certificates is the subject of this paper.

There is a long history of claims of counts that turned out to be wrong, which emphasizes the importance of machine-checkable proofs. For instance, Sade corrected Norton’s 1939 count of the reduced Latin squares of order 7 in 1948 [41, 44, 45, 36], Berman and Köhler confirmed Church’s 1965 value of Dedekind’s number M(7)M(7), the number of monotone Boolean functions of seven variables, against Lunnon’s of 1971 [13, 33, 1], and McKay corrected the 1996 count of the closed knight’s tours of the chessboard a year later [32, 35]. An independent program that checks a certificate excludes such errors only if it is itself correct. In our evaluation (Section 6), cpog-check, the unverified checker of the certificate format CPOG (certified partitioned-operation graphs), reported a wrong count on 7 of 91 certificates.

Model counting, determining the number of models of a propositional formula, is a fundamental #P-complete problem [60]. Exact counters [21] output only the integer of the count, so an error in their search cannot be detected from the output. In certified model counting, the counter or a separate generator also outputs a certificate, which can be checked by an independent checker. CPOG [6, 7] consists of a certificate format for the circuits that counters such as d4 [30] generate, a generator for it, and a checker whose correctness is proved in Lean [39]. Such a correctness theorem for the checker is about the checker's final state. It certifies that if a run ends in the final state, the output count is the count of the parsed DIMACS file (the standard text format of CNF formulas). However, there is no theorem that connects this count to a Lean term or to the combinatorial objects that are counted, like the Latin squares in our example. A different approach is to enumerate the objects and use proof logging in VeriPB and CakePB [34] to obtain theorems in the proof assistant HOL4 about objects. However, enumeration is not a feasible approach if the number of objects is in the order we are considering in this paper.

We present count-catcher, which takes a CNF formula given as a Lean term and returns its model count as a Lean theorem. The user provides the encoding lemma, and our tool exports the formula and runs the exact counter d4 and the CPOG generator. With an untrusted translator, it translates the CPOG certificate into a node table that contains the circuit and an LRAT refutation, which it may shorten. It then writes one line, count_models, into a new Lean theorem file. This line names the theorem, the encoding lemma to apply, the certificate files and the count. When Lean processes this file, it runs a Lean-verified checker on the certificate. Lean then proves the count theorem, an instance of checkAll_sound, and from it and the encoding lemma we obtain the end-to-end theorem, for example latin8_all.

Count theorems can then be used in larger proofs, for instance, to count only up to symmetry. This is made possible by means of Burnside's lemma [22, Chapter 2], which counts objects up to symmetry as the average number of objects that a symmetry fixes. Using this, we can show, for instance, that the number of triangle-free graphs on ten vertices up to isomorphism is a weighted sum of 42 counts of labeled graphs, one for each cycle type (the multiset of cycle lengths) of a permutation of the ten vertices. Here, the count for a cycle type is the number of triangle-free graphs that a permutation of this type fixes. Each of the 42 numbers is a separate model count of a formula. Each count is shown as a theorem with count-catcher. The remainder of the proof uses Burnside's lemma from

Mathlib [51], Lean’s mathematical library, and a decision procedure that checks that the list of cycle types is complete and that the weighted sum gives the claimed number (Section 5.2). We count the Latin squares of order 8 in the same way, as a weighted sum over the cycle types of the second row, which in a reduced square (first row and first column are the identity) is a permutation of the symbols. Counting up to symmetry also allows us to obtain uniqueness results. Up to isomorphism, exactly one graph on 13 vertices has neither a triangle nor five mutually nonadjacent vertices (a (3, 5)-Ramsey graph) [26, 43]. From a count of one isomorphism class, composed of certified counts, we prove in Lean that every such graph is isomorphic to the circulant graph with jumps 1 and 5.

The difficulty is to bring the equality #V(F)=N\#_V(F) = N into the logic at the scale of the certificates. Here, #V(F)\#_V(F) is the number of models of FF over the universe. To use the count as a lemma about the objects, the theorem must be about the formula FF with its clauses and universe. Unlike unsatisfiability, which is preserved when clauses or variables are added, the count can change with one clause more or one variable more. The certificates of the equality in cases of interest are large. We use one certificate for each cycle type of the second row of the Latin square. These are four for order 7 and seven for order 8. There we split the squares of each cycle type further by three cells of the third row into 737 certificates. For the Latin squares of order 7, the certificates take under a gigabyte in total. For order 8, they take 7.9 TB, with up to 32.7 GB each (Appendix A.1). Certificates of this size are too large for a single proof term, and the kernel cannot process them.

The equality is shown by combining the encoding lemma, one refutation and two computations proved correct in Lean, forming the chain

∣X∣=encoding#V(F)=forward,reverse#V(P)=validityc(r)⋅2V−∣S(r)∣=N.(1)|X|\stackrel{\mathrm{encoding}}{=}\#_{V}(F)\stackrel{\mathrm{forward,reverse}}{=}\#_{V}(P)\stackrel{\mathrm{validity}}{=}c(r)\cdot2^{V-|S(r)|}=N. \tag{1}

Here, X denotes the set of objects, for example the Latin squares of order 8, F the formula that encodes them, and P the circuit the certificate is about, with root r, its output node. The circuit P is a POG (partitioned-operation graph), a directed acyclic graph whose leaves are literals and whose inner nodes are products (AND) and sums (OR) (Section 3). Here, #V(P)\#_V(P) denotes the number of assignments of the universe that make the root true, S(r)S(r) the set of variables below it, and c(r)c(r) the number of assignments of S(r)S(r) that make the root true. The first equality is supported by the encoding lemma provided by the user. The second is the forward implication, that every model of F makes r true, together with the reverse implication, that every assignment that makes r true is a model of F. The third is the validity of the POG: the arguments of every product have pairwise disjoint variable sets and the two arguments of every sum are not true together. Under these two conditions we compute its count bottom-up. The fourth is arithmetic. Following CPOG, we call the three checks the forward, the reverse and the validity check.

Checking the reverse implication by evaluating the POG is based on the way it is proposed in SCPOG [8], which extends CPOG to projected counting (models that agree on a given set of variables count once). The forward check requires the verification of one refutation, which proves the forward implication. We shorten it with the translator (Section 4.3) and stream it into Lean by means of lrat-catcher [48]. The validity check and the reverse check are both proved correct in Lean. We compose all in one theorem over the same term, POG and universe. All checks are run by native evaluation, i.e., Lean compiles the checker, executes it, and adds an axiom that the compiled checker returned true. Our trusted base therefore contains, besides the Lean kernel, also the compiler and the runtime. CPOG's verified checker uses the same trusted base.

Our contributions are as follows.

  1. Count theorem: we translate a CPOG certificate into a count theorem over a Lean term and its universe. In this theorem, Lean builds everything that is mentioned; the certificate only supplies data that is checked. We test our checker against enumeration on more than twelve million generated and mutated certificates (Section 4.5).

  2. Bit-parallel reverse check: we decide the reverse implication with a check, proved correct in Lean, that evaluates 63 clauses at once in the bits of a machine word. It is 4.6 times faster in the median than our Lean implementation of SCPOG's top-down evaluation (Section 4.4). The translator shortens the refutation in three untrusted steps before the forward check (Section 4.3).

  3. Counts as lemmas: based on user-provided encoding lemmas, our tool establishes the count, and lemma and count together give an end-to-end theorem. We demonstrate this with compositions by symmetry, such as the Latin squares of order 8, and with single counts, such as Dedekind's number M(7)M(7). Four of the single counts are values of sequences of the On-Line Encyclopedia of Integer Sequences (OEIS) [59] that, to our knowledge, have not been listed before (Sections 5 and 6).

  4. Evaluation: we compare our tool with CPOG's checkers on the 2022 Model Counting Competition [17] and turn 96 of 180 formulas into theorems, with certificates of up to 33.9 GB. The three checks take about a quarter of the time of CPOG's verified checker (Section 6). We found that cpog-check reports wrong counts, which a fix of one line of its source removes (Appendix A.4), and that CPOG's verified checker depends on two unproved lemmas, a dependency we removed (Section 4.5).

Code and experiments. count-catcher with its Lean proofs, the theorem files, the generators, our two patches of cpog-gen and the scripts behind every table and figure are at https://github.com/leansolving/count-catcher; the Zenodo record 10.5281/zenodo.23304375 archives this tree at the release together with the prebuilt tools for x86-64, the logs and result files of the cluster runs, and the list of the competition formulas with their SHA-256 hashes.

References (53)