我正在尝试使用HSpec和QuickCheck来验证Monoids(关联性和身份元素)的属性。我将验证特定的实例,但希望保持大部分代码的多态性。这是几个小时后我想出来的:
module Test where
import Test.Hspec
import Test.QuickCheck
import Data.Monoid
instance (Arbitrary a) => Arbitrary (Sum a) where
arbitrary = fmap Sum arbitrary
instance (Arbitrary a) => Arbitrary (Product a) where
arbitrary = fmap Product arbitrary
prop_Monoid_mappend_mempty_x x = mappend mempty x === x
sumMonoidSpec = it "mappend mempty x = x" $ property (prop_Monoid_mappend_mempty_x :: Sum Int -> Property)
productMonoidSpec = it "mappend mempty x = x" $ property (prop_Monoid_mappend_mempty_x :: Product Double -> Property)
main :: IO ()
main = hspec $ do
describe "Data.Monoid.Sum" $ do
sumMonoidSpec
describe "Data.Monoid.Product" $ do
productMonoidSpec
我想要的是多态的
monoidSpec = it "mappend mempty x = x" $ property prop_Monoid_mappend_mempty_x
并稍后指定实际的Monoid实例(Sum,Product)和类型(Int,Double)。问题是它不会打字检查。我一直在
src/Test.hs@18:42-18:50 No instance for (Arbitrary a0) arising from a use of property
The type variable a0 is ambiguous
Note: there are several potential instances:
instance Arbitrary a => Arbitrary (Product a)
-- Defined at /home/app/isolation-runner-work/projects/68426/session.207/src/src/Test.hs:10:10
instance Arbitrary a => Arbitrary (Sum a)
-- Defined at /home/app/isolation-runner-work/projects/68426/session.207/src/src/Test.hs:7:10
instance Arbitrary () -- Defined in Test.QuickCheck.Arbitrary
...plus 27 others …
src/Test.hs@18:51-18:79 No instance for (Monoid a0)
arising from a use of prop_Monoid_mappend_mempty_x
The type variable a0 is ambiguous
Note: there are several potential instances:
instance Monoid () -- Defined in Data.Monoid
instance (Monoid a, Monoid b) => Monoid (a, b)
-- Defined in Data.Monoid
instance (Monoid a, Monoid b, Monoid c) => Monoid (a, b, c)
-- Defined in Data.Monoid
...plus 18 others …
我知道我需要将多态版本中的Monoid约束为Arbitrary,Eq和Show,但我不知道如何。
问题是如何以多态方式表达Monoid的规范并避免代码重复?
答案 0 :(得分:4)
注意property :: Testable prop => prop -> Property
的类型。类型var prop
将被删除,如果类型变量不再可用,则无法进行实例解析。基本上你想要做的就是推迟实例选择,为此你必须使这个类型可用,直到你选择实例为止。
一种方法是携带额外的Proxy prop
参数:
-- Possibly Uuseful helper function
propertyP :: Testable prop => Proxy prop -> prop -> Property
propertyP _ = property
monoidProp :: forall m . (Arbitrary m, Testable m, Show m, Monoid m, Eq m)
=> Proxy m -> Property
monoidProp _ = property (prop_Monoid_mappend_mempty_x :: m -> Property)
monoidSpec :: (Monoid m, Arbitrary m, Testable m, Show m, Eq m) => Proxy m -> Spec
monoidSpec x = it "mappend mempty x = x" $ monoidProp x
main0 :: IO ()
main0 = hspec $ do
describe "Data.Monoid.Sum" $ do
monoidSpec (Proxy :: Proxy (Sum Int))
describe "Data.Monoid.Product" $ do
monoidSpec (Proxy :: Proxy (Product Double))
另一种方法是使用类似tagged
的库,它提供类型Tagged
,它只是将一些幻像类型参数添加到现有类型中:
import Data.Tagged
type TaggedProp a = Tagged a Property
type TaggedSpec a = Tagged a Spec
monoidPropT :: forall a. (Monoid a, Arbitrary a, Show a, Eq a)
=> TaggedProp a
monoidPropT = Tagged (property (prop_Monoid_mappend_mempty_x :: a -> Property))
monoidSpecT :: forall a . (Monoid a, Arbitrary a, Show a, Eq a) => TaggedSpec a
monoidSpecT = Tagged $ it "mappend mempty x = x"
(unTagged (monoidPropT :: TaggedProp a))
main1 :: IO ()
main1 = hspec $ do
describe "Data.Monoid.Sum" $ do
untag (monoidSpecT :: TaggedSpec (Sum Int))
describe "Data.Monoid.Product" $ do
untag (monoidSpecT :: TaggedSpec (Product Double))
这些解决方案基本上是等效的,但在某些情况下,一个或另一个可能更方便。由于我对您的用例了解不多,我已将两者都包括在内。
这两项都只需要-XScopedTypeVariables
。