<p dir="ltr">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).</p>
<p dir="ltr">No backtracking:</p>
<p dir="ltr">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.</p>
<p dir="ltr">Backtracking:</p>
<p dir="ltr">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.</p>