History tree leaves and children (treeLeaves, treeChildren)
DefinitiontreeLeavesA history tree is a prefix-closed finite set of words. Leaves carry a load . Node aggregate . The least common ancestor is the longest common prefix. sums over leaf pairs with ; restricts to pairs sharing a terminal tube; to pairs with distinct terminal tubes.
import Mathlib.Algebra.BigOperators.Group.Finset.Basic
import Mathlib.Data.Finset.Max
import Mathlib.Data.Fintype.Basic
import Mathlib.Data.Real.Basic
/-!
# Filtered descent — history tree (paper (131)–(155))
Finite model of the descent's history tree. Nodes are finite words
(`List α`); the tree is a prefix-closed finite set of words. The paper's
loads `u_γ` are stated here pointwise (at one spatial cell); the paper's
integrated identities follow by summation over the cells.
* `treeLeaves`: maximal words — the paper's terminal histories.
* `nodeAgg`: `W_a = Σ_{γ ∈ Desc(a)} u_γ`, paper (131)–(136).
* `treeLCA`: least common ancestor = longest common prefix.
* `Xroot`: root-cross pair mass `Σ_{a(γ,γ') = r∗} u_γ u_γ'`, LHS of (142).
* `Xdup` / `Xgeom`: the duplicate / geometric split, paper (143)–(147).
* `treeChildren`: children of a node in the prefix tree.
-/
namespace FilteredDescent
/-- Leaves = maximal elements of a prefix-closed finite node set. -/
def treeLeaves {α : Type} [DecidableEq α] (T : Finset (List α)) :
Finset (List α) :=
T.filter (fun l => ∀ l' ∈ T, l <+: l' → l' = l)
/-- Node aggregate `W_a = Σ_{γ ∈ Desc(a)} u_γ`. Paper (131)–(136). -/
noncomputable def nodeAgg {α : Type} [DecidableEq α] (T : Finset (List α))
(u : List α → ℝ) (a : List α) : ℝ :=
∑ γ ∈ treeLeaves T, if a <+: γ then u γ else 0
/-- Least common ancestor of two histories = their longest common prefix.
`γ.take k <+: γ'` holds for `k = 0`, so the set below is nonempty. -/
noncomputable def treeLCA {α : Type} [DecidableEq α] (γ γ' : List α) :
List α :=
γ.take (((Finset.range (γ.length + 1)).filter
(fun k => γ.take k <+: γ')).max' ⟨0, by simp⟩)
/-- Root-cross pair mass `X^{root} = Σ_{a(γ,γ') = r∗} u_γ u_γ'`.
LHS of the root-cross gate, paper (142). -/
noncomputable def Xroot {α : Type} [DecidableEq α] (T : Finset (List α))
(u : List α → ℝ) : ℝ :=
∑ γ ∈ treeLeaves T, ∑ γ' ∈ treeLeaves T,
if treeLCA γ γ' = [] then u γ * u γ' else 0
/-- Duplicate part: root-cross pairs ending in the same terminal tube.
Paper (143)–(147). -/
noncomputable def Xdup {α : Type} [DecidableEq α] (T : Finset (List α))
(u : List α → ℝ) {n : ℕ} (termTube : List α → Fin n) : ℝ :=
∑ γ ∈ treeLeaves T, ∑ γ' ∈ treeLeaves T,
if treeLCA γ γ' = [] ∧ termTube γ = termTube γ' then u γ * u γ' else 0
/-- Geometric part: root-cross pairs ending in different terminal tubes. -/
noncomputable def Xgeom {α : Type} [DecidableEq α] (T : Finset (List α))
(u : List α → ℝ) {n : ℕ} (termTube : List α → Fin n) : ℝ :=
∑ γ ∈ treeLeaves T, ∑ γ' ∈ treeLeaves T,
if treeLCA γ γ' = [] ∧ termTube γ ≠ termTube γ' then u γ * u γ' else 0
/-- Children of a node in the prefix tree. -/
def treeChildren {α : Type} [DecidableEq α] (T : Finset (List α))
(a : List α) : Finset (List α) :=
T.filter (fun b => a <+: b ∧ b.length = a.length + 1)
end FilteredDescent
Read-back
What the Lean code literally says, in plain math · muse-spark
Read-backs for each declaration in Def_FilteredDescent_Tree.lean (namespace FilteredDescent), based solely on the code:
FilteredDescent.treeLeaves
Given an arbitrary type with decidable equality and a finite set of finite lists over , treeLeaves returns the sub-finset of consisting of those lists such that every list in of which is a prefix (written ) is equal to itself — i.e., the prefix-maximal elements of . The definition places no requirement on (in particular it does not check that is prefix-closed); if is empty, the result is empty. Maximality is with respect to the prefix relation only, so a short list can be a "leaf" as long as no list in strictly extends it.
FilteredDescent.nodeAgg
Given an arbitrary type with decidable equality, a finite set of finite lists over , a real-valued function on finite lists over , and a list , nodeAgg returns the real number , where the sum ranges over the prefix-maximal elements of (as computed by treeLeaves) such that is a prefix of ; leaves not extending contribute rather than being excluded from the sum. If is a prefix of no leaf of (for instance if is empty, or is unrelated to ), the value is . Note the sum is over leaves of only, not over all of , and no hypothesis relates to .
FilteredDescent.treeLCA
Given an arbitrary type with decidable equality and two finite lists over , treeLCA is computed as follows: form the finite set of natural numbers with (i.e. in ) such that the list of the first entries of is a prefix of ; take the largest such (the set is nonempty because always qualifies, since the empty list is a prefix of every list — this is discharged by the by simp proof); then return the first entries of . The result is always a prefix of ; it is the empty list exactly when and have no common first entry (or is empty), and it equals itself when is a prefix of . Note the computation is not syntactically symmetric: it truncates to the longest length at which its initial segment still prefixes .
FilteredDescent.Xroot
Given an arbitrary type with decidable equality, a finite set of finite lists over , and a real-valued function on finite lists over , Xroot returns the double sum over all pairs of prefix-maximal elements (leaves) of of the quantity when treeLCA γ γ' equals the empty list, and otherwise. In other words, it sums the products of the -values over exactly those ordered leaf pairs whose longest common prefix is empty. If has no leaves, or no two leaves have empty longest common prefix, the value is ; diagonal pairs contribute only when the leaf is itself the empty list (since otherwise its longest common prefix with itself is ).
FilteredDescent.Xdup
Given an arbitrary type with decidable equality, a finite set of finite lists over , a real-valued function on finite lists over , an implicit natural number , and a function from finite lists over to (the type of natural numbers less than ), Xdup returns the double sum over all ordered pairs of leaves of of when both treeLCA γ γ' = [] and hold, and otherwise. That is, it is the same pair-sum as Xroot but restricted to pairs whose longest common prefix is empty and which are additionally assigned the same value by . Note is defined on all lists, but is only ever evaluated at leaves here; if then is empty, so no such can exist and the declaration is vacuous in that case.
FilteredDescent.Xgeom
Given exactly the same arguments as Xdup (an arbitrary type with decidable equality, a finite set of lists over , a real-valued , an implicit , and ), Xgeom returns the double sum over all ordered leaf pairs of of when both treeLCA γ γ' = [] and hold, and otherwise. It differs from Xdup only in the second conjunct of the summation condition: inequality of the values rather than equality. Consequently, for each fixed ordered leaf pair, at most one of the summands of Xdup and Xgeom is nonzero, and the pair contributes to exactly one of them (when the longest common prefix is empty) or to neither (when it is nonempty).
FilteredDescent.treeChildren
Given an arbitrary type with decidable equality, a finite set of finite lists over , and a list , treeChildren returns the sub-finset of consisting of those lists such that is a prefix of and the length of is exactly one more than the length of . No hypothesis requires itself to belong to ; if or nothing in extends by exactly one entry, the result is empty.