如何解决快速检查Wrap类型类的函子定律
我编写了一个代码来测试“常数”和“包装”这两种数据类型的仿函数定律。我完成了用于快速检查Constant数据类型的代码,但被Wrap数据类型卡住了。 (“ wrapGen”功能)我尝试过,但无法实现“ wrapGen”功能。我可以得到一些提示吗?
非常感谢。
import Test.QuickCheck
import Test.QuickCheck.Function -- for Fun
-- Functor laws
functorIdentity :: (Functor f,Eq (f a)) => f a -> Bool
functorIdentity f = fmap id f == f
functorCompose :: (Eq (f c),Functor f) => f a -> Fun a b -> Fun b c -> Bool
functorCompose x (Fun _ f) (Fun _ g) = (fmap (g . f) x) == (fmap g . fmap f $ x)
-- ┌--------------------------------------------┐
-- | Constant datatype (Example) |
-- └--------------------------------------------┘
newtype Constant a b = Constant { getConstant :: a } deriving (Eq,Show)
instance Functor (Constant m) where
fmap _ (Constant v) = Constant v
instance (Arbitrary a,Arbitrary b) => Arbitrary (Constant a b) where
arbitrary = constGen
constGen :: (Arbitrary a,Arbitrary b) => Gen (Constant a b)
constGen = do
a <- arbitrary
return (Constant a)
type IntToInt = Fun Int Int
type ConstFC = (Constant Int Int) -> IntToInt -> IntToInt -> Bool
test_Const :: IO ()
test_Const = do
quickCheck $ \x -> functorIdentity(x :: Constant Int Int)
quickCheck (functorCompose' :: ConstFC)
-- ┌--------------------------------------------┐
-- | Wrap datatype (Question) |
-- └--------------------------------------------┘
data Wrap f a = Wrap (f a) deriving (Eq,Show)
instance Functor f => Functor (Wrap f) where
fmap f (Wrap fa) = Wrap (fmap f fa)
instance (Functor f,Arbitrary a) => Arbitrary (Wrap f a) where
arbitrary = wrapGen
wrapGen :: (Functor f,Arbitrary a) => Gen (Wrap f a)
wrapGen = undefined -- TODO: How can I implement this?
type WrapFC = (Constant Int Int) -> IntToInt -> IntToInt -> Bool
qcWrap :: IO ()
qcWrap = do
quickCheck $ \x -> functorIdentity (x :: Wrap Maybe Int)
quickCheck (functorCompose :: WrapFC)
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。