File size: 7,648 Bytes
6fe30b6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
import OAI.Computability.FourierTransform.ProgramConstruction

namespace OAI

namespace PowerSaving.RAM
open Ty Finset

universe U V

/-- One algorithm; time is polynomial in (S+2), integer bounds polynomial in (B+2). Often
  S polylogarithmic in B. -/
def PolyCan {ι : Type U} (m : Bool) (B S : ι → ℕ) (s t) (x : ι → T s) (y : ι → T t) : Prop :=
  ∃ d : ℕ, Can m B s t x y (fun i => (S i+2)^d)

namespace PolyCan
variable {ι : Type U} {κ : Type V}
  {m : Bool} {s t u : Ty}
  {B S B' S' W : ι → ℕ}
  {x : ι → T s} {y : ι → T t} {z : ι → T u}

theorem of_can (h : Can m B s t x y W) (g : Small S W) :
    PolyCan m B S s t x y :=
  let ⟨d,hd⟩ := g
  ⟨d,h.mono hd⟩

theorem cong (h : PolyCan m B S s t x y) {x' y'} (hx : ∀ i, x i = x' i)
    (hy : ∀ i, y i = y' i) : PolyCan m B S s t x' y' :=
  let ⟨d, hd⟩ := h
  ⟨d,hd.cong hx hy⟩

theorem reindex (h : PolyCan m B S s t x y) (f : κ → ι) :
    PolyCan m (fun j => B (f j)) (fun j => S (f j)) s t (fun j => x (f j)) (fun j => y (f j)) :=
  let ⟨d,hd⟩ := h
  ⟨d,hd.reindex f⟩

theorem poly_power (S : ι → ℕ) (d : ℕ) : Small S (fun i => (S i+2)^d) :=
  ⟨d,fun _ => le_rfl⟩

theorem weaken (h : PolyCan m B S s t x y) (a : Small B' B) (b : Small S' S) :
    PolyCan m B' S' s t x y :=
  let ⟨d,hd⟩ := h
  of_can (hd.ambient a) ((poly_power S d).trans b)

theorem pipe (h : PolyCan m B S s t x y) (g : PolyCan m B S t u y z) :
    PolyCan m B S s u x z :=
  let ⟨d,hd⟩ := h
  let ⟨e,he⟩ := g
  of_can (hd.pipe he) ((poly_power S d).add (poly_power S e))

theorem pair (h : PolyCan m B S s t x y) (g : PolyCan m B S s u x z) :
    PolyCan m B S s (p t u) x (fun i => (y i,z i)) :=
  let ⟨d,hd⟩ := h
  let ⟨e,he⟩ := g
  of_can (hd.fork he) ((poly_power S d).add (poly_power S e))

theorem bind (h : PolyCan m B S s t x y)
    (g : PolyCan m B S (p s t) u (fun i => (x i,y i)) z) :
    PolyCan m B S s u x z :=
  let ⟨d,hd⟩ := h
  let ⟨e,he⟩ := g
  of_can (hd.save he) ((poly_power S d).add (poly_power S e))

theorem id : PolyCan m B S s s x x :=
  ⟨1,Can.id⟩

theorem fst {v : ι → T (p t u)} (h : PolyCan m B S s (p t u) x v) :
    PolyCan m B S s t x (fun i => (v i).1) :=
  let ⟨d,hd⟩ := h
  ⟨d,hd.fst⟩

theorem snd {v : ι → T (p t u)} (h : PolyCan m B S s (p t u) x v) :
    PolyCan m B S s u x (fun i => (v i).2) :=
  let ⟨d,hd⟩ := h
  ⟨d,hd.snd⟩

theorem first {x : ι → T (p t u)} : PolyCan m B S (p t u) t x (fun i => (x i).1) :=
  id.fst

theorem second {x : ι → T (p t u)} : PolyCan m B S (p t u) u x (fun i => (x i).2) :=
  id.snd

theorem wc (n : ℕ) : PolyCan m B S s w x (fun _ => n) := ⟨1,Can.wc _⟩

theorem zero {cl} : PolyCan m B S s (c cl) x (fun _ => 0) := ⟨1,Can.zero⟩

theorem one : PolyCan m B S s sc x (fun _ => 1) := ⟨1,Can.one⟩

variable {cl : Paint} {f g : ι → ℂ}

theorem add (h : PolyCan m B S s (c cl) x f)
    (k : PolyCan m B S s (c cl) x g) :
    PolyCan m B S s (c cl) x (fun i => f i + g i) :=
  (h.pair k).pipe ⟨1,Can.add Can.first Can.second⟩

theorem sub (h : PolyCan m B S s (c cl) x f)
    (k : PolyCan m B S s (c cl) x g) :
    PolyCan m B S s (c cl) x (fun i => f i - g i) :=
  (h.pair k).pipe ⟨1,Can.sub Can.first Can.second⟩

theorem mul (h : PolyCan m B S s sc x f)
    (k : PolyCan m B S s (c cl) x g) :
    PolyCan m B S s (c cl) x (fun i => f i * g i) :=
  (h.pair k).pipe ⟨1,Can.mul Can.first Can.second⟩

theorem inv (h : PolyCan m B S s sc x f) (hk : ∀ i, f i ≠ 0) :
    PolyCan m B S s sc x (fun i => (f i)⁻¹) :=
  h.pipe ⟨1,Can.inv Can.id hk⟩

variable {n q : ι → ℕ} {v : ι → Tape (T t)}

theorem look (h : PolyCan m B S s (a t) x v) (k : PolyCan m B S s w x n) :
    PolyCan m B S s t x (fun i => (v i).look (n i) t.blank) :=
  (h.pair k).pipe ⟨1,Can.look Can.first Can.second⟩

theorem len (h : PolyCan m B S s (a t) x v) (k : Small B fun i => (v i).len) :
    PolyCan m B S s w x (fun i => (v i).len) :=
  h.pipe ⟨1,Can.len Can.id k⟩


theorem wadd (h : PolyCan m B S s w x n) (k : PolyCan m B S s w x q)
    (hb : Small B (fun i => n i + q i)) :
    PolyCan m B S s w x (fun i => n i + q i) :=
  (h.pair k).pipe ⟨1,Can.wadd Can.first Can.second hb⟩

theorem wsub (h : PolyCan m B S s w x n) (k : PolyCan m B S s w x q)
    (hb : Small B (fun i => n i - q i)) :
    PolyCan m B S s w x (fun i => n i - q i) :=
  (h.pair k).pipe ⟨1,Can.wsub Can.first Can.second hb⟩

theorem wmul (h : PolyCan m B S s w x n) (k : PolyCan m B S s w x q)
    (hb : Small B (fun i => n i * q i)) :
    PolyCan m B S s w x (fun i => n i * q i) :=
  (h.pair k).pipe ⟨1,Can.wmul Can.first Can.second hb⟩

theorem wdiv (h : PolyCan m B S s w x n) (k : PolyCan m B S s w x q)
    (hb : Small B (fun i => n i / q i)) :
    PolyCan m B S s w x (fun i => n i / q i) :=
  (h.pair k).pipe ⟨1,Can.wdiv Can.first Can.second hb⟩

theorem wmod (h : PolyCan m B S s w x n) (k : PolyCan m B S s w x q)
    (hb : Small B (fun i => n i % q i)) :
    PolyCan m B S s w x (fun i => n i % q i) :=
  (h.pair k).pipe ⟨1,Can.wmod Can.first Can.second hb⟩

theorem wlt (h : PolyCan m B S s w x n) (k : PolyCan m B S s w x q) :
    PolyCan m B S s w x (fun i => if n i < q i then 1 else 0) :=
  (h.pair k).pipe ⟨1,Can.wlt Can.first Can.second⟩

theorem loop {v : ι → ℕ → T t}
    (h : PolyCan m B S s w x n)
    (hi : PolyCan m B S s t x (fun i => v i 0))
    (hs : PolyCan m (fun d : During n => B d.i) (fun d => S d.i)
      (p s (p w t)) t
      (fun d => (x d.i,d.j,v d.i d.j)) (fun d => v d.i (d.j+1)))
    (hc : Small B n) (hd : Small S n) :
    PolyCan m B S s t x (fun i => v i (n i)) := by
  obtain ⟨u,hu⟩ := h
  obtain ⟨l,hl⟩ := hi
  obtain ⟨f,hf⟩ := hs
  refine of_can (Can.loop (v:=v) (uB:=fun i _ => (S i+2)^f) hu hl hf hc) ?_
  simpa using (((poly_power S u).add (poly_power S l)).add hd).add (hd.mul (poly_power S f))

theorem tab {v : ι → ℕ → T t}
    (h : PolyCan m B S s w x n)
    (hs : PolyCan m (fun d : During n => B d.i) (fun d => S d.i)
      (p s w) t (fun d => (x d.i,d.j)) (fun d => v d.i d.j))
    (hc : Small B n) (hd : Small S n) :
    PolyCan m B S s (a t) x (fun i => Tape.tab (n i) (v i)) := by
  obtain ⟨u,hu⟩ := h
  obtain ⟨f,hf⟩ := hs
  refine of_can (Can.tab (v:=v) (uB:=fun i _ => (S i+2)^f) hu hf hc) ?_
  simpa using ((poly_power S u).add hd).add (hd.mul (poly_power S f))

theorem sow {v : ι → ℕ → ℕ × T t}
    (hl : PolyCan m B S s w x q)
    (h : PolyCan m B S s w x n)
    (hs : PolyCan m (fun d : During n => B d.i) (fun d => S d.i)
      (p s w) (p w t) (fun d => (x d.i,d.j)) (fun d => v d.i d.j))
    (hg : Small B q) (hc : Small B n) (hf : Small S q) (hd : Small S n) :
    PolyCan m B S s (a t) x (fun i => Tape.sow (q i) (n i) t.blank (v i)) := by
  obtain ⟨l,hl⟩ := hl
  obtain ⟨u,hu⟩ := h
  obtain ⟨k,hk⟩ := hs
  refine of_can (Can.sow (v:=v) (uB:=fun i _ => (S i+2)^k) hl hu hk hg hc) ?_
  simpa using ((((poly_power S l).add (poly_power S u)).add hf).add hd).add
    (hd.mul (poly_power S k))

theorem testz (h : PolyCan m B S s w x n)
    (g : PolyCan m (fun i : {i // n i = 0} => B i.val) (fun i => S i.val) s t
      (fun i => x i.val) (fun i => y i.val))
    (j : PolyCan m (fun i : {i // n i ≠ 0} => B i.val) (fun i => S i.val) s t
      (fun i => x i.val) (fun i => y i.val)) :
    PolyCan m B S s t x y := by
  obtain ⟨l,hl⟩ := h
  obtain ⟨u,hu⟩ := g
  obtain ⟨k,hk⟩ := j
  refine ⟨l+u+k,Can.testz (n:=n) (hl.mono ?_) (hu.mono ?_) (hk.mono ?_)⟩ <;>
    intro i <;> apply Small.pow_mono_degree <;> omega

end PolyCan
end PowerSaving.RAM

end OAI