Section: 2.4 Type Synonyms

第 2.4 节 类型同义词

Since type synonyms are part of the type language only, it's hard to write a program which shows what they do. Essentially, they are like macros for the type language. They can be used interchangeably with their definition:

由于类型同义词只属于类型语言的一部分,很难写一个程序来展示它们的

作用。本质上,它们就像类型语言的宏。它们可以和自己的定义互相替换

使用:

e1 :: String
e1 = "abc"
e2 :: [Char]   -- No different than String
e2 = e1

In the written tutorial the declaration of Addr is a data type declaration, not a synonym declaration. This shows that the data type declaration as well as a signature can reference a synonym.

在书面教程中,Addr 的声明是数据类型声明,而不是同义词声明。这说明

数据类型声明和类型签名一样,都可以引用同义词。

Section: 2.5 Built-in Types

第 2.5 节 内建类型

Tuples are an easy way of grouping a set of data values. Here are a few tuples. Note the consistancy in notation between the values and types.

元组是把一组数据值聚合在一起的简便方式。这里有几个元组。注意值和类型

之间记法的一致性。

e3 :: (Bool,Int)
e3 = (True,4)
e4 :: (Char,[Int],Char)
e4 = ('a',[1,2,3],'b')

Here's a function which returns the second component of a 3 tuple.

这是一个返回 3 元组第二个分量的函数。

second :: (a,b,c) -> b
second (a,b,c) = b

Try out second e3 and second e4 - what happens?

试一下 second e3 和 second e4——会发生什么?

Each different size of tuple is a completely distinct type. There is no general way to append two arbitrary tuples or randomly select the i'th component of an arbitrary tuple. Here's a function built using 2-tuples to represent intervals.

每种不同大小的元组都是完全不同类型的类型。不存在一种通用的方法来

拼接两个任意元组,或随机选取任意元组的第 i 个分量。下面是一个用

2 元组表示区间的函数。

Use a type synonym to represent homogeneous 2 tuples

用一个类型同义词来表示同质 2 元组:

type Interval a = (a,a)
containsInterval :: Interval Int -> Interval Int -> Bool
containsInterval (xmin,xmax) (ymin,ymax) = xmin <= ymin && xmax >= ymax
p1 :: Interval Int
p1 = (2,3)
p2 :: Interval Int
p2 = (1,4)
e5 = containsInterval p1 p2
e6 = containsInterval p2 p1

Here's a type declaration for a type isomorphic to lists:

下面是一个与列表同构的类型的类型声明:

data List a = Nil | Cons a (List a) deriving Show

Except for the notation, this is completely equivalent to ordinary lists in Haskell.

除了记法之外,它与 Haskell 中的普通列表完全等价。

length' :: List a -> Int
length' Nil = 0
length' (Cons x y) = 1 + length' y
e7 = length' (Cons 'a' (Cons 'b' (Cons 'c' Nil)))

It is hard to demonstrate much about the non-specialness of built-in types. However, here is a brief summary:

很难用示例充分说明内建类型的"非特殊性"。不过,这里给出一个简要总结:

Numbers and characters, such as 1, 2.2, or 'a', are the same as nullary type constructors.

数字和字符,如 1、2.2 或 'a',与零元类型构造器没有区别。

Lists have a special type constructor, [a] instead of List a, and an odd looking data constructor, []. The other data constructor, :, is not unusual, syntactically speaking. The notation [x,y] is just syntax for x:y:[] and "abc" for 'a' : 'b' : 'c' : [].

列表有一个特殊的类型构造器 [a](而不是 List a),以及一个看起来奇怪的

数据构造器 []。另一个数据构造器 : 从语法上讲并不"特殊"。记法 [x,y]

只是 x:y:[] 的语法糖,"abc" 则是 'a' : 'b' : 'c' : [] 的简写。

Tuples use a special syntax. In a type expression, a 2 tuple containing types a and be would be written (a,b) instead of using a prefix type constructor such as Tuple2 a b. This same notation is used to build tuple values: (1,2) would construct a 2 tuple containing the values 1 and 2.

元组使用了特殊的语法。在类型表达式中,包含类型 a 和 b 的 2 元组写作

(a,b),而不是使用前缀类型构造器如 Tuple2 a b。同样的记法也用于构造

元组值:(1,2) 构造出一个包含值 1 和 2 的 2 元组。