Skip to content

Commit 0ee7a8a

Browse files
committed
refactor(Data/PFunctor/FreeM): flip pure_eq_pure direction
Flip `pure_eq_pure` to normalize `FreeM.pure` to `pure` (typeclass), matching the direction advocated in leanprover#417. Adjust proofs of `bind_pure`, `bind_pure_comp`, `bind_eq_pure_iff`, `pure_eq_bind_iff`, `pure_inj`, `mapM_bind`, and `mapM_map` to account for the new simp normal form. Made-with: Cursor
1 parent ff3359e commit 0ee7a8a

1 file changed

Lines changed: 21 additions & 13 deletions

File tree

Cslib/Foundations/Data/PFunctor/FreeM.lean

Lines changed: 21 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ variable {P : PFunctor.{uA, uB}} {α β γ : Type v}
5151
instance : Pure (FreeM P) where pure := .pure
5252

5353
@[simp]
54-
theorem pure_eq_pure : (pure : α → FreeM P α) = FreeM.pure := rfl
54+
theorem pure_eq_pure : (FreeM.pure : α → FreeM P α) = pure := rfl
5555

5656
/-- Lift an object of the base polynomial functor into the free monad. -/
5757
def lift (x : P.Obj α) : FreeM P α := FreeM.liftBind x.1 (fun y ↦ FreeM.pure (x.2 y))
@@ -118,12 +118,13 @@ lemma pure_bind (a : α) (f : α → FreeM P β) :
118118
(FreeM.pure a).bind f = f a := rfl
119119

120120
@[simp]
121-
lemma bind_pure : ∀ x : FreeM P α, x.bind (.pure) = x
121+
lemma bind_pure : ∀ x : FreeM P α, x.bind pure = x
122122
| .pure a => rfl
123-
| .liftBind a cont => by simp [FreeM.bind, bind_pure]
123+
| .liftBind a cont => by
124+
simp only [FreeM.bind]; congr 1; funext u; exact bind_pure (cont u)
124125

125126
@[simp]
126-
lemma bind_pure_comp (f : α → β) : ∀ x : FreeM P α, x.bind (.pure ∘ f) = map f x
127+
lemma bind_pure_comp (f : α → β) : ∀ x : FreeM P α, x.bind (pure ∘ f) = map f x
127128
| .pure a => rfl
128129
| .liftBind a cont => by simp only [FreeM.bind, map, bind_pure_comp]
129130

@@ -136,12 +137,16 @@ lemma lift_bind (x : P.Obj α) (f : α → FreeM P β) :
136137
(FreeM.lift x).bind f = FreeM.liftBind x.1 (fun a ↦ f (x.2 a)) := rfl
137138

138139
@[simp] lemma bind_eq_pure_iff (x : FreeM P α) (f : α → FreeM P β) (b : β) :
139-
x.bind f = FreeM.pure b ↔ ∃ a, x = pure a ∧ f a = pure b := by
140-
cases x <;> simp
140+
x.bind f = pure b ↔ ∃ a, x = pure a ∧ f a = pure b := by
141+
cases x with
142+
| pure a => exact ⟨fun h => ⟨a, rfl, h⟩, fun ⟨_, h, hf⟩ => by rwa [FreeM.pure.inj h]⟩
143+
| liftBind a cont => simp [FreeM.bind]
141144

142145
@[simp] lemma pure_eq_bind_iff (x : FreeM P α) (f : α → FreeM P β) (b : β) :
143-
FreeM.pure b = x.bind f ↔ ∃ a, x = pure a ∧ pure b = f a := by
144-
cases x <;> simp
146+
pure b = x.bind f ↔ ∃ a, x = pure a ∧ pure b = f a := by
147+
cases x with
148+
| pure a => exact ⟨fun h => ⟨a, rfl, h⟩, fun ⟨_, h, hf⟩ => by rwa [FreeM.pure.inj h]⟩
149+
| liftBind a cont => simp [FreeM.bind]
145150

146151
instance : LawfulFunctor (FreeM P) where
147152
map_const := rfl
@@ -156,7 +161,10 @@ instance : LawfulMonad (FreeM P) := LawfulMonad.mk'
156161
(pure_bind := pure_bind)
157162
(bind_assoc := FreeM.bind_assoc)
158163

159-
lemma pure_inj (a b : α) : FreeM.pure (P := P) a = FreeM.pure b ↔ a = b := by simp
164+
lemma pure_inj (a b : α) : (pure a : FreeM P α) = pure b ↔ a = b := by
165+
constructor
166+
· exact FreeM.pure.inj
167+
· rintro rfl; rfl
160168

161169
@[simp] lemma liftBind_inj (a a' : P.A)
162170
(cont : P.B a → P.FreeM α) (cont' : P.B a' → P.FreeM α) :
@@ -237,8 +245,8 @@ variable [LawfulMonad m]
237245
@[simp]
238246
lemma mapM_bind {α β : Type uB} (x : FreeM P α) (f : α → FreeM P β) :
239247
(x.bind f).mapM interp = x.mapM interp >>= fun u => (f u).mapM interp := by
240-
induction x using FreeM.inductionOn with
241-
| pure _ => simp
248+
induction x with
249+
| pure _ => simp [FreeM.bind, FreeM.mapM]
242250
| liftBind a cont h => simp [h]
243251

244252
@[simp]
@@ -249,8 +257,8 @@ lemma mapM_bind' {α β : Type uB} (x : FreeM P α) (f : α → FreeM P β) :
249257
@[simp]
250258
lemma mapM_map {α β : Type uB} (x : FreeM P α) (f : α → β) :
251259
FreeM.mapM interp (map f x) = f <$> FreeM.mapM interp x := by
252-
induction x using FreeM.inductionOn with
253-
| pure _ => simp
260+
induction x with
261+
| pure _ => simp [map, FreeM.mapM]
254262
| liftBind a cont h => simp [h]
255263

256264
@[simp]

0 commit comments

Comments
 (0)