Category Theory
Zulip Server
Archive

You're reading the public-facing archive of the Category Theory Zulip server.
To join the server you need an invite. Anybody can get an invite by contacting Matteo Capucci at name dot surname at gmail dot com.
For all things related to this archive refer to the same person.


Stream: learning: questions

Topic: Interpreting slice categories in programming


view this post on Zulip Davi Sales Barreira (Dec 08 2023 at 15:02):

First let me give some context. I'm coding an application, and I'm trying to interpret within category theory. Thus, I've started by assuming that the programming code was in the category of sets, with types as sets, and programming functions as mathematical functions in set-theory.

From that, I've coded a free monad D:SetSetD:\textbf{Set}\to \textbf{Set}. Given a type XX, an element of DXDX is an expression tree where the leafs have values of type XX.

In my code, I have an special type called PP, which represents primitives. Given a type (set) AA, I wanted to assign a function θA:ADP\theta_A: A \to D P, i.e. a function that receives a value of type AA and constructs an expression formed by primitives. After some discussion, some people pointed out that the whole thing could be seen as the slice category Kleisli(D)/P\text{Kleisli}(D)/P. Indeed, after inspection, they were right, and this interpretation fits nicely (I wanted the composition to work like it does in the Kleisli category).

Here is my new conundrum. I now want to use my monad DD to construct expression trees where the leafs have "type" Kleisli(D)/P\text{Kleisli}(D)/P, but Kleisli(D)/P\text{Kleisli}(D)/P is a category not a set. Hence, I can't just interpret it as a type. I was wondering if there is a way to interpret this category in programming. My first idea was to somehow represent Kleisli(D)/P\text{Kleisli}(D)/P as a set or perhaps as a subcategory within the category of sets. I thought of Yoneda lemma (which I am frankly still in the process of fully understanding).

view this post on Zulip David Egolf (Dec 08 2023 at 18:02):

I have a few thoughts/questions for you, as I'm trying to understand what you want to do:

  1. D:SetSetD: \mathsf{Set} \to \mathsf{Set} is a functor, right? I think you're saying it sends a set XX to the set DXDX of all expression trees we can form (using some operators) where all the leaves are elements of XX. If f:XYf: X \to Y is a function, I'm wondering what Df:DXDYDf: DX \to DY is. Is this a function that takes in an expression tree, and then swaps out any occurrence of xXx \in X with f(x)Yf(x) \in Y?
  2. I think one idea you are describing is that a function θA:ADP\theta_A: A \to DP tells us how to "build" any element aAa \in A using a corresponding expression tree θA(a)\theta_A(a) which describes how to use "primitives" (elements of PP) to make aa. That seems really cool!

view this post on Zulip David Egolf (Dec 08 2023 at 18:04):

  1. I'm trying to understand your last paragraph. If each function θA:ADP\theta_A: A \to DP is like a collection of recipes for building elements of aa (and these functions are objects of Kleisli(D)/P\mathrm{Kleisli}(D)/P), then I guess that you are talking about sticking together different collections of recipes to make a fancier collection of recipes (with the "sticking together" process described by some new expression tree)?

view this post on Zulip David Egolf (Dec 08 2023 at 18:11):

This starts to remind me of composition in an operad, which looks like this:
composition in an operad

We could imagine the "wires" coming down the page as the different ingredients we need to build something, and each triangle as a process that uses a given recipe to build something new from these ingredients. (The image is from "Entropy and diversity" by Leinster).

I'm not sure how closely this relates to what you want to do.

view this post on Zulip Davi Sales Barreira (Dec 08 2023 at 19:01):

For 1., the answer is that DfDf is the function that applies ff to each leaf of DXDX, turning it into a DYDY.

view this post on Zulip Davi Sales Barreira (Dec 08 2023 at 19:02):

About 3., yeah, it does sounds like operads. But I never quite understood operads and how to use them in programming.

view this post on Zulip Davi Sales Barreira (Dec 08 2023 at 19:03):

Thus I just went another route. I ended up with the idea of the free monad DD.

view this post on Zulip David Egolf (Dec 08 2023 at 19:23):

