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: A Category for DSLs?


view this post on Zulip Davi Sales Barreira (Aug 10 2023 at 19:37):

I've been trying to understand how the concept of a domain-specific language fits within Category Theory. More specifically, here is the situation I have.

Consider a set Prim\mathbf{Prim}, from which I construct a free monoid [Prim][\mathbf{Prim}]. Now, a DSL defined over this set Prim\mathbf{Prim} would be a functor (or something) such that one could manipulate values of Prim\mathbf{Prim} and apply an
eval:DSLPrim[Prim]eval: DSL \mathbf{Prim} \to [\mathbf{Prim}].

This looked very similar to FF-algebras, but the caveat is that my evaleval function is not evaluating to the carrier, but to the free monoid over the carrier. Moreover, I'm interested in understanding if fixing the "carrier" and varying the functors (DSL?) I could define a terminal DSL, from which the other DSL could be derived. Does any of this makes any sense?

view this post on Zulip Kevin Arlin (Aug 10 2023 at 19:42):

A DSL is a kind of programming language. Do you know anything about denotational semantics of programming languages?

view this post on Zulip Davi Sales Barreira (Aug 10 2023 at 19:47):

Yeah, I know a bit about denotational semantics, mostly from functional programming. Not an expert though

view this post on Zulip Steve Huntsman (Aug 10 2023 at 20:53):

I recall Alan Jeffrey did some _very_ interesting work on semantics and monoidal stuff in the late nineties but I don't know where to find it anymore: it was a webpage, now just a glorified citation as far as I can tell under the title "Premonoidal categories and flow graphs." Some more recent stuff in this vein is (e.g.) https://arxiv.org/abs/2202.02061

view this post on Zulip Ben Logsdon (Aug 31 2023 at 20:00):

@Davi Sales Barreira Could you give an example of such a DSL you're interested in?

view this post on Zulip Jencel Panic (Sep 03 2023 at 08:04):

Primer on F-algebras: https://www.schoolofhaskell.com/user/bartosz/understanding-algebras

@Davi Sales Barreira, why did you choose the free monoid datastructure? Structures like lists are often the basis of the DSL, not the result.

view this post on Zulip Davi Sales Barreira (Sep 04 2023 at 11:45):

What I'm trying to understand is the following. I have Prim\mathbf{Prim} which is a collection of primitives. To describe complex objects, I want to generate collections of primitive. For example, in my case, I have primitives which are basic geometric objects which I use to describe a scene. Thus, a complex scene is just a collection of these basic geometric objects, i.e. an element of the free monoid over primitives. The thing is, how do I then construct these collections? Here is where a DSL comes in. The DSL incremenets some syntax/operations that I can use over my primitives in order to more "easily" create this scene. For example, a DSL can give a tree-like structure, which could then be flattened into a list of primitives. Before flattening, I have an expression in the DSL. Makes sense?

What I'm therefore trying to understand is if this notion of a DSL has a categorical analogue.

view this post on Zulip Jencel Panic (Sep 04 2023 at 15:00):

I think that this will be an F-algebra with [Prim][Prim] as a carrier.

view this post on Zulip Davi Sales Barreira (Sep 04 2023 at 16:36):

@Jencel Panic , in fact, if we consider the "DSL" over [Prim][Prim] so that eval(DSL[Prim])>[Prim]eval(DSL [Prim]) -> [Prim], than that would be an algebra... But using [Prim][Prim] as the carrier is not so natural. It is more natural to think of DSLDSL over PrimPrim.

view this post on Zulip Davi Sales Barreira (Sep 04 2023 at 16:36):

Hence why the more "natural" construction is not directly an F-Algebra.

view this post on Zulip Davi Sales Barreira (Sep 04 2023 at 16:37):

But perhaps there is some isomorphism hidden somewhere, and we can use DSLDSL over [Prim][Prim] and there is some natural extension to a DPrimD Prim.

view this post on Zulip Ben Logsdon (Sep 04 2023 at 17:59):

@Davi Sales Barreira Based on your example, I'm wondering if the free commutative monoid would be better for your application than the free monoid. Free commutative monoids capture the idea of "finite collection with multiplicity and without order", while free monoids capture the idea of "finite collection with multiplicity and with order". Perhaps the former makes more sense in this case?

view this post on Zulip Ben Logsdon (Sep 04 2023 at 18:01):

Anyway, what you're describing sounds vaguely like a morphism of monads. I'm far from an expert on monads, so take everything I say with a grain of salt. But if you had some monad TT capturing some tree-like structure encoding the abstract syntax of your language, then I would expect there to be a monad morphism TMT \Rightarrow M, where MM is the free (commutative) monoid functor. This is the "flattening" operation you described. (I know this can be done in simple cases, such as flattening binary trees to lists. What you want might be a bit more complicated.)

view this post on Zulip Ben Logsdon (Sep 04 2023 at 18:03):

So, if you're willing to make your syntax as simple as, "a scene consists of either A) a primitive, or B) a pair of scenes", then that would be represented by a monad morphism as I described above.

view this post on Zulip Ben Logsdon (Sep 04 2023 at 18:04):

In that simple case, I believe the DSL over PrimPrim would be the free algebra of the endofunctor XX2X \mapsto X^2 generated by PrimPrim, and it would indeed come with a canonical map to the free (commutative) monoid generated by PrimPrim.

view this post on Zulip Ben Logsdon (Sep 04 2023 at 18:09):

If you want more complicated syntax, the endofunctor you would choose would change in ways that I'd be happy to elaborate on if you'd like more detail.

view this post on Zulip Ben Logsdon (Sep 04 2023 at 18:11):

One thing to keep in mind, though, is that the actual language is probably more complicated than the simple version you're describing. I'm guessing that you have some primitives such as "translate", "rotate", "reflect", and "dilate" that are involved as well. In order to have a full account of what you're doing, you would need to take those into account as well.

view this post on Zulip Davi Sales Barreira (Sep 05 2023 at 11:37):

@Ben Logsdon , thanks for the answer. I actually want non-commutative, as the order is the order of the rendering of the objects.

view this post on Zulip Davi Sales Barreira (Sep 05 2023 at 11:42):

If I understood you, the idea is that we have free algebras over PrimPrim, and there is a canonical way to go to the free monoid... Is that it?

view this post on Zulip Davi Sales Barreira (Sep 05 2023 at 11:43):

Since you said I needed monads, would it be Free Monad?

view this post on Zulip Steve Huntsman (Sep 18 2023 at 17:18):

I just noticed https://arxiv.org/abs/2302.00744

view this post on Zulip Ben Logsdon (Oct 10 2023 at 21:45):

@Davi Sales Barreira sorry for the delay...I've been buried in teaching recently, so I'm just now coming back to this!

Yes, it sounds like you understood what I said correctly. The monad which generates free monoids is called the "free monoid monad", because the monad takes as an input an object XX and returns the free monoid generated by XX. And the key operation I think you're looking for is a natural transformation (i.e. a monad morphism) from some other monad TT to the free monoid monad.

And yes, what you said about the non-commutativity makes sense :)