In the following code, GHC cannot find the Functor instance in the Monoidal instance Monoidal .
Why doesn't the GHC deduce that, given the Applicative constraint, the Functor should be somewhere narrower? (is there a name for the argument "opportunity"?)
import Prelude hiding (Applicative (..), Monad (..)) class Functor f => Applicative f where pure :: a -> fa (<*>) :: f (a -> b) -> fa -> fb class Functor f => Monoidal f where unit::f () (*) ::fa -> fb -> f (a,b) instance Applicative f => Monoidal f where unit = pure () a * b = undefined
I know, of course, I can add an explicit restriction on Functor f for Monoidal so as not to have errors, but my question is more about why instance resolution works this way
import Prelude hiding ((*), Applicative (..), Monad (..)) class Functor f => Applicative f where pure :: a -> fa (<*>) :: f (a -> b) -> fa -> fb class Functor f => Monoidal f where unit::f () (*) ::fa -> fb -> f (a,b) instance (Applicative f, Functor f) => Monoidal f where unit = pure () a * b = (pure (,) <*> a <*> b ) instance (Monoidal f, Functor f) => Applicative f where pure x = fmap (\_ -> x) unit mu <*> mx = fmap (\(f, x) -> fx) ((mu * mx) :: f (a -> b, a))
haskell typeclass
nicolas
source share