[Haskell-cafe] Closed classes

Clinton Mead clintonmead at gmail.com
Thu Feb 18 08:06:59 UTC 2016


The backtracking approach would be useful. It would be nice to say:

instance (A t) => C t where ...

instance (B t) => C t where ...

in the case where you can define a C instance if you've already got a B
instance or an A instance. Indeed (aside from efficiency) you don't really
care which one.

Yes, this means if someone else defines and instance of A somewhere else in
the program then suddenly you're silently calling a different instance.
Indeed you could be using different instances in different parts of the
program. But as long as they have the same effect that shouldn't be an
issue. If they have different effects it's likely one is wrong anyway.

For (perhaps a silly) example:

class DrawRhombus drawerT where
  drawRhombus :: drawerT -> Sidelength -> Angle -> Shape

class DrawRectangle drawerT where
  drawRectangle :: drawerT -> Sidelength -> Sidelength -> Shape

class DrawSquare t where
  drawSquare :: drawerT -> Sidelength -> Shape

instance (DrawRhombus drawerT) => DrawSquare drawerT where
  drawSquare d l = drawRhombus d l (Angle 0)

instance (DrawRectangle drawerT) => DrawSquare drawerT where
  drawSquare d l = drawRectangle d l l


On Thu, Feb 18, 2016 at 9:21 AM, David Feuer <david.feuer at gmail.com> wrote:

> I was thinking about ~two notions of "closed class" yesterday, and I'm
> curious if anyone's done any work on either concept. In each case, the
> class definition is followed by *all* of its instances, and the instances
> are checked *in order* (rather than based on specificity and
> OVERLAPS/OVERLAPPABLE pragmas).
>
> No backtracking:
>
> If the instance head matches, GHC commits to it. Associated types are
> treated as closed type families, and would work just the same (I don't
> think any significant extension to the closed type family mechanism would
> be required). This seems to make a very nice parallel to the usual open
> classes with open associated types. And it lets you combine overlapping
> instances with associated types without (I believe) risking type safety.
>
> Backtracking:
>
> GHC does not commit to the instance until it has satisfied the instance
> constraints. This lets instance writers offer multiple alternative instance
> constraints. Associated types would be a good bit trickier. One option
> would be to require all instances with the same head to share a type/data
> instance. The other (much more invasive) option would be to allow the
> instance chosen to guide the type selection, which would push the
> backtracking into the type checker.
>
> _______________________________________________
> Haskell-Cafe mailing list
> Haskell-Cafe at haskell.org
> http://mail.haskell.org/cgi-bin/mailman/listinfo/haskell-cafe
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.haskell.org/pipermail/haskell-cafe/attachments/20160218/b9c83044/attachment.html>


More information about the Haskell-Cafe mailing list