File size: 8,100 Bytes
e63eb7f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
import OAI.Combinatorics.EditApproximation.Binary.BinaryFiniteLoops
import OAI.Combinatorics.EditApproximation.Probability.FiniteEnumerationWork
import OAI.Combinatorics.EditApproximation.Numerics.RationalPrefix
import OAI.Combinatorics.EditApproximation.Definitions


namespace OAI

/-! Absolute differences used in the literal prefix and band tests.
The sign update retains the magnitude words; subtraction uses signed
Boolean arithmetic. -/

namespace EditApproximation
namespace BinaryFraction



@[simp] theorem absolute_value (a : BinaryFraction) : a.absolute.value = |a.value| := by
  have hn : |(a.numerator.value : ℚ)| = (bitWordValue a.numerator.bits : ℚ) := by
    cases h : a.numerator.negative <;> simp [SignedBinary.value, signedMagnitude, h]
  have hd : (0 : ℚ) < bitWordValue a.denominator := by exact_mod_cast a.denominator_pos
  change (bitWordValue a.numerator.bits : ℚ) / bitWordValue a.denominator =
    |(a.numerator.value : ℚ) / bitWordValue a.denominator|
  rw [abs_div, hn, abs_of_pos hd]

@[simp] theorem absolute_width (a : BinaryFraction) : a.absolute.width = a.width := rfl

@[simp] theorem nat_value (n : ℕ) : (nat n).value = (n : ℚ) := by
  rw [← representation_value, nat_representation, UnreducedRational.value_nat]

theorem canonicalAddWithWork_width (a b : BinaryFraction) (B : ℕ)
    (ha : a.width ≤ B) (hb : b.width ≤ B) :
    (a.canonicalAddWithWork b).1.width ≤ 8 * B + 6 := by
  have hs := UnreducedRational.storageBits_add a.representation b.representation
  have ha' := representation_storageBits_le a
  have hb' := representation_storageBits_le b
  have hw := canonicalAddWithWork_self_width a b
  rw [canonicalAddWithWork_representation] at hw
  omega

theorem subWithWork_width (a b : BinaryFraction) (B : ℕ)
    (ha : a.width ≤ B) (hb : b.width ≤ B) :
    (a.subWithWork b).1.width ≤ 8 * B + 6 := by
  exact canonicalAddWithWork_width a b.neg B ha (by simpa only [neg_width] using hb)



@[simp] theorem distanceWithWork_value (a b : BinaryFraction) :
    (distanceWithWork a b).1.value = |a.value - b.value| := by
  simp only [distanceWithWork, absolute_value, subWithWork_value]

theorem distanceWithWork_width (a b : BinaryFraction) (B : ℕ)
    (ha : a.width ≤ B) (hb : b.width ≤ B) :
    (distanceWithWork a b).1.width ≤ 8 * B + 6 :=
  subWithWork_width a b B ha hb

theorem distanceWithWork_cost (a b : BinaryFraction) (B : ℕ)
    (ha : a.width ≤ B) (hb : b.width ≤ B) :
    (distanceWithWork a b).2 ≤ 6000 * (B + 1) ^ 2 + 2 := by
  exact Nat.add_le_add_right (subWithWork_cost a b B ha hb) 1

/-- List of the literal endpoint differences. Natural endpoints are encoded
when encountered, and their bit-writing work is charged here. -/


@[simp] theorem connectionTermsWithWork_length {n : ℕ} (start finish : BinaryFraction)
    (states : List (TargetInterval n)) :
    (connectionTermsWithWork start states finish).1.length = states.length + 1 := by
  induction states generalizing start with
  | nil => rfl
  | cons state rest ih =>
    simp only [connectionTermsWithWork, List.length_cons, ih]

theorem connectionTermsWithWork_value {n : ℕ} (start finish : BinaryFraction)
    (states : List (TargetInterval n)) :
    ((connectionTermsWithWork start states finish).1.map value).sum =
      rationalConnectionBudget start.value states finish.value := by
  induction states generalizing start with
  | nil => simp only [connectionTermsWithWork, List.map_cons, List.map_nil,
      List.sum_cons, List.sum_nil, add_zero, distanceWithWork_value, rationalConnectionBudget]
  | cons state rest ih =>
    simp only [connectionTermsWithWork, List.map_cons, List.sum_cons,
      distanceWithWork_value, nat_value, ih, rationalConnectionBudget]

