cfahlgren1/math / lean /OAI /Algorithms /SubexponentialSAT /ExecCNFTupleOdometer.lean
cfahlgren1's picture
download
raw
21 kB
import OAI.Algorithms.SubexponentialSAT.LeanCompat
import OAI.Algorithms.SubexponentialSAT.CounterPrograms
import OAI.Algorithms.SubexponentialSAT.StackRoutines
import OAI.Algorithms.SubexponentialSAT.Encoding
import OAI.Algorithms.SubexponentialSAT.BitProgramCompile
namespace OAI.Algorithms
/-!
# Fixed-control increment of framed binary tuples
Tuple components are little-endian Boolean words carried by
`Encoding.encodeList id`. The finite table below skips framing bits and
increments only payload bits, with carry continuing into the next component.
Neither the number of components nor their widths enters the control table.
-/
namespace SubexponentialSAT.ExecCNFTupleOdometer
open SubexponentialSAT.BitProgram SubexponentialSAT.CounterPrograms
/-- Fixed-width increment. The first component reports carry beyond the word. -/
def bumpWord : List Bool → Bool × List Bool
| [] => (true, [])
| false :: bs => (false, true :: bs)
| true :: bs => let next := bumpWord bs; (next.1, false :: next.2)
/-- Carry travels through successive records in their encoded order. -/
def bumpWords : List (List Bool) → Bool × List (List Bool)
| [] => (true, [])
| word :: words =>
let next := bumpWord word
if next.1 then
let tail := bumpWords words
(tail.1, next.2 :: tail.2)
else (false, next.2 :: words)
@[simp] theorem bumpWord_length (word : List Bool) :
(bumpWord word).2.length = word.length := by
induction word with
| nil => rfl
| cons b bs ih => cases b <;> simp [bumpWord, ih]
@[simp] theorem bumpWords_lengths (words : List (List Bool)) :
(bumpWords words).2.map List.length = words.map List.length := by
induction words with
| nil => rfl
| cons word words ih =>
simp only [bumpWords]
split <;> simp [ih]
/-- All arithmetic is on payload bits; overflow contributes exactly one modulus. -/
theorem bumpWord_value (word : List Bool) :
value (bumpWord word).2 + (if (bumpWord word).1 then 2 ^ word.length else 0) =
value word + 1 := by
induction word with
| nil => rfl
| cons b bs ih =>
cases b with
| false => simp [bumpWord, value]; omega
| true =>
cases h : (bumpWord bs).1 <;>
simp [bumpWord, value, h, pow_succ] at * <;> omega
/-- Three registers hold the scanned bit, presence, and final overflow flag. -/
abbrev program : Program 3 2 18 where
inputStack := 0
outputStack := 0
entry := 0
code q := match q.val with
| 0 => .set 2 true 1
| 1 => .pop 0 0 1 2
| 2 => .branch 0 3 13
| 3 => .push 1 0 4
| 4 => .pop 0 0 1 5
| 5 => .branch 0 6 12
| 6 => .push 1 0 7
| 7 => .pop 0 0 1 8
| 8 => .bnot 0 0 9
| 9 => .push 1 0 10
| 10 => .branch 0 11 4
| 11 => .set 2 false 14
| 12 => .push 1 0 1
| 13 => .push 1 0 14
| 14 => .pop 1 0 1 15
| 15 => .branch 1 16 17
| 16 => .push 0 0 14
| _ => .halt
def cfg (pc : Option (Fin 18)) (v present carry : Bool)
(input scratch : List Bool) : Cfg 3 2 18 :=
⟨pc, counterRegs v present carry, counterStacks input scratch⟩
theorem restore_trace (input scratch : List Bool) (v present carry : Bool) :
Trace program (3 * scratch.length + 3)
(cfg (some 14) v present carry input scratch)
(cfg none false false carry (scratch.reverse ++ input) []) := by
have ht := StackRoutines.transfer_trace program (1 : Fin 2) 0 (0 : Fin 3) 1
14 15 16 17 (by decide) (by decide) rfl rfl rfl
(counterRegs v present carry) (counterStacks input scratch)
have hs : StackRoutines.transferStacks (1 : Fin 2) 0 (counterStacks input scratch) =
counterStacks (scratch.reverse ++ input) [] := by
funext i
fin_cases i <;> simp [StackRoutines.transferStacks, counterStacks]
have hr : readRegs (counterRegs v present carry) 0 1 none =
counterRegs false false carry := by simp [readRegs]
have ht' : Trace program (3 * scratch.length + 2)
(cfg (some 14) v present carry input scratch)
(cfg (some 17) false false carry (scratch.reverse ++ input) []) := by
simpa [cfg, StackRoutines.transferResult, hs, hr] using ht
have hh : Trace program 1
(cfg (some 17) false false carry (scratch.reverse ++ input) [])
(cfg none false false carry (scratch.reverse ++ input) []) :=
.cons (by simp [step, cfg, execInstr]) (.refl _)
simpa [Nat.add_assoc] using ht'.trans hh
theorem word_bit_trace (b v present : Bool) (input scratch : List Bool) :
Trace program 7
(cfg (some 4) v present true (true :: b :: input) scratch)
(cfg (some (if b then 4 else 11)) (!b) true true input ((!b) :: true :: scratch)) := by
have h0 : step program (cfg (some 4) v present true (true :: b :: input) scratch) =
some (cfg (some 5) true true true (b :: input) scratch) := by
simp [step, cfg, execInstr, readRegs]
have h1 : step program (cfg (some 5) true true true (b :: input) scratch) =
some (cfg (some 6) true true true (b :: input) scratch) := by
simp [step, cfg, execInstr]
have h2 : step program (cfg (some 6) true true true (b :: input) scratch) =
some (cfg (some 7) true true true (b :: input) (true :: scratch)) := by
simp [step, cfg, execInstr]
have h3 : step program (cfg (some 7) true true true (b :: input) (true :: scratch)) =
some (cfg (some 8) b true true input (true :: scratch)) := by
simp [step, cfg, execInstr, readRegs]
have h4 : step program (cfg (some 8) b true true input (true :: scratch)) =
some (cfg (some 9) (!b) true true input (true :: scratch)) := by
simp [step, cfg, execInstr]
have h5 : step program (cfg (some 9) (!b) true true input (true :: scratch)) =
some (cfg (some 10) (!b) true true input ((!b) :: true :: scratch)) := by
simp [step, cfg, execInstr]
have h6 : step program (cfg (some 10) (!b) true true input ((!b) :: true :: scratch)) =
some (cfg (some (if b then 4 else 11)) (!b) true true input ((!b) :: true :: scratch)) := by
cases b <;> simp [step, cfg, execInstr]
exact .cons h0 (.cons h1 (.cons h2 (.cons h3 (.cons h4 (.cons h5 (.cons h6 (.refl _)))))))
theorem frame_end_trace (v present : Bool) (input scratch : List Bool) :
Trace program 3
(cfg (some 4) v present true (false :: input) scratch)
(cfg (some 1) false true true input (false :: scratch)) := by
refine .cons (b := cfg (some 5) false true true input scratch) ?_
(.cons (b := cfg (some 12) false true true input scratch) ?_ (.cons ?_ (.refl _)))
all_goals simp [step, cfg, execInstr, readRegs]
theorem record_start_trace (v present : Bool) (input scratch : List Bool) :
Trace program 3
(cfg (some 1) v present true (true :: input) scratch)
(cfg (some 4) true true true input (true :: scratch)) := by
refine .cons (b := cfg (some 2) true true true input scratch) ?_
(.cons (b := cfg (some 3) true true true input scratch) ?_ (.cons ?_ (.refl _)))
all_goals simp [step, cfg, execInstr, readRegs]
theorem list_end_trace (v present : Bool) (input scratch : List Bool) :
Trace program (3 * scratch.length + 9)
(cfg (some 1) v present true (false :: input) scratch)
(cfg none false false true (scratch.reverse ++ false :: input) []) := by
have hp : Trace program 3
(cfg (some 1) v present true (false :: input) scratch)
(cfg (some 14) false true true input (false :: scratch)) := by
refine .cons (b := cfg (some 2) false true true input scratch) ?_
(.cons (b := cfg (some 13) false true true input scratch) ?_ (.cons ?_ (.refl _)))
all_goals simp [step, cfg, execInstr, readRegs]
have h := hp.trans (restore_trace input (false :: scratch) false true true)
have ht : 3 + (3 * (false :: scratch).length + 3) = 3 * scratch.length + 9 := by
simp only [List.length_cons]
omega
rw [ht] at h
simpa [List.reverse_cons, List.append_assoc] using h
/-- If a component is exhausted, scanning continues at the next record. -/
theorem frame_carry_trace (word rest scratch : List Bool) (v present : Bool)
(hcarry : (bumpWord word).1 = true) :
Trace program (7 * word.length + 3)
(cfg (some 4) v present true (Encoding.frame word ++ rest) scratch)
(cfg (some 1) false true true rest
((Encoding.frame (bumpWord word).2).reverse ++ scratch)) := by
induction word generalizing v present scratch with
| nil => simpa [Encoding.frame, bumpWord] using frame_end_trace v present rest scratch
| cons b bs ih =>
cases b with
| false => simp [bumpWord] at hcarry
| true =>
have hc : (bumpWord bs).1 = true := hcarry
have hp := word_bit_trace true v present (Encoding.frame bs ++ rest) scratch
have hi := ih (false :: true :: scratch) false true hc
simpa [Encoding.frame, bumpWord, List.reverse_cons, List.append_assoc,
Nat.mul_add, Nat.add_assoc, Nat.add_comm, Nat.add_left_comm] using hp.trans hi
/-- Once a zero payload bit is found, the finite program restores the entire
scanned prefix and returns without reading later payloads. -/
theorem frame_stop_runs (word rest scratch : List Bool) (v present : Bool)
(hcarry : (bumpWord word).1 = false) :
RunsIn program (13 * word.length + 3 * scratch.length + 4)
(cfg (some 4) v present true (Encoding.frame word ++ rest) scratch)
(cfg none false false false
(scratch.reverse ++ Encoding.frame (bumpWord word).2 ++ rest) []) := by
induction word generalizing v present scratch with
| nil => simp [bumpWord] at hcarry
| cons b bs ih =>
cases b with
| false =>
have hp := word_bit_trace false v present (Encoding.frame bs ++ rest) scratch
have hm : Trace program 1
(cfg (some 11) true true true (Encoding.frame bs ++ rest) (true :: true :: scratch))
(cfg (some 14) true true false (Encoding.frame bs ++ rest) (true :: true :: scratch)) :=
.cons (by simp [step, cfg, execInstr]) (.refl _)
have hr := restore_trace (Encoding.frame bs ++ rest) (true :: true :: scratch)
true true false
refine ⟨7 + 1 + (3 * (true :: true :: scratch).length + 3), ?_, ?_⟩
· simp only [List.length_cons]
omega
· simpa [Encoding.frame, bumpWord, List.reverse_cons, List.append_assoc] using
(hp.trans hm).trans hr
| true =>
have hc : (bumpWord bs).1 = false := hcarry
have hp := word_bit_trace true v present (Encoding.frame bs ++ rest) scratch
have hi := ih (false :: true :: scratch) false true hc
have hp' : RunsIn program 7
(cfg (some 4) v present true (true :: true :: (Encoding.frame bs ++ rest)) scratch)
(cfg (some 4) false true true (Encoding.frame bs ++ rest) (false :: true :: scratch)) :=
⟨7, le_rfl, hp⟩
have hrun := (hp'.trans hi).mono
(show 7 + (13 * bs.length + 3 * (false :: true :: scratch).length + 4) ≤
13 * (true :: bs).length + 3 * scratch.length + 4 by
simp only [List.length_cons]
omega)
simpa [Encoding.frame, bumpWord, List.reverse_cons, List.append_assoc] using hrun
/-- Full framed traversal. A pre-existing reversed prefix on scratch is also
restored, so this theorem composes directly with the record-entry transition. -/
theorem list_runs (words : List (List Bool)) (rest scratch : List Bool) (v present : Bool) :
RunsIn program (7 * (Encoding.encodeList id words).length + 3 * scratch.length + 2)
(cfg (some 1) v present true (Encoding.encodeList id words ++ rest) scratch)
(cfg none false false (bumpWords words).1
(scratch.reverse ++ Encoding.encodeList id (bumpWords words).2 ++ rest) []) := by
induction words generalizing scratch v present with
| nil =>
refine ⟨3 * scratch.length + 9, ?_, ?_⟩
· simp [Encoding.encodeList]
omega
· simpa [Encoding.encodeList, bumpWords, List.append_assoc] using
list_end_trace v present rest scratch
| cons word words ih =>
have hp : Trace program 3
(cfg (some 1) v present true (Encoding.encodeList id (word :: words) ++ rest) scratch)
(cfg (some 4) true true true
(Encoding.frame word ++ (Encoding.encodeList id words ++ rest)) (true :: scratch)) := by
simpa [Encoding.encodeList, List.append_assoc] using
record_start_trace v present (Encoding.frame word ++ (Encoding.encodeList id words ++ rest)) scratch
cases hc : (bumpWord word).1 with
| false =>
have hf := frame_stop_runs word (Encoding.encodeList id words ++ rest)
(true :: scratch) true true hc
have hp' : RunsIn program 3
(cfg (some 1) v present true (Encoding.encodeList id (word :: words) ++ rest) scratch)
(cfg (some 4) true true true
(Encoding.frame word ++ (Encoding.encodeList id words ++ rest)) (true :: scratch)) :=
⟨3, le_rfl, hp⟩
have hb : 3 + (13 * word.length + 3 * (true :: scratch).length + 4) ≤
7 * (Encoding.encodeList id (word :: words)).length + 3 * scratch.length + 2 := by
simp only [Encoding.encodeList, List.length_cons, List.length_append,
Encoding.length_frame, id_eq]
omega
simpa [bumpWords, hc, Encoding.encodeList, List.reverse_cons, List.append_assoc] using
(hp'.trans hf).mono hb
| true =>
have hf := frame_carry_trace word (Encoding.encodeList id words ++ rest)
(true :: scratch) true true hc
have hi := ih ((Encoding.frame (bumpWord word).2).reverse ++ true :: scratch) false true
have hp' : RunsIn program (3 + (7 * word.length + 3))
(cfg (some 1) v present true (Encoding.encodeList id (word :: words) ++ rest) scratch)
(cfg (some 1) false true true (Encoding.encodeList id words ++ rest)
((Encoding.frame (bumpWord word).2).reverse ++ true :: scratch)) :=
⟨_, le_rfl, hp.trans hf⟩
have hb : (3 + (7 * word.length + 3)) +
(7 * (Encoding.encodeList id words).length +
3 * ((Encoding.frame (bumpWord word).2).reverse ++ true :: scratch).length + 2) ≤
7 * (Encoding.encodeList id (word :: words)).length + 3 * scratch.length + 2 := by
simp only [Encoding.encodeList, List.length_cons, List.length_append,
List.length_reverse, Encoding.length_frame, bumpWord_length, id_eq]
omega
simpa [bumpWords, hc, Encoding.encodeList, List.reverse_append,
List.reverse_cons, List.append_assoc] using (hp'.trans hi).mono hb
/-- One complete invocation of the fixed table. The suffix is untouched and
the scratch stack is cleared. The bound counts actual bit-program instructions. -/
theorem increment_runs (words : List (List Bool)) (rest : List Bool)
(v present carry : Bool) :
RunsIn program (7 * (Encoding.encodeList id words).length + 3)
(cfg (some 0) v present carry (Encoding.encodeList id words ++ rest) [])
(cfg none false false (bumpWords words).1
(Encoding.encodeList id (bumpWords words).2 ++ rest) []) := by
have hp : Trace program 1
(cfg (some 0) v present carry (Encoding.encodeList id words ++ rest) [])
(cfg (some 1) v present true (Encoding.encodeList id words ++ rest) []) :=
.cons (by simp [step, cfg, execInstr]) (.refl _)
have hp' : RunsIn program 1
(cfg (some 0) v present carry (Encoding.encodeList id words ++ rest) [])
(cfg (some 1) v present true (Encoding.encodeList id words ++ rest) []) :=
⟨1, le_rfl, hp⟩
have h := hp'.trans (list_runs words rest [] v present)
have hc : 1 + (7 * (Encoding.encodeList id words).length + 3 * ([] : List Bool).length + 2) =
7 * (Encoding.encodeList id words).length + 3 := by simp; omega
rw [hc] at h
simpa using h
/-- The verified compiler yields actual primitive-tape execution, with fixed
overhead independent of every tuple dimension and payload value. -/
theorem increment_primitive (words : List (List Bool)) (rest : List Bool)
(v present carry : Bool) :
Computation.RunsIn (primitive program).step
(primitiveOverhead program * (7 * (Encoding.encodeList id words).length + 3))
(primitiveCfg program
(cfg (some 0) v present carry (Encoding.encodeList id words ++ rest) []))
(primitiveCfg program
(cfg none false false (bumpWords words).1
(Encoding.encodeList id (bumpWords words).2 ++ rest) [])) :=
primitive_execution program (increment_runs words rest v present carry)
/-- Concatenation transports carry, independently of the frame representation. -/
theorem bumpWord_append (first second : List Bool) :
bumpWord (first ++ second) =
if (bumpWord first).1 then
((bumpWord second).1, (bumpWord first).2 ++ (bumpWord second).2)
else (false, (bumpWord first).2 ++ second) := by
induction first with
| nil => simp [bumpWord]
| cons b bs ih =>
cases b with
| false => simp [bumpWord]
| true =>
cases hc : (bumpWord bs).1 <;> simp [bumpWord, ih, hc]
/-- The tuple odometer is exactly fixed-width increment of all payload bits. -/
theorem bumpWords_flatten (words : List (List Bool)) :
((bumpWords words).1, (bumpWords words).2.flatten) = bumpWord words.flatten := by
induction words with
| nil => rfl
| cons word words ih =>
have hf : (bumpWords words).1 = (bumpWord words.flatten).1 := congrArg Prod.fst ih
have hs : (bumpWords words).2.flatten = (bumpWord words.flatten).2 := congrArg Prod.snd ih
cases hc : (bumpWord word).1 <;>
simp [bumpWords, hc, List.flatten_cons, bumpWord_append, hf, hs]
theorem value_lt_power_length (word : List Bool) : value word < 2 ^ word.length := by
induction word with
| nil => simp
| cons b bs ih =>
cases b <;> simp [value, pow_succ] at * <;> omega
@[simp] theorem bumpWords_flatten_length (words : List (List Bool)) :
(bumpWords words).2.flatten.length = words.flatten.length := by
have hs : (bumpWords words).2.flatten = (bumpWord words.flatten).2 :=
congrArg Prod.snd (bumpWords_flatten words)
rw [hs, bumpWord_length]
theorem bumpWords_value (words : List (List Bool)) :
value (bumpWords words).2.flatten +
(if (bumpWords words).1 then 2 ^ words.flatten.length else 0) =
value words.flatten + 1 := by
have hf : (bumpWords words).1 = (bumpWord words.flatten).1 :=
congrArg Prod.fst (bumpWords_flatten words)
have hs : (bumpWords words).2.flatten = (bumpWord words.flatten).2 :=
congrArg Prod.snd (bumpWords_flatten words)
rw [hf, hs]
exact bumpWord_value words.flatten
/-- Before the last bit-tuple, the counter advances by exactly one and does
not report overflow. This supports a rank argument for exhaustive traversal. -/
theorem advance_before_last (words : List (List Bool))
(h : value words.flatten + 1 < 2 ^ words.flatten.length) :
(bumpWords words).1 = false ∧ value (bumpWords words).2.flatten = value words.flatten + 1 := by
have hv := bumpWords_value words
cases hc : (bumpWords words).1 with
| false =>
refine ⟨rfl, ?_⟩
simpa only [hc, Bool.false_eq_true, ite_false, add_zero] using hv
| true =>
have hv' : value (bumpWords words).2.flatten + 2 ^ words.flatten.length =
value words.flatten + 1 := by simpa only [hc, ite_true] using hv
omega
/-- The last bit-tuple resets all payload bits to value zero and reports
overflow. Record boundaries and every component width remain unchanged. -/
theorem overflow_at_last (words : List (List Bool))
(h : value words.flatten + 1 = 2 ^ words.flatten.length) :
(bumpWords words).1 = true ∧ value (bumpWords words).2.flatten = 0 := by
have hv := bumpWords_value words
have hb := value_lt_power_length (bumpWords words).2.flatten
rw [bumpWords_flatten_length] at hb
cases hc : (bumpWords words).1 with
| false =>
have hv' : value (bumpWords words).2.flatten = value words.flatten + 1 := by
simpa only [hc, Bool.false_eq_true, ite_false, add_zero] using hv
omega
| true =>
refine ⟨rfl, ?_⟩
have hv' : value (bumpWords words).2.flatten + 2 ^ words.flatten.length =
value words.flatten + 1 := by simpa only [hc, ite_true] using hv
omega
theorem encoded_length (words : List (List Bool)) :
(Encoding.encodeList id words).length =
2 * words.flatten.length + 2 * words.length + 1 := by
induction words with
| nil => rfl
| cons word words ih =>
rw [Encoding.length_encodeList_cons]
simp only [id_eq, List.flatten_cons, List.length_append, List.length_cons]
omega
/-- A useful bound when each index record has at most w payload bits. -/
theorem increment_runs_width (words : List (List Bool)) (rest : List Bool)
(v present carry : Bool) (w : ℕ) (hwidth : ∀ word ∈ words, word.length ≤ w) :
RunsIn program ((14 * w + 14) * words.length + 10)
(cfg (some 0) v present carry (Encoding.encodeList id words ++ rest) [])
(cfg none false false (bumpWords words).1
(Encoding.encodeList id (bumpWords words).2 ++ rest) []) := by
apply (increment_runs words rest v present carry).mono
have h := Encoding.length_encodeList_upper id words w hwidth
nlinarith
end SubexponentialSAT.ExecCNFTupleOdometer
end OAI.Algorithms

Xet Storage Details

Size:
21 kB
·
Xet hash:
de1b56429937bd5d4a047e2850d46282b10ad97f094a48780bc8debff45860a8

Xet efficiently stores files, intelligently splitting them into unique chunks and accelerating uploads and downloads. More info.