小壁虎进家有什么预兆| 吃什么水果| 什么叫蛇胆疮| 滴水不漏是什么生肖| 张学良为什么叫小六子| 为什么土豆不能炒鸡蛋| 篮子是什么意思| 马后面是什么生肖| 甲状腺肿物是什么意思| 人生观价值观世界观是什么意思| 冬虫虫念什么| 腿痛挂什么科| 附带是什么意思| 九死一生是指什么生肖| 凭什么我买单| 鹦鹉能吃什么| 这个季节种什么菜合适| 昏什么昏什么| 乾卦代表什么| 古代警察叫什么| 快乐的反义词是什么| 新奇的什么| 白带是黄色是什么原因| 全糖是什么意思| 北京朝阳医院擅长什么| 消化不好吃什么药| 风热感冒什么症状| 激素六项什么时候查最准| 下肢静脉曲张是什么原因引起的| 拔罐有什么作用| 食用葡萄糖是什么| jay是什么意思| 落花雨你飘摇的美丽是什么歌| 变爻是什么意思| 子宫肌瘤吃什么好| 溶栓是什么意思| hla一b27阳性是什么意思| 坐骨神经吃什么药效果最好| 阴道炎用什么药效果最好| 复方对乙酰氨基酚片是什么药| 心情烦躁吃什么药| 企业性质指的是什么| 胃酸烧心吃什么药可以根治| 面部脂溢性皮炎用什么药| 孕妇喝咖啡有什么危害| 松鼠尾巴有什么作用| ada是什么意思| 妇乐颗粒的功效能治什么病| 血糖高吃什么水果| 六月二十六是什么日子| 姝五行属什么| 头顶冒汗是什么原因| 什么食物含锌多| 黄瓜籽有什么功效| 精子长什么样| 牛杂是牛的什么部位| exo什么时候出道的| 佛法无边是什么意思| 什么的水| 三观是什么| 肚子胀屁多是什么原因| 喝黑苦荞茶有什么好处和坏处| 中山市有什么大学| 什么叫不动产| 承欢膝下什么意思| 为什么身上有红色的痣| 女人晚上盗汗是什么原因| 孜然是什么植物| 血小板减少吃什么能补回来| 50岁掉牙齿是什么原因| 中午吃什么饭| 眼压高用什么眼药水| 子宫脱落有什么症状| 嘴唇发麻什么病兆| 社保断交有什么影响| 健康管理是做什么的| 银杏属于什么植物| 眼压高滴什么眼药水| 什么是翻墙软件| 早上起来嘴苦口臭是什么原因| 什么地跑步| 摸不到心跳是什么情况| gmail是什么邮箱| 喉软骨发育不良有什么症状| 樟脑丸是干什么的| 尿隐血什么意思| 行李箱什么材质的好| 夏天有什么植物| 面包虫是什么| 72年属什么生肖| 卵巢黄体是什么意思| 流金铄石是什么意思| 秋葵补什么| 心脏在人体什么位置| 龙脉是什么意思| 沈阳有什么特产| gln是什么意思| 尿毒症是什么引起的| 拉姆什么意思| 叹服是什么意思| neighborhood什么意思| 禾字五行属什么的| vegan是什么意思| 羊传染人的病叫什么名| 楞头青是什么意思| 1985年属牛是什么命| 肺阳虚吃什么中成药| 腋下疼痛是什么原因| 喉咙痛上火吃什么药效果最好| 宫颈息肉不切除有什么危害| 汗臭和狐臭有什么区别怎么辨别| 心脏什么情况下需要支架| 桑葚有什么好处| 全身骨显像是查什么的| 翻白草长什么样| 女人身体发热预示什么| lsp什么意思| 一朝一夕是什么意思| 黑色柳丁是什么意思| 记忆力减退吃什么药效果好| 奶豆腐是什么| 什么的元帅| 樱桃有什么营养价值| 汉防己甲素片治什么病| 骨骼是什么意思| 骨髓穿刺能查出什么病| 血脂指的是什么| gravy是什么意思| 红花跟藏红花有什么区别| 918是什么星座| 脚心痒是什么原因引起的| pvd是什么材料| 午时银花露有什么功效| blackpink什么意思| prada什么牌子| 力争是什么意思| 脑部有结节意味着什么| 宫颈锥切术是什么意思| 右肺纤维灶是什么意思| 铁马是什么| 美尼尔综合征吃什么药| 阴虱长什么样子| 胰岛素高是什么原因| 一唱一和是什么生肖| 吃过榴莲不能吃什么| 软肋是什么意思| 6.28什么星座| 杜鹃花什么颜色| 青筋凸起是什么原因| 立夏吃什么| 属狗的幸运色是什么颜色| 看望病人送什么花合适| 以爱之名什么意思| 无产阶级是什么意思| 外甥女是什么关系| 忌讳什么意思| 小儿抽搐是什么原因引起的| 什么叫密度| 大什么一什么| 妊娠线什么时候长| 路怒症是什么| 安徽菜属于什么菜系| 补牙挂什么科| 免疫球蛋白是什么东西| 林可霉素主治什么病| 天是什么结构的字| whatsapp是什么软件| 菜板什么材质的好| 胆固醇和血脂有什么区别| 什么是五常大米| 什么是手足口病| 臭粉是什么东西| 血压什么时候最高| av是什么| 前列腺回声欠均匀什么意思| 四季豆不能和什么一起吃| 梦到和男朋友分手是什么征兆| 吃什么补铁快| 什么情况下需要做肠镜| 胃体隆起是什么意思| 什么是富氢水| 神经性耳鸣有什么症状| 眼袋浮肿是什么原因| 肚脐下三寸是什么位置| 眼镜轴位是什么| 什么山没有石头| 白醋泡脚有什么好处| 抗核抗体是检查什么的| 上火喝什么饮料| 拿的起放的下是什么意思| 现在什么年| 他乡遇故知什么意思| 扁桃体发炎不能吃什么东西| 什么叫台风| 中国姓什么的人最多| 牙痛 吃什么药| 大姨夫是什么| 打美国电话前面加什么| 官印是什么意思| 为什么一直睡不着| 界定是什么意思| 碱吃多了有什么危害| 87年五行属什么| 两岁宝宝拉肚子吃什么药| 同舟共济是什么意思| 跳蚤咬了擦什么药最好| 舌苔厚白用什么泡水喝| 常打嗝是什么原因| 8月23日是什么星座| 血糖高喝什么牛奶好| 陈五行属什么| 腐叶土是什么土| 眉州东坡是什么菜系| 什么的身影| 什么是ok镜| 什么验孕棒准确率高| 尿多什么原因| 为什么会有蚊子| 幽门螺杆菌阳性什么意思| 梦见和邻居吵架什么预兆| 右边脸颊长痘是什么原因| 成年人改名字需要什么手续| 1991年属什么| 跌打损伤用什么药好得快| 大千是什么意思| 浪荡闲游是什么生肖| 参拜是什么意思| 血糖低吃什么补的最快| 尿是什么味道| 什么是色盲| md是什么牌子| 山峰是什么意思| 心绞痛吃什么药最好| 庆大霉素治疗鱼什么病| 咳嗽吃什么能治好| 妈妈弟弟的儿子叫什么| 43岁属什么生肖| 看静脉曲张挂什么科| 宫颈醋酸白色上皮是什么病变| 白羊座男和什么星座最配| 去威海玩需要准备什么| 蜻蜓吃什么食物| 胜造七级浮屠是什么意思| 泰五行属什么| 焦虑症是什么意思| 玫瑰花有什么作用| 美味佳肴是什么意思| 磨牙是什么原因怎么治疗| 天天晚上睡觉做梦是什么原因| 什么通便效果最快最好| 血脂高吃什么能降下来| 道是什么意思| 绌是什么意思| 梅毒检查挂什么科| 四五月份是什么星座| 用盐洗脸有什么好处| 中二病的意思是什么| 大便带油花是什么原因| 糜烂性胃炎有什么症状| 高血压高血糖能吃什么水果| 马冬梅是什么梗| 骨髓瘤是什么病| 眼睑痉挛挂什么科| 牙周炎用什么药最见效| 百度

