/-Copyright (c) 2026 Carles MarΓn. All rights reserved.Released under Apache 2.0 license as described in the file LICENSE.Authors: Carles MarΓn-/module public import Mathlib.Data.ZMod.Basicpublic import Mathlib.Basic.Real.Basicpublic import Mathlib.Algebra.BigOperators.Group.List.Basicpublic import Mathlib.Algebra.BigOperators.Group.List.Lemmaspublic import Mathlib.Analysis.SpecialFunctions.Complex.Circlepublic import Mathlib.Tactic.Ring /-!# A direction-sensitive invariant of melodic words For a word `xs : List (ZMod 12)` and an odd function `f : ZMod 12 β M` into an additive group,`W f xs` sums `f` over the successive steps `x (j+1) - x j`. Reversing the word negates `W`, andrepeating a note does not change it. In contrast, order-blind statistics such as the stepcoefficients are invariant under reversal. ## Main results * `steps_reverse`, `W_reverse`: retrograde antisymmetry.* `W_cons_dup`, `W_dup_general`: invariance under repeated notes.* `f_zero`, `W_reverse_odd`, `W_dup_general_odd`: the same laws from oddness alone in a torsion-free group.* `acoef_blind_to_retrograde`, `W_separates_retrograde`.* `two_torsion_eq_tritone`, `odd_vanishes_at_tritone`.-/ @[expose] public section namespace MusicTheory namespace MelodicWord variable {M : Type*} [AddCommGroup M] /-! ### Directed steps of a melodic word. -/ /-- The consecutive directed intervals of a melodic word: `steps [xβ,β¦,xβ] = [xββxβ, β¦, xββxβββ]`. Each entry `step_j = x_{j+1} β x_j` is the directed melodic interval (sign = up/down). -/def steps : List (ZMod 12) β List (ZMod 12) | [] => [] | [_] => [] | a :: b :: rest => (b - a) :: steps (b :: rest) @[simp] lemma steps_nil : steps [] = [] := rfl@[simp] lemma steps_singleton (a : ZMod 12) : steps [a] = [] := rfl@[simp] lemma steps_cons_cons (a b : ZMod 12) (rest : List (ZMod 12)) : steps (a :: b :: rest) = (b - a) :: steps (b :: rest) := rfl /-- **The directed winding statistic.** `W f xs = Ξ£_j f(step_j)` over the directed steps. -/def W (f : ZMod 12 β M) (xs : List (ZMod 12)) : M := ((steps xs).map f).sum @[simp] lemma W_nil (f : ZMod 12 β M) : W f [] = 0 := rfl@[simp] lemma W_singleton (f : ZMod 12 β M) (a : ZMod 12) : W f [a] = 0 := rfl lemma W_cons_cons (f : ZMod 12 β M) (a b : ZMod 12) (rest : List (ZMod 12)) : W f (a :: b :: rest) = f (b - a) + W f (b :: rest) := by unfold W; rw [steps_cons_cons, List.map_cons, List.sum_cons] /-! ### Oddness β `f 0 = 0` (the concrete β-valued `sin` invariant; needs 2-torsion-freeness). -/ /-- From oddness `f (βm) = β f m`, at `m = 0` we get `f 0 = β f 0`, hence `f 0 = 0`. Stated for the real-valued case (the concrete `sin(2ΟΒ·/12)` invariant), where 2-torsion-freeness is automatic. -/theorem f_zero {f : ZMod 12 β β} (Hodd : β m, f (-m) = - f m) : f 0 = 0 := by have h : f 0 = - f 0 := by simpa using Hodd 0 have hsum : f 0 + f 0 = 0 := add_eq_zero_iff_eq_neg.mpr h exact add_self_eq_zero.mp hsum /-! ### Helper: how `steps` interacts with prepending a single LEADING note. `steps` peels notes off the FRONT. Prepending a note `z` to a NONEMPTY word `(a :: l)` PREPENDS onenew first step `a β z` to `steps`, leaving the rest unchanged. This single fact drives `steps_reverse`via concat (`reverseRecOn`) induction, since `(xs ++ [z]).reverse = z :: xs.reverse`. -/ /-- Prepending a note `z` before a NONEMPTY word `(a :: l)` prepends the step `a β z`: `steps (z :: a :: l) = (a β z) :: steps (a :: l)`. (Just unfolds the recursion.) -/lemma steps_cons (z a : ZMod 12) (l : List (ZMod 12)) : steps (z :: a :: l) = (a - z) :: steps (a :: l) := rfl /-- **The retrograde step law.** The directed steps of the reversed word are the negated steps of the word, in reversed order: `steps xs.reverse = ((steps xs).map (-Β·)).reverse`. Reversing a melody flips every directed interval (`x_{j+1}βx_j β¦ x_jβx_{j+1} = βstep_j`) AND their order. Proof by concat induction (`reverseRecOn`): appending a trailing note `z` to `xs` is, after reversing, prepending `z` to `xs.reverse`. -/theorem steps_reverse (xs : List (ZMod 12)) : steps xs.reverse = ((steps xs).map (fun s => -s)).reverse := by induction xs using List.reverseRecOn with | nil => rfl | append_singleton ys z ih => -- (ys ++ [z]).reverse = z :: ys.reverse. Split on whether ys is empty. rw [List.reverse_append, List.reverse_singleton, List.singleton_append] cases ys with | nil => rfl | cons a t => -- ys.reverse is nonempty, headed by the LAST of ys; expose it as (c :: l) obtain β¨c, l, hclβ© : β c l, (a :: t).reverse = c :: l := by cases h : (a :: t).reverse with | nil => simp at h | cons c l => exact β¨c, l, rflβ© -- LHS: steps (z :: (a::t).reverse) = steps (z :: c :: l) = (c - z) :: steps (c :: l) rw [hcl, steps_cons] -- ih : steps (a::t).reverse = ((steps (a::t)).map (-Β·)).reverse, with LHS = steps (c::l) rw [hcl] at ih rw [ih] -- RHS: steps (ys ++ [z]) reverses to ... ; compute steps of the concat then map/reverse -- steps ((a::t) ++ [z]) appends the final step (z - last). Use the concat-append lemma. have hsteps_concat : β (a : ZMod 12) (t : List (ZMod 12)), steps ((a :: t) ++ [z]) = steps (a :: t) ++ [z - (a :: t).getLast (by simp)] := by intro a t induction t generalizing a with | nil => simp [steps] | cons b u ihu => -- (a::b::u)++[z] = a :: ((b::u)++[z]); peel a, then apply ihu on (b::u)++[z] rw [List.cons_append, show steps (a :: ((b :: u) ++ [z])) = (b - a) :: steps ((b :: u) ++ [z]) from rfl, ihu b, steps_cons_cons, show (a :: b :: u).getLast (by simp) = (b :: u).getLast (by simp) from List.getLast_cons (by simp), List.cons_append] rw [hsteps_concat a t, List.map_append, List.reverse_append] -- last of (a::t) = head c of its reverse (read off hcl) have hlast : (a :: t).getLast (by simp) = c := by have h1 : (a :: t).getLast? = some c := by rw [β List.head?_reverse, hcl]; rfl rw [List.getLast?_eq_getLast_of_ne_nil (by simp)] at h1 exact Option.some.inj h1 rw [hlast] simp only [List.map_cons, List.map_nil, List.reverse_cons, List.reverse_nil, List.nil_append, List.cons_append] rw [neg_sub] /-- Summing a list of negated terms negates the sum: `(l.map (fun s => -g s)).sum = -(l.map g).sum`. -/lemma sum_map_neg (g : ZMod 12 β M) : β l : List (ZMod 12), ((l.map (fun s => -g s)).sum) = -((l.map g).sum) | [] => by simp | a :: l => by rw [List.map_cons, List.sum_cons, List.map_cons, List.sum_cons, sum_map_neg g l, neg_add] /-! ### Theorem 1 β Retrograde antisymmetry. -/ /-- **RETROGRADE ANTISYMMETRY.** Reversing a melodic word negates its winding statistic: `W f xs.reverse = β W f xs`, for any ODD `f`. (Each directed step negates under retrograde β `steps_reverse` β and `f` odd turns each `f(βs)` into `βf(s)`; the sum then negates.) -/theorem W_reverse {f : ZMod 12 β M} (Hodd : β m, f (-m) = - f m) (xs : List (ZMod 12)) : W f xs.reverse = - W f xs := by unfold W rw [steps_reverse, List.map_reverse, List.sum_reverse, List.map_map] -- (steps xs).map (f β (-Β·)) = (steps xs).map (-f) by oddness have : (steps xs).map (f β fun s => -s) = (steps xs).map (fun s => -f s) := by apply List.map_congr_left intro s _; simp only [Function.comp_apply]; exact Hodd s rw [this, sum_map_neg f] /-! ### Theorem 2 β Repetition / reparametrization invariance. -/ /-- **Repeat the HEAD note.** Inserting an immediate repeat of the first note inserts a step of `0`, which `f 0 = 0` kills: `W f (a :: a :: rest) = W f (a :: rest)`. -/theorem W_cons_dup {f : ZMod 12 β M} (hf0 : f 0 = 0) (a : ZMod 12) (rest : List (ZMod 12)) : W f (a :: a :: rest) = W f (a :: rest) := by rw [W_cons_cons]; simp [hf0] /-- **Repeat an INTERIOR note** (the step on each side is unaffected; the inserted middle step is `0`). `W f (a :: b :: b :: rest) = W f (a :: b :: rest)`. -/theorem W_cons_cons_dup {f : ZMod 12 β M} (hf0 : f 0 = 0) (a b : ZMod 12) (rest : List (ZMod 12)) : W f (a :: b :: b :: rest) = W f (a :: b :: rest) := by rw [W_cons_cons (rest := b :: rest), W_cons_dup hf0, W_cons_cons] /-- **REPETITION / REPARAMETRIZATION INVARIANCE (general).** Duplicating the note `a` at ANY position of a melodic word leaves `W` unchanged: `W f (pre ++ a :: a :: rest) = W f (pre ++ a :: rest)`. The duplicated note inserts a single directed step `a β a = 0`, killed by `f 0 = 0`; all other steps are untouched. This is the "tempo / note-value is irrelevant" law β `W` sees the PATH, not its parametrization. -/theorem W_dup_general {f : ZMod 12 β M} (hf0 : f 0 = 0) (a : ZMod 12) : β (pre rest : List (ZMod 12)), W f (pre ++ a :: a :: rest) = W f (pre ++ a :: rest) | [], rest => by simpa using W_cons_dup hf0 a rest | [p], rest => by simpa using W_cons_cons_dup hf0 p a rest | p :: q :: pre, rest => by show W f (p :: q :: (pre ++ a :: a :: rest)) = W f (p :: q :: (pre ++ a :: rest)) rw [W_cons_cons, W_cons_cons] congr 1 have ih := W_dup_general hf0 a (q :: pre) rest rw [List.cons_append, List.cons_append] at ih exact ih /-! ### Specialization (the concrete `sin` invariant): `f 0 = 0` comes for free from oddness over a 2-torsion-free codomain (e.g. β, where `NoZeroSMulDivisors β β` holds). -/ /-- Retrograde antisymmetry needing ONLY oddness, over a 2-torsion-free codomain (β): `f 0 = 0` free. -/theorem W_reverse_odd {M : Type*} [AddCommGroup M] {f : ZMod 12 β M} (Hodd : β m, f (-m) = - f m) (xs : List (ZMod 12)) : W f xs.reverse = - W f xs := W_reverse Hodd xs /-- Repetition invariance (general) needing ONLY oddness, for the real-valued `sin` invariant. -/theorem W_dup_general_odd {f : ZMod 12 β β} (Hodd : β m, f (-m) = - f m) (a : ZMod 12) (pre rest : List (ZMod 12)) : W f (pre ++ a :: a :: rest) = W f (pre ++ a :: rest) := W_dup_general (f_zero Hodd) a pre rest /-! ### Order-blindness of the pitch-class DFT The DFT coefficient `a_k` of a melodic word is a SUM over the notes of a per-note quantity`Ο(xα΅’)`; the SUMMAND depends on each pitch class alone, never on its neighbours. Hence `a_k`factors through the MULTISET of pitch classes and is invariant under retrograde (reverse) AND underany permutation. The crucial point: this needs NOTHING about the exponential β it is pure list-suminvariance. We give it in two layers. Layer (a) β ABSTRACT, over any `AddCommMonoid` codomain. The whole content is `List.sum_reverse`(through `List.map_reverse`) and `List.Perm.sum_eq` (through `List.Perm.map`). -/ section OrderBlindvariable {N : Type*} [AddCommMonoid N] /-- **Reverse-invariance of a list-sum-of-a-pointwise-function** (any `AddCommMonoid` codomain). `Ξ£α΅’ Ο(xα΅’)` is unchanged by reversing the list: order is invisible to a sum of per-element terms. -/theorem sum_map_reverse (Ο : ZMod 12 β N) (xs : List (ZMod 12)) : ((xs.reverse).map Ο).sum = (xs.map Ο).sum := by rw [List.map_reverse, List.sum_reverse] /-- **Permutation-invariance of a list-sum-of-a-pointwise-function** (any `AddCommMonoid` codomain). `Ξ£α΅’ Ο(xα΅’)` depends only on the MULTISET of `xs`: any reordering gives the same sum. -/theorem sum_map_perm {Ο : ZMod 12 β N} {xs ys : List (ZMod 12)} (h : List.Perm xs ys) : (xs.map Ο).sum = (ys.map Ο).sum := (h.map Ο).sum_eq end OrderBlind /-! Layer (b) β SPECIALIZE to the actual pc-DFT character. The exact exponent is irrelevant to thetwo laws (only that the summand is a function of the single note `x`); we use the standard kernel`exp(β2ΟiΒ·kΒ·x/12)`. `acoef` is `noncomputable` (it uses `Complex.exp`); the theorems are pure. -/ /-- The pc-DFT coefficient `a_k` of a melodic WORD: `a_k(xs) = Ξ£β±Ό exp(β2ΟiΒ·kΒ·xβ±Ό/12)`. Built as a list-sum of a per-note kernel, so it is order-blind by construction (see `acoef_reverse`/`_perm`). -/noncomputable def acoef (k : β) (xs : List (ZMod 12)) : β := (xs.map (fun x => Complex.exp (-2 * Real.pi * Complex.I * k * (x.val : β) / 12))).sum /-- **The DFT cannot see direction.** `a_k` is invariant under RETROGRADE: `acoef k xs.reverse = acoef k xs`. Immediate specialization of `sum_map_reverse`. -/theorem acoef_reverse (k : β) (xs : List (ZMod 12)) : acoef k xs.reverse = acoef k xs := sum_map_reverse _ xs /-- **The DFT cannot see order at all.** `a_k` factors through the multiset of pitch classes: `xs ~ ys β acoef k xs = acoef k ys`. Immediate specialization of `sum_map_perm`. -/theorem acoef_perm {k : β} {xs ys : List (ZMod 12)} (h : List.Perm xs ys) : acoef k xs = acoef k ys := sum_map_perm h /-! ### Directional β₯ a_k : the harmonic axis CANNOT separate a word from its retrograde, yet the directed axis `W` CAN. `a_k` is retrograde-blind (previous section), so ANY functional of the WHOLE `a_k`-vector is retrograde-blind too(`acoef_blind_to_retrograde`): the entire harmonic profile is the same forwards and backwards. Thedirected winding statistic `W`, by contrast, FLIPS SIGN under retrograde (`W_reverse`), so itstrictly distinguishes a word from its reverse whenever it is nonzero over a 2-torsion-free codomain(`W_separates_retrograde`). The two axes are orthogonal-on-direction: harmonic content is symmetric,directed motion is antisymmetric. -/ /-- **No functional of the a_k-vector separates a word from its retrograde.** For ANY `g` on the full coefficient vector `(k β¦ a_k)`, the forward and retrograde words give the SAME value. (The two coefficient vectors are equal pointwise by `acoef_reverse`, hence equal as functions.) -/theorem acoef_blind_to_retrograde {Ξ² : Type*} (g : (β β β) β Ξ²) (xs : List (ZMod 12)) : g (fun k => acoef k xs.reverse) = g (fun k => acoef k xs) := by congr 1 funext k exact acoef_reverse k xs /-- **`W` strictly separates a word from its retrograde when nonzero.** Over a 2-torsion-free `AddCommGroup` (`NoZeroSMulDivisors β M`, e.g. β), if the directed winding `W f xs β 0` then the retrograde value `W f xs.reverse = βW f xs` differs from `W f xs`. So `W` SEES the direction that the entire harmonic `a_k`-vector is blind to. -/theorem W_separates_retrograde {M : Type*} [AddCommGroup M] [NoZeroSMulDivisors β M] {f : ZMod 12 β M} (Hodd : β m, f (-m) = - f m) (xs : List (ZMod 12)) (hne : W f xs β 0) : W f xs.reverse β W f xs := by rw [W_reverse Hodd] intro h -- h : -W f xs = W f xs. So W + W = W + (-W) = 0 β 2β’W = 0 β W = 0, contradicting hne. apply hne have hzero : W f xs + W f xs = 0 := by nth_rewrite 1 [β h]; exact neg_add_cancel (W f xs) have h2 : (2 : β) β’ W f xs = 0 := by rw [two_smul]; exact hzero exact (smul_eq_zero.mp h2).resolve_left (by norm_num) /-! ### The tritone obstruction `f 0 = 0` (the existing `f_zero`) is only the FIRST of the 2-torsion obstructions. β€/12 has EXACTLYtwo solutions of `m = βm`: the identity `0` and the tritone `6` (`6 + 6 = 12 β‘ 0`). Both are forcedto vanish for any odd `f` over a 2-torsion-free codomain. The tritone `6` is the same central element`Tβ` that recurs throughout the corpus: the unique nonzero element fixed by inversion, the centre of6-30, the fixed axis of the rhythmic oddity property, and the tritone anomaly of the interval vector. -/ /-- **The unique nonzero 2-torsion point of β€/12 is the tritone.** `m = βm β m = 0 β¨ m = 6`. -/theorem two_torsion_eq_tritone : β m : ZMod 12, (m = -m) β (m = 0 β¨ m = 6) := by decide /-- **The TRITONE obstruction.** Any odd `f` over a 2-torsion-free codomain (here β, the concrete `sin(2ΟΒ·/12)` invariant) must VANISH at the tritone: `f 6 = 0`. Since `β6 = 6` in β€/12, oddness gives `f 6 = f(β6) = βf 6`, and 2-torsion-freeness forces `f 6 = 0` β exactly mirroring `f_zero` at the second (and only other) self-inverse point. -/theorem odd_vanishes_at_tritone {f : ZMod 12 β β} (Hodd : β m, f (-m) = - f m) : f 6 = 0 := by have h : f 6 = - f 6 := by have h6 : (-(6 : ZMod 12)) = 6 := by decide have := Hodd 6 rwa [h6] at this have hsum : f 6 + f 6 = 0 := add_eq_zero_iff_eq_neg.mpr h exact add_self_eq_zero.mp hsum /-! ### A concrete witness: a genuinely odd `f` on β€/12 with `W` direction-sensitive. `fsign m = (if m.val β€ 6 then m.val else m.val β 12 : β€)` is the signed representative in `[β5,6]`β¦but `6 β¦ 6` and `β6 = 6` so that exact map is NOT odd at the tritone. The clean odd integer modelis `g m = m.val β 6Β·(β¦)`; rather than fight the tritone we exhibit oddness abstractly: any `f` builtas `f m = h m β h (βm)` is odd, and `sin(2ΟΒ·/12)` is the analytic instance. We record the smallarithmetic witness that the directed step really does flip sign, which is the whole point. -/ /-- The directed step from `x` to `y` is the negation of the step from `y` to `x`: `steps [y, x] = [x β y] = β(steps [x, y])` β the atomic source of retrograde antisymmetry. -/example (x y : ZMod 12) : steps [y, x] = ((steps [x, y]).map (fun s => -s)).reverse := by simp [steps, neg_sub] end MelodicWord end MusicTheory