Skip to content

Commit fc88a58

Browse files
author
Sebastian Graf
committed
fix: Remove simp annotations for Id.{pure_eq,map_eq,bind_eq}
This PR removes `simp` annotations for lemmas `Id.pure_eq`, `Id.map_eq` and `Id.bind_eq` because they tend to break the abstraction of computations in `Id`. In particular, they are problematic for compositional reasoning about `do` notation. While a better `simp` framework is on the horizon with #7352; this PR simply adds the old simp lemmas wherever needed. Breaking change: Remove the `simp` annotation from `Id.pure_eq`, `Id.map_eq` and `Id.bind_eq`. Workaround: add manually to simp set.
1 parent 02f7a1d commit fc88a58

File tree

19 files changed

+57
-57
lines changed

19 files changed

+57
-57
lines changed

src/Init/Control/Lawful/Basic.lean

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -238,9 +238,9 @@ theorem LawfulMonad.mk' (m : Type u → Type v) [Monad m]
238238

239239
namespace Id
240240

241-
@[simp] theorem map_eq (x : Id α) (f : α → β) : f <$> x = f x := rfl
242-
@[simp] theorem bind_eq (x : Id α) (f : α → id β) : x >>= f = f x := rfl
243-
@[simp] theorem pure_eq (a : α) : (pure a : Id α) = a := rfl
241+
theorem map_eq (x : Id α) (f : α → β) : f <$> x = f x := rfl
242+
theorem bind_eq (x : Id α) (f : α → id β) : x >>= f = f x := rfl
243+
theorem pure_eq (a : α) : (pure a : Id α) = a := rfl
244244

245245
instance : LawfulMonad Id := by
246246
refine LawfulMonad.mk' _ ?_ ?_ ?_ <;> intros <;> rfl

