1 module PreludeCore ( Int(..), rangeComplaint_Ix_Int#{-see comment later-} ) where
5 import IInteger -- instances
8 import List ( (++), foldr )
9 import Prel ( otherwise, (&&), (||), chr, ord )
10 import PS ( _PackedString, _unpackPS )
13 -- definitions of the boxed PrimOps; these will be
14 -- used in the case of partial applications, etc.
16 plusInt (I# x) (I# y) = I# (plusInt# x y)
17 minusInt(I# x) (I# y) = I# (minusInt# x y)
18 timesInt(I# x) (I# y) = I# (timesInt# x y)
19 quotInt (I# x) (I# y) = I# (quotInt# x y)
20 divInt (I# x) (I# y) = I# (divInt# x y)
21 remInt (I# x) (I# y) = I# (remInt# x y)
22 negateInt (I# x) = I# (negateInt# x)
23 gtInt (I# x) (I# y) = gtInt# x y
24 geInt (I# x) (I# y) = geInt# x y
25 eqInt (I# x) (I# y) = eqInt# x y
26 neInt (I# x) (I# y) = neInt# x y
27 ltInt (I# x) (I# y) = ltInt# x y
28 leInt (I# x) (I# y) = leInt# x y
30 ---------------------------------------------------------------
36 instance Ord Int where
42 max a b = case _tagCmp a b of { _LT -> b; _EQ -> a; _GT -> a }
43 min a b = case _tagCmp a b of { _LT -> a; _EQ -> a; _GT -> b }
45 _tagCmp (I# a#) (I# b#)
46 = if (a# ==# b#) then _EQ
47 else if (a# <# b#) then _LT else _GT
49 instance Num Int where
51 (-) x y = minusInt x y
52 negate x = negateInt x
53 (*) x y = timesInt x y
54 abs n = if n `geInt` 0 then n else (negateInt n)
56 signum n | n `ltInt` 0 = negateInt 1
60 fromInteger (J# a# s# d#)
61 = case (integer2Int# a# s# d#) of { i# -> I# i# }
65 instance Real Int where
66 toRational x = toInteger x % 1
68 instance Integral Int where
69 a@(I# _) `quotRem` b@(I# _) = (a `quotInt` b, a `remInt` b)
70 -- OK, so I made it a little stricter. Shoot me. (WDP 94/10)
72 -- following chks for zero divisor are non-standard (WDP)
73 a `quot` b = if b /= 0
75 else error "Integral.Int.quot{PreludeCore}: divide by 0\n"
78 else error "Integral.Int.rem{PreludeCore}: divide by 0\n"
80 x `div` y = if x > 0 && y < 0 then quotInt (x-y-1) y
81 else if x < 0 && y > 0 then quotInt (x-y+1) y
83 x `mod` y = if x > 0 && y < 0 || x < 0 && y > 0 then
84 if r/=0 then r+y else 0
89 divMod x@(I# _) y@(I# _) = (x `div` y, x `mod` y)
90 -- Stricter. Sorry if you don't like it. (WDP 94/10)
92 even x = eqInt (x `mod` 2) 0
93 odd x = neInt (x `mod` 2) 0
95 toInteger (I# n#) = int2Integer# n# -- give back a full-blown Integer
98 rangeComplaint_Ix_Int# i m n -- export it so it will *not* be floated inwards
99 = error ("Ix.Int.index2{PreludeCore}: Index "
100 ++ show (I# i) ++ " outside the range "
101 ++ show (I# m,I# n) ++ ".\n")
103 instance Ix Int where
105 index b@(I# m, I# n) (I# i)
106 | inRange b (I# i) = I# (i -# m)
107 | otherwise = rangeComplaint_Ix_Int# i m n
108 inRange (I# m, I# n) (I# i) = m <=# i && i <=# n
110 instance Enum Int where
111 {- RAW PRELUDE ************************
112 enumFrom = numericEnumFrom
113 enumFromThen = numericEnumFromThen
115 #ifndef USE_FOLDR_BUILD
116 enumFrom x = x : enumFrom (x `plusInt` 1)
118 {-# INLINE enumFromTo #-}
119 {-# INLINE enumFrom #-}
120 enumFromTo x y = _build (\ c n ->
121 let g x = if x <= y then x `c` g (x `plusInt` 1) else n in g x)
122 enumFrom x = _build (\ c _ ->
123 let g x = x `c` g (x `plusInt` 1) in g x)
125 enumFromThen m n = en' m (n `minusInt` m)
126 where en' m n = m : en' (m `plusInt` n) n
128 instance Text Int where
129 readsPrec p x = readSigned readDec x
130 showsPrec x = showSigned showInt x
132 ---------------------------------------------------------------
133 instance _CCallable Int
134 instance _CReturnable Int
136 #if defined(__UNBOXED_INSTANCES__)
137 ---------------------------------------------------------------
138 -- Instances for Int#
139 ---------------------------------------------------------------
141 instance Eq Int# where
142 (==) x y = eqInt# x y
143 (/=) x y = neInt# x y
145 instance Ord Int# where
146 (<=) x y = leInt# x y
148 (>=) x y = geInt# x y
151 max a b = case _tagCmp a b of { _LT -> b; _EQ -> a; _GT -> a }
152 min a b = case _tagCmp a b of { _LT -> a; _EQ -> a; _GT -> b }
155 = if (a `eqInt#` b) then _EQ
156 else if (a `ltInt#` b) then _LT else _GT
158 instance Num Int# where
159 (+) x y = plusInt# x y
160 (-) x y = minusInt# x y
161 negate x = negateInt# x
162 (*) x y = timesInt# x y
163 abs n = if n `geInt#` 0 then n else (negateInt# n)
165 signum n | n `ltInt#` 0 = negateInt# 1
169 fromInteger (J# a# s# d#)
170 = integer2Int# a# s# d#
174 instance Real Int# where
175 toRational x = toInteger x % 1
177 instance Integral Int# where
178 a `quotRem` b = (a `quotInt#` b, a `remInt#` b)
180 -- following chks for zero divisor are non-standard (WDP)
181 a `quot` b = if b /= 0
183 else error "Integral.Int#.quot{PreludeCore}: divide by 0\n"
184 a `rem` b = if b /= 0
186 else error "Integral.Int#.rem{PreludeCore}: divide by 0\n"
188 x `div` y = if x > 0 && y < 0 then quotInt# (x-y-1) y
189 else if x < 0 && y > 0 then quotInt# (x-y+1) y
191 x `mod` y = if x > 0 && y < 0 || x < 0 && y > 0 then
192 if r/=0 then r+y else 0
195 where r = remInt# x y
197 divMod x y = (x `div` y, x `mod` y)
199 even x = eqInt# (x `mod` 2) 0
200 odd x = neInt# (x `mod` 2) 0
202 toInteger n# = int2Integer# n# -- give back a full-blown Integer
205 instance Ix Int# where
208 | inRange b i = I# (i -# m)
209 | otherwise = rangeComplaint_Ix_Int# i m n
210 inRange (m, n) i = m <=# i && i <=# n
212 instance Enum Int# where
213 enumFrom x = x : enumFrom (x `plusInt#` 1)
214 enumFromThen m n = en' m (n `minusInt#` m)
215 where en' m n = m : en' (m `plusInt#` n) n
216 -- default methods not specialised!
217 enumFromTo n m = takeWhile (<= m) (enumFrom n)
218 enumFromThenTo n m p = takeWhile (if m >= n then (<= p) else (>= p))
221 -- ToDo: efficient Text Int# instance
222 instance Text Int# where
223 readsPrec p s = map (\ (I# i#, s) -> (i#, s)) (readsPrec p s)
224 showsPrec p x = showsPrec p (I# x)
225 readList s = map (\ (x, s) -> (map (\ (I# i#) -> i#) x, s)) (readList s)
226 showList l = showList (map I# l)
228 instance _CCallable Int#
229 instance _CReturnable Int#
231 #endif {-UNBOXED INSTANCES-}
233 ---------------------------------------------------------------
234 -- Instances for Addr Word etc #
235 ---------------------------------------------------------------
237 instance _CCallable _Addr
238 instance _CCallable _Word
239 instance _CCallable _MallocPtr
241 instance _CReturnable _Addr
242 instance _CReturnable _Word
243 instance _CReturnable ()
244 instance _CReturnable _MallocPtr
246 #ifndef __PARALLEL_HASKELL__
247 instance _CCallable (_StablePtr a)
248 instance _CReturnable (_StablePtr a)
251 ---------------------------------------------------------------
252 gtAddr (A# x) (A# y) = gtAddr# x y
253 geAddr (A# x) (A# y) = geAddr# x y
254 eqAddr (A# x) (A# y) = eqAddr# x y
255 neAddr (A# x) (A# y) = neAddr# x y
256 ltAddr (A# x) (A# y) = ltAddr# x y
257 leAddr (A# x) (A# y) = leAddr# x y
259 instance Eq _Addr where
260 (==) x y = eqAddr x y
261 (/=) x y = neAddr x y
263 instance Ord _Addr where
264 (<=) x y = leAddr x y
266 (>=) x y = geAddr x y
269 max a b = case _tagCmp a b of { _LT -> b; _EQ -> a; _GT -> a }
270 min a b = case _tagCmp a b of { _LT -> a; _EQ -> a; _GT -> b }
272 _tagCmp (A# a#) (A# b#)
273 = if (eqAddr# a# b#) then _EQ
274 else if (ltAddr# a# b#) then _LT else _GT
276 ---------------------------------------------------------------
277 gtWord (W# x) (W# y) = gtWord# x y
278 geWord (W# x) (W# y) = geWord# x y
279 eqWord (W# x) (W# y) = eqWord# x y
280 neWord (W# x) (W# y) = neWord# x y
281 ltWord (W# x) (W# y) = ltWord# x y
282 leWord (W# x) (W# y) = leWord# x y
284 instance Eq _Word where
285 (==) x y = eqWord x y
286 (/=) x y = neWord x y
288 instance Ord _Word where
289 (<=) x y = leWord x y
291 (>=) x y = geWord x y
294 max a b = case _tagCmp a b of { _LT -> b; _EQ -> a; _GT -> a }
295 min a b = case _tagCmp a b of { _LT -> a; _EQ -> a; _GT -> b }
297 _tagCmp (W# a#) (W# b#)
298 = if (eqWord# a# b#) then _EQ
299 else if (ltWord# a# b#) then _LT else _GT