I am learning about Algebraic DTs in haskell. What I would like to do is create a new ADT that kind of “extends” an existing one. I cannot find how to express what I would like, can someone sugest an alternative pattern or sugest a solution. I want them to be distinct types, but copying and pasting just seams like a silly solution. The code below best describes what I am seeking.
data Power =
Abkhazia |
-- A whole bunch of World powers and semi-powers
Transnistria
deriving (Eq, Show)
data Country =
--Everything in Power |
Netural |
Water
deriving (Eq, Show)
Edit: I think It need a little clarification… I want to be able to do this (in ghci)
let a = Abkhazia :: Country
and not
let a = Power Abkhazia :: Country
You need to represent them as a tree:
Edit: your extension to the question makes this a bit simpler: both the Country and Power types share some common behavior as “countries”. This suggests you use the open, extensible type class feature of Haskell to given common behaviors to the data type. E.g.
then, a type class for things both Power and Countries share:
then you can use
landareacleanly on either powers or countries. And you can extend it to new types in the future by adding more instances.