feat(Monad): define coproducts by their universal property #1197
Did you consider a more computational design which defines IsMonadCoproduct as data, which instead of the ∃! h ... the map desc is included in the definition. Something like:
/-- The maps `inl` and `inr` exhibit `p` as a coproduct of lawful monads. -/
structure IsMonadCoproduct
(m n p : Type u → Type v)
[Monad m] [LawfulMonad m]
[Monad n] [LawfulMonad n]
[Monad p] [LawfulMonad p]
(inl : ∀ {α}, m α → p α)
(inr : ∀ {α}, n α → p α) where
/-- The left inclusion preserves monadic structure. -/
inl_hom : IsMonadHom m p inl
/-- The right inclusion preserves monadic structure. -/
inr_hom : IsMonadHom n p inr
/-- Every pair of monad morphisms has descends to the coproduct. -/
desc {t : Type u → Type v} [Monad t] [LawfulMonad t]
{f : ∀ {α}, m α → t α} {g : ∀ {α}, n α → t α}
(hf : IsMonadHom m t f) (hg : IsMonadHom n t g) :
∀ {α}, p α → t α
isMonadHom_desc {t : Type u → Type v} [Monad t] [LawfulMonad t]
{f : ∀ {α}, m α → t α} {g : ∀ {α}, n α → t α}
(hf : IsMonadHom m t f) (hg : IsMonadHom n t g) :
IsMonadHom p t (desc hf hg)
desc_inl ...
desc_inr ...
desc_uniq ...
i ask because a) in the free monad example you construct in a followup
desc is computed explicitly, and b) this is how (co)limits are defined in mathlib, with alternative noncomputable constructors when all you have an an existential.def copy {inl' : ∀ {α}, m α → p α} {inr' : ∀ {α}, n α → p α}
(hinl : @inl = @inl') (hinr : @inr = @inr') : IsMonadCoproduct m n p inl' inr' :=
hinl ▸ hinr ▸ h
@[simp] lemma desc_copy {inl' : ∀ {α}, m α → p α} {inr' : ∀ {α}, n α → p α}
(hinl : @inl = @inl') (hinr : @inr = @inr') : @(h.copy hinl hinr).desc = @h.desc := by
subst hinl hinr
rfl
i think this lemma could be useful
/-- A coproduct of `m` and `n` is also a coproduct of `n` and `m`. -/
def symm : IsMonadCoproduct n m p inr inl where
inl_hom := h.inr_hom
inr_hom := h.inl_hom
desc hg hf := h.desc hf hg
isMonadHom_desc hg hf := h.isMonadHom_desc hf hg
desc_inl hg hf := h.desc_inr hf hg
desc_inr hg hf := h.desc_inl hf hg
desc_unique hg hf _ hk hinr hinl := h.desc_unique hf hg hk hinl hinr
and similarly elsewhere
h.desc_unique (hk.comp hf) (hk.comp hg) (hk.comp (h.isMonadHom_desc hf hg)) (by simp) (by simp)
we usually prefer not to squeeze terminal
simp calls, unless you had a particular reason? apply elsewhere if notsome optional alternatives (also elsewhere)
theorem desc_inl_inr_apply {α} (x : p α) : h.desc h.inl_hom h.inr_hom x = x :=
congr($h.desc_inl_inr x)
theorem desc_inl_inr_apply {α} (x : p α) : h.desc h.inl_hom h.inr_hom x = x := by
congrm $h.desc_inl_inr x
personally i prefer tactic mode proofs to their long term mode versions for readability. you can also investigate adding [simps] (or [simps!] if necessary) to other structure declarations here.
/-- Coproducts are associative: if `p` is a coproduct of `m` and `n`, `q` is a coproduct of
`p` and `o`, and `r` is a coproduct of `n` and `o`, then `q` is a coproduct of `m` and `r`. -/
@[simps] def assoc :
IsMonadCoproduct m r q (fun x => inl' (inl x))
(hr.desc (hq.inl_hom.comp h.inr_hom) hq.inr_hom) where
inl_hom := hq.inl_hom.comp h.inl_hom
inr_hom := hr.isMonadHom_desc _ _
desc hf hg :=
hq.desc (h.isMonadHom_desc hf (hg.comp hr.inl_hom)) (hg.comp hr.inr_hom)
isMonadHom_desc _ _ := hq.isMonadHom_desc _ _
desc_inl := by simp
desc_inr hf hg α x := by
congrm $(hr.hom_ext ((hq.isMonadHom_desc ..).comp (hr.isMonadHom_desc ..)) hg ?_ ?_) x
all_goals simp
desc_unique hf hg _ hk hinl hinr := by
apply hq.desc_unique _ _ hk
· intro _ x
congrm $(h.hom_ext (hk.comp hq.inl_hom) (h.isMonadHom_desc hf (hg.comp hr.inl_hom)) ?_ ?_) x
· simpa
· intro _ y
simpa using hinr (jnl y)
· intro _ z
simpa using hinr (jnr z)
mostly style comments here, overall i like this. another piece of api you should be able to define is for uniqueness — first if h h' : IsMonadCoproduct m n p inl inr then h = h' (i think? since desc is uniquely determined), and second that the coproduct of m and n is unique up to unique iso. the first you could probably phrase a Subsingleton instance
i'm bikeshedding at this point, but in term mode it's maybe nicer is to use congrm over exact congr(...) (ditto elsewhere, if you decide to apply)
congrm $hh α x
i prefer to bundle these applications into the ext tactic
ext _ x
congrm $(h.desc_unique h'.inl_hom h'.inr_hom he hinl hinr) x
@[simps!]
def unitLeft : IsMonadCoproduct Id m m pure id :=
you could also add the "unapplied version" like you did for eg comp_desc and comp_desc_apply
lgtm, just some final minor comments here — i'm reviewing this mostly as a category theorist not as a computer scientist, though, so i'll defer to a review from someone who's familiar with the uses of monads in cslib before we merge
@thomaskwaring thank you! just pushed another round of fixes
thanks, looks great now -- you'd know better who can review the TCS content, i'll defer to them for final approval
I'm pretty sure this is defeq abuse; you need (pure <| Id.run .)
@f = (fun {α} x => pure x.run) := by
or similar
Are there any trivial instances you can add to show the definition is reasonable? Perhaps at least that IsCoproduct Id X X?
thanks @eric-wieser i just made those changes. note I already have unitLeft and unitRight as trivial instances, did you mean something else?
Is is usually reserved for Prop, but this carries data. Perhaps MonadCoproductLike (akin to RCLike) is a reasonable name, or HasMonadCoproductDesc?
Are we sure desc is what we want here and not lift? I've heard confusion in the past about how in math we say "x descends to the quotient" but lean calls it Quotient.lift, and I think being consistent with the lean terminology (if this would indeed be consistent) is more important.
Oh, I was looking for instance and assumed this was a class. Indeed those two declarations cover what I was thinking of.
There are two non-obvious decisions here that I'd be intereted to hear your thoughts on:
Why structure and not class? If these are subsingleton, then perhaps class is very reasonable
If we move to a class, should inl/inr be bundled? Indeed there is probably not always a unique choice, but is there at least a canonical choice?
Should we require that inl and inr are MonadLift instances?
I think the following definitions might also be useful:
IsMonadCoproduct a b c -> IsMonadCoproduct (StateT a) b (StateT c)
and the symm version, and ReaderT, OptionT, ExceptT
IsMonadCoproduct m Option (OptionT m), as a special case of the above.
You could consider also defining ext_id, which says that @f = @g for any two IsMonadHoms
worth noting that the naming conventions Is<...> and desc agree with mathlib's CategoryTheory.Limits.IsColimit, as does the amount of bundling (the t : Cocone ... in that definition is equivalent to the two injections inl inr + the fact that they are monad homs). the bundled design would be closer to CategoryTheory.Limits.ColimitCocone
thomaskwaring
tannerduve
This PR defines a coproduct of monads. Monads need not have coproducts in general, so we provide a universal property instead of a direct construction. Coproducts of monads provide a way of combining the effects of each component monad.