AFTD Auto-Formalizing Theoretical Domains

k_sorted_of_monotone

theoremverified

If an array $A : \text{Fin } n \to \alpha$ is monotone non-decreasing, then it is $k$-sorted for every $k$.

Statement

theorem k_sorted_of_monotone {n : ℕ} (k : ℕ) {α : Type*} [Preorder α] (A : Fin n → α) (hA : Monotone A) : k_sorted k A
Source
folklore
Verified
24 Nov 2026
Axioms
Classical.choiceQuot.soundpropext
Built on 3
  • k_sortedAn array $A : \text{Fin } n \to \alpha$ is $k$-sorted if $A_i \le A_{i+k}$ whenever $i+k < n$.
  • LinearOrder.toTotalPreorderEvery LinearOrder is a TotalPreorder.
  • TotalPreorderA total preorder: a preorder where ≤ is total (complete). This is weaker than LinearOrder — it does NOT require antisymmetry or…

Read back from the Lean

For any natural number $n$, natural number $k$, type $\alpha$ with a preorder, and sequence $A : \mathrm{Fin}\; n \to \alpha$, if $A$ is monotone, then $A$ is $k$-sorted.

Written by a model that saw only the Lean, never the English above. If the two disagree, that is worth a challenge.

The proof, explained

An array $A$ is $k$-sorted if $A(i) \le A(i+k)$ whenever $i+k < n$. By the monotonicity of $A$, this inequality reduces immediately to $i \le i + k$ on the index set $\text{Fin } n$, which holds because the order on $\text{Fin } n$ is inherited from the usual ordering on $\mathbb{N}$.

  1. To show that $A$ is $k$-sorted, it suffices to show that $i \le i + k$ as indices in $\text{Fin } n$.

    Unfolding the definition of a $k$-sorted array, we introduce an arbitrary index $i \in \text{Fin } n$ and the condition $hi : i + k < n$, leaving the goal $A(i) \le A(\langle i + k, hi \rangle)$. Because $A$ is assumed to be monotone ($hA$), this requirement reduces to checking that the index $i$ is at most the shifted index $\langle i + k, hi \rangle$.

    lines 3–4

      intro i hi
      apply hA
  2. The index inequality $i \le \langle i + k, hi \rangle$ holds in $\text{Fin } n$.

    Rewriting with `Fin.le_def` converts the inequality in $\text{Fin } n$ into the standard inequality $i \le i + k$ on the underlying natural numbers. The simplifier then removes the coercion from the constructor $\langle i + k, hi \rangle$ and discharges the arithmetic statement $i \le i + k$.

    lines 5–6

    uses Fin.le_def

      rw [Fin.le_def]
      simp
Fin.le_def
An inequality $a \le b$ between elements of $\text{Fin } n$ is equivalent to the inequality between their underlying natural numbers.

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

/-- A monotone array is k-sorted for any k. -/theorem k_sorted_of_monotone {n : ℕ} (k : ℕ) {α : Type*} [Preorder α] (A : Fin n → α) (hA : Monotone A) : k_sorted k A := by  intro i hi  apply hA  rw [Fin.le_def]  simp
See it in the graphDiscuss this resultChallenge it