摘录自《Software Foundation》,旨在交流与学习Coq的基本用法; 强烈建议大家下载源码自己运行一遍
文章目录
Induction
通过Simplification证明的局限性
前面已经通过simplification证明了0 + n = n
,现在尝试证明n = n + 0
Theorem plus_n_O_firsttry : forall n:nat,
n = n + 0.
Proof.
intros n.
simpl. (* Does nothing! *)
Abort.
可以看到,reflexivity
在这里无能为力,因为n
和n + 0
都是未知数,所以plus
函数的match
无法匹配。如果用destruct
呢?
Theorem plus_n_O_secondtry : forall n:nat,
n = n + 0.
Proof.
intros n. destruct n as [| n'].
- (* n = 0 *)
reflexivity. (* so far so good... *)
- (* n = S n' *)
simpl. (* ...but here we are stuck again *)
Abort.
运行以上代码会发现,destruct
将n
分为O
(也就是0)与S n'
(也就是非0)两种情况。n
为0时可以证明,但n = S n'
时仍然证明不了——即使将n'
再destruct
。
回忆数学归纳法的使用。Coq中也提供了类似的证明方法。
用induction进行归纳证明
注意这里的
induction
不是定义类型的inducive
Coq中提供了一个叫induction
的tactic,它把上述定理的证明分成两个子目标:
- 证明
P(O)
成立 - 证明
P(n')
成立可以推导出P(S n')
下面是证明过程
Theorem plus_n_O : forall n:nat, n = n + 0.
Proof.