[Haskell-cafe] QuickCheck: Why are these instances of Testable?

2013-03-11 Thread Paul Brenman
Why does QuickCheck (2.6) list the following as instances of class Testable?

Testable Result
Testable Prop
Testable prop = Testable (Gen prop)

The Hughes/Claessen paper QuickCheck: A Lightweight Tool for Random
Testing of Haskell Programs mentions nesting property combinators on page
6, but I didn't understand this reference.
___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


[Haskell-cafe] QuickCheck shrink

2012-12-05 Thread warrensomebody
Can someone point me at some documentation or examples of how to write and use 
a QuickCheck shrink function. I can't seem to find anything online. Thanks,

Warren
___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


Re: [Haskell-cafe] QuickCheck shrink

2012-12-05 Thread Roman Cheplyaka
* warrensomeb...@gmail.com warrensomeb...@gmail.com [2012-12-05 12:21:52-0800]
 Can someone point me at some documentation or examples of how to write
 and use a QuickCheck shrink function. I can't seem to find anything
 online. Thanks,

Did you try to look at the instances defined in the package itself?

Roman

___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


Re: [Haskell-cafe] QuickCheck Generators

2012-11-22 Thread Anton Kholomiov
An idea. You can make a type:

data TestContains = TestContains Tweet TweetSet

and the make an Arbitrary instance for it. When you do
a recursove call you have three different tweets one new tweet
and two from the sub-calls. Then you can place one of them in the
result. In the end you will have a random TweetSet with some value from it.

Here is a scratch of the implementation:

instance Arbitrary TestContains where
   arbitrary = sized set'
   where set' 0 = mkSingleTweet $ (arbitrary :: Tweet)
 set' n = do
  t0 - arbitrary :: Tweet
  TestContains t1 ts1 - subTweets
  TestContains t2 ts2 - subTweets
  t - oneof [t0, t1, t2]
  return $ TestContains t $ TweetSet t0 ts1 ts2

 subTweets = set' (n `div` 2)


2012/11/21 gra...@fatlazycat.com

 I have

 data Tweet = Tweet {
 user :: String,
 text :: String,
 retweets :: Double
 } deriving (Show)

 data TweetSet = NoTweets | SomeTweets Tweet TweetSet TweetSet

 and trying to create some generators for testing, with

 instance Arbitrary Tweet where
   arbitrary = liftM3 Tweet arbitrary arbitrary arbitrary

 instance Arbitrary TweetSet where
   arbitrary = sized set'
 where set' 0 = return NoTweets
   set' n | n0 = oneof[return NoTweets, liftM3 SomeTweets
   arbitrary subTweets subTweets]
 where subTweets = set' (n `div` 2)

 but wondering how I would go about generating a random TweetSet that
 contains a known random Tweet I later have reference to and I would also
 assume the known Tweet to be placed randomly.

 Then I could test a contains function.

 Thanks

 ___
 Haskell-Cafe mailing list
 Haskell-Cafe@haskell.org
 http://www.haskell.org/mailman/listinfo/haskell-cafe

___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


[Haskell-cafe] QuickCheck Generators

2012-11-20 Thread graham
I have 

data Tweet = Tweet {
user :: String,
text :: String,
retweets :: Double
} deriving (Show)

data TweetSet = NoTweets | SomeTweets Tweet TweetSet TweetSet

and trying to create some generators for testing, with

instance Arbitrary Tweet where
  arbitrary = liftM3 Tweet arbitrary arbitrary arbitrary 
  
instance Arbitrary TweetSet where
  arbitrary = sized set'
where set' 0 = return NoTweets
  set' n | n0 = oneof[return NoTweets, liftM3 SomeTweets
  arbitrary subTweets subTweets]
where subTweets = set' (n `div` 2) 

but wondering how I would go about generating a random TweetSet that
contains a known random Tweet I later have reference to and I would also
assume the known Tweet to be placed randomly.

Then I could test a contains function.

Thanks

___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


Re: [Haskell-cafe] Quickcheck

2012-11-13 Thread graham
Thanks, will try them both. With regards to the implication I assume
it's just regarded as one property test ?

To get two values greater than zero I have something like

prop_something x y = ...do blah with positive integers
  == x  0  y  0

But my test fails as it appears to be injecting a negative number and
the test fails. But the implication does not cause the failed test to be
ignored.

Must be missing something ???

Thanks

On Mon, Nov 12, 2012, at 10:00 PM, Iustin Pop wrote:
 On Mon, Nov 12, 2012 at 10:14:30PM +0100, Simon Hengel wrote:
  On Mon, Nov 12, 2012 at 07:21:06PM +, gra...@fatlazycat.com wrote:
   Hi, 
   
   Trying to find some good docs on QuickCheck, if anyone has one ?
   
   Been scanning what I can find, but a question.
   
   What would be the best way to generate two different/distinct integers ?
  
  I would use Quickcheck's implication operator here:
  
  quickCheck $ \x y - x /= (y :: Int) == ...
 
 That's good, but it only eliminates test cases after they have been
 generated. A slightly better (IMHO) version is to generate correct
 values in the first place:
 
 prop_Test :: Property
 prop_Test =
 forAll (arbitrary::Gen Int) $ \x -
 forAll (arbitrary `suchThat` (/= x)) $ \y -
 …
 
 regards,
 iustin

___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


Re: [Haskell-cafe] Quickcheck

2012-11-13 Thread Clark Gaebel
Your implication is backwards. == is read implies

So your way has do blah with positive integers implies x  0  y  0.
That's backwards.

Try prop_something x y = x  0  y  0 == ... do blah with positive
integers

  - Clark


On Tue, Nov 13, 2012 at 4:52 PM, gra...@fatlazycat.com wrote:

 Thanks, will try them both. With regards to the implication I assume
 it's just regarded as one property test ?

 To get two values greater than zero I have something like

 prop_something x y = ...do blah with positive integers
   == x  0  y  0

 But my test fails as it appears to be injecting a negative number and
 the test fails. But the implication does not cause the failed test to be
 ignored.

 Must be missing something ???

 Thanks

 On Mon, Nov 12, 2012, at 10:00 PM, Iustin Pop wrote:
  On Mon, Nov 12, 2012 at 10:14:30PM +0100, Simon Hengel wrote:
   On Mon, Nov 12, 2012 at 07:21:06PM +, gra...@fatlazycat.com wrote:
Hi,
   
Trying to find some good docs on QuickCheck, if anyone has one ?
   
Been scanning what I can find, but a question.
   
What would be the best way to generate two different/distinct
 integers ?
  
   I would use Quickcheck's implication operator here:
  
   quickCheck $ \x y - x /= (y :: Int) == ...
 
  That's good, but it only eliminates test cases after they have been
  generated. A slightly better (IMHO) version is to generate correct
  values in the first place:
 
  prop_Test :: Property
  prop_Test =
  forAll (arbitrary::Gen Int) $ \x -
  forAll (arbitrary `suchThat` (/= x)) $ \y -
  …
 
  regards,
  iustin

 ___
 Haskell-Cafe mailing list
 Haskell-Cafe@haskell.org
 http://www.haskell.org/mailman/listinfo/haskell-cafe

___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


Re: [Haskell-cafe] Quickcheck

2012-11-13 Thread Ozgur Akgun
hi,

On 13 November 2012 21:52, gra...@fatlazycat.com wrote:

 prop_something x y = ...do blah with positive integers
   == x  0  y  0


quickcheck provides a few nice new types for such cases. try:

prop_something (Positive x) (Positive y) = ...

this way qc only generates positive numbers, instead of generating and
discarding some.

hth,

-- 
Ozgur Akgun
___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


Re: [Haskell-cafe] Quickcheck

2012-11-13 Thread graham
Thanks, that handy and works for my test.



Any idea why the implication does not ???





On Tue, Nov 13, 2012, at 09:59 PM, Ozgur Akgun wrote:

hi,



On 13 November 2012 21:52, [1]gra...@fatlazycat.com wrote:

prop_something x y = ...do blah with positive integers

  == x  0  y  0


quickcheck provides a few nice new types for such cases. try:



prop_something (Positive x) (Positive y) = ...



this way qc only generates positive numbers, instead of generating and
discarding some.

hth,

--
Ozgur Akgun

References

1. mailto:gra...@fatlazycat.com
___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


[Haskell-cafe] Quickcheck

2012-11-12 Thread graham
Hi, 

Trying to find some good docs on QuickCheck, if anyone has one ?

Been scanning what I can find, but a question.

What would be the best way to generate two different/distinct integers ?

Use arbitrary ( if so do you have an example ) or a conditional on the
property.

Though I read the later can reduce passing test count.

Thanks
Graham

___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


Re: [Haskell-cafe] Quickcheck

2012-11-12 Thread Simon Hengel
On Mon, Nov 12, 2012 at 07:21:06PM +, gra...@fatlazycat.com wrote:
 Hi, 
 
 Trying to find some good docs on QuickCheck, if anyone has one ?
 
 Been scanning what I can find, but a question.
 
 What would be the best way to generate two different/distinct integers ?

I would use Quickcheck's implication operator here:

quickCheck $ \x y - x /= (y :: Int) == ...

Cheers,
Simon

___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


Re: [Haskell-cafe] Quickcheck

2012-11-12 Thread Iustin Pop
On Mon, Nov 12, 2012 at 10:14:30PM +0100, Simon Hengel wrote:
 On Mon, Nov 12, 2012 at 07:21:06PM +, gra...@fatlazycat.com wrote:
  Hi, 
  
  Trying to find some good docs on QuickCheck, if anyone has one ?
  
  Been scanning what I can find, but a question.
  
  What would be the best way to generate two different/distinct integers ?
 
 I would use Quickcheck's implication operator here:
 
 quickCheck $ \x y - x /= (y :: Int) == ...

That's good, but it only eliminates test cases after they have been
generated. A slightly better (IMHO) version is to generate correct
values in the first place:

prop_Test :: Property
prop_Test =
  forAll (arbitrary::Gen Int) $ \x -
  forAll (arbitrary `suchThat` (/= x)) $ \y -
  …

regards,
iustin

___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


Re: [Haskell-cafe] Quickcheck research

2011-11-23 Thread Jonathan Fischoff
 which makes me think that Arbitrary isn't the right set of
 abstractions for controlling coverage of the value space.

I agree with you Wren. I think what is needed is a library for expressing
the distribution of values for a given type.

I can see several ways of specifying distributions, directly (exponential,
normal, etc), qualitative constraints (a tuple where each value is less
then the next), constraints on moments (MaxEnt), etc, there are many ways
that are not mutually exclusive.

There are other ways to improve quickcheck.

Writing arbitrary instance could be made more declarative. The process of
describing an indicator function that fully determines the set of values
that are valid for my type, and writing the sequence of steps necessary to
make the function efficient could be separated.

Combinatorial species and symbolic combinatorics are too possibilities to
simplify the creation of arbitrary functions. There might be way go from
list of functions to an generator. Something like

make_arb :: [a - Bool] - Gen a

That is psuedo code, since you can't reify a function. But that would be
the idea, although I don't think it would work in all cases.

Also, the current quickcheck workflow is to run it until a example of valid
input fails a test. When this happens the example should be used in a
regression. Creating unit tests automatically from failed quickcheck cases
would be a time saver.

Additionally the right type mutation of testing would be a great addition
to quickcheck.

-Jonathan
___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


Re: [Haskell-cafe] Quickcheck research

2011-11-23 Thread Jason Dagit
On Tue, Nov 22, 2011 at 9:42 AM, wren ng thornton w...@freegeek.org wrote:
 On 11/22/11 6:09 AM, Macías López wrote:

 Hello:

 I'm a Master's student in Computer Science. I have to make a project
 involving some research, I'm very interested in Quickcheck and I wonder if
 there are some areas which need work or if there is some potential
 research
 topic related to it.

 In particular I know that Erlang Quickcheck has been worked on a lot and
 has some features like state machines or C bindings which may be useful to
 the Haskell community.

 I would appreciate any directions.

 Something I think would be nice is to see full integration between
 SmallCheck and QuickCheck. In particular, I'd like to use SmallCheck to
 exhaustively search the small cases, and then use QuickCheck in a way that
 ensures that it only tests on things larger than the ones which have already
 been tested.

 One of the problems with mixing the two these days is that QuickCheck often
 wastes a lot of time checking things that SmallCheck will also test. While
 the goal may not seem very researchy, it actually gets at one of the main
 weaknesses of QuickCheck: namely, how to properly control generation of
 arbitrary values in order to ensure you're testing something helpful. It's
 too easy to design Arbitrary instances which only generate small values
 (e.g., half of all lists are the empty list) or which loop forever (because
 of trying to avoid the too-small problem), which makes me think that
 Arbitrary isn't the right set of abstractions for controlling coverage of
 the value space.

Especially in the case where you find a counter example it may help to
slightly mutate that input and see if cases near it fail or succeed.

On a similar line of reasoning, I've wondered if Perlin style noise
generation could be applied to get a sort of fuzzing effect.  This
would be more interesting for cases where writing instances of
arbitrary is hard to do but test cases do exist.  Apply some sort of
pseudo-random noise to your examples and see if your properties still
hold.  I could see this having applications in parsers.

As far as I can tell, no one has used Perlin noise on algebraic
structures.  It seems to have only been applied to real valued spaces.
 Imagine having a parse tree then applying noise to the structure of
the tree then unparsing the tree back to concrete syntax.  You're
making the structure noisy instead of just fussing the concrete syntax
directly (which should increase the frequency that you change the
shape/meaning instead of just changing the tokens in the parse tree).

Jason

___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


Re: [Haskell-cafe] Quickcheck research

2011-11-23 Thread Jacques Carette

On 11-11-23 08:28 PM, Jason Dagit wrote:

On a similar line of reasoning, I've wondered if Perlin style noise
generation could be applied to get a sort of fuzzing effect.  This
would be more interesting for cases where writing instances of
arbitrary is hard to do but test cases do exist.  Apply some sort of
pseudo-random noise to your examples and see if your properties still
hold.  I could see this having applications in parsers.

As far as I can tell, no one has used Perlin noise on algebraic
structures.  It seems to have only been applied to real valued spaces.
  Imagine having a parse tree then applying noise to the structure of
the tree then unparsing the tree back to concrete syntax.  You're
making the structure noisy instead of just fussing the concrete syntax
directly (which should increase the frequency that you change the
shape/meaning instead of just changing the tokens in the parse tree).



Interesting idea!  With the strategy based unified 
Quickcheck/Smallcheck that we're finishing up, it would be quite easy to 
program that as a new generation strategy and try it.


We've already got Boltzmann sampling on the list of things to look at in 
the future.


Jacques


___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


[Haskell-cafe] Quickcheck research

2011-11-22 Thread Macías López
Hello:

I'm a Master's student in Computer Science. I have to make a project
involving some research, I'm very interested in Quickcheck and I wonder if
there are some areas which need work or if there is some potential research
topic related to it.

In particular I know that Erlang Quickcheck has been worked on a lot and
has some features like state machines or C bindings which may be useful to
the Haskell community.

