Documentation

CslibFrontier.Algorithms.WordRAM.Schoening.Counter

A multiword counter on the word RAM #

Schöning's algorithm runs about (4 / 3) ^ n trials for 3-SAT, a number of 0.415 n bits, which does not fit a word of O(log n) bits. This module stores such a number in memory as its little-endian binary digits, one digit per word, and provides the three routines the amplified algorithm needs (Cslib.Algorithms.WordRAM.Schoening.ampProgram):

Each routine is specified against a function on digit lists (mulAddDigits, divDigits, decDigits), whose value under Nat.ofDigits 2 is proved separately. A multiply-add or a division takes time linear in the number of digits. A decrement stops at the lowest nonzero digit, so it takes decSteps ds iterations, and decSteps_add_count_le bounds this by the number of zero digits it consumes, which gives the amortized constant cost per decrement used by the amplified algorithm.

The routines use registers of the register file of Schöning's algorithm (Cslib.Algorithms.WordRAM.Schoening.Reg). Those beyond the trial's own (bBase, bLen, flag) keep the counter between trials; the others share registers with the scan, which are dead between trials.

Main definitions #

Main results #

References #

Digit lists #

The multiply-add pass over the little-endian binary digits ds with multiplier a and carry cy: one new digit per old digit, and the final carry.

The binary digits of v a + cy, where ds are the digits of v: the pass, then the digits of the final carry.

Division by d of the number with digits ds, with r added at the top: the digits of the quotient, one per digit, and the remainder. The high digits are divided first.

Decrement on binary digits: zeros below the lowest nonzero digit become ones, and that digit becomes zero. A list of zeros becomes a list of ones.

The number of digits a decrement reads: up to and including the lowest nonzero digit.

theorem Cslib.Algorithms.WordRAM.Schoening.ofDigits_mulAddPass (a cy : ℕ) (ds : List ℕ) :
Nat.ofDigits 2 (mulAddPass a cy ds).1 + 2 ^ ds.length * (mulAddPass a cy ds).2 = Nat.ofDigits 2 ds * a + cy

The pass computes v a + cy, split into its low digits and the final carry.

theorem Cslib.Algorithms.WordRAM.Schoening.mulAddPass_carry_le {a cy B : ℕ} {ds : List ℕ} (hds : ∀ d ∈ ds, d ≤ 1) (ha : a ≤ B) (hc : cy ≤ B) :
(mulAddPass a cy ds).2 ≤ B

The carry of the pass stays below any bound on the multiplier and the initial carry.

theorem Cslib.Algorithms.WordRAM.Schoening.mulAddPass_digit_le (a cy : ℕ) (ds : List ℕ) (d : ℕ) :
d ∈ (mulAddPass a cy ds).1 → d ≤ 1

The digits of the pass are binary.

The value of a multiply-add.

The digits of a multiply-add are binary.

theorem Cslib.Algorithms.WordRAM.Schoening.length_mulAddDigits_le {a cy B : ℕ} {ds : List ℕ} (hds : ∀ d ∈ ds, d ≤ 1) (ha : a ≤ B) (hc : cy ≤ B) :

A multiply-add appends at most the number of digits of a bound on the multiplier and the carry.

A multiply-add does not shorten the digit list.

theorem Cslib.Algorithms.WordRAM.Schoening.divDigits_append_singleton (d r b : ℕ) (bs : List ℕ) :
divDigits d r (bs ++ [b]) = ((divDigits d ((2 * r + b) % d) bs).1 ++ [(2 * r + b) / d], (divDigits d ((2 * r + b) % d) bs).2)

Dividing a list with one more digit on top: the top digit is divided first.

theorem Cslib.Algorithms.WordRAM.Schoening.divDigits_rem_lt {d r : ℕ} (hd : 0 < d) (hr : r < d) (ds : List ℕ) :
(divDigits d r ds).2 < d

The remainder stays below the divisor.

theorem Cslib.Algorithms.WordRAM.Schoening.divDigits_digit_le {d r : ℕ} (hd : 0 < d) (hr : r < d) {ds : List ℕ} (hds : ∀ b ∈ ds, b ≤ 1) (q : ℕ) :
q ∈ (divDigits d r ds).1 → q ≤ 1

The quotient digits are binary.

Division with remainder.

The value of a division from remainder 0: the floor of the quotient.

A number is zero exactly when all its digits are.

theorem Cslib.Algorithms.WordRAM.Schoening.ofDigits_decDigits {ds : List ℕ} (hds : ∀ d ∈ ds, d ≤ 1) (hpos : 0 < Nat.ofDigits 2 ds) :

