import OAI.Computability.StarHeight.RegularLanguages namespace OAI namespace GeneralizedStarHeight universe u universe uAlphabet uAlphabet2 uAlphabet3 uAlphabet4 uAlphabet5 uAlphabet6 uAlphabet7 uJ universe uC namespace StreamSplice open LocalMarkers WordIntervals PeriodicSummary variable {Alphabet : Type uAlphabet} lemma periodic_of_dvd {v : ℤ → Alphabet} {p δ : ℤ} (h : Function.Periodic v p) (hd : p∣δ) : Function.Periodic v δ := by obtain ⟨k,rfl⟩ := hd simpa only [Int.cast_id,mul_comm] using h.int_mul k lemma periodic_pattern {p : ℕ} [NeZero p] {v : ℤ → Alphabet} (h : Function.Periodic v (p:ℤ)) (N : ℤ) (i : ℕ) : v (N+(((i:ZMod p).val:ℕ):ℤ))=v (N+i) := by have he := ZMod.val_intCast (n := p) (i:ℤ) simp only [Int.cast_natCast] at he rw [he] exact (periodic_remainder h N (N+i) |>.trans (by rw [add_sub_cancel_left])).symm lemma fits_realizes {p : ℕ} [NeZero p] {v f : ℤ → Alphabet} {N : ℤ} {w : List Alphabet} (hp : Function.Periodic v (p:ℤ)) (hf : Realizes w N f) : Fits (fun q : ZMod p => v (N+q.val)) 0 w ↔ AgreesOn f v N (N+w.length) := by rw [fits_iff] simp only [zero_add] constructor · intro h z hz hz' let i : Fin w.length := ⟨(z-N).toNat,by omega⟩ have hi : N+(i.val:ℤ)=z := by dsimp [i];omega rw [←hi,hf i,h i,periodic_pattern hp] · intro h i rw [←hf i,periodic_pattern hp] exact h _ (by omega) (by omega) namespace Frame variable {f f' : ℤ → Alphabet} {N : ℤ} {x y : List Alphabet} (H : Frame f f' N x y) include H lemma symm : Frame f' f N y x := by refine ⟨fun z hz => (H.left z hz).symm,H.newMiddle,H.oldMiddle,?_⟩ intro z hz have he := H.right (z-((x.length:ℤ)-y.length)) (by omega) rw [show z-((x.length:ℤ)-y.length)-((y.length:ℤ)-x.length)=z by ring] at he exact he.symm include H lemma agrees_periodic {p : ℕ} [NeZero p] {v : ℤ → Alphabet} (hp : Function.Periodic v (p:ℤ)) (hd : (p:ℤ) ∣ (y.length:ℤ)-x.length) (hm : PeriodicSummary.transition (p := p) (FreeMonoid.ofList x)= PeriodicSummary.transition (FreeMonoid.ofList y)) {z q : ℤ} (hz : z≤N) (hq : N+x.length≤q) (ha : AgreesOn f v z q) : AgreesOn f' v z (q+((y.length:ℤ)-x.length)) := by have hmidx : AgreesOn f v N (N+x.length) := by intro t ht ht' exact ha t (hz.trans ht) (ht'.trans_le hq) have hmidx' := (fits_realizes hp H.oldMiddle).mpr hmidx have hmidy := (fits_realizes hp H.newMiddle).mp ((fits_congr hm _ 0).mp hmidx') have hδ := periodic_of_dvd hp hd intro t ht ht' by_cases htN : t congr 1 <;> ring · rw [show b+((y.length:ℤ)-x.length)-tail=(b-tail)+((y.length:ℤ)-x.length) by ring] exact H.agrees_periodic hp hd hm (by omega) hb hmis.2.2 lemma endAt {C : CanonicalEpisodes.Parameters Alphabet} {z b : ℤ} (hvis : C.Visible (-N) N z) (hstrict : z+(C.d:ℤ)+(C.B-1)*C.S have hq := (EndSearch.search_some C.B_pos C.S_nonneg).mp hη have hw := (EndSearch.width_bounds (x := q-z) (a := C.d) C.B_pos C.S_nonneg).2 have hbd := (abs_le.mp (hq.1.2.trans hw)).2 rw [hη] at he change b=q+C.tail at he omega | none => rw [hη] at he exact H.endAt_none hvis.2.2.2 hb hd hm he end StreamSplice.Frame namespace CanonicalEpisodes.Parameters open LocalMarkers WordIntervals EpisodeEnd StreamSplice variable {Alphabet : Type uAlphabet3} (C : CanonicalEpisodes.Parameters Alphabet) lemma exists_horizon (z : ℤ) : ∃ N : ℕ, C.Visible (-(N:ℤ)) N z ∧ z+(C.d:ℤ)+(C.B-1)*C.S.trans MulOpposite.opEquiv) apply FiniteRecognition.regular_sandwich T N C.tail.toNat intro a b x y ha hb he have hh : C.tail.toNat=C.tail := Int.toNat_of_nonneg C.tail_pos.le have trans : ∀ (x y : List Alphabet), T (FreeMonoid.ofList x)=T (FreeMonoid.ofList y) → a++x++b∈C.complete lag → a++y++b∈C.complete lag := by intro x y he hw obtain ⟨f,hf,hv,hend⟩ := hw let f' := splice f N x y have H : Frame f f' N x y := by simpa only [ha] using splice_frame hf y have hnew := H.endAt hN hs (by simp only [List.length_append,Nat.cast_add,ha,hb];omega) (PeriodicSummary.lengthMod_dvd (congrArg Prod.fst he)) (congrArg Prod.snd he) hend refine ⟨f',?_,?_,?_⟩ · simpa only [f',ha] using splice_realizes hf y · exact C.visible_long hN (by simp only [List.length_append,Nat.cast_add,ha,hb];omega) hv.1 hv.2.2.1 · convert hnew using 1 simp only [List.length_append,Nat.cast_add] omega exact ⟨trans x y he,trans y x he.symm⟩ end CanonicalEpisodes.Parameters namespace EpisodeAlgebra variable {Alphabet : Type uAlphabet4} def remainder (E : Language Alphabet) : Language Alphabet := {w | ∀ e∈E, ¬ e <+: w} lemma remainder_eq (E : Language Alphabet) : remainder E=(E*⊤)ᶜ := by ext w constructor · intro h hw obtain ⟨e,he,v,_,rfl⟩ := hw exact h e he ⟨v,rfl⟩ · intro h e he ⟨v,hv⟩ exact h ⟨e,he,v,Set.mem_univ _,hv⟩ lemma factor_remainder {E : Language Alphabet} (hnil : []∉E) (w : List Alphabet) : ∃ es r, AllIn E es ∧ r∈remainder E ∧ w=es.flatten++r := by classical induction hlen : w.length using Nat.strong_induction_on generalizing w with | h n ih => by_cases h : w∈remainder E · exact ⟨[],w,by simp [AllIn],h,by simp⟩ · change ¬∀ e∈E, ¬ e <+: w at h push Not at h obtain ⟨e,he,hp⟩ := h obtain ⟨v,rfl⟩ := hp have hpos : 0 ?_,hr,?_⟩ · rcases List.mem_cons.mp ha with rfl|ha · exact he · exact hes a ha · simp only [List.flatten_cons,List.append_assoc,hv] end EpisodeAlgebra namespace EpisodeEnd open LocalMarkers variable {Alphabet : Type uAlphabet5} lemma firstMismatch_of_disagrees {d K : ℕ} {f v : ℤ → Alphabet} {z q : ℤ} (hv : ShortContinuation d K f z v) (hq : z≤q) (hm : f q≠v q) : ∃ q', q'≤q ∧ FirstMismatch f v z K q' := by rcases MarkerSearch.search_some_or_none {x | f x≠v x} z q with hn|⟨q',hq',hle⟩ · exact False.elim (hn q hm hq le_rfl) · refine ⟨q',hle,?_,hq'.1,?_⟩ · by_contra h exact hq'.1 (hv.2 q' hq'.2.1 (by omega)) · intro x hx hx' by_contra h exact (not_le_of_gt hx') (hq'.2.2 x h hx) end EpisodeEnd namespace CanonicalEpisodes.Parameters open LocalMarkers WordIntervals EpisodeEnd EpisodeAlgebra variable {Alphabet : Type uAlphabet6} (C : CanonicalEpisodes.Parameters Alphabet) lemma complete_prefix {lag b : ℤ} {w : List Alphabet} {f : ℤ → Alphabet} (hf : Realizes w 0 f) (hv : C.Visible 0 b (lag+C.offset)) (he : EndAt C.d C.K C.tail f (lag+C.offset) (C.small f (lag+C.offset)) b) (hb : b≤w.length) : ∃ e∈C.complete lag, e<+:w := by have hb0 : 0≤b := hv.2.2.1.trans (le_trans (by omega) hv.2.2.2) let e := w.take b.toNat have hl : (e.length:ℤ)=b := by simp only [e,List.length_take] omega refine ⟨e,⟨f,hf.prefix (List.take_prefix _ _),?_,?_⟩,List.take_prefix _ _⟩ · simpa only [hl] using hv · simpa only [hl] using he lemma remainder_middle [Nonempty Alphabet] (lag : ℤ) (hvisible : ∀ (f : ℤ → Alphabet) b, EndAt C.d C.K C.tail f (lag+C.offset) (C.small f (lag+C.offset)) b → C.Visible 0 b (lag+C.offset)) : ∃ N : ℕ, ∀ w∈remainder (C.complete lag), w.length≤N+C.tail.toNat ∨ ∃ (f v : ℤ → Alphabet) (p : ℕ), Realizes w 0 f ∧ N+C.tail.toNat≤w.length ∧ 0

rfl | some q => have hq := ((EndSearch.search_some C.B_pos C.S_nonneg).mp hη).1.2 have hwidth := (EndSearch.width_bounds (x := q-(lag+C.offset)) (a := C.d) C.B_pos C.S_nonneg).2 have hright := (abs_le.mp (hq.trans hwidth)).2 have he : EndAt C.d C.K C.tail f (lag+C.offset) (C.small f (lag+C.offset)) (q+C.tail) := by rw [hη];rfl obtain ⟨e,he,hp⟩ := C.complete_prefix hf (hvisible f _ he) he (by omega) exact False.elim (hw e he hp) obtain ⟨v,hv⟩ := EndSearch.absent_continuation C.rule (by nlinarith [C.seed_long]) C.B_pos C.S_nonneg hnone obtain ⟨p,hp,hpd,hper⟩ := hv.1 refine ⟨f,v,p,hf,by omega,hp,hpd.le,hper,?_⟩ intro q hq hq' by_contra hm have hzq : lag+C.offset≤q := by have := hN.2.2.2;omega obtain ⟨q',hle,hm'⟩ := firstMismatch_of_disagrees hv hzq hm have he : EndAt C.d C.K C.tail f (lag+C.offset) (C.small f (lag+C.offset)) (q'+C.tail) := by rw [hnone] exact ⟨v,hv,by simpa only [add_sub_cancel_right] using hm'⟩ obtain ⟨e,he,hpre⟩ := C.complete_prefix hf (hvisible f _ he) he (by omega) exact hw e he hpre lemma remainder_regular [Finite Alphabet] (lag : ℤ) : (remainder (C.complete lag)).IsRegular := by rw [remainder_eq] apply Language.IsRegular.compl apply FiniteRecognition.regular_mul (C.complete_regular lag) have ht : (⊤ : Language Alphabet)=(0 : Language Alphabet)ᶜ := by ext w change True ↔ ¬False simp rw [ht] exact Language.IsRegular.compl (FiniteRecognition.regular_finite Set.finite_empty) lemma remainder_height [Finite Alphabet] [Nonempty Alphabet] (lag : ℤ) (hvisible : ∀ (f : ℤ → Alphabet) b, EndAt C.d C.K C.tail f (lag+C.offset) (C.small f (lag+C.offset)) b → C.Visible 0 b (lag+C.offset)) : HasHeightAtMost (remainder (C.complete lag)) 1 := by obtain ⟨N,hN⟩ := C.remainder_middle lag hvisible exact PeriodicTemplates.regular_middle_height _ (C.remainder_regular lag) (N+C.tail.toNat) N C.tail.toNat C.d hN end CanonicalEpisodes.Parameters namespace ExceptionalOrigins.Parameters open LocalMarkers variable {Alphabet : Type uAlphabet7} {J : Type uJ} {C : Type uC} (A : ExceptionalOrigins.Parameters Alphabet J C) lemma residue_eq_of_dvd {x y : ℤ} (h : (A.B:ℤ)∣y-x) : A.residue x=A.residue y := by apply Fin.ext have he : x%A.B=y%A.B := Int.modEq_iff_dvd.mpr h exact congrArg Int.toNat he lemma exists_horizon [Fintype J] (D : ℕ) : ∃ N : ℕ, D omega · intro j have hj : |A.offset j|≤U := SourceSchedule.abs_le_absoluteBound A.offset j have hH' : |A.H (A.residue (Q-t))|≤H := SourceSchedule.abs_le_absoluteBound A.H _ have hj' := abs_le.mp hj have hH'' := abs_le.mp hH' have hmax : max A.ext A.w₀≤|A.ext|+|A.w₀| := by apply max_le <;> omega constructor <;> omega lemma Failed_phase_congr {s b t Q Q' : ℤ} {f g : ℤ → Alphabet} (hw₀ : 0≤A.w₀) (hv : A.Visible s b t Q) (he : AgreesOn f g s b) (hφ : A.φ (Q-t)=A.φ (Q'-t)) (hu : A.residue (Q-t)=A.residue (Q'-t)) : A.Failed f t Q ↔ A.Failed g t Q' := by rw [A.Failed_congr hw₀ hv he] simp only [Failed,Star,hφ,hu] end ExceptionalOrigins.Parameters end GeneralizedStarHeight end OAI