Thanks for clarifying!

I still don't think I quite understand what you want to do. Is a tree where the leaves are objects from Kleisli(D)/P\mathrm{Kleisli}(D)/P the sort of thing you are looking for?

view this post on Zulip Davi Sales Barreira (Dec 08 2023 at 19:27):

Yes. For example, in DPDP I have p1+p2+p3p1 + p2 + p3. Which is an expression that says "draw p1p1 then p2p2 then p3p3". I want to have (aA,θA)+(bB,θB)+(cC,θC)(a \in A,\theta_A) + (b \in B,\theta_B) + (c \in C, \theta_C).

view this post on Zulip Davi Sales Barreira (Dec 08 2023 at 19:27):

Which is an expression constructed using DD, but applying to the object in the slice category.

view this post on Zulip Davi Sales Barreira (Dec 08 2023 at 19:29):

Note that this expression can be translated into DPDP by applying θ\theta, e.g. θA(a)+θB(b)+θC(c)\theta_A(a) + \theta_B(b) + \theta_C(c) gives me an expression of expressions i.e. DDPDDP. Since DD is a monad, I can simply use μ\mu, thus, μ(θA(a)+θB(b)+θC(c))\mu (\theta_A(a) + \theta_B(b) + \theta_C(c)) returns a DPDP.

view this post on Zulip Davi Sales Barreira (Dec 08 2023 at 19:30):

This interests me because it means I can construct complex expressions in DPDP using the "encapsulations".

view this post on Zulip Davi Sales Barreira (Dec 08 2023 at 19:30):

Makes sense?

view this post on Zulip Davi Sales Barreira (Dec 08 2023 at 19:31):

This has be clumsly coded, but it works... I'm now trying to find a categorical justification

view this post on Zulip Davi Sales Barreira (Dec 08 2023 at 19:33):

David Egolf said:

Thanks for clarifying!

I still don't think I quite understand what you want to do. Is a tree where the leaves are objects from Kleisli(D)/P\mathrm{Kleisli}(D)/P the sort of thing you are looking for?

Perhaps my example clarified that the leafs have "types"that are objects in Kleisi(D)/P\text{Kleisi(D)}/P, they are not directly the objects.

view this post on Zulip Davi Sales Barreira (Dec 08 2023 at 19:35):

I guess the idea is similar to saying that I want to use DMonoidD \text{Monoid} which should mean that I want to construct a tree where each leave is a monoid, but each monoid can be distinct (e.g. one can be a list of strings, the other can be the integers using sum, and so on).

view this post on Zulip David Egolf (Dec 08 2023 at 19:49):

Davi Sales Barreira said:

Yes. For example, in DPDP I have p1+p2+p3p1 + p2 + p3. Which is an expression that says "draw p1p1 then p2p2 then p3p3". I want to have (aA,θA)+(bB,θB)+(cC,θC)(a \in A,\theta_A) + (b \in B,\theta_B) + (c \in C, \theta_C).

I'm still working on reading the rest of what you said, but these tuples remind me a lot of the "category of elements" construction. If we have a "forgetful" functor U:Kleisli(D)/PSetU: \mathrm{Kleisli}(D)/P \to \mathsf{Set} that sends θA:ADP\theta_A: A \to DP to just AA, then an object in the category of elements for UU is a tuple (θA,a)(\theta_A, a), where aU(θA)=Aa \in U(\theta_A) = A.

view this post on Zulip David Egolf (Dec 08 2023 at 19:50):

Here's the definition, for convenience (from "Category theory in context"):
category of elements

view this post on Zulip David Egolf (Dec 08 2023 at 19:55):

If I understand what you are saying, you want to construct trees where the leaves are tuples of this kind. (These trees encode expressions combining these tuples). If so, I think you are looking for trees having leaves that are objects in the category of elements of U:Kleisli(D)/PSetU: \mathrm{Kleisli}(D)/P \to \mathsf{Set}.

view this post on Zulip Davi Sales Barreira (Dec 08 2023 at 20:00):

Hmm, I'm not sure. I'll have to think about it.