← Back to Pull requests

feat(Monad): define coproducts by their universal property #1197

Open
tannerduve opened this pull request with 368 lines diff across 4 files
View on GitHub
tannerduve tannerduve commented on Nov 04, 2026 19:57 UTC
Author

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.

Diff: 368 lines (+368 / -0) across 4 files Reviewer: thomaskwaring Review duration: 31765m
All Vouch review confidence checks passed
Residual risk 0.04 = Change Risk (0.43) × (1 − Review Confidence (0.90)). Review depth and reviewer attention state are adequate.
Model 1 · Change Risk (XGBoost)
Score: 0.43 · 368 lines diff across 4 files
0.43
Model 2 · Review Depth (DistilBERT)
Depth Score: 0.88 · 5 substantive comment(s)
0.88
Model 3 · Review Timing & Attention
Session adequacy: 100% · Reviewer familiarity: 50%
90%
Bedrock Claude 3 · Risk Narration
(disconnected)
Groq
Groq Risk Narration
AWS Bedrock is currently disconnected. Generate high-confidence PR triage narration on-demand using Groq.
Model 1 · Change Risk Attribution
SHAP Weights
Diff Size diff_size
0.354
Real PR Conversation Timeline
24 items
thomaskwaring thomaskwaring commented on Nov 05, 2026 11:22 UTC
Reviewer

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.

thomaskwaring thomaskwaring commented on code on Nov 06, 2026 09:35 UTC
Code Review
Cslib/Foundations/Control/Monad/Coproducts.lean:1
@@ -0,0 +1,276 @@
+/-
+Copyright (c) 2026 Tanner Duve. All rights reserved.
+Released under Apache 2.0 license as described in the file LICENSE.
+Authors: Tanner Duve
+-/
+
+module
+
+public import Cslib.Foundations.Control.Monad.IsMonadHom
+
+/-!
+# Coproducts of monads
+
+This file defines the universal property of coproducts of lawful monads (`IsMonadCoproduct`)
+using `IsMonadHom`. The universal property is carried as data: `IsMonadCoproduct.desc`
+interprets the coproduct into any monad equipped with interpretations of both components,
+so that concrete coproducts (such as free monads on a sum of signatures) yield computable
+interpreters.
+
+A coproduct combines the effects of two monads: computations from either monad can be
+included and sequenced in the coproduct using `do` notation. A pair of monad morphisms
+interpreting the component effects determines a unique interpretation of the combined effects.
+
+## References
+
+* [Jiří Adámek, Nathan Bowler, Paul B. Levy, Stefan Milius,
+  *Coproducts of Monads on Set*][Adamek2012]
+* [Christoph Lüth, Neil Ghani, *Composing Monads Using Coproducts*][LuthGhani2002]
+-/
+
+@[expose] public section
+
+namespace Cslib
+
+universe u v
+
+/-- 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 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 α
+  /-- The universal interpreter preserves monadic structure. -/
+  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)
+  /-- The universal interpreter extends the left morphism. -/
+  desc_inl {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) {α} (x : m α) :
+    desc hf hg (inl x) = f x
+  /-- The universal interpreter extends the right morphism. -/
+  desc_inr {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) {α} (x : n α) :
+    desc hf hg (inr x) = g x
+  /-- Any monad morphism extending `f` and `g` equals the universal interpreter. -/
+  desc_unique {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)
+    {k : ∀ {α}, p α → t α} (hk : IsMonadHom p t k)
+    (hinl : ∀ {α} (x : m α), k (inl x) = f x)
+    (hinr : ∀ {α} (x : n α), k (inr x) = g x) :
+    @k = (desc hf hg : ∀ {α}, p α → t α)
+
+namespace IsMonadCoproduct
+
+attribute [simp] desc_inl desc_inr
+
+variable {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 α}
+    (h : IsMonadCoproduct m n p inl inr)
+    {t : Type u → Type v} [Monad t] [LawfulMonad t]
+
+include h in
+/-- Monad morphisms out of a coproduct agree if they agree on both inclusions. -/
+theorem hom_ext {f g : ∀ {α}, p α → t α}
+    (hf : IsMonadHom p t f) (hg : IsMonadHom p t g)
+    (hinl : ∀ {α} (x : m α), f (inl x) = g (inl x))
+    (hinr : ∀ {α} (x : n α), f (inr x) = g (inr x)) : @f = @g :=
+  (h.desc_unique (hf.comp h.inl_hom) (hf.comp h.inr_hom) hf (fun _ => rfl) (fun _ => rfl)).trans
+    (h.desc_unique (hf.comp h.inl_hom) (hf.comp h.inr_hom) hg
+      (fun x => (hinl x).symm) (fun x => (hinr x).symm)).symm
+
+/-- Descending along the inclusions themselves gives the identity. -/
+theorem desc_inl_inr :
+    (h.desc h.inl_hom h.inr_hom : ∀ {α}, p α → p α) = fun {α} (x : p α) => x :=
+  (h.desc_unique h.inl_hom h.inr_hom (IsMonadHom.id p) (fun _ => rfl) (fun _ => rfl)).symm
+
+@[simp]
+theorem desc_inl_inr_apply {α} (x : p α) : h.desc h.inl_hom h.inr_hom x = x :=
+  congrArg (fun F : ∀ {α}, p α → p α => F x) h.desc_inl_inr
+
+/-- Replace the inclusions of a coproduct by equal maps. -/
+def copy {inl' : ∀ {α}, m α → p α} {inr' : ∀ {α}, n α → p α}
+    (hinl : @inl = @inl') (hinr : @inr = @inr') : IsMonadCoproduct m n p inl' inr' :=
+  hinl ▸ hinr ▸ h
+

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

thomaskwaring thomaskwaring commented on code on Nov 06, 2026 09:37 UTC
Code Review
Cslib/Foundations/Control/Monad/Coproducts.lean:1
@@ -0,0 +1,276 @@
+/-
+Copyright (c) 2026 Tanner Duve. All rights reserved.
+Released under Apache 2.0 license as described in the file LICENSE.
+Authors: Tanner Duve
+-/
+
+module
+
+public import Cslib.Foundations.Control.Monad.IsMonadHom
+
+/-!
+# Coproducts of monads
+
+This file defines the universal property of coproducts of lawful monads (`IsMonadCoproduct`)
+using `IsMonadHom`. The universal property is carried as data: `IsMonadCoproduct.desc`
+interprets the coproduct into any monad equipped with interpretations of both components,
+so that concrete coproducts (such as free monads on a sum of signatures) yield computable
+interpreters.
+
+A coproduct combines the effects of two monads: computations from either monad can be
+included and sequenced in the coproduct using `do` notation. A pair of monad morphisms
+interpreting the component effects determines a unique interpretation of the combined effects.
+
+## References
+
+* [Jiří Adámek, Nathan Bowler, Paul B. Levy, Stefan Milius,
+  *Coproducts of Monads on Set*][Adamek2012]
+* [Christoph Lüth, Neil Ghani, *Composing Monads Using Coproducts*][LuthGhani2002]
+-/
+
+@[expose] public section
+
+namespace Cslib
+
+universe u v
+
+/-- 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 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 α
+  /-- The universal interpreter preserves monadic structure. -/
+  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)
+  /-- The universal interpreter extends the left morphism. -/
+  desc_inl {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) {α} (x : m α) :
+    desc hf hg (inl x) = f x
+  /-- The universal interpreter extends the right morphism. -/
+  desc_inr {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) {α} (x : n α) :
+    desc hf hg (inr x) = g x
+  /-- Any monad morphism extending `f` and `g` equals the universal interpreter. -/
+  desc_unique {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)
+    {k : ∀ {α}, p α → t α} (hk : IsMonadHom p t k)
+    (hinl : ∀ {α} (x : m α), k (inl x) = f x)
+    (hinr : ∀ {α} (x : n α), k (inr x) = g x) :
+    @k = (desc hf hg : ∀ {α}, p α → t α)
+
+namespace IsMonadCoproduct
+
+attribute [simp] desc_inl desc_inr
+
+variable {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 α}
+    (h : IsMonadCoproduct m n p inl inr)
+    {t : Type u → Type v} [Monad t] [LawfulMonad t]
+
+include h in
+/-- Monad morphisms out of a coproduct agree if they agree on both inclusions. -/
+theorem hom_ext {f g : ∀ {α}, p α → t α}
+    (hf : IsMonadHom p t f) (hg : IsMonadHom p t g)
+    (hinl : ∀ {α} (x : m α), f (inl x) = g (inl x))
+    (hinr : ∀ {α} (x : n α), f (inr x) = g (inr x)) : @f = @g :=
+  (h.desc_unique (hf.comp h.inl_hom) (hf.comp h.inr_hom) hf (fun _ => rfl) (fun _ => rfl)).trans
+    (h.desc_unique (hf.comp h.inl_hom) (hf.comp h.inr_hom) hg
+      (fun x => (hinl x).symm) (fun x => (hinr x).symm)).symm
+
+/-- Descending along the inclusions themselves gives the identity. -/
+theorem desc_inl_inr :
+    (h.desc h.inl_hom h.inr_hom : ∀ {α}, p α → p α) = fun {α} (x : p α) => x :=
+  (h.desc_unique h.inl_hom h.inr_hom (IsMonadHom.id p) (fun _ => rfl) (fun _ => rfl)).symm
+
+@[simp]
+theorem desc_inl_inr_apply {α} (x : p α) : h.desc h.inl_hom h.inr_hom x = x :=
+  congrArg (fun F : ∀ {α}, p α → p α => F x) h.desc_inl_inr
+
+/-- Replace the inclusions of a coproduct by equal maps. -/
+def copy {inl' : ∀ {α}, m α → p α} {inr' : ∀ {α}, n α → p α}
+    (hinl : @inl = @inl') (hinr : @inr = @inr') : IsMonadCoproduct m n p inl' inr' :=
+  hinl ▸ hinr ▸ h
+
+/-- 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 := @fun _ _ _ _ _ hg hf => h.desc hf hg
+  isMonadHom_desc := @fun _ _ _ _ _ hg hf => h.isMonadHom_desc hf hg
+  desc_inl := @fun _ _ _ _ _ hg hf _ x => h.desc_inr hf hg x
+  desc_inr := @fun _ _ _ _ _ hg hf _ x => h.desc_inl hf hg x
+  desc_unique := @fun _ _ _ _ _ hg hf _ hk hinr hinl => h.desc_unique hf hg hk hinl hinr

