-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathIndPrinciples.v
More file actions
363 lines (298 loc) · 8.52 KB
/
IndPrinciples.v
File metadata and controls
363 lines (298 loc) · 8.52 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
(** * IndPrinciples: Induction Principles *)
Set Warnings "-notation-overridden".
From LF Require Export ProofObjects.
Check nat_ind :
forall P : nat -> Prop,
P 0 ->
(forall n : nat, P n -> P (S n)) ->
forall n : nat, P n.
Theorem mul_0_r' : forall n:nat,
n * 0 = 0.
Proof.
apply nat_ind.
- (* n = O *) reflexivity.
- (* n = S n' *) simpl. intros n' IHn'. rewrite -> IHn'.
reflexivity.
Qed.
Theorem plus_one_r' : forall n:nat,
n + 1 = S n.
Proof.
apply nat_ind.
- reflexivity.
- intros n' IHn'. simpl. rewrite IHn'. reflexivity.
Qed.
Inductive time : Type :=
| day
| night.
Check time_ind.
Inductive rgb : Type :=
| red
| green
| blue.
Check rgb_ind.
Inductive natlist : Type :=
| nnil
| ncons (n : nat) (l : natlist).
Check natlist_ind :
forall P : natlist -> Prop,
P nnil ->
(forall (n : nat) (l : natlist),
P l -> P (ncons n l)) ->
forall l : natlist, P l.
Inductive natlist' : Type :=
| nnil'
| nsnoc (l : natlist') (n : nat).
Check natlist'_ind :
forall P : natlist' -> Prop,
P nnil' ->
(forall l : natlist', P l -> forall n : nat, P (nsnoc l n)) ->
forall n : natlist', P n.
Inductive booltree : Type :=
| bt_empty
| bt_leaf (b : bool)
| bt_branch (b : bool) (t1 t2 : booltree).
Check booltree_ind :
forall P : booltree -> Prop,
P bt_empty ->
(forall b : bool, P (bt_leaf b)) ->
(forall (b : bool) (t1 : booltree), P t1 -> forall t2 : booltree, P t2 -> P (bt_branch b t1 t2)) -> forall b : booltree, P b.
Definition booltree_property_type : Type := booltree -> Prop.
Definition base_case (P : booltree_property_type) : Prop
:= P bt_empty.
Definition leaf_case (P : booltree_property_type) : Prop
:= forall b: bool, P (bt_leaf b).
Definition branch_case (P : booltree_property_type) : Prop
:= forall (b:bool)(t1:booltree), P t1 -> forall (t2:booltree), P t2 -> P (bt_branch b t1 t2).
Definition booltree_ind_type :=
forall (P : booltree_property_type),
base_case P ->
leaf_case P ->
branch_case P ->
forall (b : booltree), P b.
Theorem booltree_ind_type_correct : booltree_ind_type.
Proof.
exact booltree_ind.
Qed.
Inductive Toy : Type :=
| con1 (b : bool)
| con2 (n:nat)(t : Toy)
.
Theorem Toy_correct : exists f g,
forall P : Toy -> Prop,
(forall b : bool, P (f b)) ->
(forall (n : nat) (t : Toy), P t -> P (g n t)) ->
forall t : Toy, P t.
Proof.
exists con1.
exists con2.
exact Toy_ind.
Qed.
Inductive tree (X:Type) : Type :=
| leaf (x : X)
| node (t1 t2 : tree X).
Check tree_ind.
Check tree_ind :
forall (X : Type) (P : tree X -> Prop),
(forall x : X, P (leaf X x)) ->
(forall t1 : tree X, P t1 -> forall t2 : tree X, P t2 -> P(node X t1 t2)) ->
forall t:tree X, P t .
Inductive mytype (X : Type) : Type :=
| constr1 (x : X)
| constr2 (n : nat)
| constr3 ( m : mytype X)(n:nat)
.
Check mytype_ind.
Inductive foo (X Y : Type) : Type :=
| bar (x : X)
| baz (y : Y)
| quux (f1 : nat -> foo X Y).
Check foo_ind.
Inductive foo' (X:Type) : Type :=
| C1 (l : list X) (f : foo' X)
| C2.
Definition P_m0r (n:nat) : Prop :=
n * 0 = 0.
Definition P_m0r' : nat -> Prop :=
fun n => n * 0 = 0.
Theorem mul_0_r'' : forall n:nat,
P_m0r n.
Proof.
apply nat_ind.
- (* n = O *) reflexivity.
- (* n = S n' *)
(* Note the proof state at this point! *)
intros n IHn.
unfold P_m0r in IHn. unfold P_m0r. simpl. apply IHn. Qed.
Theorem add_assoc' : forall n m p : nat,
n + (m + p) = (n + m) + p.
Proof.
intros n m p.
(* ...we first introduce all 3 variables into the context,
which amounts to saying "Consider an arbitrary n, m, and
p..." *)
induction n as [| n'].
(* ...We now use the induction tactic to prove P n (that
is, n + (m + p) = (n + m) + p) for _all_ n,
and hence also for the particular n that is in the context
at the moment. *)
- (* n = O *) reflexivity.
- (* n = S n' *)
simpl. rewrite -> IHn'. reflexivity. Qed.
Theorem add_comm' : forall n m : nat,
n + m = m + n.
Proof.
induction n as [| n'].
- (* n = O *) intros m. rewrite -> add_0_r. reflexivity.
- (* n = S n' *) intros m. simpl. rewrite -> IHn'.
rewrite <- plus_n_Sm. reflexivity. Qed.
Theorem add_comm'' : forall n m : nat,
n + m = m + n.
Proof.
(* Let's do induction on [m] this time, instead of [n]... *)
induction m as [| m']. (* [n] is already introduced into the context *)
- (* m = O *) simpl. rewrite -> add_0_r. reflexivity.
- (* m = S m' *) simpl. rewrite <- IHm'.
rewrite <- plus_n_Sm. reflexivity. Qed.
Definition Passoc (n m p : nat) : Prop := n + (m + p) = (n + m) + p.
Definition Pcomm (n m : nat) : Prop := n + m = m + n.
Theorem add_assoc'' : forall n m p : nat, Passoc n m p.
Proof.
intros n m p.
induction n as [| n'].
- reflexivity.
- unfold Passoc. simpl. rewrite -> IHn'. reflexivity.
Qed.
Theorem add_comm''' : forall n m : nat, Pcomm n m.
Proof.
induction n as [| n'].
- intros m. unfold Pcomm. rewrite -> add_0_r. reflexivity.
- intros m. unfold Pcomm. simpl. rewrite -> IHn'.
rewrite <- plus_n_Sm. reflexivity.
Qed.
Print ev.
Check ev_ind :
forall P : nat -> Prop,
P 0 ->
(forall n : nat, ev n -> P n -> P (S (S n))) ->
forall n : nat, ev n -> P n.
Inductive ev' : nat -> Prop :=
| ev'_0 : ev' 0
| ev'_2 : ev' 2
| ev'_sum n m (Hn : ev' n) (Hm : ev' m) : ev' (n + m).
Theorem ev_ev' : forall n, ev n -> ev' n.
Proof.
apply ev_ind.
- (* ev_0 *)
apply ev'_0.
- (* ev_SS *)
intros m Hm IH.
apply (ev'_sum 2 m).
+ apply ev'_2.
+ apply IH.
Qed.
Inductive le1 : nat -> nat -> Prop :=
| le1_n : forall n, le1 n n
| le1_S : forall n m, (le1 n m) -> (le1 n (S m)).
Notation "m <=1 n" := (le1 m n) (at level 70).
Inductive le2 (n:nat) : nat -> Prop :=
| le2_n : le2 n n
| le2_S m (H : le2 n m) : le2 n (S m).
Notation "m <=2 n" := (le2 m n) (at level 70).
Check le1_ind :
forall P : nat -> nat -> Prop,
(forall n : nat, P n n) ->
(forall n m : nat, n <=1 m -> P n m -> P n (S m)) ->
forall n n0 : nat, n <=1 n0 -> P n n0.
Check le2_ind :
forall (n : nat) (P : nat -> Prop),
P n ->
(forall m : nat, n <=2 m -> P m -> P (S m)) ->
forall n0 : nat, n <=2 n0 -> P n0.
Check nat_ind :
forall P : nat -> Prop,
P 0 ->
(forall n : nat, P n -> P (S n)) ->
forall n : nat, P n.
Print nat_ind.
Fixpoint build_proof
(P : nat -> Prop)
(evPO : P 0)
(evPS : forall n : nat, P n -> P (S n))
(n : nat) : P n :=
match n with
| 0 => evPO
| S k => evPS k (build_proof P evPO evPS k)
end.
Definition nat_ind_tidy := build_proof.
Lemma even_ev : forall n: nat, even n = true -> ev n.
Proof.
induction n; intros.
- apply ev_0.
- destruct n.
+ simpl in H. inversion H.
+ simpl in H.
apply ev_SS.
Abort.
Definition nat_ind2 :
forall (P : nat -> Prop),
P 0 ->
P 1 ->
(forall n : nat, P n -> P (S(S n))) ->
forall n : nat , P n :=
fun P => fun P0 => fun P1 => fun PSS =>
fix f (n:nat) := match n with
0 => P0
| 1 => P1
| S (S n') => PSS n' (f n')
end.
Lemma even_ev : forall n, even n = true -> ev n.
Proof.
intros.
induction n as [ | |n'] using nat_ind2.
- apply ev_0.
- simpl in H.
inversion H.
- simpl in H.
apply ev_SS.
apply IHn'.
apply H.
Qed.
Notation "( x , y , .. , z )" := (pair .. (pair x y) .. z) : core_scope.
Inductive t_tree (X : Type) : Type :=
| t_leaf
| t_branch : (t_tree X * X * t_tree X) -> t_tree X.
Arguments t_leaf {X}.
Arguments t_branch {X}.
Check t_tree_ind.
Fixpoint reflect {X : Type} (t : t_tree X) : t_tree X :=
match t with
| t_leaf => t_leaf
| t_branch (l, v, r) => t_branch (reflect r, v, reflect l)
end.
Theorem reflect_involution : forall (X : Type) (t : t_tree X),
reflect (reflect t) = t.
Proof.
intros X t. induction t.
- reflexivity.
- destruct p as [[l v] r]. simpl. Abort.
Definition better_t_tree_ind_type : Prop :=
forall (X : Type) (P : t_tree X -> Prop),
P (t_leaf) ->
(forall v : X, forall l : t_tree X, P l ->
forall r : t_tree X, P r ->
P (t_branch (l, v, r))) ->
forall (t : t_tree X), P t.
Definition better_t_tree_ind : better_t_tree_ind_type
:= fun X P Pleaf Pbranch => fix f t :=
match t with
| t_leaf => Pleaf
| t_branch (l, v, r) => Pbranch v l (f l) r (f r)
end.
Theorem reflect_involution : forall (X : Type) (t : t_tree X),
reflect (reflect t) = t.
Proof.
intros X.
apply better_t_tree_ind.
- reflexivity.
- intros v l IHl r IHr. simpl. rewrite IHl. rewrite IHr. reflexivity.
Qed.