I have a newtype X
, which is basically a list of Ints. I use ClassyPrelude instead of the standard Prelude and want to derive the IsSequence class. This makes it necessary to also derive lots of other classes.
The language extension GeneralizedNewtypeDeriving should allow this (here used together with the DerivingStrategies extension). I want:
newtype X = X [Int] deriving newtype (MonoFunctor, MonoFoldable, MonoTraversable, Monoid, GrowingAppend, SemiSequence, MonoPointed, IsSequence)
full file:
{-# LANGUAGE GeneralizedNewtypeDeriving #-}
{-# LANGUAGE DerivingStrategies #-}
{-# LANGUAGE TypeFamilies #-}
{-# LANGUAGE UndecidableInstances #-}
{-# LANGUAGE NoImplicitPrelude #-}
{-# LANGUAGE FlexibleContexts #-}
module Test where
import ClassyPrelude
newtype X = X [Int] deriving newtype (MonoFunctor, MonoFoldable, MonoTraversable, Monoid, GrowingAppend, SemiSequence, MonoPointed, IsSequence)
type instance Element X = Int
(all other language extensions seem to be important)
However, this produces lots of error messages for MonoTraversable
and IsSequence
:
/path/to/file/Test.hs:13:48: error:
• Couldn't match representation of type ‘m [Int]’
with that of ‘m X’
arising from the coercion of the method ‘omapM’
from type ‘forall (m :: * -> *).
Applicative m =>
(Element [Int] -> m (Element [Int])) -> [Int] -> m [Int]’
to type ‘forall (m :: * -> *).
Applicative m =>
(Element X -> m (Element X)) -> X -> m X’
NB: We cannot know what roles the parameters to ‘m’ have;
we must assume that the role is nominal
• When deriving the instance for (MonoTraversable X)
|
13 | deriving newtype (MonoFunctor, MonoFoldable, MonoTraversable, Monoid, GrowingAppend, SemiSequence, MonoPointed, IsSequence)
| ^^^^^^^^^^^^^^^
/path/to/file/Test.hs:13:48: error:
• Couldn't match representation of type ‘f [Int]’
with that of ‘f X’
arising from the coercion of the method ‘otraverse’
from type ‘forall (f :: * -> *).
Applicative f =>
(Element [Int] -> f (Element [Int])) -> [Int] -> f [Int]’
to type ‘forall (f :: * -> *).
Applicative f =>
(Element X -> f (Element X)) -> X -> f X’
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 X)
|
13 | deriving newtype (MonoFunctor, MonoFoldable, MonoTraversable, Monoid, GrowingAppend, SemiSequence, MonoPointed, IsSequence)
| ^^^^^^^^^^^^^^^
/path/to/file/Test.hs:13:115: error:
• Couldn't match type ‘[Int]’ with ‘X’
arising from the coercion of the method ‘initMay’
from type ‘IsSequence [Int] => [Int] -> Maybe [Int]’
to type ‘IsSequence X => X -> Maybe X’
• When deriving the instance for (IsSequence X)
|
13 | deriving newtype (MonoFunctor, MonoFoldable, MonoTraversable, Monoid, GrowingAppend, SemiSequence, MonoPointed, IsSequence)
| ^^^^^^^^^^
/path/to/file/Test.hs:13:115: error:
• Couldn't match representation of type ‘m [Int]’
with that of ‘m X’
arising from the coercion of the method ‘replicateM’
from type ‘forall (m :: * -> *).
Monad m =>
Index [Int] -> m (Element [Int]) -> m [Int]’
to type ‘forall (m :: * -> *).
Monad m =>
Index X -> m (Element X) -> m X’
NB: We cannot know what roles the parameters to ‘m’ have;
we must assume that the role is nominal
• When deriving the instance for (IsSequence X)
|
13 | deriving newtype (MonoFunctor, MonoFoldable, MonoTraversable, Monoid, GrowingAppend, SemiSequence, MonoPointed, IsSequence)
| ^^^^^^^^^^
/path/to/file/Test.hs:13:115: error:
• Couldn't match representation of type ‘m [Int]’
with that of ‘m X’
arising from the coercion of the method ‘filterM’
from type ‘forall (m :: * -> *).
Monad m =>
(Element [Int] -> m Bool) -> [Int] -> m [Int]’
to type ‘forall (m :: * -> *).
Monad m =>
(Element X -> m Bool) -> X -> m X’
NB: We cannot know what roles the parameters to ‘m’ have;
we must assume that the role is nominal
• When deriving the instance for (IsSequence X)
|
13 | deriving newtype (MonoFunctor, MonoFoldable, MonoTraversable, Monoid, GrowingAppend, SemiSequence, MonoPointed, IsSequence)
|
which I cannot read (maybe it has to do with default signatures?, no idea...). Leaving out the 2 classes from the deriving clause makes the code compile.
Question: how to derive IsSequence in this case?
For several reasons, a type-alias is not possible for my use-case but I want to use the functions provided by these classes. If the deriving is not possible, it would be necessary to implement the class-methods myself.