-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathpaa_2021_2_coq.v
337 lines (291 loc) · 7.66 KB
/
paa_2021_2_coq.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
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
(** * Projeto e Análise de Algoritmos *)
(** ** O algoritmo de ordenação por inserção
*)
Require Import List.
Require Import Arith.
(**
Definição da função de inserção
*)
Fixpoint insere x l :=
match l with
| nil => x :: nil
| h :: tl => if (x <=? h) then x :: l else h :: (insere x tl)
end.
Eval compute in (insere 3 (1::2::nil)).
Eval compute in (insere 3 (2::1::nil)).
Inductive sorted: list nat -> Prop :=
| sorted_nil: sorted nil
| sorted_one: forall x, sorted (x::nil)
| sorted_all: forall l x y, x <=? y = true -> sorted (y::l) -> sorted (x::y::l).
Lemma insere_preserva_ordem: forall l x, sorted l -> sorted (insere x l).
(* begin hide *)
Proof.
induction l. (* A prova é por indução em l... *)
- intro x.
intro H.
simpl.
apply sorted_one.
- intros x H.
simpl.
destruct (x <=? a) eqn:Hle.
+ apply sorted_all.
* assumption.
* exact H.
+ generalize dependent l.
intro l. case l.
* intros IH H.
simpl.
apply sorted_all.
** clear IH H.
apply Nat.leb_gt in Hle.
apply Nat.leb_le.
apply Nat.lt_le_incl.
assumption.
** apply sorted_one.
* intros n l' IH H.
simpl in *.
destruct (x <=? n) eqn:Hle'.
** apply sorted_all.
*** apply Nat.leb_gt in Hle.
apply Nat.leb_le.
apply Nat.lt_le_incl.
assumption.
*** apply sorted_all.
**** assumption.
**** inversion H; subst.
assumption.
** inversion H; subst.
apply sorted_all.
*** assumption.
***specialize (IH x).
rewrite Hle' in IH.
apply IH.
assumption.
Qed.
(* end hide *)
Fixpoint ord_insercao l :=
match l with
| nil => nil
| h :: tl => insere h (ord_insercao tl)
end.
Eval compute in (ord_insercao (3::2::1::nil)).
Lemma ord_insercao_preserva_ordem: forall l, sorted (ord_insercao l).
Proof.
induction l.
- simpl.
apply sorted_nil.
- simpl.
apply insere_preserva_ordem.
apply IHl.
Qed.
(*
Inductive Permutation': list nat -> list nat -> Prop :=
perm'_nil : Permutation' nil nil
| perm'_skip : forall (x : nat) (l l' : list nat),
Permutation' l l' -> Permutation' (x :: l) (x :: l')
| perm'_swap : forall (x y : nat) (l : list nat),
Permutation' (y :: x :: l) (x :: y :: l)
| perm'_trans : forall l l' l'' : list nat,
Permutation' l l' ->
Permutation' l' l'' -> Permutation' l l''.
Lemma Permutation'_refl: forall l, Permutation' l l.
Proof.
induction l.
- apply perm'_nil.
- apply perm'_skip.
apply IHl.
Qed.
Lemma Permutation'_insere: forall l a, Permutation' (a :: l) (insere a l).
Proof.
induction l.
- intro a.
simpl.
apply Permutation'_refl.
- intros a'.
simpl.
destruct (a' <=? a).
+ apply Permutation'_refl.
+ apply perm'_trans with (a::a'::l).
* apply perm'_swap.
* apply perm'_skip.
apply IHl.
Qed.
Lemma Permutation'_insere_diff: forall l l' a, Permutation' l l' -> Permutation' (a :: l) (insere a l').
Proof.
intros l l' a H.
apply perm'_skip with (x := a) in H.
apply perm'_trans with (a::l').
- assumption.
- apply Permutation'_insere.
Qed.
*)
Require Import Permutation.
Print Permutation.
Lemma Permutation_insere: forall l a, Permutation (a :: l) (insere a l).
Proof.
induction l.
- intro a.
simpl.
apply Permutation_refl.
- intros a'.
simpl.
destruct (a' <=? a).
+ apply Permutation_refl.
+ apply perm_trans with (a::a'::l).
* apply perm_swap.
* apply perm_skip.
apply IHl.
Qed.
Lemma Permutation_insere_diff: forall l l' a, Permutation l l' -> Permutation (a :: l) (insere a l').
Proof.
intros l l' a H.
rewrite H.
apply Permutation_insere.
Qed.
Lemma ord_insercao_Permutation: forall l, Permutation l (ord_insercao l).
Proof.
induction l.
- simpl.
apply perm_nil.
- simpl.
apply Permutation_insere_diff.
apply IHl.
Qed.
Theorem correcao_ord_insercao: forall l, sorted (ord_insercao l) /\ Permutation l (ord_insercao l).
Proof.
intro l; split.
- apply ord_insercao_preserva_ordem.
- apply ord_insercao_Permutation.
Qed.
Fixpoint num_oc x l := match l with
| nil => 0
| h::tl => if (x =? h) then S(num_oc x tl) else num_oc x tl
end.
Eval compute in (num_oc 2 (1::2::3::2::2::nil)).
Definition perm' l l' := forall x, num_oc x l = num_oc x l'.
Lemma num_oc_insere: forall l l' x a, perm' l l' -> (if x =? a then S (num_oc x l) else num_oc x l) =
num_oc x (insere a l').
Proof.
Admitted.
(* Abordagem alternativa:
Lemma num_oc_insere_eq: num_oc a (insere a l) = S(num_oc a l)
Lemma num_oc_insere_diff: x <> a -> num_oc x (insere a l)
*)
Lemma ord_insercao_perm': forall l, perm' l (ord_insercao l).
Proof.
induction l.
- simpl.
unfold perm'.
intro x.
reflexivity.
- simpl.
unfold perm' in *.
intro x.
simpl.
apply num_oc_insere.
unfold perm'.
intro x'.
apply IHl.
Qed.
(** * Equivalência entre Permutation e perm' *)
(** Exercício: (4 pontos) prazo: 23h59 da segunda, dia 14. *)
Lemma Permutation_implica_perm': forall l l', Permutation l l' -> perm' l l'.
Proof.
induction 1.
(* intros l l' H.
induction H. *)
Admitted.
(** Desafio: pontuação extra: 10 pontos - primeiro grupo. Prazo: até 13 de março de 2022. *)
Lemma perm'_nil: forall l, perm' nil l -> l = nil.
Proof.
Admitted.
(* Ideia que pode não ser a melhor. *)
Lemma perm'_exists: forall l l' a, perm' (a :: l) l' -> exists l1 l2, l' = l1++a::l2.
Proof.
Admitted.
Lemma perm'_implica_Permutation: forall l l', perm' l l' -> Permutation l l'.
Proof.
induction l.
- intros l' H.
apply perm'_nil in H.
subst.
apply perm_nil.
- intros l' H.
assert (H' := H). (* cópia da hipótese H no contexto *)
apply perm'_exists in H'.
destruct H' as [l1 [l2 H']].
subst.
Admitted.
Theorem Permutation_equiv_perm': forall l l', Permutation l l' <-> perm' l l'.
Proof.
intros l l'.
split.
- apply Permutation_implica_perm'.
- apply perm'_implica_Permutation.
Qed.
(** * Análise da complexidade do algoritmo de ordenação por inserção *)
Fixpoint T_insere (x: nat) (l: list nat) : nat :=
match l with
| nil => 0
| h :: tl => if (x <=? h) then 1 else S (T_insere x tl)
end.
Require Import Lia.
Lemma T_insere_linear: forall l x, T_insere x l <= length l.
Proof.
induction l.
- intros x.
simpl.
auto.
- intros x.
simpl.
destruct (x <=? a).
+ apply le_n_S.
lia.
+ apply le_n_S.
apply IHl.
Qed.
Fixpoint T_is (l: list nat) : nat :=
match l with
| nil => 0
| h::tl => (T_is tl) + (T_insere h (ord_insercao tl))
end.
Lemma ord_insercao_length: forall l, length (ord_insercao l) = length l.
Proof.
Admitted.
Lemma T_is_quad: forall l, T_is l <= (length l)*(length l).
Proof.
induction l.
- simpl.
auto.
- simpl.
apply le_trans with ((length l)*(length l) + length (ord_insercao l)).
+ apply Nat.add_le_mono.
* apply IHl.
* apply T_insere_linear.
+ rewrite ord_insercao_length.
lia.
Qed.
(** ** Análise do pior caso *)
Fixpoint Tw_insere (n:nat) :=
match n with
| 0 => 0
| S k => S (Tw_insere k)
end.
Lemma Tw_insere_linear: forall n, Tw_insere n = n.
Proof.
induction n.
- simpl.
reflexivity.
- simpl.
rewrite IHn.
reflexivity.
Qed.
Fixpoint Tw_is (n: nat) :=
match n with
| 0 => 0
| S k => k + Tw_is k
end.
(* Exercício (2 pontos) - prazo: 23h59 de 21/02 *)
Theorem Tw_is_quad: forall n, 2 * Tw_is (S n) = n * (S n).
Proof.
Admitted.