Documentation

Complexity.Computability.Circuit.Boolean.DuplicateFlags

Duplicate flags with shared comparisons and saved swaps #

A sorted array has a repeated key at a position exactly when that key equals a neighbor. adjacentDuplicateCircuit computes each adjacent equality once and shares it between the neighbors. It adds one OR gate per record; the last record reuses the first equality output, which is false, for its missing successor. The empty array uses no gates.

duplicateFlagsCircuit accepts an arbitrary array of 2 ^ d records. It sorts by selected key coordinates, computes the flags, then reverses the saved swaps on the flags alone. Outputs refer to the original positions, including when complete records repeat. No record identifiers or second sort are needed. Header and payload bits can be retained by free input wiring.

Costs include constants and negations. Gate counts do not certify construction runtime.

Main definitions #

Main results #

References #

Simplifies MassProduction/Nonuniform/AdjacentDuplicates.lean and the conflict-detection construction in S. Schlesinger, Boolean circuit mass production formalization, using the saved decisions of K. E. Batcher, Sorting Networks and Their Applications.

def Cslib.Circuits.Boolean.adjacentDuplicateCircuit {input count k : ℕ} (key : Fin count → Fin k → Fin input) :
Circuit signature input count

Test both neighbors, sharing each adjacent equality once.

Instances For
    theorem Cslib.Circuits.Boolean.eval_adjacentDuplicateCircuit_iff {input count k : ℕ} (key : Fin count → Fin k → Fin input) (x : BitString input) (ordered : Monotone fun (i : Fin count) => toLex (x ∘ key i)) (i : Fin count) :
    (adjacentDuplicateCircuit key).eval interpretation x i = true ↔ ∃ (j : Fin count), j ≠ i ∧ x ∘ key j = x ∘ key i

    On sorted keys, adjacent equality detects every repeated key.

    theorem Cslib.Circuits.Boolean.cost_program_adjacentDuplicateCircuit {input count k : ℕ} (key : Fin count → Fin k → Fin input) (cost : signature.Op → ℕ) :

    Sharing the equality outputs adds exactly one OR gate per record.

    Regard a vector of flags as an array of one-bit records.

    Instances For

      Select the flag from each one-bit record.

      Instances For

        Sort once, flag repeated selected keys, and return flags to original positions.

        Instances For

          A flag is true exactly when another original record has the same selected key.

          Exact weighted cost of one sort, shared adjacent tests, OR gates, and flag restoration.

          Exact total gate count, including empty keys and zero-width records.