Can I specialize a Foldable instance method based on class a?

I have a container Foldablefor as. One of the methods provided by class Foldablein BasicPreludeis elem :: (Eq a) => a -> t a -> Bool. Now for my container, I can implement elemmore efficiently than the default if mine ais an instance Ord, but only in this case.

Now I would like to have a more efficient one elemavailable, if possible, preferably with the same signature (that is, "transparently"). However, I can only see one way to do this, which requires an (Ord a)instance restriction Foldablefor my container, which does not make sense in the general case.

Is it possible for me to have specialized elemonly for cases when it ais Ord, and common elemfor others anyway? I do not mind using GHC extensions if necessary.

+4
source share
1 answer

No. This is a long-standing wart that classes of the standard type are not compatible with types that require limited parameters.

monotraversable library, , , MonofoldableOrd , . monotraversable Foldable/Traversable, ( ByteString Text) ( Set).

TypeFamilies, , , , , .

+4

All Articles