Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Just ignore the Subtype class for a second and examine the type of the coerce function you are writing. If the <code>a</code> is a <code>Man</code> and the <code>b</code> is an <code>Animal</code>, then the type of the coerce function you are writing should be:</p> <pre><code>coerce :: Man -&gt; Animal </code></pre> <p>This means that all you have to do is write a sensible function that converts each one of your <code>Man</code> constructors (i.e. <code>C | D | E | K</code>) to a corresponding <code>Animal</code> constructor (i.e. <code>A | B</code>). That's what it means to subtype, where you define some function that maps the "sub" type onto the original type.</p> <p>Of course, you can imagine that because you have four constructors for your <code>Man</code> type and only two constructors for your <code>Animal</code> type then you will end up with more than one <code>Man</code> constructor mapping to the same <code>Animal</code> constructor. There's nothing wrong with that and it just means that the coerce function is not reversible. I can't comment more on that without knowing exactly what those constructors were meant to represent.</p> <p>The more general answer to your question is that there is no way to automatically know which constructors in <code>Man</code> should map to which constructors in <code>Animal</code>. That's why you have to write the coerce function to tell it what the relationship between men and animals is.</p> <p>Note also that there is nothing special about the 'Subtype' class and 'coerce' function. You can just skip them and write an 'manToAnimal' function. After all there is no built-in language or compiler support for sub-typing and Subtype is just another class that some random guy came up with (and frankly, subtyping is not really idiomatic Haskell, but you didn't really ask about that). All that defining the class instance does is allow you to overload the function <code>coerce</code> to work on the <code>Man</code> type.</p> <p>I hope that helps.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload