Lean 4 · 19
declarations statementsfull
def step (z : List Bool × Bool) : List Bool := body · 1 line
if z.2 then true :: z.1 else if z.1.isEmpty then [] else false :: z.1def normalize (l : List Bool) : List Bool := body · 1 line
l.foldr (fun b acc => step (acc,b)) []lemma normalize_cons (b : Bool) (l : List Bool) :
normalize (b::l) = step (normalize l,b) := rfl lemma step_length (l : List Bool) (b : Bool) :
(step (l,b)).length ≤ l.length+1 := by proof · 1 line
cases b <;> cases l <;> simp [step]
theorem length_normalize (l : List Bool) : (normalize l).length ≤ l.length := by proof · 7 lines
induction l with
| nil => simp [normalize]
| cons b l ih =>
rw [normalize_cons]
have h := step_length (normalize l) b
simp only [List.length_cons]
omegalemma value_step (l : List Bool) (b : Bool) :
value (step (l,b)) = b.toNat + 2*value l := by proof · 1 line
cases b <;> cases l <;> simp [step, value]
theorem value_normalize (l : List Bool) : value (normalize l) = value l := by proof · 3 lines
induction l with
| nil => rfl
| cons b l ih => rw [normalize_cons, value_step, ih]; rfltheorem normalize_eq_bits (l : List Bool) : normalize l = (value l).bits := by proof · 20 lines
induction l with
| nil => simp [normalize, value]
| cons b l ih =>
rw [normalize_cons, ih]
by_cases hz : value l = 0
· cases b <;> simp [step, value, hz]
· have hb : value l = 0 → b = true := fun h => (hz h).elim
have hn : (value l).bits ≠ [] := by
intro h
have hv := BitAdd.value_bits (value l)
rw [h] at hv
exact hz hv.symm
have he : ((value l).bits).isEmpty = false := by
cases h : (value l).bits with
| nil => exact (hn h).elim
| cons a as => rfl
have hv : value (b::l) = Nat.bit b (value l) := by
cases b <;> simp [value, Nat.bit]; omega
rw [hv, Nat.bits_append_bit _ _ hb]
cases b <;> simp [step, he]theorem normalize_bits (n : ℕ) : normalize n.bits = n.bits := by proof · 1 line
rw [normalize_eq_bits, BitAdd.value_bits]theorem normalize_idempotent (l : List Bool) : normalize (normalize l) = normalize l := by proof · 1 line
rw [normalize_eq_bits l, normalize_bits]
@[fun_prop] theorem step_poly : Poly step := by proof · 2 lines
unfold step fun_prop
lemma step_growth (z : List Bool × Bool) :
(encode (step z)).length ≤ (encode z.1).length + 1*(2*(encode z.2).length+1) := by proof · 5 lines
rw [BitAdd.boolList_code_length, BitAdd.boolList_code_length]
simp only [encode_bool, List.length_singleton]
have h := step_length z.1 z.2
change (step z).length ≤ z.1.length + 1 at h
omega@[fun_prop] theorem normalize_poly : Poly normalize := by proof · 3 lines
have hf := Poly.listFoldAdd step step_poly 1 step_growth
have hh := Poly.comp _ (fun l : List Bool => (l.reverse, ([] : List Bool))) hf (by fun_prop)
exact hh.congr (fun l => by simp [normalize, List.foldl_reverse])def add (z : List Bool × List Bool) : List Bool := normalize (BitAdd.add z) @[fun_prop] theorem add_poly : Poly add := by unfold add; fun_prop theorem add_eq_bits (z : List Bool × List Bool) :
add z = (value z.1 + value z.2).bits := by proof · 1 line
rw [add, normalize_eq_bits, BitAdd.value_add]theorem value_add (z : List Bool × List Bool) : value (add z) = value z.1 + value z.2 := by proof · 1 line
rw [add, value_normalize, BitAdd.value_add]theorem add_bits (m n : ℕ) : add (m.bits,n.bits) = (m+n).bits := by proof · 1 line
rw [add_eq_bits, BitAdd.value_bits, BitAdd.value_bits]theorem length_add (z : List Bool × List Bool) :
(add z).length ≤ max z.1.length z.2.length + 1 := by proof · 2 lines
rw [add_eq_bits]
exact BitAdd.length_bits_value_add z.1 z.2Outline
- def
step12 - def
normalize15 - lemma
normalize_cons18 - lemma
step_length21 - theorem
length_normalize25 - lemma
value_step34 - theorem
value_normalize38 - theorem
normalize_eq_bits43 - theorem
normalize_bits65 - theorem
normalize_idempotent68 - theorem
step_poly71 - lemma
step_growth75 - theorem
normalize_poly83 - def
add88 - theorem
add_poly90 - theorem
add_eq_bits92 - theorem
value_add96 - theorem
add_bits99 - theorem
length_add102
Xet Storage Details
- Size:
- 3.82 kB
- Xet hash:
- d453c66dcad47cd4b0c4d7608fd806028cd1e0e186b41be323eba9bd611c1296
·
Xet efficiently stores files, intelligently splitting them into unique chunks and accelerating uploads and downloads. More info.