-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathNaturals.v
463 lines (431 loc) · 13.7 KB
/
Naturals.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
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
Require Import Basics.
Require Import Reduction.
Fixpoint encode_aux (n: nat): term :=
match n with
| 0 => #0
| S n' => $ (#1) (encode_aux n')
end
.
(* Module test_encode.
Compute encode 0.
Compute encode 1.
Compute encode 2.
(* etc. *)
End test_encode. *)
(* Church encoding of a given natural *)
Definition encode (n: nat): term :=
\\(encode_aux n)
.
Definition succ: term :=
\\\ $ (#1) ($ ($ (#2) (#1)) (#0))
.
Require Import Coq.Arith.EqNat.
Compute beta $ (\ #23) (#0) PRoot.
Compute let n:=2 in (increase_free_variables (encode_aux n) 2 2, encode_aux n).
(* Some lemmas for the main thms *)
Lemma aux (n: nat): increase_free_variables (encode_aux n) 2 2 = encode_aux n.
Proof.
induction n.
- now simpl.
- unfold encode_aux; fold encode_aux.
simpl.
now rewrite IHn.
Qed.
Lemma aux' (n: nat): increase_free_variables (encode_aux n) 1 2 = encode_aux n.
Proof.
induction n.
- now simpl.
- unfold encode_aux; fold encode_aux.
simpl.
now rewrite IHn.
Qed.
Lemma aux'' (n: nat): increase_free_variables (encode_aux n) 0 2 = encode_aux n.
Proof.
induction n.
- now simpl.
- unfold encode_aux; fold encode_aux.
simpl.
now rewrite IHn.
Qed.
Require Import Coq.Classes.Morphisms. (* to use f_equiv if necessary *)
Compute (replace (encode_aux 0) (# 1) 1).
Compute (replace (encode_aux 1) (# 1) 1).
Compute (replace (encode_aux 2) (# 1) 1).
Compute (replace (encode_aux 3) (# 1) 1).
Lemma lemma_replace (n: nat): replace (replace (encode_aux n) (# 1) 1) (# 0) 0 = encode_aux n.
Proof.
induction n.
- easy.
- unfold encode_aux; fold encode_aux.
simpl.
f_equal.
apply IHn.
Qed.
Lemma succ_encode (n: nat): $ succ (encode n) == encode (S n).
Proof.
unfold succ.
Compute beta ($ (\ (\ (\ $ (# 1) $ $ (# 2) (# 1) (# 0)))) (encode n)) (PRoot).
transitivity (\ (\ $ (# 1) $ $ (encode n) (# 1) (# 0))).
- apply CStep.
exists PRoot.
simpl.
unfold encode.
now rewrite (aux n).
- unfold encode.
unfold encode_aux; fold encode_aux.
destruct n.
+ common_reduct.
+ unfold encode_aux; fold encode_aux.
transitivity (\\ $ (#1) $ (\ $ (#2) (replace (encode_aux n) (#1) 1)) (#0)).
* apply CStep.
now exists (PAbs (PAbs (PRight (PLeft PRoot)))).
* transitivity (\ (\ $ (# 1) $ (# 1) (replace (replace (encode_aux n) (# 1) 1) (# 0) 0))).
-- apply CStep.
now exists (PAbs (PAbs (PRight PRoot))).
-- now rewrite lemma_replace.
Qed.
(* Sx = Sy -> x = y *)
Lemma encode_inj: forall (n m: nat), encode n == encode m -> n = m.
Proof.
intros.
unfold encode in H.
induction n, m.
- easy.
- unfold encode in H.
unfold encode_aux in H; fold encode_aux in H.
(* TODO should be easy, haven't had time *)
Admitted.
Lemma succ_inj: forall (x y: nat), ($ succ (encode x)) == ($ succ (encode y)) -> (encode x) == (encode y).
Proof.
intros.
rewrite succ_encode in H.
rewrite succ_encode in H.
apply encode_inj in H.
injection H.
intro.
rewrite H0.
reflexivity.
Qed.
(* x + 0 = 0 *)
Lemma succ_zero: forall (x: nat), ~(($ succ (encode x)) == encode 0).
Proof.
intro.
rewrite succ_encode.
intro.
now apply encode_inj in H.
Qed.
Definition plus: term := \\ $ $ (#1) succ (#0).
(* More useful lemmas *)
Lemma repl_lemma' (n: nat) (p: term): replace (encode_aux n) p 3 = encode_aux n.
Proof.
induction n.
- easy.
- unfold encode_aux; fold encode_aux.
simpl.
repeat f_equal.
unfold encode_aux in IHn at 2.
apply IHn.
Qed.
Lemma repl_lemma (n: nat) (p: term): replace (encode_aux n) (p) 2 = encode_aux n.
Proof.
induction n.
- easy.
- unfold encode_aux; fold encode_aux.
simpl.
repeat f_equal.
unfold encode_aux in IHn at 2.
apply IHn.
Qed.
(* x + 0 = x *)
Lemma zero_neutral: forall (n: nat), $ $ plus (encode n) (encode 0) == encode n.
Proof.
intros.
induction n.
- common_reduct.
- rewrite <- succ_encode.
rewrite <- IHn at 2.
unfold plus.
transitivity ($ (\ $ ($ ($ succ (encode n)) succ) (# 0)) (encode 0));
[ apply CStep;
exists (PLeft (PRoot));
simpl;
repeat f_equiv;
unfold encode;
repeat f_equal;
apply aux' | ].
transitivity ($ $ $ succ (encode n) succ (encode 0));
[ apply CStep;
exists PRoot;
simpl;
repeat f_equal;
unfold encode at 2;
repeat f_equal;
apply repl_lemma | ].
symmetry.
transitivity ($ succ $ (\ $ $ (encode n) succ (# 0)) (encode 0));
[ apply CStep;
exists (PRight (PLeft PRoot));
simpl;
repeat f_equal;
unfold encode;
repeat f_equal;
apply aux' | ].
transitivity ($ succ $ $ (encode n) succ (encode 0));
[ apply CStep;
exists (PRight PRoot);
simpl;
repeat f_equal;
unfold encode;
repeat f_equal;
apply repl_lemma | ].
unfold succ.
transitivity (\\ $ (# 1) $ $ ($ $ (encode n) (\ (\ (\ $ (# 1) $ $ (# 2) (# 1) (# 0)))) (encode 0)) (# 1) (# 0));
[ apply CStep;
exists PRoot;
simpl;
repeat f_equal;
unfold encode;
repeat f_equal;
apply aux | ].
symmetry.
transitivity ($ $ (\ (\ $ (# 1) $ $ (encode n) (# 1) (# 0))) (\ (\ (\ $ (# 1) $ $ (# 2) (# 1) (# 0)))) (encode 0));
[ apply CStep;
exists (PLeft (PLeft PRoot));
simpl;
repeat f_equal;
unfold encode;
repeat f_equal;
apply aux | ].
transitivity ($ ((\ $ ((\ (\ (\ $ (# 1) $ $ (# 2) (# 1) (# 0))))) $ $ (encode n) ((\ (\ (\ $ (# 1) $ $ (# 2) (# 1) (# 0))))) (# 0))) (encode 0));
[ apply CStep;
exists (PLeft PRoot);
simpl;
repeat f_equal;
unfold encode;
repeat f_equal;
apply repl_lemma' | ].
transitivity (($ (\ (\ (\ $ (# 1) $ $ (# 2) (# 1) (# 0)))) $ $ (encode n) (\ (\ (\ $ (# 1) $ $ (# 2) (# 1) (# 0)))) (encode 0)));
[ apply CStep;
exists PRoot;
simpl;
repeat f_equal;
unfold encode;
repeat f_equal;
apply repl_lemma | ].
transitivity (((\ (\ $ (# 1) $ $ ($ $ (encode n) (\ (\ (\ $ (# 1) $ $ (# 2) (# 1) (# 0)))) (encode 0)) (# 1) (# 0)))));
[ apply CStep;
exists PRoot;
simpl;
repeat f_equal;
unfold encode;
repeat f_equal;
apply aux | ].
reflexivity.
Qed.
(* Automate some work *)
Ltac step p rt := transitivity p;
[ apply CStep;
exists rt;
simpl;
repeat f_equal;
unfold encode;
repeat f_equal;
try apply repl_lemma;
try apply repl_lemma';
try apply aux;
try apply aux';
try apply aux'' | ].
(* x + S(y) = S(x + y) *)
Lemma plus_succ (n m: nat): $ $ plus (encode n) ($ succ (encode m)) == $ succ $ $ plus (encode n) (encode m).
Proof.
induction m.
- rewrite zero_neutral.
unfold plus.
step ($ ((\ $ $ (encode n) succ (# 0))) $ succ (encode 0)) (PLeft PRoot).
step (($ $ (encode n) succ ($ succ (encode 0)))) (PRoot).
induction n.
+ common_reduct.
+ rewrite <- succ_encode.
unfold succ at 1.
step ($ $ ((\ (\ $ (# 1) $ $ (encode n) (# 1) (# 0)))) succ $ succ (encode 0)) (PLeft (PLeft PRoot)).
step ($ ((\ $ succ $ $ (encode n) succ (# 0))) $ succ (encode 0)) (PLeft PRoot).
step (($ succ $ $ (encode n) succ $ succ (encode 0))) PRoot.
f_equiv.
apply IHn.
- rewrite <- succ_encode.
unfold plus at 1.
step ($ ((\ $ $ (encode n) succ (# 0))) $ succ $ succ (encode m)) (PLeft PRoot).
step (($ $ (encode n) succ ($ succ $ succ (encode m)))) (PRoot).
rewrite IHm.
clear IHm.
induction n.
+ unfold encode at 1.
unfold encode_aux at 1.
step ($ (\ # 0) $ succ $ succ (encode m)) (PLeft PRoot).
step ($ succ $ succ (encode m)) (PRoot).
repeat f_equiv.
unfold plus, encode at 2.
symmetry.
step ($ ((\ $ $ (\ (\ encode_aux 0)) succ (# 0))) (encode m)) (PLeft PRoot).
step ((($ $ (\ (\ encode_aux 0)) succ (encode m))) ) (PRoot).
unfold encode_aux.
step ($ ((\ # 0)) (encode m)) (PLeft PRoot).
step (encode m) (PRoot).
reflexivity.
+ rewrite <- succ_encode in *.
unfold succ at 1.
step ($ $ ((\ (\ $ (# 1) $ $ (encode n) (# 1) (# 0)))) succ $ succ $ succ (encode m)) (PLeft (PLeft PRoot)).
step ($ ((\ $ succ $ $ (encode n) succ (# 0))) $ succ $ succ (encode m)) (PLeft PRoot).
step (($ succ $ $ (encode n) succ ($ succ $ succ (encode m)))) (PRoot).
f_equiv.
transitivity ($ succ $ succ $ $ plus (encode n) (encode m)).
* apply IHn.
* f_equiv.
symmetry.
unfold plus at 1.
step ($ ((\ $ $ $ succ (encode n) succ (# 0))) (encode m)) (PLeft PRoot).
step (($ $ $ succ (encode n) succ (encode m))) PRoot.
unfold succ at 1.
step ($ $ ((\ (\ $ (# 1) $ $ ((encode n)) (# 1) (# 0)))) succ (encode m)) (PLeft (PLeft PRoot)).
step ($ ((\ $ succ $ $ (encode n) succ (# 0))) (encode m)) (PLeft PRoot).
step ($ succ $ $ (encode n) succ (encode m)) PRoot.
f_equiv.
symmetry.
unfold plus.
step ($ ((\ $ $ (encode n) succ (# 0))) (encode m)) (PLeft PRoot).
step ($ $ (encode n) succ (encode m)) PRoot.
reflexivity.
Qed.
Definition mult := \\$ $ (#1) ($ plus #0) (encode 0).
(* x * 0 = 0 *)
Lemma mult_nil (n: nat): $ $ mult (encode n) (encode 0) == encode 0.
Proof.
unfold mult.
transitivity ($ ((\ $ $ ((encode n)) $ plus (# 0) (encode 0))) (encode 0));
[ apply CStep;
exists (PLeft PRoot);
simpl;
repeat f_equal;
unfold encode;
repeat f_equal;
apply aux' | ].
transitivity ((($ $ ((encode n)) $ plus (encode 0) (encode 0))));
[ apply CStep;
exists (PRoot);
simpl;
repeat f_equal;
unfold encode;
repeat f_equal;
apply repl_lemma | ].
induction n.
- common_reduct.
- rewrite <- succ_encode.
unfold succ.
transitivity ($ $ ((\ (\ $ (# 1) $ $ ((encode n)) (# 1) (# 0)))) $ plus (encode 0) (encode 0));
[ apply CStep;
exists (PLeft (PLeft PRoot));
simpl;
repeat f_equal;
unfold encode;
repeat f_equal;
apply aux | ].
transitivity ($ ((\ $ ($ plus (encode 0)) $ $ (encode n) ($ plus (encode 0)) (# 0))) (encode 0));
[ apply CStep;
exists (PLeft (PRoot));
simpl;
repeat f_equal;
unfold encode;
repeat f_equal;
apply repl_lemma' | ].
transitivity (($ $ plus (encode 0) $ $ (encode n) $ plus (encode 0) ((encode 0))) );
[ apply CStep;
exists ((PRoot));
simpl;
repeat f_equal;
unfold encode;
repeat f_equal;
apply repl_lemma | ].
unfold plus at 1.
transitivity ($ ((\ $ $ (encode 0) succ (# 0))) $ $ (encode n) $ plus (encode 0) (encode 0));
[ apply CStep;
exists (PLeft (PRoot));
simpl;
repeat f_equal;
unfold encode;
repeat f_equal;
apply repl_lemma | ].
transitivity (($ $ (encode 0) succ ($ $ (encode n) $ plus (encode 0) (encode 0))));
[ apply CStep;
exists ((PRoot));
simpl;
repeat f_equal;
unfold encode;
repeat f_equal;
apply aux'' | ].
unfold encode at 1.
unfold encode_aux.
transitivity ($ ((\ # 0)) $ $ (encode n) $ plus (encode 0) (encode 0));
[ apply CStep;
exists (PLeft (PRoot));
simpl;
repeat f_equal;
unfold encode;
repeat f_equal | ].
transitivity ($ $ (encode n) $ plus (encode 0) (encode 0));
[ apply CStep;
exists ((PRoot));
simpl;
repeat f_equal;
unfold encode;
repeat f_equal;
apply aux'' | ].
apply IHn.
Qed.
(* x * S(y) = x * y + x *)
Lemma mult_plus (n m: nat): $ $ mult (encode n) ($ succ (encode m)) == $ $ plus $ $ mult (encode n) (encode m) (encode n).
Proof.
induction n, m.
- common_reduct.
- rewrite <- succ_encode.
unfold mult.
step ($ ((\ $ $ ((encode 0)) $ plus (# 0) (encode 0))) $ succ $ succ (encode m)) (PLeft PRoot).
step (($ $ (encode 0) $ plus ($ succ $ succ (encode m)) (encode 0))) PRoot.
unfold encode at 1.
simpl.
step ($ ((\ # 0)) (encode 0)) (PLeft PRoot).
step (encode 0) PRoot.
symmetry.
unfold plus at 1.
step ($ ((\ $ $ ($ $ (\ (\ $ $ (# 1) $ plus (# 0) (encode 0))) (encode 0) $ succ (encode m)) succ (# 0))) (encode 0)) (PLeft PRoot).
step (($ $ $ $ (\ (\ $ $ (# 1) $ plus (# 0) (encode 0))) (encode 0) $ succ (encode m) succ ((encode 0)))) PRoot.
step ($ $ $ ((\ $ $ (encode 0) $ plus (# 0) (encode 0))) $ succ (encode m) succ (encode 0)) (PLeft (PLeft (PLeft PRoot))).
step ($ $ ($ $ (encode 0) $ plus ($ succ (encode m)) (encode 0)) succ (encode 0)) (PLeft (PLeft PRoot)).
unfold encode at 1.
simpl.
step ($ $ $ ((\ # 0)) (encode 0) succ (encode 0)) (PLeft (PLeft (PLeft PRoot))).
step ($ $ (encode 0) succ (encode 0)) (PLeft (PLeft PRoot)).
unfold encode at 1.
simpl.
step ($ ((\ # 0)) (encode 0)) (PLeft PRoot).
step (encode 0) PRoot.
reflexivity.
- rewrite mult_nil.
rewrite mult_nil in IHn.
rewrite <- succ_encode.
rewrite plus_succ.
rewrite <- IHn.
unfold mult at 1.
step ($ ((\ $ $ $ succ (encode n) $ plus (# 0) (encode 0))) $ succ (encode 0)) (PLeft PRoot).
step (($ $ $ succ (encode n) $ plus ($ succ (encode 0)) (encode 0))) PRoot.
symmetry.
unfold mult at 1.
step ($ succ $ ((\ $ $ ((encode n)) $ plus (# 0) (encode 0))) $ succ (encode 0)) (PRight (PLeft PRoot)).
step ($ succ ($ $ (encode n) $ plus ($ succ (encode 0)) (encode 0))) (PRight PRoot).
symmetry.
(* unfold plus at 1.
step ($ $ $ succ (encode n) ((\ $ $ ($ succ (encode 0)) succ (# 0))) (encode 0)) (PLeft (PRight PRoot)). *)
unfold succ at 1.
step ($ $ ((\ (\ $ (# 1) $ $ ((encode n)) (# 1) (# 0)))) $ plus $ succ (encode 0) (encode 0)) (PLeft (PLeft PRoot)).
step ($ ((\ $ ($ plus $ succ (encode 0)) $ $ (encode n) ($ plus $ succ (encode 0)) (# 0))) (encode 0)) (PLeft PRoot).
step (( $ $ plus $ succ (encode 0) $ $ (encode n) $ plus $ succ (encode 0) (encode 0))) PRoot.
Admitted.