import Std

/-! Exact counterexamples to overclaiming frontier training-time optimality.
These are mathematical witnesses in stated toy models, not training runs. -/
namespace TrainingTimeCounterexamples

/-- In the ideal capacity-only model, these positive-rate recipes both
attain their own lower bounds; less compute can require more time. -/
theorem less_work_can_take_longer :
    (0 : Nat) < 4 ∧ (0 : Nat) < 6 ∧ (0 : Nat) < 1 ∧ (0 : Nat) < 3 ∧
    (4 : Nat) < 6 ∧ (2 : Nat) < 4 ∧
    (4 : Nat) = 1*4 ∧ (6 : Nat) = 3*2 ∧
    (∀ t : Nat, 4 ≤ 1*t → 4 ≤ t) ∧
    (∀ t : Nat, 6 ≤ 3*t → 2 ≤ t) := by
  refine ⟨by decide, by decide, by decide, by decide, by decide, by decide,
          by decide, by decide, ?_, ?_⟩
  · intro t h
    simpa using h
  · intro t h
    omega

/-- The effective rate of the slower run (4/4=1) is not a universal cap:
the other recipe is explicitly feasible at time 2 and does 6 units. -/
theorem observed_rate_not_common_cap :
    (4 : Nat) / 4 = 1 ∧ (6 : Nat) ≤ 3 * 2 ∧ ¬ (6 : Nat) ≤ 1 * 2 := by decide

def monotoneTrace (threshold t : Nat) : Prop := threshold ≤ t
def firstHit (trace : Nat → Prop) (s : Nat) : Prop :=
  trace s ∧ ∀ t, trace t → s ≤ t

theorem trace_monotone (threshold a b : Nat) (le : a ≤ b) :
    monotoneTrace threshold a → monotoneTrace threshold b := by
  intro h
  exact Nat.le_trans h le

theorem trace_first (threshold : Nat) :
    firstHit (monotoneTrace threshold) threshold := ⟨Nat.le_refl _, fun _ h => h⟩

/-- Same failed checkpoint 1 and successful checkpoint 10, monotone in
both cases, with exact different first hits 2 and 8. -/
theorem same_observations_different_first_hits :
    (∀ a b, a ≤ b → monotoneTrace 2 a → monotoneTrace 2 b) ∧
    (∀ a b, a ≤ b → monotoneTrace 8 a → monotoneTrace 8 b) ∧
    ¬ monotoneTrace 2 1 ∧ ¬ monotoneTrace 8 1 ∧
    monotoneTrace 2 10 ∧ monotoneTrace 8 10 ∧
    firstHit (monotoneTrace 2) 2 ∧ firstHit (monotoneTrace 8) 8 ∧ (2 : Nat) ≠ 8 := by
  refine ⟨trace_monotone 2, trace_monotone 8, ?_, ?_, ?_, ?_,
           trace_first 2, trace_first 8, by decide⟩
  all_goals unfold monotoneTrace; decide

section ReciprocalExample

/-- At positive N,D, the reciprocal loss constraint is exactly
the denominator-cleared condition used for the integer witness. -/
theorem reciprocal_to_cross (N D : Rat) (hn : 0 < N) (hd : 0 < D) :
    1/N + 1/D ≤ 3/4 ↔ 4*(N+D) ≤ 3*(N*D) := by
  have np := Rat.mul_pos hn hd
  have fp := Rat.mul_pos (show 0 < (4 : Rat) by decide) np
  have hn' := Rat.div_mul_cancel (a := (1 : Rat)) (b := N) (Rat.ne_of_gt hn)
  have hd' := Rat.div_mul_cancel (a := (1 : Rat)) (b := D) (Rat.ne_of_gt hd)
  have h4 := Rat.div_mul_cancel (a := (3 : Rat)) (b := 4) (by decide)
  have lhs : (1/N+1/D)*(4*(N*D)) = 4*(N+D) := by
    calc
      _ = 4*(((1/N)*N)*D+((1/D)*D)*N) := by grind
      _ = _ := by rw [hn',hd']; grind
  have rhs : (3/4 : Rat)*(4*(N*D)) = 3*(N*D) := by
    calc
      _ = ((3/4 : Rat)*4)*(N*D) := by grind
      _ = _ := by rw [h4]
  constructor
  · intro h
    have z := Rat.mul_le_mul_of_nonneg_right h (Rat.le_of_lt fp)
    rwa [lhs,rhs] at z
  · intro h
    apply Rat.le_of_mul_le_mul_right _ fp
    rwa [lhs,rhs]

