forked from jamiepg1/software-foundations-coq-workshop
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLogic.v
More file actions
761 lines (610 loc) · 13.9 KB
/
Copy pathLogic.v
File metadata and controls
761 lines (610 loc) · 13.9 KB
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
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
Require Export "Prop".
Check nat->Prop.
Inductive and (P Q:Prop) : Prop :=
conj : P -> Q -> and P Q.
Notation "P /\ Q" := (and P Q) : type_scope.
Theorem and_example :
(beautiful 0) /\ (beautiful 3).
apply conj.
apply b_0.
apply b_3.
Qed.
Theorem and_example' :
(ev 0) /\ (ev 4).
Proof.
split.
apply ev_0.
apply ev_4.
Qed.
Theorem proj1 : forall P Q : Prop,
P /\ Q -> P.
Proof.
intros P Q H.
inversion H as [HP HQ].
apply HP.
Qed.
Theorem proj2 : forall P Q : Prop,
P /\ Q -> Q.
Proof.
intros P Q H.
inversion H as [HP HQ].
apply HQ.
Qed.
Theorem and_commut : forall P Q : Prop,
P /\ Q -> Q /\ P.
Proof.
intros P Q H.
inversion H as [HP HQ].
split.
apply HQ.
apply HP.
Qed.
Theorem and_assoc : forall P Q R : Prop,
P /\ (Q /\ R) -> (P /\ Q) /\ R.
Proof.
intros P Q R H.
inversion H as [HP [HQ HR]].
split.
split.
apply HP.
apply HQ.
apply HR.
Qed.
Definition conj_fact : forall P Q R, P /\ Q -> Q /\ R -> P /\ R :=
fun (P Q R:Prop) (HPQ:P/\Q) (HQR:Q/\R) =>
conj P R (proj1 P Q HPQ) (proj2 Q R HQR).
Definition iff (P Q:Prop) := (P -> Q) /\ (Q -> P).
Notation "P <-> Q" := (iff P Q)
(at level 95, no associativity) : type_scope.
Theorem iff_implies : forall P Q : Prop,
(P <-> Q) -> P -> Q.
intros P Q HPQ HP.
inversion HPQ as [HA HB].
apply HA in HP.
apply HP.
Qed.
Theorem iff_sym : forall P Q : Prop,
(P <-> Q) -> (Q <-> P).
Proof.
intros P Q H.
inversion H as [HA HB].
split.
apply HB.
apply HA.
Qed.
Theorem iff_refl : forall P : Prop,
P <-> P.
Proof.
intros P.
split.
intro HP.
apply HP.
intro HP.
apply HP.
Qed.
Theorem iff_trans : forall P Q R : Prop,
(P <-> Q) -> (Q <-> R) -> (P <-> R).
Proof.
intros P Q R HEPQ HEQR.
inversion HEPQ as [HPQ HQP].
inversion HEQR as [HQR HRQ].
split.
intros HP. apply HQR. apply HPQ. apply HP.
intros HR. apply HQP. apply HRQ. apply HR.
Qed.
Definition beautiful_iff_gorgeous : forall n, beautiful n <-> gorgeous n :=
fun (n:nat) =>
conj (beautiful n -> gorgeous n) (gorgeous n -> beautiful n)
(beautiful__gorgeous n)
(gorgeous__beautiful n).
Inductive or (P Q : Prop) : Prop :=
| or_introl : P -> or P Q
| or_intror : Q -> or P Q.
Notation "P \/ Q" := (or P Q) : type_scope.
Theorem or_commut : forall P Q : Prop,
P \/ Q -> Q \/ P.
Proof.
intros P Q H.
inversion H as [ HP | HQ ].
apply or_intror.
apply HP.
apply or_introl.
apply HQ.
Qed.
Theorem or_commut' : forall P Q : Prop,
P \/ Q -> Q \/ P.
intros P Q H.
inversion H as [HP | HQ].
right.
apply HP.
left.
apply HQ.
Qed.
Theorem or_distributes_over_and_1 : forall P Q R : Prop,
P \/ (Q /\ R) -> (P \/ Q) /\ (P \/ R).
Proof.
intros P Q R H.
inversion H as [ HP | [ HQ HR ] ].
split.
left. apply HP.
left. apply HP.
split.
right. apply HQ.
right. apply HR.
Qed.
Theorem or_distributes_over_and_2 : forall P Q R : Prop,
(P \/ Q) /\ (P \/ R) -> P \/ (Q /\ R).
Proof.
intros P Q R H.
inversion H as [HPQ HPR].
inversion HPQ as [ HP | HQ ].
left. apply HP.
inversion HPR as [ HP | HR ].
left. apply HP.
right.
apply conj.
apply HQ.
apply HR.
Qed.
Print andb.
Theorem andb_true__and : forall b c,
andb b c = true -> b = true /\ c = true.
Proof.
intros b c H.
split.
destruct b.
reflexivity.
simpl in H.
inversion H.
destruct c.
reflexivity.
destruct b.
inversion H.
inversion H.
Qed.
Theorem and__andb_true : forall b c,
b = true /\ c = true -> andb b c = true.
Proof.
intros b c H.
inversion H.
rewrite H0. rewrite H1. reflexivity.
Qed.
Theorem andb_false : forall b c,
andb b c = false -> b = false \/ c = false.
Proof.
intros b c H.
destruct b.
simpl in H.
right. apply H.
left. reflexivity.
Qed.
Theorem orb_true : forall b c,
orb b c = true -> b = true \/ c = true.
Proof.
intros b c. intro H.
destruct b.
left. reflexivity.
destruct c. right. reflexivity.
inversion H.
Qed.
Theorem orb_false : forall b c,
orb b c = false -> b = false /\ c = false.
Proof.
intros b c H.
destruct b.
inversion H.
split. reflexivity.
destruct c.
inversion H.
reflexivity.
Qed.
Inductive False : Prop := .
Theorem False_implies_nonsense :
False -> 2 + 2 = 5.
Proof.
intros.
inversion H.
Qed.
Theorem nonsense_implies_False :
2 + 2 = 5 -> False.
Proof.
intros H.
simpl in H.
inversion H.
Qed.
Theorem ex_falso_quodlibet : forall (P:Prop),
False -> P.
Proof.
intros P contra.
inversion contra.
Qed.
Inductive True : Prop :=
tt : True.
Check tt.
Theorem xx : False \/ True.
right.
apply tt.
Qed.
Definition not (P:Prop) := P -> False.
Notation "~ x" := (not x) : type_scope.
Theorem not_False :
~ False.
Proof.
unfold not.
intro contra.
inversion contra.
Qed.
Theorem contradiction_implies_anything : forall P Q : Prop,
(P /\ ~P) -> Q.
Proof.
intros p q contra.
inversion contra as [HP HNP].
unfold not in HNP. (* Dragan: novo *)
apply HNP in HP.
inversion HP.
Qed.
Theorem double_neg : forall P : Prop,
P -> ~~P.
Proof.
intros P HP.
unfold not.
intro H.
apply H in HP.
inversion HP.
Qed.
Theorem contrapositive : forall P Q : Prop,
(P -> Q) -> (~Q -> ~P).
Proof.
intros P Q H.
intro HNQ.
unfold not.
intro HP.
apply H in HP.
apply contradiction_implies_anything with (P:=Q).
split.
apply HP.
apply HNQ.
Qed.
Theorem not_both_true_and_false : forall P : Prop,
~ (P /\ ~P).
Proof.
unfold not.
intros P contra.
apply contradiction_implies_anything with (P:=P).
apply contra.
Qed.
Theorem five_not_even :
~ ev 5.
Proof.
unfold not.
intro contra.
inversion contra.
inversion H0.
inversion H2.
Qed.
Theorem ev_not_ev_S : forall n,
ev n -> ~ ev (S n).
Proof.
intros n E.
induction E as [ | n' E' ].
unfold not.
intros contra. inversion contra.
unfold not.
intros ES.
inversion ES.
unfold not in IHE'.
apply IHE' in H0.
inversion H0.
Qed.
Definition peirce := forall P Q: Prop,
((P -> Q) -> P) -> P.
Definition classic := forall P:Prop,
~~P -> P.
Definition excluded_middle := forall P:Prop,
P \/ ~P.
Definition de_morgan_not_and_not := forall P Q:Prop,
~(~P /\ ~Q) -> P \/ Q.
Definition implies_to_or := forall P Q:Prop,
(P -> Q) -> (~P \/ Q).
Theorem peirce_eq_excluded_middle : peirce <-> excluded_middle.
split.
unfold peirce.
unfold excluded_middle.
intros peirceH.
intros P.
apply peirceH with (Q:=False).
intros H.
right.
unfold not.
intro HP.
apply H.
left.
apply HP.
unfold excluded_middle.
unfold peirce.
intros excluded_middleH.
intros P Q H.
assert (P \/ ~P).
apply excluded_middleH.
inversion H0.
apply H1.
apply H.
intros HP.
unfold not in H1.
apply H1 in HP.
inversion HP.
Qed.
Notation "x <> y" := (~ (x = y)) : type_scope.
Theorem not_false_then_true : forall b : bool,
b <> false -> b = true.
Proof.
intros b H.
destruct b.
reflexivity.
apply ex_falso_quodlibet.
unfold not in H.
apply H.
reflexivity.
Qed.
Theorem not_eq_beq_false : forall n n' : nat,
n <> n' ->
beq_nat n n' = false.
Proof.
intros n.
induction n.
intros n' H.
destruct n' as [| n''].
simpl.
apply ex_falso_quodlibet.
apply H.
reflexivity.
simpl.
reflexivity.
intros n' H.
destruct n' as [| n''].
simpl.
reflexivity.
simpl.
apply IHn.
unfold not.
intros H'.
unfold not in H.
apply H.
rewrite H'.
reflexivity.
Qed.
Print beq_nat.
Theorem beq_false_not_eq : forall n m,
false = beq_nat n m -> n <> m.
Proof.
intro n.
induction n as [| n' H].
intros m H0.
destruct m as [| m'].
unfold not.
simpl in H0.
inversion H0.
unfold not.
intro contra.
inversion contra.
intros m H'.
destruct m as [| m'].
unfold not.
intro contra.
inversion contra.
unfold not.
intro H''.
simpl in H'.
apply H in H'.
unfold not in H'.
apply H'.
inversion H''.
reflexivity.
Qed.
Inductive ex (A:Type) (P: A -> Prop) : Prop :=
ex_intro : forall (witness : A), P witness -> ex A P.
Definition some_nat_is_even : Prop :=
ex nat ev.
Check ev_SS.
Definition snie : some_nat_is_even :=
ex_intro nat ev 2 (ev_SS 0 (ev_0)).
Notation "'exists' x , p" := (ex _ (fun x => p))
(at level 200, x ident, right associativity) : type_scope.
Notation "'exists' x : X , p" := (ex _ (fun x:X => p))
(at level 200, x ident, right associativity) : type_scope.
Check exists z:nat, ev z.
Example exists_example_1 : exists n, n + (n * n) = 6.
apply ex_intro with (witness := 2).
reflexivity.
Qed.
Theorem exists_example_2 : forall n,
(exists m, n = 4 + m) ->
(exists o, n = 2 + o).
Proof.
intros n H.
inversion H as [m Hm].
exists (2 + m).
rewrite Hm.
reflexivity.
Qed.
Theorem dist_not_exists : forall (X:Type) (P : X -> Prop),
(forall x, P x) -> ~ (exists x, ~ P x).
Proof.
intros X P H.
unfold not.
intro E.
inversion E as [ x Hx ].
apply Hx.
apply H.
Qed.
Theorem not_exists_dist :
excluded_middle ->
forall (X:Type) (P : X -> Prop),
~ (exists x, ~ P x) -> (forall x, P x).
Proof.
unfold excluded_middle.
intros excl_mid.
intros X P H.
intros x.
assert ( P x \/ ~ P x).
apply excl_mid.
inversion H0.
apply H1.
apply ex_falso_quodlibet.
unfold not in H.
apply H.
unfold not in H1.
exists x.
apply H1.
Qed.
Module MyEquality.
Inductive eq_ (X:Type) : X -> X -> Prop :=
refl_equal : forall x:X, eq_ X x x.
Notation "x =_ y" := (eq_ _ x y)
(at level 70, no associativity) : type_scope.
Inductive eq' (X:Type) (x:X) : X -> Prop :=
refl_equal' : eq' X x x.
Notation "x =' y" := (eq' _ x y)
(at level 70, no associativity) : type_scope.
Theorem two_defs_of_eq_coincide : forall (X:Type) (x y : X),
x =_ y <-> x =' y.
Proof.
split.
intro H.
inversion H.
apply refl_equal'.
intros. inversion H. apply refl_equal.
Qed.
Definition inc x := S x.
Definition inc_3_eq_4 : inc 3 =_ 4 := refl_equal nat (4).
Eval cbv delta beta in 3.
End MyEquality.
Module LeFirstTry.
Inductive le : nat -> nat -> Prop :=
| le_n : forall n, le n n
| le_S : forall n, forall m, le n m -> le n (S m).
End LeFirstTry.
Inductive le (n: nat) : nat -> Prop :=
| le_n : le n n
| le_S : forall m, le n m -> le n (S m).
Inductive le' : nat -> nat -> Prop :=
| le'_0 : forall n, le' 0 n
| le'_S : forall n m, le' n m -> le' (S n) (S m).
Notation "m <= n" := (le m n).
Theorem test_le1 :
3 <= 3.
apply le_n.
Qed.
Theorem test_le2 :
3 <= 5.
apply le_S.
apply le_S.
apply le_n.
Qed.
Theorem test_le3 :
~ (2 <= 1).
Proof.
unfold not.
intro H.
inversion H.
inversion H1.
Qed.
Definition lt (n m:nat) := le (S n) m.
Notation "m < n" := (lt m n).
Inductive square_of : nat -> nat -> Prop :=
sq : forall x, square_of x (x * x).
Inductive next_nat (n:nat) : nat -> Prop :=
| nn : next_nat n (S n).
Inductive next_even (n:nat) : nat -> Prop :=
| ne_1 : ev (S n) -> next_even n (S n)
| ne_2 : ev (S (S n)) -> next_even n (S (S n)).
Inductive total_relation (R : nat -> nat -> Prop) : Prop :=
total : (forall x y, R x y \/ R y x) -> total_relation R.
Inductive symmetric_relation (R : nat -> nat -> Prop) : Prop :=
sym : (forall x y, R x y -> R y x) -> symmetric_relation R.
Inductive R : nat -> nat -> nat -> Prop :=
| c1 : R 0 0 0
| c2 : forall m n o, R m n o -> R (S m) n (S o)
| c3 : forall m n o, R m n o -> R m (S n) (S o)
| c4 : forall m n o, R (S m) (S n) (S (S o)) -> R m n o
| c5 : forall m n o, R m n o -> R n m o.
Fixpoint forallb (X:Type) (test: X -> bool) (l: list X) : bool :=
match l with
| [] => true
| h::t => andb (test h) (forallb X test t)
end.
Inductive all (X:Type) (P:X->Prop) : list X -> Prop :=
| all_nil : all X P nil
| all_cons : forall (l:list X) (x:X), all X P l -> P x -> all X P (x::l).
Theorem forallb_ok : forall X l test,
forallb X test l = true <-> all X (fun x => test x = true) l.
intros X l test.
split.
intro H.
induction l as [| x l'].
apply all_nil.
simpl in H.
apply andb_true__and in H.
inversion H.
apply IHl' in H1.
apply all_cons.
apply H1.
apply H0.
intros H.
induction H as [|l' x H1 H2].
reflexivity.
simpl.
apply and__andb_true.
split.
apply H.
apply H2.
Qed.
Fixpoint filter (X:Type) (test : X -> bool) (l : list X) : list X :=
match l with
| [] => []
| h :: t => if (test h) then h::(filter X test t) else (filter X test t)
end.
Inductive in_order_merge (X:Type) : list X -> list X -> list X -> Prop :=
| in_order_merge_nil : in_order_merge X [] [] []
| in_order_merge_left : forall (x:X) (l1 l2 l3: list X),
in_order_merge X l1 l2 l3 -> in_order_merge X (x::l1) l2 (x::l3)
| in_order_merge_right : forall (x:X) (l1 l2 l3: list X),
in_order_merge X l1 l2 l3 -> in_order_merge X (l1) (x::l2) (x::l3).
(*
---------------------
in_order_merge [] [] []
-------------------------- in_order_merge_right
in_order_merge [] [4] [4]
------------------------------
in_order_merge [2] [4] [2,4]
------------------------------- left
in_order_merge [1,2] [4] [1,2,4]
-------------------------------- in_order_merge_right
in_order_merge [1,2] [3,4] [3,1,2,4]
*)
Goal in_order_merge nat [1,2] [3,4] [3,1,2,4].
apply in_order_merge_right.
apply in_order_merge_left.
apply in_order_merge_left.
apply in_order_merge_right.
apply in_order_merge_nil.
Qed.
Goal ~ (in_order_merge nat [1,2] [3] [3,2,1]).
intro H.
inversion H.
inversion H2.
Qed.
Theorem filter_correct : forall (X:Type) (l1 l2 l3 : list X) (test : X -> bool),
in_order_merge X l1 l2 l3 ->
all X (fun x => test x = true) l1 ->
all X (fun x => test x = false) l2 ->
filter X test l3 = l1.
intros X l1 l2 l3 test Hiom Ha Hn.
induction Hiom as [| x l1' l2 l3' | x' l1 l2' l3'].
reflexivity.
inversion Ha.
simpl. rewrite H2. apply eq_remove_cons. apply IHHiom.
apply H1.
apply Hn.
inversion Hn. simpl. rewrite H2. apply IHHiom. apply Ha. apply H1.
Qed.