Haskell - QuickCheck:检查 TestTree 中的相等性

问题描述 投票:0回答:1

我正在尝试编写一个 TestTree,在其中检查我的数据类型的以下公理

type Deque a = [a]

我的方法如下

prop1 :: TestTree prop1 = QC.testProperty "read_empty" $ peekFront [] == Nothing

但是,我收到以下错误消息:

* Ambiguous type variable 
a0' 由使用
==' prevents the constraint 
(Eq a0)' 求解而产生。 可能的修复:使用类型注释来指定
a0' should be. Potentially matching instances: instance Eq Ordering -- Defined in 
GHC.Classes' 实例 Eq 依赖类型 -- 定义于
tasty-1.5:Test.Tasty.Core' ...plus 29 others ...plus 14 instances involving out-of-scope types (use -fprint-potential-instances to see them all)

可能是因为我还没有为列表的内容定义类型,比如[Int]之类的。并且编译器不知道里面的内容是否属于 Eq 类,因此无法与 Eq 进行比较。

但是我如何告诉编译器,这个类型将是带有 Eq a 的 Maybe a ?

haskell quickcheck
1个回答
0
投票

可能是因为我还没有为列表的内容定义类型,比如[Int]之类的。并且编译器不知道里面的内容是否属于 Eq 类,因此无法与 Eq 进行比较。

确实,QuickCheck 无法生成未指定类型的值,因此您必须为

a
选择一个值。为此目的的常见选择是
Int
。您可以通过多种方式指定类型,例如

prop1 :: TestTree
prop1 = QC.testProperty "read_empty" $ peekFront ([] :: [Int]) == Nothing

{-# LANGUAGE TypeApplications #-}
module <...> where

<...>

prop1 :: TestTree
prop1 = QC.testProperty "read_empty" $ peekFront [] == Nothing @Int
© www.soinside.com 2019 - 2024. All rights reserved.