简体   繁体   English

如何在coq中证明b = c if(andb b c = orb b c)?

[英]How would I prove that b = c if (andb b c = orb b c) in coq?

I'm new to coq and I'm trying to prove this... 我是coq的新手,我正试图证明这一点......

Theorem andb_eq_orb :
  forall (b c : bool),
  (andb b c = orb b c) -> (b = c).

Here is my proof, but I get stuck when I get to the goal (false = true -> false = true). 这是我的证明,但是当我到达目标时我被卡住了(假=真 - >假=真)。

Proof.
intros b c.
induction c.
destruct b.
reflexivity.
simpl.
reflexivity.

I'm not sure how I would rewrite that expression so I can use reflexivity. 我不确定如何重写那个表达式,所以我可以使用反身性。 But even if I do that, I'm not sure it will lead to the proof. 但即使我这样做,我也不确定它会导致证据。

I was able to solve the prove if I started with the hypothesis b = c though. 如果我从假设b = c开始,我能够解决证明。 Namely... 即...

Theorem andb_eq_orb_rev :
  forall (b c : bool),
  (b = c) -> (andb b c = orb b c).
Proof.
intros.
simpl.
rewrite H.
destruct c.
reflexivity.
reflexivity.
Qed.

But I can't figure out how to solve if I start with the hypothesis that has boolean functions. 但是,如果我从具有布尔函数的假设开始,我无法弄清楚如何解决。

You don't need induction, since bool is not a recursive data structure. 您不需要归纳,因为bool不是递归数据结构。 Just go through the different cases for the values of b and c . 只需通过bc值的不同情况。 Use destruct to do that. 使用destruct来做到这一点。 In two cases the hypothesis H will be of the type true = false , and you can finish the proof with inversion H . 在两种情况下,假设H的类型为true = false ,您可以使用inversion H来完成证明。 In the other two cases, the goal will be of the type true = true and it can be solved with reflexivity . 在另外两种情况下,目标将是true = true类型,并且可以通过reflexivity来解决。

Theorem andb_eq_orb : forall b c, andb b c = orb b c -> b = c.
Proof. destruct b,c;  intro H; inversion H; reflexivity. Qed.

You'll want to use the intro tactic. 你会想要使用intro策略。 This will move false = true into your proof context as an assumption which you can then use to rewrite. 这会将false = true移动到您的证明上下文中,作为您可以用来重写的假设。

This might not be the most efficient way to do it. 这可能不是最有效的方法。

At the step induction c. 在步进induction c. (where it's stuck): (它被卡住了):

______________________________________(1/2)
b && true = b || true -> b = true
______________________________________(2/2)
b && false = b || false -> b = false

You can use rewrite and basic theorems in [bool][1] to simplify terms such as b && true to b , and b || true 您可以在[bool] [1]中使用rewrite和基本定理来简化诸如b && truebb || true术语。 b || true to true . b || truetrue

This can reduce it to two "trivial" sub goals: 这可以将它减少到两个“微不足道”的子目标:

b : bool
______________________________________(1/2)
b = true -> b = true
______________________________________(2/2)
false = b -> b = false

This is almost trivial proof using assumption , except it is one symmetry away. 这几乎是使用assumption微不足道的证明,除了它是一个symmetry You can try if symmetry will make them match using: 您可以try使用symmetry使它们匹配:

try (symmetry;assumption); try assumption.

(Someone who really knows Coq may enlighten me how to try this more succinctly) (真正了解Coq的人可能会告诉我如何更简洁地try这个)

Putting it together: 把它放在一起:

Require Import Bool.
Theorem andb_eq_orb : forall b c, andb b c = orb b c -> b = c.
Proof. 
destruct c; 

try (rewrite andb_true_r);
try (rewrite orb_true_r);
try (rewrite andb_false_r);
try (rewrite orb_false_r);
intro H;
try (symmetry;assumption); try assumption.
Qed.

A second approach is to brute-force it and using the "Truth table" method. 第二种方法是强制它并使用“真值表”方法。 This means you can break down all variables to their truth values, and simplify: destruct b, c; simpl. 这意味着您可以将所有变量分解为其真值,并简化: destruct b, c; simpl. destruct b, c; simpl. . This again gives four trivial implications, up to one symmetry to try : 这再次给出了四个微不足道的含义,最多可以try一个symmetry

4 subgoal
______________________________________(1/4)
true = true -> true = true
______________________________________(2/4)
false = true -> true = false
______________________________________(3/4)
false = true -> false = true
______________________________________(4/4)
false = false -> false = false

Putting it together: 把它放在一起:

Theorem andb_eq_orb1 : forall b c, andb b c = orb b c -> b = c.
Proof. 
destruct b, c; simpl; intro; try (symmetry;assumption); try assumption.
Qed.

The first approach is more troublesome but it does not involve enumerating all truth table rows (I think). 第一种方法更麻烦,但它不涉及枚举所有真值表行(我认为)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM