import OAI.Computability.FourierTransform.BinaryFrames import OAI.Computability.FourierTransform.FourierIdentities namespace OAI /-! Layer words using the binary kernel copies and scaled-index layers. Exact-width by a closure property on indices touched (indices outside the width never used and are identity). Arrays modeled on ℕ. Monomial-slot representation doesn't need a permutation certificate; it works even for a "gather". -/ namespace PowerSaving.Slant open Binary Spectral Matrix Finset noncomputable section structure Stroke where ix : ℕ → ℕ dye : Line partner : ℕ → ℕ namespace Stroke def applyM (u : Stroke) (x : Line) : Line := fun i => u.dye i * x (u.ix i) def applyC (u : Stroke) (x : Line) : Line := fun i => if u.partner i = i then x i else ca * x i + cb * x (u.partner i) def send (u : Stroke) (x : Line) := u.applyC (u.applyM x) structure Sound (u : Stroke) (n : ℕ) : Prop where bdix : ∀ i < n, u.ix i < n bdp : ∀ i < n, u.partner i < n pair : ∀ i, u.partner (u.partner i)=i awayix : ∀ i, n ≤ i → u.ix i=i awayp : ∀ i, n ≤ i → u.partner i=i awayd : ∀ i, n ≤ i → u.dye i=1 namespace Sound variable {u : Stroke} {n n' : ℕ} lemma wider (h : u.Sound n) (h' : n ≤ n') : u.Sound n' := by refine ⟨fun i hi => ?_, fun i hi => ?_, h.pair, fun i hi => h.awayix i (by omega), fun i hi => h.awayp i (by omega), fun i hi => h.awayd i (by omega)⟩ · rcases lt_or_ge i n with g|g · exact (h.bdix i g).trans_le h' · rwa [h.awayix i g] · rcases lt_or_ge i n with g|g · exact (h.bdp i g).trans_le h' · rwa [h.awayp i g] theorem same (h : u.Sound n) (x : Line) (i : ℕ) (hi : n ≤ i) : u.send x i=x i := by simp [send,applyC,h.awayp i hi,applyM,h.awayix i hi,h.awayd i hi] theorem send_congr (h : u.Sound n) {x y : Line} (H : ∀ i 1,id⟩ @[simp] theorem send_unmark (x : Line) : unmark.send x=x := by ext i; simp [send,unmark,applyC,applyM] theorem sound_unmark (n : ℕ) : unmark.Sound n := ⟨fun _ hi => hi, fun _ hi => hi,fun _ => rfl,fun _ _ => rfl,fun _ _ => rfl,fun _ _ => rfl⟩ /-- A scaled lookup; used for monomial slots. -/ def plain (n : ℕ) (l : ℕ→ℕ) (d : Line) : Stroke := ⟨fun i => if i < n then l i else i, fun i => if i < n then d i else 1, id⟩ theorem plain_sound (n : ℕ) (l : ℕ→ℕ) (d : Line) (h : ∀ i ?_,fun i hi => hi,fun i => rfl,fun i hi => ?_, fun i hi => rfl,fun i hi => ?_⟩ <;> simp [plain, hi, h, Nat.not_lt.mpr] theorem plain_send (n : ℕ) (l : ℕ→ℕ) (d : Line) (x : Line) (i : ℕ) : (plain n l d).send x i=if i simp [send,plain,applyM,applyC,h] -- parallel sum def beside (p : ℕ) (u v : Stroke) : Stroke where ix i := if i

?_, fun i hi => ?_, fun i => ?_, fun i hi => ?_, fun i hi => ?_, fun i hi => ?_⟩ · unfold Stroke.beside; dsimp; split · rename_i h have := hu.bdix i h omega · have := hv.bdix (i-p) (by omega) omega · unfold Stroke.beside; dsimp; split · rename_i h have := hu.bdp i h omega · have := hv.bdp (i-p) (by omega) omega · by_cases h : i

