Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to organize Haskell modules with instances: stick to data type vs type class?
    primarykey
    data
    text
    <p>The general question is which module structure is more convenient when adding instances for existing objects? Which pros and cons there are?</p> <p>Let's say I want to add NFData instance for Seq type. I can place it in:</p> <ul> <li><code>Data.Sequence.Extra</code> (as the same thing is done in the <a href="https://github.com/coreyoconnor/vty/blob/master/src/Data/Sequence/Extra.hs">vty</a> package)</li> <li><code>Data.Sequence.Instances.NFData</code> (more precise)</li> <li><code>Control.DeepSeq.Instances</code></li> <li><code>Control.DeepSeq.Instances.Sequence</code></li> </ul> <p>It's the case when I don't own neither the type class nor the data type. The other common situation is when I own a type type class and want to add instances for data type(s) from some "heavy" package from Hackage, like OpenGL. Let's say the type class I designed is very light and has no direct relation to OpenGL. I don't want my type class depend on "heavy" package, so I want to place OpenGL instances in a separate module (it's my intuitive feeling, if you have other opinion, let's discuss it). So, what this module should be:</p> <ul> <li><code>MyClass.Instances.OpenGL</code></li> <li><code>Graphics.Rendering.OpenGL.Extra</code> (together with instances for other classes)</li> <li><code>Graphics.Rendering.OpenGL.Instances.MyClass</code></li> </ul> <p>What is more flexible solution? At some point OpenGL can be replaced with other library, or MyClass can be replaced too. Are there any subtle nuances?</p> <p>Also, which scheme is better if choose <code>MyClass.Instances</code> variant:</p> <ul> <li><code>MyClass.Class</code> module for the class itself and basic instances and <code>MyClass</code> module reexports it (and maybe <code>MyClass.Instances</code>)</li> <li><code>MyClass</code> module for the class and basic instances, and <code>MyClass.All</code> reexports everything</li> <li><code>MyClass</code> module for the class and basic instances and no module for reexporting.</li> </ul>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. COWhat you are doing is called "orphan instances" and is considered evil because they make it easy to define multiple instances of the same class for the same type and make otherwise good libraries incompatible with each other. There was that other question discussing them in detail: http://stackoverflow.com/questions/3079537/orphaned-instances-in-haskell .
      singulars
    2. COFor Rotsor's problem - a "general" library shouldn't be in the business of implementing instances for classes / datatypes that it doesn't define. It's fine for a private library or an executable / application to do this - but a public library shouldn't do it. For the original problem - the problem is not really where in the namespace the instance is defined but that there can be more than one library providing an instance. The best solution is probably to have "instances" packages on Hackage where developers provide canonical instances for classes / datatypes outside Base.
      singulars
    3. CO@Rotsor, that's a good point (while there is no common opinion at the comment at the link). Does it follow from there that when I own a type class, I shall tie instances to it?
      singulars
 

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