我如何在coq中证明函数f
接受bool true|false
并返回bool true|false
(如下所示),当应用两次bool {{ 1}}总会返回相同的值true|false
:
true|false
例如,函数(f:bool -> bool)
只能做4件事,让我们调用函数f
的输入:
b
true
false
(如果b为真则返回true,反之亦然)b
(即如果b为真,则返回false)因此,如果函数始终返回true:
not b
如果函数总是返回false,我们会得到:
f (f bool) = f true = true
对于其他情况,我们假设该函数返回f (f bool) = f false = false
not b
在两种可能的输入情况下,我们总是最终得到原始输入。如果我们假设函数返回f (f true) = f false = true
f (f false) = f true = false
,则同样成立。
那么您如何在coq中证明这一点?
b
答案 0 :(得分:10)
Goal forall (f:bool -> bool) (b:bool), f (f (f b)) = f b.
Proof.
intros.
remember (f true) as ft.
remember (f false) as ff.
destruct ff ; destruct ft ; destruct b ;
try rewrite <- Heqft ; try rewrite <- Heqff ;
try rewrite <- Heqft ; try rewrite <- Heqff ; auto.
Qed.
答案 1 :(得分:4)
一点点证明:
Require Import Sumbool.
Goal forall (f : bool -> bool) (b:bool), f (f (f b)) = f b.
Proof.
destruct b; (* case analysis on [b] *)
destruct (sumbool_of_bool (f true)); (* case analysis on [f true] *)
destruct (sumbool_of_bool (f false)); (* case analysis on [f false] *)
congruence. (* equational reasoning *)
Qed.
答案 2 :(得分:4)
在SSReflect中:
Require Import ssreflect.
Goal forall (f:bool -> bool) (b:bool), f (f (f b)) = f b.
Proof.
move=> f.
by case et:(f true); case ef:(f false); case; rewrite ?et ?ef // !et ?ef.
Qed.
答案 3 :(得分:2)
感谢您的精彩任务!这么可爱的定理!
这是使用Coq的C-zar声明性证明样式的证明。它比命令式的要长得多(虽然因为我的技能太低而可能是这样)。
Theorem bool_cases : forall a, a = true \/ a = false. proof. let a:bool. per cases on a. suppose it is false. thus thesis. suppose it is true. thus thesis. end cases. end proof. Qed. Goal forall (b:bool), f (f (f b)) = f b. proof. let b:bool. per cases on b. suppose it is false. per cases of (f false = false \/ f false = true) by bool_cases. suppose (f false = false). hence (f (f (f false)) = f false). suppose H:(f false = true). per cases of (f true = false \/ f true = true) by bool_cases. suppose (f true = false). hence (f (f (f false)) = f false) by H. suppose (f true = true). hence (f (f (f false)) = f false) by H. end cases. end cases. suppose it is true. per cases of (f true = false \/ f true = true) by bool_cases. suppose H:(f true = false). per cases of (f false = false \/ f false = true) by bool_cases. suppose (f false = false). hence (f (f (f true)) = f true) by H. suppose (f false = true). hence (f (f (f true)) = f true) by H. end cases. suppose (f true = true). hence (f (f (f true)) = f true). end cases. end cases. end proof. Qed.