5
$\begingroup$

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

$\endgroup$
5
$\begingroup$

For a better understanding I would recommend Stepanov's book , Mike Stay's article, and his more recent blog on Category Theory in Javascript.

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.

$\endgroup$
  • 2
    $\begingroup$ 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. $\endgroup$ – Hi-Angel Jan 17 '16 at 11:30
9
$\begingroup$

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.

$\endgroup$
3
$\begingroup$

Bart Jacobs tackled this problem at one point. In his view, objects 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 streams of integers:

class Stream
  int GetCurrentVal()
  Stream Next()

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 Next is represented with the right component $X$.

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.

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.