I'm writing a type where I want to be able to change the behavior of a method based on the typeclass instantiations of internal types.
As an example, consider this:
class Reduceable a where
reduce :: a -> a
-- Other methods not relevant here
instance Reduceable [[a]] where
-- Reduce a list by dropping empty lists in the front
reduce = dropWhile null
-- Other methods implemented
instance Reduceable [a] => Reduceable [[a]] where
-- Reduce a list of reduceable things by dropping empty lists in the front
-- and also reducing the elements of the list
reduce = dropWhile null . map reduce
-- Other methods identical to Reduceable [[a]]
In this case, I could use OverlappingInstances to make this code work. However, the other functions are somewhat involved in their implementation and do not change based on the type, and so I'd rather not have to implement them multiple times (which, by my understanding of overlapping instances, would be required).
Is there a way I can get what I want in Haskell?