Skip to content

Commit

Permalink
feat: minor lemmas about List.ofFn (#5982)
Browse files Browse the repository at this point in the history
`List.ofFn` still has very incomplete API.
  • Loading branch information
kim-em authored Nov 6, 2024
1 parent 196b1e9 commit a542261
Showing 1 changed file with 25 additions and 0 deletions.
25 changes: 25 additions & 0 deletions src/Init/Data/List/OfFn.lean
Original file line number Diff line number Diff line change
Expand Up @@ -52,4 +52,29 @@ protected theorem getElem?_ofFn (f : Fin n → α) (i) : (ofFn f)[i]? = if h : i
rw [dif_neg] <;>
simpa using h

/-- `ofFn` on an empty domain is the empty list. -/
@[simp]
theorem ofFn_zero (f : Fin 0 → α) : ofFn f = [] :=
ext_get (by simp) (fun i hi₁ hi₂ => by contradiction)

@[simp]
theorem ofFn_succ {n} (f : Fin (n + 1) → α) : ofFn f = f 0 :: ofFn fun i => f i.succ :=
ext_get (by simp) (fun i hi₁ hi₂ => by
cases i
· simp
· simp)

@[simp]
theorem ofFn_eq_nil_iff {f : Fin n → α} : ofFn f = [] ↔ n = 0 := by
cases n <;> simp only [ofFn_zero, ofFn_succ, eq_self_iff_true, Nat.succ_ne_zero, reduceCtorEq]

theorem head_ofFn {n} (f : Fin n → α) (h : ofFn f ≠ []) :
(ofFn f).head h = f ⟨0, Nat.pos_of_ne_zero (mt ofFn_eq_nil_iff.2 h)⟩ := by
rw [← getElem_zero (length_ofFn _ ▸ Nat.pos_of_ne_zero (mt ofFn_eq_nil_iff.2 h)),
List.getElem_ofFn]

theorem getLast_ofFn {n} (f : Fin n → α) (h : ofFn f ≠ []) :
(ofFn f).getLast h = f ⟨n - 1, Nat.sub_one_lt (mt ofFn_eq_nil_iff.2 h)⟩ := by
simp [getLast_eq_getElem, length_ofFn, List.getElem_ofFn]

end List

0 comments on commit a542261

Please sign in to comment.