17

Across programming languages, I've encountered similar composite types with different names:

  1. Optional / Maybe
  2. Any
  3. Variant / Sum
  4. Record / Product

People often use the term vocabulary type,
yet I've never seen a definition of what makes a type "vocabulary".

Does this term have a loose definition?
What does type theory and other programming languages have to say about vocabulary types?

Is everything above a vocabulary type? Are there more?

4

1 回答 1

3

我对编程和词汇类型方面的词汇的理解通常赋予某些对象具有明确含义的属性。

以下是 Haskell 中的一些示例:

考虑Optional/ Maybetype 和一个纯函数,它接受 Web 服务器的 HTTP 回复并提取回复的代码。

getCode :: String -> Int

现在假设当我们运行这个函数时,我们不知道响应是否成功——代码可能根本不存在。当代码丢失时,我们如何表示案例?我们可以分配一些人为的值-1,或者0我们可以将整个函数类型更改为:

getCode :: String -> Maybe Int

此外,在 Haskell 中还Maybe形成了MonadFunctor、和一堆其他类型类。每个类型类都增加了额外的能力来操纵有问题的值,同时尊重它的存在/不存在。ApplicativeFoldable

Product/Sum类型在 Haskell 中表示为对和Either a b. 再次 - 通过定义ProductSum添加明确定义的含义Product a b- 两个值都必须存在,Sum a b- 一个值必须存在并免费添加一堆法律。

于 2017-07-02T07:18:25.267 回答