/-- 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

thomaskwaring thomaskwaring commented on code on Nov 06, 2026 09:39 UTC
Code Review
Cslib/Foundations/Control/Monad/Coproducts.lean:1
@@ -0,0 +1,276 @@
+/-
+Copyright (c) 2026 Tanner Duve. All rights reserved.
+Released under Apache 2.0 license as described in the file LICENSE.
+Authors: Tanner Duve
+-/
+
+module
+
+public import Cslib.Foundations.Control.Monad.IsMonadHom
+
+/-!
+# Coproducts of monads
+
+This file defines the universal property of coproducts of lawful monads (`IsMonadCoproduct`)
+using `IsMonadHom`. The universal property is carried as data: `IsMonadCoproduct.desc`
+interprets the coproduct into any monad equipped with interpretations of both components,
+so that concrete coproducts (such as free monads on a sum of signatures) yield computable
+interpreters.
+
+A coproduct combines the effects of two monads: computations from either monad can be
+included and sequenced in the coproduct using `do` notation. A pair of monad morphisms
+interpreting the component effects determines a unique interpretation of the combined effects.
+
+## References
+
+* [Jiří Adámek, Nathan Bowler, Paul B. Levy, Stefan Milius,
+  *Coproducts of Monads on Set*][Adamek2012]
+* [Christoph Lüth, Neil Ghani, *Composing Monads Using Coproducts*][LuthGhani2002]
+-/
+
+@[expose] public section
+
+namespace Cslib
+
+universe u v
+
+/-- 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 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 α
+  /-- The universal interpreter preserves monadic structure. -/
+  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)
+  /-- The universal interpreter extends the left morphism. -/
+  desc_inl {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) {α} (x : m α) :
+    desc hf hg (inl x) = f x
+  /-- The universal interpreter extends the right morphism. -/
+  desc_inr {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) {α} (x : n α) :
+    desc hf hg (inr x) = g x
+  /-- Any monad morphism extending `f` and `g` equals the universal interpreter. -/
+  desc_unique {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)
+    {k : ∀ {α}, p α → t α} (hk : IsMonadHom p t k)
+    (hinl : ∀ {α} (x : m α), k (inl x) = f x)
+    (hinr : ∀ {α} (x : n α), k (inr x) = g x) :
+    @k = (desc hf hg : ∀ {α}, p α → t α)
+
+namespace IsMonadCoproduct
+
+attribute [simp] desc_inl desc_inr
+
+variable {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 α}
+    (h : IsMonadCoproduct m n p inl inr)
+    {t : Type u → Type v} [Monad t] [LawfulMonad t]
+
+include h in
+/-- Monad morphisms out of a coproduct agree if they agree on both inclusions. -/
+theorem hom_ext {f g : ∀ {α}, p α → t α}
+    (hf : IsMonadHom p t f) (hg : IsMonadHom p t g)
+    (hinl : ∀ {α} (x : m α), f (inl x) = g (inl x))
+    (hinr : ∀ {α} (x : n α), f (inr x) = g (inr x)) : @f = @g :=
+  (h.desc_unique (hf.comp h.inl_hom) (hf.comp h.inr_hom) hf (fun _ => rfl) (fun _ => rfl)).trans
+    (h.desc_unique (hf.comp h.inl_hom) (hf.comp h.inr_hom) hg
+      (fun x => (hinl x).symm) (fun x => (hinr x).symm)).symm
+
+/-- Descending along the inclusions themselves gives the identity. -/
+theorem desc_inl_inr :
+    (h.desc h.inl_hom h.inr_hom : ∀ {α}, p α → p α) = fun {α} (x : p α) => x :=
+  (h.desc_unique h.inl_hom h.inr_hom (IsMonadHom.id p) (fun _ => rfl) (fun _ => rfl)).symm
+
+@[simp]
+theorem desc_inl_inr_apply {α} (x : p α) : h.desc h.inl_hom h.inr_hom x = x :=
+  congrArg (fun F : ∀ {α}, p α → p α => F x) h.desc_inl_inr
+
+/-- Replace the inclusions of a coproduct by equal maps. -/
+def copy {inl' : ∀ {α}, m α → p α} {inr' : ∀ {α}, n α → p α}
+    (hinl : @inl = @inl') (hinr : @inr = @inr') : IsMonadCoproduct m n p inl' inr' :=
+  hinl ▸ hinr ▸ h
+
+/-- 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 := @fun _ _ _ _ _ hg hf => h.desc hf hg
+  isMonadHom_desc := @fun _ _ _ _ _ hg hf => h.isMonadHom_desc hf hg
+  desc_inl := @fun _ _ _ _ _ hg hf _ x => h.desc_inr hf hg x
+  desc_inr := @fun _ _ _ _ _ hg hf _ x => h.desc_inl hf hg x
+  desc_unique := @fun _ _ _ _ _ hg hf _ hk hinr hinl => h.desc_unique hf hg hk hinl hinr
+
+section Fusion
+
+variable {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)
+    {k : ∀ {α}, t α → t' α} (hk : IsMonadHom t t' k)
+
+/-- A monad morphism out of the target fuses with the universal interpreter. -/
+theorem comp_desc :
+    (k ∘ h.desc hf hg : ∀ {α}, p α → t' α) =
+      (h.desc (hk.comp hf) (hk.comp hg) : ∀ {α}, p α → t' α) :=
+  h.desc_unique (hk.comp hf) (hk.comp hg) (hk.comp (h.isMonadHom_desc hf hg))
+    (fun x => by simp only [Function.comp_apply, desc_inl])
+    (fun x => by simp only [Function.comp_apply, desc_inr])

  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 not

thomaskwaring thomaskwaring commented on code on Nov 06, 2026 09:44 UTC
Code Review
Cslib/Foundations/Control/Monad/Coproducts.lean:1
@@ -0,0 +1,276 @@
+/-
+Copyright (c) 2026 Tanner Duve. All rights reserved.
+Released under Apache 2.0 license as described in the file LICENSE.
+Authors: Tanner Duve
+-/
+
+module
+
+public import Cslib.Foundations.Control.Monad.IsMonadHom
+
+/-!
+# Coproducts of monads
+
+This file defines the universal property of coproducts of lawful monads (`IsMonadCoproduct`)
+using `IsMonadHom`. The universal property is carried as data: `IsMonadCoproduct.desc`
+interprets the coproduct into any monad equipped with interpretations of both components,
+so that concrete coproducts (such as free monads on a sum of signatures) yield computable
+interpreters.
+
+A coproduct combines the effects of two monads: computations from either monad can be
+included and sequenced in the coproduct using `do` notation. A pair of monad morphisms
+interpreting the component effects determines a unique interpretation of the combined effects.
+
+## References
+
+* [Jiří Adámek, Nathan Bowler, Paul B. Levy, Stefan Milius,
+  *Coproducts of Monads on Set*][Adamek2012]
+* [Christoph Lüth, Neil Ghani, *Composing Monads Using Coproducts*][LuthGhani2002]
+-/
+
+@[expose] public section
+
+namespace Cslib
+
+universe u v
+
+/-- 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 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 α
+  /-- The universal interpreter preserves monadic structure. -/
+  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)
+  /-- The universal interpreter extends the left morphism. -/
+  desc_inl {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) {α} (x : m α) :
+    desc hf hg (inl x) = f x
+  /-- The universal interpreter extends the right morphism. -/
+  desc_inr {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) {α} (x : n α) :
+    desc hf hg (inr x) = g x
+  /-- Any monad morphism extending `f` and `g` equals the universal interpreter. -/
+  desc_unique {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)
+    {k : ∀ {α}, p α → t α} (hk : IsMonadHom p t k)
+    (hinl : ∀ {α} (x : m α), k (inl x) = f x)
+    (hinr : ∀ {α} (x : n α), k (inr x) = g x) :
+    @k = (desc hf hg : ∀ {α}, p α → t α)
+
+namespace IsMonadCoproduct
+
+attribute [simp] desc_inl desc_inr
+
+variable {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 α}
+    (h : IsMonadCoproduct m n p inl inr)
+    {t : Type u → Type v} [Monad t] [LawfulMonad t]
+
+include h in
+/-- Monad morphisms out of a coproduct agree if they agree on both inclusions. -/
+theorem hom_ext {f g : ∀ {α}, p α → t α}
+    (hf : IsMonadHom p t f) (hg : IsMonadHom p t g)
+    (hinl : ∀ {α} (x : m α), f (inl x) = g (inl x))
+    (hinr : ∀ {α} (x : n α), f (inr x) = g (inr x)) : @f = @g :=
+  (h.desc_unique (hf.comp h.inl_hom) (hf.comp h.inr_hom) hf (fun _ => rfl) (fun _ => rfl)).trans
+    (h.desc_unique (hf.comp h.inl_hom) (hf.comp h.inr_hom) hg
+      (fun x => (hinl x).symm) (fun x => (hinr x).symm)).symm
+
+/-- Descending along the inclusions themselves gives the identity. -/
+theorem desc_inl_inr :
+    (h.desc h.inl_hom h.inr_hom : ∀ {α}, p α → p α) = fun {α} (x : p α) => x :=
+  (h.desc_unique h.inl_hom h.inr_hom (IsMonadHom.id p) (fun _ => rfl) (fun _ => rfl)).symm
+
+@[simp]
+theorem desc_inl_inr_apply {α} (x : p α) : h.desc h.inl_hom h.inr_hom x = x :=
+  congrArg (fun F : ∀ {α}, p α → p α => F x) h.desc_inl_inr

some 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

thomaskwaring thomaskwaring commented on code on Nov 06, 2026 10:08 UTC
Code Review
Cslib/Foundations/Control/Monad/Coproducts.lean:1
@@ -0,0 +1,276 @@
+/-
+Copyright (c) 2026 Tanner Duve. All rights reserved.
+Released under Apache 2.0 license as described in the file LICENSE.
+Authors: Tanner Duve
+-/
+
+module
+
+public import Cslib.Foundations.Control.Monad.IsMonadHom
+
+/-!
+# Coproducts of monads
+
+This file defines the universal property of coproducts of lawful monads (`IsMonadCoproduct`)
+using `IsMonadHom`. The universal property is carried as data: `IsMonadCoproduct.desc`
+interprets the coproduct into any monad equipped with interpretations of both components,
+so that concrete coproducts (such as free monads on a sum of signatures) yield computable
+interpreters.
+
+A coproduct combines the effects of two monads: computations from either monad can be
+included and sequenced in the coproduct using `do` notation. A pair of monad morphisms
+interpreting the component effects determines a unique interpretation of the combined effects.
+
+## References
+
+* [Jiří Adámek, Nathan Bowler, Paul B. Levy, Stefan Milius,
+  *Coproducts of Monads on Set*][Adamek2012]
+* [Christoph Lüth, Neil Ghani, *Composing Monads Using Coproducts*][LuthGhani2002]
+-/
+
+@[expose] public section
+
+namespace Cslib
+
+universe u v
+
+/-- 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 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 α
+  /-- The universal interpreter preserves monadic structure. -/
+  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)
+  /-- The universal interpreter extends the left morphism. -/
+  desc_inl {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) {α} (x : m α) :
+    desc hf hg (inl x) = f x
+  /-- The universal interpreter extends the right morphism. -/
+  desc_inr {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) {α} (x : n α) :
+    desc hf hg (inr x) = g x
+  /-- Any monad morphism extending `f` and `g` equals the universal interpreter. -/
+  desc_unique {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)
+    {k : ∀ {α}, p α → t α} (hk : IsMonadHom p t k)
+    (hinl : ∀ {α} (x : m α), k (inl x) = f x)
+    (hinr : ∀ {α} (x : n α), k (inr x) = g x) :
+    @k = (desc hf hg : ∀ {α}, p α → t α)
+
+namespace IsMonadCoproduct
+
+attribute [simp] desc_inl desc_inr
+
+variable {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 α}
+    (h : IsMonadCoproduct m n p inl inr)
+    {t : Type u → Type v} [Monad t] [LawfulMonad t]
+
+include h in
+/-- Monad morphisms out of a coproduct agree if they agree on both inclusions. -/
+theorem hom_ext {f g : ∀ {α}, p α → t α}
+    (hf : IsMonadHom p t f) (hg : IsMonadHom p t g)
+    (hinl : ∀ {α} (x : m α), f (inl x) = g (inl x))
+    (hinr : ∀ {α} (x : n α), f (inr x) = g (inr x)) : @f = @g :=
+  (h.desc_unique (hf.comp h.inl_hom) (hf.comp h.inr_hom) hf (fun _ => rfl) (fun _ => rfl)).trans
+    (h.desc_unique (hf.comp h.inl_hom) (hf.comp h.inr_hom) hg
+      (fun x => (hinl x).symm) (fun x => (hinr x).symm)).symm
+
+/-- Descending along the inclusions themselves gives the identity. -/
+theorem desc_inl_inr :
+    (h.desc h.inl_hom h.inr_hom : ∀ {α}, p α → p α) = fun {α} (x : p α) => x :=
+  (h.desc_unique h.inl_hom h.inr_hom (IsMonadHom.id p) (fun _ => rfl) (fun _ => rfl)).symm
+
+@[simp]
+theorem desc_inl_inr_apply {α} (x : p α) : h.desc h.inl_hom h.inr_hom x = x :=
+  congrArg (fun F : ∀ {α}, p α → p α => F x) h.desc_inl_inr
+
+/-- Replace the inclusions of a coproduct by equal maps. -/
+def copy {inl' : ∀ {α}, m α → p α} {inr' : ∀ {α}, n α → p α}
+    (hinl : @inl = @inl') (hinr : @inr = @inr') : IsMonadCoproduct m n p inl' inr' :=
+  hinl ▸ hinr ▸ h
+
+/-- 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 := @fun _ _ _ _ _ hg hf => h.desc hf hg
+  isMonadHom_desc := @fun _ _ _ _ _ hg hf => h.isMonadHom_desc hf hg
+  desc_inl := @fun _ _ _ _ _ hg hf _ x => h.desc_inr hf hg x
+  desc_inr := @fun _ _ _ _ _ hg hf _ x => h.desc_inl hf hg x
+  desc_unique := @fun _ _ _ _ _ hg hf _ hk hinr hinl => h.desc_unique hf hg hk hinl hinr
+
+section Fusion
+
+variable {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)
+    {k : ∀ {α}, t α → t' α} (hk : IsMonadHom t t' k)
+
+/-- A monad morphism out of the target fuses with the universal interpreter. -/
+theorem comp_desc :
+    (k ∘ h.desc hf hg : ∀ {α}, p α → t' α) =
+      (h.desc (hk.comp hf) (hk.comp hg) : ∀ {α}, p α → t' α) :=
+  h.desc_unique (hk.comp hf) (hk.comp hg) (hk.comp (h.isMonadHom_desc hf hg))
+    (fun x => by simp only [Function.comp_apply, desc_inl])
+    (fun x => by simp only [Function.comp_apply, desc_inr])
+
+theorem comp_desc_apply {α} (x : p α) :
+    k (h.desc hf hg x) = h.desc (hk.comp hf) (hk.comp hg) x :=
+  congrArg (fun F : ∀ {α}, p α → t' α => F x) (h.comp_desc hf hg hk)
+
+end Fusion
+
+section Map
+
+variable {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' α}
+    (h' : IsMonadCoproduct m' n' p' inl' inr')
+    {f : ∀ {α}, m α → m' α} {g : ∀ {α}, n α → n' α}
+    (hf : IsMonadHom m m' f) (hg : IsMonadHom n n' g)
+
+/-- The monad morphism between coproducts induced by morphisms of the components. -/
+def map : ∀ {α}, p α → p' α :=
+  h.desc (h'.inl_hom.comp hf) (h'.inr_hom.comp hg)
+
+/-- The induced morphism between coproducts preserves monadic structure. -/
+theorem isMonadHom_map : IsMonadHom p p' (h.map h' hf hg) :=
+  h.isMonadHom_desc _ _
+
+@[simp]
+theorem map_inl {α} (x : m α) : h.map h' hf hg (inl x) = inl' (f x) :=
+  h.desc_inl _ _ x
+
+@[simp]
+theorem map_inr {α} (x : n α) : h.map h' hf hg (inr x) = inr' (g x) :=
+  h.desc_inr _ _ x
+
+/-- The morphism induced by the identities is the identity. -/
+theorem map_id :
+    (h.map h (IsMonadHom.id m) (IsMonadHom.id n) : ∀ {α}, p α → p α) =
+      fun {α} (x : p α) => x :=
+  (h.desc_unique _ _ (IsMonadHom.id p) (fun _ => rfl) (fun _ => rfl)).symm
+
+@[simp]
+theorem map_id_apply {α} (x : p α) : h.map h (IsMonadHom.id m) (IsMonadHom.id n) x = x :=
+  congrArg (fun F : ∀ {α}, p α → p α => F x) h.map_id
+
+variable {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'' α}
+    (h'' : IsMonadCoproduct m'' n'' p'' inl'' inr'')
+    {f' : ∀ {α}, m' α → m'' α} {g' : ∀ {α}, n' α → n'' α}
+    (hf' : IsMonadHom m' m'' f') (hg' : IsMonadHom n' n'' g')
+
+/-- The morphism induced by composites is the composite of the induced morphisms. -/
+theorem map_comp :
+    (h'.map h'' hf' hg' ∘ h.map h' hf hg : ∀ {α}, p α → p'' α) =
+      (h.map h'' (hf'.comp hf) (hg'.comp hg) : ∀ {α}, p α → p'' α) :=
+  h.desc_unique (h''.inl_hom.comp (hf'.comp hf)) (h''.inr_hom.comp (hg'.comp hg))
+    ((h'.isMonadHom_map h'' hf' hg').comp (h.isMonadHom_map h' hf hg))
+    (fun x => by simp only [Function.comp_apply, map_inl])
+    (fun x => by simp only [Function.comp_apply, map_inr])
+
+theorem map_map {α} (x : p α) :
+    h'.map h'' hf' hg' (h.map h' hf hg x) = h.map h'' (hf'.comp hf) (hg'.comp hg) x :=
+  congrArg (fun F : ∀ {α}, p α → p'' α => F x) (h.map_comp h' hf hg h'' hf' hg')
+
+end Map
+
+section Assoc
+
+variable {o q r : Type u → Type v}
+    [Monad o] [LawfulMonad o]
+    [Monad q] [LawfulMonad q]
+    [Monad r] [LawfulMonad r]
+    {inl' : ∀ {α}, p α → q α} {inr' : ∀ {α}, o α → q α}
+    (hq : IsMonadCoproduct p o q inl' inr')
+    {jnl : ∀ {α}, n α → r α} {jnr : ∀ {α}, o α → r α}
+    (hr : IsMonadCoproduct n o r jnl jnr)
+
+/-- 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`. -/
+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 := @fun _ _ _ _ _ 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 := @fun _ _ _ _ _ _ _ _ x => by simp only [desc_inl]
+  desc_inr := @fun t _ _ _ _ hf hg _ x =>
+    congrArg (fun F : ∀ {α}, r α → t α => F x) <|
+      hr.hom_ext ((hq.isMonadHom_desc _ _).comp (hr.isMonadHom_desc _ _)) hg
+        (fun y => by simp only [Function.comp_apply, desc_inl, desc_inr])
+        (fun z => by simp only [Function.comp_apply, desc_inr])
+  desc_unique := @fun t _ _ _ _ hf hg _ hk hinl hinr =>
+    hq.desc_unique _ _ hk
+      (fun x => congrArg (fun F : ∀ {α}, p α → t α => F x) <|
+        h.hom_ext (hk.comp hq.inl_hom) (h.isMonadHom_desc hf (hg.comp hr.inl_hom))
+          (fun x => by
+            rw [desc_inl]
+            exact hinl x)
+          (fun y => by
+            rw [desc_inr]
+            have hy := hinr (jnl y)
+            rw [desc_inl] at hy
+            exact hy))
+      (fun z => by
+        have hz := hinr (jnr z)
+        rw [desc_inr] at hz
+        exact hz)

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)

thomaskwaring thomaskwaring requested changes on Nov 06, 2026 10:11 UTC
Changes Requested Reviewer

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

thomaskwaring thomaskwaring commented on code on Nov 06, 2026 21:55 UTC
Code Review
Cslib/Foundations/Control/Monad/Coproducts.lean:1
@@ -0,0 +1,331 @@
+/-
+Copyright (c) 2026 Tanner Duve. All rights reserved.
+Released under Apache 2.0 license as described in the file LICENSE.
+Authors: Tanner Duve
+-/
+
+module
+
+public import Cslib.Foundations.Control.Monad.IsMonadHom
+public import Mathlib.Logic.Equiv.Defs
+
+/-!
+# Coproducts of monads
+
+This file defines the universal property of coproducts of lawful monads (`IsMonadCoproduct`)
+using `IsMonadHom`. The universal property is carried as data: `IsMonadCoproduct.desc`
+interprets the coproduct into any monad equipped with interpretations of both components,
+so that concrete coproducts (such as free monads on a sum of signatures) yield computable
+interpreters.
+
+A coproduct combines the effects of two monads: computations from either monad can be
+included and sequenced in the coproduct using `do` notation. A pair of monad morphisms
+interpreting the component effects determines a unique interpretation of the combined effects.
+
+## References
+
+* [Jiří Adámek, Nathan Bowler, Paul B. Levy, Stefan Milius,
+  *Coproducts of Monads on Set*][Adamek2012]
+* [Christoph Lüth, Neil Ghani, *Composing Monads Using Coproducts*][LuthGhani2002]
+-/
+
+@[expose] public section
+
+namespace Cslib
+
+universe u v
+
+/-- 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 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 α
+  /-- The universal interpreter preserves monadic structure. -/
+  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)
+  /-- The universal interpreter extends the left morphism. -/
+  desc_inl {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) {α} (x : m α) :
+    desc hf hg (inl x) = f x
+  /-- The universal interpreter extends the right morphism. -/
+  desc_inr {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) {α} (x : n α) :
+    desc hf hg (inr x) = g x
+  /-- Any monad morphism extending `f` and `g` equals the universal interpreter. -/
+  desc_unique {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)
+    {k : ∀ {α}, p α → t α} (hk : IsMonadHom p t k)
+    (hinl : ∀ {α} (x : m α), k (inl x) = f x)
+    (hinr : ∀ {α} (x : n α), k (inr x) = g x) :
+    @k = (desc hf hg : ∀ {α}, p α → t α)
+
+namespace IsMonadCoproduct
+
+attribute [simp] desc_inl desc_inr
+
+variable {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 α}
+    (h : IsMonadCoproduct m n p inl inr)
+    {t : Type u → Type v} [Monad t] [LawfulMonad t]
+
+include h in
+/-- Monad morphisms out of a coproduct agree if they agree on both inclusions. -/
+theorem hom_ext {f g : ∀ {α}, p α → t α}
+    (hf : IsMonadHom p t f) (hg : IsMonadHom p t g)
+    (hinl : ∀ {α} (x : m α), f (inl x) = g (inl x))
+    (hinr : ∀ {α} (x : n α), f (inr x) = g (inr x)) : @f = @g :=
+  (h.desc_unique (hf.comp h.inl_hom) (hf.comp h.inr_hom) hf (fun _ => rfl) (fun _ => rfl)).trans
+    (h.desc_unique (hf.comp h.inl_hom) (hf.comp h.inr_hom) hg
+      (fun x => (hinl x).symm) (fun x => (hinr x).symm)).symm
+
+instance : Subsingleton (IsMonadCoproduct m n p inl inr) where
+  allEq h h' := by
+    have hd : @h.desc = @h'.desc := by
+      funext t _ _ f g hf hg
+      exact h'.desc_unique hf hg (h.isMonadHom_desc hf hg) (h.desc_inl hf hg) (h.desc_inr hf hg)
+    cases h
+    cases h'
+    cases hd
+    rfl
+
+/-- Descending along the inclusions themselves gives the identity. -/
+theorem desc_inl_inr :
+    (h.desc h.inl_hom h.inr_hom : ∀ {α}, p α → p α) = fun {α} (x : p α) => x :=
+  (h.desc_unique h.inl_hom h.inr_hom (IsMonadHom.id p) (fun _ => rfl) (fun _ => rfl)).symm
+
+@[simp]
+theorem desc_inl_inr_apply {α} (x : p α) : h.desc h.inl_hom h.inr_hom x = x :=
+  congr($h.desc_inl_inr x)
+
+/-- Replace the inclusions of a coproduct by equal maps. -/
+def copy {inl' : ∀ {α}, m α → p α} {inr' : ∀ {α}, n α → p α}
+    (hinl : @inl = @inl') (hinr : @inr = @inr') : IsMonadCoproduct m n p inl' inr' :=
+  hinl ▸ hinr ▸ h
+
+/-- Changing the inclusions by equality leaves the universal interpreter unchanged. -/
+@[simp]
+theorem 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
+
+/-- A coproduct of `m` and `n` is also a coproduct of `n` and `m`. -/
+@[simps]
+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
+
+section Fusion
+
+variable {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)
+    {k : ∀ {α}, t α → t' α} (hk : IsMonadHom t t' k)
+
+/-- A monad morphism out of the target fuses with the universal interpreter. -/
+theorem comp_desc :
+    (k ∘ h.desc hf hg : ∀ {α}, p α → t' α) =
+      (h.desc (hk.comp hf) (hk.comp hg) : ∀ {α}, p α → t' α) :=
+  h.desc_unique (hk.comp hf) (hk.comp hg) (hk.comp (h.isMonadHom_desc hf hg)) (by simp) (by simp)
+
+theorem comp_desc_apply {α} (x : p α) :
+    k (h.desc hf hg x) = h.desc (hk.comp hf) (hk.comp hg) x :=
+  congr($(h.comp_desc hf hg hk) x)
+
+end Fusion
+
+section Map
+
+variable {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' α}
+    (h' : IsMonadCoproduct m' n' p' inl' inr')
+    {f : ∀ {α}, m α → m' α} {g : ∀ {α}, n α → n' α}
+    (hf : IsMonadHom m m' f) (hg : IsMonadHom n n' g)
+
+/-- The monad morphism between coproducts induced by morphisms of the components. -/
+def map : ∀ {α}, p α → p' α :=
+  h.desc (h'.inl_hom.comp hf) (h'.inr_hom.comp hg)
+
+/-- The induced morphism between coproducts preserves monadic structure. -/
+theorem isMonadHom_map : IsMonadHom p p' (h.map h' hf hg) :=
+  h.isMonadHom_desc _ _
+
+@[simp]
+theorem map_inl {α} (x : m α) : h.map h' hf hg (inl x) = inl' (f x) :=
+  h.desc_inl _ _ x
+
+@[simp]
+theorem map_inr {α} (x : n α) : h.map h' hf hg (inr x) = inr' (g x) :=
+  h.desc_inr _ _ x
+
+/-- The morphism induced by the identities is the identity. -/
+theorem map_id :
+    (h.map h (IsMonadHom.id m) (IsMonadHom.id n) : ∀ {α}, p α → p α) =
+      fun {α} (x : p α) => x :=
+  (h.desc_unique _ _ (IsMonadHom.id p) (fun _ => rfl) (fun _ => rfl)).symm
+
+@[simp]
+theorem map_id_apply {α} (x : p α) : h.map h (IsMonadHom.id m) (IsMonadHom.id n) x = x :=
+  congr($h.map_id x)
+
+variable {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'' α}
+    (h'' : IsMonadCoproduct m'' n'' p'' inl'' inr'')
+    {f' : ∀ {α}, m' α → m'' α} {g' : ∀ {α}, n' α → n'' α}
+    (hf' : IsMonadHom m' m'' f') (hg' : IsMonadHom n' n'' g')
+
+/-- The morphism induced by composites is the composite of the induced morphisms. -/
+theorem map_comp :
+    (h'.map h'' hf' hg' ∘ h.map h' hf hg : ∀ {α}, p α → p'' α) =
+      (h.map h'' (hf'.comp hf) (hg'.comp hg) : ∀ {α}, p α → p'' α) :=
+  h.desc_unique (h''.inl_hom.comp (hf'.comp hf)) (h''.inr_hom.comp (hg'.comp hg))
+    ((h'.isMonadHom_map h'' hf' hg').comp (h.isMonadHom_map h' hf hg))
+    (by simp) (by simp)
+
+theorem map_map {α} (x : p α) :
+    h'.map h'' hf' hg' (h.map h' hf hg x) = h.map h'' (hf'.comp hf) (hg'.comp hg) x :=
+  congr($(h.map_comp h' hf hg h'' hf' hg') x)
+
+end Map
+
+section Uniqueness
+
+variable {q : Type u → Type v} [Monad q] [LawfulMonad q]
+    {inl' : ∀ {α}, m α → q α} {inr' : ∀ {α}, n α → q α}
+    (h' : IsMonadCoproduct m n q inl' inr')
+
+/-- Two coproducts of the same monads are canonically equivalent. -/
+@[simps]
+def equiv (α : Type u) : p α ≃ q α where
+  toFun := h.desc h'.inl_hom h'.inr_hom
+  invFun := h'.desc h.inl_hom h.inr_hom
+  left_inv x := by
+    have hh := h.hom_ext
+      ((h'.isMonadHom_desc h.inl_hom h.inr_hom).comp (h.isMonadHom_desc h'.inl_hom h'.inr_hom))
+      (IsMonadHom.id p) (by simp) (by simp)
+    exact congr($hh α x)

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

thomaskwaring thomaskwaring commented on code on Nov 06, 2026 21:58 UTC
Code Review
Cslib/Foundations/Control/Monad/Coproducts.lean:1
@@ -0,0 +1,331 @@
+/-
+Copyright (c) 2026 Tanner Duve. All rights reserved.
+Released under Apache 2.0 license as described in the file LICENSE.
+Authors: Tanner Duve
+-/
+
+module
+
+public import Cslib.Foundations.Control.Monad.IsMonadHom
+public import Mathlib.Logic.Equiv.Defs
+
+/-!
+# Coproducts of monads
+
+This file defines the universal property of coproducts of lawful monads (`IsMonadCoproduct`)
+using `IsMonadHom`. The universal property is carried as data: `IsMonadCoproduct.desc`
+interprets the coproduct into any monad equipped with interpretations of both components,
+so that concrete coproducts (such as free monads on a sum of signatures) yield computable
+interpreters.
+
+A coproduct combines the effects of two monads: computations from either monad can be
+included and sequenced in the coproduct using `do` notation. A pair of monad morphisms
+interpreting the component effects determines a unique interpretation of the combined effects.
+
+## References
+
+* [Jiří Adámek, Nathan Bowler, Paul B. Levy, Stefan Milius,
+  *Coproducts of Monads on Set*][Adamek2012]
+* [Christoph Lüth, Neil Ghani, *Composing Monads Using Coproducts*][LuthGhani2002]
+-/
+
+@[expose] public section
+
+namespace Cslib
+
+universe u v
+
+/-- 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 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 α
+  /-- The universal interpreter preserves monadic structure. -/
+  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)
+  /-- The universal interpreter extends the left morphism. -/
+  desc_inl {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) {α} (x : m α) :
+    desc hf hg (inl x) = f x
+  /-- The universal interpreter extends the right morphism. -/
+  desc_inr {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) {α} (x : n α) :
+    desc hf hg (inr x) = g x
+  /-- Any monad morphism extending `f` and `g` equals the universal interpreter. -/
+  desc_unique {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)
+    {k : ∀ {α}, p α → t α} (hk : IsMonadHom p t k)
+    (hinl : ∀ {α} (x : m α), k (inl x) = f x)
+    (hinr : ∀ {α} (x : n α), k (inr x) = g x) :
+    @k = (desc hf hg : ∀ {α}, p α → t α)
+
+namespace IsMonadCoproduct
+
+attribute [simp] desc_inl desc_inr
+
+variable {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 α}
+    (h : IsMonadCoproduct m n p inl inr)
+    {t : Type u → Type v} [Monad t] [LawfulMonad t]
+
+include h in
+/-- Monad morphisms out of a coproduct agree if they agree on both inclusions. -/
+theorem hom_ext {f g : ∀ {α}, p α → t α}
+    (hf : IsMonadHom p t f) (hg : IsMonadHom p t g)
+    (hinl : ∀ {α} (x : m α), f (inl x) = g (inl x))
+    (hinr : ∀ {α} (x : n α), f (inr x) = g (inr x)) : @f = @g :=
+  (h.desc_unique (hf.comp h.inl_hom) (hf.comp h.inr_hom) hf (fun _ => rfl) (fun _ => rfl)).trans
+    (h.desc_unique (hf.comp h.inl_hom) (hf.comp h.inr_hom) hg
+      (fun x => (hinl x).symm) (fun x => (hinr x).symm)).symm
+
+instance : Subsingleton (IsMonadCoproduct m n p inl inr) where
+  allEq h h' := by
+    have hd : @h.desc = @h'.desc := by
+      funext t _ _ f g hf hg
+      exact h'.desc_unique hf hg (h.isMonadHom_desc hf hg) (h.desc_inl hf hg) (h.desc_inr hf hg)
+    cases h
+    cases h'
+    cases hd
+    rfl
+
+/-- Descending along the inclusions themselves gives the identity. -/
+theorem desc_inl_inr :
+    (h.desc h.inl_hom h.inr_hom : ∀ {α}, p α → p α) = fun {α} (x : p α) => x :=
+  (h.desc_unique h.inl_hom h.inr_hom (IsMonadHom.id p) (fun _ => rfl) (fun _ => rfl)).symm
+
+@[simp]
+theorem desc_inl_inr_apply {α} (x : p α) : h.desc h.inl_hom h.inr_hom x = x :=
+  congr($h.desc_inl_inr x)
+
+/-- Replace the inclusions of a coproduct by equal maps. -/
+def copy {inl' : ∀ {α}, m α → p α} {inr' : ∀ {α}, n α → p α}
+    (hinl : @inl = @inl') (hinr : @inr = @inr') : IsMonadCoproduct m n p inl' inr' :=
+  hinl ▸ hinr ▸ h
+
+/-- Changing the inclusions by equality leaves the universal interpreter unchanged. -/
+@[simp]
+theorem 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
+
+/-- A coproduct of `m` and `n` is also a coproduct of `n` and `m`. -/
+@[simps]
+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
+
+section Fusion
+
+variable {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)
+    {k : ∀ {α}, t α → t' α} (hk : IsMonadHom t t' k)
+
+/-- A monad morphism out of the target fuses with the universal interpreter. -/
+theorem comp_desc :
+    (k ∘ h.desc hf hg : ∀ {α}, p α → t' α) =
+      (h.desc (hk.comp hf) (hk.comp hg) : ∀ {α}, p α → t' α) :=
+  h.desc_unique (hk.comp hf) (hk.comp hg) (hk.comp (h.isMonadHom_desc hf hg)) (by simp) (by simp)
+
+theorem comp_desc_apply {α} (x : p α) :
+    k (h.desc hf hg x) = h.desc (hk.comp hf) (hk.comp hg) x :=
+  congr($(h.comp_desc hf hg hk) x)
+
+end Fusion
+
+section Map
+
+variable {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' α}
+    (h' : IsMonadCoproduct m' n' p' inl' inr')
+    {f : ∀ {α}, m α → m' α} {g : ∀ {α}, n α → n' α}
+    (hf : IsMonadHom m m' f) (hg : IsMonadHom n n' g)
+
+/-- The monad morphism between coproducts induced by morphisms of the components. -/
+def map : ∀ {α}, p α → p' α :=
+  h.desc (h'.inl_hom.comp hf) (h'.inr_hom.comp hg)
+
+/-- The induced morphism between coproducts preserves monadic structure. -/
+theorem isMonadHom_map : IsMonadHom p p' (h.map h' hf hg) :=
+  h.isMonadHom_desc _ _
+
+@[simp]
+theorem map_inl {α} (x : m α) : h.map h' hf hg (inl x) = inl' (f x) :=
+  h.desc_inl _ _ x
+
+@[simp]
+theorem map_inr {α} (x : n α) : h.map h' hf hg (inr x) = inr' (g x) :=
+  h.desc_inr _ _ x
+
+/-- The morphism induced by the identities is the identity. -/
+theorem map_id :
+    (h.map h (IsMonadHom.id m) (IsMonadHom.id n) : ∀ {α}, p α → p α) =
+      fun {α} (x : p α) => x :=
+  (h.desc_unique _ _ (IsMonadHom.id p) (fun _ => rfl) (fun _ => rfl)).symm
+
+@[simp]
+theorem map_id_apply {α} (x : p α) : h.map h (IsMonadHom.id m) (IsMonadHom.id n) x = x :=
+  congr($h.map_id x)
+
+variable {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'' α}
+    (h'' : IsMonadCoproduct m'' n'' p'' inl'' inr'')
+    {f' : ∀ {α}, m' α → m'' α} {g' : ∀ {α}, n' α → n'' α}
+    (hf' : IsMonadHom m' m'' f') (hg' : IsMonadHom n' n'' g')
+
+/-- The morphism induced by composites is the composite of the induced morphisms. -/
+theorem map_comp :
+    (h'.map h'' hf' hg' ∘ h.map h' hf hg : ∀ {α}, p α → p'' α) =
+      (h.map h'' (hf'.comp hf) (hg'.comp hg) : ∀ {α}, p α → p'' α) :=
+  h.desc_unique (h''.inl_hom.comp (hf'.comp hf)) (h''.inr_hom.comp (hg'.comp hg))
+    ((h'.isMonadHom_map h'' hf' hg').comp (h.isMonadHom_map h' hf hg))
+    (by simp) (by simp)
+
+theorem map_map {α} (x : p α) :
+    h'.map h'' hf' hg' (h.map h' hf hg x) = h.map h'' (hf'.comp hf) (hg'.comp hg) x :=
+  congr($(h.map_comp h' hf hg h'' hf' hg') x)
+
+end Map
+
+section Uniqueness
+
+variable {q : Type u → Type v} [Monad q] [LawfulMonad q]
+    {inl' : ∀ {α}, m α → q α} {inr' : ∀ {α}, n α → q α}
+    (h' : IsMonadCoproduct m n q inl' inr')
+
+/-- Two coproducts of the same monads are canonically equivalent. -/
+@[simps]
+def equiv (α : Type u) : p α ≃ q α where
+  toFun := h.desc h'.inl_hom h'.inr_hom
+  invFun := h'.desc h.inl_hom h.inr_hom
+  left_inv x := by
+    have hh := h.hom_ext
+      ((h'.isMonadHom_desc h.inl_hom h.inr_hom).comp (h.isMonadHom_desc h'.inl_hom h'.inr_hom))
+      (IsMonadHom.id p) (by simp) (by simp)
+    exact congr($hh α x)
+  right_inv x := by
+    have hh := h'.hom_ext
+      ((h.isMonadHom_desc h'.inl_hom h'.inr_hom).comp (h'.isMonadHom_desc h.inl_hom h.inr_hom))
+      (IsMonadHom.id q) (by simp) (by simp)
+    exact congr($hh α x)
+
+/-- The canonical equivalence between coproducts preserves monadic structure. -/
+theorem isMonadHom_equiv : IsMonadHom p q (fun {α} => h.equiv h' α) :=
+  h.isMonadHom_desc _ _
+
+/-- The inverse canonical equivalence preserves monadic structure. -/
+theorem isMonadHom_equiv_symm : IsMonadHom q p (fun {α} => (h.equiv h' α).symm) :=
+  h'.isMonadHom_desc _ _
+
+theorem equiv_inl {α} (x : m α) : h.equiv h' α (inl x) = inl' x := by simp
+
+theorem equiv_inr {α} (x : n α) : h.equiv h' α (inr x) = inr' x := by simp
+
+/-- The canonical equivalence is the unique monad isomorphism preserving both inclusions. -/
+theorem equiv_unique {e : ∀ α, p α ≃ q α}
+    (he : IsMonadHom p q (fun {α} => e α))
+    (hinl : ∀ {α} (x : m α), e α (inl x) = inl' x)
+    (hinr : ∀ {α} (x : n α), e α (inr x) = inr' x) : e = h.equiv h' := by
+  funext α
+  apply Equiv.ext
+  intro x
+  exact congr($(h.desc_unique h'.inl_hom h'.inr_hom he hinl hinr) 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

thomaskwaring thomaskwaring commented on code on Nov 06, 2026 21:59 UTC
Code Review
Cslib/Foundations/Control/Monad/Coproducts.lean:1
@@ -0,0 +1,331 @@
+/-
+Copyright (c) 2026 Tanner Duve. All rights reserved.
+Released under Apache 2.0 license as described in the file LICENSE.
+Authors: Tanner Duve
+-/
+
+module
+
+public import Cslib.Foundations.Control.Monad.IsMonadHom
+public import Mathlib.Logic.Equiv.Defs
+
+/-!
+# Coproducts of monads
+
+This file defines the universal property of coproducts of lawful monads (`IsMonadCoproduct`)
+using `IsMonadHom`. The universal property is carried as data: `IsMonadCoproduct.desc`
+interprets the coproduct into any monad equipped with interpretations of both components,
+so that concrete coproducts (such as free monads on a sum of signatures) yield computable
+interpreters.
+
+A coproduct combines the effects of two monads: computations from either monad can be
+included and sequenced in the coproduct using `do` notation. A pair of monad morphisms
+interpreting the component effects determines a unique interpretation of the combined effects.
+
+## References
+
+* [Jiří Adámek, Nathan Bowler, Paul B. Levy, Stefan Milius,
+  *Coproducts of Monads on Set*][Adamek2012]
+* [Christoph Lüth, Neil Ghani, *Composing Monads Using Coproducts*][LuthGhani2002]
+-/
+
+@[expose] public section
+
+namespace Cslib
+
+universe u v
+
+/-- 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 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 α
+  /-- The universal interpreter preserves monadic structure. -/
+  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)
+  /-- The universal interpreter extends the left morphism. -/
+  desc_inl {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) {α} (x : m α) :
+    desc hf hg (inl x) = f x
+  /-- The universal interpreter extends the right morphism. -/
+  desc_inr {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) {α} (x : n α) :
+    desc hf hg (inr x) = g x
+  /-- Any monad morphism extending `f` and `g` equals the universal interpreter. -/
+  desc_unique {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)
+    {k : ∀ {α}, p α → t α} (hk : IsMonadHom p t k)
+    (hinl : ∀ {α} (x : m α), k (inl x) = f x)
+    (hinr : ∀ {α} (x : n α), k (inr x) = g x) :
+    @k = (desc hf hg : ∀ {α}, p α → t α)
+
+namespace IsMonadCoproduct
+
+attribute [simp] desc_inl desc_inr
+
+variable {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 α}
+    (h : IsMonadCoproduct m n p inl inr)
+    {t : Type u → Type v} [Monad t] [LawfulMonad t]
+
+include h in
+/-- Monad morphisms out of a coproduct agree if they agree on both inclusions. -/
+theorem hom_ext {f g : ∀ {α}, p α → t α}
+    (hf : IsMonadHom p t f) (hg : IsMonadHom p t g)
+    (hinl : ∀ {α} (x : m α), f (inl x) = g (inl x))
+    (hinr : ∀ {α} (x : n α), f (inr x) = g (inr x)) : @f = @g :=
+  (h.desc_unique (hf.comp h.inl_hom) (hf.comp h.inr_hom) hf (fun _ => rfl) (fun _ => rfl)).trans
+    (h.desc_unique (hf.comp h.inl_hom) (hf.comp h.inr_hom) hg
+      (fun x => (hinl x).symm) (fun x => (hinr x).symm)).symm
+
+instance : Subsingleton (IsMonadCoproduct m n p inl inr) where
+  allEq h h' := by
+    have hd : @h.desc = @h'.desc := by
+      funext t _ _ f g hf hg
+      exact h'.desc_unique hf hg (h.isMonadHom_desc hf hg) (h.desc_inl hf hg) (h.desc_inr hf hg)
+    cases h
+    cases h'
+    cases hd
+    rfl
+
+/-- Descending along the inclusions themselves gives the identity. -/
+theorem desc_inl_inr :
+    (h.desc h.inl_hom h.inr_hom : ∀ {α}, p α → p α) = fun {α} (x : p α) => x :=
+  (h.desc_unique h.inl_hom h.inr_hom (IsMonadHom.id p) (fun _ => rfl) (fun _ => rfl)).symm
+
+@[simp]
+theorem desc_inl_inr_apply {α} (x : p α) : h.desc h.inl_hom h.inr_hom x = x :=
+  congr($h.desc_inl_inr x)
+
+/-- Replace the inclusions of a coproduct by equal maps. -/
+def copy {inl' : ∀ {α}, m α → p α} {inr' : ∀ {α}, n α → p α}
+    (hinl : @inl = @inl') (hinr : @inr = @inr') : IsMonadCoproduct m n p inl' inr' :=
+  hinl ▸ hinr ▸ h
+
+/-- Changing the inclusions by equality leaves the universal interpreter unchanged. -/
+@[simp]
+theorem 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
+
+/-- A coproduct of `m` and `n` is also a coproduct of `n` and `m`. -/
+@[simps]
+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
+
+section Fusion
+
+variable {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)
+    {k : ∀ {α}, t α → t' α} (hk : IsMonadHom t t' k)
+
+/-- A monad morphism out of the target fuses with the universal interpreter. -/
+theorem comp_desc :
+    (k ∘ h.desc hf hg : ∀ {α}, p α → t' α) =
+      (h.desc (hk.comp hf) (hk.comp hg) : ∀ {α}, p α → t' α) :=
+  h.desc_unique (hk.comp hf) (hk.comp hg) (hk.comp (h.isMonadHom_desc hf hg)) (by simp) (by simp)
+
+theorem comp_desc_apply {α} (x : p α) :
+    k (h.desc hf hg x) = h.desc (hk.comp hf) (hk.comp hg) x :=
+  congr($(h.comp_desc hf hg hk) x)
+
+end Fusion
+
+section Map
+
+variable {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' α}
+    (h' : IsMonadCoproduct m' n' p' inl' inr')
+    {f : ∀ {α}, m α → m' α} {g : ∀ {α}, n α → n' α}
+    (hf : IsMonadHom m m' f) (hg : IsMonadHom n n' g)
+
+/-- The monad morphism between coproducts induced by morphisms of the components. -/
+def map : ∀ {α}, p α → p' α :=
+  h.desc (h'.inl_hom.comp hf) (h'.inr_hom.comp hg)
+
+/-- The induced morphism between coproducts preserves monadic structure. -/
+theorem isMonadHom_map : IsMonadHom p p' (h.map h' hf hg) :=
+  h.isMonadHom_desc _ _
+
+@[simp]
+theorem map_inl {α} (x : m α) : h.map h' hf hg (inl x) = inl' (f x) :=
+  h.desc_inl _ _ x
+
+@[simp]
+theorem map_inr {α} (x : n α) : h.map h' hf hg (inr x) = inr' (g x) :=
+  h.desc_inr _ _ x
+
+/-- The morphism induced by the identities is the identity. -/
+theorem map_id :
+    (h.map h (IsMonadHom.id m) (IsMonadHom.id n) : ∀ {α}, p α → p α) =
+      fun {α} (x : p α) => x :=
+  (h.desc_unique _ _ (IsMonadHom.id p) (fun _ => rfl) (fun _ => rfl)).symm
+
+@[simp]
+theorem map_id_apply {α} (x : p α) : h.map h (IsMonadHom.id m) (IsMonadHom.id n) x = x :=
+  congr($h.map_id x)
+
+variable {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'' α}
+    (h'' : IsMonadCoproduct m'' n'' p'' inl'' inr'')
+    {f' : ∀ {α}, m' α → m'' α} {g' : ∀ {α}, n' α → n'' α}
+    (hf' : IsMonadHom m' m'' f') (hg' : IsMonadHom n' n'' g')
+
+/-- The morphism induced by composites is the composite of the induced morphisms. -/
+theorem map_comp :
+    (h'.map h'' hf' hg' ∘ h.map h' hf hg : ∀ {α}, p α → p'' α) =
+      (h.map h'' (hf'.comp hf) (hg'.comp hg) : ∀ {α}, p α → p'' α) :=
+  h.desc_unique (h''.inl_hom.comp (hf'.comp hf)) (h''.inr_hom.comp (hg'.comp hg))
+    ((h'.isMonadHom_map h'' hf' hg').comp (h.isMonadHom_map h' hf hg))
+    (by simp) (by simp)
+
+theorem map_map {α} (x : p α) :
+    h'.map h'' hf' hg' (h.map h' hf hg x) = h.map h'' (hf'.comp hf) (hg'.comp hg) x :=
+  congr($(h.map_comp h' hf hg h'' hf' hg') x)
+
+end Map
+
+section Uniqueness
+
+variable {q : Type u → Type v} [Monad q] [LawfulMonad q]
+    {inl' : ∀ {α}, m α → q α} {inr' : ∀ {α}, n α → q α}
+    (h' : IsMonadCoproduct m n q inl' inr')
+
+/-- Two coproducts of the same monads are canonically equivalent. -/
+@[simps]
+def equiv (α : Type u) : p α ≃ q α where
+  toFun := h.desc h'.inl_hom h'.inr_hom
+  invFun := h'.desc h.inl_hom h.inr_hom
+  left_inv x := by
+    have hh := h.hom_ext
+      ((h'.isMonadHom_desc h.inl_hom h.inr_hom).comp (h.isMonadHom_desc h'.inl_hom h'.inr_hom))
+      (IsMonadHom.id p) (by simp) (by simp)
+    exact congr($hh α x)
+  right_inv x := by
+    have hh := h'.hom_ext
+      ((h.isMonadHom_desc h'.inl_hom h'.inr_hom).comp (h'.isMonadHom_desc h.inl_hom h.inr_hom))
+      (IsMonadHom.id q) (by simp) (by simp)
+    exact congr($hh α x)
+
+/-- The canonical equivalence between coproducts preserves monadic structure. -/
+theorem isMonadHom_equiv : IsMonadHom p q (fun {α} => h.equiv h' α) :=
+  h.isMonadHom_desc _ _
+
+/-- The inverse canonical equivalence preserves monadic structure. -/
+theorem isMonadHom_equiv_symm : IsMonadHom q p (fun {α} => (h.equiv h' α).symm) :=
+  h'.isMonadHom_desc _ _
+
+theorem equiv_inl {α} (x : m α) : h.equiv h' α (inl x) = inl' x := by simp
+
+theorem equiv_inr {α} (x : n α) : h.equiv h' α (inr x) = inr' x := by simp
+
+/-- The canonical equivalence is the unique monad isomorphism preserving both inclusions. -/
+theorem equiv_unique {e : ∀ α, p α ≃ q α}
+    (he : IsMonadHom p q (fun {α} => e α))
+    (hinl : ∀ {α} (x : m α), e α (inl x) = inl' x)
+    (hinr : ∀ {α} (x : n α), e α (inr x) = inr' x) : e = h.equiv h' := by
+  funext α
+  apply Equiv.ext
+  intro x
+  exact congr($(h.desc_unique h'.inl_hom h'.inr_hom he hinl hinr) x)
+
+end Uniqueness
+
+section Assoc
+
+variable {o q r : Type u → Type v}
+    [Monad o] [LawfulMonad o]
+    [Monad q] [LawfulMonad q]
+    [Monad r] [LawfulMonad r]
+    {inl' : ∀ {α}, p α → q α} {inr' : ∀ {α}, o α → q α}
+    (hq : IsMonadCoproduct p o q inl' inr')
+    {jnl : ∀ {α}, n α → r α} {jnr : ∀ {α}, o α → r α}
+    (hr : IsMonadCoproduct n o r jnl jnr)
+
+/-- 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)
+
+end Assoc
+
+section Unit
+
+variable (m : Type u → Type u) [Monad m] [LawfulMonad m]
+
+/-- Every monad is a coproduct of itself and the identity monad. -/
+@[simps]
+def unitRight : IsMonadCoproduct m Id m id pure where
+  inl_hom := IsMonadHom.id m
+  inr_hom := IsMonadHom.pure m
+  desc := @fun _ _ _ f _ _ _ => f
+  isMonadHom_desc hf _ := hf
+  desc_inl _ _ _ _ := rfl
+  desc_inr hf hg _ x := (hf.map_pure x).trans (hg.eq_pure x).symm
+  desc_unique _ _ _ _ hinl _ := by
+    funext α x
+    exact hinl x
+
+/-- Every monad is a coproduct of the identity monad and itself. -/
+def unitLeft : IsMonadCoproduct Id m m pure id :=

@[simps!]
def unitLeft : IsMonadCoproduct Id m m pure id :=

thomaskwaring thomaskwaring commented on code on Nov 06, 2026 22:00 UTC
Code Review
Cslib/Foundations/Control/Monad/IsMonadHom.lean:1
@@ -214,6 +214,16 @@ protected theorem comp {f : ∀ {α}, n α → p α} {g : ∀ {α}, m α → n 
   toIsApplicativeHom := hf.toIsApplicativeHom.comp hg.toIsApplicativeHom
   map_bind _ _ := by simp [hf.map_bind, hg.map_bind]
 
+variable (m) in
+/-- `pure` is a morphism of monads out of the identity monad. -/
+protected theorem pure [LawfulMonad m] : IsMonadHom Id m pure :=
+  .mk' (fun _ => rfl) (fun x y => (pure_bind x (fun a => pure (y a))).symm)
+
+/-- The identity monad is initial: every morphism of monads out of it is `pure`. -/
+theorem eq_pure {f : ∀ {α}, Id α → m α} (hf : IsMonadHom Id m f)

you could also add the "unapplied version" like you did for eg comp_desc and comp_desc_apply

thomaskwaring thomaskwaring requested changes on Nov 06, 2026 22:03 UTC
Changes Requested Reviewer

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

tannerduve tannerduve commented on Nov 07, 2026 22:03 UTC
Author

@thomaskwaring thank you! just pushed another round of fixes

thomaskwaring thomaskwaring approved these changes on Nov 07, 2026 22:39 UTC
Approved Reviewer

thanks, looks great now -- you'd know better who can review the TCS content, i'll defer to them for final approval

eric-wieser eric-wieser commented on code on Nov 08, 2026 03:00 UTC
Code Review
Cslib/Foundations/Control/Monad/IsMonadHom.lean:1
@@ -214,6 +214,22 @@ protected theorem comp {f : ∀ {α}, n α → p α} {g : ∀ {α}, m α → n 
   toIsApplicativeHom := hf.toIsApplicativeHom.comp hg.toIsApplicativeHom
   map_bind _ _ := by simp [hf.map_bind, hg.map_bind]
 
+variable (m) in
+/-- `pure` is a morphism of monads out of the identity monad. -/
+protected theorem pure [LawfulMonad m] : IsMonadHom Id m pure :=

I'm pretty sure this is defeq abuse; you need (pure <| Id.run .)

eric-wieser eric-wieser commented on code on Nov 08, 2026 03:01 UTC
Code Review
Cslib/Foundations/Control/Monad/IsMonadHom.lean:1
@@ -214,6 +214,22 @@ protected theorem comp {f : ∀ {α}, n α → p α} {g : ∀ {α}, m α → n 
   toIsApplicativeHom := hf.toIsApplicativeHom.comp hg.toIsApplicativeHom
   map_bind _ _ := by simp [hf.map_bind, hg.map_bind]
 
+variable (m) in
+/-- `pure` is a morphism of monads out of the identity monad. -/
+protected theorem pure [LawfulMonad m] : IsMonadHom Id m pure :=
+  .mk' (fun _ => rfl) (fun x y => (pure_bind x (fun a => pure (y a))).symm)
+
+/-- The identity monad is initial: every morphism of monads out of it is `pure`. -/
+theorem eq_pure {f : ∀ {α}, Id α → m α} (hf : IsMonadHom Id m f) :
+    @f = (pure : ∀ {α}, Id α → m α) := by

    @f = (fun {α} x => pure x.run) := by

or similar

eric-wieser eric-wieser commented on Nov 08, 2026 08:22 UTC
Reviewer

Are there any trivial instances you can add to show the definition is reasonable? Perhaps at least that IsCoproduct Id X X?

tannerduve tannerduve commented on Nov 09, 2026 02:26 UTC
Author

thanks @eric-wieser i just made those changes. note I already have unitLeft and unitRight as trivial instances, did you mean something else?

eric-wieser eric-wieser commented on code on Nov 09, 2026 02:39 UTC
Code Review
Cslib/Foundations/Control/Monad/Coproducts.lean:1
@@ -0,0 +1,330 @@
+/-
+Copyright (c) 2026 Tanner Duve. All rights reserved.
+Released under Apache 2.0 license as described in the file LICENSE.
+Authors: Tanner Duve
+-/
+
+module
+
+public import Cslib.Foundations.Control.Monad.IsMonadHom
+public import Mathlib.Logic.Equiv.Defs
+
+/-!
+# Coproducts of monads
+
+This file defines the universal property of coproducts of lawful monads (`IsMonadCoproduct`)
+using `IsMonadHom`. The universal property is carried as data: `IsMonadCoproduct.desc`
+interprets the coproduct into any monad equipped with interpretations of both components,
+so that concrete coproducts (such as free monads on a sum of signatures) yield computable
+interpreters.
+
+A coproduct combines the effects of two monads: computations from either monad can be
+included and sequenced in the coproduct using `do` notation. A pair of monad morphisms
+interpreting the component effects determines a unique interpretation of the combined effects.
+
+## References
+
+* [Jiří Adámek, Nathan Bowler, Paul B. Levy, Stefan Milius,
+  *Coproducts of Monads on Set*][Adamek2012]
+* [Christoph Lüth, Neil Ghani, *Composing Monads Using Coproducts*][LuthGhani2002]
+-/
+
+@[expose] public section
+
+namespace Cslib
+
+universe u v
+
+/-- The maps `inl` and `inr` exhibit `p` as a coproduct of lawful monads. -/
+structure IsMonadCoproduct

Is is usually reserved for Prop, but this carries data. Perhaps MonadCoproductLike (akin to RCLike) is a reasonable name, or HasMonadCoproductDesc?

eric-wieser eric-wieser commented on code on Nov 09, 2026 02:40 UTC
Code Review
Cslib/Foundations/Control/Monad/Coproducts.lean:1
@@ -0,0 +1,330 @@
+/-
+Copyright (c) 2026 Tanner Duve. All rights reserved.
+Released under Apache 2.0 license as described in the file LICENSE.
+Authors: Tanner Duve
+-/
+
+module
+
+public import Cslib.Foundations.Control.Monad.IsMonadHom
+public import Mathlib.Logic.Equiv.Defs
+
+/-!
+# Coproducts of monads
+
+This file defines the universal property of coproducts of lawful monads (`IsMonadCoproduct`)
+using `IsMonadHom`. The universal property is carried as data: `IsMonadCoproduct.desc`
+interprets the coproduct into any monad equipped with interpretations of both components,
+so that concrete coproducts (such as free monads on a sum of signatures) yield computable
+interpreters.
+
+A coproduct combines the effects of two monads: computations from either monad can be
+included and sequenced in the coproduct using `do` notation. A pair of monad morphisms
+interpreting the component effects determines a unique interpretation of the combined effects.
+
+## References
+
+* [Jiří Adámek, Nathan Bowler, Paul B. Levy, Stefan Milius,
+  *Coproducts of Monads on Set*][Adamek2012]
+* [Christoph Lüth, Neil Ghani, *Composing Monads Using Coproducts*][LuthGhani2002]
+-/
+
+@[expose] public section
+
+namespace Cslib
+
+universe u v
+
+/-- 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 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 α

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.

eric-wieser eric-wieser commented on Nov 09, 2026 02:50 UTC
Contributor

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.

eric-wieser eric-wieser commented on code on Nov 09, 2026 02:59 UTC
Code Review
Cslib/Foundations/Control/Monad/IsMonadHom.lean:1
@@ -214,6 +214,22 @@ protected theorem comp {f : ∀ {α}, n α → p α} {g : ∀ {α}, m α → n 
   toIsApplicativeHom := hf.toIsApplicativeHom.comp hg.toIsApplicativeHom
   map_bind _ _ := by simp [hf.map_bind, hg.map_bind]
 
+variable (m) in
+/-- `pure` is a morphism of monads out of the identity monad. -/
+protected theorem pure [LawfulMonad m] : IsMonadHom Id m (fun x => pure x.run) :=
+  .mk' (fun _ => rfl) (fun x y => (pure_bind x.run (fun a => pure (y a).run)).symm)
+
+/-- The identity monad is initial: every morphism of monads out of it is `pure`. -/
+theorem eq_pure {f : ∀ {α}, Id α → m α} (hf : IsMonadHom Id m f) :
+    @f = (fun {_} x => pure x.run) := by
+  funext α x
+  exact hf.map_pure x.run

You could consider also defining ext_id, which says that @f = @g for any two IsMonadHoms

thomaskwaring thomaskwaring commented on Nov 09, 2026 12:34 UTC
Contributor

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

Reviewers
Assignees
tannerduve tannerduve
Labels
risk: low confidence: 90.1% reviewed
Vouch Risk Breakdown
AI-powered review risk analysis
Change Risk
Change Risk · Model 1 (XGBoost)
Predicted defect probability (0.00–1.00) based on diff churn, modified sensitive files, and author risk history.
Score: 42.8% (MEDIUM) · Higher = larger blast radius
42.8%
Review Confidence
Review Confidence · Models 2 & 3
Scrutiny score (0.00–1.00) combining DistilBERT semantic review depth with reviewer familiarity and session dwell time.
Score: 90.1% · Lower = rushed or shallow review
90.1%
Residual Risk
Residual Risk · Core Equation
Residual = Change × (1 − Confidence)
Unmitigated risk surviving review. ≥ 0.65 triggers an automated re-queue to protect production.
Score: 4.2% · Safe to merge
4.2%