[project @ 1996-01-18 16:33:17 by partain]
[ghc-hetmet.git] / ghc / lib / prelude / IBool.hs
1 module PreludeCore ( Bool(..) ) where
2
3 import Prel             ( (&&) )
4 import Core
5 import Cls
6 import IChar
7 import IInt
8 import IList
9 import List             ( (++), map, foldr, takeWhile )
10 import PS               ( _PackedString, _unpackPS )
11 import Text
12 import TyArray
13 import TyComplex
14
15 ----------------------------------------------------------------------
16 instance Eq Bool where
17     True  == True       = True
18     False == False      = True
19     _     == _          = False
20
21     True  /= False      = True
22     False /= True       = True
23     _     /= _          = False
24
25 ----------------------------------------------------------------------
26 instance Ord Bool where
27     a <  b  = case _tagCmp a b of { _LT -> True;  _EQ -> False; _GT -> False }
28     a <= b  = case _tagCmp a b of { _LT -> True;  _EQ -> True;  _GT -> False }
29     a >= b  = case _tagCmp a b of { _LT -> False; _EQ -> True;  _GT -> True  }
30     a >  b  = case _tagCmp a b of { _LT -> False; _EQ -> False; _GT -> True  }
31
32     max a b = case _tagCmp a b of { _LT -> b; _EQ -> a;  _GT -> a }
33     min a b = case _tagCmp a b of { _LT -> a; _EQ -> a;  _GT -> b }
34
35     _tagCmp True  True  = _EQ
36     _tagCmp True         False  = _GT
37     _tagCmp False True  = _LT
38     _tagCmp False False = _EQ
39
40 ----------------------------------------------------------------------
41 instance Ix Bool where
42     range   (l,u)   = map bdecode [(bencode l) .. (bencode u)]
43     index   (l,u) i = (bencode i) - (bencode l)
44     inRange (l,u) i = (bencode i) >= (bencode l) && (bencode i) <= (bencode u)
45
46 bencode :: Bool -> Int
47 bencode False = 0
48 bencode True  = 1
49
50 bdecode :: Int -> Bool
51 bdecode b = if      b == 0 then False
52             else if b == 1 then True
53             else error "decode Bool\n"
54
55 ----------------------------------------------------------------------
56 instance Enum Bool where
57     enumFrom False      =  [False, True]
58     enumFrom True       =  [True]
59
60     enumFromThen False True   = [False, True]
61     enumFromThen True  False  = [True, False]
62     enumFromThen b     _      = bs where bs = b : bs
63
64     enumFromTo n m       =  takeWhile (<= m) (enumFrom n)
65     enumFromThenTo n m p =  takeWhile (if m >= n then (<= p) else (>= p))
66                                       (enumFromThen n m)
67
68 ----------------------------------------------------------------------
69 instance Text Bool where
70     readsPrec p r
71       = readParen False (\ b -> [ (False, c) | ("False", c) <- lex b ]) r
72      ++ readParen False (\ b -> [ (True,  c) | ("True",  c) <- lex b ]) r
73
74     showsPrec d p = showString (if p then "True" else "False")
75
76     readList = _readList (readsPrec 0)
77     showList = _showList (showsPrec 0) 
78
79 -- ToDo: Binary