cfahlgren1's picture
download
raw
39.4 kB

The unsigned value of a little-endian Boolean stack.

19
def value : List Bool → ℕ
body · 2 lines
  | [] => 0
  | b :: bs => b.toNat + 2 * value bs
23
@[simp] theorem value_nil : value [] = 0 := rfl
24
@[simp] theorem value_false_cons (bs : List Bool) : value (false :: bs) = 2 * value bs := by
proof · 1 line
  simp [value]
26
@[simp] theorem value_true_cons (bs : List Bool) : value (true :: bs) = 1 + 2 * value bs := rfl

Compatibility with the arithmetic module's identical little-endian convention.

29
theorem value_eq_toNat (bs : List Bool) : value bs = BinaryArithmetic.toNat bs := by
proof · 4 lines
  induction bs with
  | nil => rfl
  | cons b bs ih =>
      cases b <;> simp [value, BinaryArithmetic.toNat, BinaryArithmetic.bitVal, ih]

The same finite carry/borrow control works in both directions. true is increment; false is decrement, whose value contract requires positive input.

37
def ripple (carry : Bool) : List Bool → List Bool
body · 2 lines
  | [] => [carry]
  | b :: bs => if b = carry then (!carry) :: ripple carry bs else carry :: bs
41
@[simp] theorem ripple_nil (carry : Bool) : ripple carry [] = [carry] := rfl
43
theorem value_increment (bs : List Bool) : value (ripple true bs) = value bs + 1 := by
proof · 4 lines
  induction bs with
  | nil => rfl
  | cons b bs ih =>
      cases b <;> simp [ripple, value] at * <;> omega
49
theorem value_decrement {bs : List Bool} (h : 0 < value bs) :
    value (ripple false bs) + 1 = value bs := by
proof · 10 lines
  induction bs with
  | nil => simp at h
  | cons b bs ih =>
      cases b with
      | false =>
          have hb : 0 < value bs := by simpa using h
          have hi := ih hb
          simp [ripple, value] at *
          omega
      | true => simp [ripple, value]; omega
62
theorem ripple_length_le (carry : Bool) (bs : List Bool) :
    (ripple carry bs).length ≤ bs.length + 1 := by
proof · 5 lines
  induction bs with
  | nil => simp [ripple]
  | cons b bs ih =>
      simp only [ripple, List.length_cons]
      split_ifs <;> simp_all only [List.length_cons] <;> omega
70
theorem decrement_length {bs : List Bool} (h : 0 < value bs) :
    (ripple false bs).length = bs.length := by
proof · 8 lines
  induction bs with
  | nil => simp at h
  | cons b bs ih =>
      cases b with
      | false =>
          have hb : 0 < value bs := by simpa using h
          simp [ripple, ih hb]
      | true => simp [ripple]

A direct finite Boolean zero test, valid even for noncanonical encodings.

82
def isZero : List Bool → Bool
body · 2 lines
  | [] => true
  | b :: bs => (!b) && isZero bs
86
@[simp] theorem isZero_eq_true_iff (bs : List Bool) : isZero bs = true ↔ value bs = 0 := by
proof · 3 lines
  induction bs with
  | nil => simp [isZero]
  | cons b bs ih => cases b <;> simp [isZero, value, ih]
91
theorem isZero_eq_false_iff (bs : List Bool) : isZero bs = false ↔ 0 < value bs := by
proof · 4 lines
  rw [Bool.eq_false_iff]
  change ¬ (isZero bs = true) ↔ _
  rw [isZero_eq_true_iff]
  exact Nat.pos_iff_ne_zero.symm

Three fixed Boolean registers: current bit, presence bit, and an accumulator.

101
def counterRegs (b p z : Bool) : Fin 3 → Bool :=
body · 1 line
  fun i => if i = 0 then b else if i = 1 then p else z

The value stack and one scratch stack; neither stack is part of finite control.

105
def counterStacks (xs ys : List Bool) : Fin 2 → List Bool :=
body · 1 line
  fun i => if i = 0 then xs else ys
108
@[simp] theorem counterRegs_zero (b p z : Bool) : counterRegs b p z 0 = b := rfl
109
@[simp] theorem counterRegs_one (b p z : Bool) : counterRegs b p z 1 = p := rfl
110
@[simp] theorem counterRegs_two (b p z : Bool) : counterRegs b p z 2 = z := rfl
111
@[simp] theorem counterStacks_zero (xs ys : List Bool) : counterStacks xs ys 0 = xs := rfl
112
@[simp] theorem counterStacks_one (xs ys : List Bool) : counterStacks xs ys 1 = ys := rfl
114
@[simp] theorem update_counterRegs_zero (b p z v : Bool) :
    Function.update (counterRegs b p z) 0 v = counterRegs v p z := by
proof · 2 lines
  funext i
  fin_cases i <;> simp [counterRegs]
119
@[simp] theorem update_counterRegs_one (b p z v : Bool) :
    Function.update (counterRegs b p z) 1 v = counterRegs b v z := by
proof · 2 lines
  funext i
  fin_cases i <;> simp [counterRegs]
124
@[simp] theorem update_counterRegs_two (b p z v : Bool) :
    Function.update (counterRegs b p z) 2 v = counterRegs b p v := by
proof · 2 lines
  funext i
  fin_cases i <;> simp [counterRegs]
129
@[simp] theorem update_counterStacks_zero (xs ys vs : List Bool) :
    Function.update (counterStacks xs ys) 0 vs = counterStacks vs ys := by
proof · 2 lines
  funext i
  fin_cases i <;> simp [counterStacks]
134
@[simp] theorem update_counterStacks_one (xs ys vs : List Bool) :
    Function.update (counterStacks xs ys) 1 vs = counterStacks xs vs := by
proof · 2 lines
  funext i
  fin_cases i <;> simp [counterStacks]
