简体   繁体   English

为什么以下类型构造函数的参数少于数据构造函数?

[英]Why does the following type constructor take less arguments than the data constructor?

I have the following type declaration: 我有以下类型声明:

data Quadruple a b = Quad a a b b

(Some of you might recognize this declaration from Yet Another Haskell Tutorial ). (你们中的一些人可能会认识到另一个Haskell教程中的这个声明)。

I understand the code's intent to be: create a data type called Quadruple. 我理解代码的意图: 创建一个名为Quadruple的数据类型。 It has a single type constructor called Quad, which takes four values. 它有一个名为Quad的单一类型构造函数,它带有四个值。 The first two values must have the same type, and the last two values must have the same type. 前两个值必须具有相同的类型,并且后两个值必须具有相同的类型。

I am having trouble explaining to myself why the type constructor takes less arguments than the data constructor. 我无法向自己解释为什么类型构造函数的参数少于数据构造函数。 The only explanation I could conjure up is that Haskell is able to infer from the type declaration that if the first value is of type a, so is the second value, and if the third value is of type b, so is the fourth value. 我能想到的唯一解释是Haskell能够从类型声明中推断出,如果第一个值是a类型,那么第二个值也是如此,如果第三个值是b类型,那么第四个值也是如此。

Is this correct? 这个对吗? Am I missing something here? 我在这里错过了什么吗? I know Haskell has type inference so this makes sense, but I don't want to run with the wrong mental model. 我知道Haskell有类型推断所以这是有道理的,但我不想用错误的心理模型运行。 (Second time trying to learn Haskell). (第二次尝试学习Haskell)。

It might be clearer with using the type 使用该类型可能更清楚

q = Quad 1 2 "peter" "paul"

Here you can see there are 2 types in play, Int and String . 在这里你可以看到有两种类型, IntString So q is of the type Quadruple Int String . 所以qQuadruple Int String类型。

So there are 2 types in play: the number of type parameters of Quadruple . 因此有两种类型: Quadruple的类型参数数量。 And there are 4 values(/fields) needed for the value constructor: the number of arguments of the Quad value constructor. 值构造函数需要4个值(/ fields): Quad值构造函数的参数个数。

It's like with functions, where each formal parameter can be used more than once, like sqr x = x * x . 它就像函数一样,每个形式参数可以多次使用,比如sqr x = x * x

Same here: Quadruple is a type constructor: it constructs a new type when given two argument types, a and b . 同样在这里: Quadruple是一个类型构造函数:当给定两个参数类型ab时,它构造一个新类型。 So Quadruple Int Double is a type; 所以Quadruple Int Double是一种类型; Quadruple Int Int is another type. Quadruple Int Int是另一种类型。

How its values are created? 它的价值是如何创造的? By using a data constructor Quad with four argument values: a pair with one type and another pair with another type. 通过使用具有四个参数值的数据构造函数Quad :一对具有一种类型,另一对具有另一种类型。 This is what you are telling to Haskell with that definition. 这就是告诉Haskell的定义。 So yes, it will use this knowledge in inferring and checking the types. 所以是的,它会在推断和检查类型时使用这些知识。

I think you are right - the left hand side only declares the type-variables that can be used on the right hand side 1 . 我认为你是对的 - 左侧只声明可以在右侧使用的类型变量1

So all of the following are valid: 所以以下所有内容都是有效的:

data A = A Int

has no variable on the LHS => none on the RHS 在RHS上没有LHS => none的变量

data B b = B1 Int | B2 b

has one variable on the LHS and uses this in one of it's constructors on the RHS; 在LHS上有一个变量,并在RHS的一个构造函数中使用它; note you don't need to use the variable at all - this is called phantom type. 请注意,您根本不需要使用变量 - 这称为幻像类型。

data C c = C Int

which is still something useful. 这还是有用的东西。

1 : except for existential types 1 :除了存在类型

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 arguments 对数据构造函数的约束 - Constraints on arguments to data constructor Haskell中数据构造函数和Type构造函数的构造函数的含义 - The meaning of constructor in data constructor and Type constructor in Haskell 收集某种类型的构造函数的参数 - Collecting the arguments of a constructor for some type 为什么在Haskell中,除了类型构造函数外,还要有一个值构造函数? - Why is there a value constructor in addition to the type constructor in Haskell? 为什么使用模式匹配构造的函数具有Eq类型约束,但在使用数据构造函数时却没有? - Why does a function constructed with pattern matching have the Eq type constraint but not when using a data constructor? ( - >)有数据构造函数吗? - Does (->) have a data constructor? 在函数上传递数据构造函数而不是Type构造函数 - Passing a data constructor instead of a Type constructor on a function 为什么包含列表构造函数的`a`的总和类型与`a`的列表不匹配? - Why does a sum-type of `a` containing a list constructor not match a list of `a`? 没有数据构造函数的数据声明。 可以实例化吗? 为什么编译? - Data declaration with no data constructor. Can it be instantiated? Why does it compile? 单构造函数代数数据类型:这是什么意思? - Single Constructor Algebraic Data Type: what does it mean?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM