[project @ 1996-01-18 16:33:17 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 import TyArray
15 import TyComplex
16
17 instance (Eq a, Eq b, Eq c, Eq d) => Eq (a,b,c,d) where
18     (a1,a2,a3,a4) == (b1,b2,b3,b4) = a1 == b1 && a2 == b2 && a3 == b3 && a4 == b4
19     aaaa          /= bbbb          = if (aaaa == bbbb) then False else True
20
21 instance (Ord a, Ord b, Ord c, Ord d) => Ord (a,b,c,d) where
22     a <  b  = case _tagCmp a b of { _LT -> True;  _EQ -> False; _GT -> False }
23     a <= b  = case _tagCmp a b of { _LT -> True;  _EQ -> True;  _GT -> False }
24     a >= b  = case _tagCmp a b of { _LT -> False; _EQ -> True;  _GT -> True  }
25     a >  b  = case _tagCmp a b of { _LT -> False; _EQ -> False; _GT -> True  }
26
27     max a b = case _tagCmp a b of { _LT -> b; _EQ -> a;  _GT -> a }
28     min a b = case _tagCmp a b of { _LT -> a; _EQ -> a;  _GT -> b }
29
30     _tagCmp (a1, b1, c1, d1) (a2, b2, c2, d2)
31       = case (_tagCmp a1 a2) of {
32           _LT -> _LT;
33           _GT -> _GT;
34           _EQ -> case _tagCmp b1 b2 of {
35                       _LT -> _LT;
36                       _GT -> _GT;
37                       _EQ -> case _tagCmp c1 c2 of {
38                                   _LT -> _LT;
39                                   _GT -> _GT;
40                                   _EQ -> _tagCmp d1 d2
41                                 }
42                     }
43         }
44
45 instance  (Ix a1, Ix a2, Ix a3, Ix a4) => Ix (a1,a2,a3,a4)  where
46     range ((l1,l2,l3,l4),(u1,u2,u3,u4)) =
47         [(i1,i2,i3,i4) | i1 <- range (l1,u1),
48                          i2 <- range (l2,u2),
49                          i3 <- range (l3,u3),
50                          i4 <- range (l4,u4)]
51
52     index ((l1,l2,l3,l4),(u1,u2,u3,u4)) (i1,i2,i3,i4) =
53       index (l4,u4) i4 + rangeSize (l4,u4) * (
54        index (l3,u3) i3 + rangeSize (l3,u3) * (
55          index (l2,u2) i2 + rangeSize (l2,u2) * (
56            index (l1,u1) i1)))
57       where
58         rangeSize (l,u) = index (l,u) u + (1 :: Int)
59
60     inRange ((l1,l2,l3,l4),(u1,u2,u3,u4)) (i1,i2,i3,i4) =
61         inRange (l1,u1) i1 && inRange (l2,u2) i2 &&
62            inRange (l3,u3) i3 && inRange (l4,u4) i4
63
64 -- ToDo: something for Binary
65
66 instance (Text a, Text b, Text c, Text d) => Text (a, b, c, d) where
67     readsPrec p = readParen False
68                     (\a -> [((w,x,y,z), j) | ("(",b) <- lex a,
69                                              (w,c)   <- readsPrec 0 b,
70                                              (",",d) <- lex c,
71                                              (x,e)   <- readsPrec 0 d,
72                                              (",",f) <- lex e,
73                                              (y,g)   <- readsPrec 0 f,
74                                              (",",h) <- lex g,
75                                              (z,i)   <- readsPrec 0 h,
76                                              (")",j) <- lex i ] )
77
78     showsPrec p (w,x,y,z) = showChar '(' . showsPrec 0 w . showString ", " .
79                                            showsPrec 0 x . showString ", " .
80                                            showsPrec 0 y . showString ", " .
81                                            showsPrec 0 z . showChar ')'
82
83     readList    = _readList (readsPrec 0)
84     showList    = _showList (showsPrec 0) 
85
86 {-# SPECIALIZE instance Eq   (Int,Int,Int,Int) #-}
87 {-# SPECIALIZE instance Ord  (Int,Int,Int,Int) #-}
88
89 #if defined(__UNBOXED_INSTANCES__)
90
91 -- We only create SPECIALIZED instances unboxed tuples
92 -- which have all the same unboxed component
93
94 -- {-# SPECIALIZE instance Eq   (Char#,Char#,Char#,Char#) #-}
95 -- {-# SPECIALIZE instance Ord  (Char#,Char#,Char#,Char#) #-}
96 -- {-# SPECIALIZE instance Ix   (Char#,Char#,Char#,Char#) #-}
97 -- {-# SPECIALIZE instance Text (Char#,Char#,Char#,Char#) #-}
98
99 -- {-# SPECIALIZE instance Eq   (Int#,Int#,Int#,Int#) #-}
100 -- {-# SPECIALIZE instance Ord  (Int#,Int#,Int#,Int#) #-}
101 -- {-# SPECIALIZE instance Ix   (Int#,Int#,Int#,Int#) #-}
102 -- {-# SPECIALIZE instance Text (Int#,Int#,Int#,Int#) #-}
103
104 -- {-# SPECIALIZE instance Eq   (Double#,Double#,Double#,Double#) #-}
105 -- {-# SPECIALIZE instance Ord  (Double#,Double#,Double#,Double#) #-}
106 -- {-# SPECIALIZE instance Text (Double#,Double#,Double#,Double#) #-}
107
108 #endif