The easiest way to do something like this is to parameterize the Checker type with the so-called phantom type:
data Black data White
Note that none of them have any meaning. They exist only to indicate the color of the Checker .
data Checker a = Checker Int data GameBoard = GameBoard [Checker Black] [Checker White]
By the way, you don't need those brackets in the GameBoard ad.
The disadvantage of this approach is that the two colors are now different, which means that you cannot write a function that accepts, say, a list of checkers of several colors, only one color.
You can make phantom types a little more specific to keep track of valid colors:
data Black = Black data White = White data Checker a = Checker a Int type AnyChecker = Checker (Either Black White)
But this can quickly become a lot of hassle to use.
What I suspect you really want is a way to limit the range of values ββallowed in one context without making it a completely different type in all contexts. Unfortunately, in Haskell this is not entirely possible.
This is a reasonable idea, and some languages ββhave similar features. Thus, supporting such differences in a generalized way is not easy to add to an existing Haskell-type system without collateral damage, although, for example, making type inference less reliable even in code that does not use such a function.
CA McCann
source share