2160 lines
104 KiB
HTML
2160 lines
104 KiB
HTML
<?xml version="1.0" encoding="iso-8859-1" ?>
|
|
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
|
|
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
|
|
<!--http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd-->
|
|
<html xmlns="http://www.w3.org/1999/xhtml"
|
|
>
|
|
<head><title>6 Predefined Types and Classes</title>
|
|
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
|
|
<meta name="generator" content="TeX4ht (http://www.cse.ohio-state.edu/~gurari/TeX4ht/)" />
|
|
<meta name="originator" content="TeX4ht (http://www.cse.ohio-state.edu/~gurari/TeX4ht/)" />
|
|
<!-- 2,html,xhtml -->
|
|
<meta name="src" content="haskell.tex" />
|
|
<meta name="date" content="2010-07-20 13:11:00" />
|
|
<link rel="stylesheet" type="text/css" href="haskell.css" />
|
|
</head><body
|
|
>
|
|
<!--l. 7--><div class="crosslinks"><p class="noindent">[<a
|
|
href="haskellch7.html" >next</a>] [<a
|
|
href="haskellch5.html" >prev</a>] [<a
|
|
href="haskellch5.html#tailhaskellch5.html" >prev-tail</a>] [<a
|
|
href="#tailhaskellch6.html">tail</a>] [<a
|
|
href="haskellpa1.html#haskellch6.html" >up</a>] </p></div>
|
|
<h2 class="chapterHead"><span class="titlemark">Chapter 6</span><br /><a
|
|
id="x13-1160006"></a>Predefined Types and Classes</h2> The Haskell Prelude contains predefined classes, types, and functions that
|
|
are implicitly imported into every Haskell program. In this chapter, 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 Chapter <a
|
|
href="haskellch9.html#x16-1710009">9<!--tex4ht:ref: stdprelude --></a>. Other predefined types such as arrays, complex numbers, and rationals are defined in
|
|
Part <a
|
|
href="haskellpa2.html#x20-192000II">II<!--tex4ht:ref: libraries --></a>.
|
|
<h3 class="sectionHead"><span class="titlemark">6.1 </span> <a
|
|
id="x13-1170006.1"></a>Standard Haskell Types</h3>
|
|
<p class="noindent"> These types are defined by the Haskell Prelude. Numeric types are described in Section <a
|
|
href="#x13-1350006.4">6.4<!--tex4ht:ref: numbers --></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.
|
|
<p class="noindent">
|
|
<h4 class="subsectionHead"><span class="titlemark">6.1.1 </span> <a
|
|
id="x13-1180006.1.1"></a>Booleans</h4>
|
|
<a
|
|
id="dx13-118001"></a>
|
|
<p class="noindent">
|
|
<div class="quote">
|
|
|
|
|
|
|
|
<div class="verbatim" id="verbatim-106">
|
|
data  Bool  =  False | True deriving
|
|
 <br />                             (Read, Show, Eq, Ord, Enum, Bounded)
|
|
</div>
|
|
<p class="noindent"></div>
|
|
<p class="noindent"> The boolean type <span
|
|
class="pcrr7t-">Bool</span> is an enumeration. The basic boolean functions are <span
|
|
class="pcrr7t-">&&</span> (and), <span
|
|
class="pcrr7t-">||</span> (or), and <span
|
|
class="pcrr7t-">not</span>. The name
|
|
<span
|
|
class="pcrr7t-">otherwise</span> is defined as <span
|
|
class="pcrr7t-">True</span> to make guarded expressions more readable.
|
|
<a
|
|
id="dx13-118002"></a>
|
|
<a
|
|
id="dx13-118003"></a>
|
|
<a
|
|
id="dx13-118004"></a>
|
|
<a
|
|
id="dx13-118005"></a>
|
|
<a
|
|
id="dx13-118006"></a>
|
|
<a
|
|
id="dx13-118007"></a>
|
|
<a
|
|
id="dx13-118008"></a>
|
|
<p class="noindent">
|
|
<h4 class="subsectionHead"><span class="titlemark">6.1.2 </span> <a
|
|
id="x13-1190006.1.2"></a>Characters and Strings</h4>
|
|
<a
|
|
id="dx13-119001"></a>
|
|
<a
|
|
id="dx13-119002"></a>
|
|
<p class="noindent"> The character type <span
|
|
class="pcrr7t-">Char</span><a
|
|
id="dx13-119003"></a> is an enumeration whose values represent Unicode characters <span class="cite">[<a
|
|
href="haskellli3.html#Xunicode">2</a>]</span>. The lexical syntax for
|
|
characters is defined in Section <a
|
|
href="haskellch2.html#x7-200002.6">2.6<!--tex4ht:ref: lexemes-char --></a>; character literals are nullary constructors in the datatype <span
|
|
class="pcrr7t-">Char</span>.
|
|
Type <span
|
|
class="pcrr7t-">Char</span> is an instance of the classes <span
|
|
class="pcrr7t-">Read</span>, <span
|
|
class="pcrr7t-">Show</span>, <span
|
|
class="pcrr7t-">Eq</span>, <span
|
|
class="pcrr7t-">Ord</span>, <span
|
|
class="pcrr7t-">Enum</span>, and <span
|
|
class="pcrr7t-">Bounded</span>. The <span
|
|
class="pcrr7t-">toEnum</span>
|
|
and <span
|
|
class="pcrr7t-">fromEnum</span> functions, standard functions from class <span
|
|
class="pcrr7t-">Enum</span>, map characters to and from the <span
|
|
class="pcrr7t-">Int</span>
|
|
type.
|
|
<p class="noindent"> Note that ASCII control characters each have several representations in character literals: numeric escapes, ASCII
|
|
mnemonic escapes, and the <span
|
|
class="pcrr7t-">\^</span><span
|
|
class="cmmi-10">X</span> notation. In addition, there are the following equivalences: <span
|
|
class="pcrr7t-">\a</span> and <span
|
|
class="pcrr7t-">\BEL</span>, <span
|
|
class="pcrr7t-">\b</span> and
|
|
<span
|
|
class="pcrr7t-">\BS</span>, <span
|
|
class="pcrr7t-">\f</span> and <span
|
|
class="pcrr7t-">\FF</span>, <span
|
|
class="pcrr7t-">\r</span> and <span
|
|
class="pcrr7t-">\CR</span>, <span
|
|
class="pcrr7t-">\t</span> and <span
|
|
class="pcrr7t-">\HT</span>, <span
|
|
class="pcrr7t-">\v</span> and <span
|
|
class="pcrr7t-">\VT</span>, and <span
|
|
class="pcrr7t-">\n</span> and <span
|
|
class="pcrr7t-">\LF</span>.
|
|
<p class="noindent"> A <span
|
|
class="ptmri7t-">string </span>is a list of characters:
|
|
<div class="quote">
|
|
|
|
|
|
|
|
<div class="verbatim" id="verbatim-107">
|
|
type  String  =  [Char]
|
|
</div>
|
|
<p class="noindent"></div>
|
|
<a
|
|
id="dx13-119004"></a>
|
|
<p class="noindent"> Strings may be abbreviated using the lexical syntax described in Section <a
|
|
href="haskellch2.html#x7-200002.6">2.6<!--tex4ht:ref: lexemes-char --></a>. For example, <span
|
|
class="pcrr7t-">"A</span><span
|
|
class="pcrr7t-"> string"</span>
|
|
abbreviates
|
|
<span
|
|
class="pcrr7t-">[</span><span
|
|
class="pcrr7t-"> 'A','</span><span
|
|
class="pcrr7t-"> ','s','t','r',</span><span
|
|
class="pcrr7t-"> 'i','n','g']</span>
|
|
<p class="noindent">
|
|
<h4 class="subsectionHead"><span class="titlemark">6.1.3 </span> <a
|
|
id="x13-1200006.1.3"></a>Lists</h4>
|
|
<a
|
|
id="dx13-120001"></a>
|
|
<p class="noindent">
|
|
<div class="quote">
|
|
|
|
|
|
|
|
<div class="verbatim" id="verbatim-108">
|
|
data  [a]  =  [] | a : [a]  deriving (Eq, Ord)
|
|
</div>
|
|
<p class="noindent"></div>
|
|
<p class="noindent"> Lists are an algebraic datatype of two constructors, although with special syntax, as described in Section <a
|
|
href="haskellch3.html#x8-340003.7">3.7<!--tex4ht:ref: lists --></a>.
|
|
The first constructor is the null list, written ‘<span
|
|
class="pcrr7t-">[]</span>’ (“nil”), <a
|
|
id="dx13-120002"></a>and the second is ‘<span
|
|
class="pcrr7t-">:</span>’ (“cons”). <a
|
|
id="dx13-120003"></a>The module
|
|
<span
|
|
class="pcrr7t-">PreludeList</span> (see Section <a
|
|
href="haskellch9.html#x16-1720009.1">9.1<!--tex4ht:ref: preludelist --></a>) defines many standard list functions. Arithmetic sequences <a
|
|
id="dx13-120004"></a>and list
|
|
comprehensions, <a
|
|
id="dx13-120005"></a>two convenient syntaxes for special kinds of lists, are described in Sections <a
|
|
href="haskellch3.html#x8-400003.10">3.10<!--tex4ht:ref: arithmetic-sequences --></a> and
|
|
<a
|
|
href="haskellch3.html#x8-420003.11">3.11<!--tex4ht:ref: list-comprehensions --></a>, respectively. Lists are an instance of classes <span
|
|
class="pcrr7t-">Read</span>, <span
|
|
class="pcrr7t-">Show</span>, <span
|
|
class="pcrr7t-">Eq</span>, <span
|
|
class="pcrr7t-">Ord</span>, <span
|
|
class="pcrr7t-">Monad</span>, <span
|
|
class="pcrr7t-">Functor</span>, and
|
|
<span
|
|
class="pcrr7t-">MonadPlus</span>.
|
|
<p class="noindent">
|
|
<h4 class="subsectionHead"><span class="titlemark">6.1.4 </span> <a
|
|
id="x13-1210006.1.4"></a>Tuples</h4>
|
|
<a
|
|
id="dx13-121001"></a>
|
|
<p class="noindent"> Tuples are algebraic datatypes with special syntax, as defined in Section <a
|
|
href="haskellch3.html#x8-360003.8">3.8<!--tex4ht:ref: tuples --></a>. Each tuple type has a single
|
|
constructor. All tuples are instances of <span
|
|
class="pcrr7t-">Eq</span>, <span
|
|
class="pcrr7t-">Ord</span>, <span
|
|
class="pcrr7t-">Bounded</span>, <span
|
|
class="pcrr7t-">Read</span>, and <span
|
|
class="pcrr7t-">Show</span> (provided, of course, that all their
|
|
component types are).
|
|
<p class="noindent"> 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 implementation must support tuples up to
|
|
size 15, together with the instances for <span
|
|
class="pcrr7t-">Eq</span>, <span
|
|
class="pcrr7t-">Ord</span>, <span
|
|
class="pcrr7t-">Bounded</span>, <span
|
|
class="pcrr7t-">Read</span>, and <span
|
|
class="pcrr7t-">Show</span>. The Prelude and libraries define tuple
|
|
functions such as <span
|
|
class="pcrr7t-">zip</span> for tuples up to a size of 7.
|
|
<p class="noindent"> The constructor for a tuple is written by omitting the expressions surrounding the commas; thus <span
|
|
class="pcrr7t-">(x,y)</span> and
|
|
<span
|
|
class="pcrr7t-">(,)</span><span
|
|
class="pcrr7t-"> x</span><span
|
|
class="pcrr7t-"> y</span> produce the same value. The same holds for tuple type constructors; thus, <span
|
|
class="pcrr7t-">(Int,Bool,Int)</span> and
|
|
<span
|
|
class="pcrr7t-">(,,)</span><span
|
|
class="pcrr7t-"> Int</span><span
|
|
class="pcrr7t-"> Bool</span><span
|
|
class="pcrr7t-"> Int</span> denote the same type. <a
|
|
id="dx13-121002"></a><a
|
|
id="dx13-121003"></a>
|
|
<p class="noindent"> The following functions are defined for pairs (2-tuples): <span
|
|
class="pcrr7t-">fst</span>, <span
|
|
class="pcrr7t-">snd</span>, <span
|
|
class="pcrr7t-">curry</span>, and <span
|
|
class="pcrr7t-">uncurry</span>. Similar functions are
|
|
not predefined for larger tuples. <a
|
|
id="dx13-121004"></a><a
|
|
id="dx13-121005"></a> <a
|
|
id="dx13-121006"></a><a
|
|
id="dx13-121007"></a> <a
|
|
id="dx13-121008"></a>
|
|
<p class="noindent">
|
|
<h4 class="subsectionHead"><span class="titlemark">6.1.5 </span> <a
|
|
id="x13-1220006.1.5"></a>The Unit Datatype</h4>
|
|
<p class="noindent">
|
|
<div class="quote">
|
|
|
|
|
|
|
|
<div class="verbatim" id="verbatim-109">
|
|
data  () = () deriving (Eq, Ord, Bounded, Enum, Read, Show)
|
|
</div>
|
|
<p class="noindent"></div>
|
|
<p class="noindent"> The unit datatype <span
|
|
class="pcrr7t-">()</span> has one non-<span
|
|
class="cmsy-10">⊥</span> member, the nullary constructor <span
|
|
class="pcrr7t-">()</span>. See also Section <a
|
|
href="haskellch3.html#x8-380003.9">3.9<!--tex4ht:ref: unit-expression --></a>. <a
|
|
id="dx13-122001"></a>
|
|
<p class="noindent">
|
|
<h4 class="subsectionHead"><span class="titlemark">6.1.6 </span> <a
|
|
id="x13-1230006.1.6"></a>Function Types</h4>
|
|
<a
|
|
id="dx13-123001"></a>
|
|
<p class="noindent"> Functions are an abstract type: no constructors directly create functional values. The following simple functions are
|
|
found in the Prelude: <span
|
|
class="pcrr7t-">id</span>, <span
|
|
class="pcrr7t-">const</span>, <span
|
|
class="pcrr7t-">(.)</span>, <span
|
|
class="pcrr7t-">flip</span>, <span
|
|
class="pcrr7t-">($)</span>, and <span
|
|
class="pcrr7t-">until</span>. <a
|
|
id="dx13-123002"></a><a
|
|
id="dx13-123003"></a> <a
|
|
id="dx13-123004"></a><a
|
|
id="dx13-123005"></a> <a
|
|
id="dx13-123006"></a>
|
|
<p class="noindent">
|
|
<h4 class="subsectionHead"><span class="titlemark">6.1.7 </span> <a
|
|
id="x13-1240006.1.7"></a>The IO and IOError Types</h4>
|
|
<p class="noindent"> The <span
|
|
class="pcrr7t-">IO</span> type serves as a tag for operations (actions) that interact with the outside world. The <span
|
|
class="pcrr7t-">IO</span> type is abstract: no
|
|
constructors are visible to the user. <span
|
|
class="pcrr7t-">IO</span> is an instance of the <span
|
|
class="pcrr7t-">Monad</span> and <span
|
|
class="pcrr7t-">Functor</span> classes. Chapter <a
|
|
href="haskellch7.html#x14-1420007">7<!--tex4ht:ref: io --></a> describes I/O
|
|
operations.
|
|
<p class="noindent"> <span
|
|
class="pcrr7t-">IOError</span> is an abstract type representing errors raised by I/O operations. It is an instance of <span
|
|
class="pcrr7t-">Show</span> and <span
|
|
class="pcrr7t-">Eq</span>. 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="haskellch9.html#x16-1740009.3">9.3<!--tex4ht:ref: preludeio --></a>), and Part <a
|
|
href="haskellpa2.html#x20-192000II">II<!--tex4ht:ref: libraries --></a> contains many more.
|
|
<a
|
|
id="dx13-124001"></a><a
|
|
id="dx13-124002"></a>
|
|
<p class="noindent">
|
|
<h4 class="subsectionHead"><span class="titlemark">6.1.8 </span> <a
|
|
id="x13-1250006.1.8"></a>Other Types</h4>
|
|
<p class="noindent">
|
|
<div class="quote">
|
|
|
|
|
|
|
|
<div class="verbatim" id="verbatim-110">
|
|
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)
|
|
</div>
|
|
<p class="noindent"> <a
|
|
id="dx13-125001"></a><a
|
|
id="dx13-125002"></a><a
|
|
id="dx13-125003"></a><a
|
|
id="dx13-125004"></a><a
|
|
id="dx13-125005"></a><a
|
|
id="dx13-125006"></a><a
|
|
id="dx13-125007"></a><a
|
|
id="dx13-125008"></a><a
|
|
id="dx13-125009"></a><a
|
|
id="dx13-125010"></a><a
|
|
id="dx13-125011"></a><a
|
|
id="dx13-125012"></a></div>
|
|
<p class="noindent"> The <span
|
|
class="pcrr7t-">Maybe</span> type is an instance of classes <span
|
|
class="pcrr7t-">Functor</span>, <span
|
|
class="pcrr7t-">Monad</span>, and <span
|
|
class="pcrr7t-">MonadPlus</span>. The <span
|
|
class="pcrr7t-">Ordering</span> type is used by
|
|
<span
|
|
class="pcrr7t-">compare</span> in the class <span
|
|
class="pcrr7t-">Ord</span>. The functions <span
|
|
class="pcrr7t-">maybe</span> and <span
|
|
class="pcrr7t-">either</span> are found in the Prelude.
|
|
<p class="noindent">
|
|
<h3 class="sectionHead"><span class="titlemark">6.2 </span> <a
|
|
id="x13-1260006.2"></a>Strict Evaluation</h3>
|
|
<a
|
|
id="dx13-126001"></a>
|
|
<a
|
|
id="dx13-126002"></a>
|
|
<a
|
|
id="dx13-126003"></a>
|
|
<p class="noindent"> 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 <span
|
|
class="pcrr7t-">seq</span> function:
|
|
<div class="quote">
|
|
|
|
|
|
|
|
<div class="verbatim" id="verbatim-111">
|
|
  seq :: a -> b -> b