I would appreciate any directions.

Cheers,
Macías López.
___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


Re: [Haskell-cafe] Quickcheck research

2011-11-22 Thread Jacques Carette

On 22/11/2011 12:42 PM, wren ng thornton wrote:


Something I think would be nice is to see full integration between 
SmallCheck and QuickCheck. In particular, I'd like to use SmallCheck 
to exhaustively search the small cases, and then use QuickCheck in a 
way that ensures that it only tests on things larger than the ones 
which have already been tested.


Actually, (Gordon Uszkay and I) have already done that.  We are hoping 
to make a release of this in November.


Jacques


___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


Re: [Haskell-cafe] Quickcheck research

2011-11-22 Thread wren ng thornton

On 11/22/11 1:36 PM, Jacques Carette wrote:

On 22/11/2011 12:42 PM, wren ng thornton wrote:


Something I think would be nice is to see full integration between
SmallCheck and QuickCheck. In particular, I'd like to use SmallCheck
to exhaustively search the small cases, and then use QuickCheck in a
way that ensures that it only tests on things larger than the ones
which have already been tested.


Actually, (Gordon Uszkay and I) have already done that. We are hoping to
make a release of this in November.


Excellent!

--
Live well,
~wren

___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


Re: [Haskell-cafe] Quickcheck, Arbitrary and Overlapping instances

2011-09-22 Thread Valentin Robert
I think it is one case where you should use newtype instead of type, so that 
it is not just a simple type synonym, but an actual other type.

With type, you're just making an alias, but Haskell sees the same type. A 
newtype wrapper will hide this, at the price of having to define a (unique) 
constructor... So yes, you'll have to change some code.
___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


Re: [Haskell-cafe] QuickCheck Questions

2011-08-02 Thread Øystein Kolsrud
Hi! I usually use the function 'sized' for this. The function would look
something like this:

myIntGen :: Gen Int
myIntGen = sized $ \n - choose (0,f n)

where 'f' is a function that uses the size value to generate an upper value
for your random range. I usually use ^2 or sqrt or something like that
depending on my needs.

Hope it helps!

Best regards, Øystein

On Mon, Jul 25, 2011 at 6:31 AM, Mark Spezzano mark.spezz...@chariot.net.au
 wrote:

 Hi Kevin,

 Thanks for the response. The first part works well with minor
 modifications.

 Part 2 is still a bit vague to me. I basically want to clamp the Integers
 generated within the Queue to between 0 and some positive number. At present
 they're giving me numbers all over the place (specifically negative number)

 Thanks

 Mark


 On 25/07/2011, at 4:44 AM, Kevin Quick wrote:

  On Sun, 24 Jul 2011 07:30:56 -0700, Mark Spezzano 
 mark.spezz...@chariot.net.au wrote:
 
  Hi all,
 
  I would appreciate it if someone can point me in the right direction
 with the following problem.
 
  I'm deliberately implementing a naive Queues packages that uses finite
 lists as the underlying representation. I've already read through Hughes'
 paper and the article in The Fun of Programming, but I'm still having some
 difficulties. Specifically:
 
  1. I have a newtype Queue a = Queue [a] and I want to generate Queues of
 random Integers that are also of random size. How do I do this in
 QuickCheck? I guess that  I need to write a generator and then make my
 Queue a concrete type an instance of Arbitrary? How?
 
  Mark,
 
  One of the great things about QuickCheck is that it is automatically
 compositional.
  What I mean by this is that all you need in your instance is how to form
 a Queue [a] given [a], because there are already QuickCheck instances
 for forming lists, and as long as a is pretty standard (Integers is fine)
 then there's likely an Arbitrary instance for that as well.
 
  So (from my head, not actually tested in GHC):
 
  import Control.Applicative
  import Test.QuickCheck
 
  instance Arbitrary Queue where
arbitrary = Queue $ arbitrary
 
  Then you can use this as:
 
  testProperty length is something propQInts
 
  propQInts t = length t == 
 where types = (t :: Queue Integers)
 
  The where clause is a fancy way of specifying what the type of t should
 be without having to express the overall type of propQInts.  You could use a
 more conventional type specification as well.
 
 
  2. If I wanted to specify/constrain the ranges of random Integers
 generated, how would I do this?
 
  Probably something like this:
 
  instance Arbitrary Queue where
 arbitrary = do li - listOf $ arbitrary
   lr - liftM $ map rangelimit li
   return $ Queue lr
 where rangelimit n = case (n  LOW, n  HIGH) of
(True,_) - LOW
(_,True) - HIGH
_ - n
 
 
 
  3. If I wanted to specify/constrain the Queue sizes how would I do this?
 
  Similar to #2.  Perhaps:
 
arbitrary = arbitrary = (return . Queue . take CNT . listOf)
 
 
  --
  -KQ
 
  ___
  Haskell-Cafe mailing list
  Haskell-Cafe@haskell.org
  http://www.haskell.org/mailman/listinfo/haskell-cafe
 
 


 ___
 Haskell-Cafe mailing list
 Haskell-Cafe@haskell.org
 http://www.haskell.org/mailman/listinfo/haskell-cafe




-- 
Mvh Øystein Kolsrud
___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


[Haskell-cafe] QuickCheck Questions

2011-07-24 Thread Mark Spezzano
Hi all,

I would appreciate it if someone can point me in the right direction with the 
following problem.

I'm deliberately implementing a naive Queues packages that uses finite lists as 
the underlying representation. I've already read through Hughes' paper and the 
article in The Fun of Programming, but I'm still having some difficulties. 
Specifically:

1. I have a newtype Queue a = Queue [a] and I want to generate Queues of random 
Integers that are also of random size. How do I do this in QuickCheck? I guess 
that  I need to write a generator and then make my Queue a concrete type an 
instance of Arbitrary? How?

2. If I wanted to specify/constrain the ranges of random Integers generated, 
how would I do this?

3. If I wanted to specify/constrain the Queue sizes how would I do this?

On a separate issue, I also see that QuickCheck 2 has some features not 
discussed anywhere. For example Coarbitrary and shrink are a bit of a 
mystery to me.

Any code examples for any of the above questions would help greatly.

I've been stuck on this problem for hours. :)

Cheers,

Mark


___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


Re: [Haskell-cafe] QuickCheck Questions

2011-07-24 Thread Kevin Quick
On Sun, 24 Jul 2011 07:30:56 -0700, Mark Spezzano  
mark.spezz...@chariot.net.au wrote:



Hi all,

I would appreciate it if someone can point me in the right direction  
with the following problem.


I'm deliberately implementing a naive Queues packages that uses finite  
lists as the underlying representation. I've already read through  
Hughes' paper and the article in The Fun of Programming, but I'm still  
having some difficulties. Specifically:


1. I have a newtype Queue a = Queue [a] and I want to generate Queues of  
random Integers that are also of random size. How do I do this in  
QuickCheck? I guess that  I need to write a generator and then make my  
Queue a concrete type an instance of Arbitrary? How?


Mark,

One of the great things about QuickCheck is that it is automatically  
compositional.
What I mean by this is that all you need in your instance is how to form a  
Queue [a] given [a], because there are already QuickCheck instances  
for forming lists, and as long as a is pretty standard (Integers is fine)  
then there's likely an Arbitrary instance for that as well.


So (from my head, not actually tested in GHC):

import Control.Applicative
import Test.QuickCheck

instance Arbitrary Queue where
   arbitrary = Queue $ arbitrary

Then you can use this as:

testProperty length is something propQInts

propQInts t = length t == 
where types = (t :: Queue Integers)

The where clause is a fancy way of specifying what the type of t should be  
without having to express the overall type of propQInts.  You could use a  
more conventional type specification as well.




2. If I wanted to specify/constrain the ranges of random Integers  
generated, how would I do this?


Probably something like this:

instance Arbitrary Queue where
arbitrary = do li - listOf $ arbitrary
   lr - liftM $ map rangelimit li
   return $ Queue lr
where rangelimit n = case (n  LOW, n  HIGH) of
(True,_) - LOW
(_,True) - HIGH
_ - n




3. If I wanted to specify/constrain the Queue sizes how would I do this?


Similar to #2.  Perhaps:

   arbitrary = arbitrary = (return . Queue . take CNT . listOf)


--
-KQ

___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


Re: [Haskell-cafe] QuickCheck Questions

2011-07-24 Thread Mark Spezzano
Hi Kevin,

Thanks for the response. The first part works well with minor modifications. 

Part 2 is still a bit vague to me. I basically want to clamp the Integers 
generated within the Queue to between 0 and some positive number. At present 
they're giving me numbers all over the place (specifically negative number)

Thanks

Mark


On 25/07/2011, at 4:44 AM, Kevin Quick wrote:

 On Sun, 24 Jul 2011 07:30:56 -0700, Mark Spezzano 
 mark.spezz...@chariot.net.au wrote:
 
 Hi all,
 
 I would appreciate it if someone can point me in the right direction with 
 the following problem.
 
 I'm deliberately implementing a naive Queues packages that uses finite lists 
 as the underlying representation. I've already read through Hughes' paper 
 and the article in The Fun of Programming, but I'm still having some 
 difficulties. Specifically:
 
 1. I have a newtype Queue a = Queue [a] and I want to generate Queues of 
 random Integers that are also of random size. How do I do this in 
 QuickCheck? I guess that  I need to write a generator and then make my 
 Queue a concrete type an instance of Arbitrary? How?
 
 Mark,
 
 One of the great things about QuickCheck is that it is automatically 
 compositional.
 What I mean by this is that all you need in your instance is how to form a 
 Queue [a] given [a], because there are already QuickCheck instances for 
 forming lists, and as long as a is pretty standard (Integers is fine) then 
 there's likely an Arbitrary instance for that as well.
 
 So (from my head, not actually tested in GHC):
 
 import Control.Applicative
 import Test.QuickCheck
 
 instance Arbitrary Queue where
   arbitrary = Queue $ arbitrary
 
 Then you can use this as:
 
 testProperty length is something propQInts
 
 propQInts t = length t == 
where types = (t :: Queue Integers)
 
 The where clause is a fancy way of specifying what the type of t should be 
 without having to express the overall type of propQInts.  You could use a 
 more conventional type specification as well.
 
 
 2. If I wanted to specify/constrain the ranges of random Integers generated, 
 how would I do this?
 
 Probably something like this:
 
 instance Arbitrary Queue where
arbitrary = do li - listOf $ arbitrary
  lr - liftM $ map rangelimit li
  return $ Queue lr
where rangelimit n = case (n  LOW, n  HIGH) of
   (True,_) - LOW
   (_,True) - HIGH
   _ - n
 
 
 
 3. If I wanted to specify/constrain the Queue sizes how would I do this?
 
 Similar to #2.  Perhaps:
 
   arbitrary = arbitrary = (return . Queue . take CNT . listOf)
 
 
 -- 
 -KQ
 
 ___
 Haskell-Cafe mailing list
 Haskell-Cafe@haskell.org
 http://www.haskell.org/mailman/listinfo/haskell-cafe
 
 


___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


Re: [Haskell-cafe] QuickCheck Questions

2011-07-24 Thread Ivan Lazar Miljenovic
On 25 July 2011 14:31, Mark Spezzano mark.spezz...@chariot.net.au wrote:
 Hi Kevin,

 Thanks for the response. The first part works well with minor modifications.

 Part 2 is still a bit vague to me. I basically want to clamp the Integers 
 generated within the Queue to between 0 and some positive number. At present 
 they're giving me numbers all over the place (specifically negative number)

QuickCheck has a NonNegative newtype wrapper you can use for the =0
criteria; to specify the maximum, use the resize function (the
instance for Int, etc. use the current size parameter as the maximum
value).

-- 
Ivan Lazar Miljenovic
ivan.miljeno...@gmail.com
IvanMiljenovic.wordpress.com

___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


[Haskell-cafe] Quickcheck: Help on non trivial test

2011-06-23 Thread jean-christophe mincke
Hello Café,

I am using quicheck for some kind of non trivial tests.

The general form of these tesst is summarized by the following code.


-- Function to be tested. Given a list of splitting functions, split the
given list
process :: [a] - [[a] - [a]] - [a]
process l splitFuns =
List.foldl proc l splitFuns
where
proc l splitFun = splitFun l

-- a split function generator
splitFunGen :: Gen ([a] - [a])
splitFunGen = return $ proc
  where
  proc l = let splitPos = List.length l `div` 2 --
Problem I would like  splitPos = some random value between [0 and length of
l]
  in fst $ List.splitAt splitPos l

splitFunsGen :: Gen  [[a] - [a]]
splitFunsGen = vectorOf 20 splitFunGen

instance Show a = Show ([a] - [a]) where
show _ =  a split fun 

r = quickCheck $ forAll splitFunsGen prop
where
prop splitFuns = let l = process [1..100] splitFuns
in List.length l = 0 -- dummy test here for the
sake of example


The process to be tested takes a list that is randomly perturbated
(spitFuns). The result of each perturbation is fed into the next
perturbation.

Ideally I would like the perturbating function to depend on the previous
perturbated list (see computation of splitPos in splitFunGen).

I am not sure how I could use quickcheck in this case.

Has anyone a better idea?

Thanks

J-C
___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


Re: [Haskell-cafe] Quickcheck: Help on non trivial test

2011-06-23 Thread Arnaud Bailly
Hello,
What if the '2' that appears in splitPos is itself generated (ie. an int
parameter of splitFunGen) and you replace `div` by `mod`? Sorry, I have no
code to show right now.

HTH
Arnaud

On Thu, Jun 23, 2011 at 10:58 AM, jean-christophe mincke 
jeanchristophe.min...@gmail.com wrote:

 Hello Café,

 I am using quicheck for some kind of non trivial tests.

 The general form of these tesst is summarized by the following code.


 -- Function to be tested. Given a list of splitting functions, split the
 given list
 process :: [a] - [[a] - [a]] - [a]
 process l splitFuns =
 List.foldl proc l splitFuns
 where
 proc l splitFun = splitFun l

 -- a split function generator
 splitFunGen :: Gen ([a] - [a])
 splitFunGen = return $ proc
   where
   proc l = let splitPos = List.length l `div` 2 --
 Problem I would like  splitPos = some random value between [0 and length of
 l]
   in fst $ List.splitAt splitPos l

 splitFunsGen :: Gen  [[a] - [a]]
 splitFunsGen = vectorOf 20 splitFunGen

 instance Show a = Show ([a] - [a]) where
 show _ =  a split fun 

 r = quickCheck $ forAll splitFunsGen prop
 where
 prop splitFuns = let l = process [1..100] splitFuns
 in List.length l = 0 -- dummy test here for
 the sake of example


 The process to be tested takes a list that is randomly perturbated
 (spitFuns). The result of each perturbation is fed into the next
 perturbation.

 Ideally I would like the perturbating function to depend on the previous
 perturbated list (see computation of splitPos in splitFunGen).

 I am not sure how I could use quickcheck in this case.

 Has anyone a better idea?

 Thanks

 J-C


 ___
 Haskell-Cafe mailing list
 Haskell-Cafe@haskell.org
 http://www.haskell.org/mailman/listinfo/haskell-cafe


