File size: 15,347 Bytes
760f90e | 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 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 | import OAI.Computability.Superstring.Schedule
namespace OAI
namespace Superstring.BitCode
abbrev Store (K : Type) := K → List Bool
/-- A finite-control bit-stack instruction. There is no unbounded internal
state and no operation on an entire stack other than one push/pop/peek. -/
inductive Instr (K L : Type)
| push (k : K) (b : Bool) (next : L)
| pop (k : K) (next : L)
| branch (k : K) (empty low high : L)
| jump (next : L)
namespace Instr
def map {K L M : Type} (f : L → M) : Instr K L → Instr K M
| .push k b l => .push k b (f l)
| .pop k l => .pop k (f l)
| .branch k e l h => .branch k (f e) (f l) (f h)
| .jump l => .jump (f l)
def run {K L : Type} [DecidableEq K] : Instr K L → Store K → L × Store K
| .push k b l, s => (l, Function.update s k (b :: s k))
| .pop k l, s => (l, Function.update s k (s k).tail)
| .branch k e l h, s => (match (s k).head? with
| none => e
| some false => l
| some true => h, s)
| .jump l, s => (l, s)
@[simp] theorem run_map {K L M : Type} [DecidableEq K] (f : L → M)
(i : Instr K L) (s : Store K) : (i.map f).run s = Prod.map f id (i.run s) := by
cases i with
| branch k e l h => cases hs : (s k).head? with
| none => simp [map, run, hs]
| some b => cases b <;> simp [map, run, hs]
| _ => rfl
def emit {K L : Type} : Instr K (Option L) →
Turing.TM2.Stmt (fun _ : K => Bool) L (Option Bool)
| .push k b l => .push k (fun _ => b) (finish l)
| .pop k l => .pop k (fun _ _ => none) (finish l)
| .branch k e l h => .peek k (fun _ b => b)
(.branch Option.isNone (finish e)
(.branch (fun b => b.getD false) (finish h) (finish l)))
| .jump l => finish l
where
finish {K L : Type} (l : Option L) :
Turing.TM2.Stmt (fun _ : K => Bool) L (Option Bool) :=
.load (fun _ => none) (match l with | none => .halt | some x => .goto (fun _ => x))
theorem finish_step {K L : Type} [DecidableEq K] (l : Option L)
(v : Option Bool) (s : Store K) :
Turing.TM2.stepAux (emit.finish l) v s = ⟨l, none, s⟩ := by
cases l <;> rfl
theorem emit_step {K L : Type} [DecidableEq K] (i : Instr K (Option L))
(v : Option Bool) (s : Store K) :
Turing.TM2.stepAux i.emit v s = ⟨(i.run s).1, none, (i.run s).2⟩ := by
cases i with
| push k b l => exact finish_step l v _
| pop k l => exact finish_step l none _
| branch k e l h =>
cases hs : (s k).head? with
| none => simp [emit, Turing.TM2.stepAux, hs, finish_step, run]
| some b => cases b <;> simp [emit, Turing.TM2.stepAux, hs, finish_step, run]
| jump l => exact finish_step l v s
end Instr
/-- Structured bit-stack code. Loops are ordinary operational loops, not
unbounded-cost primitives. Their iteration count is charged in `Exec`. -/
inductive Block (K : Type)
| push (k : K) (b : Bool)
| pop (k : K)
| skip
| branch (k : K) (empty low high : Block K)
| seq (first second : Block K)
| loop (k : K) (body : Block K)
namespace Block
def Label {K : Type} : Block K → Type
| .push _ _ | .pop _ | .skip => Unit
| .branch _ a b c => Unit ⊕ (Label a ⊕ Label b ⊕ Label c)
| .seq a b => Label a ⊕ Label b
| .loop _ a => Option (Label a)
instance labelFintype {K : Type} : (b : Block K) → Fintype b.Label
| .push _ _ | .pop _ | .skip => inferInstanceAs (Fintype Unit)
| .branch _ a b c => by
letI := labelFintype a
letI := labelFintype b
letI := labelFintype c
exact inferInstanceAs (Fintype (Unit ⊕ (Label a ⊕ Label b ⊕ Label c)))
| .seq a b => by
letI := labelFintype a
letI := labelFintype b
exact inferInstanceAs (Fintype (Label a ⊕ Label b))
| .loop _ a => by
letI := labelFintype a
exact inferInstanceAs (Fintype (Option (Label a)))
instance labelDecidableEq {K : Type} : (b : Block K) → DecidableEq b.Label
| .push _ _ | .pop _ | .skip => inferInstanceAs (DecidableEq Unit)
| .branch _ a b c => by
letI := labelDecidableEq a
letI := labelDecidableEq b
letI := labelDecidableEq c
exact inferInstanceAs (DecidableEq (Unit ⊕ (Label a ⊕ Label b ⊕ Label c)))
| .seq a b => by
letI := labelDecidableEq a
letI := labelDecidableEq b
exact inferInstanceAs (DecidableEq (Label a ⊕ Label b))
| .loop _ a => by
letI := labelDecidableEq a
exact inferInstanceAs (DecidableEq (Option (Label a)))
def start {K : Type} : (b : Block K) → b.Label
| .push _ _ | .pop _ | .skip => ()
| .branch _ _ _ _ => .inl ()
| .seq a _ => .inl a.start
| .loop _ _ => none
def code {K : Type} : (b : Block K) → b.Label → Instr K (Option b.Label)
| .push k b, _ => .push k b none
| .pop k, _ => .pop k none
| .skip, _ => .jump none
| .branch k a b c, .inl _ => .branch k
(some (.inr (.inl a.start))) (some (.inr (.inr (.inl b.start))))
(some (.inr (.inr (.inr c.start))))
| .branch _ a _ _, .inr (.inl l) => (a.code l).map (Option.map (Sum.inr ∘ Sum.inl))
| .branch _ _ b _, .inr (.inr (.inl l)) =>
(b.code l).map (Option.map (Sum.inr ∘ Sum.inr ∘ Sum.inl))
| .branch _ _ _ c, .inr (.inr (.inr l)) =>
(c.code l).map (Option.map (Sum.inr ∘ Sum.inr ∘ Sum.inr))
| .seq a b, .inl l => (a.code l).map (fun q =>
match q with | none => some (.inr b.start) | some x => some (.inl x))
| .seq _ b, .inr l => (b.code l).map (Option.map Sum.inr)
| .loop k a, none => .branch k none (some (some a.start)) (some (some a.start))
| .loop _ a, some l => (a.code l).map (fun q => some q)
end Block
end Superstring.BitCode
namespace Superstring.BitCode
variable {K : Type} [DecidableEq K]
/-- Exact execution in the finite-control graph, stopping before a halt label
is stepped. This is a finite trace, not an oracle for a computation. -/
inductive Steps {L : Type} (code : L → Instr K (Option L)) :
(Option L × Store K) → ℕ → (Option L × Store K) → Prop
| refl (c) : Steps code c 0 c
| step (l : L) (s : Store K) {n d} (h : Steps code ((code l).run s) n d) :
Steps code (some l, s) (n + 1) d
namespace Steps
variable {L M : Type} {code : L → Instr K (Option L)}
theorem trans {a b c} {m n : ℕ} (h : Steps code a m b) (h' : Steps code b n c) :
Steps code a (m + n) c := by
induction h with
| refl => simpa using h'
| step l s h ih => simpa only [Nat.add_assoc, Nat.add_comm 1] using step l s (ih h')
theorem map (f : Option L → Option M) (j : L → M)
{big : M → Instr K (Option M)}
(hl : ∀ l, f (some l) = some (j l))
(hc : ∀ l, big (j l) = (code l).map f)
{a b} {n} (h : Steps code a n b) :
Steps big (Prod.map f id a) n (Prod.map f id b) := by
induction h with
| refl => exact .refl _
| step l s h ih =>
simp only [Prod.map, id_eq, hl] at *
apply Steps.step
simpa only [hc, Instr.run_map, Prod.map, id_eq] using ih
end Steps
namespace Block
/-- Big-step semantics with the exact number of primitive bit instructions. -/
inductive Exec : Block K → Store K → Store K → ℕ → Prop
| push (k b s) : Exec (.push k b) s (Function.update s k (b :: s k)) 1
| pop (k s) : Exec (.pop k) s (Function.update s k (s k).tail) 1
| skip (s) : Exec .skip s s 1
| branch_none {k a b c s t n} (h : (s k).head? = none) (run : Exec a s t n) :
Exec (.branch k a b c) s t (n + 1)
| branch_false {k a b c s t n} (h : (s k).head? = some false) (run : Exec b s t n) :
Exec (.branch k a b c) s t (n + 1)
| branch_true {k a b c s t n} (h : (s k).head? = some true) (run : Exec c s t n) :
Exec (.branch k a b c) s t (n + 1)
| seq {a b s t u m n} (first : Exec a s t m) (second : Exec b t u n) :
Exec (.seq a b) s u (m + n)
| loop_nil {k a s} (h : s k = []) : Exec (.loop k a) s s 1
| loop_cons {k a s t u m n} (h : s k ≠ []) (body : Exec a s t m)
(rest : Exec (.loop k a) t u n) : Exec (.loop k a) s u (m + n + 1)
theorem exec_steps {b : Block K} {s t : Store K} {n : ℕ} (h : b.Exec s t n) :
Steps b.code (some b.start, s) n (none, t) := by
induction h with
| push k b s => exact .step () s (.refl _)
| pop k s => exact .step () s (.refl _)
| skip s => exact .step () s (.refl _)
| @branch_none k a b c s t n hh run ih =>
apply Steps.step
simp only [code, start, Instr.run, hh]
exact ih.map (Option.map (Sum.inr ∘ Sum.inl)) (Sum.inr ∘ Sum.inl)
(fun _ => rfl) (fun _ => rfl)
| @branch_false k a b c s t n hh run ih =>
apply Steps.step
simp only [code, start, Instr.run, hh]
exact ih.map (Option.map (Sum.inr ∘ Sum.inr ∘ Sum.inl))
(Sum.inr ∘ Sum.inr ∘ Sum.inl) (fun _ => rfl) (fun _ => rfl)
| @branch_true k a b c s t n hh run ih =>
apply Steps.step
simp only [code, start, Instr.run, hh]
exact ih.map (Option.map (Sum.inr ∘ Sum.inr ∘ Sum.inr))
(Sum.inr ∘ Sum.inr ∘ Sum.inr) (fun _ => rfl) (fun _ => rfl)
| @seq a b s t u m n first second ih₁ ih₂ =>
let f : Option a.Label → Option ((seq a b).Label) := fun q =>
match q with | none => some (.inr b.start) | some x => some (.inl x)
have h₁ : Steps (seq a b).code (some (.inl a.start), s) m (some (.inr b.start), t) :=
ih₁.map f Sum.inl (fun _ => rfl) (fun _ => rfl)
have h₂ : Steps (seq a b).code (some (.inr b.start), t) n (none, u) :=
ih₂.map (Option.map Sum.inr) Sum.inr (fun _ => rfl) (fun _ => rfl)
exact h₁.trans h₂
| @loop_nil k a s hh =>
apply Steps.step
simpa only [code, start, Instr.run, hh, List.head?_nil] using
(Steps.refl (code := code (.loop k a)) (none, s))
| @loop_cons k a s t u m n hh body rest ih₁ ih₂ =>
apply Steps.step
have hstart : ((code (.loop k a)) none).run s = (some (some a.start), s) := by
cases he : s k with
| nil => exact False.elim (hh he)
| cons x xs => cases x <;> simp only [code, Instr.run, he, List.head?_cons] <;> rfl
dsimp only [start]
rw [hstart]
exact (ih₁.map some some (fun _ => rfl) (fun _ => rfl)).trans ih₂
def machine [Fintype K] (b : Block K) (input output : K) : Turing.FinTM2 where
K := K
k₀ := input
k₁ := output
Γ _ := Bool
Λ := b.Label
main := b.start
σ := Option Bool
initialState := none
m l := (b.code l).emit
def pack {L : Type} (c : Option L × Store K) :
Turing.TM2.Cfg (fun _ : K => Bool) L (Option Bool) := ⟨c.1, none, c.2⟩
theorem steps_iterate {L : Type} {code : L → Instr K (Option L)}
{s t : Option L × Store K} {n : ℕ} (h : Steps code s n t) :
(fun c => c.bind (Turing.TM2.step (fun l => (code l).emit)))^[n]
(some (pack s)) = some (pack t) := by
induction h with
| refl => rfl
| step l S h ih =>
rw [Function.iterate_succ_apply]
simpa only [pack, Option.bind_some, Turing.TM2.step, Instr.emit_step] using ih
def exec_machine [Fintype K] (b : Block K) (input output : K)
{s t : Store K} {n : ℕ} (h : b.Exec s t n) :
StateTransition.EvalsToInTime (b.machine input output).step
(pack (some b.start, s)) (some (pack (none, t))) n :=
⟨⟨n, steps_iterate (exec_steps h)⟩, le_rfl⟩
end Block
end Superstring.BitCode
namespace Superstring.BitCode
variable {K : Type} [DecidableEq K]
/-- A single populated stack: the exact Mathlib initial/final convention. -/
def single (k : K) (w : List Bool) : Store K := fun j => if j = k then w else []
namespace Block
theorem init_eq [Fintype K] (b : Block K) (i o : K) (w : List Bool) :
Turing.initList (b.machine i o) w = pack (some b.start, single i w) := by
dsimp only [Turing.initList, machine, pack]
apply congrArg (fun S : Store K => Turing.TM2.Cfg.mk (some b.start) none S)
funext k
by_cases h : k = i
· subst k
rfl
· simp only [dite_eq_right h, single, ite_eq_right h]
theorem halt_eq [Fintype K] (b : Block K) (i o : K) (w : List Bool) :
Turing.haltList (b.machine i o) w = pack (none, single o w) := by
dsimp only [Turing.haltList, machine, pack]
apply congrArg (fun S : Store K => Turing.TM2.Cfg.mk none none S)
funext k
by_cases h : k = o
· subst k
rfl
· simp only [dite_eq_right h, single, ite_eq_right h]
/-- Execution with a polynomial clock gives a polynomial-time TM2 certificate. -/
noncomputable def certificate [Fintype K] {α β : Type}
(ea : α → List Bool) (eb : β → List Bool) (f : α → β)
(b : Block K) (i o : K) (P : Polynomial ℕ)
(correct : ∀ a, ∃ n, n ≤ P.eval (ea a).length ∧
b.Exec (single i (ea a)) (single o (eb (f a))) n) :
Turing.TM2ComputableInPolyTime ea eb f where
tm := b.machine i o
inputAlphabet := Equiv.refl Bool
outputAlphabet := Equiv.refl Bool
time := P
outputsFun a := by
let n := (correct a).choose
have hn := (correct a).choose_spec
refine ⟨⟨n, ?_⟩, hn.1⟩
change (flip bind (b.machine i o).step)^[n]
(some (Turing.initList (b.machine i o) ((ea a).map id))) =
some (Turing.haltList (b.machine i o) ((eb (f a)).map id))
have hi := init_eq b i o (ea a)
have ho := halt_eq b i o (eb (f a))
dsimp only [machine] at *
simp only [List.map_id]
rw [hi, ho]
exact steps_iterate (exec_steps hn.2)
theorem certificate_finite [Fintype K] {α β : Type}
(ea : α → List Bool) (eb : β → List Bool) (f : α → β)
(b : Block K) (i o : K) (P : Polynomial ℕ)
(correct : ∀ a, ∃ n, n ≤ P.eval (ea a).length ∧
b.Exec (single i (ea a)) (single o (eb (f a))) n)
(k : (b.certificate ea eb f i o P correct).tm.K) :
Finite ((b.certificate ea eb f i o P correct).tm.Γ k) := inferInstanceAs (Finite Bool)
end Block
section Size
variable [Fintype K]
def size (s : Store K) : ℕ := ∑ k, (s k).length
theorem size_update (s : Store K) (k : K) (w : List Bool) :
size (Function.update s k w) + (s k).length = size s + w.length := by
classical
have h₁ := Finset.sum_erase_add Finset.univ (fun j => (Function.update s k w j).length)
(Finset.mem_univ k)
have h₂ := Finset.sum_erase_add Finset.univ (fun j => (s j).length) (Finset.mem_univ k)
have he : (∑ j ∈ Finset.univ.erase k, (Function.update s k w j).length) =
∑ j ∈ Finset.univ.erase k, (s j).length := by
apply Finset.sum_congr rfl
intro j hj
rw [Function.update_of_ne (Finset.mem_erase.mp hj).1]
simp only [he, Function.update_self] at h₁
dsimp only [size]
omega
theorem size_push (s : Store K) (k : K) (b : Bool) :
size (Function.update s k (b :: s k)) = size s + 1 := by
have hh := size_update s k (b :: s k)
simp only [List.length_cons] at hh
omega
theorem size_pop (s : Store K) (k : K) :
size (Function.update s k (s k).tail) ≤ size s := by
have hh := size_update s k (s k).tail
simp only [List.length_tail] at hh
omega
/-- Every actually executed primitive bit instruction increases total stored
length by at most one; branch tests and iterations do not hide bulk operations. -/
theorem Block.Exec.size_le {b : Block K} {s t : Store K} {n : ℕ}
(h : b.Exec s t n) : size t ≤ size s + n := by
induction h with
| push k b s => exact le_of_eq (size_push s k b)
| pop k s => exact (size_pop s k).trans (Nat.le_add_right _ _)
| skip => omega
| branch_none _ _ ih => omega
| branch_false _ _ ih => omega
| branch_true _ _ ih => omega
| seq _ _ ih₁ ih₂ => omega
| loop_nil => omega
| loop_cons _ _ _ ih₁ ih₂ => omega
end Size
end Superstring.BitCode
end OAI
|