Search code examples
haskellalgebraic-data-typespattern-synonyms

Define Haskell type constructor equality?


I'm not meaning as a member of Eq. My code:

data Race = Terran | Zerg | Protoss deriving (Eq, Show, Read);

data MU = MU Race Race deriving (Eq, Show);

In this case I define for instance (MU Terran Zerg). I want to create a data constructor TvZ which is essentially identical in all aspects for intance so I can match a function pattern:

foo TvZ = ...

instead of having to do

foo (MU Terran Zerg) = ...

Which you can't do if you assign it to a variable as in tvZ = (MU Terran Zerg)

Another thing I want to do is making short forms as in making the type constructor T and Terran identical.

Last thing, medivac speed boost needs a minor nerf I feel.


Solution

  • What you're asking for is called a "pattern synonym", and has been proposed multiple times. It is currently not implemented. You can view the proposals here, here, and a bunch of other places (links courtesy of hammar).

    However, as a solution, this works just as well:

    foo (MU Terran Zerg) = ...
    foo (MU Zerg Terran) = foo $ MU Terran Zerg
    

    and will effectively achieve the same thing, even if it doesn't look as nice.