Comparing and selecting words #
Circuits over the De Morgan basis that compare words of w bits, read as numbers with the least
significant bit first (BitString.equivFin), and select one of two words.
- The multiplexer
muxCircuit wreads a control bitcand two wordsaandb, and outputsawhencis true andbotherwise. Bitiis(c ∧ a i) ∨ (¬c ∧ b i), with one negation ofcshared by all bits:3 w + 1gates and depth at most3. - The comparator
leCircuit wreads two wordsxandyand outputs whetherx ≤ y. It scans the bits from the least significant:x ≤ yon the firstk + 1bits whenx k < y k, or whenx k = y kandx ≤ yon the firstkbits; that is the majority of¬x k,y kand the comparison of the firstkbits. This costs five gates a bit and one for the comparison of empty words:5 w + 1gates and depth at most4 w + 1. - The minimum
minCircuit wselects the smaller of two words, the first on ties: a comparator feeding a multiplexer, of8 w + 2gates. The keyed minimumkeyedMinCircuit k pselects, of two records made of a key ofkbits followed by a payload ofpbits, the one with the smaller key, the first on ties, with8 k + 3 p + 2gates.
Zero-width words and keys are allowed; the comparator of empty words returns true. Pairwise keyed selection preserves the first payload on equal keys.
A balanced tree of keyed minimums selects a record with the least key among many
(Cslib.Circuits.Boolean.tournamentCircuit).
Main definitions #
Cslib.Circuits.Boolean.muxCircuit,Cslib.Circuits.Boolean.leCircuit,Cslib.Circuits.Boolean.minCircuit,Cslib.Circuits.Boolean.keyedMinCircuit: the circuits.Cslib.BitString.recordKey,Cslib.BitString.keyedMinFn: the key of a record, and the record with the smaller key.
Main results #
Cslib.Circuits.Boolean.eval_muxCircuit,size_muxCircuit,depth_muxCircuit_le.Cslib.Circuits.Boolean.eval_leCircuit,size_leCircuit,depth_leCircuit_le.Cslib.Circuits.Boolean.eval_minCircuit,size_minCircuit,depth_minCircuit_le.Cslib.Circuits.Boolean.eval_keyedMinCircuit,size_keyedMinCircuit,depth_keyedMinCircuit_le.
References #
Adapted from Samuel Schlesinger's earlier Complexitylib modules
Circuits/{Multiplexer,BinaryComparison,BinaryMinimum,KeyedMinimum}.
The multiplexer #
@[simp]
theorem Cslib.Circuits.Boolean.eval_muxCircuit {w : ℕ} (c : Bool) (a b : BitString w) :
(muxCircuit w).eval interpretation (Fin.append (fun (x : Fin 1) => c) (Fin.append a b)) = if c = true then a else b
@[simp]
The comparator #
One step of the comparator: from whether x ≤ y on the bits below k, and from x k and
y k, whether x ≤ y on the bits up to k.
Instances For
@[simp]
theorem Cslib.Circuits.Boolean.eval_leCircuit {w : ℕ} (x y : BitString w) :
(leCircuit w).eval interpretation (Fin.append x y) = fun (x_1 : Fin 1) =>
decide ((BitString.equivFin w) x ≤ (BitString.equivFin w) y)
The minimum and the keyed minimum #
@[simp]
theorem Cslib.Circuits.Boolean.eval_minCircuit {w : ℕ} (x y : BitString w) :
(minCircuit w).eval interpretation (Fin.append x y) = if (BitString.equivFin w) x ≤ (BitString.equivFin w) y then x else y
@[simp]
@[simp]
@[simp]