1 /* -----------------------------------------------------------------------------
2 * $Id: CTypes.h,v 1.2 2001/12/21 15:07:26 simonmar Exp $
4 * Dirty CPP hackery for CTypes/CTypesISO
6 * (c) The FFI task force, 2000
7 * -------------------------------------------------------------------------- */
11 /* As long as there is no automatic derivation of classes for newtypes we resort
12 to extremely dirty cpp-hackery. :-P Some care has to be taken when the
13 macros below are modified, otherwise the layout rule will bite you. */
15 /* A hacked version for GHC follows the Haskell 98 version... */
16 #ifndef __GLASGOW_HASKELL__
18 #define NUMERIC_TYPE(T,C,S,B) \
19 newtype T = T B deriving (Eq, Ord) ; \
24 INSTANCE_TYPEABLE(T,C,S) ;
26 #define INTEGRAL_TYPE(T,C,S,B) \
27 NUMERIC_TYPE(T,C,S,B) ; \
28 INSTANCE_BOUNDED(T) ; \
30 INSTANCE_INTEGRAL(T) ; \
33 #define FLOATING_TYPE(T,C,S,B) \
34 NUMERIC_TYPE(T,C,S,B) ; \
36 INSTANCE_FRACTIONAL(T) ; \
37 INSTANCE_FLOATING(T) ; \
38 INSTANCE_REALFRAC(T) ; \
41 #define INSTANCE_READ(T) \
42 instance Read T where { \
43 readsPrec p s = fakeMap (\(x, t) -> (T x, t)) (readsPrec p s) }
45 #define INSTANCE_SHOW(T) \
46 instance Show T where { \
47 showsPrec p (T x) = showsPrec p x }
49 #define INSTANCE_NUM(T) \
50 instance Num T where { \
51 (T i) + (T j) = T (i + j) ; \
52 (T i) - (T j) = T (i - j) ; \
53 (T i) * (T j) = T (i * j) ; \
54 negate (T i) = T (negate i) ; \
55 abs (T i) = T (abs i) ; \
56 signum (T i) = T (signum i) ; \
57 fromInteger x = T (fromInteger x) }
59 #define INSTANCE_TYPEABLE(T,C,S) \
62 instance Typeable T where { \
63 typeOf _ = mkAppTy C [] }
65 #define INSTANCE_BOUNDED(T) \
66 instance Bounded T where { \
67 minBound = T minBound ; \
68 maxBound = T maxBound }
70 #define INSTANCE_ENUM(T) \
71 instance Enum T where { \
72 succ (T i) = T (succ i) ; \
73 pred (T i) = T (pred i) ; \
74 toEnum x = T (toEnum x) ; \
75 fromEnum (T i) = fromEnum i ; \
76 enumFrom (T i) = fakeMap T (enumFrom i) ; \
77 enumFromThen (T i) (T j) = fakeMap T (enumFromThen i j) ; \
78 enumFromTo (T i) (T j) = fakeMap T (enumFromTo i j) ; \
79 enumFromThenTo (T i) (T j) (T k) = fakeMap T (enumFromThenTo i j k) }
81 #define INSTANCE_REAL(T) \
82 instance Real T where { \
83 toRational (T i) = toRational i }
85 #define INSTANCE_INTEGRAL(T) \
86 instance Integral T where { \
87 (T i) `quot` (T j) = T (i `quot` j) ; \
88 (T i) `rem` (T j) = T (i `rem` j) ; \
89 (T i) `div` (T j) = T (i `div` j) ; \
90 (T i) `mod` (T j) = T (i `mod` j) ; \
91 (T i) `quotRem` (T j) = let (q,r) = i `quotRem` j in (T q, T r) ; \
92 (T i) `divMod` (T j) = let (d,m) = i `divMod` j in (T d, T m) ; \
93 toInteger (T i) = toInteger i }
95 #define INSTANCE_BITS(T) \
96 instance Bits T where { \
97 (T x) .&. (T y) = T (x .&. y) ; \
98 (T x) .|. (T y) = T (x .|. y) ; \
99 (T x) `xor` (T y) = T (x `xor` y) ; \
100 complement (T x) = T (complement x) ; \
101 shift (T x) n = T (shift x n) ; \
102 rotate (T x) n = T (rotate x n) ; \
103 bit n = T (bit n) ; \
104 setBit (T x) n = T (setBit x n) ; \
105 clearBit (T x) n = T (clearBit x n) ; \
106 complementBit (T x) n = T (complementBit x n) ; \
107 testBit (T x) n = testBit x n ; \
108 bitSize (T x) = bitSize x ; \
109 isSigned (T x) = isSigned x }
111 #define INSTANCE_FRACTIONAL(T) \
112 instance Fractional T where { \
113 (T x) / (T y) = T (x / y) ; \
114 recip (T x) = T (recip x) ; \
115 fromRational r = T (fromRational r) }
117 #define INSTANCE_FLOATING(T) \
118 instance Floating T where { \
120 exp (T x) = T (exp x) ; \
121 log (T x) = T (log x) ; \
122 sqrt (T x) = T (sqrt x) ; \
123 (T x) ** (T y) = T (x ** y) ; \
124 (T x) `logBase` (T y) = T (x `logBase` y) ; \
125 sin (T x) = T (sin x) ; \
126 cos (T x) = T (cos x) ; \
127 tan (T x) = T (tan x) ; \
128 asin (T x) = T (asin x) ; \
129 acos (T x) = T (acos x) ; \
130 atan (T x) = T (atan x) ; \
131 sinh (T x) = T (sinh x) ; \
132 cosh (T x) = T (cosh x) ; \
133 tanh (T x) = T (tanh x) ; \
134 asinh (T x) = T (asinh x) ; \
135 acosh (T x) = T (acosh x) ; \
136 atanh (T x) = T (atanh x) }
138 #define INSTANCE_REALFRAC(T) \
139 instance RealFrac T where { \
140 properFraction (T x) = let (m,y) = properFraction x in (m, T y) ; \
141 truncate (T x) = truncate x ; \
142 round (T x) = round x ; \
143 ceiling (T x) = ceiling x ; \
144 floor (T x) = floor x }
146 #define INSTANCE_REALFLOAT(T) \
147 instance RealFloat T where { \
148 floatRadix (T x) = floatRadix x ; \
149 floatDigits (T x) = floatDigits x ; \
150 floatRange (T x) = floatRange x ; \
151 decodeFloat (T x) = decodeFloat x ; \
152 encodeFloat m n = T (encodeFloat m n) ; \
153 exponent (T x) = exponent x ; \
154 significand (T x) = T (significand x) ; \
155 scaleFloat n (T x) = T (scaleFloat n x) ; \
156 isNaN (T x) = isNaN x ; \
157 isInfinite (T x) = isInfinite x ; \
158 isDenormalized (T x) = isDenormalized x ; \
159 isNegativeZero (T x) = isNegativeZero x ; \
160 isIEEE (T x) = isIEEE x ; \
161 (T x) `atan2` (T y) = T (x `atan2` y) }
163 #else /* __GLASGOW_HASKELL__ */
165 /* GHC can derive any class for a newtype, so we make use of that
169 #define NUMERIC_CLASSES Eq,Ord,Num,Enum
170 #define INTEGRAL_CLASSES Bounded,Real,Integral,Bits
171 #define FLOATING_CLASSES Real,Fractional,Floating,RealFrac,RealFloat
173 #define NUMERIC_TYPE(T,C,S,B) \
174 newtype T = T B deriving (NUMERIC_CLASSES); \
175 INSTANCE_READ(T,B); \
178 #define INTEGRAL_TYPE(T,C,S,B) \
179 newtype T = T B deriving (NUMERIC_CLASSES, INTEGRAL_CLASSES); \
180 INSTANCE_READ(T,B); \
183 #define FLOATING_TYPE(T,C,S,B) \
184 newtype T = T B deriving (NUMERIC_CLASSES, FLOATING_CLASSES); \
185 INSTANCE_READ(T,B); \
188 #define INSTANCE_READ(T,B) \
189 instance Read T where { \
190 readsPrec = unsafeCoerce# (readsPrec :: Int -> ReadS B); \
191 readList = unsafeCoerce# (readList :: ReadS [B]); }
193 #define INSTANCE_SHOW(T,B) \
194 instance Show T where { \
195 showsPrec = unsafeCoerce# (showsPrec :: Int -> B -> ShowS); \
196 show = unsafeCoerce# (show :: B -> String); \
197 showList = unsafeCoerce# (showList :: [B] -> ShowS); }
199 #endif /* __GLASGOW_HASKELL__ */