import OAI.Computability.StarHeight.PeriodicSplicing namespace OAI namespace GeneralizedStarHeight universe u universe uAlphabet uJ uC uAlphabet2 uJ2 uC2 uAlphabet3 uAlphabet4 universe uJ3 uC3 uAlphabet5 uM uAlphabet6 uJ4 uC4 uP universe uAlphabet7 uM2 uJ5 uC5 uAlphabet8 uAlphabet9 uJ6 uC6 namespace StreamSplice.Frame open LocalMarkers WordIntervals variable {Alphabet : Type uAlphabet} {J : Type uJ} {C : Type uC} {f f' : ℤ → Alphabet} {N : ℤ} {x y : List Alphabet} (H : Frame f f' N x y) (A : ExceptionalOrigins.Parameters Alphabet J C) include H lemma anchor_phase {t b : ℤ} (hv : A.endRule.Visible (-N) N (t+A.endRule.offset)) (hd : (A.B:ℤ) ∣ (y.length:ℤ)-x.length) (hφ : A.φ ((y.length:ℤ)-x.length)=0) : A.residue (A.anchor f (b-A.endRule.tail) t-t)= A.residue (A.anchor f' (b+((y.length:ℤ)-x.length)-A.endRule.tail) t-t) ∧ A.φ (A.anchor f (b-A.endRule.tail) t-t)= A.φ (A.anchor f' (b+((y.length:ℤ)-x.length)-A.endRule.tail) t-t) := by have hs := (A.endRule.searches_congr hv (H.agrees_left (le_refl N))).2 unfold ExceptionalOrigins.Parameters.anchor rw [←hs] cases hζ : A.endRule.large f (t+A.endRule.offset) with | some q => simp only [Option.getD_some,and_self] | none => simp only [Option.getD_none] constructor · apply A.residue_eq_of_dvd convert hd using 1 ring · rw [show b+((y.length:ℤ)-x.length)-A.endRule.tail-t= (b-A.endRule.tail-t)+((y.length:ℤ)-x.length) by ring,map_add,hφ,add_zero] lemma failed {t b : ℤ} (hw₀ : 0≤A.w₀) (hv : A.Visible (-N) N t (A.anchor f (b-A.endRule.tail) t)) (hd : (A.B:ℤ) ∣ (y.length:ℤ)-x.length) (hφ : A.φ ((y.length:ℤ)-x.length)=0) : A.Failed f t (A.anchor f (b-A.endRule.tail) t) ↔ A.Failed f' t (A.anchor f' (b+((y.length:ℤ)-x.length)-A.endRule.tail) t) := by have hphase := H.anchor_phase A (b := b) hv.base hd hφ exact A.Failed_phase_congr hw₀ hv (H.agrees_left (le_refl N)) hphase.2 hphase.1 end StreamSplice.Frame namespace ExceptionalOrigins.Parameters open LocalMarkers WordIntervals StreamSplice variable {Alphabet : Type uAlphabet2} {J : Type uJ2} {C : Type uC2} (A : ExceptionalOrigins.Parameters Alphabet J C) def failedLanguage (t : ℤ) : Language Alphabet := {w | ∃ f : ℤ → Alphabet, Realizes w 0 f ∧ A.Failed f t (A.anchor f (w.length-A.endRule.tail) t)} lemma failed_regular [Finite Alphabet] [Fintype J] (n : ℕ) (hn : 0 omega lemma InnerVisible.mono {s b s' b' t : ℤ} {j : J} {u : Fin A.B} (hv : A.InnerVisible s b t j u) (hs : s' ≤ s) (hb : b ≤ b') : A.InnerVisible s' b' t j u := ⟨hs.trans hv.1,hv.2.trans hb⟩ lemma Visible.from_bounds {s b s' N b' t Q Q' : ℤ} (hv : A.Visible s b t Q) (hN : A.Visible s' N t Q') (hb : N ≤ b') : A.Visible s b' t Q' := by refine ⟨⟨hv.base.1,hN.base.2.1.trans hb,hv.base.2.2.1,hN.base.2.2.2.trans hb⟩,?_,?_⟩ · intro v hv' hw exact ⟨(hv.ends v hv' hw).1,(hN.ends v hv' hw).2.trans hb⟩ · intro j exact ⟨(hv.inner j).1,(hN.inner j).2.trans hb⟩ end ExceptionalOrigins.Parameters namespace StreamSplice.Frame open LocalMarkers WordIntervals BoundarySnapshot variable {Alphabet : Type uAlphabet5} {M : Type uM} [Monoid M] {f f' : ℤ → Alphabet} {N : ℤ} {x y : List Alphabet} (H : Frame f f' N x y) (T : FreeMonoid Alphabet →* M) include H lemma factors {g : ℕ} {s b : ℤ} (hb : N+x.length≤b) (hT : T (FreeMonoid.ofList x)=T (FreeMonoid.ofList y)) (search : Fin (g+1) → Option ℤ) (hsearch : ∀ i q, search i=some q → q≤N) : BoundarySnapshot.factors T f' s (b+((y.length:ℤ)-x.length)) search= BoundarySnapshot.factors T f s b search := by classical have hbN : N≤b := by omega have hbN' : N≤b+((y.length:ℤ)-x.length) := by omega have hp : Present search s b ↔ Present search s (b+((y.length:ℤ)-x.length)) := by constructor · rintro ⟨p,hp,hm,hs,_⟩ exact ⟨p,hp,hm,hs,(hsearch _ _ (hp _)).trans hbN'⟩ · rintro ⟨p,hp,hm,hs,_⟩ exact ⟨p,hp,hm,hs,(hsearch _ _ (hp _)).trans hbN⟩ by_cases hpres : Present search s b · obtain ⟨p,hp',hm,hs',hb'⟩ := hpres rw [factors_present T f hp' hm hs' hb', factors_present T f' hp' hm hs' ((hsearch _ _ (hp' _)).trans hbN')] congr 2 · exact (product_congr T hs' (H.agrees_left (hsearch _ _ (hp' _)))).symm · funext i exact (product_congr T (hm (Fin.castSucc_le_succ i)) (H.agrees_left (hsearch _ _ (hp' _)))).symm · exact H.product T hT (hsearch _ _ (hp' _)) hb · simp only [BoundarySnapshot.factors,dite_eq_right hpres,dite_eq_right (mt hp.mpr hpres)] lemma later {g : ℕ} {s b : ℤ} (hs : s≤N) (hb : N+x.length≤b) (hT : T (FreeMonoid.ofList x)=T (FreeMonoid.ofList y)) (search : Fin (g+1) → Option ℤ) (hsearch : ∀ i q, search i=some q → q≤N) : BoundarySnapshot.later T f' s (b+((y.length:ℤ)-x.length)) search= BoundarySnapshot.later T f s b search := by apply BoundarySnapshot.later_ext · exact H.product T hT hs hb · exact H.factors T hb hT search hsearch end StreamSplice.Frame namespace StreamSplice.Frame open LocalMarkers WordIntervals variable {Alphabet : Type uAlphabet6} {J : Type uJ4} {C : Type uC4} {f f' : ℤ → Alphabet} {N : ℤ} {x y : List Alphabet} (H : Frame f f' N x y) include H lemma guard (E : CanonicalEpisodes.Parameters Alphabet) {O : Finset ℤ} {r k b : ℤ} (hN : ∀ t∈O, E.Visible (-N) N (t+E.offset) ∧ t+E.offset+(E.d:ℤ)+(E.B-1)*E.S E.searches_congr (hN t ht).1 (H.agrees_left (le_refl N)) refine ⟨hg.1,?_,?_,H.endAt (hN r hg.1).1 (hN r hg.1).2 hb hd hm hg.2.2.2⟩ · intro t ht have hv := hg.2.1 t ht have hv' := (hN t ht).1 exact ⟨hv.1,hv'.2.1.trans hbN,hv.2.2.1,hv'.2.2.2.trans hbN⟩ · intro t ht rw [←(hs t ht).1,←(hs r hg.1).1,←(hs t ht).2,←(hs r hg.1).2] exact hg.2.2.1 t ht lemma anchor_relative_phase (A : ExceptionalOrigins.Parameters Alphabet J C) {t b r : ℤ} (hv : A.endRule.Visible (-N) N (t+A.endRule.offset)) (hd : (A.B:ℤ)∣(y.length:ℤ)-x.length) (hφ : A.φ ((y.length:ℤ)-x.length)=0) : A.residue (A.anchor f (b-A.endRule.tail) t-r)= A.residue (A.anchor f' (b+((y.length:ℤ)-x.length)-A.endRule.tail) t-r) ∧ A.φ (A.anchor f (b-A.endRule.tail) t-r)= A.φ (A.anchor f' (b+((y.length:ℤ)-x.length)-A.endRule.tail) t-r) := by have hs := (A.endRule.searches_congr hv (H.agrees_left (le_refl N))).2 unfold ExceptionalOrigins.Parameters.anchor rw [←hs] cases A.endRule.large f (t+A.endRule.offset) with | some q => simp only [Option.getD_some,and_self] | none => simp only [Option.getD_none] constructor · apply A.residue_eq_of_dvd convert hd using 1 ring · rw [show b+((y.length:ℤ)-x.length)-A.endRule.tail-r= (b-A.endRule.tail-r)+((y.length:ℤ)-x.length) by ring,map_add,hφ,add_zero] lemma decoder (A : ExceptionalOrigins.Parameters Alphabet J C) {P : Type uP} {τ : P → ℤ} {reference decoded : P} {k b : ℤ} (hw₀ : 0≤A.w₀) (hN : ∀ t, A.Visible (-N) N (τ t) (A.anchor f (b-A.endRule.tail) (τ t))) (hN' : ∀ t Q, A.Visible (-N) N (τ t) Q) (hstrict : ∀ t, τ t+A.endRule.offset+(A.endRule.d:ℤ)+ (A.endRule.B-1)*A.endRule.S A.endRule.searches_congr (hN t).base (H.agrees_left (le_refl N)) have hfail := fun t => H.failed A hw₀ (hN t) hB hφ refine ⟨?_,?_,⟨(hfail decoded).mp ht.unique.1,?_⟩,?_⟩ · intro t exact ExceptionalOrigins.Parameters.Visible.from_bounds A (ht.visible t) (hN' t _) hbN · intro habsent have ha : ∃ i, A.endRule.large f (τ i+A.endRule.offset)=none := by obtain ⟨i,hi⟩ := habsent exact ⟨i,(hs i).2.trans hi⟩ have hg := ht.reference_guard ha refine ⟨fun i => (hs i).1.symm.trans (hg.1 i),?_⟩ exact H.endAt (hN reference).base (hstrict reference) hb hd hm hg.2 · intro i hi exact ht.unique.2 i ((hfail i).mpr hi) · exact H.endAt (hN decoded).base (hstrict decoded) hb hd hm ht.final_guard end StreamSplice.Frame namespace StreamSplice open LocalMarkers WordIntervals variable {Alphabet : Type uAlphabet7} {M : Type uM2} {J : Type uJ5} {C : Type uC5} [Monoid M] def wordTest (R : (ℤ → Alphabet) → ℤ → Prop) : Language Alphabet := {w | ∃ f, Realizes w 0 f ∧ R f w.length} structure SummaryEq (A : ExceptionalOrigins.Parameters Alphabet J C) (T : FreeMonoid Alphabet →* M) (x y : List Alphabet) : Prop where monoid_eq : T (FreeMonoid.ofList x)=T (FreeMonoid.ofList y) residue_dvd : (A.B:ℤ)∣(y.length:ℤ)-x.length period_dvd : (A.endRule.d.factorial:ℤ)∣(y.length:ℤ)-x.length matcher_eq : PeriodicSummary.transition (p := A.endRule.d.factorial) (FreeMonoid.ofList x)= PeriodicSummary.transition (FreeMonoid.ofList y) clock_eq : A.φ ((y.length:ℤ)-x.length)=0 lemma clock_eq_of_dvd (φ : ℤ →+ (C → RobustClock.Circle)) {n : ℕ} (hφ : φ (n:ℤ)=0) {δ : ℤ} (hδ : (n:ℤ)∣δ) : φ δ=0 := by obtain ⟨k,rfl⟩ := hδ rw [mul_comm,←Int.zsmul_eq_mul,map_zsmul,hφ,smul_zero] lemma wordTest_regular [Finite Alphabet] [Finite M] (A : ExceptionalOrigins.Parameters Alphabet J C) (T : FreeMonoid Alphabet →* M) (n : ℕ) (hn : 0.trans MulOpposite.opEquiv) let U := (T.prod (PeriodicSummary.lengthMod m)).prod (PeriodicSummary.transition (p := p)) apply FiniteRecognition.regular_sandwich U N A.endRule.tail.toNat intro a b x y ha hb he have ht : (A.endRule.tail.toNat:ℤ)=A.endRule.tail := Int.toNat_of_nonneg A.endRule.tail_pos.le have trans : ∀ (x y : List Alphabet), U (FreeMonoid.ofList x)=U (FreeMonoid.ofList y) → a++x++b∈wordTest R → a++y++b∈wordTest R := by intro x y he ⟨f,hf,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 hdiv := PeriodicSummary.lengthMod_dvd (congrArg (Prod.snd ∘ Prod.fst) he) have hB : (A.B:ℤ)∣(y.length:ℤ)-x.length := (show (A.B:ℤ)∣(m:ℤ) by exact_mod_cast dvd_mul_of_dvd_left (dvd_mul_right A.B n) p).trans hdiv have hn' : (n:ℤ)∣(y.length:ℤ)-x.length := (show (n:ℤ)∣(m:ℤ) by exact_mod_cast dvd_mul_of_dvd_left (dvd_mul_left n A.B) p).trans hdiv have hp' : (p:ℤ)∣(y.length:ℤ)-x.length := (show (p:ℤ)∣(m:ℤ) by exact_mod_cast dvd_mul_left p (A.B*n)).trans hdiv have hh := hc f f' _ x y H (by simp only [List.length_append,Nat.cast_add,ha,hb];omega) ⟨congrArg (Prod.fst ∘ Prod.fst) he,hB,hp',congrArg Prod.snd he,clock_eq_of_dvd A.φ hφ hn'⟩ hw refine ⟨f',by simpa only [ha] using splice_realizes hf y,?_⟩ have hl : ((a++y++b).length:ℤ)=((a++x++b).length:ℤ)+((y.length:ℤ)-x.length) := by simp only [List.length_append,Nat.cast_add] omega rw [hl] exact hh exact ⟨trans x y he,trans y x he.symm⟩ end StreamSplice namespace CanonicalEpisodes.Parameters open LocalMarkers WordIntervals EpisodeEnd variable {Alphabet : Type uAlphabet8} (C : CanonicalEpisodes.Parameters Alphabet) lemma ending_middle (L : Language Alphabet) (D : ℕ) (hL : ∀ w∈L, ∃ f t, Realizes w 0 f ∧ |t|≤(D:ℤ) ∧ EndAt C.d C.K C.tail f (t+C.offset) (C.small f (t+C.offset)) w.length) : ∃ N : ℕ, ∀ w∈L, w.length≤N+C.tail.toNat ∨ ∃ (f v : ℤ → Alphabet) (p : ℕ), Realizes w 0 f ∧ N+C.tail.toNat≤w.length ∧ 0

have hq := ((EndSearch.search_some C.B_pos C.S_nonneg).mp hη).1.2 have hwidth := (EndSearch.width_bounds (x := q-(t+C.offset)) (a := C.d) C.B_pos C.S_nonneg).2 have hright := (abs_le.mp (hq.trans hwidth)).2 rw [hη] at he change (w.length:ℤ)=q+C.tail at he have hs := (hN t ht).2 omega | none => rw [hη] at he obtain ⟨v,hv,hmis⟩ := he obtain ⟨p,hp,hpd,hper⟩ := hv.1 refine ⟨f,v,p,hf,by omega,hp,hpd.le,hper,?_⟩ intro z hz hz' have hseed := (hN t ht).1.2.2.2 exact hmis.2.2 z (by omega) (by omega) lemma ending_height [Finite Alphabet] (L : Language Alphabet) (hreg : L.IsRegular) (D : ℕ) (hL : ∀ w∈L, ∃ f t, Realizes w 0 f ∧ |t|≤(D:ℤ) ∧ EndAt C.d C.K C.tail f (t+C.offset) (C.small f (t+C.offset)) w.length) : HasHeightAtMost L 1 := by obtain ⟨N,hN⟩ := C.ending_middle L D hL exact PeriodicTemplates.regular_middle_height L hreg (N+C.tail.toNat) N C.tail.toNat C.d hN end CanonicalEpisodes.Parameters namespace ExceptionalOrigins.Parameters open LocalMarkers variable {Alphabet : Type uAlphabet9} {J : Type uJ6} {C : Type uC6} (A : ExceptionalOrigins.Parameters Alphabet J C) structure Inspection (D N : ℕ) : Prop where contains : D