|
|
</div>
|
|
<p class="noindent"></div>
|
|
<p class="noindent"> The function <span
|
|
class="pcrr7t-">seq</span> is defined by the equations:
|
|
<div class="array"> <table id="TBL-87" class="array"
|
|
cellpadding="0" cellspacing="0"
|
|
><colgroup id="TBL-87-1g"><col
|
|
id="TBL-87-1" /></colgroup><tr
|
|
style="vertical-align:baseline;"><td style="white-space:nowrap; text-align:left;"
|
|
><div class="td11"> <span
|
|
class="pcrr7t-">seq</span><span
|
|
class="cmmi-10"> </span><span
|
|
class="cmsy-10">⊥</span><span
|
|
class="cmmi-10"> b</span><span
|
|
class="cmmi-10">  </span><span
|
|
class="cmr-10">=</span> <span
|
|
class="cmmi-10"> </span><span
|
|
class="cmsy-10">⊥</span> </div></td>
|
|
</tr><tr
|
|
style="vertical-align:baseline;"><td style="white-space:nowrap; text-align:left;"
|
|
><div class="td11"> <span
|
|
class="pcrr7t-">seq</span><span
|
|
class="cmmi-10"> a</span><span
|
|
class="cmmi-10"> b</span><span
|
|
class="cmmi-10">  </span><span
|
|
class="cmr-10">=</span> <span
|
|
class="cmmi-10"> b,</span><span
|
|
class="cmmi-10"> if</span><span
|
|
class="cmmi-10"> a</span><span
|
|
class="cmmi-10"> </span><span
|
|
class="cmmi-10">≠</span><span
|
|
class="cmmi-10"> </span><span
|
|
class="cmsy-10">⊥</span> </div></td>
|
|
</tr><tr
|
|
style="vertical-align:baseline;"><td style="white-space:nowrap; text-align:left;"
|
|
><div class="td11"> </div></td>
|
|
</tr></table> </div>
|
|
<br
|
|
class="newline" /><span
|
|
class="pcrr7t-">seq</span> is usually introduced to improve performance by avoiding unneeded laziness. Strict datatypes (see
|
|
<a
|
|
id="dx13-126004"></a>Section <a
|
|
href="haskellch4.html#x10-710004.2.1">4.2.1<!--tex4ht:ref: strictness-flags --></a>) are defined in terms of the <span
|
|
class="pcrr7t-">$!</span> operator. However, the provision of <span
|
|
class="pcrr7t-">seq</span> has important semantic
|
|
consequences, because it is available <span
|
|
class="ptmri7t-">at every type</span>. As a consequence, <span
|
|
class="cmsy-10">⊥</span> is not the same as <span
|
|
class="pcrr7t-">\x</span><span
|
|
class="pcrr7t-"> -></span><span
|
|
class="pcrr7t-"> </span><span
|
|
class="cmsy-10">⊥</span>, since <span
|
|
class="pcrr7t-">seq</span>
|
|
can be used to distinguish them. For the same reason, the existence of <span
|
|
class="pcrr7t-">seq</span> weakens Haskell’s parametricity
|
|
properties.
|
|
<p class="noindent"> The operator <span
|
|
class="pcrr7t-">$!</span> is strict (call-by-value) application, and is defined in terms of <span
|
|
class="pcrr7t-">seq</span>. The Prelude also defines the <span
|
|
class="pcrr7t-">$</span>
|
|
operator to perform non-strict application.
|
|
<div class="quote">
|
|
|
|
|
|
|
|
<div class="verbatim" id="verbatim-112">
|
|
  infixr 0 $, $!
|
|
 <br />  ($), ($!) :: (a -> b) -> a -> b
|
|
 <br />  f $  x   =          f x
|
|
 <br />  f $! x   =  x ‘seq‘ f x
|
|
</div>
|
|
<p class="noindent"></div>
|
|
<p class="noindent"> The non-strict application operator <span
|
|
class="pcrr7t-">$</span> may appear redundant, since ordinary application <span
|
|
class="pcrr7t-">(f</span><span
|
|
class="pcrr7t-"> x)</span> means the same as
|
|
<span
|
|
class="pcrr7t-">(f</span><span
|
|
class="pcrr7t-"> $</span><span
|
|
class="pcrr7t-"> x)</span>. However, <span
|
|
class="pcrr7t-">$</span> has low, right-associative binding precedence, so it sometimes allows parentheses to be
|
|
omitted; for example:
|
|
<div class="quote">
|
|
|
|
|
|
|
|
<div class="verbatim" id="verbatim-113">
|
|
  f $ g $ h x  =  f (g (h x))
|
|
</div>
|
|
<p class="noindent"></div>
|
|
<p class="noindent"> It is also useful in higher-order situations, such as <span
|
|
class="pcrr7t-">map</span><span
|
|
class="pcrr7t-"> ($</span><span
|
|
class="pcrr7t-"> 0)</span><span
|
|
class="pcrr7t-"> xs</span>, or <span
|
|
class="pcrr7t-">zipWith</span><span
|
|
class="pcrr7t-"> ($)</span><span
|
|
class="pcrr7t-"> fs</span><span
|
|
class="pcrr7t-"> xs</span>.
|
|
<p class="noindent">
|
|
<h3 class="sectionHead"><span class="titlemark">6.3 </span> <a
|
|
id="x13-1270006.3"></a>Standard Haskell Classes</h3>
|
|
<p class="noindent"> Figure <a
|
|
href="#x13-1270011">6.1<!--tex4ht:ref: standard-classes --></a> shows the hierarchy of Haskell classes defined in the Prelude and the Prelude types that are instances of
|
|
these classes. <hr class="figure" /><div class="figure"
|
|
>
|
|
|
|
|
|
|
|
<a
|
|
id="x13-1270011"></a>
|
|
|
|
|
|
|
|
<p class="noindent"> <img
|
|
src="haskell2x.png" alt="PIC" class="graphics" width="286.66974pt" height="372.38962pt" /><!--tex4ht:graphics
|
|
name="haskell2x.png" src="classes.eps"
|
|
-->
|
|
<br /> <div class="caption"
|
|
><span class="id">Figure 6.1: </span><span
|
|
class="content">Standard Haskell Classes</span></div><!--tex4ht:label?: x13-1270011 -->
|
|
|
|
|
|
|
|
</div><hr class="endfigure" />
|
|
<p class="noindent"> Default class method declarations (Section <a
|
|
href="haskellch4.html#x10-750004.3">4.3<!--tex4ht:ref: classes --></a>) are provided for many of the methods in standard
|
|
classes. A comment with each <span
|
|
class="pcrr7t-">class</span> declaration in Chapter <a
|
|
href="haskellch9.html#x16-1710009">9<!--tex4ht:ref: stdprelude --></a> specifies the 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.
|
|
<h4 class="subsectionHead"><span class="titlemark">6.3.1 </span> <a
|
|
id="x13-1280006.3.1"></a>The Eq Class</h4>
|
|
<a
|
|
id="dx13-128001"></a>
|
|
<a
|
|
id="dx13-128002"></a>
|
|
<a
|
|
id="dx13-128003"></a>
|
|
<p class="noindent">
|
|
<div class="quote">
|
|
|
|
|
|
|
|
<div class="verbatim" id="verbatim-114">
|
|
  class  Eq a  where
