Skip to content

Commit 44ff700

Browse files
authored
feat: add simp lemma writing Vector.tail in terms of Vector.extract (#8445)
This PR adds a `@[simp]` lemma, and comments explaining that there is intentionally no verification API for `Vector.take`, `Vector.drop`, or `Vector.tail`, which should all be rewritten in terms of `Vector.extract`.
1 parent ae1ab94 commit 44ff700

File tree

1 file changed

+21
-8
lines changed

1 file changed

+21
-8
lines changed

src/Init/Data/Vector/Basic.lean

Lines changed: 21 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -198,6 +198,8 @@ result is empty. If `stop` is greater than the size of the vector, the size is u
198198
/--
199199
Extract the first `i` elements of a vector. If `i` is greater than or equal to the size of the
200200
vector then the vector is returned unchanged.
201+
202+
We immediately simplify this to the `extract` operation, so there is no verification API for this function.
201203
-/
202204
@[inline] def take (xs : Vector α n) (i : Nat) : Vector α (min i n) :=
203205
⟨xs.toArray.take i, by simp⟩
@@ -207,16 +209,22 @@ vector then the vector is returned unchanged.
207209
/--
208210
Deletes the first `i` elements of a vector. If `i` is greater than or equal to the size of the
209211
vector then the empty vector is returned.
212+
213+
We immediately simplify this to the `extract` operation, so there is no verification API for this function.
210214
-/
211215
@[inline] def drop (xs : Vector α n) (i : Nat) : Vector α (n - i) :=
212216
⟨xs.toArray.drop i, by simp⟩
213217

214218
set_option linter.indexVariables false in
215219
@[simp] theorem drop_eq_cast_extract (xs : Vector α n) (i : Nat) :
216-
xs.drop i = (xs.extract i n).cast (by simp) := by
220+
xs.drop i = (xs.extract i).cast (by simp) := by
217221
simp [drop, extract, Vector.cast]
218222

219-
/-- Shrinks a vector to the first `m` elements, by repeatedly popping the last element. -/
223+
/--
224+
Shrinks a vector to the first `m` elements, by repeatedly popping the last element.
225+
226+
We immediately simplify this to the `extract` operation, so there is no verification API for this function.
227+
-/
220228
@[inline] def shrink (xs : Vector α n) (i : Nat) : Vector α (min i n) :=
221229
⟨xs.toArray.shrink i, by simp⟩
222230

@@ -394,12 +402,17 @@ instance [BEq α] : BEq (Vector α n) where
394402
have : Inhabited (Vector α (n+1)) := ⟨xs.push x⟩
395403
panic! "index out of bounds"
396404

397-
/-- Delete the first element of a vector. Returns the empty vector if the input vector is empty. -/
398-
@[inline] def tail (xs : Vector α n) : Vector α (n-1) :=
399-
if _ : 0 < n then
400-
xs.eraseIdx 0
401-
else
402-
xs.cast (by omega)
405+
/--
406+
Delete the first element of a vector. Returns the empty vector if the input vector is empty.
407+
408+
We immediately simplify this to the `extract` operation, so there is no verification API for this function.
409+
-/
410+
@[inline]
411+
def tail (xs : Vector α n) : Vector α (n-1) :=
412+
(xs.extract 1).cast (by omega)
413+
414+
@[simp] theorem tail_eq_cast_extract (xs : Vector α n) :
415+
xs.tail = (xs.extract 1).cast (by omega) := rfl
403416

404417
/--
405418
Finds the first index of a given value in a vector using `==` for comparison. Returns `none` if the

0 commit comments

Comments
 (0)