-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathInduction.v
633 lines (547 loc) · 16.9 KB
/
Induction.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
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
(* Case study for porting a library from one Coq definition to another *)
(* Original problems are from Software Foundations (Inductive.v), solutions are from students *)
(* Cleaned up for easy write-up, got rid of extraneous things, but mostly the original proofs *)
Require Import Patcher.Patch.
Notation "x + y" := (plus x y)
(at level 50, left associativity)
: nat_scope.
Notation "x - y" := (minus x y)
(at level 50, left associativity)
: nat_scope.
Notation "x * y" := (mult x y)
(at level 40, left associativity)
: nat_scope.
(** **** Exercise: 3 stars (binary) *)
(** Consider a different, more efficient representation of natural
numbers using a binary rather than unary system. That is, instead
of saying that each natural number is either zero or the successor
of a natural number, we can say that each binary number is either
- zero,
- twice a binary number, or
- one more than twice a binary number.
(a) First, write an inductive definition of the type [bin]
corresponding to this description of binary numbers.
(Hint: Recall that the definition of [nat] above,
Inductive nat : Type := | O : nat | S : nat -> nat.
says nothing about what [O] and [S] "mean." It just says "[O] is
in the set called [nat], and if [n] is in the set then so is [S
n]." The interpretation of [O] as zero and [S] as successor/plus
one comes from the way that we _use_ [nat] values, by writing
functions to do things with them, proving things about them, and
so on. Your definition of [bin] should be correspondingly simple;
it is the functions you will write next that will give it
mathematical meaning.)
(b) Next, write an increment function [incr] for binary numbers,
and a function [bin_to_nat] to convert binary numbers to unary
numbers.
(c) Write five unit tests [test_bin_incr1], [test_bin_incr2], etc.
for your increment and binary-to-unary functions. (A "unit
test" in Coq is a specific [Example] that can be proved with
just [reflexivity], as we've done for several of our
definitions.) Notice that incrementing a binary number and
then converting it to unary should yield the same result as
first converting it to unary and then incrementing. *)
(* Talia: From ghostfs, but common auxiliary lemmas *)
Theorem plus_0_r : forall n:nat, n + 0 = n.
Proof.
intros n. induction n as [| n'].
- reflexivity.
- simpl. rewrite -> IHn'. reflexivity.
Qed.
Theorem plus_comm : forall n m : nat,
n + m = m + n.
Proof.
intros n m.
induction m as [| m'].
- simpl. rewrite -> plus_0_r. reflexivity.
- simpl. rewrite <- IHm'. rewrite -> plus_n_Sm. reflexivity.
Qed.
(* Talia: From marshall's, but really they are the same so just define a common one *)
Inductive bin : Type :=
| B0 : bin
| B2 : bin -> bin
| B21 : bin -> bin.
Fixpoint incr (n : bin) : bin :=
match n with
| B0 => B21 B0
| B2 n' => B21 n'
| B21 n' => B2 (incr n')
end.
Fixpoint nat_to_bin (n : nat) : bin :=
match n with
| 0 => B0
| S n' => incr (nat_to_bin n')
end.
Fixpoint normalize (b:bin) : bin :=
match b with
| B0 => B0
| B2 b' => match normalize b' with
| B0 => B0
| b'' => B2 b''
end
| B21 b' => B21 (normalize b')
end.
(* Talia: https://github.com/marshall-lee/software_foundations/commit/4c4c4962ad1c56ab7f97b9aedf708f4ac97e7c5a *)
Module marshall.
Fixpoint bin_to_nat (n : bin) : nat :=
match n with
| B0 => O
| B2 n' => (bin_to_nat n') + (bin_to_nat n')
| B21 n' => S ((bin_to_nat n') + (bin_to_nat n'))
end.
End marshall.
(* https://github.com/blindFS/Software-Foundations-Solutions/blob/master/Basics.v *)
Module blindFS.
Fixpoint bin_to_nat (b: bin) : nat :=
match b with
| B0 => O
| B2 b' => 2 * (bin_to_nat b')
| B21 b' => 1 + 2 * (bin_to_nat b')
end.
End blindFS.
Module marshall_induction.
Definition bin_to_nat := marshall.bin_to_nat.
Lemma S_S_plus : forall a b : nat,
S a + S b = S (S (a + b)).
Proof.
intros a b.
induction a.
reflexivity.
simpl. rewrite <- IHa. reflexivity. Qed.
(* Talia: Original is below *)
Theorem bin_to_nat_pres_incr_original : forall b : bin,
bin_to_nat (incr b) = S (bin_to_nat b).
Proof.
intros b.
induction b as [|b'|b'].
- reflexivity.
- reflexivity.
- simpl.
rewrite -> IHb'.
rewrite -> S_S_plus.
reflexivity.
Qed.
(* Talia: But for now, we only support one format as a proof of concept: *)
Theorem bin_to_nat_pres_incr : forall b : bin,
bin_to_nat (incr b) = S (bin_to_nat b).
Proof.
intros b.
induction b as [|b'|b'].
- reflexivity.
- reflexivity.
- simpl.
rewrite -> IHb'.
assert (H : forall a b : nat, S a + S b = S (S (a + b))).
+ intros a b. apply S_S_plus.
+ rewrite -> H.
reflexivity.
Qed.
(** [] *)
Theorem bin_to_nat_nat_to_bin : forall n : nat,
bin_to_nat (nat_to_bin n) = n.
Proof.
induction n as [|n'].
- reflexivity.
- simpl.
rewrite -> bin_to_nat_pres_incr.
rewrite -> IHn'.
reflexivity.
Qed.
End marshall_induction.
Module blindfs_induction.
Definition bin_to_nat := blindFS.bin_to_nat.
(* Talia: Original, but with indentation. *)
Theorem bin_to_nat_pres_incr : forall n : bin,
bin_to_nat (incr n) = 1 + bin_to_nat n.
Proof.
intros n.
induction n.
- reflexivity.
- reflexivity.
- simpl.
rewrite -> IHn.
simpl.
assert (H : forall a :nat, S (a + S (a + 0)) = S (S (a + (a + 0)))).
+ intros a.
rewrite <- plus_n_O.
rewrite -> plus_comm.
induction a.
* reflexivity.
* simpl. reflexivity.
+ rewrite -> H.
reflexivity.
Qed.
Theorem double_convertion : forall n:nat,
bin_to_nat (nat_to_bin n) = n.
Proof.
intros n.
induction n.
reflexivity.
simpl.
rewrite -> bin_to_nat_pres_incr.
rewrite -> IHn.
reflexivity.
Qed.
Theorem normalize_again : forall b :bin, normalize (normalize b) = normalize b.
Proof.
intros b.
induction b as [| b' | b'].
reflexivity.
simpl.
remember (normalize b') as n_b'.
destruct n_b' as [| b'' | b''].
reflexivity.
replace (normalize (B2 (B2 b''))) with (match normalize (B2 b'') with | B0 => B0 | B2 c => B2 (B2 c) | B21 c => B2 (B21 c) end).
rewrite -> IHb'. reflexivity.
simpl. reflexivity.
simpl. rewrite <- IHb'. reflexivity.
simpl. rewrite -> IHb'. reflexivity.
Qed.
Theorem normalize_incr_comm : forall b : bin, normalize (incr b) = incr (normalize b).
Proof.
intros b. induction b.
reflexivity.
simpl.
remember (normalize b) as nb.
destruct nb.
reflexivity.
reflexivity.
reflexivity.
simpl.
rewrite -> IHb.
remember (normalize b) as nb.
destruct nb.
reflexivity.
reflexivity.
reflexivity.
Qed.
Theorem D_means_double : forall n:nat,
nat_to_bin(n + n) = normalize(B2 (nat_to_bin n)).
Proof.
intros n. induction n.
reflexivity.
replace (nat_to_bin (S n + S n)) with (incr (incr (nat_to_bin (n + n)))).
rewrite -> IHn. rewrite <- normalize_incr_comm.
rewrite <- normalize_incr_comm.
simpl. reflexivity.
simpl. rewrite <- plus_n_Sm.
reflexivity.
Qed.
Theorem normalize_correctness : forall b:bin,
nat_to_bin (bin_to_nat b) = normalize b.
Proof.
intros b.
induction b.
- reflexivity.
- simpl. rewrite -> plus_0_r.
rewrite -> D_means_double.
simpl. rewrite -> IHb. rewrite -> normalize_again.
reflexivity.
- simpl. rewrite -> plus_0_r.
rewrite -> D_means_double.
rewrite -> IHb.
simpl.
rewrite -> normalize_again.
remember (normalize b) as nb.
destruct nb.
+ reflexivity.
+ reflexivity.
+ reflexivity.
Qed.
End blindfs_induction.
(* Port blindFS to use Marshall's datatype *)
Module blindFS_induction_ported.
Definition bin_to_nat := marshall.bin_to_nat.
Theorem bin_to_nat_pres_incr : forall n : bin,
bin_to_nat (incr n) = 1 + bin_to_nat n.
Proof.
intros n.
induction n.
- reflexivity.
- reflexivity.
- simpl.
rewrite -> IHn.
simpl.
assert (H : forall a :nat, S (a + S a) = S (S (a + a))).
+ intros.
rewrite -> plus_comm.
induction a.
* reflexivity.
* simpl. reflexivity.
+ rewrite <- H. reflexivity.
Qed.
Definition cut :=
forall (a : nat),
S (a + S a) = S (S (a + a)) ->
S (a + S (a + 0)) = S (S (a + (a + 0))).
(* Patch *)
Patch Proof blindfs_induction.bin_to_nat_pres_incr bin_to_nat_pres_incr cut by (fun (H : cut) b0 => H (bin_to_nat b0)) as patch.
Print patch.
Print patch_inv.
(* patch
: forall (P : nat -> Prop) (b : bin),
P (bin_to_nat b) -> P (bin_to_nat b + 0) *)
(* Talia: Now we have a patch in one direction. Let's invert it to get the other part of the isomorphism. *)
(* patch_inv =
fun (P : nat -> Prop) (b : bin) (H : P (bin_to_nat b + 0)) =>
eq_ind (bin_to_nat b + 0) P H (bin_to_nat b)
(eq_sym (plus_n_O (bin_to_nat b)))
: forall (P : nat -> Prop) (b : bin),
P (bin_to_nat b + 0) -> P (bin_to_nat b) *)
(* Talia: Note that this is a common way to switch between types when one representation
is more convenient for something. You prove an isomorphism between the two. Here we're
just extracting the isomorphism from a change in an auxiliary lemma. *)
Theorem double_convertion : forall n:nat,
bin_to_nat (nat_to_bin n) = n.
Proof.
intros n.
induction n.
reflexivity.
simpl.
rewrite -> bin_to_nat_pres_incr.
rewrite -> IHn.
reflexivity.
Qed.
Theorem normalize_again : forall b :bin, normalize (normalize b) = normalize b.
Proof.
intros b.
induction b as [| b' | b'].
reflexivity.
simpl.
remember (normalize b') as n_b'.
destruct n_b' as [| b'' | b''].
reflexivity.
replace (normalize (B2 (B2 b''))) with (match normalize (B2 b'') with | B0 => B0 | B2 c => B2 (B2 c) | B21 c => B2 (B21 c) end).
rewrite -> IHb'. reflexivity.
simpl. reflexivity.
simpl. rewrite <- IHb'. reflexivity.
simpl. rewrite -> IHb'. reflexivity.
Qed.
Theorem normalize_incr_comm : forall b : bin, normalize (incr b) = incr (normalize b).
Proof.
intros b. induction b.
reflexivity.
simpl.
remember (normalize b) as nb.
destruct nb.
reflexivity.
reflexivity.
reflexivity.
simpl.
rewrite -> IHb.
remember (normalize b) as nb.
destruct nb.
reflexivity.
reflexivity.
reflexivity.
Qed.
Theorem D_means_double : forall n:nat,
nat_to_bin(n + n) = normalize(B2 (nat_to_bin n)).
Proof.
intros n. induction n.
reflexivity.
replace (nat_to_bin (S n + S n)) with (incr (incr (nat_to_bin (n + n)))).
rewrite -> IHn. rewrite <- normalize_incr_comm.
rewrite <- normalize_incr_comm.
simpl. reflexivity.
simpl. rewrite <- plus_n_Sm.
reflexivity.
Qed.
Theorem normalize_correctness : forall b:bin,
nat_to_bin (bin_to_nat b) = normalize b.
Proof.
intros b.
induction b.
- reflexivity.
- simpl. apply patch_inv. rewrite -> plus_0_r.
rewrite -> D_means_double.
simpl. rewrite -> IHb. rewrite -> normalize_again.
reflexivity.
- simpl. apply patch_inv. rewrite -> plus_0_r.
rewrite -> D_means_double.
rewrite -> IHb.
simpl.
rewrite -> normalize_again.
remember (normalize b) as nb.
destruct nb.
+ reflexivity.
+ reflexivity.
+ reflexivity.
Qed.
(* Talia: Note that we've applied the patch in two places. *)
(* Not yet done: Adding this to hints [can't add it as-is] *)
(* Talia: One thing that's notable however is that it is applied to its inverse.
The natural thing to do is to remove it altogether: *)
Theorem normalize_correctness_better : forall b:bin,
nat_to_bin (bin_to_nat b) = normalize b.
Proof.
intros b.
induction b.
- reflexivity.
- simpl.
rewrite -> D_means_double.
simpl. rewrite -> IHb. rewrite -> normalize_again.
reflexivity.
- simpl.
rewrite -> D_means_double.
rewrite -> IHb.
simpl.
rewrite -> normalize_again.
remember (normalize b) as nb.
destruct nb.
+ reflexivity.
+ reflexivity.
+ reflexivity.
Qed.
(* Talia: Since in fact we have switched to a definition that is easier to work with
than the one we originally had. This is a common way to change definitions. *)
(* Talia: Just looking at tactics doesn't help us here since rewrite -> plus_0_r is used here
instead of rewrite <- plus_n_0, even though the two produce equivalent terms (since they are inverses). *)
(* Talia: It would be good to do this automatically, but for now we can have the tool determine that patch_inv
is applied to its own inverse. Let's see how that works: *)
(* Talia: The thing to do here [not yet implemented] is to call the path finding algorithm.
Then, when the path is broken down, check for pairs that are each others inverses. Eliminate
these pairs, and then fold the result. *)
(* Talia: Let's break that into a tactic first and see if it finds the pair. (Not yet done).
Then we can make another tactic searches through the old term and finds applications of patch or
patch_inv in its path. If it finds them, it tells you that you can remove them. (Not yet done).
Alternatively, it can run on the new term, find the pair, and let you know that you can remove both
of those functions. *)
(* These don't work yet because the factoring component doesn't yet support
certain kinds of terms:
Factor normalize_correctness using prefix normalize.
Print normalize_0.
Print normalize_1. *)
End blindFS_induction_ported.
Module marshall_induction_ported.
Definition bin_to_nat := blindFS.bin_to_nat.
(* Talia: For now, our tactic only supports one format,
with an explicitly cut lemma as a proof of concept *)
Theorem bin_to_nat_pres_incr : forall b : bin,
bin_to_nat (incr b) = S (bin_to_nat b).
Proof.
intros b.
induction b as [|b'|b'].
- reflexivity.
- reflexivity.
- simpl.
rewrite -> IHb'.
assert (forall a b : nat, S a + (S b + 0) = S (S (a + (b + 0)))).
+ intros.
repeat rewrite <- plus_n_O.
apply marshall_induction.S_S_plus.
+ rewrite -> H.
reflexivity.
Qed.
(* Eventually, want this working with original *)
Theorem bin_to_nat_pres_incr_alt : forall b : bin,
bin_to_nat (incr b) = S (bin_to_nat b).
Proof.
intros b.
induction b as [|b'|b'].
- reflexivity.
- reflexivity.
- simpl.
rewrite -> IHb'.
repeat rewrite <- plus_n_O.
rewrite -> marshall_induction.S_S_plus.
reflexivity.
Qed.
(*
Print marshall_induction.bin_to_nat_pres_incr_original.
Print bin_to_nat_pres_incr_alt.
*)
Definition cut_alt :=
forall (b : bin),
S (bin_to_nat b) + S (bin_to_nat b) = S (S (bin_to_nat b + bin_to_nat b)) ->
S (bin_to_nat b) + (S (bin_to_nat b) + 0) = S (S (bin_to_nat b + (bin_to_nat b + 0))).
(* : forall b' : bin,
S (bin_to_nat b') + S (bin_to_nat b') =
S (S (bin_to_nat b' + bin_to_nat b')) *)
(* Talia: For now we expect a specific format *)
(* Talia: Then we can try patching that theorem: *)
Definition cut :=
forall (a b : nat),
S a + S b = S (S (a + b)) ->
S a + S (b + 0) = S (S (a + (b + 0))).
Patch Proof marshall_induction.bin_to_nat_pres_incr bin_to_nat_pres_incr cut by (fun (H : cut) b => H (bin_to_nat b) (bin_to_nat b)) as patch.
Print patch.
Print patch_inv.
(* Talia: Now we have an isomorphism. *)
Theorem bin_to_nat_nat_to_bin : forall n : nat,
bin_to_nat (nat_to_bin n) = n.
Proof.
induction n as [|n'].
- reflexivity.
- simpl.
rewrite -> bin_to_nat_pres_incr.
rewrite -> IHn'.
reflexivity.
Qed.
Lemma nat_to_bin_Sn_plus_n : forall n : nat,
nat_to_bin (S n + n) = B21 (nat_to_bin n).
Proof.
intros n.
induction n.
reflexivity.
simpl.
rewrite -> plus_comm.
rewrite -> IHn.
reflexivity.
Qed.
Lemma nat_to_bin_n_plus_Sn : forall n : nat,
nat_to_bin (n + S n) = B21 (nat_to_bin n).
Proof.
intros n.
rewrite -> plus_comm.
rewrite -> nat_to_bin_Sn_plus_n.
reflexivity.
Qed.
Lemma nat_to_bin_Sn_Sn : forall n : nat,
nat_to_bin (S n + S n) = B2 (incr (nat_to_bin n)).
Proof.
intros n.
simpl.
rewrite -> nat_to_bin_n_plus_Sn.
simpl.
reflexivity.
Qed.
(* Talia: In this case, the type got more complex for some of these theorems, so we actually do need
the patch and it doesn't lead to an identity term.
This shows up in a very straightforward way for this lemma. *)
Theorem bin_to_nat_plus : forall b : bin,
(bin_to_nat b) + (bin_to_nat b) = bin_to_nat (B2 b).
Proof.
intros b.
destruct b as [|b'|b'].
- reflexivity.
- simpl. apply patch. auto.
- simpl. apply patch. auto.
Qed.
(* Talia: And again here. *)
Theorem nat_to_bin_bin_to_nat : forall b : bin,
nat_to_bin(bin_to_nat(b)) = normalize b.
Proof.
intros b.
induction b as [|b'|b'].
- reflexivity.
- simpl.
rewrite <- IHb'.
apply patch_inv.
destruct (bin_to_nat b').
+ reflexivity.
+ rewrite -> nat_to_bin_Sn_Sn.
simpl.
destruct (nat_to_bin n) as [|b''|b''].
* reflexivity.
* simpl. reflexivity.
* simpl. reflexivity.
- simpl.
rewrite <- IHb'.
apply patch_inv.
destruct (bin_to_nat b') as [|n].
+ reflexivity.
+ rewrite -> nat_to_bin_Sn_Sn.
simpl. reflexivity.
Qed.
End marshall_induction_ported.