src/Init/Data/Array/Lemmas.lean

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -595,7 +595,7 @@ theorem anyM_loop_iff_exists {p : α → Bool} {as : Array α} {start stop} (h :
595595
∃ (i : Nat) (_ : i < as.size), start ≤ i ∧ i < stop ∧ p as[i] = true := by
596596
unfold anyM.loop
597597
split <;> rename_i h₁
598-
· dsimp
598+
· dsimp [Id.pure_eq, Id.bind_eq]
599599
split <;> rename_i h₂
600600
· simp only [true_iff]
601601
refine ⟨start, by omega, by omega, by omega, h₂⟩
@@ -1322,8 +1322,8 @@ theorem mapM_map_eq_foldl {as : Array α} {f : α → β} {i : Nat} :
13221322
-- Calling `split` here gives a bad goal.
13231323
have : size as - i = Nat.succ (size as - i - 1) := by omega
13241324
rw [this]
1325-
simp [foldl, foldlM, Id.run, Nat.sub_add_eq]
1326-
· dsimp [foldl, Id.run, foldlM]
1325+
simp [foldl, foldlM, Id.run, Nat.sub_add_eq, Id.pure_eq, Id.bind_eq]
1326+
· dsimp [foldl, Id.run, foldlM, Id.pure_eq, Id.bind_eq]
13271327
rw [dif_pos (by omega), foldlM.loop, dif_neg h]
13281328
rfl
13291329
termination_by as.size - i
@@ -1539,8 +1539,8 @@ theorem filterMap_congr {as bs : Array α} (h : as = bs)
15391539
as.toList ++ List.filterMap f xs := ?_
15401540
exact this #[]
15411541
induction xs
1542-
· simp_all [Id.run]
1543-
· simp_all [Id.run, List.filterMap_cons]
1542+
· simp_all [Id.run, Id.pure_eq]
1543+
· simp_all [Id.run, List.filterMap_cons, Id.bind_eq, Id.pure_eq]
15441544
split <;> simp_all
15451545

15461546
theorem toList_filterMap {f : α → Option β} {xs : Array α} :
@@ -3377,15 +3377,15 @@ theorem foldrM_append [Monad m] [LawfulMonad m] {f : α → β → m β} {b} {xs
33773377
(w : start = xs.size + ys.size) :
33783378
(xs ++ ys).foldr f b start 0 = xs.foldr f (ys.foldr f b) := by
33793379
subst w
3380-
simp [foldr_eq_foldrM]
3380+
simp [foldr_eq_foldrM, Id.bind_eq]
33813381

33823382
theorem foldl_append {β : Type _} {f : β → α → β} {b} {xs ys : Array α} :
33833383
(xs ++ ys).foldl f b = ys.foldl f (xs.foldl f b) := by
3384-
simp [foldl_eq_foldlM]
3384+
simp [foldl_eq_foldlM, Id.bind_eq]
33853385

33863386
theorem foldr_append {f : α → β → β} {b} {xs ys : Array α} :
33873387
(xs ++ ys).foldr f b = xs.foldr f (ys.foldr f b) := by
3388-
simp [foldr_eq_foldrM]
3388+
simp [foldr_eq_foldrM, Id.bind_eq]
33893389

33903390
@[simp] theorem foldl_flatten' {f : β → α → β} {b} {xss : Array (Array α)} {stop : Nat}
33913391
(w : stop = xss.flatten.size) :
@@ -3654,7 +3654,7 @@ abbrev pop_mkArray := @pop_replicate
36543654

36553655
@[simp] theorem size_modify {xs : Array α} {i : Nat} {f : α → α} : (xs.modify i f).size = xs.size := by
36563656
unfold modify modifyM Id.run
3657-
split <;> simp
3657+
split <;> simp [Id.pure_eq, Id.bind_eq]
36583658

36593659
theorem getElem_modify {xs : Array α} {j i} (h : i < (xs.modify j f).size) :
36603660
(xs.modify j f)[i] = if j = i then f (xs[i]'(by simpa using h)) else xs[i]'(by simpa using h) := by

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

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -26,11 +26,11 @@ protected theorem not_le_iff_gt [DecidableEq α] [LT α] [DecidableLT α] {l₁
2626
Decidable.not_not
2727

2828
@[simp] theorem lex_empty [BEq α] {lt : α → α → Bool} {xs : Array α} : xs.lex #[] lt = false := by
29-
simp [lex, Id.run]
29+
simp [lex, Id.run, Id.pure_eq, Id.bind_eq]
3030

3131
@[simp] theorem singleton_lex_singleton [BEq α] {lt : α → α → Bool} : #[a].lex #[b] lt = lt a b := by
3232
simp only [lex, List.getElem_toArray, List.getElem_singleton]
33-
cases lt a b <;> cases a != b <;> simp [Id.run]
33+
cases lt a b <;> cases a != b <;> simp [Id.run, Id.pure_eq, Id.bind_eq]
3434

3535
private theorem cons_lex_cons [BEq α] {lt : α → α → Bool} {a b : α} {xs ys : Array α} :
3636
(#[a] ++ xs).lex (#[b] ++ ys) lt =
@@ -42,16 +42,16 @@ private theorem cons_lex_cons [BEq α] {lt : α → α → Bool} {a b : α} {xs
4242
getElem_append_right]
4343
cases lt a b
4444
· rw [bne]
45-
cases a == b <;> simp
46-
· simp
45+
cases a == b <;> simp [Id.pure_eq, Id.bind_eq]
46+
· simp [Id.pure_eq, Id.bind_eq]
4747

4848
@[simp] theorem _root_.List.lex_toArray [BEq α] {lt : α → α → Bool} {l₁ l₂ : List α} :
4949
l₁.toArray.lex l₂.toArray lt = l₁.lex l₂ lt := by
5050
induction l₁ generalizing l₂ with
51-
| nil => cases l₂ <;> simp [lex, Id.run]
51+
| nil => cases l₂ <;> simp [lex, Id.run, Id.pure_eq, Id.bind_eq]
5252
| cons x l₁ ih =>
5353
cases l₂ with
54-
| nil => simp [lex, Id.run]
54+
| nil => simp [lex, Id.run, Id.pure_eq, Id.bind_eq]
5555
| cons y l₂ =>
5656
rw [List.toArray_cons, List.toArray_cons y, cons_lex_cons, List.lex, ih]
5757

src/Init/Data/Fin/Fold.lean

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -189,7 +189,7 @@ theorem foldl_succ_last (f : α → Fin (n+1) → α) (x) :
189189

190190
theorem foldl_eq_foldlM (f : α → Fin n → α) (x) :
191191
foldl n f x = foldlM (m:=Id) n f x := by
192-
induction n generalizing x <;> simp [foldl_succ, foldlM_succ, *]
192+
induction n generalizing x <;> simp [foldl_succ, foldlM_succ, *, Id.pure_eq, Id.bind_eq]
193193

194194
/-! ### foldr -/
195195

@@ -222,7 +222,7 @@ theorem foldr_succ_last (f : Fin (n+1) → α → α) (x) :
222222

223223
theorem foldr_eq_foldrM (f : Fin n → α → α) (x) :
224224
foldr n f x = foldrM (m:=Id) n f x := by
225-
induction n <;> simp [foldr_succ, foldrM_succ, *]
225+
induction n <;> simp [foldr_succ, foldrM_succ, *, Id.pure_eq, Id.bind_eq]
226226

227227
theorem foldl_rev (f : Fin n → α → α) (x) :
228228
foldl n (fun x i => f i.rev x) x = foldr n f x := by

src/Init/Data/List/Lemmas.lean

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2558,11 +2558,11 @@ theorem flatMap_reverse {β} {l : List α} {f : α → List β} : (l.reverse.fla
25582558

25592559
theorem foldl_eq_foldlM {f : β → α → β} {b : β} {l : List α} :
25602560
l.foldl f b = l.foldlM (m := Id) f b := by
2561-
induction l generalizing b <;> simp [*, foldl]
2561+
induction l generalizing b <;> simp [*, foldl, Id.pure_eq, Id.bind_eq]
25622562

25632563
theorem foldr_eq_foldrM {f : α → β → β} {b : β} {l : List α} :
25642564
l.foldr f b = l.foldrM (m := Id) f b := by
2565-
induction l <;> simp [*, foldr]
2565+
induction l <;> simp [*, foldr, Id.pure_eq, Id.bind_eq]
25662566

25672567
@[simp] theorem id_run_foldlM {f : β → α → Id β} {b : β} {l : List α} :
25682568
Id.run (l.foldlM f b) = l.foldl f b := foldl_eq_foldlM.symm
@@ -2659,10 +2659,10 @@ theorem foldr_map_hom {g : α → β} {f : α → α → α} {f' : β → β →
26592659
induction l <;> simp [*]
26602660

26612661
@[simp] theorem foldl_append {β : Type _} {f : β → α → β} {b : β} {l l' : List α} :
2662-
(l ++ l').foldl f b = l'.foldl f (l.foldl f b) := by simp [foldl_eq_foldlM]
2662+
(l ++ l').foldl f b = l'.foldl f (l.foldl f b) := by simp [foldl_eq_foldlM, Id.bind_eq]
26632663

26642664
@[simp] theorem foldr_append {f : α → β → β} {b : β} {l l' : List α} :
2665-
(l ++ l').foldr f b = l.foldr f (l'.foldr f b) := by simp [foldr_eq_foldrM]
2665+
(l ++ l').foldr f b = l.foldr f (l'.foldr f b) := by simp [foldr_eq_foldrM, Id.bind_eq]
26662666

26672667
theorem foldl_flatten {f : β → α → β} {b : β} {L : List (List α)} :
26682668
(flatten L).foldl f b = L.foldl (fun b l => l.foldl f b) b := by

src/Init/Data/List/Monadic.lean

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -341,7 +341,7 @@ theorem forIn'_eq_foldlM [Monad m] [LawfulMonad m]
341341
forIn' (m := Id) l init (fun a m b => .yield (f a m b)) =
342342
l.attach.foldl (fun b ⟨a, h⟩ => f a h b) init := by
343343
simp only [forIn'_eq_foldlM]
344-
induction l.attach generalizing init <;> simp_all
344+
induction l.attach generalizing init <;> simp_all[Id.bind_eq, Id.pure_eq, Id.map_eq]
345345

346346
@[simp] theorem forIn'_map [Monad m] [LawfulMonad m]
347347
{l : List α} (g : α → β) (f : (b : β) → b ∈ l.map g → γ → m (ForInStep γ)) :
@@ -394,7 +394,7 @@ theorem forIn_eq_foldlM [Monad m] [LawfulMonad m]
394394
forIn (m := Id) l init (fun a b => .yield (f a b)) =
395395
l.foldl (fun b a => f a b) init := by
396396
simp only [forIn_eq_foldlM]
397-
induction l generalizing init <;> simp_all
397+
induction l generalizing init <;> simp_all[Id.bind_eq, Id.pure_eq, Id.map_eq]
398398

399399
@[simp] theorem forIn_map [Monad m] [LawfulMonad m]
400400
{l : List α} {g : α → β} {f : β → γ → m (ForInStep γ)} :

src/Init/Data/Option/Monadic.lean

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ theorem forIn'_eq_pelim [Monad m] [LawfulMonad m]
5656
(o : Option α) (f : (a : α) → a ∈ o → β → β) (b : β) :
5757
forIn' (m := Id) o b (fun a m b => .yield (f a m b)) =
5858
o.pelim b (fun a h => f a h b) := by
59-
cases o <;> simp
59+
cases o <;> simp [Id.bind_eq, Id.pure_eq]
6060

6161
@[simp] theorem forIn'_map [Monad m] [LawfulMonad m]
6262
(o : Option α) (g : α → β) (f : (b : β) → b ∈ o.map g → γ → m (ForInStep γ)) :
@@ -85,7 +85,7 @@ theorem forIn_eq_elim [Monad m] [LawfulMonad m]
8585
(o : Option α) (f : (a : α) → β → β) (b : β) :
8686
forIn (m := Id) o b (fun a b => .yield (f a b)) =
8787
o.elim b (fun a => f a b) := by
88-
cases o <;> simp
88+
cases o <;> simp [Id.bind_eq, Id.pure_eq]
8989

9090
@[simp] theorem forIn_map [Monad m] [LawfulMonad m]
9191
(o : Option α) (g : α → β) (f : β → γ → m (ForInStep γ)) :

src/Init/Data/Vector/Lemmas.lean

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2397,10 +2397,10 @@ theorem foldr_map_hom {g : α → β} {f : α → α → α} {f' : β → β →
23972397
simp
23982398

23992399
@[simp] theorem foldl_append {β : Type _} {f : β → α → β} {b} {xs : Vector α n} {ys : Vector α k} :
2400-
(xs ++ ys).foldl f b = ys.foldl f (xs.foldl f b) := by simp [foldl_eq_foldlM]
2400+
(xs ++ ys).foldl f b = ys.foldl f (xs.foldl f b) := by simp [foldl_eq_foldlM, Id.pure_eq, Id.bind_eq]
24012401

24022402
@[simp] theorem foldr_append {f : α → β → β} {b} {xs : Vector α n} {ys : Vector α k} :
2403-
(xs ++ ys).foldr f b = xs.foldr f (ys.foldr f b) := by simp [foldr_eq_foldrM]
2403+
(xs ++ ys).foldr f b = xs.foldr f (ys.foldr f b) := by simp [foldr_eq_foldrM, Id.pure_eq, Id.bind_eq]
24042404

24052405
@[simp] theorem foldl_flatten {f : β → α → β} {b} {xss : Vector (Vector α m) n} :
24062406
(flatten xss).foldl f b = xss.foldl (fun b xs => xs.foldl f b) b := by

src/Init/Data/Vector/Lex.lean

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ protected theorem not_le_iff_gt [DecidableEq α] [LT α] [DecidableLT α] {xs ys
5757

5858
@[simp] theorem singleton_lex_singleton [BEq α] {lt : α → α → Bool} : #v[a].lex #v[b] lt = lt a b := by
5959
simp only [lex, getElem_mk, List.getElem_toArray, List.getElem_singleton]
60-
cases lt a b <;> cases a != b <;> simp [Id.run]
60+
cases lt a b <;> cases a != b <;> simp [Id.run, Id.pure_eq, Id.bind_eq]
6161

6262
protected theorem lt_irrefl [LT α] [Std.Irrefl (· < · : α → α → Prop)] (xs : Vector α n) : ¬ xs < xs :=
6363
Array.lt_irrefl xs.toArray

src/Std/Data/DHashMap/Internal/AssocList/Lemmas.lean

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ open Std.Internal
3636
@[simp]
3737
theorem foldl_eq {f : δ → (a : α) → β a → δ} {init : δ} {l : AssocList α β} :
3838
l.foldl f init = l.toList.foldl (fun d p => f d p.1 p.2) init := by
39-
induction l generalizing init <;> simp_all [foldl, Id.run, foldlM]
39+
induction l generalizing init <;> simp_all [foldl, Id.run, foldlM, Id.pure_eq, Id.bind_eq]
4040

4141
@[simp]
4242
theorem length_eq {l : AssocList α β} : l.length = l.toList.length := by
@@ -260,11 +260,11 @@ end Const
260260
theorem foldl_apply {l : AssocList α β} {acc : List δ} (f : (a : α) → β a → δ) :
261261
l.foldl (fun acc k v => f k v :: acc) acc =
262262
(l.toList.map (fun p => f p.1 p.2)).reverse ++ acc := by
263-
induction l generalizing acc <;> simp_all [AssocList.foldl, AssocList.foldlM, Id.run]
263+
induction l generalizing acc <;> simp_all [AssocList.foldl, AssocList.foldlM, Id.run, Id.pure_eq, Id.bind_eq]
264264

265265
theorem foldr_apply {l : AssocList α β} {acc : List δ} (f : (a : α) → β a → δ) :
266266
l.foldr (fun k v acc => f k v :: acc) acc =
267267
(l.toList.map (fun p => f p.1 p.2)) ++ acc := by
268-
induction l generalizing acc <;> simp_all [AssocList.foldr, AssocList.foldrM, Id.run]
268+
induction l generalizing acc <;> simp_all [AssocList.foldr, AssocList.foldrM, Id.run, Id.pure_eq, Id.bind_eq]
269269

270270
end Std.DHashMap.Internal.AssocList

0 commit comments

Comments
 (0)