139
def counterCfg {l : ℕ} (q : Fin l) (b p z : Bool) (xs ys : List Bool) : Cfg 3 2 l :=
body · 1 line
  ⟨some q, counterRegs b p z, counterStacks xs ys⟩

Eleven fixed labels implement carry/borrow and scratch restoration. The carry parameter is a fixed program bit, not an unbounded control value.

144
abbrev rippleProgram (carry : Bool) : Program 3 2 11 where
body · 15 lines
  inputStack := 0
  outputStack := 0
  entry := 0
  code q := match q.val with
    | 0 => .pop 0 0 1 1
    | 1 => .branch 1 2 5
    | 2 => if carry then .branch 0 3 5 else .branch 0 5 3
    | 3 => .bnot 0 0 4
    | 4 => .push 1 0 0
    | 5 => .set 0 carry 6
    | 6 => .push 0 0 7
    | 7 => .pop 1 0 1 8
    | 8 => .branch 1 9 10
    | 9 => .push 0 0 7
    | _ => .halt
161
@[simp] theorem ripple_step0_nil (c b p z : Bool) (ys : List Bool) :
    step (rippleProgram c) (counterCfg 0 b p z [] ys) =
      some (counterCfg 1 false false z [] ys) := by
proof · 1 line
  simp [step, counterCfg, execInstr, readRegs]
166
@[simp] theorem ripple_step0_cons (c b p z a : Bool) (xs ys : List Bool) :
    step (rippleProgram c) (counterCfg 0 b p z (a :: xs) ys) =
      some (counterCfg 1 a true z xs ys) := by
proof · 1 line
  simp [step, counterCfg, execInstr, readRegs]
171
@[simp] theorem ripple_step1 (c b p z : Bool) (xs ys : List Bool) :
    step (rippleProgram c) (counterCfg 1 b p z xs ys) =
      some (counterCfg (if p then 2 else 5) b p z xs ys) := by
proof · 2 lines
  simp [step, counterCfg, execInstr]
  rfl
177
@[simp] theorem ripple_step2 (c b p z : Bool) (xs ys : List Bool) :
    step (rippleProgram c) (counterCfg 2 b p z xs ys) =
      some (counterCfg (if b = c then 3 else 5) b p z xs ys) := by
proof · 1 line
  cases c <;> cases b <;> simp [step, counterCfg, execInstr]
182
@[simp] theorem ripple_step3 (c b p z : Bool) (xs ys : List Bool) :
    step (rippleProgram c) (counterCfg 3 b p z xs ys) =
      some (counterCfg 4 (!b) p z xs ys) := by
proof · 1 line
  simp [step, counterCfg, execInstr]
187
@[simp] theorem ripple_step4 (c b p z : Bool) (xs ys : List Bool) :
    step (rippleProgram c) (counterCfg 4 b p z xs ys) =
      some (counterCfg 0 b p z xs (b :: ys)) := by
proof · 1 line
  simp [step, counterCfg, execInstr]
192
@[simp] theorem ripple_step5 (c b p z : Bool) (xs ys : List Bool) :
    step (rippleProgram c) (counterCfg 5 b p z xs ys) =
      some (counterCfg 6 c p z xs ys) := by
proof · 1 line
  simp [step, counterCfg, execInstr]
197
@[simp] theorem ripple_step6 (c b p z : Bool) (xs ys : List Bool) :
    step (rippleProgram c) (counterCfg 6 b p z xs ys) =
      some (counterCfg 7 b p z (b :: xs) ys) := by
proof · 1 line
  simp [step, counterCfg, execInstr]
202
@[simp] theorem ripple_step7_nil (c b p z : Bool) (xs : List Bool) :
    step (rippleProgram c) (counterCfg 7 b p z xs []) =
      some (counterCfg 8 false false z xs []) := by
proof · 1 line
  simp [step, counterCfg, execInstr, readRegs]
207
@[simp] theorem ripple_step7_cons (c b p z a : Bool) (xs ys : List Bool) :
    step (rippleProgram c) (counterCfg 7 b p z xs (a :: ys)) =
      some (counterCfg 8 a true z xs ys) := by
proof · 1 line
  simp [step, counterCfg, execInstr, readRegs]
212
@[simp] theorem ripple_step8 (c b p z : Bool) (xs ys : List Bool) :
    step (rippleProgram c) (counterCfg 8 b p z xs ys) =
      some (counterCfg (if p then 9 else 10) b p z xs ys) := by
proof · 2 lines
  simp [step, counterCfg, execInstr]
  rfl
218
@[simp] theorem ripple_step9 (c b p z : Bool) (xs ys : List Bool) :
    step (rippleProgram c) (counterCfg 9 b p z xs ys) =
      some (counterCfg 7 b p z (b :: xs) ys) := by
proof · 1 line
  simp [step, counterCfg, execInstr]

Exact instruction count of the complete scratch-restoration loop.

224
theorem ripple_restore_trace (c b p z : Bool) (xs ys : List Bool) :
    Trace (rippleProgram c) (3 * ys.length + 2)
      (counterCfg 7 b p z xs ys)
      (counterCfg 10 false false z (ys.reverse ++ xs) []) := by
proof · 11 lines
  induction ys generalizing b p xs with
  | nil =>
      simpa using SubexponentialSAT.Computation.Trace.cons (ripple_step7_nil c b p z xs)
        (SubexponentialSAT.Computation.Trace.cons (ripple_step8 c false false z xs [])
          (SubexponentialSAT.Computation.Trace.refl (counterCfg 10 false false z xs [])))
  | cons a ys ih =>
      have h := SubexponentialSAT.Computation.Trace.cons (ripple_step7_cons c b p z a xs ys)
        (SubexponentialSAT.Computation.Trace.cons (ripple_step8 c a true z xs ys)
          (SubexponentialSAT.Computation.Trace.cons (ripple_step9 c a true z xs ys) (ih a true (a :: xs))))
      convert h using 1
      simp [ List.reverse_cons, List.append_assoc]

Exact instruction count, ending at the halt label before executing halt.