x (p+i) theorem beside_left (p : ℕ) (u v : Stroke) (hu : u.Sound p) (x : Line) (i : ℕ) (hi : i x | t+1 => (p.atBeat t).send (reach p x t) def go (p : Reel) (x : Line) := p.reach x p.beats def Sound (p : Reel) (n : ℕ) : Prop := ∀ t (h t ht).wider g theorem still (h : u.Sound n) (x : Line) (i : ℕ) (hi : n ≤ i) : u.go x i=x i := by have H (t : ℕ) (ht : t ≤ u.beats) : u.reach x t i=x i := by induction t with | zero => rfl | succ t ih => rw [reach,(h t (by omega)).same _ i hi, ih (by omega)] exact H u.beats le_rfl theorem go_congr (h : u.Sound n) {x y : Line} (H : ∀ i exact H | succ t ih => exact (h t (by omega)).send_congr (ih (by omega)) exact G _ le_rfl end Sound def wait (d : ℕ) : Reel := ⟨d,fun _ => unmark⟩ @[simp] theorem go_wait (d : ℕ) (x : Line) : (wait d).go x=x := by have H (t : ℕ) : (wait d).reach x t=x := by induction t <;> simp_all [reach,wait] exact H _ theorem sound_wait (d n : ℕ) : (wait d).Sound n := fun _ _ => sound_unmark n def tick (s : Stroke) : Reel := ⟨1,fun _ => s⟩ @[simp] theorem go_tick (s : Stroke) (x : Line) : (tick s).go x=s.send x := rfl lemma sound_tick {n : ℕ} {s : Stroke} (h : s.Sound n) : (tick s).Sound n := fun _ _ => h /-- Stack layer sequences, a performed first. -/ def stack (a b : Reel) : Reel := ⟨a.beats+b.beats,fun i => if i rfl | succ t ih => rw [reach,reach,ih (by omega)] simp only [stack,ite_eq_left (show t simpa [go,reach] using H _ le_rfl | succ t ih => rw [← add_assoc,reach,reach,ih] simp [stack] exact G b.beats lemma Sound.stack {n : ℕ} {a b : Reel} (h : a.Sound n) (g : b.Sound n) : (a.stack b).Sound n := by intro t ht change t beside p (a.atBeat i) (b.atBeat i)⟩ theorem Sound.parallel {p q : ℕ} {a b : Reel} (h : a.Sound p) (g : b.Sound q) (hl : a.beats=b.beats) : (Reel.parallel p a b).Sound (p+q) := fun i hi => (h i hi).beside (g i (hl ▸ (by exact hi))) theorem parallel_left (p : ℕ) (a b : Reel) (h : a.Sound p) (x : Line) : ∀ i intros; rfl | succ t ih => intro i hi have g := h t (by omega) exact (beside_left p _ (b.atBeat t) g _ i hi).trans (g.send_congr (ih (by omega)) i hi) exact H a.beats le_rfl theorem parallel_right (p : ℕ) (a b : Reel) (hl : a.beats=b.beats) (x : Line) : drift p ((parallel p a b).go x)=b.go (drift p x) := by have H (t : ℕ) : drift p ((parallel p a b).reach x t)=b.reach (drift p x) t := by induction t with | zero => rfl | succ t ih => exact (beside_right p _ _ _).trans (congr_arg ((b.atBeat t).send) ih) change drift p ((parallel p a b).reach x a.beats)=_ rw [H,hl]; rfl /-- Use at offset p. -/ def atBase (p : ℕ) (a : Reel) : Reel := parallel p (wait a.beats) a lemma Sound.atBase {q : ℕ} {a : Reel} (h : a.Sound q) (p : ℕ) : (Reel.atBase p a).Sound (p+q) := (sound_wait _ _).parallel h rfl lemma atBase_left (p : ℕ) (a : Reel) (x : Line) (i : ℕ) (hi : i 1)) lemma sound_swatch (n : ℕ) (d : Line) : (swatch n d).Sound n := sound_tick (Stroke.plain_sound n _ _ fun _ hi => hi) lemma go_swatch (n : ℕ) (d : Line) (x : Line) (i : ℕ) (hi : i