# Category Theory in Programming

**URL:** <https://racket.discourse.group/t/category-theory-in-programming/3375>\
**Category:** General\
**Tags:** category, advent-2024\
**Created:** [December 1, 2024, 12:30am UTC](https://racket.discourse.group/t/category-theory-in-programming/3375 "2024-12-01T00:30:01Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![NoahStoryM](https://yyz2.discourse-cdn.com/free1/user_avatar/racket.discourse.group/noahstorym/32/17_2.png) [@NoahStoryM](https://racket.discourse.group/u/NoahStoryM)\
**Post date:** [December 1, 2024, 12:30am UTC](https://racket.discourse.group/t/category-theory-in-programming/3375/1 "2024-12-01T00:30:01Z")

</div>

The full tutorial is here [CTP](https://docs.racket-lang.org/ctp/index.html).

In this tutorial, I explore the applications of category theory in programming, using Racket to represent abstract mathematical concepts. The completed sections introduce categories, functors, and natural transformations, demonstrating how these ideas map into programming constructs with practical code examples. This work aims to help programmers understand system design from a mathematical perspective, building a foundation that progresses from basic structures to advanced abstractions.

The motivation for this tutorial stems from my own journey of learning category theory. Initially, I often wondered what category theory meant in the context of programming but struggled to find answers. This changed when I encountered @countvajhula's [Qi](https://github.com/drym-org/qi) project, which amazed me with its ability to align category theory concepts with programming constructs. Using qi, one can program in a Cartesian closed category style, distinct from the lambda calculus approach. This inspired me to abstract the concept of Category much like how one abstracts pair, enabling the use of programming examples to further explore category theory.

As someone without a formal mathematics background, I have self-taught category theory and written this book to share the connections I've discovered between programming and category theory. My hope is to inspire programmers to think about and solve problems, as well as design systems, through the lens of category theory. However, as a non-mathematician, I acknowledge that there may be inaccuracies or a lack of rigor in some areas, and I warmly welcome feedback to improve this work.

---

<div class="post-metadata">

**Author:** ![countvajhula](https://yyz2.discourse-cdn.com/free1/user_avatar/racket.discourse.group/countvajhula/32/65_2.png) [@countvajhula](https://racket.discourse.group/u/countvajhula)\
**Post date:** [December 1, 2024, 1:08am UTC](https://racket.discourse.group/t/category-theory-in-programming/3375/2 "2024-12-01T01:08:40Z")

</div>

This looks amazing! 🤩

(Btw, this probably needs some kind of tag to indicate it’s part of the advent calendar. What would you advise @spdegabrielle ?)

---

<div class="post-metadata">

**Author:** ![spdegabrielle](https://yyz2.discourse-cdn.com/free1/user_avatar/racket.discourse.group/spdegabrielle/32/95_2.png) [@spdegabrielle](https://racket.discourse.group/u/spdegabrielle)\
**Post date:** [December 1, 2024, 4:55pm UTC](https://racket.discourse.group/t/category-theory-in-programming/3375/3 "2024-12-01T16:55:48Z")

</div>

I've added the tag #advent-2024

Thank you @NoahStoryM

Stephen  
🎄

---

<div class="post-metadata">

**Author:** ![NoahStoryM](https://yyz2.discourse-cdn.com/free1/user_avatar/racket.discourse.group/noahstorym/32/17_2.png) [@NoahStoryM](https://racket.discourse.group/u/NoahStoryM)\
**Post date:** [December 3, 2024, 2:47am UTC](https://racket.discourse.group/t/category-theory-in-programming/3375/4 "2024-12-03T02:47:15Z")

</div>

# Motivation Behind _CTP_

The _CTP_ project was inspired by my exploration of the [Qi](https://github.com/drym-org/qi) project, which offers a rich set of operators for composing functions. Qi's core operators, such as `n>`, `==`, `-<`, and `><`, revolve around `values`, and @countvajhula once proposed the idea of complementary operators (like `<>`) without fully defining their semantics ([source](https://racket.discourse.group/t/construct-monad-in-sicp-pict/985/4)).

When I started using Qi, these operators—especially `values`—reminded me of category theory's **product** in the **Set** category. Specifically, `values` seemed to correspond to the Cartesian product of Set.

For example:

- The Qi operator `-<` aligns with the **pairing** and `==*` aligns with the **product** in category theory:

```scheme
  f : A -> X
  g : A -> Y
  h : B -> Z
  (-< f g) : A -> X × Y
  (==* g h) : A × B -> Y × Z

```

Similarly, Qi inspired me to think about **sums** in Set (disjoint unions or tagged unions). For example, X = {x}, Y = {y}, then X + Y = {(x, 0), (y, 0)}. This led to defining dual operators for Qi:

- `>-` corresponds to **copairing** and `==+` corresponds to **coproduct** :

```scheme
  f : X -> A
  g : Y -> A
  h : Z -> B
  (>- f g) : X + Y -> A
  (==+ g h) : Y + Z -> A + B

```

This realization led me to re-examine examples of Cartesian closed categories (CCCs) in _Category Theory for Computing Science_ using Qi ([source](https://github.com/drym-org/qi/issues/62)). To my surprise, Qi's operators aligned naturally with the mathematical constructs.

As a programmer, the functional programming languages ​​I have encountered are all based on lambda calculus, and I had never heard of CCC before. Now I know another programming style that is centered on functions (more precisely, morphisms).

This exploration of Qi also made me think about its type system. In Typed Racket, the handling of `values` is limited, restricting the use of `compose`, which is central to Qi. I think Typed Racket's type and contract systems could be extended using ideas from limits in category theory. This could enable a more robust and expressive system for composing functions.

On the other hand, Qi also aims to enable **flow-oriented programming** ([source](https://github.com/drym-org/qi/issues/116)). In my understand, each Qi instance could represent a category, its flows represent morphisms in that category. Using functors, we could establish relationships between different Qi instances, leading to natural transformations and even higher abstractions like 2-categories.

I've experimented with code drafts to explore these deeper connections between category theory and programming ([source](https://racket.discourse.group/t/functor-natural-tranformation-and-monad-in-racket/1930)), and the results have been fascinating! I think this way of programming can be called **morphism-oriented programming**.

# Why Write _CTP_?

_CTP_ is not a comprehensive textbook but more of a collection of mathematical notes inspired by my programming experiments. As a programmer without formal mathematical training, I use programming to better understand category theory concepts. Writing this book has been an iterative process, focusing on foundational topics like categories, functors, and natural transformations to ensure a solid understanding before tackling more abstract ideas.

Unlike traditional category theory resources that use examples from other mathematical fields, I rely exclusively on programming examples. The goal is to demonstrate that programmers can understand the basics of category theory using only familiar programming constructs. By knowing what categories, functors, and natural transformations are and how to abstract them in code, programmers can independently explore deeper aspects of category theory, which is my main motivation for releasing this work-in-progress at the moment.

While the content is still rough, I hope it will inspire programmers to consider category theory as a new way of thinking about designing systems and solving problems, just like I did with Qi! 😛
