<p>Update (2020)</p>
<p>Have a look at this year's CPPCon talk -  <a href="https://www.youtube.com/watch?v=giWCdQ7fnQU" rel="nofollow noreferrer">Monoids, Monads, and Applicative Functors: Repeated Software Patterns</a></p>
<p>Also look for mentions of the Fin category in <a href="http://www.math.jhu.edu/%7Eeriehl/context.pdf" rel="nofollow noreferrer">Category Theory in Context</a></p>
<p>Objects can implement category theoretic data types and access patterns.</p>
<hr />
<p>For a better understanding I would recommend <a href="http://www.elementsofprogramming.com/book.html" rel="nofollow noreferrer">Stepanov's book</a> , <a href="http://reperiendi.wordpress.com/2007/11/03/category-theory-for-the-java-programmer/" rel="nofollow noreferrer">Mike Stay's article</a>, and his more recent blog on <a href="http://jscategory.wordpress.com/" rel="nofollow noreferrer">Category Theory in Javascript</a>.</p>
<p>Use composition not inheritance.</p>
<p>Use sum types like enums to shrink the state space of your variables.</p>
<p>Use product types (tuples) like std::pair in C++.</p>
<p>If you have a function like  A someFunc(A a, B b, C c); the state space from the type signature is |A|^(|A|<em>|B|</em>|C|). Beware of functions that take many arguments as the state space of your function is astronomical.</p>

CC BY-SA 3.0
2016-01-17T11:30:32.607Z
For those who would bother reading the links — they're simply about implementing category theory concepts in OOP languages. I didn't find there the relation, i.e. things like OOP from the view point of category theory, critique or justification of OOP techniques from the view point of category theory, and etc.
Hi-Angel
CC BY-SA 4.0
2014-03-06T00:15:26.847Z
2020-10-12T18:58:27.503Z
2020-10-12T18:58:27.503Z
Chad Brewbaker
<p>There are absolutely some relationships between the semantics and practice of OOP and category theory. This is somewhat unsurprising since both fields attempt to give a principled <em>generic</em> account of structure and behavior in a synthetic manner.</p>

<p>The most apparent work I am aware of is the categorical semantics of UML, which is admittedly different from OOP in the large, but I think captures much of the crux of the debate on the semantics of objects themselves. One example is Zinovy Diskin's <a href="http://www.cs.toronto.edu/~zdiskin/Pubs/KluwerBook-2003.pdf">Mathematics of UML</a>.</p>

CC BY-SA 3.0
2016-01-19T18:35:52.207Z
2016-01-19T18:35:52.207Z
cody
<p>Bart Jacobs tackled this problem at one point. In his view, classes can be considered as coalgebras. Roughly, we have a polynomial endofunctor <span class="math-container">$F : \mathbf{Sets} \to \mathbf{Sets}$</span> which gives the class's type signature. A pair of a carrier set <span class="math-container">$X$</span> and an arrow <span class="math-container">$X \to FX$</span> is then used to &quot;implement&quot; the class. For example, consider a class representing counters:</p>
<pre><code>class Counter
  int GetCurrentVal()
  void Increment()
</code></pre>
<p>For this class interface, we have <span class="math-container">$F(X) \doteq \mathbb Z \times X$</span>. Each method is represented by a different product component: GetCurrentVal is represented with the left component <span class="math-container">$\mathbb Z$</span> and Increment is represented with the right component <span class="math-container">$X$</span>. If <span class="math-container">$f : X \to \mathbb Z \times X$</span> represents an implementation of this class and <span class="math-container">$x \in X$</span> represents an instance of the class, then the first component <span class="math-container">$\pi_1 f(x)$</span> is the Counter <span class="math-container">$x$</span>'s current value and <span class="math-container">$\pi_2 f(x)$</span> is the state of the counter after calling Increment.</p>
<p>The class implementations <span class="math-container">$(X \in \mathbf{Sets}_0, f : X \to FX)$</span> form a category, where an arrow from <span class="math-container">$(X,f)$</span> to <span class="math-container">$(Y,g)$</span> is an arrow <span class="math-container">$h : X \to Y$</span> in Sets that &quot;preserves observable behavior&quot;.</p>
<p>For more information, see <a href="http://citeseerx.ist.psu.edu/viewdoc/download;jsessionid=466BD1639255F6FD6BD151A873A5AE1D?doi=10.1.1.29.6018&amp;rep=rep1&amp;type=pdf" rel="nofollow noreferrer">Objects and Classes, Coalgebraically</a>.</p>

