Haskell quickBatch测试:Ap ZipList的新型包装器
我正试图根据在中给出的建议为我的Haskell quickBatch测试:Ap ZipList的新型包装器,haskell,Haskell,我正试图根据在中给出的建议为我的apziplist创建一个包装器新类型。我的GHC版本是8.8.4,我使用堆栈进行编译。我的完整代码: 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 (<&
apziplist
创建一个包装器新类型。我的GHC版本是8.8.4,我使用堆栈进行编译。我的完整代码:
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 ZipList a) where
xs =-= ys = xs' `eq` ys' where
xs' =
let (Ap (ZipList l)) = xs
in take 3000 l
ys' =
let l = (getZipList . getAp) ys
in take 3000 l
newtype MonZipList a =
MonZipList (Ap ZipList a)
deriving (Semigroup, Monoid, Eq, Show)
deriving instance Functor f =>
Functor (Ap f)
deriving instance Applicative f =>
Applicative (Ap f)
monapp :: MonZipList (Sum Int)
monapp = MonZipList app
instance Arbitrary a =>
Arbitrary (MonZipList a) where
arbitrary = MonZipList <$> arbitrary
instance Eq a => EqProp (MonZipList a) where
(=-=) = eq
main :: IO ()
main = do
quickBatch $ monoid app
quickBatch $ monoid monapp
quickBatch $ functor monapp
quickBatch $ applicative monapp
newtype Ap f a=Ap{getAp::f a}
推导(等式,显示)
实例(应用f,半群a)=>
半群(Ap f a)其中
Ap xs Ap ys=
应付账款$liftA2()xs ys
实例(应用f,幺半群a)=>
幺半群(Ap f a)其中
mempty=Ap$纯mempty
Ap xs`mappend`Ap ys=
Ap$liftA2 mappend xs ys
应用程序::Ap ZipList(总和整数)
app=Ap(ZipList[1,2::Sum Int])
测试::Ap ZipList(Sum Int)
测试=应用程序
实例任意(f a)=>
任意(Ap f a)其中
任意=Ap任意
实例Eq a=>EqProp(Ap ZipList a),其中
xs=-=ys=xs'`eq`ys'其中
xs'=
let(Ap(ZipList l))=xs
取3000升
Y'=
设l=(getZipList.getAp)ys
取3000升
新类型MonZipList a=
蒙兹普利斯特(Ap兹普利斯特a)
求导(半群,幺半群,Eq,Show)
派生实例函子f=>
函子(APF)
派生实例应用程序f=>
应用型(APF)
monapp::MonZipList(Sum Int)
monapp=MonZipList应用程序
实例a=>
任意(MonZipList a)其中
任意=MonZipList任意
实例Eq a=>EqProp(MonZipList a),其中
(=-=)=eq
main::IO()
main=do
quickBatch$monoid应用程序
quickBatch$monapp幺半群
quickBatch$函子monapp
quickBatch$ApplicationMonApp
基本quickBatch$monoid应用程序测试运行良好,没有问题
我面临的问题是,当我运行quickBatch$monoid monapp
时,monoid测试挂起在mconcat测试上,无法停止。我不得不关闭WSL Bash以阻止它运行
当我尝试quickBatch$functor monapp
或quickBatch$applicative monapp
时,抛出了错误,从而使包装器newtype的有效性受到质疑:
无法将类型“Sum Int”与“(a0、b0、c0)”匹配
预期类型:MonZipList(a0、b0、c0)
实际类型:MonZipList(总和整数)
在“functor”的第一个参数中,即“monapp”
这些新类型包装器问题有什么解决方案吗?看起来您遇到了两个不同的问题,这两个问题都不是使用新类型包装器的结果
首先,ZipList
在mempty
的情况下在引擎盖下有无限个列表,这可能就是为什么您的monoid monapp
测试挂起的原因。看起来您在EqProp(Ap ZipList a)
实例中处理了这个问题,但在EqProp(MonZipList a)
实例中没有。这有点奇怪。也许您想要做的是删除EqProp(Ap ZipList a)
实例,只需要使用EqProp(MonZipList a)
实例,但是使用EqProp(Ap ZipList a)
实例中的代码?比如:
实例Eq a=>EqProp(MonZipList a)其中
MonZipList(Ap(ZipList xs))=-=MonZipList(Ap(ZipList ys))=
取3000 x`eq`取3000 ys
至于functor monapp
,类型根本没有对齐。对于functor
和applicative
,您需要为测试提供3种任意类型
您可能不知道这一点,但是您提供给的实际值(例如,functor
)是不相关的——它只对类型感兴趣。就我个人而言,我发现直接为这些函数提供类型作为参数更容易,如:
{-#语言类型应用程序}
main=do
quickBatch$monoid@(MonZipList(Sum Int))未定义
quickBatch$functor@MonZipList@Int@Bool@Char未定义
quickBatch$applicative@MonZipList@String@Int@Char未定义
现在,情况变得很清楚了:对于functor
测试,使用MonZipList
functor以及Int
、Bool
和Char
类型中的任意(和共轨)值进行测试。您可以随意更改这些类型,或者运行多个functor
测试,如果您真的想用其他类型测试它。谢谢!你的解决方案奏效了。我确实有一个相关的问题,这个问题是在第一次提出的,如果ZipList有无限的mempty列表,为什么程序挂起在monoid的mconcat测试上?它不应该也挂在monoid的mappend测试上吗?我发布了一个