introvoyz045's picture
Migrated from GitHub
4a56cd6 verified
Raw History Blame Contribute Delete
9.07 kB
import Mathlib
namespace OAI
namespace WLTime
inductive Convention where
| joint | separate
deriving DecidableEq
def Graph (n : β„•) :=
{ a : Fin n β†’ Fin n β†’ Bool //
(βˆ€ x y, a x y = a y x) ∧ (βˆ€ x, a x x = false) }
namespace Graph
def adj {n : β„•} (G : Graph n) : Fin n β†’ Fin n β†’ Bool := G.val
def diameterTwo {n : β„•} (G : Graph n) : Prop :=
0 < n ∧ βˆ€ x y, x = y ∨ G.adj x y = true ∨
βˆƒ z, G.adj x z = true ∧ G.adj z y = true
noncomputable instance (n : β„•) : Fintype (Graph n) := by
classical
unfold Graph
infer_instance
end Graph
abbrev AtomicColor (k : β„•) :=
(Fin k β†’ Fin k β†’ Bool) Γ— (Fin k β†’ Fin k β†’ Bool)
def Color (c : Convention) (k : β„•) : β„• β†’ Type
| 0 => AtomicColor k
| t + 1 => match c with
| .joint => Color c k t Γ— Multiset (Fin k β†’ Color c k t)
| .separate => Color c k t Γ— (Fin k β†’ Multiset (Color c k t))
noncomputable def tupleColor {n : β„•} (c : Convention) (k : β„•) (G : Graph n) :
(t : β„•) β†’ (Fin k β†’ Fin n) β†’ Color c k t
| 0, v => (fun i j => decide (v i = v j), fun i j => G.adj (v i) (v j))
| t + 1, v => match c with
| .joint => (tupleColor .joint k G t v,
Finset.univ.val.map (fun z => fun i =>
tupleColor .joint k G t (Function.update v i z)))
| .separate => (tupleColor .separate k G t v,
fun i => Finset.univ.val.map (fun z =>
tupleColor .separate k G t (Function.update v i z)))
noncomputable def histogram {n : β„•} (c : Convention) (k : β„•) (G : Graph n)
(t : β„•) : Multiset (Color c k t) :=
Finset.univ.val.map (tupleColor c k G t)
def Equivalent {n : β„•} (c : Convention) (k : β„•) (G H : Graph n) : Prop :=
βˆ€ t, histogram c k G t = histogram c k H t
def encodeNat (n : β„•) : List Bool :=
let bits := (Nat.digits 2 n).map (fun b => decide (b = 1))
List.replicate bits.length true ++ [false] ++ bits
def matrixBits {n : β„•} (G : Graph n) : List Bool :=
(List.ofFn fun i : Fin n => List.ofFn fun j : Fin n => G.adj i j).flatten
def encodePair {n : β„•} (G H : Graph n) : List Bool :=
encodeNat n ++ matrixBits G ++ matrixBits H
inductive Move where
| left | stay | right
deriving DecidableEq
def Move.displacement : Move β†’ β„€
| .left => -1
| .stay => 0
| .right => 1
structure TM where
tapes : β„•
states : β„•
symbols : β„•
tapes_pos : 0 < tapes
states_ge : 3 ≀ states
symbols_ge : 3 ≀ symbols
transition : Fin states β†’ (Fin tapes β†’ Fin symbols) β†’
Fin states Γ— (Fin tapes β†’ Fin symbols) Γ— (Fin tapes β†’ Move)
namespace TM
structure Config (M : TM) where
state : Fin M.states
head : Fin M.tapes β†’ β„€
tape : Fin M.tapes β†’ β„€ β†’ Fin M.symbols
def symbol (M : TM) (b : Bool) : Fin M.symbols :=
if b then ⟨2, by have := M.symbols_ge; omega⟩ else
⟨1, by have := M.symbols_ge; omega⟩
def initial (M : TM) (w : List Bool) : M.Config where
state := ⟨0, by have := M.states_ge; omega⟩
head := fun _ => 0
tape := fun a z => if a.val = 0 ∧ 0 ≀ z ∧ z.toNat < w.length then
M.symbol (w.getD z.toNat false) else ⟨0, by have := M.symbols_ge; omega⟩
def halted (M : TM) (C : M.Config) : Prop := C.state.val = 1 ∨ C.state.val = 2
instance (M : TM) (C : M.Config) : Decidable (M.halted C) := inferInstanceAs
(Decidable (C.state.val = 1 ∨ C.state.val = 2))
def step (M : TM) (C : M.Config) : M.Config :=
if M.halted C then C else
let tr := M.transition C.state (fun a => C.tape a (C.head a))
{ state := tr.1
head := fun a => C.head a + (tr.2.2 a).displacement
tape := fun a z => if z = C.head a then tr.2.1 a else C.tape a z }
def run (M : TM) (w : List Bool) (t : β„•) : M.Config := M.step^[t] (M.initial w)
def halts (M : TM) (w : List Bool) : Prop := βˆƒ t, M.halted (M.run w t)
noncomputable def time (M : TM) (w : List Bool) : β„• := by
classical
exact if h : M.halts w then Nat.find h else 0
def accepts (M : TM) (w : List Bool) : Prop :=
βˆƒ t, (M.run w t).state.val = 1
def outputs (M : TM) (input output : List Bool) : Prop :=
M.halts input ∧ βˆ€ i : Fin output.length,
(M.run input (M.time input)).tape ⟨0, M.tapes_pos⟩ (i.val : β„€) =
M.symbol (output.get i)
end TM
abbrev Word (w : β„•) := Fin (2 ^ w)
def wordBits {w : β„•} (a : Word w) : List Bool :=
List.ofFn (fun i : Fin w => a.val.testBit i.val)
def registerBits {R w : β„•} (a : Fin R β†’ Word w) : List Bool :=
(List.ofFn fun i : Fin R => wordBits (a i)).flatten
structure WordOperation (R : β„•) where
apply : (w : β„•) β†’ (Fin R β†’ Word w) β†’ (Fin R β†’ Word w)
implementation : TM
exponent : β„•
constant : β„•
computes : βˆ€ w a, implementation.outputs
(encodeNat w ++ registerBits a) (registerBits (apply w a))
cost : βˆ€ w (a : Fin R β†’ Word w), implementation.time (encodeNat w ++ registerBits a) ≀
constant * (w + 1) ^ exponent
inductive RAMInstruction (R P : β„•) where
| halt (answer : Bool)
| jump (pc : Fin P)
| compute (op : WordOperation R) (next : RAMInstruction R P)
| load (dest addr : Fin R) (next : RAMInstruction R P)
| store (addr src : Fin R) (next : RAMInstruction R P)
| branchZero (reg : Fin R) (yes no : RAMInstruction R P)
structure WordWidth where
bits : β„• β†’ β„•
positive : βˆ€ n, 0 < bits n
bound : β„•
logarithmic : βˆ€ n, bits n ≀ bound * (Nat.log2 (n + 2) + 1)
structure RAM where
registers : β„•
locations : β„•
locations_pos : 0 < locations
width : WordWidth
program : Fin locations β†’ RAMInstruction registers locations
input_fits : βˆ€ n (G H : Graph n),
(encodePair G H).length ≀ width.bits n * 2 ^ width.bits n
namespace RAM
structure Config (A : RAM) (n : β„•) where
pc : Fin A.locations
registers : Fin A.registers β†’ Word (A.width.bits n)
memory : Word (A.width.bits n) β†’ Word (A.width.bits n)
answer : Option Bool
def packedWord (w : β„•) (input : List Bool) (a : Word w) : Word w :=
⟨(βˆ‘ i ∈ Finset.range w, if input.getD (w * a.val + i) false then 2 ^ i else 0)
% (2 ^ w), Nat.mod_lt _ (by positivity)⟩
def initial (A : RAM) (n : β„•) (w : List Bool) : A.Config n where
pc := ⟨0, A.locations_pos⟩
registers := fun _ => ⟨0, by positivity⟩
memory := packedWord (A.width.bits n) w
answer := none
def execute (A : RAM) {n : β„•} :
RAMInstruction A.registers A.locations β†’ A.Config n β†’ A.Config n
| .halt b, C => { C with answer := some b }
| .jump p, C => { C with pc := p }
| .compute op next, C => A.execute next { C with
registers := op.apply (A.width.bits n) C.registers }
| .load dest addr next, C => A.execute next { C with
registers := Function.update C.registers dest (C.memory (C.registers addr)) }
| .store addr src next, C => A.execute next { C with
memory := Function.update C.memory (C.registers addr) (C.registers src) }
| .branchZero reg yes no, C =>
if (C.registers reg).val = 0 then A.execute yes C else A.execute no C
def step (A : RAM) {n : β„•} (C : A.Config n) : A.Config n :=
match C.answer with
| some _ => C
| none => A.execute (A.program C.pc) C
def run (A : RAM) (n : β„•) (w : List Bool) (t : β„•) : A.Config n :=
A.step^[t] (A.initial n w)
def halts (A : RAM) (n : β„•) (w : List Bool) : Prop :=
βˆƒ t, (A.run n w t).answer.isSome = true
noncomputable def time (A : RAM) (n : β„•) (w : List Bool) : β„• := by
classical
exact if h : A.halts n w then Nat.find h else 0
def accepts (A : RAM) (n : β„•) (w : List Bool) : Prop :=
βˆƒ t, (A.run n w t).answer = some true
end RAM
inductive Model where
| turing (machine : TM)
| ram (machine : RAM)
def Model.halts (A : Model) (n : β„•) (w : List Bool) : Prop := match A with
| .turing M => M.halts w
| .ram M => M.halts n w
def Model.accepts (A : Model) (n : β„•) (w : List Bool) : Prop := match A with
| .turing M => M.accepts w
| .ram M => M.accepts n w
noncomputable def Model.time (A : Model) (n : β„•) (w : List Bool) : β„• := match A with
| .turing M => M.time w
| .ram M => M.time n w
inductive InputClass where
| unrestricted | diameterTwo
deriving DecidableEq
def InputClass.contains (p : InputClass) {n : β„•} (G H : Graph n) : Prop := match p with
| .unrestricted => True
| .diameterTwo => G.diameterTwo ∧ H.diameterTwo
def Decides (A : Model) (c : Convention) (k : β„•) (p : InputClass) : Prop :=
βˆ€ n (G H : Graph n), p.contains G H β†’
A.halts n (encodePair G H) ∧
(A.accepts n (encodePair G H) ↔ Equivalent c k G H)
noncomputable def worstTime (A : Model) (p : InputClass) (n : β„•) : β„• := by
classical
exact Finset.univ.sup (fun pair : Graph n Γ— Graph n =>
if p.contains pair.1 pair.2 then A.time n (encodePair pair.1 pair.2) else 0)
def MainClaim : Prop :=
βˆƒ c : ℝ, 0 < c ∧ βˆƒ kβ‚€ : β„•, βˆ€ k : β„•, kβ‚€ ≀ k β†’
βˆ€ convention : Convention, βˆ€ p : InputClass, βˆ€ A : Model,
Decides A convention k p β†’ βˆƒ nβ‚€ : β„•, βˆ€ n : β„•, nβ‚€ ≀ n β†’
(n : ℝ) ^ (c * (k : ℝ)) ≀ (worstTime A p n : ℝ)
end WLTime
end OAI