___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


Re: [Haskell-cafe] QuickCheck, (Ord a)= [a] - Property problem

2011-04-22 Thread larry.liuxinyu
Hi,

I tested with Haskell platform 2011 with QuickCheck 2.4.0.1.
It produced 100 cases passed, but can't report failed case.
verboseCheck still told me that [(), (), ... ()] are generated as
instance to (Ord a)

The only way is to specify the non-ambitious type for example, Int,
like below:

test (prop_foo::[Int]-Property)

Cheers.
--
Larry.

On Apr 22, 5:56 am, Nick Smallbone nick.smallb...@gmail.com wrote:
 larry.liuxinyu liuxiny...@gmail.com writes:
  Somebody told me that:
  Eduard Sergeev • BTW, more recent QuickCheck (from Haskell Platform
  2011.2.0.X - contains QuickCheck-2.4.0.1) seems to identifies the
  problem correctly:

  *** Failed! Falsifiable (after 3 tests and 2 shrinks):
  [0,1]
  False

 I don't think this can be true: the problem occurs in GHCi and there's
 no way for QuickCheck to detect it. And when I tested it I got the same
 problem. There must be some difference between the properties you both
 tested...

 Nick

 ___
 Haskell-Cafe mailing list
 Haskell-C...@haskell.orghttp://www.haskell.org/mailman/listinfo/haskell-cafe

___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


Re: [Haskell-cafe] QuickCheck, (Ord a)= [a] - Property problem

2011-04-21 Thread Nick Smallbone
larry.liuxinyu liuxiny...@gmail.com writes:

 Somebody told me that:
 Eduard Sergeev • BTW, more recent QuickCheck (from Haskell Platform
 2011.2.0.X - contains QuickCheck-2.4.0.1) seems to identifies the
 problem correctly:

 *** Failed! Falsifiable (after 3 tests and 2 shrinks):
 [0,1]
 False

I don't think this can be true: the problem occurs in GHCi and there's
no way for QuickCheck to detect it. And when I tested it I got the same
problem. There must be some difference between the properties you both
tested...

Nick


___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


Re: [Haskell-cafe] QuickCheck, (Ord a)= [a] - Property problem

2011-04-21 Thread Maciej Marcin Piechotka
On Thu, 2011-04-21 at 23:56 +0200, Nick Smallbone wrote:
 larry.liuxinyu liuxiny...@gmail.com writes:
 
  Somebody told me that:
  Eduard Sergeev • BTW, more recent QuickCheck (from Haskell Platform
  2011.2.0.X - contains QuickCheck-2.4.0.1) seems to identifies the
  problem correctly:
 
  *** Failed! Falsifiable (after 3 tests and 2 shrinks):
  [0,1]
  False
 
 I don't think this can be true: the problem occurs in GHCi and there's
 no way for QuickCheck to detect it. And when I tested it I got the same
 problem. There must be some difference between the properties you both
 tested...
 
 Nick
 

There was an additional class contrain (Num a). Num default to Int IIRC.

Regards


signature.asc
Description: This is a digitally signed message part
___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


Re: [Haskell-cafe] QuickCheck, (Ord a)= [a] - Property problem

2011-04-21 Thread Ivan Lazar Miljenovic
On 22 April 2011 09:36, Maciej Marcin Piechotka uzytkown...@gmail.com wrote:
 On Thu, 2011-04-21 at 23:56 +0200, Nick Smallbone wrote:
 larry.liuxinyu liuxiny...@gmail.com writes:

  Somebody told me that:
  *** Failed! Falsifiable (after 3 tests and 2 shrinks):
  [0,1]
  False

 I don't think this can be true: the problem occurs in GHCi and there's
 no way for QuickCheck to detect it. And when I tested it I got the same
 problem. There must be some difference between the properties you both
 tested...

 There was an additional class contrain (Num a). Num default to Int IIRC.

Integer I believe.

-- 
Ivan Lazar Miljenovic
ivan.miljeno...@gmail.com
IvanMiljenovic.wordpress.com

___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


[Haskell-cafe] QuickCheck, (Ord a)= [a] - Property problem

2011-04-20 Thread larry.liuxinyu
Hi,

I found there is similar question as:
http://groups.google.com/group/haskell-cafe/browse_thread/thread/7439262e9ac80dd2/91ca18e11ff00649?lnk=gstq=QuickCheck+Ord+a#91ca18e11ff00649

I am still think it's very strange. For example:

prop_foo :: (Ord a) = [a] - Property
prop_foo xs = not (null xs) == maximum xs == minimum xs

This is an extreme case that the property is always wrong.

However, QuickCheck produces:
*Main test prop_foo
OK, passed 100 tests.

Why this happen? If I use verboseCheck, I can find the sample test
data are as the following:
*MainverboseCheck prop_foo
...
97:
[(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),()]
98:
[(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),
(),(),(),(),(),(),()]
99:
[(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),
(),(),()]
OK, passed 100 tests.

So Arbitrary () is generated as the instance of Ord a. I have to
change the prototype as
prop_foo :: (Num a) = [a] - Property

This works at least, However, since 'a''b', they are order-able, what
if I want to test prop_foo works for char?

I am using Haskell Platform (version 6.10.4), with QuickCheck version
1.2.0.0

Thanks
--
Larry, LIU Xinyu
https://sites.google.com/site/algoxy/home

___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


Re: [Haskell-cafe] QuickCheck, (Ord a)= [a] - Property problem

2011-04-20 Thread Daniel Fischer
On Wednesday 20 April 2011 11:43:08, larry.liuxinyu wrote:
 Hi,
 
 I found there is similar question as:
 http://groups.google.com/group/haskell-cafe/browse_thread/thread/7439262
 e9ac80dd2/91ca18e11ff00649?lnk=gstq=QuickCheck+Ord+a#91ca18e11ff00649
 
 I am still think it's very strange. For example:
 
 prop_foo :: (Ord a) = [a] - Property
 prop_foo xs = not (null xs) == maximum xs == minimum xs
 
 This is an extreme case that the property is always wrong.

Not always, replicate n x has this property.

 
 However, QuickCheck produces:
 *Main test prop_foo
 OK, passed 100 tests.

 
 This works at least, However, since 'a''b', they are order-able, what
 if I want to test prop_foo works for char?

ghci test (prop_foo :: [Char] - Property)

You have to determine the type, by an explicit signature or by context.

___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


Re: [Haskell-cafe] QuickCheck, (Ord a)= [a] - Property problem

2011-04-20 Thread Nick Smallbone
larry.liuxinyu liuxiny...@gmail.com writes:

 prop_foo :: (Ord a) = [a] - Property
 prop_foo xs = not (null xs) == maximum xs == minimum xs

 This is an extreme case that the property is always wrong.

 However, QuickCheck produces:
 *Main test prop_foo
 OK, passed 100 tests.

 Why this happen? If I use verboseCheck, I can find the sample test
 data are as the following:
 *MainverboseCheck prop_foo
 ...
 97:
 [(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),()]
 98:
 [(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),
 (),(),(),(),(),(),()]
 99:
 [(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),
 (),(),()]
 OK, passed 100 tests.

This is an unfortunate feature of GHCi: if the thing you want to
evaluate has a polymorphic type then all the type variables default to
(), see:
  
http://www.haskell.org/ghc/docs/7.0.3/html/users_guide/interactive-evaluation.html#extended-default-rules
So prop_foo is only tested for lists of (). Nasty.

The usual way to work around it is to declare all your properties
monomorphic, so write:
  prop_foo :: [Integer] - Property

 This works at least, However, since 'a''b', they are order-able, what
 if I want to test prop_foo works for char?

Testing with Integers should always[*] be enough because of
parametricity.

Nick

[*] For certain values of always :)


___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


Re: [Haskell-cafe] QuickCheck, (Ord a)= [a] - Property problem

2011-04-20 Thread larry.liuxinyu
Hi,

Thanks a lot.

The following type protocol also works.
prop_foo :: (Ord a)=(Num a) = [a] - Property

Somebody told me that:
Eduard Sergeev • BTW, more recent QuickCheck (from Haskell Platform
2011.2.0.X - contains QuickCheck-2.4.0.1) seems to identifies the
problem correctly:

*** Failed! Falsifiable (after 3 tests and 2 shrinks):
[0,1]
False

--
Larry

On Apr 20, 11:36 pm, Nick Smallbone nick.smallb...@gmail.com wrote:
 larry.liuxinyu liuxiny...@gmail.com writes:
  prop_foo :: (Ord a) = [a] - Property
  prop_foo xs = not (null xs) == maximum xs == minimum xs

  This is an extreme case that the property is always wrong.

  However, QuickCheck produces:
  *Main test prop_foo
  OK, passed 100 tests.

  Why this happen? If I use verboseCheck, I can find the sample test
  data are as the following:
  *MainverboseCheck prop_foo
  ...
  97:
  [(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),()]
  98:
  [(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),
  (),(),(),(),(),(),()]
  99:
  [(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),
  (),(),()]
  OK, passed 100 tests.

 This is an unfortunate feature of GHCi: if the thing you want to
 evaluate has a polymorphic type then all the type variables default to
 (), see:
  http://www.haskell.org/ghc/docs/7.0.3/html/users_guide/interactive-ev...
 So prop_foo is only tested for lists of (). Nasty.

 The usual way to work around it is to declare all your properties
 monomorphic, so write:
   prop_foo :: [Integer] - Property

  This works at least, However, since 'a''b', they are order-able, what
  if I want to test prop_foo works for char?

 Testing with Integers should always[*] be enough because of
 parametricity.

 Nick

 [*] For certain values of always :)

 ___
 Haskell-Cafe mailing list
 Haskell-C...@haskell.orghttp://www.haskell.org/mailman/listinfo/haskell-cafe

___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


[Haskell-cafe] QuickCheck and Pairing Heaps

2010-08-10 Thread Matias Eyzaguirre
Hi, I'm trying to figure out QuickCheck and how to generate arbitrary test
data form complex data structures; I thought an interesting experiment would
be Pairing Heaps.
A pairing heap is essentially just a tree whose nodes obey the heap property
(for all nodes A in a pairing whose nodes can be compared, the value of A is
greater then the value of any descendants of A).
I came with a definition that I think seems reasonable
\begin{code}
  data (Ord a) = Heap a = Heap a [Heap a]
\end{code}
But I cannot think of a way to generate arbitrary heaps, I see three main
issues:
1. Based on the examples I have seen, I understand how you could define
arbitrary so that it uses frequency to choose either a leaf or a branch, but
how would you generate a variable number of children?
2. That would hardcode the probably size of the heap, how would you define
an arbitrary heap that was dependent on some sort of size parameter?
3. The way I see it, you could generate arbitrary heaps of type A (where A
is an instance of Arbitrary and Ord), but how would you make sure that each
node of the generated tree was heapy (the max of all its descendants)?
___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


[Haskell-cafe] QuickCheck 2

2010-06-10 Thread wren ng thornton
Since GHC 6.12 ships with QC2 it looks like it's finally time to get 
around to converting some old testing scripts. Unfortunately, one of the 
things I couldn't figure out last time I looked (and hence why I haven't 
switched) is how to reconfigure the configuration parameters to the 
driver function. Is there a porting guide anywhere, or how else can I 
adjust the configuration parameters (in particular, the configMaxTest 
and configMaxFail parameters)?


--
Live well,
~wren

___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


Re: [Haskell-cafe] QuickCheck 2

2010-06-10 Thread wren ng thornton

wren ng thornton wrote:
Since GHC 6.12 ships with QC2 it looks like it's finally time to get 
around to converting some old testing scripts. Unfortunately, one of the 
things I couldn't figure out last time I looked (and hence why I haven't 
switched) is how to reconfigure the configuration parameters to the 
driver function. Is there a porting guide anywhere, or how else can I 
adjust the configuration parameters (in particular, the configMaxTest 
and configMaxFail parameters)?


Ah, nevermind. I found what they renamed things to

--
Live well,
~wren
___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


Re: [Haskell-cafe] QuickCheck 2

2010-06-10 Thread Ivan Lazar Miljenovic
wren ng thornton w...@freegeek.org writes:

 Since GHC 6.12 ships with QC2 it looks like it's finally time to get
 around to converting some old testing scripts.

