2

背景

有时我喜欢向我的where和绑定添加类型签名,因为它可以帮助我记住我需要我的结果是什么以及我的输入是什么。let它还有助于类型检查器在我出错时给我一个明确的错误消息。但是,它最近给我带来了问题:

给定

这是一个公认的人为的最小示例:

data Foo a = Foo a | NotFoo                  -- 3 
                                             -- 4 
data Bar a = Bar a                           -- 5 
                                             -- 6 
funct :: Bar a -> (a -> Foo a) -> Bar a      -- 7 
funct (Bar x) fn = loop (Bar x, Foo x)       -- 8 
  where                                      -- 9 
    loop :: (Bar a,Foo a) -> Bar a           -- 10
    loop (x, (Foo y)) = loop (Bar y, fn y)   -- 11
    loop (x, NotFoo) = x                     -- 12

GHC 吐出一个错误:

min_test.hs:11:41: error:
    * Couldn't match expected type `a' with actual type `a1'
      `a1' is a rigid type variable bound by
        the type signature for:
          loop :: forall a1. (Bar a1, Foo a1) -> Bar a1
        at min_test.hs:10:13
      `a' is a rigid type variable bound by
        the type signature for:
          funct :: forall a. Bar a -> (a -> Foo a) -> Bar a
        at min_test.hs:7:10
    * In the first argument of `fn', namely `y'
      In the expression: fn y
      In the first argument of `loop', namely `(Bar y, fn y)'
    * Relevant bindings include
        y :: a1 (bound at min_test.hs:11:19)
        x :: Bar a1 (bound at min_test.hs:11:11)
        loop :: (Bar a1, Foo a1) -> Bar a1 (bound at min_test.hs:11:5)
        fn :: a -> Foo a (bound at min_test.hs:8:15)
        funct :: Bar a -> (a -> Foo a) -> Bar a (bound at min_test.hs:8:1)

TL;DR = GHC 无法推断a第 7 行的类型签名中的 与第 11 行的类型签名中的 相同a

如果我注释掉第 11 行,每个人都很高兴。

问题

loop有没有办法以GHC / Haskell 知道它在两个地方都相同的方式编写类型签名a

4

0 回答 0