1 -----------------------------------------------------------------------------
3 -- Module : Debug.QuickCheck.Poly
4 -- Copyright : (c) Andy Gill 2001
5 -- License : BSD-style (see the file libraries/core/LICENSE)
7 -- Maintainer : libraries@haskell.org
8 -- Stability : experimental
9 -- Portability : non-portable (uses Control.Exception, Control.Concurrent)
11 -- This is an attempt to emulate polymorphic types for the
12 -- purposes of testing by using abstract monomorphic types.
14 -- It is likely that future versions of QuickCheck will
15 -- include some polymorphic emulation testing facility,
16 -- but this module can be used for now.
18 -----------------------------------------------------------------------------
20 module Debug.QuickCheck.Poly
29 import Debug.QuickCheck
30 import Debug.QuickCheck.Utils
32 {- This is the basic pseudo-polymorphic object.
33 - The idea is you can't cheat, and use the integer
34 - directly, but need to use the abstraction.
36 - We use phantom types (ref: Domain Specific Embedded Compilers,
37 - Daan Leijen & Erik Meijer, 2nd Conference of Domain Specific
38 - Languages, Austin, TX, 1999)
41 newtype Poly a = Poly Int
43 instance Show (Poly a) where
44 show (Poly a) = "_" ++ show a
46 instance Arbitrary (Poly a) where
47 arbitrary = sized $ \n -> (choose (1,n) >>= return . Poly)
48 coarbitrary (Poly n) = variant (if n >= 0 then 2*n else 2*(-n) + 1)
50 instance Eq a => Eq (Poly a) where
51 (Poly a) == (Poly b) = a == b
53 instance Ord a => Ord (Poly a) where
54 (Poly a) `compare` (Poly b) = a `compare` b
57 - These are what we export, our pseudo-polymorphic instances.
60 type ALPHA = Poly ALPHA_
61 data ALPHA_ = ALPHA_ deriving (Eq)
63 type BETA = Poly BETA_
64 data BETA_ = BETA_ deriving (Eq)
66 type GAMMA = Poly GAMMA_
67 data GAMMA_ = GAMMA_ deriving (Eq)
69 type OrdALPHA = Poly OrdALPHA_
70 data OrdALPHA_ = OrdALPHA_ deriving (Eq,Ord)
72 type OrdBETA = Poly OrdBETA_
73 data OrdBETA_ = OrdBETA_ deriving (Eq,Ord)
75 type OrdGAMMA = Poly OrdGAMMA_
76 data OrdGAMMA_ = OrdGAMMA_ deriving (Eq,Ord)
79 - This is a condition on OrdALPHA, OrdBETA, etc, itself.
80 - It states that all OrdALPHA objects obey total ordering.
83 prop_OrdPOLY x y = isTotalOrder x y
84 where types = (x :: OrdALPHA, y :: OrdALPHA)