-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathBasics.v
443 lines (337 loc) · 9.6 KB
/
Basics.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
(* Chapter 1 Basics *)
Inductive day : Type :=
| monday : day
| tuesday : day
| wednesday : day
| thursday : day
| friday : day
| saturday : day
| sunday : day.
Check day_rect. (* types *)
Check day_ind. (* props *)
Check day_rec. (* sets *)
Definition next_weekday (d:day) : day :=
match d with
| monday => tuesday
| tuesday => wednesday
| wednesday => thursday
| thursday => friday
| _ => monday
end.
Eval compute in next_weekday friday. (* monday *)
Eval compute in next_weekday (next_weekday saturday). (* tuesday *)
Example test_next_weekday : next_weekday (next_weekday saturday) = tuesday.
Proof. simpl. reflexivity. Qed.
Extraction next_weekday. (* Extracts the ocaml version of next_weekday *)
(* Booleans *)
Inductive bool : Type :=
| true : bool
| false : bool.
Definition negb (b:bool) : bool :=
match b with
| true => false
| false => true
end.
Definition andb (b1 b2 : bool) : bool :=
match b1 with
| true => b2
| false => false
end.
Definition orb (b1 b2 : bool) : bool :=
match b1 with
| true => true
| false => b2
end.
Example test_orb1: orb true false = true.
Proof. reflexivity. Qed.
Example test_orb2: orb false false = false.
Proof. reflexivity. Qed.
Example test_orb3: orb false true = true.
Proof. reflexivity. Qed.
Example test_orb4: orb true true = true.
Proof. reflexivity. Qed.
(* Exercise: * nandb *)
Definition nandb (b1 b2 : bool) : bool :=
match b1, b2 with
| true, true => false
| _, _ => true
end.
Example test_nandb1: nandb true false = true.
Proof. reflexivity. Qed.
Example test_nandb2: nandb false false = true.
Proof. reflexivity. Qed.
Example test_nandb3: nandb false true = true.
Proof. reflexivity. Qed.
Example test_nandb4: nandb true true = false.
Proof. reflexivity. Qed.
(* Exercise: * andb3 *)
Definition andb3 (b1 b2 b3 : bool) : bool :=
match b1, b2, b3 with
| true, true, true => true
| _, _, _ => false
end.
Example test_andb31: andb3 true true true = true.
Proof. reflexivity. Qed.
Example test_andb32: andb3 false true true = false.
Proof. reflexivity. Qed.
Example test_andb33: andb3 true false true = false.
Proof. reflexivity. Qed.
Example test_andb34: andb3 true true false = false.
Proof. reflexivity. Qed.
(* Function Types *)
Check true. (* bool *)
Check (negb true). (* bool *)
Check negb. (* bool -> bool *)
Check andb. (* bool -> bool -> bool *)
(* Numbers *)
Module Playground1.
Inductive nat : Type :=
| O : nat
| S : nat -> nat.
Definition pred (n:nat) : nat :=
match n with
| O => O
| S n' => n'
end.
End Playground1.
Definition minustwo (n:nat) : nat :=
match n with
| O => O
| S O => O
| S (S n') => n'
end.
Check S (S (S (S O))). (* 4 : nat *)
Eval compute in minustwo 4. (* 2 : nat *)
Check S. (* nat -> nat *)
Check pred. (* nat -> nat *)
Check minustwo. (* nat -> nat *)
Fixpoint evenb (n:nat) : bool :=
match n with
| O => true
| S O => false
| S (S n') => evenb n'
end.
Definition oddb (n:nat) : bool := negb (evenb n).
Example test_oddb1: oddb (S O) = true.
Proof. reflexivity. Qed.
Example test_oddb2: oddb (S (S (S (S 0)))) = false.
Proof. reflexivity. Qed.
Module Playground2.
Fixpoint plus (n m : nat) : nat :=
match n with
| O => m
| S n' => S (plus n' m)
end.
Eval compute in plus (S (S (S O))) (S (S O)).
Fixpoint mult (n m : nat) : nat :=
match n with
| O => O
| S n' => plus m (mult n' m)
end.
Example test_mult1: mult 3 3 = 9.
Proof. reflexivity. Qed.
Fixpoint minus (n m : nat) : nat :=
match n, m with
| O, _ => O
| _, O => n
| S n', S m' => minus n' m'
end.
End Playground2.
Fixpoint exp (base power : nat) : nat :=
match power with
| O => S O
| S p => mult base (exp base p)
end.
(* Exercise: * factorial *)
Fixpoint factorial (n:nat) : nat :=
match n with
| O => S O
| S n' => mult n (factorial n')
end.
Example test_factorial1: factorial 3 = 6.
Proof. reflexivity. Qed.
Example test_factorial2: factorial 5 = mult 10 12.
Proof. reflexivity. Qed.
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.
Check ((0 + 1) + 1).
Fixpoint beq_nat (n m : nat) : bool :=
match n, m with
| O, O => true
| S n', S m' => beq_nat n' m'
| _, _ => false
end.
Fixpoint ble_nat (n m : nat) : bool :=
match n, m with
| O, _ => true
| S n', S m' => ble_nat n' m'
| _, _ => false
end.
Example test_ble_nat1: ble_nat 2 2 = true.
Proof. reflexivity. Qed.
Example test_ble_nat2: ble_nat 2 4 = true.
Proof. reflexivity. Qed.
Example test_ble_nat3: ble_nat 4 2 = false.
Proof. reflexivity. Qed.
(* Exerice: ** blt_nat *)
Definition blt_nat (n m : nat) : bool :=
andb (ble_nat n m) (negb (beq_nat n m)).
Example test_blt_nat1: blt_nat 2 2 = false.
Proof. reflexivity. Qed.
Example test_blt_nat2: blt_nat 2 4 = true.
Proof. reflexivity. Qed.
Example test_blt_nat3: blt_nat 4 2 = false.
Proof. reflexivity. Qed.
(* Proof by Simplification *)
Theorem plus_0_n : forall n:nat, 0 + n = n.
Proof.
intros n. reflexivity. Qed.
(* Theorem, Example, Lemma, Fact, Remark: all meant the same thing to Coq. *)
Theorem plus_1_l : forall n:nat, 1 + n = S n.
Proof.
intros n. reflexivity. Qed.
Theorem mult_0_l : forall n:nat, 0 * n = 0.
Proof.
intros n. reflexivity. Qed.
(* Proof by Rewriting *)
Theorem plus_id_example : forall n m : nat,
n = m ->
n + n = m + m.
Proof.
intros n m. (* Introduce things into the context *)
intros H. (* Introduce hypothesis into the context, and call it H *)
rewrite -> H. (* Replace LHS H with RHS H in the goal *)
reflexivity.
Qed.
(* Exercise: * plus_id_exercise *)
Theorem plus_id_exercise : forall n m o : nat,
n = m -> m = o -> n + m = m + o.
Proof.
intros n m o.
intros H1 H2.
rewrite H1. rewrite <- H2.
reflexivity.
Qed.
Theorem mult_0_plus : forall n m : nat,
(0 + n) * m = n * m.
Proof.
intros n m.
rewrite plus_0_n.
reflexivity.
Qed.
(* Exercise: ** mult_S_1 *)
Theorem mult_S_1 : forall n m : nat,
m = S n ->
m * (1 + n) = m * m.
Proof.
intros n m.
intros H.
rewrite plus_1_l.
rewrite <- H.
reflexivity.
Qed.
(* Proof by Case Analysis *)
Theorem plus_1_neq_0_firsttry : forall n:nat,
beq_nat (n + 1) 0 = false.
Proof.
intros n.
simpl. (* does nothing *)
Abort.
Theorem plus_1_neq_0 : forall n:nat,
beq_nat (n + 1) 0 = false.
Proof.
intros n.
destruct n as [| n'].
reflexivity.
reflexivity.
Qed.
Theorem negb_involutive : forall b:bool,
negb (negb b) = b.
Proof.
intros b.
destruct b.
reflexivity. reflexivity.
Qed.
(* Exercise: * zero_nbeq_plus_1 *)
Theorem zero_nbeq_plus_1 : forall n:nat,
beq_nat 0 (n + 1) = false.
Proof.
intros n.
destruct n as [| n'].
reflexivity.
reflexivity.
Qed.
(* More Exercises *)
(* Exercise: ** boolean functions *)
Theorem identity_fn_applied_twice : forall (f : bool -> bool),
(forall (x : bool), f x = x) ->
forall b:bool, f (f b) = b.
Proof.
intros.
destruct b.
rewrite H. rewrite H. reflexivity.
rewrite H. rewrite H. reflexivity.
Qed.
Theorem negation_fn_applied_twice : forall (f : bool -> bool),
(forall (x : bool), f x = negb x) ->
forall b:bool, f (f b) = b.
Proof.
intros.
destruct b.
rewrite H. rewrite H. rewrite negb_involutive. reflexivity.
rewrite H. rewrite H. rewrite negb_involutive. reflexivity.
Qed.
(* Exercise: ** andb_eq_orb *)
Theorem andb_eq_orb : forall b c : bool,
andb b c = orb b c ->
b = c.
Proof.
intros b c.
destruct b.
destruct c.
intros H. reflexivity.
intros H. simpl in H. rewrite H. reflexivity.
destruct c.
intros H. simpl in H. rewrite H. reflexivity.
intros H. reflexivity.
Qed.
(* Exercise: *** binary *)
Inductive bin : Type :=
| zero : bin
| twice : bin -> bin
| twicePlus1 : bin -> bin.
Fixpoint increment (b:bin) : bin :=
match b with
| zero => twicePlus1 b
| twice b' => twicePlus1 b'
| twicePlus1 b' => twice (increment b')
end.
Fixpoint bin_to_nat (b:bin) : nat :=
match b with
| zero => O
| twice b' => mult 2 (bin_to_nat b')
| twicePlus1 b' => plus 1 (mult 2 (bin_to_nat b'))
end.
Example inc_bin_to_nat_1 : bin_to_nat (increment zero) = 1.
Proof. reflexivity. Qed.
Example inc_bin_to_nat_2 : bin_to_nat (increment (twice zero)) = 1.
Proof. reflexivity. Qed.
Example inc_bin_to_nat_3 : bin_to_nat (increment (twicePlus1 zero)) = 2.
Proof. reflexivity. Qed.
Example inc_bin_to_nat_4 : bin_to_nat (increment (twice (twicePlus1 zero))) = 3.
Proof. reflexivity. Qed.
Example inc_bin_to_nat_5 :
bin_to_nat (increment (twice (twice (twicePlus1 zero)))) = 5.
Proof. reflexivity. Qed.
(* Exercise: ** optional decreasing *)
(* Write a simple fixpoint function on numbers that does terminate on all inputs
but Coq won't accept because a term isn't decreasing
*)
(*
Fixpoint not_zero (n:nat) : nat :=
match n with
| O => not_zero (S n) (* increases *)
| S n' => S n' (* returns same thing *)
end.
*)