241
def rippleCost (c : Bool) : List Bool → ℕ
body · 2 lines
  | [] => 6
  | b :: bs => if b = c then rippleCost c bs + 8 else 7

Complete program correctness, allowing arbitrary initial scratch contents. The old scratch contents are restored to the front of the result in reverse.

247
theorem ripple_trace (c b p z : Bool) (xs ys : List Bool) :
    Trace (rippleProgram c) (rippleCost c xs + 3 * ys.length)
      (counterCfg 0 b p z xs ys)
      (counterCfg 10 false false z (ys.reverse ++ ripple c xs) []) := by
proof · 31 lines
  induction xs generalizing b p ys with
  | nil =>
      have h := SubexponentialSAT.Computation.Trace.cons (ripple_step0_nil c b p z ys)
        (SubexponentialSAT.Computation.Trace.cons (ripple_step1 c false false z [] ys)
          (SubexponentialSAT.Computation.Trace.cons (ripple_step5 c false false z [] ys)
            (SubexponentialSAT.Computation.Trace.cons (ripple_step6 c c false z [] ys)
              (ripple_restore_trace c c false z [c] ys))))
      convert h using 1
      · simp [rippleCost ]
        omega
      · simp [ ripple]
  | cons a xs ih =>
      by_cases ha : a = c
      · subst a
        have h := SubexponentialSAT.Computation.Trace.cons (ripple_step0_cons c b p z c xs ys)
          (SubexponentialSAT.Computation.Trace.cons (ripple_step1 c c true z xs ys)
            (SubexponentialSAT.Computation.Trace.cons (by simp)
              (SubexponentialSAT.Computation.Trace.cons (ripple_step3 c c true z xs ys)
                (SubexponentialSAT.Computation.Trace.cons (ripple_step4 c (!c) true z xs ys)
                  (ih (!c) true ((!c) :: ys))))))
        convert h using 1 <;>
          simp [rippleCost, ripple, List.length_cons, List.reverse_cons, List.append_assoc]
        omega
      · have h := SubexponentialSAT.Computation.Trace.cons (ripple_step0_cons c b p z a xs ys)
          (SubexponentialSAT.Computation.Trace.cons (ripple_step1 c a true z xs ys)
            (SubexponentialSAT.Computation.Trace.cons (by simp [ha])
              (SubexponentialSAT.Computation.Trace.cons (ripple_step5 c a true z xs ys)
                (SubexponentialSAT.Computation.Trace.cons (ripple_step6 c c true z xs ys)
                  (ripple_restore_trace c c true z (c :: xs) ys)))))
        convert h using 1 <;> simp [rippleCost, ripple, ha]
        omega
283
theorem rippleCost_le (c : Bool) (xs : List Bool) :
    rippleCost c xs ≤ 8 * xs.length + 7 := by
proof · 5 lines
  induction xs with
  | nil => simp [rippleCost]
  | cons a xs ih =>
      simp only [rippleCost, List.length_cons]
      split_ifs <;> omega

Increment has an actual finite-table trace and a linear instruction bound.

292
theorem increment_runs (xs : List Bool) :
    RunsIn (rippleProgram true) (8 * xs.length + 7)
      (counterCfg 0 false false false xs [])
      (counterCfg 10 false false false (ripple true xs) []) := by
proof · 2 lines
  refine ⟨rippleCost true xs, rippleCost_le true xs, ?_⟩
  simpa using ripple_trace true false false false xs []

Decrement executes the same fixed-size table; the positivity precondition is needed only for its arithmetic interpretation.

301
theorem decrement_runs (xs : List Bool) :
    RunsIn (rippleProgram false) (8 * xs.length + 7)
      (counterCfg 0 false false false xs [])
      (counterCfg 10 false false false (ripple false xs) []) := by
proof · 2 lines
  refine ⟨rippleCost false xs, rippleCost_le false xs, ?_⟩
  simpa using ripple_trace false false false false xs []
309
@[simp] theorem initial_ripple_eq (c : Bool) (xs : List Bool) :
    initial (rippleProgram c) xs = counterCfg 0 false false false xs [] := by
proof · 5 lines
  simp [initial, counterCfg ]
  constructor
  · funext i
    simp [counterRegs]
  · rfl
317
theorem ripple_halt_step (c : Bool) (xs : List Bool) :
    step (rippleProgram c) (counterCfg 10 false false false xs []) =
      some (terminal (rippleProgram c) xs) := by
proof · 5 lines
  simp [step, counterCfg, execInstr, terminal, counterRegs, counterStacks]
  constructor
  · funext i
    simp [counterRegs]
  · rfl

Complete execution from the language's actual input convention through halt.

327
theorem ripple_terminal_trace (c : Bool) (xs : List Bool) :
    Trace (rippleProgram c) (rippleCost c xs + 1)
      (initial (rippleProgram c) xs) (terminal (rippleProgram c) (ripple c xs)) := by
proof · 9 lines
  have hbase : Trace (rippleProgram c) (rippleCost c xs)
      (counterCfg 0 false false false xs [])
      (counterCfg 10 false false false (ripple c xs) []) := by
    simpa using ripple_trace c false false false xs []
  have hhalt : Trace (rippleProgram c) 1
      (counterCfg 10 false false false (ripple c xs) [])
      (terminal (rippleProgram c) (ripple c xs)) :=
    .cons (ripple_halt_step c (ripple c xs)) (.refl _)
  simpa only [initial_ripple_eq] using hbase.trans hhalt
340
theorem ripple_terminal_runs (c : Bool) (xs : List Bool) :
    RunsIn (rippleProgram c) (8 * xs.length + 8)
      (initial (rippleProgram c) xs) (terminal (rippleProgram c) (ripple c xs)) := by
proof · 2 lines
  exact ⟨rippleCost c xs + 1, by have h := rippleCost_le c xs; omega,
    ripple_terminal_trace c xs⟩
