Strange Behavior with QuanitifiedConstraints, DerivingStrategies, and GND
Summary
Code that should compile whilst using a newtype deriving strategy fails to compile unless using Standalone deriving.
Steps to reproduce
The following program fails to compile:
{-# LANGUAGE DerivingStrategies #-}
{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE GeneralizedNewtypeDeriving #-}
{-# LANGUAGE MonoLocalBinds #-}
{-# LANGUAGE QuantifiedConstraints #-}
{-# LANGUAGE StandaloneDeriving #-}
module Main where
import Control.Monad.Reader
import Data.Coerce
class (forall a b. Coercible a b => Coercible (f a) (f b)) => CoerceRole f
instance (forall a b. Coercible a b => Coercible (f a) (f b)) => CoerceRole f
data Env = Env
data Message = Message
class Run a where
run :: forall m. (MonadReader Env m, CoerceRole m) => Message -> a -> m a
data Inner = Inner
instance Run Inner where
run _ = pure
newtype Wrapper = Wrapper Inner
deriving newtype Run
-- deriving newtype instance Run Wrapper
main :: IO ()
main = pure ()
The observed error is:
/private/tmp/CoerceBug/app/Main.hs:28:20-22: error:
• Couldn't match representation of type ‘m Inner’
with that of ‘m Wrapper’
arising from the coercion of the method ‘run’
from type ‘forall (m :: * -> *).
(MonadReader Env m, CoerceRole m) =>
Message -> Inner -> m Inner’
to type ‘forall (m :: * -> *).
(MonadReader Env m, CoerceRole m) =>
Message -> Wrapper -> m Wrapper’
NB: We cannot know what roles the parameters to ‘m’ have;
we must assume that the role is nominal
• When deriving the instance for (Run Wrapper)
|
28 | deriving newtype Run
| ^^^
However, commenting out deriving newtype Run
and uncommenting deriving newtype instance Run Wrapper
will cause compilation to succeed.
Expected behavior
Both programs should be considered valid programs.
Environment
- GHC version used: ghc-8.10.7
Optional:
- Operating System: macOS 11.6
- System Architecture: x86_64