Category theory illustrated by RA2 prism towers
Tags: math, trolololol
Published: 2018-11-30 13:43:40

---
title: Category theory illustrated by RA2 prism towers
published: true
description: 
tags: #math #trolololol
---

Although I've already written a [somewhat formal post about category theory](https://dev.to/drbearhands/functors-monads-and-better-functions-26f3), I realized a much more evocative way to think about category theory is using Red Alert 2's prism towers.

![An image of prism towers](http://www.sztab.com/tapety/galeria/command-and-conquer-red-alert-2-5.jpg)
<figcaption>Prism towers defending the white house in RA2</figcaption>

For those who have never player RA2: (a) we cannot be friends and (b) prism towers would shoot colorful death rays and had the ability to fire through each other, so that if one tower was in range of another tower, it was also in range of everything that was in range of the second tower (see picture).

So, units and buildings in RA2 are objects in CT, and an arrows from CT mean "being in range of a prism tower".

Now we have that if prism tower B is in range of prism tower A, and soviet tank C is in range of prism tower B, prism tower A can shoot soviet tank C. If we assume a prism tower is in range of itself we can call a group of prism towers (and their targets) a category, as we have composition and identity.

## Prism tower functors

Networks of prism towers can also have functors. Moving, rotating and mirroring the network does not change how it is connected. But neither does collapsing (part of) the network to a single point. _Now I know placing buildings on top of each other is cheating, but this is hard countered by Kirov airships so I will allow it._

Any transformation that does not change the connectivity of the network is a functor, even if this somehow reduces the number of towers. _E.g. when collapsing the network to a line of single point you could reduce the towers occupying the same square after transformation to 1._

![mirrored prism network](https://thepracticaldev.s3.amazonaws.com/i/ohgo88j43zvvx4e5udg4.png)
<figcaption>Sovjets ground forces still die when attacking form the other side!</figcaption>

So if a functor `F` means "moving 5 blocks north", prism tower `F a` is 5 blocks north of prism tower `a`.

In FP we require `map : (a -> b) -> F a -> F b`, expressed in prism towers: if prism tower `a` can reach prism tower `b`, then the transformed prism tower `F a` can reach transformed prism tower `F b`. _There's also something about preservation of identity and composition but I will ignore that for now, mostly because I'm having trouble thinking about a transformation that does_ not _do that._

## Anti-air monads.

Monads are a little bit trickier. They must be endofunctors, meaning there must be a connection between each prism tower and it's transformed counterpart. Plus applying the transformation multiple times should have the same result as applying it once (up to isomorphism). The only spatial transformation I can think of that does that is identity, but that's cheating.

So suppose instead, we upgrade the prism towers to shoot upwards _(fuck you Kirov airship!)_. Any regular prism tower can connect to an AA prism tower in the same position, so it's an endofunctor, and setting `anti_air` to true twice is no different than doing it once, which means it's a Monad.

So let's check out the FP definition of a monad: `andThen : (a -> M b) -> M a -> M b`. 
Expressed in prism towers: if prism tower `a` can reach AA prism tower `M b`, then `a` will still be able to reach `M b` after being upgraded with AA (`M a`).


PS.: I've edited my last article, so you might want to give it another look if it was too much previously.