[Haskell-cafe] Partial instance of a class

Jeroen Bransen jeroen at chordify.net
Mon Feb 26 15:53:45 UTC 2018


Hi Haskell cafe,

Is there any standard or guideline for making a "partial" instance of a 
class, by which I mean implementing some methods of a class, but not all 
of them? In my concrete case I've got some type X which is almost an 
Arrow, except that I cannot lift any function a -> b to my type (of 
course I can for some a and b), so I cannot give a sensible 
implementation for arr. I can however give sensible implementations for 
the other methods in the Arrow class, and I'd like to use them (and 
possibly derived combinators) in other places.

I see three possible solutions for this situation, I think I've seen at 
least the first two being used in well-known packages, but I couldn't 
find them anymore:

1. Make a class instance, and for the methods you don't implement put an 
'error' call
2. Create functions with names from the class and with types specialised 
to your type, so that you can use it as if the instance was there. I.e. 
provide a function first :: X a b -> X (a, c) (b, c), which should be 
imported qualified to avoid name conflicts with the real Arrow class. 
The drawback of this is of course the conflicting names, but also the 
fact that derived combinators cannot be used.
3. Create functions with different names, to also avoid name clashes, 
i.e. firstX :: X a b -> X (a, c) (b, c).

My preference would be to pick 1., but it feels quite bad in the same 
time. Any suggestions?

Jeroen


More information about the Haskell-Cafe mailing list