[project @ 2002-04-26 13:34:05 by simonmar]
[ghc-base.git] / Debug / QuickCheck / Poly.hs
1 -----------------------------------------------------------------------------
2 -- |
3 -- Module      :  Debug.QuickCheck.Poly
4 -- Copyright   :  (c) Andy Gill 2001
5 -- License     :  BSD-style (see the file libraries/core/LICENSE)
6 -- 
7 -- Maintainer  :  libraries@haskell.org
8 -- Stability   :  experimental
9 -- Portability :  non-portable (uses Control.Exception, Control.Concurrent)
10 --
11 -- This is an attempt to emulate polymorphic types for the 
12 -- purposes of testing by using abstract monomorphic types.
13 -- 
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.
17 --
18 -----------------------------------------------------------------------------
19
20 module Debug.QuickCheck.Poly
21   ( ALPHA
22   , BETA
23   , GAMMA
24   , OrdALPHA
25   , OrdBETA
26   , OrdGAMMA
27   ) where
28
29 import Debug.QuickCheck
30 import Debug.QuickCheck.Utils
31
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.
35  - 
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)
39  -}
40
41 newtype Poly a = Poly Int
42
43 instance Show (Poly a) where
44         show (Poly a) = "_" ++ show a
45
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)
49
50 instance Eq a => Eq (Poly a) where
51         (Poly a) == (Poly b) = a == b
52
53 instance Ord a => Ord (Poly a) where
54         (Poly a) `compare` (Poly b) = a `compare` b
55
56 {-
57  - These are what we export, our pseudo-polymorphic instances.
58  -}
59
60 type ALPHA = Poly ALPHA_
61 data ALPHA_ = ALPHA_ deriving (Eq)
62
63 type BETA = Poly BETA_
64 data BETA_ = BETA_ deriving (Eq)
65
66 type GAMMA = Poly GAMMA_
67 data GAMMA_ = GAMMA_ deriving (Eq)
68
69 type OrdALPHA = Poly OrdALPHA_
70 data OrdALPHA_ = OrdALPHA_ deriving (Eq,Ord)
71
72 type OrdBETA = Poly OrdBETA_
73 data OrdBETA_ = OrdBETA_ deriving (Eq,Ord)
74
75 type OrdGAMMA = Poly OrdGAMMA_
76 data OrdGAMMA_ = OrdGAMMA_ deriving (Eq,Ord)
77
78 {-
79  - This is a condition on OrdALPHA, OrdBETA, etc, itself.
80  - It states that all OrdALPHA objects obey total ordering.
81  -}
82
83 prop_OrdPOLY x y = isTotalOrder x y
84     where types = (x :: OrdALPHA, y :: OrdALPHA)