import Std

/-!
A conditional discrete training-time floor. Work units and time ticks must
be specified consistently, and the per-tick rate is a valid upper bound.
No theorem here supplies the amount of work needed for capability.
-/
namespace FrontierTrainingTime

/-- Integer ceiling of work divided by the rate, when the rate is positive. -/
def ceilTicks (work rate : Nat) : Nat := (work + (rate - 1)) / rate

/-- Capacity-only arithmetic: no premise about model quality, architecture
or achievability of the ideal packing. -/
theorem capacity_iff_ceiling (work rate ticks : Nat) (positive : 0 < rate) :
    work ≤ rate * ticks ↔ ceilTicks work rate ≤ ticks := by
  rw [Nat.mul_comm rate ticks]
  constructor
  · intro h
    unfold ceilTicks
    apply Nat.le_of_lt_succ
    apply (Nat.div_lt_iff_lt_mul positive).2
    rw [Nat.succ_mul]
    omega
  · intro h
    have q : (work + (rate - 1)) / rate < ticks + 1 :=
      Nat.lt_succ_of_le h
    have b := (Nat.div_lt_iff_lt_mul positive).1 q
    rw [Nat.add_mul, Nat.one_mul] at b
    omega

theorem work_floor (work rate ticks : Nat) (positive : 0 < rate)
    (capacity : work ≤ rate * ticks) :
    ceilTicks work rate ≤ ticks :=
  (capacity_iff_ceiling work rate ticks positive).mp capacity

/-- The arithmetic bound is tight for the capacity inequality alone; this
does not construct a realizable computation at that rate. -/
theorem ceiling_satisfies_ideal_capacity (work rate : Nat) (positive : 0 < rate) :
    work ≤ rate * ceilTicks work rate :=
  (capacity_iff_ceiling work rate (ceilTicks work rate) positive).mpr (Nat.le_refl _)

/-- Zero aggregate throughput is incompatible with strictly positive work. -/
theorem no_positive_work_at_zero_rate (work ticks : Nat) (positive : 0 < work) :
    ¬ work ≤ 0 * ticks := by
  simp [Nat.not_le_of_gt positive]

/-- Per-tick upper bounds of independent resources can be summed.
This grants perfect simultaneous availability for a lower bound only. -/
def aggregateRate (rates : List Nat) : Nat := rates.sum
def aggregateCapacity (rates : List Nat) (ticks : Nat) : Nat :=
  (rates.map (fun r => r * ticks)).sum

theorem capacity_across_workers (rates : List Nat) (ticks : Nat) :
    aggregateCapacity rates ticks = aggregateRate rates * ticks := by
  induction rates with
  | nil => simp [aggregateCapacity, aggregateRate]
  | cons r rs ih =>
    simpa only [aggregateCapacity, List.map_cons, List.sum_cons, aggregateRate, Nat.add_mul] using
      congrArg (fun x => r * ticks + x) ih

theorem parallel_floor (work ticks : Nat) (rates : List Nat)
    (positive : 0 < aggregateRate rates)
    (capacity : work ≤ aggregateCapacity rates ticks) :
    ceilTicks work (aggregateRate rates) ≤ ticks := by
  rw [capacity_across_workers] at capacity
  exact work_floor work (aggregateRate rates) ticks positive capacity

/-- A serial critical path can overlap other work; take max, not sum. -/
theorem overlapping_critical_path (work rate ticks serial : Nat)
    (positive : 0 < rate) (capacity : work ≤ rate * ticks)
    (critical : serial ≤ ticks) :
    max serial (ceilTicks work rate) ≤ ticks := by
  by_cases h : serial ≤ ceilTicks work rate
  · rw [Nat.max_eq_right h]
    exact work_floor work rate ticks positive capacity
  · have h' : ceilTicks work rate ≤ serial := by omega
    rw [Nat.max_eq_left h']
    exact critical

/-- The two durations add only under a disjoint-phase hypothesis:
the counted work can use at most the ticks left after the serial phase. -/
theorem disjoint_serial_phase (work rate ticks serial : Nat)
    (positive : 0 < rate) (serial_fits : serial ≤ ticks)
    (remaining_capacity : work ≤ rate * (ticks - serial)) :
    serial + ceilTicks work rate ≤ ticks := by
  have floor := work_floor work rate (ticks - serial) positive remaining_capacity
  omega

/-! Counterexample to an unconditional positive capability-acquisition
time bound based only on the architecture. The target may already hold for
the starting state. There need not be any new training steps. -/
section ZeroWorkCounterexample
variable {State : Type}

def successfulBy (reachable : Nat → State → Prop) (target : State → Prop)
    (ticks : Nat) : Prop :=
  ∃ state, reachable ticks state ∧ target state

theorem already_target_refutes_positive_bound
    (reachable : Nat → State → Prop) (target : State → Prop) (initial : State)
    (at_zero : reachable 0 initial) (already : target initial)
    (claimed : Nat) (positive : 0 < claimed) :
    ¬ (∀ t, successfulBy reachable target t → claimed ≤ t) := by
  intro asserted
  have h := asserted 0 ⟨initial, at_zero, already⟩
  omega

/-- Concrete zero-update witness with an already-satisfied target. -/
example : successfulBy (fun t x : Nat => t = 0 ∧ x = 7) (fun x => x = 7) 0 :=
  ⟨7, ⟨rfl, rfl⟩, rfl⟩
end ZeroWorkCounterexample

example : ceilTicks 101 10 = 11 := by decide
example : ceilTicks 100 10 = 10 := by decide
example : ceilTicks 0 10 = 0 := by decide

#print axioms capacity_iff_ceiling
#print axioms parallel_floor
#print axioms overlapping_critical_path
#print axioms disjoint_serial_phase
#print axioms already_target_refutes_positive_bound

end FrontierTrainingTime