|
|
 <br />        (==), (/=)  ::  a -> a -> Bool
|
|
 <br />
|
|
 <br />        x /= y  = not (x == y)
|
|
 <br />        x == y  = not (x /= y)
|
|
</div>
|
|
<p class="noindent"></div>
|
|
<p class="noindent"> The <span
|
|
class="pcrr7t-">Eq</span> class provides equality (<span
|
|
class="pcrr7t-">==</span>) and inequality (<span
|
|
class="pcrr7t-">/=</span>) methods. All basic datatypes except for functions and <span
|
|
class="pcrr7t-">IO</span>
|
|
are instances of this class. Instances of <span
|
|
class="pcrr7t-">Eq</span> can be derived for any user-defined datatype whose constituents are also
|
|
instances of <span
|
|
class="pcrr7t-">Eq</span>.
|
|
<p class="noindent"> This declaration gives default method declarations for both <span
|
|
class="pcrr7t-">/=</span> and <span
|
|
class="pcrr7t-">==</span>, each being defined in terms of the other. If
|
|
an instance declaration for <span
|
|
class="pcrr7t-">Eq</span> defines neither <span
|
|
class="pcrr7t-">==</span> nor <span
|
|
class="pcrr7t-">/=</span>, 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 class="noindent">
|
|
<h4 class="subsectionHead"><span class="titlemark">6.3.2 </span> <a
|
|
id="x13-1290006.3.2"></a>The Ord Class</h4>
|
|
<a
|
|
id="dx13-129001"></a>
|
|
<a
|
|
id="dx13-129002"></a>
|
|
<a
|
|
id="dx13-129003"></a>
|
|
<a
|
|
id="dx13-129004"></a>
|
|
<a
|
|
id="dx13-129005"></a>
|
|
<a
|
|
id="dx13-129006"></a>
|
|
<a
|
|
id="dx13-129007"></a>
|
|
<a
|
|
id="dx13-129008"></a>
|
|
<p class="noindent">
|
|
<div class="quote">
|
|
|
|
|
|
|
|
<div class="verbatim" id="verbatim-115">
|
|
  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 | 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    =  y
|
|
 <br />            | otherwise =  x
|
|
 <br />    min x y | x <= y    =  x
|
|
 <br />            | otherwise =  y
|
|
</div>
|
|
<p class="noindent"></div>
|
|
<p class="noindent"> The <span
|
|
class="pcrr7t-">Ord</span> class is used for totally ordered datatypes. All basic datatypes except for functions, <span
|
|
class="pcrr7t-">IO</span>, and <span
|
|
class="pcrr7t-">IOError</span>, are
|
|
instances of this class. Instances of <span
|
|
class="pcrr7t-">Ord</span> can be derived for any user-defined datatype whose constituent types are in
|
|
<span
|
|
class="pcrr7t-">Ord</span>. The declared order of the constructors in the data declaration determines the ordering in derived <span
|
|
class="pcrr7t-">Ord</span>
|
|
instances. The <span
|
|
class="pcrr7t-">Ordering</span> datatype allows a single comparison to determine the precise ordering of two
|
|
objects.
|
|
<p class="noindent"> The default declarations allow a user to create an <span
|
|
class="pcrr7t-">Ord</span> instance either with a type-specific <span
|
|
class="pcrr7t-">compare</span> function or
|
|
with type-specific <span
|
|
class="pcrr7t-">==</span> and <span
|
|
class="pcrr7t-"><=</span> functions.
|
|
<p class="noindent">
|
|
<h4 class="subsectionHead"><span class="titlemark">6.3.3 </span> <a
|
|
id="x13-1300006.3.3"></a>The Read and Show Classes</h4>
|
|
<a
|
|
id="dx13-130001"></a>
|
|
<a
|
|
id="dx13-130002"></a>
|
|
<a
|
|
id="dx13-130003"></a>
|
|
<a
|
|
id="dx13-130004"></a>
|
|
<a
|
|
id="dx13-130005"></a>
|
|
<a
|
|
id="dx13-130006"></a>
|
|
<a
|
|
id="dx13-130007"></a>
|
|
<a
|
|
id="dx13-130008"></a>
|
|
<a
|
|
id="dx13-130009"></a>
|
|
<p class="noindent">
|
|
|
|
|
|
|
|
<div class="quote">
|
|
|
|
|
|
|
|
<div class="verbatim" id="verbatim-116">
|
|
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
|
|
</div>
|
|
<p class="noindent"></div>
|
|
<p class="noindent"> The <span
|
|
class="pcrr7t-">Read</span> and <span
|
|
class="pcrr7t-">Show</span> classes are used to convert values to or from strings. The <span
|
|
class="pcrr7t-">Int</span> argument to <span
|
|
class="pcrr7t-">showsPrec</span> and
|
|
<span
|
|
class="pcrr7t-">readsPrec</span> gives the operator precedence of the enclosing context (see Section <a
|
|
href="haskellch11.html#x18-18600011.4">11.4<!--tex4ht:ref: derived-text --></a>).
|
|
<p class="noindent"> <span
|
|
class="pcrr7t-">showsPrec</span> and <span
|
|
class="pcrr7t-">showList</span> return a <span
|
|
class="pcrr7t-">String</span>-to-<span
|
|
class="pcrr7t-">String</span> function, to allow constant-time concatenation
|
|
of its results using function composition. A specialised variant, <span
|
|
class="pcrr7t-">show</span>, is also provided, which uses
|
|
precedence context zero, and returns an ordinary <span
|
|
class="pcrr7t-">String</span>. The method <span
|
|
class="pcrr7t-">showList</span> is provided to allow the
|
|
programmer to give a specialised way of showing lists of values. This is particularly useful for the <span
|
|
class="pcrr7t-">Char</span>
|
|
type, where values of type <span
|
|
class="pcrr7t-">String</span> should be shown in double quotes, rather than between square
|
|
brackets.
|
|
<p class="noindent"> Derived instances of <span
|
|
class="pcrr7t-">Read</span> and <span
|
|
class="pcrr7t-">Show</span> replicate the style in which a constructor is declared: infix constructors and
|
|
field names are used on input and output. Strings produced by <span
|
|
class="pcrr7t-">showsPrec</span> are usually readable by
|
|
<span
|
|
class="pcrr7t-">readsPrec</span>.
|
|
<p class="noindent"> All <span
|
|
class="pcrr7t-">Prelude</span> types, except function types and <span
|
|
class="pcrr7t-">IO</span> types, are instances of <span
|
|
class="pcrr7t-">Show</span> and <span
|
|
class="pcrr7t-">Read</span>. (If desired, a
|
|
programmer can easily make functions and <span
|
|
class="pcrr7t-">IO</span> types into (vacuous) instances of <span
|
|
class="pcrr7t-">Show</span>, by providing an instance
|
|
declaration.)
|
|
<a
|
|
id="dx13-130010"></a>
|
|
<a
|
|
id="dx13-130011"></a>
|
|
<a
|
|
id="dx13-130012"></a>
|
|
<p class="noindent"> For convenience, the Prelude provides the following auxiliary functions:
|
|
<div class="quote">
|
|
|
|
|
|
|
|
<div class="verbatim" id="verbatim-117">
|
|
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"
|
|
</div>
|
|
<p class="noindent"></div>
|
|
<a
|
|
id="dx13-130013"></a>
|
|
<a
|
|
id="dx13-130014"></a>
|
|
<a
|
|
id="dx13-130015"></a>
|
|
<a
|
|
id="dx13-130016"></a>
|
|
<p class="noindent"><span
|
|
class="pcrr7t-">shows</span> and <span
|
|
class="pcrr7t-">reads</span> use a default precedence of 0. The <span
|
|
class="pcrr7t-">read</span> function reads input from a string, which must be
|
|
completely consumed by the input process.
|
|
<a
|
|
id="dx13-130017"></a>
|
|
<p class="noindent"> The function <span
|
|
class="pcrr7t-">lex</span><span
|
|
class="pcrr7t-"> ::</span><span
|
|
class="pcrr7t-"> ReadS</span><span
|
|
class="pcrr7t-"> String</span>, used by <span
|
|
class="pcrr7t-">read</span>, 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, <span
|
|
class="pcrr7t-">lex</span> returns a single successful “lexeme” consisting of the empty string. (Thus <span
|
|
class="pcrr7t-">lex</span><span
|
|
class="pcrr7t-"> ""</span> =
|
|
<span
|
|
class="pcrr7t-">[("","")]</span>.) If there is no legal lexeme at the beginning of the input string, <span
|
|
class="pcrr7t-">lex</span> fails (i.e. returns
|
|
<span
|
|
class="pcrr7t-">[]</span>).
|
|
<p class="noindent">
|
|
<h4 class="subsectionHead"><span class="titlemark">6.3.4 </span> <a
|
|
id="x13-1310006.3.4"></a>The Enum Class</h4>
|
|
<a
|
|
id="dx13-131001"></a>
|
|
<a
|
|
id="dx13-131002"></a>
|
|
<a
|
|
id="dx13-131003"></a>
|
|
<a
|
|
id="dx13-131004"></a>
|
|
<a
|
|
id="dx13-131005"></a>
|
|
<a
|
|
id="dx13-131006"></a>
|
|
<a
|
|
id="dx13-131007"></a>
|
|
<p class="noindent">
|
|
<div class="quote">
|
|
|
|
|
|
|
|
<div class="verbatim" id="verbatim-118">
|
|
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
|
|
</div>
|
|
<p class="noindent"></div>
|
|
<p class="noindent"> Class <span
|
|
class="pcrr7t-">Enum</span> defines operations on sequentially ordered types. The functions <span
|
|
class="pcrr7t-">succ</span> and <span
|
|
class="pcrr7t-">pred</span> return the successor
|
|
and predecessor, respectively, of a value. The functions <span
|
|
class="pcrr7t-">fromEnum</span> and <span
|
|
class="pcrr7t-">toEnum</span> map values from a type in
|
|
<span
|
|
class="pcrr7t-">Enum</span> to and from <span
|
|
class="pcrr7t-">Int</span>. The <span
|
|
class="pcrr7t-">enumFrom</span>... methods are used when translating arithmetic sequences
|
|
(Section <a
|
|
href="haskellch3.html#x8-400003.10">3.10<!--tex4ht:ref: arithmetic-sequences --></a>).
|
|
<p class="noindent"> Instances of <span
|
|
class="pcrr7t-">Enum</span> may be derived for any enumeration type (types whose constructors have no fields); see
|
|
Chapter <a
|
|
href="haskellch11.html#x18-18200011">11<!--tex4ht:ref: derived-appendix --></a>.
|
|
<p class="noindent"> For any type that is an instance of class <span
|
|
class="pcrr7t-">Bounded</span> as well as <span
|
|
class="pcrr7t-">Enum</span>, the following should hold:
|
|
<ul class="itemize1">
|
|
<li class="itemize">The calls <span
|
|
class="pcrr7t-">succ</span><span
|
|
class="pcrr7t-"> maxBound</span> and <span
|
|
class="pcrr7t-">pred</span><span
|
|
class="pcrr7t-"> minBound</span> should result in a runtime error.
|
|
</li>
|
|
<li class="itemize"><span
|
|
class="pcrr7t-">fromEnum</span> and <span
|
|
class="pcrr7t-">toEnum</span> should give a runtime error if the result value is not representable in the
|
|
result type. For example, <span
|
|
class="pcrr7t-">toEnum</span><span
|
|
class="pcrr7t-"> 7</span><span
|
|
class="pcrr7t-"> ::</span><span
|
|
class="pcrr7t-"> Bool</span> is an error.
|
|
</li>
|
|
<li class="itemize"><span
|
|
class="pcrr7t-">enumFrom</span> and <span
|
|
class="pcrr7t-">enumFromThen</span> should be defined with an implicit bound, thus:
|
|
<div class="quote">
|
|
|
|
|
|
|
|
<div class="verbatim" id="verbatim-119">
|
|
  enumFrom     x   = enumFromTo     x maxBound
|
|
 <br />  enumFromThen x y = enumFromThenTo x y bound
|
|
 <br />    where
|
|
 <br />      bound | fromEnum y >= fromEnum x = maxBound
|
|
 <br />            | otherwise                = minBound