346
theorem increment_correct (xs : List Bool) :
    ∃ output : List Bool, value output = value xs + 1 ∧
      RunsIn (rippleProgram true) (8 * xs.length + 8)
        (initial (rippleProgram true) xs) (terminal (rippleProgram true) output) :=
proof · 1 line
  ⟨ripple true xs, value_increment xs, ripple_terminal_runs true xs⟩
352
theorem decrement_correct {xs : List Bool} (h : 0 < value xs) :
    ∃ output : List Bool, value output + 1 = value xs ∧ output.length = xs.length ∧
      RunsIn (rippleProgram false) (8 * xs.length + 8)
        (initial (rippleProgram false) xs) (terminal (rippleProgram false) output) :=
proof · 1 line
  ⟨ripple false xs, value_decrement h, decrement_length h, ripple_terminal_runs false xs⟩

A ten-label zero test. It preserves the data stack, clears scratch, and returns its answer in Boolean register 2.

360
abbrev zeroProgram : Program 3 2 10 where
body · 14 lines
  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 1 3 6
    | 3 => .push 1 0 4
    | 4 => .bnot 0 0 5
    | 5 => .band 2 2 0 1
    | 6 => .pop 1 0 1 7
    | 7 => .branch 1 8 9
    | 8 => .push 0 0 6
    | _ => .halt
376
@[simp] theorem zero_step0 (b p z : Bool) (xs ys : List Bool) :
    step zeroProgram (counterCfg 0 b p z xs ys) =
      some (counterCfg 1 b p true xs ys) := by
proof · 1 line
  simp [step, counterCfg, execInstr]
381
@[simp] theorem zero_step1_nil (b p z : Bool) (ys : List Bool) :
    step zeroProgram (counterCfg 1 b p z [] ys) =
      some (counterCfg 2 false false z [] ys) := by
proof · 1 line
  simp [step, counterCfg, execInstr, readRegs]
386
@[simp] theorem zero_step1_cons (b p z a : Bool) (xs ys : List Bool) :
    step zeroProgram (counterCfg 1 b p z (a :: xs) ys) =
      some (counterCfg 2 a true z xs ys) := by
proof · 1 line
  simp [step, counterCfg, execInstr, readRegs]
391
@[simp] theorem zero_step2 (b p z : Bool) (xs ys : List Bool) :
    step zeroProgram (counterCfg 2 b p z xs ys) =
      some (counterCfg (if p then 3 else 6) b p z xs ys) := by
proof · 2 lines
  simp [step, counterCfg, execInstr]
  rfl
397
@[simp] theorem zero_step3 (b p z : Bool) (xs ys : List Bool) :
    step zeroProgram (counterCfg 3 b p z xs ys) =
      some (counterCfg 4 b p z xs (b :: ys)) := by
proof · 1 line
  simp [step, counterCfg, execInstr]
402
@[simp] theorem zero_step4 (b p z : Bool) (xs ys : List Bool) :
    step zeroProgram (counterCfg 4 b p z xs ys) =
      some (counterCfg 5 (!b) p z xs ys) := by
proof · 1 line
  simp [step, counterCfg, execInstr]
407
@[simp] theorem zero_step5 (b p z : Bool) (xs ys : List Bool) :
    step zeroProgram (counterCfg 5 b p z xs ys) =
      some (counterCfg 1 b p (z && b) xs ys) := by
proof · 1 line
  simp [step, counterCfg, execInstr]
412
@[simp] theorem zero_step6_nil (b p z : Bool) (xs : List Bool) :
    step zeroProgram (counterCfg 6 b p z xs []) =
      some (counterCfg 7 false false z xs []) := by
proof · 1 line
  simp [step, counterCfg, execInstr, readRegs]
417
@[simp] theorem zero_step6_cons (b p z a : Bool) (xs ys : List Bool) :
    step zeroProgram (counterCfg 6 b p z xs (a :: ys)) =
      some (counterCfg 7 a true z xs ys) := by
proof · 1 line
  simp [step, counterCfg, execInstr, readRegs]
422
@[simp] theorem zero_step7 (b p z : Bool) (xs ys : List Bool) :
    step zeroProgram (counterCfg 7 b p z xs ys) =
      some (counterCfg (if p then 8 else 9) b p z xs ys) := by
proof · 2 lines
  simp [step, counterCfg, execInstr]
  rfl
428
@[simp] theorem zero_step8 (b p z : Bool) (xs ys : List Bool) :
    step zeroProgram (counterCfg 8 b p z xs ys) =
      some (counterCfg 6 b p z (b :: xs) ys) := by
proof · 1 line
  simp [step, counterCfg, execInstr]
433
theorem zero_restore_trace (b p z : Bool) (xs ys : List Bool) :
    Trace zeroProgram (3 * ys.length + 2)
      (counterCfg 6 b p z xs ys)
      (counterCfg 9 false false z (ys.reverse ++ xs) []) := by
proof · 11 lines
  induction ys generalizing b p xs with
  | nil =>
      simpa using SubexponentialSAT.Computation.Trace.cons (zero_step6_nil b p z xs)
        (SubexponentialSAT.Computation.Trace.cons (zero_step7 false false z xs [])
          (SubexponentialSAT.Computation.Trace.refl (counterCfg 9 false false z xs [])))
  | cons a ys ih =>
      have h := SubexponentialSAT.Computation.Trace.cons (zero_step6_cons b p z a xs ys)
        (SubexponentialSAT.Computation.Trace.cons (zero_step7 a true z xs ys)
          (SubexponentialSAT.Computation.Trace.cons (zero_step8 a true z xs ys) (ih a true (a :: xs))))
      convert h using 1
      simp [ List.reverse_cons, List.append_assoc]

Exact trace through scan and restore, preserving every scanned data bit.

450
theorem zero_scan_trace (b p z : Bool) (xs ys : List Bool) :
    Trace zeroProgram (8 * xs.length + 3 * ys.length + 4)
      (counterCfg 1 b p z xs ys)
      (counterCfg 9 false false (z && isZero xs) (ys.reverse ++ xs) []) := by
proof · 15 lines
  induction xs generalizing b p z ys with
  | nil =>
      have h := SubexponentialSAT.Computation.Trace.cons (zero_step1_nil b p z ys)
        (SubexponentialSAT.Computation.Trace.cons (zero_step2 false false z [] ys) (zero_restore_trace false false z [] ys))
      convert h using 1 <;> simp [isZero]
  | cons a xs ih =>
      have h := SubexponentialSAT.Computation.Trace.cons (zero_step1_cons b p z a xs ys)
        (SubexponentialSAT.Computation.Trace.cons (zero_step2 a true z xs ys)
          (SubexponentialSAT.Computation.Trace.cons (zero_step3 a true z xs ys)
            (SubexponentialSAT.Computation.Trace.cons (zero_step4 a true z xs (a :: ys))
              (SubexponentialSAT.Computation.Trace.cons (zero_step5 (!a) true z xs (a :: ys))
                (ih (!a) true (z && !a) (a :: ys))))))
      convert h using 1 <;>
        simp [isZero, Bool.and_assoc, List.length_cons, List.reverse_cons, List.append_assoc]
      omega

The zero-test answer, counter preservation, scratch clearing, and cost are all witnessed by a trace of the executable finite instruction table.

472
theorem zero_trace (xs : List Bool) :
    Trace zeroProgram (8 * xs.length + 5)
      (counterCfg 0 false false false xs [])
      (counterCfg 9 false false (isZero xs) xs []) := by
proof · 3 lines
  have h := SubexponentialSAT.Computation.Trace.cons (zero_step0 false false false xs [])
    (zero_scan_trace false false true xs [])
  simpa [Nat.add_assoc] using h
481
@[simp] theorem initial_zero_eq (xs : List Bool) :
    initial zeroProgram xs = counterCfg 0 false false false xs [] := by
proof · 5 lines
  simp [initial, counterCfg ]
  constructor
  · funext i
    simp [counterRegs]
  · rfl
489
theorem zero_initial_trace (xs : List Bool) :
    Trace zeroProgram (8 * xs.length + 5) (initial zeroProgram xs)
      (counterCfg 9 false false (isZero xs) xs []) := by
proof · 1 line
  simpa only [initial_zero_eq] using zero_trace xs

Specialization of the shared linker to identical data ports.

495
abbrev relabelInstr {r s l m : ℕ} (f : Fin l → Fin m) : Instr r s l → Instr r s m :=
body · 1 line
  ProgramLinking.relocateInstr id id f none
498
def relabelCfg {r s l m : ℕ} (f : Fin l → Fin m) (c : Cfg r s l) : Cfg r s m :=
body · 1 line
  ⟨c.pc.map f, c.regs, c.stacks⟩
501
@[simp] theorem embedCfg_id {r s l m : ℕ} (f : Fin l → Fin m)
    (frame : Cfg r s m) (c : Cfg r s l) :
    ProgramLinking.embedCfg id id f none frame c = relabelCfg f c := by
proof · 2 lines
  rcases c with ⟨pc, regs, stk⟩
  cases pc <;> simp [ProgramLinking.embedCfg, ProgramLinking.relocatePc, relabelCfg]

The actual shared linker transfers the trace ending before the exit instruction.

508
theorem relabel_trace_to_exit {r s l m : ℕ} {p : Program r s l} {q : Program r s m}
    (f : Fin l → Fin m) (exit : Fin l) (hhalt : p.code exit = .halt)
    (hcode : ∀ i, i ≠ exit → q.code (f i) = relabelInstr f (p.code i))
    {n : ℕ} {a b : Cfg r s l} (ht : Trace p n a b) (hb : b.pc = some exit) :
    Trace q n (relabelCfg f a) (relabelCfg f b) := by
proof · 3 lines
  have h := ProgramLinking.relocate_trace_to_exit p q id id f none (relabelCfg f a)
    Function.injective_id Function.injective_id exit hhalt hcode ht hb
  simpa only [embedCfg_id] using h

A bounded countdown loop. Labels 0--9 test the counter, 10--19 decrement it, label 20 is the iteration boundary, and label 21 is the exit.

519
abbrev countdownProgram : Program 3 2 22 where
body · 26 lines
  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 1 3 6
    | 3 => .push 1 0 4
    | 4 => .bnot 0 0 5
    | 5 => .band 2 2 0 1
    | 6 => .pop 1 0 1 7
    | 7 => .branch 1 8 9
    | 8 => .push 0 0 6
    | 9 => .branch 2 21 10
    | 10 => .pop 0 0 1 11
    | 11 => .branch 1 12 15
    | 12 => .branch 0 15 13
    | 13 => .bnot 0 0 14
    | 14 => .push 1 0 10
    | 15 => .set 0 false 16
    | 16 => .push 0 0 17
    | 17 => .pop 1 0 1 18
    | 18 => .branch 1 19 20
    | 19 => .push 0 0 17
    | 20 => .goto 0
    | _ => .halt
547
def zeroLabel (i : Fin 10) : Fin 22 := ⟨i.val, by omega⟩
548
def decrementLabel (i : Fin 11) : Fin 22 := ⟨10 + i.val, by omega⟩
550
theorem countdown_zero_code (i : Fin 10) (hi : i ≠ 9) :
    countdownProgram.code (zeroLabel i) = relabelInstr zeroLabel (zeroProgram.code i) := by
proof · 1 line
  fin_cases i <;> simp_all [ zeroLabel, relabelInstr, ProgramLinking.relocateInstr]
554
theorem countdown_decrement_code (i : Fin 11) (hi : i ≠ 10) :
    countdownProgram.code (decrementLabel i) =
      relabelInstr decrementLabel ((rippleProgram false).code i) := by
proof · 1 line
  fin_cases i <;> simp_all [ decrementLabel, relabelInstr, ProgramLinking.relocateInstr]
