Batcher sorting circuits #
Records occupy consecutive blocks of input wires. The key selects coordinates in
lexicographic order, with coordinate zero most significant. Each comparison moves whole
records and shares its comparison flag across the record bits. Descending layers reverse
ascending pairs, including ties; the complete sorter is not stable.
Traced outputs prepend each comparison decision and its complement to the record blocks,
sharing the original negation. Ordinary sorting selects the record outputs by free wiring.
Empty records bypass the traced construction and use identity wiring; traced sorting still
computes its control pairs.
At depth d there are 2^d records and d * (d + 1) * 2^d / 4 comparisons.
The circuit constructor is executable; the gate-count theorems do not certify its running time.
Main definitions #
Cslib.Circuits.Boolean.bitonicLayerCircuit,Cslib.Circuits.Boolean.bitonicLayerCircuit.pairInput,Cslib.Circuits.Boolean.bitonicLayerCircuit.gather,Cslib.Circuits.Boolean.bitonicLayerCircuit.scatter: compare corresponding records in two halves.Cslib.Circuits.Boolean.bitonicMergeCircuit,Cslib.Circuits.Boolean.bitonicMergeCircuit.firstHalf,Cslib.Circuits.Boolean.bitonicMergeCircuit.secondHalf,Cslib.Circuits.Boolean.bitonicMergeTraceCircuit: merge a bitonic record array, with or without retaining comparison controls.Cslib.Circuits.Boolean.bitonicSortCircuit: sort a power-of-two record array.Cslib.Circuits.Boolean.bitonicSortTraceCircuit,Cslib.Circuits.Boolean.bitonicSortTraceCircuit.layer,Cslib.Circuits.Boolean.bitonicSortTraceCircuit.pair,Cslib.Circuits.Boolean.bitonicSortTraceCircuit.pairs,Cslib.Circuits.Boolean.bitonicSortTraceCircuit.halves,Cslib.Circuits.Boolean.bitonicSortTraceCircuit.collect,Cslib.Circuits.Boolean.bitonicSortTraceCircuit.chain: sort and retain the comparison controls.Cslib.Circuits.Boolean.bitonicSortTraceCircuit.flatten,Cslib.Circuits.Boolean.bitonicSortTraceCircuit.controls,Cslib.Circuits.Boolean.bitonicSortTraceCircuit.records,Cslib.Circuits.Boolean.bitonicSortTraceCircuit.pack,Cslib.Circuits.Boolean.bitonicSortTraceCircuit.layout: wire layout of controls followed by records.
Main results #
Cslib.Circuits.Boolean.cost_program_bitonicSortTraceCircuit: exact cost of the traced sort.Cslib.Circuits.Boolean.eval_bitonicSortCircuit: agreement with the generic algorithm.Cslib.Circuits.Boolean.eval_bitonicSortTraceCircuit: exact trace and record semantics.Cslib.Circuits.Boolean.monotone_bitonicSortCircuitandCslib.Circuits.Boolean.antitone_bitonicSortCircuit: key sortedness in both directions.Cslib.Circuits.Boolean.bitonicSortCircuit_perm: preservation of complete records.Cslib.Circuits.Boolean.four_mul_cost_program_bitonicSortCircuit: exact weighted gate cost.Cslib.Circuits.Boolean.cost_program_bitonicSortTraceCircuit_eq: retaining controls adds no gates at nonzero record width, under arbitrary gate weights.
References #
- K. E. Batcher, Sorting Networks and Their Applications.
- Adapted from MassProduction/SortingNetwork.lean and MassProduction/SortingCorrectness.lean in S. Schlesinger, Boolean circuit mass production formalization.
Apply a circuit to the payload while retaining every control pair by free wiring.
Bitonic merge, retaining comparison decisions before the merged records.
Bitonic sorting, retaining comparison decisions before the sorted records.
The forward merge exposes exactly the semantic trace and merged records.
The forward sorter exposes exactly the semantic trace and sorted records.
A traced sort computes each comparator once, retaining its existing control pair.
Reading complete output records agrees with the generic bitonic comparison layer.
The native circuit realizes bitonic merge on complete records.
The native circuit realizes Batcher sorting on complete records.
The output keys of ascending sorting are nondecreasing.
The output keys of descending sorting are nonincreasing.
Sorting preserves the multiset of complete input records, including payload and duplicates.
A layer uses n copies of the comparator under any assignment of gate costs.
Exact sorting cost: depth * (depth + 1) * 2^depth / 4 comparisons. The identity includes
depth zero, empty keys, empty records, and arbitrary gate costs.
Retaining the trace adds no gates to ordinary sorting when records are nonempty.