-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlab3_nat.v
349 lines (288 loc) · 7.3 KB
/
lab3_nat.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
(**************************************************************)
(* Copyright Dominique Larchey-Wendling [*] *)
(* *)
(* [*] Affiliation LORIA -- CNRS *)
(**************************************************************)
(* This file is distributed under the terms of the *)
(* CeCILL v2 FREE SOFTWARE LICENSE AGREEMENT *)
(**************************************************************)
Require Setoid.
Section plus_minus_mult.
Print nat.
Check 2.
Reserved Notation "a ⊕ b" (at level 50, left associativity).
Reserved Notation "a ⊖ b" (at level 50, left associativity).
Reserved Notation "a ⊗ b" (at level 40, left associativity).
(* We redefine plus as myplus denote with ⊕ to
avoid the conflicts with the Init module
that contains parts of the Arith library
Notice that we import the inductive definition of nat
*)
Print nat.
Fixpoint myplus (a b : nat) (* { struct a } *) :=
match a with
| 0 => b
| S a' => S (a' ⊕ b)
end
where "a ⊕ b" := (myplus a b).
Print myplus.
(* 0, 1, 2 is a notation of S (S ... O)
in particular, 0 is identical to O
*)
Fact plus_0_l n : 0 ⊕ n = n.
Proof.
simpl.
trivial.
Qed.
Fact plus_1_l n : 1 ⊕ n = S n.
Proof.
simpl.
trivial.
Qed.
Fact plus_0_r n : n ⊕ 0 = n.
Proof.
simpl.
induction n.
+ trivial.
+ simpl.
(* rewrite IHn. *)
f_equal.
exact IHn.
Qed.
Fact plus_a_Sb a b : a ⊕ S b = S (a ⊕ b).
Proof. (* induction a; simpl; f_equal; trivial. *)
induction a as [ | a' IHa' ].
+ simpl. trivial.
+ simpl.
f_equal.
assumption.
Qed.
Hint Resolve plus_0_r plus_a_Sb : core.
Fact plus_comm a b : a ⊕ b = b ⊕ a.
Proof.
induction a as [ | a IHa ].
+ trivial. (* using Hint plus_0_r *)
+ simpl.
rewrite IHa.
symmetry.
trivial.
Qed.
Fact plus_assoc a b c : (a ⊕ b) ⊕ c = a ⊕ (b ⊕ c).
Proof. (* induction a; simpl; f_equal; trivial. *)
induction a as [ | a IHa ].
+ simpl. trivial.
+ simpl.
f_equal.
trivial.
Qed.
Fixpoint myminus' (a b : nat) { struct b } :=
match b with
| 0 => a
| S b' =>
match a with
| 0 => 0
| S a' => myminus' a' b'
end
end.
Fixpoint myminus (a b : nat) { struct a } :=
match a, b with
| 0, _ => 0
| S a', 0 => S a'
| S a', S b' => a' ⊖ b'
end
where "a ⊖ b" := (myminus a b).
Print myminus.
Fact minus'_0 a : myminus' a 0 = a.
Proof. trivial. Qed.
Fact minus_0 a : a ⊖ 0 = a.
Proof. (* destruct a; simpl; trivial. *)
simpl.
destruct a as [ | a' ].
+ simpl. trivial.
+ simpl. trivial.
Qed.
Hint Resolve minus_0 : core.
Fact plus_minus a b : (a ⊕ b) ⊖ a = b.
Proof.
induction a as [ | a IHa ].
+ simpl. trivial.
+ simpl. trivial.
Qed.
Fact minus_diag a : a ⊖ a = 0.
Proof.
rewrite <- (plus_0_r a) at 1.
apply plus_minus.
Qed.
Hint Resolve minus_diag : core.
(* a ⊖ b ⊕ b <> a *)
Eval compute in 1 ⊖ 3 ⊕ 3.
Fact plus_cancel_l a b c : a ⊕ b = a ⊕ c -> b = c.
Proof.
intros E.
rewrite <- (plus_minus a c).
rewrite <- E.
rewrite plus_minus.
trivial.
Qed.
Fact plus_cancel_r a b c : a ⊕ c = b ⊕ c -> a = b.
Proof.
rewrite !(plus_comm _ c).
apply plus_cancel_l.
Qed.
Fact discriminate n : S n = O -> False.
Proof.
(* discriminate. *)
intros H.
set (f n := match n with 0 => False | S _ => True end).
change (f 0).
rewrite <- H.
simpl.
trivial.
Qed.
Fact plus_eq_0 a b : a ⊕ b = 0 <-> a = 0 /\ b = 0.
Proof.
split.
+ destruct a.
* simpl.
intros ->; auto.
* simpl.
intros C.
exfalso.
discriminate.
+ intros (-> & ->).
trivial.
Qed.
Fact minus_plus_assoc a b c : a ⊖ b ⊖ c = a ⊖ (b ⊕ c).
Proof.
(* induction a as [ | a IHa ].
+ simpl; trivial.
+ simpl.
destruct b; simpl.
* trivial.
*
destruct c; auto. FAIL *)
(* revert b; induction a; simpl; trivial; intros []; simpl; trivial. *)
revert b.
induction a as [ | a IHa ].
+ intros b.
simpl; trivial.
+ intros [ | b ].
* simpl.
trivial.
* simpl.
apply IHa.
Qed.
Fact minus_eq a b : a = b <-> (a ⊖ b = 0 /\ b ⊖ a = 0).
Proof.
split.
+ (* intros E; split; rewrite E, minus_diag; reflexivity. *)
(* intros E; rewrite -> E; clear E. *)
intros ->; auto.
+ intros [ H1 H2 ].
revert a b H1 H2.
induction a.
* simpl.
intros b.
rewrite minus_0.
auto.
* simpl.
intros [ | b ].
- trivial.
- simpl.
intros.
f_equal.
apply IHa; trivial.
Qed.
Fixpoint mymult a b :=
match a with
| 0 => 0
| S a => b ⊕ a ⊗ b
end
where "a ⊗ b" := (mymult a b).
Fact mult_0_l b : 0 ⊗ b = 0.
Proof.
reflexivity.
Qed.
Fact mult_0_r a : a ⊗ 0 = 0.
Proof.
induction a as [ | a IHa ].
+ trivial.
+ unfold mymult; fold mymult.
simpl; trivial.
Qed.
Hint Resolve plus_comm mult_0_r : core.
Fact mult_a_Sb a b : a ⊗ S b = a ⊕ a ⊗ b.
Proof.
induction a as [ | a IHa ].
+ simpl; trivial.
+ simpl.
f_equal.
rewrite IHa.
(* generalize (a ⊗ b); intros c. *)
Check plus_assoc.
rewrite <- !plus_assoc.
f_equal.
apply plus_comm.
Qed.
Fact mult_comm a b : a ⊗ b = b ⊗ a.
Proof.
induction a as [ | a IHa ].
+ simpl; auto.
+ simpl.
rewrite mult_a_Sb.
f_equal; trivial.
Qed.
Fact plus_mult_distr_l a b c : (a ⊕ b) ⊗ c = a ⊗ c ⊕ b ⊗ c.
Proof.
induction a as [ | a IHa ]; simpl; trivial.
rewrite IHa, plus_assoc; trivial.
Qed.
Hint Resolve plus_mult_distr_l : core.
Fact plus_mult_distr_r a b c : c ⊗ (a ⊕ b) = c ⊗ a ⊕ c ⊗ b.
Proof.
Admitted.
Fact mult_assoc a b c : a ⊗ b ⊗ c = a ⊗ (b ⊗ c).
Proof.
induction a as [ | a IHa ]; simpl; trivial.
Admitted.
Fact mult_1_l a : 1 ⊗ a = a.
Proof.
Admitted.
Hint Resolve mult_1_l : core.
Fact mult_1_r a : a ⊗ 1 = a.
Proof.
Admitted.
Hint Resolve mult_1_r : core.
Fact mult_minus a b c : a ⊗ (b ⊖ c) = a ⊗ b ⊖ a ⊗ c.
Proof.
rewrite !(mult_comm a).
revert c; induction b as [ | b IHb ]; intros c.
Admitted.
Fact mult_eq_0 a b : a ⊗ b = 0 <-> a = 0 \/ b = 0.
Proof.
Admitted.
Fact mult_cancel_0 a b c : a ⊗ b = a ⊗ c -> a = 0 \/ b = c.
Proof.
intros E.
rewrite minus_eq in E.
rewrite <- !mult_minus in E.
rewrite !mult_eq_0 in E.
rewrite (minus_eq b c).
destruct E as [ [] [] ]; auto.
Qed.
Fact mult_cancel_l a b c : S a ⊗ b = S a ⊗ c -> b = c.
Proof.
intros E.
apply mult_cancel_0 in E.
destruct E; trivial.
discriminate.
Qed.
Fact mult_cancel_r a b c : a ⊗ S c = b ⊗ S c -> a = b.
Proof.
rewrite !(mult_comm _ (S _)).
apply mult_cancel_l.
Qed.
End plus_minus_mult.
Require Import Arith Lia Ring Omega. (* In day to day practice with nat, Z *)
Fact test (a b c : nat) : a <= b -> a+b <= b*3.
Proof. lia. Qed.