File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff 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+
7493end getD
7594
7695section getI
You can’t perform that action at this time.
0 commit comments