-
Notifications
You must be signed in to change notification settings - Fork 1
/
SetSpecs.v
227 lines (167 loc) · 8.97 KB
/
SetSpecs.v
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
(* -----------------Description --------------------------------------------------
This file summarises some useful results from List Module of standard Library.
We create a hint database to remember important results regarding lists (hint_list).
Some new definitions:
Definition Empty (s:list A):Prop := forall a : A, ~ In a s.
Definition Equal (s s': list A) := forall a : A, In a s <-> In a s'.
Definition Subset (s s': list A) := forall a : A, In a s -> In a s'.
Notation "s [=] t" := (Equal s t) (at level 70, no associativity).
Notation "s [<=] t" := (Subset s t) (at level 70, no associativity).
Notation "| s |":= (length s) (at level 70, no associativity).
-------------------- ------------------ ------------------------------------- *)
From Coq Require Export ssreflect ssrbool.
Require Export Lists.List Omega.
Set Implicit Arguments.
Hint Resolve in_eq in_cons in_inv in_nil in_dec: core.
Section BasicListFacts.
Variable A:Type.
Lemma in_inv1 : forall (a b:A) (l:list A), In b (a :: l) -> b = a \/ In b l.
Proof. { intros a b l H. cut (a=b \/ In b l).
2: { auto. } intros H1; destruct H1 as [H1 | H2].
left; symmetry; auto. right;auto. } Qed.
Lemma in_inv2: forall (x a:A) (l:list A), In x (a::l)-> x <> a -> In x l.
Proof. { intros x a l H. cut (x=a \/ In x l). intro H1;destruct H1 as [Hl1|Hr1].
intro;contradiction. auto. eapply in_inv1;auto. } Qed.
Lemma in_inv3: forall (x a:A) (l:list A), In x (a::l)-> ~ In x l -> x = a.
Proof. { intros x a l H. cut (x=a \/ In x l). intro H1;destruct H1 as [Hl1|Hr1].
intro;auto. intro;contradiction. eapply in_inv1;auto. } Qed.
Hint Resolve in_inv1 in_inv2 in_inv3 : core.
(*---------Some facts about NoDup on a list --------------------------------*)
Lemma nodup_intro (a:A)(l: list A): ~ In a l -> NoDup l -> NoDup (a::l).
Proof. intros H1 H2; eapply NoDup_cons_iff;tauto. Qed.
Lemma nodup_elim1 (a:A)(l: list A): NoDup (a::l)-> NoDup (l).
Proof. intro H. eapply NoDup_cons_iff; eauto. Qed.
Lemma nodup_elim2 (a:A)(l: list A): NoDup (a::l) -> ~ In a l.
Proof. intro H. eapply NoDup_cons_iff; eauto. Qed.
Hint Immediate nodup_elim1 nodup_elim2 nodup_intro : core.
End BasicListFacts.
Hint Resolve in_inv1 in_inv2 in_inv3: core.
Hint Immediate nodup_elim1 nodup_elim2 nodup_intro : core.
Section SetSpec.
Variable A:Type.
Definition Empty (s:list A):Prop := forall a : A, ~ In a s.
Definition Subset (s s': list A) := forall a : A, In a s -> In a s'.
Definition Equal (s s': list A):= Subset s s' /\ Subset s' s.
(* Inductive Forall (A : Type) (P : A -> Prop) : list A -> Prop :=
| Forall_nil : Forall P nil
| Forall_cons : forall (x : A) (l : list A), P x -> Forall P l -> Forall P (x :: l) *)
(* Inductive Exists (A : Type) (P : A -> Prop) : list A -> Prop :=
|Exists_cons_hd : forall (x : A) (l : list A), P x -> Exists P (x :: l)
| Exists_cons_tl : forall (x : A) (l : list A), Exists P l -> Exists P (x :: l) *)
(* Inductive NoDup (A : Type) : list A -> Prop :=
| NoDup_nil : NoDup nil
| NoDup_cons : forall (x : A) (l : list A), ~ In x l -> NoDup l -> NoDup (x :: l) *)
End SetSpec.
Ltac unfold_spec := try(unfold Equal);try(unfold Subset);try(unfold Empty).
Notation "s [=] t" := (Equal s t) (at level 70, no associativity).
Notation "s [<=] t" := (Subset s t) (at level 70, no associativity).
Notation "| s |":= (length s) (at level 70, no associativity).
Section BasicSetFacts.
Variable A:Type.
(*-----------------------Subset (spec) and its properties ------------------------*)
Lemma Subset_intro (a:A)(l s: list A): l [<=] s -> (a::l) [<=] (a::s).
Proof. intros H x H1. destruct H1. subst a;auto. auto. Qed.
Lemma Subset_intro1 (a:A)(l s: list A): l [<=] s -> l [<=] (a::s).
Proof. intros H x H1. simpl;right;auto. Qed.
Lemma Subset_elim1 (a:A) (s s':list A): Subset (a:: s) s'-> In a s'.
Proof. { unfold Subset. intro H. apply H. auto. } Qed.
Lemma Subset_elim2 (a:A) (s s':list A): Subset (a:: s) s'-> Subset s s'.
Proof. { unfold Subset. intro H. intros a1 H1.
apply H. auto. } Qed.
Lemma self_incl (l:list A): l [<=] l.
Proof. unfold Subset; tauto. Qed.
Hint Resolve self_incl: core.
Lemma Subset_nil (l: list A): nil [<=] l.
Proof. unfold "[<=]"; simpl; intros; contradiction. Qed.
Lemma Subset_of_nil (l: list A): l [<=] nil -> l=nil.
Proof. induction l. auto. intro H. absurd (In a nil); auto. Qed.
Lemma Subset_trans (l1 l2 l3: list A): l1 [<=] l2 -> l2 [<=] l3 -> l1 [<=] l3.
Proof. intros H H1 x Hx1. eauto. Qed.
Hint Extern 0 (?x [<=] ?z) =>
match goal with
| H: (x [<=] ?y) |- _ => apply (@Subset_trans x y z)
| H: (?y [<=] z) |- _ => apply (@Subset_trans x y z)
end : core.
(* ---------------------- Equal (spec) and their properties--------------------*)
Lemma Eq_refl (s: list A): s [=] s.
Proof. unfold Equal. split;auto using self_incl. Qed.
Lemma Eq_sym (s s':list A): s [=] s' -> s' [=] s.
Proof. unfold Equal. tauto. Qed.
Lemma Eq_trans1 ( x y z : list A) : x [=] y -> y [=] z -> x [=] z.
Proof. { unfold Equal. intros H H1. destruct H as [H0 H]; destruct H1 as [H1a H1].
split; auto. } Qed.
Hint Extern 0 (?x [=] ?z) =>
match goal with
| H: (x [=] ?y) |- _ => apply (@Eq_trans1 x y z)
| H: (?y [=] z) |- _ => apply (@Eq_trans1 x y z)
end : core.
Lemma Equal_intro (s s': list A): s [<=] s' -> s' [<=] s -> s [=] s'.
Proof. unfold "[=]". tauto. Qed.
Lemma Equal_intro1 (s s': list A): s = s' -> Equal s s'.
Proof. intro; subst s; apply Eq_refl; auto. Qed.
Lemma Equal_elim ( s s': list A): s [=] s' -> s [<=] s' /\ s' [<=] s.
Proof. unfold_spec; unfold iff. intros H; split; intro a;apply H. Qed.
(* ---------------- introduction and elimination for filter operation---------- *)
(* Check filter : forall A : Type, (A -> bool) -> list A -> list A *)
(* Check filter_In : forall (A : Type) (f : A -> bool) (x : A) (l : list A),
In x (filter f l) <-> In x l /\ f x = true *)
Lemma filter_elim1 (f: A->bool)(l: list A)(x: A): In x (filter f l)-> In x l.
Proof. apply filter_In. Qed.
Lemma filter_elim2 (f: A->bool)(l: list A)(x: A): In x (filter f l)-> (f x).
Proof. apply filter_In. Qed.
Lemma filter_intro (f: A->bool)(l: list A)(x: A): In x l -> (f x)-> In x (filter f l).
Proof. intros; apply filter_In; split;auto. Qed.
Hint Immediate filter_elim1 filter_elim2 filter_intro: core.
(*--------Strong Induction, Well founded induction and set cardinality ----------*)
Theorem strong_induction: forall P : nat -> Prop,
(forall n : nat, (forall k : nat, (k < n -> P k)) -> P n) ->
forall n : nat, P n.
Proof. { intros P Strong_IH n.
pose (Q:= fun (n: nat)=> forall k:nat, k<= n -> P k).
assert (H: Q n);unfold Q.
{ induction n.
{ intros k H;apply Strong_IH.
intros k0 H1. cut (k0 < 0). intro H2; inversion H2. omega. }
{ intros k H0.
assert (H1: k < (S n) \/ k = (S n) ). apply le_lt_or_eq; auto.
elim H1. intro. apply IHn. omega.
intro H. subst k. apply Strong_IH. intros. apply IHn. omega. } }
unfold Q in H. apply H. omega. } Qed.
Definition lt_set (l1 l2: list A):= |l1| < |l2|.
Lemma lt_set_is_well_founded: well_founded lt_set.
Proof. { unfold well_founded. intro a.
remember (|a|) as n. revert Heqn. revert a.
induction n using strong_induction.
{ intros a H1. apply Acc_intro.
intros a0 H2. apply H with (k:= |a0|).
subst n; apply H2. auto. } } Qed.
Lemma non_zero_size (a:A)(l: list A): In a l -> |l| > 0.
Proof. { induction l.
{ simpl; tauto. }
{ intros. simpl. omega. } } Qed.
Lemma non_nil_size (l: list A): |l| > 0 -> l<>nil.
Proof. { induction l.
{ simpl. intro H. omega. }
{ intros. intro. assert (H1: In a nil).
rewrite <- H0. auto. destruct H1. } } Qed.
Lemma element_list (l: list A)(a b : A): a<>b -> In b (a::l) -> In b l.
Proof. intros H1 H2. simpl in H2. destruct H2. destruct H1;auto. exact. Qed.
Hint Resolve non_zero_size: core.
End BasicSetFacts.
Hint Extern 0 (?x [<=] ?z) =>
match goal with
| H: (x [<=] ?y) |- _ => apply (@Subset_trans _ x y z)
| H: (?y [<=] z) |- _ => apply (@Subset_trans _ x y z)
end : core.
Hint Extern 0 (?x [=] ?z) =>
match goal with
| H: (x [=] ?y) |- _ => apply (@Eq_trans1 _ x y z)
| H: (?y [=] z) |- _ => apply (@Eq_trans1 _ x y z)
end : core.
Hint Immediate Eq_refl Eq_sym Equal_elim Equal_intro Equal_intro1: core.
Hint Immediate Subset_elim1 Subset_elim2 Subset_nil Subset_of_nil: core.
Hint Resolve self_incl: core.
Hint Resolve Subset_intro Subset_intro1: core.
Hint Immediate filter_elim1 filter_elim2 filter_intro: core.
Hint Resolve lt_set_is_well_founded: core.
Hint Resolve non_zero_size element_list: core.