[project @ 1996-01-08 20:28:12 by partain]
[ghc-hetmet.git] / ghc / lib / prelude / ITup4.hs
1 module PreludeBuiltin where
2
3 --- 4-tuples ------------------------------------------
4
5 import Cls
6 import Core
7 import IChar
8 import IInt
9 import IList
10 import List             ( (++), foldr )
11 import Prel             ( (&&), (.) )
12 import PS               ( _PackedString, _unpackPS )
13 import Text
14
15 instance (Eq a, Eq b, Eq c, Eq d) => Eq (a,b,c,d) where
16     (a1,a2,a3,a4) == (b1,b2,b3,b4) = a1 == b1 && a2 == b2 && a3 == b3 && a4 == b4
17     aaaa          /= bbbb          = if (aaaa == bbbb) then False else True
18
19 instance (Ord a, Ord b, Ord c, Ord d) => Ord (a,b,c,d) where
20     a <  b  = case _tagCmp a b of { _LT -> True;  _EQ -> False; _GT -> False }
21     a <= b  = case _tagCmp a b of { _LT -> True;  _EQ -> True;  _GT -> False }
22     a >= b  = case _tagCmp a b of { _LT -> False; _EQ -> True;  _GT -> True  }
23     a >  b  = case _tagCmp a b of { _LT -> False; _EQ -> False; _GT -> True  }
24
25     max a b = case _tagCmp a b of { _LT -> b; _EQ -> a;  _GT -> a }
26     min a b = case _tagCmp a b of { _LT -> a; _EQ -> a;  _GT -> b }
27
28     _tagCmp (a1, b1, c1, d1) (a2, b2, c2, d2)
29       = case (_tagCmp a1 a2) of {
30           _LT -> _LT;
31           _GT -> _GT;
32           _EQ -> case _tagCmp b1 b2 of {
33                       _LT -> _LT;
34                       _GT -> _GT;
35                       _EQ -> case _tagCmp c1 c2 of {
36                                   _LT -> _LT;
37                                   _GT -> _GT;
38                                   _EQ -> _tagCmp d1 d2
39                                 }
40                     }
41         }
42
43 instance  (Ix a1, Ix a2, Ix a3, Ix a4) => Ix (a1,a2,a3,a4)  where
44     range ((l1,l2,l3,l4),(u1,u2,u3,u4)) =
45         [(i1,i2,i3,i4) | i1 <- range (l1,u1),
46                          i2 <- range (l2,u2),
47                          i3 <- range (l3,u3),
48                          i4 <- range (l4,u4)]
49
50     index ((l1,l2,l3,l4),(u1,u2,u3,u4)) (i1,i2,i3,i4) =
51       index (l4,u4) i4 + rangeSize (l4,u4) * (
52        index (l3,u3) i3 + rangeSize (l3,u3) * (
53          index (l2,u2) i2 + rangeSize (l2,u2) * (
54            index (l1,u1) i1)))
55       where
56         rangeSize (l,u) = index (l,u) u + (1 :: Int)
57
58     inRange ((l1,l2,l3,l4),(u1,u2,u3,u4)) (i1,i2,i3,i4) =
59         inRange (l1,u1) i1 && inRange (l2,u2) i2 &&
60            inRange (l3,u3) i3 && inRange (l4,u4) i4
61
62 -- ToDo: something for Binary
63
64 instance (Text a, Text b, Text c, Text d) => Text (a, b, c, d) where
65     readsPrec p = readParen False
66                     (\a -> [((w,x,y,z), j) | ("(",b) <- lex a,
67                                              (w,c)   <- reads b,
68                                              (",",d) <- lex c,
69                                              (x,e)   <- reads d,
70                                              (",",f) <- lex e,
71                                              (y,g)   <- reads f,
72                                              (",",h) <- lex g,
73                                              (z,i)   <- reads h,
74                                              (")",j) <- lex i ] )
75
76     showsPrec p (w,x,y,z) = showChar '(' . shows w . showString ", " .
77                                            shows x . showString ", " .
78                                            shows y . showString ", " .
79                                            shows z . showChar ')'
80
81 #if defined(__UNBOXED_INSTANCES__)
82
83 -- We only create SPECIALIZED instances unboxed tuples
84 -- which have all the same unboxed component
85
86 -- {-# SPECIALIZE instance Eq   (Char#,Char#,Char#,Char#) #-}
87 -- {-# SPECIALIZE instance Ord  (Char#,Char#,Char#,Char#) #-}
88 -- {-# SPECIALIZE instance Ix   (Char#,Char#,Char#,Char#) #-}
89 -- {-# SPECIALIZE instance Text (Char#,Char#,Char#,Char#) #-}
90
91 -- {-# SPECIALIZE instance Eq   (Int#,Int#,Int#,Int#) #-}
92 -- {-# SPECIALIZE instance Ord  (Int#,Int#,Int#,Int#) #-}
93 -- {-# SPECIALIZE instance Ix   (Int#,Int#,Int#,Int#) #-}
94 -- {-# SPECIALIZE instance Text (Int#,Int#,Int#,Int#) #-}
95
96 -- {-# SPECIALIZE instance Eq   (Double#,Double#,Double#,Double#) #-}
97 -- {-# SPECIALIZE instance Ord  (Double#,Double#,Double#,Double#) #-}
98 -- {-# SPECIALIZE instance Text (Double#,Double#,Double#,Double#) #-}
99
100 #endif