Chapter 6
Predefined Types and Classes

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 9. Other predefined types such as arrays, complex numbers, and rationals are defined in Part II.

6.1 Standard Haskell Types

These types are defined by the Haskell Prelude. Numeric types are described in Section 6.4. 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.

6.1.1 Booleans

data  Bool  =  False | True deriving  
                             (Read, Show, Eq, Ord, Enum, Bounded)

The boolean type Bool is an enumeration. The basic boolean functions are && (and), || (or), and not. The name otherwise is defined as True to make guarded expressions more readable.

6.1.2 Characters and Strings

The character type Char is an enumeration whose values represent Unicode characters [2]. The lexical syntax for characters is defined in Section 2.6; character literals are nullary constructors in the datatype Char. Type Char is an instance of the classes Read, Show, Eq, Ord, Enum, and Bounded. The toEnum and fromEnum functions, standard functions from class Enum, map characters to and from the Int type.

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

A string is a list of characters:

type  String  =  [Char]

Strings may be abbreviated using the lexical syntax described in Section 2.6. For example, "A string" abbreviates [ 'A',' ','s','t','r', 'i','n','g']

6.1.3 Lists

data  [a]  =  [] | a : [a]  deriving (Eq, Ord)

Lists are an algebraic datatype of two constructors, although with special syntax, as described in Section 3.7. The first constructor is the null list, written ‘[]’ (“nil”), and the second is ‘:’ (“cons”). The module PreludeList (see Section 9.1) defines many standard list functions. Arithmetic sequences and list comprehensions, two convenient syntaxes for special kinds of lists, are described in Sections 3.10 and 3.11, respectively. Lists are an instance of classes Read, Show, Eq, Ord, Monad, Functor, and MonadPlus.

