为什么Haskell强制数据构造函数的第一个字母是大写? [英] Why does Haskell force data constructor's first letter to be upper case?

查看:161
本文介绍了为什么Haskell强制数据构造函数的第一个字母是大写?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

举一个丑陋的例子:

  data Bighead = Big 

little = 1

f1 = little :: Int

f2 = Big :: BigHead

在我看来:

f1 f2 全部指向一些数据。 ( little Big )的唯一区别是
有一小段代码可以进行评估。但Big不会。



它们都有一个可擦写的机构,很少可以从一组数据转换为结果,Big只是不做最后一步---它始终保存这些数据表单(但递归地可以评估它们)。



但是在语法形式上,它们几乎是相同的:可以应用,可以进行评估。

有一件大事可能是函数不能改变其应用参数,但数据可以做到。



这是Haskell处理数据和函数名称的唯一原因吗?



请求分析: - )



$ p
$ b $ p $ A









$ b $

类型 B

  B :: Int  - > A 

b :: Int - > A

b = B


解决方案

Haskell 98语言中,我们看到了Haskell中标识符标记的核心区别:

  varid  - > (small {small | large | digit |'})< reservedid> 
conid - >大{小|大|数字| '}

也就是说,该语言从根本上区分变量名称 )来自构造函数名称(conid),该语言的所有级别(包括值和类型变量以及构造函数)。很显然,Haskell将标识符区分为两个主要的命名空间(好吧,如果你计算模块和类的话,还有其他的命名空间),但是有两个主要命名空间,一个以小写字母开头(变量标识符),另一个以upper因此,假设我们 do 将构造函数与变量区分开来,问题是为什么?。

阅读类型

一个合理的论点是,它非常容易



模式匹配



其次,更重要的是,我们有统一的数据构造和解构(模式匹配)语法。每当你在一个模式中看到一个大写字母标识符时,

  case x of 
Foo y z - > ...

您知道 Foo 是一个拆分的数据结构,并命名它的组件。相应地,无论何时在表达式中看到大写标识符,

  g(f(Foo 1 2)

您知道 f 正在接收一个新创建的 Foo 数据类型有两个参数。

因此,由于构造函数(类型和值)都是这样这种对大写字母标识符的简单限制使得人们更容易看到一段代码中发生了什么,在某些方面,大写字母弥补了语言中缺少其他语法噪声,作为对读者的帮助。




命名空间


$在Haskell中有六种类型的名称:变量和构造函数的名称表示值;类型变量,类型构造函数和类型类别指的是与类型系统相关的实体;和模块名称是指模块,命名有两个限制:

Nam es用于变量和类型变量是以小写字母或下划线开头的标识符;其他四种名称都是以大写字母开头的标识符。
一个标识符不能用作同一范围内的类型构造函数和类的名称。
这些是唯一的限制;例如,Int可能同时是单个作用域内的模块,类和构造函数的名称。


Haskell B

在Haskell B中,构造函数和变量可以使用任何一种情况。


Give an ugly example:

data Bighead = Big

little = 1 

f1 = little :: Int

f2 = Big :: BigHead

In my opinion:

f1 and f2 all point to some data. the only different of (little and Big) is little has a piece of code to do evaluation. but Big doesn't.

They all have a rewritable body, little can be transformed from a collection of data to a result, and Big is just don't do the last step --- it always holds this data forms (but recursively they can be evaluated).

But in syntax form, they are almost the same: can be applied, can be evaluated.

One big deal may be that functions can't alter its applied params, but data can do.

Is this the only reason that Haskell treats data and function's names differently?

Call for analysis :-)

edit: some more pads

data A = B Int

type of B:

B :: Int -> A

b :: Int -> A

b = B 

解决方案

From the Haskell 98 Language we see the core distinction in identifier tokens in Haskell:

varid   ->   (small {small | large | digit | ' })<reservedid>
conid   ->   large {small | large | digit | ' }

That is, the language fundamentally distinguish variable names ("varid") from constructor names ("conid"), at all levels of the language (both value and type variables and constructors). So clearly, Haskell distinguishes identifiers into two main namespaces (well, there are others, if you count modules and classes), but two primary ones, those that begin with a lower-case letter (variable identifiers) and those that begin with an upper-case letter (constructor identifiers).

So, given that we do distinguish constructors from variables, the question is "why?".

Reading types

One plausible argument is that it makes it very easy to spot parameters in types (e.g. polymorphic types).

Pattern matching

Secondly, and more importantly, we have uniform syntax for data construction and deconstruction (pattern matching). Whenever you see an upper case identifier in a pattern,

case x of
   Foo y z -> ...

You know that Foo is a data structure being taken apart and its components named. Correspondingly, whenever you see an upper case identifier in an expression,

g (f (Foo 1 2)

you know that f is receiving a newly built Foo data type with two arguments.

So, since constructors (both type and value) are so important in the language, this simple restriction to upper case identifiers makes it much easier for a human to see what is going on in a piece of code. In some ways upper case letters make up for the lack of other syntactic noise in the language, as an aid to the reader.


Namespaces

There are six kinds of names in Haskell : those for variables and constructors denote values; those for type variables, type constructors, and type classes refer to entities related to the type system; and module names refer to modules. There are two constraints on naming:

Names for variables and type variables are identifiers beginning with lowercase letters or underscore; the other four kinds of names are identifiers beginning with uppercase letters. An identifier must not be used as the name of a type constructor and a class in the same scope. These are the only constraints; for example, Int may simultaneously be the name of a module, class, and constructor within a single scope.

Haskell B

In Haskell B, constructors and variables could use either case.

这篇关于为什么Haskell强制数据构造函数的第一个字母是大写?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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