import OAI.Computability.WLIdentification.RefinementCorrectness namespace OAI namespace WLIdentification namespace DimensionDecider def matrix (n : ℕ) (w : Word) : Word := (w.drop (n+1)).take (n*n) def bits (n : ℕ) (w : Word) : Word := w.drop (n+1+n*n) def valid (n : ℕ) (w : Word) : Bool := decide (0 !(NumericWL.equivalent n k cap W (NumericGraph.mate n x)) || NumericGraph.iso n cap W (NumericGraph.mate n x)) def atOrder (n cap : ℕ) (w : Word) : Bool := valid n w && NumericGraph.simple n (matrix n w) && identifies n (min (binaryValue (bits n w)) (max 2 n)) cap (matrix n w) def decideWord (cap : ℕ) (w : Word) : Bool := (List.range (w.length+1)).any (fun n => atOrder n cap w) def budget (m : ℕ) : ℕ := 2^(64*(m+1)^6) end DimensionDecider namespace NumericGraph @[simp] theorem ofWord_matrixWord (G : Graph) : ofWord G.order (matrixWord G) (simple_matrixWord G) = G := by apply Graph.ext (ofWord G.order (matrixWord G) (simple_matrixWord G)) G rfl intro i j exact edge_matrixWord G i j @[simp] theorem mate_simple (n x : ℕ) : simple n (mate n x) = true := by apply (simple_true _ _).mpr constructor · intro i; rw [edge_mate]; exact (mateGraph n x).loopless i · intro i j; rw [edge_mate, edge_mate]; exact (mateGraph n x).symmetric i j @[simp] theorem ofWord_mate (n x : ℕ) : ofWord n (mate n x) (mate_simple n x) = mateGraph n x := by apply Graph.ext (ofWord n (mate n x) (mate_simple n x)) (mateGraph n x) rfl exact edge_mate n x @[simp] theorem matrixWord_ofWord (n : ℕ) (W : Word) (h : simple n W = true) (hl : W.length=n*n) : matrixWord (ofWord n W h) = W := by apply List.ext_getElem (by simp [hl]) intro t ht ht' have hn : 0 dsimp only at hn subst m apply Graph.ext (NumericGraph.mateGraph G.order x) ⟨G.order,ed,hs,hl⟩ rfl intro i j exact NumericGraph.mateGraph_covers ⟨G.order,ed,hs,hl⟩ i j simpa only [he] using h x hx (by simpa only [he] using hH) namespace DimensionDecider theorem valid_iff (n : ℕ) (w : Word) : valid n w = true ↔ 0 simp | cons b w => cases b <;> simp rw [hb] tauto theorem binaryValue_leading_pos {w : Word} (h : ∃ t, w=true::t) : 0 2*z+if b then 1 else 0) c := by induction t generalizing c with | nil => exact hc | cons b t ih => apply ih cases b <;> simp only [Bool.false_eq_true, ↓reduceIte] <;> omega exact ha t 1 (by decide) theorem input_valid (G : Graph) (w : Word) (hn : 0 cases b <;> decide rw [hbool, NumericWL.equivalent_correct n k cap W (NumericGraph.mate n x) hW (NumericGraph.mate_simple n x) hk hn ht hp, NumericGraph.iso_correct n cap W (NumericGraph.mate n x) hW (NumericGraph.mate_simple n x) he, NumericGraph.ofWord_mate] theorem capped_identifies (G : Graph) (k : ℕ) (hk : 0 < k) : Identifies (min k (max 2 G.order)) G ↔ WLdim G ≤ k := by have hm : 0 < max 2 G.order := by have := le_max_left 2 G.order; omega have hd : WLdim G ≤ max 2 G.order := (WLdim_le_iff hm).mpr (identifies_order G) rw [← WLdim_le_iff (lt_min hk hm)] omega theorem decideWord_correct (w : Word) : decideWord (budget w.length) w=true ↔ w∈dimensionLanguage := by simp only [decideWord, List.any_eq_true, List.mem_range] constructor · rintro ⟨n,hn,ha⟩ have hn' : n ≤ w.length := by omega simp only [atOrder, Bool.and_eq_true] at ha obtain ⟨⟨hv,hs⟩,hi⟩ := ha obtain ⟨hpos,_,_,hb⟩ := (valid_iff n w).mp hv have hk := binaryValue_leading_pos hb have hc : 0 < min (binaryValue (bits n w)) (max 2 n) := lt_min hk (by have := le_max_left 2 n; omega) obtain ⟨b1,b2,b3,b4,b5⟩ := budget_bounds hn' (min_le_right (binaryValue (bits n w)) (max 2 n)) have hI := (identifies_correct _ _ _ _ hs hc b1 b2 b3 b4 b5).mp hi have hd := (capped_identifies (NumericGraph.ofWord n (matrix n w) hs) _ hk).mp hI exact ⟨NumericGraph.ofWord n (matrix n w) hs, bits n w, hpos, hb, hk, reconstruct hv hs, hd⟩ · rintro ⟨G,B,hG,hB,hk,rfl,hd⟩ let w := inputWord G B have hn : G.order ≤ w.length := by simp [w, inputWord] refine ⟨G.order, by dsimp only [w] at hn; omega, ?_⟩ have hv := input_valid G B hG hB obtain ⟨hmat,hbits⟩ := input_extract G B change atOrder G.order (budget w.length) w=true simp only [atOrder, w, hmat, hbits, hv, NumericGraph.simple_matrixWord, Bool.true_and] have hc : 0 < min (binaryValue B) (max 2 G.order) := lt_min hk (by have := le_max_left 2 G.order; omega) obtain ⟨b1,b2,b3,b4,b5⟩ := budget_bounds hn (min_le_right (binaryValue B) (max 2 G.order)) rw [identifies_correct _ _ _ _ (NumericGraph.simple_matrixWord G) hc b1 b2 b3 b4 b5, NumericGraph.ofWord_matrixWord, capped_identifies G _ hk] exact hd end DimensionDecider namespace DimensionDecider open WordExpressions theorem matrix_poly {m} {n : (Fin m → Word) → ℕ} {w : (Fin m → Word) → Word} (hn : NatPoly n) (hw : Poly w) : Poly (fun S => matrix (n S) (w S)) := Poly.take (Poly.dropPrefix hw (NatPoly.succ hn)) (hn.mul hn) theorem bits_poly {m} {n : (Fin m → Word) → ℕ} {w : (Fin m → Word) → Word} (hn : NatPoly n) (hw : Poly w) : Poly (fun S => bits (n S) (w S)) := Poly.dropPrefix hw ((NatPoly.succ hn).add (hn.mul hn)) theorem valid_poly {m} {n : (Fin m → Word) → ℕ} {w : (Fin m → Word) → Word} (hn : NatPoly n) (hw : Poly w) : BoolPoly (fun S => valid (n S) (w S)) := BoolPoly.and (BoolPoly.and (BoolPoly.and (NatPoly.lt (NatPoly.const 0) hn) (Poly.eqtest (Poly.take hw (NatPoly.succ hn)) (Poly.append (Poly.replicate hn true) (WordExpressions.Poly.const [false])))) (NatPoly.eqtest (NatPoly.length (matrix_poly hn hw)) (hn.mul hn))) (Poly.nth (bits_poly hn hw) (NatPoly.const 0)) theorem identifies_poly : BoolPoly (fun S : Fin 4 → Word => identifies (S 0).length (S 1).length (S 2).length (S 3)) := by unfold identifies apply BoolPoly.allRange · word_poly · apply BoolPoly.or · apply BoolPoly.not apply NumericWL.equivalent_poly_of · word_poly · word_poly · word_poly · word_poly · apply NumericGraph.mate_poly_of <;> word_poly · apply NumericGraph.iso_poly_of · word_poly · word_poly · word_poly · apply NumericGraph.mate_poly_of <;> word_poly theorem identifies_poly_of {m} {n k c : (Fin m → Word) → ℕ} {W : (Fin m → Word) → Word} (hn : NatPoly n) (hk : NatPoly k) (hc : NatPoly c) (hW : Poly W) : BoolPoly (fun S => identifies (n S) (k S) (c S) (W S)) := by have h := identifies_poly.subst (g := ![fun S => unary (n S), fun S => unary (k S), fun S => unary (c S), W]) (by intro i; fin_cases i; exacts [hn,hk,hc,hW]) simpa [unary] using h theorem atOrder_poly {m} {n c : (Fin m → Word) → ℕ} {w : (Fin m → Word) → Word} (hn : NatPoly n) (hc : NatPoly c) (hw : Poly w) : BoolPoly (fun S => atOrder (n S) (c S) (w S)) := BoolPoly.and (BoolPoly.and (valid_poly hn hw) (NumericGraph.simple_poly_of hn (matrix_poly hn hw))) (identifies_poly_of hn (NatPoly.decode (bits_poly hn hw) (NatPoly.max (NatPoly.const 2) hn)) hc (matrix_poly hn hw)) theorem decideWord_poly : BoolPoly (fun S : Fin 2 → Word => decideWord (S 1).length (S 0)) := by unfold decideWord apply BoolPoly.anyRange (NatPoly.succ (NatPoly.var 0)) apply atOrder_poly <;> word_poly end DimensionDecider /-- Membership for the literal parser, all-mates identification, and finite-alphabet tape model. -/ theorem dimensionLanguage_inEXPTIME : InEXPTIME dimensionLanguage := by apply WordExpressions.inEXPTIME_of_budget 64 6 DimensionDecider.decideWord_poly intro w simp only [Matrix.cons_val_zero, Matrix.cons_val_one, WordExpressions.unary, List.length_replicate] exact DimensionDecider.decideWord_correct w end WLIdentification end OAI