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