Skip to content

Commit 31d629c

Browse files
authored
feat: more Nat range lemmas (#11321)
This PR provides specialized lemmas about `Nat` ranges, including `simp` annotations and induction principles for proving properties for all ranges.
1 parent d60ef53 commit 31d629c

File tree

5 files changed

+2479
-47
lines changed

5 files changed

+2479
-47
lines changed

src/Init/Data/Array/Lex/Lemmas.lean

Lines changed: 5 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -73,19 +73,11 @@ private theorem cons_lex_cons [BEq α] {lt : α → α → Bool} {a b : α} {xs
7373
(lt a b || a == b && xs.lex ys lt) := by
7474
simp only [lex, size_append, List.size_toArray, List.length_cons, List.length_nil, Nat.zero_add,
7575
Nat.add_min_add_left, Nat.add_lt_add_iff_left, Std.Rco.forIn'_eq_forIn'_toList]
76-
conv =>
77-
lhs; congr; congr
78-
rw [cons_lex_cons.forIn'_congr_aux Std.Rco.toList_eq_if_roo rfl (fun _ _ _ => rfl)]
79-
simp only [bind_pure_comp, map_pure]
80-
rw [cons_lex_cons.forIn'_congr_aux (if_pos (by omega)) rfl (fun _ _ _ => rfl)]
81-
simp only [Std.toList_roo_eq_toList_rco_of_isSome_succ? (lo := 0) (h := rfl),
82-
Std.PRange.UpwardEnumerable.succ?, Nat.add_comm 1, Std.PRange.Nat.toList_rco_succ_succ,
83-
Option.get_some, List.forIn'_cons, List.size_toArray, List.length_cons, List.length_nil,
84-
Nat.lt_add_one, getElem_append_left, List.getElem_toArray, List.getElem_cons_zero]
85-
cases lt a b
86-
· rw [bne]
87-
cases a == b <;> simp
88-
· simp
76+
rw [cons_lex_cons.forIn'_congr_aux (Nat.toList_rco_eq_cons (by omega)) rfl (fun _ _ _ => rfl)]
77+
simp only [bind_pure_comp, map_pure, Nat.toList_rco_succ_succ, Nat.add_comm 1]
78+
cases h : lt a b
79+
· cases h' : a == b <;> simp [bne, *]
80+
· simp [*]
8981

9082
@[simp, grind =] theorem _root_.List.lex_toArray [BEq α] {lt : α → α → Bool} {l₁ l₂ : List α} :
9183
l₁.toArray.lex l₂.toArray lt = l₁.lex l₂ lt := by

src/Init/Data/List/Range.lean

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ open Nat
3737
rw [← length_eq_zero_iff, length_range']
3838

3939
theorem range'_ne_nil_iff (s : Nat) {n step : Nat} : range' s n step ≠ [] ↔ n ≠ 0 := by
40-
cases n <;> simp
40+
simp
4141

4242
theorem range'_eq_cons_iff : range' s n step = a :: xs ↔ s = a ∧ 0 < n ∧ xs = range' (a + step) (n - 1) step := by
4343
induction n generalizing s with

src/Init/Data/Range/Polymorphic/Lemmas.lean

Lines changed: 55 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -467,6 +467,23 @@ public theorem Rxo.Iterator.toArray_eq_match [LT α] [DecidableLT α]
467467
· rfl
468468
· split <;> simp
469469

470+
public theorem Rxc.Iterator.toList_eq_toList_rxoIterator [LE α] [DecidableLE α] [LT α] [DecidableLT α]
471+
[UpwardEnumerable α] [Rxc.IsAlwaysFinite α] [Rxo.IsAlwaysFinite α] [LawfulUpwardEnumerable α]
472+
[LawfulUpwardEnumerableLE α] [LawfulUpwardEnumerableLT α]
473+
[InfinitelyUpwardEnumerable α] [LinearlyUpwardEnumerable α] {it : Iter (α := Rxc.Iterator α) α}:
474+
it.toList = (⟨⟨it.internalState.next, succ it.internalState.upperBound⟩⟩ : Iter (α := Rxo.Iterator α) α).toList := by
475+
induction it using Iter.inductSteps with | step it ihy ihs
476+
rw [Rxc.Iterator.toList_eq_match, Rxo.Iterator.toList_eq_match]
477+
split
478+
· simp [*]
479+
· simp only [UpwardEnumerable.le_iff, UpwardEnumerable.lt_iff, *]
480+
split <;> rename_i h
481+
· rw [ihy]; rotate_left
482+
· simp [Iter.IsPlausibleStep, IterM.IsPlausibleStep, Iterator.IsPlausibleStep,
483+
Iterator.Monadic.step, Iter.toIterM, *]; rfl
484+
· simpa [UpwardEnumerable.lt_iff, UpwardEnumerable.le_iff, UpwardEnumerable.lt_succ_iff] using h
485+
· simpa [UpwardEnumerable.lt_iff, UpwardEnumerable.le_iff, UpwardEnumerable.lt_succ_iff] using h
486+
470487
public theorem Rxi.Iterator.toList_eq_match
471488
[UpwardEnumerable α] [Rxi.IsAlwaysFinite α] [LawfulUpwardEnumerable α]
472489
{it : Iter (α := Rxi.Iterator α) α} :
@@ -561,22 +578,6 @@ namespace Rcc
561578

562579
variable {r : Rcc α}
563580

564-
public theorem toList_eq_if_roo [UpwardEnumerable α] [LE α] [DecidableLE α]
565-
[LawfulUpwardEnumerable α] [Rxc.IsAlwaysFinite α] [LawfulUpwardEnumerableLE α] :
566-
r.toList = if r.lower ≤ r.upper then r.lower :: (r.lower<...=r.upper).toList else [] := by
567-
rw [Internal.toList_eq_toList_iter, Rxc.Iterator.toList_eq_match]; rfl
568-
569-
@[deprecated toList_eq_if_roo (since := "2025-10-29")]
570-
def toList_eq_if_Roo := @toList_eq_if_roo
571-
572-
public theorem toArray_eq_if_roo [UpwardEnumerable α] [LE α] [DecidableLE α]
573-
[LawfulUpwardEnumerable α] [Rxc.IsAlwaysFinite α] [LawfulUpwardEnumerableLE α] :
574-
r.toArray = if r.lower ≤ r.upper then #[r.lower] ++ (r.lower<...=r.upper).toArray else #[] := by
575-
rw [Internal.toArray_eq_toArray_iter, Rxc.Iterator.toArray_eq_match]; rfl
576-
577-
@[deprecated toArray_eq_if_roo (since := "2025-10-29")]
578-
def toArray_eq_if_Roo := @toArray_eq_if_roo
579-
580581
public theorem toList_eq_if_roc [LE α] [DecidableLE α] [UpwardEnumerable α]
581582
[LawfulUpwardEnumerable α] [LawfulUpwardEnumerableLE α] [Rxc.IsAlwaysFinite α] :
582583
r.toList = if r.lower ≤ r.upper then
@@ -585,6 +586,16 @@ public theorem toList_eq_if_roc [LE α] [DecidableLE α] [UpwardEnumerable α]
585586
[] := by
586587
rw [Internal.toList_eq_toList_iter, Rxc.Iterator.toList_eq_match]; rfl
587588

589+
@[simp]
590+
public theorem toList_eq_toList_rco [LE α] [DecidableLE α] [LT α] [DecidableLT α]
591+
[UpwardEnumerable α] [LawfulUpwardEnumerable α]
592+
[LawfulUpwardEnumerableLE α] [LawfulUpwardEnumerableLT α]
593+
[Rxc.IsAlwaysFinite α] [Rxo.IsAlwaysFinite α]
594+
[InfinitelyUpwardEnumerable α] [LinearlyUpwardEnumerable α] :
595+
r.toList = (r.lower...(succ r.upper)).toList := by
596+
simp [Internal.toList_eq_toList_iter, Rco.Internal.toList_eq_toList_iter,
597+
Internal.iter, Rco.Internal.iter, Rxc.Iterator.toList_eq_toList_rxoIterator]
598+
588599
@[deprecated toList_eq_if_roc (since := "2025-10-29")]
589600
def toList_eq_match := @toList_eq_if_roc
590601

@@ -816,6 +827,23 @@ public theorem toArray_eq_if_roo [UpwardEnumerable α] [LT α] [DecidableLT α]
816827
#[] := by
817828
rw [Internal.toArray_eq_toArray_iter, Rxo.Iterator.toArray_eq_match]; rfl
818829

830+
public theorem toList_eq_if_rco [UpwardEnumerable α] [LT α] [DecidableLT α]
831+
[LawfulUpwardEnumerable α] [Rxo.IsAlwaysFinite α] [LawfulUpwardEnumerableLT α] :
832+
r.toList = if r.lower < r.upper then
833+
match UpwardEnumerable.succ? r.lower with
834+
| none => [r.lower]
835+
| some next => r.lower :: (next...r.upper).toList
836+
else
837+
[] := by
838+
rw [Internal.toList_eq_toList_iter, Rxo.Iterator.toList_eq_match]
839+
simp only [Internal.iter]
840+
split
841+
· split
842+
· simp [Rxo.Iterator.toList_eq_match, *]
843+
· simp only [*]
844+
rfl
845+
· rfl
846+
819847
public theorem toArray_eq_if_rco [UpwardEnumerable α] [LT α] [DecidableLT α]
820848
[LawfulUpwardEnumerable α] [Rxo.IsAlwaysFinite α] [LawfulUpwardEnumerableLT α] :
821849
r.toArray = if r.lower < r.upper then
@@ -1272,6 +1300,16 @@ public theorem toArray_eq_match_rcc [LE α] [DecidableLE α] [UpwardEnumerable
12721300
simp only [← Internal.toList_eq_toList_iter, toList_eq_match_rcc]
12731301
split <;> simp
12741302

1303+
@[simp]
1304+
public theorem toList_eq_toList_roo [LE α] [DecidableLE α] [LT α] [DecidableLT α]
1305+
[UpwardEnumerable α] [LawfulUpwardEnumerable α]
1306+
[LawfulUpwardEnumerableLE α] [LawfulUpwardEnumerableLT α]
1307+
[Rxc.IsAlwaysFinite α] [Rxo.IsAlwaysFinite α]
1308+
[InfinitelyUpwardEnumerable α] [LinearlyUpwardEnumerable α] :
1309+
r.toList = (r.lower<...(succ r.upper)).toList := by
1310+
simp [Internal.toList_eq_toList_iter, Roo.Internal.toList_eq_toList_iter,
1311+
Internal.iter, Roo.Internal.iter, Rxc.Iterator.toList_eq_toList_rxoIterator]
1312+
12751313
@[simp]
12761314
public theorem toArray_toList [LE α] [DecidableLE α] [UpwardEnumerable α] [LawfulUpwardEnumerable α]
12771315
[Rxc.IsAlwaysFinite α] :
@@ -2856,7 +2894,7 @@ public theorem length_toList [LE α] [DecidableLE α] [UpwardEnumerable α]
28562894
· simpa [toList_eq_nil_iff, size_eq_if_roc] using h
28572895
· rename_i n ih
28582896
rw [size_eq_if_rcc] at h
2859-
simp only [toList_eq_if_roo, ← h]
2897+
simp only [toList_eq_if_roc, ← h]
28602898
simp only [Roc.toList_eq_match_rcc]
28612899
split
28622900
· split

0 commit comments

Comments
 (0)