-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathLogic.v
401 lines (340 loc) · 8.11 KB
/
Logic.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
Require Export MoreCoq.
Check ( 3 = 3 ).
Check ( forall ( n : nat ), n = 2 ).
Lemma silly : 0 * 3 = 0.
Proof. reflexivity. Qed.
Print silly.
Lemma silly_implication : ( 1 + 1 ) = 2 -> 0 * 3 = 0.
Proof.
intros H. reflexivity.
Qed.
Print silly_implication.
Inductive and ( P Q : Prop ) : Prop :=
conj : P -> Q -> ( and P Q ).
Print and_rect.
Notation "P /\ Q " := ( and P Q ) : type_scope.
Check conj.
Theorem and_example :
( 0 = 0 ) /\ ( 4 = mult 2 2 ).
Proof.
apply conj.
Case "left".
reflexivity.
Case "right".
reflexivity.
Qed.
Theorem and_example' :
( 0 = 0 ) /\ ( 4 = mult 2 2 ).
Proof.
split. (* short hand for apply conj *)
Case "left".
reflexivity.
Case "right".
reflexivity.
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_commute : forall ( P Q : Prop ),
P /\ Q -> Q /\ P.
Proof.
intros P Q H. inversion H as [ HP HQ ].
split.
Case "left".
apply HQ.
Case "right".
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.
Case "left".
split.
SCase "left". apply HP.
SCase "right". apply HQ.
Case "right". apply HR.
Qed.
Definition iff ( P Q : Prop ) : 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 ).
Proof.
intros P Q H.
inversion H as [ Hf Hs ].
apply Hf.
Qed.
Theorem iff_sym : forall P Q : Prop,
( P <-> Q ) -> ( Q <-> P ).
Proof.
intros P Q H. inversion H as [ HPQ HQP].
split.
Case "left". apply HQP.
Case "right". apply HPQ.
Qed.
Theorem iff_refl : forall P : Prop,
P <-> P.
Proof.
intros P.
split.
Case "left".
intros H. apply H.
Case "right".
intros H. apply H.
Qed.
Theorem iff_trans : forall P Q R : Prop,
( P <-> Q ) -> ( Q <-> R ) -> ( P <-> R ).
Proof.
intros P Q R HPQ HQR. inversion HPQ as [ HP HQ]. inversion HQR as [ HQ' HP'].
split.
Case "left". intros H.
apply HP in H. apply HQ' in H. apply H.
Case "right". intros H.
apply HP' in H. apply HQ in H. apply H.
Qed.
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.
Check or_introl.
Check or_intror.
Theorem or_commut : forall P Q : Prop,
P \/ Q -> Q \/ P.
Proof.
intros P Q H.
inversion H as [ HP | HQ ].
Case "left". apply or_intror. apply HP.
Case "right". apply or_introl. apply HQ.
Qed.
Theorem or_commut' : forall P Q : Prop,
P \/ Q -> Q \/ P.
Proof.
intros P Q H.
inversion H as [ HP | HQ].
Case "left". right. apply HP.
Case "right". left. apply HQ.
Qed.
Theorem or_distribute_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 ] ].
Case "left". split.
SCase "left". left. apply HP.
SCase "right". left. apply HP.
Case "right". split.
SCase "left". right. apply HQ.
SCase "right". 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 [ [ HP | HQ] [ HP' | HQ']].
Case "left". left. apply HP'.
Case "right". left. apply HP.
Case "left". left. apply HP'.
Case "right". right. split.
SCase "left". apply HQ.
SCase "right". apply HQ'.
Qed.
Theorem or_distributes_over_and : forall P Q R : Prop,
P \/ (Q /\ R) <-> (P \/ Q) /\ (P \/ R).
Proof.
intros P Q R. split.
Case "left".
intros H. apply or_distribute_over_and_1. apply H.
Case "right".
intros H. apply or_distributes_over_and_2 . apply H.
Qed.
Theorem andb_prop : forall b c,
andb b c = true -> b = true /\ c = true.
Proof.
intros b c H. unfold andb in H.
destruct b.
Case "b = true". split.
SCase "left". reflexivity.
SCase "right". apply H.
Case "b = false". inversion H.
Qed.
Theorem andb_true_intro : 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. unfold andb in H.
destruct b.
Case "b = true".
right. apply H.
Case "b = false".
left. apply H.
Qed.
Theorem orb_prop : forall b c,
orb b c = true -> b = true \/ c = true.
Proof.
intros b c H. unfold orb in H.
destruct b.
Case "b = true".
left. reflexivity.
Case "b = false".
right. apply H.
Qed.
Theorem orb_false_elim : forall b c,
orb b c = false -> b = false /\ c = false.
Proof.
intros b c H. unfold orb in H.
destruct b.
Case "b = true".
inversion H.
Case "b = false".
split.
SCase "left". reflexivity.
SCase "right". apply H.
Qed.
Inductive False : Prop := .
Theorem false_implies_nonsense :
False -> 2 + 2 = 5.
Proof.
intros H. inversion H.
Qed.
(*
Conversely, the only way to prove False is if there is already something
nonsensical or contradictory in the context:
*)
Theorem nonsense_implies_False :
2 + 2 = 5 -> False.
Proof.
intros H. simpl in H. inversion H.
Qed.
(*
You can prove anything from false assumption *)
Theorem ex_falso_quodlibet : forall (P:Prop),
False -> P.
Proof.
intros P H. inversion H.
Qed.
Inductive True : Prop := tt.
Print True.
Definition not ( P : Prop ) : Prop := P -> False.
Notation "~ x" := (not x) : type_scope.
Check not.
Theorem not_False :
~ False.
Proof.
unfold not. intros H. inversion H.
Qed.
Theorem contradiction_implies_anything : forall P Q : Prop,
(P /\ ~P) -> Q.
Proof.
intros P Q H. inversion H as [ HP NHP ].
unfold not in NHP. apply NHP in HP. inversion HP.
Qed.
Theorem double_neg : forall P : Prop,
P -> ~~P.
Proof.
intros P H. unfold not. intros HN.
apply HN in H. inversion H.
Qed.
Theorem contrapositive : forall P Q : Prop,
(P -> Q) -> (~Q -> ~P).
Proof.
intros P Q H1 H2. unfold not in H2. unfold not.
intros Hp. apply H1 in Hp. apply H2 in Hp.
inversion Hp.
Qed.
Theorem not_both_true_and_false : forall P : Prop,
~ (P /\ ~P).
Proof.
intros P. unfold not. intros H.
inversion H. apply H1 in H0. apply H0.
Qed.
Theorem classic_double_neg : forall P : Prop,
~~P -> P.
Proof.
intros P H. unfold not in H.
Abort.
Theorem excluded_middle_irrefutable: forall (P:Prop), ~ ~ (P \/ ~ P).
Proof.
intros P. unfold not. intros H.
apply H. right. intros H1.
apply H. left. apply H1.
Qed.
Notation "x <> y" := (~ (x = y)) : type_scope.
Theorem not_false_then_true :forall b : bool,
b <> false -> b = true.
Proof.
intros b. destruct b.
Case "b = true".
intros H. reflexivity.
Case "b = false".
unfold not. intros H. apply ex_falso_quodlibet. apply H.
reflexivity.
Qed.
Theorem false_beq_nat : forall n m : nat,
n <> m ->
beq_nat n m = false.
Proof.
Abort.
SearchAbout beq_nat.
Theorem beq_nat_false : forall n m,
beq_nat n m = false -> n <> m.
Proof.
Abort.
(* proofs from type theory and functional programming *)
Theorem implication_trans : forall A B C : Prop,
( A -> B ) -> ( B -> C ) -> ( A -> C).
Proof.
intros A B C H1 H2 H3. apply H1 in H3. apply H2 in H3. apply H3.
Qed.
Theorem prob_second : forall A B C : Prop ,
( ( A \/ B ) -> C ) -> ( ( A -> C ) /\ ( B -> C ) ).
Proof.
intros A B C H. split.
Case "left".
intros H0. apply H.
left. apply H0.
Case "right".
intros H0. apply H.
right. apply H0.
Qed.
Theorem prob_thrid : forall A B C : Prop ,
( A -> ( B -> C ) ) -> ( ( A /\ B ) -> C ).
Proof.
intros A B C H1 H2. inversion H2. apply H1 in H. apply H. apply H0.
Qed.
Theorem prob_four : forall A B : Prop ,
( A -> B ) -> ( B -> A ).
Proof.
intros A B H1 H2.
(* probably not provable in Coq.
A : Prop
B : Prop
H1 : A -> B
H2 : B
============================
A
*)
Abort.
Theorem prob_five : forall A B : Prop,
A -> ~ ~ A.
Proof.
intros A B H. unfold not. intros Hf. apply Hf in H.
inversion H.
Qed.