I recently discovered (evidently I’m slow in keeping up with recent conferences) a nice paper from LDTA ’12 called “Language composition untangled [pdf].” I’m going to pull two concepts out of that paper, and talk about them a little bit more.
They (Erdweg, Giarrusso, Rendel) introduce two operators for talking about language composition. (Well, more really, but I’m just interested in these two.) The first they called “language extension” and they write to represent the language formed by composing a host language H with an extension E that was written specifically for H. There are two algebraic properties to note: it’s not commutative and it’s not associative.
The second operator they call “language unification” (I really don’t like this term, but oh well!) and they write to represent the language composed with a separate language using the “glue code” g. There are two algebraic properties to note: it’s not associative, but it is commutative.
So far we have very little theory here. We can at best say that . Not very exciting. So let’s take a detour and ask ourselves “well, what do we want to say?”
Well, let’s start by relating the two operators. Can we say they’re the same in some fashion? Well, that depends. We can write something like . In essence this says we can turn a language unification into an extension by merging in the glue code with one or the other languages, which seems logical. However, this introduces a new operator: that informal little that talks about merging in the glue code to a language. So to admit this kind of equation, we need to force a property onto our language composition framework. We need to be able to manipulate the specifications in this way, and consequently the specifications can only say things that CAN be manipulated in this way. Okay, what else might we want?
Suppose we have two language extensions: and . We might want some ability to obtain a language with both extensions at the same time. For example, . That’s nice. We can write that. But it does leave us with a little problem: we already had the two extended languages that worked just fine, but now all of a sudden to get them both to work together we need this pesky g glue code to fix up the composition. We can’t get a language with both extensions without it. Well, let’s assert something about our language composition framework again: if we have and , let’s say we can obtain . That is, for two extensions to a common host, we do “language unification” without any glue code ()
There’s a nice property that has, too: it’s associative. In fact, it seems that it is also associative with any “language unification” operation: seems a little bit weird, but I think that’s because is just plain weird in the first place. It kinda only makes sense if you think about it in terms of its operands having some common host language, somewhere deep down. Certainly, that’s the only way we’ve said you can obtain it. Anyway, we also have a nice property that , which doesn’t necessarily hold if .
Okay, what can we do with these properties? Well, for one we can compose any number of extensions to H: is legit. That’s neato.
Let’s look a little bit at . We can rewrite this as . Taking advantage of the associativity of and commutativity of in general, we can produce , which we can rewrite back to . And we can flip it the other way around, too. Cool!
Let’s try something different, starting again with , we can do the rewrites in a different direction and get . Let’s take some small liberties to write that as . Awesome! These things that seem to be the same, are! Well… with a few liberties about how we treat that glue code, that is.
So it would seem that capability to obtain lets us “lift” an extension to “depend” on another extension if we so choose.
Let’s get crazier. Suppose we have , , , and . Can we compose them all? Well, we can certainly produce . And from there we can use to above to obtain , and of course with that we can obtain . Reversing direction, we can get right back to , and from there it’s obvious we’ll be able to manipulate in as well.
Alright, so what’s the point of all this? Well, I found it interesting to try to think about what sort of properties we need to be able to do the kind of complicated composition in the last paragraph. I’m not sure I’ve formulated things well enough yet, but so far we needed:
- The ability to produce from and .
- The equalities , which although they do not even mention it, seem to boil down to some ability to manipulate glue code like a set.
Our little attribute grammar playground, Silver, gets the second property easily, since for attribute grammars you pretty much can define and . Although this does mean extensions (and glue code) can’t go do anything like delete from a host language. Or overwrite equations from the host language. (Well, we never had that in the first place! Hooray!)
The tricky part is the first property. That composition has to you know, work. And like, not just be a broken compiler. Getting that first property will be my thesis. Whheeee!