【发布时间】:2017-12-19 01:16:01
【问题描述】:
我正在使用 hspec 和 QuickCheck 来验证 Functor 实例的函子定律。我有功能
functorIdentity :: (Functor f, Eq (f a)) => f a -> Bool
和
functorComposition :: (Functor f, Eq (f c)) => (Fun a b) -> (Fun b c) -> f a -> Bool
然后我使用如下代码块测试这两个:
testListFunctorness :: IO ()
testListFunctorness =
hspec $ do
describe "list" $ do
it "should obey functor identity" $ do
property (functorIdentity :: [Int] -> Bool)
it "should obey functor composition" $ do
property
(functorComposition :: (Fun Int String) -> (Fun String Int) -> [Int] -> Bool)
问题是,要为不同的 Functor 实例测试相同的属性,我需要复制除 [Int]s 之外的所有内容:
testMaybeFunctorness :: IO ()
testMaybeFunctorness =
hspec $ do
describe "maybe" $ do
it "should obey functor identity" $ do
property (functorIdentity :: Maybe Int -> Bool)
it "should obey functor composition" $ do
property
(functorComposition :: (Fun Int String) -> (Fun String Int) -> Maybe Int -> Bool)
感觉我应该能够编写一个在不同Functor 实例上以某种方式具有多态性的表达式,但我什至想不出如何开始。
如何方便地为多个不同的Functors 重用该测试逻辑块?
【问题讨论】:
-
我 99% 确定我在这里遇到了 AB 问题,但我看不到 B 是什么 :)
标签: haskell