Restructuring an OOP datatype into Haskell types : <p>Coming from an OOP background, Haskell's type system and the way data constructors and typeclasses interact is difficult to conceptualize. I can understand how each are used for simple examples, but some more complication examples of data structures that are very well-suited for an OOP style are proving non-trivial to translate into similarly elegant and understandable types.</p>

<p>In particular, I have a problem with organizing a hierarchy of data such as the following.</p>

<p><img src="https://chitu.okoli.org/media/pro/teach/oop/uml.gif" alt="data"></p>

<p>This is a deeply nested hierarchical inheritance structure, and the lack of support for subtyping makes it unclear how to turn this structure into a natural-feeling alternative in Haskell. It may be fine to replace something like <code>Polygon</code> with a sum data type, declaring it like</p>

<pre><code>data Polygon
= Quad Point Point
| Triangle Point Point Point
| RegularNGon Int Radius
| ...
</code></pre>

<p>But this loses some of the structure, and can only really satisfactorily be done for one level of the hierarchy. Typeclasses can be used to implement a form of inheritance and substructure in that a <code>Polygon</code> typeclass could be a subclass of a <code>Shape</code>, and so maybe all <code>Polygon</code> instances have implementations for <code>centroid :: Point</code> and also <code>vertices :: [Point]</code>, but this seems unsatisfactory. What would be a good way of capturing the structure of the picture in Haskell?</p>
