# 存在类型

存在类型是形如 \( \exists \alpha.\ \tau \) 的类型，它描述的值包含某个隐藏类型 \( \alpha \) 以及以该类型表达的操作，但不揭示隐藏类型究竟是什么。在编程语言中，存在类型是数据抽象的类型论解释：它们让实现方将一个具体表示与其操作打包在一起，而客户端只能使用这些操作。<sup>[1](https://theory.stanford.edu/~jcm/papers/mitch-plotkin-88.pdf)</sup>

| 关键事实 | 详情 |
|---|---|
| 形式 | 类型 \( \exists \alpha.\ \tau \) 绑定一个代表未指定抽象类型的类型变量 \( \alpha \)，外加一个描述操作的类型 \( \tau \)；读作“存在一个类型 \( \alpha \)，使得该值具有类型 \( \tau \)”。<sup>[1](https://theory.stanford.edu/~jcm/papers/mitch-plotkin-88.pdf)</sup><sup> • </sup><sup>[2](https://freest-lang.github.io/tutorial/existentials/)</sup> |
| 起源 | 存在类型最初在构造性逻辑中发展起来，与范畴论中的无限和密切相关。<sup>[1](https://theory.stanford.edu/~jcm/papers/mitch-plotkin-88.pdf)</sup> |
| 里程碑论文 | John C. Mitchell 与 Gordon D. Plotkin，《抽象类型具有存在类型》，POPL 1985；期刊版发表于 ACM Transactions on Programming Languages and Systems，第 10 卷第 3 期，1988 年 7 月，第 470–502 页。<sup>[3](https://dl.acm.org/doi/10.1145/44501.45065)</sup> |
| 核心主张 | Ada、Alphard、CLU 和 ML 等语言中的抽象数据类型声明就是存在类型：操作使用具体表示来定义，但表示在声明之外是隐藏的。<sup>[1](https://theory.stanford.edu/~jcm/papers/mitch-plotkin-88.pdf)</sup> |
| 对偶性 | 存在类型与全称多态类型 \( \forall \alpha.\ \tau \) 相对偶；在 System F 中，存在类型可定义为 \( \exists X.\, F(X) \triangleq \forall Y.\,(\forall X.\,F(X) \to Y) \to Y \)。<sup>[4](https://www.cs.cmu.edu/Groups/fox/people/fp/courses/15312-f04/lectures/12-abstraction.html)</sup><sup> • </sup><sup>[5](https://www.pls-lab.org/en/Existential_types)</sup> |
| 安全性保证 | 参数性保证数据抽象不可被破坏：存在类型包的客户端在任何两个抽象类型实现上行为相同。<sup>[4](https://www.cs.cmu.edu/Groups/fox/people/fp/courses/15312-f04/lectures/12-abstraction.html)</sup><sup> • </sup><sup>[6](https://www.cs.cmu.edu/~fp/courses/15814-f20/lectures/18-independence.pdf)</sup> |
| 语言支持 | ML 将存在类型折叠进其模块系统而非直接支持；Scala 3 取消了 `forSome` 存在类型；任何具有二阶多态的语言都可以编码它们。<sup>[4](https://www.cs.cmu.edu/Groups/fox/people/fp/courses/15312-f04/lectures/12-abstraction.html)</sup><sup> • </sup><sup>[7](https://scala-lang.org/api/3.3_LTS/docs/docs/reference/dropped-features/existential-types.html)</sup><sup> • </sup><sup>[8](https://kobylewis.net/How-and-Why-to-Use-Existential-Types-in-Python)</sup> |

## Mitchell–Plotkin 分析

[抽象数据类型](https://www.edgepedia.org/zh/abstract-data-type)声明出现在 20 世纪 70 年代末和 80 年代的类型化语言中，包括 Ada 包、Alphard 形式、CLU 簇以及 ML 中的 `abstype` 声明。每种声明都将一组名字绑定到一个复合值上，该复合值由一个私有类型和一个或多个操作组成，Mitchell 和 Plotkin 称之为数据代数。在他们的 ML 示例中，`abstype complex = real # real` 将 `complex`、`create`、`plus`、`re` 和 `im` 绑定到一种将复数实现为实数对的实现上；`re` 的声明类型是 `complex → real`，但实现函数的类型是 `real # real → real`，因为操作是针对具体表示编写的，而表示在声明之外是隐藏的。<sup>[1](https://theory.stanford.edu/~jcm/papers/mitch-plotkin-88.pdf)</sup>

为了给这类包赋予类型，论文使用了一种称为 SOL 的二阶类型化 lambda 演算，它是 Jean-Yves Girard 的 [System F](https://www.edgepedia.org/zh/system-f) 在构造性逻辑分析中发展出的记法变体，也是 John C. Reynolds 的多态 lambda 演算的扩展。数据代数通过 `pack` 表达式形成，写作 `pack τ M1 ... Mk to ∃t.u`，其中 \( \tau \) 是具体表示，\( M_i \) 是实现操作，\( \exists t.u \) 是描述该包可以如何使用的存在类型。每个操作 \( M_i \) 必须具有将其声明类型 \( u_i \) 中的 \( t \) 替换为 \( \tau \) 后得到的类型。<sup>[1](https://theory.stanford.edu/~jcm/papers/mitch-plotkin-88.pdf)</sup>

论文为 `abstype` 声明确定了三条类型规则。规则 (AB.1) 要求数据代数表达式具有所声明的存在类型。规则 (AB.2) 禁止作用域中任何自由标识符的类型提及抽象类型名，从而防止在声明外部定义的函数被应用于新抽象类型的值。规则 (AB.3) 禁止抽象类型名自由出现在声明结果的类型中，因此表示无法逃逸作用域。Mitchell 和 Plotkin 给出了 (AB.3) 的若干理由：没有它，对条件数据代数表达式的类型检查将依赖于编译时测试的值，类型相等将依赖于普通表达式的相等，无递归程序的终止性也会失效。<sup>[1](https://theory.stanford.edu/~jcm/papers/mitch-plotkin-88.pdf)</sup>

由于数据代数是普通的类型化值，SOL 允许将抽象数据类型的实现作为参数传递，并作为函数调用的结果返回。他们的例子是一个通用的树搜索例程，深度优先搜索和广度优先搜索的写法几乎完全相同：传入栈数据代数作为实际参数得到深度优先搜索，传入队列得到广度优先搜索，传入优先队列得到最佳优先搜索。<sup>[1](https://theory.stanford.edu/~jcm/papers/mitch-plotkin-88.pdf)</sup>

## 类型规则

在更接近现代课程讲义的表述中，包由两条规则引入和消去。引入规则从具体类型和表达式构造存在类型的值：<sup>[6](https://www.cs.cmu.edu/~fp/courses/15814-f20/lectures/18-independence.pdf)</sup>

\[
\frac{\Gamma \vdash \sigma\ \mathrm{type} \qquad \Gamma \vdash e : [\sigma/\alpha]\tau}{\Gamma \vdash \langle \sigma, e \rangle : \exists \alpha.\ \tau}
\]

消去规则在 case 构造下打开包，在客户端代码中绑定抽象类型名 \( \alpha \) 和内容 \( x \)：<sup>[6](https://www.cs.cmu.edu/~fp/courses/15814-f20/lectures/18-independence.pdf)</sup>

\[
\frac{\Gamma \vdash e : \exists \alpha.\tau \qquad \Gamma,\alpha\ \mathrm{type}, x:\tau \vdash e' : \tau'}{\Gamma \vdash \mathrm{case}\ e\ ((\alpha, x) \Rightarrow e') : \tau'}
\]

客户端 \( e' \) 可以使用操作，但不能命名具体类型，这正是抽象数据类型的隐私边界。在斯坦福 CS 242 的表述中，同样的思想表现为 `export`（通过擦除类型组件创建模块）和 `import`（在只知道抽象类型的情况下使用模块）：一个带有类型为 `num` 的私有 `state` 字段的记录变成一个 `state` 字段具有未知类型 \( \beta \) 的模块，因此客户端无法对其执行数值操作，必须通过公共的 `get` 函数。<sup>[9](https://stanford-cs242.github.io/f19/lectures/04-2-modules.html)</sup>

## 参数性与表示独立性

抽象不可被破坏的保证来自参数性。以存在类型表达的模块的客户端在实现类型上是参数性的：经 case 消去的客户端可以作为多态函数 \( \Lambda \alpha.\ \lambda x.\ e' \) 进行类型检查，因此其行为不能依赖于打包的是哪个具体类型。<sup>[6](https://www.cs.cmu.edu/~fp/courses/15814-f20/lectures/18-independence.pdf)</sup> Frank Pfenning 的 CMU 课程讲义利用这一点证明，一进制计数器实现可以替换为二进制实现而不破坏任何客户端，反之亦然，这是参数性和逻辑相等扩展到存在类型的结果。<sup>[6](https://www.cs.cmu.edu/~fp/courses/15814-f20/lectures/18-independence.pdf)</sup> Robert Harper 的《编程语言实用基础》陈述了相应结果：如果 \( R \) 是抽象类型的任意两个实现之间的互模拟关系，则客户端在它们上的行为相同。<sup>[10](https://ncatlab.org/nlab/files/2nded.pdf)</sup>

这是表示独立性的形式对应物，后者是软件工程性质：只要接口保持不变，抽象类型的实现改变时客户端代码无需修改即可工作。<sup>[11](https://ocw.mit.edu/ans7870/6/6.005/s16/classes/12-abstract-data-types/)</sup>

## 与全称类型和模块的关系

存在类型与全称多态类型相对偶：\( \forall \alpha.\ \tau \) 是从类型到值的映射，而 \( \exists \alpha.\ \tau \) 将一个类型与一个提及它的值打包在一起。两者都在核心处包含参数性。<sup>[4](https://www.cs.cmu.edu/Groups/fox/people/fp/courses/15312-f04/lectures/12-abstraction.html)</sup> System F 本身只有[全称量化](https://www.edgepedia.org/zh/universal-quantification)，但存在量化可通过编码 \( \exists X.\, F(X) \triangleq \forall Y.\,(\forall X.\,F(X) \to Y) \to Y \) 定义；Pitts 在 2000 年证明，在同时具有两种形式的语言中，这两个类型在上下文等价意义下是双射的。<sup>[5](https://www.pls-lab.org/en/Existential_types)</sup>

与模块的对应关系密切但并非字面相同。带有抽象类型组件的 ML 模块，例如具有 `type t` 和操作 `zero`、`incr`、`get` 的 OCaml `COUNTER` 签名，类似于存在类型，而具有存在类型的项可以看作具有单个隐藏类型的简单模块或包。<sup>[12](https://people.csail.mit.edu/feser/pld-s23/existential_types.html)</sup> ML 不直接支持存在类型；它们被折叠进模块系统。<sup>[4](https://www.cs.cmu.edu/Groups/fox/people/fp/courses/15312-f04/lectures/12-abstraction.html)</sup> 后来的理论工作将图景扩展到更丰富的模块系统：2022 年的一篇综述《存在类型反思》发展了具有一等模块和计算效应的 ML 风格语言的语义模型，最终形成了一个容纳高阶一等递归模块和高阶存储的模型。<sup>[13](https://arxiv.org/abs/2210.00758)</sup>

## 编程语言中的支持

直接支持情况各异。Haskell 在数据声明中提供[存在量化](https://www.edgepedia.org/zh/existential-quantification)，任何具有二阶多态的语言都可以编码存在类型；[Python](https://www.edgepedia.org/zh/python-programming-language) 和大多数主流语言没有内置支持，但当操作必须接受抽象类型的多个实例或将其存储在非泛型字段中时，编码是实用的。<sup>[8](https://kobylewis.net/How-and-Why-to-Use-Existential-Types-in-Python)</sup> Scala 2 通过 `forSome` 语法支持存在类型，但 Scala 3 取消了它们：它们违反了 DOT 演算和 Scala 3 所构建于其上的类型可靠性原则，与其他 Scala 构造产生困难的特性交互，并且与路径依赖类型大量重叠。仅通配符形式如 `Map[_ <: AnyRef, Int]` 仍然支持，但被视为精化类型。<sup>[7](https://scala-lang.org/api/3.3_LTS/docs/docs/reference/dropped-features/existential-types.html)</sup> 存在类型也被应用于数据抽象之外，例如在信息流安全类型系统中，抽象机制表达只能通过所提供的操作解除保密的秘密。<sup>[14](https://arxiv.org/abs/1911.04560)</sup>

## References

1. [John C. Mitchell and Gordon D. Plotkin (1988). Abstract Types Have Existential Type. ACM Transactions on Programming Languages and Systems 10(3), 470–502.](https://theory.stanford.edu/~jcm/papers/mitch-plotkin-88.pdf)
2. [Abstract types, FreeST tutorial](https://freest-lang.github.io/tutorial/existentials/)
3. [Abstract types have existential type, ACM Digital Library (TOPLAS 1988; POPL '85 predecessor)](https://dl.acm.org/doi/10.1145/44501.45065)
4. [15-312 Foundations of Programming Languages, Lecture 12: Data Abstraction, Carnegie Mellon University](https://www.cs.cmu.edu/Groups/fox/people/fp/courses/15312-f04/lectures/12-abstraction.html)
5. [Existential types, PLS Lab](https://www.pls-lab.org/en/Existential_types)
6. [Frank Pfenning (2020). Lecture Notes on Representation Independence, 15-814 Types and Programming Languages, Carnegie Mellon University](https://www.cs.cmu.edu/~fp/courses/15814-f20/lectures/18-independence.pdf)
7. [Dropped: Existential Types, Scala 3 documentation](https://scala-lang.org/api/3.3_LTS/docs/docs/reference/dropped-features/existential-types.html)
8. [Koby Lewis. How and Why to Use Existential Types in Python (+TypeScript)](https://kobylewis.net/How-and-Why-to-Use-Existential-Types-in-Python)
9. [CS 242: Modules, Stanford University](https://stanford-cs242.github.io/f19/lectures/04-2-modules.html)
10. [Robert Harper. Practical Foundations for Programming Languages, 2nd edition](https://ncatlab.org/nlab/files/2nded.pdf)
11. [Reading 12: Abstract Data Types, MIT 6.005](https://ocw.mit.edu/ans7870/6/6.005/s16/classes/12-abstract-data-types/)
12. [Interfaces, abstraction, and information hiding, MIT PLD seminar](https://people.csail.mit.edu/feser/pld-s23/existential_types.html)
13. [Reflections on existential types, arXiv:2210.00758](https://arxiv.org/abs/2210.00758)
14. [Existential Types for Relaxed Noninterference, arXiv:1911.04560](https://arxiv.org/abs/1911.04560)

---
*Initially written Oct 15, 2026*

Topic: 技术与人造世界 › 计算技术与数字系统 › 软件与编程 › Programming languages › Programming language concepts › Type systems and type theory

Copyright 2026 Edgepedia

License: Edgepedia Community License 1.0, https://www.edgepedia.org/edgepedia/license

Source: https://www.edgepedia.org/zh/existential-type

English original: https://www.edgepedia.org/existential-type
