AFTD Auto-Formalizing Theoretical Domains

op_2130_path_prefix_discrepancies_coin_monotone

theoremverified

For path requests written left to right, increasing any subset of the tie coins cannot decrease any prefix sum of the discrepancy vector at any time.

Statement

theorem op_2130_path_prefix_discrepancies_coin_monotone (n : ℕ) (edges : List (Fin n × Fin n)) (hpath : ∀ e ∈ edges, e.1.val + 1 = e.2.val) (r s : Fin edges.length → Bool) (hcoin : ∀ i, r i = false ∨ s i = true) (t k : ℕ) (ht : t ≤ edges.length) (hk : k ≤ n) : (∑ i ∈ Finset.univ.filter (fun i : Fin n => i.val < k), ((carpool_discrepancies n edges (carpool_greedy_orientations n edges (List.ofFn r))).getD t (fun _ => 0)) i) ≤ ∑ i ∈ Finset.univ.filter (fun i : Fin n => i.val < k), ((carpool_discrepancies n edges (carpool_greedy_orientations n edges (List.ofFn s))).getD t (fun _ => 0)) i
Source
OP-2130: attractive coupling for cumulative path discrepancies
Verified
22 Nov 2026
Axioms
Classical.choiceQuot.soundpropext
Built on 6
  • 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_greedy_orientationsGiven a list of edges and a list of tie-breaking coins, randomized greedy carpooling orients each edge toward the endpoint with smaller…
  • op_2130_path_height_update_monotoneA single cumulative-height update preserves coordinatewise order when the center heights have the same parity and the two tie-breaking…
  • TotalPreorderA total preorder: a preorder where ≤ is total (complete). This is weaker than LinearOrder — it does NOT require antisymmetry or…
  • LinearOrder.toTotalPreorderEvery LinearOrder is a TotalPreorder.
Used by 2
  • op2130_setup_replay_matches_carpoolFor every valid list of path-edge indices and coin bits, replaying the list-state greedy updates from zero agrees with the repository…
  • op2130_path_height_step_bridgeFor a left-to-right path request, the cumulative discrepancy profile after the greedy update is exactly the local height update at the…

The proof, explained