CC BY-SA 4.0
2019-05-17T17:52:02.730Z
2020-10-01T01:22:55.083Z
2020-10-01T01:22:55.083Z
Kevin Clancy
<p>What's the relation between OOP and category theory?  Is there some related work on this topic one can read?</p>

CC BY-SA 4.0
2019-05-20T10:00:49.293Z
For an early reference, check     https://dblp.org/rec/conf/rex/EhrichS89 .
Kai
<pl.programming-languages><ct.category-theory><survey><object-oriented>
What's the relation between OOP and category theory?
qazwsx
# What's the relation between OOP and category theory?

(Asked by: username_0 on 2014-03-05)

What's the relation between OOP and category theory? Is there some related work on this topic one can read?

**Tags:** pl.programming-languages, ct.category-theory, survey, object-oriented
> **Comments:**
> **username_1**: For an early reference, check     https://dblp.org/rec/conf/rex/EhrichS89 .  

---

## 3 answers

### Answer by username_2 on 2014-03-06. Score: 6 | Accepted answer

Update (2020)

Have a look at this year's CPPCon talk - [Monoids, Monads, and Applicative Functors: Repeated Software Patterns](https://www.youtube.com/watch?v=giWCdQ7fnQU)

Also look for mentions of the Fin category in [Category Theory in Context](http://www.math.jhu.edu/%7Eeriehl/context.pdf)

Objects can implement category theoretic data types and access patterns.

---

For a better understanding I would recommend [Stepanov's book](http://www.elementsofprogramming.com/book.html) , [Mike Stay's article](http://reperiendi.wordpress.com/2007/11/03/category-theory-for-the-java-programmer/), and his more recent blog on [Category Theory in Javascript](http://jscategory.wordpress.com/).

Use composition not inheritance.

Use sum types like enums to shrink the state space of your variables.

Use product types (tuples) like std::pair in C++.

If you have a function like A someFunc(A a, B b, C c); the state space from the type signature is |A|^(|A|*|B|*|C|). Beware of functions that take many arguments as the state space of your function is astronomical.

> **Comments:**
> **username_3**: For those who would bother reading the links — they're simply about implementing category theory concepts in OOP languages. I didn't find there the relation, i.e. things like OOP from the view point of category theory, critique or justification of OOP techniques from the view point of category theory, and etc.  

---

### Answer by username_4 on 2016-01-19. Score: 11

There are absolutely some relationships between the semantics and practice of OOP and category theory. This is somewhat unsurprising since both fields attempt to give a principled *generic* account of structure and behavior in a synthetic manner.

The most apparent work I am aware of is the categorical semantics of UML, which is admittedly different from OOP in the large, but I think captures much of the crux of the debate on the semantics of objects themselves. One example is Zinovy Diskin's [Mathematics of UML](http://www.cs.toronto.edu/~zdiskin/Pubs/KluwerBook-2003.pdf).


---

### Answer by username_5 on 2019-05-17. Score: 5

Bart Jacobs tackled this problem at one point. In his view, classes can be considered as coalgebras. Roughly, we have a polynomial endofunctor $F : \mathbf{Sets} \to \mathbf{Sets}$ which gives the class's type signature. A pair of a carrier set $X$ and an arrow $X \to FX$ is then used to "implement" the class. For example, consider a class representing counters:
class Counter
  int GetCurrentVal()
  void Increment()

For this class interface, we have $F(X) \doteq \mathbb Z \times X$. Each method is represented by a different product component: GetCurrentVal is represented with the left component $\mathbb Z$ and Increment is represented with the right component $X$. If $f : X \to \mathbb Z \times X$ represents an implementation of this class and $x \in X$ represents an instance of the class, then the first component $\pi_1 f(x)$ is the Counter $x$'s current value and $\pi_2 f(x)$ is the state of the counter after calling Increment.
The class implementations $(X \in \mathbf{Sets}_0, f : X \to FX)$ form a category, where an arrow from $(X,f)$ to $(Y,g)$ is an arrow $h : X \to Y$ in Sets that "preserves observable behavior".
For more information, see Objects and Classes, Coalgebraically.


---

