schuelermine
schuelermine

Reputation: 2288

Why does GHCi report "A constraint must be a monotype" on this instance declaration?

I've written the following code

data I f m = forall x. f x => I (m x)

deriving instance (Show x => Show (m x)) => Show (I Show m)

with the following extensions:

-XConstrainedClassMethods
-XConstraintKinds
-XDataKinds
-XNoDatatypeContexts
-XExistentialQuantification
-XExplicitForAll
-XExplicitNamespaces
-XFlexibleInstances
-XGADTSyntax
-XGADTs
-XKindSignatures
-XMonoLocalBinds
-XMultiParamTypeClasses
-XNondecreasingIndentation
-XPolyKinds
-XPolymorphicComponents
-XRank2Types
-XRankNTypes
-XStandaloneDeriving
-XTypeInType
-XTypeFamilies
-XTypeOperators
-XTypeSynonymInstances
-XUndecidableInstances

However, GHCi complains with this error message:

<interactive>:192:19: error:
    • Illegal qualified type: Show x => Show (m x)
      A constraint must be a monotype
    • In the context: (Show x => Show (m x))
      While checking an instance declaration
      In the stand-alone deriving instance for
        ‘(Show x => Show (m x)) => Show (I Show m)’

I find that this should be possible, as it seems to be equivalent to the following:

data I f = forall x. f x => I x

deriving instance Show (I Show)

instance Show x => Show (m x) ...

So therefore

instance Show [I Show]

Upvotes: 2

Views: 179

Answers (1)

chi
chi

Reputation: 116174

The issue is that, for that to work, the type x in the context should be universally quantified, as in

data I f m = forall x. f x => I (m x)

deriving instance (forall x. Show x => Show (m x)) => Show (I Show m)

So, on the left of => we need a forall x . .... This was not allowed by GHC, until recently with GHC 8.6.1 and the QuantifiedConstraints extension.

Upvotes: 4

Related Questions