-
Notifications
You must be signed in to change notification settings - Fork 1
/
BaseLib.v
278 lines (237 loc) · 7.53 KB
/
BaseLib.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
Require Import Eqdep.
Require Export ZArith.
Require Export List.
Require Export Bool.
(* General Axioms *)
(* The first axiom (unprovable in Coq) is the one of extensionality.
It is pretty simple. If forall x, f x = g x, then we have f = g.
*)
Axiom extensionality:
forall (A B: Set) (f g: A -> B),
(forall x, f x = g x) -> f = g.
(* The second axiom is that of proof irrelevance. If we have 2 ways
to derive the same proposition, the way we derived it is irrelevant.
This is rule is pretty basic in mathematics *)
Axiom proof_irrelevance:
forall (P: Prop) (p1 p2: P), p1 = p2.
(* General tactics *)
(* Cut the goal into 2, solve the first with contradiction, the second with
omega. The cut is modus ponens in inverse. *)
Ltac omegaContradiction :=
cut False; [contradiction|omega].
Ltac decEq :=
match goal with
| [ |- _ = _ ] => f_equal
| [ |- (?X ?A <> ?X ?B) ] =>
cut (A <> B); [intro; congruence | try discriminate]
end.
Ltac caseEq name :=
generalize (refl_equal name); pattern name at -1 in |- *; case name.
(* Chain injection and substition *)
Ltac inject H :=
injection H; clear H; intros; try subst.
(* apply f to hypotheses *)
Ltac appHyps f :=
match goal with
| [ H : _ |- _] => f H
end.
(* Try to find an element in [ls] that [f] likes *)
Ltac app f ls :=
match ls with
| (?LS, ?X) => f X || app f LS || fail 1
| _ => f ls
end.
(* Guard that x is in ls *)
Ltac inList x ls :=
match ls with
| x => idtac
| (_, x) => idtac
| (?LS, _) => inList x LS
end.
(* Hypothesis simplifier *)
Ltac simplHyp invOne :=
let invert H F :=
inList F invOne; (inversion H; fail)
|| (inversion H; [idtac]; clear H; try subst) in
match goal with
| [ H : ex _ |- _ ] => destruct H
| [ H : ?F ?X = ?F ?Y |- _ ] => injection H;
match goal with
| [ |- F X = F Y -> _ ] => fail 1
| [ |- _ = _ -> _ ] => try clear H; intros; try subst
end
| [ H : ?F _ _ = ?F _ _ |- _ ] => injection H;
match goal with
| [ |- _ = _ -> _ = _ -> _ ] => try clear H; intros; try subst
end
| [ H : ?F _ |- _ ] => invert H F
| [ H : ?F _ _ |- _ ] => invert H F
| [ H : ?F _ _ _ |- _ ] => invert H F
| [ H : ?F _ _ _ _ |- _ ] => invert H F
| [ H : ?F _ _ _ _ _ |- _ ] => invert H F
| [ H : existT _ ?T _ = existT _ ?T _ |- _ ] =>
generalize (inj_pair2 _ _ _ _ _ H); clear H
| [ H : existT _ _ _ = existT _ _ _ |- _ ] => inversion H; clear H
| [ H : Some _ = Some _ |- _ ] => injection H; clear H
end.
Ltac rewriteHyp :=
match goal with
| [ H : _ |- _ ] => rewrite H; auto; [idtac]
end.
Ltac rewriterP := repeat (rewriteHyp; autorewrite with mortar in *).
Ltac rewriter := autorewrite with mortar in *; rewriterP.
Hint Rewrite app_ass : mortar.
Definition done (T : Type) (x : T) := True.
Ltac inster e trace :=
match type of e with
| forall x : _, _ =>
match goal with
| [ H : _ |- _ ] =>
inster (e H) (trace, H)
| _ => fail 2
end
| _ =>
match trace with
| (_, _) =>
match goal with
| [ H : done (trace, _) |- _ ] => fail 1
| _ =>
let T := type of e in
match type of T with
| Prop =>
generalize e; intro;
assert (done (trace, tt)); [constructor | idtac]
end
end
end
end.
Ltac un_done :=
repeat match goal with
| [ H : done _ |- _ ] => clear H
end.
Ltac grind' lemmas invOne :=
let sintuition := simpl in *; intuition; try subst;
repeat (simplHyp invOne; intuition; try subst); try congruence in
let rewriter := autorewrite with mortar in *;
repeat (match goal with
| [ H : _ |- _ ] => (rewrite H; [])
|| (rewrite H; [ | solve [grind' lemmas invOne] ])
|| (rewrite H; [ | solve [grind' lemmas invOne] |
solve [grind' lemmas invOne ] ])
end; autorewrite with mortar in *)
in (sintuition; rewriter;
match lemmas with
| false => idtac
| _ => repeat ((app ltac:(fun L => inster L L) lemmas
|| appHyps ltac:(fun L => inster L L));
repeat (simplHyp invOne; intuition)); un_done
end; sintuition; rewriter; sintuition; try omega;
try (elimtype False; omega)).
Ltac grind := grind' false fail.
(* Definitions and theorems over the type [Z] *)
Definition zeq: forall (x y: Z), {x = y} + {x <> y} := Z_eq_dec.
Lemma zeq_true:
forall (A: Set) (x: Z) (a b: A), (if zeq x x then a else b) = a.
intros. case (zeq x x); intros.
trivial. elim n. trivial.
Qed.
Lemma zeq_false: forall (A: Set) (x y: Z) (a b: A),
x <> y -> (if zeq x y then a else b) = b.
intros. case (zeq x y); intros.
destruct H. assumption.
reflexivity.
Qed.
Open Scope Z_scope.
Definition zlt: forall (x y: Z), {x < y} + {x >= y} := Z_lt_ge_dec.
Lemma zlt_true: forall (A: Set) (x y: Z) (a b: A),
x < y -> (if zlt x y then a else b) = a.
intros. case (zlt x y); intros.
reflexivity.
omegaContradiction.
Qed.
Lemma zlt_false: forall (A: Set) (x y: Z) (a b: A),
x >= y -> (if zlt x y then a else b) = b.
intros. case (zlt x y); intros.
omegaContradiction.
reflexivity.
Qed.
Definition zle: forall (x y: Z), {x <= y} + {x > y} := Z_le_gt_dec.
Lemma zle_true: forall (A: Set) (x y: Z) (a b: A),
x <= y -> (if zle x y then a else b) = a.
intros. case (zle x y); intros.
reflexivity.
omegaContradiction.
Qed.
Lemma zle_false: forall (A: Set) (x y: Z) (a b: A),
x > y -> (if zle x y then a else b) = b.
intros. case (zle x y); intros.
omegaContradiction.
reflexivity.
Qed.
(* Properties about powers of 2 *)
Lemma two_power_nat_O : two_power_nat 0 = 1.
reflexivity.
Qed.
Lemma two_power_nat_pos : forall (n : nat),
two_power_nat n > 0.
induction n. rewrite two_power_nat_O. omega.
rewrite two_power_nat_S. omega.
Qed.
(* Proofs about functions *)
Lemma equal_f : forall (A B : Set) (f g : A -> B) (x : A), f = g -> f x = g x.
Proof.
grind.
Qed.
Lemma equal_f_2 : forall (A B C : Set) (f g : A -> B -> C)
(x : A) (y : B),
f = g -> f x y = g x y.
Proof.
grind.
Qed.
Lemma equal_f_3 : forall (A B C D : Set) (f g : A -> B -> C -> D)
(x : A) (y : B) (z : C),
f = g -> f x y z = g x y z.
Proof.
grind.
Qed.
Lemma Zdiv_small:
forall x y, 0 <= x < y -> x / y = 0.
Proof.
intros. assert (y > 0). omega.
assert (forall a b,
0 <= a < y ->
0 <= y * b + a < y ->
b = 0).
intros.
assert (b = 0 \/ b > 0 \/ (-b) > 0). omega.
elim H3; intro. auto.
elim H4; intro. auto.
assert (y * b >= y * 1). apply Zmult_ge_compat_l. omega. omega.
omegaContradiction.
assert (y * (-b) >= y * 1). apply Zmult_ge_compat_l. omega. omega.
rewrite <- Zopp_mult_distr_r in H6. omegaContradiction.
apply H1 with (x mod y).
apply Z_mod_lt. auto.
rewrite <- Z_div_mod_eq. auto. auto.
Qed.
Lemma Zmod_small:
forall x y, 0 <= x < y -> x mod y = x.
Proof.
intros. assert (y > 0). omega.
generalize (Z_div_mod_eq x y H0).
rewrite (Zdiv_small x y H). omega.
Qed.
Lemma Zmod_unique:
forall x y a b,
x = a * y + b -> 0 <= b < y -> x mod y = b.
Proof.
intros. subst x. rewrite Zplus_comm.
rewrite Z_mod_plus. apply Zmod_small. trivial. omega.
Qed.
Lemma Zdiv_unique:
forall x y a b,
x = a * y + b -> 0 <= b < y -> x / y = a.
Proof.
intros. subst x. rewrite Zplus_comm.
rewrite Z_div_plus. rewrite (Zdiv_small b y H0). omega. omega.
Qed.