Preface前言

欢迎

构建经形式化验证的正确软件,有一种好方法:
  • 用表达力强、且有良好【证明理论|Proof Theory】的语言 写程序(即嵌入 Rocq 逻辑中的 Gallina 语言)。
  • 在 Rocq 中证明程序正确。
  • 提取为 ML,再用优化的 ML 编译器编译。
遗憾的是,某些应用用不起带垃圾回收的 高阶函数式编程语言(如 Gallina 或 ML)。 也许你正在编写操作系统内核、位操作密码学原语, 或函数式语言的运行时系统与垃圾回收器! 这时你可能想改用底层的指令式系统编程语言, 比如 C。
可你仍希望 OS、加密与 GC 正确无误! 为此应当在 Rocq 中做机器检查的【程序验证|Program Verification】。 用的正是【可验证 C|Verifiable C】: 一种针对 C 的【程序逻辑|Program Logic】与证明系统。
什么是程序逻辑?一个例子是【霍尔逻辑|Hoare Logic】, 你已在本系列【《编程语言基础》】一卷中学过。 (若尚未学习,请研读该卷的 Hoare 与 Hoare2 两章, 并完成习题。)
可验证 C 基于霍尔逻辑的一个 21 世纪版本, 称为【高阶非直谓并发分离逻辑|Higher-Order Impredicative Concurrent Separation Logic】。 回到 20 世纪。霍尔逻辑不擅长验证带【指针|Pointer】的 数据结构,于是有了【分离逻辑|Separation Logic】。 霍尔逻辑写【并发|Concurrent】程序很笨拙, 于是有了【并发分离逻辑|Concurrent Separation Logic】。 霍尔逻辑还处理不了高阶面向对象编程模式与函数闭包, 于是有了【高阶非直谓程序逻辑|Higher-Order Impredicative Program Logic】。
这本电子书是【《软件基础》】系列的第 5 卷。 这个系列讲可靠软件的数学基础。 它的新意在于:百分之百形式化,且经机器检查。 全文其实就是一份 Rocq 脚本。 建议你开一个 Rocq 交互式会话,边读边试。 正文的每个细节都在 Rocq 中形式化, 习题也要求用 Rocq 完成。
在学习本卷之前,你应当是 Rocq 的熟练使用者:
  • 学习【《逻辑基础》】(【《软件基础》】第 1 卷),并完成习题!
  • 学习【《编程语言基础》】(【《软件基础》】第 2 卷)的 Hoare 与 Hoare2 两章,并完成习题!
  • 学习【《经验证的函数式算法》】(【《软件基础》】第 3 卷)的 Sort、SearchTree 与 ADT 三章,并完成习题!
你还需要具备 C 编程语言的实用知识。

实用事项

系统需求

Rocq 可在 Windows、Linux 与 OS X 上运行。 第 1 卷的前言描述了你需要的 Rocq 安装方式。 本版用 Rocq 9.0.0 构建。
你需要安装 VST 2.16。两种装法:把它作为 每个新版本 Rocq 随附的「Rocq Platform」的一部分, 或用 opam(软件包名为 coq-vst)。 本章末尾有一项测试,检查你装的 VST 版本是否正确。
注意:截至 2026 年 1 月,尚无针对 Rocq 9 的 Rocq Platform 发行版。请按此处说明用 opam 安装。
【如果你使用 opam】,可按以下说明安装 Rocq 与 RocqIDE: https://rocq-prover.org/opam-using.html 然后继续执行:
  • opam update (【视需要】)
  • opam install coq-vst.2.16 (【要花 30 分钟以上】)
【不装 CompCert clightgen】 也能完成本卷习题。 但若你想改动并重新解析 .c 文件, 或验证自己的 C 程序,请装上 CompCert, 即那个经验证的优化 C 编译器。 可在标准「Rocq Package」中获取 CompCert, 或用 opam(软件包名为 coq-compcert)。

