11 Nov 23:31 2013

## Quickcheck generator help

Hi, have the following type

data BinomialHeap a = EmptyHeap | Node a Int (BinomialHeap a)

and wanted to create a test generator with type ‘a’ as an Int for example

newtype BinominalHeapInt = BinominalHeap Int deriving (Eq, Show)

But struggling to get the generator correct, currently have

instance Arbitrary BinominalHeapInt where
arbitrary = sized heap' where
heap' 0 = return EmptyHeap
heap' n | n>0 = oneof [return EmptyHeap, liftM3 Node arbitrary arbitrary subnode]
where subnode = heap' (n `div` 2)

But it complains

Expected type: Int -> Gen BinominalHeapInt
Actual type: Int -> Gen (BinomialHeap a1)

in sized heap’

Any pointers ?

Thanks

```_______________________________________________
```
11 Nov 23:59 2013

### Re: Quickcheck generator help

```On 12 November 2013 09:31, Graham Berks <graham <at> fatlazycat.com> wrote:
> Hi, have the following type
>
> data BinomialHeap a = EmptyHeap | Node a Int (BinomialHeap a)
>
> and wanted to create a test generator with type ‘a’ as an Int for example
>
>
> newtype BinominalHeapInt = BinominalHeap Int deriving (Eq, Show)

Methinks this line is an error: what you've said is that
BinominalHeapInt is an Int wrapped in a BinominalHeap constructor; you
probably want:

newtype BinomialHeapInt = BHI (BinomialHeap Int)

Notice also your type of "Binominal" instead of "Binomial".

(You can't derive anything as Eq and Show aren't derived/defined for
BinomialHeap)

>
> But struggling to get the generator correct, currently have
>
> instance Arbitrary BinominalHeapInt where
> arbitrary = sized heap' where
> heap' 0 = return EmptyHeap
> heap' n | n>0 = oneof [return EmptyHeap, liftM3 Node arbitrary arbitrary
> subnode]
> where subnode = heap' (n `div` 2)

What you'd probably want is to have "instance (Arbitrary a) =>
Arbitrary (BinomialHeap a) where ..." with this definition, and then:

instance Arbitrary BinomialHeapInt where
arbitrary = fmap BHI arbitrary

>
> But it complains
>
>     Expected type: Int -> Gen BinominalHeapInt
>       Actual type: Int -> Gen (BinomialHeap a1)
>
> in sized heap’
>
> Any pointers ?
>
> Thanks
>
>
>
>
> _______________________________________________
> Haskell-Cafe mailing list
>

--

--
Ivan Lazar Miljenovic
Ivan.Miljenovic <at> gmail.com
http://IvanMiljenovic.wordpress.com
_______________________________________________