Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Define dependent version of Fin.foldl #1071

Merged
merged 15 commits into from
Dec 4, 2024
18 changes: 18 additions & 0 deletions Batteries/Data/Fin/Fold.lean
Original file line number Diff line number Diff line change
Expand Up @@ -45,3 +45,21 @@ theorem foldr_eq_foldr_finRange (f : Fin n → α → α) (x) :

@[deprecated (since := "2024-11-19")]
alias foldr_eq_foldr_list := foldr_eq_foldr_finRange

variable (n : Nat) (α : Fin (n + 1) → Sort _)

/-- Dependent version of `Fin.foldl`. -/
def fold (f : ∀ (i : Fin n), α i.castSucc → α i.succ) (init : α 0) : α (last n) :=
loop 0 (Nat.zero_lt_succ n) init where
loop (i : Nat) (h : i < n + 1) (x : α ⟨i, h⟩) : α (last n) :=
if h' : i < n then
loop (i + 1) (Nat.succ_lt_succ h') (f ⟨i, h'⟩ x)
else
haveI : ⟨i, h⟩ = last n := by
ext
simp only [val_last]
exact Nat.eq_of_lt_succ_of_not_lt h h'
quangvdao marked this conversation as resolved.
Show resolved Hide resolved
_root_.cast (congrArg α this) x
termination_by n - i
decreasing_by
exact Nat.sub_add_lt_sub h' (Nat.lt_add_one 0)
quangvdao marked this conversation as resolved.
Show resolved Hide resolved
Loading