The value of a decrement of a nonzero number with binary digits.

theorem Cslib.Algorithms.WordRAM.Schoening.decDigits_digit_le {ds : List ℕ} (hds : ∀ d ∈ ds, d ≤ 1) (d : ℕ) :
d ∈ decDigits ds → d ≤ 1

The digits of a decrement of binary digits are binary.

Amortized cost of a decrement. The digits read, minus two, are paid for by the zero digits consumed.

Registers #

@[reducible, inline]

Base address of the counter digits.

@[reducible, inline]

Number of counter digits.

@[reducible, inline]

Whether the amplified algorithm runs another trial.

@[reducible, inline]

Digit index of the counter routines; shares register 12 with sgn.

@[reducible, inline]

Multiplier of a multiply-add; shares register 8 with lend.

@[reducible, inline]

Carry of a multiply-add; shares register 9 with sat.

@[reducible, inline]

Divisor of a division; shares register 10 with lit.

@[reducible, inline]

Remainder of a division; shares register 11 with var.

@[reducible, inline]

Scratch value of a division; shares register 16 with cPtr.

Multiply-add #

theorem Cslib.Algorithms.WordRAM.Schoening.mulAddLoop_spec {w a Bd : ℕ} (haB : a ≤ Bd) (hf : WordRAM.Fits w (2 * Bd)) (ds : List ℕ) (x : WordRAM.Data 24) (cy : ℕ) (hone : x.regs Reg.one = 1) (ha : x.regs Reg.mulr = a) (hi : x.regs Reg.idx = 0) (hl : x.regs Reg.bLen = ds.length) (hc : x.regs Reg.carry = cy) (hcB : cy ≤ Bd) (hh : WordRAM.Block.HoldsList (x.regs Reg.bBase) ds x.mem) (hds : ∀ d ∈ ds, d ≤ 1) (hfit : WordRAM.Fits w (x.regs Reg.bBase + ds.length)) :
∃ (y : WordRAM.Data 24), WordRAM.ExecWithin w mulAddLoop x y (10 * ds.length + 2) ∧ WordRAM.Block.HoldsList (x.regs Reg.bBase) (mulAddPass a cy ds).1 y.mem ∧ y.regs Reg.carry = (mulAddPass a cy ds).2 ∧ ∀ b < x.regs Reg.bBase, y.mem b = x.mem b

The multiply-add pass. From the digits ds with idx = 0, the loop replaces them by the digits of the pass and leaves its final carry in carry.

theorem Cslib.Algorithms.WordRAM.Schoening.carryLoop_spec {w : ℕ} (cy : ℕ) (ds : List ℕ) (x : WordRAM.Data 24) (hz : x.regs Reg.zero = 0) (hone : x.regs Reg.one = 1) (hc : x.regs Reg.carry = cy) (hl : x.regs Reg.bLen = ds.length) (hh : WordRAM.Block.HoldsList (x.regs Reg.bBase) ds x.mem) (hfc : WordRAM.Fits w cy) (hfit : WordRAM.Fits w (x.regs Reg.bBase + ds.length + (Nat.digits 2 cy).length)) :

Appending the carry.

theorem Cslib.Algorithms.WordRAM.Schoening.mulAdd_spec {w a cy Bd : ℕ} {ds : List ℕ} {x : WordRAM.Data 24} (hz : x.regs Reg.zero = 0) (hone : x.regs Reg.one = 1) (ha : x.regs Reg.mulr = a) (hc : x.regs Reg.carry = cy) (hl : x.regs Reg.bLen = ds.length) (hh : WordRAM.Block.HoldsList (x.regs Reg.bBase) ds x.mem) (hds : ∀ d ∈ ds, d ≤ 1) (haB : a ≤ Bd) (hcB : cy ≤ Bd) (hf : WordRAM.Fits w (2 * Bd)) (hfit : WordRAM.Fits w (x.regs Reg.bBase + (mulAddDigits a cy ds).length)) :

Multiply-add. On a counter with binary digits ds, with mulr = a, carry = cy and both at most Bd, the routine leaves the digits mulAddDigits a cy ds of v a + cy.

Division #

One digit of the division, from the top: divide 2 · rmd + digit by dvs.

Division: replace the counter v by ⌊v / dvs⌋.

