The type signature from Monoid mempty function is:
*Exercises Data.Monoid> :t mempty
mempty :: Monoid a => a
When I look at the type signature for function(->) type on typeclass Monoid for mempty function it becomes to:
mempty :: Monoid b => a -> b
Because a will be replace through a -> b.
mempty :: Monoid a => a
mempty :: a -> b
I did not mention that b has to be a Monoid, otherwise the mappend function would not work.
Now I have following data type
newtype Combine a b =
Combine { unCombine :: a -> b }
instance (Semigroup b)
=> Semigroup (Combine a b) where
Combine {unCombine=f} <> Combine {unCombine=g} = Combine (f <> g)
instance (Semigroup b, Monoid b)
=> Monoid (Combine a b) where
mempty = Combine mempty
mappend = (<>)
How the mempty function signature looks like for Combine?
So I am going to do exactly the same way as above:
The mempty declaration:
mempty :: Monoid a => a
a will be replace through Combine, then it becomes to:
mempty :: Combine a b
?
Monoid b.mempty :: Monoid b => Combine a b.Combine a bis just a wrapper fora -> b. So it is basicallyMonoid b => a -> bagain. The part before the=>is the typeclass, which is replaced by the corresponding type dictionary at compile time. It tells the compiler how to produce a value of typeb. - user6445533