|
|
</div>
|
|
<p class="noindent"></div>
|
|
</li></ul>
|
|
<p class="noindent"> The following <span
|
|
class="pcrr7t-">Prelude</span> types are instances of <span
|
|
class="pcrr7t-">Enum</span>:
|
|
<ul class="itemize1">
|
|
<li class="itemize">Enumeration types: <span
|
|
class="pcrr7t-">()</span>, <span
|
|
class="pcrr7t-">Bool</span>, and <span
|
|
class="pcrr7t-">Ordering</span>. The semantics of these instances is given by
|
|
Chapter <a
|
|
href="haskellch11.html#x18-18200011">11<!--tex4ht:ref: derived-appendix --></a>. For example, <span
|
|
class="pcrr7t-">[LT</span><span
|
|
class="pcrr7t-"> ..]</span> is the list <span
|
|
class="pcrr7t-">[LT,EQ,GT]</span>.
|
|
</li>
|
|
<li class="itemize"><span
|
|
class="pcrr7t-">Char</span>: the instance is given in Chapter <a
|
|
href="haskellch9.html#x16-1710009">9<!--tex4ht:ref: stdprelude --></a>, based on the primitive functions that convert between a
|
|
<span
|
|
class="pcrr7t-">Char</span> and an <span
|
|
class="pcrr7t-">Int</span>. For example, <span
|
|
class="pcrr7t-">enumFromTo</span><span
|
|
class="pcrr7t-"> 'a'</span><span
|
|
class="pcrr7t-"> 'z'</span> denotes the list of lowercase letters in
|
|
alphabetical order.
|
|
</li>
|
|
<li class="itemize">Numeric types: <span
|
|
class="pcrr7t-">Int</span>, <span
|
|
class="pcrr7t-">Integer</span>, <span
|
|
class="pcrr7t-">Float</span>, <span
|
|
class="pcrr7t-">Double</span>. The semantics of these instances is given next.</li></ul>
|
|
<p class="noindent"> For all four numeric types, <span
|
|
class="pcrr7t-">succ</span> adds 1, and <span
|
|
class="pcrr7t-">pred</span> subtracts 1. The conversions <span
|
|
class="pcrr7t-">fromEnum</span> and <span
|
|
class="pcrr7t-">toEnum</span> convert
|
|
between the type and <span
|
|
class="pcrr7t-">Int</span>. In the case of <span
|
|
class="pcrr7t-">Float</span> and <span
|
|
class="pcrr7t-">Double</span>, the digits after the decimal point may be lost. It is
|
|
implementation-dependent what <span
|
|
class="pcrr7t-">fromEnum</span> returns when applied to a value that is too large to fit in an
|
|
<span
|
|
class="pcrr7t-">Int</span>.
|
|
<p class="noindent"> For the types <span
|
|
class="pcrr7t-">Int</span> and <span
|
|
class="pcrr7t-">Integer</span>, the enumeration functions have the following meaning:
|
|
<ul class="itemize1">
|
|
<li class="itemize">The sequence <span
|
|
class="pcrr7t-">enumFrom</span><span
|
|
class="cmmi-10"> e</span><sub><span
|
|
class="cmr-7">1</span></sub> is the list <span
|
|
class="pcrr7t-">[</span><span
|
|
class="cmmi-10">e</span><sub><span
|
|
class="cmr-7">1</span></sub><span
|
|
class="pcrr7t-">,</span><span
|
|
class="cmmi-10">e</span><sub><span
|
|
class="cmr-7">1</span></sub> <span
|
|
class="cmr-10">+ 1</span><span
|
|
class="pcrr7t-">,</span><span
|
|
class="cmmi-10">e</span><sub><span
|
|
class="cmr-7">1</span></sub> <span
|
|
class="cmr-10">+ 2</span><span
|
|
class="pcrr7t-">,</span><span
|
|
class="cmmi-10">…</span><span
|
|
class="pcrr7t-">]</span>.
|
|
</li>
|
|
<li class="itemize">The sequence <span
|
|
class="pcrr7t-">enumFromThen</span><span
|
|
class="cmmi-10"> e</span><sub><span
|
|
class="cmr-7">1</span></sub><span
|
|
class="cmmi-10"> e</span><sub><span
|
|
class="cmr-7">2</span></sub> is the list <span
|
|
class="pcrr7t-">[</span><span
|
|
class="cmmi-10">e</span><sub><span
|
|
class="cmr-7">1</span></sub><span
|
|
class="pcrr7t-">,</span><span
|
|
class="cmmi-10">e</span><sub><span
|
|
class="cmr-7">1</span></sub> <span
|
|
class="cmr-10">+ </span><span
|
|
class="cmmi-10">i</span><span
|
|
class="pcrr7t-">,</span><span
|
|
class="cmmi-10">e</span><sub><span
|
|
class="cmr-7">1</span></sub> <span
|
|
class="cmr-10">+ 2</span><span
|
|
class="cmmi-10">i</span><span
|
|
class="pcrr7t-">,</span><span
|
|
class="cmmi-10">…</span><span
|
|
class="pcrr7t-">]</span>, where the increment, <span
|
|
class="cmmi-10">i</span>, is
|
|
<span
|
|
class="cmmi-10">e</span><sub><span
|
|
class="cmr-7">2</span></sub> <span
|
|
class="cmsy-10">− </span><span
|
|
class="cmmi-10">e</span><sub><span
|
|
class="cmr-7">1</span></sub>. The increment may be zero or negative. If the increment is zero, all the list elements are the
|
|
same.
|
|
</li>
|
|
<li class="itemize">The sequence <span
|
|
class="pcrr7t-">enumFromTo</span><span
|
|
class="cmmi-10"> e</span><sub><span
|
|
class="cmr-7">1</span></sub><span
|
|
class="cmmi-10"> e</span><sub><span
|
|
class="cmr-7">3</span></sub> is the list <span
|
|
class="pcrr7t-">[</span><span
|
|
class="cmmi-10">e</span><sub><span
|
|
class="cmr-7">1</span></sub><span
|
|
class="pcrr7t-">,</span><span
|
|
class="cmmi-10">e</span><sub><span
|
|
class="cmr-7">1</span></sub> <span
|
|
class="cmr-10">+ 1</span><span
|
|
class="pcrr7t-">,</span><span
|
|
class="cmmi-10">e</span><sub><span
|
|
class="cmr-7">1</span></sub> <span
|
|
class="cmr-10">+ 2</span><span
|
|
class="pcrr7t-">,</span><span
|
|
class="cmmi-10">…</span><span
|
|
class="cmmi-10">e</span><sub><span
|
|
class="cmr-7">3</span></sub><span
|
|
class="pcrr7t-">]</span>. The list is empty if
|
|
<span
|
|
class="cmmi-10">e</span><sub><span
|
|
class="cmr-7">1</span></sub><span
|
|
class="cmmi-10">  ></span> <span
|
|
class="cmmi-10"> e</span><sub><span
|
|
class="cmr-7">3</span></sub>.
|
|
</li>
|
|
<li class="itemize">The sequence <span
|
|
class="pcrr7t-">enumFromThenTo</span><span
|
|
class="cmmi-10"> e</span><sub><span
|
|
class="cmr-7">1</span></sub><span
|
|
class="cmmi-10"> e</span><sub><span
|
|
class="cmr-7">2</span></sub><span
|
|
class="cmmi-10"> e</span><sub><span
|
|
class="cmr-7">3</span></sub> is the list <span
|
|
class="pcrr7t-">[</span><span
|
|
class="cmmi-10">e</span><sub><span
|
|
class="cmr-7">1</span></sub><span
|
|
class="pcrr7t-">,</span><span
|
|
class="cmmi-10">e</span><sub><span
|
|
class="cmr-7">1</span></sub> <span
|
|
class="cmr-10">+ </span><span
|
|
class="cmmi-10">i</span><span
|
|
class="pcrr7t-">,</span><span
|
|
class="cmmi-10">e</span><sub><span
|
|
class="cmr-7">1</span></sub> <span
|
|
class="cmr-10">+ 2</span><span
|
|
class="cmmi-10">i</span><span
|
|
class="pcrr7t-">,</span><span
|
|
class="cmmi-10">…</span><span
|
|
class="cmmi-10">e</span><sub><span
|
|
class="cmr-7">3</span></sub><span
|
|
class="pcrr7t-">]</span>, where the
|
|
increment, <span
|
|
class="cmmi-10">i</span>, is <span
|
|
class="cmmi-10">e</span><sub><span
|
|
class="cmr-7">2</span></sub> <span
|
|
class="cmsy-10">− </span><span
|
|
class="cmmi-10">e</span><sub><span
|
|
class="cmr-7">1</span></sub>. If the increment is positive or zero, the list terminates when the next element
|
|
would be greater than <span
|
|
class="cmmi-10">e</span><sub><span
|
|
class="cmr-7">3</span></sub>; the list is empty if <span
|
|
class="cmmi-10">e</span><sub><span
|
|
class="cmr-7">1</span></sub><span
|
|
class="cmmi-10">  ></span> <span
|
|
class="cmmi-10"> e</span><sub><span
|
|
class="cmr-7">3</span></sub>. If the increment is negative, the list terminates
|
|
when the next element would be less than <span
|
|
class="cmmi-10">e</span><sub><span
|
|
class="cmr-7">3</span></sub>; the list is empty if <span
|
|
class="cmmi-10">e</span><span
|
|
class="cmr-10">1</span><span
|
|
class="cmmi-10">  <</span> <span
|
|
class="cmmi-10"> e</span><sub><span
|
|
class="cmr-7">3</span></sub>.</li></ul>
|
|
|
|
|
|
|
|
<p class="noindent"> For <span
|
|
class="pcrr7t-">Float</span> and <span
|
|
class="pcrr7t-">Double</span>, the semantics of the <span
|
|
class="pcrr7t-">enumFrom</span> family is given by the rules for <span
|
|
class="pcrr7t-">Int</span> above, except that
|
|
the list terminates when the elements become greater than <span
|
|
class="cmmi-10">e</span><sub><span
|
|
class="cmr-7">3</span></sub> <span
|
|
class="cmr-10">+ </span><span
|
|
class="cmmi-10">i∕</span><span
|
|
class="cmr-10">2</span> for positive increment <span
|
|
class="cmmi-10">i</span>, or when they become
|
|
less than <span
|
|
class="cmmi-10">e</span><sub><span
|
|
class="cmr-7">3</span></sub> <span
|
|
class="cmr-10">+ </span><span
|
|
class="cmmi-10">i∕</span><span
|
|
class="cmr-10">2</span> for negative <span
|
|
class="cmmi-10">i</span>.
|
|
<p class="noindent"> For all four of these Prelude numeric types, all of the <span
|
|
class="pcrr7t-">enumFrom</span> family of functions are strict in all their
|
|
arguments.
|
|
<p class="noindent">
|
|
<h4 class="subsectionHead"><span class="titlemark">6.3.5 </span> <a
|
|
id="x13-1320006.3.5"></a>The Functor Class</h4>
|
|
<a
|
|
id="dx13-132001"></a>
|
|
<a
|
|
id="dx13-132002"></a>
|
|
<a
|
|
id="dx13-132003"></a>
|
|
<p class="noindent">
|
|
<div class="quote">
|
|
|
|
|
|
|
|
<div class="verbatim" id="verbatim-120">
|
|
class  Functor f  where
|
|
 <br />    fmap    :: (a -> b) -> f a -> f b
