I have defined a Fine-Grained Functor class (FgFunctor
) in order to apply a constraint to the type of functions that may map over my Ordered Triple datatype (OrdTriple
), which requires the contained type be orderable.
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 other Functor
s to also be FgFunctor
s, like so:
-- all regular functors are also fine-grained ones
instance Functor f => FgFunctor f a b where
fgmap = fmap
With that instance declaration, as soon as I try to use fgmap
on my OrdTriple
type, 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
-- Defined at OrdTriple.hs:15:10
instance (Ord a, Ord b) => FgFunctor OrdTriple a b
-- Defined at OrdTriple.hs:18:10
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
However, the first 'matching instance' it refers to should never apply to OrdTriple
, as OrdTriple
is not a Functor
, and so I am struggling to determine what is causing the supposed overlap.