6.1.4 Tuples

Tuples are algebraic datatypes with special syntax, as defined in Section 3.8. Each tuple type has a single constructor. All tuples are instances of Eq, Ord, Bounded, Read, and Show (provided, of course, that all their component types are).

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 Eq, Ord, Bounded, Read, and Show. The Prelude and libraries define tuple functions such as zip for tuples up to a size of 7.

The constructor for a tuple is written by omitting the expressions surrounding the commas; thus (x,y) and (,) x y produce the same value. The same holds for tuple type constructors; thus, (Int,Bool,Int) and (,,) Int Bool Int denote the same type.

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

6.1.5 The Unit Datatype

data  () = () deriving (Eq, Ord, Bounded, Enum, Read, Show)

The unit datatype () has one non- member, the nullary constructor (). See also Section 3.9.

6.1.6 Function Types

Functions are an abstract type: no constructors directly create functional values. The following simple functions are found in the Prelude: id, const, (.), flip, ($), and until.

6.1.7 The IO and IOError Types

The IO type serves as a tag for operations (actions) that interact with the outside world. The IO type is abstract: no constructors are visible to the user. IO is an instance of the Monad and Functor classes. Chapter 7 describes I/O operations.

IOError is an abstract type representing errors raised by I/O operations. It is an instance of Show and Eq. Values of this type are constructed by the various I/O functions and are not presented in any further detail in this report. The Prelude contains a few I/O functions (defined in Section 9.3), and Part II contains many more.

6.1.8 Other Types

data  Maybe a     =  Nothing | Just a  deriving (Eq, Ord, Read, Show)  
data  Either a b  =  Left a | Right b  deriving (Eq, Ord, Read, Show)  
data  Ordering    =  LT | EQ | GT deriving  
                                  (Eq, Ord, Bounded, Enum, Read, Show)

The Maybe type is an instance of classes Functor, Monad, and MonadPlus. The Ordering type is used by compare in the class Ord. The functions maybe and either are found in the Prelude.

6.2 Strict Evaluation

Function application in Haskell is non-strict; that is, a function argument is evaluated only when required. Sometimes it is desirable to force the evaluation of a value, using the seq function:

  seq :: a -> b -> b

The function seq is defined by the equations:

seq  b  =  
seq a b  =  b, if a  

seq is usually introduced to improve performance by avoiding unneeded laziness. Strict datatypes (see Section 4.2.1) are defined in terms of the $! operator. However, the provision of seq has important semantic consequences, because it is available at every type. As a consequence, is not the same as \x -> , since seq can be used to distinguish them. For the same reason, the existence of seq weakens Haskell’s parametricity properties.

The operator $! is strict (call-by-value) application, and is defined in terms of seq. The Prelude also defines the $ operator to perform non-strict application.

  infixr 0 $, $!  
  ($), ($!) :: (a -> b) -> a -> b  
  f $  x   =          f x  
  f $! x   =  x ‘seq‘ f x

The non-strict application operator $ may appear redundant, since ordinary application (f x) means the same as (f $ x). However, $ has low, right-associative binding precedence, so it sometimes allows parentheses to be omitted; for example:

  f $ g $ h x  =  f (g (h x))