Well, the Haskell Platform does, not GHC...

 Unfortunately, one of the things I couldn't figure out last time I
 looked (and hence why I haven't switched) is how to reconfigure the
 configuration parameters to the driver function.  Is there a porting
 guide anywhere, or how else can I adjust the configuration parameters
 (in particular, the configMaxTest and configMaxFail parameters)?

I'm not sure what you mean by driver function, but there is
quickCheckWith:
http://hackage.haskell.org/packages/archive/QuickCheck/2.1.0.3/doc/html/Test-QuickCheck.html#v%3AquickCheckWith
(and also quickCheckWithResult) which let you customise the number of
tests you want, etc.  Is that what you were after?

-- 
Ivan Lazar Miljenovic
ivan.miljeno...@gmail.com
IvanMiljenovic.wordpress.com
___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


Re: [Haskell-cafe] QuickCheck 2

2010-06-10 Thread wren ng thornton

Ivan Lazar Miljenovic wrote:

wren ng thornton w...@freegeek.org writes:


Since GHC 6.12 ships with QC2 it looks like it's finally time to get
around to converting some old testing scripts.


Well, the Haskell Platform does, not GHC...


Fair enough (it was one of the two :)



Unfortunately, one of the things I couldn't figure out last time I
looked (and hence why I haven't switched) is how to reconfigure the
configuration parameters to the driver function.  Is there a porting
guide anywhere, or how else can I adjust the configuration parameters
(in particular, the configMaxTest and configMaxFail parameters)?


I'm not sure what you mean by driver function, but there is
quickCheckWith:
http://hackage.haskell.org/packages/archive/QuickCheck/2.1.0.3/doc/html/Test-QuickCheck.html#v%3AquickCheckWith
(and also quickCheckWithResult) which let you customise the number of
tests you want, etc.  Is that what you were after?


Yeah, that's the one.

--
Live well,
~wren
___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


[Haskell-cafe] QuickCheck: test passes in GHCi, fails when compiled

2010-01-13 Thread Lauri Pesonen
I provided a Java solution to a problem of returning the first digit
of an integer on StackOverflow and someone wondered if there were any
floating point point problems with the solution, so I though I'd
implement the algorithm in Haskell and run QuickCheck on it.
Everything works fine on GHCi, but if I compile the code and run the
test, it fails with -1000, 1000, -100. Any ideas why?

In any case it seems that the commenter was right and there are some
subtle problems with my solution. I'd just like to know more
details...

import Data.Char
import Test.QuickCheck

-- my solution
getFirstDigit :: Int - Int
getFirstDigit 0 = 0
getFirstDigit x = let x' = abs x
  digits = (floor $ logBase 10 $ fromIntegral x')
  in x' `div` (floor $ 10 ** (fromIntegral digits))

-- two reference implementation that agree with each other
getFirstDigitRef1 :: Int - Int
getFirstDigitRef1 x = digitToInt $ head $ show $ abs x

getFirstDigitRef2 :: Int - Int
getFirstDigitRef2 x | x  0 = getFirstDigitRef2 (-x)
| x  10 = x
| otherwise = getFirstDigitRef2 $ x `div` 10

myTest x = getFirstDigit x == getFirstDigitRef1 x
--myTest x = getFirstDigitRef2 x == getFirstDigitRef1 x

myCheck n = check (defaultConfig { configMaxTest = n }) myTest

main = myCheck 10

-- 
  ! Lauri
___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


Re: [Haskell-cafe] QuickCheck Questions

2009-09-28 Thread Yusaku Hashimoto
After a few more investigations, I can say

QuickCheck does:
- make easy to finding couter-cases and refactoring codes
- make easy to test some functions if they have good mathematical properties
- generate random test cases

But QuickCheck does *not*:
- help us to find good properties

So what I want to know is how to find good properties. Please let me
know how do you find QuickCheck properties. There are so many
tutorials or papers for using QuickCheck, but when I try to apply them
to my programming, I often miss properties in my codes.

Cheers
-nwn
___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


Re: [Haskell-cafe] QuickCheck Questions

2009-09-28 Thread Gwern Branwen

On Mon, Sep 28, 2009 at 10:59 AM, Yusaku Hashimoto nonow...@gmail.com wrote:

After a few more investigations, I can say

QuickCheck does:
- make easy to finding couter-cases and refactoring codes
- make easy to test some functions if they have good mathematical properties
- generate random test cases

But QuickCheck does *not*:
- help us to find good properties

So what I want to know is how to find good properties. Please let me
know how do you find QuickCheck properties. There are so many
tutorials or papers for using QuickCheck, but when I try to apply them
to my programming, I often miss properties in my codes.

Cheers
-nwn


I don't think there's any automated way to come up with good properties given a 
function; that seems tantamount to solving AI.

One thing I've meant to do is look at checkers: 
http://hackage.haskell.org/package/checkers - which I understand includes a 
collection of lots of properties for various datatypes and classes. Presumably 
you could look through it for ideas ('ooh, I forgot that my results should 
monotonically increase!') or pick the module closest to what you're testing and 
steal as many of its properties as you can.

--
gwern

signature.asc
Description: OpenPGP digital signature
___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


Re: [Haskell-cafe] QuickCheck Questions

2009-09-28 Thread Emil Axelsson
Not sure this is what you want, but I thought I'd mention Formal 
Specifications for Free:


  http://www.erlang.org/euc/08/1005Hughes2.pdf

(I wasn't able to find a better link. That talk is for Erlang, but 
people are working on this for Haskell QuickCheck.)


/ Emil



Yusaku Hashimoto skrev:

After a few more investigations, I can say

QuickCheck does:
- make easy to finding couter-cases and refactoring codes
- make easy to test some functions if they have good mathematical properties
- generate random test cases

But QuickCheck does *not*:
- help us to find good properties

So what I want to know is how to find good properties. Please let me
know how do you find QuickCheck properties. There are so many
tutorials or papers for using QuickCheck, but when I try to apply them
to my programming, I often miss properties in my codes.

Cheers
-nwn
___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe

___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


Re: [Haskell-cafe] QuickCheck Questions

2009-09-28 Thread Duncan Coutts
On Mon, 2009-09-28 at 23:59 +0900, Yusaku Hashimoto wrote:
 After a few more investigations, I can say
 
 QuickCheck does:
 - make easy to finding couter-cases and refactoring codes
 - make easy to test some functions if they have good mathematical properties
 - generate random test cases
 
 But QuickCheck does *not*:
 - help us to find good properties
 
 So what I want to know is how to find good properties. Please let me
 know how do you find QuickCheck properties.

This requires thought and practise. The properties are a partial formal
description of what your program does. You have to decide what your
program is supposed to do. There is no magic here.

Sometimes you find that you adjust your properties to fit the program
and sometimes adjust the program to fit the properties.

To gain experience in thinking about properties of programs you probably
want to look at books and articles and try examples. This is the kind of
thing they teach in CS degrees. It is a matter of training yourself.

Duncan

___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


Re: [Haskell-cafe] QuickCheck Questions

2009-09-28 Thread Pasqualino Titto Assini
Fantastic.

If I understand correctly it inductively derives equations that hold
for a set of examples.

I am looking forward to see it in Haskell, who is working on the port?

 titto

2009/9/28 Emil Axelsson e...@chalmers.se:
 Not sure this is what you want, but I thought I'd mention Formal
 Specifications for Free:

  http://www.erlang.org/euc/08/1005Hughes2.pdf

 (I wasn't able to find a better link. That talk is for Erlang, but people
 are working on this for Haskell QuickCheck.)

 / Emil



 Yusaku Hashimoto skrev:

 After a few more investigations, I can say

 QuickCheck does:
 - make easy to finding couter-cases and refactoring codes
 - make easy to test some functions if they have good mathematical
 properties
 - generate random test cases

 But QuickCheck does *not*:
 - help us to find good properties

 So what I want to know is how to find good properties. Please let me
 know how do you find QuickCheck properties. There are so many
 tutorials or papers for using QuickCheck, but when I try to apply them
 to my programming, I often miss properties in my codes.

 Cheers
 -nwn
 ___
 Haskell-Cafe mailing list
 Haskell-Cafe@haskell.org
 http://www.haskell.org/mailman/listinfo/haskell-cafe

 ___
 Haskell-Cafe mailing list
 Haskell-Cafe@haskell.org
 http://www.haskell.org/mailman/listinfo/haskell-cafe




-- 
Pasqualino Titto Assini, Ph.D.
http://quicquid.org/
___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


Re: [Haskell-cafe] QuickCheck Questions

2009-09-28 Thread Emil Axelsson

Pasqualino Titto Assini skrev:

Fantastic.

If I understand correctly it inductively derives equations that hold
for a set of examples.


AFAIU, it enumerates a set of terms and uses random testing to 
approximate an equivalence relation for these. The real trick, 
apparently, is in filtering out the interesting equations.



I am looking forward to see it in Haskell, who is working on the port?


John Hughes, Koen Claessen and Nick Smallbone. (At least.)

/ Emil

___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


[Haskell-cafe] QuickCheck Questions

2009-09-27 Thread Yusaku Hashimoto
Hello, I recently worked with QuickCheck for a while, But I still
can't handle it well, And a few questions  come to my mind.

1. How to find properties

In QuickCheck examples on the codes or the papers, they find good
properties easily. How did they find these properties? What property
can make us believed its implementation is collect?

2. Property driven developments

But finally, I could find some patterns of the properties. For
example, occasionally, their shape is like

  unF . f == id
  or
  unF . f == Just

I thought it could be applied to writing parser, and lead me to
Property Driven Developments. I tried writing S-expression parser in
this way. First, I defined my property as

parseSexpr . prettySexpr == Just

where parseSexpr :: String - Maybe Sexpr, prettySexpr :: Sexpr -
String. As you see, I should write printer before parser. Fortunately
writing such printer for S-expr is easy, but I don't know if what I
did is right.

Do you think I wasted times? Have you ever tried PDD? And has it
worked? If you have experience with TDD, how do you think about PDD?

If you have any answers in any questions above, please tell me them.
Thanks in advance.

Cheers
-nwn
___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


Re: [Haskell-cafe] QuickCheck Questions

2009-09-27 Thread Gwern Branwen

On Sun, Sep 27, 2009 at 3:19 PM, Yusaku Hashimoto nonow...@gmail.com wrote:
...

Do you think I wasted times? Have you ever tried PDD? And has it
worked? If you have experience with TDD, how do you think about PDD?

If you have any answers in any questions above, please tell me them.
Thanks in advance.

Cheers
-nwn


Here are some links from my Wikipedia article on QC which tout it:

- http://haskell.org/haskellwiki/QuickCheck_as_a_test_set_generator
- 
http://blog.moertel.com/articles/2006/10/31/introductory-haskell-solving-the-sorting-it-out-kata
- http://blog.moertel.com/pages/seven-lessons-from-the-icfp-programming-contest
- http://neilmitchell.blogspot.com/2006/11/systemfilepath-automated-testing.html
- http://book.realworldhaskell.org/read/testing-and-quality-assurance.html

--
gwern

signature.asc
Description: OpenPGP digital signature
___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


Re: [Haskell-cafe] QuickCheck Questions

2009-09-27 Thread Yusaku Hashimoto
On Mon, Sep 28, 2009 at 4:42 AM, Gwern Branwen gwe...@gmail.com wrote:
 On Sun, Sep 27, 2009 at 3:19 PM, Yusaku Hashimoto nonow...@gmail.com
 wrote:
 ...

 Do you think I wasted times? Have you ever tried PDD? And has it
 worked? If you have experience with TDD, how do you think about PDD?

 If you have any answers in any questions above, please tell me them.
 Thanks in advance.

 Cheers
 -nwn

 Here are some links from my Wikipedia article on QC which tout it:

Thanks for pointers. But I feel curious about in many QC examples
(especially RWH's in your pointers), if property is falsifiable, they
changes definition of the property, not implementation. And it annoys
me because it seemed to almost duplicate its implementation. Am I
misunderstanding?

Cheers
-nwn
___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


Re: [Haskell-cafe] Quickcheck for common typeclass laws

2009-08-14 Thread Henning Thielemann


On Sun, 9 Aug 2009, Jeremy Shaw wrote:


This perhaps?

http://hackage.haskell.org/package/checkers


It seems dangerous to me to supply orphan instances in a package which is 
not obviously related to QuickCheck. I propose to get approval from the 
QuickCheck authors, that the instances can be considered official and 
use a package name which reflects that. The package name should start with 
QuickCheck in order to get the 'checkers' package close to QuickCheck in a 
lexicographic list. Then people cannot miss it so easily.

___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


[Haskell-cafe] Quickcheck for common typeclass laws

2009-08-09 Thread Job Vranish
Is there a hackage package that contains quickcheck properties for the laws
of common typeclasses?
(Functor, Monad, Num, Ord, Eq, Applicative, etc...)
so that one could quickly check (har har) that their new instances satisfy
the appropriate laws?

It would be very nice to have a
isValidMonad (undefined :: MyNewMonadType)
function.

If one does not exist, I may just have to make one.

- Job
___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


Re: [Haskell-cafe] Quickcheck for common typeclass laws

2009-08-09 Thread Jeremy Shaw
This perhaps? 

http://hackage.haskell.org/package/checkers

- jeremy 

At Sun, 9 Aug 2009 17:44:05 -0400,
Job Vranish wrote:
 
 [1  multipart/alternative (7bit)]
 [1.1  text/plain; ISO-8859-1 (7bit)]
 Is there a hackage package that contains quickcheck properties for the laws
 of common typeclasses?
 (Functor, Monad, Num, Ord, Eq, Applicative, etc...)
 so that one could quickly check (har har) that their new instances satisfy
 the appropriate laws?
 
 It would be very nice to have a
 isValidMonad (undefined :: MyNewMonadType)
 function.
 
 If one does not exist, I may just have to make one.
 
 - Job
 [1.2  text/html; ISO-8859-1 (quoted-printable)]
 
 [2  text/plain; us-ascii (7bit)]
 ___
 Haskell-Cafe mailing list
 Haskell-Cafe@haskell.org
 http://www.haskell.org/mailman/listinfo/haskell-cafe
___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


[Haskell-cafe] QuickCheck behaving strange

2009-07-24 Thread Tobias Olausson
Hey Guys!
I was writing a small implementation of the earliest-end-first algorithm
for the Interval Scheduling problem just now. When I was done, I thought
it would be a nice thing to have a QuickCheck property for my code. This
is what I came up with:

-- Intervals are just triplets
type Interval a t = (a,t,t)

end :: Interval a t - t
end (_,_,t) = t

begin :: Interval a t - t
begin (_,t,_) = t

And so the property:

prop_schedule :: Ord t = [Interval a t] - Bool
prop_schedule []= True
prop_schedule [a]   = True
prop_schedule (x:y:ys)  = end x = begin y  prop_schedule (y:ys)

Essentially, it looks up wheather the given list is sorted (given
the constraints
of the problem). However, in this property I forgot to add that the
lists should have
been run through my algorithm, which I noticed only after this strange problem
appeared:

*Interval quickCheck prop_schedule
+++ OK, passed 100 tests.

How come QuickCheck passes 100 tests of random lists? One would think that
at least one of the generated lists would be unsorted. It also passes
1000 and even
1 tests.

It also seems that changing the type helps:
prop_schedule :: [Interval a Int] - Bool
...
*Interval quickCheck prop_schedule
*** Failed! Falsifiable (after 5 tests and 1 shrink):
[((),0,0),((),-1,0)]

-- 
Tobias Olausson
tob...@gmail.com
___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


Re: [Haskell-cafe] QuickCheck behaving strange

2009-07-24 Thread Felipe Lessa
On Fri, Jul 24, 2009 at 08:11:12PM +0200, Tobias Olausson wrote:
 prop_schedule :: Ord t = [Interval a t] - Bool
 prop_schedule []= True
 prop_schedule [a]   = True
 prop_schedule (x:y:ys)  = end x = begin y  prop_schedule (y:ys)
[..]
 How come QuickCheck passes 100 tests of random lists? One would think that
 at least one of the generated lists would be unsorted. It also passes
 1000 and even
 1 tests.

Probably it was defaulting to 'Interval () ()'.  Try to do

   :s -Wall

on your GHCi prompt to see when these things happen.

--
Felipe.
___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


Re: [Haskell-cafe] QuickCheck behaving strange

2009-07-24 Thread Jason Dagit
On Fri, Jul 24, 2009 at 11:29 AM, Felipe Lessa felipe.le...@gmail.comwrote:

 On Fri, Jul 24, 2009 at 08:11:12PM +0200, Tobias Olausson wrote:
  prop_schedule :: Ord t = [Interval a t] - Bool
  prop_schedule []= True
  prop_schedule [a]   = True
  prop_schedule (x:y:ys)  = end x = begin y  prop_schedule (y:ys)
 [..]
  How come QuickCheck passes 100 tests of random lists? One would think
 that
  at least one of the generated lists would be unsorted. It also passes
  1000 and even
  1 tests.

 Probably it was defaulting to 'Interval () ()'.  Try to do


Cases like this make me feel as though the instance of Ord for () was  a
mistake.

Jason
___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


Re: [Haskell-cafe] QuickCheck behaving strange

2009-07-24 Thread Tobias Olausson
It seems this was the case, thank you!

/Tobias

2009/7/24 Felipe Lessa felipe.le...@gmail.com:
 On Fri, Jul 24, 2009 at 08:11:12PM +0200, Tobias Olausson wrote:
 prop_schedule :: Ord t = [Interval a t] - Bool
 prop_schedule []        = True
 prop_schedule [a]       = True
 prop_schedule (x:y:ys)  = end x = begin y  prop_schedule (y:ys)
 [..]
 How come QuickCheck passes 100 tests of random lists? One would think that
 at least one of the generated lists would be unsorted. It also passes
 1000 and even
 1 tests.

 Probably it was defaulting to 'Interval () ()'.  Try to do

   :s -Wall

 on your GHCi prompt to see when these things happen.

 --
 Felipe.
 ___
 Haskell-Cafe mailing list
 Haskell-Cafe@haskell.org
 http://www.haskell.org/mailman/listinfo/haskell-cafe




-- 
Tobias Olausson
tob...@gmail.com
___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


Re: [Haskell-cafe] QuickCheck behaving strange

2009-07-24 Thread Miguel Mitrofanov
I've felt a bit stupid using instance Monoid ()... but it seemed quite  
natural.


On 24 Jul 2009, at 22:33, Jason Dagit wrote:




On Fri, Jul 24, 2009 at 11:29 AM, Felipe Lessa  
felipe.le...@gmail.com wrote:

On Fri, Jul 24, 2009 at 08:11:12PM +0200, Tobias Olausson wrote:
 prop_schedule :: Ord t = [Interval a t] - Bool
 prop_schedule []= True
 prop_schedule [a]   = True
 prop_schedule (x:y:ys)  = end x = begin y  prop_schedule (y:ys)
[..]
 How come QuickCheck passes 100 tests of random lists? One would  
think that
 at least one of the generated lists would be unsorted. It also  
passes

 1000 and even
 1 tests.

Probably it was defaulting to 'Interval () ()'.  Try to do

Cases like this make me feel as though the instance of Ord for ()  
was  a mistake.


Jason

___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


[Haskell-cafe] Quickcheck in industry - own experience.

2008-12-05 Thread Gianfranco Alongi
http://writert.blogspot.com/2008/12/quickchecking-code-i-c.html

/Gf
___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


[Haskell-cafe] QuickCheck: outdated manual

2008-07-09 Thread Roman Cheplyaka
Online QC manual[1] says[2] that 'vector' takes number of elements and
generator, while in QuickCheck-1.1.0.0 it takes only number and
generates vector of arbitrary's. Please fix that.

By the way, I find the old version as useful as the new one.
Although both are trivially implemented, I don't see any reason of why
one is included and the other is not. Does anyone?

  1. http://www.cs.chalmers.se/~rjmh/QuickCheck/manual.html
  2. http://www.cs.chalmers.se/~rjmh/QuickCheck/manual_body.html#17
-- 
Roman I. Cheplyaka :: http://ro-che.info/
...being in love is totally punk rock...
___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


Re: [Haskell-cafe] QuickCheck

2008-03-17 Thread Ryan Ingram
2008/3/16 Sebastian Sylvan [EMAIL PROTECTED]:
 featureGenNormal = do
 id - stringGen
 name - stringGen
 featuretype - arbitrary
  grouptype - arbitrary
 children - arbitrary
 properties - listGen stringGen
 return (Feature id name featuretype grouptype children properties)


 Note that we use arbitrary to generate the list of children recursively.

Also, you can shorten this significantly with liftM or ap (from Control.Monad):
 featureGenNormal = liftM6 Feature stringGen stringGen arbitrary arbitrary 
 arbitrary (listGen stringGen)
 featureGenNormal = return Feature `ap` stringGen `ap` stringGen `ap` 
 arbitrary `ap` arbitrary `ap` listGen stringGen
___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


Re: [Haskell-cafe] QuickCheck

2008-03-17 Thread Yitzchak Gale
Sebastian Sylvan:
 featureGenNormal = do
 id - stringGen
 name - stringGen
 featuretype - arbitrary
 grouptype - arbitrary
 children - arbitrary
 properties - listGen stringGen
 return (Feature id name featuretype grouptype children properties)

Ryan Ingram wrote:
  Also, you can shorten this significantly with liftM or ap (from 
 Control.Monad):

True, but in this case I like being able to see meaningful
names for each parameter of the constructor.
___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


Re: [Haskell-cafe] QuickCheck

2008-03-17 Thread rodrigo.bonifacio
Hi all,

Is it possible to define a limit for the size of children list bellow?

I've tried:

children - resize (10 (listGen featureGenNormal))

But it didn't work.

Thanks a lot,

Rodrigo.


 Sebastian Sylvan:
  featureGenNormal = do
  id - stringGen
  name - stringGen
  featuretype - arbitrary
  grouptype - arbitrary
  children - arbitrary
  properties - listGen stringGen
  return (Feature id name featuretype grouptype children properties)

 Ryan Ingram wrote:
   Also, you can shorten this significantly with liftM or ap (from 
  Control.Monad):

 True, but in this case I like being able to see meaningful
 names for each parameter of the constructor.


---
Rodrigo Bonifácio de Almeida
Universidade Católica de Brasília
 - Grupo de Engenharia de Software
 - JavaComBr (www.ucb.br/java)

___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


Re: [Haskell-cafe] QuickCheck

2008-03-17 Thread Henning Thielemann


On Mon, 17 Mar 2008, rodrigo.bonifacio wrote:


Hi all,

Is it possible to define a limit for the size of children list bellow?


take 10 ?
___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


Re: [Haskell-cafe] QuickCheck

2008-03-17 Thread Thomas Schilling


On 17 mar 2008, at 14.37, rodrigo.bonifacio wrote:


Hi all,

Is it possible to define a limit for the size of children list bellow?

I've tried:

children - resize (10 (listGen featureGenNormal))




You are calling a number as a function.

Also, listGen has to use the size argument.  Try something like (not  
tested):


  listGen =
sized (\maxSize - do
n - arbitrary
x - g
xs - frequency [ (1, return []), (n, listGen g) ]
return (x:xs)

___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


Re: [Haskell-cafe] QuickCheck

2008-03-17 Thread Sebastian Sylvan
On Mon, Mar 17, 2008 at 1:54 PM, Thomas Schilling [EMAIL PROTECTED]
wrote:


 On 17 mar 2008, at 14.37, rodrigo.bonifacio wrote:

  Hi all,
 
  Is it possible to define a limit for the size of children list bellow?
 
  I've tried:
 
  children - resize (10 (listGen featureGenNormal))
 

 You are calling a number as a function.

 Also, listGen has to use the size argument.  Try something like (not
 tested):

   listGen =
 sized (\maxSize - do
 n - arbitrary
 x - g
 xs - frequency [ (1, return []), (n, listGen g) ]
 return (x:xs)


In retrospect, this function isn't very good at all, because it never
generates the empty list... Something like this is probably better
(untested):

listGen g = sized (\maxSize - do
   count - choose (0, maxSize - 1)
   replicateM count g )


-- 
Sebastian Sylvan
+44(0)7857-300802
UIN: 44640862
___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


[Haskell-cafe] QuickCheck

2008-03-16 Thread rodrigo.bonifacio
Hi all,

I'm trying to use the quick-check library for checking some properties of a 
user defined data type. Bellow the target data type:

data Feature =
 Feature Id Name FeatureType GroupType Children Properties |
 FeatureError

where:

Id = String
Name = String
FeatureType = int
GroupType = int
Children = [Feature]
Propertyes = [String]


I've written the following quick-check property:

prop_AlternativeFeature :: Feature - Feature - QuickCheck.Property
prop_AlternativeFeature fm fc = length (children fc) == 0 == length  
(checkAlternativeFeature fm fc)  0

When I try to check such property, the result is:

ERROR ./EshopModelChecking.hs:11 - Type error in instance member binding
*** Term   : arbitrary
*** Type   : Feature
*** Does not match : Gen Feature

I think that I need to write some arbitrary or generator functions, but I 
didn't realize how to do that with the availalble quick-checking documentation.

Any help will be welcome.

Thanks in advance.

Rodrigo.

___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


Re: [Haskell-cafe] QuickCheck

2008-03-16 Thread Sebastian Sylvan
On Sun, Mar 16, 2008 at 5:42 PM, rodrigo.bonifacio 
[EMAIL PROTECTED] wrote:

 Hi all,

 I'm trying to use the quick-check library for checking some properties of
 a user defined data type. Bellow the target data type:

 data Feature =
  Feature Id Name FeatureType GroupType Children Properties |
  FeatureError

 where:

 Id = String
 Name = String
 FeatureType = int
 GroupType = int
 Children = [Feature]
 Propertyes = [String]


 I've written the following quick-check property:

 prop_AlternativeFeature :: Feature - Feature - QuickCheck.Property
 prop_AlternativeFeature fm fc = length (children fc) == 0 == length
  (checkAlternativeFeature fm fc)  0

 When I try to check such property, the result is:

 ERROR ./EshopModelChecking.hs:11 - Type error in instance member binding
 *** Term   : arbitrary
 *** Type   : Feature
 *** Does not match : Gen Feature

 I think that I need to write some arbitrary or generator functions, but I
 didn't realize how to do that with the availalble quick-checking
 documentation.

 Any help will be welcome.


You use the available functions to build up a generator for your data type.

First, let's give the instanc itself. For this I'm just going to use the
frequency function to use featureGenNormal five times more often than
return FeatureError. This means that will get a FeatureError every now and
then, but mostly you'll get featureGenNormal (see below). You can change
these frequences, of course.

instance Arbitrary Feature where
arbitrary = do
frequency [ (5, featureGenNormal),  (1, return FeatureError) ]

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 arbitrary for each component, and then
returning a datatype. However, there is no Arbitrary instance for String,
which is a bit annoying, so let's write our own generator for strings.

First a generator for a single letter:

letterGen = oneof $ map return $ ['a'..'z'] ++ ['A'..'Z']

Then a combinator for generating a list of values given a generator for a
single value:

listGen :: Gen a - Gen [a]
listGen g = do
x - g
xs - frequency [ (1, return []), (10, listGen g) ]
return (x:xs)

And then we use this to build our stringGen generator.

stringGen :: Gen String
stringGen = listGen letterGen

Now, we have all we need to write the featureGenNormal generator:

featureGenNormal = do
id - stringGen
name - stringGen
featuretype - arbitrary
grouptype - arbitrary
children - arbitrary
properties - listGen stringGen
return (Feature id name featuretype grouptype children properties)


Note that we use arbitrary to generate the list of children recursively.

-- 
Sebastian Sylvan
+44(0)7857-300802
UIN: 44640862
___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


Re: [Haskell-cafe] QuickCheck

2008-03-16 Thread rodrigo.bonifacio
Dear Sebastian Sylvan,

Thanks for your datailed answer. It saved me a lot of time.

Best regards,

Rodrigo.

 On Sun, Mar 16, 2008 at 5:42 PM, rodrigo.bonifacio 
 [EMAIL PROTECTED] wrote:

  Hi all,
 
  I'm trying to use the quick-check library for checking some properties of
  a user defined data type. Bellow the target data type:
 
  data Feature =
   Feature Id Name FeatureType GroupType Children Properties |
   FeatureError
 
  where:
 
  Id = String
  Name = String
  FeatureType = int
  GroupType = int
  Children = [Feature]
  Propertyes = [String]
 
 
  I've written the following quick-check property:
 
  prop_AlternativeFeature :: Feature - Feature - QuickCheck.Property
  prop_AlternativeFeature fm fc = length (children fc) == 0 == length
   (checkAlternativeFeature fm fc)  0
 
  When I try to check such property, the result is:
 
  ERROR ./EshopModelChecking.hs:11 - Type error in instance member binding
  *** Term   : arbitrary
  *** Type   : Feature
  *** Does not match : Gen Feature
 
  I think that I need to write some arbitrary or generator functions, but I
  didn't realize how to do that with the availalble quick-checking
  documentation.
 
  Any help will be welcome.
 
 
 You use the available functions to build up a generator for your data type.

 First, let's give the instanc itself. For this I'm just going to use the
 frequency function to use featureGenNormal five times more often than
 return FeatureError. This means that will get a FeatureError every now and
 then, but mostly you'll get featureGenNormal (see below). You can change
 these frequences, of course.

 instance Arbitrary Feature where
 arbitrary = do
 frequency [ (5, featureGenNormal),  (1, return FeatureError) ]

 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 arbitrary for each component, and then
 returning a datatype. However, there is no Arbitrary instance for String,
 which is a bit annoying, so let's write our own generator for strings.

 First a generator for a single letter:

 letterGen = oneof $ map return $ ['a'..'z'] ++ ['A'..'Z']

 Then a combinator for generating a list of values given a generator for a
 single value:

 listGen :: Gen a - Gen [a]
 listGen g = do
 x - g
 xs - frequency [ (1, return []), (10, listGen g) ]
 return (x:xs)

 And then we use this to build our stringGen generator.

 stringGen :: Gen String
 stringGen = listGen letterGen

 Now, we have all we need to write the featureGenNormal generator:

 featureGenNormal = do
 id - stringGen
 name - stringGen
 featuretype - arbitrary
 grouptype - arbitrary
 children - arbitrary
 properties - listGen stringGen
 return (Feature id name featuretype grouptype children properties)


 Note that we use arbitrary to generate the list of children recursively.

 --
 Sebastian Sylvan
 +44(0)7857-300802
 UIN: 44640862


---
Rodrigo Bonifácio de Almeida
Universidade Católica de Brasília
 - Grupo de Engenharia de Software
 - JavaComBr (www.ucb.br/java)

___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


Re: [Haskell-cafe] QuickCheck properties: export or not?

2008-01-14 Thread Don Stewart
paul:
 I was recently sent a patch for my Ranged Sets library which exported 
 all the QuickCheck properties.  I'd left them unexported on the grounds 
 that they clutter up the documentation (although simplified versions are 
 included in the documentation) and you can easily run them with the 
 standard quickcheck script.  The contributor, [EMAIL PROTECTED] 
 suggested an explicit test harness instead.
 
 I'm not unduly bothered one way or the other, but I thought I'd ask the 
 community: what is the best practice here?

Most libs seem to include an external Properties.hs or similar file.
The only tension there is how much do you need to test thoroughly.
You might still end up exporting more than you wish to.

-- Don
___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


[Haskell-cafe] QuickCheck properties: export or not?

2008-01-14 Thread Paul Johnson
I was recently sent a patch for my Ranged Sets library which exported 
all the QuickCheck properties.  I'd left them unexported on the grounds 
that they clutter up the documentation (although simplified versions are 
included in the documentation) and you can easily run them with the 
standard quickcheck script.  The contributor, [EMAIL PROTECTED] 
suggested an explicit test harness instead.


I'm not unduly bothered one way or the other, but I thought I'd ask the 
community: what is the best practice here?


Paul.

___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


Re: [Haskell-cafe] QuickCheck properties: export or not?

2008-01-14 Thread Don Stewart
ndmitchell:
 Hi
 
  standard quickcheck script.  The contributor, [EMAIL PROTECTED]
  suggested an explicit test harness instead.
 
 Unless you have a test harness (ideally through Cabal), the properties
 will go out of sync, and you'll forget to run them. Tests are good,
 they should be able to be invoked as standard. Every time I've *not*
 done a test harness, and then relied on manually remembering things or
 putting them in the documentation, it has gone wrong very quickly!

I usually have a main = runTests file, and then use darcs to ensure the
tests are up to date:

$ cat _darcs/prefs/prefs 
test ghc -no-recomp -Onot -fasm -itests tests/Unit.hs --make -odir /tmp  
tests/Unit

darcs will run the testsuite, and it needs to pass, before a commit will
be accepted. We do a similar thing for xmonad.

The test command to run on every commit can be set via,

darcs setpref test my test command

-- Don
___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


Re: [Haskell-cafe] QuickCheck properties: export or not?

2008-01-14 Thread David Benbennick
I think that type classes with nontrivial requirements should export
QuickCheck properties that test those requirements.  For example, the
Data.Monoid module
(http://haskell.org/ghc/docs/latest/html/libraries/base/Data-Monoid.html)
could export properties that check the monoid laws (for an Arbitrary
Monoid with Eq).  That would serve as a formal specification of the
requirements, and allow any user to check that their implementation is
right.

-- 
I'm doing Science and I'm still alive.
___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


[Haskell-cafe] Quickcheck generator for arrays of integers

2007-12-11 Thread Paulo J. Matos
Hello all,

I using an IArray to represent a matrix. I'm trying to write some
properties checks with Quickcheck.
Quickcheck lacks instance generators for arrays of ints for example,
is there anything I can use out there or should I define it myself?
(I'm asking because it seems to be something that other people already did)


Cheers,

-- 
Paulo Jorge Matos - pocm at soton.ac.uk
http://www.personal.soton.ac.uk/pocm
PhD Student @ ECS
University of Southampton, UK
___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


[Haskell-cafe] QuickCheck Arbitrary - infinite recursion on recursive GADTs?

2007-06-04 Thread Marc Weber
example:

data A = A INt
   | B [A]

instace Arbitrary A where
  arbitrary = oneof [ liftM A arbitrary
, liftM B arbitrary
]

But now QuickCheck will propably create a test value
A ( B [ A ( B [ A  - no end

Is there an easy QuickCheck way to prevent this?

I can think of:
a) using sized to pass when to stop (decrement the value ..)
b) using some type hackary
  data Recursive a = R a
  then use arbitrary :: R ( R ( a ))
   and remove one R each time..
c) pass a recursion indicator manually and decrement that...
  
Which would you choose?

Marc
___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


Re: [Haskell-cafe] QuickCheck Arbitrary - infinite recursion on recursive GADTs?

2007-06-04 Thread Bryan O'Sullivan

Marc Weber wrote:


data A = A INt
   | B [A]

instace Arbitrary A where
  arbitrary = oneof [ liftM A arbitrary
, liftM B arbitrary
]

But now QuickCheck will propably create a test value
A ( B [ A ( B [ A  - no end

Is there an easy QuickCheck way to prevent this?


There are two successive sections in the QuickCheck manual that cover 
exactly this topic.


http://www.cs.chalmers.se/~rjmh/QuickCheck/manual_body.html#15

b
___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


[Haskell-cafe] QuickCheck invariants for AST transformations

2007-05-08 Thread Joel Reymont
I'm looking for suggestions on how to create invariants for the  
following AST transformation code. Any suggestions are appreciated!


I asked this question before and Lennart suggested abstract  
interpretation as a solution. This would require interpreters for  
both ASTs to determine that the result they achieve is the same. I  
don't fancy writing a C# interpreter, though, so I'm looking for an  
easier way out.


Thanks, Joel

[1] http://tinyurl.com/368whq

---

instance SharpTransform C.Type Type where
toSharp C.TyInt = return TyInt
toSharp C.TyFloat = return TyFloat
toSharp C.TyStr = return TyStr
toSharp C.TyBool = return TyBool
toSharp (C.TyArray x) = liftM2 TyArray (toSharp x) (return [])
toSharp (C.TySeries C.TyFloat) = return $ TyCustom DataSeries
toSharp (C.TySeries x) = error $ Unsupported series type:  ++  
show x

toSharp (C.TyProp x) = toSharp x
toSharp C.TyUnit = return TyVoid

instance SharpTransform C.VarIdent VarIdent where
toSharp (C.VarIdent x) = return $ VarIdent x

instance SharpTransform (Maybe C.Expr) (Maybe Expr) where
toSharp Nothing = return Nothing
toSharp (Just e) = liftM Just (toSharp e)

instance SharpTransform C.Subscript [Expr] where
toSharp xs = mapM toSharp xs

instance SharpTransform C.BarsAgo Expr where
toSharp C.Now = return $ Int 0
toSharp (C.BarsAgo e) = toSharp e


--
http://wagerlabs.com/





___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


Re: [Haskell-cafe] QuickCheck invariants for AST transformations

2007-05-08 Thread Brandon Michael Moore
On Tue, May 08, 2007 at 10:06:32AM +0100, Joel Reymont wrote:
 I'm looking for suggestions on how to create invariants for the  
 following AST transformation code. Any suggestions are appreciated!
 
 I asked this question before and Lennart suggested abstract  
 interpretation as a solution. This would require interpreters for  
 both ASTs to determine that the result they achieve is the same. I  
 don't fancy writing a C# interpreter, though, so I'm looking for an  
 easier way out.
 
   Thanks, Joel

You can't claim a translation preserves meaning, if you don't say anything
about what C# means. But, you can use a description somebody else already
wrote, like the microsoft implementation, or maybe there are formalizations
floating around for some theorem provers.

For partial specification, checking that a tranformation preserves types
is good.  If you're translating between languages you can at least define
another translation between types, and check that the type of the
translation of an expression is the translation of the expressions types.
You still need a model of the C# type system, but you shouldn't need to
trust the model if you generate code with type annotations, and any
missmatches will be caught.

You might avoid specifying the meaning of C# directly by instead assuming
that certain pairs of expression and translation have the same meaning,
whatever that is, and then use some other rules about when two expression
in the same langauge are equivalent to stretch your primitive assumptions
about translations to the correctness of your whole translation. How you
show those rules are correct, I don't know.

Brandon.
___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


[Haskell-cafe] QuickCheck testing of AST transformers

2007-04-23 Thread Joel Reymont
My previous post did not receive any replies so I thought I might try  
generalizing the problem a bit...


Suppose I'm parsing a language into a syntax tree and then  
transforming that tree into another AST representing a core  
language. The core language is a more general AST that should help  
with compiling to other languages.


My problem is how to best structure my AST transformations to be able  
to test them with QuickCheck. I suspect that I'm not going about it  
in the most optimal way so I thought I should ask for suggestions.


The transformation into the core AST applies operations to simplify,  
or desugar, the AST of the original language. Here's sample code in  
the source language which, incidentally, was recently highlighted at  
Lambda the Ultimate [1].


Array: MyArray[10](10 + 2);
Value1 = MyArray[5][10];

This declares an array of 10 elements and initializes each element to  
12. Value1 (a built-in variable) is then initialized to the value of  
element #5 as of 10 bars ago. A bar is, basically, a stock quote. The  
code is invoked on every bar and so 5 bars ago can be treated as 5  
invocations ago.


The syntax tree of the above code is a 1-1 mapping. We declare an  
array of integers of 10 elements. Initialize it to the sum of two  
integers and then assign to Value1.


[ ArrayDecs [ VarDecl (VarIdent MyArray) TyInt [Int 10]
  (Op Plus (Int 10) (Int 2)) ]
, Assign (VarIdent Value1) [] (Var (VarIdent MyArray) [Int 5]
 (BarsBack (Int 10))) ]

The desugared version does away with the array declaration  
statement and declares MyArray to be a variable of array type. Arrays  
in the core language do not remember values from one invocation to  
another but there's a data series type, so we declare a series  
variable to hold the value of element #5.


We must manually store the value of the array element in the data  
series and can then refer to the value of the series 10 data points ago.


vars = [ (MyArray, VarDecl (TyArray TyInt) [Int 10]
   (Just (Plus (Int 10) (Int 2
   , (series0, VarDecl (TySeries TyInt) [] Nothing)
   ]

code = [ AddToSeries (VarIdent series0) (Var (VarIdent MyArray)  
[Int 5])

   , Assign (Var (VarIdent Value1) [])
(Series (VarIdent series0) (Int 10))
   ]

The next step would be to take the above core syntax tree and  
transform it yet again into a C# (or other target language) AST. It's  
assumed that all target languages have a data series type.


The OCaml version of my code translated directly into the C# AST but  
I figured an intermediate syntax tree will help me translate into  
other languages such as Haskell, Erlang or OCaml.


The part I can't figure out is how to come up with a set of  
invariants for my transformations.


Should I, for example, state that every access to an array value in a  
previous invocation should introduce an extra variable to hold the  
series plus the appropriate assignment code?


Should I write the translator as a series of small transformers in  
the ST monad that can be threaded and tested separately?


Thanks in advance, Joel

[1] http://lambda-the-ultimate.org/node/2201

--
http://wagerlabs.com/





___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


Re: [Haskell-cafe] QuickCheck testing of AST transformers

2007-04-23 Thread Lennart Augustsson
Without looking into your language and transformation in more detail  
it's hard to come up with concrete suggestions.  But here are some  
anyway:


Write an interpreter for each of your languages (original AST,  
transformed AST) etc, and then use a quickcheck property stating that  
well formed programs have the same denotation before and after  
transformation, i.e., the two interpreters give the same value (you  
might need some relaxed notion of same).


You transformations are trying to get rid of some language construct,  
I presume.  So you can have some properties stating that they will be  
gone in the transformed program..


-- Lennart


On Apr 23, 2007, at 22:46 , Joel Reymont wrote:

My previous post did not receive any replies so I thought I might  
try generalizing the problem a bit...


Suppose I'm parsing a language into a syntax tree and then  
transforming that tree into another AST representing a core  
language. The core language is a more general AST that should help  
with compiling to other languages.


My problem is how to best structure my AST transformations to be  
able to test them with QuickCheck. I suspect that I'm not going  
about it in the most optimal way so I thought I should ask for  
suggestions.


The transformation into the core AST applies operations to  
simplify, or desugar, the AST of the original language. Here's  
sample code in the source language which, incidentally, was  
recently highlighted at Lambda the Ultimate [1].


Array: MyArray[10](10 + 2);
Value1 = MyArray[5][10];

This declares an array of 10 elements and initializes each element  
to 12. Value1 (a built-in variable) is then initialized to the  
value of element #5 as of 10 bars ago. A bar is, basically, a stock  
quote. The code is invoked on every bar and so 5 bars ago can be  
treated as 5 invocations ago.


The syntax tree of the above code is a 1-1 mapping. We declare an  
array of integers of 10 elements. Initialize it to the sum of two  
integers and then assign to Value1.


[ ArrayDecs [ VarDecl (VarIdent MyArray) TyInt [Int 10]
  (Op Plus (Int 10) (Int 2)) ]
, Assign (VarIdent Value1) [] (Var (VarIdent MyArray) [Int 5]
 (BarsBack (Int 10))) ]

The desugared version does away with the array declaration  
statement and declares MyArray to be a variable of array type.  
Arrays in the core language do not remember values from one  
invocation to another but there's a data series type, so we declare  
a series variable to hold the value of element #5.


We must manually store the value of the array element in the data  
series and can then refer to the value of the series 10 data points  
ago.


vars = [ (MyArray, VarDecl (TyArray TyInt) [Int 10]
   (Just (Plus (Int 10) (Int 2
   , (series0, VarDecl (TySeries TyInt) [] Nothing)
   ]

code = [ AddToSeries (VarIdent series0) (Var (VarIdent MyArray)  
[Int 5])

   , Assign (Var (VarIdent Value1) [])
(Series (VarIdent series0) (Int 10))
   ]

The next step would be to take the above core syntax tree and  
transform it yet again into a C# (or other target language) AST.  
It's assumed that all target languages have a data series type.


The OCaml version of my code translated directly into the C# AST  
but I figured an intermediate syntax tree will help me translate  
into other languages such as Haskell, Erlang or OCaml.


The part I can't figure out is how to come up with a set of  
invariants for my transformations.


Should I, for example, state that every access to an array value in  
a previous invocation should introduce an extra variable to hold  
the series plus the appropriate assignment code?


Should I write the translator as a series of small transformers in  
the ST monad that can be threaded and tested separately?


Thanks in advance, Joel

[1] http://lambda-the-ultimate.org/node/2201

--
http://wagerlabs.com/





___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


Re: [Haskell-cafe] QuickCheck testing of AST transformers

2007-04-23 Thread Thomas Schilling
Additionally, as a safety net, you might want to type-check the code  
that's being produced by your Arbitrary instances and state some  
invariants on your code.  Also, you'll likely want to limit your  
number of evaluation steps if your language allows non-terminating  
programs.


In any case, QuickCheck may not get you far enough to gain enough  
confidence, so proving properties by hand (after you made sure that  
QuickCheck doesn't find any counter-examples, of course) can give you  
interesting insights, since, this way, you have to take a look at all  
the possible cases yourself.


/Thomas


On 24 apr 2007, at 00.05, Lennart Augustsson wrote:

Without looking into your language and transformation in more  
detail it's hard to come up with concrete suggestions.  But here  
are some anyway:


Write an interpreter for each of your languages (original AST,  
transformed AST) etc, and then use a quickcheck property stating  
that well formed programs have the same denotation before and after  
transformation, i.e., the two interpreters give the same value  
(you might need some relaxed notion of same).


You transformations are trying to get rid of some language  
construct, I presume.  So you can have some properties stating that  
they will be gone in the transformed program..


-- Lennart


On Apr 23, 2007, at 22:46 , Joel Reymont wrote:

My previous post did not receive any replies so I thought I might  
try generalizing the problem a bit...


Suppose I'm parsing a language into a syntax tree and then  
transforming that tree into another AST representing a core  
language. The core language is a more general AST that should  
help with compiling to other languages.


My problem is how to best structure my AST transformations to be  
able to test them with QuickCheck. I suspect that I'm not going  
about it in the most optimal way so I thought I should ask for  
suggestions.


The transformation into the core AST applies operations to  
simplify, or desugar, the AST of the original language. Here's  
sample code in the source language which, incidentally, was  
recently highlighted at Lambda the Ultimate [1].


Array: MyArray[10](10 + 2);
Value1 = MyArray[5][10];

This declares an array of 10 elements and initializes each element  
to 12. Value1 (a built-in variable) is then initialized to the  
value of element #5 as of 10 bars ago. A bar is, basically, a  
stock quote. The code is invoked on every bar and so 5 bars ago  
can be treated as 5 invocations ago.


The syntax tree of the above code is a 1-1 mapping. We declare an  
array of integers of 10 elements. Initialize it to the sum of two  
integers and then assign to Value1.


[ ArrayDecs [ VarDecl (VarIdent MyArray) TyInt [Int 10]
  (Op Plus (Int 10) (Int 2)) ]
, Assign (VarIdent Value1) [] (Var (VarIdent MyArray) [Int 5]
 (BarsBack (Int 10))) ]

The desugared version does away with the array declaration  
statement and declares MyArray to be a variable of array type.  
Arrays in the core language do not remember values from one  
invocation to another but there's a data series type, so we  
declare a series variable to hold the value of element #5.


We must manually store the value of the array element in the data  
series and can then refer to the value of the series 10 data  
points ago.


vars = [ (MyArray, VarDecl (TyArray TyInt) [Int 10]
   (Just (Plus (Int 10) (Int 2
   , (series0, VarDecl (TySeries TyInt) [] Nothing)
   ]

code = [ AddToSeries (VarIdent series0) (Var (VarIdent  
MyArray) [Int 5])

   , Assign (Var (VarIdent Value1) [])
(Series (VarIdent series0) (Int 10))
   ]

The next step would be to take the above core syntax tree and  
transform it yet again into a C# (or other target language) AST.  
It's assumed that all target languages have a data series type.


The OCaml version of my code translated directly into the C# AST  
but I figured an intermediate syntax tree will help me translate  
into other languages such as Haskell, Erlang or OCaml.


The part I can't figure out is how to come up with a set of  
invariants for my transformations.


Should I, for example, state that every access to an array value  
in a previous invocation should introduce an extra variable to  
hold the series plus the appropriate assignment code?


Should I write the translator as a series of small transformers in  
the ST monad that can be threaded and tested separately?


Thanks in advance, Joel

[1] http://lambda-the-ultimate.org/node/2201

--
http://wagerlabs.com/





___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe



Re: [Haskell-cafe] QuickCheck testing of AST transformers

2007-04-23 Thread Neil Mitchell

Hi

My experience is that generating correct AST's is hard to get right.
I've found regression testing to be more useful when doing program
transformation.

I would follow Lennarts solution of writing a function that
semantically evaluates each expression. I would then add a bit into
your program which automatically does this check after each
transformation. This way if your code goes wrong you'll find out
sooner.

Thanks

Neil


On 4/23/07, Joel Reymont [EMAIL PROTECTED] wrote:

My previous post did not receive any replies so I thought I might try
generalizing the problem a bit...

Suppose I'm parsing a language into a syntax tree and then
transforming that tree into another AST representing a core
language. The core language is a more general AST that should help
with compiling to other languages.

My problem is how to best structure my AST transformations to be able
to test them with QuickCheck. I suspect that I'm not going about it
in the most optimal way so I thought I should ask for suggestions.

The transformation into the core AST applies operations to simplify,
or desugar, the AST of the original language. Here's sample code in
the source language which, incidentally, was recently highlighted at
Lambda the Ultimate [1].

Array: MyArray[10](10 + 2);
Value1 = MyArray[5][10];

This declares an array of 10 elements and initializes each element to
12. Value1 (a built-in variable) is then initialized to the value of
element #5 as of 10 bars ago. A bar is, basically, a stock quote. The
code is invoked on every bar and so 5 bars ago can be treated as 5
invocations ago.

The syntax tree of the above code is a 1-1 mapping. We declare an
array of integers of 10 elements. Initialize it to the sum of two
integers and then assign to Value1.

[ ArrayDecs [ VarDecl (VarIdent MyArray) TyInt [Int 10]
   (Op Plus (Int 10) (Int 2)) ]
, Assign (VarIdent Value1) [] (Var (VarIdent MyArray) [Int 5]
  (BarsBack (Int 10))) ]

The desugared version does away with the array declaration
statement and declares MyArray to be a variable of array type. Arrays
in the core language do not remember values from one invocation to
another but there's a data series type, so we declare a series
variable to hold the value of element #5.

We must manually store the value of the array element in the data
series and can then refer to the value of the series 10 data points ago.

vars = [ (MyArray, VarDecl (TyArray TyInt) [Int 10]
(Just (Plus (Int 10) (Int 2
, (series0, VarDecl (TySeries TyInt) [] Nothing)
]

code = [ AddToSeries (VarIdent series0) (Var (VarIdent MyArray)
[Int 5])
, Assign (Var (VarIdent Value1) [])
 (Series (VarIdent series0) (Int 10))
]

The next step would be to take the above core syntax tree and
transform it yet again into a C# (or other target language) AST. It's
assumed that all target languages have a data series type.

The OCaml version of my code translated directly into the C# AST but
I figured an intermediate syntax tree will help me translate into
other languages such as Haskell, Erlang or OCaml.

The part I can't figure out is how to come up with a set of
invariants for my transformations.

Should I, for example, state that every access to an array value in a
previous invocation should introduce an extra variable to hold the
series plus the appropriate assignment code?

Should I write the translator as a series of small transformers in
the ST monad that can be threaded and tested separately?

Thanks in advance, Joel

[1] http://lambda-the-ultimate.org/node/2201

--
http://wagerlabs.com/





___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


[Haskell-cafe] QuickCheck subsumes unit testing

2007-04-20 Thread Donald Bruce Stewart
Just to walk the walk, and not just talk the talk, here's a quick unit
testing 'diff' driver I hacked up for QuickCheck.

When run, it 'diffs' (well, just prints ;-) the incorrect values from
the unit test:

$ runhaskell T.hs
sort unit test   : Falsifiable after 0 tests:
- [1,2,3]
+ [1,3,2]

From a normal QC specification like:

   prop0 = (sort [3,2,1], [1,3,2])

   main = mapM_ (\(s,a) - printf %-25s:  s  a n) tests
 where
   n = 100
   tests = [(sort unit test, mytest prop0)]

The full driver code is attached. It is just proof of concept, but you
can see how to extend it to be smarter/prettier.

Note that we actually probably want to use SmallCheck here, to prevent
bogus repetition of the test. (I.e. 500 tests all passed, for a unit test).
Note also, the driver would need further extending, since we've changed
the structure of the Testable values.

Cheers,
  Don
import Data.List
import Text.Printf
import System.IO
import System.Random
import Test.QuickCheck

prop0 = (sort [3,2,1], [1,3,2])

main = mapM_ (\(s,a) - printf %-25s:  s  a n) tests
 where
   n = 100
   tests = [(sort unit test, mytest prop0)]





-- And a custom driver that `diff's the output

mytest :: (Show a, Eq a) = (a,a) - Int - IO ()
mytest (a,b) n = mycheck defaultConfig
{ configMaxTest=n
, configEvery= \n args - [] } a b

mycheck :: (Show a , Eq a) = Config - a - a - IO ()
mycheck config a b =
  do rnd - newStdGen
 mytests config (evaluate (a == b)) a b rnd 0 0 []

mytests :: (Show a , Eq a)
= Config - Gen Result - a - a - StdGen - Int - Int - [[String]] 
- IO ()
mytests config gen a b rnd0 ntest nfail stamps
  | ntest == configMaxTest config = do done OK, ntest stamps
  | nfail == configMaxFail config = do done Arguments exhausted after ntest 
stamps
  | otherwise   =
  do putStr (configEvery config ntest (arguments result))  hFlush stdout
 case ok result of
   Nothing-
 mytests config gen a b rnd1 ntest (nfail+1) stamps
   Just True  -
 mytests config gen a b rnd1 (ntest+1) nfail (stamp result:stamps)
   Just False -
 putStr ( Falsifiable after 
   ++ show ntest
   ++  tests:\n
   ++ -  ++ show a
   ++ \n
   ++ +  ++ show b
   ++ \n
)  hFlush stdout
 where
  result  = generate (configSize config ntest) rnd2 gen
  (rnd1,rnd2) = split rnd0

done :: String - Int - [[String]] - IO ()
done mesg ntest stamps =
  do putStr ( mesg ++   ++ show ntest ++  tests ++ table )
 where
  table = display
. map entry
. reverse
. sort
. map pairLength
. group
. sort
. filter (not . null)
$ stamps

  display []  = .\n
  display [x] =  ( ++ x ++ ).\n
  display xs  = .\n ++ unlines (map (++ .) xs)

  pairLength xss@(xs:_) = (length xss, xs)
  entry (n, xs) = percentage n ntest
   ++  
   ++ concat (intersperse ,  xs)

  percentage n m= show ((100 * n) `div` m) ++ %

___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


[Haskell-cafe] QuickCheck: Arbitrary for a complex type

2007-04-04 Thread Joel Reymont
Suppose I have a type describing a statement and that I'm trying to  
make it an instance of arbitrary. The type looks like this:


data Statement
= InputDecs [InputDecl]
| VarDecs [VarDecl]
| ArrayDecs [ArrayDecl]
| Compound [Statement]
| Assign (VarIdent, Expr)
| ArrayAssign (VarIdent, [Expr], Expr)

Assuming that other types involved were instances of arbitrary, how  
do I write arbitrary for Statement?


Poking around various bits of source code I think that for a type  
like the following


data Style
= StyleValue Expr
| Solid
| Dashed
| Dotted
| Dashed2
| Dashed3
deriving Show

I can write

instance Arbitrary Style where
arbitrary = oneOf [ StyleValue . arbitrary,
elements [ Solid
 , Dashed
 , Dotted
 , Dashed2
 , Dashed3
 ]
  ]

I'm not sure if this is correct, though, so any help is appreciated!

Thanks in advance, Joel

--
http://wagerlabs.com/





___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


[Haskell-cafe] QuickCheck Fun with Phantom Types

2006-05-13 Thread Dominic Steinitz
I was doing Exercise 5 of Ralf's Fun with Phantom Types and naturally thought 
I'd check my solution with QuickCheck. The best I could was this. Is there 
something better? Can you somehow generate random types as well as random 
values in those types?

Thanks, Dominic.

PS the full source for my solution with tests is attached.

prop_Idem t x = x == uncompress t (compress t x)

instance Arbitrary Char where
   arbitrary = oneof (map return ['A'..'z'])

class Reflect a where
   reflect :: Type a

instance Reflect Int where
   reflect = RInt

instance Reflect Char where
   reflect = RChar

instance (Reflect a, Reflect b) = Reflect (a,b) where
   reflect = RPair reflect reflect

instance Reflect a = Reflect [a] where
   reflect = RList reflect

type Test1 = Int - Bool
type Test2 = (Int,Int) - Bool
type Test3 = String - Bool
type Test4 = (String,String) - Bool

main =
   do quickCheck ((prop_Idem reflect) :: Test1)
  quickCheck ((prop_Idem reflect) :: Test2)
  quickCheck ((prop_Idem reflect) :: Test3)
  quickCheck ((prop_Idem reflect) :: Test4)
import Data.List
import Data.Char
import Test.QuickCheck
import Control.Monad.State

data Type :: * - * where
   RInt :: Type Int
   RChar :: Type Char
   RList :: Type a - Type [a]
   RPair :: Type a - Type b - Type (a,b)

rString :: Type String
rString = RList RChar

g = unfoldr f

f n = Just (n `mod` 2, n `div` 2)

compressInt x = take 32 (g x)

compressChar x = take 7 (g ((ord x)))

compress :: Type a - a - [Int]
compress RInt x = compressInt x
compress RChar x = compressChar x
compress (RList t) [] = [0]
compress (RList t) (x:xs) = 1:(compress t x ++ compress (RList t) xs)
compress (RPair s t) (x,y) = compress s x ++ compress t y

powersOf2 = 1:(map (2*) powersOf2)

uncompressInt xs = sum (zipWith (*) xs powersOf2)

uncompressChar x = chr (uncompressInt x)

uncompress :: Type a - [Int] - a
uncompress t x = let (r,_) = runState (bar t) x in r

bar :: Type a - State [Int] a
bar RInt =
   do xs - get
  let (ys,zs) = splitAt 32 xs
  put zs
  return (uncompressInt ys)
bar RChar =
   do xs - get
  let (ys,zs) = splitAt 7 xs
  put zs
  return (uncompressChar ys)
bar (RList t) = 
   do s - get
  let (f,rs) = splitAt 1 s
  put rs
  if f == [0] 
 then return []
 else 
do x - bar t
   xs - bar (RList t)
   return (x:xs)
bar (RPair s t) = 
   do x - bar s
  y - bar t
  return (x,y)

prop_Idem t x = x == uncompress t (compress t x)

instance Arbitrary Char where
   arbitrary = oneof (map return ['A'..'z'])

class Reflect a where
   reflect :: Type a

instance Reflect Int where
   reflect = RInt

instance Reflect Char where
   reflect = RChar

instance (Reflect a, Reflect b) = Reflect (a,b) where
   reflect = RPair reflect reflect

instance Reflect a = Reflect [a] where
   reflect = RList reflect

type Test1 = Int - Bool
type Test2 = (Int,Int) - Bool
type Test3 = String - Bool
type Test4 = (String,String) - Bool

main =
   do quickCheck ((prop_Idem reflect) :: Test1)
  quickCheck ((prop_Idem reflect) :: Test2)
  quickCheck ((prop_Idem reflect) :: Test3)
  quickCheck ((prop_Idem reflect) :: Test4)

___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


[Haskell-cafe] QuickCheck arbitrary/co-arbitrary

2005-12-15 Thread Joel Reymont

Folks,

I'm collecting examples of QuickCheck arbitrary/co-arbitrary  
definitions, the more complex the better. Please point me to any or  
send them if you are willing to!


Also, is there a cood explanation of co-arbitrary someplace?

Thanks, Joel

--
http://wagerlabs.com/





___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


Re: [Haskell-cafe] QuickCheck arbitrary/co-arbitrary

2005-12-15 Thread Donald Bruce Stewart
joelr1:
 Folks,
 
 I'm collecting examples of QuickCheck arbitrary/co-arbitrary  
 definitions, the more complex the better. Please point me to any or  
 send them if you are willing to!
 
 Also, is there a cood explanation of co-arbitrary someplace?

Check the QuickCheck paper for the definition and motiviation of
coarbitrary.
___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


[Haskell-cafe] Quickcheck examples and Data.Word32

2005-10-27 Thread Joel Reymont

Folks,

Does anyone have QuickCheck examples they could send me?

Also, how can I check Word32, Word64, etc. properties? It looks like  
the built-in random generator only works with Int values and for the  
life of me I cannot figure out how to extend it.


Last but not least, I'm trying to figure out how I can use QuickCheck  
with tests against a network server. I would want to make sure that a  
certain packet sequence always produces a particular packet sequence  
in response. Is this a good area to apply QC?


Thanks, Joel

--
http://wagerlabs.com/





___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


Re: [Haskell-cafe] Quickcheck examples and Data.Word32

2005-10-27 Thread Sebastian Sylvan
On 10/27/05, Joel Reymont [EMAIL PROTECTED] wrote:
 Folks,

 Does anyone have QuickCheck examples they could send me?

 Also, how can I check Word32, Word64, etc. properties? It looks like
 the built-in random generator only works with Int values and for the
 life of me I cannot figure out how to extend it.


Something like (untested!):

instance Arbitrary Word32 where
arbitrary = arbitrary :: Gen Integer = return . fromIntegral


--
Sebastian Sylvan
+46(0)736-818655
UIN: 44640862
___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


Re: [Haskell-cafe] Quickcheck examples and Data.Word32

2005-10-27 Thread Joel Reymont
Would it cover the range between minBound :: Word32 and maxBound ::  
Word32? I cannot figure out how to do this since maxBound :: Int32 is  
less that that of Word32.


Also, I get the following error with ghci -fglasgow-exts

foo.hs:7:52: parse error on input `.'
--
module Foo where

import Data.Word
import Test.QuickCheck

instance Arbitrary Word32 where
arbitrary = arbitrary :: Gen Integer = return . fromIntegral

prop_Word32 :: Word32 - Bool
prop_Word32 a = a == a

Thanks, Joel

On Oct 27, 2005, at 3:44 PM, Sebastian Sylvan wrote:


Something like (untested!):

instance Arbitrary Word32 where
arbitrary = arbitrary :: Gen Integer = return . fromIntegral


--
http://wagerlabs.com/





___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


Re: [Haskell-cafe] Quickcheck examples and Data.Word32

2005-10-27 Thread Sebastian Sylvan
On 10/27/05, Joel Reymont [EMAIL PROTECTED] wrote:
 Would it cover the range between minBound :: Word32 and maxBound ::
 Word32? I cannot figure out how to do this since maxBound :: Int32 is
 less that that of Word32.

 Also, I get the following error with ghci -fglasgow-exts

 foo.hs:7:52: parse error on input `.'

Okay, try this then:

import Data.Word
import Test.QuickCheck

instance Arbitrary Word32 where
  arbitrary = do let mx,mn :: Integer
 mx = fromIntegral (maxBound :: Word32)
 mn = fromIntegral (minBound :: Word32)
 c - choose (mx, mn)
 return (fromIntegral c)

That really should work. However the following will work too

instance Arbitrary Word32 where
  arbitrary = do c - arbitrary :: Gen Integer
  return (fromIntegral c)

Though I'm not sure of the range and distribution of the generated
Word32's (since it would depend on how fromIntegral behaves
transforming an Integer to a Word32 when the Integer is larger than
maxBound::Word32).


/S

 --
 module Foo where

 import Data.Word
 import Test.QuickCheck

 instance Arbitrary Word32 where
  arbitrary = arbitrary :: Gen Integer = return . fromIntegral

 prop_Word32 :: Word32 - Bool
 prop_Word32 a = a == a

  Thanks, Joel

 On Oct 27, 2005, at 3:44 PM, Sebastian Sylvan wrote:

  Something like (untested!):
 
  instance Arbitrary Word32 where
  arbitrary = arbitrary :: Gen Integer = return . fromIntegral

 --
 http://wagerlabs.com/








--
Sebastian Sylvan
+46(0)736-818655
UIN: 44640862
___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


Re: [Haskell-cafe] Quickcheck examples and Data.Word32

2005-10-27 Thread Sebastian Sylvan
On 10/27/05, Sebastian Sylvan [EMAIL PROTECTED] wrote:
 On 10/27/05, Joel Reymont [EMAIL PROTECTED] wrote:
  Would it cover the range between minBound :: Word32 and maxBound ::
  Word32? I cannot figure out how to do this since maxBound :: Int32 is
  less that that of Word32.
 
  Also, I get the following error with ghci -fglasgow-exts
 
  foo.hs:7:52: parse error on input `.'

 Okay, try this then:

 import Data.Word
 import Test.QuickCheck

 instance Arbitrary Word32 where
   arbitrary = do let mx,mn :: Integer
  mx = fromIntegral (maxBound :: Word32)
  mn = fromIntegral (minBound :: Word32)
  c - choose (mx, mn)
  return (fromIntegral c)


Sorry, indentation got screwed up there...

instance Arbitrary Word32 where
 arbitrary = do let mx,mn :: Integer
 mx = fromIntegral (maxBound :: Word32)
 mn = fromIntegral (minBound :: Word32)
 c - choose (mx, mn)
 return (fromIntegral c)
___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


Re: [Haskell-cafe] Quickcheck examples and Data.Word32

2005-10-27 Thread Joel Reymont
I came up with this but can it be done better? I'm wishing for  
default class methods :-).


instance Arbitrary Word16 where
arbitrary = arbitraryBound
coarbitrary a = error Not implemented

instance Arbitrary Word32 where
arbitrary = arbitraryBound
coarbitrary a = error Not implemented

instance Arbitrary Word64 where
arbitrary = arbitraryBound
coarbitrary a = error Not implemented

arbitraryBound :: forall a.(Integral a, Bounded a) = Gen a
arbitraryBound = do let mx,mn :: Integer
mx = fromIntegral (maxBound :: a)
mn = fromIntegral (minBound :: a)
c - choose (mx, mn)
return (fromIntegral c)

On Oct 27, 2005, at 6:13 PM, Joel Reymont wrote:


Is there a way to squeeze this boilerplate code?

class Arbitrary
instance Arbitrary Word16 where
arbitrary = do let mx,mn :: Integer
   mx = fromIntegral (maxBound :: Word16)
   mn = fromIntegral (minBound :: Word16)
   c - choose (mx, mn)
   return (fromIntegral c)
coarbitrary a = error Not implemented

instance Arbitrary Word32 where
arbitrary = do let mx,mn :: Integer
   mx = fromIntegral (maxBound :: Word32)
   mn = fromIntegral (minBound :: Word32)
   c - choose (mx, mn)
   return (fromIntegral c)
coarbitrary a = error Not implemented

instance Arbitrary Word64 where
arbitrary = do let mx,mn :: Integer
   mx = fromIntegral (maxBound :: Word64)
   mn = fromIntegral (minBound :: Word64)
   c - choose (mx, mn)
   return (fromIntegral c)
coarbitrary a = error Not implemented



--
http://wagerlabs.com/





___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


Re: [Haskell-cafe] Quickcheck examples and Data.Word32

2005-10-27 Thread Bryn Keller

How about this?

class ArbitraryDefault a where {}

instance (Integral a, Bounded a, ArbitraryDefault a) = Arbitrary a where
   arbitrary = arbitraryBound
   coarbitrary a = error Not implemented

instance ArbitraryDefault Word16   
instance ArbitraryDefault Word32
instance ArbitraryDefault Word64   



arbitraryBound :: forall a.(Integral a, Bounded a) = Gen a
arbitraryBound = do let mx,mn :: Integer
   mx = fromIntegral (maxBound :: a)
   mn = fromIntegral (minBound :: a)
   c - choose (mx, mn)
   return (fromIntegral c)


Joel Reymont wrote:
I came up with this but can it be done better? I'm wishing for 
default class methods :-).


instance Arbitrary Word16 where
arbitrary = arbitraryBound
coarbitrary a = error Not implemented

instance Arbitrary Word32 where
arbitrary = arbitraryBound
coarbitrary a = error Not implemented

instance Arbitrary Word64 where
arbitrary = arbitraryBound
coarbitrary a = error Not implemented

arbitraryBound :: forall a.(Integral a, Bounded a) = Gen a
arbitraryBound = do let mx,mn :: Integer
mx = fromIntegral (maxBound :: a)
mn = fromIntegral (minBound :: a)
c - choose (mx, mn)
return (fromIntegral c)

On Oct 27, 2005, at 6:13 PM, Joel Reymont wrote:


Is there a way to squeeze this boilerplate code?

class Arbitrary
instance Arbitrary Word16 where
arbitrary = do let mx,mn :: Integer
   mx = fromIntegral (maxBound :: Word16)
   mn = fromIntegral (minBound :: Word16)
   c - choose (mx, mn)
   return (fromIntegral c)
coarbitrary a = error Not implemented

instance Arbitrary Word32 where
arbitrary = do let mx,mn :: Integer
   mx = fromIntegral (maxBound :: Word32)
   mn = fromIntegral (minBound :: Word32)
   c - choose (mx, mn)
   return (fromIntegral c)
coarbitrary a = error Not implemented

instance Arbitrary Word64 where
arbitrary = do let mx,mn :: Integer
   mx = fromIntegral (maxBound :: Word64)
   mn = fromIntegral (minBound :: Word64)
   c - choose (mx, mn)
   return (fromIntegral c)
coarbitrary a = error Not implemented



--
http://wagerlabs.com/





___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe




___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


Re: [Haskell-cafe] Quickcheck examples and Data.Word32

2005-10-27 Thread Joel Reymont

This requires

{-# OPTIONS_GHC -fallow-undecidable-instances #-}

but since I'm using -fglasgow-exts in a lot of places I'm wondering  
if adding undecidable instances would be a bad habit. I guess not...  
not until I shoot myself in the foot :-).


Any explanation of undecidable instances, the good and the bad?

Joel

On Oct 27, 2005, at 6:49 PM, Bryn Keller wrote:


How about this?

class ArbitraryDefault a where {}

instance (Integral a, Bounded a, ArbitraryDefault a) = Arbitrary a  
where

   arbitrary = arbitraryBound
   coarbitrary a = error Not implemented

instance ArbitraryDefault Word16   instance ArbitraryDefault Word32
instance ArbitraryDefault Word64


--
http://wagerlabs.com/





___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


Re: [Haskell-cafe] Quickcheck examples and Data.Word32

2005-10-27 Thread Bryn Keller
I've not had any problems with them, though of course your mileage may 
vary. Have a look at section 7.4.4.3 in 
http://www.haskell.org/ghc/docs/latest/html/users_guide/type-extensions.html#multi-param-type-classes 
for an explanation. Basically, if you have a cyclic class dependency 
graph, the type checker won't terminate. Well, it will, but only because 
there's a limit on the recursion depth. The trivial example is


instance C a = C a where ...

In practice, I'm not sure that this is really a big issue, and it does 
come in handy. Perhaps someone who's actually been bitten by a problem 
with undecidable instances can comment?


Bryn


Joel Reymont wrote:

This requires

{-# OPTIONS_GHC -fallow-undecidable-instances #-}

but since I'm using -fglasgow-exts in a lot of places I'm wondering if 
adding undecidable instances would be a bad habit. I guess not... not 
until I shoot myself in the foot :-).


Any explanation of undecidable instances, the good and the bad?

Joel

On Oct 27, 2005, at 6:49 PM, Bryn Keller wrote:


How about this?

class ArbitraryDefault a where {}

instance (Integral a, Bounded a, ArbitraryDefault a) = Arbitrary a 
where

   arbitrary = arbitraryBound
   coarbitrary a = error Not implemented

instance ArbitraryDefault Word16   instance ArbitraryDefault Word32
instance ArbitraryDefault Word64


--
http://wagerlabs.com/









___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


Re: [Haskell-cafe] Quickcheck examples and Data.Word32

2005-10-27 Thread Nils Anders Danielsson
On Thu, 27 Oct 2005, Sebastian Sylvan [EMAIL PROTECTED] wrote:

 instance Arbitrary Word32 where
   arbitrary = do c - arbitrary :: Gen Integer
  return (fromIntegral c)

This definition will usually only generate very small or very large
Word32 values. The reason is the wrapping mentioned elsewhere and the
arbitrary definition for Integer:

  arbitrary = sized $ \n - choose (-fromIntegral n,fromIntegral n)

You would need to manually ask for larger sizes (using
Test.QuickCheck.check with a suitable Config). If a uniform
distribution of Word32s is really needed then I would go with the
other definition.

-- 
/NAD

___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


Re: [Haskell-cafe] Quickcheck examples and Data.Word32

2005-10-27 Thread Joel Reymont

Just one more question...

data Prop = forall a b. (Eq a, Eq b, Show a, Packet b, Convertible a b)
= Attr a b := a
   deriving (Typeable)

data Attr a b = Attr String
(a - Dynamic, Dynamic - Maybe a)
(a - b, b - a)

makeAttr :: (Typeable a, Convertible a b) = String - Attr a b
makeAttr name = Attr name
(toDyn, fromDynamic)
(convert_AB, convert_BA)

I can do this for Attr

instance (Typeable a, Arbitrary a, Typeable b, Arbitrary b,  
Convertible a b) = Arbitrary (Attr a b) where

arbitrary = makeAttr `fmap` arbitrary
coarbitrary a = error Not implemented

How do I define an arbitrary prop, though? Following does not work:

arbitraryProp :: forall a b.(Arbitrary a, Arbitrary b) = Attr a b -  
Gen Prop

arbitraryProp  = arbitrary := arbitrary

Thanks, Joel

--
http://wagerlabs.com/





___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


Re: [Haskell-cafe] Quickcheck examples and Data.Word32

2005-10-27 Thread Joel Reymont

This compiles:

instance (Typeable a, Arbitrary a, Typeable b, Arbitrary b,  
Convertible a b) = Arbitrary (Attr a b) where

arbitrary = makeAttr `fmap` arbitrary
coarbitrary a = error Not implemented

arbitraryProp :: forall a b.(Eq a, Packet b, Show a, Convertible a b,  
Arbitrary a, Arbitrary b) = Gen (Attr a b) - Gen Prop

arbitraryProp attr = liftM2 (:=) attr arbitrary

Joel

On Oct 28, 2005, at 12:13 AM, Joel Reymont wrote:


Just one more question...

data Prop = forall a b. (Eq a, Eq b, Show a, Packet b, Convertible  
a b)

= Attr a b := a
   deriving (Typeable)

data Attr a b = Attr String
(a - Dynamic, Dynamic - Maybe a)
(a - b, b - a)

makeAttr :: (Typeable a, Convertible a b) = String - Attr a b
makeAttr name = Attr name
(toDyn, fromDynamic)
(convert_AB, convert_BA)

I can do this for Attr

instance (Typeable a, Arbitrary a, Typeable b, Arbitrary b,  
Convertible a b) = Arbitrary (Attr a b) where

arbitrary = makeAttr `fmap` arbitrary
coarbitrary a = error Not implemented

How do I define an arbitrary prop, though? Following does not work:

arbitraryProp :: forall a b.(Arbitrary a, Arbitrary b) = Attr a b - 
 Gen Prop

arbitraryProp  = arbitrary := arbitrary


--
http://wagerlabs.com/





___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


Re: [Haskell-cafe] Quickcheck examples and Data.Word32

2005-10-27 Thread John Meacham
On Thu, Oct 27, 2005 at 07:06:12PM +0100, Joel Reymont wrote:
 This requires
 
 {-# OPTIONS_GHC -fallow-undecidable-instances #-}
 
 but since I'm using -fglasgow-exts in a lot of places I'm wondering  
 if adding undecidable instances would be a bad habit. I guess not...  
 not until I shoot myself in the foot :-).

I would avoid them if at all possible. especially if you are still
learning haskell, I have found they often hide real errors in ones
design of a class hierarchy by making something compile that shouldn't.

I had a particularly nasty bug in my regex library when I only had one
operator (=~) that was not being caught because of undecidable instances
being allowed, when I turned them off, I thought about the problem again
and realized that separate monadic (=~~) and non monadic (=~) versions
not only solved the issue but was actually a much nicer design with much
clearer semantics.

John

-- 
John Meacham - ⑆repetae.net⑆john⑈ 
___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


Re: [Haskell-cafe] QuickCheck - Extracting data values from tests

2004-09-04 Thread Jorge Adriano Aires

 The generate function is exported:

 generate :: Int - StdGen - Gen a - a

Thanks, that's the one I was missing.

J.A.
___
Haskell-Cafe mailing list
[EMAIL PROTECTED]
http://www.haskell.org/mailman/listinfo/haskell-cafe


  1   2   >