op_2130_terminal_discrepancy_le_prefix_max
For equally long request and orientation lists, the maximum absolute discrepancy in the final state is no greater than the maximum over all prefix states.
Statement
theorem op_2130_terminal_discrepancy_le_prefix_max (n : ℕ) (edges : List (Fin n × Fin n)) (signs : List Bool) (hlen : edges.length = signs.length) : max_vertex_discrepancy n ((edges.zip signs).foldl (fun d (e, b) => carpool_step n d e b) (fun _ => 0)) ≤ carpool_max_discrepancy n edges signs
- Source
- OP-2130: convert a final discrepancy witness into a prefix maximum witness
- Verified
- 22 Nov 2026
- Axioms
Classical.choiceQuot.soundpropext- Built on 4
- carpool_stepIn online carpooling on n vertices, given an edge e joining two distinct vertices and an orientation bit b, the discrepancy vector d…
- carpool_discrepanciesThe sequence of discrepancy vectors (d₀, ..., d_T) starting from the zero vector, where each d_t is updated from d_{t-1} using the t-th…
- carpool_max_discrepancyThe maximum discrepancy M_T over all time steps 0 ≤ t ≤ T of the maximum vertex discrepancy.
- max_vertex_discrepancyThe maximum absolute discrepancy among all vertices for an integer discrepancy vector d on n vertices.
- Used by 1
- op_2130_path_cascade_witnessFor every D ≥ 1 there is an edge sequence on SimpleGraph.pathGraph (2*D) of length at most D^3 and one coin tape whose greedy run reaches…
The proof, explained
The proof shows that the terminal discrepancy is bounded by the maximum prefix discrepancy by observing that the final state of a fold operation is always one of the intermediate states accumulated in the corresponding scan. Unfolding the definitions reduces the goal to showing that an element of a list of natural numbers is at most the maximum fold over that list, which is proved by induction. Finally, the terminal state is exhibited as an element of the prefix scan, ensuring its discrepancy appears in the list of prefix discrepancies and is thus bounded by their maximum.
The final value of folding f over a list is an element of the prefix scan list.
By induction on the list xs. For the empty list, folding and scanning both produce the initial accumulator a, so a ∈ [a]. For the inductive step x :: xs, the fold reduces to folding over xs with accumulator f a x, and the scan prepends a to the scan over xs with accumulator f a x. By the induction hypothesis, the fold result belongs to the tail scan, hence to the cons list.
uses
List.foldl_cons,List.scanl_cons,List.mem_cons_of_meminduction xs generalizing a with | nil => simp | cons x xs ih => simp only [List.foldl_cons, List.scanl_cons] exact List.mem_cons_of_mem _ (ih (f a x))
Folding max over a list of natural numbers starting from a is at least a.
By induction on xs. For the empty list, a ≤ a holds trivially. For x :: xs, the new accumulator is max a x; since a ≤ max a x by le_max_left and max a x ≤ xs.foldl max (max a x) by the induction hypothesis, transitivity (le_trans) gives a ≤ (x :: xs).foldl max a.
uses
List.foldl_cons,le_trans,le_max_leftinduction xs generalizing a with | nil => simp | cons x xs ih => simpa only [List.foldl_cons] using le_trans (le_max_left a x) (ih (max a x))
Any element of a list of natural numbers is bounded above by folding max over the list.
By induction on xs. The base case is impossible since x cannot belong to the empty list. For y :: ys, either x = y or x ∈ ys. In the first case, x ≤ max a x by le_max_right, which is bounded by ys.foldl max (max a x) via op2130_max_fold_ge, giving x ≤ (y :: ys).foldl max a by transitivity. In the second case, the induction hypothesis applied to ys with accumulator max a y directly gives the bound.
uses
List.mem_cons,List.foldl_cons,le_max_right,op2130_max_fold_ge,le_transinduction xs generalizing a with | nil => simp at hx | cons y ys ih => simp only [List.mem_cons] at hx simp only [List.foldl_cons] rcases hx with rfl | hx · exact le_trans (le_max_right a x) (op2130_max_fold_ge ys (max a x)) · exact ih (max a y) hx
Bounding the terminal discrepancy reduces to showing it belongs to the list of prefix discrepancies.
Unfolding carpool_max_discrepancy and carpool_discrepancies expresses the maximum prefix discrepancy as folding max over the discrepancies of all prefix states. By op2130_max_fold_member, any element of this list is bounded above by the maximum fold, so it suffices to show that the terminal discrepancy is an element of the mapped prefix discrepancies.
uses
op2130_max_fold_memberunfold carpool_max_discrepancy carpool_discrepancies apply op2130_max_fold_member
The terminal discrepancy is an element of the prefix discrepancies, completing the proof.
By List.mem_map.mpr, an element is in the image of map if it is the discrepancy of some state in the prefix scan. We witness the terminal state itself: by op2130_fold_member_scan, the final folded state is indeed an element of the scan list of prefix states, and its discrepancy is definitionally the terminal discrepancy.
uses
List.mem_map.mpr,op2130_fold_member_scanapply List.mem_map.mpr refine ⟨_, op2130_fold_member_scan _ _ _, rfl⟩
List.foldl_cons- Folding a function over a cons list evaluates the function on the accumulator and the head, then continues over the tail.
List.scanl_cons- Scanning a function over a cons list prepends the current accumulator to the scan over the tail starting from the updated accumulator.
List.mem_cons_of_mem- An element belonging to a list also belongs to any list formed by prepending an element to it.
le_trans- Inequality on preordered types is transitive: if a ≤ b and b ≤ c, then a ≤ c.
le_max_left- Any element is less than or equal to the maximum of itself and another element.
le_max_right- Any element is less than or equal to the maximum of another element and itself.
List.mem_cons- An element belongs to a cons list if and only if it equals the head or belongs to the tail.
op2130_max_fold_ge- Folding the maximum operation over a list of natural numbers with an initial accumulator a yields a result at least a.
op2130_max_fold_member- Every element of a list of natural numbers is less than or equal to the maximum fold over that list.
List.mem_map.mpr- An element belongs to the image of a list under a function if it is the image of some element in the list.
op2130_fold_member_scan- The final state obtained by folding a step function over a list is always an element of the prefix scan list.
Written by a model (gemini-3.8-flash) from the Lean proof and the goal states Lean reported at each step. Lean verified the proof; this explanation is a reading aid and is not itself machine-checked.
Lean source view module on GitHub
Choose a tactic — a marked line — to see the goals Lean had before and after it, as in Lean’s infoview. Step with ← → or j k.
private lemma op2130_fold_member_scan {α β : Type} (f : α → β → α) (xs : List β) (a : α) : xs.foldl f a ∈ xs.scanl f a := by induction xs generalizing a with | nil => simp | cons x xs ih => simp only [List.foldl_cons, List.scanl_cons] exact List.mem_cons_of_mem _ (ih (f a x))lemma op2130_max_fold_ge (xs : List ℕ) (a : ℕ) : a ≤ xs.foldl max a := by induction xs generalizing a with | nil => simp | cons x xs ih => simpa only [List.foldl_cons] using le_trans (le_max_left a x) (ih (max a x))lemma op2130_max_fold_member (xs : List ℕ) (a x : ℕ) (hx : x ∈ xs) : x ≤ xs.foldl max a := by induction xs generalizing a with | nil => simp at hx | cons y ys ih => simp only [List.mem_cons] at hx simp only [List.foldl_cons] rcases hx with rfl | hx · exact le_trans (le_max_right a x) (op2130_max_fold_ge ys (max a x)) · exact ih (max a y) hxtheorem op_2130_terminal_discrepancy_le_prefix_max (n : ℕ) (edges : List (Fin n × Fin n)) (signs : List Bool) (hlen : edges.length = signs.length) : max_vertex_discrepancy n ((edges.zip signs).foldl (fun d (e, b) => carpool_step n d e b) (fun _ => 0)) ≤ carpool_max_discrepancy n edges signs := by unfold carpool_max_discrepancy carpool_discrepancies apply op2130_max_fold_member apply List.mem_map.mpr refine ⟨_, op2130_fold_member_scan _ _ _, rfl⟩