1 / 65

证明辅助工具 Coq 简介

证明辅助工具 Coq 简介. 郭宇 2010-07-20 中科大 - 耶鲁高可信软件联合研究中心. http://kyhcs.ustcsz.edu.cn/summer-school. 课程主页. http://kyhcs.ustcsz.edu.cn/summer-school. 问题. 如何表示数学证明?. Miracles often occur …. 费马大定理 ( 1673). X n = Y n + Z n 当 n>2 时无整数解 A. Wiles 1993 证明 ? 超过一百页 世界上能看懂的人屈指可数 原始证明有错,一年多以后更正.

casey
Download Presentation

证明辅助工具 Coq 简介

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. 证明辅助工具Coq简介 郭宇 2010-07-20 中科大-耶鲁高可信软件联合研究中心 http://kyhcs.ustcsz.edu.cn/summer-school

  2. 课程主页 http://kyhcs.ustcsz.edu.cn/summer-school

  3. 问题 • 如何表示数学证明? • Miracles often occur…

  4. 费马大定理 (1673) • Xn = Yn + Zn • 当n>2时无整数解 • A. Wiles • 1993 • 证明? • 超过一百页 • 世界上能看懂的人屈指可数 • 原始证明有错,一年多以后更正

  5. 四色定理 • K.Appel & W. Haken • 1976年证明 • 证明? • 分1,936种情况讨论 • 利用计算机程序来自动分析,不产生证明 • 一些数学家拒绝承认 • 仍然难以检查证明是否正确

  6. 四色定理 • G. Gonthier & B. Werner • 2004年证明 • 证明? • 完整详细的Coq证明 • 机器可检查 • 没有跳过任何步骤 • 证明检查程序规模小

  7. 高可信软件 • 嵌入式系统内核——seL4 • G. Klein 等人 • OSDI’09 Best Paper • 8700 lines of C • 经过形式化验证 • 证明? • 机器可检查证明 • 200,000 lines of Isabelle script

  8. VeriSoft • 芯片 • 嵌入式内核 • 汽车电子控制单元 • 应用 • 宝马汽车的应急呼叫系统 • 证明: • Isabelle 2005

  9. CompCert • 经过完全证明的C语言编译器 • Xavier Leroy [CACM ’09] • Power PC backend • 证明? • 机器可检查证明 • 使用Coq代码编写并证明 • 抽取出O’Caml代码运行

  10. Certified Software • Certified Software = Program + Proof

  11. Coq是什么 • 一个证明系统 • 编写证明,检查证明 • 一套形式化语言 • 编写数学定义、算法、定理 • 类型化  演算 • 一个环境 • 交互式证明

  12. 其它证明辅助工具 • Isabelle 2005 • Twelf • Agda

  13. Coq介绍 • Coq环境 • 函数式编程 • 逻辑推理 • 归纳

  14. 运行 coq • 命令行解释器 • coqtop • 编译器 • coqc

  15. 用户界面 • CoqIDE

  16. 用户界面 • emacs + proofgeneral(推荐)

  17. 运行 Coq • 运行 coqtop • 检查一个表达式的类型 Coq< Check 3. 3 : nat Coq< Check 3 + 5. 3+5 : nat Coq 命令 (以. 结尾)

  18. 类型检查 • 每一个合式的项都有一个类型 • 每一个类型也是一个项 > Check 3 + true. Error: The term “true” has type “bool” While it is expected to have type “nat”.

  19. 定义 > Definition a := 5. a is defined > Definition b := a + 6. b is defined > Eval compute in b. = 11 : nat

  20. Coq命令——打印 Coq< Print b. b = a + 6 : nat Coq< Set Printing All. Coq< Print b. b = plus a (S (S (S (S (S (S O)))))) : nat

  21. 自然数加法 Coq< Check plus. plus : nat -> nat -> nat Coq< Unset Printing All. Coq< Eval compute in (plus 7 8). = 15 : nat

  22. 自然数加法 Coq <Print plus. plus = fix plus (n m : nat) : nat := match n with | 0 => m | S p => S (plus p m) end : nat -> nat -> nat Argument scopes are [nat_scope nat_scope]

  23. 函数式语言编程

  24. 函数式语言编程 • 函数是一等公民 • First-class • 可以做参数,也可以作为函数返回值 • 高阶函数

  25. 示例一:布尔值计算 Coq < Inductive bool : Set := true : bool | false : bool. bool is defined bool_rect is defined bool_ind is defined bool_rec is defined Coq < Print bool.

  26. 示例一:布尔值计算 Coq < Inductive bool : Set := true : bool | false : bool Coq < Definition negb (a : bool) : bool := match a with | true => false | false => true end. Coq < Eval compute in (negb true).

  27. 示例一:布尔值计算 Coq <Inductive bool : Set := true : bool | false : bool Coq< Definition andb (a : bool) (b : bool) : bool := match a with | true => b | false => false end. Coq< Eval compute in (andb true false).

  28. 示例一:布尔值计算 Coq < Inductive bool : Set := true : bool | false : bool Coq < Definition andb (a : bool) (b : bool) : bool := match a with | true => b | false => false end. Coq < Check (andb). Coq < Check (andb true).

  29. 示例一:布尔值计算 Coq < Inductive bool : Set := true : bool | false : bool Coq < Definition andb (a : bool) : bool -> bool := match a with | true => fun (b : bool) => b | false => fun (b : bool) => false end. Coq < Check (andb). Coq < Check (andb true).

  30. 示例一:布尔值计算 Coq < Definition double (f : bool -> bool) : bool -> bool := fun (a : bool) => f (f a). Coq < Eval compute in (double negb true).

  31. 示例二:自然数 Coq < Inductive nat : Set := O : nat | S : nat -> nat. nat is defined nat_rect is defined nat_ind is defined nat_rec is defined

  32. 偶数判定 Fixpoint evenb (n:nat) : bool := match n with | O => true | S O => false | S (S n') => evenb n' end.

  33. 加法 • 原始递归函数(Primitive Recursion) • 保证函数的终止性 Fixpoint plus (n : nat) (m : nat) {struct n}: nat := match n with | O => m | S n’ => S (plus n’ m) end.

  34. 基本逻辑推理

  35. 原子命题定义 Variables A B C : Prop.

  36. 定理证明演示 Theorem T1 : A -> A.

  37. 证明构造的编程

  38. 打印证明 • Curry-Howard 同构 • 类型形如 A ->B 的证明是一个函数 • 以命题A的证明为参数,返回B的证明 Theorem T1 : A -> A. Coq < Print T1. T1 = fun H : A => H : A -> A

  39. 程序即证明 Definition T1’ := fun (H : A) => H. Check T1’.

  40. 程序即证明 • 证明的构造方式并不唯一 • 构造 ——Construction Definition T1’ := fun (H : A) => H. Check T1’. Definition T1’’:= fun (H : A) => T1 H. Check T1’’.

  41. 练习 Theorem T2 : A -> B -> A. Proof ?

  42. 答案 • T2是定理 A->B ->A的“构造” Theorem T2 : A -> B -> A. Proof fun (H : A) => fun (H2 : B) => H.

  43. 合取连接词 Theorem T3 : A /\ B -> A. Proof fun (H : A /\ B) => (proj1 H). Theorem T4 : A /\ B -> B. Proof fun (H : A /\ B) => (proj2 H).

  44. 析取连接词 Theorem T5 : A -> A \/ B. Proof fun (H : A) => or_intro1 H. Theorem T6 : A /\ B -> A \/ B. Proof ?

  45. 析取连接词 Theorem T3 : A /\ B -> A. Theorem T5 : A -> A \/ B. Theorem T6 : A /\ B -> A \/ B. Proof fun (H : A /\ B) => T5 (T3 H).

  46. 全称量词 Theorem T7 : forall A : Prop, A -> A. • Curry-Howard 同构 • 类型形如 forall x :A, B 的证明仍然是一个函数 • 以 x为参数,返回B的证明

  47. 全称量词 Theorem T7 : forall A : Prop, A -> A. Proof fun (A : Prop) => fun (x : A) => x. 【思考】下面证明构造的含义: Definition T4 := T3 (forall A : Prop, A)

  48. 全称量词 Theorem T7 : forall A : Prop, A -> A. 【思考】下面证明构造的含义: Definition T8 := T7 (forall A : Prop, A) T8 : False -> False

  49. 练习 Theorem T9 : forall A B C: Prop, (A -> B) -> (B -> C) -> (A -> C). Proof ?. 函数复合

  50. 归纳

More Related