|
|
</div>
|
|
<p class="noindent"></div>
|
|
<p class="noindent"> The <span
|
|
class="pcrr7t-">Functor</span> class is used for types that can be mapped over. Lists, <span
|
|
class="pcrr7t-">IO</span>, and <span
|
|
class="pcrr7t-">Maybe</span> are in this class.
|
|
<p class="noindent"> Instances of <span
|
|
class="pcrr7t-">Functor</span> should satisfy the following laws:
|
|
<div class="array"> <table id="TBL-88" class="array"
|
|
cellpadding="0" cellspacing="0"
|
|
><colgroup id="TBL-88-1g"><col
|
|
id="TBL-88-1" /><col
|
|
id="TBL-88-2" /><col
|
|
id="TBL-88-3" /></colgroup><tr
|
|
style="vertical-align:baseline;"><td style="white-space:nowrap; text-align:left;"
|
|
><div class="td11"> <span
|
|
class="pcrr7t-">fmap</span><span
|
|
class="pcrr7t-"> id</span> </div></td><td style="white-space:nowrap; text-align:center;"
|
|
><div class="td11"> <span
|
|
class="cmr-10">= </span></div></td><td style="white-space:nowrap; text-align:left;"
|
|
><div class="td11"> <span
|
|
class="pcrr7t-">id</span> </div></td>
|
|
</tr><tr
|
|
style="vertical-align:baseline;"><td style="white-space:nowrap; text-align:left;"
|
|
><div class="td11"> <span
|
|
class="pcrr7t-">fmap</span><span
|
|
class="pcrr7t-"> (f</span><span
|
|
class="pcrr7t-"> .</span><span
|
|
class="pcrr7t-"> g)</span> </div></td><td style="white-space:nowrap; text-align:center;"
|
|
><div class="td11"> <span
|
|
class="cmr-10">= </span></div></td><td style="white-space:nowrap; text-align:left;"
|
|
><div class="td11"> <span
|
|
class="pcrr7t-">fmap</span><span
|
|
class="pcrr7t-"> f</span><span
|
|
class="pcrr7t-"> .</span><span
|
|
class="pcrr7t-"> fmap</span><span
|
|
class="pcrr7t-"> g</span> </div></td>
|
|
</tr><tr
|
|
style="vertical-align:baseline;"><td style="white-space:nowrap; text-align:left;"
|
|
><div class="td11"> </div></td>
|
|
</tr></table> </div>
|
|
<br
|
|
class="newline" /> All instances of <span
|
|
class="pcrr7t-">Functor</span> defined in the Prelude satisfy these laws.
|
|
<p class="noindent">
|
|
<h4 class="subsectionHead"><span class="titlemark">6.3.6 </span> <a
|
|
id="x13-1330006.3.6"></a>The Monad Class</h4>
|
|
<a
|
|
id="dx13-133001"></a>
|
|
<a
|
|
id="dx13-133002"></a>
|
|
<a
|
|
id="dx13-133003"></a>
|
|
<a
|
|
id="dx13-133004"></a>
|
|
<a
|
|
id="dx13-133005"></a>
|
|
<a
|
|
id="dx13-133006"></a>
|
|
<p class="noindent">
|
|
<div class="quote">
|
|
|
|
|
|
|
|
<div class="verbatim" id="verbatim-121">
|
|
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
|
|
</div>
|
|
<p class="noindent"></div>
|
|
<p class="noindent"> The <span
|
|
class="pcrr7t-">Monad</span> class defines the basic operations over a <span
|
|
class="ptmri7t-">monad</span>. See Chapter <a
|
|
href="haskellch7.html#x14-1420007">7<!--tex4ht:ref: io --></a> for more information about
|
|
monads.
|
|
<p class="noindent"> “<span
|
|
class="pcrr7t-">do</span>” expressions provide a convenient syntax for writing monadic expressions (see Section <a
|
|
href="haskellch3.html#x8-470003.14">3.14<!--tex4ht:ref: do-expressions --></a>). The <span
|
|
class="pcrr7t-">fail</span>
|
|
method is invoked on pattern-match failure in a <span
|
|
class="pcrr7t-">do</span> expression.
|
|
<p class="noindent"> In the Prelude, lists, <span
|
|
class="pcrr7t-">Maybe</span>, and <span
|
|
class="pcrr7t-">IO</span> are all instances of <span
|
|
class="pcrr7t-">Monad</span>. The <span
|
|
class="pcrr7t-">fail</span> method for lists returns the
|
|
empty list <span
|
|
class="pcrr7t-">[]</span>, for <span
|
|
class="pcrr7t-">Maybe</span> returns <span
|
|
class="pcrr7t-">Nothing</span>, and for <span
|
|
class="pcrr7t-">IO</span> raises a user exception in the IO monad (see
|
|
Section <a
|
|
href="haskellch7.html#x14-1480007.3">7.3<!--tex4ht:ref: io-exceptions --></a>).
|
|
<p class="noindent"> Instances of <span
|
|
class="pcrr7t-">Monad</span> should satisfy the following laws:
|
|
<div class="array"> <table id="TBL-89" class="array"
|
|
cellpadding="0" cellspacing="0"
|
|
><colgroup id="TBL-89-1g"><col
|
|
id="TBL-89-1" /><col
|
|
id="TBL-89-2" /><col
|
|
id="TBL-89-3" /></colgroup><tr
|
|
style="vertical-align:baseline;"><td style="white-space:nowrap; text-align:left;"
|
|
><div class="td11"> <span
|
|
class="pcrr7t-">return</span><span
|
|
class="pcrr7t-"> a</span><span
|
|
class="pcrr7t-"> >>=</span><span
|
|
class="pcrr7t-"> k</span> </div></td><td style="white-space:nowrap; text-align:center;"
|
|
><div class="td11"> <span
|
|
class="cmr-10">= </span></div></td><td style="white-space:nowrap; text-align:left;"
|
|
><div class="td11"> <span
|
|
class="pcrr7t-">k</span><span
|
|
class="pcrr7t-"> a</span> </div></td></tr><tr
|
|
style="vertical-align:baseline;"><td style="white-space:nowrap; text-align:left;"
|
|
><div class="td11"> <span
|
|
class="pcrr7t-">m</span><span
|
|
class="pcrr7t-"> >>=</span><span
|
|
class="pcrr7t-"> return</span> </div></td><td style="white-space:nowrap; text-align:center;"
|
|
><div class="td11"> <span
|
|
class="cmr-10">= </span></div></td><td style="white-space:nowrap; text-align:left;"
|
|
><div class="td11"> <span
|
|
class="pcrr7t-">m</span></div></td>
|
|
</tr><tr
|
|
style="vertical-align:baseline;"><td style="white-space:nowrap; text-align:left;"
|
|
><div class="td11"> <span
|
|
class="pcrr7t-">m</span><span
|
|
class="pcrr7t-"> >>=</span><span
|
|
class="pcrr7t-"> (\x</span><span
|
|
class="pcrr7t-"> -></span><span
|
|
class="pcrr7t-"> k</span><span
|
|
class="pcrr7t-"> x</span><span
|
|
class="pcrr7t-"> >>=</span><span
|
|
class="pcrr7t-"> h)</span> </div></td><td style="white-space:nowrap; text-align:center;"
|
|
><div class="td11"> <span
|
|
class="cmr-10">= </span></div></td><td style="white-space:nowrap; text-align:left;"
|
|
><div class="td11"> <span
|
|
class="pcrr7t-">(m</span><span
|
|
class="pcrr7t-"> >>=</span><span
|
|
class="pcrr7t-"> k)</span><span
|
|
class="pcrr7t-"> >>=</span><span
|
|
class="pcrr7t-"> h</span> </div></td>
|
|
</tr><tr
|
|
style="vertical-align:baseline;"><td style="white-space:nowrap; text-align:left;"
|
|
><div class="td11"> </div></td>
|
|
</tr></table> </div>
|
|
<br
|
|
class="newline" /> Instances of both <span
|
|
class="pcrr7t-">Monad</span> and <span
|
|
class="pcrr7t-">Functor</span> should additionally satisfy the law:
|
|
|
|
|
|
|
|
<div class="array"> <table id="TBL-90" class="array"
|
|
cellpadding="0" cellspacing="0"
|
|
><colgroup id="TBL-90-1g"><col
|
|
id="TBL-90-1" /><col
|
|
id="TBL-90-2" /><col
|
|
id="TBL-90-3" /></colgroup><tr
|
|
style="vertical-align:baseline;"><td style="white-space:nowrap; text-align:left;"
|
|
><div class="td11"> <span
|
|
class="pcrr7t-">fmap</span><span
|
|
class="pcrr7t-"> f</span><span
|
|
class="pcrr7t-"> xs</span> </div></td><td style="white-space:nowrap; text-align:center;"
|
|
><div class="td11"> <span
|
|
class="cmr-10">= </span></div></td><td style="white-space:nowrap; text-align:left;"
|
|
><div class="td11"> <span
|
|
class="pcrr7t-">xs</span><span
|
|
class="pcrr7t-"> >>=</span><span
|
|
class="pcrr7t-"> return</span><span
|
|
class="pcrr7t-"> .</span><span
|
|
class="pcrr7t-"> f</span> </div></td>
|
|
</tr><tr
|
|
style="vertical-align:baseline;"><td style="white-space:nowrap; text-align:left;"
|
|
><div class="td11"> </div></td>
|
|
</tr></table> </div>
|
|
<br
|
|
class="newline" /> All instances of <span
|
|
class="pcrr7t-">Monad</span> defined in the Prelude satisfy these laws.
|
|
<p class="noindent"> The Prelude provides the following auxiliary functions:
|
|
<div class="quote">
|
|
|
|
|
|
|
|
<div class="verbatim" id="verbatim-122">
|
|
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
|
|
</div>
|
|
<p class="noindent"></div>
|
|
<a
|
|
id="dx13-133007"></a>
|
|
<a
|
|
id="dx13-133008"></a>
|
|
<a
|
|
id="dx13-133009"></a>
|
|
<a
|
|
id="dx13-133010"></a>
|
|
<a
|
|
id="dx13-133011"></a>
|
|
<p class="noindent">
|
|
<h4 class="subsectionHead"><span class="titlemark">6.3.7 </span> <a
|
|
id="x13-1340006.3.7"></a>The Bounded Class</h4>
|
|
<a
|
|
id="dx13-134001"></a>
|
|
<a
|
|
id="dx13-134002"></a>
|
|
<a
|
|
id="dx13-134003"></a>
|
|
|
|
|
|
|
|
<div class="verbatim" id="verbatim-123">
|
|
class  Bounded a  where
|
|
 <br />    minBound, maxBound :: a
