Haskell术语:类型与数据类型的含义,他们是同义词吗? [英] Haskell terminology: meaning of type vs. data type, are they synonyms?

查看:169
本文介绍了Haskell术语:类型与数据类型的含义,他们是同义词吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在阅读这本书:Haskell School of Expression和第56页,在第5章的开头,我读了术语多态数据类型和多态类型。



这两个术语是指相同的概念吗?

它们是同义词吗?

或者两者有什么不同?如果是,什么?

解决方案

类型(在Haskell中)是一种语法有意义地放在 :: 的右边,以便对 :: 左边的表达式进行分类。类型的每个语法成分本身都是由 kind 分类的,其中类型的类型(对表达式进行分类)是 * 。有些人很乐意使用type这个词来指代任何类型语法的组件,无论它是否允许它对表达式进行分类。



类型的语法可以通过各种声明形式进行扩展。


  1. 类型同义词,例如,键入Foo xyz = [x] - > IO(y,z),添加完整应用表单的类型组件 Foo x y z ,它们根据宏定义的定义公式扩展宏时尚。
  2. 一个数据声明,例如数据Goo xyz = ThisGoo x | ThatGoo(Goo yzx)为类型的语法引入了一个新的类型构造函数符号 Goo ,它用于构建数据构造函数生成的值的类型,这里 ThisGoo ThatGoo

  3. 一个 newtype 声明,例如 newtype Noo xyz = MkNoo(x,[y],z)使用与类型语法中的原始类型不同的现有类型的副本。

类型是 polymorphic ,如果它包含类型变量,可以用其他类型组件替换:通过多态类型分类的值可以是特殊的,用于类型变量的任何替换实例。例如。追加(++):: [a] - > [a] - > [a] 适用于其元素具有相同类型的列表,但任何类型都可以。带有多态类型的值通常被称为多态值。

有时,数据类型通常被用来表示由 data 声明。从这个意义上讲,所有数据类型都是类型,但并非所有类型都是数据类型。不是数据类型的类型的例子包括 IO() Int - > INT 。另外, Int 这个意义上不是数据类型:它是一个硬连线的原始类型。为了避免疑惑,有些人称这些类型为代数数据类型,因为构造函数给出了一个代数,意思是通过组合其他值来构建值的一系列操作。 多态数据类型是一个数据类型,其中包含类型变量,例如 [(a,Bool)] ,与 [Int ] 。有时候人们会谈论声明一个多态数据类型或者说 Maybe也许是是一个多态数据类型,当它们真的只是表示类型构造函数具有参数(并且可以因此可以用来形成多态类型):迂回地,我们声明一个多态数据类型,但不是任何旧的多态数据类型,而是一个应用于形式参数的类型构造函数)。

当然,按类型分类的所有第一类值在某种意义上都是数据,而在Haskell中,类型不用于分类不是一级值的任何东西,因此从松散的意义上讲,每个类型是一种数据类型。在有数据类型以外的事物(例如,Java中的方法)以外的语言中,区别变得更加有意义。

非正式用法通常在中间某处,而不是非常明确。人们通常在功能或流程与他们运作的东西(数据)之间进行某种区分。或者他们可能会认为数据是根据它们的制作方式来理解的(并且揭示它们的表示,例如通过模式匹配),而不是根据它们的使用方式来理解。 数据的最后一个用法与抽象数据类型的概念有点不协调,这是一种隐藏底层内容表示的类型。表示隐藏的抽象数据类型因此与表示 - 暴露代数数据类型的对比强烈,这就是为什么很遗憾ADT被随意用作两者的缩写。



恐怕,结果是模糊的。

I'm reading the book: Haskell School of Expression and on page 56, at the beginning of chapter 5, I read the terms "polymorphic data types" and "polymorphic types".

Are these two terms refer to the same concept?

Are they synonyms ?

Or is there any difference between the two? If yes, what?

解决方案

A type (in Haskell) is a piece of syntax which can meaningfully be put right of :: to classify an expression left of ::. Each syntactic component of a type is itself classified by a kind, where the kind of types (which classify expressions) is *. Some people are happy to use the word "type" to refer to any component of the type syntax, whether or not its kind allows it to classify expressions.

The syntax of types can be extended by various declaration forms.

  1. A type synonym, e.g., type Foo x y z = [x] -> IO (y, z), adds type components of fully applied form Foo x y z, which expand macro-fashion in accordance with their defining equation.
  2. A data declaration, e.g., data Goo x y z = ThisGoo x | ThatGoo (Goo y z x) introduces a fresh type constructor symbol Goo to the syntax of types, which is used to build the types which classify values generated by the data constructors, here ThisGoo and ThatGoo.
  3. A newtype declaration, e.g., newtype Noo x y z = MkNoo (x, [y], z) makes a copy of an existing type which is distinguished from the original in the syntax of types.

A type is polymorphic if it contains type variables which can be substituted with other type components: the values classified by polymorphic types can be specialized to any substitution instance of the type variables. E.g. append (++) :: [a] -> [a] -> [a] works on lists whose elements have the same type, but any type will do. Values with polymorphic types are often referred to as "polymorphic values".

Sometimes, "data type" is used to mean, quite simply, a type introduced by a data declaration. In this sense, all data types are types, but not all types are data types. Examples of types which are not data types include IO () and Int -> Int. Also, Int is not a data type in this sense: it is a hardwired primitive type. For the avoidance of doubt, some people call these types algebraic data types, because the constructors give an algebra, meaning "a bunch of operations for building values by combining other values". A "polymorphic data type" is a data type with type variables in it, such as [(a, Bool)], by contrast with [Int]. Sometimes people talk about "declaring a polymorphic data type" or say things like "Maybe is a polymorphic data type" when they really just mean that the type constructor has parameters (and can thus be used to form polymorphic types): pedantically, one does declare a polymorphic data type, but not any old polymorphic datatype, rather a type constructor applied to formal parameters).

Of course, all first-class values classified by types are in some sense "data", and in Haskell, types are not used to classify anything which is not a first-class value, so in that looser sense, every "type" is a "data type". The distinction becomes more meaningful in languages where there are things other than data which have types (e.g., methods in Java).

Informal usage is often somewhere in the middle and not very well defined. People are often driving at some sort of distinction between functions or processes and the sort of stuff (the "data") on which they operate. Or they might think of data as being "understood in terms of the way they're made" (and exposing their representation, e.g. by pattern matching) rather than "understood in terms of the way they're used". This last usage of "data" sits a little uncomfortably with the notion of an abstract data type, being a type which hides the representation of the underlying stuff. Representation-hiding abstract data types thus contrast rather strongly with representation-exposing algebraic data types, which is why it is rather unfortunate that "ADT" is casually used as an abbreviation for both.

The upshot, I'm afraid, is vague.

这篇关于Haskell术语:类型与数据类型的含义,他们是同义词吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