[project @ 2001-08-04 06:19:54 by ken]
[ghc-hetmet.git] / ghc / lib / std / Prelude.lhs
index 236558c..ebe7b82 100644 (file)
@@ -1,3 +1,11 @@
+% ------------------------------------------------------------------------------
+% $Id: Prelude.lhs,v 1.26 2001/05/18 16:54:05 simonmar Exp $
+%
+% (c) The University of Glasgow, 1992-2000
+%
+
+\section[Prelude]{Module @Prelude@}
+
 We add the option -fno-implicit-prelude here to tell the reader that
 special names such as () and -> shouldn't be resolved to Prelude.()
 and Prelude.-> (as they are normally). -- SDM 8/10/97
@@ -7,8 +15,10 @@ and Prelude.-> (as they are normally). -- SDM 8/10/97
 
 module Prelude (
 
-       -- Everything from these modules
-    module PrelList,
+       -- Everything corresponding to the Report's PreludeList
+    module PrelList, 
+    lines, words, unlines, unwords,
+    sum, product,
 
         -- Everything corresponding to the Report's PreludeText
     ReadS, ShowS,
@@ -18,10 +28,12 @@ module Prelude (
     showChar, showString, readParen, showParen,
     
         -- Everything corresponding to the Report's PreludeIO
-    FilePath, IOError,
     ioError, userError, catch,
-    putChar, putStr, putStrLn, print,
-    getChar, getLine, getContents, interact,
+    FilePath, IOError,
+    putChar,
+    putStr, putStrLn, print,
+    getChar,
+    getLine, getContents, interact,
     readFile, writeFile, appendFile, readIO, readLn,
 
     Bool(..),
@@ -29,7 +41,7 @@ module Prelude (
     Either(..),
     Ordering(..), 
     Char, String, Int, Integer, Float, Double, IO,
-    Ratio, Rational, 
+    Rational,
     []((:), []),
     
     module PrelTup,
@@ -41,15 +53,15 @@ module Prelude (
     Ord(..), 
     Enum(..),
     Bounded(..), 
-    Num((+), (-), (*), negate, abs, signum, fromInteger, fromInt{-glaExt-}),
+    Num(..),
     Real(..),
-    Integral(quot, rem, div, mod, quotRem, divMod, toInteger, toInt{-partain-}),
+    Integral(..),
     Fractional(..),
     Floating(..),
     RealFrac(..),
     RealFloat(..),
 
-       -- From Monad
+       -- Monad stuff, from PrelBase, and defined here
     Monad(..),
     Functor(..), 
     mapM, mapM_, sequence, sequence_, (=<<),
@@ -65,33 +77,71 @@ module Prelude (
 
   ) where
 
+import Monad
+
 import PrelBase
-import PrelList hiding ( takeUInt )
+import PrelList
+#ifndef USE_REPORT_PRELUDE
+     hiding ( takeUInt_append )
+#endif
+import PrelIO
+import PrelIOBase
+import PrelException
 import PrelRead
+import PrelEnum
 import PrelNum
-import PrelNumExtra
+import PrelReal
+import PrelFloat
 import PrelTup
 import PrelMaybe
-import PrelEither
-import PrelBounded
+import PrelShow
 import PrelConc
-import Monad
-import Maybe
-import PrelErr   ( error )
-import IO
+import PrelErr   ( error, undefined )
 
 infixr 0 $!
+\end{code}
 
+
+%*********************************************************
+%*                                                     *
+\subsection{Miscellaneous functions}
+%*                                                     *
+%*********************************************************
+
+\begin{code}
 ($!)    :: (a -> b) -> a -> b
 f $! x  = x `seq` f x
+\end{code}
 
--- It is expected that compilers will recognize this and insert error
--- messages which are more appropriate to the context in which undefined 
--- appears. 
 
-undefined               :: a
-undefined               =  error "Prelude.undefined"
-\end{code}
+%*********************************************************
+%*                                                     *
+\subsection{List sum and product}
+%*                                                     *
+%*********************************************************
 
+List sum and product are defined here because PrelList is too far
+down the compilation chain to "see" the Num class.
 
+\begin{code}
+-- sum and product compute the sum or product of a finite list of numbers.
+{-# SPECIALISE sum     :: [Int] -> Int #-}
+{-# SPECIALISE sum     :: [Integer] -> Integer #-}
+{-# SPECIALISE product :: [Int] -> Int #-}
+{-# SPECIALISE product :: [Integer] -> Integer #-}
+sum, product            :: (Num a) => [a] -> a
+#ifdef USE_REPORT_PRELUDE
+sum                     =  foldl (+) 0  
+product                 =  foldl (*) 1
+#else
+sum    l       = sum' l 0
+  where
+    sum' []     a = a
+    sum' (x:xs) a = sum' xs (a+x)
+product        l       = prod l 1
+  where
+    prod []     a = a
+    prod (x:xs) a = prod xs (a*x)
+#endif
+\end{code}