|
|
</div>
|
|
<p class="noindent">
|
|
<p class="noindent"> The <span
|
|
class="pcrr7t-">Bounded</span> class is used to name the upper and lower limits of a type. <span
|
|
class="pcrr7t-">Ord</span> is not a superclass of <span
|
|
class="pcrr7t-">Bounded</span> since
|
|
types that are not totally ordered may also have upper and lower bounds. The types <span
|
|
class="pcrr7t-">Int</span>, <span
|
|
class="pcrr7t-">Char</span>, <span
|
|
class="pcrr7t-">Bool</span>, <span
|
|
class="pcrr7t-">()</span>,
|
|
<span
|
|
class="pcrr7t-">Ordering</span>, and all tuples are instances of <span
|
|
class="pcrr7t-">Bounded</span>. The <span
|
|
class="pcrr7t-">Bounded</span> class may be derived for any
|
|
enumeration type; <span
|
|
class="pcrr7t-">minBound</span> is the first constructor listed in the <span
|
|
class="pcrr7t-">data</span> declaration and <span
|
|
class="pcrr7t-">maxBound</span> is the
|
|
last. <span
|
|
class="pcrr7t-">Bounded</span> may also be derived for single-constructor datatypes whose constituent types are in
|
|
<span
|
|
class="pcrr7t-">Bounded</span>.
|
|
<p class="noindent">
|
|
<h3 class="sectionHead"><span class="titlemark">6.4 </span> <a
|
|
id="x13-1350006.4"></a>Numbers</h3>
|
|
<a
|
|
id="dx13-135001"></a>
|
|
<p class="noindent"> 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="#x13-1270011">6.1<!--tex4ht:ref: standard-classes --></a>. The class <span
|
|
class="pcrr7t-">Num</span><a
|
|
id="dx13-135002"></a> of numeric types is a subclass of
|
|
<span
|
|
class="pcrr7t-">Eq</span><a
|
|
id="dx13-135003"></a>, since all numbers may be compared for equality; its subclass <span
|
|
class="pcrr7t-">Real</span><a
|
|
id="dx13-135004"></a> is also a subclass of <span
|
|
class="pcrr7t-">Ord</span><a
|
|
id="dx13-135005"></a>, since
|
|
the other comparison operations apply to all but complex numbers (defined in the <span
|
|
class="pcrr7t-">Complex</span> library).
|
|
The class <span
|
|
class="pcrr7t-">Integral</span><a
|
|
id="dx13-135006"></a> contains integers of both limited and unlimited range; the class <span
|
|
class="pcrr7t-">Fractional</span><a
|
|
id="dx13-135007"></a>
|
|
contains all non-integral types; and the class <span
|
|
class="pcrr7t-">Floating</span><a
|
|
id="dx13-135008"></a> contains all floating-point types, both real and
|
|
complex.
|
|
<p class="noindent"> The Prelude defines only the most basic numeric types: fixed sized integers (<span
|
|
class="pcrr7t-">Int</span>), arbitrary precision integers
|
|
(<span
|
|
class="pcrr7t-">Integer</span>), single precision floating (<span
|
|
class="pcrr7t-">Float</span>), and double precision floating (<span
|
|
class="pcrr7t-">Double</span>). Other numeric types such
|
|
as rationals and complex numbers are defined in libraries. In particular, the type <span
|
|
class="pcrr7t-">Rational</span> is a ratio of two
|
|
<span
|
|
class="pcrr7t-">Integer</span> values, as defined in the <span
|
|
class="pcrr7t-">Ratio</span> library.
|
|
<p class="noindent"> 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 floating point standard have been accounted for
|
|
in Prelude class <span
|
|
class="pcrr7t-">RealFloat</span>.
|
|
<p class="noindent"> The standard numeric types are listed in Table <a
|
|
href="#x13-1350121">6.1<!--tex4ht:ref: standard-numeric-types --></a>. The finite-precision integer type <span
|
|
class="pcrr7t-">Int</span><a
|
|
id="dx13-135009"></a> covers at
|
|
least the range <span
|
|
class="cmr-10">[</span><span
|
|
class="cmmi-10">  </span><span
|
|
class="cmsy-10">−</span> <span
|
|
class="cmmi-10"> </span><span
|
|
class="cmr-10">2</span><sup><span
|
|
class="cmr-7">29</span></sup><span
|
|
class="cmmi-10">,</span><span
|
|
class="cmmi-10"> </span><span
|
|
class="cmr-10">2</span><sup><span
|
|
class="cmr-7">29</span></sup><span
|
|
class="cmmi-10">  </span><span
|
|
class="cmsy-10">−</span> <span
|
|
class="cmmi-10"> </span><span
|
|
class="cmr-10">1]</span>. As <span
|
|
class="pcrr7t-">Int</span> is an instance of the <span
|
|
class="pcrr7t-">Bounded</span> class, <span
|
|
class="pcrr7t-">maxBound</span> and
|
|
<span
|
|
class="pcrr7t-">minBound</span> can be used to determine the exact <span
|
|
class="pcrr7t-">Int</span> range defined by an implementation. <span
|
|
class="pcrr7t-">Float</span><a
|
|
id="dx13-135010"></a> is
|
|
implementation-defined; it is desirable that this type be at least equal in range and precision to the IEEE
|
|
single-precision type. Similarly, <span
|
|
class="pcrr7t-">Double</span><a
|
|
id="dx13-135011"></a> 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 (<span
|
|
class="cmsy-10">⊥</span>, semantically), a truncated value, or a special value such as infinity, indefinite,
|
|
etc.
|
|
|
|
|
|
|
|
<div class="table">
|
|
|
|
|
|
|
|
<p class="noindent"> <a
|
|
id="x13-1350121"></a><hr class="float" /><div class="float"
|
|
>
|
|
|
|
|
|
|
|
<!--tex4ht:inline--><div class="tabular"> <table id="TBL-91" class="tabular"
|
|
cellspacing="0" cellpadding="0" rules="groups"
|
|
><colgroup id="TBL-91-1g"><col
|
|
id="TBL-91-1" /></colgroup><colgroup id="TBL-91-2g"><col
|
|
id="TBL-91-2" /></colgroup><colgroup id="TBL-91-3g"><col
|
|
id="TBL-91-3" /></colgroup><tr
|
|
class="hline"><td><hr /></td><td><hr /></td><td><hr /></td></tr><tr
|
|
style="vertical-align:baseline;" id="TBL-91-1-"><td style="white-space:nowrap; text-align:center;" id="TBL-91-1-1"
|
|
class="td11"> <div class="multicolumn" style="white-space:nowrap; text-align:center;">Type</div> </td><td style="white-space:nowrap; text-align:center;" id="TBL-91-1-2"
|
|
class="td11"> <div class="multicolumn" style="white-space:nowrap; text-align:center;">Class</div> </td><td style="white-space:nowrap; text-align:center;" id="TBL-91-1-3"
|
|
class="td11"> <div class="multicolumn" style="white-space:nowrap; text-align:center;">Description</div>
|
|
</td></tr><tr
|
|
class="hline"><td><hr /></td><td><hr /></td><td><hr /></td></tr><tr
|
|
style="vertical-align:baseline;" id="TBL-91-2-"><td style="white-space:nowrap; text-align:left;" id="TBL-91-2-1"
|
|
class="td11"> <span
|
|
class="pcrr7t-">Integer</span> </td><td style="white-space:nowrap; text-align:left;" id="TBL-91-2-2"
|
|
class="td11"> <span
|
|
class="pcrr7t-">Integral</span> </td><td style="white-space:nowrap; text-align:left;" id="TBL-91-2-3"
|
|
class="td11"> Arbitrary-precision integers </td>
|
|
</tr><tr
|
|
style="vertical-align:baseline;" id="TBL-91-3-"><td style="white-space:nowrap; text-align:left;" id="TBL-91-3-1"
|
|
class="td11"> <span
|
|
class="pcrr7t-">Int</span> </td><td style="white-space:nowrap; text-align:left;" id="TBL-91-3-2"
|
|
class="td11"> <span
|
|
class="pcrr7t-">Integral</span> </td><td style="white-space:nowrap; text-align:left;" id="TBL-91-3-3"
|
|
class="td11"> Fixed-precision integers </td>
|
|
</tr><tr
|
|
style="vertical-align:baseline;" id="TBL-91-4-"><td style="white-space:nowrap; text-align:left;" id="TBL-91-4-1"
|
|
class="td11"> <span
|
|
class="pcrr7t-">(Integral</span><span
|
|
class="pcrr7t-"> a)</span><span
|
|
class="pcrr7t-"> =></span><span
|
|
class="pcrr7t-"> Ratio</span><span
|
|
class="pcrr7t-"> a</span> </td><td style="white-space:nowrap; text-align:left;" id="TBL-91-4-2"
|
|
class="td11"> <span
|
|
class="pcrr7t-">RealFrac</span> </td><td style="white-space:nowrap; text-align:left;" id="TBL-91-4-3"
|
|
class="td11"> Rational numbers </td>
|
|
</tr><tr
|
|
style="vertical-align:baseline;" id="TBL-91-5-"><td style="white-space:nowrap; text-align:left;" id="TBL-91-5-1"
|
|
class="td11"> <span
|
|
class="pcrr7t-">Float</span> </td><td style="white-space:nowrap; text-align:left;" id="TBL-91-5-2"
|
|
class="td11"> <span
|
|
class="pcrr7t-">RealFloat</span> </td><td style="white-space:nowrap; text-align:left;" id="TBL-91-5-3"
|
|
class="td11"> Real floating-point, single precision </td>
|
|
</tr><tr
|
|
style="vertical-align:baseline;" id="TBL-91-6-"><td style="white-space:nowrap; text-align:left;" id="TBL-91-6-1"
|
|
class="td11"> <span
|
|
class="pcrr7t-">Double</span> </td><td style="white-space:nowrap; text-align:left;" id="TBL-91-6-2"
|
|
class="td11"> <span
|
|
class="pcrr7t-">RealFloat</span> </td><td style="white-space:nowrap; text-align:left;" id="TBL-91-6-3"
|
|
class="td11"> Real floating-point, double precision </td>
|
|
</tr><tr
|
|
style="vertical-align:baseline;" id="TBL-91-7-"><td style="white-space:nowrap; text-align:left;" id="TBL-91-7-1"
|
|
class="td11"> <span
|
|
class="pcrr7t-">(RealFloat</span><span
|
|
class="pcrr7t-"> a)</span><span
|
|
class="pcrr7t-"> =></span><span
|
|
class="pcrr7t-"> Complex</span><span
|
|
class="pcrr7t-"> a</span> </td><td style="white-space:nowrap; text-align:left;" id="TBL-91-7-2"
|
|
class="td11"> <span
|
|
class="pcrr7t-">Floating</span> </td><td style="white-space:nowrap; text-align:left;" id="TBL-91-7-3"
|
|
class="td11"> Complex floating-point </td>
|
|
</tr><tr
|
|
class="hline"><td><hr /></td><td><hr /></td><td><hr /></td></tr><tr
|
|
style="vertical-align:baseline;" id="TBL-91-8-"><td style="white-space:nowrap; text-align:left;" id="TBL-91-8-1"
|
|
class="td11"> </td></tr></table> </div>
|
|
<br /><div class="caption"
|
|
><span class="id">Table 6.1: </span><span
|
|
class="content">Standard Numeric Types</span></div><!--tex4ht:label?: x13-1350121 -->
|
|
<a
|
|
id="dx13-135013"></a>
|
|
|
|
|
|
|
|
</div><hr class="endfloat" />
|
|
</div>
|
|
<p class="noindent"> The standard numeric classes and other numeric functions defined in the Prelude are shown in Figures <a
|
|
href="#x13-1350142">6.2<!--tex4ht:ref: basic-numeric-1 --></a>–<a
|
|
href="#x13-1350573">6.3<!--tex4ht:ref: basic-numeric-2 --></a>.
|
|
Figure <a
|
|
href="#x13-1270011">6.1<!--tex4ht:ref: standard-classes --></a> shows the class dependencies and built-in types that are instances of the numeric classes.
|
|
<hr class="figure" /><div class="figure"
|
|
>
|
|
|
|
|
|
|
|
<a
|
|
id="x13-1350142"></a>
|
|
|
|
|
|
|
|
<div class="center"
|
|
>
|
|
<p class="noindent">
|
|
<div class="fbox"><div class="minipage"><div class="verbatim" id="verbatim-124">
|
|
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
|
|
</div>
|
|
<p class="noindent"> </div></div>
|
|
</div>
|
|
<br /><div class="caption"
|
|
><span class="id">Figure 6.2: </span><span
|
|
class="content">Standard Numeric Classes and Related Operations, Part 1</span></div><!--tex4ht:label?: x13-1350142 -->
|
|
<a
|
|
id="dx13-135015"></a>
|
|
<a
|
|
id="dx13-135016"></a>
|
|
<a
|
|
id="dx13-135017"></a>
|
|
<a
|
|
id="dx13-135018"></a>
|
|
<a
|
|
id="dx13-135019"></a>
|
|
<a
|
|
id="dx13-135020"></a>
|
|
<a
|
|
id="dx13-135021"></a>
|
|
<a
|
|
id="dx13-135022"></a>
|
|
<a
|
|
id="dx13-135023"></a>
|
|
<a
|
|
id="dx13-135024"></a>
|
|
<a
|
|
id="dx13-135025"></a>
|
|
<a
|
|
id="dx13-135026"></a>
|
|
<a
|
|
id="dx13-135027"></a>
|
|
<a
|
|
id="dx13-135028"></a>
|
|
<a
|
|
id="dx13-135029"></a>
|
|
|
|
|
|
|
|
<a
|
|
id="dx13-135030"></a>
|
|
<a
|
|
id="dx13-135031"></a>
|
|
<a
|
|
id="dx13-135032"></a>
|
|
<a
|
|
id="dx13-135033"></a>
|
|
<a
|
|
id="dx13-135034"></a>
|
|
<a
|
|
id="dx13-135035"></a>
|
|
<a
|
|
id="dx13-135036"></a>
|
|
<a
|
|
id="dx13-135037"></a>
|
|
<a
|
|
id="dx13-135038"></a>
|
|
<a
|
|
id="dx13-135039"></a>
|
|
<a
|
|
id="dx13-135040"></a>
|
|
<a
|
|
id="dx13-135041"></a>
|
|
<a
|
|
id="dx13-135042"></a>
|
|
<a
|
|
id="dx13-135043"></a>
|
|
<a
|
|
id="dx13-135044"></a>
|
|
<a
|
|
id="dx13-135045"></a>
|
|
<a
|
|
id="dx13-135046"></a>
|
|
<a
|
|
id="dx13-135047"></a>
|
|
<a
|
|
id="dx13-135048"></a>
|
|
<a
|
|
id="dx13-135049"></a>
|
|
<a
|
|
id="dx13-135050"></a>
|
|
<a
|
|
id="dx13-135051"></a>
|
|
<a
|
|
id="dx13-135052"></a>
|
|
<a
|
|
id="dx13-135053"></a>
|
|
<a
|
|
id="dx13-135054"></a>
|
|
<a
|
|
id="dx13-135055"></a>
|
|
<a
|
|
id="dx13-135056"></a>
|
|
|
|
|
|
|
|
</div><hr class="endfigure" />
|
|
<hr class="figure" /><div class="figure"
|
|
>
|
|
|
|
|
|
|
|
<a
|
|
id="x13-1350573"></a>
|
|
|
|
|
|
|
|
<div class="center"
|
|
>
|
|
<p class="noindent">
|
|
<div class="fbox"><div class="minipage"><div class="verbatim" id="verbatim-125">
|
|
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 />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 />fromIntegral :: (Integral a, Num b) => a -> b
|
|
 <br />realToFrac   :: (Real a, Fractional b) => a -> b
