291030876f1768a58b48cc663a4f3369c6b1c7d9
[ghc-hetmet.git] / ghc / tests / specialise / spec003 / PreludeNum.hs
1 module PreludeNum (f1, f2, fac, f3, fac_two) where
2
3 {- Preliminaries ... -}
4
5 {- patError# { Int# } (built into compiler) -}
6 local_map f []     = []
7 local_map f (x:xs) = (f x) : local_map f xs
8
9 instance  Eq Int#  where
10     x == y = eqInt# x y
11     x /= y = neInt# x y
12
13 instance  Read Int#  where
14     readsPrec p s = map (\ (I# i#, s) -> (i#, s)) (readsPrec p s)
15     readList s = map (\ (x, s) -> (local_map (\ (I# i#) -> i#) x, s)) (readList s)
16
17 instance  Show Int#  where
18     showsPrec p x = showsPrec p (I# x)
19     showList l = showList (local_map I# l)
20
21 instance  Num Int#  where
22     (+)    x y =  plusInt# x y
23     (-)    x y =  minusInt# x y
24     negate x   =  negateInt# x
25     (*)    x y =  timesInt# x y
26     abs    n   = if n `geInt#` 0# then n else (negateInt# n)
27
28     signum n | n `ltInt#` 0# = negateInt# 1#
29              | n `eqInt#` 0# = 0#
30              | otherwise     = 1#
31
32     fromInteger (J# a# s# d#)
33       = integer2Int# a# s# d#
34
35     fromInt (I# i#) = i#
36
37
38 first  (a, b) = a
39 second (a, b) = b
40
41 {- Here we go ... -}
42
43 fac 0 = 1
44 fac n = n * (fac (n - 1))
45
46 {-# INLINE f1 #-}
47 f1 = fac 10#
48
49 f2 = f1 * f1
50
51 fac_two n two = case n of 0 -> (1, 1)
52                           n -> (n * (first (fac_two (n - 1) two)), 2)
53
54 f3 = let (res1, two1) = fac_two (10::Int#) (two2::Int#)
55          (res2, two2) = fac_two (10::Int)  (two1::Int)
56      in
57      res1 + two2
58