-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathLists.v
723 lines (587 loc) · 15 KB
/
Lists.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
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
Require Import Induction.
Module NatList.
Inductive natprod : Type :=
| pair : nat -> nat -> natprod.
Check ( pair 2 3 ).
Definition fst ( p : natprod ) : nat :=
match p with
| pair x y => x
end.
Definition snd ( p : natprod ) : nat :=
match p with
| pair x y => y
end.
Eval compute in ( fst ( pair 3 5 ) ).
Notation "( x , y )" := ( pair x y ).
Eval compute in ( snd ( 3 , 5 ) ).
Definition fst' ( p : natprod ) :=
match p with
|( x , y ) => x
end.
Definition snd' ( p : natprod ) :=
match p with
| ( x , y ) => y
end.
Definition swap_pair ( p : natprod ) : natprod :=
match p with
| ( x , y ) => ( y , x )
end.
Theorem surjective_pairing' : forall n m : nat,
( n , m ) = ( fst ( n , m ) , snd ( n , m ) ).
Proof.
intros n m. simpl. reflexivity.
Qed.
Theorem surjective_pairing_stuck : forall ( p : natprod ),
p = ( fst p , snd p ).
Proof.
intros. destruct p as [ n m ]. rewrite <- surjective_pairing'. simpl. reflexivity.
Qed.
Theorem snd_fst_is_swap : forall(p : natprod),
(snd p, fst p) = swap_pair p.
Proof.
intros. destruct p as [ n m ]. simpl. reflexivity.
Qed.
Theorem fst_swap_is_snd : forall(p : natprod),
fst (swap_pair p) = snd p.
Proof.
intros. destruct p as [ n m ]. simpl. reflexivity.
Qed.
Inductive natlist : Type :=
| nil : natlist
| cons : nat -> natlist -> natlist.
Definition mylist := cons 1 ( cons 2 ( cons 3 nil ) ) .
Check mylist.
Print mylist.
Notation "x :: l" := ( cons x l ) ( at level 60, right associativity ).
Notation "[ ]" := nil.
Notation "[ x ; .. ; y ]" := ( cons x .. ( cons y nil ) .. ).
Definition mylist1 := 1 :: (2 :: (3 :: nil)).
Definition mylist2 := 1 :: 2 :: 3 :: nil.
Definition mylist3 := [1;2;3].
Print mylist1.
Print mylist2.
Print mylist3.
Notation "x + y" := ( plus x y )
( at level 50 , left associativity ).
Check 1 + 2 :: [3].
Fixpoint repeat ( n count : nat ) : natlist :=
match count with
| O => nil
| S count' => n :: repeat n count'
end.
Definition mylist4 := repeat 3 4.
Eval compute in mylist4.
Fixpoint length ( l : natlist ) : nat :=
match l with
| nil => O
| h :: t => S ( length t )
end.
Fixpoint app ( l1 l2 : natlist ) : natlist :=
match l1 with
| nil => l2
| h :: t => h :: app t l2
end.
Notation "x ++ y" := ( app x y )
( right associativity, at level 60 ).
Example test_app1 : [1;2;3] ++ [ 4;5] = [1;2;3;4;5].
Proof.
simpl. reflexivity.
Qed.
Example test_app2: nil ++ [4;5] = [4;5].
Proof.
simpl. reflexivity.
Qed.
Example test_app3: [1;2;3] ++ nil = [1;2;3].
Proof.
simpl. reflexivity.
Qed.
Definition hd ( default : nat ) ( l : natlist ) : nat :=
match l with
| nil => default
| h :: t => h
end.
Definition tl ( l : natlist ) : natlist :=
match l with
| nil => nil
| h :: t => t
end.
Example test_hd1 : hd 0 [ 1 ; 2 ; 3 ] = 1.
Proof.
simpl. reflexivity.
Qed.
Example test_hd2 : hd 0 [] = 0.
Proof.
simpl. reflexivity.
Qed.
Example test_tl : tl [ 1 ; 2 ; 3 ] = [ 2 ; 3 ].
Proof.
simpl. reflexivity.
Qed.
Example test_tl1 : forall a : nat, tl [a] = nil.
Proof.
intros. simpl. reflexivity.
Qed.
Fixpoint nonzeros ( l : natlist ) : natlist :=
match l with
| nil => nil
| O :: t => nonzeros t
| h :: t => h :: nonzeros t
end.
Example test_nonzeros: nonzeros [0;1;0;2;3;0;0] = [1;2;3].
Proof.
simpl. reflexivity.
Qed.
Fixpoint oddmembers ( l : natlist ) : natlist :=
match l with
| nil => nil
| h :: t => if evenb h then oddmembers t else h :: oddmembers t
end.
Example test_oddmembers: oddmembers [0;1;0;2;3;0;0] = [1;3].
Proof.
simpl. reflexivity.
Qed.
Fixpoint countoddmembers ( l : natlist ) : nat :=
match l with
| nil => O
| h :: t => if evenb h then countoddmembers t else S ( countoddmembers t )
end.
Example test_countoddmembers1: countoddmembers [1;0;3;1;4;5] = 4.
Proof.
simpl. reflexivity.
Qed.
Example test_countoddmembers2: countoddmembers [0;2;4] = 0.
Proof.
simpl. reflexivity.
Qed.
Example test_countoddmembers3: countoddmembers nil = 0.
Proof.
simpl. reflexivity.
Qed.
Fixpoint alternate ( l1 l2 : natlist ) : natlist :=
match l1 with
| nil => l2
| h :: t => match l2 with
| nil => l1
| h' :: t' => h :: h' :: alternate t t'
end
end.
Example test_alternate1: alternate [1;2;3] [4;5;6] = [1;4;2;5;3;6].
Proof.
simpl. reflexivity.
Qed.
Example test_alternate2: alternate [1] [4;5;6] = [1;4;5;6].
Proof.
simpl. reflexivity.
Qed.
Example test_alternate3: alternate [1;2;3] [4] = [1;4;2;3].
Proof.
simpl. reflexivity.
Qed.
Example test_alternate4: alternate [] [20;30] = [20;30].
Proof.
simpl. reflexivity.
Qed.
Definition bag := natlist.
Fixpoint count ( v : nat ) ( s : bag ) : nat :=
match s with
| nil => O
| h :: t => if beq_nat v h then S ( count v t ) else count v t
end.
Example test_count1: count 1 [1;2;3;1;4;1] = 3.
Proof.
simpl. reflexivity.
Qed.
Example test_count2: count 6 [1;2;3;1;4;1] = 0.
Proof.
simpl. reflexivity.
Qed.
Check app.
Definition sum : bag -> bag -> bag := app.
Example test_sum1: count 1 (sum [1;2;3] [1;4;1]) = 3.
Proof.
simpl. reflexivity.
Qed.
Definition add (v:nat) (s:bag) : bag := cons v s.
Example test_add1: count 1 (add 1 [1;4;1]) = 3.
Proof.
simpl. reflexivity.
Qed.
Example test_add2: count 5 (add 1 [1;4;1]) = 0.
Proof.
simpl. reflexivity.
Qed.
Definition member (v:nat) (s:bag) : bool := negb ( beq_nat ( count v s ) O ).
Example test_member1: member 1 [1;4;1] = true.
Proof.
compute. reflexivity.
Qed.
Example test_member2: member 2 [1;4;1] = false.
Proof.
compute. reflexivity.
Qed.
Fixpoint remove_one (v:nat) (s:bag) : bag :=
match s with
| nil => nil
| h :: t => if beq_nat h v then t else h :: remove_one v t
end.
Example test_remove_one1: count 5 (remove_one 5 [2;1;5;4;1]) = 0.
Proof.
simpl. reflexivity.
Qed.
Example test_remove_one2: count 5 (remove_one 5 [2;1;4;1]) = 0.
Proof.
simpl. reflexivity.
Qed.
Example test_remove_one3: count 4 (remove_one 5 [2;1;4;5;1;4]) = 2.
Proof.
simpl. reflexivity.
Qed.
Example test_remove_one4: count 5 (remove_one 5 [2;1;5;4;5;1;4]) = 1.
Proof.
simpl. reflexivity.
Qed.
Fixpoint remove_all (v:nat) (s:bag) : bag :=
match s with
| nil => nil
| h :: t => if beq_nat h v then remove_all v t else h :: remove_all v t
end.
Example test_remove_all1: count 5 (remove_all 5 [2;1;5;4;1]) = 0.
Proof.
simpl. reflexivity.
Qed.
Example test_remove_all2: count 5 (remove_all 5 [2;1;4;1]) = 0.
Proof.
simpl. reflexivity.
Qed.
Example test_remove_all3: count 4 (remove_all 5 [2;1;4;5;1;4]) = 2.
Proof.
simpl. reflexivity.
Qed.
Example test_remove_all4: count 5 (remove_all 5 [2;1;5;4;5;1;4;5;1;4]) = 0.
Proof.
simpl. reflexivity.
Qed.
Fixpoint subset ( s1 : bag ) ( s2 : bag ) : bool :=
match s1 with
| nil => true
| h :: t => if member h s2 then subset t ( remove_one h s2 )
else false
end.
Example test_subset1: subset [1;2] [2;1;4;1] = true.
Proof.
simpl. reflexivity.
Qed.
Example test_subset2: subset [1;2;2] [2;1;4;1] = false.
Proof.
simpl. reflexivity.
Qed.
Theorem nil_app : forall l : natlist, [] ++ l = l.
Proof.
intros l. simpl. reflexivity.
Qed.
Theorem tl_length_pred : forall l:natlist,
pred (length l) = length (tl l).
Proof.
intros l. destruct l as [ | n l' ].
Case "l = []".
reflexivity.
Case "l = cons n l'".
simpl. reflexivity.
Qed.
Theorem app_assoc : forall l1 l2 l3 : natlist,
(l1 ++ l2) ++ l3 = l1 ++ (l2 ++ l3).
Proof.
intros l1 l2 l3. induction l1 as [ | n l1'].
Case "l1 = nil".
simpl. reflexivity.
Case "l1 = cons n l'".
simpl. rewrite -> IHl1'. reflexivity.
Qed.
Theorem app_length : forall l1 l2 : natlist,
length (l1 ++ l2) = (length l1) + (length l2).
Proof.
intros l1 l2. induction l1 as [ | n l1' ].
Case "l1 = nil".
simpl. reflexivity.
Case "l1 = cons n l1'".
simpl. rewrite -> IHl1'. reflexivity.
Qed.
Fixpoint snoc ( l : natlist ) ( v : nat ) : natlist :=
match l with
| nil => [ v ]
| h :: t => h :: snoc t v
end.
Fixpoint rev ( l : natlist ) : natlist :=
match l with
| nil => nil
| h :: t => snoc ( rev t ) h
end.
Example test_rev1: rev [1;2;3] = [3;2;1].
Proof.
simpl. reflexivity.
Qed.
Example test_rev2: rev nil = nil.
Proof.
simpl. reflexivity.
Qed.
(*
Theorem rev_lenght_firsttry : forall l : natlist,
length ( rev l ) = length l.
Proof.
intros. induction l as [ | n l'].
case "l = nil".
simpl. reflexivity.
case "l = cons n l'".
simpl. rewrite <- IHl'.
*)
Theorem length_snoc : forall n : nat, forall l : natlist,
length (snoc l n) = S (length l).
Proof.
intros n l. induction l as [ | h l'].
Case "l = nil".
simpl. reflexivity.
Case "l = cons h l'".
simpl. rewrite -> IHl'. reflexivity.
Qed.
Theorem rev_lenght_firsttry : forall l : natlist,
length ( rev l ) = length l.
Proof.
intros. induction l as [ | n l'].
Case "l = nil".
simpl. reflexivity.
Case "l = cons n l'".
simpl. rewrite -> length_snoc. rewrite -> IHl'. reflexivity.
Qed.
Theorem app_nil_end : forall l : natlist,
l ++ [] = l.
Proof.
intros l. induction l as [ | n l'].
Case "l = nil".
simpl. reflexivity.
Case "l = cons n l'".
simpl. rewrite -> IHl'. reflexivity.
Qed.
(*
Theorem snoc_theorem : forall ( n : nat ) ( l : natlist ),
snoc l n = l ++ [ n ].
Proof.
intros n l. induction l as [ | n' l'].
Case "l = nil".
simpl. reflexivity.
Case "l = cons n' l'".
simpl. rewrite -> IHl'. reflexivity.
Qed.
*)
Theorem snoc_rev : forall ( n : nat ) ( l : natlist ),
rev ( snoc l n ) = n :: rev l.
Proof.
intros n l. induction l as [ | n' l'].
Case "l = nil".
simpl. reflexivity.
Case "l = cons n' l'".
simpl. rewrite -> IHl'. simpl. reflexivity.
Qed.
Theorem rev_involutive : forall l : natlist,
rev (rev l) = l.
Proof.
intros l. induction l as [ | n l'].
Case "l = nil".
simpl. reflexivity.
Case "l = cons n l'".
simpl. rewrite -> snoc_rev. rewrite -> IHl'.
reflexivity.
Qed.
Theorem app_assoc4 : forall l1 l2 l3 l4 : natlist,
l1 ++ (l2 ++ (l3 ++ l4)) = ((l1 ++ l2) ++ l3) ++ l4.
Proof.
intros l1 l2 l3 l4. induction l1 as [ | n l1' ].
Case "l1 = nil".
simpl. rewrite -> app_assoc. reflexivity.
Case "l1 = cons n l1'".
simpl. rewrite -> app_assoc. rewrite -> app_assoc. reflexivity.
Qed.
Theorem snoc_append : forall ( l : natlist ) ( n : nat ),
snoc l n = l ++ [n].
Proof.
intros l n. induction l as [ | n' l'].
Case "l = nil".
simpl. reflexivity.
Case "l = cons n' l'".
simpl. rewrite <- IHl'. reflexivity.
Qed.
Theorem distr_rev : forall l1 l2 : natlist,
rev (l1 ++ l2) = (rev l2) ++ (rev l1).
Proof.
intros l1 l2. induction l1 as [ | n l1' ].
Case "l1 = nil".
simpl. rewrite -> app_nil_end. reflexivity.
Case "l1 = cons n l1'".
simpl. rewrite -> IHl1'. rewrite -> snoc_append.
rewrite -> app_assoc. rewrite -> snoc_append.
reflexivity.
Qed.
Lemma nonzeros_app : forall l1 l2 : natlist,
nonzeros (l1 ++ l2) = (nonzeros l1) ++ (nonzeros l2).
Proof.
intros l1 l2. induction l1 as [ | n l1' ].
Case "l1 = nil".
simpl. reflexivity.
Case "l1 = cons n l1'".
induction n as [ | n' ].
SCase "n = O".
simpl. rewrite -> IHl1'. reflexivity.
SCase "n = S n'".
simpl. rewrite -> IHl1'. reflexivity.
Qed.
Fixpoint beq_natlist (l1 l2 : natlist) : bool :=
match l1 , l2 with
| nil , nil => true
| nil , h :: t => false
| h :: t , nil => false
| h1 :: t1 , h2 :: t2 => if beq_nat h1 h2 then beq_natlist t1 t2 else false
end.
Example test_beq_natlist1 : (beq_natlist nil nil = true).
Proof.
simpl. reflexivity.
Qed.
Example test_beq_natlist2 : beq_natlist [1;2;3] [1;2;3] = true.
Proof.
simpl. reflexivity.
Qed.
Example test_beq_natlist3 : beq_natlist [1;2;3] [1;2;4] = false.
Proof.
simpl. reflexivity.
Qed.
Theorem beq_equal : forall n : nat, beq_nat n n = true.
Proof.
intros n. induction n as [ | n'].
Case "n = O".
simpl. reflexivity.
Case "n = S n'".
simpl. rewrite -> IHn'. reflexivity.
Qed.
Theorem beq_natlist_refl : forall l:natlist,
true = beq_natlist l l.
Proof.
intros l. induction l as [ | n l'].
Case "l = nil".
simpl. reflexivity.
Case "l = cons n l'".
simpl. rewrite -> beq_equal. rewrite -> IHl'.
reflexivity.
Qed.
Theorem count_member_nonzero : forall (s : bag),
ble_nat 1 (count 1 (1 :: s)) = true.
Proof.
intros s. simpl. reflexivity.
Qed.
Theorem ble_n_Sn : forall n,
ble_nat n (S n) = true.
Proof.
intros n. induction n as [ | n' ].
Case "n = O".
simpl. reflexivity.
Case "n = S n'".
simpl. rewrite -> IHn'. reflexivity.
Qed.
(*
Theorem remove_decreases_count: forall (s : bag),
ble_nat (count 0 (remove_one 0 s)) (count 0 s) = true.
Proof.
intros s. induction s as [ | n' s'].
Case "s = nil".
simpl. reflexivity.
Case "s = cons n' s'".
*)
Theorem rev_injective : forall (l1 l2 : natlist) ,
rev l1 = rev l2 -> l1 = l2.
Proof.
intros l1 l2 H. rewrite <- rev_involutive. rewrite <- H.
rewrite -> rev_involutive. reflexivity.
Qed.
Fixpoint index_bad ( n : nat ) ( l : natlist ) : nat :=
match l with
| nil => 42
| h :: tl => match beq_nat n O with
| true => h
| false => index_bad ( pred n ) tl
end
end.
Inductive natoption : Type :=
| None : natoption
| Some : nat -> natoption.
Fixpoint index ( n : nat ) ( l : natlist ) : natoption :=
match l with
| nil => None
| h :: tl => match n with
| O => Some h
| S n' => index n' tl
end
end.
Example test_index1 : index 0 [4;5;6;7] = Some 4.
Proof. reflexivity. Qed.
Example test_index2 : index 3 [4;5;6;7] = Some 7.
Proof. reflexivity. Qed.
Example test_index3 : index 10 [4;5;6;7] = None.
Proof. reflexivity. Qed.
Fixpoint index' ( n : nat ) ( l : natlist ) : natoption :=
match l with
| nil => None
| h :: tl => if beq_nat n O then Some h else index' ( pred n ) tl
end.
Definition option_elim ( d : nat ) ( o : natoption ) : nat :=
match o with
| None => d
| Some a => a
end.
Definition hd_opt ( l : natlist ) : natoption :=
match l with
| nil => None
| h :: tl => Some h
end.
Example test_hd_opt1 : hd_opt [] = None.
Proof.
reflexivity.
Qed.
Example test_hd_opt2 : hd_opt [1] = Some 1.
Proof.
reflexivity.
Qed.
Example test_hd_opt3 : hd_opt [5;6] = Some 5.
Proof.
reflexivity.
Qed.
Theorem option_elim_hd : forall (l:natlist) (default:nat),
hd default l = option_elim default (hd_opt l).
Proof.
intros. induction l as [ | h' l'].
Case "l = nil".
simpl. reflexivity.
Case "l = cons h' l'".
simpl. reflexivity.
Qed.
Module Dictionary.
Inductive dictionary : Type :=
| empty : dictionary
| record : nat -> nat -> dictionary -> dictionary.
Definition insert ( key value : nat ) ( d : dictionary ) : dictionary :=
record key value d.
Fixpoint find ( key : nat ) ( d : dictionary ) : natoption :=
match d with
| empty => None
| record key' value' d' => if beq_nat key key' then Some value'
else find key d'
end.
Theorem dictionary_invariant1' : forall (d : dictionary) (k v: nat),
(find k (insert k v d)) = Some v.
Proof.
intros d k v. simpl.
rewrite -> beq_equal. reflexivity.
Qed.
Theorem dictionary_invariant2' : forall (d : dictionary) (m n o: nat),
beq_nat m n = false -> find m d = find m (insert n o d).
Proof.
intros d m n o H. simpl. rewrite -> H. reflexivity.
Qed.
End Dictionary.
End NatList.