Offline · exact source
Exact Lean source

YoungRSKInsertion

formalization/Cloning/YoungRSKInsertion.lean

Source hash: f839cd5edc832f42e43df582975e6caf554ce7ec1f2b8046b6007d5f579a52cb

Full source copied without mathematical changes from the snapshot whose input hash matches the passing audit. Select a line number for a stable source pointer.

The declarations belong to the compiled inventory covered by the project’s shared axiom audit. That audit records an aggregate union, without attributing axioms to individual declarations.

Declarations in this module
definition Cloning.YoungGeneral.RowContentdefinition Cloning.YoungGeneral.rowAdddefinition Cloning.YoungGeneral.rowRemovedefinition Cloning.YoungGeneral.rowReplacedefinition Cloning.YoungGeneral.rowBumpCandidatesdefinition Cloning.YoungGeneral.rowReverseCandidatesdefinition Cloning.YoungGeneral.rowBumpdefinition Cloning.YoungGeneral.rowReverseBumpdefinition Cloning.YoungGeneral.rowInserteddefinition Cloning.YoungGeneral.rowReversedtheorem Cloning.YoungGeneral.rowBump_eq_some_ifftheorem Cloning.YoungGeneral.rowReverseBump_eq_some_ifftheorem Cloning.YoungGeneral.rowBump_eq_none_ifftheorem Cloning.YoungGeneral.rowReplace_canceltheorem Cloning.YoungGeneral.rowReverseBump_after_inserttheorem Cloning.YoungGeneral.rowReversed_after_inserttheorem Cloning.YoungGeneral.rowBump_after_reversetheorem Cloning.YoungGeneral.rowInserted_after_reversetheorem Cloning.YoungGeneral.rowReplace_contenttheorem Cloning.YoungGeneral.rowReplace_massdefinition Cloning.YoungGeneral.rowPrefixdefinition Cloning.YoungGeneral.rowStrictPrefixtheorem Cloning.YoungGeneral.monotone_rowPrefixtheorem Cloning.YoungGeneral.monotone_rowStrictPrefixtheorem Cloning.YoungGeneral.rowPrefix_le_strictPrefixtheorem Cloning.YoungGeneral.rowPrefix_eq_of_no_entriestheorem Cloning.YoungGeneral.rowStrictPrefix_slacktheorem Cloning.YoungGeneral.rowReplace_sumtheorem Cloning.YoungGeneral.rowReplace_prefixtheorem Cloning.YoungGeneral.rowReplace_strictPrefixtheorem Cloning.YoungGeneral.rowAdd_contenttheorem Cloning.YoungGeneral.rowAdd_prefixtheorem Cloning.YoungGeneral.rowAdd_strictPrefixtheorem Cloning.YoungGeneral.rowInserted_prefix_of_bumptheorem Cloning.YoungGeneral.rowInserted_prefix_of_nonetheorem Cloning.YoungGeneral.rowInserted_strictPrefix_of_bumptheorem Cloning.YoungGeneral.rowInserted_strictPrefix_of_nonetheorem Cloning.YoungGeneral.rowRemove_rowAddtheorem Cloning.YoungGeneral.rowAdd_rowRemovedefinition Cloning.YoungGeneral.rowLastdefinition Cloning.YoungGeneral.rowDeleteLasttheorem Cloning.YoungGeneral.rowLast_eq_some_ifftheorem Cloning.YoungGeneral.rowLast_after_terminal_inserttheorem Cloning.YoungGeneral.rowDeleteLast_after_terminal_inserttheorem Cloning.YoungGeneral.rowBump_after_deleteLasttheorem Cloning.YoungGeneral.rowInserted_after_deleteLasttheorem Cloning.YoungGeneral.rowAdd_massdefinition Cloning.YoungGeneral.rowMonomialtheorem Cloning.YoungGeneral.rowReplace_monomialtheorem Cloning.YoungGeneral.rowAdd_monomial
1import Cloning.YoungGeneralTableaux
2import Mathlib.Data.Finset.Max
3 
4/-!
5# Reversible row insertion on literal letter-count arrays
6 
7A weakly increasing row is represented by the multiplicity of each letter.
8Insertion replaces the least occupied letter strictly above the input;
9reverse insertion replaces the greatest occupied letter strictly below the
10returned letter. These are the actual RSK row operations, with their local
11inverse and monomial-conservation laws proved below.
12-/
13 
14noncomputable section
15open scoped BigOperators Classical
16namespace Cloning.YoungGeneral
17 
18abbrev RowContent (d : ℕ) := Fin d → ℕ
19 
20def rowAdd {d : ℕ} (A : RowContent d) (x : Fin d) : RowContent d :=
21  Function.update A x (A x + 1)
22 
23def rowRemove {d : ℕ} (A : RowContent d) (x : Fin d) : RowContent d :=
24  Function.update A x (A x - 1)
25 
26def rowReplace {d : ℕ} (A : RowContent d) (x y : Fin d) : RowContent d :=
27  rowAdd (rowRemove A y) x
28 
29def rowBumpCandidates {d : ℕ} (A : RowContent d) (x : Fin d) : Finset (Fin d) :=
30  Finset.univ.filter (fun y => x < y ∧ 0 < A y)
31 
32def rowReverseCandidates {d : ℕ} (A : RowContent d) (y : Fin d) : Finset (Fin d) :=
33  Finset.univ.filter (fun x => x < y ∧ 0 < A x)
34 
35def rowBump {d : ℕ} (A : RowContent d) (x : Fin d) : Option (Fin d) :=
36  if h : (rowBumpCandidates A x).Nonempty then some ((rowBumpCandidates A x).min' h)
37  else none
38 
39def rowReverseBump {d : ℕ} (A : RowContent d) (y : Fin d) : Option (Fin d) :=
40  if h : (rowReverseCandidates A y).Nonempty then some ((rowReverseCandidates A y).max' h)
41  else none
42 
43def rowInserted {d : ℕ} (A : RowContent d) (x : Fin d) : RowContent d :=
44  match rowBump A x with
45  | none => rowAdd A x
46  | some y => rowReplace A x y
47 
48def rowReversed {d : ℕ} (A : RowContent d) (y : Fin d) : RowContent d :=
49  match rowReverseBump A y with
50  | none => A
51  | some x => rowReplace A y x
52 
53theorem rowBump_eq_some_iff {d : ℕ} (A : RowContent d) (x y : Fin d) :
54    rowBump A x = some y ↔ x < y ∧ 0 < A y ∧
55      ∀ z, x < z → 0 < A z → y ≤ z := by
56  classical
57  constructor
58  · intro he
59    unfold rowBump at he
60    split_ifs at he with h
61    · have hmin := Finset.min'_mem (rowBumpCandidates A x) h
62      have hy : (rowBumpCandidates A x).min' h = y := Option.some.inj he
63      rw [hy] at hmin
64      obtain ⟨_, hxy, hypos⟩ := Finset.mem_filter.mp hmin
65      refine ⟨hxy, hypos, fun z hxz hzpos => ?_⟩
66      rw [← hy]
67      exact Finset.min'_le _ _ (Finset.mem_filter.mpr ⟨Finset.mem_univ _, hxz, hzpos⟩)
68  · rintro ⟨hxy, hypos, hmin⟩
69    have hymem : y ∈ rowBumpCandidates A x := by simp [rowBumpCandidates, hxy, hypos]
70    have hs : (rowBumpCandidates A x).Nonempty := ⟨y, hymem⟩
71    rw [rowBump, dif_pos hs]
72    congr 1
73    apply le_antisymm (Finset.min'_le _ _ hymem)
74    have hm := Finset.mem_filter.mp (Finset.min'_mem (rowBumpCandidates A x) hs)
75    exact hmin _ hm.2.1 hm.2.2
76 
77theorem rowReverseBump_eq_some_iff {d : ℕ} (A : RowContent d) (y x : Fin d) :
78    rowReverseBump A y = some x ↔ x < y ∧ 0 < A x ∧
79      ∀ z, z < y → 0 < A z → z ≤ x := by
80  classical
81  constructor
82  · intro he
83    unfold rowReverseBump at he
84    split_ifs at he with h
85    · have hmax := Finset.max'_mem (rowReverseCandidates A y) h
86      have hx : (rowReverseCandidates A y).max' h = x := Option.some.inj he
87      rw [hx] at hmax
88      obtain ⟨_, hxy, hxpos⟩ := Finset.mem_filter.mp hmax
89      refine ⟨hxy, hxpos, fun z hzy hzpos => ?_⟩
90      rw [← hx]
91      exact Finset.le_max' _ _ (Finset.mem_filter.mpr ⟨Finset.mem_univ _, hzy, hzpos⟩)
92  · rintro ⟨hxy, hxpos, hmax⟩
93    have hxmem : x ∈ rowReverseCandidates A y := by
94      simp [rowReverseCandidates, hxy, hxpos]
95    have hs : (rowReverseCandidates A y).Nonempty := ⟨x, hxmem⟩
96    rw [rowReverseBump, dif_pos hs]
97    congr 1
98    apply le_antisymm _ (Finset.le_max' _ _ hxmem)
99    apply Finset.max'_le
100    intro z hz
101    obtain ⟨_, hzy, hzpos⟩ := Finset.mem_filter.mp hz
102    exact hmax z hzy hzpos
103 
104theorem rowBump_eq_none_iff {d : ℕ} (A : RowContent d) (x : Fin d) :
105    rowBump A x = none ↔ ∀ y, x < y → A y = 0 := by
106  classical
107  simp only [rowBump]
108  split_ifs with h
109  · constructor
110    · simp
111    · intro he
112      obtain ⟨y, hy⟩ := h
113      obtain ⟨_, hxy, hypos⟩ := Finset.mem_filter.mp hy
114      have := he y hxy
115      omega
116  · simp only [true_iff]
117    intro y hxy
118    by_contra hy
119    exact h ⟨y, by simp [rowBumpCandidates, hxy, Nat.pos_of_ne_zero hy]⟩
120 
121theorem rowReplace_cancel {d : ℕ} (A : RowContent d) (x y : Fin d)
122    (hxy : x ≠ y) (hy : 0 < A y) :
123    rowReplace (rowReplace A x y) y x = A := by
124  classical
125  funext z
126  by_cases hzx : z = x
127  · subst z
128    simp [rowReplace, rowAdd, rowRemove, hxy, hxy.symm]
129  · by_cases hzy : z = y
130    · subst z
131      simp only [rowReplace, rowAdd, rowRemove, Function.update_apply, hxy, hxy.symm,
132        if_true, if_false]
133      omega
134    · simp [rowReplace, rowAdd, rowRemove, hzx, hzy, hxy, hxy.symm]
135 
136/-- The reverse search recovers exactly the input letter of a forward bump. -/
137theorem rowReverseBump_after_insert {d : ℕ} (A : RowContent d) (x y : Fin d)
138    (h : rowBump A x = some y) : rowReverseBump (rowInserted A x) y = some x := by
139  classical
140  obtain ⟨hxy, hypos, hmin⟩ := (rowBump_eq_some_iff A x y).mp h
141  rw [rowInserted, h]
142  apply (rowReverseBump_eq_some_iff _ _ _).mpr
143  refine ⟨hxy, ?_, ?_⟩
144  · simp [rowReplace, rowAdd, rowRemove, ne_of_lt hxy]
145  · intro z hzy hzpos
146    by_contra hzx
147    have hxz : x < z := lt_of_not_ge hzx
148    have hzold : 0 < A z := by
149      simpa [rowReplace, rowAdd, rowRemove, Function.update_apply,
150        ne_of_gt hxz, ne_of_lt hzy, ne_of_lt hxy] using hzpos
151    exact (not_le_of_gt hzy) (hmin z hxz hzold)
152 
153/-- Forward insertion followed by its selected reverse step restores the row. -/
154theorem rowReversed_after_insert {d : ℕ} (A : RowContent d) (x y : Fin d)
155    (h : rowBump A x = some y) : rowReversed (rowInserted A x) y = A := by
156  rw [rowReversed, rowReverseBump_after_insert A x y h, rowInserted, h]
157  obtain ⟨hxy, hypos, _⟩ := (rowBump_eq_some_iff A x y).mp h
158  exact rowReplace_cancel A x y (ne_of_lt hxy) hypos
159 
160/-- Conversely, a reverse bump is recovered by forward insertion. -/
161theorem rowBump_after_reverse {d : ℕ} (A : RowContent d) (y x : Fin d)
162    (h : rowReverseBump A y = some x) : rowBump (rowReversed A y) x = some y := by
163  classical
164  obtain ⟨hxy, hxpos, hmax⟩ := (rowReverseBump_eq_some_iff A y x).mp h
165  rw [rowReversed, h]
166  apply (rowBump_eq_some_iff _ _ _).mpr
167  refine ⟨hxy, ?_, ?_⟩
168  · simp [rowReplace, rowAdd, rowRemove, (ne_of_lt hxy).symm]
169  · intro z hxz hzpos
170    by_contra hzy
171    have hzy' : z < y := lt_of_not_ge hzy
172    have hzold : 0 < A z := by
173      simpa [rowReplace, rowAdd, rowRemove, Function.update_apply,
174        ne_of_gt hxz, ne_of_lt hzy', (ne_of_lt hxy).symm] using hzpos
175    exact (not_le_of_gt hxz) (hmax z hzy' hzold)
176 
177theorem rowInserted_after_reverse {d : ℕ} (A : RowContent d) (y x : Fin d)
178    (h : rowReverseBump A y = some x) : rowInserted (rowReversed A y) x = A := by
179  rw [rowInserted, rowBump_after_reverse A y x h, rowReversed, h]
180  obtain ⟨hxy, hxpos, _⟩ := (rowReverseBump_eq_some_iff A y x).mp h
181  exact rowReplace_cancel A y x (ne_of_lt hxy).symm hxpos
182 
183/-- Literal conservation of each letter multiplicity at a bump. -/
184theorem rowReplace_content {d : ℕ} (A : RowContent d) (x y j : Fin d)
185    (hxy : x ≠ y) (hy : 0 < A y) :
186    rowReplace A x y j + (if j = y then 1 else 0) =
187      A j + (if j = x then 1 else 0) := by
188  classical
189  by_cases hjx : j = x
190  · subst j
191    simp [rowReplace, rowAdd, rowRemove, hxy]
192  · by_cases hjy : j = y
193    · subst j
194      simp only [rowReplace, rowAdd, rowRemove, Function.update_apply, hxy, hxy.symm,
195        if_true, if_false, add_zero]
196      omega
197    · simp [rowReplace, rowAdd, rowRemove, hjx, hjy, hxy]
198 
199theorem rowReplace_mass {d : ℕ} (A : RowContent d) (x y : Fin d)
200    (hxy : x ≠ y) (hy : 0 < A y) : ∑ j, rowReplace A x y j = ∑ j, A j := by
201  have h := Finset.sum_congr (s₁ := Finset.univ) rfl
202    (fun j _ => rowReplace_content A x y j hxy hy)
203  simp only [Finset.sum_add_distrib] at h
204  simpa using h
205 
206def rowPrefix {d : ℕ} (A : RowContent d) (k : Fin d) : ℕ :=
207  ∑ j ∈ Finset.univ.filter (fun j => j ≤ k), A j
208 
209def rowStrictPrefix {d : ℕ} (A : RowContent d) (k : Fin d) : ℕ :=
210  ∑ j ∈ Finset.univ.filter (fun j => j < k), A j
211 
212theorem monotone_rowPrefix {d : ℕ} (A : RowContent d) : Monotone (rowPrefix A) := by
213  intro b k hbk
214  apply Finset.sum_le_sum_of_subset
215  intro j hj
216  simp only [Finset.mem_filter, Finset.mem_univ, true_and] at hj ⊢
217  exact hj.trans hbk
218 
219theorem monotone_rowStrictPrefix {d : ℕ} (A : RowContent d) :
220    Monotone (rowStrictPrefix A) := by
221  intro b k hbk
222  apply Finset.sum_le_sum_of_subset
223  intro j hj
224  simp only [Finset.mem_filter, Finset.mem_univ, true_and] at hj ⊢
225  exact hj.trans_le hbk
226 
227theorem rowPrefix_le_strictPrefix {d : ℕ} (A : RowContent d) (b k : Fin d)
228    (hbk : b < k) : rowPrefix A b ≤ rowStrictPrefix A k := by
229  apply Finset.sum_le_sum_of_subset
230  intro j hj
231  simp only [Finset.mem_filter, Finset.mem_univ, true_and] at hj ⊢
232  exact hj.trans_lt hbk
233 
234theorem rowPrefix_eq_of_no_entries {d : ℕ} (A : RowContent d) (b k : Fin d)
235    (hbk : b ≤ k) (hzero : ∀ j, b < j → j ≤ k → A j = 0) :
236    rowPrefix A k = rowPrefix A b := by
237  symm
238  apply Finset.sum_subset
239  · intro j hj
240    simp only [Finset.mem_filter, Finset.mem_univ, true_and] at hj ⊢
241    exact hj.trans hbk
242  · intro j hj hjnot
243    simp only [Finset.mem_filter, Finset.mem_univ, true_and, not_le] at hj hjnot
244    exact hzero j hjnot hj
245 
246theorem rowStrictPrefix_slack {d : ℕ} (A : RowContent d) (b k : Fin d)
247    (hbk : b < k) (hb : 0 < A b) : rowStrictPrefix A b + 1 ≤ rowStrictPrefix A k := by
248  have hs : insert b (Finset.univ.filter (fun j => j < b)) ⊆
249      Finset.univ.filter (fun j => j < k) := by
250    intro j hj
251    simp only [Finset.mem_insert, Finset.mem_filter, Finset.mem_univ, true_and] at hj ⊢
252    rcases hj with rfl | hj
253    · exact hbk
254    · exact hj.trans hbk
255  have h := Finset.sum_le_sum_of_subset (f := A) hs
256  rw [Finset.sum_insert (by simp)] at h
257  change A b + rowStrictPrefix A b ≤ rowStrictPrefix A k at h
258  omega
259 
260theorem rowReplace_sum {d : ℕ} (A : RowContent d) (x y : Fin d)
261    (hxy : x ≠ y) (hy : 0 < A y) (s : Finset (Fin d)) :
262    (∑ j ∈ s, rowReplace A x y j) + (if y ∈ s then 1 else 0) =
263      (∑ j ∈ s, A j) + (if x ∈ s then 1 else 0) := by
264  have h := Finset.sum_congr (s₁ := s) rfl
265    (fun j _ => rowReplace_content A x y j hxy hy)
266  simpa only [Finset.sum_add_distrib, Finset.sum_ite_eq'] using h
267 
268/-- Replacing the least larger letter increases exactly the intermediate
269weak prefixes. -/
270theorem rowReplace_prefix {d : ℕ} (A : RowContent d) (x y k : Fin d)
271    (hxy : x < y) (hy : 0 < A y) :
272    rowPrefix (rowReplace A x y) k = rowPrefix A k +
273      (if x ≤ k ∧ k < y then 1 else 0) := by
274  have h := rowReplace_sum A x y (ne_of_lt hxy) hy
275    (Finset.univ.filter (fun j => j ≤ k))
276  simp only [Finset.mem_filter, Finset.mem_univ, true_and] at h
277  change rowPrefix (rowReplace A x y) k + (if y ≤ k then 1 else 0) =
278    rowPrefix A k + (if x ≤ k then 1 else 0) at h
279  split_ifs at h ⊢ <;> omega
280 
281theorem rowReplace_strictPrefix {d : ℕ} (A : RowContent d) (x y k : Fin d)
282    (hxy : x < y) (hy : 0 < A y) :
283    rowStrictPrefix (rowReplace A x y) k = rowStrictPrefix A k +
284      (if x < k ∧ k ≤ y then 1 else 0) := by
285  have h := rowReplace_sum A x y (ne_of_lt hxy) hy
286    (Finset.univ.filter (fun j => j < k))
287  simp only [Finset.mem_filter, Finset.mem_univ, true_and] at h
288  change rowStrictPrefix (rowReplace A x y) k + (if y < k then 1 else 0) =
289    rowStrictPrefix A k + (if x < k then 1 else 0) at h
290  split_ifs at h ⊢ <;> omega
291 
292theorem rowAdd_content {d : ℕ} (A : RowContent d) (x j : Fin d) :
293    rowAdd A x j = A j + (if j = x then 1 else 0) := by
294  by_cases h : j = x
295  · subst j; simp [rowAdd]
296  · simp [rowAdd, h]
297 
298theorem rowAdd_prefix {d : ℕ} (A : RowContent d) (x k : Fin d) :
299    rowPrefix (rowAdd A x) k = rowPrefix A k + (if x ≤ k then 1 else 0) := by
300  simp only [rowPrefix, rowAdd_content, Finset.sum_add_distrib,
301    Finset.sum_ite_eq', Finset.mem_filter, Finset.mem_univ, true_and]
302 
303theorem rowAdd_strictPrefix {d : ℕ} (A : RowContent d) (x k : Fin d) :
304    rowStrictPrefix (rowAdd A x) k = rowStrictPrefix A k + (if x < k then 1 else 0) := by
305  simp only [rowStrictPrefix, rowAdd_content, Finset.sum_add_distrib,
306    Finset.sum_ite_eq', Finset.mem_filter, Finset.mem_univ, true_and]
307 
308theorem rowInserted_prefix_of_bump {d : ℕ} (A : RowContent d) (x y k : Fin d)
309    (h : rowBump A x = some y) :
310    rowPrefix (rowInserted A x) k = rowPrefix A k +
311      (if x ≤ k ∧ k < y then 1 else 0) := by
312  rw [rowInserted, h]
313  obtain ⟨hxy, hy, _⟩ := (rowBump_eq_some_iff A x y).mp h
314  exact rowReplace_prefix A x y k hxy hy
315 
316theorem rowInserted_prefix_of_none {d : ℕ} (A : RowContent d) (x k : Fin d)
317    (h : rowBump A x = none) :
318    rowPrefix (rowInserted A x) k = rowPrefix A k + (if x ≤ k then 1 else 0) := by
319  rw [rowInserted, h]
320  exact rowAdd_prefix A x k
321 
322theorem rowInserted_strictPrefix_of_bump {d : ℕ} (A : RowContent d) (x y k : Fin d)
323    (h : rowBump A x = some y) :
324    rowStrictPrefix (rowInserted A x) k = rowStrictPrefix A k +
325      (if x < k ∧ k ≤ y then 1 else 0) := by
326  rw [rowInserted, h]
327  obtain ⟨hxy, hy, _⟩ := (rowBump_eq_some_iff A x y).mp h
328  exact rowReplace_strictPrefix A x y k hxy hy
329 
330theorem rowInserted_strictPrefix_of_none {d : ℕ} (A : RowContent d) (x k : Fin d)
331    (h : rowBump A x = none) :
332    rowStrictPrefix (rowInserted A x) k = rowStrictPrefix A k + (if x < k then 1 else 0) := by
333  rw [rowInserted, h]
334  exact rowAdd_strictPrefix A x k
335 
336@[simp] theorem rowRemove_rowAdd {d : ℕ} (A : RowContent d) (x : Fin d) :
337    rowRemove (rowAdd A x) x = A := by
338  funext j
339  by_cases h : j = x
340  · subst j; simp [rowRemove, rowAdd]
341  · simp [rowRemove, rowAdd, h]
342 
343theorem rowAdd_rowRemove {d : ℕ} (A : RowContent d) (x : Fin d) (hx : 0 < A x) :
344    rowAdd (rowRemove A x) x = A := by
345  funext j
346  by_cases h : j = x
347  · subst j; simp only [rowRemove, rowAdd, Function.update_self]; omega
348  · simp [rowRemove, rowAdd, h]
349 
350def rowLast {d : ℕ} (A : RowContent d) : Option (Fin d) :=
351  if h : (Finset.univ.filter (fun x => 0 < A x)).Nonempty then
352    some ((Finset.univ.filter (fun x => 0 < A x)).max' h)
353  else none
354 
355def rowDeleteLast {d : ℕ} (A : RowContent d) : RowContent d :=
356  match rowLast A with
357  | none => A
358  | some x => rowRemove A x
359 
360theorem rowLast_eq_some_iff {d : ℕ} (A : RowContent d) (x : Fin d) :
361    rowLast A = some x ↔ 0 < A x ∧ ∀ y, 0 < A y → y ≤ x := by
362  classical
363  constructor
364  · intro he
365    unfold rowLast at he
366    split_ifs at he with hs
367    have he' := Option.some.inj he
368    have hm := Finset.max'_mem _ hs
369    rw [he'] at hm
370    refine ⟨(Finset.mem_filter.mp hm).2, ?_⟩
371    intro y hy
372    rw [← he']
373    exact Finset.le_max' _ _ (by simp [hy])
374  · rintro ⟨hx, hmax⟩
375    have hm : x ∈ Finset.univ.filter (fun y => 0 < A y) := by simp [hx]
376    have hs : (Finset.univ.filter (fun y => 0 < A y)).Nonempty := ⟨x, hm⟩
377    rw [rowLast, dif_pos hs]
378    congr 1
379    apply le_antisymm _ (Finset.le_max' _ _ hm)
380    apply Finset.max'_le
381    intro y hy
382    exact hmax y (Finset.mem_filter.mp hy).2
383 
384theorem rowLast_after_terminal_insert {d : ℕ} (A : RowContent d) (x : Fin d)
385    (h : rowBump A x = none) : rowLast (rowInserted A x) = some x := by
386  rw [rowInserted, h]
387  apply (rowLast_eq_some_iff _ _).mpr
388  refine ⟨by simp [rowAdd], ?_⟩
389  intro y hy
390  by_contra hxy
391  have hxy' : x < y := lt_of_not_ge hxy
392  have hyzero := (rowBump_eq_none_iff A x).mp h y hxy'
393  simp [rowAdd, ne_of_gt hxy', hyzero] at hy
394 
395theorem rowDeleteLast_after_terminal_insert {d : ℕ} (A : RowContent d) (x : Fin d)
396    (h : rowBump A x = none) : rowDeleteLast (rowInserted A x) = A := by
397  rw [rowDeleteLast, rowLast_after_terminal_insert A x h, rowInserted, h]
398  exact rowRemove_rowAdd A x
399 
400theorem rowBump_after_deleteLast {d : ℕ} (A : RowContent d) (x : Fin d)
401    (h : rowLast A = some x) : rowBump (rowDeleteLast A) x = none := by
402  rw [rowDeleteLast, h]
403  apply (rowBump_eq_none_iff _ _).mpr
404  intro y hxy
405  have hyzero : A y = 0 := by
406    have hmax := (rowLast_eq_some_iff A x).mp h
407    by_contra hy
408    exact (not_le_of_gt hxy) (hmax.2 y (Nat.pos_of_ne_zero hy))
409  simp [rowRemove, ne_of_gt hxy, hyzero]
410 
411theorem rowInserted_after_deleteLast {d : ℕ} (A : RowContent d) (x : Fin d)
412    (h : rowLast A = some x) : rowInserted (rowDeleteLast A) x = A := by
413  rw [rowInserted, rowBump_after_deleteLast A x h, rowDeleteLast, h]
414  exact rowAdd_rowRemove A x ((rowLast_eq_some_iff A x).mp h).1
415 
416theorem rowAdd_mass {d : ℕ} (A : RowContent d) (x : Fin d) :
417    (∑ j, rowAdd A x j) = (∑ j, A j) + 1 := by
418  simp [rowAdd_content, Finset.sum_add_distrib]
419 
420def rowMonomial {d : ℕ} (p : Fin d → ℝ) (A : RowContent d) : ℝ :=
421  ∏ j, p j ^ A j
422 
423/-- Multiplicative conservation has no division and remains valid at zero
424probabilities. -/
425theorem rowReplace_monomial {d : ℕ} (p : Fin d → ℝ) (A : RowContent d)
426    (x y : Fin d) (hxy : x ≠ y) (hy : 0 < A y) :
427    rowMonomial p (rowReplace A x y) * p y = rowMonomial p A * p x := by
428  have h := Finset.prod_congr (s₁ := Finset.univ) rfl
429    (fun j _ => congrArg (fun n : ℕ => p j ^ n) (rowReplace_content A x y j hxy hy))
430  simp only [pow_add, Finset.prod_mul_distrib] at h
431  simpa only [rowMonomial, apply_ite, pow_one, pow_zero, Finset.prod_ite_eq',
432    Finset.mem_univ, if_true] using h
433 
434theorem rowAdd_monomial {d : ℕ} (p : Fin d → ℝ) (A : RowContent d) (x : Fin d) :
435    rowMonomial p (rowAdd A x) = rowMonomial p A * p x := by
436  simp only [rowMonomial, rowAdd_content, pow_add, Finset.prod_mul_distrib]
437  simp only [apply_ite, pow_one, pow_zero, Finset.prod_ite_eq', Finset.mem_univ, if_true]
438 
439end Cloning.YoungGeneral