Skip to content

Commit

Permalink
Trigger CI for leanprover/lean4#6123
Browse files Browse the repository at this point in the history
  • Loading branch information
leanprover-community-mathlib4-bot committed Nov 20, 2024
2 parents 82dbe89 + 9b9f4d0 commit 493818e
Show file tree
Hide file tree
Showing 10 changed files with 137 additions and 133 deletions.
15 changes: 5 additions & 10 deletions Batteries/Classes/SatisfiesM.lean
Original file line number Diff line number Diff line change
Expand Up @@ -185,14 +185,15 @@ theorem SatisfiesM_EStateM_eq :
rw [EStateM.run_map, EStateM.run]
split <;> simp_all

@[simp] theorem SatisfiesM_ReaderT_eq [Monad m] :
theorem SatisfiesM_ReaderT_eq [Monad m] :
SatisfiesM (m := ReaderT ρ m) p x ↔ ∀ s, SatisfiesM p (x.run s) :=
(exists_congr fun a => by exact ⟨fun eq _ => eq ▸ rfl, funext⟩).trans Classical.skolem.symm

theorem SatisfiesM_StateRefT_eq [Monad m] :
SatisfiesM (m := StateRefT' ω σ m) p x ↔ ∀ s, SatisfiesM p (x s) := by simp [ReaderT.run]
SatisfiesM (m := StateRefT' ω σ m) p x ↔ ∀ s, SatisfiesM p (x s) := by
simp [SatisfiesM_ReaderT_eq, ReaderT.run]

@[simp] theorem SatisfiesM_StateT_eq [Monad m] [LawfulMonad m] :
theorem SatisfiesM_StateT_eq [Monad m] [LawfulMonad m] :
SatisfiesM (m := StateT ρ m) (α := α) p x ↔ ∀ s, SatisfiesM (m := m) (p ·.1) (x.run s) := by
change SatisfiesM (m := StateT ρ m) (α := α) p x ↔ ∀ s, SatisfiesM (m := m) (p ·.1) (x s)
refine .trans ⟨fun ⟨f, eq⟩ => eq ▸ ?_, fun ⟨f, h⟩ => ?_⟩ Classical.skolem.symm
Expand All @@ -201,7 +202,7 @@ theorem SatisfiesM_StateRefT_eq [Monad m] :
· refine ⟨fun s => (fun ⟨⟨a, s'⟩, h⟩ => ⟨⟨a, h⟩, s'⟩) <$> f s, funext fun s => ?_⟩
show _ >>= _ = _; simp [← h]

@[simp] theorem SatisfiesM_ExceptT_eq [Monad m] [LawfulMonad m] :
theorem SatisfiesM_ExceptT_eq [Monad m] [LawfulMonad m] :
SatisfiesM (m := ExceptT ρ m) (α := α) p x ↔
SatisfiesM (m := m) (∀ a, · = .ok a → p a) x.run := by
change _ ↔ SatisfiesM (m := m) (∀ a, · = .ok a → p a) x
Expand Down Expand Up @@ -247,9 +248,6 @@ instance : MonadSatisfying (Except ε) where
| .error e => .error e
val_eq {α p x?} h := by cases x? <;> simp

-- This will be redundant after nightly-2024-11-08.
attribute [ext] ReaderT.ext

instance [Monad m] [LawfulMonad m][MonadSatisfying m] : MonadSatisfying (ReaderT ρ m) where
satisfying {α p x} h :=
have h' := SatisfiesM_ReaderT_eq.mp h
Expand All @@ -263,9 +261,6 @@ instance [Monad m] [LawfulMonad m][MonadSatisfying m] : MonadSatisfying (ReaderT
instance [Monad m] [LawfulMonad m] [MonadSatisfying m] : MonadSatisfying (StateRefT' ω σ m) :=
inferInstanceAs <| MonadSatisfying (ReaderT _ _)

-- This will be redundant after nightly-2024-11-08.
attribute [ext] StateT.ext

instance [Monad m] [LawfulMonad m] [MonadSatisfying m] : MonadSatisfying (StateT ρ m) where
satisfying {α p x} h :=
have h' := SatisfiesM_StateT_eq.mp h
Expand Down
20 changes: 1 addition & 19 deletions Batteries/Data/Array/Basic.lean
Original file line number Diff line number Diff line change
Expand Up @@ -155,25 +155,7 @@ Automatically generates proof of `i < a.size` with `get_elem_tactic` where feasi
abbrev swapAtN (a : Array α) (i : Nat) (x : α) (h : i < a.size := by get_elem_tactic) :
α × Array α := swapAt a ⟨i,h⟩ x

/--
`eraseIdxN a i h` Removes the element at position `i` from a vector of length `n`.
`h : i < a.size` has a default argument `by get_elem_tactic` which tries to supply a proof
that the index is valid.
This function takes worst case O(n) time because it has to backshift all elements at positions
greater than i.
-/
abbrev eraseIdxN (a : Array α) (i : Nat) (h : i < a.size := by get_elem_tactic) : Array α :=
a.feraseIdx ⟨i, h⟩

/--
Remove the element at a given index from an array, panics if index is out of bounds.
-/
def eraseIdx! (a : Array α) (i : Nat) : Array α :=
if h : i < a.size then
a.feraseIdx ⟨i, h⟩
else
have : Inhabited (Array α) := ⟨a⟩
panic! s!"index {i} out of bounds"
@[deprecated (since := "2024-11-20")] alias eraseIdxN := eraseIdx

end Array

Expand Down
198 changes: 99 additions & 99 deletions Batteries/Data/Array/Lemmas.lean
Original file line number Diff line number Diff line change
Expand Up @@ -71,12 +71,9 @@ where
@[simp] proof_wanted toList_erase [BEq α] {l : Array α} {a : α} :
(l.erase a).toList = l.toList.erase a

@[simp] theorem eraseIdx!_eq_eraseIdx (a : Array α) (i : Nat) :
a.eraseIdx! i = a.eraseIdx i := rfl

@[simp] theorem size_eraseIdx (a : Array α) (i : Nat) :
(a.eraseIdx i).size = if i < a.size then a.size-1 else a.size := by
simp only [eraseIdx]; split; simp; rfl
@[simp] theorem size_eraseIdxIfInBounds (a : Array α) (i : Nat) :
(a.eraseIdxIfInBounds i).size = if i < a.size then a.size-1 else a.size := by
simp only [eraseIdxIfInBounds]; split; simp; rfl

/-! ### set -/

Expand All @@ -93,96 +90,99 @@ theorem map_empty (f : α → β) : map f #[] = #[] := mapM_empty f

theorem mem_singleton : a ∈ #[b] ↔ a = b := by simp

/-! ### insertAt -/

private theorem size_insertAt_loop (as : Array α) (i : Fin (as.size+1)) (j : Fin bs.size) :
(insertAt.loop as i bs j).size = bs.size := by
unfold insertAt.loop
split
· rw [size_insertAt_loop, size_swap]
· rfl

@[simp] theorem size_insertAt (as : Array α) (i : Fin (as.size+1)) (v : α) :
(as.insertAt i v).size = as.size + 1 := by
rw [insertAt, size_insertAt_loop, size_push]

private theorem get_insertAt_loop_lt (as : Array α) (i : Fin (as.size+1)) (j : Fin bs.size)
(k) (hk : k < (insertAt.loop as i bs j).size) (h : k < i) :
(insertAt.loop as i bs j)[k] = bs[k]'(size_insertAt_loop .. ▸ hk) := by
unfold insertAt.loop
split
· have h1 : k ≠ j - 1 := by omega
have h2 : k ≠ j := by omega
rw [get_insertAt_loop_lt, getElem_swap, if_neg h1, if_neg h2]
exact h
· rfl

private theorem get_insertAt_loop_gt (as : Array α) (i : Fin (as.size+1)) (j : Fin bs.size)
(k) (hk : k < (insertAt.loop as i bs j).size) (hgt : j < k) :
(insertAt.loop as i bs j)[k] = bs[k]'(size_insertAt_loop .. ▸ hk) := by
unfold insertAt.loop
split
· have h1 : k ≠ j - 1 := by omega
have h2 : k ≠ j := by omega
rw [get_insertAt_loop_gt, getElem_swap, if_neg h1, if_neg h2]
exact Nat.lt_of_le_of_lt (Nat.pred_le _) hgt
· rfl

private theorem get_insertAt_loop_eq (as : Array α) (i : Fin (as.size+1)) (j : Fin bs.size)
(k) (hk : k < (insertAt.loop as i bs j).size) (heq : i = k) (h : i.val ≤ j.val) :
(insertAt.loop as i bs j)[k] = bs[j] := by
unfold insertAt.loop
split
· next h =>
rw [get_insertAt_loop_eq, Fin.getElem_fin, getElem_swap, if_pos rfl]
exact heq
exact Nat.le_pred_of_lt h
· congr; omega

private theorem get_insertAt_loop_gt_le (as : Array α) (i : Fin (as.size+1)) (j : Fin bs.size)
(k) (hk : k < (insertAt.loop as i bs j).size) (hgt : i < k) (hle : k ≤ j) :
(insertAt.loop as i bs j)[k] = bs[k-1] := by
unfold insertAt.loop
split
· next h =>
if h0 : k = j then
cases h0
have h1 : j.val ≠ j - 1 := by omega
rw [get_insertAt_loop_gt, getElem_swap, if_neg h1, if_pos rfl]; rfl
exact Nat.pred_lt_of_lt hgt
else
have h1 : k - 1 ≠ j - 1 := by omega
have h2 : k - 1 ≠ j := by omega
rw [get_insertAt_loop_gt_le, getElem_swap, if_neg h1, if_neg h2]
apply Nat.le_of_lt_add_one
rw [Nat.sub_one_add_one]
exact Nat.lt_of_le_of_ne hle h0
exact Nat.not_eq_zero_of_lt h
exact hgt
· next h =>
absurd h
exact Nat.lt_of_lt_of_le hgt hle

theorem getElem_insertAt_lt (as : Array α) (i : Fin (as.size+1)) (v : α)
(k) (hlt : k < i.val) {hk : k < (as.insertAt i v).size} {hk' : k < as.size} :
(as.insertAt i v)[k] = as[k] := by
simp only [insertAt]
rw [get_insertAt_loop_lt, getElem_push, dif_pos hk']
exact hlt

theorem getElem_insertAt_gt (as : Array α) (i : Fin (as.size+1)) (v : α)
(k) (hgt : k > i.val) {hk : k < (as.insertAt i v).size} {hk' : k - 1 < as.size} :
(as.insertAt i v)[k] = as[k - 1] := by
simp only [insertAt]
rw [get_insertAt_loop_gt_le, getElem_push, dif_pos hk']
exact hgt
rw [size_insertAt] at hk
exact Nat.le_of_lt_succ hk

theorem getElem_insertAt_eq (as : Array α) (i : Fin (as.size+1)) (v : α)
(k) (heq : i.val = k) {hk : k < (as.insertAt i v).size} :
(as.insertAt i v)[k] = v := by
simp only [insertAt]
rw [get_insertAt_loop_eq, Fin.getElem_fin, getElem_push_eq]
exact heq
exact Nat.le_of_lt_succ i.is_lt
-- FIXME: temporarily commented out; I've broken this on nightly-2024-11-20,
-- and am hoping it is not necessary to get Mathlib working again

-- /-! ### insertAt -/

-- private theorem size_insertAt_loop (as : Array α) (i : Fin (as.size+1)) (j : Fin bs.size) :
-- (insertAt.loop as i bs j).size = bs.size := by
-- unfold insertAt.loop
-- split
-- · rw [size_insertAt_loop, size_swap]
-- · rfl

-- @[simp] theorem size_insertAt (as : Array α) (i : Fin (as.size+1)) (v : α) :
-- (as.insertAt i v).size = as.size + 1 := by
-- rw [insertAt, size_insertAt_loop, size_push]

-- private theorem get_insertAt_loop_lt (as : Array α) (i : Fin (as.size+1)) (j : Fin bs.size)
-- (k) (hk : k < (insertAt.loop as i bs j).size) (h : k < i) :
-- (insertAt.loop as i bs j)[k] = bs[k]'(size_insertAt_loop .. ▸ hk) := by
-- unfold insertAt.loop
-- split
-- · have h1 : k ≠ j - 1 := by omega
-- have h2 : k ≠ j := by omega
-- rw [get_insertAt_loop_lt, getElem_swap, if_neg h1, if_neg h2]
-- exact h
-- · rfl

-- private theorem get_insertAt_loop_gt (as : Array α) (i : Fin (as.size+1)) (j : Fin bs.size)
-- (k) (hk : k < (insertAt.loop as i bs j).size) (hgt : j < k) :
-- (insertAt.loop as i bs j)[k] = bs[k]'(size_insertAt_loop .. ▸ hk) := by
-- unfold insertAt.loop
-- split
-- · have h1 : k ≠ j - 1 := by omega
-- have h2 : k ≠ j := by omega
-- rw [get_insertAt_loop_gt, getElem_swap, if_neg h1, if_neg h2]
-- exact Nat.lt_of_le_of_lt (Nat.pred_le _) hgt
-- · rfl

-- private theorem get_insertAt_loop_eq (as : Array α) (i : Fin (as.size+1)) (j : Fin bs.size)
-- (k) (hk : k < (insertAt.loop as i bs j).size) (heq : i = k) (h : i.val ≤ j.val) :
-- (insertAt.loop as i bs j)[k] = bs[j] := by
-- unfold insertAt.loop
-- split
-- · next h =>
-- rw [get_insertAt_loop_eq, Fin.getElem_fin, getElem_swap, if_pos rfl]
-- exact heq
-- exact Nat.le_pred_of_lt h
-- · congr; omega

-- private theorem get_insertAt_loop_gt_le (as : Array α) (i : Fin (as.size+1)) (j : Fin bs.size)
-- (k) (hk : k < (insertAt.loop as i bs j).size) (hgt : i < k) (hle : k ≤ j) :
-- (insertAt.loop as i bs j)[k] = bs[k-1] := by
-- unfold insertAt.loop
-- split
-- · next h =>
-- if h0 : k = j then
-- cases h0
-- have h1 : j.val ≠ j - 1 := by omega
-- rw [get_insertAt_loop_gt, getElem_swap, if_neg h1, if_pos rfl]; rfl
-- exact Nat.pred_lt_of_lt hgt
-- else
-- have h1 : k - 1 ≠ j - 1 := by omega
-- have h2 : k - 1 ≠ j := by omega
-- rw [get_insertAt_loop_gt_le, getElem_swap, if_neg h1, if_neg h2]
-- apply Nat.le_of_lt_add_one
-- rw [Nat.sub_one_add_one]
-- exact Nat.lt_of_le_of_ne hle h0
-- exact Nat.not_eq_zero_of_lt h
-- exact hgt
-- · next h =>
-- absurd h
-- exact Nat.lt_of_lt_of_le hgt hle

-- theorem getElem_insertAt_lt (as : Array α) (i : Fin (as.size+1)) (v : α)
-- (k) (hlt : k < i.val) {hk : k < (as.insertAt i v).size} {hk' : k < as.size} :
-- (as.insertAt i v)[k] = as[k] := by
-- simp only [insertAt]
-- rw [get_insertAt_loop_lt, getElem_push, dif_pos hk']
-- exact hlt

-- theorem getElem_insertAt_gt (as : Array α) (i : Fin (as.size+1)) (v : α)
-- (k) (hgt : k > i.val) {hk : k < (as.insertAt i v).size} {hk' : k - 1 < as.size} :
-- (as.insertAt i v)[k] = as[k - 1] := by
-- simp only [insertAt]
-- rw [get_insertAt_loop_gt_le, getElem_push, dif_pos hk']
-- exact hgt
-- rw [size_insertAt] at hk
-- exact Nat.le_of_lt_succ hk

-- theorem getElem_insertAt_eq (as : Array α) (i : Fin (as.size+1)) (v : α)
-- (k) (heq : i.val = k) {hk : k < (as.insertAt i v).size} :
-- (as.insertAt i v)[k] = v := by
-- simp only [insertAt]
-- rw [get_insertAt_loop_eq, Fin.getElem_fin, getElem_push_eq]
-- exact heq
-- exact Nat.le_of_lt_succ i.is_lt
7 changes: 7 additions & 0 deletions Batteries/Data/Range/Lemmas.lean
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,11 @@ theorem mem_range'_elems (r : Range) (h : x ∈ List.range' r.start r.numElems r
refine Nat.not_le.1 fun h =>
Nat.not_le.2 h' <| (numElems_le_iff (Nat.pos_of_ne_zero step0)).2 h

-- FIXME: temporarily commented out; I've broken this on nightly-2024-11-20,
-- and am hoping it is not necessary to get Mathlib working again

/-
theorem forIn'_eq_forIn_range' [Monad m] (r : Std.Range)
(init : β) (f : (a : Nat) → a ∈ r → β → m (ForInStep β)) :
forIn' r init f =
Expand Down Expand Up @@ -97,3 +102,5 @@ theorem forIn_eq_forIn_range' [Monad m] (r : Std.Range)
· simp [forIn, forIn']
· suffices ∀ L H, forIn (List.pmap Subtype.mk L H) init (f ·.1) = forIn L init f from this _ ..
intro L; induction L generalizing init <;> intro H <;> simp [*]
-/
2 changes: 1 addition & 1 deletion Batteries/Data/UnionFind/Basic.lean
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ theorem rank'_lt_rankMax (self : UnionFind) (i : Nat) (h) : (self.arr[i]).rank <
| a::l, _, List.Mem.head _ => by dsimp; apply Nat.le_max_left
| a::l, _, .tail _ h => by dsimp; exact Nat.le_trans (go h) (Nat.le_max_right ..)
simp only [Array.get_eq_getElem, rankMax, ← Array.foldr_toList]
exact Nat.lt_succ.2 <| go (self.arr.toList.get_mem i h)
exact Nat.lt_succ.2 <| go (self.arr.toList.getElem_mem h)

theorem rankD_lt_rankMax (self : UnionFind) (i : Nat) :
rankD self.arr i < self.rankMax := by
Expand Down
7 changes: 7 additions & 0 deletions Batteries/Data/Vector/Basic.lean
Original file line number Diff line number Diff line change
Expand Up @@ -263,6 +263,11 @@ proof_wanted instLawfulBEq (α n) [BEq α] [LawfulBEq α] : LawfulBEq (Vector α
@[inline] def reverse (v : Vector α n) : Vector α n :=
⟨v.toArray.reverse, by simp⟩

-- FIXME: temporarily commented out; I've broken this on nightly-2024-11-20,
-- and am hoping it is not necessary to get Mathlib working again

/-
/-- Delete an element of a vector using a `Fin` index. -/
@[inline] def feraseIdx (v : Vector α n) (i : Fin n) : Vector α (n-1) :=
⟨v.toArray.feraseIdx (Fin.cast v.size_toArray.symm i), by simp [Array.size_feraseIdx]
Expand All @@ -289,6 +294,8 @@ synthesise a proof that the index is within bounds.
@[inline] def eraseIdxN (v : Vector α n) (i : Nat) (h : i < n := by get_elem_tactic) :
Vector α (n - 1) := ⟨v.toArray.eraseIdxN i (by simp [*]), by simp⟩
-/

/--
Finds the first index of a given value in a vector using `==` for comparison. Returns `none` if the
no element of the index matches the given value.
Expand Down
4 changes: 2 additions & 2 deletions Batteries/Tactic/Lint/Frontend.lean
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ sanity check, lint, cleanup, command, tactic
-/

namespace Batteries.Tactic.Lint
open Lean
open Lean Elab Command

/-- Verbosity for the linter output. -/
inductive LintVerbosity
Expand Down Expand Up @@ -104,7 +104,7 @@ def lintCore (decls : Array Name) (linters : Array NamedLinter) :
(linter, ·) <$> decls.mapM fun decl => (decl, ·) <$> do
BaseIO.asTask do
match ← withCurrHeartbeats (linter.test decl)
|>.run' {}
|>.run' mkMetaContext -- We use the context used by `Command.liftTermElabM`
|>.run' {options, fileName := "", fileMap := default} {env}
|>.toBaseIO with
| Except.ok msg? => pure msg?
Expand Down
2 changes: 1 addition & 1 deletion Batteries/Tactic/Lint/Simp.lean
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ see note [simp-normal form] for tips how to debug this.
https://leanprover-community.github.io/mathlib_docs/notes.html#simp-normal%20form"
test := fun declName => do
unless ← isSimpTheorem declName do return none
let ctx := ← Simp.Context.mkDefault
let ctx ← Simp.Context.mkDefault
checkAllSimpTheoremInfos (← getConstInfo declName).type fun {lhs, rhs, isConditional, ..} => do
let isRfl ← isRflTheorem declName
let ({ expr := lhs', proof? := prf1, .. }, prf1Stats) ←
Expand Down
13 changes: 13 additions & 0 deletions BatteriesTest/lint_simpNF.lean
Original file line number Diff line number Diff line change
Expand Up @@ -38,4 +38,17 @@ theorem foo_eq_ite (n : Nat) : foo n = if n = n then n else 0 := by

end Equiv

namespace List

private axiom test_sorry : ∀ {α}, α

@[simp]
theorem ofFn_getElem_eq_map {β : Type _} (l : List α) (f : α → β) :
ofFn (fun i : Fin l.length => f <| l[(i : Nat)]) = l.map f := test_sorry

example {β : Type _} (l : List α) (f : α → β) :
ofFn (fun i : Fin l.length => f <| l[(i : Nat)]) = l.map f := by simp only [ofFn_getElem_eq_map]

end List

#lint- only simpNF
2 changes: 1 addition & 1 deletion BatteriesTest/show_term.lean
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ Authors: Kim Morrison
exact n
exact 37

/-- info: Try this: refine (?fst, ?snd) -/
/-- info: Try this: refine (?_, ?_) -/
#guard_msgs in example : Nat × Nat := by
show_term constructor
repeat exact 42
Expand Down

0 comments on commit 493818e

Please sign in to comment.