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: compare corresponding records in two halves.Cslib.Circuits.Boolean.bitonicMergeCircuit: merge a bitonic record array.Cslib.Circuits.Boolean.bitonicSortCircuit: sort a power-of-two record array.Cslib.Circuits.Boolean.bitonicSortTraceCircuit: sort and retain the comparison controls.Cslib.Circuits.Boolean.bitonicSortTraceCircuit.pack: controls followed by records.
Main results #
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.
Gather reads corresponding records from the two halves without changing their bits.
The first flat input block is the first half of the record array.
The second flat input block is the second half of the record array.
Flattening the record view recovers the original wire assignment.
Apply a circuit to the payload while retaining every control pair by free wiring.
Bitonic merge, retaining comparison decisions before the merged records.
Instances For
Bitonic sorting, retaining comparison decisions before the sorted records.
Instances For
The forward merge exposes exactly the semantic trace and merged records.
The forward sorter exposes exactly the semantic trace and sorted records.
A traced merge computes each comparator once, retaining its existing control pair.
A traced sort computes each comparator once, retaining its existing control pair.
Merge a bitonic array by selecting its traced merge's record outputs.
Instances For
Sort complete records, retaining the traced construction's sharing internally.
Instances For
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.