[Haskell-cafe] Need advise with using Quickcheck

Tom Ellis tom-lists-haskell-cafe-2017 at jaguarpaw.co.uk
Sat Jun 12 10:57:28 UTC 2021


You say you have another function show' :: Foo -> String.  Then how
about defining

newtype AnotherFoo = AnotherFoo Foo

instance Show AnotherFoo where
    show (AnotherFoo foo) = show' food

instance Arbitrary AnotherFoo where
    arbitrary = AnotherFoo <$> arbitrary

etc.?

On Sat, Jun 12, 2021 at 12:49:47PM +0200, Han Joosten wrote:
> Thanks for this respons, Rubén. I might not have explained what I am
> looking for very well.
> I already have
> instance Arbitrary Foo
> That is not the problem. The problem is, that when a counterexample is
> found, it is dumped to the output with the `show` function. I would like to
> be able to use another function for that, say show'. In order to do so, I
> need to obtain the counterexample itself in some way, so I can use it to
> generate a useful message after the tests are done.
> 
> Op vr 11 jun. 2021 om 23:31 schreef Ruben Astudillo <ruben.astud at gmail.com>:
> 
> > Hello Han
> >
> > On 11-06-21 17:16, Han Joosten wrote:
> > > [..]
> > > *parseFoo :: Text -> Guarded Foo*
> > > *prettyFoo :: Foo -> Text*
> > >
> > > The idea is that for all foo :: Foo, the following holds:
> > >     *parseFoo . prettyFoo $ foo*  should be equal to  *Checked foo []*
> > >
> > > Now, if there is a counterexample, I would like to see prettyFoo foo as
> > > output, folowed by the show of the Errors part.
> >
> > It seems you know what property should hold for `parseFoo` and `prettyFoo`.
> > Per the QuickCheck manual [1] this could be written as
> >
> >     import Test.QuickCheck
> >
> >     propOnAllFooText :: Property
> >     propOnAllFooText = forAll (arbitrary :: Gen Foo) $ \exFoo ->
> >         parseFoo (prettyFoo exFoo) == Checked exFoo []
> >
> > What you are missing is a way to generate the test cases (the `arbitrary'
> > generator above). You need a way to declare this instance
> >
> >     instance Arbitrary Foo where
> >         -- arbitrary :: Gen Foo
> >         arbitrary = <something>
> >
> > You can see example on how to define it on the manual [1]. Good luck.
> >
> > [1]: http://www.cse.chalmers.se/~rjmh/QuickCheck/manual.html
> >
> > --
> > -- Rubén
> > -- pgp: 4EE9 28F7 932E F4AD


More information about the Haskell-Cafe mailing list