math / data /lean /OAI /Computability /Superstring /GroupBridge.lean
introvoyz045's picture
Migrated from GitHub
4a56cd6 verified
Raw History Blame Contribute Delete
16.1 kB
import OAI.Computability.Superstring.LexCode
namespace OAI
namespace Superstring.ThreadArray
open BitCode Hierarchical LayerArray
variable {Ξ± : Type} [Coded Ξ±] [DecidableEq Ξ±] [Inhabited Ξ±]
def data (K : Code Ξ±) (g : β„•) : GroupData Ξ± :=
(layerWord K g,(List.range (aligned K g).length).map (layerExits K g))
@[fun_prop] theorem poly_data : Poly (fun q : Code Ξ± Γ— β„• => data q.1 q.2) := by
have hm : Poly (fun q : Code Ξ± Γ— β„• => (List.range (aligned q.1 q.2).length).map (layerExits q.1 q.2)) :=
BitCode.Poly.mapFn (f := fun q : (Code Ξ± Γ— β„•) Γ— β„• => layerExits q.1.1 q.1.2 q.2) (by fun_prop) (by fun_prop)
unfold data
fun_prop
theorem data_represents {K : Code Ξ±} {V : Set (List Ξ±)}
{C : Threading V (Fin K.1.length)} (h : Represents K C) (closed : SubstringClosed V)
(g : Fin K.1.length) (j : Fin (Fintype.card (C.Aligned (C.text g)))) :
LayerArray.Represents (data K g.val).1 (exits (data K g.val) j.val)
(C.layer closed (period_spec h g).1.periodic j) := by
have he : exits (data K g.val) j.val = layerExits K g.val j.val := by
have hj : j.val < (aligned K g.val).length := by rw [aligned_length h]; exact j.isLt
simp only [exits,data,List.getElem?_map,List.getElem?_range hj,Option.map_some,Option.getD_some]
refine ⟨text_layerWord h g,length_layerWord h g,?_⟩
Β· funext x
rw [he,exit_layerExits K g.val j.val (period_spec h g).1.positive,exit_eq h closed]
theorem data_size {K : Code Ξ±} {V : Set (List Ξ±)}
{C : Threading V (Fin K.1.length)} (h : Represents K C) (g : Fin K.1.length) :
(data K g.val).2.length = Fintype.card (C.Aligned (C.text g)) := by
simp only [data,List.length_map,List.length_range,aligned_length h]
omit [Coded Ξ±] in
theorem aligned_length_le (K : Code Ξ±) (g : β„•) : (aligned K g).length ≀ K.1.length := by
exact (List.length_filter_le ..).trans_eq List.length_range
omit [Coded Ξ±] in
theorem letters_mem {K : Code Ξ±} {V : Set (List Ξ±)}
{C : Threading V (Fin K.1.length)} (h : Represents K C) (closed : SubstringClosed V)
(g : Fin K.1.length) (x : β„€) : [C.text g x] ∈ V := by
exact (C.layer closed (period_spec h g).1.periodic
⟨0,Fintype.card_pos_iff.mpr ⟨⟨g,rfl⟩⟩⟩).letters closed x
end Superstring.ThreadArray
namespace Superstring.UpperCode
open BitCode Hierarchical WalkCode LayerArray
variable {Ξ± : Type} [Inhabited Ξ±] [DecidableEq Ξ±]
abbrev Book (Ξ± : Type) := List (List Ξ± Γ— List β„•)
def lookup (B : Book Ξ±) (i : β„•) : List Ξ± Γ— List β„• := B[i]?.getD ([],[])
def upper (m cut : β„•) : List β„• := (List.range m).filter (lessEq cut)
@[simp] theorem mem_upper (m cut i : β„•) : i ∈ upper m cut ↔ cut ≀ i ∧ i < m := by
simp only [upper,List.mem_filter,List.mem_range,lessEq,decide_eq_true_eq,and_comm]
theorem length_upper (m cut : β„•) : (upper m cut).length = m-cut := by
have he : (upper m cut).toFinset = Finset.Ico cut m := by ext i; simp
have hn : (upper m cut).Nodup := List.Nodup.filter _ List.nodup_range
rw [← List.toFinset_card_of_nodup hn,he,Nat.card_Ico]
theorem upper_filter {m cut lo : β„•} (h : cut ≀ lo) :
(upper m cut).filter (lessEq lo) = upper m lo := by
simp only [upper,List.filter_filter]
apply List.filter_congr
intro i _
apply Bool.eq_iff_iff.mpr
simp only [Bool.and_eq_true,lessEq,decide_eq_true_eq]
omega
abbrev Param := β„• Γ— β„• Γ— RecordCode.Code
def params (B : Book Ξ±) (ids : List β„•) (m M : β„•) (t : β„€) : List Param :=
(List.range (m+1)).flatMap (fun k => ids.flatMap (fun i =>
(RecordCode.pool (lookup B i).1.length M t).map (fun r => (k,i,r))))
def test (D : GroupData Ξ±) (B : Book Ξ±) (cut : β„•) (t : β„€) (v : Param) : Bool :=
let m := D.2.length
let p := D.1.length
let k := v.1
let T := lookup B v.2.1
let r := v.2.2
less 0 k && lessEq k (m-cut) &&
RecordCode.check D.1 T.1 T.2 r &&
decide (RecordCode.start r ≀ t) && decide (t < RecordCode.stop r) &&
decide (GeoCode.first (exits D (m-1)) t ≀ RecordCode.stop r+(k*p : β„•)) &&
decide (GeoCode.first (exits D (m-1)) t-GeoCode.exit (exits D (m-k)) t ≀ ((k-1)*p : β„•)) &&
((upper m cut).filter (fun i => less i (m-k))).all
(fun i => decide (GeoCode.first (exits D i) (RecordCode.start r) ≀ RecordCode.stop r))
-- A foreign-text filter is deliberately separate: its equality test uses only
-- a common product period, not a potentially unbounded infinite stream.
def choose (D : GroupData Ξ±) (B : Book Ξ±) (ids : List β„•) (cut M : β„•) (t : β„€)
(foreign : Param β†’ Bool) : Param :=
((params B ids D.2.length M t).filter (fun v => test D B cut t v && foreign v)).headD (0,0,0,0,0)
omit [Inhabited Ξ±] [DecidableEq Ξ±] in
theorem params_mem {B : Book Ξ±} {ids : List β„•} {m M k i : β„•} {t : β„€} {r : RecordCode.Code}
(hk : k ≀ m) (hi : i ∈ ids) (hr : r ∈ RecordCode.pool (lookup B i).1.length M t) :
(k,i,r) ∈ params B ids m M t := by
exact List.mem_flatMap.mpr ⟨k,List.mem_range.mpr (by omega),List.mem_flatMap.mpr
⟨i,hi,List.mem_map.mpr ⟨r,hr,rfl⟩⟩⟩
theorem choose_spec (D : GroupData Ξ±) (B : Book Ξ±) (ids : List β„•) (cut M : β„•) (t : β„€)
(foreign : Param β†’ Bool)
(hex : βˆƒ v ∈ params B ids D.2.length M t, test D B cut t v = true ∧ foreign v = true) :
let v := choose D B ids cut M t foreign
v ∈ params B ids D.2.length M t ∧ test D B cut t v = true ∧ foreign v = true := by
let vs := (params B ids D.2.length M t).filter (fun v => test D B cut t v && foreign v)
have hn : vs β‰  [] := by
obtain ⟨v,hv,ht,hf⟩ := hex
have hm : v ∈ vs := List.mem_filter.mpr ⟨hv,by simp only [ht,hf,Bool.and_true]⟩
exact List.ne_nil_of_mem hm
have hm : vs.headD (0,0,0,0,0) ∈ vs := by
cases he : vs with
| nil => exact False.elim (hn he)
| cons a as => simp
simpa only [vs,List.mem_filter,Bool.and_eq_true,choose] using hm
variable [Coded Ξ±]
omit [Inhabited Ξ±] [DecidableEq Ξ±] in
@[fun_prop] theorem poly_lookup : Poly (fun q : Book Ξ± Γ— β„• => lookup q.1 q.2) := by
exact BitCode.Poly.getD ([],[])
@[fun_prop] theorem poly_upper : Poly (fun q : β„• Γ— β„• => upper q.1 q.2) := by
unfold upper
exact BitCode.Poly.filterFn (p := fun q : (β„• Γ— β„•) Γ— β„• => lessEq q.1.2 q.2) (by fun_prop) (by fun_prop)
abbrev ParamsInput (Ξ± : Type) := Book Ξ± Γ— List β„• Γ— β„• Γ— β„• Γ— β„€
omit [Inhabited Ξ±] [DecidableEq Ξ±] in
@[fun_prop] theorem poly_params : Poly (fun q : ParamsInput Ξ± => params q.1 q.2.1 q.2.2.1 q.2.2.2.1 q.2.2.2.2) := by
unfold params
apply BitCode.Poly.flatMapFn (f := fun q : ParamsInput Ξ± Γ— β„• => q.1.2.1.flatMap (fun i =>
(RecordCode.pool (lookup q.1.1 i).1.length q.1.2.2.2.1 q.1.2.2.2.2).map (fun r => (q.2,i,r))))
Β· apply BitCode.Poly.flatMapFn (f := fun q : (ParamsInput Ξ± Γ— β„•) Γ— β„• =>
(RecordCode.pool (lookup q.1.1.1 q.2).1.length q.1.1.2.2.2.1 q.1.1.2.2.2.2).map (fun r => (q.1.2,q.2,r)))
Β· exact BitCode.Poly.mapFn (f := fun q : ((ParamsInput Ξ± Γ— β„•) Γ— β„•) Γ— RecordCode.Code =>
(q.1.1.2,q.1.2,q.2)) (by fun_prop) (by fun_prop)
Β· fun_prop
Β· fun_prop
abbrev TestInput (Ξ± : Type) := GroupData Ξ± Γ— Book Ξ± Γ— β„• Γ— β„€ Γ— Param
instance codedTestInput : Coded (TestInput Ξ±) := codedProd
@[fun_prop] theorem poly_test : Poly (fun q : TestInput Ξ± => test q.1 q.2.1 q.2.2.1 q.2.2.2.1 q.2.2.2.2) := by
unfold test
dsimp only
have ha : Poly (fun q : TestInput Ξ± =>
((upper q.1.2.length q.2.2.1).filter (fun i => less i (q.1.2.length-q.2.2.2.2.1))).all
(fun i => decide (GeoCode.first (exits q.1 i) (RecordCode.start q.2.2.2.2.2.2) ≀ RecordCode.stop q.2.2.2.2.2.2))) := by
apply BitCode.Poly.allFn (p := fun q : TestInput Ξ± Γ— β„• =>
decide (GeoCode.first (exits q.1.1 q.2) (RecordCode.start q.1.2.2.2.2.2.2) ≀ RecordCode.stop q.1.2.2.2.2.2.2))
Β· unfold RecordCode.start RecordCode.stop
fun_prop
Β· exact BitCode.Poly.filterFn (p := fun q : TestInput Ξ± Γ— β„• => less q.2 (q.1.1.2.length-q.1.2.2.2.2.1)) (by fun_prop) (by fun_prop)
have hr : Poly (fun q : TestInput Ξ± => RecordCode.check q.1.1 (lookup q.2.1 q.2.2.2.2.2.1).1 (lookup q.2.1 q.2.2.2.2.2.1).2 q.2.2.2.2.2.2) :=
RecordCode.poly_check.comp (f := fun q : TestInput Ξ± => ((q.1.1,(lookup q.2.1 q.2.2.2.2.2.1).1,(lookup q.2.1 q.2.2.2.2.2.1).2),q.2.2.2.2.2.2)) (by fun_prop)
unfold RecordCode.start RecordCode.stop at *
fun_prop
end Superstring.UpperCode
namespace Superstring.PeriodWord
open BitCode Hierarchical
variable {Ξ± : Type} [Inhabited Ξ±] [DecidableEq Ξ±]
def different (U W : List Ξ±) (d : β„€) : Bool :=
!equal (window U 0 (U.length*W.length : β„•)) (window W d (d+(U.length*W.length : β„•)))
theorem different_iff (U W : List Ξ±) (d : β„€) (hU : 0 < U.length) (hW : 0 < W.length) :
different U W d = true ↔ text U β‰  fun x => text W (x+d) := by
have hm : 0 < U.length*W.length := Nat.mul_pos hU hW
have perU : Function.Periodic (text U) ((U.length*W.length : β„•) : β„€) := by
simpa only [nsmul_eq_mul,Nat.cast_mul,mul_comm] using (periodic U).nsmul W.length
have perW : Function.Periodic (fun x => text W (x+d)) ((U.length*W.length : β„•) : β„€) := by
have hp := (periodic W).nsmul U.length
intro x
simpa only [nsmul_eq_mul,Nat.cast_mul,add_right_comm] using hp (x+d)
have he : window U 0 (U.length*W.length : β„•) = window W d (d+(U.length*W.length : β„•)) ↔
text U = fun x => text W (x+d) := by
rw [window_eq,window_eq]
have hr := textWord_reindex (text W) d 0 (U.length*W.length : β„•)
simp only [zero_add] at hr
rw [add_comm d,← hr]
constructor
Β· intro h
funext x
let r := x % ((U.length*W.length : β„•) : β„€)
have hr0 : 0 ≀ r := Int.emod_nonneg _ (by omega)
have hrm : r < (U.length*W.length : β„•) := Int.emod_lt_of_pos _ (by omega)
have hri : r.toNat < U.length*W.length := by omega
have ha := congrArg (fun w : List Ξ± => w[r.toNat]?) h
simp only [textWord,List.getElem?_ofFn] at ha
have hlen : (((U.length*W.length : β„•) : β„€)-0).toNat = U.length*W.length := by omega
simp only [hlen,hri,↓reduceDIte,zero_add,Option.some.injEq,Int.toNat_of_nonneg hr0] at ha
have hu := periodic_emod (text U) ((U.length*W.length : β„•) : β„€) perU x
have hw := periodic_emod (fun x => text W (x+d)) ((U.length*W.length : β„•) : β„€) perW x
exact hu.symm.trans (ha.trans hw)
Β· intro h
rw [h]
simpa only [different,equal,Bool.not_eq_true',decide_eq_false_iff_not] using not_congr he
variable [Coded Ξ±]
@[fun_prop] theorem poly_different : Poly (fun q : List Ξ± Γ— List Ξ± Γ— β„€ => different q.1 q.2.1 q.2.2) := by
unfold different
fun_prop
end Superstring.PeriodWord
namespace Superstring.UpperCode
open BitCode Hierarchical LayerArray
variable {Ξ± G : Type} [Inhabited Ξ±] [LinearOrder Ξ±] {V : Set (List Ξ±)}
def foreign (D : GroupData Ξ±) (B : Book Ξ±) (v : Param) : Bool :=
PeriodWord.different D.1 (lookup B v.2.1).1 (RecordCode.offset v.2.2)
theorem supply_params (C : LayerCollection V G) (g : G) (supply : C.RecordSupply)
(D : GroupData Ξ±) (B : Book Ξ±) (ids : List β„•) (encode : C.Id β†’ β„•)
(hsize : D.2.length = C.size g)
(hD : βˆ€ i : Fin (C.size g), Represents D.1 (exits D i.val) (C.layer g i))
(hB : βˆ€ i : C.Id, Represents (lookup B (encode i)).1 (lookup B (encode i)).2 (C.layerAt i))
(hids : βˆ€ i, encode i ∈ ids) (M : β„•) (len : βˆ€ w ∈ V, w.length ≀ M)
(cut : β„•) (hcut : cut < C.size g)
(he : ¬ (C.layer g ⟨C.size g-1,by have := C.nonempty g; omega⟩).first (C.distinguished g) - C.distinguished g
≀ ((C.size g-cut)*C.period g : β„•))
(hb : Β¬ βˆƒ base : Fin (C.size g), base.val+1=cut ∧
(C.layer g ⟨C.size g-1,by have := C.nonempty g; omega⟩).first (C.distinguished g) -
(C.layer g base).last (C.distinguished g) ≀ ((C.size g-cut)*C.period g : β„•)) :
βˆƒ v ∈ params B ids D.2.length M (C.distinguished g),
test D B cut (C.distinguished g) v = true ∧ foreign D B v = true := by
classical
let n := C.size g-cut
let top : Fin (C.size g) := ⟨C.size g-1,by have := C.nonempty g; omega⟩
let t := C.distinguished g
let H := (C.layer g top).first t
have hn : 0 < n := by dsimp only [n]; omega
have hnm : n ≀ C.size g := Nat.sub_le ..
have hz : Monotone (fun i : Fin (C.size g) => (C.layer g i).last t) :=
fun i j hij => C.ordered g i j hij t
have hstopn : (Processing.qualifying (fun i => (C.layer g i).last t) H (C.period g) n).card ≀ n := by
by_cases hzero : cut = 0
Β· have hh := Finset.card_le_univ (Processing.qualifying (fun i => (C.layer g i).last t) H (C.period g) n)
simpa only [Fintype.card_fin,n,hzero,Nat.sub_zero] using hh
· let base : Fin (C.size g) := ⟨cut-1,by omega⟩
have hfail : (C.layer g base).last t < H-(n*C.period g : β„•) := by
by_contra hh
apply hb
exact ⟨base,by dsimp only [base]; omega,by dsimp only [H,t,n,top] at hh ⊒; omega⟩
have hh := Processing.qualifying_bound _ hz H (C.period g) n base hfail
change _ ≀ C.size g-1-(cut-1) at hh
dsimp only [n] at hh ⊒
omega
obtain ⟨k,hk,hkn,hstop,hsharp⟩ := Processing.choose_k hn hnm _ hz H
(C.period g) ((C.layer g top).first_le_last t) hstopn
have hkm : k ≀ C.size g := hkn.trans hnm
have hmul : k*C.period g ≀ n*C.period g := Nat.mul_le_mul_right _ hkn
have hhard : t+(k*C.period g : β„•) < (C.layer g top).first t := by
dsimp only [t,top] at *
dsimp only [n] at hmul
omega
obtain ⟨d,R,ha,hh,hd⟩ := supply g top t k hk hhard hstop
have ht : t < R.stop := by omega
let R' := R.normalize (C.periodic d.1)
let r : RecordCode.Code := (R'.offset,R.start,R.stop)
have htext := (hD top).text_eq
have hp := (hD top).length_eq
have hr : r ∈ RecordCode.pool (lookup B (encode d)).1.length M t := by
rw [(hB d).length_eq]
exact RecordCode.normalized_mem_pool (C.positive d.1) (C.periodic d.1) R ha ht len
refine ⟨(k,encode d,r),params_mem (by omega) (hids d) hr,?_,?_⟩
Β· have hc : RecordCode.check D.1 (lookup B (encode d)).1 (lookup B (encode d)).2 r = true :=
RecordCode.check_record htext (hB d) R'
have htop : GeoCode.first (exits D (D.2.length-1)) t = (C.layer g top).first t := by
rw [hsize]
exact congrFun (hD top).exit_eq (t-1)
have hlo : GeoCode.exit (exits D (D.2.length-k)) t = (C.layer g ⟨C.size g-k,by omega⟩).last t := by
rw [hsize]
exact congrFun (hD ⟨C.size g-k,by omega⟩).exit_eq t
have hall : ((upper D.2.length cut).filter (fun i => less i (D.2.length-k))).all
(fun i => decide (GeoCode.first (exits D i) R.start ≀ R.stop)) = true := by
apply List.all_eq_true.mpr
intro i hi
have hi' : cut ≀ i ∧ i < C.size g ∧ i < C.size g-k := by
simpa only [List.mem_filter,mem_upper,less,decide_eq_true_eq,hsize,and_assoc] using hi
let j : Fin (C.size g) := ⟨i,hi'.2.1⟩
have hj := Processing.below_stopped _ hz H (C.period g) k hstop j hi'.2.2
have hfa := (C.layer g j).first_mono ha
have hfl := (C.layer g j).first_le_last t
have hg : GeoCode.first (exits D i) R.start = (C.layer g j).first R.start :=
congrFun (hD j).exit_eq (R.start-1)
apply decide_eq_true
rw [hg]
dsimp only [H] at hj
omega
simp only [test,less,lessEq,Bool.and_eq_true,decide_eq_true_eq]
refine ⟨⟨⟨⟨⟨⟨⟨hk,by omega⟩,hc⟩,ha⟩,ht⟩,?_⟩,?_⟩,?_⟩
Β· change GeoCode.first _ t ≀ R.stop+(k*D.1.length : β„•)
rw [htop,hp]
omega
Β· change GeoCode.first _ t-GeoCode.exit _ t ≀ ((k-1)*D.1.length : β„•)
rw [htop,hlo,hp]
dsimp only [H] at hsharp
omega
Β· exact hall
Β· unfold foreign
apply (PeriodWord.different_iff _ _ _ (by rw [hp]; exact C.positive g)
(by rw [(hB d).length_eq]; exact C.positive d.1)).mpr
rw [htext,(hB d).text_eq]
change C.text g β‰  fun x => C.text d.1 (x+R'.offset)
rw [R.normalize_text (C.periodic d.1)]
exact hd
end Superstring.UpperCode
end OAI