File size: 6,200 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
import OAI.Combinatorics.EditApproximation.Coarse.CoarseScalarControlSpace
import OAI.Combinatorics.EditApproximation.Coarse.CoarseArrayAllocation

namespace OAI

namespace EditApproximation
namespace BinaryFraction

def coarseGridScanInvocations (two previous : BinaryFraction) : List BinaryFraction → ℕ
  | [] => 1
  | head :: rest =>
    let added := coarseGridAddWithWork previous two
    let next := minWithWork head added.1
    coarseGridAddInvocations previous two + compareInvocations head added.1 +
      coarseGridScanInvocations two next.1 rest + 2

theorem coarseGridScanInvocations_le_work (two previous : BinaryFraction) (source : List BinaryFraction) :
    coarseGridScanInvocations two previous source ≤ (coarseGridScanWithWork two previous source).2 + 1 := by
  induction source generalizing previous with
  | nil => rfl
  | cons head rest ih =>
    have ha := coarseGridAddInvocations_le_work previous two
    have hm := compareInvocations_le_work head (coarseGridAddWithWork previous two).1
    have ht := ih (minWithWork head (coarseGridAddWithWork previous two).1).1
    simp only [coarseGridScanInvocations, coarseGridScanWithWork]
    simp only [minWithWork] at ht ⊢
    omega

def coarseGridLeftInvocations (two : BinaryFraction) : List BinaryFraction → ℕ
  | [] => 1
  | head :: rest => coarseGridScanInvocations two head rest + 1

theorem coarseGridLeftInvocations_le_work (two : BinaryFraction) (source : List BinaryFraction) :
    coarseGridLeftInvocations two source ≤ (coarseGridLeftWithWork two source).2 + 2 := by
  cases source with
  | nil => simp [coarseGridLeftInvocations, coarseGridLeftWithWork]
  | cons head rest =>
    have h := coarseGridScanInvocations_le_work two head rest
    simp only [coarseGridLeftInvocations, coarseGridLeftWithWork]
    omega

def coarseGridZipMinimumInvocations : List BinaryFraction → List BinaryFraction → ℕ
  | a :: left, b :: right => compareInvocations a b + coarseGridZipMinimumInvocations left right + 2
  | _, _ => 1

theorem coarseGridZipMinimumInvocations_le_work (left right : List BinaryFraction) :
    coarseGridZipMinimumInvocations left right ≤ (coarseGridZipMinimumWithWork left right).2 + 1 := by
  induction left generalizing right with
  | nil => exact Nat.le_refl 1
  | cons a left ih =>
    cases right with
    | nil => exact Nat.le_refl 1
    | cons b right =>
      have hh := compareInvocations_le_work a b
      have ht := ih right
      simp only [coarseGridZipMinimumInvocations, coarseGridZipMinimumWithWork, minWithWork]
      omega

/-- Includes one invocation per reverse-list step, even if implemented with
an iterative constant-stack loop. -/
def coarseGridPenaltyInvocations (two : BinaryFraction) (source : List BinaryFraction) : ℕ :=
  let left := coarseGridLeftWithWork two source
  let right := coarseGridLeftWithWork two source.reverse
  coarseGridLeftInvocations two source + coarseGridLeftInvocations two source.reverse +
    source.length + right.1.length + coarseGridZipMinimumInvocations left.1 right.1.reverse + 3

theorem coarseGridPenaltyInvocations_le_work (two : BinaryFraction) (source : List BinaryFraction) :
    coarseGridPenaltyInvocations two source ≤ 2 * (coarseGridPenaltyWithWork two source).2 + 8 := by
  have hl := coarseGridLeftInvocations_le_work two source
  have hr := coarseGridLeftInvocations_le_work two source.reverse
  have hz := coarseGridZipMinimumInvocations_le_work
    (coarseGridLeftWithWork two source).1 (coarseGridLeftWithWork two source.reverse).1.reverse
  have hlen := coarseGridLeftWithWork_length two source.reverse
  simp only [List.length_reverse] at hr hlen
  unfold coarseGridPenaltyInvocations coarseGridPenaltyWithWork
  dsimp only
  rw [hlen]
  omega

def coarseGridZipAddInvocations : List BinaryFraction → List BinaryFraction → ℕ
  | a :: left, b :: right => coarseGridAddInvocations a b + coarseGridZipAddInvocations left right + 1
  | _, _ => 1

theorem coarseGridZipAddInvocations_le_work (left right : List BinaryFraction) :
    coarseGridZipAddInvocations left right ≤ (coarseGridZipAddWithWork left right).2 + 1 := by
  induction left generalizing right with
  | nil => exact Nat.le_refl 1
  | cons a left ih =>
    cases right with
    | nil => exact Nat.le_refl 1
    | cons b right =>
      have hh := coarseGridAddInvocations_le_work a b
      have ht := ih right
      simp only [coarseGridZipAddInvocations, coarseGridZipAddWithWork]
      omega

def coarseBinaryAddInvocations : CoarseBinaryArray → CoarseBinaryArray → ℕ
  | some left, some right => coarseGridZipAddInvocations left right + 1
  | _, _ => 1

theorem coarseBinaryAddInvocations_le_work (left right : CoarseBinaryArray) :
    coarseBinaryAddInvocations left right ≤ (coarseBinaryAddWithWork left right).2 + 2 := by
  cases left <;> cases right
  all_goals first | exact (by decide : 1 ≤ 2) |
    exact Nat.add_le_add_right (coarseGridZipAddInvocations_le_work _ _) 1

def coarseBinaryPenaltyInvocations (two : BinaryFraction) : CoarseBinaryArray → ℕ
  | none => 1
  | some values => coarseGridPenaltyInvocations two values + 1

theorem coarseBinaryPenaltyInvocations_le_work (two : BinaryFraction) (array : CoarseBinaryArray) :
    coarseBinaryPenaltyInvocations two array ≤ 2 * (coarseBinaryPenaltyWithWork two array).2 + 9 := by
  cases array with
  | none => exact (by decide : 1 ≤ 9)
  | some values => exact Nat.add_le_add_right (coarseGridPenaltyInvocations_le_work two values) 1

def coarseBinaryMapInvocations (invocations : BinaryFraction → ℕ) : CoarseBinaryArray → ℕ
  | none => 1
  | some values => arithmeticMapInvocations invocations values + 1

theorem coarseBinaryMapInvocations_le_work (f : BinaryFraction → BinaryFraction × ℕ)
    (invocations : BinaryFraction → ℕ) (h : ∀ a, invocations a ≤ (f a).2) (array : CoarseBinaryArray) :
    coarseBinaryMapInvocations invocations array ≤ (coarseBinaryMapWithWork f array).2 + 2 := by
  cases array with
  | none => exact (by decide : 1 ≤ 2)
  | some values =>
    exact Nat.add_le_add_right
      (arithmeticMapInvocations_le_work f invocations values (fun a _ => h a)) 1

end BinaryFraction
end EditApproximation

end OAI