import Mathlib namespace OAI noncomputable section namespace TruffetV4Counterexample abbrev Coeff := WithBot ℝ abbrev Form := Fin 3 → Coeff abbrev Var := Fin 2 abbrev RowIndex := Fin 2 def column (j : Var) : Fin 3 := ⟨j.val, Nat.lt_trans j.isLt (by decide)⟩ def nullForm : Form := fun _ => ⊥ structure Row where lhs : Form rhs : Form structure State where rows : RowIndex → Row cost : Form remaining : Var → Prop def wellFormed (s : State) : Prop := ∀ j, ¬ s.remaining j → s.cost (column j) = ⊥ ∧ ∀ i, (s.rows i).lhs (column j) = ⊥ ∧ (s.rows i).rhs (column j) = ⊥ def eval (f : Form) (w : Fin 3 → ℝ) : Coeff := max (f 0 + (w 0 : Coeff)) (max (f 1 + (w 1 : Coeff)) (f 2 + (w 2 : Coeff))) def feasible (s : State) (w : Fin 3 → ℝ) : Prop := ∀ i, eval (s.rows i).rhs w ≤ eval (s.rows i).lhs w def lower (s : State) (i : RowIndex) (j : Var) : Prop := (s.rows i).lhs (column j) ≠ ⊥ ∧ (s.rows i).rhs (column j) < (s.rows i).lhs (column j) def upper (s : State) (i : RowIndex) (j : Var) : Prop := (s.rows i).rhs (column j) ≠ ⊥ ∧ (s.rows i).lhs (column j) < (s.rows i).rhs (column j) def dominating (s : State) (j : Var) : Prop := s.remaining j ∧ (∃ i, lower s i j) ∧ ∃ i, upper s i j def eligible (s : State) (i : RowIndex) (j : Var) : Prop := s.remaining j ∧ lower s i j ∧ ((∃ k, dominating s k) → dominating s j) def bounded (s : State) : Prop := ∀ j, s.remaining j → (∃ i, lower s i j) ∨ ∃ i, upper s i j def stop (s : State) : Prop := ∀ i, (s.rows i).rhs 2 ≤ (s.rows i).lhs 2 def switch (s : State) : Prop := (∀ j, s.remaining j → s.cost (column j) = ⊥) ∧ ¬ stop s ∧ (∃ j, s.remaining j) ∧ (¬ ∃ i j, s.remaining j ∧ lower s i j) ∧ ∃ i j, s.remaining j ∧ upper s i j def failure (s : State) : Prop := ¬ stop s ∧ ((¬ ∃ j, s.remaining j) ∨ ((¬ ∃ i j, s.remaining j ∧ lower s i j) ∧ (¬ ∃ i j, s.remaining j ∧ upper s i j))) def lowerForm (s : State) (i : RowIndex) (j : Var) : Form := fun k => if k = column j then ⊥ else (s.rows i).rhs k + ((-WithBot.unbotD 0 ((s.rows i).lhs (column j)) : ℝ) : Coeff) def substitute (f : Form) (j : Var) (g : Form) : Form := fun k => if k = column j then ⊥ else max (f k) (f (column j) + g k) def cleanup (r : Row) : Row := by classical exact if ∀ k, r.rhs k ≤ r.lhs k then ⟨nullForm, nullForm⟩ else r def advance (s : State) (i : RowIndex) (j : Var) : State where rows := fun r => cleanup ⟨substitute (s.rows r).lhs j (lowerForm s i j), substitute (s.rows r).rhs j (lowerForm s i j)⟩ cost := substitute s.cost j (lowerForm s i j) remaining := fun k => s.remaining k ∧ k ≠ j def ForcedSingletonStep (s s' : State) (i : RowIndex) (j : Var) : Prop := wellFormed s ∧ wellFormed s' ∧ bounded s ∧ ¬ stop s ∧ ¬ switch s ∧ ¬ failure s ∧ (∀ r k, eligible s r k ↔ r = i ∧ k = j) ∧ (∃ k, (substitute s.cost j (lowerForm s i j)) k ≠ ⊥) ∧ s' = advance s i j def initial : State where rows := ![⟨![(0 : Coeff), 0, ⊥], ![⊥, ⊥, (0 : Coeff)]⟩, ⟨![(0 : Coeff), ⊥, ⊥], ![⊥, (1 : Coeff), ((-2 : ℝ) : Coeff)]⟩] cost := ![(0 : Coeff), ⊥, ⊥] remaining := fun _ => True def middle : State where rows := ![⟨nullForm, nullForm⟩, ⟨![(0 : Coeff), ⊥, ⊥], ![⊥, ⊥, (1 : Coeff)]⟩] cost := ![(0 : Coeff), ⊥, ⊥] remaining := fun j => j = 0 def finalState : State where rows := fun _ => ⟨nullForm, nullForm⟩ cost := ![⊥, ⊥, (1 : Coeff)] remaining := fun _ => False def crossRowRestriction (s : State) : Prop := ∀ i r : RowIndex, i ≠ r → ∀ α : ℝ, ∃ w : Fin 3 → ℝ, eval (s.rows i).lhs w < (α : Coeff) + eval (s.rows r).rhs w def output : Fin 3 → ℝ := ![1, 0, 0] def better : Fin 3 → ℝ := ![0, -1, 0] end TruffetV4Counterexample end end OAI