# 部分函数

从集合 X 到集合 Y 的部分函数是这样一条规则：它为 X 的每个元素至多指派 Y 中的一个元素，但不必为 X 的每个元素都指派值；形式上，它是从 X 的某个子集到 Y 的函数，记作 f : X ⇀ Y<sup>[1](https://leanprover-community.github.io/logic_and_proof/functions.html)</sup>。全函数为每个输入恰好指派一个值，是上述子集取整个 X 时的特例<sup>[1](https://leanprover-community.github.io/logic_and_proof/functions.html)</sup>。部分函数在可计算性理论中居于核心地位，在那里“未定义”通常意味着“不终止”<sup>[2](https://tamnd.github.io/brain/maths/03/10.2/)</sup>。

| 关键事实 | 细节 |
|---|---|
| 定义 | 部分函数 f : X ⇀ Y 是从 X 的某个子集 A 到 Y 的函数；A 称为定义域，f(x) ↓ 表示 f 在 x 处有定义<sup>[1](https://leanprover-community.github.io/logic_and_proof/functions.html)</sup> |
| 图像形式 | 部分函数是一种函数关系：对每个 x，至多有一个 y 使得 (x, y) 属于其图像；全函数把每个元素映到恰好一个值<sup>[3](https://ai.dmi.unibas.ch/_files/teaching/hs22/dmics/slides/dmics-b08-handout.pdf)</sup> |
| 复合 | g ∘ f 在 x 处有定义，当且仅当 f 在 x 处有定义且 g 在 f(x) 处有定义；否则无定义<sup>[3](https://ai.dmi.unibas.ch/_files/teaching/hs22/dmics/slides/dmics-b08-handout.pdf)</sup> |
| 提升 | 通过添加一个未定义元素 ⊥，并把定义域之外的输入送到 ⊥，f : A ⇀ B 可变成全函数 A → B⊥<sup>[4](https://ncatlab.org/nlab/show/partial%20map%20classifier)</sup> |
| 可计算性 | 部分递归函数由复合、原始递归和极小化构造而成；极小化是引入真正部分性的运算<sup>[5](https://www.cl.cam.ac.uk/teaching/1112/CompTheory/lectures/lecture-8.pdf)</sup> |
| 未定义 = 不终止 | 在可计算性理论中，f(x) 无定义通常意味着算法在输入 x 上不停机<sup>[2](https://tamnd.github.io/brain/maths/03/10.2/)</sup> |
| 语言支持 | Scala 提供带 isDefinedAt 的 PartialFunction；OCaml 建议对非缺陷类失败使用 'a option 或 ('a, 'b) result<sup>[6](https://www.scala-lang.org/api/current/scala/PartialFunction.html)</sup><sup> • </sup><sup>[7](https://github.com/ocaml/ocaml.org/blob/main/data/tutorials/guides/1wf_02_error_handling.md)</sup> |

## 定义与基本性质

从集合 A 到集合 B 的部分函数 f 由 A 与 B 上的一个函数关系 G 给出，G 称为 f 的图像；当不存在 y ∈ B 使 (x, y) ∈ G 时，f(x) 无定义<sup>[3](https://ai.dmi.unibas.ch/_files/teaching/hs22/dmics/slides/dmics-b08-handout.pdf)</sup>。等价地，它由一个子集 D ⊆ A 连同从 D 到 B 的一个全函数组成<sup>[8](https://ncatlab.org/nlab/show/partial+function)</sup>。集合 D 就是定义域，记号 f(x) ↓ 表示 f 在 x 处有定义<sup>[1](https://leanprover-community.github.io/logic_and_proof/functions.html)</sup>。

两个部分函数可以用一种特殊相等来比较：f(x) ≃ g(x) 的意思是二者在 x 处都有定义且值相同，或者二者在 x 处都无定义<sup>[1](https://leanprover-community.github.io/logic_and_proof/functions.html)</sup>。全函数 f : A → B 是在 A 中每个 x 处都有定义的部分函数；对全函数而言，输入集合与定义域之间没有区别<sup>[3](https://ai.dmi.unibas.ch/_files/teaching/hs22/dmics/slides/dmics-b08-handout.pdf)</sup>。

把部分函数看作关系，f 可由关系 R_f(x, y) 表示，其中每个 x 至多与一个 y 相关。多值函数则允许每个 x 至少对应一个 y，而部分多值函数允许零个或多个值，本质上不过是一个任意关系<sup>[1](https://leanprover-community.github.io/logic_and_proof/functions.html)</sup>。

## 函数为何是部分的

部分性可以来自底层的定义域：除法是经典例子，因为除以零在数学上没有定义<sup>[9](https://ericnormand.substack.com/p/total-functions-and-beyond)</sup>。它也可以来自计算：在可计算性理论中，未定义值通常表示不终止，因此 f(x) = y 意味着算法在输入 x 上停机并输出 y，而 f(x) 无定义意味着它不停机<sup>[2](https://tamnd.github.io/brain/maths/03/10.2/)</sup>。它还可以来自定义本身：用所罗门·费弗曼的话说，有定义性问题涉及对可能没有值也可能有值的表达式进行推理，这类问题在数学中无处不在<sup>[10](https://math.stanford.edu/%7Efeferman/papers/definedness.pdf)</sup>。

不同领域的约定不同。在中学数学中，函数通常默认是部分的，若要是全函数则需另行说明；现代范畴论与类型论的约定相反，要求函数必须是全的<sup>[8](https://ncatlab.org/nlab/show/partial+function)</sup>。

## 可计算性理论中的部分函数

若部分函数 f 能从基本函数出发，经有限步使用复合、原始递归和极小化构造出来，则称 f 为部分递归函数<sup>[5](https://www.cl.cam.ac.uk/teaching/1112/CompTheory/lectures/lecture-8.pdf)</sup>。真正的部分性正是在极小化处进入：µ-算子作用于全函数时，若存在最小的 y 使 f(x₁, …, xₙ, y) = 0，则返回该 y，否则无定义<sup>[11](https://builds.openlogicproject.org/content/computability/recursive-functions/partial-functions.pdf)</sup>。有界极小化只搜索到固定上界 z，失败时返回 z + 1，从而保持算子为全的，原始递归函数类在该运算下封闭<sup>[12](https://encyclopediaofmath.org/wiki/Primitive_recursive_function)</sup>。

若某个算法在函数有值的每个输入上都给出正确结果，而在函数无值的每个输入上都永远发散，则该部分函数算作可计算的<sup>[2](https://tamnd.github.io/brain/maths/03/10.2/)</sup>。既部分递归又是全的函数就简称为递归函数<sup>[13](https://www.cs.umd.edu/users/gasarch/papers/comput.pdf)</sup>，而递归函数类传统上经由丘奇论题被认为对应于可由算法有效计算的函数<sup>[14](https://plato.stanford.edu/entries/recursive-functions/)</sup>。

与停机问题的联系是直接的。若存在全递归函数 f，使得当 x ∈ A 时 f(x) = 1，否则 f(x) = 0，则称集合 A 可解，也称可判定或递归；否则称 A 不可解<sup>[13](https://www.cs.umd.edu/users/gasarch/papers/comput.pdf)</sup>。停机问题 HALT = { e | φ_e(e) ↓ } 就是用部分递归函数陈述的不可解问题之一例<sup>[13](https://www.cs.umd.edu/users/gasarch/papers/comput.pdf)</sup>。在许多情形下，验证全性在算法上困难甚至不可能，这是可计算性理论中不可判定现象的主要来源之一<sup>[2](https://tamnd.github.io/brain/maths/03/10.2/)</sup>。

## 复合、提升与显式化部分性

部分函数 f : A ⇀ B 与 g : B ⇀ C 的复合 g ∘ f 在 x 处有定义，当且仅当 f 在 x 处有定义且 g 在 f(x) 处有定义，否则无定义<sup>[3](https://ai.dmi.unibas.ch/_files/teaching/hs22/dmics/slides/dmics-b08-handout.pdf)</sup>。部分函数在其定义域上的限制是全函数，而 f 的扩张 f₀ 满足 A ⊆ A₀ 且 f₀|A = f<sup>[3](https://ai.dmi.unibas.ch/_files/teaching/hs22/dmics/slides/dmics-b08-handout.pdf)</sup>。

**提升为全函数。** 标准技巧是在陪域 B 上添加一个新元素 ⊥，读作“未定义”，并把定义域之外的每个输入都送到 ⊥，从而把 f : A ⇀ B 变成全函数 f̄ : A → B⊥<sup>[4](https://ncatlab.org/nlab/show/partial%20map%20classifier)</sup>。在编程中这表现为 option 类型：OCaml 的 'a option 或者表示类型 'a 的数据，如 Some 42 作为 int option，或者表示数据缺失，即 None<sup>[7](https://github.com/ocaml/ocaml.org/blob/main/data/tutorials/guides/1wf_02_error_handling.md)</sup>。Isabelle 的代码生成器使用同样的装置：非尾递归的部分函数可以改写为返回 option 类型，其中 Some x 表示结果 x，None 表示不终止<sup>[15](https://isabelle.in.tum.de/website-Isabelle2026-RC0/dist/library/Doc/Codegen/Partial_Functions.html)</sup>。

任意的全扩张是有代价的。部分函数 f : Nⁿ ⇀ N 可以通过在 f 无定义的输入上赋任意值而扩张为全函数 g，但这样的扩张可能丢失原函数的计算意义<sup>[2](https://tamnd.github.io/brain/maths/03/10.2/)</sup>。

## 范畴论与域论中的部分性

在有拉回的范畴中，部分映射 A ⇀ B 是一个 span A ← D → B，其中 D → A 是单态；子对象 D ↪ A 称为该部分映射的定义域<sup>[4](https://ncatlab.org/nlab/show/partial%20map%20classifier)</sup>。对集合而言，这恢复了子集定义：部分映射是从定义域的一个子对象出发的全映射，该子对象就是有定义域<sup>[16](https://strathprints.strath.ac.uk/60204/1/Chapman_etal_JFR2017_Formalizing_restriction_categories.pdf)</sup>。

集合与部分函数构成的范畴可以通过部分映射分类器变成全函数范畴：把每个集合 S 替换为 S⊥，即 S 的至多含一个元素的子集所构成的集合；所得范畴与带点集合范畴等价但不同构<sup>[8](https://ncatlab.org/nlab/show/partial+function)</sup>。在 Lean 的 Mathlib 中，PartialFun 是配备部分函数的类型范畴，经典地等价于带点类型范畴；该等价在构造性意义下不能成立，因为对 Part 而言是否有定义一般是不可判定的，而被送到 Option.none 则是可判定的<sup>[17](https://leanprover-community.github.io/mathlib4_docs/Mathlib/CategoryTheory/Category/PartialFun.html)</sup>。

**限制范畴。** 科克特与拉克的限制范畴为部分性提供了抽象公理化描述，相对于部分映射范畴是可靠且完备的<sup>[16](https://strathprints.strath.ac.uk/60204/1/Chapman_etal_JFR2017_Formalizing_restriction_categories.pdf)</sup>。限制结构为每个映射 f : A → B 指派一个幂等映射 f̄ : A → A，满足 ff̄ = f 等公理，用以度量箭头的部分性程度；在集合与部分函数的基本例子中，当 f 有定义时 f̄(x) 就是 x，否则无定义<sup>[18](https://pages.cpsc.ucalgary.ca/%7Erobin/FMCS/FMCS_06/RestrictionsI.pdf)</sup>。研究部分映射的一个重要动机是研究部分递归函数范畴<sup>[18](https://pages.cpsc.ucalgary.ca/%7Erobin/FMCS/FMCS_06/RestrictionsI.pdf)</sup>。

在逻辑方面，部分项基本逻辑（BPT）可扩展为分别针对按值调用和按名调用的逻辑，用于处理带配对、投影、条件分支与最小不动点递归的简单类型 λ 项，并通过指称语义证明其充分性；值得注意的是，程序与逻辑中都不含常量“未定义”<sup>[19](https://www.cambridge.org/core/journals/journal-of-functional-programming/article/why-the-constant-undefined-logics-of-partial-terms-for-strict-and-nonstrict-functional-programming-languages/3351BE72FC143DEEC339C155D7910A0B)</sup>。费弗曼关于有定义性的综述从一般部分项逻辑，经由部分组合子演算与 λ 演算，一直讲到计算语境中表达力丰富的部分函数理论<sup>[10](https://math.stanford.edu/%7Efeferman/papers/definedness.pdf)</sup>。在程序验证中，部分函数逻辑（LPF）通过使用一种更弱的逻辑从根本上处理无定义性，据称适合“先设定后证明”的开发风格<sup>[20](https://www.sciencedirect.com/science/article/pii/S1571066105052473)</sup>。

## 编程中的部分函数

语言以多种方式表示部分性，每种方式都有不同的安全性特征。

**显式定义域谓词。** Scala 标准库提供 PartialFunction[A, B]，这是一个一元函数，其定义域不一定包含类型 A 的所有值，并提供 isDefinedAt 来动态测试某个值是否在定义域中<sup>[6](https://www.scala-lang.org/api/current/scala/PartialFunction.html)</sup>。这一契约比看上去更弱：即使 isDefinedAt 对某个值返回 true，对其调用 apply 仍可能抛出异常，例如 { case x => x / 0 } 会抛出 ArithmeticException: / by zero，因此调用者负责先检查 isDefinedAt<sup>[6](https://www.scala-lang.org/api/current/scala/PartialFunction.html)</sup>。文档建议以 applyOrElse 作为遵守该契约的常用方式，因为预期它比分别调用 isDefinedAt 和 apply 更高效<sup>[6](https://www.scala-lang.org/api/current/scala/PartialFunction.html)</sup>。

**Option 与 result 类型。** 在 OCaml 中，对于可能在非缺陷情形下失败的函数，例如网络故障或键缺失，良好实践是返回 'a option 或 ('a, 'b) result，而不是抛出异常<sup>[7](https://github.com/ocaml/ocaml.org/blob/main/data/tutorials/guides/1wf_02_error_handling.md)</sup>。

**哨兵值。** 在 JavaScript 与 TypeScript 中，除以零会静默返回 NaN、Infinity 或 -Infinity，这些值会传播到其他算术运算中；在除零时抛出异常会使函数成为部分的，但能保护代码免受这些值影响<sup>[9](https://ericnormand.substack.com/p/total-functions-and-beyond)</sup>。

在 Isabelle/HOL 中，每个函数都是全的：把 f 作用于 x 总会得到值 f x，无定义性作为一个概念并不存在。因此部分函数被建模为带额外定义域谓词的全函数，该谓词刻画函数终止的那些输入<sup>[21](https://isabelle.in.tum.de/website-Isabelle2026-RC0/dist/Isabelle2026-RC0/doc/functions.pdf)</sup>。函数包导出受定义域条件保护的化简规则与归纳规则，称为 psimps 与 pinduct<sup>[21](https://isabelle.in.tum.de/website-Isabelle2026-RC0/dist/Isabelle2026-RC0/doc/functions.pdf)</sup>。

## 近期进展

近期的主要进展在证明助手中。Lean 4.17.0 于 2025-03-03 发布，附带 Partial Fixpoint 特性（PR #6355），增加了定义可能不终止的函数并仍以等式方式推理的能力，只要它们是尾递归的或工作在 Option 等特定单子中<sup>[22](https://lean-lang.org/doc/reference/latest/releases/v4.17.0/)</sup>。该特性深受 Isabelle 的 partial_function 命令启发<sup>[23](https://github.com/leanprover/lean4/pull/6355)</sup>，它把域论的一部分引入证明助手，其中包括 Knaster–Tarski 定理的一个变体，并附带单调性 tactic、支持依赖递归与互递归的去递归器，以及不动点归纳原理<sup>[23](https://github.com/leanprover/lean4/pull/6355)</sup>。

这两个系统现在代表两种已有方案：Isabelle 的 psimps 与 pinduct 规则用定义域条件保护等式推理<sup>[21](https://isabelle.in.tum.de/website-Isabelle2026-RC0/dist/Isabelle2026-RC0/doc/functions.pdf)</sup>，而 Lean 的 partial_fixpoint 则通过构造最小不动点，为一类受限的部分定义支持等式推理<sup>[23](https://github.com/leanprover/lean4/pull/6355)</sup>。

## References

1. [15. Functions, Logic and Proof 3.18.4, LeanProver Community](https://leanprover-community.github.io/logic_and_proof/functions.html)
2. [10.2 Partial vs Total Functions, brain (tamnd)](https://tamnd.github.io/brain/maths/03/10.2/)
3. [Discrete Mathematics in Computer Science, Functions, University of Basel lecture notes](https://ai.dmi.unibas.ch/_files/teaching/hs22/dmics/slides/dmics-b08-handout.pdf)
4. [partial map classifier, nLab](https://ncatlab.org/nlab/show/partial%20map%20classifier)
5. [Partial Recursive Functions, University of Cambridge Computer Laboratory lecture 8](https://www.cl.cam.ac.uk/teaching/1112/CompTheory/lectures/lecture-8.pdf)
6. [scala.PartialFunction, Scala API documentation](https://www.scala-lang.org/api/current/scala/PartialFunction.html)
7. [Error handling, OCaml tutorial (ocaml.org)](https://github.com/ocaml/ocaml.org/blob/main/data/tutorials/guides/1wf_02_error_handling.md)
8. [partial function, nLab](https://ncatlab.org/nlab/show/partial+function)
9. [Total functions and beyond, Eric Normand's Newsletter](https://ericnormand.substack.com/p/total-functions-and-beyond)
10. [Solomon Feferman, Definedness](https://math.stanford.edu/%7Efeferman/papers/definedness.pdf)
11. [rec.1 Partial Recursive Functions, Open Logic Project](https://builds.openlogicproject.org/content/computability/recursive-functions/partial-functions.pdf)
12. [Primitive recursive function, Encyclopedia of Mathematics](https://encyclopediaofmath.org/wiki/Primitive_recursive_function)
13. [Recursive Functions and the Church-Turing Thesis, University of Maryland course notes (Gasarch)](https://www.cs.umd.edu/users/gasarch/papers/comput.pdf)
14. [Recursive Functions, Stanford Encyclopedia of Philosophy](https://plato.stanford.edu/entries/recursive-functions/)
15. [Theory Partial_Functions, Isabelle Codegen documentation](https://isabelle.in.tum.de/website-Isabelle2026-RC0/dist/library/Doc/Codegen/Partial_Functions.html)
16. [Formalizing Restriction Categories, Chapman et al., Journal of Formalized Reasoning (2017)](https://strathprints.strath.ac.uk/60204/1/Chapman_etal_JFR2017_Formalizing_restriction_categories.pdf)
17. [Mathlib.CategoryTheory.Category.PartialFun, Mathlib4 documentation](https://leanprover-community.github.io/mathlib4_docs/Mathlib/CategoryTheory/Category/PartialFun.html)
18. [Restrictions, Cockett & Lack, Theoretical Computer Science](https://pages.cpsc.ucalgary.ca/%7Erobin/FMCS/FMCS_06/RestrictionsI.pdf)
19. [Why the constant 'undefined'? Logics of partial terms, Journal of Functional Programming](https://www.cambridge.org/core/journals/journal-of-functional-programming/article/why-the-constant-undefined-logics-of-partial-terms-for-strict-and-nonstrict-functional-programming-languages/3351BE72FC143DEEC339C155D7910A0B)
20. [Reasoning About Partial Functions in the Formal Development of Programs, ScienceDirect](https://www.sciencedirect.com/science/article/pii/S1571066105052473)
21. [Defining Recursive Functions in Isabelle/HOL, §8 Partiality](https://isabelle.in.tum.de/website-Isabelle2026-RC0/dist/Isabelle2026-RC0/doc/functions.pdf)
22. [Lean 4.17.0 release notes (2025-03-03)](https://lean-lang.org/doc/reference/latest/releases/v4.17.0/)
23. [feat: partial_fixpoint: partial functions with equations, Lean 4 PR #6355](https://github.com/leanprover/lean4/pull/6355)

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

Topic: 物质世界与数学 › 数学与统计学 › 逻辑与离散数学 › General discrete mathematics and discrete structures › Discrete mathematics

Copyright 2026 Edgepedia

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

Source: https://www.edgepedia.org/zh/partial-function

English original: https://www.edgepedia.org/partial-function
