【发布时间】:2019-11-10 07:39:15
【问题描述】:
我有函数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.
我不确定为什么会收到此错误以及如何解决它。任何见解都值得赞赏。
【问题讨论】:
-
您是否在 shell 中编写了
prop_2?你有没有用:{和:}把签名和函数写在同一个“块”里? -
不,我在一个带有 prop_1 和 foo 的文件中有 prop_2。
-
看起来 Haskell 似乎无法理解
foo xs (g . f)的类型,所以这通常意味着类型签名在这里没有说它是[Int]。 -
你能分享一个可编译的例子来重现这个问题吗?
-
我尝试编译和运行您的属性。我无法重现您的错误消息,但我发现了另外两个问题:首先,您的函数
foo等效于const [],因此您的第一个属性将始终失败。其次,prop_2不能被quickCheck运行,因为没有Show (Int -> Int)实例(quickCheck需要这个,所以它可以打印反例)
标签: haskell functional-programming quickcheck property-based-testing