Tropical string mathematics
Draft of 2018.10.16
In doing some background reading for another project, I came across this readable and interesting introduction to Tropical Mathematics from several years ago. Since the other project involves language design for genetic programming, maybe the confluence led to the dream I had last night about matrices of strings. Who knows?
Tropical arithmetic focuses on the tropical semiring , where represents the “tropical sum” operator (which produces the minimum of its two numerical arguments), and the “tropical product” (which produces the sum of its two arguments). That is, for example,
As it happens, familiar behavior from traditional algebra holds here as well: associativity, commutativity, and so on (though not subtraction or division, apparently). All kinds of interesting mathematical and useful models can be derived from this simple beginning, because (as you might imagine) just as a lot of linear algebraic formalism follows from the simple facts of traditional arithmetic, a lot of interesting tropical linear algebra follows from these variations.
But I’m going in a different direction here today, since it’s taking the form of a game or “what if” exercise or an exploration or a distraction from the work I should be doing… or whatever.
Tropical string manipulations
Let me hand-wave in my non-mathematical way and draw an analogy from the minimization and addition operations in tropical arithmetic to a slightly different pair of string operations. Specifically, let me define nearly-identical lexical operators for strings: will return the “minimum” or lexically first of its two arguments, and will produce the “concatenation” of its arguments.
There are some noteworthy differences and slack definitions in play here. This all came to me in the form of a dream as I was waking up this morning, and so this whole piece is merely notes on that dream, so maybe we’re not all the way to rigor yet, OK?
First, there’s the troubling and/or interesting observation that unlike the case with tropical arithmetic, . I guess we’ll have to live with that for now.
Then there’s that character I’ve dropped into the examples above. That’s just there to signify the empty string, “”. I’ll probably use “” more often than , because I can type it faster.
The other difference comes from my weird retention of the symbol I’ve slapped in there. I suspect we all have a notion of what an infinite string might be: a string of characters going on forever. And given a rule for alphabetization that places
"aaaa" in a list, it is the case that for any infinitely long string of characters, a finite string will appear before it in the list. It turns out that there is such a rule, typically used in the field of combinatorics, and it’s called shortlex order. That is, shorter strings appear before longer ones (in all instances), and within a fixed length we alphabetize the strings.
If we were using traditional lexicographic ordering, where
"aaaa" appears before
"bbb", we might be in trouble. If we were to make that design decision, we would need to consider what an “infinite string” might mean if any infinitely-long sequence of letters would be salted infinitely often here and there within a size-independent alphabetically-sorted sequence, rather than at the end, as though they were the irrational numbers written out in decimal form on the number line. You might find that notion interesting and useful to pursue, or it might hurt your head, or (probably) both. In any case, here I’ll use shortlex order.
What else do we get? Group Theorists seem to be concerned with commutativity and suchlike. Do we have any of that-there stuff with strings?
Seems like we have associativity for and :
Which is to say:
We have commutativity for
But not for
How about left distributivity?
How about right distributivity?
Seems to check out.
Maybe there are some weirdnesses about the additive and multiplicative identities, and/or zero? The identity operations feel about right when we “add” an infinite string, or “multiply by” (concatenate) an empty string. There doesn’t seem to be a string such that, if we “multiply” (concatenate) it, the result is gone? That is, there doesn’t seem to be a “zeroing” term under .
So something something maybe-not-a-semiring I guess?
Hapless fun with vectors and matrices
Given “addition” and “multiplication”, as you might expect there is a reasonably well-behaved version of matrix multiplication using tropical arithmetic, and indeed it’s quite useful in many graph theory and combinatorics algorithms.
Eliding for the foreseeable future what a “vector” or “matrix of strings” even means (geometrically), let’s play with them as if we were on firm theoretical ground already.
First off, let me dispense with the quotes around strings like “foo” and “bar” entirely (I’ll reserve the use of quotes as scare quotes around my slapdash terminology), and use letters to signify strings in question.
It looks like we can multiply a string “scalar” by a “vector” of strings like this:
but of course (indicating concatenation), so
That seems OK. In functional programming terms, I’ve just mapped some kind of
prepend function over this ordered collection of strings.
Let’s jump all the way to a simple matrix or two. Sticking with the tried-and-true versions for the moment:
Whew. That was fun to typeset. But it’s not intuitively obvious what is going on here, so let me try it with one-letter strings (and leaving out all the fricking quotes):
So what appears to be happening here is that the row-column application of is constructing an interesting subset of concatenations of the strings in those elements, and the final application of is dropping most of that set to return only the alphabetically earliest one.
Let me try something a little teeny bit more challenging, just to see if I have this straight:
Well that’s interesting. Seems to do something, doesn’t it?
There’s a sense of “expanding” strings combinatorially in the first phase where we concatenate several pairs of them together, but then there’s a sense of “collapse” towards smaller strings closer to the start of the alphabet in the second phase where we apply shortlex minimization.
Is this at all useful? I’m not the one to judge. It’s been fun exercise for me to typeset the things in MathJax, so it’s surely “useful” in the sense of being fun exercise.
It may also have some applications—out along the usual hand-wavy sort of path—in bioinformatics and/or finite automata grammar stuff. But surely (“surely” in scare quotes) concatenation and alphabetization have not much to do with natural language processing. I’m thinking that there may be some fun things here revolving around the combinatorics of variable naming. And to be frank I think there may be some interesting things going on here that may be useful in code generation schemes in GP, with concatenation and possibly some different partner operations not “alphabetical minimization” but perhaps something involving performance measures of the resulting concatenated strings.
Maybe I can play with that some day. But it’s fun enough for now. It’s from a dream. Maybe someday it will bubble up again in reality, now that I’ve got it out of my system.