鸽笼原理是集合论中非常经典的结论啊.在《Software Foundations》系列教材中,有一道练习题就是使用排中律证明鸽笼原理.这并不困难,请看下面:
Lemma in_split : forall (X:Type) (x:X) (l:list X),
In x l ->
exists l1 l2, l = l1 ++ x :: l2.
Proof.
intros X.
intros x l.
generalize dependent x.
induction l as [|y l' IHl].
- intros x contra. inversion contra.
- intros x H. inversion H.
+ exists []. exists l'. simpl. rewrite H0. reflexivity.
+ apply IHl in H0. destruct H0 as [l1 [l2 H1]].
exists (y::l1). exists l2. rewrite H1. simpl. reflexivity.
Qed.
Inductive repeats {X:Type} : list X -> Prop :=
|Repeated (x: X) (l:list X) (H:repeats l) : repeats (x::l)
|OneIn (x: X) (l:list X) (H:In x l) : repeats (x::l)
.
Theorem pigeonhole_principle: excluded_middle ->
forall (X:Type) (l1 l2:list X),
(forall x, In x l1 -> In x l2) ->
length l2 < length l1 ->
repeats l1.
Proof.
intros EM X l1. induction l1 as [|x l1' IHl1'].
- intros l2. intros _. intros contra. exfalso.
simpl in contra. unfold lt in contra. inversion contra.
- intros l2. intros Hin. intros Hlt.
assert(HxIn: In x l2). {specialize (Hin x). apply Hin. simpl. left. reflexivity. }
apply in_split in HxIn. destruct HxIn as [l1 [l3 Happ]].
specialize (IHl1' (l1++l3)). rewrite Happ in Hlt.
rewrite cons_to_app in Hlt. rewrite app_length in Hlt. rewrite app_length in Hlt.
simpl in Hlt. rewrite<- plus_n_Sm in Hlt. apply Sn_le_Sm__n_le_m in Hlt.
rewrite <-app_length in Hlt.
assert(Hn:~(In x l1') -> repeats l1').
{
intros Nin. apply IHl1'.
+ intros x0 Inx0. specialize (Hin x0).
assert(HInx0p : In x0 (x::l1')). {simpl. right. apply Inx0. }
apply Hin in HInx0p. rewrite Happ in HInx0p. rewrite cons_to_app in HInx0p.
apply In_app_iff in HInx0p. destruct HInx0p as [Hl1|Ht].
* apply In_app_iff. left. apply Hl1.
* apply In_app_iff in Ht. destruct Ht as[Hx0|Hl3].
- inversion Hx0.
{rewrite <- H in Inx0. apply Nin in Inx0. destruct Inx0. }
{inversion H. }
- apply In_app_iff. right. apply Hl3.
+ apply Hlt.
}
assert(HIn: (In x l1')\/ ~(In x l1')). {unfold excluded_middle in EM. apply EM. }
destruct HIn as [H1|H2].
+ apply OneIn. apply H1.
+ apply Hn in H2. apply Repeated. apply H2.
Qed.
简单来说,它做的是用list来模拟集合,然后来判断list是否重复.然而,只需稍微一看就可以发现,这个排中律几乎没地方可以去掉.原因是:如果我们要证明最后的结论repeats l1,则要么证明In x l1'要么证明repeats l1',这里天然有一个或结构,我必须拿一个或结构destruct后才有可能将它们逐个击破.
一个稍显自然的策略应该是对l2做destruct,然而此时我们的条件变成了allin (x::l1') (y::l2'),这个几乎无法再进一步操作了(网上有个做法是强行分类讨论这个地方接下来该怎么做,写了超长一坨不明所以的命题然后硬证).
然而,考虑其的一个弱一点的推论应当是In x (y::l2'),这个条件直接做inversion可以得到x=y\/ In x l2',这俩看上去友善多了,可还是没有解决这个问题啊?
然而,此时你应当已经发现端倪了:我们得到的最好用的结论好像还是x=y啊,可既然我们已经知道In x l2可以将l2=l3++[x]++l4,我为什么不直接把它的顺序也变了呢?
如果我们把l2的顺序变换,最简单地,搞一个(x::l4)++l3,那我们拿到的就是allin (x::l1') (x::l4)++l3,这个可太棒了!
Definition allin {X:Type} (l1 l2:list X) :Prop := forall (x:X), In x l1-> In x l2.
Lemma empty_allin{X:Type}: forall (l:list X),allin [] l.
Proof.
intros l.
unfold allin.
intros x.
intros contra.
inversion contra.
Qed.
Lemma part_allin{X:Type} : forall (x:X) (l1 l2:list X),
allin (x::l1) l2 ->
allin l1 l2.
Proof.
intros x l1.
generalize dependent x.
induction l1 as [|y l1' IHl1].
- intros * *. intros _. apply empty_allin.
- intros x l2. intros Hallin. unfold allin in Hallin.
unfold allin.
intros x0.
intros Hin.
inversion Hin.
+ apply Hallin. rewrite <-H. simpl. right. left. reflexivity.
+ unfold allin in IHl1.
assert(HW: forall (x0:X), In x0 l1' -> In x0 l2).
{
specialize (IHl1 y l2).
intros x1.
intros Hx1in.
apply IHl1.
- intros x2 Hx2in. apply Hallin. simpl. inversion Hx2in.
+ right. left. apply H0.
+ right. right. apply H0.
- apply Hx1in.
}
apply HW in H. apply H.
Qed.
Lemma cons_allin{X:Type} : forall(x:X) (l1 l2:list X),
allin l1 l2 ->
In x l2 ->
allin (x::l1) l2.
Proof.
intros. unfold allin. intros.
inversion H1.
- rewrite <-H2. apply H0.
- unfold allin in H. specialize (H x0). apply H in H2. apply H2.
Qed.
Lemma same_cons_allin{X:Type} : forall (x:X) (l1 l2:list X),
allin (x::l1) (x::l2)->
(allin l1 l2) \/ (In x l1).
Proof.
intros x l1.
generalize dependent x.
induction l1 as [|x1 l1' IHl1].
- intros. left. apply empty_allin.
- intros x l2. intros Hin.
assert(Hx1in : In x1 (x::l2)). {unfold allin in Hin. specialize (Hin x1). apply Hin. simpl. right. left. reflexivity. }
inversion Hx1in.
+ right. simpl. left. symmetry. apply H.
+ assert(Hx: allin (x::l1') (x::l2)).
{
apply cons_allin.
apply part_allin in Hin. apply part_allin in Hin.
apply Hin.
simpl. left. reflexivity.
}
specialize (IHl1 x l2).
apply IHl1 in Hx. destruct Hx as [H1|H2].
* left. apply cons_allin. apply H1. apply H.
* right. simpl. right. apply H2.
Qed.
这正是我们想要的结论,于是剩下的任务只在如何交换了,请看:
Lemma In_swap {X:Type} : forall (x:X)(l2 l3:list X),
In x (l2++l3)->
In x (l3++l2).
Proof.
intros.
apply In_app_iff. apply In_app_iff in H. destruct H as [H1|H2].
- right. apply H1.
- left. apply H2.
Qed.
Lemma allin_swap {X:Type} : forall (l1 l2 l3:list X),
allin l1 (l2++l3)->
allin l1 (l3++l2).
Proof.
intros l1.
induction l1 as [|x l1 IHl1].
- intros. apply empty_allin.
- intros. assert(Hin : In x (l3++l2)).
{
unfold allin in H.
specialize (H x).
apply In_swap. apply H. simpl. left. reflexivity.
}
apply cons_allin.
apply IHl1. apply part_allin in H. apply H. apply Hin.
Qed.
Lemma swap_to_first{X:Type} : forall (x:X) (l1 l2:list X),
allin (x::l1) l2 ->
(exists (l3 l4:list X), l2=l3++(x::l4)/\allin (x::l1) ((x::l4)++l3)).
Proof.
intros x l1 l2.
generalize dependent x.
generalize dependent l1.
induction l2 as [|y l2 IHl2].
- intros. unfold allin in H. specialize (H x).
assert(contra: In x []). {apply H. simpl. left. reflexivity. }
inversion contra.
- intros. assert(Hin: In x (y::l2)). {unfold allin in H. specialize (H x). apply H. simpl. left. reflexivity. }
apply in_split in Hin. destruct Hin as [l3[l4 Heq]].
exists l3. exists l4. split.
+ apply Heq.
+ rewrite Heq in H. apply allin_swap in H. apply H.
Qed.
我们于是做完了全部的任务,最后的定理收尾请看:
Theorem pigeonhole_principle_no_em:
forall (X:Type) (l1 l2:list X),
(forall x, In x l1 -> In x l2) ->
length l2 < length l1 ->
repeats l1.
Proof.
intros X l1. induction l1 as [|x l1' IHl1'].
- intros l2. intros _. intros contra. exfalso.
simpl in contra. unfold lt in contra. inversion contra.
- intros l2. intros Hin. intros Hlt.
assert(Hallin : allin (x::l1') l2). {unfold allin. apply Hin. }
assert(Hans: In x l1' \/ repeats l1').
{
assert(Hxin : In x l2). {specialize (Hin x). apply Hin. simpl. left. reflexivity. }
apply swap_to_first in Hallin.
destruct Hallin as [l3[l4[H1 H2]]].
simpl in H2.
apply same_cons_allin in H2.
destruct H2 as [H2|H2].
+ rewrite H1 in Hlt. rewrite cons_to_app in Hlt. rewrite app_length in Hlt. rewrite app_length in Hlt.
simpl in Hlt. rewrite <-plus_n_Sm in Hlt. apply Sn_le_Sm__n_le_m in Hlt.
right. apply (IHl1' (l4++l3)).
unfold allin in H2. apply H2.
rewrite app_length.
rewrite add_comm. apply Hlt.
+ left. apply H2.
}
destruct Hans as [H1|H2].
+ apply OneIn. apply H1.
+ apply Repeated. apply H2.
Qed.