Skip to content

Commit bd14e70

Browse files
authored
fix: make Array.size not reducible (#8513)
This PR removes the `@[reducible]` annotation on `Array.size`. This is probably best gone anyway in order to keep separation between the `List` and `Array` APIs, but it also helps avoid uselessly instantiating `Array` theorems when `grind` is working on `List` problems.
1 parent f214708 commit bd14e70

File tree

17 files changed

+46
-37
lines changed

17 files changed

+46
-37
lines changed

src/Init/Data/Array/Basic.lean

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,8 @@ theorem ext' {xs ys : Array α} (h : xs.toList = ys.toList) : xs = ys := by
9191
@[simp, grind =] theorem getElem_toList {xs : Array α} {i : Nat} (h : i < xs.size) : xs.toList[i] = xs[i] := rfl
9292

9393
@[simp, grind =] theorem getElem?_toList {xs : Array α} {i : Nat} : xs.toList[i]? = xs[i]? := by
94-
simp [getElem?_def]
94+
simp only [getElem?_def, getElem_toList]
95+
simp only [Array.size]
9596

9697
/-- `a ∈ as` is a predicate which asserts that `a` is in the array `as`. -/
9798
-- NB: This is defined as a structure rather than a plain def so that a lemma

src/Init/Data/Array/Bootstrap.lean

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,8 @@ theorem foldrM_eq_reverse_foldlM_toList [Monad m] {f : α → β → m β} {init
7878
have : xs = #[] ∨ 0 < xs.size :=
7979
match xs with | ⟨[]⟩ => .inl rfl | ⟨a::l⟩ => .inr (Nat.zero_lt_succ _)
8080
match xs, this with | _, .inl rfl => simp [foldrM] | xs, .inr h => ?_
81-
simp [foldrM, h, ← foldrM_eq_reverse_foldlM_toList.aux, List.take_length]
81+
simp only [foldrM, h, ← foldrM_eq_reverse_foldlM_toList.aux]
82+
simp [Array.size]
8283

8384
@[simp, grind =] theorem foldrM_toList [Monad m]
8485
{f : α → β → m β} {init : β} {xs : Array α} :

src/Init/Data/Array/Count.lean

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,7 @@ theorem boole_getElem_le_countP {xs : Array α} {i : Nat} (h : i < xs.size) :
105105
theorem countP_set {xs : Array α} {i : Nat} {a : α} (h : i < xs.size) :
106106
(xs.set i a).countP p = xs.countP p - (if p xs[i] then 1 else 0) + (if p a then 1 else 0) := by
107107
rcases xs with ⟨xs⟩
108+
simp at h
108109
simp [List.countP_set, h]
109110

110111
theorem countP_filter {xs : Array α} :

src/Init/Data/Array/DecidableEq.lean

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ theorem isEqv_eq_decide (xs ys : Array α) (r) :
6969
simpa [isEqv_iff_rel] using h'
7070

7171
@[simp, grind =] theorem isEqv_toList [BEq α] (xs ys : Array α) : (xs.toList.isEqv ys.toList r) = (xs.isEqv ys r) := by
72-
simp [isEqv_eq_decide, List.isEqv_eq_decide]
72+
simp [isEqv_eq_decide, List.isEqv_eq_decide, Array.size]
7373

7474
theorem eq_of_isEqv [DecidableEq α] (xs ys : Array α) (h : Array.isEqv xs ys (fun x y => x = y)) : xs = ys := by
7575
have ⟨h, h'⟩ := rel_of_isEqv h
@@ -100,7 +100,7 @@ theorem beq_eq_decide [BEq α] (xs ys : Array α) :
100100
simp [BEq.beq, isEqv_eq_decide]
101101

102102
@[simp, grind =] theorem beq_toList [BEq α] (xs ys : Array α) : (xs.toList == ys.toList) = (xs == ys) := by
103-
simp [beq_eq_decide, List.beq_eq_decide]
103+
simp [beq_eq_decide, List.beq_eq_decide, Array.size]
104104

105105
end Array
106106

src/Init/Data/Array/Find.lean

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -655,21 +655,22 @@ theorem findFinIdx?_append {xs ys : Array α} {p : α → Bool} :
655655
theorem isSome_findFinIdx? {xs : Array α} {p : α → Bool} :
656656
(xs.findFinIdx? p).isSome = xs.any p := by
657657
rcases xs with ⟨xs⟩
658-
simp
658+
simp [Array.size]
659659

660660
@[simp]
661661
theorem isNone_findFinIdx? {xs : Array α} {p : α → Bool} :
662662
(xs.findFinIdx? p).isNone = xs.all (fun x => ¬ p x) := by
663663
rcases xs with ⟨xs⟩
664-
simp
664+
simp [Array.size]
665665

666666
@[simp] theorem findFinIdx?_subtype {p : α → Prop} {xs : Array { x // p x }}
667667
{f : { x // p x } → Bool} {g : α → Bool} (hf : ∀ x h, f ⟨x, h⟩ = g x) :
668668
xs.findFinIdx? f = (xs.unattach.findFinIdx? g).map (fun i => i.cast (by simp)) := by
669669
cases xs
670670
simp only [List.findFinIdx?_toArray, hf, List.findFinIdx?_subtype]
671671
rw [findFinIdx?_congr List.unattach_toArray]
672-
simp [Function.comp_def]
672+
simp only [Option.map_map, Function.comp_def, Fin.cast_trans]
673+
simp [Array.size]
673674

674675
/-! ### idxOf
675676
@@ -733,18 +734,19 @@ theorem finIdxOf?_empty [BEq α] : (#[] : Array α).finIdxOf? a = none := by sim
733734
@[simp] theorem finIdxOf?_eq_none_iff [BEq α] [LawfulBEq α] {xs : Array α} {a : α} :
734735
xs.finIdxOf? a = none ↔ a ∉ xs := by
735736
rcases xs with ⟨xs⟩
736-
simp [List.finIdxOf?_eq_none_iff]
737+
simp [List.finIdxOf?_eq_none_iff, Array.size]
737738

738739
@[simp] theorem finIdxOf?_eq_some_iff [BEq α] [LawfulBEq α] {xs : Array α} {a : α} {i : Fin xs.size} :
739740
xs.finIdxOf? a = some i ↔ xs[i] = a ∧ ∀ j (_ : j < i), ¬xs[j] = a := by
740741
rcases xs with ⟨xs⟩
742+
unfold Array.size at i ⊢
741743
simp [List.finIdxOf?_eq_some_iff]
742744

743745
@[simp]
744746
theorem isSome_finIdxOf? [BEq α] [LawfulBEq α] {xs : Array α} {a : α} :
745747
(xs.finIdxOf? a).isSome ↔ a ∈ xs := by
746748
rcases xs with ⟨xs⟩
747-
simp
749+
simp [Array.size]
748750

749751
theorem isNone_finIdxOf? [BEq α] [LawfulBEq α] {xs : Array α} {a : α} :
750752
(xs.finIdxOf? a).isNone = ¬ a ∈ xs := by

src/Init/Data/Array/InsertIdx.lean

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ theorem insertIdx_zero {xs : Array α} {x : α} : xs.insertIdx 0 x = #[x] ++ xs
4444

4545
@[simp] theorem size_insertIdx {xs : Array α} (h : i ≤ xs.size) : (xs.insertIdx i a).size = xs.size + 1 := by
4646
rcases xs with ⟨xs⟩
47+
simp at h
4748
simp [List.length_insertIdx, h]
4849

4950
theorem eraseIdx_insertIdx {i : Nat} {xs : Array α} (h : i ≤ xs.size) :

src/Init/Data/Array/Lemmas.lean

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ theorem ne_empty_of_size_pos (h : 0 < xs.size) : xs ≠ #[] := by
7575
cases xs
7676
simpa using List.ne_nil_of_length_pos h
7777

78-
theorem size_eq_zero_iff : xs.size = 0 ↔ xs = #[] :=
78+
@[simp] theorem size_eq_zero_iff : xs.size = 0 ↔ xs = #[] :=
7979
⟨eq_empty_of_size_eq_zero, fun h => h ▸ rfl⟩
8080

8181
@[deprecated size_eq_zero_iff (since := "2025-02-24")]
@@ -169,6 +169,7 @@ theorem getD_getElem? {xs : Array α} {i : Nat} {d : α} :
169169
theorem getElem_push_lt {xs : Array α} {x : α} {i : Nat} (h : i < xs.size) :
170170
have : i < (xs.push x).size := by simp [*, Nat.lt_succ_of_le, Nat.le_of_lt]
171171
(xs.push x)[i] = xs[i] := by
172+
rw [Array.size] at h
172173
simp only [push, ← getElem_toList, List.concat_eq_append, List.getElem_append_left, h]
173174

174175
@[simp] theorem getElem_push_eq {xs : Array α} {x : α} : (xs.push x)[xs.size] = x := by
@@ -1858,7 +1859,7 @@ theorem getElem_append_right {xs ys : Array α} {h : i < (xs ++ ys).size} (hle :
18581859
(xs ++ ys)[i] = ys[i - xs.size]'(Nat.sub_lt_left_of_lt_add hle (size_append .. ▸ h)) := by
18591860
simp only [← getElem_toList]
18601861
have h' : i < (xs.toList ++ ys.toList).length := by rwa [← length_toList, toList_append] at h
1861-
conv => rhs; rw [← List.getElem_append_right (h₁ := hle) (h₂ := h')]
1862+
conv => rhs; unfold Array.size; rw [← List.getElem_append_right (h₁ := hle) (h₂ := h')]
18621863
apply List.get_of_eq; rw [toList_append]
18631864

18641865
theorem getElem?_append_left {xs ys : Array α} {i : Nat} (hn : i < xs.size) :
@@ -2025,7 +2026,7 @@ theorem append_eq_append_iff {ws xs ys zs : Array α} :
20252026
xs ++ ys.set (i - xs.size) x (by simp at h; omega) := by
20262027
rcases xs with ⟨s⟩
20272028
rcases ys with ⟨t⟩
2028-
simp only [List.append_toArray, List.set_toArray, List.set_append]
2029+
simp only [List.append_toArray, List.set_toArray, List.set_append, Array.size]
20292030
split <;> simp
20302031

20312032
@[simp] theorem set_append_left {xs ys : Array α} {i : Nat} {x : α} (h : i < xs.size) :
@@ -2045,7 +2046,7 @@ theorem append_eq_append_iff {ws xs ys zs : Array α} :
20452046
xs ++ ys.setIfInBounds (i - xs.size) x := by
20462047
rcases xs with ⟨s⟩
20472048
rcases ys with ⟨t⟩
2048-
simp only [List.append_toArray, List.setIfInBounds_toArray, List.set_append]
2049+
simp only [List.append_toArray, List.setIfInBounds_toArray, List.set_append, Array.size]
20492050
split <;> simp
20502051

20512052
@[simp] theorem setIfInBounds_append_left {xs ys : Array α} {i : Nat} {x : α} (h : i < xs.size) :
@@ -4500,6 +4501,7 @@ abbrev contains_def [DecidableEq α] {a : α} {xs : Array α} : xs.contains a
45004501
@[simp] theorem size_zipWith {xs : Array α} {ys : Array β} {f : α → β → γ} :
45014502
(zipWith f xs ys).size = min xs.size ys.size := by
45024503
rw [size_eq_length_toList, toList_zipWith, List.length_zipWith]
4504+
simp only [Array.size]
45034505

45044506
@[simp] theorem size_zip {xs : Array α} {ys : Array β} :
45054507
(zip xs ys).size = min xs.size ys.size :=
@@ -4572,7 +4574,7 @@ theorem toListRev_toArray {l : List α} : l.toArray.toListRev = l.reverse := by
45724574
| nil => simp
45734575
| cons a l ih =>
45744576
simp only [foldlM_toArray] at ih
4575-
rw [size_toArray, mapM'_cons, foldlM_toArray]
4577+
rw [size_toArray, mapM'_cons]
45764578
simp [ih]
45774579

45784580
theorem uset_toArray {l : List α} {i : USize} {a : α} {h : i.toNat < l.toArray.size} :

src/Init/Data/Array/MapIdx.lean

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -192,15 +192,16 @@ theorem mapFinIdx_empty {f : (i : Nat) → α → (h : i < 0) → β} : mapFinId
192192
theorem mapFinIdx_eq_ofFn {xs : Array α} {f : (i : Nat) → α → (h : i < xs.size) → β} :
193193
xs.mapFinIdx f = Array.ofFn fun i : Fin xs.size => f i xs[i] i.2 := by
194194
cases xs
195-
simp [List.mapFinIdx_eq_ofFn]
195+
simp only [List.mapFinIdx_toArray, List.mapFinIdx_eq_ofFn, Fin.getElem_fin, List.getElem_toArray]
196+
simp [Array.size]
196197

197198
theorem mapFinIdx_append {xs ys : Array α} {f : (i : Nat) → α → (h : i < (xs ++ ys).size) → β} :
198199
(xs ++ ys).mapFinIdx f =
199200
xs.mapFinIdx (fun i a h => f i a (by simp; omega)) ++
200201
ys.mapFinIdx (fun i a h => f (i + xs.size) a (by simp; omega)) := by
201202
cases xs
202203
cases ys
203-
simp [List.mapFinIdx_append]
204+
simp [List.mapFinIdx_append, Array.size]
204205

205206
@[simp]
206207
theorem mapFinIdx_push {xs : Array α} {a : α} {f : (i : Nat) → α → (h : i < (xs.push a).size) → β} :
@@ -264,12 +265,12 @@ theorem mapFinIdx_eq_append_iff {xs : Array α} {f : (i : Nat) → α → (h : i
264265
toArray_eq_append_iff]
265266
constructor
266267
· rintro ⟨l₁, l₂, rfl, rfl, rfl⟩
267-
refine ⟨l₁.toArray, l₂.toArray, by simp_all⟩
268+
refine ⟨l₁.toArray, l₂.toArray, by simp_all [Array.size]
268269
· rintro ⟨⟨l₁⟩, ⟨l₂⟩, rfl, h₁, h₂⟩
269270
simp [← toList_inj] at h₁ h₂
270271
obtain rfl := h₁
271272
obtain rfl := h₂
272-
refine ⟨l₁, l₂, by simp_all⟩
273+
refine ⟨l₁, l₂, by simp_all [Array.size]
273274

274275
theorem mapFinIdx_eq_push_iff {xs : Array α} {b : β} {f : (i : Nat) → α → (h : i < xs.size) → β} :
275276
xs.mapFinIdx f = ys.push b ↔
@@ -307,7 +308,7 @@ abbrev mapFinIdx_eq_mkArray_iff := @mapFinIdx_eq_replicate_iff
307308
@[simp] theorem mapFinIdx_reverse {xs : Array α} {f : (i : Nat) → α → (h : i < xs.reverse.size) → β} :
308309
xs.reverse.mapFinIdx f = (xs.mapFinIdx (fun i a h => f (xs.size - 1 - i) a (by simp; omega))).reverse := by
309310
rcases xs with ⟨l⟩
310-
simp [List.mapFinIdx_reverse]
311+
simp [List.mapFinIdx_reverse, Array.size]
311312

312313
/-! ### mapIdx -/
313314

@@ -486,7 +487,7 @@ namespace List
486487
| x :: xs => simp only [mapFinIdxM.go, mapIdxM.go, go]
487488
unfold Array.mapIdxM
488489
rw [mapFinIdxM_toArray]
489-
simp only [mapFinIdxM, mapIdxM]
490+
simp only [mapFinIdxM, mapIdxM, Array.size]
490491
rw [go]
491492

492493
end List

src/Init/Data/Array/Monadic.lean

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -310,7 +310,7 @@ namespace List
310310
@[simp] theorem filterM_toArray' [Monad m] [LawfulMonad m] {l : List α} {p : α → m Bool} (w : stop = l.length) :
311311
l.toArray.filterM p 0 stop = toArray <$> l.filterM p := by
312312
subst w
313-
rw [filterM_toArray]
313+
simp [← filterM_toArray]
314314

315315
@[grind =] theorem filterRevM_toArray [Monad m] [LawfulMonad m] {l : List α} {p : α → m Bool} :
316316
l.toArray.filterRevM p = toArray <$> l.filterRevM p := by
@@ -322,7 +322,7 @@ namespace List
322322
@[simp] theorem filterRevM_toArray' [Monad m] [LawfulMonad m] {l : List α} {p : α → m Bool} (w : start = l.length) :
323323
l.toArray.filterRevM p start 0 = toArray <$> l.filterRevM p := by
324324
subst w
325-
rw [filterRevM_toArray]
325+
simp [← filterRevM_toArray]
326326

327327
@[grind =] theorem filterMapM_toArray [Monad m] [LawfulMonad m] {l : List α} {f : α → m (Option β)} :
328328
l.toArray.filterMapM f = toArray <$> l.filterMapM f := by
@@ -340,7 +340,7 @@ namespace List
340340
@[simp] theorem filterMapM_toArray' [Monad m] [LawfulMonad m] {l : List α} {f : α → m (Option β)} (w : stop = l.length) :
341341
l.toArray.filterMapM f 0 stop = toArray <$> l.filterMapM f := by
342342
subst w
343-
rw [filterMapM_toArray]
343+
simp [← filterMapM_toArray]
344344

345345
@[simp, grind =] theorem flatMapM_toArray [Monad m] [LawfulMonad m] {l : List α} {f : α → m (Array β)} :
346346
l.toArray.flatMapM f = toArray <$> l.flatMapM (fun a => Array.toList <$> f a) := by

src/Init/Data/List/Impl.lean

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -550,7 +550,7 @@ def zipIdxTR (l : List α) (n : Nat := 0) : List (α × Nat) :=
550550
(as.foldr (fun a (n, acc) => (n-1, (a, n-1) :: acc)) (n + as.size, [])).2
551551

552552
@[csimp] theorem zipIdx_eq_zipIdxTR : @zipIdx = @zipIdxTR := by
553-
funext α l n; simp only [zipIdxTR, size_toArray]
553+
funext α l n; simp only [zipIdxTR]
554554
let f := fun (a : α) (n, acc) => (n-1, (a, n-1) :: acc)
555555
let rec go : ∀ l i, l.foldr f (i + l.length, []) = (i, zipIdx l i)
556556
| [], n => rfl
@@ -571,7 +571,7 @@ def enumFromTR (n : Nat) (l : List α) : List (Nat × α) :=
571571
set_option linter.deprecated false in
572572
@[deprecated zipIdx_eq_zipIdxTR (since := "2025-01-21"), csimp]
573573
theorem enumFrom_eq_enumFromTR : @enumFrom = @enumFromTR := by
574-
funext α n l; simp only [enumFromTR, size_toArray]
574+
funext α n l; simp only [enumFromTR]
575575
let f := fun (a : α) (n, acc) => (n-1, (n-1, a) :: acc)
576576
let rec go : ∀ l n, l.foldr f (n + l.length, []) = (n, enumFrom n l)
577577
| [], n => rfl

0 commit comments

Comments
 (0)