[project @ 2001-06-28 14:15:04 by simonmar]
[ghc-base.git] / include / CTypes.h
1 /* -----------------------------------------------------------------------------
2  * $Id: CTypes.h,v 1.1 2001/06/28 14:15:04 simonmar Exp $
3  *
4  * Dirty CPP hackery for CTypes/CTypesISO
5  *
6  * (c) The FFI task force, 2000
7  * -------------------------------------------------------------------------- */
8
9 #include "MachDeps.h"
10
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. */
14
15 /* A hacked version for GHC follows the Haskell 98 version... */
16 #ifndef __GLASGOW_HASKELL__
17
18 #define NUMERIC_TYPE(T,C,S,B) \
19 newtype T = T B deriving (Eq, Ord) ; \
20 INSTANCE_NUM(T) ; \
21 INSTANCE_READ(T) ; \
22 INSTANCE_SHOW(T) ; \
23 INSTANCE_ENUM(T) ; \
24 INSTANCE_TYPEABLE(T,C,S) ;
25
26 #define INTEGRAL_TYPE(T,C,S,B) \
27 NUMERIC_TYPE(T,C,S,B) ; \
28 INSTANCE_BOUNDED(T) ; \
29 INSTANCE_REAL(T) ; \
30 INSTANCE_INTEGRAL(T) ; \
31 INSTANCE_BITS(T)
32
33 #define FLOATING_TYPE(T,C,S,B) \
34 NUMERIC_TYPE(T,C,S,B) ; \
35 INSTANCE_REAL(T) ; \
36 INSTANCE_FRACTIONAL(T) ; \
37 INSTANCE_FLOATING(T) ; \
38 INSTANCE_REALFRAC(T) ; \
39 INSTANCE_REALFLOAT(T)
40
41 #define INSTANCE_READ(T) \
42 instance Read T where { \
43    readsPrec p s = fakeMap (\(x, t) -> (T x, t)) (readsPrec p s) }
44
45 #define INSTANCE_SHOW(T) \
46 instance Show T where { \
47    showsPrec p (T x) = showsPrec p x }
48
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) }
58
59 #define INSTANCE_TYPEABLE(T,C,S) \
60 C :: TyCon ; \
61 C = mkTyCon S ; \
62 instance Typeable T where { \
63   typeOf _ = mkAppTy C [] }
64
65 #define INSTANCE_BOUNDED(T) \
66 instance Bounded T where { \
67    minBound = T minBound ; \
68    maxBound = T maxBound }
69
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) }
80
81 #define INSTANCE_REAL(T) \
82 instance Real T where { \
83    toRational (T i) = toRational i }
84
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 }
94
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 }
110
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) }
116
117 #define INSTANCE_FLOATING(T) \
118 instance Floating T where { \
119    pi                    = pi ; \
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) }
137
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 }
145
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) }
162
163 #else /* __GLASGOW_HASKELL__ */
164
165 /* On GHC, we just cast the type of each method to the underlying
166  * type.  This means that GHC only needs to generate the dictionary
167  * for each instance, rather than a new function for each method (the
168  * simplifier currently isn't clever enough to reduce a method that
169  * simply deconstructs a newtype and calls the underlying method into
170  * an indirection to the underlying method, so that's what we're doing
171  * here). 
172  */
173
174 #define NUMERIC_TYPE(T,C,S,B) \
175 newtype T = T B ; \
176 INSTANCE_EQ(T,B) ; \
177 INSTANCE_ORD(T,B) ; \
178 INSTANCE_NUM(T,B) ; \
179 INSTANCE_READ(T,B) ; \
180 INSTANCE_SHOW(T,B) ; \
181 INSTANCE_ENUM(T,B) 
182
183 #define INTEGRAL_TYPE(T,C,S,B) \
184 NUMERIC_TYPE(T,C,S,B) ;  \
185 INSTANCE_BOUNDED(T,B) ; \
186 INSTANCE_REAL(T,B) ; \
187 INSTANCE_INTEGRAL(T,B) ; \
188 INSTANCE_BITS(T,B)
189
190 #define FLOATING_TYPE(T,C,S,B) \
191 NUMERIC_TYPE(T,C,S,B) ; \
192 INSTANCE_REAL(T,B) ; \
193 INSTANCE_FRACTIONAL(T,B) ; \
194 INSTANCE_FLOATING(T,B) ; \
195 INSTANCE_REALFRAC(T) ; \
196 INSTANCE_REALFLOAT(T,B)
197
198 #define INSTANCE_EQ(T,B) \
199 instance Eq T where { \
200    (==)         = unsafeCoerce# ((==) :: B -> B -> Bool); \
201    (/=)         = unsafeCoerce# ((/=) :: B -> B -> Bool); }
202
203 #define INSTANCE_ORD(T,B) \
204 instance Ord T where { \
205    compare              = unsafeCoerce# (compare :: B -> B -> Ordering); \
206    (<)                  = unsafeCoerce# ((<) :: B -> B -> Bool); \
207    (<=)                 = unsafeCoerce# ((<=) :: B -> B -> Bool); \
208    (>=)                 = unsafeCoerce# ((>=) :: B -> B -> Bool); \
209    (>)                  = unsafeCoerce# ((>) :: B -> B -> Bool); \
210    max                  = unsafeCoerce# (max :: B -> B -> B); \
211    min                  = unsafeCoerce# (min :: B -> B -> B); }
212
213 #define INSTANCE_READ(T,B) \
214 instance Read T where { \
215    readsPrec            = unsafeCoerce# (readsPrec :: Int -> ReadS B); \
216    readList             = unsafeCoerce# (readList  :: ReadS [B]); }
217
218 #define INSTANCE_SHOW(T,B) \
219 instance Show T where { \
220    showsPrec            = unsafeCoerce# (showsPrec :: Int -> B -> ShowS); \
221    show                 = unsafeCoerce# (show :: B -> String); \
222    showList             = unsafeCoerce# (showList :: [B] -> ShowS); }
223
224 #define INSTANCE_NUM(T,B) \
225 instance Num T where { \
226    (+)                  = unsafeCoerce# ((+) :: B -> B -> B); \
227    (-)                  = unsafeCoerce# ((-) :: B -> B -> B); \
228    (*)                  = unsafeCoerce# ((*) :: B -> B -> B); \
229    negate               = unsafeCoerce# (negate :: B -> B); \
230    abs                  = unsafeCoerce# (abs :: B -> B); \
231    signum               = unsafeCoerce# (signum :: B -> B); \
232    fromInteger          = unsafeCoerce# (fromInteger :: Integer -> B); }
233
234 #define INSTANCE_BOUNDED(T,B) \
235 instance Bounded T where { \
236    minBound = T minBound ; \
237    maxBound = T maxBound }
238
239 #define INSTANCE_ENUM(T,B) \
240 instance Enum T where { \
241     succ                = unsafeCoerce# (succ :: B -> B); \
242     pred                = unsafeCoerce# (pred :: B -> B); \
243     toEnum              = unsafeCoerce# (toEnum :: Int -> B); \
244     fromEnum            = unsafeCoerce# (fromEnum :: B -> Int); \
245     enumFrom            = unsafeCoerce# (enumFrom :: B -> [B]); \
246     enumFromThen        = unsafeCoerce# (enumFromThen :: B -> B -> [B]); \
247     enumFromTo          = unsafeCoerce# (enumFromTo :: B -> B -> [B]); \
248     enumFromThenTo      = unsafeCoerce# (enumFromThenTo :: B -> B -> B -> [B]);}
249
250 #define INSTANCE_REAL(T,B) \
251 instance Real T where { \
252    toRational = unsafeCoerce# (toRational :: B -> Rational) }
253
254 #define INSTANCE_INTEGRAL(T,B) \
255 instance Integral T where { \
256     quot                = unsafeCoerce# (quot:: B -> B -> B); \
257     rem                 = unsafeCoerce# (rem:: B -> B -> B); \
258     div                 = unsafeCoerce# (div:: B -> B -> B); \
259     mod                 = unsafeCoerce# (mod:: B -> B -> B); \
260     quotRem             = unsafeCoerce# (quotRem:: B -> B -> (B,B)); \
261     divMod              = unsafeCoerce# (divMod:: B -> B -> (B,B)); \
262     toInteger           = unsafeCoerce# (toInteger:: B -> Integer); }
263
264 #define INSTANCE_BITS(T,B) \
265 instance Bits T where { \
266   (.&.)                 = unsafeCoerce# ((.&.) :: B -> B -> B); \
267   (.|.)                 = unsafeCoerce# ((.|.) :: B -> B -> B); \
268   xor                   = unsafeCoerce# (xor:: B -> B -> B); \
269   complement            = unsafeCoerce# (complement:: B -> B); \
270   shift                 = unsafeCoerce# (shift:: B -> Int -> B); \
271   rotate                = unsafeCoerce# (rotate:: B -> Int -> B); \
272   bit                   = unsafeCoerce# (bit:: Int -> B); \
273   setBit                = unsafeCoerce# (setBit:: B -> Int -> B); \
274   clearBit              = unsafeCoerce# (clearBit:: B -> Int -> B); \
275   complementBit         = unsafeCoerce# (complementBit:: B -> Int -> B); \
276   testBit               = unsafeCoerce# (testBit:: B -> Int -> Bool); \
277   bitSize               = unsafeCoerce# (bitSize:: B -> Int); \
278   isSigned              = unsafeCoerce# (isSigned:: B -> Bool); }
279
280 #define INSTANCE_FRACTIONAL(T,B) \
281 instance Fractional T where { \
282     (/)                 = unsafeCoerce# ((/) :: B -> B -> B); \
283     recip               = unsafeCoerce# (recip :: B -> B); \
284     fromRational        = unsafeCoerce# (fromRational :: Rational -> B); }
285
286 #define INSTANCE_FLOATING(T,B) \
287 instance Floating T where { \
288     pi                  = unsafeCoerce# (pi :: B); \
289     exp                 = unsafeCoerce# (exp :: B -> B); \
290     log                 = unsafeCoerce# (log :: B -> B); \
291     sqrt                = unsafeCoerce# (sqrt :: B -> B); \
292     (**)                = unsafeCoerce# ((**) :: B -> B -> B); \
293     logBase             = unsafeCoerce# (logBase :: B -> B -> B); \
294     sin                 = unsafeCoerce# (sin :: B -> B); \
295     cos                 = unsafeCoerce# (cos :: B -> B); \
296     tan                 = unsafeCoerce# (tan :: B -> B); \
297     asin                = unsafeCoerce# (asin :: B -> B); \
298     acos                = unsafeCoerce# (acos :: B -> B); \
299     atan                = unsafeCoerce# (atan :: B -> B); \
300     sinh                = unsafeCoerce# (sinh :: B -> B); \
301     cosh                = unsafeCoerce# (cosh :: B -> B); \
302     tanh                = unsafeCoerce# (tanh :: B -> B); \
303     asinh               = unsafeCoerce# (asinh :: B -> B); \
304     acosh               = unsafeCoerce# (acosh :: B -> B); \
305     atanh               = unsafeCoerce# (atanh :: B -> B); }
306
307 /* The coerce trick doesn't work for RealFrac, these methods are
308  * polymorphic and overloaded.
309  */
310 #define INSTANCE_REALFRAC(T) \
311 instance RealFrac T where { \
312    properFraction (T x) = let (m,y) = properFraction x in (m, T y) ; \
313    truncate (T x) = truncate x ; \
314    round    (T x) = round x ; \
315    ceiling  (T x) = ceiling x ; \
316    floor    (T x) = floor x }
317
318 #define INSTANCE_REALFLOAT(T,B) \
319 instance RealFloat T where { \
320     floatRadix          = unsafeCoerce# (floatRadix :: B -> Integer); \
321     floatDigits         = unsafeCoerce# (floatDigits :: B -> Int); \
322     floatRange          = unsafeCoerce# (floatRange :: B -> (Int,Int)); \
323     decodeFloat         = unsafeCoerce# (decodeFloat :: B -> (Integer,Int)); \
324     encodeFloat         = unsafeCoerce# (encodeFloat :: Integer -> Int -> B); \
325     exponent            = unsafeCoerce# (exponent :: B -> Int); \
326     significand         = unsafeCoerce# (significand :: B -> B); \
327     scaleFloat          = unsafeCoerce# (scaleFloat :: Int -> B -> B); \
328     isNaN               = unsafeCoerce# (isNaN :: B -> Bool); \
329     isInfinite          = unsafeCoerce# (isInfinite :: B -> Bool); \
330     isDenormalized      = unsafeCoerce# (isDenormalized :: B -> Bool); \
331     isNegativeZero      = unsafeCoerce# (isNegativeZero :: B -> Bool); \
332     isIEEE              = unsafeCoerce# (isIEEE :: B -> Bool); \
333     atan2               = unsafeCoerce# (atan2 :: B -> B -> B); }
334
335 #endif /* __GLASGOW_HASKELL__ */