# 依赖类型

依赖类型是一种其定义依赖于某个值的类型：一个由项 x : A 索引的类型族 B，使得对于每个不同的 a : A，实例 B(a) 可能是真正不同的类型。这样的族最好被理解为从类型 A 的项到类型的函数，而正是这一核心特征将马丁-洛夫类型论等依赖类型理论与简单类型系统区分开来。<sup>[1](https://cs.lmu.edu/~ray/notes/typetheory/)</sup>

| 关键事实 | 详情 |
|---|---|
| 定义 | 由项 x : A 索引的类型族 B；对于每个 a : A，B(a) 可能是不同的类型<sup>[1](https://cs.lmu.edu/~ray/notes/typetheory/)</sup> |
| 依赖函数类型 | \( \Pi_{x:A} B \)：给定 a : A 后返回类型为 B[x ↦ a] 的项的函数；当 B 不依赖于 x 时，它退化为 A → B<sup>[1](https://cs.lmu.edu/~ray/notes/typetheory/)</sup> |
| 依赖对类型 | \( \Sigma_{x:A} B(x) \)：由 a : A 和 b : B(a) 组成的对 (a, b)；当 B 不依赖于 x 时，它退化为 A × B<sup>[1](https://cs.lmu.edu/~ray/notes/typetheory/)</sup> |
| 逻辑解读 | 在命题即类型下，Π 的行为类似于 ∀，Σ 类似于 ∃；诸如 Prime(x) 的谓词变为“x 是素数”的证明的类型<sup>[2](https://math.berkeley.edu/~forte/notes/type_theory.pdf)</sup><sup> • </sup><sup>[3](https://plato.stanford.edu/entries/type-theory-intuitionistic/)</sup> |
| 典型例子 | 长度索引向量：对于每个 n，Vec t n 是一个不同的类型，而长度为 m 的向量与长度为 n 的向量的拼接被声明为返回长度为 m + n 的向量<sup>[1](https://cs.lmu.edu/~ray/notes/typetheory/)</sup><sup> • </sup><sup>[4](https://www.funcp.org/wiki/dependent-type)</sup> |
| 起源 | 佩尔·马丁-洛夫 1972 年的论文《一种直觉主义类型论》将该理论呈现为形式化直觉主义数学的完整系统<sup>[5](https://archive-pml.github.io/martin-lof/pdfs/An-Intuitionistic-Theory-of-Types-1972.pdf)</sup> |
| 语言 | Agda、Rocq（前身为 Coq）、Lean 和 Idris 是依赖类型语言和证明助手<sup>[6](https://agda.readthedocs.io/en/latest/getting-started/what-is-agda.html?highlight=instance)</sup><sup> • </sup><sup>[7](https://lean-lang.org/doc/reference/latest/)</sup> |

## 两种基本类型构造器

**依赖函数类型（Π）。** 给定一个类型 A 以及对于每个 x : A 的一个类型 B(x)，类型 \( \Pi_{x:A} B(x) \)（读作“pi 型”）包含依赖函数：该类型的项 f 对于每个输入 a : A，返回一个类型恰好为 B[a/x] 的结果。定义这样的 f 等价于为每个 x : A 定义一个项 f(x) : B(x)。当 B 实际上不依赖于 x 时，该类型退化为普通函数类型 A → B，因此 Π 严格地推广了箭头。<sup>[2](https://math.berkeley.edu/~forte/notes/type_theory.pdf)</sup><sup> • </sup><sup>[1](https://cs.lmu.edu/~ray/notes/typetheory/)</sup>

**依赖对类型（Σ）。** 给定相同的数据，类型 \( \Sigma_{x:A} B(x) \)（读作“sigma 型”）包含对 (a, b)，其中 a : A 且 b : B(a)；第二个分量的类型依赖于第一个分量的值。当 B 不依赖于 x 时，\( \Sigma_{x:A} B \) 就是 A × B，因此 Σ 以与 Π 推广 → 相同的方式推广了普通对类型。<sup>[1](https://cs.lmu.edu/~ray/notes/typetheory/)</sup>

每个构造器都带有一组标准的推理规则：从 A 和族 B 构造该类型的形成规则，引入规则（Π 的 λ-抽象，Σ 的配对），消去规则（Π 的应用，Σ 的对的归纳），以及计算规则，如 \( (\lambda x.\, b)(a) \equiv b[a/x] \)。<sup>[2](https://math.berkeley.edu/~forte/notes/type_theory.pdf)</sup>

## 解读

同一套语法支持三种标准解读，这是该概念实用性的很大一部分：<sup>[2](https://math.berkeley.edu/~forte/notes/type_theory.pdf)</sup>

- **集合。** \( \Pi_{x:A} B(x) \) 是由 x : A 索引的集合族 B(x) 的乘积，而 \( \Sigma_{x:A} B(x) \) 是其不交并。<sup>[2](https://math.berkeley.edu/~forte/notes/type_theory.pdf)</sup>
- **空间。** Π 是以 A 为底空间、B(x) 为纤维的纤维化的截面空间；Σ 是该纤维化的全空间。<sup>[2](https://math.berkeley.edu/~forte/notes/type_theory.pdf)</sup>
- **命题。** Π 是全称量词，因此 \( \Pi_{x:A} B \) 的项是 ∀x : A, B(x) 的证明；Σ 是存在量词，因此 \( \Sigma_{x:A} B \) 的项是 ∃x : A, B(x) 的证明。<sup>[2](https://math.berkeley.edu/~forte/notes/type_theory.pdf)</sup>

《斯坦福哲学百科全书》将直觉主义类型论描述为具有用于一般和与积的本原类型构造器 Σ 和 Π，以及自身也是依赖的同一类型：同一类型 \( \mathrm{I}(\mathbb{N}, m, n) \) 依赖于元素 m, n : ℕ，使其成为一个索引类型族。<sup>[3](https://plato.stanford.edu/entries/type-theory-intuitionistic/)</sup>

## 类型中的计算

依赖类型可以通过原始递归来定义，而定义方程就是计算规则。由 n : ℕ 索引的 n-元组类型 A<sup>n</sup> 由 \( A^0 = 1 \) 和 \( A^{n+1} = A \times A^n \) 给出，其中 1 是单元素类型；从这些规则可以计算，例如 \( A^3 = A \times (A \times (A \times 1)) \)。<sup>[3](https://plato.stanford.edu/entries/type-theory-intuitionistic/)</sup> 这正是使得函数的结果类型可以从其参数计算出来的原因，因此长度为 m 和 n 的向量拼接被声明为返回长度 m + n。<sup>[4](https://www.funcp.org/wiki/dependent-type)</sup>

## 命题即类型

在柯里-霍华德解读下，谓词变为依赖类型：谓词 Prime(x) 变为“x 是素数”的证明的类型，而 x < y 变为“x 小于 y”的证明的类型。<sup>[3](https://plato.stanford.edu/entries/type-theory-intuitionistic/)</sup> 一个具体的例子是“存在任意大的素数”这一命题，它变为类型 \( \Pi m{:}\mathbb{N}.\ \Sigma n{:}\mathbb{N}.\ m < n \times \mathrm{Prime}(n) \)：一个将每个数 m 映射到三元组 (n, (p, q)) 的函数，其中 p 证明 m < n，q 证明 n 是素数。因此，该命题的构造性证明就是一个程序，给定任意数，产生一个更大的素数以及两个证明；这就是证明即程序原则。假设存在最大素数的经典反证法不是构造性的，因为它不计算出一个更大的素数。<sup>[3](https://plato.stanford.edu/entries/type-theory-intuitionistic/)</sup>

**精化类型。** 精化类型是通过基于值的限制从其超类型形成的子类型。正自然数可以写作 \( \Sigma_{n:\mathrm{Nat}} (n > 0) \)，即一个数与其为正的证明组成的对。只有当诸如 n > 0 的命题被允许作为类型出现时，这种解读才有意义。<sup>[1](https://cs.lmu.edu/~ray/notes/typetheory/)</sup>

## 依赖类型编程

典型的编程例子是长度索引向量：对于每个自然数 n，Vec t n 是一个不同的类型，表示恰好该长度的向量。于是操作带有精确的类型，例如 head : \( \Pi_{n:\mathrm{Nat}} (\mathrm{Vec}\ t\ (n+1) \to t) \)，仅定义在非空向量上。<sup>[1](https://cs.lmu.edu/~ray/notes/typetheory/)</sup> 由于向量的长度构成其类型的一部分，将长度为 m 的向量追加到长度为 n 的向量可以给出指定长度为 m + n 的结果类型。<sup>[4](https://www.funcp.org/wiki/dependent-type)</sup>

若干语言围绕这一思想构建程序。Agda 是一种依赖类型编程语言，是马丁-洛夫类型论的扩展，可用作证明助手，允许在构造性环境中证明定理并将此类证明作为算法运行。<sup>[6](https://agda.readthedocs.io/en/latest/getting-started/what-is-agda.html?highlight=instance)</sup> Lean 是一个基于依赖类型论的交互式定理证明器，专为前沿数学和软件验证而设计，同时也是一种纯函数式编程语言。<sup>[7](https://lean-lang.org/doc/reference/latest/)</sup> Idris 是一种具有完整依赖类型的通用语言，与 Epigram 和 Agda 密切相关，其中类型以值为依据，因此程序行为的各个方面可以在类型中精确指定；其较新的版本推崇类型驱动开发，将类型视为程序的计划。<sup>[8](https://idris-hackers.github.io/idris-doc/)</sup><sup> • </sup><sup>[9](https://idris-lang.org/)</sup> Rocq（前身为 Coq）同样被描述为一种具有依赖类型的表达性函数式编程语言，以及一个用于机械化交互式定理证明的证明助手。<sup>[10](https://rocq-prover.org/docs)</sup>

## 可判定性与实际限制

完全的一般性是有代价的：在“Type”是类型的系统中，即存在一个包含自身在内的所有类型的类型时，类型检查是不可判定的。<sup>[11](https://dspace.mit.edu/entities/publication/8b2db262-e703-4b1e-b19a-7044efa612d5)</sup> 实际系统以两种方式处理这一问题。在进行函数式编程时，属于类型族的索引对象来自一个可以被高效处理的约束域——例如整数线性等式和不等式；洪伟希（Hongwei Xi）和卡内基梅隆大学的编程语言研究者弗兰克·普芬宁（Frank Pfenning）在他们 1999 年的 POPL 论文《实用编程中的依赖类型》中应用了这种方法。<sup>[12](https://www.cs.cmu.edu/~fp/courses/15312-f04/lectures/27-dependent.html)</sup><sup> • </sup><sup>[13](https://dblp.uni-trier.de/rec/conf/popl/XiP99.html)</sup> 在逻辑框架中，通过限制为纯 lambda 演算来保持可判定性，其中类型检查仍然是可判定的。<sup>[12](https://www.cs.cmu.edu/~fp/courses/15312-f04/lectures/27-dependent.html)</sup>

## 历史

该理论由瑞典逻辑学家、当时任职于斯德哥尔摩大学的佩尔·马丁-洛夫（Per Martin-Löf）在 1972 年的论文《一种直觉主义类型论》中提出，旨在作为遵循毕晓普传统的形式化直觉主义数学的完整系统。该论文提到达纳·斯科特（Dana Scott）在 1970 年对此类理论的较早但尚未定论的表述尝试，以及克赖塞尔（Kreisel）和古德曼（Goodman）关于构造的相关无类型理论。霍华德（Howard）1969 年提出的公理是该理论存在消去规则的特殊情况。<sup>[5](https://archive-pml.github.io/martin-lof/pdfs/An-Intuitionistic-Theory-of-Types-1972.pdf)</sup> 后来带有原始同一类型、良基树类型、宇宙层级以及一般归纳定义和归纳-递归定义的扩展增加了该理论的证明论强度，并使其更便于编程和数学形式化。<sup>[3](https://plato.stanford.edu/entries/type-theory-intuitionistic/)</sup>

## References

1. [Notes on Type Theory, LMU (Ray Toal)](https://cs.lmu.edu/~ray/notes/typetheory/)
2. [Introduction to Type Theory, UC Berkeley lecture notes](https://math.berkeley.edu/~forte/notes/type_theory.pdf)
3. [Intuitionistic Type Theory, Stanford Encyclopedia of Philosophy](https://plato.stanford.edu/entries/type-theory-intuitionistic/)
4. [Dependent type, The Functional Programmer wiki](https://www.funcp.org/wiki/dependent-type)
5. [Per Martin-Löf (1972). An Intuitionistic Theory of Types](https://archive-pml.github.io/martin-lof/pdfs/An-Intuitionistic-Theory-of-Types-1972.pdf)
6. [What is Agda?, Agda User Manual](https://agda.readthedocs.io/en/latest/getting-started/what-is-agda.html?highlight=instance)
7. [The Lean Language Reference](https://lean-lang.org/doc/reference/latest/)
8. [idris](https://idris-hackers.github.io/idris-doc/)
9. [Idris: A Language for Type-Driven Development](https://idris-lang.org/)
10. [Learn Rocq, Rocq documentation](https://rocq-prover.org/docs)
11. [Typechecking is Undecidable when 'Type' is a Type, MIT DSpace](https://dspace.mit.edu/entities/publication/8b2db262-e703-4b1e-b19a-7044efa612d5)
12. [15-312 Foundations of Programming Languages, Lecture 27: Dependent Types, Carnegie Mellon University](https://www.cs.cmu.edu/~fp/courses/15312-f04/lectures/27-dependent.html)
13. [Hongwei Xi and Frank Pfenning (1999). Dependent Types in Practical Programming, POPL 1999, dblp](https://dblp.uni-trier.de/rec/conf/popl/XiP99.html)

---
*Initially written Sep 17, 2026 · Reviewed: Sep 17, 2026; Oct 15, 2026 · Edited: Oct 15, 2026 · Last review: Oct 15, 2026*

Topic: 物质世界与数学 › 数学与统计学 › 逻辑与离散数学 › Formal logic and foundations › Logical calculi and logical syntax › Lambda calculus and type theory › Dependent type theory and Martin-Löf type theory

Copyright 2026 Edgepedia

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

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

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