I defined the Fine-Grained Functor ( FgFunctor) class to apply a constraint to the type of functions that can be displayed by my ordered triple data type ( OrdTriple), which requires the contained type to be ordered.
import Data.List (sort)
-- 'fine-grained' functor
class FgFunctor f a b where
fgmap :: (a -> b) -> f a -> f b
data OrdTriple a = OrdTriple a a a deriving Show
instance (Ord a, Ord b) => FgFunctor OrdTriple a b where
fgmap f (OrdTriple n d x) = OrdTriple n' d' x'
where [n', d', x'] = sort [f n, f d, f x]
main :: IO ()
main = do
let test = fgmap (* 10^4) $ OrdTriple 1 6 11
print test
The code works fine until I define all the others Functoralso be FgFunctors, for example:
-- all regular functors are also fine-grained ones
instance Functor f => FgFunctor f a b where
fgmap = fmap
With this instance declaration, as soon as I try to use fgmapin my type OrdTriple, the compiler complains about overlapping instance declarations
Overlapping instances for FgFunctor OrdTriple b0 b0
arising from a use of ‘fgmap’
Matching instances:
instance Functor f => FgFunctor f a b
instance (Ord a, Ord b) => FgFunctor OrdTriple a b
In the expression: fgmap (* 10 ^ 4)
In the expression: fgmap (* 10 ^ 4) $ OrdTriple 1 6 11
In an equation for ‘test’:
test = fgmap (* 10 ^ 4) $ OrdTriple 1 6 11
" ", , OrdTriple, OrdTriple Functor, , .