theorem Cslib.Algorithms.WordRAM.Schoening.divLoop_spec {w d : ℕ} (hd : 0 < d) (hfd : WordRAM.Fits w (2 * d)) (ds : List ℕ) (x : WordRAM.Data 24) (hz : x.regs Reg.zero = 0) (hone : x.regs Reg.one = 1) (hdv : x.regs Reg.dvs = d) (hi : x.regs Reg.idx = ds.length) (hr : x.regs Reg.rmd = 0) (hh : WordRAM.Block.HoldsList (x.regs Reg.bBase) ds x.mem) (hb : ∀ b ∈ ds, b ≤ 1) (hfit : WordRAM.Fits w (x.regs Reg.bBase + ds.length)) :
∃ (y : WordRAM.Data 24), WordRAM.ExecWithin w divLoop x y (10 * ds.length + 2) ∧ WordRAM.Block.HoldsList (x.regs Reg.bBase) (divDigits d 0 ds).1 y.mem ∧ ∀ b < x.regs Reg.bBase, y.mem b = x.mem b

The division loop. From the digits ds with idx at their end and remainder 0, the loop replaces them, from the top, by the digits of the quotient.

theorem Cslib.Algorithms.WordRAM.Schoening.divFloor_spec {w d : ℕ} {ds : List ℕ} {x : WordRAM.Data 24} (hd : 0 < d) (hfd : WordRAM.Fits w (2 * d)) (hz : x.regs Reg.zero = 0) (hone : x.regs Reg.one = 1) (hdv : x.regs Reg.dvs = d) (hl : x.regs Reg.bLen = ds.length) (hh : WordRAM.Block.HoldsList (x.regs Reg.bBase) ds x.mem) (hds : ∀ b ∈ ds, b ≤ 1) (hfit : WordRAM.Fits w (x.regs Reg.bBase + ds.length)) :
∃ (y : WordRAM.Data 24), WordRAM.ExecWithin w divFloor x y (10 * ds.length + 4) ∧ WordRAM.Block.HoldsList (x.regs Reg.bBase) (divDigits d 0 ds).1 y.mem ∧ ∀ b < x.regs Reg.bBase, y.mem b = x.mem b

Division. On a counter with binary digits ds and dvs = d > 0, the routine leaves the digits (divDigits d 0 ds).1 of ⌊v / d⌋, with the same length.

Decrement #

One digit of the decrement: a zero becomes one and the scan continues; a nonzero digit becomes zero, the routine records that the counter was nonzero, and the scan stops.

Decrement: decrement a nonzero counter and set found = 1; on a zero counter set found = 0.

theorem Cslib.Algorithms.WordRAM.Schoening.decLoop_spec {w : ℕ} (ds pre : List ℕ) (x : WordRAM.Data 24) :
x.regs Reg.zero = 0 → x.regs Reg.one = 1 → x.regs Reg.idx = pre.length → x.regs Reg.bLen = pre.length + ds.length → x.regs Reg.found = 0 → WordRAM.Block.HoldsList (x.regs Reg.bBase) (pre ++ ds) x.mem → WordRAM.Fits w (x.regs Reg.bBase + pre.length + ds.length) → ∃ (y : WordRAM.Data 24), WordRAM.ExecWithin w decLoop x y (9 * decSteps ds + 2) ∧ WordRAM.Block.HoldsList (x.regs Reg.bBase) (pre ++ decDigits ds) y.mem ∧ (y.regs Reg.found = if ∀ d ∈ ds, d = 0 then 0 else 1) ∧ ∀ b < x.regs Reg.bBase, y.mem b = x.mem b

The decrement loop.

theorem Cslib.Algorithms.WordRAM.Schoening.decCounter_spec {w : ℕ} {ds : List ℕ} {x : WordRAM.Data 24} (hz : x.regs Reg.zero = 0) (hone : x.regs Reg.one = 1) (hl : x.regs Reg.bLen = ds.length) (hh : WordRAM.Block.HoldsList (x.regs Reg.bBase) ds x.mem) (hfit : WordRAM.Fits w (x.regs Reg.bBase + ds.length)) :
∃ (y : WordRAM.Data 24), WordRAM.ExecWithin w decCounter x y (9 * decSteps ds + 4) ∧ WordRAM.Block.HoldsList (x.regs Reg.bBase) (decDigits ds) y.mem ∧ (y.regs Reg.found = if ∀ d ∈ ds, d = 0 then 0 else 1) ∧ ∀ b < x.regs Reg.bBase, y.mem b = x.mem b

Decrement. On a counter with digits ds, the routine leaves the digits decDigits ds within 9 · decSteps ds + 4 instructions and sets found to whether some digit was nonzero.