It is also useful in higher-order situations, such as map ($ 0) xs, or zipWith ($) fs xs.

6.3 Standard Haskell Classes

Figure 6.1 shows the hierarchy of Haskell classes defined in the Prelude and the Prelude types that are instances of these classes.


PIC

Figure 6.1: Standard Haskell Classes

Default class method declarations (Section 4.3) are provided for many of the methods in standard classes. A comment with each class declaration in Chapter 9 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.

6.3.1 The Eq Class

  class  Eq a  where  
        (==), (/=)  ::  a -> a -> Bool  
 
        x /= y  = not (x == y)  
        x == y  = not (x /= y)

The Eq class provides equality (==) and inequality (/=) methods. All basic datatypes except for functions and IO are instances of this class. Instances of Eq can be derived for any user-defined datatype whose constituents are also instances of Eq.

This declaration gives default method declarations for both /= and ==, each being defined in terms of the other. If an instance declaration for Eq defines neither == nor /=, then both will loop. If one is defined, the default method for the other will make use of the one that is defined. If both are defined, neither default method is used.

6.3.2 The Ord Class

  class  (Eq a) => Ord a  where  
    compare              :: a -> a -> Ordering  
    (<), (<=), (>=), (>) :: a -> a -> Bool  
    max, min             :: a -> a -> a  
 
    compare x y | x == y    = EQ  
                | x <= y    = LT  
                | otherwise = GT  
 
    x <= y  = compare x y /= GT  
    x <  y  = compare x y == LT  
    x >= y  = compare x y /= LT  
    x >  y  = compare x y == GT  
 
    -- Note that (min x y, max x y) = (x,y) or (y,x)  
    max x y | x <= y    =  y  
            | otherwise =  x  
    min x y | x <= y    =  x  
            | otherwise =  y

The Ord class is used for totally ordered datatypes. All basic datatypes except for functions, IO, and IOError, are instances of this class. Instances of Ord can be derived for any user-defined datatype whose constituent types are in Ord. The declared order of the constructors in the data declaration determines the ordering in derived Ord instances. The Ordering datatype allows a single comparison to determine the precise ordering of two objects.

The default declarations allow a user to create an Ord instance either with a type-specific compare function or with type-specific == and <= functions.

6.3.3 The Read and Show Classes

type  ReadS a = String -> [(a,String)]  
type  ShowS   = String -> String  
 
class  Read a  where  
    readsPrec :: Int -> ReadS a  
    readList  :: ReadS [a]  
    -- ... default decl for readList given in Prelude  
 
class  Show a  where  
    showsPrec :: Int -> a -> ShowS  
    show      :: a -> String  
    showList  :: [a] -> ShowS  
 
    showsPrec _ x s   = show x ++ s  
    show x            = showsPrec 0 x ""  
    -- ... default decl for showList given in Prelude

The Read and Show classes are used to convert values to or from strings. The Int argument to showsPrec and readsPrec gives the operator precedence of the enclosing context (see Section 11.4).

showsPrec and showList return a String-to-String function, to allow constant-time concatenation of its results using function composition. A specialised variant, show, is also provided, which uses precedence context zero, and returns an ordinary String. The method showList is provided to allow the programmer to give a specialised way of showing lists of values. This is particularly useful for the Char type, where values of type String should be shown in double quotes, rather than between square brackets.

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

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

For convenience, the Prelude provides the following auxiliary functions:

reads   :: (Read a) => ReadS a  
reads   =  readsPrec 0  
 
shows   :: (Show a) => a -> ShowS  
shows   =  showsPrec 0  
 
read    :: (Read a) => String -> a  
read s  =  case [x | (x,t) <- reads s, ("","") <- lex t] of  
              [x] -> x  
              []  -> error "PreludeText.read: no parse"  
              _   -> error "PreludeText.read: ambiguous parse"

shows and reads use a default precedence of 0. The read function reads input from a string, which must be completely consumed by the input process.

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

6.3.4 The Enum Class

