cfahlgren1's picture
download
raw
3.82 kB
12
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.1
15
def normalize (l : List Bool) : List Bool :=
body · 1 line
  l.foldr (fun b acc => step (acc,b)) []
18
lemma normalize_cons (b : Bool) (l : List Bool) :
    normalize (b::l) = step (normalize l,b) := rfl
21
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]
25
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]
    omega
34
lemma 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]
38
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]; rfl
43
theorem 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]
65
theorem normalize_bits (n : ℕ) : normalize n.bits = n.bits := by
proof · 1 line
  rw [normalize_eq_bits, BitAdd.value_bits]
68
theorem normalize_idempotent (l : List Bool) : normalize (normalize l) = normalize l := by
proof · 1 line
  rw [normalize_eq_bits l, normalize_bits]
71
@[fun_prop] theorem step_poly : Poly step := by
proof · 2 lines
  unfold step
  fun_prop
75
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
83
@[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])
88
def add (z : List Bool × List Bool) : List Bool := normalize (BitAdd.add z)
90
@[fun_prop] theorem add_poly : Poly add := by unfold add; fun_prop
92
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]
96
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]
99
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]
102
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.2

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.