QuickCheck Fun with Phantom Types

classic Classic list List threaded Threaded
2 messages Options
Reply | Threaded
Open this post in threaded view
|

QuickCheck Fun with Phantom Types

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)

_______________________________________________
Haskell-Cafe mailing list
[hidden email]
http://www.haskell.org/mailman/listinfo/haskell-cafe

qcgadt.hs (2K) Download Attachment
Reply | Threaded
Open this post in threaded view
|

Re: QuickCheck Fun with Phantom Types

Dominic Steinitz
Since Don was kind enough to include my question in HWN
(http://www.cse.unsw.edu.au/~dons/code/hwn/archives/20060522.html) and I
have now come up with a solution, I have created a wiki page
(http://haskell.org/haskellwiki/QuickCheck_/_GADT) with it in.

Dominic.

_______________________________________________
Haskell-Cafe mailing list
[hidden email]
http://www.haskell.org/mailman/listinfo/haskell-cafe