snoyberg / mono-traversable

Type classes for mapping, folding, and traversing monomorphic containers
152 stars 63 forks source link

Can't derive MonoTraversable for newtype #163

Closed jchia closed 6 years ago

jchia commented 6 years ago

I can't derive MonoTraversable for a newtype of Text. Is this expected? This is with LTS-10.10.

code:

{-# LANGUAGE DerivingStrategies, FlexibleContexts, GeneralizedNewtypeDeriving, TypeFamilies #-}

import Data.MonoTraversable
import Data.Text

newtype MetaSig = MetaSig Text
  deriving newtype ( MonoFunctor, MonoFoldable, MonoTraversable )
type instance Element MetaSig = Char

main :: IO ()
main = pure ()

error:

    • Couldn't match representation of type ‘f Text’
                               with that of ‘f MetaSig’
        arising from the coercion of the method ‘otraverse’
          from type ‘forall (f :: * -> *).
                     Applicative f =>
                     (Element Text -> f (Element Text)) -> Text -> f Text’
            to type ‘forall (f :: * -> *).
                     Applicative f =>
                     (Element MetaSig -> f (Element MetaSig)) -> MetaSig -> f MetaSig’
      NB: We cannot know what roles the parameters to ‘f’ have;
        we must assume that the role is nominal
    • When deriving the instance for (MonoTraversable MetaSig)
snoyberg commented 6 years ago

It's a limitation of the role system, discussed here: https://stackoverflow.com/questions/49776924/newtype-deriving-issequence. A documentation PR with a link to this info would definitely be appreciated.

snoyberg commented 6 years ago

Thanks for the PR!