Download data/lean/OAI/Computability/WeisfeilerLeman/Model.lean from introvoyz045/math: direct link, hf CLI and curl.
- Browser
- Download file 9.07 kB
-
https://huggingface.co/datasets/introvoyz045/math/resolve/main/data/lean/OAI/Computability/WeisfeilerLeman/Model.lean
- Command line
-
hf download hf://datasets/introvoyz045/math/data/lean/OAI/Computability/WeisfeilerLeman/Model.lean
-
curl -L -o Model.lean https://huggingface.co/datasets/introvoyz045/math/resolve/main/data/lean/OAI/Computability/WeisfeilerLeman/Model.lean
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 | |