TypeFamilies 或 GADTs 突然破坏有效代码

TypeFamilies or GADTs suddenly breaks the valid code

我的代码看起来很无辜

data Config = Config
    { cInts    :: [Int]
    , cStrings :: [String] }

instance Semigroup Config where
    c1 <> c2 = Config
        { cInts    = andCombiner cInts
        , cStrings = andCombiner cStrings }
      where
        andCombiner field = field c1 <> field c2

它编译并且工作正常。但是,如果我添加 TypeFamiliesGADTs 扩展名,我会看到非常奇怪的错误:

.../Main.hs:19:22: error:
    • Couldn't match type ‘Int’ with ‘[Char]’
      Expected type: [String]
        Actual type: [Int]
    • In the ‘cStrings’ field of a record
      In the expression:
        Config {cInts = andCombiner cInts, cStrings = andCombiner cStrings}
      In an equation for ‘<>’:
          c1 <> c2
            = Config
                {cInts = andCombiner cInts, cStrings = andCombiner cStrings}
            where
                andCombiner field = field c1 <> field c2
   |
19 |         , cStrings = andCombiner cStrings }
   |                      ^^^^^^^^^^^^^^^^^^^^

.../Main.hs:19:34: error:
    • Couldn't match type ‘[Char]’ with ‘Int’
      Expected type: Config -> [Int]
        Actual type: Config -> [String]
    • In the first argument of ‘andCombiner’, namely ‘cStrings’
      In the ‘cStrings’ field of a record
      In the expression:
        Config {cInts = andCombiner cInts, cStrings = andCombiner cStrings}
   |
19 |         , cStrings = andCombiner cStrings }
   |                                  ^^^^^^^^

此编译器错误的原因可能是什么?

这是由于 -XMonoLocalBinds-XGADTs-XTypeFamilies 暗示。您可以通过向 andCombiner 添加类型签名(或通过打开 -XNoMonoLocalBinds,尽管我 推荐这样做)来让您的代码再次编译:

instance Semigroup Config where
    c1 <> c2 = Config
        { cInts    = andCombiner cInts
        , cStrings = andCombiner cStrings }
      where
        andCombiner :: Semigroup a => (Config -> a) -> a
        andCombiner field = field c1 <> field c2

使用我链接的 GHC 文档中的术语,andCombiner 并未完全概括,因为它提到 c1c2 未关闭或导入。