I was reading a classic Math Overflow thread the other day about jokes “in the sense of Littlewood.” The original joke was the Euler product formula but there are several other cute entries. As a CS major who has fallen under Klaus’s influence,1 one that I found particularly funny is the following: given a (not necessarily commutative) ring where is invertible, also exists, and is given by the formula The joke is to derive this using a neat trick with a power series, but as a comment points out, the result actually makes a lot of sense if you think of these things as regular expressions over , or more generally as Kleene algebras. In a way, you can think of as (i.e. Kleene star): to play a little fast and loose with notation. Likewise, one can think of as the empty string , since they form the identities for multiplication and string concatenation respectively.2 Then the result is remarkably obvious: In my opinion, this is rather funny.
This “joke” exploits the “convergence” of a geometric series in a setting where such convergence has no obvious meaning: abstract algebra and regular expressions.3 Paul Halmos once remarked on the utility of exactly this trick in an expository article,4 calling the geometric series, along with quotient structures and eigenvalues (and more generally fixed points), an “element of mathematics” that keeps cropping up in odd places where one least expects it. In fact, he used the very same example with .
In a similar spirit, I propose the following joke in the realm of type theory, based on things that I picked up from 98-317 (Hype for Types). We can impose some algebraic structure on types in programming languages; for instance, forming a tuple of types and is analogous to taking their Cartesian product .5 I would assume that this is what inspires the Standard ML syntax for tuples:
type ('t1, 't2) product = 't1 * 't2
val _ : (int, real) product = (15, 1.50)
Similarly, given types and , we can interpret the type “something from or something from ” as their tagged union, or with a more algebraic flavor, as their sum .6 In Standard ML:
datatype ('t1, 't2) sum = Left of 't1
of 't2
| Right
val _ : (string, bool) sum = Left "foo"
val _ : (string, bool) sum = Right false
An intuitive way to think about this is that if is inhabited by values and is inhabited by values, then is inhabited by values, and is inhabited by values.
With these preliminaries aside, let’s try to express the type of a list algebraically. An (i.e. a list whose elements are of type ) is really just either an empty list, or a singleton list, or a pair of elements, or a triple of elements…essentially, a list is a sum of products:
datatype 'a list = Empty of unit
of 'a
| Single of 'a * 'a
| Pair of 'a * 'a * 'a
| Triple of 'a * 'a * 'a * 'a
| Quadruple | ...
And now using our representation of product types and sum types, we can do the very curious thing of thinking of a list as a power series: Now let’s step back for a moment and apply the usual trick of rewriting the geometric series using the formula: Now rearranging both sides, we see that this is the same as saying But let’s write this out in code:
datatype 'a list = Nil of unit
of 'a * 'a list | Cons
This is precisely the canonical recursive definition of a list!
As a side note, Scott Aaronson has a blog post containing a few funny proofs, although this sense of humor is slightly different from the one above. Actually, I think I was asked a few of the questions in his blog post in a complexity theory class!
Klaus Sutner teaches computational discrete math at CMU, and he is notably enthusiastic about the role of abstract algebra in computation.↩︎
Or more formally and with less notational abuse, . Notice that this plays well with the Kleene star idea: if is the regular expression (the identities for addition and set union respectively), we see that , and likewise .↩︎
Though funnily enough, I recently stumbled across a blog post on the permanence of identities, which uses category theory and model theory to justify such manipulations.↩︎
Halmos, P.R. “Does mathematics have elements?”. The Mathematical Intelligencer 3, 147-153 (1981).↩︎
As an aside, this also explains the name of the unit
type, which is inhabited by the single value ()
: it acts as the multiplicative unit.↩︎
If you’ve studied category theory before, this is just the binary coproduct.↩︎
Comments