I'm stuck with Functor instance for composition of other functors in haskell.
data Cmps f g x = Cmps {getCmps :: f (g x)} deriving (Eq,Show)
--
instance (Functor f, Functor g) => Functor (Cmps f g) where
-- fmap :: (a -> b) -> (Cmps f g) a -> (Cmps f g) b
fmap = ?
In GHC.Generics functor just derived with {-# LANGUAGE DeriveFunctor #-}
-- | Composition of functors
infixr 7 :.:
newtype (:.:) f (g :: * -> *) (p :: *) = Comp1 { unComp1 :: f (g p) }
deriving (Eq, Ord, Read, Show, Functor, Generic, Generic1)`
I can't get some like fmap h (Cmps f g x) = Cmps f g (h x) becouse of error
The constructor ‘Cmps’ should have 1 argument, but has been given 3, and something like fmap h (f (g x)) don't work too. How can I get f, g and x from Cmps type in fmap implementation?
Cmpslike it has three fields, but it only has one, of typef (g x). so the left hand side should look likefmap f (Cmps x) = ...try again with that in mind. You can also see the instance GHC generates usingDeriveFunctorwith the-ddump-derivoption. - Li-yao Xia