[project @ 2001-08-04 06:19:54 by ken]
[ghc-hetmet.git] / ghc / lib / std / Prelude.lhs
1 % ------------------------------------------------------------------------------
2 % $Id: Prelude.lhs,v 1.26 2001/05/18 16:54:05 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 PrelConc
99 import PrelErr   ( error, undefined )
100
101 infixr 0 $!
102 \end{code}
103
104
105 %*********************************************************
106 %*                                                      *
107 \subsection{Miscellaneous functions}
108 %*                                                      *
109 %*********************************************************
110
111 \begin{code}
112 ($!)    :: (a -> b) -> a -> b
113 f $! x  = x `seq` f x
114 \end{code}
115
116
117 %*********************************************************
118 %*                                                      *
119 \subsection{List sum and product}
120 %*                                                      *
121 %*********************************************************
122
123 List sum and product are defined here because PrelList is too far
124 down the compilation chain to "see" the Num class.
125
126 \begin{code}
127 -- sum and product compute the sum or product of a finite list of numbers.
128 {-# SPECIALISE sum     :: [Int] -> Int #-}
129 {-# SPECIALISE sum     :: [Integer] -> Integer #-}
130 {-# SPECIALISE product :: [Int] -> Int #-}
131 {-# SPECIALISE product :: [Integer] -> Integer #-}
132 sum, product            :: (Num a) => [a] -> a
133 #ifdef USE_REPORT_PRELUDE
134 sum                     =  foldl (+) 0  
135 product                 =  foldl (*) 1
136 #else
137 sum     l       = sum' l 0
138   where
139     sum' []     a = a
140     sum' (x:xs) a = sum' xs (a+x)
141 product l       = prod l 1
142   where
143     prod []     a = a
144     prod (x:xs) a = prod xs (a*x)
145 #endif
146 \end{code}
147