File size: 6,107 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
import OAI.Combinatorics.EditApproximation.Numerics.ScalarControlSpace
import OAI.Combinatorics.EditApproximation.Coarse.CoarseReadAccess
import OAI.Combinatorics.EditApproximation.Coarse.CoarseReadLeafWork

namespace OAI

namespace EditApproximation

def saturatingSubtractInvocations (a b : ℕ) : ℕ :=
  bitCompareInvocations a.bits b.bits + 1 +
    if (binaryNaturalCompareWithWork a b).1 = .lt then 0
    else bitSubtractInvocations a.bits b.bits false

theorem saturatingSubtractInvocations_le_work (a b : ℕ) :
    saturatingSubtractInvocations a b ≤ (saturatingSubtractWithWork a b).2 := by
  have hc := bitCompareInvocations_le_work a.bits b.bits
  have hs := bitSubtractInvocations_le_work a.bits b.bits false
  unfold saturatingSubtractInvocations saturatingSubtractWithWork
  dsimp only
  split_ifs <;> simp only [binaryNaturalSubtractWithWork, binaryNaturalCompareWithWork] at * <;> omega

def coarseDescriptorInvocations (B d n start : ℕ) : ℕ :=
  bitPowerInvocations B.bits d + saturatingSubtractInvocations n start +
    bitCompareInvocations (saturatingSubtractWithWork n start).1 (bitPowerWithWork B.bits d).1 + 3

theorem coarseDescriptorInvocations_le_work (B d n start : ℕ) :
    coarseDescriptorInvocations B d n start ≤ (coarseDescriptorWithWork B d n start).2 := by
  have hp := bitPowerInvocations_le_work B.bits d
  have hs := saturatingSubtractInvocations_le_work n start
  have hc := bitCompareInvocations_le_work (saturatingSubtractWithWork n start).1
    (bitPowerWithWork B.bits d).1
  unfold coarseDescriptorInvocations coarseDescriptorWithWork
  simp only [wordMinWithWork, wordLEWithWork]
  omega

def coarseChildOffsetInvocations (offset capacity index : ℕ) : ℕ :=
  bitMulInvocations index.bits capacity.bits +
    bitAddInvocations offset.bits (bitWordValue (binaryNaturalMulWithWork index capacity).1).bits false + 1

theorem coarseChildOffsetInvocations_le_work (offset capacity index : ℕ) :
    coarseChildOffsetInvocations offset capacity index ≤ (coarseChildOffsetWithWork offset capacity index).2 := by
  have hm := bitMulInvocations_le_work index.bits capacity.bits
  have ha := bitAddInvocations_le_work offset.bits
    (bitWordValue (binaryNaturalMulWithWork index capacity).1).bits false
  unfold coarseChildOffsetInvocations coarseChildOffsetWithWork
  simp only [binaryNaturalMulWithWork, binaryNaturalAddWithWork] at ha ⊢
  omega

def coarsePaddedReadInvocations (n offset : ℕ) : ℕ := bitCompareInvocations offset.bits n.bits + 1

theorem coarsePaddedReadInvocations_le_work {α : Type*} (n : ℕ) (source : List α) (offset : ℕ) :
    coarsePaddedReadInvocations n offset ≤ (coarsePaddedReadWithWork n source offset).2 := by
  have hc := bitCompareInvocations_le_work offset.bits n.bits
  unfold coarsePaddedReadInvocations coarsePaddedReadWithWork binaryNaturalCompareWithWork
  dsimp only
  omega

def coarseFramedReadInvocations (k n i : ℕ) : ℕ :=
  bitCompareInvocations i.bits k.bits + 1 +
    if (binaryNaturalCompareWithWork i k).1 = .lt then 0 else
      bitAddInvocations k.bits n.bits false +
        bitCompareInvocations i.bits (bitWordValue (binaryNaturalAddWithWork k n).1).bits +
        if (binaryNaturalCompareWithWork i (bitWordValue (binaryNaturalAddWithWork k n).1)).1 = .lt
        then saturatingSubtractInvocations i k else 0

theorem coarseFramedReadInvocations_le_work {α : Type*} (k n : ℕ) (word : List α) (i : ℕ) :
    coarseFramedReadInvocations k n i ≤ (coarseFramedReadWithWork k n word i).2 := by
  have hl := bitCompareInvocations_le_work i.bits k.bits
  have ha := bitAddInvocations_le_work k.bits n.bits false
  have hu := bitCompareInvocations_le_work i.bits (bitWordValue (binaryNaturalAddWithWork k n).1).bits
  have hs := saturatingSubtractInvocations_le_work i k
  unfold coarseFramedReadInvocations coarseFramedReadWithWork
  dsimp only
  split_ifs <;> simp only [binaryNaturalAddWithWork, binaryNaturalCompareWithWork] at * <;> omega

def coarseLetterMatchInvocations : Option (Option ℕ) → Option ℕ → ℕ
  | some (some a), some b => bitCompareInvocations a.bits b.bits + 1
  | _, _ => 1

theorem coarseLetterMatchInvocations_le_work (actual : Option (Option ℕ)) (wanted : Option ℕ) :
    coarseLetterMatchInvocations actual wanted ≤ (coarseLetterMatchWithWork actual wanted).2 := by
  cases actual with
  | none => cases wanted <;> exact Nat.le_refl 1
  | some actual =>
    cases actual with
    | none => cases wanted <;> exact Nat.le_refl 1
    | some a =>
      cases wanted with
      | none => exact Nat.le_refl 1
      | some b =>
        have h := bitCompareInvocations_le_work a.bits b.bits
        exact Nat.add_le_add_right h 1

namespace BinaryFraction

def coarseReadShiftInvocations (k n : ℕ) (target : List ℕ) (letter : Option (Option ℕ))
    (start : ℕ) (shift : Fin (2 * k + 1)) : ℕ :=
  match letter with
  | none => 1
  | some wanted =>
    let position := binaryNaturalAddWithWork start shift.val
    bitAddInvocations start.bits shift.val.bits false +
      coarseFramedReadInvocations k n (bitWordValue position.1) +
      coarseLetterMatchInvocations (coarseFramedReadWithWork k n target (bitWordValue position.1)).1 wanted + 1

theorem coarseReadShiftInvocations_le_work (k n : ℕ) (target : List ℕ)
    (letter : Option (Option ℕ)) (start : ℕ) (shift : Fin (2 * k + 1)) :
    coarseReadShiftInvocations k n target letter start shift ≤ (coarseReadShiftWithWork k n target letter start shift).2 := by
  cases letter with
  | none => exact Nat.le_refl 1
  | some letter =>
    have ha := bitAddInvocations_le_work start.bits shift.val.bits false
    have hf := coarseFramedReadInvocations_le_work k n target
      (bitWordValue (binaryNaturalAddWithWork start shift.val).1)
    have hm := coarseLetterMatchInvocations_le_work
      (coarseFramedReadWithWork k n target (bitWordValue (binaryNaturalAddWithWork start shift.val).1)).1 letter
    unfold coarseReadShiftInvocations coarseReadShiftWithWork
    simp only [binaryNaturalAddWithWork] at hf hm ⊢
    omega

end BinaryFraction
end EditApproximation

end OAI