Skip to content

Commit e1bb0ec

Browse files
committed
feat(Data/List/GetD): getD is surjective iff every element is in the list
1 parent 3407d8a commit e1bb0ec

1 file changed

Lines changed: 19 additions & 0 deletions

File tree

Mathlib/Data/List/GetD.lean

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,25 @@ theorem getD_eq_getD_getElem? (n : ℕ) : l.getD n d = l[n]?.getD d := by
7171
| inl h => rw [getD_eq_getElem _ _ h, getElem?_eq_getElem h, Option.getD_some]
7272
| inr h => rw [getD_eq_default _ _ h, getElem?_eq_none_iff.mpr h, Option.getD_none]
7373

74+
theorem getD_surjective_iff [DecidableEq α] {l : List α} (d : α) :
75+
(∀ x, x = d ∨ x ∈ l) ↔ (l.getD · d).Surjective := by
76+
refine ⟨fun h x ↦ ?_, fun h x ↦ ?_⟩
77+
· obtain h | h := h x
78+
· exact ⟨l.length, by simp [h]⟩
79+
· have ⟨n, h⟩ := mem_iff_get.mp h
80+
exact ⟨n, by simp [← h]⟩
81+
· have ⟨n, hx⟩ := h x
82+
by_cases hn : l.length ≤ n
83+
· exact Or.inl <| hx ▸ l.getD_eq_default d hn
84+
let n' : Fin l.length := ⟨n, by cutsat⟩
85+
refine Decidable.or_iff_not_imp_left.mpr fun hd ↦ mem_iff_get.mpr ⟨n', ?_⟩
86+
simp [← hx, show n = n' by rfl]
87+
88+
theorem getD_surjective {l : List α} (h : ∀ x, x ∈ l) (d : α) : (l.getD · d).Surjective := by
89+
intro x
90+
have ⟨n, h⟩ := mem_iff_get.mp <| h x
91+
exact ⟨n, by simp [← h]⟩
92+
7493
end getD
7594

7695
section getI

0 commit comments

Comments
 (0)