The theorem is proved by induction on the sequence of path requests (edges of the form (u, u+1)). By expressing the discrepancy vector in terms of its prefix sums, each carpool step on (u, u+1) leaves prefix sums outside the interval unchanged, while modifying the midpoint prefix sum at u+1 by ±1 according to whether it lies above or below the average of its adjacent prefix sums. This reduces the single-step evolution to a discrete Laplacian height-update operation (op_2130_path_height_update_monotone), which preserves both the ordering and modulo-2 parities of prefix sums when tie coins are increased. Starting from the zero initial discrepancy vector, where prefix sums are trivially ordered and equal in parity, monotonicity of prefix sums is inductively preserved at all times t.

  1. The prefix sum of any discrepancy vector up to index 0 is 0.

    By definition, P(d, 0) sums d(i) over all indices i in Fin n satisfying i < 0. Since indices are natural numbers, no such index exists, making the summation set empty and the sum 0.

    lines 6–10

    uses Finset.sum_eq_zero, Finset.mem_filter, Finset.mem_univ, true_and

      unfold op_2130_path_prefix_discrepancies_coin_monotone_pfx
      apply Finset.sum_eq_zero
      intro i hi
      simp only [Finset.mem_filter, Finset.mem_univ, true_and] at hi
      omega
  2. For any k < n, the prefix sum up to k + 1 equals the prefix sum up to k plus the k-th discrepancy d(k).

    The set of indices i < k + 1 decomposes into the disjoint union of {i | i < k} and the singleton {k}. Rewriting the index set as insert ⟨k, hk⟩ {i | i < k} and applying Finset.sum_insert, the sum splits into d(k) plus the sum over i < k, which is P(d, k) + d(k).

    lines 15–36

    uses Finset.mem_filter, Finset.mem_univ, true_and, Finset.mem_insert, Or.inr, Fin.ext, Finset.sum_insert

      unfold op_2130_path_prefix_discrepancies_coin_monotone_pfx
      have hset : Finset.univ.filter (fun i : Fin n => i.val < k+1) =
          insert ⟨k, hk⟩ (Finset.univ.filter (fun i : Fin n => i.val < k)) := by
        ext i
        simp only [Finset.mem_filter, Finset.mem_univ, true_and, Finset.mem_insert]
        constructor
        · intro h
          by_cases h' : i.val < k
          · exact Or.inr h'
          · left
            apply Fin.ext
            show i.val = k
            omega
        · intro h
          rcases h with h | h'
          · rw [h]; simp
          · omega
      rw [hset]
      have hnot : ⟨k, hk⟩ ∉ Finset.univ.filter (fun i : Fin n => i.val < k) := by
        simp
      rw [Finset.sum_insert hnot]
      ring
  3. After a carpool step on edge e = (u, v) with orientation b, the prefix sum at cut k equals P(d, k) plus an indicator contribution for u < k and an indicator contribution for v < k.

    Unfolding the definition of carpool_step and distributing the finite sum across additions, the step only modifies coordinates u and v. Applying Finset.sum_ite_eq', the single-point indicator sums collapse to testing whether u < k and v < k.

    lines 42–46

    uses Finset.sum_add_distrib, Finset.sum_ite_eq'

      unfold op_2130_path_prefix_discrepancies_coin_monotone_pfx carpool_step
      rw [Finset.sum_add_distrib, Finset.sum_add_distrib]
      congr 1
      · rw [Finset.sum_ite_eq']; simp
      · rw [Finset.sum_ite_eq']; simp
  4. For an adjacent edge e = (u, u+1), the prefix sum at cut k is unchanged if k ≤ u or k ≥ u + 2, and changes by +1 (if b) or -1 (if not b) when k = u + 1.

    For k ≤ u, neither u < k nor u+1 < k holds, so both update terms vanish. For k ≥ u + 2, both u < k and u+1 < k hold; the two updates (+1 and -1, or -1 and +1) cancel to 0. For k = u + 1, u < u + 1 holds while u+1 < u + 1 fails, so only vertex u contributes, adding +1 if b = true and -1 if b = false.

    lines 52–74

    uses op_2130_path_prefix_discrepancies_coin_monotone_pfx_step, if_neg, if_pos

      rw [op_2130_path_prefix_discrepancies_coin_monotone_pfx_step]
      have h1 : ¬ e.1.val < k := by omega
      have h2 : ¬ e.2.val < k := by omega
      rw [if_neg h1, if_neg h2]; ring
    
    lemma op_2130_path_prefix_discrepancies_coin_monotone_pfx_step_high {n : ℕ} (d : Fin n → ℤ) (e : Fin n × Fin n) (b : Bool) (k : ℕ)
        (h : e.2.val = e.1.val + 1) (hk : e.1.val + 2 ≤ k) :
        op_2130_path_prefix_discrepancies_coin_monotone_pfx (carpool_step n d e b) k =
          op_2130_path_prefix_discrepancies_coin_monotone_pfx d k := by
      rw [op_2130_path_prefix_discrepancies_coin_monotone_pfx_step]
      have h1 : e.1.val < k := by omega
      have h2 : e.2.val < k := by omega
      rw [if_pos h1, if_pos h2]
      cases b <;> simp
    
    lemma op_2130_path_prefix_discrepancies_coin_monotone_pfx_step_mid {n : ℕ} (d : Fin n → ℤ) (e : Fin n × Fin n) (b : Bool)
        (h : e.2.val = e.1.val + 1) :
        op_2130_path_prefix_discrepancies_coin_monotone_pfx (carpool_step n d e b) (e.1.val + 1) =
          op_2130_path_prefix_discrepancies_coin_monotone_pfx d (e.1.val + 1) + (if b then 1 else -1) := by
      rw [op_2130_path_prefix_discrepancies_coin_monotone_pfx_step]
      have h1 : e.1.val < e.1.val + 1 := by omega
      have h2 : ¬ e.2.val < e.1.val + 1 := by omega
      rw [if_pos h1, if_neg h2]; ring
  5. The greedy branching condition comparing differences a - b and c - a is algebraically equivalent to comparing 2 * a with b + c.

    By linear arithmetic over the integers, a - b < c - a is equivalent to 2 * a < b + c, and c - a < a - b is equivalent to b + c < 2 * a. A case split on these inequalities shows that the nested conditionals match in every branch.

    lines 79–87

      by_cases h1 : a - b < c - a
      · have h2 : 2 * a < b + c := by omega
        simp [h1, h2]
      · have h2 : ¬ 2 * a < b + c := by omega
        by_cases h3 : c - a < a - b
        · have h4 : b + c < 2 * a := by omega
          simp [h1, h2, h3, h4]
        · have h4 : ¬ b + c < 2 * a := by omega
          simp [h1, h2, h3, h4]
  6. For an adjacent edge e = (u, u+1), the greedy orientation bit and the resulting increment to P(d, u+1) are completely determined by comparing 2 * P(d, u+1) with P(d, u) + P(d, u+2).

    By the prefix recurrence, d(u) = P(d, u+1) - P(d, u) and d(u+1) = P(d, u+2) - P(d, u+1). Substituting these into the greedy condition, op_2130_path_prefix_discrepancies_coin_monotone_ite_arith transforms the comparison d(u) < d(u+1) into 2 * P(d, u+1) < P(d, u) + P(d, u+2). Evaluating the signed update (+1 if true, -1 if false) yields the discrete Laplacian update rule.

    lines 99–135

    uses op_2130_path_prefix_discrepancies_coin_monotone_pfx_succ, Fin.ext, op_2130_path_prefix_discrepancies_coin_monotone_ite_arith, op_2130_path_prefix_discrepancies_coin_monotone_greedy_bit_pfx

      have hi1 : e.1.val + 1 < n := by have := e.2.isLt; omega
      have hd1 : d e.1 = op_2130_path_prefix_discrepancies_coin_monotone_pfx d (e.1.val + 1) -
          op_2130_path_prefix_discrepancies_coin_monotone_pfx d e.1.val := by
        have hh := op_2130_path_prefix_discrepancies_coin_monotone_pfx_succ d (k := e.1.val) e.1.isLt
        change d ⟨e.1.val, e.1.isLt⟩ = op_2130_path_prefix_discrepancies_coin_monotone_pfx d (e.1.val + 1) -
          op_2130_path_prefix_discrepancies_coin_monotone_pfx d e.1.val
        rw [hh]; ring
      have hd2 : d e.2 = op_2130_path_prefix_discrepancies_coin_monotone_pfx d (e.1.val + 2) -
          op_2130_path_prefix_discrepancies_coin_monotone_pfx d (e.1.val + 1) := by
        have hh := op_2130_path_prefix_discrepancies_coin_monotone_pfx_succ d (k := e.1.val + 1) hi1
        have he2 : (⟨e.1.val + 1, hi1⟩ : Fin n) = e.2 := by
          apply Fin.ext; show e.1.val + 1 = e.2.val; omega
        rw [show e.1.val + 1 + 1 = e.1.val + 2 by ring] at hh
        rw [← he2]
        rw [hh]; ring
      rw [hd1, hd2, op_2130_path_prefix_discrepancies_coin_monotone_ite_arith]
    
    lemma op_2130_path_prefix_discrepancies_coin_monotone_greedy_step_inc {n : ℕ} (d : Fin n → ℤ) (e : Fin n × Fin n) (c : Bool)
        (h : e.2.val = e.1.val + 1) :
        (if (if d e.1 < d e.2 then true else if d e.2 < d e.1 then false else c) then (1:ℤ) else -1)
          = (if 2 * op_2130_path_prefix_discrepancies_coin_monotone_pfx d (e.1.val+1) <
                op_2130_path_prefix_discrepancies_coin_monotone_pfx d e.1.val +
                op_2130_path_prefix_discrepancies_coin_monotone_pfx d (e.1.val+2) then 1
             else if op_2130_path_prefix_discrepancies_coin_monotone_pfx d e.1.val +
                  op_2130_path_prefix_discrepancies_coin_monotone_pfx d (e.1.val+2) <
                  2 * op_2130_path_prefix_discrepancies_coin_monotone_pfx d (e.1.val+1) then -1
             else if c then 1 else -1) := by
      rw [op_2130_path_prefix_discrepancies_coin_monotone_greedy_bit_pfx d e c h]
      by_cases h1 : 2 * op_2130_path_prefix_discrepancies_coin_monotone_pfx d (e.1.val+1) <
          op_2130_path_prefix_discrepancies_coin_monotone_pfx d e.1.val +
          op_2130_path_prefix_discrepancies_coin_monotone_pfx d (e.1.val+2)
      · simp [h1]
      · by_cases h2 : op_2130_path_prefix_discrepancies_coin_monotone_pfx d e.1.val +
            op_2130_path_prefix_discrepancies_coin_monotone_pfx d (e.1.val+2) <
            2 * op_2130_path_prefix_discrepancies_coin_monotone_pfx d (e.1.val+1)
        · simp [h1, h2]
        · cases c <;> simp [h1, h2]
  7. A single greedy carpool step on an adjacent edge preserves prefix sum inequalities under coin increase, provided initial prefix sums are ordered and share modulo-2 parities.

    For k ≤ u and k ≥ u + 2, prefix sums are unchanged, so the assumed inequality P(d, k) ≤ P(d', k) persists. For k = u + 1, the prefix sums update according to the 1D discrete Laplacian height rule. Setting l = P(d, u), m = P(d, u+1), r = P(d, u+2) and similarly for d', the assumptions l ≤ L, m ≤ M, r ≤ R, m % 2 = M % 2, and coin = false ∨ coin' = true satisfy the hypotheses of op_2130_path_height_update_monotone, ensuring the updated midpoints remain ordered.

    lines 149–169

    uses op_2130_path_prefix_discrepancies_coin_monotone_pfx_step_low, op_2130_path_prefix_discrepancies_coin_monotone_pfx_step_high, op_2130_path_prefix_discrepancies_coin_monotone_pfx_step_mid, op_2130_path_prefix_discrepancies_coin_monotone_greedy_step_inc, op_2130_path_height_update_monotone

      intro k hk
      by_cases hlow : k ≤ e.1.val
      · rw [op_2130_path_prefix_discrepancies_coin_monotone_pfx_step_low d e _ k h hlow,
            op_2130_path_prefix_discrepancies_coin_monotone_pfx_step_low d' e _ k h hlow]
        exact hord k hk
      · by_cases hhigh : e.1.val + 2 ≤ k
        · rw [op_2130_path_prefix_discrepancies_coin_monotone_pfx_step_high d e _ k h hhigh,
              op_2130_path_prefix_discrepancies_coin_monotone_pfx_step_high d' e _ k h hhigh]
          exact hord k hk
        · have hk_eq : k = e.1.val + 1 := by omega
          subst hk_eq
          rw [op_2130_path_prefix_discrepancies_coin_monotone_pfx_step_mid d e _ h,
              op_2130_path_prefix_discrepancies_coin_monotone_pfx_step_mid d' e _ h]
          rw [op_2130_path_prefix_discrepancies_coin_monotone_greedy_step_inc d e coin h,
              op_2130_path_prefix_discrepancies_coin_monotone_greedy_step_inc d' e coin' h]
          exact op_2130_path_height_update_monotone _ _ _ _ _ _ coin coin'
            (hord e.1.val (by have := e.1.isLt; omega))
            (hord (e.1.val+1) (by have := e.2.isLt; omega))
            (hord (e.1.val+2) (by have := e.2.isLt; omega))
            (hpar (e.1.val+1) (by have := e.2.isLt; omega))
            hc
  8. A single carpool step on an adjacent edge preserves prefix sum parities modulo 2 across two configurations, regardless of the chosen orientation bits.

    For cuts k ≤ u and k ≥ u + 2, the prefix sums do not change, so their parities are trivially preserved. For cut k = u + 1, both prefix sums are updated by ±1. Because ±1 ≡ 1 (mod 2), both sides flip parity simultaneously, preserving parity equality.

    lines 178–192

    uses op_2130_path_prefix_discrepancies_coin_monotone_pfx_step_low, op_2130_path_prefix_discrepancies_coin_monotone_pfx_step_high, op_2130_path_prefix_discrepancies_coin_monotone_pfx_step_mid, Bool.false_eq_true, if_false, if_true

      intro k hk
      by_cases hlow : k ≤ e.1.val
      · rw [op_2130_path_prefix_discrepancies_coin_monotone_pfx_step_low d e _ k h hlow,
            op_2130_path_prefix_discrepancies_coin_monotone_pfx_step_low d' e _ k h hlow]
        exact hpar k hk
      · by_cases hhigh : e.1.val + 2 ≤ k
        · rw [op_2130_path_prefix_discrepancies_coin_monotone_pfx_step_high d e _ k h hhigh,
              op_2130_path_prefix_discrepancies_coin_monotone_pfx_step_high d' e _ k h hhigh]
          exact hpar k hk
        · have hk_eq : k = e.1.val + 1 := by omega
          subst hk_eq
          rw [op_2130_path_prefix_discrepancies_coin_monotone_pfx_step_mid d e b h,
              op_2130_path_prefix_discrepancies_coin_monotone_pfx_step_mid d' e b' h]
          have hp := hpar (e.1.val+1) (by have := e.2.isLt; omega)
          cases b <;> cases b' <;> simp only [Bool.false_eq_true, if_false, if_true] <;> omega
  9. The sequence of state discrepancies across time unfolds recursively as statesL, and starting from zero discrepancy equals carpool_discrepancies.

    Unfolding statesL on non-empty lists e :: es and c :: cs, carpool_greedy_orientations.aux.eq_def extracts the greedy orientation for edge e, and List.scanl_cons splits the left scan into the current state d followed by the scan of the remaining edges under the updated state. The correspondence with carpool_discrepancies holds definitionally.

    lines 202–211

    uses carpool_greedy_orientations.aux.eq_def, List.zip_cons_cons, List.scanl_cons

      unfold op_2130_path_prefix_discrepancies_coin_monotone_statesL
      rw [carpool_greedy_orientations.aux.eq_def]
      simp only [List.zip_cons_cons, List.scanl_cons]
    
    lemma op_2130_path_prefix_discrepancies_coin_monotone_discrepancies_statesL (n : ℕ) (edges : List (Fin n × Fin n)) (coins : List Bool) :
        carpool_discrepancies n edges (carpool_greedy_orientations n edges coins) =
          op_2130_path_prefix_discrepancies_coin_monotone_statesL n edges coins 0 := by
      simp only [carpool_discrepancies, carpool_greedy_orientations,
        op_2130_path_prefix_discrepancies_coin_monotone_statesL]
      rfl
  10. Prefix sum monotonicity is maintained at every time step t ≤ length(edges) along the sequence of path requests.

    By induction on the edge list. In the base case (empty edge list), t = 0 is the only valid time, where the prefix sums are the initial states d and d', ordered by assumption. In the inductive step with e :: es, if t = 0 the state is still the initial state. If t = t' + 1, edge e is processed: by single-step monotonicity and parity preservation, the updated states satisfy the ordering and parity hypotheses. Applying the induction hypothesis to es at time t' completes the induction.

    lines 228–293

    uses List.zip_nil_left, List.scanl_nil, List.getD_cons_zero, List.getD_cons_succ, op_2130_path_prefix_discrepancies_coin_monotone_greedy_step_pfx_le, op_2130_path_prefix_discrepancies_coin_monotone_greedy_step_pfx_par, op_2130_path_prefix_discrepancies_coin_monotone_statesL_cons

      induction edges generalizing r s d d' with
      | nil =>
        intro t ht k hk
        have hr0 : r = [] := List.length_eq_zero_iff.mp (by simpa using hr)
        have hs0 : s = [] := List.length_eq_zero_iff.mp (by simpa using hs)
        subst hr0; subst hs0
        have ht0 : t = 0 := by simpa using ht
        subst ht0
        simp only [op_2130_path_prefix_discrepancies_coin_monotone_statesL, List.zip_nil_left, List.scanl_nil, List.getD_cons_zero]
        exact hord k hk
      | cons e es ih =>
        intro t ht k hk
        cases r with
        | nil =>
          simp only [List.length_nil, List.length_cons] at hr; omega
        | cons c cs =>
          cases s with
          | nil =>
            simp only [List.length_nil, List.length_cons] at hs; omega
          | cons c' cs' =>
            simp only [List.length_cons] at hr hs
            have hcs : cs.length = es.length := by omega
            have hcs' : cs'.length = es.length := by omega
            have hpath' : ∀ e' ∈ es, e'.1.val + 1 = e'.2.val := by
              intro e' he'; exact hpath e' (List.mem_cons_of_mem e he')
            have hcoin_shift : ∀ i, cs.getD i false = false ∨ cs'.getD i true = true := by
              intro i
              have hh := hcoin (i+1)
              simpa only [List.getD_cons_succ] using hh
            have hc0 : c = false ∨ c' = true := by
              have hh := hcoin 0
              simpa only [List.getD_cons_zero] using hh
            have he : e.2.val = e.1.val + 1 := by
              have := hpath e (List.mem_cons_self); omega
            have hord1 : ∀ k, k ≤ n →
                op_2130_path_prefix_discrepancies_coin_monotone_pfx
                  (carpool_step n d e (if d e.1 < d e.2 then true else if d e.2 < d e.1 then false else c)) k
                ≤ op_2130_path_prefix_discrepancies_coin_monotone_pfx
                  (carpool_step n d' e (if d' e.1 < d' e.2 then true else if d' e.2 < d' e.1 then false else c')) k :=
              op_2130_path_prefix_discrepancies_coin_monotone_greedy_step_pfx_le e he c c' d d' hc0 hord hpar
            have hpar1 : ∀ k, k ≤ n →
                op_2130_path_prefix_discrepancies_coin_monotone_pfx
                  (carpool_step n d e (if d e.1 < d e.2 then true else if d e.2 < d e.1 then false else c)) k % 2
                = op_2130_path_prefix_discrepancies_coin_monotone_pfx
                  (carpool_step n d' e (if d' e.1 < d' e.2 then true else if d' e.2 < d' e.1 then false else c')) k % 2 :=
              op_2130_path_prefix_discrepancies_coin_monotone_greedy_step_pfx_par e he _ _ d d' hpar
            have hcons1 : op_2130_path_prefix_discrepancies_coin_monotone_statesL n (e :: es) (c :: cs) d =
                d :: op_2130_path_prefix_discrepancies_coin_monotone_statesL n es cs
                  (carpool_step n d e (if d e.1 < d e.2 then true else if d e.2 < d e.1 then false else c)) :=
              op_2130_path_prefix_discrepancies_coin_monotone_statesL_cons e es c cs d
            have hcons2 : op_2130_path_prefix_discrepancies_coin_monotone_statesL n (e :: es) (c' :: cs') d' =
                d' :: op_2130_path_prefix_discrepancies_coin_monotone_statesL n es cs'
                  (carpool_step n d' e (if d' e.1 < d' e.2 then true else if d' e.2 < d' e.1 then false else c')) :=
              op_2130_path_prefix_discrepancies_coin_monotone_statesL_cons e es c' cs' d'
            cases t with
            | zero =>
              rw [hcons1, hcons2]
              simp only [List.getD_cons_zero]
              exact hord k hk
            | succ t' =>
              have ht' : t' ≤ es.length := by
                simp only [List.length_cons] at ht
                omega
              rw [hcons1, hcons2]
              simp only [List.getD_cons_succ]
              exact ih hpath' cs cs' hcs hcs' hcoin_shift _ _ hord1 hpar1 t' ht' k hk
  11. Increasing any subset of tie coins cannot decrease any prefix sum of the discrepancy vector at any time t.

    The coin hypothesis ∀ i, r i = false ∨ s i = true is converted to the list-level property via List.ofFn and List.getD. Specializing the inductive invariant op_2130_path_prefix_discrepancies_coin_monotone_statesL_aux_le to the all-zero initial discrepancy vector—where 0 ≤ 0 and 0 % 2 = 0 % 2 hold trivially—and rewriting the discrepancies in terms of statesL yields the theorem.

    lines 296–312

    uses List.getD_eq_getElem, List.getElem_ofFn, List.getD_eq_default, Nat.le_of_not_lt, op_2130_path_prefix_discrepancies_coin_monotone_statesL_aux_le, le_rfl, op_2130_path_prefix_discrepancies_coin_monotone_discrepancies_statesL

      have hcoin' : ∀ i, (List.ofFn r).getD i false = false ∨ (List.ofFn s).getD i true = true := by
        intro i
        by_cases hi : i < edges.length
        · have h1 : (List.ofFn r).getD i false = r ⟨i, hi⟩ := by
            rw [List.getD_eq_getElem _ _ (by simpa using hi), List.getElem_ofFn]
          have h2 : (List.ofFn s).getD i true = s ⟨i, hi⟩ := by
            rw [List.getD_eq_getElem _ _ (by simpa using hi), List.getElem_ofFn]
          rw [h1, h2]
          exact hcoin ⟨i, hi⟩
        · left
          rw [List.getD_eq_default _ _ (by simpa using Nat.le_of_not_lt hi)]
      have hres := op_2130_path_prefix_discrepancies_coin_monotone_statesL_aux_le n edges hpath (List.ofFn r) (List.ofFn s)
        (by simp) (by simp) hcoin' (fun _ => 0) (fun _ => 0)
        (fun k _ => le_rfl) (fun k _ => rfl) t ht k hk
      rw [op_2130_path_prefix_discrepancies_coin_monotone_discrepancies_statesL,
          op_2130_path_prefix_discrepancies_coin_monotone_discrepancies_statesL]
      exact hres
Finset.sum_eq_zero
A finite sum is 0 if every summand in the index set is 0.
Finset.mem_filter
An element belongs to a filtered finset if and only if it belongs to the set and satisfies the predicate.
Finset.mem_univ
Every element of a finite type belongs to the universal finset Finset.univ.
true_and
The proposition True ∧ p is equivalent to p.
Finset.mem_insert
An element belongs to insert b s if and only if it equals b or belongs to s.
Or.inr
If b holds, then a ∨ b holds.
Fin.ext
Two Fin n elements are equal if and only if their underlying natural values are equal.
Finset.sum_insert
Inserting an element a ∉ s into a finset adds f(a) to the sum of f over s.
Finset.sum_add_distrib
The sum of a sum of functions equals the sum of their individual sums.
Finset.sum_ite_eq'
Summing a single-point indicator over a finset yields the value at that point if present, and 0 otherwise.
op_2130_path_prefix_discrepancies_coin_monotone_pfx_step
After a step on edge e = (u, v) with sign b, the prefix sum up to k changes by ±1 depending on whether u < k and v < k.
if_neg
If condition c is false, then `if c then t else e` evaluates to e.
if_pos
If condition c is true, then `if c then t else e` evaluates to t.
op_2130_path_prefix_discrepancies_coin_monotone_pfx_succ
The prefix sum up to k + 1 equals the prefix sum up to k plus the k-th discrepancy d(k).
op_2130_path_prefix_discrepancies_coin_monotone_ite_arith
Comparing differences a - b and c - a is equivalent to comparing 2 * a with b + c.
op_2130_path_prefix_discrepancies_coin_monotone_greedy_bit_pfx
For an adjacent edge (u, u+1), the greedy orientation bit equals the comparison of 2 * P(d, u+1) with P(d, u) + P(d, u+2).
op_2130_path_prefix_discrepancies_coin_monotone_pfx_step_low
For an adjacent edge e = (u, u+1), any prefix sum at cut k ≤ u is unchanged by the step.
op_2130_path_prefix_discrepancies_coin_monotone_pfx_step_high
For an adjacent edge e = (u, u+1), any prefix sum at cut k ≥ u+2 is unchanged by the step.
op_2130_path_prefix_discrepancies_coin_monotone_pfx_step_mid
For an adjacent edge e = (u, u+1), the prefix sum at u+1 changes by +1 if b is true and -1 if b is false.
op_2130_path_prefix_discrepancies_coin_monotone_greedy_step_inc
The signed increment ±1 at u+1 under greedy orientation equals the height update increment determined by 2 * P(d, u+1) versus P(d, u) + P(d, u+2).
op_2130_path_height_update_monotone
The discrete Laplacian height update operation is monotone under coordinatewise bounds, equal parity, and coin increase.
Bool.false_eq_true
The boolean false does not equal true.
if_false
An if-then-else whose condition is False reduces to its else branch.
if_true
An if-then-else whose condition is True reduces to its then branch.
carpool_greedy_orientations.aux.eq_def
Equation lemma unfolding carpool_greedy_orientations.aux on cons lists.
List.zip_cons_cons
Zipping two cons lists pairs their heads and zips their tails.
List.scanl_cons
A left scan of a cons list yields the initial value followed by scanning the tail.
List.zip_nil_left
Zipping an empty list with any list gives an empty list.
List.scanl_nil
A left scan of an empty list yields a singleton list of the initial accumulator.
List.getD_cons_zero
The 0-th element with default of a non-empty list is its head.
List.getD_cons_succ
The (n+1)-th element with default of a non-empty list is the n-th element of its tail.
op_2130_path_prefix_discrepancies_coin_monotone_greedy_step_pfx_le
A single greedy step on an adjacent edge preserves prefix sum inequalities if initial prefix sums are ordered and have matching parities.
op_2130_path_prefix_discrepancies_coin_monotone_greedy_step_pfx_par
A single carpool step on an adjacent edge preserves modulo-2 prefix sum parities across two runs regardless of chosen orientations.
op_2130_path_prefix_discrepancies_coin_monotone_statesL_cons
The state list on a cons of edges begins with the initial state followed by the scan of the tail under the updated state.
List.getD_eq_getElem
If index n is strictly within bounds of list l, l.getD n d equals the n-th element l[n].
List.getElem_ofFn
Indexing into List.ofFn f at i yields the value f(i).
List.getD_eq_default
If index n is at or beyond the length of list l, l.getD n d returns default d.
Nat.le_of_not_lt
If a is not strictly less than b, then b ≤ a.
op_2130_path_prefix_discrepancies_coin_monotone_statesL_aux_le
Prefix sum inequalities and parities are preserved at all steps t along a sequence of adjacent edges under coin increase.
le_rfl
The reflexive property of ≤: any element is less than or equal to itself.
op_2130_path_prefix_discrepancies_coin_monotone_discrepancies_statesL
The greedy carpool discrepancies list equals the state trajectory starting from the all-zero discrepancy vector.

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

def op_2130_path_prefix_discrepancies_coin_monotone_pfx {n : ℕ} (d : Fin n → ℤ) (k : ℕ) : ℤ :=  ∑ i ∈ Finset.univ.filter (fun i : Fin n => i.val < k), d ilemma op_2130_path_prefix_discrepancies_coin_monotone_pfx_zero {n : ℕ} (d : Fin n → ℤ) :    op_2130_path_prefix_discrepancies_coin_monotone_pfx d 0 = 0 := by  unfold op_2130_path_prefix_discrepancies_coin_monotone_pfx  apply Finset.sum_eq_zero  intro i hi  simp only [Finset.mem_filter, Finset.mem_univ, true_and] at hi  omegalemma op_2130_path_prefix_discrepancies_coin_monotone_pfx_succ {n : ℕ} (d : Fin n → ℤ) {k : ℕ} (hk : k < n) :    op_2130_path_prefix_discrepancies_coin_monotone_pfx d (k+1) =      op_2130_path_prefix_discrepancies_coin_monotone_pfx d k + d ⟨k, hk⟩ := by  unfold op_2130_path_prefix_discrepancies_coin_monotone_pfx  have hset : Finset.univ.filter (fun i : Fin n => i.val < k+1) =      insert ⟨k, hk⟩ (Finset.univ.filter (fun i : Fin n => i.val < k)) := by    ext i    simp only [Finset.mem_filter, Finset.mem_univ, true_and, Finset.mem_insert]    constructor    · intro h      by_cases h' : i.val < k      · exact Or.inr h'      · left        apply Fin.ext        show i.val = k        omega    · intro h      rcases h with h | h'      · rw [h]; simp      · omega  rw [hset]  have hnot : ⟨k, hk⟩ ∉ Finset.univ.filter (fun i : Fin n => i.val < k) := by    simp  rw [Finset.sum_insert hnot]  ringlemma op_2130_path_prefix_discrepancies_coin_monotone_pfx_step {n : ℕ} (d : Fin n → ℤ) (e : Fin n × Fin n) (b : Bool) (k : ℕ) :    op_2130_path_prefix_discrepancies_coin_monotone_pfx (carpool_step n d e b) k =      op_2130_path_prefix_discrepancies_coin_monotone_pfx d k + (if e.1.val < k then (if b then 1 else -1) else 0)        + (if e.2.val < k then (if b then -1 else 1) else 0) := by  unfold op_2130_path_prefix_discrepancies_coin_monotone_pfx carpool_step  rw [Finset.sum_add_distrib, Finset.sum_add_distrib]  congr 1  · rw [Finset.sum_ite_eq']; simp  · rw [Finset.sum_ite_eq']; simplemma op_2130_path_prefix_discrepancies_coin_monotone_pfx_step_low {n : ℕ} (d : Fin n → ℤ) (e : Fin n × Fin n) (b : Bool) (k : ℕ)    (h : e.2.val = e.1.val + 1) (hk : k ≤ e.1.val) :    op_2130_path_prefix_discrepancies_coin_monotone_pfx (carpool_step n d e b) k =      op_2130_path_prefix_discrepancies_coin_monotone_pfx d k := by  rw [op_2130_path_prefix_discrepancies_coin_monotone_pfx_step]  have h1 : ¬ e.1.val < k := by omega  have h2 : ¬ e.2.val < k := by omega  rw [if_neg h1, if_neg h2]; ringlemma op_2130_path_prefix_discrepancies_coin_monotone_pfx_step_high {n : ℕ} (d : Fin n → ℤ) (e : Fin n × Fin n) (b : Bool) (k : ℕ)    (h : e.2.val = e.1.val + 1) (hk : e.1.val + 2 ≤ k) :    op_2130_path_prefix_discrepancies_coin_monotone_pfx (carpool_step n d e b) k =      op_2130_path_prefix_discrepancies_coin_monotone_pfx d k := by  rw [op_2130_path_prefix_discrepancies_coin_monotone_pfx_step]  have h1 : e.1.val < k := by omega  have h2 : e.2.val < k := by omega  rw [if_pos h1, if_pos h2]  cases b <;> simplemma op_2130_path_prefix_discrepancies_coin_monotone_pfx_step_mid {n : ℕ} (d : Fin n → ℤ) (e : Fin n × Fin n) (b : Bool)    (h : e.2.val = e.1.val + 1) :    op_2130_path_prefix_discrepancies_coin_monotone_pfx (carpool_step n d e b) (e.1.val + 1) =      op_2130_path_prefix_discrepancies_coin_monotone_pfx d (e.1.val + 1) + (if b then 1 else -1) := by  rw [op_2130_path_prefix_discrepancies_coin_monotone_pfx_step]  have h1 : e.1.val < e.1.val + 1 := by omega  have h2 : ¬ e.2.val < e.1.val + 1 := by omega  rw [if_pos h1, if_neg h2]; ringlemma op_2130_path_prefix_discrepancies_coin_monotone_ite_arith (a b c : ℤ) (coin : Bool) :    (if a - b < c - a then true else if c - a < a - b then false else coin)      = (if 2 * a < b + c then true else if b + c < 2 * a then false else coin) := by  by_cases h1 : a - b < c - a  · have h2 : 2 * a < b + c := by omega    simp [h1, h2]  · have h2 : ¬ 2 * a < b + c := by omega    by_cases h3 : c - a < a - b    · have h4 : b + c < 2 * a := by omega      simp [h1, h2, h3, h4]    · have h4 : ¬ b + c < 2 * a := by omega      simp [h1, h2, h3, h4]lemma op_2130_path_prefix_discrepancies_coin_monotone_greedy_bit_pfx {n : ℕ} (d : Fin n → ℤ) (e : Fin n × Fin n) (c : Bool)    (h : e.2.val = e.1.val + 1) :    (if d e.1 < d e.2 then true else if d e.2 < d e.1 then false else c)      = (if 2 * op_2130_path_prefix_discrepancies_coin_monotone_pfx d (e.1.val + 1) <            op_2130_path_prefix_discrepancies_coin_monotone_pfx d e.1.val +            op_2130_path_prefix_discrepancies_coin_monotone_pfx d (e.1.val + 2) then true         else if op_2130_path_prefix_discrepancies_coin_monotone_pfx d e.1.val +              op_2130_path_prefix_discrepancies_coin_monotone_pfx d (e.1.val + 2) <              2 * op_2130_path_prefix_discrepancies_coin_monotone_pfx d (e.1.val + 1) then false         else c) := by  have hi1 : e.1.val + 1 < n := by have := e.2.isLt; omega  have hd1 : d e.1 = op_2130_path_prefix_discrepancies_coin_monotone_pfx d (e.1.val + 1) -      op_2130_path_prefix_discrepancies_coin_monotone_pfx d e.1.val := by    have hh := op_2130_path_prefix_discrepancies_coin_monotone_pfx_succ d (k := e.1.val) e.1.isLt    change d ⟨e.1.val, e.1.isLt⟩ = op_2130_path_prefix_discrepancies_coin_monotone_pfx d (e.1.val + 1) -      op_2130_path_prefix_discrepancies_coin_monotone_pfx d e.1.val    rw [hh]; ring  have hd2 : d e.2 = op_2130_path_prefix_discrepancies_coin_monotone_pfx d (e.1.val + 2) -      op_2130_path_prefix_discrepancies_coin_monotone_pfx d (e.1.val + 1) := by    have hh := op_2130_path_prefix_discrepancies_coin_monotone_pfx_succ d (k := e.1.val + 1) hi1    have he2 : (⟨e.1.val + 1, hi1⟩ : Fin n) = e.2 := by      apply Fin.ext; show e.1.val + 1 = e.2.val; omega    rw [show e.1.val + 1 + 1 = e.1.val + 2 by ring] at hh    rw [← he2]    rw [hh]; ring  rw [hd1, hd2, op_2130_path_prefix_discrepancies_coin_monotone_ite_arith]lemma op_2130_path_prefix_discrepancies_coin_monotone_greedy_step_inc {n : ℕ} (d : Fin n → ℤ) (e : Fin n × Fin n) (c : Bool)    (h : e.2.val = e.1.val + 1) :    (if (if d e.1 < d e.2 then true else if d e.2 < d e.1 then false else c) then (1:ℤ) else -1)      = (if 2 * op_2130_path_prefix_discrepancies_coin_monotone_pfx d (e.1.val+1) <            op_2130_path_prefix_discrepancies_coin_monotone_pfx d e.1.val +            op_2130_path_prefix_discrepancies_coin_monotone_pfx d (e.1.val+2) then 1         else if op_2130_path_prefix_discrepancies_coin_monotone_pfx d e.1.val +              op_2130_path_prefix_discrepancies_coin_monotone_pfx d (e.1.val+2) <              2 * op_2130_path_prefix_discrepancies_coin_monotone_pfx d (e.1.val+1) then -1         else if c then 1 else -1) := by  rw [op_2130_path_prefix_discrepancies_coin_monotone_greedy_bit_pfx d e c h]  by_cases h1 : 2 * op_2130_path_prefix_discrepancies_coin_monotone_pfx d (e.1.val+1) <      op_2130_path_prefix_discrepancies_coin_monotone_pfx d e.1.val +      op_2130_path_prefix_discrepancies_coin_monotone_pfx d (e.1.val+2)  · simp [h1]  · by_cases h2 : op_2130_path_prefix_discrepancies_coin_monotone_pfx d e.1.val +        op_2130_path_prefix_discrepancies_coin_monotone_pfx d (e.1.val+2) <        2 * op_2130_path_prefix_discrepancies_coin_monotone_pfx d (e.1.val+1)    · simp [h1, h2]    · cases c <;> simp [h1, h2]lemma op_2130_path_prefix_discrepancies_coin_monotone_greedy_step_pfx_le {n : ℕ} (e : Fin n × Fin n) (h : e.2.val = e.1.val + 1)    (coin coin' : Bool) (d d' : Fin n → ℤ)    (hc : coin = false ∨ coin' = true)    (hord : ∀ k, k ≤ n → op_2130_path_prefix_discrepancies_coin_monotone_pfx d k ≤      op_2130_path_prefix_discrepancies_coin_monotone_pfx d' k)    (hpar : ∀ k, k ≤ n → op_2130_path_prefix_discrepancies_coin_monotone_pfx d k % 2 =      op_2130_path_prefix_discrepancies_coin_monotone_pfx d' k % 2) :    ∀ k, k ≤ n →      op_2130_path_prefix_discrepancies_coin_monotone_pfx        (carpool_step n d e (if d e.1 < d e.2 then true else if d e.2 < d e.1 then false else coin)) k      ≤ op_2130_path_prefix_discrepancies_coin_monotone_pfx        (carpool_step n d' e (if d' e.1 < d' e.2 then true else if d' e.2 < d' e.1 then false else coin')) k := by  intro k hk  by_cases hlow : k ≤ e.1.val  · rw [op_2130_path_prefix_discrepancies_coin_monotone_pfx_step_low d e _ k h hlow,        op_2130_path_prefix_discrepancies_coin_monotone_pfx_step_low d' e _ k h hlow]    exact hord k hk  · by_cases hhigh : e.1.val + 2 ≤ k    · rw [op_2130_path_prefix_discrepancies_coin_monotone_pfx_step_high d e _ k h hhigh,          op_2130_path_prefix_discrepancies_coin_monotone_pfx_step_high d' e _ k h hhigh]      exact hord k hk    · have hk_eq : k = e.1.val + 1 := by omega      subst hk_eq      rw [op_2130_path_prefix_discrepancies_coin_monotone_pfx_step_mid d e _ h,          op_2130_path_prefix_discrepancies_coin_monotone_pfx_step_mid d' e _ h]      rw [op_2130_path_prefix_discrepancies_coin_monotone_greedy_step_inc d e coin h,          op_2130_path_prefix_discrepancies_coin_monotone_greedy_step_inc d' e coin' h]      exact op_2130_path_height_update_monotone _ _ _ _ _ _ coin coin'        (hord e.1.val (by have := e.1.isLt; omega))        (hord (e.1.val+1) (by have := e.2.isLt; omega))        (hord (e.1.val+2) (by have := e.2.isLt; omega))        (hpar (e.1.val+1) (by have := e.2.isLt; omega))        hclemma op_2130_path_prefix_discrepancies_coin_monotone_greedy_step_pfx_par {n : ℕ} (e : Fin n × Fin n) (h : e.2.val = e.1.val + 1)    (b b' : Bool) (d d' : Fin n → ℤ)    (hpar : ∀ k, k ≤ n → op_2130_path_prefix_discrepancies_coin_monotone_pfx d k % 2 =      op_2130_path_prefix_discrepancies_coin_monotone_pfx d' k % 2) :    ∀ k, k ≤ n →      op_2130_path_prefix_discrepancies_coin_monotone_pfx (carpool_step n d e b) k % 2 =        op_2130_path_prefix_discrepancies_coin_monotone_pfx (carpool_step n d' e b') k % 2 := by  intro k hk  by_cases hlow : k ≤ e.1.val  · rw [op_2130_path_prefix_discrepancies_coin_monotone_pfx_step_low d e _ k h hlow,        op_2130_path_prefix_discrepancies_coin_monotone_pfx_step_low d' e _ k h hlow]    exact hpar k hk  · by_cases hhigh : e.1.val + 2 ≤ k    · rw [op_2130_path_prefix_discrepancies_coin_monotone_pfx_step_high d e _ k h hhigh,          op_2130_path_prefix_discrepancies_coin_monotone_pfx_step_high d' e _ k h hhigh]      exact hpar k hk    · have hk_eq : k = e.1.val + 1 := by omega      subst hk_eq      rw [op_2130_path_prefix_discrepancies_coin_monotone_pfx_step_mid d e b h,          op_2130_path_prefix_discrepancies_coin_monotone_pfx_step_mid d' e b' h]      have hp := hpar (e.1.val+1) (by have := e.2.isLt; omega)      cases b <;> cases b' <;> simp only [Bool.false_eq_true, if_false, if_true] <;> omegadef op_2130_path_prefix_discrepancies_coin_monotone_statesL (n : ℕ) (edges : List (Fin n × Fin n)) (coins : List Bool) (d : Fin n → ℤ) :    List (Fin n → ℤ) :=  List.scanl (fun d x => carpool_step n d x.1 x.2) d (edges.zip (carpool_greedy_orientations.aux n edges coins d))lemma op_2130_path_prefix_discrepancies_coin_monotone_statesL_cons {n : ℕ} (e : Fin n × Fin n) (es : List (Fin n × Fin n)) (c : Bool) (cs : List Bool) (d : Fin n → ℤ) :    op_2130_path_prefix_discrepancies_coin_monotone_statesL n (e :: es) (c :: cs) d =      d :: op_2130_path_prefix_discrepancies_coin_monotone_statesL n es cs        (carpool_step n d e (if d e.1 < d e.2 then true else if d e.2 < d e.1 then false else c)) := by  unfold op_2130_path_prefix_discrepancies_coin_monotone_statesL  rw [carpool_greedy_orientations.aux.eq_def]  simp only [List.zip_cons_cons, List.scanl_cons]lemma op_2130_path_prefix_discrepancies_coin_monotone_discrepancies_statesL (n : ℕ) (edges : List (Fin n × Fin n)) (coins : List Bool) :    carpool_discrepancies n edges (carpool_greedy_orientations n edges coins) =      op_2130_path_prefix_discrepancies_coin_monotone_statesL n edges coins 0 := by  simp only [carpool_discrepancies, carpool_greedy_orientations,    op_2130_path_prefix_discrepancies_coin_monotone_statesL]  rfllemma op_2130_path_prefix_discrepancies_coin_monotone_statesL_aux_le    (n : ℕ) (edges : List (Fin n × Fin n))    (hpath : ∀ e ∈ edges, e.1.val + 1 = e.2.val)    (r s : List Bool) (hr : r.length = edges.length) (hs : s.length = edges.length)    (hcoin : ∀ i, r.getD i false = false ∨ s.getD i true = true)    (d d' : Fin n → ℤ)    (hord : ∀ k, k ≤ n → op_2130_path_prefix_discrepancies_coin_monotone_pfx d k ≤      op_2130_path_prefix_discrepancies_coin_monotone_pfx d' k)    (hpar : ∀ k, k ≤ n → op_2130_path_prefix_discrepancies_coin_monotone_pfx d k % 2 =      op_2130_path_prefix_discrepancies_coin_monotone_pfx d' k % 2) :    ∀ t, t ≤ edges.length → ∀ k, k ≤ n →      op_2130_path_prefix_discrepancies_coin_monotone_pfx        ((op_2130_path_prefix_discrepancies_coin_monotone_statesL n edges r d).getD t (fun _ => 0)) k        ≤ op_2130_path_prefix_discrepancies_coin_monotone_pfx        ((op_2130_path_prefix_discrepancies_coin_monotone_statesL n edges s d').getD t (fun _ => 0)) k := by  induction edges generalizing r s d d' with  | nil =>    intro t ht k hk    have hr0 : r = [] := List.length_eq_zero_iff.mp (by simpa using hr)    have hs0 : s = [] := List.length_eq_zero_iff.mp (by simpa using hs)    subst hr0; subst hs0    have ht0 : t = 0 := by simpa using ht    subst ht0    simp only [op_2130_path_prefix_discrepancies_coin_monotone_statesL, List.zip_nil_left, List.scanl_nil, List.getD_cons_zero]    exact hord k hk  | cons e es ih =>    intro t ht k hk    cases r with    | nil =>      simp only [List.length_nil, List.length_cons] at hr; omega    | cons c cs =>      cases s with      | nil =>        simp only [List.length_nil, List.length_cons] at hs; omega      | cons c' cs' =>        simp only [List.length_cons] at hr hs        have hcs : cs.length = es.length := by omega        have hcs' : cs'.length = es.length := by omega        have hpath' : ∀ e' ∈ es, e'.1.val + 1 = e'.2.val := by          intro e' he'; exact hpath e' (List.mem_cons_of_mem e he')        have hcoin_shift : ∀ i, cs.getD i false = false ∨ cs'.getD i true = true := by          intro i          have hh := hcoin (i+1)          simpa only [List.getD_cons_succ] using hh        have hc0 : c = false ∨ c' = true := by          have hh := hcoin 0          simpa only [List.getD_cons_zero] using hh        have he : e.2.val = e.1.val + 1 := by          have := hpath e (List.mem_cons_self); omega        have hord1 : ∀ k, k ≤ n →            op_2130_path_prefix_discrepancies_coin_monotone_pfx              (carpool_step n d e (if d e.1 < d e.2 then true else if d e.2 < d e.1 then false else c)) k            ≤ op_2130_path_prefix_discrepancies_coin_monotone_pfx              (carpool_step n d' e (if d' e.1 < d' e.2 then true else if d' e.2 < d' e.1 then false else c')) k :=          op_2130_path_prefix_discrepancies_coin_monotone_greedy_step_pfx_le e he c c' d d' hc0 hord hpar        have hpar1 : ∀ k, k ≤ n →            op_2130_path_prefix_discrepancies_coin_monotone_pfx              (carpool_step n d e (if d e.1 < d e.2 then true else if d e.2 < d e.1 then false else c)) k % 2            = op_2130_path_prefix_discrepancies_coin_monotone_pfx              (carpool_step n d' e (if d' e.1 < d' e.2 then true else if d' e.2 < d' e.1 then false else c')) k % 2 :=          op_2130_path_prefix_discrepancies_coin_monotone_greedy_step_pfx_par e he _ _ d d' hpar        have hcons1 : op_2130_path_prefix_discrepancies_coin_monotone_statesL n (e :: es) (c :: cs) d =            d :: op_2130_path_prefix_discrepancies_coin_monotone_statesL n es cs              (carpool_step n d e (if d e.1 < d e.2 then true else if d e.2 < d e.1 then false else c)) :=          op_2130_path_prefix_discrepancies_coin_monotone_statesL_cons e es c cs d        have hcons2 : op_2130_path_prefix_discrepancies_coin_monotone_statesL n (e :: es) (c' :: cs') d' =            d' :: op_2130_path_prefix_discrepancies_coin_monotone_statesL n es cs'              (carpool_step n d' e (if d' e.1 < d' e.2 then true else if d' e.2 < d' e.1 then false else c')) :=          op_2130_path_prefix_discrepancies_coin_monotone_statesL_cons e es c' cs' d'        cases t with        | zero =>          rw [hcons1, hcons2]          simp only [List.getD_cons_zero]          exact hord k hk        | succ t' =>          have ht' : t' ≤ es.length := by            simp only [List.length_cons] at ht            omega          rw [hcons1, hcons2]          simp only [List.getD_cons_succ]          exact ih hpath' cs cs' hcs hcs' hcoin_shift _ _ hord1 hpar1 t' ht' k hktheorem op_2130_path_prefix_discrepancies_coin_monotone (n : ℕ) (edges : List (Fin n × Fin n)) (hpath : ∀ e ∈ edges, e.1.val + 1 = e.2.val) (r s : Fin edges.length → Bool) (hcoin : ∀ i, r i = false ∨ s i = true) (t k : ℕ) (ht : t ≤ edges.length) (hk : k ≤ n) : (∑ i ∈ Finset.univ.filter (fun i : Fin n => i.val < k), ((carpool_discrepancies n edges (carpool_greedy_orientations n edges (List.ofFn r))).getD t (fun _ => 0)) i) ≤ ∑ i ∈ Finset.univ.filter (fun i : Fin n => i.val < k), ((carpool_discrepancies n edges (carpool_greedy_orientations n edges (List.ofFn s))).getD t (fun _ => 0)) i := by  have hcoin' : ∀ i, (List.ofFn r).getD i false = false ∨ (List.ofFn s).getD i true = true := by    intro i    by_cases hi : i < edges.length    · have h1 : (List.ofFn r).getD i false = r ⟨i, hi⟩ := by        rw [List.getD_eq_getElem _ _ (by simpa using hi), List.getElem_ofFn]      have h2 : (List.ofFn s).getD i true = s ⟨i, hi⟩ := by        rw [List.getD_eq_getElem _ _ (by simpa using hi), List.getElem_ofFn]      rw [h1, h2]      exact hcoin ⟨i, hi⟩    · left      rw [List.getD_eq_default _ _ (by simpa using Nat.le_of_not_lt hi)]  have hres := op_2130_path_prefix_discrepancies_coin_monotone_statesL_aux_le n edges hpath (List.ofFn r) (List.ofFn s)    (by simp) (by simp) hcoin' (fun _ => 0) (fun _ => 0)    (fun k _ => le_rfl) (fun k _ => rfl) t ht k hk  rw [op_2130_path_prefix_discrepancies_coin_monotone_discrepancies_statesL,      op_2130_path_prefix_discrepancies_coin_monotone_discrepancies_statesL]  exact hres
See it in the graphDiscuss this resultChallenge it