559
theorem countdown_zero_trace (xs : List Bool) :
    Trace countdownProgram (8 * xs.length + 5)
      (counterCfg 0 false false false xs [])
      (counterCfg 9 false false (isZero xs) xs []) := by
proof · 3 lines
  have h := relabel_trace_to_exit zeroLabel 9 (by rfl)
    countdown_zero_code (zero_trace xs) rfl
  simpa [relabelCfg, counterCfg, zeroLabel] using h
567
theorem countdown_decrement_trace (xs : List Bool) :
    Trace countdownProgram (rippleCost false xs)
      (counterCfg 10 false false false xs [])
      (counterCfg 20 false false false (ripple false xs) []) := by
proof · 3 lines
  have h := relabel_trace_to_exit decrementLabel 10 (by rfl)
    countdown_decrement_code (ripple_trace false false false false xs []) rfl
  simpa [relabelCfg, counterCfg, decrementLabel] using h
575
@[simp] theorem countdown_step9 (z : Bool) (xs : List Bool) :
    step countdownProgram (counterCfg 9 false false z xs []) =
      some (counterCfg (if z then 21 else 10) false false z xs []) := by
proof · 2 lines
  simp [step, counterCfg, execInstr]
  rfl
581
@[simp] theorem countdown_step20 (xs : List Bool) :
    step countdownProgram (counterCfg 20 false false false xs []) =
      some (counterCfg 0 false false false xs []) := by
proof · 1 line
  simp [step, counterCfg, execInstr]

One positive iteration is a measured trace of the combined table, and strictly decreases the binary counter while preserving its width.

588
theorem countdown_iteration {xs : List Bool} (hx : 0 < value xs) :
    RunsIn countdownProgram (16 * xs.length + 14)
      (counterCfg 0 false false false xs [])
      (counterCfg 0 false false false (ripple false xs) []) := by
proof · 19 lines
  have hz : isZero xs = false := (isZero_eq_false_iff xs).mpr hx
  have hzrun : RunsIn countdownProgram (8 * xs.length + 5)
      (counterCfg 0 false false false xs [])
      (counterCfg 9 false false false xs []) :=
    ⟨_, le_rfl, by simpa [hz] using countdown_zero_trace xs⟩
  have hbranch : RunsIn countdownProgram 1
      (counterCfg 9 false false false xs [])
      (counterCfg 10 false false false xs []) :=
    ⟨1, le_rfl, .cons (by simp) (.refl _)⟩
  have hdec : RunsIn countdownProgram (8 * xs.length + 7)
      (counterCfg 10 false false false xs [])
      (counterCfg 20 false false false (ripple false xs) []) :=
    ⟨_, rippleCost_le false xs, countdown_decrement_trace xs⟩
  have hreturn : RunsIn countdownProgram 1
      (counterCfg 20 false false false (ripple false xs) [])
      (counterCfg 0 false false false (ripple false xs) []) :=
    ⟨1, le_rfl, .cons (by simp) (.refl _)⟩
  convert ((hzrun.trans hbranch).trans hdec).trans hreturn using 1
  omega

The bounded loop really terminates after repeatedly decrementing its input counter. Its runtime is polynomial in the binary width and numeric loop bound; the bound itself is data on the stack, never a program label or register.

615
theorem countdown_runs (xs : List Bool) :
    ∃ ys : List Bool, value ys = 0 ∧ ys.length = xs.length ∧
      RunsIn countdownProgram ((value xs + 1) * (16 * xs.length + 14))
        (counterCfg 0 false false false xs [])
        (counterCfg 21 false false true ys []) := by
proof · 35 lines
  have main : ∀ n : ℕ, ∀ xs : List Bool, value xs = n →
      ∃ ys : List Bool, value ys = 0 ∧ ys.length = xs.length ∧
        RunsIn countdownProgram ((n + 1) * (16 * xs.length + 14))
          (counterCfg 0 false false false xs [])
          (counterCfg 21 false false true ys []) := by
    intro n
    induction n using Nat.strong_induction_on with
    | h n ih =>
        intro xs hv
        by_cases hn : n = 0
        · have hzero : value xs = 0 := hv.trans hn
          have hz : isZero xs = true := (isZero_eq_true_iff xs).mpr hzero
          have htest : Trace countdownProgram (8 * xs.length + 5)
              (counterCfg 0 false false false xs [])
              (counterCfg 9 false false true xs []) := by
            simpa [hz] using countdown_zero_trace xs
          have hexit : Trace countdownProgram 1
              (counterCfg 9 false false true xs [])
              (counterCfg 21 false false true xs []) := .cons (by simp) (.refl _)
          refine ⟨xs, hzero, rfl, ?_⟩
          exact ⟨_, by simp only [hn, zero_add, one_mul]; omega, htest.trans hexit⟩
        · have hpos : 0 < value xs := by omega
          have hdec := value_decrement hpos
          have hlen := decrement_length hpos
          have hlt : value (ripple false xs) < n := by omega
          obtain ⟨ys, hyzero, hylen, hyrun⟩ :=
            ih (value (ripple false xs)) hlt (ripple false xs) rfl
          have hprefix := countdown_iteration hpos
          rw [hlen] at hyrun
          have hdec' : value (ripple false xs) + 1 = n := by omega
          rw [hdec'] at hyrun
          refine ⟨ys, hyzero, hylen.trans hlen, ?_⟩
          convert hprefix.trans hyrun using 1
          ring
  exact main (value xs) xs rfl

Reusable bounded-counter loop rule for an arbitrary actual finite program. The hypotheses are local measured executions (zero exit and one positive iteration), not a cost annotation for an uninterpreted mathematical function. The invariant may track any caller work stacks and registers.

661
theorem boundedCounterLoop {r s l : ℕ} (p : Program r s l) (counter : Fin s)
    (Invariant Post : Cfg r s l → Prop) (iterationCost exitCost : ℕ)
    (zeroExit : ∀ c, Invariant c → value (c.stacks counter) = 0 →
      ∃ d, Post d ∧ RunsIn p exitCost c d)
    (positiveIteration : ∀ c, Invariant c → 0 < value (c.stacks counter) →
      ∃ d, Invariant d ∧ value (d.stacks counter) + 1 = value (c.stacks counter) ∧
        RunsIn p iterationCost c d)
    {initialCfg : Cfg r s l} (hinit : Invariant initialCfg) :
    ∃ finalCfg, Post finalCfg ∧
      RunsIn p (value (initialCfg.stacks counter) * iterationCost + exitCost)
        initialCfg finalCfg := by
proof · 18 lines
  have main : ∀ n : ℕ, ∀ c : Cfg r s l, value (c.stacks counter) = n → Invariant c →
      ∃ d, Post d ∧ RunsIn p (n * iterationCost + exitCost) c d := by
    intro n
    induction n with
    | zero =>
        intro c hv hc
        obtain ⟨d, hd, hrun⟩ := zeroExit c hc hv
        exact ⟨d, hd, by simpa using hrun⟩
    | succ n ih =>
        intro c hv hc
        have hp : 0 < value (c.stacks counter) := by omega
        obtain ⟨c', hc', hvalue, hstep⟩ := positiveIteration c hc hp
        have hv' : value (c'.stacks counter) = n := by omega
        obtain ⟨d, hd, htail⟩ := ih c' hv' hc'
        refine ⟨d, hd, ?_⟩
        convert hstep.trans htail using 1
        ring
  exact main _ initialCfg rfl hinit

