basic.verb 37.7 KB
Newer Older
Simon Peyton Jones's avatar
Simon Peyton Jones committed
1
%
Simon Peyton Jones's avatar
Simon Peyton Jones committed
2
% $Header: /home/cvs/root/haskell-report/report/basic.verb,v 1.17 2003/01/13 13:08:54 simonpj Exp $
Simon Peyton Jones's avatar
Simon Peyton Jones committed
3
%
Ross Paterson's avatar
Ross Paterson committed
4
%**<title>The Haskell 98 Report: Predefined Types and Classes</title>
Simon Peyton Jones's avatar
Simon Peyton Jones committed
5
6
7
8
9
10
%*section 6
%**~header
\section{Predefined Types and Classes}
\label{basic-types-and-classes}
The \Haskell{} Prelude contains predefined classes, types,
and functions that are implicitly imported into every Haskell
Simon Peyton Jones's avatar
Simon Peyton Jones committed
11
program.  In this chapter, we describe the types and classes found in
Simon Peyton Jones's avatar
Simon Peyton Jones committed
12
13
the Prelude.
Most functions are not described in detail here as they
Simon Peyton Jones's avatar
Simon Peyton Jones committed
14
can easily be understood from their definitions as given in Chapter~\ref{stdprelude}.
Simon Peyton Jones's avatar
Simon Peyton Jones committed
15
Other predefined types such as arrays, complex numbers, and rationals
Simon Peyton Jones's avatar
Simon Peyton Jones committed
16
are defined in Part~\ref{libraries}.
Simon Peyton Jones's avatar
Simon Peyton Jones committed
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52

\subsection{Standard Haskell Types}
\label{basic-types}
These types are defined by the \Haskell{} Prelude.  Numeric types are
described in Section \ref{numbers}.  When appropriate, the \Haskell{}
definition of the type is given.  Some definitions may not be
completely valid on syntactic grounds but they faithfully convey the
meaning of the underlying type.

\subsubsection{Booleans}
\label{booleans}
\index{boolean}
\bprog
@
data  Bool  =  False | True deriving 
                             (Read, Show, Eq, Ord, Enum, Bounded)
@
\eprog
The boolean type @Bool@ is an enumeration. % ; Figure~\ref{prelude-bool}
The basic boolean functions are @&&@ (and), @||@ (or), and @not@.
The name @otherwise@ is defined as @True@ to make guarded expressions
more readable.

\label{prelude-bool}
\indextycon{Bool}
\indextt{False}\indextt{True}
\index{""|""|@@{\tt  {\char'174}{\char'174}}}
\index{&&@@{\tt  \&\&}}
\indextt{not}
\indextt{otherwise}

\subsubsection{Characters and Strings}
\label{characters}
\index{character}\index{string}

The character type @Char@\indextycon{Char}
Simon Peyton Jones's avatar
Simon Peyton Jones committed
53
54
55
is an enumeration whose values represent Unicode characters~\cite{unicode}.
% and consists of 16 bit values, conforming to
% the Unicode standard~\cite{unicode}.
Simon Peyton Jones's avatar
Simon Peyton Jones committed
56
57
58
59
60
61
62
% of which the first 128
% are the ASCII\index{ASCII character set} character set.  
The lexical syntax for
characters is defined in Section~\ref{lexemes-char}; character
literals are nullary constructors in the datatype @Char@.  Type @Char@
is an instance of the classes @Read@, @Show@, @Eq@, @Ord@, 
@Enum@, and @Bounded@.  The @toEnum@ and @fromEnum@ functions,
Simon Peyton Jones's avatar
Simon Peyton Jones committed
63
64
65
standard functions from class @Enum@, map characters to and from the
@Int@ type.
% @Int@ values in the range "[ 0 , 2^{16}-1 ]".
Simon Peyton Jones's avatar
Simon Peyton Jones committed
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100

Note that ASCII control characters each have several representations
in character literals: numeric escapes, ASCII mnemonic escapes,
and the @\^@"X" notation.
In addition, there are the following equivalences:
@\a@ and @\BEL@, @\b@ and @\BS@, @\f@ and @\FF@, @\r@ and @\CR@,
@\t@ and @\HT@, @\v@ and @\VT@, and @\n@ and @\LF@.

A {\em string} is a list of characters:\nopagebreak[4]
\bprog
@
type  String  =  [Char]
@
\eprog
\indexsynonym{String}
Strings may be abbreviated using the lexical syntax described in
Section~\ref{lexemes-char}.  For example, @"A string"@ abbreviates
\[
@[ 'A',' ','s','t','r', 'i','n','g']@
\]

\subsubsection{Lists}
\label{basic-lists}
\index{list}
\bprog
@
data  [a]  =  [] | a : [a]  deriving (Eq, Ord)
@
\eprog
Lists are an algebraic datatype of two constructors, although
with special syntax, as described in Section~\ref{lists}.
The first constructor is the null list, written `@[]@' (``nil''),
\index{[]@@{\tt  []} (nil)}%
and the second is `@:@' (``cons'').
\indextt{:}
Simon Peyton Jones's avatar
Simon Peyton Jones committed
101
The module @PreludeList@ (see Section~\ref{preludelist})
Simon Peyton Jones's avatar
Simon Peyton Jones committed
102
103
104
105
106
107
108
109
110
defines many standard list functions.  
Arithmetic sequences
\index{arithmetic sequence}
and list comprehensions,
\index{list comprehension}
two convenient
syntaxes for special kinds of lists, are described in
Sections~\ref{arithmetic-sequences} and \ref{list-comprehensions},
respectively.  Lists are an instance of classes @Read@, @Show@, @Eq@, @Ord@, 
111
@Monad@, @Functor@, and @MonadPlus@.
Simon Peyton Jones's avatar
Simon Peyton Jones committed
112
113
114
115
116
117
118

\subsubsection{Tuples}
\label{basic-tuples}
\index{tuple}

Tuples are algebraic datatypes with special syntax, as defined
in Section~\ref{tuples}.  Each tuple type has a single constructor.
Simon Peyton Jones's avatar
Simon Peyton Jones committed
119
All tuples are instances of @Eq@, @Ord@, @Bounded@, @Read@,
Simon Peyton Jones's avatar
Simon Peyton Jones committed
120
and @Show@ (provided, of course, that all their component types are).
Simon Peyton Jones's avatar
Simon Peyton Jones committed
121

Simon Peyton Jones's avatar
Simon Peyton Jones committed
122
123
124
There is no upper bound on the size of a tuple, but some \Haskell{}
implementations may restrict the size of tuples, and limit the
instances associated with larger tuples.  However, every Haskell
Simon Peyton Jones's avatar
Simon    
Simon Peyton Jones committed
125
implementation must support tuples up to size 15, together with the instances
Simon Peyton Jones's avatar
Simon Peyton Jones committed
126
for @Eq@, @Ord@, @Bounded@, @Read@, and @Show@.  The Prelude and
Simon Peyton Jones's avatar
Simon Peyton Jones committed
127
128
129
libraries define tuple functions such as @zip@ for tuples up to a size
of 7.

