DEV Community

DrBearhands
DrBearhands

Posted on

Category theory illustrated by RA2 prism towers

Although I've already written a somewhat formal post about category theory, 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

Prism towers defending the white house in RA2

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

Sovjets ground forces still die when attacking form the other side!

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.

Top comments (1)

Collapse
 
mazentouati profile image
Mazen Touati

I'm a simple man, I see Red Alert 2 I click 🦄