[project @ 2001-11-14 11:15:53 by simonmar]
[ghc-hetmet.git] / ghc / lib / std / Prelude.lhs
1 % ------------------------------------------------------------------------------
2 % $Id: Prelude.lhs,v 1.27 2001/11/14 11:15:53 simonmar Exp $
3 %
4 % (c) The University of Glasgow, 1992-2000
5 %
6
7 \section[Prelude]{Module @Prelude@}
8
9 We add the option -fno-implicit-prelude here to tell the reader that
10 special names such as () and -> shouldn't be resolved to Prelude.()
11 and Prelude.-> (as they are normally). -- SDM 8/10/97
12
13 \begin{code}
14 {-# OPTIONS -fno-implicit-prelude #-}
15
16 module Prelude (
17
18         -- Everything corresponding to the Report's PreludeList
19     module PrelList, 
20     lines, words, unlines, unwords,
21     sum, product,
22
23         -- Everything corresponding to the Report's PreludeText
24     ReadS, ShowS,
25     Read(readsPrec, readList),
26     Show(showsPrec, showList, show),
27     reads, shows, read, lex, 
28     showChar, showString, readParen, showParen,
29     
30         -- Everything corresponding to the Report's PreludeIO
31     ioError, userError, catch,
32     FilePath, IOError,
33     putChar,
34     putStr, putStrLn, print,
35     getChar,
36     getLine, getContents, interact,
37     readFile, writeFile, appendFile, readIO, readLn,
38
39     Bool(..),
40     Maybe(..),
41     Either(..),
42     Ordering(..), 
43     Char, String, Int, Integer, Float, Double, IO,
44     Rational,
45     []((:), []),
46     
47     module PrelTup,
48         -- Includes tuple types + fst, snd, curry, uncurry
49     ()(..),             -- The unit type
50     (->),               -- functions
51     
52     Eq(..),
53     Ord(..), 
54     Enum(..),
55     Bounded(..), 
56     Num(..),
57     Real(..),
58     Integral(..),
59     Fractional(..),
60     Floating(..),
61     RealFrac(..),
62     RealFloat(..),
63
64         -- Monad stuff, from PrelBase, and defined here
65     Monad(..),
66     Functor(..), 
67     mapM, mapM_, sequence, sequence_, (=<<),
68
69     maybe, either,
70     (&&), (||), not, otherwise,
71     subtract, even, odd, gcd, lcm, (^), (^^), 
72     fromIntegral, realToFrac,
73     --exported by PrelTup: fst, snd, curry, uncurry,
74     id, const, (.), flip, ($), until,
75     asTypeOf, error, undefined,
76     seq, ($!)
77
78   ) where
79
80 import Monad
81
82 import PrelBase
83 import PrelList
84 #ifndef USE_REPORT_PRELUDE
85      hiding ( takeUInt_append )
86 #endif
87 import PrelIO
88 import PrelIOBase
89 import PrelException
90 import PrelRead
91 import PrelEnum
92 import PrelNum
93 import PrelReal
94 import PrelFloat
95 import PrelTup
96 import PrelMaybe
97 import PrelShow
98 import PrelErr   ( error, undefined )
99
100 infixr 0 $!
101 \end{code}
102
103
104 %*********************************************************
105 %*                                                      *
106 \subsection{Miscellaneous functions}
107 %*                                                      *
108 %*********************************************************
109
110 \begin{code}
111 ($!)    :: (a -> b) -> a -> b
112 f $! x  = x `seq` f x
113 \end{code}
114
115
116 %*********************************************************
117 %*                                                      *
118 \subsection{List sum and product}
119 %*                                                      *
120 %*********************************************************
121
122 List sum and product are defined here because PrelList is too far
123 down the compilation chain to "see" the Num class.
124
125 \begin{code}
126 -- sum and product compute the sum or product of a finite list of numbers.
127 {-# SPECIALISE sum     :: [Int] -> Int #-}
128 {-# SPECIALISE sum     :: [Integer] -> Integer #-}
129 {-# SPECIALISE product :: [Int] -> Int #-}
130 {-# SPECIALISE product :: [Integer] -> Integer #-}
131 sum, product            :: (Num a) => [a] -> a
132 #ifdef USE_REPORT_PRELUDE
133 sum                     =  foldl (+) 0  
134 product                 =  foldl (*) 1
135 #else
136 sum     l       = sum' l 0
137   where
138     sum' []     a = a
139     sum' (x:xs) a = sum' xs (a+x)
140 product l       = prod l 1
141   where
142     prod []     a = a
143     prod (x:xs) a = prod xs (a*x)
144 #endif
145 \end{code}
146