Simon Peyton Jones's avatar
Simon Peyton Jones committed
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
The constructor for a tuple is written by omitting the expressions
surrounding the commas; thus @(x,y)@ and @(,) x y@ produce the same
value. The same holds for tuple type constructors; thus, @(Int,Bool,Int)@
and @(,,) Int Bool Int@ denote the same type.
\indextt{(,)}
\indextt{(,,)}

The following functions are defined for pairs (2-tuples):
@fst@, @snd@, @curry@, and @uncurry@.  Similar functions are not
predefined for larger tuples.
\indextt{fst}
\indextt{snd}
\indextt{curry}
\indextt{uncurry}
\indextt{zip}

\subsubsection{The Unit Datatype}
\label{basic-trivial}
\bprog
@
data  () = () deriving (Eq, Ord, Bounded, Enum, Read, Show)
@
\eprog
The unit datatype @()@ has one non-"\bot"
member, the nullary constructor @()@.  See also Section~\ref{unit-expression}.
\index{trivial type}

\subsubsection{Function Types}
\index{function}
Functions are an abstract type: no constructors directly create
160
functional values.  The following simple functions are found in the Prelude:
Simon Peyton Jones's avatar
Simon Peyton Jones committed
161
162
163
164
165
166
167
168
169
170
@id@, @const@, @(.)@, @flip@, @($)@, and @until@.
\indextt{id}
\indextt{const}
\indextt{.}
\indextt{flip}
\indextt{until}

\subsubsection{The IO and IOError Types}
The @IO@ type serves as a tag for operations (actions) that interact
with the outside world.  The @IO@ type is abstract: no constructors are
171
visible to the user.  @IO@ is an instance of the @Monad@ and @Functor@
Simon Peyton Jones's avatar
Simon Peyton Jones committed
172
classes.  Chapter~\ref{io} describes I/O operations.
Simon Peyton Jones's avatar
Simon Peyton Jones committed
173
174
175
176
177

@IOError@ is an abstract type representing errors raised by I/O
operations.  It is an instance of @Show@ and @Eq@.  Values of this type
are constructed by the various I/O functions and are not presented in
any further detail in this report.  The Prelude contains a few
Simon Peyton Jones's avatar
Simon Peyton Jones committed
178
179
I/O functions (defined in Section~\ref{preludeio}), and Part~\ref{libraries}
contains many more.
Simon Peyton Jones's avatar
Simon Peyton Jones committed
180
181
182
183
\indextycon{IO}
\indextycon{IOError}

\subsubsection{Other Types}
184
{\small
Simon Peyton Jones's avatar
Simon Peyton Jones committed
185
186
\bprog
@
187
188
data  Maybe a     =  Nothing | Just a  deriving (Eq, Ord, Read, Show)
data  Either a b  =  Left a | Right b  deriving (Eq, Ord, Read, Show)
Simon Peyton Jones's avatar
Simon Peyton Jones committed
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
data  Ordering    =  LT | EQ | GT deriving
                                  (Eq, Ord, Bounded, Enum, Read, Show)
@
\indextt{Just}
\indextt{Nothing}
\indextycon{Maybe}
\indextt{Left}
\indextt{Right}
\indextycon{Either}
\indextt{LT}
\indextt{EQ}
\indextt{GT}
\indextycon{Ordering}
\indextt{maybe}
\indextt{either}
\eprog
205
}
Simon Peyton Jones's avatar
Simon Peyton Jones committed
206
207
208
209
210
211
212
The @Maybe@ type is an instance of classes @Functor@, @Monad@,
and @MonadPlus@.  The @Ordering@ type is used by @compare@
in the class @Ord@. The functions @maybe@ and @either@ are found in
the Prelude.

