Haskell快速批测试:应用幺半群ZipList
我正在尝试对提供的解决方案进行Haskell快速批测试:应用幺半群ZipList,haskell,Haskell,我正在尝试对提供的解决方案进行quickBatch测试。我需要一些关于如何从这里继续的建议,或者我应该为EqProp(Ap f a)尝试其他方法吗?我该如何推导出这个问题的解决方案 newtype Ap f a = Ap { getAp :: f a } deriving (Eq, Show) instance (Applicative f, Semigroup a) => Semigroup (Ap f a) where Ap xs <> Ap ys =
quickBatch
测试。我需要一些关于如何从这里继续的建议,或者我应该为EqProp(Ap f a)
尝试其他方法吗?我该如何推导出这个问题的解决方案
newtype Ap f a = Ap { getAp :: f a }
deriving (Eq, Show)
instance (Applicative f, Semigroup a) =>
Semigroup (Ap f a) where
Ap xs <> Ap ys =
Ap $ liftA2 (<>) xs ys
instance (Applicative f, Monoid a) =>
Monoid (Ap f a) where
mempty = Ap $ pure mempty
Ap xs `mappend` Ap ys =
Ap $ liftA2 mappend xs ys
app :: Ap ZipList (Sum Int)
app = Ap (ZipList [1,2 :: Sum Int])
test :: Ap ZipList (Sum Int)
test = app <> app
instance Arbitrary (f a) =>
Arbitrary (Ap f a) where
arbitrary = Ap <$> arbitrary
instance Eq a => EqProp (Ap f a) where
xs =-= ys = xs' `eq` ys' where
xs' =
let (Ap l) = xs
in take 3000 l
ys' =
let (Ap l) = ys
in take 3000 l
main :: IO ()
main = do
quickBatch $ monoid app
问题是你说
instance Eq a => EqProp (Ap f a)
但是在这个实例中,您使用了take
,它只适用于列表,而不适用于任意类型的构造函数f
。出于测试目的,仅使用以下两种方法限制实例是合理的:
instance Eq a => EqProp (Ap ZipList a)
或
您仍然需要打开ZipList
s。其中任何一种都需要额外的语言扩展;按照错误消息所说的去做。在测试之外,使用新类型的包装器通常是更好的做法:类似
{-# language GeneralizedNewtypeDeriving, DeriveTraversable #-}
newtype MonZipList a = MonZipList (Ap ZipList a)
deriving (Applicative, Alternative, Semigroup
, Monoid, Functor, Foldable, Traversable, ...)
问题是你说
instance Eq a => EqProp (Ap f a)
但是在这个实例中,您使用了take
,它只适用于列表,而不适用于任意类型的构造函数f
。出于测试目的,仅使用以下两种方法限制实例是合理的:
instance Eq a => EqProp (Ap ZipList a)
或
您仍然需要打开ZipList
s。其中任何一种都需要额外的语言扩展;按照错误消息所说的去做。在测试之外,使用新类型的包装器通常是更好的做法:类似
{-# language GeneralizedNewtypeDeriving, DeriveTraversable #-}
newtype MonZipList a = MonZipList (Ap ZipList a)
deriving (Applicative, Alternative, Semigroup
, Monoid, Functor, Foldable, Traversable, ...)
只是不要呼叫
take
?那到底是为什么?(事实上,您可以将其进一步缩短为(=-=)=eq
)请删除错误消息的图像,而不是复制并粘贴消息的实际文本。视障用户无法访问文本图像,无法进行搜索。@DanielWagner,take
大概是为了避免无限循环检查,例如,mempty mempty=mempty
@dfeuer aaah,是的,这是。。。不幸的是。@DanielWagner,我想这实际上只是为了mempty mempty=mempty
。因此,可以将截止限制为一对列表,这些列表的第一个n
元素都是mempty
。但这开始有点傻了。只是。。。不要呼叫take
?那到底是为什么?(事实上,您可以将其进一步缩短为(=-=)=eq
)请删除错误消息的图像,而不是复制并粘贴消息的实际文本。视障用户无法访问文本图像,无法进行搜索。@DanielWagner,take
大概是为了避免无限循环检查,例如,mempty mempty=mempty
@dfeuer aaah,是的,这是。。。不幸的是。@DanielWagner,我想这实际上只是为了mempty mempty=mempty
。因此,可以将截止限制为一对列表,这些列表的第一个n
元素都是mempty
。但是这开始有点傻了。谢谢,我试过:instance Eq a=>EqProp(Ap ZipList a)where..
,但在相同的代码行中,我收到了另一条错误消息:无法将“take”的第二个参数中的预期类型“[a1]”与实际类型“ZipList a”匹配,即表达式中的“l”:取3000 l
@maxloo,对,您必须打开ZipList
,而不仅仅是Ap
@maxloo,getZipList。getAp应该可以做到这一点。谢谢,这很有效!我还可以知道MonZipList是怎么用的吗?@maxloo-huh?这相当含糊。你还不明白什么?谢谢,我试过:实例Eq a=>EqProp(Ap ZipList a)where..
,但我在相同的代码行中收到另一条错误消息:无法将“take”的第二个参数中的预期类型“[a1]”与实际类型“ZipList a”匹配,即表达式中的“l”:取3000 l
@maxloo,对,您必须打开ZipList
,而不仅仅是Ap
@maxloo,getZipList。getAp应该可以做到这一点。谢谢,这很有效!我还可以知道MonZipList是怎么用的吗?@maxloo-huh?这相当含糊。你还不明白什么?