Using closed type families to write functions “if type A, then X, else type Y”
I want to define a function that has a specialized implementation for some types and a general implementation for all other types, with more magic than {-# SPECIALIZE #-} or rewrite rules can get me. (Roughly, I want to switch from IORef to Ptr when possible.) Closed type families look like they might provide a solution: ...but it's unclear how, from here, to build a function special :: Specialized a , or something more or less equivalent. Closed type families seem to work on the type level, but I can't figure out how to do the appropriate thing on the data level. If I were willing to explicitly list all types I didn't want to specialize for, I could write a type class and make an instance for all the other types, but I want to write something that specializes a finite list of types and then does something else for all other types. How can I accomplish something like this? You can accomplish it with overlapping classes. Here's an example for writing nub which cho...