File size: 6,925 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
import OAI.Combinatorics.EditApproximation.Coarse.CoarseCellBounds

namespace OAI

namespace EditApproximation
namespace BinaryFraction

/-- A zero marker or one full shift array on the execution's common grid. -/
def CoarseBinaryBox (two : BinaryFraction) (n V D A : ℕ) (array : CoarseBinaryArray) : Prop :=
  ∀ cells, array = some cells → cells.length = n ∧ ∀ a ∈ cells, CoarseGridCell two a V D A

 theorem coarseBinaryBox_none (two : BinaryFraction) (n V D A : ℕ) :
    CoarseBinaryBox two n V D A none := by
  intro cells h; cases h

 theorem coarseBinaryBox_mono (two : BinaryFraction) (n V W D A : ℕ)
    (array : CoarseBinaryArray) (h : CoarseBinaryBox two n V D A array) (hle : V ≤ W) :
    CoarseBinaryBox two n W D A array := by
  intro cells hc
  exact ⟨(h cells hc).1, fun a ha => coarseGridCell_mono two a V W D A ((h cells hc).2 a ha) hle⟩

 theorem coarseGridZipAddWithWork_bounds (two : BinaryFraction) (left right : List BinaryFraction)
    (V W D A : ℕ) (hl : ∀ a ∈ left, CoarseGridCell two a V D A)
    (hr : ∀ a ∈ right, CoarseGridCell two a W D A)
    (hbound : Nat.size (V + W) + 2 * D + 1 ≤ A) :
    ∀ a ∈ (coarseGridZipAddWithWork left right).1, CoarseGridCell two a (V + W) D A := by
  induction left generalizing right with
  | nil => simp only [coarseGridZipAddWithWork, List.not_mem_nil, false_implies, implies_true]
  | cons a left ih =>
    cases right with
    | nil => simp only [coarseGridZipAddWithWork, List.not_mem_nil, false_implies, implies_true]
    | cons b right =>
      intro z hz
      change z ∈ (coarseGridAddWithWork a b).1 :: _ at hz
      rcases List.mem_cons.mp hz with rfl | hz
      · exact coarseGridAddWithWork_bounds two a b V W D A (hl a List.mem_cons_self)
          (hr b List.mem_cons_self) hbound
      · exact ih right (fun a ha => hl a (List.mem_cons_of_mem _ ha))
          (fun b hb => hr b (List.mem_cons_of_mem _ hb)) z hz

 theorem coarseGridZipAddWithWork_length (left right : List BinaryFraction) :
    (coarseGridZipAddWithWork left right).1.length = min left.length right.length := by
  induction left generalizing right with
  | nil => simp only [coarseGridZipAddWithWork, List.length_nil, Nat.zero_min]
  | cons a left ih =>
    cases right with
    | nil => simp only [coarseGridZipAddWithWork, List.length_nil, Nat.min_zero]
    | cons b right =>
      change _ + 1 = min _ _
      rw [ih]
      simp only [List.length_cons]
      omega

 theorem coarseBinaryAddWithWork_box (two : BinaryFraction) (n V W D A : ℕ)
    (left right : CoarseBinaryArray) (hl : CoarseBinaryBox two n V D A left)
    (hr : CoarseBinaryBox two n W D A right)
    (hbound : Nat.size (V + W) + 2 * D + 1 ≤ A) :
    CoarseBinaryBox two n (V + W) D A (coarseBinaryAddWithWork left right).1 := by
  cases left with
  | none => exact coarseBinaryBox_mono two n W (V + W) D A right hr (by omega)
  | some left =>
    cases right with
    | none => exact coarseBinaryBox_mono two n V (V + W) D A (some left) hl (by omega)
    | some right =>
      intro cells hc
      have heq : (coarseGridZipAddWithWork left right).1 = cells := Option.some.inj hc
      rw [← heq]
      refine ⟨?_, coarseGridZipAddWithWork_bounds two left right V W D A
        (hl left rfl).2 (hr right rfl).2 hbound⟩
      rw [coarseGridZipAddWithWork_length, (hl left rfl).1, (hr right rfl).1, min_self]

 theorem coarseBinaryAddWithWork_cost (two : BinaryFraction) (n V W D A : ℕ)
    (left right : CoarseBinaryArray) (hl : CoarseBinaryBox two n V D A left)
    (hr : CoarseBinaryBox two n W D A right) :
    (coarseBinaryAddWithWork left right).2 ≤ n * (100 * (A + 1) + 3) := by
  cases left with
  | none => change 0 ≤ _; omega
  | some left =>
    cases right with
    | none => change 0 ≤ _; omega
    | some right =>
      have h := coarseGridZipAddWithWork_cost left right A
        (fun a ha => ((hl left rfl).2 a ha).2.2.2.2)
        (fun b hb => ((hr right rfl).2 b hb).2.2.2.2)
      simpa only [coarseBinaryAddWithWork, (hl left rfl).1, (hr right rfl).1, min_self] using h

 theorem coarseBinaryPenaltyWithWork_box (two : BinaryFraction) (n V D A : ℕ)
    (array : CoarseBinaryArray) (h : CoarseBinaryBox two n V D A array)
    (htwo : two.value = 2) (htw : two.width ≤ A)
    (hbound : Nat.size (V + 2) + 2 * D + 1 ≤ A) :
    CoarseBinaryBox two n V D A (coarseBinaryPenaltyWithWork two array).1 := by
  cases array with
  | none => exact coarseBinaryBox_none two n V D A
  | some source =>
    intro cells hc
    have heq : (coarseGridPenaltyWithWork two source).1 = cells := Option.some.inj hc
    rw [← heq]
    refine ⟨?_, coarseGridPenaltyWithWork_bounds two source V D A htwo htw hbound (h source rfl).2⟩
    have hv := congrArg List.length (coarseGridPenaltyWithWork_value two source htwo
      (fun a ha => ((h source rfl).2 a ha).1))
    simpa only [List.length_map, coarsePenaltyTransform_length, (h source rfl).1] using hv

 theorem coarseBinaryPenaltyWithWork_cost (two : BinaryFraction) (n V D A : ℕ)
    (array : CoarseBinaryArray) (h : CoarseBinaryBox two n V D A array)
    (htwo : two.value = 2) (htw : two.width ≤ A)
    (hbound : Nat.size (V + 2) + 2 * D + 1 ≤ A) :
    (coarseBinaryPenaltyWithWork two array).2 ≤ n * (3500 * (A + 1) ^ 2 + 20) := by
  cases array with
  | none => change 0 ≤ _; omega
  | some source =>
    have hc := coarseGridPenaltyWithWork_cost two source V D A htwo htw hbound (h source rfl).2
    simpa only [coarseBinaryPenaltyWithWork, (h source rfl).1] using hc

 theorem coarseBinaryMapWithWork_box (two : BinaryFraction) (n V W D A : ℕ)
    (f : BinaryFraction → BinaryFraction × ℕ) (array : CoarseBinaryArray)
    (h : CoarseBinaryBox two n V D A array)
    (hf : ∀ a, CoarseGridCell two a V D A → CoarseGridCell two (f a).1 W D A) :
    CoarseBinaryBox two n W D A (coarseBinaryMapWithWork f array).1 := by
  cases array with
  | none => exact coarseBinaryBox_none two n W D A
  | some source =>
    intro cells hc
    have heq : (arithmeticMapWithWork f source).1 = cells := Option.some.inj hc
    rw [← heq, arithmeticMapWithWork_value]
    refine ⟨?_, ?_⟩
    · simpa only [List.length_map] using (h source rfl).1
    · intro a ha
      obtain ⟨b, hb, rfl⟩ := List.mem_map.mp ha
      exact hf b ((h source rfl).2 b hb)

 theorem coarseBinaryMapWithWork_cost (two : BinaryFraction) (n V D A C : ℕ)
    (f : BinaryFraction → BinaryFraction × ℕ) (array : CoarseBinaryArray)
    (h : CoarseBinaryBox two n V D A array)
    (hf : ∀ a, CoarseGridCell two a V D A → (f a).2 ≤ C) :
    (coarseBinaryMapWithWork f array).2 ≤ n * (C + 3) := by
  cases array with
  | none => change 0 ≤ _; omega
  | some source =>
    have hc := arithmeticMapWithWork_cost f source C (fun a ha => hf a ((h source rfl).2 a ha))
    simpa only [coarseBinaryMapWithWork, (h source rfl).1] using hc

end BinaryFraction
end EditApproximation

end OAI