Documentation

Provenance.Algorithms.CountEnum

Correctness of COUNT enumeration #

This file formalises a count-based enumeration algorithm. The algorithm enumerates the non-empty subsets W of a finite set of occurrences U whose cardinality satisfies |W| op C for a fixed comparison operator op ∈ {=, ≠, <, ≤, >, ≥} and a constant C ∈ ℕ. The main result countEnum_correct shows that the list produced by countEnum coincides with that set, in the sense of membership.

An aggregate term t would be irrelevant to COUNT and is dropped from the Lean signature. The "distinct occurrences" hypothesis is encoded as List.Nodup and is needed in the spec only so that Finset cardinality matches list length: without it, the algorithm still returns subsets of occs.toFinset, but cardinality bookkeeping breaks.

def CountEnum.combinations {α : Type u_1} [DecidableEq α] :
List αFinset αList (Finset α)

Combinations(i, x, W): expressed with the suffix list occs (representing the occurrences (uᵢ, αᵢ), …, (u_N, α_N)) instead of an explicit index i. Returns the list of subsets obtained by extending the accumulator W with exactly x further elements drawn from occs.

Equations
Instances For
    Dependency graph
    def CountEnum.addExact {α : Type u_1} [DecidableEq α] (occs : List α) (x : ) :

    AddExact(x): enumerate the non-empty subsets of occs of cardinality exactly x. The x = 0 case returns [] so that the empty world is excluded from the output.

    Equations
    Instances For
      Dependency graph
      def CountEnum.countEnum {α : Type u_1} [DecidableEq α] (occs : List α) (C : ) (op : CompOp) :

      CountEnum(U, C, op): top-level routine. The six cases of the algorithm collapse into a single flatMap over the satisfying cardinalities x ∈ {0, …, N} because addExact occs 0 is already empty and addExact occs x is empty whenever x > N.

      Equations
      Instances For
        Dependency graph

        Correctness lemmas #

        theorem CountEnum.combinations_mem {α : Type u_1} [DecidableEq α] (occs : List α) :
        occs.Nodup∀ (x : ) (W : Finset α), Disjoint W occs.toFinset∀ (S : Finset α), S combinations occs x W Toccs.toFinset, T.card = x S = W T

        Membership characterisation of combinations. Under disjointness of the accumulator and the suffix list, the output enumerates exactly the sets of the form W ∪ T with T a subset of occs.toFinset of size x. Nodup occs is needed so that the disjointness is preserved on recursion (the head u must not appear later in the list).

        Dependency graph
        theorem CountEnum.addExact_mem {α : Type u_1} [DecidableEq α] (occs : List α) (hnodup : occs.Nodup) (x : ) (S : Finset α) :
        S addExact occs x Soccs.toFinset S.card = x S

        Membership characterisation of addExact: the output enumerates the non-empty subsets of occs of cardinality exactly x.

        Dependency graph
        theorem CountEnum.combinations_nodup {α : Type u_1} [DecidableEq α] (occs : List α) :
        occs.Nodup∀ (x : ) (W : Finset α), Disjoint W occs.toFinset(combinations occs x W).Nodup

        The enumeration combinations contains no duplicate subset. As in combinations_mem, the disjointness of the accumulator and the suffix list is preserved through the recursion; the two recursive calls produce sets that differ on membership of the head u.

        Dependency graph
        theorem CountEnum.addExact_nodup {α : Type u_1} [DecidableEq α] (occs : List α) (hnodup : occs.Nodup) (x : ) :
        (addExact occs x).Nodup

        The enumeration addExact contains no duplicate subset.

        Dependency graph
        theorem CountEnum.countEnum_nodup {α : Type u_1} [DecidableEq α] (occs : List α) (hnodup : occs.Nodup) (C : ) (op : CompOp) :
        (countEnum occs C op).Nodup

        The top-level enumeration countEnum contains no duplicate subset: within one bucket x by addExact_nodup, and across buckets because a subset in bucket x has cardinality exactly x. This is not cosmetic: the provenance attached to the enumeration is the -sum of the world annotations over the returned list, and in a non-idempotent m-semiring a duplicated world would change the value.

        Dependency graph
        theorem CountEnum.countEnum_correct {α : Type u_1} [DecidableEq α] (occs : List α) (hnodup : occs.Nodup) (C : ) (op : CompOp) (S : Finset α) :
        S countEnum occs C op Soccs.toFinset S op.eval S.card C

        Correctness of countEnum. For a list occs of distinct occurrences, a constant C : ℕ, and a comparison operator op, the list countEnum occs C op enumerates exactly the non-empty subsets S ⊆ occs.toFinset whose cardinality satisfies op.eval S.card C.

        Dependency graph