jcreed blog > What Type is a Deck of Cards?

What Type is a Deck of Cards?

Consider a standard deck $D_\mathsf{French}$ of French-suited playing cards. It has 52 cards. Or, if we include the jokers, we can talk about the extended deck $D^+_\mathsf{French}$, containing 54 cards. We could also consider the Rider-Waite Tarot Deck It has 22 "major arcana" cards, and the "minor arcana" resembles the standard playing cards somewhat, albeit with different suits. I'm sweeping under the rug a tremendous amount of historical variations of these sets. The idea I want to focus on is

Decks of Cards are Finite Types

On the one hand, this is obvious. A deck of cards is a finite collection of things. Any finite type is, up to isomorphism, determined by its cardinality. But the interesting things about how people think about a given card deck design are obscured by thinking about it up to isomorphism. So instead think a little more intensionally: let's consider what type-theoretic operations are used to build up a deck of cards.

For the standard french-suited 52-card deck, it is clear that we arrive at it by taking the product type of two types, the 13-element type of ranks, and the 4-element type of suits. \[\mathsf{Rank}_\mathsf{French} = \{A, K, Q, J, 10, 9, 8, 7, 6, 5, 4, 3, 2\}\] \[\mathsf{Suit}_\mathsf{French} = \{\heartsuit, \diamondsuit, \clubsuit, \spadesuit\}\] \[ D_\mathsf{French} = \mathsf{Rank}_\mathsf{French} \x \mathsf{Suit}_\mathsf{French}\] If we introduce a 2-element type of jokers, \[J = \{ \mathsf{RedJoker}, \mathsf{BlackJoker} \}\] then we can explain the extended deck as a sum type. \[ D^+_\mathsf{French} = (\mathsf{Rank}_\mathsf{French} \x \mathsf{Suit}_\mathsf{French}) + J\] The Rider-Waite Tarot is also a combination of sums and products. \[\mathsf{Rank}_\mathsf{Tarot} = \{ K, Q, N, P, 10, 9, 8, 7, 6, 5, 4, 3, 2, A\}\] \[\mathsf{Suit}_\mathsf{Tarot} = \{\mathsf{Pentacle}, \mathsf{Cup}, \mathsf{Wand}, \mathsf{Sword}\}\] \[\mathsf{MinorArcana}_\mathsf{Tarot} = \mathsf{Rank}_\mathsf{Tarot} \x \mathsf{Suit}_\mathsf{Tarot}\] \[\mathsf{MajorArcana}_\mathsf{Tarot} = \{0, I, II, III, \ldots, XIX, XX, XXI\}\] \[D_\mathsf{Tarot} = \mathsf{MinorArcana}_\mathsf{Tarot} + \mathsf{MajorArcana}_\mathsf{Tarot}\]

Other Type Constructors

The reader may object that this is barely interesting at all. That is exactly my point! I intend to draw attention to the triviality of the type structure of these decks in order to ask:
What else could we be doing instead? What other type could a deck of cards have?

Function Types

Imagine we had suits $S = \{\mathsf{Red}, \mathsf{Green}, \mathsf{Blue}\}$ and quantities $Q = \{0,1,2\}$, and made a deck of cards whose type was $S \to Q$. This would mean something like:

Dependent Product Types

The card game Set has a structure that could be thought of as a dependent product type. Say \[ \mathsf{Attribute} = \{ \mathsf{Color}, \mathsf{Number}, \mathsf{Shape}, \mathsf{Shading} \}\] \[ \mathsf{Values} : \mathsf{Attribute} \to \mathsf{Type}\] \[ \mathsf{Values}(\mathsf{Color}) = \{\mathsf{Red}, \mathsf{Green}, \mathsf{Blue}\} \] \[ \mathsf{Values}(\mathsf{Number}) =\{\mathsf{1}, \mathsf{2}, \mathsf{3}\} \] \[ \mathsf{Values}(\mathsf{Shape}) =\{\mathsf{Round}, \mathsf{Diamond}, \mathsf{Squiggly}\} \] \[ \mathsf{Values}(\mathsf{Shading}) =\{\mathsf{Solid}, \mathsf{Shaded}, \mathsf{Hollow}\} \] and then type of the deck of Set cards is the type \[ \prod_{\alpha \in \mathsf{Attribute}} \mathsf{Values}(\alpha) \]

Dependent Sum Types and Multinomial Types

Decktet has cards with varying numbers of suits per card.

One idea that might work for the specifically the multi-suited ranked cards in Decktet is to set \[\mathsf{Suit} = \{\mathsf{Moon}, \mathsf{Sun}, \mathsf{Wave}, \mathsf{Leaf}, \mathsf{Wyrm}, \mathsf{Knot}\}\] \[\mathsf{Rank} = \{2,3,4,5,6,7,8,9\}\] and then specify exactly the function \[\delta : \mathsf{Rank} \to {\mathsf{Suit} \choose 2,2,2}\] which maps each rank to which partition of 6 into three pairs it corresponds to.

The codomain --- written to suggest the multinomial coefficient --- is meant to be the set of partitions of the set of suits into three pairs. We could express this by postulating a function \[ \mathsf{Part} : {\mathsf{Suit} \choose 2,2,2} \to \mathsf{Type} \] that for each partition $p$ gives the (three-element) type $\mathsf{Part}(p)$ of parts of $p$, and \[ \_{\in}\_ : \textcolor{gray}{ \prod_{p : {\mathsf{Suit} \choose 2,2,2}} } \mathsf{Suit}\x \mathsf{Part}(p) \to \mathsf{Type} \] which, given a partition $p$, (implicitly omitted in applications of $\in$) a suit $\sigma$, and a part $q$ of $p$, yields a type $\sigma \in q$ that has exactly one inhabitant iff indeed $\sigma$ is in $q$, and is empty otherwise.

Given all this machinery, the type of ranked cards in Decktet could be said to be \[\sum_{r \in \mathsf{Rank}} \sum_{q \in \mathsf{Part}(\delta(r))} \sum_{\sigma \in \mathsf{Suit}} \sigma \in q \]

Types with Other Algebraic Structure

Arguably it is already the case that we understand \[\mathsf{Rank}_\mathsf{French} = \{A, K, Q, J, 10, 9, 8, 7, 6, 5, 4, 3, 2\}\] to come equipped with a total order \[A \ge K \ge Q \ge J \ge 10 \ge 9 \ge 8 \ge 7 \ge 6 \ge 5 \ge 4 \ge 3 \ge 2\] or, perhaps, depending on the game being played, an alternative order such as \[ K \ge Q \ge J \ge 10 \ge 9 \ge 8 \ge 7 \ge 6 \ge 5 \ge 4 \ge 3 \ge 2 \ge A\] But why stop at total orders? Would it be interesting to have a type that comes with:

Inductive Datatypes

What might it mean for a deck of cards to be labelled by lists, abstract syntax trees, monadic expressions?