class  Enum a  where  
    succ, pred     :: a -> a  
    toEnum         :: Int -> a  
    fromEnum       :: a -> Int  
    enumFrom       :: a -> [a]            -- [n..]  
    enumFromThen   :: a -> a -> [a]       -- [n,n'..]  
    enumFromTo     :: a -> a -> [a]       -- [n..m]  
    enumFromThenTo :: a -> a -> a -> [a]  -- [n,n'..m]  
 
    -- Default declarations given in Prelude

Class Enum defines operations on sequentially ordered types. The functions succ and pred return the successor and predecessor, respectively, of a value. The functions fromEnum and toEnum map values from a type in Enum to and from Int. The enumFrom... methods are used when translating arithmetic sequences (Section 3.10).

Instances of Enum may be derived for any enumeration type (types whose constructors have no fields); see Chapter 11.

For any type that is an instance of class Bounded as well as Enum, the following should hold:

The following Prelude types are instances of Enum:

For all four numeric types, succ adds 1, and pred subtracts 1. The conversions fromEnum and toEnum convert between the type and Int. In the case of Float and Double, the digits after the decimal point may be lost. It is implementation-dependent what fromEnum returns when applied to a value that is too large to fit in an Int.

For the types Int and Integer, the enumeration functions have the following meaning:

For Float and Double, the semantics of the enumFrom family is given by the rules for Int above, except that the list terminates when the elements become greater than e3 + i∕2 for positive increment i, or when they become less than e3 + i∕2 for negative i.

For all four of these Prelude numeric types, all of the enumFrom family of functions are strict in all their arguments.

6.3.5 The Functor Class

class  Functor f  where  
    fmap    :: (a -> b) -> f a -> f b

The Functor class is used for types that can be mapped over. Lists, IO, and Maybe are in this class.

Instances of Functor should satisfy the following laws:

fmap id
=
id
fmap (f . g)
=
fmap f . fmap g

All instances of Functor defined in the Prelude satisfy these laws.

6.3.6 The Monad Class

class  Monad m  where  
    (>>=)   :: m a -> (a -> m b) -> m b  
    (>>)    :: m a -> m b -> m b  
    return  :: a -> m a  
    fail    :: String -> m a  
 
    m >> k  =  m >>= \_ -> k  
    fail s  = error s

The Monad class defines the basic operations over a monad. See Chapter 7 for more information about monads.

do” expressions provide a convenient syntax for writing monadic expressions (see Section 3.14). The fail method is invoked on pattern-match failure in a do expression.

In the Prelude, lists, Maybe, and IO are all instances of Monad. The fail method for lists returns the empty list [], for Maybe returns Nothing, and for IO raises a user exception in the IO monad (see Section 7.3).

Instances of Monad should satisfy the following laws:

return a >>= k
=
k a
m >>= return
=
m
m >>= (\x -> k x >>= h)
=
(m >>= k) >>= h

Instances of both Monad and Functor should additionally satisfy the law:
fmap f xs
=
xs >>= return . f

All instances of Monad defined in the Prelude satisfy these laws.

The Prelude provides the following auxiliary functions:

sequence  :: Monad m => [m a] -> m [a]  
sequence_ :: Monad m => [m a] -> m ()  
mapM      :: Monad m => (a -> m b) -> [a] -> m [b]  
mapM_     :: Monad m => (a -> m b) -> [a] -> m ()  
(=<<)     :: Monad m => (a -> m b) -> m a -> m b

6.3.7 The Bounded Class

class  Bounded a  where  
    minBound, maxBound :: a

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

6.4 Numbers

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 6.1. The class Num of numeric types is a subclass of Eq, since all numbers may be compared for equality; its subclass Real is also a subclass of Ord, since the other comparison operations apply to all but complex numbers (defined in the Complex library). The class Integral contains integers of both limited and unlimited range; the class Fractional contains all non-integral types; and the class Floating contains all floating-point types, both real and complex.

The Prelude defines only the most basic numeric types: fixed sized integers (Int), arbitrary precision integers (Integer), single precision floating (Float), and double precision floating (Double). Other numeric types such as rationals and complex numbers are defined in libraries. In particular, the type Rational is a ratio of two Integer values, as defined in the Ratio library.

The default floating point operations defined by the Haskell Prelude do not conform to current language independent arithmetic (LIA) standards. These standards require considerably more complexity in the numeric structure and have thus been relegated to a library. Some, but not all, aspects of the IEEE floating point standard have been accounted for in Prelude class RealFloat.

The standard numeric types are listed in Table 6.1. The finite-precision integer type Int covers at least the range [   229, 229   1]. As Int is an instance of the Bounded class, maxBound and minBound can be used to determine the exact Int range defined by an implementation. Float is implementation-defined; it is desirable that this type be at least equal in range and precision to the IEEE single-precision type. Similarly, Double 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 (, semantically), a truncated value, or a special value such as infinity, indefinite, etc.





Type
Class
Description



Integer Integral Arbitrary-precision integers
Int Integral Fixed-precision integers
(Integral a) => Ratio a RealFrac Rational numbers
Float RealFloat Real floating-point, single precision
Double RealFloat Real floating-point, double precision
(RealFloat a) => Complex a Floating Complex floating-point




Table 6.1: Standard Numeric Types

The standard numeric classes and other numeric functions defined in the Prelude are shown in Figures 6.26.3. Figure 6.1 shows the class dependencies and built-in types that are instances of the numeric classes.


class  (Eq a, Show a) => Num a  where  
    (+), (-), (⋆)  :: a -> a -> a  
    negate         :: a -> a  
    abs, signum    :: a -> a  
    fromInteger    :: Integer -> a  
 
class  (Num a, Ord a) => Real a  where  
    toRational ::  a -> Rational  
 
class  (Real a, Enum a) => Integral a  where  
    quot, rem, div, mod :: a -> a -> a  
    quotRem, divMod     :: a -> a -> (a,a)  
    toInteger           :: a -> Integer  
 
class  (Num a) => Fractional a  where  
    (/)          :: a -> a -> a  
    recip        :: a -> a  
    fromRational :: Rational -> a  
 
class  (Fractional a) => Floating a  where  
    pi                  :: a  
    exp, log, sqrt      :: a -> a  
    (⋆⋆), logBase       :: a -> a -> a  
    sin, cos, tan       :: a -> a  
    asin, acos, atan    :: a -> a  
    sinh, cosh, tanh    :: a -> a  
    asinh, acosh, atanh :: a -> a


Figure 6.2: Standard Numeric Classes and Related Operations, Part 1


class  (Real a, Fractional a) => RealFrac a  where  
    properFraction   :: (Integral b) => a -> (b,a)  
    truncate, round  :: (Integral b) => a -> b  
    ceiling, floor   :: (Integral b) => a -> b  
 
class  (RealFrac a, Floating a) => RealFloat a  where  
    floatRadix          :: a -> Integer  
    floatDigits         :: a -> Int  
    floatRange          :: a -> (Int,Int)  
    decodeFloat         :: a -> (Integer,Int)  
    encodeFloat         :: Integer -> Int -> a  
    exponent            :: a -> Int  
    significand         :: a -> a  
    scaleFloat          :: Int -> a -> a  
    isNaN, isInfinite, isDenormalized, isNegativeZero, isIEEE  
                        :: a -> Bool  
    atan2               :: a -> a -> a  
 
gcd, lcm :: (Integral a) => a -> a-> a  
(^)      :: (Num a, Integral b) => a -> b -> a  
(^^)     :: (Fractional a, Integral b) => a -> b -> a  
 
fromIntegral :: (Integral a, Num b) => a -> b  
realToFrac   :: (Real a, Fractional b) => a -> b


Figure 6.3: Standard Numeric Classes and Related Operations, Part 2

6.4.1 Numeric Literals

The syntax of numeric literals is given in Section 2.5. An integer literal represents the application of the function fromInteger to the appropriate value of type Integer. Similarly, a floating literal stands for an application of fromRational to a value of type Rational (that is, Ratio Integer). Given the typings:

fromInteger  :: (Num a) => Integer -> a  
fromRational :: (Fractional a) => Rational -> a

integer and floating literals have the typings (Num a) => a and (Fractional a) => a, respectively. Numeric literals are defined in this indirect way so that they may be interpreted as values of any appropriate numeric type. See Section 4.3.4 for a discussion of overloading ambiguity.

6.4.2 Arithmetic and Number-Theoretic Operations

The infix class methods (+), (⋆), (-), and the unary function negate (which can also be written as a prefix minus sign; see section 3.4) apply to all numbers. The class methods quot, rem, div, and mod apply only to integral numbers, while the class method (/)applies only to fractional ones. The quot, rem, div, and mod class methods satisfy these laws if y is non-zero:

(x ‘quot‘ y)⋆y + (x ‘rem‘ y) == x  
(x ‘div‘  y)⋆y + (x ‘mod‘ y) == x

‘quot‘ is integer division truncated toward zero, while the result of ‘div‘ is truncated toward negative infinity. The quotRem class method takes a dividend and a divisor as arguments and returns a (quotient, remainder) pair; divMod is defined similarly:

quotRem x y  =  (x ‘quot‘ y, x ‘rem‘ y)  
divMod  x y  =  (x ‘div‘  y, x ‘mod‘ y)

Also available on integral numbers are the even and odd predicates:

even x =  x ‘rem‘ 2 == 0  
odd    =  not . even

Finally, there are the greatest common divisor and least common multiple functions. gcd x y is the greatest (positive) integer that divides both x and y; for example gcd (-3) 6 = 3, gcd (-3) (-6) = 3, gcd 0 4 = 4. gcd 0 0 raises a runtime error.

lcm x y is the smallest positive integer that both x and y divide.

6.4.3 Exponentiation and Logarithms

The one-argument exponential function exp and the logarithm function log act on floating-point numbers and use base e. logBase a x returns the logarithm of x in base a. sqrt returns the principal square root of a floating-point number. There are three two-argument exponentiation operations: (^) raises any number to a nonnegative integer power, (^^) raises a fractional number to any integer power, and (⋆⋆)takes two floating-point arguments. The value of x^0 or x^^0 is 1 for any x, including zero; 0⋆⋆y is 1 if y is 1, and 0 otherwise.

6.4.4 Magnitude and Sign

A number has a magnitude and a sign. The functions abs and signum apply to any number and satisfy the law:

abs x ⋆ signum x == x

For real numbers, these functions are defined by:

abs x    | x >= 0  = x  
         | x <  0  = -x  
 
signum x | x >  0  = 1  
         | x == 0  = 0  
         | x <  0  = -1

6.4.5 Trigonometric Functions

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

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

The precise definition of the above functions is as in Common Lisp, which in turn follows Penfield’s proposal for APL [12]. See these references for discussions of branch cuts, discontinuities, and implementation.

6.4.6 Coercions and Component Extraction

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

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

Two functions convert numbers to type Rational: toRational returns the rational equivalent of its real argument with full precision; approxRational takes two real fractional arguments x and ϵ and returns the simplest rational number within ϵ of x, where a rational p∕q in reduced form is simpler than another p∕q if |p|≤|p| and q q. Every real interval contains a unique simplest rational; in particular, note that 01 is the simplest rational of all.

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

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

Also available are the following coercion functions:

fromIntegral :: (Integral a, Num b)    => a -> b  
realToFrac   :: (Real a, Fractional b) => a -> b

乳钉的作用是什么 1974年属虎是什么命 肛门疼痛用什么药 吃什么可以提高免疫力和抵抗力 柳是什么生肖
气滞血瘀吃什么药 法会是什么意思 经常喝红茶有什么好处和坏处吗 血糖高的人应该吃什么食物最好 相安无事什么意思
右胳膊麻木是什么征兆 什么身什么骨 结膜炎什么症状 英国的全称是什么 盘古是一个什么样的人
关节发黑是什么原因 什么爱 茯苓是什么植物 拉姆藏语什么意思 鳄龟吃什么
颈动脉斑块吃什么药hcv8jop1ns4r.cn 纷纷扬扬是什么意思bysq.com 宫颈管短有什么症状hcv9jop2ns9r.cn 百福图挂在家里什么位置好hcv9jop6ns3r.cn r0lex是什么牌子手表96micro.com
戴芬是什么药dayuxmw.com 生日吃什么hcv9jop7ns3r.cn 可以组什么词语hcv8jop5ns6r.cn 吗丁啉有什么功效hcv7jop6ns9r.cn 农历六月十八是什么星座shenchushe.com
梦泪什么意思hcv9jop1ns5r.cn 麦乳精是什么东西hcv8jop0ns3r.cn 小孩咳嗽是什么原因引起的jasonfriends.com sad是什么意思jinxinzhichuang.com 久负盛名的负是什么意思hcv8jop5ns8r.cn
hiv是什么意思hcv8jop5ns7r.cn 丰富多腔的腔是什么意思hcv8jop8ns2r.cn 1977属什么生肖hcv8jop3ns1r.cn 下雨天穿什么衣服hcv9jop7ns0r.cn 咖啡喝多了有什么危害hcv7jop7ns1r.cn
百度