树结构上Coq的归纳
这是一个非常基本的问题,我很抱歉,但我一直试图用Coq来证明下面的定理,但似乎不知道怎么做树结构上Coq的归纳,coq,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) : bo
(* 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上应用归纳法,但这并不能真正正确地工作。似乎我需要同时对两者应用归纳法,但不知道如何使用。我能够用简单的归纳法证明这一点
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.
通过在
简介
之前应用归纳
我们的归纳假设在tree2上是多态的,这允许我们在最后的案例中使用它。很酷,谢谢,这基本上对我有效,但我不得不做一些调整。首先,我没有定义and B_true_iff——我猜这是在某个库中,但我得到了要点,并为它建立了自己的定理(见上文)。我想我能问的最重要的问题是:当IHtree1_1和IHtree1_2都指tree2而不是像tree2_1或(Node tree2_1 tree2_2)之类的东西时,为什么我可以使用IHtree1_1和IHtree1_2?我觉得答案将被你的短语“树上的多态性2”所包围,它允许我们在最后的案例中使用它。树2没有定义吗?酷。需要导入Bool。成功了。我真的,真的是Coq的新手(你可能已经收集到了),所以像这样应该很明显的事情还没有发生。@quadelirus没有,这是我的错:)我只是从emacs复制和粘贴,没有意识到我的导入是在顶部