|
|
</div>
|
|
<p class="noindent"> </div></div>
|
|
</div>
|
|
<br /><div class="caption"
|
|
><span class="id">Figure 6.3: </span><span
|
|
class="content">Standard Numeric Classes and Related Operations, Part 2</span></div><!--tex4ht:label?: x13-1350573 -->
|
|
<a
|
|
id="dx13-135058"></a>
|
|
<a
|
|
id="dx13-135059"></a>
|
|
<a
|
|
id="dx13-135060"></a>
|
|
<a
|
|
id="dx13-135061"></a>
|
|
<a
|
|
id="dx13-135062"></a>
|
|
<a
|
|
id="dx13-135063"></a>
|
|
<a
|
|
id="dx13-135064"></a>
|
|
<a
|
|
id="dx13-135065"></a>
|
|
<a
|
|
id="dx13-135066"></a>
|
|
<a
|
|
id="dx13-135067"></a>
|
|
<a
|
|
id="dx13-135068"></a>
|
|
<a
|
|
id="dx13-135069"></a>
|
|
<a
|
|
id="dx13-135070"></a>
|
|
<a
|
|
id="dx13-135071"></a>
|
|
<a
|
|
id="dx13-135072"></a>
|
|
<a
|
|
id="dx13-135073"></a>
|
|
<a
|
|
id="dx13-135074"></a>
|
|
<a
|
|
id="dx13-135075"></a>
|
|
|
|
|
|
|
|
<a
|
|
id="dx13-135076"></a>
|
|
<a
|
|
id="dx13-135077"></a>
|
|
<a
|
|
id="dx13-135078"></a>
|
|
<a
|
|
id="dx13-135079"></a>
|
|
<a
|
|
id="dx13-135080"></a>
|
|
<a
|
|
id="dx13-135081"></a>
|
|
<a
|
|
id="dx13-135082"></a>
|
|
<a
|
|
id="dx13-135083"></a>
|
|
<a
|
|
id="dx13-135084"></a>
|
|
<a
|
|
id="dx13-135085"></a>
|
|
|
|
|
|
|
|
</div><hr class="endfigure" />
|
|
<h4 class="subsectionHead"><span class="titlemark">6.4.1 </span> <a
|
|
id="x13-1360006.4.1"></a>Numeric Literals</h4>
|
|
<p class="noindent"> The syntax of numeric literals is given in Section <a
|
|
href="haskellch2.html#x7-190002.5">2.5<!--tex4ht:ref: lexemes-numeric --></a>. An integer literal represents the application of the function
|
|
<span
|
|
class="pcrr7t-">fromInteger</span><a
|
|
id="dx13-136001"></a> to the appropriate value of type <span
|
|
class="pcrr7t-">Integer</span>. Similarly, a floating literal stands for an application
|
|
of <span
|
|
class="pcrr7t-">fromRational</span><a
|
|
id="dx13-136002"></a> to a value of type <span
|
|
class="pcrr7t-">Rational</span> (that is, <span
|
|
class="pcrr7t-">Ratio</span><span
|
|
class="pcrr7t-"> Integer</span>). Given the typings:
|
|
<div class="quote">
|
|
|
|
|
|
|
|
<div class="verbatim" id="verbatim-126">
|
|
fromInteger  :: (Num a) => Integer -> a
|
|
 <br />fromRational :: (Fractional a) => Rational -> a
|
|
</div>
|
|
<p class="noindent"></div>
|
|
<a
|
|
id="dx13-136003"></a>
|
|
<a
|
|
id="dx13-136004"></a>
|
|
<p class="noindent"> integer and floating literals have the typings <span
|
|
class="pcrr7t-">(Num</span><span
|
|
class="pcrr7t-"> a)</span><span
|
|
class="pcrr7t-"> =></span><span
|
|
class="pcrr7t-"> a</span> and <span
|
|
class="pcrr7t-">(Fractional</span><span
|
|
class="pcrr7t-"> a)</span><span
|
|
class="pcrr7t-"> =></span><span
|
|
class="pcrr7t-"> a</span>, 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="haskellch4.html#x10-790004.3.4">4.3.4<!--tex4ht:ref: default-decls --></a> for a discussion of overloading ambiguity.
|
|
<p class="noindent">
|
|
<h4 class="subsectionHead"><span class="titlemark">6.4.2 </span> <a
|
|
id="x13-1370006.4.2"></a>Arithmetic and Number-Theoretic Operations</h4>
|
|
<a
|
|
id="dx13-137001"></a>
|
|
<p class="noindent"> The infix class methods <span
|
|
class="pcrr7t-">(+)</span>, <a
|
|
id="dx13-137002"></a><span
|
|
class="pcrr7t-">(⋆)</span>, <a
|
|
id="dx13-137003"></a><span
|
|
class="pcrr7t-">(-)</span>, <a
|
|
id="dx13-137004"></a>and the unary function <span
|
|
class="pcrr7t-">negate</span><a
|
|
id="dx13-137005"></a> (which can also be written as a prefix
|
|
minus sign; see section <a
|
|
href="haskellch3.html#x8-280003.4">3.4<!--tex4ht:ref: operators --></a>) apply to all numbers. The class methods <span
|
|
class="pcrr7t-">quot</span><a
|
|
id="dx13-137006"></a>, <span
|
|
class="pcrr7t-">rem</span><a
|
|
id="dx13-137007"></a>, <span
|
|
class="pcrr7t-">div</span><a
|
|
id="dx13-137008"></a>, and <span
|
|
class="pcrr7t-">mod</span><a
|
|
id="dx13-137009"></a> apply only to
|
|
integral numbers, while the class method <span
|
|
class="pcrr7t-">(/)</span><a
|
|
id="dx13-137010"></a>applies only to fractional ones. The <span
|
|
class="pcrr7t-">quot</span>, <span
|
|
class="pcrr7t-">rem</span>, <span
|
|
class="pcrr7t-">div</span>, and <span
|
|
class="pcrr7t-">mod</span> class
|
|
methods satisfy these laws if <span
|
|
class="pcrr7t-">y</span> is non-zero:
|
|
<div class="quote">
|
|
|
|
|
|
|
|
<div class="verbatim" id="verbatim-127">
|
|
(x ‘quot‘ y)⋆y + (x ‘rem‘ y) == x
|
|
 <br />(x ‘div‘  y)⋆y + (x ‘mod‘ y) == x
|
|
</div>
|
|
<p class="noindent"></div>
|
|
<p class="noindent"><span
|
|
class="pcrr7t-">‘quot‘</span> is integer division truncated toward zero, while the result of <span
|
|
class="pcrr7t-">‘div‘</span> is truncated toward negative infinity.
|
|
The <span
|
|
class="pcrr7t-">quotRem</span> class method takes a dividend and a divisor as arguments and returns a (quotient, remainder) pair;
|
|
<span
|
|
class="pcrr7t-">divMod</span> is defined similarly:
|
|
<div class="quote">
|
|
|
|
|
|
|
|
<div class="verbatim" id="verbatim-128">
|
|
quotRem x y  =  (x ‘quot‘ y, x ‘rem‘ y)
|
|
 <br />divMod  x y  =  (x ‘div‘  y, x ‘mod‘ y)
|
|
</div>
|
|
<p class="noindent"></div>
|
|
<p class="noindent"> Also available on integral numbers are the even and odd predicates:
|
|
<div class="quote">
|
|
|
|
|
|
|
|
<div class="verbatim" id="verbatim-129">
|
|
even x =  x ‘rem‘ 2 == 0
|
|
 <br />odd    =  not . even
|
|
</div>
|
|
<p class="noindent"></div>
|
|
<a
|
|
id="dx13-137011"></a>
|
|
<a
|
|
id="dx13-137012"></a>
|
|
<p class="noindent"> Finally, there are the greatest common divisor and least common multiple functions. <span
|
|
class="pcrr7t-">gcd</span><a
|
|
id="dx13-137013"></a> <span
|
|
class="cmmi-10">x</span> <span
|
|
class="cmmi-10">y</span> is the greatest
|
|
(positive) integer that divides both <span
|
|
class="cmmi-10">x</span> and <span
|
|
class="cmmi-10">y</span>; for example <span
|
|
class="pcrr7t-">gcd</span><span
|
|
class="pcrr7t-"> (-3)</span><span
|
|
class="pcrr7t-"> 6</span> = <span
|
|
class="pcrr7t-">3</span>, <span
|
|
class="pcrr7t-">gcd</span><span
|
|
class="pcrr7t-"> (-3)</span><span
|
|
class="pcrr7t-"> (-6)</span> = <span
|
|
class="pcrr7t-">3</span>, <span
|
|
class="pcrr7t-">gcd</span><span
|
|
class="pcrr7t-"> 0</span><span
|
|
class="pcrr7t-"> 4</span> = <span
|
|
class="pcrr7t-">4</span>.
|
|
<span
|
|
class="pcrr7t-">gcd</span><span
|
|
class="pcrr7t-"> 0</span><span
|
|
class="pcrr7t-"> 0</span> raises a runtime error.
|
|
<p class="noindent"> <span
|
|
class="pcrr7t-">lcm</span><a
|
|
id="dx13-137014"></a> <span
|
|
class="cmmi-10">x</span> <span
|
|
class="cmmi-10">y</span> is the smallest positive integer that both <span
|
|
class="cmmi-10">x</span> and <span
|
|
class="cmmi-10">y</span> divide.
|
|
<p class="noindent">
|
|
<h4 class="subsectionHead"><span class="titlemark">6.4.3 </span> <a
|
|
id="x13-1380006.4.3"></a>Exponentiation and Logarithms</h4>
|
|
<a
|
|
id="dx13-138001"></a>
|
|
<a
|
|
id="dx13-138002"></a>
|
|
<p class="noindent"> The one-argument exponential function <span
|
|
class="pcrr7t-">exp</span><a
|
|
id="dx13-138003"></a> and the logarithm function <span
|
|
class="pcrr7t-">log</span><a
|
|
id="dx13-138004"></a> act on floating-point numbers and use
|
|
base <span
|
|
class="cmmi-10">e</span>. <span
|
|
class="pcrr7t-">logBase</span><a
|
|
id="dx13-138005"></a> <span
|
|
class="cmmi-10">a</span> <span
|
|
class="cmmi-10">x</span> returns the logarithm of <span
|
|
class="cmmi-10">x</span> in base <span
|
|
class="cmmi-10">a</span>. <span
|
|
class="pcrr7t-">sqrt</span><a
|
|
id="dx13-138006"></a> returns the principal square root of a
|
|
floating-point number. There are three two-argument exponentiation operations: <span
|
|
class="pcrr7t-">(^)</span><a
|
|
id="dx13-138007"></a> raises any number to a
|
|
nonnegative integer power, <span
|
|
class="pcrr7t-">(^^)</span><a
|
|
id="dx13-138008"></a> raises a fractional number to any integer power, and <span
|
|
class="pcrr7t-">(⋆⋆)</span><a
|
|
id="dx13-138009"></a>takes two
|
|
floating-point arguments. The value of <span
|
|
class="cmmi-10">x</span><span
|
|
class="pcrr7t-">^0</span> or <span
|
|
class="cmmi-10">x</span><span
|
|
class="pcrr7t-">^^0</span> is <span
|
|
class="pcrr7t-">1</span> for any <span
|
|
class="cmmi-10">x</span>, including zero; <span
|
|
class="pcrr7t-">0⋆⋆</span><span
|
|
class="cmmi-10">y</span> is <span
|
|
class="pcrr7t-">1</span> if <span
|
|
class="cmmi-10">y</span> is <span
|
|
class="pcrr7t-">1</span>, and <span
|
|
class="pcrr7t-">0</span>
|
|
otherwise.
|
|
<p class="noindent">
|
|
<h4 class="subsectionHead"><span class="titlemark">6.4.4 </span> <a
|
|
id="x13-1390006.4.4"></a>Magnitude and Sign</h4>
|
|
<a
|
|
id="dx13-139001"></a>
|
|
<a
|
|
id="dx13-139002"></a>
|
|
<p class="noindent"> A number has a <span
|
|
class="ptmri7t-">magnitude </span>and a <span
|
|
class="ptmri7t-">sign</span>. The functions <span
|
|
class="pcrr7t-">abs</span><a
|
|
id="dx13-139003"></a> and <span
|
|
class="pcrr7t-">signum</span><a
|
|
id="dx13-139004"></a> apply to any number and satisfy the law:
|
|
<div class="quote">
|
|
|
|
|
|
|
|
<div class="verbatim" id="verbatim-130">
|
|
abs x ⋆ signum x == x
|
|
</div>
|
|
<p class="noindent"></div>
|
|
<p class="noindent"> For real numbers, these functions are defined by:
|
|
<div class="quote">
|
|
|
|
|
|
|
|
<div class="verbatim" id="verbatim-131">
|
|
abs x    | x >= 0  = x
|
|
 <br />         | x <  0  = -x
|
|
 <br />
|
|
 <br />signum x | x >  0  = 1
|
|
 <br />         | x == 0  = 0
|
|
 <br />         | x <  0  = -1
