Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Yes, it would probably be useful. In fact, all of the slightly incompatible versions of it would be useful! Which is kinda the problem.</p> <p>It's not clear what such a class would even mean, which makes it hard to actually use, because inevitably you'll hit types where there are multiple choices of a default value, and if it's not <em>immediately</em> clear which one the instance provides, you pretty much lose all benefit of having the class in the first place.</p> <p>A few examples:</p> <ul> <li><p>For <code>Monoid</code> instances, you'd obviously expect the identity element to be the default. But now you're back to the problem of so many types having two or more sensible <code>Monoid</code> instances. Is the default <code>Integer</code> 0 or 1? For <code>Monoid</code>, the standard library uses <code>newtype</code> wrappers, but those are clumsy and make it difficult to work with the wrapped types--with <code>Monoid</code> it works okay because you get access to <code>mconcat</code> and such, but you can't <em>do</em> anything interesting with just a default value.</p></li> <li><p>For <code>Functor</code>-like types with an "empty" value, that gives an obvious default. This is what <code>MonadPlus</code> and <code>Alternative</code> are doing... and also overlaps with <code>Monoid</code>, and if memory serves me there's at least one type where those three instances aren't identical. Which do you pick, when there's more than one choice? Consider lists: You can blindly append them, giving an arbitrary <code>Monoid</code>, with the empty list as identity; but for lists of <code>Monoids</code> you can also <code>zipWith mappend</code>, giving a lifted monoid with <code>repeat mempty</code> as the identity. Many functors have analogous <code>Monoid</code> instances, but not always both--so whichever you pick for lists, you'll be conceptually inconsistent with some other <code>Functor</code>!</p></li> <li><p>For unit types like <code>()</code>, it's not hard to pick a default! But what about enumerations? Does it make sense to pick the first constructor? Sometimes, but not always. How will people using the class know?</p></li> <li><p>What about <code>Bounded</code>? If none of the above applies, you could use <code>minBound</code>. But some of the above types could be <code>Bounded</code> as well, so you'll confuse matters if their default isn't their minimum value.</p></li> </ul> <p>Basically, there's just enough overlap that it <em>seems</em> to make sense... but really, you've got at least three different type classes in mind here, and trying to unify them is probably not as helpful as it seems at first.</p> <hr> <p>If you can pin things down a little better and give a clear, consistent semantic interpretation of a "default" value, <em>without</em> just reinventing <code>Monoid</code> or another existing class, such that the type class is easy to use without having to stop and think about what "default" gets chosen, great! But I wouldn't get my hopes up on making it work.</p> <p>That said, the one obviously sensible case that's not covered by any standard type class is singletons like <code>()</code>. Most of the time these aren't terribly useful--for obvious reasons!--which is probably why there isn't such a class. One place where such a class is <em>extremely</em> useful, though, is when you're doing something involving type-level shenanigans, because such a type represents a single value at both the type and term level--so a class for such types lets you manipulate type-level values freely, then conjure up the term that goes with it, so you can pass it to some other function that might, e.g., select a type class instance based on it. For that reason, I have <a href="https://github.com/isomorphism/typewriter/blob/master/Data/Typewriter/Demote.hs">a class along those lines in my perpetually-incomplete type-hackery library</a>, e.g.:</p> <pre><code>class TermProxy t where term :: t -- This makes explicit the lexical pun of () having type (). instance TermProxy () where term = () instance (TermProxy a, TermProxy b) =&gt; TermProxy (a, b) where term = (term, term) </code></pre> <p>I doubt such a class is very useful in any other context, though.</p>
    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. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

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