-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathweek-03_exercises.v
448 lines (399 loc) · 11.7 KB
/
week-03_exercises.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
(* week-03_exercises.v *)
(* FPP 2020 - YSC3236 2020-2011, Sem1 *)
(* ********** *)
(*
Your name:
Bernard Boey
Tristan Koh
Your e-mail address:
Your student number:
A0191234L
A0191222R
*)
(* ********** *)
(* Exercise 1 *)
Definition tail_recursive_specification_of_addition (add : nat -> nat -> nat) :=
(forall y : nat,
add O y = y)
/\
(forall x' y : nat,
add (S x') y = add x' (S y)).
Proposition there_is_at_most_one_tail_recursive_addition :
forall add1 add2 : nat -> nat -> nat,
tail_recursive_specification_of_addition add1 ->
tail_recursive_specification_of_addition add2 ->
forall x y : nat,
add1 x y = add2 x y.
Proof.
intros add1 add2.
unfold tail_recursive_specification_of_addition.
intros [H_add1_O H_add1_S] [H_add2_O H_add2_S].
intro x.
induction x as [ | x' IHx'].
- intro y.
Check (H_add2_O y).
rewrite -> (H_add2_O y).
Check (H_add1_O y).
exact (H_add1_O y).
- intro y.
Check (H_add2_S x' y).
rewrite -> (H_add2_S x' y).
Check (H_add1_S x' y).
rewrite -> (H_add1_S x' y).
Check (IHx' (S y)).
rewrite -> (IHx'(S y)).
reflexivity.
Qed.
(* Exercise 4 *)
Theorem the_resident_addition_function_satisfies_the_tail_recursive_specification_of_addition :
tail_recursive_specification_of_addition Nat.add.
Proof.
unfold tail_recursive_specification_of_addition.
split.
- intro y.
Search (0 + _ = _).
exact (plus_O_n y).
- intros x' y.
Search (S _ + _ = S (_ + _)).
Check (plus_Sn_m x' y).
rewrite -> (plus_Sn_m x' y).
Search (S (_ + _) = _ + S _).
Check (plus_n_Sm x' y).
rewrite <- (plus_n_Sm x' y).
reflexivity.
Qed.
(* Exercise 9 *)
Inductive binary_tree (V : Type) : Type :=
| Leaf : V -> binary_tree V
| Node : binary_tree V -> binary_tree V -> binary_tree V.
Definition specification_of_mirror (mirror : forall V : Type, binary_tree V -> binary_tree V) : Prop :=
(forall (V : Type)
(v : V),
mirror V (Leaf V v) =
Leaf V v)
/\
(forall (V : Type)
(t1 t2 : binary_tree V),
mirror V (Node V t1 t2) =
Node V (mirror V t2) (mirror V t1)).
Proposition there_is_at_most_one_mirror :
forall mirror1 mirror2 : forall V : Type, binary_tree V -> binary_tree V,
specification_of_mirror mirror1 ->
specification_of_mirror mirror2 ->
forall (V : Type)
(t : binary_tree V),
mirror1 V t = mirror2 V t.
Proof.
intros mirror1 mirror2.
unfold specification_of_mirror.
intros [H_mirror1_Leaf H_mirror1_Node] [H_mirror2_Leaf H_mirror2_Node].
intros V t.
induction t as [v | t1 IHt1 t2 IHt2].
- Check (H_mirror2_Leaf V).
rewrite -> (H_mirror2_Leaf V).
Check (H_mirror1_Leaf V).
rewrite -> (H_mirror1_Leaf V).
reflexivity.
- Check (H_mirror2_Node V t1 t2).
rewrite -> (H_mirror2_Node V t1 t2).
Check (H_mirror1_Node V t1 t2).
rewrite -> (H_mirror1_Node V t1 t2).
Check IHt1.
rewrite -> IHt1.
Check IHt2.
rewrite -> IHt2.
reflexivity.
Qed.
Definition specification_of_number_of_leaves (number_of_leaves : forall V : Type, binary_tree V -> nat) : Prop :=
(forall (V : Type)
(v : V),
number_of_leaves V (Leaf V v) =
1)
/\
(forall (V : Type)
(t1 t2 : binary_tree V),
number_of_leaves V (Node V t1 t2) =
number_of_leaves V t1 + number_of_leaves V t2).
Proposition there_is_at_most_one_number_of_leaves :
forall leaves1 leaves2 : forall V : Type, binary_tree V -> nat,
specification_of_number_of_leaves leaves1 ->
specification_of_number_of_leaves leaves2 ->
forall (V: Type)
(t : binary_tree V),
leaves1 V t = leaves2 V t.
Proof.
intros leaves1 leaves2.
unfold specification_of_number_of_leaves.
intros [H_leaves1_Leaf H_leaves1_Node] [H_leaves2_Leaf H_leaves2_Node].
intros V t.
induction t as [v | t1 IHt1 t2 IHt2].
- Check (H_leaves2_Leaf V).
rewrite -> (H_leaves2_Leaf V).
rewrite -> (H_leaves1_Leaf V).
Check (H_leaves1_Leaf V).
reflexivity.
- Check (H_leaves2_Node V t1 t2).
rewrite -> (H_leaves2_Node V t1 t2).
Check (H_leaves1_Node V t1 t2).
rewrite -> (H_leaves1_Node V t1 t2).
Check IHt1.
rewrite -> IHt1.
Check IHt2.
rewrite -> IHt2.
reflexivity.
Qed.
Definition specification_of_number_of_nodes (number_of_nodes : forall V : Type, binary_tree V -> nat) : Prop :=
(forall (V : Type)
(v : V),
number_of_nodes V (Leaf V v) =
0)
/\
(forall (V : Type)
(t1 t2 : binary_tree V),
number_of_nodes V (Node V t1 t2) =
S (number_of_nodes V t1 + number_of_nodes V t2)).
Proposition there_is_at_most_one_number_of_nodes :
forall nodes1 nodes2 : forall V : Type, binary_tree V -> nat,
specification_of_number_of_nodes nodes1 ->
specification_of_number_of_nodes nodes2 ->
forall (V : Type)
(t : binary_tree V),
nodes1 V t = nodes2 V t.
Proof.
intros nodes1 nodes2.
unfold specification_of_number_of_nodes.
intros [H_nodes1_Leaf H_nodes1_Node] [H_nodes2_Leaf H_nodes2_Node].
intros V t.
induction t as [v | t1 IHt1 t2 IHt2].
- Check (H_nodes2_Leaf V).
rewrite -> (H_nodes2_Leaf V).
Check (H_nodes1_Leaf V).
rewrite -> (H_nodes1_Leaf V).
reflexivity.
- Check (H_nodes2_Node V t1 t2).
rewrite -> (H_nodes2_Node V t1 t2).
Check (H_nodes1_Node V t1 t2).
rewrite -> (H_nodes1_Node V t1 t2).
Check IHt1.
rewrite -> IHt1.
Check IHt2.
rewrite -> IHt2.
reflexivity.
Qed.
(* Exercise 5 *)
Definition recursive_specification_of_addition (add : nat -> nat -> nat) :=
(forall y : nat,
add O y = y)
/\
(forall x' y : nat,
add (S x') y = S (add x' y)).
Proposition recursive_and_tail_recursive_specifications_of_addition_are_equivalent :
forall add : nat -> nat -> nat,
tail_recursive_specification_of_addition add <->
recursive_specification_of_addition add.
Proof.
intro add.
unfold tail_recursive_specification_of_addition.
unfold recursive_specification_of_addition.
split.
- intros [H_add1_O H_add1_S].
split.
* exact H_add1_O.
* intros x' y.
rewrite -> (H_add1_S x' y).
revert y.
induction x' as [ | x'' IHx''].
-- intro y.
Check (H_add1_O (S y)).
rewrite -> (H_add1_O (S y)).
rewrite -> (H_add1_O y).
reflexivity.
-- intro y.
Check (H_add1_S x'' (S y)).
rewrite -> (H_add1_S x'' (S y)).
rewrite -> (H_add1_S x'' y).
rewrite -> (IHx'' (S y)).
reflexivity.
- intros [H_add2_O H_add2_S].
split.
* exact H_add2_O.
* intros x' y.
rewrite -> (H_add2_S x' y).
revert y.
induction x' as [ | x'' IHx''].
-- intro y.
Check (H_add2_O (S y)).
rewrite -> (H_add2_O (S y)).
rewrite -> (H_add2_O y).
reflexivity.
-- intro y.
Check (H_add2_S x'' y).
rewrite -> (H_add2_S x'' (S y)).
rewrite -> (H_add2_S x'' y).
rewrite -> (IHx'' y).
reflexivity.
Qed.
(* Exercise 8 *)
Proposition O_is_left_neutral_for_recursive_addition :
forall add : nat -> nat -> nat,
recursive_specification_of_addition add ->
forall x : nat,
add 0 x = x.
Proof.
intro add.
unfold recursive_specification_of_addition.
intros [H_add_O H_add_S].
intro x.
induction x as [ | x' IHx'].
- Check (H_add_O 0).
rewrite -> (H_add_O 0).
reflexivity.
- rewrite -> (H_add_O (S x')).
reflexivity.
Qed.
Proposition O_is_right_neutral_for_recursive_addition :
forall add : nat -> nat -> nat,
recursive_specification_of_addition add ->
forall x : nat,
add x 0 = x.
Proof.
intro add.
unfold recursive_specification_of_addition.
intros [H_add_O H_add_S].
intro x.
induction x as [ | x' IHx'].
- rewrite -> (H_add_O 0).
reflexivity.
- rewrite -> (H_add_S x' 0).
rewrite -> IHx'.
reflexivity.
Qed.
Require Import Setoid.
Proposition O_is_left_neutral_for_tail_recursive_addition :
forall add : nat -> nat -> nat,
tail_recursive_specification_of_addition add ->
forall x : nat,
add 0 x = x.
Proof.
intro add.
unfold tail_recursive_specification_of_addition.
intros [H_add_O H_add_S].
intro x.
induction x as [ | x' IHx'].
- exact (H_add_O 0).
- exact (H_add_O (S x')).
Restart.
intro add.
rewrite -> (recursive_and_tail_recursive_specifications_of_addition_are_equivalent add).
exact (O_is_left_neutral_for_recursive_addition add).
Qed.
Lemma tail_rec_add_x_Sy :
forall add : nat -> nat -> nat,
tail_recursive_specification_of_addition add ->
forall x y : nat,
add x (S y) = S (add x y).
Proof.
intro add.
unfold tail_recursive_specification_of_addition.
intros [H_add_O H_add_S].
intro x.
induction x as [ | x' IHx'].
- intro y.
rewrite -> (H_add_O (S y)).
rewrite -> (H_add_O y).
reflexivity.
- intro y.
rewrite -> (H_add_S x' (S y)).
rewrite -> (H_add_S x' y).
rewrite -> (IHx' (S y)).
reflexivity.
Qed.
Proposition O_is_right_neutral_for_tail_recursive_addition :
forall add : nat -> nat -> nat,
tail_recursive_specification_of_addition add ->
forall x : nat,
add x 0 = x.
Proof.
intro add.
intro H_tail_rec_spec_add.
assert (_H_tail_rec_spec_add := H_tail_rec_spec_add).
unfold tail_recursive_specification_of_addition in H_tail_rec_spec_add.
destruct H_tail_rec_spec_add as [H_add_O H_add_S].
intro x.
induction x as [ | x' IHx'].
- exact (H_add_O 0).
- rewrite -> (H_add_S x' 0).
Check (tail_rec_add_x_Sy add _H_tail_rec_spec_add x' 0).
rewrite -> (tail_rec_add_x_Sy add _H_tail_rec_spec_add x' 0).
rewrite -> IHx'.
reflexivity.
Restart.
intro add.
rewrite -> (recursive_and_tail_recursive_specifications_of_addition_are_equivalent add).
exact (O_is_right_neutral_for_recursive_addition add).
Qed.
(* Exercise 6 *)
Proposition recursive_addition_is_associative :
forall add : nat -> nat -> nat,
recursive_specification_of_addition add ->
forall x y z : nat,
add (add x y) z = add x (add y z).
Proof.
intro add.
unfold recursive_specification_of_addition.
intros [H_add_O H_add_S].
intros x y z.
induction x as [ | x' IHx'].
- Check (H_add_O y).
rewrite -> (H_add_O y).
Check (H_add_O (add y z)).
rewrite -> (H_add_O (add y z)).
reflexivity.
- Check (H_add_S x' y).
rewrite -> (H_add_S x' y).
Check (H_add_S (add x' y) z).
rewrite -> (H_add_S (add x' y) z).
Check (H_add_S x' (add y z)).
rewrite -> (H_add_S x' (add y z)).
rewrite -> IHx'.
reflexivity.
Qed.
Proposition tail_recursive_addition_is_associative :
forall add : nat -> nat -> nat,
tail_recursive_specification_of_addition add ->
forall x y z : nat,
add (add x y) z = add x (add y z).
Proof.
intro add.
intro H_tail_rec_spec_add.
assert (_H_tail_rec_spec_add := H_tail_rec_spec_add).
unfold tail_recursive_specification_of_addition in H_tail_rec_spec_add.
destruct H_tail_rec_spec_add as [H_add_O H_add_S].
intros x.
induction x as [ | x' IHx'].
- intros y z.
Check (H_add_O y).
rewrite -> (H_add_O y).
Check (H_add_O (add y z)).
rewrite -> (H_add_O (add y z)).
reflexivity.
- intros y z.
Check (H_add_S x' y).
rewrite -> (H_add_S x' y).
Check (H_add_S x' (add y z)).
rewrite -> (H_add_S x' (add y z)).
Check (IHx' (S y) z).
rewrite -> (IHx' (S y) z).
Check (H_add_S y z).
rewrite -> (H_add_S y z).
Check (tail_rec_add_x_Sy add _H_tail_rec_spec_add y z).
rewrite -> (tail_rec_add_x_Sy add _H_tail_rec_spec_add y z).
reflexivity.
Restart.
intro add.
rewrite -> (recursive_and_tail_recursive_specifications_of_addition_are_equivalent add).
exact (recursive_addition_is_associative add).
Qed.
(* end of week-03_exercises.v *)