[Haskell-cafe] Quick check finite data generation
ezra at ezrakilty.net
ezra at ezrakilty.net
Fri Sep 23 03:18:15 CEST 2011
Mukesh,
You need to write a generator function for the type (IntMap
Index) which
keeps track of the n parameter that you're using in arbIndex.
You can't rely on the Arbitrary instance for (IntMap a) because
it
doesn't have access to the n parameter that you are so
controlling
in arbIndex.
(Actually, you could use the "size" number that QuickCheck keeps
behind the scenes, using the functions "sized" and "resize", but
that
would have the consequence of affecting all other arbitrary
values
generated as part of that recursion--including the floats and
ints and
so on, which may or may not be what you want.)
Also, since your trees are unranked, rather than binary as in the
paper
example, take care with the factor of 2 that you're using to
resize the
generated subtrees. You might find that the resulting trees are
still
much bigger than you want, or have different statistical
properties.
Hope that helps--
Ezra
On Friday, September 23, 2011 4:22 AM, "mukesh tiwari"
<mukeshtiwari.iiitm at gmail.com> wrote:
Hello all
I have a data type
data Index = Index {indexSize::Float, indexIds::[Int],
indexDown::(IntMap.IntMap Index)}
| IndexLeaf {indexSize::Float, indexIds::[Int]}
| IndexEmpty {indexSize::Float}
deriving ( Show , Eq )
I derived some thing like this for QuickCheck testing
instance Arbitrary a => Arbitrary ( IntMap a ) where
arbitrary = liftM IntMap.fromList arbitrary
instance Arbitrary Index where
arbitrary =
oneof [ liftM3 Index arbitrary arbitrary arbitrary ,
liftM2 IndexLeaf arbitrary arbitrary , liftM IndexEmpty
arbitrary ]
but its generating infinite list of data. After reading the
paper [1]QuickCheck: A Lightweight Tool for Random Testing of
Haskell Programs
i tried to write above one using sized but i am not sure how
to write this .
instance Arbitrary a => Arbitrary ( IntMap a ) where
arbitrary = liftM IntMap.fromList arbitrary
instance Arbitrary Index where
arbitrary = sized arbIndex
arbIndex 0 = liftM IndexEmpty arbitrary
arbIndex 1 = liftM2 IndexLeaf arbitrary arbitrary
arbIndex n = frequency [ ( 1 , liftM IndexEmpty arbitrary ) ,
( 1 , liftM2 IndexLeaf arbitrary arbitrary ) , ( 2 , liftM2
Index arbitrary arbitrary ( arbIndex $ div n 2 ) ) ]
but from this type i can see arbIndex $ div n 2 will return
Gen Index not Gen ( IntMap Index ) and i am getting compiler
error . Could some one please tell me , how to write arbIndex
n in terms of smaller n like in the paper
arbTree 0 = liftM Leaf arbitrary
arbTree n = frequency[ ( 1 , liftM Leaf arbitrary ) , ( 4 ,
liftM2 Branch ( arbTree $ div n 2 ) ( arbTree $ div n 2 ) ) ]
Thank You
Mukesh Tiwari
_______________________________________________
Haskell-Cafe mailing list
Haskell-Cafe at haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe
References
Visible links
1. http://www.eecs.northwestern.edu/%7Erobby/courses/395-495-2009-fall/quick.pdf
Hidden links:
2. http://www.eecs.northwestern.edu/%7Erobby/courses/395-495-2009-fall/quick.pdf
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://www.haskell.org/pipermail/haskell-cafe/attachments/20110922/52367c80/attachment.htm>
More information about the Haskell-Cafe
mailing list