繁体   English   中英

TypeFamilies或GADT突然中断了有效代码

[英]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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM