Just ignore the subtype class for a second and examine the type of coerce function you are writing. If a
is Man
and b
is Animal
, then the type of coerce function you write should be:
coerce :: Man -> Animal
This means that all you have to do is write a reasonable function that converts each of your Man
constructors (i.e. C | D | E | K
) to the corresponding Animal
constructor (i.e. A | B
). This means that it means a subtype where you define some function that maps the type "under" to the original type.
Of course, you can imagine that since you have four constructors for your Man
type and only two constructors for your Animal
type, then you will get more than one Man
constructor constructor for the same Animal
constructor. There is nothing wrong with that, and it just means that the coercive function is not reversible. I can no longer comment on this without knowing what these constructors mean.
A more general answer to your question is that there is no way to automatically find out which constructors in Man
should map to constructors in Animal
. That's why you need to write a coercive function to tell her what the relationship between men and animals is.
Also note that there is nothing special about the Subtype and Coercion features. You can simply skip them and write the "manToAnimal" function. After all, there is no built-in language or compiler support for typing, and a subtype is another class that some random guy came across (and, frankly, subtyping is not really idiomatic for Haskell, but you really didn't ask about it) , Everything that defines an instance of the class allows you to overload the coerce
function to work with the Man
type.
I hope this helps.
source share