private theorem sum_square_ge_four_product (x y : Rat) :
    4*x*y ≤ (x+y)*(x+y) := by
  rcases (Rat.le_total : x ≤ y ∨ y ≤ x) with h | h
  · have hs : 0 ≤ y-x := by grind
    have ht := Rat.mul_nonneg hs hs
    grind
  · have hs : 0 ≤ x-y := by grind
    have ht := Rat.mul_nonneg hs hs
    grind

/-- Exact continuous-arithmetic optimum in the rational reciprocal toy. -/
theorem rational_work_lower (N D : Rat) (hn : 0 < N) (hd : 0 < D)
    (loss : 1/N + 1/D ≤ 3/4) : (64/9 : Rat) ≤ N*D := by
  have p := Rat.mul_pos hn hd
  have s : 0 ≤ 4*(N+D) := by grind
  have rhs : 0 ≤ 3*(N*D) :=
    Rat.mul_nonneg (by decide) (Rat.le_of_lt p)
  have cond := (reciprocal_to_cross N D hn hd).mp loss
  have m1 := Rat.mul_le_mul_of_nonneg_right cond s
  have m2 := Rat.mul_le_mul_of_nonneg_left cond rhs
  have a := sum_square_ge_four_product N D
  have m3 := Rat.mul_le_mul_of_nonneg_left a (show 0 ≤ (16 : Rat) by decide)
  have edge : (64 : Rat)*(N*D) ≤ 9*(N*D)*(N*D) := by grind
  have pos9 := Rat.mul_pos (show 0 < (9 : Rat) by decide) p
  have he : (64/9 : Rat)*9=64 := Rat.div_mul_cancel (by decide)
  apply Rat.le_of_mul_le_mul_right _ pos9
  calc
    _ = 64*(N*D) := by calc
      _ = ((64/9 : Rat)*9)*(N*D) := by grind
      _ = _ := by rw [he]
    _ ≤ _ := by grind

theorem rational_work_attains :
    0 < (8/3 : Rat) ∧ (1 : Rat)/(8/3) + 1/(8/3) = 3/4 ∧
    (8/3 : Rat)*(8/3) = 64/9 := by decide +kernel

/-- Arithmetic over positive whole counts yields the stronger minimum 8. -/
theorem integer_work_lower (N D : Nat) (hn : 0 < N) (hd : 0 < D)
    (target : 4*(N+D) ≤ 3*(N*D)) : 8 ≤ N*D := by
  have n2 : 2 ≤ N := by
    by_cases h : 2 ≤ N
    · exact h
    · have z : N=1 := by omega
      subst N
      simp at target
      omega
  have d2 : 2 ≤ D := by
    by_cases h : 2 ≤ D
    · exact h
    · have z : D=1 := by omega
      subst D
      simp at target
      omega
  by_cases n3 : 3 ≤ N
  · by_cases d3 : 3 ≤ D
    · have z : 3*3 ≤ N*D := Nat.mul_le_mul n3 d3
      omega
    · have eq : D=2 := by omega
      subst D
      omega
  · have eq : N=2 := by omega
    subst N
    omega

/-- Rounding both 8/3 coordinates up to 3 is NOT optimal: (2,4)
hits threshold exactly, and has work 8 rather than 9. -/
theorem integer_work_attains_and_symmetric_rounding_loses :
    (0 : Nat) < 2 ∧ (0 : Nat) < 4 ∧ (4 : Nat)*(2+4) = 3*(2*4) ∧
    (2 : Nat)*4 = 8 ∧ (2 : Nat)*4 < 3*3 ∧ (64/9 : Rat) < 8 := by decide +kernel

end ReciprocalExample

#print axioms less_work_can_take_longer
#print axioms same_observations_different_first_hits
#print axioms reciprocal_to_cross
#print axioms rational_work_lower
#print axioms integer_work_lower
#print axioms integer_work_attains_and_symmetric_rounding_loses

end TrainingTimeCounterexamples
