1

作为一个学习练习,我想编写一个Data.MeldableHeap. 我在以下之后对其进行了图案化:

https://gitorious.org/aocapq/aocapq/source/7f24bb1571b3bd89ada668ea81c37ccdeb825498:src/PriorityQueue/QuickCheck.hs

genericArbitrary :: (PriorityQueue pq, Arbitrary a, Ord a) => Gen (pq a)
genericArbitrary = fromList `fmap` listOf arbitrary

我的代码:

{-# LANGUAGE TypeSynonymInstances, FlexibleInstances #-}

import Test.QuickCheck
import Control.Monad (liftM)
import Data.MeldableHeap

genericArbitrary :: (PQ pq, Arbitrary a, Ord a) => Gen (pq a)
genericArbitrary = liftM (foldr (\x acc -> insert x acc) empty) (listOf arbitrary)

但这上面得到:

`pq' is applied to too many type arguments
In the type signature for `genericArbitrary':
  genericArbitrary :: (PQ pq, Arbitrary a, Ord a) => Gen (pq a)

请提供指导。

更新:在您的帮助下,我得到了它:

https://github.com/haroldcarr/learn-haskell-coq-ml-etc/blob/master/haskell/course/coursera-fp-odersky-but-in-haskell/FRP01QuickCheck.hs

实际测试可能会更好,但重点是要学会在一个重要的例子上使用 QuickCheck。谢谢,

4

1 回答 1

2

不同之处在于,在您查看的代码中,它们有一个类型类

class PriorityQueue pq where
  insert ...

对不同优先级队列有一堆操作。但是,您没有类型类。您有一个数据类型PQ,它是表示优先级队列的一种实现的具体类型,即可合并堆。

试试吧

pqArbitrary :: (Arbitrary a, Ord a) => Gen (PQ a)
pqArbitrary = liftM (foldr (\x acc -> insert x acc) empty) (listOf arbitrary)
pqArbitrary' = foldr insert empty `fmap` listOf arbitrary

另外,我必须评论一下在 Hackage 上测试少数几个被证明是正确的包之一的讽刺意味:)(meldable-heap 已用 coq 验证)

于 2013-11-07T18:26:19.120 回答