Coq在树结构上的感应

时间:2013-11-04 04:39:53

标签: coq

这是一个非常基本的问题,我道歉,但我一直在尝试使用Coq来证明以下定理,而且似乎无法弄清楚如何去做。

(* Binary tree definition. *)
Inductive btree : Type := 
  | EmptyTree
  | Node : btree -> btree -> btree.
(* Checks if two trees are equal. *)

Fixpoint isEqual (tree1 : btree) (tree2 : btree) : bool :=
  match tree1, tree2 with
    | EmptyTree, EmptyTree => true
    | EmptyTree, _ => false
    | _, EmptyTree => false
    | Node n11 n12, Node n21 n22 => (andb (isEqual n11 n21) (isEqual n12 n22))
end.

Lemma isEqual_implies_equal : forall tree1 tree2 : btree, 
(isEqual tree1 tree2) = true -> tree1 = tree2.

我一直在尝试做的是在tree1上应用归纳,然后是tree2,但这并不能正常工作。似乎我需要同时应用归纳,但无法弄清楚如何。

1 个答案:

答案 0 :(得分:1)

我能够使用简单的归纳法证明这一点

Require Import Bool. (* Sorry! Forgot to add that the first time *)

Lemma isEqual_implies_equal : forall tree1 tree2 : btree, 
(isEqual tree1 tree2) = true -> tree1 = tree2.
  induction tree1, tree2; intuition eauto.
  inversion H.
  inversion H.
  apply andb_true_iff in H.
  intuition eauto; fold isEqual in *.
  apply IHtree1_1 in H0.
  apply IHtree1_2 in H1.
  congruence.
Qed.

(* An automated version *)
Lemma isEqual_implies_equal' : forall tree1 tree2 : btree, 
(isEqual tree1 tree2) = true -> tree1 = tree2.
  induction tree1, tree2; intuition; simpl in *;
  repeat match goal with
            | [ H : false = true |- _ ]   => inversion H
            | [ H : (_ && _) = true |- _] => apply andb_true_iff in H; intuition
            | [ IH : context[ _ = _ -> _], 
                H : _ = _ |- _]           => apply IH in H
         end; congruence.
  Qed.

通过在induction之前应用intros,我们的归纳假设比tree2具有多态性,这允许我们在最后的情况下使用它。