function - 编译器中止测试

标签 function variables haskell testing compiler-errors

我试过在函数中指定变量,但没用。

功能:

prop_unzip_check :: forall a b. Ord a => [(a,b)] -> Bool
prop_unzip_check xs = length (unzip (xs::[(a,b)])) >= 0

prop_merge_check :: forall a. Ord a => [a] -> [a] -> Bool
prop_merge_check xs ys = length (merge (xs::[a]) (ys::[a]))
                           == length (sort ((xs::[a]) ++ (ys::[a])))

编译后我收到这个令人困惑的错误:

> Test property "prop_merge_check" ... aborted.
Reason: Ambiguous type variable `a0' arising from a use of `quickCheckBool'
prevents the constraint `(Arbitrary a0)' from being solved.
Probable fix: use a type annotation to specify what `a0' should be.
These potential instances exist:
  instance Arbitrary QCGen -- Defined in `Test.QuickCheck.Arbitrary'
  instance Arbitrary (a b c) => Arbitrary (WrappedArrow a b c)
    -- Defined in `Test.QuickCheck.Arbitrary'
  instance Arbitrary (m a) => Arbitrary (WrappedMonad m a)
    -- Defined in `Test.QuickCheck.Arbitrary'
  ...plus 80 others
  (use -fprint-potential-instances to see them all)
Ambiguous type variable `a0' arising from a use of `prop_merge_check'
prevents the constraint `(Ord a0)' from being solved.
Probable fix: use a type annotation to specify what `a0' should be.
These potential instances exist:
  instance Ord (Encoding' a)
    -- Defined in `Data.Aeson.Encoding.Internal'
  instance Ord DotNetTime -- Defined in `Data.Aeson.Types.Internal'
  instance Ord JSONPathElement
    -- Defined in `Data.Aeson.Types.Internal'
  ...plus 310 others
  (use -fprint-potential-instances to see them all)

最佳答案

通过将某些函数定义为 Int 而不是任何类型的函数 'a' 来解决问题。

> prop_unzip_check :: [(Int,Int)] -> Bool
> prop_unzip_check xs = length (unzip (xs::[(Int,Int)])) >= 0

关于function - 编译器中止测试,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59023293/

相关文章:

jquery - 我可以在 .on Jquery 中使用变量吗

haskell - 如何拆箱数据类型?

c - 如何引用变量?

javascript - 如何获取输入字段的值并使用它来过滤数组?

javascript - 创建一个函数,该函数返回一个数组,其中包含从 5 到 118 的所有奇数

php - 在PHP中为外部函数定义$ id

php - 如果变量为 null 则显示另一个变量

json - 可以使用 GHCI 中的 json 包解析 JSON,但使用 GHC 编译时不行

haskell - 嵌套类型级编程

javascript - 调整屏幕大小时再次运行函数