[project @ 1998-01-30 16:59:06 by sof]
[ghc-hetmet.git] / ghc / lib / ghc / PrelBase.lhs
index 87fd4d0..c8b4da1 100644 (file)
@@ -13,7 +13,7 @@ module PrelBase(
                                -- to import it explicitly
   ) where
 
-import {-# SOURCE #-} Error ( error )
+import {-# SOURCE #-} GHCerr ( error )
 import GHC
 
 infixr 9  .
@@ -28,6 +28,107 @@ infixl 1  >>, >>=
 infixr 0  $
 \end{code}
 
+
+\begin{code}
+{-
+class Eval a
+data Bool = False | True
+data Int = I# Int#
+data Double    = D# Double#
+data  ()  =  ()  --easier to do explicitly: deriving (Eq, Ord, Enum, Show, Bounded)
+                -- (avoids weird-named functions, e.g., con2tag_()#
+
+data  Maybe a  =  Nothing | Just a     
+data Ordering = LT | EQ | GT    deriving( Eq )
+
+type  String = [Char]
+
+data Char = C# Char#   
+data [] a = [] | a : [a]  -- do explicitly: deriving (Eq, Ord)
+                         -- to avoid weird names like con2tag_[]#
+
+
+-------------- Stage 2 -----------------------
+not True = False
+not False = True
+True  && x             =  x
+False && x             =  False
+otherwise = True
+
+maybe :: b -> (a -> b) -> Maybe a -> b
+maybe n f Nothing  = n
+maybe n f (Just x) = f x
+
+-------------- Stage 3 -----------------------
+class  Eq a  where
+    (==), (/=)         :: a -> a -> Bool
+
+    x /= y             =  not (x == y)
+
+-- f :: Eq a => a -> a -> Bool
+f x y = x == y
+
+g :: Eq a => a -> a -> Bool
+g x y =  f x y 
+
+-------------- Stage 4 -----------------------
+
+class  (Eq a) => Ord a  where
+    compare             :: a -> a -> Ordering
+    (<), (<=), (>=), (>):: a -> a -> Bool
+    max, min           :: a -> a -> a
+
+-- An instance of Ord should define either compare or <=
+-- Using compare can be more efficient for complex types.
+    compare x y
+           | x == y    = EQ
+           | x <= y    = LT
+           | otherwise = GT
+
+    x <= y  = compare x y /= GT
+    x <         y  = compare x y == LT
+    x >= y  = compare x y /= LT
+    x >         y  = compare x y == GT
+    max x y = case (compare x y) of { LT -> y ; EQ -> x ; GT -> x }
+    min x y = case (compare x y) of { LT -> x ; EQ -> x ; GT -> y }
+
+eqInt  (I# x) (I# y) = x ==# y
+
+instance Eq Int where
+    (==) x y = x `eqInt` y
+
+instance Ord Int where
+    compare x y = error "help"
+  
+class  Bounded a  where
+    minBound, maxBound :: a
+
+
+type  ShowS     = String -> String
+
+class  Show a  where
+    showsPrec :: Bool -> a -> ShowS
+    showList  :: [a] -> ShowS
+
+    showList ls = showList__ (showsPrec True) ls 
+
+showList__ :: (a -> ShowS) ->  [a] -> ShowS
+showList__ showx []     = showString "[]"
+
+showString      :: String -> ShowS
+showString      =  (++)
+
+[] ++ [] = []
+
+shows           :: (Show a) => a -> ShowS
+shows           =  showsPrec True
+
+-- show            :: (Show a) => a -> String
+--show x          =  shows x ""
+-}
+\end{code}
+
+
 %*********************************************************
 %*                                                     *
 \subsection{Standard classes @Eq@, @Ord@, @Bounded@, @Eval@}
@@ -323,6 +424,7 @@ it here seems more direct.
 \begin{code}
 data  ()  =  ()  --easier to do explicitly: deriving (Eq, Ord, Enum, Show, Bounded)
                 -- (avoids weird-named functions, e.g., con2tag_()#
+
 instance Eq () where
     () == () = True
     () /= () = False
@@ -345,10 +447,6 @@ instance Enum () where
     enumFromTo () ()   = [()]
     enumFromThenTo () () () = [()]
 
-instance Bounded () where
-    minBound = ()
-    maxBound = ()
-
 instance  Show ()  where
     showsPrec p () = showString "()"
     showList ls    = showList__ (showsPrec 0) ls
@@ -399,10 +497,6 @@ efttCh now step done
     go now | done now  = []
           | otherwise = C# (chr# now) : go (now +# step)
 
-instance  Bounded Char  where
-    minBound            =  '\0'
-    maxBound            =  '\255'
-
 instance  Show Char  where
     showsPrec p '\'' = showString "'\\''"
     showsPrec p c    = showChar '\'' . showLitChar c . showChar '\''
@@ -532,10 +626,6 @@ eftInt now step
     go now = I# now : go (now +# step)
 
 
-instance  Bounded Int where
-    minBound =  -2147483647            -- **********************
-    maxBound =  2147483647            -- **********************
-
 instance  Num Int  where
     (+)           x y =  plusInt x y
     (-)           x y =  minusInt x y
@@ -701,7 +791,7 @@ intToDigit :: Int -> Char
 intToDigit i
  | i >= 0  && i <=  9   =  toEnum (fromEnum '0' + i)
  | i >= 10 && i <= 15   =  toEnum (fromEnum 'a' + i -10)
- | otherwise           =  error "Char.intToDigit: not a digit" -- ....
+ | otherwise           =  error ("Char.intToDigit: not a digit: " ++ show i) -- ....
 
 \end{code}