Skip to content

Commit 819cb87

Browse files
authored
chore: upstream Vector lemmas (#6271)
This PR upstreams existing lemmas about `Vector` from Batteries. Thanks to @fgdorais for preparing these in leanprover-community/batteries#1062. Further contributions to the `Vector` API welcome via PR here.
1 parent 3ee2842 commit 819cb87

File tree

1 file changed

+151
-5
lines changed

1 file changed

+151
-5
lines changed

src/Init/Data/Vector/Lemmas.lean

Lines changed: 151 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,6 @@ Lemmas about `Vector α n`
1313

1414
namespace Vector
1515

16-
theorem length_toList {α n} (xs : Vector α n) : xs.toList.length = n := by simp
17-
1816
@[simp] theorem getElem_mk {data : Array α} {size : data.size = n} {i : Nat} (h : i < n) :
1917
(Vector.mk data size)[i] = data[i] := rfl
2018

@@ -23,9 +21,6 @@ theorem length_toList {α n} (xs : Vector α n) : xs.toList.length = n := by sim
2321
cases xs
2422
simp
2523

26-
theorem getElem_toList {α n} (xs : Vector α n) (i : Nat) (h : i < xs.toList.length) :
27-
xs.toList[i] = xs[i]'(by simpa using h) := by simp
28-
2924
@[simp] theorem getElem_ofFn {α n} (f : Fin n → α) (i : Nat) (h : i < n) :
3025
(Vector.ofFn f)[i] = f ⟨i, by simpa using h⟩ := by
3126
simp [ofFn]
@@ -93,6 +88,157 @@ defeq issues in the implicit size argument.
9388
subst h
9489
simp [pop, back, back!, ← Array.eq_push_pop_back!_of_size_ne_zero]
9590

91+
92+
/-! ### mk lemmas -/
93+
94+
theorem toArray_mk (a : Array α) (h : a.size = n) : (Vector.mk a h).toArray = a := rfl
95+
96+
@[simp] theorem allDiff_mk [BEq α] (a : Array α) (h : a.size = n) :
97+
(Vector.mk a h).allDiff = a.allDiff := rfl
98+
99+
@[simp] theorem mk_append_mk (a b : Array α) (ha : a.size = n) (hb : b.size = m) :
100+
Vector.mk a ha ++ Vector.mk b hb = Vector.mk (a ++ b) (by simp [ha, hb]) := rfl
101+
102+
@[simp] theorem back!_mk [Inhabited α] (a : Array α) (h : a.size = n) :
103+
(Vector.mk a h).back! = a.back! := rfl
104+
105+
@[simp] theorem back?_mk (a : Array α) (h : a.size = n) :
106+
(Vector.mk a h).back? = a.back? := rfl
107+
108+
@[simp] theorem drop_mk (a : Array α) (h : a.size = n) (m) :
109+
(Vector.mk a h).drop m = Vector.mk (a.extract m a.size) (by simp [h]) := rfl
110+
111+
@[simp] theorem eraseIdx_mk (a : Array α) (h : a.size = n) (i) (h') :
112+
(Vector.mk a h).eraseIdx i h' = Vector.mk (a.eraseIdx i) (by simp [h]) := rfl
113+
114+
@[simp] theorem eraseIdx!_mk (a : Array α) (h : a.size = n) (i) (hi : i < n) :
115+
(Vector.mk a h).eraseIdx! i = Vector.mk (a.eraseIdx i) (by simp [h, hi]) := by
116+
simp [Vector.eraseIdx!, hi]
117+
118+
@[simp] theorem extract_mk (a : Array α) (h : a.size = n) (start stop) :
119+
(Vector.mk a h).extract start stop = Vector.mk (a.extract start stop) (by simp [h]) := rfl
120+
121+
@[simp] theorem indexOf?_mk [BEq α] (a : Array α) (h : a.size = n) (x : α) :
122+
(Vector.mk a h).indexOf? x = (a.indexOf? x).map (Fin.cast h) := rfl
123+
124+
@[simp] theorem mk_isEqv_mk (r : α → α → Bool) (a b : Array α) (ha : a.size = n) (hb : b.size = n) :
125+
Vector.isEqv (Vector.mk a ha) (Vector.mk b hb) r = Array.isEqv a b r := by
126+
simp [Vector.isEqv, Array.isEqv, ha, hb]
127+
128+
@[simp] theorem mk_isPrefixOf_mk [BEq α] (a b : Array α) (ha : a.size = n) (hb : b.size = m) :
129+
(Vector.mk a ha).isPrefixOf (Vector.mk b hb) = a.isPrefixOf b := rfl
130+
131+
@[simp] theorem map_mk (a : Array α) (h : a.size = n) (f : α → β) :
132+
(Vector.mk a h).map f = Vector.mk (a.map f) (by simp [h]) := rfl
133+
134+
@[simp] theorem reverse_mk (a : Array α) (h : a.size = n) :
135+
(Vector.mk a h).reverse = Vector.mk a.reverse (by simp [h]) := rfl
136+
137+
@[simp] theorem set_mk (a : Array α) (h : a.size = n) (i x w) :
138+
(Vector.mk a h).set i x = Vector.mk (a.set i x) (by simp [h]) := rfl
139+
140+
@[simp] theorem set!_mk (a : Array α) (h : a.size = n) (i x) :
141+
(Vector.mk a h).set! i x = Vector.mk (a.set! i x) (by simp [h]) := rfl
142+
143+
@[simp] theorem setIfInBounds_mk (a : Array α) (h : a.size = n) (i x) :
144+
(Vector.mk a h).setIfInBounds i x = Vector.mk (a.setIfInBounds i x) (by simp [h]) := rfl
145+
146+
@[simp] theorem swap_mk (a : Array α) (h : a.size = n) (i j) (hi hj) :
147+
(Vector.mk a h).swap i j = Vector.mk (a.swap i j) (by simp [h]) :=
148+
rfl
149+
150+
@[simp] theorem swapIfInBounds_mk (a : Array α) (h : a.size = n) (i j) :
151+
(Vector.mk a h).swapIfInBounds i j = Vector.mk (a.swapIfInBounds i j) (by simp [h]) := rfl
152+
153+
@[simp] theorem swapAt_mk (a : Array α) (h : a.size = n) (i x) (hi) :
154+
(Vector.mk a h).swapAt i x =
155+
((a.swapAt i x).fst, Vector.mk (a.swapAt i x).snd (by simp [h])) :=
156+
rfl
157+
158+
@[simp] theorem swapAt!_mk (a : Array α) (h : a.size = n) (i x) : (Vector.mk a h).swapAt! i x =
159+
((a.swapAt! i x).fst, Vector.mk (a.swapAt! i x).snd (by simp [h])) := rfl
160+
161+
@[simp] theorem take_mk (a : Array α) (h : a.size = n) (m) :
162+
(Vector.mk a h).take m = Vector.mk (a.take m) (by simp [h]) := rfl
163+
164+
@[simp] theorem mk_zipWith_mk (f : α → β → γ) (a : Array α) (b : Array β)
165+
(ha : a.size = n) (hb : b.size = n) : zipWith (Vector.mk a ha) (Vector.mk b hb) f =
166+
Vector.mk (Array.zipWith a b f) (by simp [ha, hb]) := rfl
167+
168+
/-! ### toArray lemmas -/
169+
170+
@[simp] theorem toArray_append (a : Vector α m) (b : Vector α n) :
171+
(a ++ b).toArray = a.toArray ++ b.toArray := rfl
172+
173+
@[simp] theorem toArray_drop (a : Vector α n) (m) :
174+
(a.drop m).toArray = a.toArray.extract m a.size := rfl
175+
176+
@[simp] theorem toArray_empty : (#v[] : Vector α 0).toArray = #[] := rfl
177+
178+
@[simp] theorem toArray_mkEmpty (cap) :
179+
(Vector.mkEmpty (α := α) cap).toArray = Array.mkEmpty cap := rfl
180+
181+
@[simp] theorem toArray_eraseIdx (a : Vector α n) (i) (h) :
182+
(a.eraseIdx i h).toArray = a.toArray.eraseIdx i (by simp [h]) := rfl
183+
184+
@[simp] theorem toArray_eraseIdx! (a : Vector α n) (i) (hi : i < n) :
185+
(a.eraseIdx! i).toArray = a.toArray.eraseIdx! i := by
186+
cases a; simp_all [Array.eraseIdx!]
187+
188+
@[simp] theorem toArray_extract (a : Vector α n) (start stop) :
189+
(a.extract start stop).toArray = a.toArray.extract start stop := rfl
190+
191+
@[simp] theorem toArray_map (f : α → β) (a : Vector α n) :
192+
(a.map f).toArray = a.toArray.map f := rfl
193+
194+
@[simp] theorem toArray_ofFn (f : Fin n → α) : (Vector.ofFn f).toArray = Array.ofFn f := rfl
195+
196+
@[simp] theorem toArray_pop (a : Vector α n) : a.pop.toArray = a.toArray.pop := rfl
197+
198+
@[simp] theorem toArray_push (a : Vector α n) (x) : (a.push x).toArray = a.toArray.push x := rfl
199+
200+
@[simp] theorem toArray_range : (Vector.range n).toArray = Array.range n := rfl
201+
202+
@[simp] theorem toArray_reverse (a : Vector α n) : a.reverse.toArray = a.toArray.reverse := rfl
203+
204+
@[simp] theorem toArray_set (a : Vector α n) (i x h) :
205+
(a.set i x).toArray = a.toArray.set i x (by simpa using h):= rfl
206+
207+
@[simp] theorem toArray_set! (a : Vector α n) (i x) :
208+
(a.set! i x).toArray = a.toArray.set! i x := rfl
209+
210+
@[simp] theorem toArray_setIfInBounds (a : Vector α n) (i x) :
211+
(a.setIfInBounds i x).toArray = a.toArray.setIfInBounds i x := rfl
212+
213+
@[simp] theorem toArray_singleton (x : α) : (Vector.singleton x).toArray = #[x] := rfl
214+
215+
@[simp] theorem toArray_swap (a : Vector α n) (i j) (hi hj) : (a.swap i j).toArray =
216+
a.toArray.swap i j (by simp [hi, hj]) (by simp [hi, hj]) := rfl
217+
218+
@[simp] theorem toArray_swapIfInBounds (a : Vector α n) (i j) :
219+
(a.swapIfInBounds i j).toArray = a.toArray.swapIfInBounds i j := rfl
220+
221+
@[simp] theorem toArray_swapAt (a : Vector α n) (i x h) :
222+
((a.swapAt i x).fst, (a.swapAt i x).snd.toArray) =
223+
((a.toArray.swapAt i x (by simpa using h)).fst,
224+
(a.toArray.swapAt i x (by simpa using h)).snd) := rfl
225+
226+
@[simp] theorem toArray_swapAt! (a : Vector α n) (i x) :
227+
((a.swapAt! i x).fst, (a.swapAt! i x).snd.toArray) =
228+
((a.toArray.swapAt! i x).fst, (a.toArray.swapAt! i x).snd) := rfl
229+
230+
@[simp] theorem toArray_take (a : Vector α n) (m) : (a.take m).toArray = a.toArray.take m := rfl
231+
232+
@[simp] theorem toArray_zipWith (f : α → β → γ) (a : Vector α n) (b : Vector β n) :
233+
(Vector.zipWith a b f).toArray = Array.zipWith a.toArray b.toArray f := rfl
234+
235+
/-! ### toList lemmas -/
236+
237+
theorem length_toList {α n} (xs : Vector α n) : xs.toList.length = n := by simp
238+
239+
theorem getElem_toList {α n} (xs : Vector α n) (i : Nat) (h : i < xs.toList.length) :
240+
xs.toList[i] = xs[i]'(by simpa using h) := by simp
241+
96242
/-! ### Decidable quantifiers. -/
97243

98244
theorem forall_zero_iff {P : Vector α 0Prop} :

0 commit comments

Comments
 (0)