Zulip Chat Archive
Stream: lean4
Topic: Monad assumptions in fields of other typeclasses
Eric Wieser (Sep 01 2025 at 12:40):
I found this definitions rather strange:
class ForM (m : Type u → Type v) (γ : Type w₁) (α : outParam (Type w₂)) where
/--
Runs the monadic action `f` on each element of the collection `coll`.
-/
forM [Monad m] (coll : γ) (f : α → m PUnit) : m PUnit
what is the [Monad m] argument doing there? Shouldn't it be up to individual instances to decide if they need m to be a Monad?
Aaron Liu (Sep 01 2025 at 12:47):
That not how it works that's so weird
Eric Wieser (Sep 01 2025 at 12:50):
In particular, this means instance : ForM Id _ _ requires you to implement ForM for any possible Monad Id, not just the canonical one.
Eric Wieser (Sep 01 2025 at 13:40):
lean4#10204 tries removing it
François G. Dorais (Sep 02 2025 at 15:53):
It is odd looking but it allows for the Monad m instance to be defined after the ForM instance.
Aaron Liu (Sep 02 2025 at 15:58):
François G. Dorais said:
It is odd looking but it allows for the
Monad minstance to be defined after theForMinstance.
You can do that without hard-coding it into ForM
def myMonad := sorry
instance [Monad myMonad] : ForM myMonad foo bar := sorry
instance : Monad myMonad := sorry
Eric Wieser (Sep 02 2025 at 15:59):
Note that nowhere in core or batteries does this
François G. Dorais (Sep 02 2025 at 16:04):
You're totally right! I guess it just prevents hard-coding the monad. I don't know whether there is a good reason to do that.
Eric Wieser (Sep 02 2025 at 16:10):
I think this is pretty clearly the wrong way to use typeclasses generally, and if this was deliberate it was a hack to make elaboration behave better
Eric Wieser (Sep 02 2025 at 16:10):
(there is a small elaboration regression that impacts two lean4 tests where the type of the monad cannot be inferred from the outside in)
Last updated: Dec 20 2025 at 21:32 UTC