\subsection{Strict Evaluation}
\label{strict-eval}
213
\index{$""!@@{\tt  {\char'044}{\char'041}}}
Simon Peyton Jones's avatar
Simon Peyton Jones committed
214
\index{$@@{\tt  {\char'044}}}
Simon Peyton Jones's avatar
Simon Peyton Jones committed
215
216
217
218
219
220
221
222
223
224
225
226
227
\indextt{seq}
Function application in Haskell is non-strict; that is, a function
argument is evaluated only when required.  Sometimes it is desirable to
force the evaluation of a value, using the @seq@ function:
\bprog
@
  seq :: a -> b -> b
@
\eprog
The function @seq@ is defined by the equations:
\[\ba{l}
"@seq@ \bot b = \bot" \\
"@seq@  a b =  b,  if a \ne \bot" \\
Simon Marlow's avatar
Simon Marlow committed
228
\ea\]\ifhtml{\\}{}
Simon Peyton Jones's avatar
Simon Peyton Jones committed
229
230
231
232
@seq@ is usually introduced to improve performance by
avoiding unneeded laziness.  Strict datatypes (see
\index{strictness flags}
Section~\ref{strictness-flags}) are defined in terms of the @$!@
233
234
operator. 
However, the provision of @seq@ has important semantic consequences, because it is available
Simon Peyton Jones's avatar
Simon Peyton Jones committed
235
236
237
238
239
240
{\em at every type}.
As a consequence, "\bot" is
not the same as @\x -> @ "\bot", since @seq@ can be used to distinguish them.
For the same reason, the existence of @seq@ weakens Haskell's parametricity properties.

The operator @$!@ is strict (call-by-value) application, and is defined
241
242
in terms of @seq@.  The Prelude also defines the @$@ operator to perform 
non-strict application.
Simon Peyton Jones's avatar
Simon Peyton Jones committed
243
244
245
246
\bprog
@
  infixr 0 $, $!
  ($), ($!) :: (a -> b) -> a -> b
Simon Peyton Jones's avatar
Simon Peyton Jones committed
247
248
  f $  x   =          f x
  f $! x   =  x `seq` f x
Simon Peyton Jones's avatar
Simon Peyton Jones committed
249
250
@
\eprog
251
The non-strict application operator @$@ may appear redundant, since 
Simon Peyton Jones's avatar
Simon Peyton Jones committed
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
ordinary application @(f x)@ means the same as @(f $ x)@.
However, @$@ has low, right-associative binding precedence,
so it sometimes allows parentheses to be omitted; for example:
\bprog
@
  f $ g $ h x  =  f (g (h x))
@
\eprog
It is also useful in higher-order situations, such as @map ($ 0) xs@,
or @zipWith ($) fs xs@.

\subsection{Standard Haskell Classes}
Figure~\ref{standard-classes} shows the hierarchy of 
\Haskell{} classes defined in the Prelude and the Prelude types that
are instances of these classes.
\begin{figure}%
%*ignore
% \epsfbox{class-fig.eps}
270
\includegraphics[scale=0.7]{classes}
Simon Peyton Jones's avatar
Simon Peyton Jones committed
271
272
273
274
275
276
277
278
279
% \struthack{11pt}
%*endignore
%**<div align=center><img src="classes.gif" alt="Diagram of standard Haskell classes"> 
%**<h4>Figure 5</h4> </div>
\ecaption{Standard Haskell Classes}
\label{standard-classes}
\end{figure}

Default class method declarations (Section~\ref{classes}) are provided
Simon Peyton Jones's avatar
Simon Peyton Jones committed
280
for many of the methods in standard classes.  A comment with each
Simon Peyton Jones's avatar
Simon Peyton Jones committed
281
@class@ declaration in Chapter~\ref{stdprelude} specifies the
Simon Peyton Jones's avatar
Simon Peyton Jones committed
282
283
284
285
smallest collection of method definitions that, together with the
default declarations, provide a reasonable definition for all the
class methods.  If there is no such comment, then all class methods
must be given to fully specify an instance.
Simon Peyton Jones's avatar
Simon Peyton Jones committed
286
287
288
289
290
291
292
293
294
295
296

\subsubsection{The Eq Class}
\indexclass{Eq}
\indextt{==}
\indextt{/=}
\bprog
@
  class  Eq a  where
        (==), (/=)  ::  a -> a -> Bool

        x /= y  = not (x == y)
297
        x == y  = not (x /= y)
Simon Peyton Jones's avatar
Simon Peyton Jones committed
298
299
300
301
302
303
304
@
\eprog
The @Eq@ class provides equality (@==@) and inequality (@/=@) methods.
All basic datatypes except for functions and @IO@ are instances of this class.
Instances of @Eq@ can be derived for any user-defined datatype whose
constituents are also instances of @Eq@.

Simon Peyton Jones's avatar
Simon Peyton Jones committed
305
306
307
308
309
310
This declaration gives default method declarations for both @/=@ and @==@,
each being defined in terms of the other.  If an instance declaration
for @Eq@ defines neither @==@ nor @/=@, then both will loop.
If one is defined, the default method for the other will make use of
the one that is defined.  If both are defined, neither default method is used.

Simon Peyton Jones's avatar
Simon Peyton Jones committed
311
312
313
314
315
316
317
318
319
320
321
322
323
324
\subsubsection{The Ord Class}
\indexclass{Ord}
\indextt{<}
\indextt{<=}
\indextt{>}
\indextt{>=}
\indextt{compare}
\indextt{max}
\indextt{min}
\bprog
@
  class  (Eq a) => Ord a  where
    compare              :: a -> a -> Ordering
    (<), (<=), (>=), (>) :: a -> a -> Bool
325
    max, min             :: a -> a -> a
Simon Peyton Jones's avatar
Simon Peyton Jones committed
326

Simon Peyton Jones's avatar
Simon Peyton Jones committed
327
    compare x y | x == y    = EQ
328
329
                | x <= y    = LT
                | otherwise = GT
Simon Peyton Jones's avatar
Simon Peyton Jones committed
330

Simon Peyton Jones's avatar
Simon Peyton Jones committed
331
    x <= y  = compare x y /= GT
332
    x <  y  = compare x y == LT
Simon Peyton Jones's avatar
Simon Peyton Jones committed
333
    x >= y  = compare x y /= LT
334
    x >  y  = compare x y == GT
Simon Peyton Jones's avatar
Simon Peyton Jones committed
335

Simon Peyton Jones's avatar
Simon Peyton Jones committed
336
337
338
339
340
    -- Note that (min x y, max x y) = (x,y) or (y,x)
    max x y | x <= y    =  y
            | otherwise =  x
    min x y | x <= y    =  x
            | otherwise =  y
Simon Peyton Jones's avatar
Simon Peyton Jones committed
341
342
343
344
345
346
347
348
349
350
351
352
353
354
@
\eprog
The @Ord@ class is used for totally ordered datatypes.  All basic
datatypes
except for functions, @IO@, and @IOError@, are instances of this class.  Instances
of @Ord@ 
can be derived for any user-defined datatype whose constituent types
are in @Ord@.  The declared order
of the constructors in the data declaration determines the ordering in
derived @Ord@ instances.
The @Ordering@ datatype
allows a single comparison to determine the precise ordering of two
objects.

Simon Peyton Jones's avatar
Simon Peyton Jones committed
355
356
357
The default declarations allow a user to create an @Ord@ instance 
either with a type-specific @compare@ function or with type-specific
@==@ and @<=@ functions.
Simon Peyton Jones's avatar
Simon Peyton Jones committed
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384

\subsubsection{The Read and Show Classes}
\indexsynonym{ReadS}
\indexsynonym{ShowS}
\indexclass{Read}
\indexclass{Show}
\indextt{show}
\indextt{readsPrec}
\indextt{showsPrec}
\indextt{readList}
\indextt{showList}
\bprog
@
type  ReadS a = String -> [(a,String)]
type  ShowS   = String -> String

class  Read a  where
    readsPrec :: Int -> ReadS a
    readList  :: ReadS [a]
    -- ... default decl for readList given in Prelude

class  Show a  where
    showsPrec :: Int -> a -> ShowS
    show      :: a -> String 
    showList  :: [a] -> ShowS

    showsPrec _ x s   = show x ++ s
385
    show x            = showsPrec 0 x ""
Simon Peyton Jones's avatar
Simon Peyton Jones committed
386
387
388
389
    -- ... default decl for showList given in Prelude
@
\eprog
The @Read@ and @Show@ classes are used to convert values to
390
391
or from strings. 
The @Int@ argument to @showsPrec@ and @readsPrec@ gives the operator
Simon Peyton Jones's avatar
Simon Peyton Jones committed
392
precedence of the enclosing context (see Section~\ref{derived-text}).
393
394

@showsPrec@ and @showList@ return a @String@-to-@String@
Simon Peyton Jones's avatar
Simon Peyton Jones committed
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
function, to allow constant-time concatenation of its results using function
composition.
A specialised variant, @show@, is also provided, which
uses precedence context zero, and returns an ordinary @String@.
The method @showList@ is provided to allow the programmer to
give a specialised way of showing lists of values.  This is particularly
useful for the @Char@ type, where values of type @String@ should be
shown in double quotes, rather than between square brackets.

Derived instances of @Read@ and @Show@ replicate the style in which a
constructor is declared: infix constructors and field names are used
on input and output.  Strings produced by @showsPrec@ are usually
readable by @readsPrec@.  

All @Prelude@ types, except function types and @IO@ types,
are instances of @Show@ and @Read@.
(If desired, a programmer can easily make functions and @IO@ types 
into (vacuous) instances of @Show@, by providing an instance declaration.)

\indextt{reads}
\indextt{shows}
\indextt{read}
For convenience, the Prelude provides the following auxiliary
functions: 
\bprog
@
421
422
reads   :: (Read a) => ReadS a
reads   =  readsPrec 0
Simon Peyton Jones's avatar
Simon Peyton Jones committed
423

424
425
shows   :: (Show a) => a -> ShowS
shows   =  showsPrec 0
Simon Peyton Jones's avatar
Simon Peyton Jones committed
426

427
428
429
430
431
read    :: (Read a) => String -> a
read s  =  case [x | (x,t) <- reads s, ("","") <- lex t] of
              [x] -> x
              []  -> error "PreludeText.read: no parse"
              _   -> error "PreludeText.read: ambiguous parse"
Simon Peyton Jones's avatar
Simon Peyton Jones committed
432
433
434
435
436
@
\eprog
\indextt{shows}\indextt{reads}\indextt{show}\indextt{read}
@shows@ and @reads@ use a default precedence of 0.  The @read@ function reads
input from a string, which must be completely consumed by the input
437
438
439
440
441
442
443
444
445
446
process.  

\indextt{lex}
The function @lex :: ReadS String@, used by @read@, is also part of the Prelude.
It reads a single lexeme from the input, discarding initial white space, and
returning the characters that constitute the lexeme.  If the input string contains
only white space, @lex@ returns a single successful ``lexeme'' consisting of the
empty string.  (Thus @lex ""@ = @[("","")]@.)  If there is no legal lexeme at the
beginning of the input string, @lex@ fails (i.e. returns @[]@).

Simon Peyton Jones's avatar
Simon Peyton Jones committed
447
448

\subsubsection{The Enum Class}
Simon Peyton Jones's avatar
Simon Peyton Jones committed
449
\label{enum-class}
Simon Peyton Jones's avatar
Simon Peyton Jones committed
450
451
452
453
454
455
456
457
458
459
\indexclass{Enum}
\indextt{toEnum}
\indextt{fromEnum}
\indextt{enumFrom}
\indextt{enumFromThen}
\indextt{enumFromTo}
\indextt{enumFromThenTo}
\bprog
@
class  Enum a  where
460
461
462
463
464
465
466
    succ, pred     :: a -> a
    toEnum         :: Int -> a
    fromEnum       :: a -> Int
    enumFrom       :: a -> [a]            -- [n..]
    enumFromThen   :: a -> a -> [a]       -- [n,n'..]
    enumFromTo     :: a -> a -> [a]       -- [n..m]
    enumFromThenTo :: a -> a -> a -> [a]  -- [n,n'..m]
Simon Peyton Jones's avatar
Simon Peyton Jones committed
467
468
469
470
471
472
473

    -- Default declarations given in Prelude
@
\eprog
Class @Enum@ defines operations on sequentially ordered types.
The functions @succ@ and @pred@ return the successor and predecessor,
respectively, of a value.
Simon Peyton Jones's avatar
Simon Peyton Jones committed
474
475
The functions @fromEnum@ and @toEnum@ map values from a type in
@Enum@ to and from @Int@.  
Simon Peyton Jones's avatar
Simon Peyton Jones committed
476
The @enumFrom@... methods are used when translating arithmetic
Simon Peyton Jones's avatar
Simon Peyton Jones committed
477
478
479
sequences (Section~\ref{arithmetic-sequences}).

Instances of @Enum@ may be derived for any enumeration type (types
Simon Peyton Jones's avatar
Simon Peyton Jones committed
480
whose constructors have no fields); see Chapter~\ref{derived-appendix}.
Simon Peyton Jones's avatar
Simon Peyton Jones committed
481

Simon Peyton Jones's avatar
Simon Peyton Jones committed
482
For any type that is an instance of class @Bounded@ as well as @Enum@, the following
Simon Peyton Jones's avatar
Simon Peyton Jones committed
483
484
485
486
487
488
489
490
should hold:
\begin{itemize}
\item The calls @succ maxBound@ and @pred minBound@ should result in
a runtime error.

\item @fromEnum@ and @toEnum@ should give a runtime error if the 
result value is not representable in the result type.  For example,
@toEnum 7 :: Bool@ is an error.
Simon Peyton Jones's avatar
Simon Peyton Jones committed
491

Simon Peyton Jones's avatar
Simon Peyton Jones committed
492
493
494
495
496
497
498
\item @enumFrom@ and @enumFromThen@ should be defined with 
an implicit bound, thus:
\bprog
@
  enumFrom     x   = enumFromTo     x maxBound
  enumFromThen x y = enumFromThenTo x y bound
    where
Simon Peyton Jones's avatar
Simon Peyton Jones committed
499
500
      bound | fromEnum y >= fromEnum x = maxBound
            | otherwise                = minBound
Simon Peyton Jones's avatar
Simon Peyton Jones committed
501
502
503
504
505
506
507
@
\eprog
\end{itemize}

The following @Prelude@ types are instances of @Enum@: 
\begin{itemize}
\item Enumeration types: @()@, @Bool@, and @Ordering@. The
Simon Peyton Jones's avatar
Simon Peyton Jones committed
508
semantics of these instances is given by Chapter~\ref{derived-appendix}.
Simon Peyton Jones's avatar
Simon Peyton Jones committed
509
510
For example, @[LT..]@ is the list @[LT,EQ,GT]@.

Simon Peyton Jones's avatar
Simon Peyton Jones committed
511
\item @Char@: the instance is given in Chapter~\ref{stdprelude}, based
Simon Peyton Jones's avatar
Simon Peyton Jones committed
512
513
514
515
516
517
518
on the primitive functions that convert between a @Char@ and an @Int@.
For example, @enumFromTo 'a' 'z'@ denotes
the list of lowercase letters in alphabetical order.

\item Numeric types: @Int@, @Integer@, @Float@, @Double@.  The semantics
of these instances is given next.
\end{itemize}
Simon Peyton Jones's avatar
Simon Peyton Jones committed
519
For all four numeric types, @succ@ adds 1, and @pred@ subtracts 1.
Simon Peyton Jones's avatar
Simon Peyton Jones committed
520
The conversions @fromEnum@ and @toEnum@ convert between the type and @Int@.
Simon Peyton Jones's avatar
Simon Peyton Jones committed
521
In the case of @Float@ and @Double@, the digits after the decimal point may be lost.
Simon Peyton Jones's avatar
Simon Peyton Jones committed
522
523
524
525
526
527
528
529
It is implementation-dependent what @fromEnum@ returns when applied to 
a value that is too large to fit in an @Int@.

For the types @Int@ and @Integer@, the enumeration functions 
have the following meaning:
\begin{itemize}
\item The sequence "@enumFrom@~e_1" is the list "@[@e_1@,@e_1+1@,@e_1+2@,@...@]@".

Simon Peyton Jones's avatar
Simon Peyton Jones committed
530
\item The sequence "@enumFromThen@~e_1~e_2" is the list "@[@e_1@,@e_1+i@,@e_1+2i@,@...@]@",
Simon Peyton Jones's avatar
Simon Peyton Jones committed
531
532
533
534
535
536
537
where the increment, "i", is "e_2-e_1".  The increment may be zero or negative.
If the increment is zero, all the list elements are the same.

\item The sequence "@enumFromTo@~e_1~e_3" is 
the list "@[@e_1@,@e_1+1@,@e_1+2@,@...e_3@]@".
The list is empty if "e_1 > e_3".

Simon Peyton Jones's avatar
Simon Peyton Jones committed
538
\item The sequence "@enumFromThenTo@~e_1~e_2~e_3" 
Simon Peyton Jones's avatar
Simon Peyton Jones committed
539
540
541
542
543
544
545
546
547
548
549
550
551
552
is the list "@[@e_1@,@e_1+i@,@e_1+2i@,@...e_3@]@",
where the increment, "i", is "e_2-e_1".  If the increment 
is positive or zero, the list terminates when the next element would
be greater than "e_3"; the list is empty if "e_1 > e_3".
If the increment is negative, the list terminates when the next element would
be less than "e_3"; the list is empty if "e1 < e_3".
\end{itemize}
For @Float@ and @Double@, the semantics of the @enumFrom@ family is
given by the rules for @Int@ above, except that the list terminates
when the elements become greater than "e_3+i/2" for positive increment
"i", or when they become less than "e_3+i/2" for negative "i".

For all four of these Prelude numeric types, all of the @enumFrom@ 
family of functions are strict in all their arguments.
Simon Peyton Jones's avatar
Simon Peyton Jones committed
553

554
\subsubsection{The Functor Class}
Simon Peyton Jones's avatar
Simon Peyton Jones committed
555
556
557
558
559
560
561
\indexclass{Functor}
\indextt{fmap}
\index{functor}

\bprog
@
class  Functor f  where
562
    fmap    :: (a -> b) -> f a -> f b
Simon Peyton Jones's avatar
Simon Peyton Jones committed
563
564
565
566
567
568
569
570
571
572
@
\eprog
The @Functor@
class is used for types that can be mapped over.  Lists, @IO@, and
@Maybe@ are in this class. 

Instances of @Functor@ should satisfy the following laws:
\[\ba{lcl}
@fmap id@&=&@id@\\
@fmap (f . g)@&=&@fmap f . fmap g@\\
Simon Marlow's avatar
Simon Marlow committed
573
\ea\]\ifhtml{\\}{}
574
All instances of @Functor@ defined in the Prelude satisfy these laws.
Simon Peyton Jones's avatar
Simon Peyton Jones committed
575
576


577
\subsubsection{The Monad Class}
Simon Peyton Jones's avatar
Simon Peyton Jones committed
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
\label{monad-class}
\indexclass{Monad}
\indextt{return}
\indextt{fail}
\indextt{>>}
\indextt{>>=}
\index{monad}
\bprog
@
class  Monad m  where
    (>>=)   :: m a -> (a -> m b) -> m b
    (>>)    :: m a -> m b -> m b
    return  :: a -> m a
    fail    :: String -> m a

    m >> k  =  m >>= \_ -> k
    fail s  = error s
@
\eprog
The @Monad@ class defines the basic operations over a {\em monad}.
Simon Peyton Jones's avatar
Simon Peyton Jones committed
598
See Chapter~\ref{io} for more information about monads.
Simon Peyton Jones's avatar
Simon Peyton Jones committed
599
600
601
602
603
604
605
606
607

``@do@'' expressions provide a convenient syntax for writing
monadic expressions (see Section~\ref{do-expressions}).
The @fail@ method is invoked on pattern-match failure in a @do@
expression.

In the Prelude, lists, 
@Maybe@, and @IO@ are all instances of @Monad@.
The @fail@ method for lists returns the empty list @[]@,
608
609
for @Maybe@ returns @Nothing@, and for @IO@ raises a user
exception in the IO monad (see Section~\ref{io-exceptions}).
Simon Peyton Jones's avatar
Simon Peyton Jones committed
610
611
612
613
614
615

Instances of @Monad@ should satisfy the following laws:
\[\ba{lcl}
@return a >>= k@&=&@k a@ \\
@m >>= return@&=&@m@ \\
@m >>= (\x -> k x >>= h)@&=&@(m >>= k) >>= h@\\
Simon Marlow's avatar
Simon Marlow committed
616
\ea\]\ifhtml{\\}{}
617
618
Instances of both @Monad@ and @Functor@ should additionally satisfy the law:
\[\ba{lcl}
Simon Peyton Jones's avatar
Simon Peyton Jones committed
619
@fmap f xs@&=&@xs >>= return . f@\\
Simon Marlow's avatar
Simon Marlow committed
620
\ea\]\ifhtml{\\}{}
621
All instances of @Monad@ defined in the Prelude satisfy these laws.
Simon Peyton Jones's avatar
Simon Peyton Jones committed
622

623
The Prelude provides the following auxiliary functions: 
Simon Peyton Jones's avatar
Simon Peyton Jones committed
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
\bprog
@
sequence  :: Monad m => [m a] -> m [a] 
sequence_ :: Monad m => [m a] -> m () 
mapM      :: Monad m => (a -> m b) -> [a] -> m [b]
mapM_     :: Monad m => (a -> m b) -> [a] -> m ()
(=<<)     :: Monad m => (a -> m b) -> m a -> m b
@
\eprog
\indextt{sequence}
\index{sequence{\char '137}@@{\tt  sequence{\char '137}}}
\index{mapM{\char '137}@@{\tt  mapM{\char '137}}}
\indextt{mapM}
\indextt{=<<}

\subsubsection{The Bounded Class}
\indexclass{Bounded}
\indextt{minBound}
\indextt{maxBound}
@
class  Bounded a  where
    minBound, maxBound :: a
@

The @Bounded@ class is used to name the upper and lower limits of a
type.  @Ord@ is not a superclass of @Bounded@ since types that are not
totally ordered may also have upper and lower bounds.
The types @Int@, @Char@, @Bool@,
@()@, @Ordering@, and all tuples are instances of @Bounded@.  
The @Bounded@ class may be derived
for any enumeration type; @minBound@ is the first constructor listed
in the @data@ declaration and @maxBound@ is the last.  @Bounded@ may
also be derived for single-constructor datatypes whose constituent
types are in @Bounded@.

\subsection{Numbers}
\label{numbers}
\index{number}

\Haskell{} provides several kinds of numbers; the numeric
types and the operations upon them have been heavily influenced by
%Common Lisp \cite{steele:common-lisp} and Scheme \cite{RRRRS}.
Common Lisp and Scheme.
Numeric function names and operators are usually overloaded, using
several type classes with an inclusion relation shown in
669
Figure~\ref{standard-classes}.
Simon Peyton Jones's avatar
Simon Peyton Jones committed
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
The class @Num@\indexclass{Num} of numeric
types is a subclass of @Eq@\indexclass{Eq}, since all numbers may be
compared for equality; its subclass @Real@\indexclass{Real} is also a
subclass of @Ord@\indexclass{Ord}, since the other comparison operations
apply to all but complex numbers (defined in the @Complex@ library).
The class @Integral@\indexclass{Integral} contains integers of both 
limited and unlimited range; the class
@Fractional@\indexclass{Fractional} contains all non-integral types; and
the class @Floating@\indexclass{Floating} contains all floating-point
types, both real and complex.

The Prelude defines only the most basic numeric types: fixed sized
integers (@Int@), arbitrary precision integers (@Integer@), single
precision floating (@Float@), and double precision floating
(@Double@).  Other numeric types such as rationals and complex numbers
are defined in libraries.  In particular, the type @Rational@ is a
Simon Peyton Jones's avatar
Simon Peyton Jones committed
686
ratio of two @Integer@ values, as defined in the @Ratio@
Simon Peyton Jones's avatar
Simon Peyton Jones committed
687
688
689
690
691
692
693
library.  

The default floating point operations defined by the \Haskell{}
Prelude do not 
conform to current language independent arithmetic (LIA) standards.  These
standards require considerably more complexity in the numeric
structure and have thus been relegated to a library.  Some, but not
Simon Peyton Jones's avatar
Simon Peyton Jones committed
694
all, aspects of the IEEE floating point standard have been
Simon Peyton Jones's avatar
Simon Peyton Jones committed
695
accounted for in Prelude class @RealFloat@.
Simon Peyton Jones's avatar
Simon Peyton Jones committed
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742

The standard numeric types are listed in Table~\ref{standard-numeric-types}.
The finite-precision integer type @Int@\indextycon{Int} covers at
least the range 
"[ - 2^{29}, 2^{29} - 1]\/".  As @Int@ is an instance of the @Bounded@
class, @maxBound@ and @minBound@ can be used to determine the exact
@Int@ range defined by an implementation.
%(Figure~\ref{basic-numeric-2}, page~\pageref{basic-numeric-2}) define the limits of
%@Int@\indextycon{Int} in each implementation.
@Float@\indextycon{Float} is implementation-defined; it is desirable that
this type be at least equal in range and precision to the IEEE
single-precision type.  Similarly, @Double@\indextycon{Double} should
cover IEEE double-precision.  The results of exceptional
conditions (such as overflow or underflow) on the fixed-precision
numeric types are undefined; an implementation may choose error
("\bot", semantically), a truncated value, or a special value such as
infinity, indefinite, etc.

\begin{table}[tb]
\[
\bto{|l|l|l|}
\hline
\multicolumn{1}{|c|}{Type} & 
	\multicolumn{1}{c|}{Class} &
	\multicolumn{1}{c|}{Description} \\ \hline
@Integer@ & @Integral@ & Arbitrary-precision integers \\
@Int@ & @Integral@ & Fixed-precision integers \\
@(Integral a) => Ratio a@ & @RealFrac@ & Rational numbers \\
@Float@ & @RealFloat@ & Real floating-point, single precision \\
@Double@ & @RealFloat@ & Real floating-point, double precision \\
@(RealFloat a) => Complex a@ & @Floating@ & Complex floating-point \\
\hline
\eto
\]
%**<div align=center> <h4>Table 2</h4> </div>
\ecaption{Standard Numeric Types}
\label{standard-numeric-types}
\index{numeric type}
\end{table}

The standard numeric classes and other numeric functions defined in
the Prelude are shown
in Figures~\ref{basic-numeric-1}--\ref{basic-numeric-2}.
Figure~\ref{standard-classes} shows the class dependencies and
built-in types that are instances of the numeric classes.

\begin{figure}[tb]
743
\begin{outlineenv}
Simon Peyton Jones's avatar
Simon Peyton Jones committed
744
745
@
class  (Eq a, Show a) => Num a  where
746
747
748
749
    (+), (-), (*)  :: a -> a -> a
    negate         :: a -> a
    abs, signum    :: a -> a
    fromInteger    :: Integer -> a
Simon Peyton Jones's avatar
Simon Peyton Jones committed
750
751

class  (Num a, Ord a) => Real a  where
752
    toRational ::  a -> Rational
Simon Peyton Jones's avatar
Simon Peyton Jones committed
753
754

class  (Real a, Enum a) => Integral a  where
755
756
757
    quot, rem, div, mod :: a -> a -> a
    quotRem, divMod     :: a -> a -> (a,a)
    toInteger           :: a -> Integer
Simon Peyton Jones's avatar
Simon Peyton Jones committed
758
759

class  (Num a) => Fractional a  where
760
761
762
    (/)          :: a -> a -> a
    recip        :: a -> a
    fromRational :: Rational -> a
Simon Peyton Jones's avatar
Simon Peyton Jones committed
763
764

class  (Fractional a) => Floating a  where
765
766
767
768
769
770
    pi                  :: a
    exp, log, sqrt      :: a -> a
    (**), logBase       :: a -> a -> a
    sin, cos, tan       :: a -> a
    asin, acos, atan    :: a -> a
    sinh, cosh, tanh    :: a -> a
Simon Peyton Jones's avatar
Simon Peyton Jones committed
771
772
    asinh, acosh, atanh :: a -> a
@
773
\end{outlineenv}
Simon Peyton Jones's avatar
Simon Peyton Jones committed
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
%**<div align=center> <h4>Figure 6</h4> </div>
\ecaption{Standard Numeric Classes and Related Operations, Part 1}
\label{basic-numeric-1}
\indexclass{Num}
\indextt{+}
\indextt{-}
\indextt{*}
\indextt{negate}\indextt{abs}\indextt{signum}         
\indextt{fromInteger}
\indexclass{Real}\indextt{toRational}
\indexclass{Integral}
\indextt{quotRem}\indextt{divMod}\indextt{mod}\indextt{div}
\indextt{rem}\indextt{quot}
\indextt{even}\indextt{odd}
\indexclass{Fractional}
\indextt{/}
\indextt{recip}\indextt{fromRational}       
\indexclass{Floating}\indextt{pi}\indextt{exp}\indextt{log}\indextt{sqrt} 
\indextt{**}
\indextt{logBase}
\indextt{sin}\indextt{cos}\indextt{tan}                        
\indextt{asin}\indextt{acos}\indextt{atan}               
\indextt{sinh}\indextt{cosh}\indextt{tanh}               
\indextt{asinh}\indextt{acosh}\indextt{atanh}      
\end{figure}

\begin{figure}[tb]
801
\begin{outlineenv}
Simon Peyton Jones's avatar
Simon Peyton Jones committed
802
803
@
class  (Real a, Fractional a) => RealFrac a  where
804
805
806
    properFraction   :: (Integral b) => a -> (b,a)
    truncate, round  :: (Integral b) => a -> b
    ceiling, floor   :: (Integral b) => a -> b
Simon Peyton Jones's avatar
Simon Peyton Jones committed
807
808

class  (RealFrac a, Floating a) => RealFloat a  where
809
810
811
812
813
814
815
816
    floatRadix          :: a -> Integer
    floatDigits         :: a -> Int
    floatRange          :: a -> (Int,Int)
    decodeFloat         :: a -> (Integer,Int)
    encodeFloat         :: Integer -> Int -> a
    exponent            :: a -> Int
    significand         :: a -> a
    scaleFloat          :: Int -> a -> a
Simon Peyton Jones's avatar
Simon Peyton Jones committed
817
818
    isNaN, isInfinite, isDenormalized, isNegativeZero, isIEEE 
                        :: a -> Bool
819
    atan2               :: a -> a -> a
Simon Peyton Jones's avatar
Simon Peyton Jones committed
820

821
822
823
gcd, lcm :: (Integral a) => a -> a-> a
(^)      :: (Num a, Integral b) => a -> b -> a
(^^)     :: (Fractional a, Integral b) => a -> b -> a
Simon Peyton Jones's avatar
Simon Peyton Jones committed
824

825
826
fromIntegral :: (Integral a, Num b) => a -> b
realToFrac   :: (Real a, Fractional b) => a -> b
Simon Peyton Jones's avatar
Simon Peyton Jones committed
827
@
828
\end{outlineenv}
Simon Peyton Jones's avatar
Simon Peyton Jones committed
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
%**<div align=center> <h4>Figure 7</h4> </div>
\ecaption{Standard Numeric Classes and Related Operations, Part 2}
\label{basic-numeric-2}
\indexclass{RealFrac}\indextt{properFraction}\indextt{approxRational}
\indextt{truncate}\indextt{round}
\indextt{ceiling}\indextt{floor}    
\indexclass{RealFloat}
\indextt{floatRadix}\indextt{floatDigits}\indextt{floatRange} 
\indextt{decodeFloat}\indextt{encodeFloat}
\indextt{exponent}\indextt{significand}\indextt{scaleFloat}
\indextycon{Int}\indextycon{Integer}
\indextt{fromIntegral}
\indextt{gcd}\indextt{lcm} 
\index{^@@{\tt  {\char'136}}} % this is ^.  Must have 2 spaces after tt!
\index{^^@@{\tt  {\char'136}{\char'136}}} % this is ^^
\indexclass{RealFrac}
\indextycon{Float}\indextycon{Double}
846
\indextt{realToFrac}
Simon Peyton Jones's avatar
Simon Peyton Jones committed
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
\indextt{atan2}
\end{figure}

\subsubsection{Numeric Literals}
\label{numeric-literals}

The syntax of numeric literals is given in
Section~\ref{lexemes-numeric}.  An integer literal represents the
application
of the function @fromInteger@\indextt{fromInteger} to the appropriate
value of type 
@Integer@.  Similarly, a floating literal stands for an application of
@fromRational@\indextt{fromRational} to a value of type @Rational@ (that is, 
@Ratio Integer@).  Given the typings:
\bprog
@
fromInteger  :: (Num a) => Integer -> a
fromRational :: (Fractional a) => Rational -> a
@
\eprog\indextt{fromInteger}\indextt{fromRational}%
integer and floating literals have the
typings @(Num a) => a@ and @(Fractional a) => a@, respectively.
Numeric literals are defined in this indirect way so that they may be
interpreted as values of any appropriate numeric type.
See Section~\ref{default-decls} for a discussion of overloading ambiguity.

\subsubsection{Arithmetic and Number-Theoretic Operations}
\label{arithmetic-operators}
\index{arithmetic operator}

The infix class methods 
@(+)@,
\indextt{+}
@(*)@,
\indextt{*}
@(-)@,
\indextt{-}
and the unary function
@negate@\indextt{negate} (which can also be written as a prefix minus sign; see
section~\ref{operators}) apply to all numbers.  The class methods
@quot@\indextt{quot}, @rem@\indextt{rem}, @div@\indextt{div}, and
@mod@\indextt{mod} apply only to integral numbers, while the class method
@(/)@
\indextt{/}
applies only to fractional ones.  The @quot@, @rem@,
Simon Peyton Jones's avatar
Simon Peyton Jones committed
892
@div@, and @mod@ class methods satisfy these laws if @y@ is non-zero:
Simon Marlow's avatar
Simon Marlow committed
893
894
895
896
897
898
\bprog
@
(x `quot` y)*y + (x `rem` y) == x
(x `div`  y)*y + (x `mod` y) == x
@
\eprog
Simon Peyton Jones's avatar
Simon Peyton Jones committed
899
900
901
902
903
904
905
906
907
908
@`quot`@ is integer division truncated toward zero,
while the result of @`div`@ is truncated toward
negative infinity. 
%Note that @quot@ should be used rather than
%@div@ to give the semantics of Pascal's @div@.
The @quotRem@ class method takes a dividend and a divisor as arguments
and returns a (quotient, remainder) pair; @divMod@ is defined
similarly:
\bprog
@
Simon Marlow's avatar
Simon Marlow committed
909
910
quotRem x y  =  (x `quot` y, x `rem` y)
divMod  x y  =  (x `div`  y, x `mod` y)
Simon Peyton Jones's avatar
Simon Peyton Jones committed
911
912
913
914
915
@
\eprog
Also available on integral numbers are the even and odd predicates:
\bprog
@
Simon Marlow's avatar
Simon Marlow committed
916
even x =  x `rem` 2 == 0
917
odd    =  not . even
Simon Peyton Jones's avatar
Simon Peyton Jones committed
918
919
920
@
\eprog\indextt{even}\indextt{odd}
Finally, there are the greatest common divisor and least common
Simon Peyton Jones's avatar
Simon Peyton Jones committed
921
922
923
924
925
multiple functions.  @gcd@\indextt{gcd} "x" "y" is the greatest
(positive) integer that divides both "x" and "y"; for example @gcd (-3) 6@ = @3@, @gcd (-3) (-6)@ = @3@, 
@gcd 0 4@ = @4@. @gcd 0 0@ raises a runtime error.

@lcm@\indextt{lcm} "x" "y" is the smallest positive integer that both "x" and "y" divide.
Simon Peyton Jones's avatar
Simon Peyton Jones committed
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067

\subsubsection{Exponentiation and Logarithms}
\index{exponentiation}\index{logarithm}

The one-argument exponential function @exp@\indextt{exp} and the
logarithm function @log@\indextt{log} act on floating-point numbers and
use base "e".  @logBase@\indextt{logBase} "a" "x" returns the
logarithm of "x" in base "a".  @sqrt@\indextt{sqrt} returns the
principal square root of a floating-point number.
There are three two-argument exponentiation operations:
@(^)@\index{^@@{\tt  {\char'136}}} raises any  % 
number to a nonnegative integer power,
@(^^)@\index{^^@@{\tt  {\char'136}{\char'136}}} raises a
fractional number to any integer power, and @(**)@
\indextt{**}
takes two floating-point arguments.  The value of "x"@^0@ or "x"@^^0@
is @1@ for any "x", including zero; @0**@"y" is undefined.
  
\subsubsection{Magnitude and Sign}
\label{magnitude-sign}
\index{magnitude}\index{sign}

A number has a {\em magnitude}
and a {\em sign}.  The functions @abs@\indextt{abs} and
@signum@\indextt{signum} apply to any number and satisfy the law:
\bprog
@
abs x * signum x == x
@
\eprog
For real numbers, these functions are defined by:
\bprog
@
abs x    | x >= 0  = x
         | x <  0  = -x

signum x | x >  0  = 1
         | x == 0  = 0
         | x <  0  = -1
@
\eprog

\subsubsection{Trigonometric Functions}
\index{trigonometric function}

Class @Floating@ provides the 
circular and hyperbolic sine\index{sine}, cosine\index{cosine},
and tangent\index{tangent} functions and their inverses.
Default implementations of @tan@, @tanh@, @logBase@, @**@, and @sqrt@ are
provided, but implementors are free to provide more accurate implementations.

Class @RealFloat@ provides a version of arctangent
taking two real floating-point arguments.
For real floating "x" and "y", @atan2@\indextt{atan2} "y" "x"
computes the angle (from the positive x-axis) of the vector from the origin
to the point "(x,y)".  @atan2@\indextt{atan2} "y" "x"
returns a value in the range @[-pi, pi]@.  It
follows the Common Lisp semantics for the origin when signed zeroes are
supported.  @atan2@ "y" @1@, with "y" in a type that is @RealFloat@, should return the
same value as @atan@ "y".  A default definition of @atan2@ is provided, but
implementors can provide a more accurate implementation. 

The precise definition of the above functions is as in Common Lisp,
which in turn follows Penfield's proposal for
APL~\cite{penfield:complex-apl}.  See these references for discussions
of branch cuts, discontinuities, and implementation.

\subsubsection{Coercions and Component Extraction}
\label{coercion}
\index{coercion}

The @ceiling@\indextt{ceiling}, @floor@\indextt{floor},
@truncate@\indextt{truncate}, and @round@\indextt{round}
functions each take a real fractional argument and return an integral
result.  \mbox{@ceiling@ "x"} returns the least integer not less than "x", and
\mbox{@floor@ "x"}, the greatest integer not greater than "x".  \mbox{@truncate@ "x"}
yields the integer nearest "x" between "0" and "x", inclusive.
\mbox{@round@ "x"} returns the nearest integer to "x", the even integer if
"x" is equidistant between two integers.

% 	Keith Wansbrough clarifies the above defn of @round@,
%	which strangely rounds both 3.5 and 4.5 to 4
%
% This is generally considered the most accurate kind of rounding, since it 
% avoids cumulative errors.  If you get lots of values on the 0.5 boundary, 
% `round up' gives you an error of +0.5 for each, whereas round-to-even gives 
% you a mean error of zero.
% 
% IEEE floating point does this by default for its basic operations.

The function @properFraction@\indextt{properFraction} takes a real
fractional number "x" and returns a pair "(n,f)" such that "x = n+f", and:
"n" is an integral number with the same sign as "x"; and "f" is a
fraction "f" with the same type and sign as "x", and with absolute
value less than 1.
The @ceiling@, @floor@, @truncate@, and @round@
functions can be defined in terms of @properFraction@.

Two functions convert numbers to type @Rational@:
@toRational@\indextt{toRational} returns the rational equivalent of
its real argument with full precision;
@approxRational@\indextt{approxRational} takes two real fractional arguments
"x" and "\epsilon" and returns the simplest rational number within
"\epsilon" of "x", where a rational $ p/q $ in reduced form is
{\em simpler} than another $ p^{\prime} / q^{\prime} $ if 
$ |p| \leq |p^{\prime}| $ and $ q \leq q^{\prime} $.
Every real interval contains a unique simplest rational;
in particular, note that $ 0/1 $ is the simplest rational of all.%
%\cite[Section 6.5.5]{RRRRS}.

The class methods of class @RealFloat@\indexclass{RealFloat} allow
efficient, machine-independent
access to the components of a floating-point number.
The functions @floatRadix@\indextt{floatRadix},
@floatDigits@\indextt{floatDigits}, and
@floatRange@\indextt{floatRange} give the parameters of a
floating-point type:  the radix of the representation, the number of
digits of this radix in the significand, and the lowest and highest
values the exponent may assume, respectively.
The function @decodeFloat@\indextt{decodeFloat}
applied to a real floating-point number returns the significand
expressed as an @Integer@ and an appropriately scaled exponent (an
@Int@).  If \mbox{@decodeFloat x@} yields \mbox{@(@"m"@,@"n"@)@}, then @x@ is
equal in value to "mb^n", where "b" is the floating-point radix, and
furthermore, either "m" and "n" are both zero or else
"b^{d-1}<=m<b^d", where "d" is the value of \mbox{@floatDigits x@}.
@encodeFloat@\indextt{encodeFloat} performs the inverse of this
transformation.  The functions @significand@\indextt{significand}
and @exponent@\indextt{exponent} together provide the same
information as @decodeFloat@,  but rather than an @Integer@,
\mbox{@significand x@} yields a value of the same type as @x@, scaled to lie
in the open interval "(-1,1)".  \mbox{@exponent 0@} is zero.  @scaleFloat@
multiplies a floating-point number by an integer power of the radix.

The functions @isNaN@, @isInfinite@, @isDenormalized@,
@isNegativeZero@, and @isIEEE@ all support numbers represented using
the IEEE standard.  For non-IEEE floating point numbers, these may all
return false.

Also available are the following coercion functions:
\bprog
@
1068
1069
fromIntegral :: (Integral a, Num b)    => a -> b
realToFrac   :: (Real a, Fractional b) => a -> b
Simon Peyton Jones's avatar
Simon Peyton Jones committed
1070
@
1071
\eprogNoSkip\indextt{fromIntegral}\indextt{realToFrac}
Simon Peyton Jones's avatar
Simon Peyton Jones committed
1072
1073
1074
1075
1076
1077
1078


%**~footer

% Local Variables: 
% mode: latex
% End: