<br><br><div class="gmail_quote">On Sun, Mar 16, 2008 at 5:42 PM, rodrigo.bonifacio &lt;<a href="mailto:rodrigo.bonifacio@uol.com.br">rodrigo.bonifacio@uol.com.br</a>&gt; wrote:<br><blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;">
Hi all,<br>
<br>
I&#39;m trying to use the quick-check library for checking some properties of a user defined data type. Bellow the target data type:<br>
<br>
data Feature =<br>
&nbsp;Feature Id Name FeatureType GroupType Children Properties |<br>
&nbsp;FeatureError<br>
<br>
where:<br>
<br>
Id = String<br>
Name = String<br>
FeatureType = int<br>
GroupType = int<br>
Children = [Feature]<br>
Propertyes = [String]<br>
<br>
<br>
I&#39;ve written the following quick-check property:<br>
<br>
prop_AlternativeFeature :: Feature -&gt; Feature -&gt; QuickCheck.Property<br>
prop_AlternativeFeature fm fc = length (children fc) == 0 ==&gt; length &nbsp;(checkAlternativeFeature fm fc) &gt; 0<br>
<br>
When I try to check such property, the result is:<br>
<br>
ERROR &quot;./EshopModelChecking.hs&quot;:11 - Type error in instance member binding<br>
*** Term &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; : arbitrary<br>
*** Type &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; : Feature<br>
*** Does not match : Gen Feature<br>
<br>
I think that I need to write some arbitrary or generator functions, but I didn&#39;t realize how to do that with the availalble quick-checking documentation.<br>
<br>
Any help will be welcome.<br>
<br>
</blockquote><div><br>You use the available functions to build up a generator for your data type.<br><br>First, let&#39;s give the instanc itself. For this I&#39;m just going to use the frequency function to use &quot;featureGenNormal&quot; five times more often than &quot;return FeatureError&quot;. This means that will get a FeatureError every now and then, but mostly you&#39;ll get featureGenNormal (see below). You can change these frequences, of course.<br>
<br>instance Arbitrary Feature where<br>&nbsp;&nbsp;&nbsp; arbitrary = do<br>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp; &nbsp;frequency [ (5, featureGenNormal),&nbsp; (1, return FeatureError) ]<br><br>In order to write featureGenNormal, we need to be able to generate random values of each of the parts of the data type. Often these types will already have Arbitrary instances, so generating an isntance for your type is quite often just a matter of calling &quot;arbitrary&quot; for each component, and then returning a datatype. However, there is no Arbitrary instance for String, which is a bit annoying, so let&#39;s write our own generator for strings.<br>
<br>First a generator for a single letter:<br><br>letterGen = oneof $ map return $ [&#39;a&#39;..&#39;z&#39;] ++ [&#39;A&#39;..&#39;Z&#39;]<br><br>Then a combinator for generating a list of values given a generator for a single value:<br>
<br>listGen :: Gen a -&gt; Gen [a]<br>listGen g = do<br>&nbsp;&nbsp;&nbsp; x &lt;- g<br>&nbsp;&nbsp;&nbsp; xs &lt;- frequency [ (1, return []), (10, listGen g) ]<br>&nbsp;&nbsp;&nbsp; return (x:xs)<br><br>And then we use this to build our &quot;stringGen&quot; generator.<br>
<br>stringGen :: Gen String<br>
stringGen = listGen letterGen<br><br>Now, we have all we need to write the featureGenNormal generator:<br><br>featureGenNormal = do<br>&nbsp;&nbsp;&nbsp; id &lt;- stringGen<br>&nbsp;&nbsp;&nbsp; name &lt;- stringGen<br>&nbsp;&nbsp;&nbsp; featuretype &lt;- arbitrary<br>
&nbsp;&nbsp;&nbsp; grouptype &lt;- arbitrary<br>&nbsp;&nbsp;&nbsp; children &lt;- arbitrary<br>&nbsp;&nbsp;&nbsp; properties &lt;- listGen stringGen<br>&nbsp;&nbsp;&nbsp; return (Feature id name featuretype grouptype children properties) <br></div></div><br><br>Note that we use &quot;arbitrary&quot; to generate the list of children recursively.<br>
<br>-- <br>Sebastian Sylvan<br>+44(0)7857-300802<br>UIN: 44640862