|
|
</div>
|
|
<p class="noindent"></div>
|
|
<p class="noindent">
|
|
<h4 class="subsectionHead"><span class="titlemark">6.4.5 </span> <a
|
|
id="x13-1400006.4.5"></a>Trigonometric Functions</h4>
|
|
<a
|
|
id="dx13-140001"></a>
|
|
<p class="noindent"> Class <span
|
|
class="pcrr7t-">Floating</span> provides the circular and hyperbolic sine<a
|
|
id="dx13-140002"></a>, cosine<a
|
|
id="dx13-140003"></a>, and tangent<a
|
|
id="dx13-140004"></a> functions and their inverses. Default
|
|
implementations of <span
|
|
class="pcrr7t-">tan</span>, <span
|
|
class="pcrr7t-">tanh</span>, <span
|
|
class="pcrr7t-">logBase</span>, <span
|
|
class="pcrr7t-">⋆⋆</span>, and <span
|
|
class="pcrr7t-">sqrt</span> are provided, but implementors are free to provide more
|
|
accurate implementations.
|
|
<p class="noindent"> Class <span
|
|
class="pcrr7t-">RealFloat</span> provides a version of arctangent taking two real floating-point arguments. For real floating <span
|
|
class="cmmi-10">x</span> and
|
|
<span
|
|
class="cmmi-10">y</span>, <span
|
|
class="pcrr7t-">atan2</span><a
|
|
id="dx13-140005"></a> <span
|
|
class="cmmi-10">y</span> <span
|
|
class="cmmi-10">x</span> computes the angle (from the positive x-axis) of the vector from the origin to the point <span
|
|
class="cmr-10">(</span><span
|
|
class="cmmi-10">x,y</span><span
|
|
class="cmr-10">)</span>. <span
|
|
class="pcrr7t-">atan2</span><a
|
|
id="dx13-140006"></a> <span
|
|
class="cmmi-10">y</span>
|
|
<span
|
|
class="cmmi-10">x</span> returns a value in the range <span
|
|
class="pcrr7t-">[-pi,</span><span
|
|
class="pcrr7t-"> pi]</span>. It follows the Common Lisp semantics for the origin when signed
|
|
zeroes are supported. <span
|
|
class="pcrr7t-">atan2</span> <span
|
|
class="cmmi-10">y</span> <span
|
|
class="pcrr7t-">1</span>, with <span
|
|
class="cmmi-10">y</span> in a type that is <span
|
|
class="pcrr7t-">RealFloat</span>, should return the same value as
|
|
<span
|
|
class="pcrr7t-">atan</span> <span
|
|
class="cmmi-10">y</span>. A default definition of <span
|
|
class="pcrr7t-">atan2</span> is provided, but implementors can provide a more accurate
|
|
implementation.
|
|
<p class="noindent"> The precise definition of the above functions is as in Common Lisp, which in turn follows Penfield’s proposal for
|
|
APL <span class="cite">[<a
|
|
href="haskellli3.html#Xpenfield:complex-apl">12</a>]</span>. See these references for discussions of branch cuts, discontinuities, and implementation.
|
|
<p class="noindent">
|
|
<h4 class="subsectionHead"><span class="titlemark">6.4.6 </span> <a
|
|
id="x13-1410006.4.6"></a>Coercions and Component Extraction</h4>
|
|
<a
|
|
id="dx13-141001"></a>
|
|
<p class="noindent"> The <span
|
|
class="pcrr7t-">ceiling</span><a
|
|
id="dx13-141002"></a>, <span
|
|
class="pcrr7t-">floor</span><a
|
|
id="dx13-141003"></a>, <span
|
|
class="pcrr7t-">truncate</span><a
|
|
id="dx13-141004"></a>, and <span
|
|
class="pcrr7t-">round</span><a
|
|
id="dx13-141005"></a> functions each take a real fractional argument and return an
|
|
integral result. <span
|
|
class="pcrr7t-">ceiling</span> <span
|
|
class="cmmi-10">x</span> returns the least integer not less than <span
|
|
class="cmmi-10">x</span>, and <span
|
|
class="pcrr7t-">floor</span> <span
|
|
class="cmmi-10">x</span>, the greatest integer not greater
|
|
than <span
|
|
class="cmmi-10">x</span>. <span
|
|
class="pcrr7t-">truncate</span> <span
|
|
class="cmmi-10">x</span> yields the integer nearest <span
|
|
class="cmmi-10">x</span> between <span
|
|
class="cmr-10">0</span> and <span
|
|
class="cmmi-10">x</span>, inclusive. <span
|
|
class="pcrr7t-">round</span> <span
|
|
class="cmmi-10">x</span> returns the nearest integer to
|
|
<span
|
|
class="cmmi-10">x</span>, the even integer if <span
|
|
class="cmmi-10">x</span> is equidistant between two integers.
|
|
<p class="noindent"> The function <span
|
|
class="pcrr7t-">properFraction</span><a
|
|
id="dx13-141006"></a> takes a real fractional number <span
|
|
class="cmmi-10">x</span> and returns a pair <span
|
|
class="cmr-10">(</span><span
|
|
class="cmmi-10">n,f</span><span
|
|
class="cmr-10">)</span> such that <span
|
|
class="cmmi-10">x</span><span
|
|
class="cmmi-10">  </span><span
|
|
class="cmr-10">=</span> <span
|
|
class="cmmi-10"> n </span><span
|
|
class="cmr-10">+ </span><span
|
|
class="cmmi-10">f</span>,
|
|
and: <span
|
|
class="cmmi-10">n</span> is an integral number with the same sign as <span
|
|
class="cmmi-10">x</span>; and <span
|
|
class="cmmi-10">f</span> is a fraction <span
|
|
class="cmmi-10">f</span> with the same type and sign as <span
|
|
class="cmmi-10">x</span>, and with
|
|
absolute value less than 1. The <span
|
|
class="pcrr7t-">ceiling</span>, <span
|
|
class="pcrr7t-">floor</span>, <span
|
|
class="pcrr7t-">truncate</span>, and <span
|
|
class="pcrr7t-">round</span> functions can be defined in terms of
|
|
<span
|
|
class="pcrr7t-">properFraction</span>.
|
|
|
|
|
|
|
|
<p class="noindent"> Two functions convert numbers to type <span
|
|
class="pcrr7t-">Rational</span>: <span
|
|
class="pcrr7t-">toRational</span><a
|
|
id="dx13-141007"></a> returns the rational equivalent of its real
|
|
argument with full precision; <span
|
|
class="pcrr7t-">approxRational</span><a
|
|
id="dx13-141008"></a> takes two real fractional arguments <span
|
|
class="cmmi-10">x</span> and <span
|
|
class="cmmi-10">ϵ</span> and returns the
|
|
simplest rational number within <span
|
|
class="cmmi-10">ϵ</span> of <span
|
|
class="cmmi-10">x</span>, where a rational <span
|
|
class="cmmi-10">p∕q </span>in reduced form is <span
|
|
class="ptmri7t-">simpler </span>than another <span
|
|
class="cmmi-10">p</span><sup><span
|
|
class="cmsy-7">′</span></sup><span
|
|
class="cmmi-10">∕q</span><sup><span
|
|
class="cmsy-7">′</span></sup> if <span
|
|
class="cmsy-10">|</span><span
|
|
class="cmmi-10">p</span><span
|
|
class="cmsy-10">|≤|</span><span
|
|
class="cmmi-10">p</span><sup><span
|
|
class="cmsy-7">′</span></sup><span
|
|
class="cmsy-10">|</span>
|
|
and <span
|
|
class="cmmi-10">q </span><span
|
|
class="cmsy-10">≤ </span><span
|
|
class="cmmi-10">q</span><sup><span
|
|
class="cmsy-7">′</span></sup>. Every real interval contains a unique simplest rational; in particular, note that <span
|
|
class="cmr-10">0</span><span
|
|
class="cmmi-10">∕</span><span
|
|
class="cmr-10">1 </span>is the simplest rational
|
|
of all.
|
|
<p class="noindent"> The class methods of class <span
|
|
class="pcrr7t-">RealFloat</span><a
|
|
id="dx13-141009"></a> allow efficient, machine-independent access to the components of a
|
|
floating-point number. The functions <span
|
|
class="pcrr7t-">floatRadix</span><a
|
|
id="dx13-141010"></a>, <span
|
|
class="pcrr7t-">floatDigits</span><a
|
|
id="dx13-141011"></a>, and <span
|
|
class="pcrr7t-">floatRange</span><a
|
|
id="dx13-141012"></a> 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 <span
|
|
class="pcrr7t-">decodeFloat</span><a
|
|
id="dx13-141013"></a> applied to a real
|
|
floating-point number returns the significand expressed as an <span
|
|
class="pcrr7t-">Integer</span> and an appropriately scaled
|
|
exponent (an <span
|
|
class="pcrr7t-">Int</span>). If <span
|
|
class="pcrr7t-">decodeFloat</span><span
|
|
class="pcrr7t-"> x</span> yields <span
|
|
class="pcrr7t-">(</span><span
|
|
class="cmmi-10">m</span><span
|
|
class="pcrr7t-">,</span><span
|
|
class="cmmi-10">n</span><span
|
|
class="pcrr7t-">)</span>, then <span
|
|
class="pcrr7t-">x</span> is equal in value to <span
|
|
class="cmmi-10">mb</span><sup><span
|
|
class="cmmi-7">n</span></sup>, where <span
|
|
class="cmmi-10">b</span> is the
|
|
floating-point radix, and furthermore, either <span
|
|
class="cmmi-10">m</span> and <span
|
|
class="cmmi-10">n</span> are both zero or else <span
|
|
class="cmmi-10">b</span><sup><span
|
|
class="cmmi-7">d</span><span
|
|
class="cmsy-7">−</span><span
|
|
class="cmr-7">1</span></sup> <span
|
|
class="cmsy-10">≤|</span><span
|
|
class="cmmi-10">m</span><span
|
|
class="cmsy-10">| </span><span
|
|
class="cmmi-10">< b</span><sup><span
|
|
class="cmmi-7">d</span></sup>, where <span
|
|
class="cmmi-10">d</span> is the
|
|
value of <span
|
|
class="pcrr7t-">floatDigits</span><span
|
|
class="pcrr7t-"> x</span>. <span
|
|
class="pcrr7t-">encodeFloat</span><a
|
|
id="dx13-141014"></a> performs the inverse of this transformation. The functions
|
|
<span
|
|
class="pcrr7t-">significand</span><a
|
|
id="dx13-141015"></a> and <span
|
|
class="pcrr7t-">exponent</span><a
|
|
id="dx13-141016"></a> together provide the same information as <span
|
|
class="pcrr7t-">decodeFloat</span>, but rather than an
|
|
<span
|
|
class="pcrr7t-">Integer</span>, <span
|
|
class="pcrr7t-">significand</span><span
|
|
class="pcrr7t-"> x</span> yields a value of the same type as <span
|
|
class="pcrr7t-">x</span>, scaled to lie in the open interval <span
|
|
class="cmr-10">(</span><span
|
|
class="cmsy-10">−</span><span
|
|
class="cmr-10">1</span><span
|
|
class="cmmi-10">,</span><span
|
|
class="cmr-10">1)</span>.
|
|
<span
|
|
class="pcrr7t-">exponent</span><span
|
|
class="pcrr7t-"> 0</span> is zero. <span
|
|
class="pcrr7t-">scaleFloat</span> multiplies a floating-point number by an integer power of the
|
|
radix.
|
|
<p class="noindent"> The functions <span
|
|
class="pcrr7t-">isNaN</span>, <span
|
|
class="pcrr7t-">isInfinite</span>, <span
|
|
class="pcrr7t-">isDenormalized</span>, <span
|
|
class="pcrr7t-">isNegativeZero</span>, and <span
|
|
class="pcrr7t-">isIEEE</span> all support
|
|
numbers represented using the IEEE standard. For non-IEEE floating point numbers, these may all return
|
|
false.
|
|
<p class="noindent"> Also available are the following coercion functions:
|
|
<div class="quote">
|
|
|
|
|
|
|
|
<div class="verbatim" id="verbatim-132">
|
|
fromIntegral :: (Integral a, Num b)    => a -> b
|
|
 <br />realToFrac   :: (Real a, Fractional b) => a -> b
|
|
</div>
|
|
<p class="noindent"></div>
|
|
<a
|
|
id="dx13-141017"></a>
|
|
<a
|
|
id="dx13-141018"></a>
|
|
|
|
|
|
|
|
|
|
|
|
|
|
<!--l. 11--><div class="crosslinks"><p class="noindent">[<a
|
|
href="haskellch7.html" >next</a>] [<a
|
|
href="haskellch5.html" >prev</a>] [<a
|
|
href="haskellch5.html#tailhaskellch5.html" >prev-tail</a>] [<a
|
|
href="haskellch6.html" >front</a>] [<a
|
|
href="haskellpa1.html#haskellch6.html" >up</a>] </p></div>
|
|
<p class="noindent"> <a
|
|
id="tailhaskellch6.html"></a>
|
|
</body></html>
|