下载 Rocq 文件

包含本书「发行版」完整源码 (一组 Rocq 脚本与 HTML 文件)的 tar 文件, 可在 https://softwarefoundations.cis.upenn.edu获取。
(如果你将本书用于课程,你的教授可能会 提供本地修改版文件的访问权限。 这种情况下应当用他给的版本,而非发行版。)

安装

将 vc.tgz tar 文件解包到一个 vc 目录。 在此 vc 目录中,用 make 命令构建。

习题

每一章都包含习题。每道题都标有 「星级」,可解释如下:
  • 一星:简单习题,用于强调正文中的要点, 对多数读者只需一两分钟。 遇到就做,养成习惯。
  • 两星:直截了当的习题(五到十分钟)。
  • 三星:需要一点思考的习题 (十分钟到半小时)。
  • 四星与五星:较难的习题(半小时起)。
【请不要在任何公开场所发布习题解答】: 软件基础被广泛用于自学与大学课程。
解答若唾手可得,它对课程的用处就大打折扣; 而课程通常有计入成绩的家庭作业。 作者特别请求读者:不要在搜索引擎能找到的 任何地方发布习题解答。

推荐的引用格式

如果你想在自己的作品中引用本卷,请采用以下格式:
    @book {Appel:SF5,
    author = {Andrew W. Appel, Lennart Beringer, and Qinxiang Cao},
    editor = {Benjamin C. Pierce},
    title = "可验证的 C",
    series = "Software Foundations",
    volume = "5",
    year = "2026",
    publisher = "Electronic textbook",
    note = {Version 2.1, \URL{http://softwarefoundations.cis.upenn.edu} },
    }

面向授课员与贡献者

若你打算在自己的课程中使用这些材料, 多半会想改动、改进或补充一些内容。 欢迎贡献!详见【《逻辑基础》】中的 Preface 一章。

鸣谢

【《软件基础》】系列的部分开发工作, 由【国家科学基金会|National Science Foundation】资助, 项目是 NSF 科研远征 1521523 号 【深度规范科学|The Science of Deep Specification】。

Check that we have the right version of VST

Require Import Stdlib.Strings.String.
Open Scope string.
Definition release_needed := "2.17".
Require Import VST.veric.version. (* If this line fails, it means
                                      you don't have a VST installed. *)

Goal release = release_needed ∨ release="2.16" ∧ release_needed="2.17".
Proof.
compute; auto ||
let need := constr:(release_needed) in let need := eval hnf in need in
let rel := constr:(release) in let rel := eval hnf in rel in
fail "The wrong version of VST is installed. You have VST version"
rel "but this version of 'Software Foundations Volume 5: Verifiable C'"
"demands version" need ". If possible, install VST version" need
"using the Rocq Platform or using opam. Or, if not From Stdlib Platform or opam,"
"for instructions about building VST from source and accessing that version, see the README file in this directory."
"Or, instead of installing VST" need ","
"if you want to proceed using VST version" rel ","
"then edit the Definition release_needed in Preface.v".
Abort.

From Stdlib Require Import ZArith.
Local Open Scope Z_scope.

Require VC.stack. (* If this line fails, do 'make stack.vo' *)
Goal VC.stack.Info.bitsize = VST.veric.version.bitsize.
reflexivity ||
let b1 := constr:(VST.veric.version.bitsize) in let b1 := eval compute in b1 in
let b2 := constr:(VC.stack.Info.bitsize) in let b2 := eval compute in b2 in
fail "Your installed VST is configured to verify C programs compiled with"
  b1 "bit pointers,"
 "but the .c files in the local directory have been clightgen'ed as if compiled with"
 b2 "bit pointers."
"You can fix this by executing the shell command, "
"bash cfiles-bitsize" b1 " to install the properly configured clightgen outputs."
"It is not necessary to have clightgen installed".
Abort.

(* 2026-11-26 16:01 *)