0

我有这个功能foo

foo :: [a] -> (a -> b) -> [b]
foo [] f = []
foo (x:xs) f = foo xs f

以及它必须满足的以下两个属性:

prop_1 :: [Int] -> Bool
prop_1 xs = foo xs id == xs 

prop_2 :: [Int] -> (Int -> Int) -> (Int -> Int) -> Bool
prop_2 xs f g = foo (foo xs f) g == foo xs (g . f)

当我尝试使用 quickCheck 测试该功能时,我收到以下错误:

 Ambiguous type variable 't0' arising from a use of '=='
      prevents the constraint '(Eq t0)' from being solved.
      Probable fix: use a type annotation to specify what 't0' should be.
      These potential instances exist:
        instance (Eq a, Eq b) => Eq (Either a b)
          -- Defined in 'Data.Either'
        instance Eq GeneralCategory -- Defined in 'GHC.Unicode'
        instance Eq Ordering -- Defined in 'ghc-prim-0.5.0.0:GHC.Classes'
        ...plus 24 others
        ...plus 107 instances involving out-of-scope types
        (use -fprint-potential-instances to see them all)
      In the expression: foo (foo xs f) g == foo xs (g . f)
      In an equation for 'prop_2':
          prop_2 xs f g = foo (foo xs f) g == foo xs (g . f)
Failed, modules loaded: none.

我不确定为什么会收到此错误以及如何解决它。任何见解都值得赞赏。

4

1 回答 1

1

我能够使用以下程序复制您的错误消息。请注意,签名foo被注释掉:

import Test.QuickCheck
import Text.Show.Functions

-- foo :: [a] -> (a -> b) -> [b]
foo [] f = []
foo (x:xs) f = foo xs f

prop_1 :: [Int] -> Bool
prop_1 xs = foo xs id == xs

prop_2 :: [Int] -> (Int -> Int) -> (Int -> Int) -> Bool
prop_2 xs f g = foo (foo xs f) g == foo xs (g . f)

main = do
  quickCheck prop_1
  quickCheck prop_2

如果我把foo签名放回去,它的类型检查就很好。测试当然会失败,因为foo没有按照您的意愿去做。

问题是foo您在此处拥有的版本已推断出签名:

foo :: [a] -> b -> [c]

所以在 中,无法推断出prop_2最顶层调用的列表元素的类型来解决正确的操作。foo(==)

如果您替换foo为正确的版本:

foo :: [a] -> (a -> b) -> [b]
foo [] f = []
foo (x:xs) f = f x : foo xs f

然后测试通过您实际上可以再次注释掉签名,因为可以推断出正确的类型。

于 2019-06-28T16:53:54.643 回答