Input data, binary count, and increment scratch, all on Boolean stacks.

695
def lengthStacks (data count scratch : List Bool) : Fin 3 → List Bool :=
body · 1 line
  fun i => if i = 0 then data else if i = 1 then count else scratch
698
@[simp] theorem lengthStacks_zero (a b c : List Bool) : lengthStacks a b c 0 = a := rfl
699
@[simp] theorem lengthStacks_one (a b c : List Bool) : lengthStacks a b c 1 = b := rfl
700
@[simp] theorem lengthStacks_two (a b c : List Bool) : lengthStacks a b c 2 = c := rfl
702
@[simp] theorem update_lengthStacks_zero (a b c d : List Bool) :
    Function.update (lengthStacks a b c) 0 d = lengthStacks d b c := by
proof · 2 lines
  funext i
  fin_cases i <;> simp [lengthStacks]
707
def lengthCfg (q : Fin 14) (b p z : Bool) (data count scratch : List Bool) : Cfg 3 3 14 :=
body · 1 line
  ⟨some q, counterRegs b p z, lengthStacks data count scratch⟩

Count a list by consuming one bit and incrementing a binary stack counter. The program has fourteen fixed labels; the input length is never in control.

712
abbrev lengthProgram : Program 3 3 14 where
body · 18 lines
  inputStack := 0
  outputStack := 1
  entry := 0
  code q := match q.val with
    | 0 => .pop 0 0 1 1
    | 1 => .branch 1 2 13
    | 2 => .pop 1 0 1 3
    | 3 => .branch 1 4 7
    | 4 => .branch 0 5 7
    | 5 => .bnot 0 0 6
    | 6 => .push 2 0 2
    | 7 => .set 0 true 8
    | 8 => .push 1 0 9
    | 9 => .pop 2 0 1 10
    | 10 => .branch 1 11 12
    | 11 => .push 1 0 9
    | 12 => .goto 0
    | _ => .halt
732
def lengthLabel (i : Fin 11) : Fin 14 := ⟨2 + i.val, by omega⟩
734
theorem length_increment_code (i : Fin 11) (hi : i ≠ 10) :
    lengthProgram.code (lengthLabel i) =
      ProgramLinking.relocateInstr id Fin.succ lengthLabel none ((rippleProgram true).code i) := by
proof · 2 lines
  fin_cases i <;>
    simp_all [ lengthLabel, ProgramLinking.relocateInstr]
740
theorem extend_lengthStacks (data count scratch : List Bool) :
    Function.extend (Fin.succ : Fin 2 → Fin 3) (counterStacks count scratch)
      (lengthStacks data [] []) = lengthStacks data count scratch := by
proof · 9 lines
  funext i
  fin_cases i
  · have hn : ¬ ∃ j : Fin 2, j.succ = (0 : Fin 3) := by simp
    change Function.extend (Fin.succ : Fin 2 → Fin 3) (counterStacks count scratch)
      (lengthStacks data [] []) 0 = data
    rw [Function.extend_apply' _ _ _ hn]
    rfl
  · exact (Fin.succ_injective 2).extend_apply _ _ 0
  · exact (Fin.succ_injective 2).extend_apply _ _ 1
753
@[simp] theorem embed_lengthCfg (q : Fin 11) (b p z : Bool) (data count scratch : List Bool) :
    ProgramLinking.embedCfg id Fin.succ lengthLabel none
      (lengthCfg 0 false false false data [] []) (counterCfg q b p z count scratch) =
        lengthCfg (lengthLabel q) b p z data count scratch := by
proof · 2 lines
  simp [ProgramLinking.embedCfg, ProgramLinking.relocatePc, counterCfg, lengthCfg,
    extend_lengthStacks]
760
theorem length_increment_trace (b p : Bool) (data count : List Bool) :
    Trace lengthProgram (rippleCost true count)
      (lengthCfg 2 b p false data count [])
      (lengthCfg 12 false false false data (ripple true count) []) := by
proof · 5 lines
  have h := ProgramLinking.relocate_trace_to_exit (rippleProgram true) lengthProgram
    id Fin.succ lengthLabel none (lengthCfg 0 false false false data [] [])
    Function.injective_id (Fin.succ_injective 2) 10 rfl length_increment_code
    (ripple_trace true b p false count []) rfl
  simpa [lengthLabel] using h
770
@[simp] theorem length_step0_nil (b p : Bool) (count : List Bool) :
    step lengthProgram (lengthCfg 0 b p false [] count []) =
      some (lengthCfg 1 false false false [] count []) := by
proof · 1 line
  simp [step, lengthCfg, execInstr, readRegs]
775
@[simp] theorem length_step0_cons (b p a : Bool) (data count : List Bool) :
    step lengthProgram (lengthCfg 0 b p false (a :: data) count []) =
      some (lengthCfg 1 a true false data count []) := by
proof · 1 line
  simp [step, lengthCfg, execInstr, readRegs]
780
@[simp] theorem length_step1 (b p : Bool) (data count : List Bool) :
    step lengthProgram (lengthCfg 1 b p false data count []) =
      some (lengthCfg (if p then 2 else 13) b p false data count []) := by
proof · 2 lines
  simp [step, lengthCfg, execInstr]
  rfl
786
@[simp] theorem length_step12 (data count : List Bool) :
    step lengthProgram (lengthCfg 12 false false false data count []) =
      some (lengthCfg 0 false false false data count []) := by
proof · 1 line
  simp [step, lengthCfg, execInstr]
791
theorem length_iteration (b p a : Bool) (data count : List Bool) (B : ℕ)
    (hB : count.length ≤ B) :
    RunsIn lengthProgram (8 * B + 10)
      (lengthCfg 0 b p false (a :: data) count [])
      (lengthCfg 0 false false false data (ripple true count) []) := by
proof · 13 lines
  have hprefix : Trace lengthProgram 2
      (lengthCfg 0 b p false (a :: data) count [])
      (lengthCfg 2 a true false data count []) :=
    .cons (length_step0_cons b p a data count)
      (.cons (length_step1 a true data count) (.refl _))
  have hsuffix : Trace lengthProgram 1
      (lengthCfg 12 false false false data (ripple true count) [])
      (lengthCfg 0 false false false data (ripple true count) []) :=
    .cons (length_step12 data (ripple true count)) (.refl _)
  refine ⟨2 + rippleCost true count + 1, ?_,
    (hprefix.trans (length_increment_trace a true data count)).trans hsuffix⟩
  have hcost := rippleCost_le true count
  omega

General accumulating count: a pre-existing binary counter gains exactly one for each consumed input bit. B is a proved width bound, not machine state.

812
theorem length_accumulate (data count : List Bool) (B : ℕ)
    (hB : count.length + data.length ≤ B) (b p : Bool) :
    ∃ output : List Bool,
      value output = value count + data.length ∧ output.length ≤ count.length + data.length ∧
      RunsIn lengthProgram (data.length * (8 * B + 10) + 2)
        (lengthCfg 0 b p false data count [])
        (lengthCfg 13 false false false [] output []) := by
proof · 24 lines
  induction data generalizing count b p with
  | nil =>
      refine ⟨count, by simp, by simp, ?_⟩
      refine ⟨2, by simp, ?_⟩
      exact .cons (length_step0_nil b p count)
        (.cons (length_step1 false false [] count) (.refl _))
  | cons a data ih =>
      have hlen := ripple_length_le true count
      have hnext : (ripple true count).length + data.length ≤ B := by
        simp only [List.length_cons] at hB
        omega
      obtain ⟨output, hvalue, hsize, hrun⟩ :=
        ih (ripple true count) hnext false false
      have hcount : count.length ≤ B := by omega
      have hprefix := length_iteration b p a data count B hcount
      refine ⟨output, ?_, ?_, ?_⟩
      · rw [value_increment] at hvalue
        simp only [List.length_cons]
        omega
      · simp only [List.length_cons]
        omega
      · convert hprefix.trans hrun using 1
        simp only [List.length_cons]
        ring
844
theorem length_halt_step (output : List Bool) :
    step lengthProgram (lengthCfg 13 false false false [] output []) =
      some (terminal lengthProgram output) := by
proof · 6 lines
  simp [step, lengthCfg, execInstr, terminal]
  constructor
  · funext i
    simp [counterRegs]
  · funext i
    fin_cases i <;> simp [lengthStacks]

The utility accepted by callers whose current value/presence bits are dirty. Only the accumulator bit must start false; both live data bits are overwritten.

856
theorem length_correct_from (b p : Bool) (data : List Bool) :
    ∃ output : List Bool, value output = data.length ∧ output.length ≤ data.length ∧
      RunsIn lengthProgram (data.length * (8 * data.length + 10) + 3)
        (lengthCfg 0 b p false data [] []) (terminal lengthProgram output) := by
proof · 7 lines
  obtain ⟨output, hvalue, hsize, hrun⟩ :=
    length_accumulate data [] data.length (by simp) b p
  have hhalt : RunsIn lengthProgram 1
      (lengthCfg 13 false false false [] output []) (terminal lengthProgram output) :=
    ⟨1, le_rfl, .cons (length_halt_step output) (.refl _)⟩
  refine ⟨output, by simpa using hvalue, by simpa using hsize, ?_⟩
  convert hrun.trans hhalt using 1
868
@[simp] theorem initial_length_eq (data : List Bool) :
    initial lengthProgram data = lengthCfg 0 false false false data [] [] := by
proof · 6 lines
  simp [initial, lengthCfg]
  constructor
  · funext i
    simp [counterRegs]
  · funext i
    fin_cases i <;> simp [lengthStacks]

A fixed finite program produces the binary representation of the raw input length with a proved polynomial bound on actual primitive source instructions.

879
theorem length_correct (data : List Bool) :
    ∃ output : List Bool, value output = data.length ∧ output.length ≤ data.length ∧
      RunsIn lengthProgram (data.length * (8 * data.length + 10) + 3)
        (initial lengthProgram data) (terminal lengthProgram output) := by
proof · 1 line
  simpa only [initial_length_eq] using length_correct_from false false data

Xet Storage Details

Size:
39.4 kB
·
Xet hash:
cb383d45d280d798ca86feb998bff38208b0998530ecc9ab451fb1a9fd02a58d

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