-
Notifications
You must be signed in to change notification settings - Fork 10
/
mid_sol.v
578 lines (451 loc) · 13.2 KB
/
mid_sol.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
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
(** **** SNU 4190.310, 2016 Spring *)
(** Mid Exam *)
(** 2016/04/16 18:00 *)
(** Important:
- Just leave [exact FILL_IN_HERE] for those problems that you fail to prove.
- You are NOT allowed to use the following tactics.
[tauto], [intuition], [firstorder], [omega].
- Here is the list of tactics and tacticals you have learned.
[intros]
[revert]
[reflexivity]
[simpl]
[rewrite]
[induction]
[assert]
[unfold]
[apply] ... [with] ... [in] ...
[destruct] ... [as] ... [eqn:] ...
[inversion]
[symmetry]
[generalize dependent]
[split]
[exists]
[clear]
[subst]
[rename] ... [into] ...
[contradiction]
[constructor]
[auto]
[repeat]
[try]
[;]
**)
Require Import Arith NPeano.
Definition FILL_IN_HERE {T: Type} : T. Admitted.
Fixpoint double (n:nat) :=
match n with
| O => O
| S n' => S (S (double n'))
end.
Fixpoint repeat_ntimes (n: nat) {X:Type} (f:X->X) (x:X) : X :=
match n with
| 0 => x
| S n' => f (repeat_ntimes n' f x)
end.
Definition c_nat := forall X : Type, (X -> X) -> X -> X.
(** Defining [zero] is somewhat trickier: how can we "apply a function
zero times"? The answer is actually simple: just return the
argument untouched. *)
Definition c_zero : c_nat :=
fun (X : Type) (f : X -> X) (x : X) => x.
(** Let's see how to write some numbers with this notation. Iterating
a function once should be the same as just applying it. Thus: *)
Definition c_one : c_nat :=
fun (X : Type) (f : X -> X) (x : X) => f x.
(** Similarly, [two] should apply [f] twice to its argument: *)
Definition c_two : c_nat :=
fun (X : Type) (f : X -> X) (x : X) => f (f x).
(** More generally, a number [n] can be written as [fun X f x => f (f
... (f x) ...)], with [n] occurrences of [f]. Notice in
particular how the [doit3times] function we've defined previously
is actually just the Church representation of [3]. *)
Definition c_three : c_nat := @repeat_ntimes 3.
Definition c_n (n: nat) : c_nat := @repeat_ntimes n.
(** Here is a more useful higher-order function, taking a list
of [X]s and a _predicate_ on [X] (a function from [X] to [bool])
and "filtering" the list, returning a new list containing just
those elements for which the predicate returns [true]. *)
(**
Definition of [list]
**)
Inductive list (X:Type) : Type :=
| nil : list X
| cons : X -> list X -> list X.
Arguments nil {X}.
Arguments cons {X} _ _.
Fixpoint app (X : Type) (l1 l2 : list X)
: (list X) :=
match l1 with
| nil => l2
| cons h t => cons h (app X t l2)
end.
Arguments app {X} l1 l2.
Notation "x :: y" := (cons x y)
(at level 60, right associativity).
Notation "[ ]" := nil.
Notation "[ x ; .. ; y ]" := (cons x .. (cons y []) ..).
Notation "x ++ y" := (app x y)
(at level 60, right associativity).
Check (3 :: ([0; 1] ++ [])).
Fixpoint nth_error {X : Type} (l : list X) (n : nat)
: option X :=
match l with
| [] => None
| a :: l' => if beq_nat n O then Some a else nth_error l' (pred n)
end.
Fixpoint length {X : Type} (l : list X) : nat :=
match l with
| nil => 0
| cons _ l' => S (length l')
end.
Fixpoint insert (n: nat) l :=
match l with
| nil => [n]
| h::t => if leb n h then n::l else h::insert n t
end.
Fixpoint sort l :=
match l with
| nil => nil
| h::t => insert h (sort t)
end.
Fixpoint choose n k :=
match n with
| 0 => 1
| S n' =>
match k with
| 0 => 1
| S k' => if ltb k' n' then choose n' k + choose n' k' else 1
end
end.
Fixpoint fact n :=
match n with
| 0 => 1
| S n' => n * fact n'
end.
(**
You may need the following lemmas.
**)
Check Nat.add_comm.
Check Nat.add_assoc.
Check Nat.add_sub.
Check Nat.add_0_l.
Check Nat.add_0_r.
Check Nat.add_succ_l.
Check Nat.add_succ_r.
Check Nat.sub_0_l.
Check Nat.sub_0_r.
Check Nat.sub_diag.
Check Nat.sub_succ_l.
Check Nat.sub_succ_r.
Check Nat.mul_comm.
Check Nat.mul_assoc.
Check Nat.mul_1_l.
Check Nat.mul_1_r.
Check Nat.mul_add_distr_l.
Check Nat.mul_add_distr_r.
Check Nat.mul_sub_distr_l.
Check Nat.mul_sub_distr_r.
Check Nat.mul_cancel_l.
Check Nat.succ_le_mono.
Check Nat.lt_0_succ.
Check Nat.lt_le_trans.
Check Nat.lt_irrefl.
Check Nat.le_trans.
Check Nat.le_max_l.
Check Nat.le_max_r.
Check Nat.max_spec.
(*=========== 3141592 ===========*)
(** Easy: [Homework Assignment] *)
(** Use the tactics you have learned so far to prove the following
theorem about boolean functions. *)
Theorem negation_fn_applied_twice :
forall (f : bool -> bool),
(forall (x : bool), f x = negb x) ->
forall (b : bool), f (f b) = b.
Proof.
intros. destruct b.
- rewrite H. rewrite H. reflexivity.
- rewrite H. rewrite H. reflexivity.
Qed.
(*-- Check --*)
Check negation_fn_applied_twice :
forall (f : bool -> bool),
(forall (x : bool), f x = negb x) ->
forall (b : bool), f (f b) = b.
(*=========== 3141592 ===========*)
(** Easy: [Homework Assignment]
(* See the definition of [double] *)
(** Use induction to prove this simple fact about [double]: *)
**)
Lemma double_plus : forall n, double n = n + n .
Proof.
induction n.
- reflexivity.
- simpl. rewrite IHn. rewrite Nat.add_succ_r. reflexivity.
Qed.
(*-- Check --*)
Check double_plus : forall n, double n = n + n .
(*=========== 3141592 ===========*)
(** Medium: [Homework Assignment]
*)
Theorem nth_error_after_last: forall (n : nat) (X : Type) (l : list X),
length l = n ->
nth_error l n = None.
Proof.
intros. revert n H. induction l.
- reflexivity.
- simpl. intros. destruct n.
+ inversion H.
+ simpl. apply IHl. inversion H. reflexivity.
Qed.
(*-- Check --*)
Check nth_error_after_last: forall (n : nat) (X : Type) (l : list X),
length l = n ->
nth_error l n = None.
(*=========== 3141592 ===========*)
(** Medium: [Homework Assignment]
Complete the definitions of the following functions. Make sure
that the corresponding unit tests pass by proving them with
[reflexivity].
**)
(** Successor of a natural number: *)
Definition c_succ (n : c_nat) : c_nat :=
fun _ f x => f (n _ f x).
(** Addition of two natural numbers: *)
Definition c_plus (n m : c_nat) : c_nat :=
fun _ f x => n _ f (m _ f x).
Example c_succ_1 : c_succ c_zero = c_one.
Proof. reflexivity. Qed.
Example c_succ_2 : c_succ c_one = c_two.
Proof. reflexivity. Qed.
Example c_succ_3 : c_succ c_two = c_three.
Proof. reflexivity. Qed.
Example c_plus_1 : c_plus c_zero c_one = c_one.
Proof. reflexivity. Qed.
Example c_plus_2 : c_plus c_two c_three = c_plus c_three c_two.
Proof. reflexivity. Qed.
Example c_plus_3 :
c_plus (c_plus c_two c_two) c_three = c_plus c_one (c_plus c_three c_three).
Proof. reflexivity. Qed.
(*-- Check --*)
Check c_succ_1 : c_succ c_zero = c_one.
Check c_succ_2 : c_succ c_one = c_two.
Check c_succ_3 : c_succ c_two = c_three.
Check c_plus_1 : c_plus c_zero c_one = c_one.
Check c_plus_2 : c_plus c_two c_three = c_plus c_three c_two.
Check c_plus_3 :
c_plus (c_plus c_two c_two) c_three = c_plus c_one (c_plus c_three c_three).
(*=========== 3141592 ===========*)
(** Hard: [Homework Assignment]
Prove the following theorem.
**)
Theorem plus_n_n_injective : forall n m,
n + n = m + m ->
n = m.
Proof.
intros n. induction n as [| n'].
- intros. destruct m.
+ reflexivity.
+ inversion H.
- simpl. intros. destruct m.
+ inversion H.
+ inversion H. rewrite Nat.add_succ_r in H1. rewrite Nat.add_succ_r in H1.
inversion H1. apply IHn' in H2. subst. reflexivity.
Qed.
(*-- Check --*)
Check plus_n_n_injective : forall n m,
n + n = m + m ->
n = m.
(*=========== 3141592 ===========*)
(** Easy:
Define a function [square_sum] satisfying:
square_sum n = 1^2 + 2^2 + ... +n^2
**)
Fixpoint square_sum (n: nat) : nat :=
match n with
| 0 => 0
| S n' => n*n + square_sum n'
end.
Example square_sum_example1: square_sum 5 = 55.
Proof. reflexivity. Qed.
Example square_sum_example2: square_sum 10 = 385.
Proof. reflexivity. Qed.
(*-- Check --*)
Check square_sum_example1: square_sum 5 = 55.
Check square_sum_example2: square_sum 10 = 385.
(*=========== 3141592 ===========*)
(** Medium:
Prove the following theorem.
**)
Lemma app_tail_cancel: forall X (l1 l2: list X) a
(EQ: l1 ++ [a] = l2 ++ [a]),
l1 = l2.
Proof.
induction l1.
- simpl. intros. destruct l2.
+ reflexivity.
+ inversion EQ. subst.
destruct l2.
* inversion H1.
* inversion H1.
- intros. destruct l2.
+ destruct l1.
* inversion EQ.
* inversion EQ.
+ inversion EQ. subst.
rewrite (IHl1 _ _ H1). reflexivity.
Qed.
(*-- Check --*)
Check app_tail_cancel: forall X (l1 l2: list X) a
(EQ: l1 ++ [a] = l2 ++ [a]),
l1 = l2.
(*=========== 3141592 ===========*)
Inductive sorted: list nat -> Prop :=
| sorted_nil: sorted nil
| sorted_cons:
forall h t
(LE: match t with nil => True | h' :: _ => h <= h' end)
(SORTED: sorted t),
sorted (h::t)
.
Example sorted_example1: sorted [1; 3; 4; 4; 5].
Proof. repeat (constructor; auto). Qed.
Example sorted_example2: sorted [2; 2; 3; 6].
Proof. repeat (constructor; auto). Qed.
Example sorted_non_example1: sorted [1; 3; 2] -> False.
Proof.
intros.
repeat match goal with
| [H: sorted _ |- _] => inversion_clear H; subst
| [H: _ <= _ |- _] => inversion_clear H; subst
end.
Qed.
(*
Fixpoint insert (n: nat) l :=
match l with
| nil => [n]
| h::t => if leb n h then n::l else h::insert n t
end.
Fixpoint sort l :=
match l with
| nil => nil
| h::t => insert h (sort t)
end.
*)
(* Hint:
Fixpoint leb (n m : nat) {struct n} : bool :=
match n with
| 0 => true
| S n' => match m with
| 0 => false
| S m' => leb n' m'
end
end
Use [Search About leb]
*)
Lemma insert_sorted: forall x l
(SORTED: sorted l),
sorted (insert x l).
Proof.
intros. revert x SORTED. induction l.
- intros. simpl. constructor; auto.
- intros. simpl. destruct (x0 <=? x) eqn: EQ.
+ constructor.
* apply leb_complete. auto.
* auto.
+ inversion SORTED. constructor.
* subst. apply Nat.leb_gt in EQ. apply Nat.lt_le_incl in EQ.
destruct l.
{ simpl. auto. }
{ simpl. destruct (x0 <=? n) eqn: EQn.
- auto.
- auto. }
* subst. auto.
Qed.
Lemma sort_sorted: forall l, sorted (sort l).
Proof.
induction l.
- constructor.
- simpl. apply insert_sorted. auto.
Qed.
(*-- Check --*)
Check sorted_example1: sorted [1; 3; 4; 4; 5].
Check sorted_example2: sorted [2; 2; 3; 6].
Check sorted_non_example1: sorted [1; 3; 2] -> False.
(*-- Check --*)
Check sort_sorted: forall l, sorted (sort l).
(*=========== 3141592 ===========*)
(** Very hard:
Prove the following theorem.
**)
(*
Fixpoint fact n :=
match n with
| 0 => 1
| S n' => n * fact n'
end.
*)
(* Hint:
Using the function [choose] will be very useful.
Fixpoint choose n k :=
match n with
| 0 => 1
| S n' =>
match k with
| 0 => 1
| S k' => if ltb k' n' then choose n' k + choose n' k' else 1
end
end.
Definition ltb n m := leb (S n) m.
Use [Search About leb] and [SearchAbout ltb]
*)
Lemma fact_unfold: forall n, fact (S n) = (S n) * fact n.
Proof. auto. Qed.
Lemma fact_decompose : forall n k
(LE: k <= n),
exists m, fact n = m * fact k * fact (n-k).
Proof.
intros. exists (choose n k).
revert k LE. induction n.
- intros. inversion LE. subst. auto.
- intros. destruct k.
{ simpl. rewrite Nat.add_0_r. reflexivity. }
apply le_S_n in LE. simpl choose. simpl (_ - _).
destruct (k <? n) eqn: LT.
{ apply Nat.ltb_lt in LT.
assert (EQ: n-k = S(n-S k)).
{ destruct n.
- inversion LT.
- rewrite Nat.sub_succ_l. auto.
apply le_S_n. auto.
}
repeat rewrite Nat.mul_add_distr_r.
rewrite (fact_unfold k) at 2.
rewrite EQ at 1.
rewrite (fact_unfold (n-S k)).
repeat rewrite <-Nat.mul_assoc.
repeat rewrite (Nat.mul_comm (S _) _).
repeat rewrite Nat.mul_assoc.
rewrite <-(IHn _ LE).
rewrite <-(IHn _ LT).
rewrite <-Nat.mul_add_distr_l.
rewrite Nat.mul_comm.
simpl.
rewrite (Nat.sub_add _ _ LT).
reflexivity.
}
{ inversion LE.
- subst.
rewrite Nat.sub_diag.
rewrite Nat.mul_1_l.
rewrite Nat.mul_1_r.
reflexivity.
- subst. apply leb_correct in H.
unfold ltb in LT. simpl in LT. rewrite H in LT. inversion LT.
}
Qed.
(*-- Check --*)
Check fact_decompose : forall n k
(LE: k <= n),
exists m, fact n = m * fact k * fact (n-k).