Skip to content

Commit d6cd738

Browse files
authored
feat: redefine String, part two (#10457)
This PR introduces safe alternatives to `String.Pos` and `Substring` that can only represent valid positions/slices. Specifically, the PR - introduces the predicate `String.Pos.IsValid`; - proves several nontrivial equivalent conditions for `String.Pos.IsValid`; - introduces `String.ValidPos`, which is a `String.Pos` with an `IsValid` proof; - introduces `String.Slice`, which is like `Substring` but made from `String.ValidPos` instead of `Pos`; - introduces `String.Pos.IsValidForSlice`, which is like `String.Pos.IsValid` but for slices; - introduces `String.Slice.Pos`, which is like `String.ValidPos` but for slices; - introduces various functions for converting between the two types of positions. The API added in this PR is not complete. It will be expanded in future PRs with addional operations and verification.
1 parent 68409ef commit d6cd738

File tree

13 files changed

+1712
-80
lines changed

13 files changed

+1712
-80
lines changed

src/Init/Data/ByteArray/Lemmas.lean

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -167,9 +167,9 @@ theorem ByteArray.append_inj_left {xs₁ xs₂ ys₁ ys₂ : ByteArray} (h : xs
167167
simp only [ByteArray.ext_iff, ← ByteArray.size_data, ByteArray.data_append] at *
168168
exact Array.append_inj_left h hl
169169

170-
theorem ByteArray.extract_append_eq_right {a b : ByteArray} {i : Nat} (hi : i = a.size) :
171-
(a ++ b).extract i (a ++ b).size = b := by
172-
subst hi
170+
theorem ByteArray.extract_append_eq_right {a b : ByteArray} {i j : Nat} (hi : i = a.size) (hj : j = a.size + b.size) :
171+
(a ++ b).extract i j = b := by
172+
subst hi hj
173173
ext1
174174
simp [← size_data]
175175

src/Init/Data/List/Nat/TakeDrop.lean

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -162,6 +162,9 @@ theorem take_eq_take_iff :
162162
| x :: xs, 0, j + 1 => by simp [succ_min_succ]
163163
| x :: xs, i + 1, j + 1 => by simp [succ_min_succ, take_eq_take_iff]
164164

165+
theorem take_eq_take_min {l : List α} {i : Nat} : l.take i = l.take (min i l.length) := by
166+
simp
167+
165168
@[grind =]
166169
theorem take_add {l : List α} {i j : Nat} : l.take (i + j) = l.take i ++ (l.drop i).take j := by
167170
suffices take (i + j) (take i l ++ drop i l) = take i l ++ take j (drop i l) by

0 commit comments

Comments
 (0)