theorem connectionTermsWithWork_width {n : ℕ} (start finish : BinaryFraction)
    (states : List (TargetInterval n)) (B : ℕ)
    (hs : start.width ≤ B) (hf : finish.width ≤ B)
    (he : ∀ state ∈ states, Nat.size state.lo + 2 ≤ B ∧ Nat.size state.hi + 2 ≤ B) :
    ∀ a ∈ (connectionTermsWithWork start states finish).1, a.width ≤ 8 * B + 6 := by
  induction states generalizing start with
  | nil =>
    intro a ha
    simp only [connectionTermsWithWork, List.mem_singleton] at ha
    subst a
    exact distanceWithWork_width start finish B hs hf
  | cons state rest ih =>
    have hstate := he state List.mem_cons_self
    intro a ha
    simp only [connectionTermsWithWork, List.mem_cons] at ha
    rcases ha with rfl | ha
    · exact distanceWithWork_width start (nat state.lo) B hs
        ((nat_width_le state.lo).trans hstate.1)
    · exact ih (nat state.hi) ((nat_width_le state.hi).trans hstate.2)
        (fun s h => he s (List.mem_cons_of_mem _ h)) a ha

 def connectionTermsBitBudget (k B : ℕ) : ℕ :=
  (k + 1) * (6000 * (B + 1) ^ 2 + 4) + k * (2 * B + 6)

theorem connectionTermsWithWork_cost {n : ℕ} (start finish : BinaryFraction)
    (states : List (TargetInterval n)) (B : ℕ)
    (hs : start.width ≤ B) (hf : finish.width ≤ B)
    (he : ∀ state ∈ states, Nat.size state.lo + 2 ≤ B ∧ Nat.size state.hi + 2 ≤ B) :
    (connectionTermsWithWork start states finish).2 ≤ connectionTermsBitBudget states.length B := by
  induction states generalizing start with
  | nil =>
    have hc := distanceWithWork_cost start finish B hs hf
    change (distanceWithWork start finish).2 + 2 ≤ _
    simpa only [connectionTermsBitBudget, List.length_nil, zero_add, one_mul, zero_mul,
      add_zero] using Nat.add_le_add_right hc 2
  | cons state rest ih =>
    have hstate := he state List.mem_cons_self
    have hc := distanceWithWork_cost start (nat state.lo) B hs
      ((nat_width_le state.lo).trans hstate.1)
    have ht := ih (nat state.hi) ((nat_width_le state.hi).trans hstate.2)
      (fun s h => he s (List.mem_cons_of_mem _ h))
    change (distanceWithWork start (nat state.lo)).2 +
      (connectionTermsWithWork (nat state.hi) rest finish).2 + Nat.size state.lo +
        Nat.size state.hi + 6 ≤ _
    simp only [connectionTermsBitBudget, List.length_cons] at ht ⊢
    simp only [Nat.add_mul, Nat.one_mul] at ht ⊢
    omega



@[simp] theorem connectionWithWork_value {n : ℕ} (start finish : BinaryFraction)
    (states : List (TargetInterval n)) :
    (connectionWithWork start finish states).1.value =
      rationalConnectionBudget start.value states finish.value := by
  simp only [connectionWithWork, sumListWithWork_value, connectionTermsWithWork_value]

def connectionWordBudget (k B : ℕ) : ℕ := bandMassTotalStorageBudget (k + 1) (8 * B + 6)

def connectionBitBudget (k B : ℕ) : ℕ :=
  connectionTermsBitBudget k B + storedSumBitBudget (k + 1) (8 * B + 6) + 6 * (k + 1) + 1

theorem connectionWithWork_width {n : ℕ} (start finish : BinaryFraction)
    (states : List (TargetInterval n)) (B : ℕ)
    (hs : start.width ≤ B) (hf : finish.width ≤ B)
    (he : ∀ state ∈ states, Nat.size state.lo + 2 ≤ B ∧ Nat.size state.hi + 2 ≤ B) :
    (connectionWithWork start finish states).1.width ≤ connectionWordBudget states.length B := by
  have h := sumListWithWork_width (connectionTermsWithWork start states finish).1
    (8 * B + 6) (connectionTermsWithWork_width start finish states B hs hf he)
  simpa only [connectionWithWork, connectionTermsWithWork_length, connectionWordBudget] using h

theorem connectionWithWork_cost {n : ℕ} (start finish : BinaryFraction)
    (states : List (TargetInterval n)) (B : ℕ)
    (hs : start.width ≤ B) (hf : finish.width ≤ B)
    (he : ∀ state ∈ states, Nat.size state.lo + 2 ≤ B ∧ Nat.size state.hi + 2 ≤ B) :
    (connectionWithWork start finish states).2 ≤ connectionBitBudget states.length B := by
  have ht := connectionTermsWithWork_cost start finish states B hs hf he
  have hs' := sumListWithWork_cost (connectionTermsWithWork start states finish).1
    (8 * B + 6) (connectionTermsWithWork_width start finish states B hs hf he)
  simp only [connectionTermsWithWork_length] at hs'
  change _ + _ + 1 ≤ _
  unfold connectionBitBudget
  omega

end BinaryFraction
end EditApproximation

end OAI