- Boxes
- definitions
- Ellipses
- theorems and lemmas
- Blue border
- the statement of this result is ready to be formalized; all prerequisites are done
- Orange border
- the statement of this result is not ready to be formalized; the blueprint needs more work
- Blue background
- the proof of this result is ready to be formalized; all prerequisites are done
- Green border
- the statement of this result is formalized
- Green background
- the proof of this result is formalized
- Dark green background
- the proof of this result and all its ancestors are formalized
- Dark green border
- this is in math-comp
Stanley EC1 §1.6.2. For every \(\sigma \in \mathfrak {S}_{n+2}\),
The longest alternating subsequence has length exactly two more than the number of turning points: one element from each “monotone run” of \(\sigma \), plus one boundary element.
Formalization remark. This took five sequential interactive subagent sessions. The naive “leftmost turn in \([\sigma _i, \sigma _{i+2})\)” injection (mapping each subseq sign-flip to a turn) is not injective — adjacent flips can share a turn-window. Session B-4 pivoted from per-interval to a global bound via the subseq-monotonicity of \(\mathrm{flip\_ count}\).
Stanley’s statement (EC1 p. 61):
Let \(S \subseteq [n-1]\). Then \(\beta _n(S) \leq E_n\), with equality if and only if \(S = \{ 1, 3, 5, \dots \} \cap [n-1]\) or \(S = \{ 2, 4, 6, \dots \} \cap [n-1]\).
Here \(E_n\) is the \(n\)-th Euler number, realized as \(\beta (\mathrm{Alt}_n)\).
Our formalization, in contrapositive form: for \(n \geq 2\) and \(D \subseteq \{ 0, \dots , n-1\} \),
The “\(S = \{ 2, 4, 6, \dots \} \)” alternative in Stanley’s statement reduces to the complementation symmetry already proved at the \(\beta \)-level (, see also 1.15): the two alternating descent sets have equal \(\beta \).
For a sequence \(\mathrm{ss}\) of internal positions, \(\texttt{apply\_ psis}(\mathrm{ss})(w)\) is the left-fold composition of the corresponding \(\psi _{i}\). By Theorem 2.11 and Theorem 2.10, the result depends only on the set \(\mathrm{ss} \subseteq \texttt{internal\_ vertices}(w)\), and the M-equivalence class \([w] = \{ \texttt{apply\_ psis}(\mathrm{ss})(w) : \mathrm{ss} \subseteq \texttt{internal\_ vertices}(w)\} \) has size \(2^{\iota (w)}\).
\(\mathrm{as}(\sigma )\) is the length of the longest alternating subsequence of \(\sigma \), formalized as
\(u_{w} \; =\; [\, \texttt{is\_ descent\_ seq}(w, k) : k \in \{ 0, \dots , |w| - 2\} \, ]\) is the bit-vector of descent positions.
For a permutation \(\sigma \), this matches Stanley’s \(u_{S} = e_1 e_2 \cdots e_{n-1}\) (eq. 1.60, p. 58) with \(e_i = a\) if \(i \notin S\) and \(e_i = b\) if \(i \in S\). Our representation uses \(\mathtt{false} \mapsto a\) and \(\mathtt{true} \mapsto b\).
Position \(i\) in \(w\) is classified as:
For a permutation \(\sigma \in \mathfrak {S}_{n}\), the cycle count \(c(\sigma )\) is the number of cycles in the disjoint-cycle decomposition of \(\sigma \). In MathComp this is the cardinality of \(\mathrm{porbits}(\sigma )\), the set of orbits of \(\sigma \) acting on \(\{ 0, \dots , n-1\} \) by iteration.
Let \(\sigma \in \mathfrak {S}_{n+1}\). Position \(i \in \{ 0, \dots , n-1\} \) is a descent of \(\sigma \) when \(\sigma (i+1) {\lt} \sigma (i)\). The descent set is
Let \(\mathrm{alt\_ desc\_ set}\, n \subseteq \{ 0, \dots , n-1\} \) denote the alternating descent pattern \(\{ 0, 2, 4, \dots \} \). Define
\(A_n\) is the number of alternating permutations of \(\mathfrak {S}_{n}\).
For \(n, k \in \mathbb {N}\), the Eulerian number is the count
We write eulerian n k in the formalization, indexed so that \(n\) is “permutation size minus one” and \(k\) ranges over \(0, \dots , n\).
\(\texttt{expand\_ cde}\, m \in \mathrm{seq}\, (\mathrm{seq}\, \mathrm{bool})\) substitutes \(\mathtt{c} \mapsto a + b\), \(\mathtt{d} \mapsto ab + ba\), \(\mathtt{e} \mapsto 1\) in the cd-monomial \(m\):
Each element of the result is a bit-string representing a descent pattern; the total length is \(2^{c} \cdot 2^{d}\) where \(c, d\) are the C- and D-counts of \(m\).
Foata’s first fundamental bijection \(\varphi : \mathfrak {S}_{n+1} \to \mathfrak {S}_{n+1}\) is defined recursively on the seq representation: read \(w = w_1 w_2 \dots w_{n+1}\) left-to-right and at each step splice the new letter into a current word via a cyclic-rotation rule on maximal blocks. Implemented as seq_to_perm \(\circ \) foata \(\circ \) perm_to_seq.
A min-max tree over a type \(T\) is the inductive
Its in-order traversal is
\(\texttt{omega\_ seq}(s)\) is the seq-level analogue of \(\omega (D)\): it lists positions \(k\) such that exactly one of \(\{ k, k+1\} \) appears in \(s\). It is the computational backbone of the omega map and the bridge between \(\omega (\) and bit-strings.
For \(D \subseteq \{ 0, \dots , n\} \), the omega set is
This is exactly Stanley’s \(\omega (S)\) from EC1 just before eq. (1.64), p. 60: positions \(k\) where exactly one of \(\{ k, k+1\} \) lies in \(D\). Equivalently, \(\omega (D) = D \, \triangle \, (D - 1)\) where \(D - 1\) is \(D\) shifted by one.
\(\Phi '_{w} = [\, \mathrm{classify\_ vertex\_ cde}(i, w) : i \in \{ 0, \dots , |w|-1\} \, ]\) is the unfiltered list of cd-letters by position. \(\Phi _{w}\) is \(\Phi '_{w}\) with \(\mathtt{E\_ letter}\)s filtered out — Stanley’s \(\Phi _w\), the cd-monomial associated with \(w\) (EC1 pp. 58–59).
Stanley EC1 §1.4. The bivariate generating function for the joint distribution of \((\mathrm{maj}, \operatorname {des})\) over \(\mathfrak {S}_{n+1}\):
Formalized in \(\mathbb {Z}[q][t]\) (mathcomp’s {poly {poly int}}).
\(D \subseteq \{ 0, \dots , n-1\} \) is set-alternating iff for every consecutive pair \(i, i+1\) in \(\{ 0, \dots , n-1\} \), exactly one of \(\{ i, i+1\} \) belongs to \(D\). There are exactly two such sets on \(\{ 0, \dots , n-1\} \) (the “odd-position” and “even-position” alternating descents).
Position \(i \in \{ 0, \dots , n-1\} \) is a turning point of \(\sigma \in \mathfrak {S}_{n+2}\) if the direction of \(\sigma \) changes between slot \(i\) and slot \(i+1\):
\(\mathrm{turn\_ count}(\sigma ) = \# \{ i \in \{ 0, \dots , n-1\} : \mathrm{is\_ turn}(\sigma , i)\} \).
\(\operatorname {win}_i(w)\) is defined by fuel-based recursion that descends \(M(w)\) via \(\texttt{mm\_ pos}\): at the root position one returns \(|w| - i\); otherwise one recurses into the left or right subtree according to whether \(i\) lies before or after the root.
For \(n, k \in \mathbb {N}\), \(\texttt{witness\_ perm}(n, k)\) is an explicit sequence whose cd-monomial is \(c^{k-1}\, d\, c^{n-2-k}\) and whose \(S_w\)-set equals \(\{ k\} \).
This is exactly Stanley’s witness from the proof of Proposition 1.6.4 (EC1 p. 61):
“If \(i \in \omega (T) \setminus \omega (S)\) then let \(\Phi _{w} = c^{i-1}\, d\, c^{n-2-i}\), so \(S_w = \{ i\} \).”
Let \(\rev _n \in \mathfrak {S}_{n+1}\) be the order-reversing permutation \(i \mapsto n - i\). Position \(i\) is a descent of \(\sigma \) if and only if \(i\) is not a descent of \(\rev _n \cdot \sigma \), hence \(\operatorname {des}(\rev _n \cdot \sigma ) = n - \operatorname {des}(\sigma )\). In particular \(\operatorname {des}(\rev _n) = n\).
For \(D \subseteq \{ 0, \dots , m\} \) with \(D\) not set-alternating, \(\omega (D) \subsetneq \mathrm{set\_ T}\).
This is the contrapositive of the “\(\Rightarrow \)” direction of Stanley’s eq. (1.65): non-alternating sets have strictly smaller omega. Together with 4.12 it gives the full characterization
For \(k \in \{ 0, \dots , m-1\} \) and \(D \subseteq \{ 0, \dots , m\} \),
This identifies the finset-level \(\omega (\) with the seq-level \()\texttt{omega\_ seq}\), letting the heavy machinery in \(\texttt{psi\_ cdindex\_ *.v}\) apply to \(\{ \, \texttt{set}\, \} \)-style consumers.
For every \(\mathrm{ss}\), every position \(i\), and every uniq \(w\): \(\operatorname {win}_i(\texttt{apply\_ psis}(\mathrm{ss})(w)) = \operatorname {win}_i(w)\), \(\operatorname {hlc}_i(\texttt{apply\_ psis}(\mathrm{ss})(w)) = \operatorname {hlc}_i(w)\), \(\operatorname {int}_i(\texttt{apply\_ psis}(\mathrm{ss})(w)) = \operatorname {int}_i(w)\), and the lists of internal vertices coincide.
These four invariances express that \(\Psi \)-action permutes labels inside windows but preserves the unlabelled tree shape — the load-bearing geometric fact behind Stanley Fact #1’s well-definedness clause for \(\Phi _{w}\) (Chapter 3).
Stanley’s statement (verbatim, EC1 p. 60):
Let \(S, T \subseteq [n-1]\). If \(\omega (S) \subsetneq \omega (T)\), then \(\beta _n(S) {\lt} \beta _n(T)\).
Our formalization, for \(D, E \subseteq \{ 0, \dots , m\} \):
Pick \(k \in \omega (E) \setminus \omega (D)\). By 3.8 — Stanley’s identity \(\Phi _{w} = \sum _{\omega (X) \supseteq S_w} u_{X}\) — for every permutation \(\sigma \) with descent set \(D\), the bit-vector \(\texttt{descent\_ to\_ bvec}(D)\) lies in \(\texttt{expand\_ cde}(\Phi _{\texttt{perm\_ to\_ seq}(\sigma )})\). The same holds for \(\texttt{descent\_ to\_ bvec}(E)\) whenever \(\omega (E) \supseteq S_w\).
The witness \(\texttt{witness\_ perm}\) from 5.1, instantiated at \(i = k\), gives a sequence with cd-monomial \(c^{k-1}\, d\, c^{n-2-k}\) and \(S_w = \{ k\} \). Hence \(\omega (E) \supseteq \{ k\} = S_w\) but \(\omega (D) \not\supseteq \{ k\} \) (since \(k \notin \omega (D)\)). This produces a permutation \(\sigma \) with \(D(\sigma ) = E\) that is not in the image of any class map starting from a permutation with descent set \(D\). Counting, via 3.9 and the \(M\)-equivalence class enumeration, then yields \(\beta (D) {\lt} \beta (E)\).
\((\mathrm{turn\_ count}(\sigma )).+2 \leq \mathrm{as\_ perm\_ max}(\sigma )\).
Witness: \(I = \{ 0, n+1\} \cup \{ (\mathrm{val}\, t).+1 : t \text{ is a turning point of }\sigma \} \). Cardinality is \(\mathrm{turn\_ count}(\sigma ) + 2\) (boundary endpoints plus one position per turn). Alternation is verified between consecutive picks via the inter-turn monotonicity lemma applied to the no-turn slot interval between them.
\(\mathrm{as\_ perm\_ max}(\sigma ) \leq (\mathrm{turn\_ count}(\sigma )).+2\).
Proved via the global bound chain: flip_count (number of direction changes in a boolean sequence) is monotone under subsequence, so \(\mathrm{flip\_ count}(\mathrm{sign\_ seq}(\mathrm{pick\_ seq}\, \sigma \, I)) \leq \mathrm{flip\_ count}(\mathrm{sign\_ seq}(\mathrm{perm\_ seq}\, \sigma ))\). The right-hand side equals \(\mathrm{turn\_ count}(\sigma )\) via the bridge .
For every \(n \in \mathbb {N}\),
Proof status. Derived in one line from two_eulerA_split (which uses beta_compl from beta_swap.v) and the named admit:
Sanity. The base case \(n = 0\) (i.e. \(2 A_2 = 2\)) is proven unconditionally as euler_rec_n0_direct (no admit used). The cases \(n = 1, 2\) are stated via euler_rec and inherit the admit transitively.
Stanley’s statement (verbatim, p. 58):
Let \(w \in \mathfrak {S}_n\), and let \([w]\) be the M-equivalence class containing \(w\). Then \(\sum _{v \in [w]} u_{D(v)} = \Phi _{w}(a + b,\; ab + ba)\).
Our formalization, with multisets represented as sorted seqs:
Stanley uses Fact #3 as a stepping stone to Theorem 1.6.3 (the ab-index of \(\mathfrak {S}_n\) factors through the cd-substitution); our \(\texttt{fact3}\) matches this directly.
The right-hand side is enumerated by Stanley’s bit-string expansion (3.5), and the left-hand side is enumerated by ranging over subsets of \(\texttt{internal\_ vertices}(w)\). Both have size \(2^{\iota (w)}\) where \(\iota (w)\) is the number of internal vertices.
The map \(\mathrm{ss} \mapsto u_{\texttt{apply\_ psis}(\mathrm{ss})(w)}\) is shown to be injective via a bit-level recovery argument (in ): for each internal vertex \(v\), one can read off \((v \in \mathrm{ss})\) from the bits of \(u_{\texttt{apply\_ psis}(\mathrm{ss})(w)}\) at positions owned by \(v\). Disjointness of the bit-positions across distinct internal vertices follows from (D-vertex predecessors are non-internal). Equality of the two multisets then follows from injectivity plus equal cardinality.
\(\varphi \) is injective (hence bijective on the finite group \(\mathfrak {S}_{n+1}\)). Proof: explicit construction of the inverse \(\varphi ^{-1}\) on seqs (cuts at \(P\)-elements rather than after them, undoing the cyclic rotation block-wise) plus a cancellation lemma \(\varphi ^{-1}(\varphi (w)) = w\).
For every \(\sigma \in \mathfrak {S}_{n+1}\),
This is the load-bearing identity of the bijection. It is proved via the seq-level invariant foata_inv_eq_maj: \(\mathrm{inv\_ seq}(\mathrm{foata}(w)) = \mathrm{maj\_ seq}(w)\), which in turn is proved by reverse induction on \(w\) using the per-step invariant foata_step_inv (a tally of cyclic-rotation effects on the inversion count).
Stanley EC1 Theorem 1.3.4 (MacMahon). For every \(n, k \in \mathbb {N}\),
The proof is a direct consequence of 6.18 and 6.19: \(\varphi \) is a bijection of \(\mathfrak {S}_{n+1}\) that maps \(\{ \sigma : \mathrm{maj}(\sigma ) = k\} \) onto \(\{ \sigma : \mathrm{inv}(\sigma ) = k\} \) for every \(k\). The closed form for both distributions is the \(q\)-factorial (6.22 and 6.23).
Stanley EC1 §1.3.4 / Cor 1.3.10. For every \(n \in \mathbb {N}\),
Proved by induction on \(n\) using the value-based “insert \(\mathrm{ord}_{\max }\)” bijection \(\mathfrak {S}_{n+2} \simeq \mathfrak {S}_{n+1} \times \{ 0, \dots , n+1\} \) from \(\texttt{eulerian.v}\). The key step is \(\mathrm{inv}(\mathrm{insert\_ max\_ perm}(\tau , p)) = \mathrm{inv}(\tau ) + (n+1 - p)\).
Let \(\rev _n \in \mathfrak {S}_{n+1}\) be the order-reversing permutation. Then
Each pair \((i, j)\) with \(i {\lt} j\) is either an inversion or a co-inversion of \(\sigma \) (exactly one), and the bijection \((i, j) \mapsto (\rev _n(j), \rev _n(i))\) swaps the two.
For \(\rev _n \in \mathfrak {S}_{n+1}\),
The naive identity \(\mathrm{maj}(\rev _n \cdot \sigma ) = \binom {n+1}{2} - \mathrm{maj}(\sigma )\) is false in general: the asymmetry comes from \(\mathrm{maj}\)’s 1-indexed positions. The correct identity above accounts for it via the per-descent offset \((n - i) + (i + 1) = n + 1\).
For every \(s \in \mathrm{seq}\, \mathbb {N}\),
This identifies \(M(s)\) as a faithful representation of the underlying sequence: every position in \(w\) corresponds to a \(\mathrm{Node}\) of \(M(w)\).
For every \(\mathrm{ss}\) and every uniq \(w\), \(\Phi _{\texttt{apply\_ psis}(\mathrm{ss})(w)} = \Phi _{w}\).
Equivalently: \(\Phi _{w}\) depends only on the M-equivalence class \([w]\) — equivalently, only on \(M(w)\) as an unlabelled tree. This is the well-definedness clause of Stanley’s Fact #1 (EC1 p. 57). Internally it follows from the shape-invariance lemmas in 2.13.
For every uniq \(w\) with \(|w| \geq 2\) and every bit-string \(X\) with \(|X| = |w| - 1\):
This formalizes Stanley’s identity at the top of EC1 p. 61 inside the proof of Prop 1.6.4.
The supporting bit-level definitions (\(\texttt{cde\_ width}\), \(\texttt{cde\_ total\_ width}\), \(\texttt{cde\_ offset}\), \(\texttt{D\_ offsets}\), \(\texttt{expand\_ cde\_ mem\_ iff}\)) live in .
For all \(i, j \in \mathbb {N}\) and every uniq sequence \(w\), \(\psi _{i}(\psi _{j}(w)) = \psi _{j}(\psi _{i}(w))\). This is the commutativity half of Stanley’s Fact #1 (EC1 p. 57): the \(\psi _{i}\) for internal vertices generate an abelian group \(G_w \cong (\mathbb {Z}/2)^{\iota (w)}\), where \(\iota (w)\) is the number of internal vertices.
\(A_n(1, t) = \mathrm{eul\_ pol}_n(t) := \sum _{k=0}^{n} A_{n+1, k+1} \cdot t^k\). Setting \(q = 1\) drops the \(\mathrm{maj}\) weight, leaving the classical Eulerian polynomial \(\sum _\sigma t^{\operatorname {des}(\sigma )}\). Formalized via the rmorphism wrapper \(\mathrm{horner\_ eval}(1)\) applied coefficient-wise across the outer polynomial.
For all \(n, k \in \mathbb {N}\),
Combinatorially: \(n+1\) either becomes a fixed point of a \(k\)-cycle permutation (giving \(c(n, k)\)), or is inserted into one of \(n\) positions of a \((k+1)\)-cycle permutation (giving \(n \cdot c(n, k+1)\)).
Formalization route. The proof factors through a construction \(\mathrm{insert\_ after}\, j_0\, s : \mathfrak {S}_{n+1}\) that splices \(\mathrm{ord\_ max}\) into the \(s\)-cycle through \(j_0\). The key algebraic identity
reduces cycle-count preservation to MathComp’s porbits_mul_tperm composed with the fixed-point cycle-count lemma in .