Skip to content

Commit 5207100

Browse files
committed
feat(CategoryTheory/MarkovCategory): Add Markov category (leanprover-community#29925)
This PR introduces Markov categories, extending copy-discard categories with natural discard to model categorical probability theory. This PR builds on leanprover-community#29939 and leanprover-community#29919 and was split up from leanprover-community#29657. ## Main additions ### 1. Markov categories (`MarkovCategory`) Extends `CopyDiscardCategory` where discard is natural: `f ≫ ε[Y] = ε[X]` for all morphisms `f : X → Y`. This forces a probabilistic interpretation where morphisms preserve normalization (probability measures sum to 1). ## Mathematical significance Markov categories capture probability theory categorically: - Morphisms: Stochastic processes/Markov kernels - Copy: Perfect correlation (both outputs always equal) - Discard: Marginalization (integrating out variables) - Natural discard: Preservation of total probability The same framework handles finite probability (stochastic matrices), measure-theoretic probability (Markov kernels), and quantum probability (completely positive maps). ## Design decisions - Terminal unit follows from axioms. We prove `unit_terminal` and `unit_isTerminal` rather than assuming them ## Implementation notes - Built on top of `CopyDiscardCategory` per suggestion [#mathlib4 > Markov categories PR @ 💬](https://leanprover.zulipchat.com/#narrow/channel/287929-mathlib4/topic/Markov.20categories.20PR/near/539656225) - Includes both computational (`unit_terminal`) and categorical (`unit_isTerminal`) characterizations of the terminal object ## References Cho & Jacobs (2019) and Fritz (2020) [![Open in Gitpod](https://gitpod.io/button/open-in-gitpod.svg)](https://gitpod.io/from-referrer/) - [x] depends on: leanprover-community#29939
1 parent 1ddcdac commit 5207100

3 files changed

Lines changed: 172 additions & 0 deletions

File tree

Mathlib.lean

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2561,6 +2561,7 @@ import Mathlib.CategoryTheory.Localization.StructuredArrow
25612561
import Mathlib.CategoryTheory.Localization.Triangulated
25622562
import Mathlib.CategoryTheory.Localization.Trifunctor
25632563
import Mathlib.CategoryTheory.LocallyDirected
2564+
import Mathlib.CategoryTheory.MarkovCategory.Basic
25642565
import Mathlib.CategoryTheory.Monad.Adjunction
25652566
import Mathlib.CategoryTheory.Monad.Algebra
25662567
import Mathlib.CategoryTheory.Monad.Basic
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
/-
2+
Copyright (c) 2025 Jacob Reinhold. All rights reserved.
3+
Released under Apache 2.0 license as described in the file LICENSE.
4+
Authors: Jacob Reinhold
5+
-/
6+
import Mathlib.CategoryTheory.CopyDiscardCategory.Basic
7+
import Mathlib.CategoryTheory.Limits.Shapes.IsTerminal
8+
9+
/-!
10+
# Markov Categories
11+
12+
Copy-discard categories where deletion is natural for all morphisms.
13+
14+
## Main definitions
15+
16+
* `MarkovCategory` - Copy-discard category with natural deletion
17+
18+
## Main results
19+
20+
* `eq_discard` - Any morphism to the unit equals discard
21+
* `isTerminalUnit` - The monoidal unit is terminal
22+
23+
## Implementation notes
24+
25+
Natural discard forces probabilistic interpretation: morphisms preserve normalization. The unit
26+
being terminal follows from naturality of discard.
27+
28+
The key property `discard_natural : f ≫ ε[Y] = ε[X]` means discard "erases" any preceding
29+
morphism, a distinguishing feature of Markov categories in categorical probability.
30+
31+
## References
32+
33+
* [Cho and Jacobs, *Disintegration and Bayesian inversion via string diagrams*][cho_jacobs_2019]
34+
* [Fritz, *A synthetic approach to Markov kernels, conditional independence
35+
and theorems on sufficient statistics*][fritz2020]
36+
37+
## Tags
38+
39+
Markov category, probability, categorical probability
40+
-/
41+
42+
universe v u
43+
44+
namespace CategoryTheory
45+
46+
open MonoidalCategory CopyDiscardCategory ComonObj Limits
47+
48+
variable {C : Type u} [Category.{v} C] [MonoidalCategory.{v} C]
49+
50+
/-- Copy-discard category where discard is natural. -/
51+
class MarkovCategory (C : Type u) [Category.{v} C] [MonoidalCategory.{v} C]
52+
extends CopyDiscardCategory C where
53+
/-- Process then discard equals discard directly. -/
54+
discard_natural {X Y : C} (f : X ⟶ Y) : f ≫ ε[Y] = ε[X]
55+
56+
namespace MarkovCategory
57+
58+
variable [MarkovCategory C]
59+
60+
attribute [reassoc (attr := simp)] discard_natural
61+
62+
/-- Any morphism to the unit equals discard. -/
63+
theorem eq_discard (X : C) (f : X ⟶ 𝟙_ C) : f = ε[X] := by
64+
rw [← Category.comp_id f, ← discard_unit, discard_natural]
65+
66+
/-- The monoidal unit is a terminal object. -/
67+
def isTerminalUnit : IsTerminal (𝟙_ C) :=
68+
IsTerminal.ofUniqueHom _ eq_discard
69+
70+
/-- There is a unique morphism to the unit (it is terminal). -/
71+
instance (X : C) : Subsingleton (X ⟶ 𝟙_ C) where
72+
allEq := isTerminalUnit.hom_ext
73+
74+
end MarkovCategory
75+
76+
end CategoryTheory
Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
/-
2+
Copyright (c) 2025 Jacob Reinhold. All rights reserved.
3+
Released under Apache 2.0 license as described in the file LICENSE.
4+
Authors: Jacob Reinhold
5+
-/
6+
import Mathlib.CategoryTheory.MarkovCategory.Basic
7+
import Mathlib.CategoryTheory.CopyDiscardCategory.Cartesian
8+
import Mathlib.CategoryTheory.Monoidal.Types.Basic
9+
10+
/-!
11+
# Tests for Markov Categories
12+
13+
Tests for the Markov category and copy-discard category implementation.
14+
15+
## Tests included
16+
17+
* Instance inference for cartesian and copy-discard structures
18+
* Comonoid laws: comultiplication and counit axioms
19+
* Markov category laws: naturality of discard and terminal unit
20+
* Simp automation for comonoid and discard naturality
21+
22+
-/
23+
24+
universe u
25+
26+
open CategoryTheory MonoidalCategory CopyDiscardCategory ComonObj IsCommComonObj
27+
28+
section BasicTests
29+
30+
/-- CartesianMonoidalCategory instance exists for Type* -/
31+
example : CartesianMonoidalCategory (Type u) := inferInstance
32+
33+
/-- CopyDiscardCategory instance for Type* via cartesian structure -/
34+
example : CopyDiscardCategory (Type u) := CartesianCopyDiscard.ofCartesianMonoidalCategory
35+
36+
end BasicTests
37+
38+
section ComonoidLaws
39+
40+
variable {C : Type u} [Category.{u} C] [MonoidalCategory.{u} C] [CopyDiscardCategory C]
41+
42+
/-- Comultiplication is commutative -/
43+
example (X : C) : Δ[X] ≫ (β_ X X).hom = Δ[X] := comul_comm X
44+
45+
/-- Left counit law: copy then discard left component recovers object -/
46+
example (X : C) : Δ[X] ≫ (ε[X] ▷ X) = (λ_ X).inv := ComonObj.counit_comul X
47+
48+
/-- Right counit law: copy then discard right component recovers object -/
49+
example (X : C) : Δ[X] ≫ (X ◁ ε[X]) = (ρ_ X).inv := ComonObj.comul_counit X
50+
51+
/-- Comultiplication is coassociative -/
52+
example (X : C) :
53+
Δ[X] ≫ (X ◁ Δ[X]) = Δ[X] ≫ (Δ[X] ▷ X) ≫ (α_ X X X).hom :=
54+
ComonObj.comul_assoc X
55+
56+
end ComonoidLaws
57+
58+
section MarkovLaws
59+
60+
variable {C : Type u} [Category.{u} C] [MonoidalCategory.{u} C] [MarkovCategory C]
61+
62+
/-- Discard is natural -/
63+
example {X Y : C} (f : X ⟶ Y) : f ≫ ε[Y] = ε[X] := MarkovCategory.discard_natural f
64+
65+
/-- Any morphism to the unit equals discard -/
66+
example (X : C) (f : X ⟶ 𝟙_ C) : f = ε[X] := MarkovCategory.eq_discard X f
67+
68+
/-- The monoidal unit is terminal -/
69+
example : Limits.IsTerminal (𝟙_ C : C) := MarkovCategory.isTerminalUnit
70+
71+
/-- Any two morphisms to the unit are equal (subsingleton instance) -/
72+
example (X : C) (f g : X ⟶ 𝟙_ C) : f = g := Subsingleton.elim f g
73+
74+
end MarkovLaws
75+
76+
section SimpLemmas
77+
78+
variable {C : Type u} [Category.{u} C] [MonoidalCategory C] [CopyDiscardCategory C]
79+
80+
/-- Simp automation for counit laws -/
81+
example (X : C) : Δ[X] ≫ (ε[X] ▷ X) = (λ_ X).inv := by simp
82+
83+
/-- Simp automation for comultiplication commutativity -/
84+
example (X : C) : Δ[X] ≫ (β_ X X).hom = Δ[X] := by simp
85+
86+
end SimpLemmas
87+
88+
section MarkovSimpLemmas
89+
90+
variable {C : Type u} [Category.{u} C] [MonoidalCategory.{u} C] [MarkovCategory C]
91+
92+
/-- Simp automation for naturality of discard -/
93+
example {X Y Z : C} (f : X ⟶ Y) (g : Y ⟶ Z) : f ≫ g ≫ ε[Z] = f ≫ ε[Y] := by simp
94+
95+
end MarkovSimpLemmas

0 commit comments

Comments
 (0)