证明 f (f bool) = bool

发布于 2024-08-09 18:31:15 字数 938 浏览 2 评论 0原文

我如何在 coq 中证明函数 f 接受 bool true|false 并返回 bool true|false (如下所示) ,当对单个 bool true|false 应用两次时,将始终返回相同的值 true|false

(f:bool -> bool)

例如,函数 f 只能执行以下操作: 4件事,让我们调用函数b的输入:

  • 总是返回true
  • 总是返回false
  • 返回b(即如果 b 为 true,则返回 true,反之亦然)
  • 返回 not b(即,如果 b 为 true,则返回 false,反之亦然)

因此,如果函数始终返回 true:

f (f bool) = f true = true

如果函数始终返回 false,我们将得到:

f (f bool) = f false = false

对于其他情况,假设函数返回不是 b

f (f true) = f false = true
f (f false) = f true = false

在两种可能的输入情况下,我们总是以原始输入结束。如果我们假设函数返回 b,情况也是如此。

那么如何在 coq 中证明这一点呢?

Goal forall (f:bool -> bool) (b:bool), f (f b) = f b.

How can I in coq, prove that a function f that accepts a bool true|false and returns a bool true|false (shown below), when applied twice to a single bool true|false would always return that same value true|false:

(f:bool -> bool)

For example the function f can only do 4 things, lets call the input of the function b:

  • Always return true
  • Always return false
  • Return b (i.e. returns true if b is true vice versa)
  • Return not b (i.e. returns false if b is true and vice vera)

So if the function always returns true:

f (f bool) = f true = true

and if the function always return false we would get:

f (f bool) = f false = false

For the other cases lets assum the function returns not b

f (f true) = f false = true
f (f false) = f true = false

In both possible input cases, we we always end up with with the original input. The same holds if we assume the function returns b.

So how would you prove this in coq?

Goal forall (f:bool -> bool) (b:bool), f (f b) = f b.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

清音悠歌 2024-08-16 18:31:15
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.
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.
无悔心 2024-08-16 18:31:15

稍微短一点的证明:

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.

A tad shorter proof:

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.
醉态萌生 2024-08-16 18:31:15

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.

In 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.
流年里的时光 2024-08-16 18:31:15

感谢精彩的任务!多么可爱的定理啊!

这是使用 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.

Thanks for wonderful assignment! Such a lovely theorem!

This is the proof using C-zar declarative proof style for Coq. It is a much longer than imperative ones (altrough it might be such because of my too low skill).

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.
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文