1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 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 53 54 55 56 57 58 59 60 61 62 63 64 65 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 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 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 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 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 385 386 387 388 389 390 391 392 393 394 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 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 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 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 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 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 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 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 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 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936
|
<title>The Haskell 98 Report: Basic Types and Classes</title>
<body bgcolor="#ffffff"> <i>The Haskell 98 Report</i><br> <a href="index.html">top</a> | <a href="modules.html">back</a> | <a href="io-13.html">next</a> | <a href="index98.html">contents</a> | <a href="prelude-index.html">function index</a> <br><hr>
<a name="basic-types-and-classes"></a><a name="sect6"></a>
<h2>6<tt> </tt>Predefined Types and Classes</h2>
The Haskell Prelude contains predefined classes, types,
and functions that are implicitly imported into every Haskell
program. In this section, we describe the types and classes found in
the Prelude.
Most functions are not described in detail here as they
can easily be understood from their definitions as given in Appendix
<a href="standard-prelude.html#stdprelude">A</a>.
Other predefined types such as arrays, complex numbers, and rationals
are defined in the Haskell Library Report.<a name="basic-types"></a><p>
<a name="sect6.1"></a>
<h3>6.1<tt> </tt>Standard Haskell Types</h3>
These types are defined by the Haskell Prelude. Numeric types are
described in Section <a href="basic.html#numbers">6.4</a>. 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.<a name="booleans"></a><p>
<a name="sect6.1.1"></a>
<h4>6.1.1<tt> </tt>Booleans</h4>
<tt><br>
<br>
data Bool = False | True deriving <br>
(Read, Show, Eq, Ord, Enum, Bounded)<br>
<br>
</tt>The boolean type <tt>Bool</tt> is an enumeration. The basic boolean functions are <tt>&&</tt> (and), <tt>||</tt> (or), and <tt>not</tt>.
The name <tt>otherwise</tt> is defined as <tt>True</tt> to make guarded expressions
more readable.<a name="prelude-bool"></a><p>
<a name="characters"></a><p>
<a name="sect6.1.2"></a>
<h4>6.1.2<tt> </tt>Characters and Strings</h4>
<p>
The character type <tt>Char
</tt>is an enumeration and consists of 16 bit values, conforming to
the Unicode standard [<a href="haskell.html#$unicode">10</a>].
The lexical syntax for
characters is defined in Section <a href="lexemes.html#lexemes-char">2.6</a>; character
literals are nullary constructors in the datatype <tt>Char</tt>. Type <tt>Char
</tt>is an instance of the classes <tt>Read</tt>, <tt>Show</tt>, <tt>Eq</tt>, <tt>Ord</tt>,
<tt>Enum</tt>, and <tt>Bounded</tt>. The <tt>toEnum</tt> and <tt>fromEnum</tt> functions,
standard functions over bounded enumerations, map characters onto
<tt>Int</tt> values in the range <I>[ 0 , 2</I><sup><I>16</I></sup><I>-1 ]</I>.<p>
Note that ASCII control characters each have several representations
in character literals: numeric escapes, ASCII mnemonic escapes,
and the <tt>\^</tt><I>X</I> notation.
In addition, there are the following equivalences:
<tt>\a</tt> and <tt>\BEL</tt>, <tt>\b</tt> and <tt>\BS</tt>, <tt>\f</tt> and <tt>\FF</tt>, <tt>\r</tt> and <tt>\CR</tt>,
<tt>\t</tt> and <tt>\HT</tt>, <tt>\v</tt> and <tt>\VT</tt>, and <tt>\n</tt> and <tt>\LF</tt>.<p>
A <I>string</I> is a list of characters:
<tt><br>
<br>
type String = [Char]<br>
<br>
</tt>Strings may be abbreviated using the lexical syntax described in
Section <a href="lexemes.html#lexemes-char">2.6</a>. For example, <tt>"A string"</tt> abbreviates
<p>
<tt>[ 'A',' ','s','t','r', 'i','n','g']
<p>
<a name="basic-lists"></a><p>
</tt><a name="sect6.1.3"></a>
<h4>6.1.3<tt> </tt>Lists</h4>
<tt><br>
<br>
data [a] = [] | a : [a] deriving (Eq, Ord)<br>
<br>
</tt>Lists are an algebraic datatype of two constructors, although
with special syntax, as described in Section <a href="exps.html#lists">3.7</a>.
The first constructor is the null list, written `<tt>[]</tt>' ("nil"),
and the second is `<tt>:</tt>' ("cons").
The module <tt>PreludeList</tt> (see Appendix <a href="standard-prelude.html#preludelist">A.1</a>)
defines many standard list functions.
Arithmetic sequences
and list comprehensions,
two convenient
syntaxes for special kinds of lists, are described in
Sections <a href="exps.html#arithmetic-sequences">3.10</a> and <a href="exps.html#list-comprehensions">3.11</a>,
respectively. Lists are an instance of classes <tt>Read</tt>, <tt>Show</tt>, <tt>Eq</tt>, <tt>Ord</tt>,
<tt>Monad</tt>, and <tt>MonadPlus</tt>.<a name="basic-tuples"></a><p>
<a name="sect6.1.4"></a>
<h4>6.1.4<tt> </tt>Tuples</h4>
<p>
Tuples are algebraic datatypes with special syntax, as defined
in Section <a href="exps.html#tuples">3.8</a>. Each tuple type has a single constructor.
There is no upper bound on the size of a tuple. However, some
Haskell implementations may restrict the size of tuples and limit
the instances associated with larger tuples.
The Prelude and libraries define tuple functions such as <tt>zip</tt> for
tuples up to a
size of 7. All tuples are instances of <tt>Eq</tt>, <tt>Ord</tt>, <tt>Bounded</tt>, <tt>Read</tt>,
and <tt>Show</tt>. Classes defined in the libraries may also supply
instances for tuple types.<p>
The constructor for a tuple is written by omitting the expressions
surrounding the commas; thus <tt>(x,y)</tt> and <tt>(,) x y</tt> produce the same
value. The same holds for tuple type constructors; thus, <tt>(Int,Bool,Int)
</tt>and <tt>(,,) Int Bool Int</tt> denote the same type.<p>
The following functions are defined for pairs (2-tuples):
<tt>fst</tt>, <tt>snd</tt>, <tt>curry</tt>, and <tt>uncurry</tt>. Similar functions are not
predefined for larger tuples.<a name="basic-trivial"></a><p>
<a name="sect6.1.5"></a>
<h4>6.1.5<tt> </tt>The Unit Datatype</h4>
<tt><br>
<br>
data () = () deriving (Eq, Ord, Bounded, Enum, Read, Show)<br>
<br>
</tt>The unit datatype <tt>()</tt> has one non-<I>_|_
</I>member, the nullary constructor <tt>()</tt>. See also Section <a href="exps.html#unit-expression">3.9</a>.<p>
<a name="sect6.1.6"></a>
<h4>6.1.6<tt> </tt>Function Types</h4>
Functions are an abstract type: no constructors directly create
functional values. Functions are an instance of the <tt>Show</tt> class but
not <tt>Read</tt>. The following simple functions are found in the Prelude:
<tt>id</tt>, <tt>const</tt>, <tt>(.)</tt>, <tt>flip</tt>, <tt>($)</tt>, and <tt>until</tt>.<p>
<a name="sect6.1.7"></a>
<h4>6.1.7<tt> </tt>The IO and IOError Types</h4>
The <tt>IO</tt> type serves as a tag for operations (actions) that interact
with the outside world. The <tt>IO</tt> type is abstract: no constructors are
visible to the user. <tt>IO</tt> is an instance of the <tt>Monad</tt> and
<tt>Show</tt> classes. Section <a href="io-13.html#io">7</a> describes I/O operations.<p>
<tt>IOError</tt> is an abstract type representing errors raised by I/O
operations. It is an instance of <tt>Show</tt> and <tt>Eq</tt>. 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
I/O functions (defined in Section <a href="standard-prelude.html#preludeio">A.3</a>), and the Library
Report contains many more.<p>
<a name="sect6.1.8"></a>
<h4>6.1.8<tt> </tt>Other Types</h4>
<tt><br>
<br>
data Maybe a = Nothing | Just a deriving (Eq, Ord, Read, Show)<br>
data Either a b = Left a | Right b deriving (Eq, Ord, Read, Show)<br>
data Ordering = LT | EQ | GT deriving<br>
(Eq, Ord, Bounded, Enum, Read, Show)<br>
<br>
</tt>The <tt>Maybe</tt> type is an instance of classes <tt>Functor</tt>, <tt>Monad</tt>,
and <tt>MonadPlus</tt>. The <tt>Ordering</tt> type is used by <tt>compare
</tt>in the class <tt>Ord</tt>. The functions <tt>maybe</tt> and <tt>either</tt> are found in
the Prelude.<a name="strict-eval"></a><p>
<a name="sect6.2"></a>
<h3>6.2<tt> </tt>Strict Evaluation</h3>
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 <tt>seq</tt> function:
<tt><br>
<br>
seq :: a -> b -> b<br>
<br>
</tt>The function <tt>seq</tt> is defined by the equations:
<p>
<table >
<tr><td>
<tt>seq</tt><I> _|_b = _|_</I> </td></tr><tr><td><tt>seq</tt><I> a b = b, if a /=_|_</I> </td></tr></table>
<p>
<tt>seq</tt> is usually introduced to improve performance by
avoiding unneeded laziness. Strict datatypes (see
Section <a href="decls.html#strictness-flags">4.2.1</a>) are defined in terms of the <tt>$!
</tt>function.
However, it has important semantic consequences, because it is available
<I>at every type</I>.
As a consequence, <I>_|_</I> is
not the same as <tt>\x -> </tt> <I>_|_</I>, since <tt>seq</tt> can be used to distinguish them.
For the same reason, the existence of <tt>seq</tt> weakens Haskell's parametricity properties.<p>
The operator <tt>$!</tt> is strict (call-by-value) application, and is defined
in terms of <tt>seq</tt>. The Prelude also defines lazy application, <tt>$</tt>.
<tt><br>
<br>
infixr 0 $, $!<br>
($), ($!) :: (a -> b) -> a -> b<br>
f $ x = f x<br>
f $! x = x `seq` f x<br>
<br>
</tt>The lazy application operator <tt>$</tt> may appear redundant, since
ordinary application <tt>(f x)</tt> means the same as <tt>(f $ x)</tt>.
However, <tt>$</tt> has low, right-associative binding precedence,
so it sometimes allows parentheses to be omitted; for example:
<tt><br>
<br>
f $ g $ h x = f (g (h x))<br>
<br>
</tt>It is also useful in higher-order situations, such as <tt>map ($ 0) xs</tt>,
or <tt>zipWith ($) fs xs</tt>.<p>
<a name="sect6.3"></a>
<h3>6.3<tt> </tt>Standard Haskell Classes</h3>
Figure <a href="basic.html#standard-classes">5</a> shows the hierarchy of
Haskell classes defined in the Prelude and the Prelude types that
are instances of these classes.
<table border=2 cellpadding=3>
<tr><td><div align=center><img src="classes.gif" alt="Diagram of standard Haskell classes">
<h4>Figure 5</h4> </div>
<div align=center><h3>Standard Haskell Classes</h3></div><a name="standard-classes"></a>
</td></tr></table>
<p>
Default class method declarations (Section <a href="decls.html#classes">4.3</a>) are provided
for many of the methods in standard classes. For example, the declaration
of class <tt>Eq</tt> is:
<tt><br>
class Eq a where<br>
(==), (/=) :: a -> a -> Bool<br>
<br>
x /= y = not (x == y)<br>
x == y = not (x /= y)<br>
</tt>This declaration gives default method declarations for both <tt>/=</tt> and <tt>==</tt>,
each being defined in terms of the other. If an instance declaration
for <tt>Eq</tt> defines neither <tt>==</tt> nor <tt>/=</tt>, 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.<p>
A comment with each <tt>class</tt> declaration in Appendix <a href="standard-prelude.html#stdprelude">A</a> specifies
the smallest collection of method definitions that,
together with the default declarations,
provide a definition for all the class methods.
If there is no such comment, then all class methods must
be given to fully specify an instance.<p>
<a name="sect6.3.1"></a>
<h4>6.3.1<tt> </tt>The Eq Class</h4>
<tt><br>
<br>
class Eq a where<br>
(==), (/=) :: a -> a -> Bool<br>
<br>
x /= y = not (x == y)<br>
x == y = not (x /= y)<br>
<br>
</tt>The <tt>Eq</tt> class provides equality (<tt>==</tt>) and inequality (<tt>/=</tt>) methods.
All basic datatypes except for functions and <tt>IO</tt> are instances of this class.
Instances of <tt>Eq</tt> can be derived for any user-defined datatype whose
constituents are also instances of <tt>Eq</tt>.<p>
<a name="sect6.3.2"></a>
<h4>6.3.2<tt> </tt>The Ord Class</h4>
<tt><br>
<br>
class (Eq a) => Ord a where<br>
compare :: a -> a -> Ordering<br>
(<), (<=), (>=), (>) :: a -> a -> Bool<br>
max, min :: a -> a -> a<br>
<br>
compare x y<br>
| x == y = EQ<br>
| x <= y = LT<br>
| otherwise = GT<br>
<br>
x <= y = compare x y /= GT<br>
x < y = compare x y == LT<br>
x >= y = compare x y /= LT<br>
x > y = compare x y == GT<br>
<br>
-- note that (min x y, max x y) = (x,y) or (y,x)<br>
max x y | x >= y = x<br>
| otherwise = y<br>
min x y | x < y = x<br>
| otherwise = y<br>
<br>
</tt>The <tt>Ord</tt> class is used for totally ordered datatypes. All basic
datatypes
except for functions, <tt>IO</tt>, and <tt>IOError</tt>, are instances of this class. Instances
of <tt>Ord</tt>
can be derived for any user-defined datatype whose constituent types
are in <tt>Ord</tt>. The declared order
of the constructors in the data declaration determines the ordering in
derived <tt>Ord</tt> instances.
The <tt>Ordering</tt> datatype
allows a single comparison to determine the precise ordering of two
objects.<p>
<a name="sect6.3.3"></a>
<h4>6.3.3<tt> </tt>The Read and Show Classes</h4>
<tt><br>
<br>
type ReadS a = String -> [(a,String)]<br>
type ShowS = String -> String<br>
<br>
class Read a where<br>
readsPrec :: Int -> ReadS a<br>
readList :: ReadS [a]<br>
-- ... default decl for readList given in Prelude<br>
<br>
class Show a where<br>
showsPrec :: Int -> a -> ShowS<br>
show :: a -> String <br>
showList :: [a] -> ShowS<br>
<br>
showsPrec _ x s = show x ++ s<br>
show x = showsPrec 0 x ""<br>
-- ... default decl for showList given in Prelude<br>
<br>
</tt>The <tt>Read</tt> and <tt>Show</tt> classes are used to convert values to
or from strings. <tt>showsPrec</tt> and <tt>showList</tt> return a <tt>String</tt>-to-<tt>String
</tt>function, to allow constant-time concatenation of its results using function
composition.
A specialised variant, <tt>show</tt>, is also provided, which
uses precedence context zero, and returns an ordinary <tt>String</tt>.
The method <tt>showList</tt> is provided to allow the programmer to
give a specialised way of showing lists of values. This is particularly
useful for the <tt>Char</tt> type, where values of type <tt>String</tt> should be
shown in double quotes, rather than between square brackets.<p>
Derived instances of <tt>Read</tt> and <tt>Show</tt> replicate the style in which a
constructor is declared: infix constructors and field names are used
on input and output. Strings produced by <tt>showsPrec</tt> are usually
readable by <tt>readsPrec</tt>. <p>
All <tt>Prelude</tt> types, except function types and <tt>IO</tt> types,
are instances of <tt>Show</tt> and <tt>Read</tt>.
(If desired, a programmer can easily make functions and <tt>IO</tt> types
into (vacuous) instances of <tt>Show</tt>, by providing an instance declaration.)<p>
For convenience, the Prelude provides the following auxiliary
functions:
<tt><br>
<br>
reads :: (Read a) => ReadS a<br>
reads = readsPrec 0<br>
<br>
shows :: (Show a) => a -> ShowS<br>
shows = showsPrec 0<br>
<br>
read :: (Read a) => String -> a<br>
read s = case [x | (x,t) <- reads s, ("","") <- lex t] of<br>
[x] -> x<br>
[] -> error "PreludeText.read: no parse"<br>
_ -> error "PreludeText.read: ambiguous parse"<br>
<br>
shows</tt> and <tt>reads</tt> use a default precedence of 0. The <tt>read</tt> function reads
input from a string, which must be completely consumed by the input
process. The <tt>lex</tt> function used by <tt>read</tt> is also part of the Prelude.<p>
<a name="sect6.3.4"></a>
<h4>6.3.4<tt> </tt>The Enum Class</h4>
<tt><br>
<br>
class Enum a where<br>
succ, pred :: a -> a<br>
toEnum :: Int -> a<br>
fromEnum :: a -> Int<br>
enumFrom :: a -> [a] -- [n..]<br>
enumFromThen :: a -> a -> [a] -- [n,n'..]<br>
enumFromTo :: a -> a -> [a] -- [n..m]<br>
enumFromThenTo :: a -> a -> a -> [a] -- [n,n'..m]<br>
<br>
-- Default declarations given in Prelude<br>
<br>
</tt>Class <tt>Enum</tt> defines operations on sequentially ordered types.
The functions <tt>succ</tt> and <tt>pred</tt> return the successor and predecessor,
respectively, of a value.
The <tt>toEnum</tt> and <tt>fromEnum</tt> functions map values from a type in
<tt>Enum</tt> onto <tt>Int</tt>. These functions are not meaningful for all
instances of <tt>Enum</tt>: floating
point values or <tt>Integer</tt> may not be mapped onto an <tt>Int</tt>. A
runtime error occurs if either <tt>toEnum</tt> or <tt>fromEnum</tt> is given a value
not mappable to the result type. <p>
The <tt>enumFrom</tt>... methods are used when translating arithmetic
sequences (Section <a href="exps.html#arithmetic-sequences">3.10</a>), and should
obey the specification given in there.<p>
Instances of <tt>Enum</tt> may be derived
for any enumeration type (types whose constructors have no fields).
There are also <tt>Enum</tt> instances for floats. <p>
<a name="sect6.3.5"></a>
<h4>6.3.5<tt> </tt>Class <tt>Functor</tt></h4><p>
<tt><br>
<br>
class Functor f where<br>
fmap :: (a -> b) -> (f a -> f b)<br>
<br>
</tt>The <tt>Functor
</tt>class is used for types that can be mapped over. Lists, <tt>IO</tt>, and
<tt>Maybe</tt> are in this class. <p>
Instances of <tt>Functor</tt> should satisfy the following laws:
<p>
<table >
<tr><td>
<tt>fmap id</tt></td><td align=center>=</td><td><tt>id</tt></td></tr><tr><td><tt>fmap (f . g)</tt></td><td align=center>=</td><td><tt>fmap f . fmap g</tt></td></tr></table>
<p>
All instances defined in the Prelude satisfy these laws.<a name="monad-class"></a><p>
<a name="sect6.3.6"></a>
<h4>6.3.6<tt> </tt>Class <tt>Monad</tt></h4>
<tt><br>
<br>
class Monad m where<br>
(>>=) :: m a -> (a -> m b) -> m b<br>
(>>) :: m a -> m b -> m b<br>
return :: a -> m a<br>
fail :: String -> m a<br>
<br>
m >> k = m >>= \_ -> k<br>
fail s = error s<br>
<br>
</tt>The <tt>Monad</tt> class defines the basic operations over a <I>monad</I>.
See Section <a href="io-13.html#io">7</a> for more information about monads.<p>
"<tt>do</tt>" expressions provide a convenient syntax for writing
monadic expressions (see Section <a href="exps.html#do-expressions">3.14</a>).
The <tt>fail</tt> method is invoked on pattern-match failure in a <tt>do
</tt>expression.<p>
In the Prelude, lists,
<tt>Maybe</tt>, and <tt>IO</tt> are all instances of <tt>Monad</tt>.
The <tt>fail</tt> method for lists returns the empty list <tt>[]</tt>,
and for <tt>Maybe</tt> returns <tt>Nothing</tt>. However, for <tt>IO</tt>, the <tt>fail</tt>
method invokes <tt>error</tt>.<p>
Instances of <tt>Monad</tt> should satisfy the following laws:
<p>
<table >
<tr><td>
<tt>return a >>= k</tt></td><td align=center>=</td><td><tt>k a</tt> </td></tr><tr><td><tt>m >>= return</tt></td><td align=center>=</td><td><tt>m</tt> </td></tr><tr><td><tt>m >>= (\x -> k x >>= h)</tt></td><td align=center>=</td><td><tt>(m >>= k) >>= h</tt></td></tr><tr><td><tt>fmap f xs</tt></td><td align=center>=</td><td><tt>xs >>= return . f</tt></td></tr></table>
<p>
All instances defined in the Prelude satisfy these laws.<p>
The Prelude provides the following auxiliary
functions:
<tt><br>
<br>
sequence :: Monad m => [m a] -> m [a] <br>
sequence_ :: Monad m => [m a] -> m () <br>
mapM :: Monad m => (a -> m b) -> [a] -> m [b]<br>
mapM_ :: Monad m => (a -> m b) -> [a] -> m ()<br>
(=<<) :: Monad m => (a -> m b) -> m a -> m b<br>
<br>
<p>
</tt><a name="sect6.3.7"></a>
<h4>6.3.7<tt> </tt>The Bounded Class</h4>
<tt><br>
class Bounded a where<br>
minBound, maxBound :: a<br>
<p>
</tt>The <tt>Bounded</tt> class is used to name the upper and lower limits of a
type. <tt>Ord</tt> is not a superclass of <tt>Bounded</tt> since types that are not
totally ordered may also have upper and lower bounds.
The types <tt>Int</tt>, <tt>Char</tt>, <tt>Bool</tt>,
<tt>()</tt>, <tt>Ordering</tt>, and all tuples are instances of <tt>Bounded</tt>.
The <tt>Bounded</tt> class may be derived
for any enumeration type; <tt>minBound</tt> is the first constructor listed
in the <tt>data</tt> declaration and <tt>maxBound</tt> is the last. <tt>Bounded</tt> may
also be derived for single-constructor datatypes whose constituent
types are in <tt>Bounded</tt>.<a name="numbers"></a><p>
<a name="sect6.4"></a>
<h3>6.4<tt> </tt>Numbers</h3>
<p>
Haskell provides several kinds of numbers; the numeric
types and the operations upon them have been heavily influenced by
Common Lisp and Scheme.
Numeric function names and operators are usually overloaded, using
several type classes with an inclusion relation shown in
Figure <a href="basic.html#standard-classes">5</a>.
The class <tt>Num</tt> of numeric
types is a subclass of <tt>Eq</tt>, since all numbers may be
compared for equality; its subclass <tt>Real</tt> is also a
subclass of <tt>Ord</tt>, since the other comparison operations
apply to all but complex numbers (defined in the <tt>Complex</tt> library).
The class <tt>Integral</tt> contains integers of both
limited and unlimited range; the class
<tt>Fractional</tt> contains all non-integral types; and
the class <tt>Floating</tt> contains all floating-point
types, both real and complex.<p>
The Prelude defines only the most basic numeric types: fixed sized
integers (<tt>Int</tt>), arbitrary precision integers (<tt>Integer</tt>), single
precision floating (<tt>Float</tt>), and double precision floating
(<tt>Double</tt>). Other numeric types such as rationals and complex numbers
are defined in libraries. In particular, the type <tt>Rational</tt> is a
ratio of two <tt>Integer</tt> values, as defined in the <tt>Rational
</tt>library. <p>
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
all, aspects of the IEEE standard floating point standard have been
accounted for in class <tt>RealFloat</tt>.<p>
The standard numeric types are listed in Table <a href="basic.html#standard-numeric-types">3</a>.
The finite-precision integer type <tt>Int</tt> covers at
least the range
<I>[ - 2</I><sup><I>29</I></sup><I>, 2</I><sup><I>29</I></sup><I> - 1]</I>. As <tt>Int</tt> is an instance of the <tt>Bounded
</tt>class, <tt>maxBound</tt> and <tt>minBound</tt> can be used to determine the exact
<tt>Int</tt> range defined by an implementation.
<tt>Float</tt> is implementation-defined; it is desirable that
this type be at least equal in range and precision to the IEEE
single-precision type. Similarly, <tt>Double</tt> 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
(<I>_|_</I>, semantically), a truncated value, or a special value such as
infinity, indefinite, etc.<p>
<div align=center>
<p>
<table border=2>
<tr><td>
Type </td><td>
Class </td><td>
Description </td></tr><tr><td>
<tt>Integer</tt> </td><td> <tt>Integral</tt> </td><td> Arbitrary-precision integers </td></tr><tr><td><tt>Int</tt> </td><td> <tt>Integral</tt> </td><td> Fixed-precision integers </td></tr><tr><td><tt>(Integral a) => Ratio a</tt> </td><td> <tt>RealFrac</tt> </td><td> Rational numbers </td></tr><tr><td><tt>Float</tt> </td><td> <tt>RealFloat</tt> </td><td> Real floating-point, single precision </td></tr><tr><td><tt>Double</tt> </td><td> <tt>RealFloat</tt> </td><td> Real floating-point, double precision </td></tr><tr><td><tt>(RealFloat a) => Complex a</tt> </td><td> <tt>Floating</tt> </td><td> Complex floating-point </td></tr><tr><td>
</td></tr></table>
<p>
<div align=center> <h4>Table 2</h4> </div>
<div align=center><h3>Standard Numeric Types</h3></div><a name="standard-numeric-types"></a>
</div><p>
The standard numeric classes and other numeric functions defined in
the Prelude are shown
in Figures <a href="basic.html#basic-numeric-1">6</a>--<a href="basic.html#basic-numeric-2">7</a>.
Figure <a href="basic.html#standard-classes">5</a> shows the class dependencies and
built-in types that are instances of the numeric classes.<p>
<table border=2 cellpadding=3>
<tr><td>
<div align=center><table border=2 cellpadding=3>
<tr><td>
<tt><br>
class (Eq a, Show a) => Num a where<br>
(+), (-), (*) :: a -> a -> a<br>
negate :: a -> a<br>
abs, signum :: a -> a<br>
fromInteger :: Integer -> a<br>
<br>
class (Num a, Ord a) => Real a where<br>
toRational :: a -> Rational<br>
<br>
class (Real a, Enum a) => Integral a where<br>
quot, rem, div, mod :: a -> a -> a<br>
quotRem, divMod :: a -> a -> (a,a)<br>
toInteger :: a -> Integer<br>
<br>
class (Num a) => Fractional a where<br>
(/) :: a -> a -> a<br>
recip :: a -> a<br>
fromRational :: Rational -> a<br>
<br>
class (Fractional a) => Floating a where<br>
pi :: a<br>
exp, log, sqrt :: a -> a<br>
(**), logBase :: a -> a -> a<br>
sin, cos, tan :: a -> a<br>
asin, acos, atan :: a -> a<br>
sinh, cosh, tanh :: a -> a<br>
asinh, acosh, atanh :: a -> a<br>
</tt></td></tr></table>
</div>
<div align=center> <h4>Figure 6</h4> </div>
<div align=center><h3>Standard Numeric Classes and Related Operations, Part 1</h3></div><a name="basic-numeric-1"></a>
</td></tr></table>
<p>
<table border=2 cellpadding=3>
<tr><td>
<div align=center><table border=2 cellpadding=3>
<tr><td>
<tt><br>
class (Real a, Fractional a) => RealFrac a where<br>
properFraction :: (Integral b) => a -> (b,a)<br>
truncate, round :: (Integral b) => a -> b<br>
ceiling, floor :: (Integral b) => a -> b<br>
<br>
class (RealFrac a, Floating a) => RealFloat a where<br>
floatRadix :: a -> Integer<br>
floatDigits :: a -> Int<br>
floatRange :: a -> (Int,Int)<br>
decodeFloat :: a -> (Integer,Int)<br>
encodeFloat :: Integer -> Int -> a<br>
exponent :: a -> Int<br>
significand :: a -> a<br>
scaleFloat :: Int -> a -> a<br>
isNaN, isInfinite, isDenormalized, isNegativeZero, isIEEE <br>
:: a -> Bool<br>
atan2 :: a -> a -> a<br>
<br>
fromIntegral :: (Integral a, Num b) => a -> b<br>
gcd, lcm :: (Integral a) => a -> a-> a<br>
(^) :: (Num a, Integral b) => a -> b -> a<br>
(^^) :: (Fractional a, Integral b) => a -> b -> a<br>
<br>
fromRealFrac :: (RealFrac a, Fractional b) => a -> b<br>
</tt></td></tr></table>
</div>
<div align=center> <h4>Figure 7</h4> </div>
<div align=center><h3>Standard Numeric Classes and Related Operations, Part 2</h3></div><a name="basic-numeric-2"></a>
</td></tr></table>
<a name="numeric-literals"></a><p>
<a name="sect6.4.1"></a>
<h4>6.4.1<tt> </tt>Numeric Literals</h4>
<p>
The syntax of numeric literals is given in
Section <a href="lexemes.html#lexemes-numeric">2.5</a>. An integer literal represents the
application
of the function <tt>fromInteger</tt> to the appropriate
value of type
<tt>Integer</tt>. Similarly, a floating literal stands for an application of
<tt>fromRational</tt> to a value of type <tt>Rational</tt> (that is,
<tt>Ratio Integer</tt>). Given the typings:
<tt><br>
<br>
fromInteger :: (Num a) => Integer -> a<br>
fromRational :: (Fractional a) => Rational -> a<br>
<br>
</tt>integer and floating literals have the
typings <tt>(Num a) => a</tt> and <tt>(Fractional a) => a</tt>, respectively.
Numeric literals are defined in this indirect way so that they may be
interpreted as values of any appropriate numeric type.
See Section <a href="decls.html#default-decls">4.3.4</a> for a discussion of overloading ambiguity.<a name="arithmetic-operators"></a><p>
<a name="sect6.4.2"></a>
<h4>6.4.2<tt> </tt>Arithmetic and Number-Theoretic Operations</h4>
<p>
The infix class methods
<tt>(+)</tt>,
<tt>(*)</tt>,
<tt>(-)</tt>,
and the unary function
<tt>negate</tt> (which can also be written as a prefix minus sign; see
section <a href="exps.html#operators">3.4</a>) apply to all numbers. The class methods
<tt>quot</tt>, <tt>rem</tt>, <tt>div</tt>, and
<tt>mod</tt> apply only to integral numbers, while the class method
<tt>(/)
</tt>applies only to fractional ones. The <tt>quot</tt>, <tt>rem</tt>,
<tt>div</tt>, and <tt>mod</tt> class methods satisfy these laws:
<p>
<table >
<tr><td align=center>
<tt>(x </tt>`<tt>quot</tt>`<tt> y)*y + (x </tt>`<tt>rem</tt>`<tt> y) == x</tt></td></tr><tr><td align=center><tt>(x </tt>`<tt>div</tt>`<tt> y)*y + (x </tt>`<tt>mod</tt>`<tt> y) == x
</tt></td></tr></table>
<p>
<tt>`quot`</tt> is integer division truncated toward zero,
while the result of <tt>`div`</tt> is truncated toward
negative infinity.
The <tt>quotRem</tt> class method takes a dividend and a divisor as arguments
and returns a (quotient, remainder) pair; <tt>divMod</tt> is defined
similarly:
<tt><br>
<br>
quotRem x y = (x </tt>`<tt>quot</tt>`<tt> y, x </tt>`<tt>rem</tt>`<tt> y)<br>
divMod x y = (x </tt>`<tt>div</tt>`<tt> y, x </tt>`<tt>mod</tt>`<tt> y)<br>
<br>
</tt>Also available on integral numbers are the even and odd predicates:
<tt><br>
<br>
even x = x </tt>`<tt>rem</tt>`<tt> 2 == 0<br>
odd = not . even<br>
<br>
</tt>Finally, there are the greatest common divisor and least common
multiple functions: <tt>gcd</tt> <I>x</I> <I>y</I> is the greatest
integer that divides both <I>x</I> and <I>y</I>. <tt>lcm</tt> <I>x</I> <I>y
</I>is the smallest positive integer that both <I>x</I> and <I>y</I> divide.<p>
<a name="sect6.4.3"></a>
<h4>6.4.3<tt> </tt>Exponentiation and Logarithms</h4><p>
The one-argument exponential function <tt>exp</tt> and the
logarithm function <tt>log</tt> act on floating-point numbers and
use base <I>e</I>. <tt>logBase</tt> <I>a</I> <I>x</I> returns the
logarithm of <I>x</I> in base <I>a</I>. <tt>sqrt</tt> returns the
principal square root of a floating-point number.
There are three two-argument exponentiation operations:
<tt>(^)</tt> raises any number to a nonnegative integer power,
<tt>(^^)</tt> raises a
fractional number to any integer power, and <tt>(**)
</tt>takes two floating-point arguments. The value of <I>x</I><tt>^0</tt> or <I>x</I><tt>^^0
</tt>is <tt>1</tt> for any <I>x</I>, including zero; <tt>0**</tt><I>y</I> is undefined.
<a name="magnitude-sign"></a>
<a name="sect6.4.4"></a>
<h4>6.4.4<tt> </tt>Magnitude and Sign</h4>
<p>
A number has a <I>magnitude
</I>and a <I>sign</I>. The functions <tt>abs</tt> and
<tt>signum</tt> apply to any number and satisfy the law:
<tt><br>
<br>
abs x * signum x == x<br>
<br>
</tt>For real numbers, these functions are defined by:
<tt><br>
<br>
abs x | x >= 0 = x<br>
| x < 0 = -x<br>
<br>
signum x | x > 0 = 1<br>
| x == 0 = 0<br>
| x < 0 = -1<br>
<br>
<p>
</tt><a name="sect6.4.5"></a>
<h4>6.4.5<tt> </tt>Trigonometric Functions</h4><p>
Class <tt>Floating</tt> provides the
circular and hyperbolic sine, cosine,
and tangent functions and their inverses.
Default implementations of <tt>tan</tt>, <tt>tanh</tt>, <tt>logBase</tt>, <tt>**</tt>, and <tt>sqrt</tt> are
provided, but implementors are free to provide more accurate implementations.<p>
Class <tt>RealFloat</tt> provides a version of arctangent
taking two real floating-point arguments.
For real floating <I>x</I> and <I>y</I>, <tt>atan2</tt> <I>y</I> <I>x
</I>computes the angle (from the positive x-axis) of the vector from the origin
to the point <I>(x,y)</I>. <tt>atan2</tt> <I>y</I> <I>x
</I>returns a value in the range <tt>[-pi, pi]</tt>. It
follows the Common Lisp semantics for the origin when signed zeroes are
supported. <tt>atan2</tt> <I>y</I> <tt>1</tt>, with <I>y</I> in a type that is <tt>RealFloat</tt>, should return the
same value as <tt>atan</tt> <I>y</I>. A default definition of <tt>atan2</tt> is provided, but
implementors can provide a more accurate implementation. <p>
The precise definition of the above functions is as in Common Lisp,
which in turn follows Penfield's proposal for
APL [<a href="haskell.html#$penfield:complex-apl">7</a>]. See these references for discussions
of branch cuts, discontinuities, and implementation.<a name="coercion"></a><p>
<a name="sect6.4.6"></a>
<h4>6.4.6<tt> </tt>Coercions and Component Extraction</h4>
<p>
The <tt>ceiling</tt>, <tt>floor</tt>,
<tt>truncate</tt>, and <tt>round
</tt>functions each take a real fractional argument and return an integral
result. <tt>ceiling</tt> <I>x</I> returns the least integer not less than <I>x</I>, and
<tt>floor</tt> <I>x</I>, the greatest integer not greater than <I>x</I>. <tt>truncate</tt> <I>x
</I>yields the integer nearest <I>x</I> between <I>0</I> and <I>x</I>, inclusive.
<tt>round</tt> <I>x</I> returns the nearest integer to <I>x</I>, the even integer if
<I>x</I> is equidistant between two integers.<p>
The function <tt>properFraction</tt> takes a real
fractional number <I>x</I> and returns a pair <I>(n,f)</I> such that <I>x = n+f</I>, and:
<I>n</I> is an integral number with the same sign as <I>x</I>; and <I>f</I> is a
fraction <I>f</I> with the same type and sign as <I>x</I>, and with absolute
value less than 1.
The <tt>ceiling</tt>, <tt>floor</tt>, <tt>truncate</tt>, and <tt>round
</tt>functions can be defined in terms of <tt>properFraction</tt>.<p>
Two functions convert numbers to type <tt>Rational</tt>:
<tt>toRational</tt> returns the rational equivalent of
its real argument with full precision;
<tt>approxRational</tt> takes two real fractional arguments
<I>x</I> and <font face="symbol">e</font> and returns the simplest rational number within
<font face="symbol">e</font> of <I>x</I>, where a rational p/q in reduced form is
<I>simpler</I> than another p<sup>'</sup> / q<sup>'</sup> if
|p| <=|p<sup>'</sup>| and q <=q<sup>'</sup> .
Every real interval contains a unique simplest rational;
in particular, note that 0/1 is the simplest rational of all.<p>
The class methods of class <tt>RealFloat</tt> allow
efficient, machine-independent
access to the components of a floating-point number.
The functions <tt>floatRadix</tt>,
<tt>floatDigits</tt>, and
<tt>floatRange</tt> 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 <tt>decodeFloat
</tt>applied to a real floating-point number returns the significand
expressed as an <tt>Integer</tt> and an appropriately scaled exponent (an
<tt>Int</tt>). If <tt>decodeFloat x</tt> yields <tt>(</tt><I>m</I><tt>,</tt><I>n</I><tt>)</tt>, then <tt>x</tt> is
equal in value to <I>mb</I><sup><I>n</I></sup>, where <I>b</I> is the floating-point radix, and
furthermore, either <I>m</I> and <I>n</I> are both zero or else
<I>b</I><sup><I>d-1</I></sup><I><=m<b</I><sup><I>d</I></sup>, where <I>d</I> is the value of <tt>floatDigits x</tt>.
<tt>encodeFloat</tt> performs the inverse of this
transformation. The functions <tt>significand
</tt>and <tt>exponent</tt> together provide the same
information as <tt>decodeFloat</tt>, but rather than an <tt>Integer</tt>,
<tt>significand x</tt> yields a value of the same type as <tt>x</tt>, scaled to lie
in the open interval <I>(-1,1)</I>. <tt>exponent 0</tt> is zero. <tt>scaleFloat
</tt>multiplies a floating-point number by an integer power of the radix.<p>
The functions <tt>isNaN</tt>, <tt>isInfinite</tt>, <tt>isDenormalized</tt>,
<tt>isNegativeZero</tt>, and <tt>isIEEE</tt> all support numbers represented using
the IEEE standard. For non-IEEE floating point numbers, these may all
return false.<p>
Also available are the following coercion functions:
<tt><br>
<br>
fromIntegral :: (Integral a, Num b) => a -> b<br>
fromRealFrac :: (RealFrac a, Fractional b) => a -> b<br>
<p>
<hr><i>The Haskell 98 Report</i><br><a href="index.html">top</a> | <a href="modules.html">back</a> | <a href="io-13.html">next</a> | <a href="index98.html">contents</a> | <a href="prelude-index.html">function index</a> <br><font size=2>1 February, 1999</font>
<p>
</tt>
|