Several programming languages allow programmers to define (potentially
recursive) custom types, by composing together existing ones. For instance,
in OCaml, one can define lists as follows:
type 'a list = | Cons of 'a * 'a list | NilThis translates in Haskell as
data List a = Cons a (List a) | NilIn Rust:
enum List<A> { Cons(A, Box< List<a> >), Nil, }In Coq:
Inductive list a := | cons : a -> list a -> list a | nilAnd so forth.Each language will have its own specific constructions, and the type systems of OCaml, Haskell, Rust and Coq —to only cite them— are far from being equivalent. That being said, they often share a common “base formalism,” usually (and sometimes abusively) referred to as algebraic datatypes . This expression is used because under the hood any datatype can be encoded as a composition of types using two operators: sum ( + ) and product ( * ) for types.
- a + b is the disjoint union of types a and b . Any term of a can be injected into a + b , and the same goes for b . Conversely, a term of a + b can be projected into either a or b .
- a * b is the Cartesian product of types a and b . Any term of a * b is made of one term of a and one term of b (remember tuples?).
- + is commutative, that is
- + is associative, that is
- + has a neutral element, that is
- * is commutative, that is
- * is associative, that is
- * has a neutral element, that is
- The distributivity of + and * , that is
- * has an absorbing element, that is
Inductive sum (A B : Type) : Type := | inl : A -> sum A B | inr : B -> sum A B Inductive prod (A B : Type) : Type := | pair : A -> B -> prod A B
-
An Equivalence for
Type
- Introducing type_equiv
- type_equiv is an Equivalence
-
- list ’s Canonical Form
- nat is a Special-Purpose list
- prod has an Absorbing Element
- prod and sum Distributivity
- Bonus: Algebraic Datatypes and Metaprogramming
Revisions
This revisions table has been automatically generated
from
the git
history
of this website repository
, and the change
descriptions may not always be as useful as they
should.
You can consult the source of this file in its current version here .
2020-07-12 | More spellchecking and typos | 48a9b49 |
2020-07-12 | Invert the table of contents and the revision tables | 0a750a2 |
2020-07-12 | Spellchecking | cec5638 |
2020-07-12 | New article on Algebraic Datatypes | 41007fc |
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持 码农网
猜你喜欢:本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们。
Android开发艺术探索
任玉刚 / 电子工业出版社 / 2015-9-1 / CNY 79.00
《Android开发艺术探索》是一本Android进阶类书籍,采用理论、源码和实践相结合的方式来阐述高水准的Android应用开发要点。《Android开发艺术探索》从三个方面来组织内容。第一,介绍Android开发者不容易掌握的一些知识点;第二,结合Android源代码和应用层开发过程,融会贯通,介绍一些比较深入的知识点;第三,介绍一些核心技术和Android的性能优化思想。 《Andro......一起来看看 《Android开发艺术探索》 这本书的介绍吧!