代数数据类型

维基百科,自由的百科全书

代数数据类型(英語:Algebraic data type,缩写:ADT)是一种复合类型,指由其他資料類型组合而成的类型,在電腦编程中,尤其是函数式编程类型论中應用。

两类常见的代数类型是积类型英语Product type(即元组和记录)与和类型(即标签联合) 。[1]

一个积类型的值通常包含多个值,即字段。该类型的所有值都有相同的字段类型组合。一个积类型的所有可能值的集合是其字段类型的所有可能值的集合的集合论乘积,即笛卡尔积

一个和类型的值通常被分组为多个类别,即变体。一个变体类型的值通常是用一个叫做构造器的准功能实体创建的。每个变体都有自己的构造函数,它接受指定数量的参数,并具有指定的类型。和类型的所有可能值的集合是其变体的所有可能值的集合的集合论之和,即不相连的联合。枚举类型是和类型的一个特例,其中构造函数不需要参数,因为每个构造函数只定义一个值。

代数类型的值是用模式匹配来分析的,它通过构造函数或字段名来识别一个值,并提取它所包含的数据。

代数数据类型被引入到Hope中。Hope是一个20世纪70年代在爱丁堡大学开发的小型函数式编程语言[2]

支持代数数据类型的编程语言[编辑]

许多编程语言将代数数据类型作为一等类型概念,包括:

参考文献[编辑]

  1. ^ Records and variants- OCaml manual section 1.4 互联网档案馆存檔,存档日期2020-04-28.
  2. ^ Paul Hudak; John Hughes; Simon Peyton Jones; Philip Wadler. A history of Haskell: being lazy with class. Proceedings of the third ACM SIGPLAN conference on History of programming languages. Presentations included Rod Burstall, Dave MacQueen, and Don Sannella on Hope, the language that introduced algebraic data types 
  3. ^ Calculus of Inductive Constructions页面存档备份,存于互联网档案馆), and basic standard libraries : Datatypes页面存档备份,存于互联网档案馆) and Logic页面存档备份,存于互联网档案馆).
  4. ^ CppCon 2016: Ben Deane "Using Types Effectively". (原始内容存档于2021-12-12) –通过www.youtube.com. 
  5. ^ Flow页面存档备份,存于互联网档案馆
  6. ^ Algebraic Data Types in Haskell. Serokell. [2023-01-03]. (原始内容存档于2023-02-15). 
  7. ^ Enum Instance. Haxe - The Cross-platform Toolkit. [2023-01-03]. (原始内容存档于2023-01-28). 
  8. ^ JEP 360: Sealed Classes (Preview). OpenJDK. [2023-01-03]. (原始内容存档于2022-05-23). 
  9. ^ Sealed Classes - Kotlin Programming Language. Kotlin. [2023-01-03]. (原始内容存档于2023-02-08). 
  10. ^ Reason · Reason lets you write simple, fast and quality type safe code while leveraging both the JavaScript & OCaml ecosystems.. reasonml.github.io. [2023-01-03]. (原始内容存档于2022-05-06). 
  11. ^ Enums and Pattern Matching - The Rust Programming Language. doc.rust-lang.org. [31 August 2021]. (原始内容存档于2023-01-18).