我在Coq尝试互感,我定义的第一种类型是
Inductive IsEven : nat -> Prop :=
| EvenO : IsEven O
| EvenS n : IsOdd n -> IsEven (S n)
with IsOdd : nat -> Prop :=
| OddS n : IsEven n -> IsOdd (S n).
我现在想要证明偶数之和是偶数。我能够使用Fixpoint和模式匹配来做到这一点:
Fixpoint even_plus_even (n m : nat) (evenn : IsEven n) (evenm : IsEven m) : IsEven (n + m) :=
match evenn with
| EvenO => evenm
| EvenS n' oddn' => EvenS (n' + m) (odd_plus_even n' m oddn' evenm)
end
with odd_plus_even (n m : nat) (oddn : IsOdd n) (evenm : IsEven m) : IsOdd (n + m) :=
match oddn with
| OddS n' evenn' => OddS (n' + m) (even_plus_even n' m evenn' evenm)
end.
这定义了even_plus_even
和odd_plus_even
。我现在想以更简洁的方式使用策略来证明这一点(最好不要使用许多预定义的引理来保持代码尽可能独立)但我还没有走得太远。
具体来说,是否可以像使用Fixpoint一样只使用一个引理来证明even_plus_even
和odd_plus_even
?
编辑:非常感谢您的回答,Lemma ... with ...
语法正是我所寻求的。事实上
Lemma even_plus_even2 (n m : nat) (evenn : IsEven n) (evenm : IsEven m) : IsEven (n + m)
with odd_plus_even2 (n m : nat) (oddn : IsOdd n) (evenm : IsEven m) : IsOdd (n + m).
Proof.
induction evenn; simpl. assumption. constructor. auto.
induction oddn; simpl. constructor. auto.
Defined.
在我的原始问题中生成与Fixpoint
完全相同的证明词。
答案 0 :(得分:3)
Coq支持互感。我知道有两种方法,但我只记得如何使用它:
以下是它的工作原理:
Scheme IsEven_ind2 := Induction for IsEven Sort Prop
with IsOdd_ind2 := Induction for IsOdd Sort Prop.
Combined Scheme IsEvenOdd_ind from IsEven_ind2, IsOdd_ind2.
Lemma foo: (forall (n: nat) (evenn: IsEven n), forall m (evenm: IsEven m), IsEven (n + m) ) /\
(forall (n: nat) (oddn: IsOdd n), forall m (evenm: IsEven m), IsOdd (n + m)).
Proof.
apply IsEvenOdd_ind.
- now intros m hm.
- intros h hn hi m hm.
rewrite plus_Sn_m.
apply EvenS.
now apply hi.
- intros h hn hi m hm.
rewrite plus_Sn_m.
apply OddS.
now apply hi.
Qed.
Lemma
with
在这个问题上,我只是不知道如何完成它,但这是一个语法问题iirc:
Lemma foo: forall (n m: nat) (evenn: IsEven n) (evenm: IsEven m), IsEven (n + m)
with bar: forall (n m: nat) (oddn: IsOdd n) (evenm: IsEven m), IsOdd (n + m).
Proof.
- intros n m hn; revert m; induction hn as [ | p hp]; intros m hm; simpl in *.
+ exact hm.
+ now apply EvenS; apply bar.
- intros n m hn hm; revert n hn; induction hm as [ | p hp]; intros n hn; simpl in *.
+ now apply bar; [ exact hn | apply EvenO ].
+ apply bar; [ exact hn | ].
now apply EvenS.
(* can't Qed, I get a Error: Cannot guess decreasing argument of fix. *)
Qed.
修改强>
以下是Lemma with
解决方案的有效语法。
Lemma foo (n: nat) (evenn: IsEven n): forall (m: nat) (evenm: IsEven m), IsEven (n + m)
with bar (n: nat) (oddn: IsOdd n): forall (m: nat) (evenm: IsEven m), IsOdd (n + m).
Proof.
- induction evenn as [ | p hp]; intros m hm; simpl in *.
+ exact hm.
+ now apply EvenS; apply bar.
- induction oddn as [p hp]; intros n hn; simpl in *.
+ apply OddS.
now apply foo.
Qed.
答案 1 :(得分:2)
例如,您可以通过将它们“并入”到一个引理中来同时证明两个引理,然后使用proj1
和proj2
提取子句的左侧或右侧部分。
Lemma even_or_odd_plus_even: forall (n m : nat),
(forall (evenn : IsEven n) (evenm : IsEven m), IsEven (n + m)) /\
(forall (oddn : IsOdd n) (evenm : IsEven m), IsOdd (n + m)).
Proof.
induction n; split; intros;
try destruct (IHn m) as [He Ho];
try apply evenm;
try inversion oddn;
try inversion evenn;
constructor; auto.
Qed.
Definition even_plus_even n m := proj1 (even_or_odd_plus_even n m).
Definition odd_plus_even n m := proj2 (even_or_odd_plus_even n m).
给你
even_plus_even : forall n m : nat, IsEven n -> IsEven m -> IsEven (n + m)
odd_plus_even : forall n m : nat, IsOdd n -> IsEven m -> IsOdd (n + m)
请注意,这两个子句共享n
和m
,如果这些条款无法单独证明,则需要它们,因为它们需要相互依赖。 (在这种特殊情况下,他们没有。你可以单独证明这些陈述,正如安东所示。)
with
语法(感谢你的表现!),但这是有道理的,我认为这是一种更简洁的方式来做这个相互依赖的定义。
Lemma even_plus_even: forall n m, IsEven n -> IsEven m -> IsEven (n+m)
with odd_plus_even: forall n m, IsOdd n -> IsEven m -> IsOdd (n+m).
Proof.
induction 1; simpl; auto using EvenS.
induction 1; simpl; auto using OddS.
Qed.