2

我有一个 newtype X,它基本上是一个整数列表。我使用 ClassyPrelude 而不是标准 Prelude 并希望派生 IsSequence 类。这使得也有必要派生许多其他类。

语言扩展 GeneralizedNewtypeDeriving 应该允许这样做(这里与 DerivingStrategies 扩展一起使用)。我想:

newtype X = X [Int] deriving newtype (MonoFunctor, MonoFoldable, MonoTraversable, Monoid, GrowingAppend, SemiSequence, MonoPointed, IsSequence)

完整文件:

{-# 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

(所有其他语言扩展似乎都很重要)

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)
   |                                              

我无法阅读(也许它与默认签名有关?,不知道......)。从派生子句中省略 2 个类会使代码编译。

问题:在这种情况下如何推导出 IsSequence?

出于多种原因,我的用例无法使用类型别名,但我想使用这些类提供的功能。如果无法派生,则有必要自己实现类方法。

4

0 回答 0