Unable to fetch superclass

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)) 
+7
haskell typeclass
source share
1 answer

Looks like a mistake. Here is a minimal file that shows the problem, and does not rely on any fraud with renaming Prelude or undefined s.

 {-# LANGUAGE FlexibleInstances #-} {-# LANGUAGE UndecidableInstances #-} class A x class A x => B x class A x => C x instance B x => C x 

I recommend submitting an error to the GHC error tracker with this file (or very similar to it); you need to find out that B x means A x .

+3
source share

All Articles