I have started this a new question as it became a follow-on from my previous question.
If I have two data types which are composed of similar constructors:
data A = X | Y | Z
data B = X | Y
is there no way I can somehow represent this as:
data A = C | Z
data B = C
data C = X | Y
if you can see what I am doing- I am trying to group the X | Y into one data type, which can then be used by multiple other data types. I cannot seem to get the compiler to allow this, or if it does, I cannot pattern-match against the X or Y, only the C??
I get the error message that C has been declared multiple times.
I thought I could maybe use types, but they do not allow multiple typings.
EDIT
Even if I declare the long way (like below), it still won’t compile and says X and Y have multiple declarations:
data A = X | Y | Z
data B = X | Y
Not only can’t you do this, you also can’t do your first option – i.e. you can’t have two types in the same module that both have constructors named
XandY.If you could do this, what should the type of
Xbe –C,AorB? The most obvious answer would beC, but then you wouldn’t be able to use it in a context where anAor aBare required (note that Haskell has no subtyping), so that would defeat the purpose of the whole construct.The best you can do is to wrap C in a constructor of A and B, i.e.:
Then you could wrap a C with either the
ACor theBCconstructor to create a value of typeAorBrespectively.