-
Notifications
You must be signed in to change notification settings - Fork 3
/
coq.v
1999 lines (1654 loc) · 111 KB
/
coq.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
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
(****************************************************************************)
(* Coq theory for encoding HOL-Light proofs. *)
(****************************************************************************)
Lemma ext_fun {A B} {f g : A -> B} : f = g -> forall x, f x = g x.
Proof. intros fg x. rewrite fg. reflexivity. Qed.
(****************************************************************************)
(* Type of non-empty types, used to interpret HOL-Light types types. *)
(****************************************************************************)
Record Type' := { type :> Type; el : type }.
Definition bool' := {| type := bool; el := true |}.
Canonical bool'.
Definition Prop' : Type' := {| type := Prop; el := True |}.
Canonical Prop'.
Definition arr a (b : Type') := {| type := a -> b; el := fun _ => el b |}.
Canonical arr.
(****************************************************************************)
(* Curryfied versions of some Coq connectives. *)
(****************************************************************************)
Definition imp (p q : Prop) : Prop := p -> q.
Definition ex1 : forall {A : Type'}, (A -> Prop) -> Prop := fun A P => exists! x, P x.
(****************************************************************************)
(* Proof of some HOL-Light rules. *)
(****************************************************************************)
Lemma MK_COMB {a b : Type} {s t : a -> b} {u v : a} (h1 : s = t) (h2 : u = v)
: (s u) = (t v).
Proof. rewrite h1, h2. reflexivity. Qed.
Lemma EQ_MP {p q : Prop} (e : p = q) (h : p) : q.
Proof. rewrite <- e. apply h. Qed.
(****************************************************************************)
(* Proof of some natural deduction rules. *)
(****************************************************************************)
Lemma or_intro1 {p:Prop} (h : p) (q:Prop) : p \/ q.
Proof. exact (@or_introl p q h). Qed.
Lemma or_intro2 (p:Prop) {q:Prop} (h : q) : p \/ q.
Proof. exact (@or_intror p q h). Qed.
Lemma or_elim {p q : Prop} (h : p \/ q) {r : Prop} (h1: p -> r) (h2: q -> r) : r.
Proof. exact (@or_ind p q r h1 h2 h). Qed.
Lemma ex_elim {a} {p : a -> Prop}
(h1 : exists x, p x) {r : Prop} (h2 : forall x : a, (p x) -> r) : r.
Proof. exact (@ex_ind a p r h2 h1). Qed.
(****************************************************************************)
(* Coq axioms necessary to handle HOL-Light proofs. *)
(****************************************************************************)
Require Import Coq.Logic.ClassicalEpsilon.
Definition ε : forall {A : Type'}, (type A -> Prop) -> type A :=
fun A P => epsilon (inhabits (el A)) P.
Lemma ε_spec {A : Type'} {P : type A -> Prop} : (exists x, P x) -> P (ε P).
Proof. intro h. unfold ε. apply epsilon_spec. exact h. Qed.
Axiom fun_ext : forall {A B : Type} {f g : A -> B}, (forall x, (f x) = (g x)) -> f = g.
Axiom prop_ext : forall {P Q : Prop}, (P -> Q) -> (Q -> P) -> P = Q.
Require Import ClassicalFacts.
Lemma prop_degen : forall P, P = True \/ P = False.
Proof.
apply prop_ext_em_degen. unfold prop_extensionality. intros A B [AB BA].
apply prop_ext. exact AB. exact BA.
intro P. apply classic.
Qed.
Require Import PropExtensionalityFacts.
Lemma is_True P : (P = True) = P.
Proof.
apply prop_ext.
intro e. rewrite e. exact I.
apply PropExt_imp_ProvPropExt.
intros a b [ab ba]. apply prop_ext. apply ab. apply ba.
Qed.
Lemma is_False P : (P = False) = ~ P.
Proof.
apply prop_ext; intro h. rewrite h. intro i. exact i.
apply prop_ext; intro i. apply h. apply i. apply False_rec. exact i.
Qed.
Lemma refl_is_True {A} (x:A) : (x = x) = True.
Proof. rewrite is_True. reflexivity. Qed.
Lemma sym {A} (x y : A) : (x = y) = (y = x).
Proof. apply prop_ext; intro e; symmetry; exact e. Qed.
Lemma True_and_True : (True /\ True) = True.
Proof. rewrite is_True. tauto. Qed.
Lemma not_forall_eq A (P : A -> Prop) : (~ forall x, P x) = exists x, ~ (P x).
Proof.
apply prop_ext; intro h. apply not_all_ex_not. exact h.
apply ex_not_not_all. exact h.
Qed.
Lemma not_exists_eq A (P : A -> Prop) : (~ exists x, P x) = forall x, ~ (P x).
Proof.
apply prop_ext; intro h. apply not_ex_all_not. exact h.
apply all_not_not_ex. exact h.
Qed.
(****************************************************************************)
(* Proof of HOL-Light axioms. *)
(****************************************************************************)
Lemma axiom_0 : forall {A B : Type'}, forall t : A -> B, (fun x : A => t x) = t.
Proof. reflexivity. Qed.
Lemma axiom_1 : forall {A : Type'}, forall P : A -> Prop, forall x : A, (P x) -> P (@ε A P).
Proof.
intros A P x h. apply (epsilon_spec (inhabits (el A)) P (ex_intro P x h)).
Qed.
(****************************************************************************)
(* Proof of mappings from HOL-Light connectives to Coq connectives. *)
(****************************************************************************)
Lemma ex1_def : forall {A : Type'}, (@ex1 A) = (fun P : A -> Prop => (ex P) /\ (forall x : A, forall y : A, ((P x) /\ (P y)) -> x = y)).
Proof.
intro A. unfold ex1. apply fun_ext; intro P. unfold unique. apply prop_ext.
intros [x [px u]]. split. apply (ex_intro P x px). intros a b [ha hb].
transitivity x. symmetry. apply u. exact ha. apply u. exact hb.
intros [[x px] u]. apply (ex_intro _ x). split. exact px. intros y py.
apply u. split. exact px. exact py.
Qed.
Lemma F_def : False = (forall p : Prop, p).
Proof.
apply prop_ext. intros b p. apply (False_rec p b). intro h. exact (h False).
Qed.
Lemma not_def : not = (fun p : Prop => p -> False).
Proof. reflexivity. Qed.
Lemma or_def : or = (fun p : Prop => fun q : Prop => forall r : Prop, (p -> r) -> (q -> r) -> r).
Proof.
apply fun_ext; intro p; apply fun_ext; intro q. apply prop_ext.
intros pq r pr qr. destruct pq. apply (pr H). apply (qr H).
intro h. apply h. intro hp. left. exact hp. intro hq. right. exact hq.
Qed.
Lemma ex_def : forall {A : Type'}, (@ex A) = (fun P : A -> Prop => forall q : Prop, (forall x : A, (P x) -> q) -> q).
Proof.
intro A. apply fun_ext; intro p. apply prop_ext.
intros [x px] q pq. eapply pq. apply px.
intro h. apply h. intros x px. apply (ex_intro p x px).
Qed.
Lemma all_def : forall {A : Type'}, (@all A) = (fun P : A -> Prop => P = (fun x : A => True)).
Proof.
intro A. apply fun_ext; intro p. apply prop_ext.
intro h. apply fun_ext; intro x. apply prop_ext.
intros _. exact I. intros _. exact (h x).
intros e x. rewrite e. exact I.
Qed.
Lemma imp_def : imp = (fun p : Prop => fun q : Prop => (p /\ q) = p).
Proof.
apply fun_ext; intro p. apply fun_ext; intro q. apply prop_ext.
intro pq. apply prop_ext. intros [hp _]. exact hp. intro hp.
split. exact hp. apply pq. exact hp.
intro e. rewrite <- e. intros [_ hq]. exact hq.
Qed.
Lemma and_def : and = (fun p : Prop => fun q : Prop => (fun f : Prop -> Prop -> Prop => f p q) = (fun f : Prop -> Prop -> Prop => f True True)).
Proof.
apply fun_ext; intro p. apply fun_ext; intro q. apply prop_ext.
intros [hp hq]. apply fun_ext; intro f.
case (prop_degen p); intro e; subst p.
case (prop_degen q); intro e; subst q.
reflexivity.
exfalso. exact hq.
exfalso. exact hp.
intro e. generalize (ext_fun e); clear e; intro e. split.
rewrite (e (fun p _ => p)). exact I.
rewrite (e (fun _ q => q)). exact I.
Qed.
Lemma T_def : True = ((fun p : Prop => p) = (fun p : Prop => p)).
Proof. apply prop_ext. reflexivity. intros _; exact I. Qed.
(****************************************************************************)
(* Mapping of HOL-Light type 1 to Coq type unit. *)
(****************************************************************************)
Definition unit' := {| type := unit; el := tt |}.
Canonical unit'.
Definition one_ABS : Prop -> unit := fun _ => tt.
Definition one_REP : unit -> Prop := fun _ => True.
Lemma axiom_2 : forall (a : unit), (one_ABS (one_REP a)) = a.
Proof. intro a. destruct a. reflexivity. Qed.
Lemma axiom_3 : forall (r : Prop), ((fun b : Prop => b) r) = ((one_REP (one_ABS r)) = r).
Proof. intro r. compute. rewrite (sym True r), is_True. reflexivity. Qed.
Lemma one_def : tt = ε one_REP.
Proof. generalize (ε one_REP). destruct t. reflexivity. Qed.
(****************************************************************************)
(* Mapping of HOL-Light type prod to Coq type prod. *)
(****************************************************************************)
Definition prod' (a b : Type') := {| type := a * b; el := pair (el a) (el b) |}.
Canonical prod'.
Definition mk_pair {A B : Type'} :=
fun x : A => fun y : B => fun a : A => fun b : B => (a = x) /\ (b = y).
Lemma mk_pair_inj (A B : Type') (x x' : A) (y y' : B) :
mk_pair x y = mk_pair x' y' -> x = x' /\ y = y'.
Proof.
intro e; generalize (ext_fun e); clear e; intro e.
generalize (ext_fun (e x)); clear e; intro e.
generalize (e y); clear e. unfold mk_pair.
rewrite refl_is_True, refl_is_True, True_and_True, sym, is_True.
intro h; exact h.
Qed.
Definition ABS_prod : forall {A B : Type'}, (A -> B -> Prop) -> prod A B :=
fun A B f => ε (fun p => f = mk_pair (fst p) (snd p)).
Lemma ABS_prod_mk_pair (A B : Type') (x : A) (y : B) :
ABS_prod (mk_pair x y) = (x,y).
Proof.
unfold ABS_prod.
match goal with [|- ε ?x = _] => set (Q := x); set (q := ε Q) end.
rewrite (surjective_pairing q).
assert (i : exists q, Q q). exists (x,y). reflexivity.
generalize (ε_spec i); fold q; unfold Q; intro h.
apply mk_pair_inj in h. destruct h as [h1 h2]. rewrite h1, h2. reflexivity.
Qed.
Lemma ABS_prod_mk_pair_eta (A B : Type') (x : A) (y : B) :
ABS_prod (fun a b => mk_pair x y a b) = (x,y).
Proof.
unfold ABS_prod.
match goal with [|- ε ?x = _] => set (Q := x); set (q := ε Q) end.
rewrite (surjective_pairing q).
assert (i : exists q, Q q). exists (x,y). reflexivity.
generalize (ε_spec i); fold q; unfold Q; intro h.
apply mk_pair_inj in h. destruct h as [h1 h2]. rewrite h1, h2. reflexivity.
Qed.
Definition REP_prod : forall {A B : Type'}, (prod A B) -> A -> B -> Prop :=
fun A B p a b => mk_pair (fst p) (snd p) a b.
Lemma pair_def {A B : Type'} : (@pair A B) = (fun x : A => fun y : B => @ABS_prod A B (@mk_pair A B x y)).
Proof.
apply fun_ext; intro x; apply fun_ext; intro y. symmetry.
apply ABS_prod_mk_pair.
Qed.
Lemma FST_def {A B : Type'} : (@fst A B) = (fun p : prod A B => @ε A (fun x : A => exists y : B, p = (@pair A B x y))).
Proof.
apply fun_ext; intros [x y]. simpl.
match goal with [|- _ = ε ?x] => set (Q := x); set (q := ε Q) end.
assert (i : exists x, Q x). exists x. exists y. reflexivity.
generalize (ε_spec i); fold q; intros [x' h']. inversion h'. reflexivity.
Qed.
Lemma SND_def {A B : Type'} : (@snd A B) = (fun p : prod A B => @ε B (fun y : B => exists x : A, p = (@pair A B x y))).
Proof.
apply fun_ext; intros [x y]. simpl.
match goal with [|- _ = ε ?x] => set (Q := x); set (q := ε Q) end.
assert (i : exists x, Q x). exists y. exists x. reflexivity.
generalize (ε_spec i); fold q; intros [x' h]. inversion h. reflexivity.
Qed.
Lemma axiom_4 : forall {A B : Type'} (a : prod A B), (@ABS_prod A B (@REP_prod A B a)) = a.
Proof. intros A B [a b]. apply ABS_prod_mk_pair_eta. Qed.
Lemma axiom_5 : forall {A B : Type'} (r : A -> B -> Prop), ((fun x : A -> B -> Prop => exists a : A, exists b : B, x = (@mk_pair A B a b)) r) = ((@REP_prod A B (@ABS_prod A B r)) = r).
Proof.
intros A B f. simpl. apply prop_ext.
intros [a [b e]]. subst. rewrite ABS_prod_mk_pair. reflexivity.
generalize (ABS_prod f); intros [a b] e. subst. exists a. exists b. reflexivity.
Qed.
(****************************************************************************)
(* HOL-Light infinite type ind. *)
(****************************************************************************)
Definition nat' := {| type := nat; el := 0 |}.
Canonical nat'.
Definition ind : Type' := nat'.
Definition ONE_ONE {A B : Type'} := fun _2064 : A -> B => forall x1 : A, forall x2 : A, ((_2064 x1) = (_2064 x2)) -> x1 = x2.
Definition ONTO {A B : Type'} := fun _2069 : A -> B => forall y : B, exists x : A, y = (_2069 x).
Lemma axiom_6 : exists f : ind -> ind, (@ONE_ONE ind ind f) /\ (~ (@ONTO ind ind f)).
Proof.
exists S. split. exact eq_add_S. intro h. generalize (h 0). intros [x hx].
discriminate.
Qed.
Definition IND_SUC_pred := fun f : ind -> ind => exists z : ind, (forall x1 : ind, forall x2 : ind, ((f x1) = (f x2)) = (x1 = x2)) /\ (forall x : ind, ~ ((f x) = z)).
Definition IND_SUC := ε IND_SUC_pred.
Lemma IND_SUC_ex : exists f, IND_SUC_pred f.
Proof.
destruct axiom_6 as [f [h1 h2]]. exists f.
unfold ONTO in h2. rewrite not_forall_eq in h2. destruct h2 as [z h2]. exists z.
split.
intros x y. apply prop_ext. apply h1. intro e. rewrite e. reflexivity.
rewrite not_exists_eq in h2. intros x e. apply (h2 x). symmetry. exact e.
Qed.
Lemma IND_SUC_prop : IND_SUC_pred IND_SUC.
Proof. unfold IND_SUC. apply ε_spec. apply IND_SUC_ex. Qed.
Lemma IND_SUC_inj : ONE_ONE IND_SUC.
Proof.
generalize IND_SUC_prop. intros [z [h1 h2]]. intros x y e. rewrite <- h1.
exact e.
Qed.
Definition IND_0_pred := fun z : ind => (forall x1 : ind, forall x2 : ind, ((IND_SUC x1) = (IND_SUC x2)) = (x1 = x2)) /\ (forall x : ind, ~ ((IND_SUC x) = z)).
Definition IND_0 := ε IND_0_pred.
Lemma IND_0_ex : exists z, IND_0_pred z.
Proof.
generalize IND_SUC_prop. intros [z [h1 h2]]. exists z. split. exact h1. exact h2.
Qed.
Lemma IND_0_prop : IND_0_pred IND_0.
Proof. unfold IND_0. apply ε_spec. apply IND_0_ex. Qed.
Lemma IND_SUC_neq_0 i : IND_SUC i <> IND_0.
Proof. generalize IND_0_prop. intros [h1 h2]. apply h2. Qed.
(****************************************************************************)
(* Mapping of HOL-Light type num to Coq type nat. *)
(****************************************************************************)
Fixpoint dest_num n :=
match n with
| 0 => IND_0
| S p => IND_SUC (dest_num p)
end.
Lemma dest_num_inj : ONE_ONE dest_num.
Proof.
intro x. induction x; intro y; destruct y; simpl.
reflexivity.
intro e. apply False_ind. eapply IND_SUC_neq_0. symmetry. exact e.
intro e. apply False_ind. eapply IND_SUC_neq_0. exact e.
intro e. apply f_equal. apply IHx. apply IND_SUC_inj. exact e.
Qed.
Definition NUM_REP := fun a : ind => forall NUM_REP' : ind -> Prop, (forall a' : ind, ((a' = IND_0) \/ (exists i : ind, (a' = (IND_SUC i)) /\ (NUM_REP' i))) -> NUM_REP' a') -> NUM_REP' a.
Definition NUM_REP' := fun a : ind => forall P : ind -> Prop, (P IND_0 /\ forall i, P i -> P (IND_SUC i)) -> P a.
Lemma NUM_REP_eq : NUM_REP = NUM_REP'.
Proof.
apply fun_ext; intro a. apply prop_ext; intros h P.
intros [p0 ps]. apply h. intros a' [i|i].
subst a'. exact p0.
destruct i as [b [e i]]. subst a'. apply ps. exact i.
intro i. apply h. split.
apply i. left. reflexivity.
intros b pb. apply i. right. exists b. split. reflexivity. exact pb.
Qed.
Lemma NUM_REP_0 : NUM_REP IND_0.
Proof. rewrite NUM_REP_eq. intros P [h _]. exact h. Qed.
Lemma NUM_REP_S i : NUM_REP i -> NUM_REP (IND_SUC i).
Proof.
rewrite NUM_REP_eq. intros hi P [h0 hS]. apply hS. apply hi.
split. exact h0. exact hS.
Qed.
Inductive NUM_REP_ID : ind -> Prop :=
| NUM_REP_ID_0 : NUM_REP_ID IND_0
| NUM_REP_ID_S i : NUM_REP_ID i -> NUM_REP_ID (IND_SUC i).
Lemma NUM_REP_eq_ID : NUM_REP = NUM_REP_ID.
Proof.
apply fun_ext; intro i. apply prop_ext.
rewrite NUM_REP_eq. intro h. apply h. split.
apply NUM_REP_ID_0.
intros j hj. apply NUM_REP_ID_S. exact hj.
induction 1. apply NUM_REP_0. apply NUM_REP_S. assumption.
Qed.
Definition dest_num_img i := exists n, i = dest_num n.
Lemma NUM_REP_eq_dest_num_img : NUM_REP = dest_num_img.
Proof.
apply fun_ext; intro i. apply prop_ext.
rewrite NUM_REP_eq_ID. revert i. induction 1.
exists 0. reflexivity.
destruct IHNUM_REP_ID as [n hn]. rewrite hn. exists (S n). reflexivity.
intros [n hn]. subst. induction n. apply NUM_REP_0. apply NUM_REP_S.
assumption.
Qed.
Lemma NUM_REP_dest_num k : NUM_REP (dest_num k).
Proof. induction k. apply NUM_REP_0. simpl. apply NUM_REP_S. assumption. Qed.
Definition mk_num_pred i n := i = dest_num n.
Definition mk_num i := ε (mk_num_pred i).
Lemma mk_num_ex i : NUM_REP i -> exists n, mk_num_pred i n.
Proof.
rewrite NUM_REP_eq_ID. induction 1.
exists 0. reflexivity.
destruct IHNUM_REP_ID as [n hn]. exists (S n). unfold mk_num_pred. rewrite hn.
reflexivity.
Qed.
Lemma mk_num_prop i : NUM_REP i -> dest_num (mk_num i) = i.
Proof. intro hi. symmetry. apply (ε_spec (mk_num_ex i hi)). Qed.
Notation dest_num_mk_num := mk_num_prop.
Lemma mk_num_dest_num k : mk_num (dest_num k) = k.
Proof. apply dest_num_inj. apply dest_num_mk_num. apply NUM_REP_dest_num. Qed.
Lemma axiom_7 : forall (a : nat), (mk_num (dest_num a)) = a.
Proof. exact mk_num_dest_num. Qed.
Lemma axiom_8 : forall (r : ind), (NUM_REP r) = ((dest_num (mk_num r)) = r).
Proof.
intro r. apply prop_ext. apply dest_num_mk_num. intro h. rewrite <- h.
apply NUM_REP_dest_num.
Qed.
Lemma mk_num_0 : mk_num IND_0 = 0.
Proof.
unfold mk_num. set (P := mk_num_pred IND_0).
assert (h: exists n, P n). exists 0. reflexivity.
generalize (ε_spec h). set (i := ε P). unfold P, mk_num_pred. intro e.
apply dest_num_inj. simpl. symmetry. exact e.
Qed.
Lemma _0_def : 0 = (mk_num IND_0).
Proof. symmetry. exact mk_num_0. Qed.
Lemma mk_num_S : forall i, NUM_REP i -> mk_num (IND_SUC i) = S (mk_num i).
Proof.
intros i hi. rewrite NUM_REP_eq_dest_num_img in hi. destruct hi as [n hn].
rewrite hn, mk_num_dest_num.
change (mk_num (dest_num (S n)) = S n). apply mk_num_dest_num.
Qed.
Lemma SUC_def : S = (fun _2104 : nat => mk_num (IND_SUC (dest_num _2104))).
Proof.
symmetry. apply fun_ext; intro x. rewrite mk_num_S. 2: apply NUM_REP_dest_num.
apply f_equal. apply axiom_7.
Qed.
(****************************************************************************)
(* Useful lemmas on booleans and natural numbers. *)
(****************************************************************************)
Require Import Lia PeanoNat. Import Nat.
Lemma eq_false_negb_true b : b = false -> negb b = true.
Proof. intro e. subst. reflexivity. Qed.
Lemma plus_1_minus_1 x : x + 1 - 1 = x.
Proof. lia. Qed.
Lemma S_minus_1 x : S x - 1 = x.
Proof. lia. Qed.
Lemma add_sub a b : a + b - a = b.
Proof. lia. Qed.
Lemma swap_add_sub a a' b : a' <= a -> a + b - a' = a - a' + b.
Proof. lia. Qed.
Lemma le_eq_lt x y : x <= y -> x = y \/ x < y.
Proof. lia. Qed.
Lemma ge_is_add {a b} : a >= b -> exists c, a = b + c.
Proof. intro h. exists (a - b). lia. Qed.
Lemma _0_le_nat_is_True y : (0 <= y) = True.
Proof. apply prop_ext; intro h. exact I. lia. Qed.
Lemma S_le_0_is_False y : (S y <= 0) = False.
Proof. apply prop_ext; lia. Qed.
Lemma S_eq_0_is_False y : (S y = 0) = False.
Proof. apply prop_ext; lia. Qed.
Lemma S_le_S x y : (S x <= S y) = (x <= y).
Proof. apply prop_ext; lia. Qed.
Lemma DIV_MULT m n : m <> 0 -> (m * n) / m = n.
Proof. (*thm_170547*) intro h. rewrite mul_comm. apply div_mul. exact h. Qed.
Lemma divmod_unicity k k' q r r' :
r < q -> r' < q -> k * q + r = k' * q + r' -> k = k' /\ r = r'.
Proof.
intros h h' e.
destruct (Compare_dec.lt_eq_lt_dec k k') as [[i|i]|i].
apply False_rec.
assert (e2 : k * q + r - k * q = k' * q + r' - k * q). lia.
rewrite add_sub, swap_add_sub, <- PeanoNat.Nat.mul_sub_distr_r in e2. nia. nia. lia.
apply False_rec.
assert (e2 : k * q + r - k' * q = k' * q + r' - k' * q). lia.
rewrite add_sub, swap_add_sub, <- PeanoNat.Nat.mul_sub_distr_r in e2. nia. nia.
Qed.
Lemma pow_add {x y z} : x ^ (y + z) = (x ^ y) * (x ^ z).
Proof. induction y. simpl. lia. simpl. rewrite IHy. lia. Qed.
(****************************************************************************)
(* Mapping of usual mathematical functions on natural numbers. *)
(****************************************************************************)
Definition NUMERAL (x : nat) := x.
Fixpoint BIT0 n :=
match n with
| 0 => 0
| S n => S (S (BIT0 n))
end.
Lemma BIT0_def : BIT0 =
(@ε (arr nat nat')
(fun fn : forall _ : nat, nat =>
and (@Logic.eq nat (fn O) O)
(forall n : nat, @Logic.eq nat (fn (S n)) (S (S (fn n)))))).
Proof.
match goal with [|- _ = ε ?x] => set (Q := x) end.
assert (i : exists q, Q q). exists BIT0. split; reflexivity.
generalize (ε_spec i). intros [h0 hs].
apply fun_ext; intro n. induction n; simpl.
rewrite h0. reflexivity. rewrite hs, IHn. reflexivity.
Qed.
Lemma BIT0_is_double : BIT0 = Nat.double.
Proof.
apply fun_ext; intro n. induction n; simpl. reflexivity. rewrite IHn.
unfold Nat.double. simpl. rewrite PeanoNat.Nat.add_succ_r. reflexivity.
Qed.
Definition BIT1 := fun _2143 : nat => S (BIT0 _2143).
Lemma PRE_def : pred = (@ε (arr (prod nat (prod nat nat)) (arr nat nat')) (fun PRE' : (prod nat (prod nat nat)) -> nat -> nat' => forall _2151 : prod nat (prod nat nat), ((PRE' _2151 ( 0)) = ( 0)) /\ (forall n : nat, (PRE' _2151 (S n)) = n)) (@pair nat (prod nat nat) ( (BIT0 (BIT0 (BIT0 (BIT0 (BIT1 (BIT0 (BIT1 0)))))))) (@pair nat nat ( (BIT0 (BIT1 (BIT0 (BIT0 (BIT1 (BIT0 (BIT1 0)))))))) ( (BIT1 (BIT0 (BIT1 (BIT0 (BIT0 (BIT0 (BIT1 0))))))))))).
Proof.
generalize (@pair nat (prod nat nat) ( (BIT0 (BIT0 (BIT0 (BIT0 (BIT1 (BIT0 (BIT1 0)))))))) (@pair nat nat ( (BIT0 (BIT1 (BIT0 (BIT0 (BIT1 (BIT0 (BIT1 0)))))))) ( (BIT1 (BIT0 (BIT1 (BIT0 (BIT0 (BIT0 (BIT1 0)))))))))).
generalize (prod nat (prod nat nat)).
intros A a.
match goal with [|- _ = ε ?x _] => set (Q := x) end.
assert (i : exists q, Q q). exists (fun _ => pred). split; reflexivity.
generalize (ε_spec i a). intros [h0 hs].
apply fun_ext; intro n. induction n; simpl.
rewrite h0. reflexivity. rewrite hs. reflexivity.
Qed.
Lemma add_def : Nat.add = (@ε (arr nat (arr nat (arr nat nat'))) (fun add' : nat -> nat -> nat -> nat => forall _2155 : nat, (forall n : nat, (add' _2155 ( 0) n) = n) /\ (forall m : nat, forall n : nat, (add' _2155 (S m) n) = (S (add' _2155 m n)))) ( (BIT1 (BIT1 (BIT0 (BIT1 (BIT0 (BIT1 0)))))))).
Proof.
generalize ( (BIT1 (BIT1 (BIT0 (BIT1 (BIT0 (BIT1 0))))))). intro a.
match goal with [|- _ = ε ?x _] => set (Q := x) end.
assert (i : exists q, Q q). exists (fun _ => Nat.add). split; reflexivity.
generalize (ε_spec i a). intros [h0 hs].
apply fun_ext; intro x. apply fun_ext; intro y.
induction x; simpl. rewrite h0. reflexivity. rewrite hs, IHx. reflexivity.
Qed.
Lemma mul_def : Nat.mul = (@ε (arr nat (arr nat (arr nat nat'))) (fun mul' : nat -> nat -> nat -> nat => forall _2186 : nat, (forall n : nat, (mul' _2186 ( 0) n) = ( 0)) /\ (forall m : nat, forall n : nat, (mul' _2186 (S m) n) = (Nat.add (mul' _2186 m n) n))) ( (BIT0 (BIT1 (BIT0 (BIT1 (BIT0 (BIT1 0)))))))).
Proof.
generalize ( (BIT0 (BIT1 (BIT0 (BIT1 (BIT0 (BIT1 0))))))). intro a.
match goal with [|- _ = ε ?x _] => set (Q := x) end.
assert (i : exists q, Q q). exists (fun _ => Nat.mul). split; simpl. reflexivity.
intros m n. rewrite PeanoNat.Nat.add_comm. reflexivity.
generalize (ε_spec i a). intros [h0 hs].
apply fun_ext; intro x. apply fun_ext; intro y.
induction x; simpl. rewrite h0. reflexivity. rewrite hs, IHx, PeanoNat.Nat.add_comm. reflexivity.
Qed.
Lemma EXP_def : Nat.pow = (@ε (arr (prod nat (prod nat nat)) (arr nat (arr nat nat'))) (fun EXP' : (prod nat (prod nat nat)) -> nat -> nat -> nat => forall _2224 : prod nat (prod nat nat), (forall m : nat, EXP' _2224 m 0 = BIT1 0) /\ (forall m : nat, forall n : nat, (EXP' _2224 m (S n)) = (Nat.mul m (EXP' _2224 m n)))) (@pair nat (prod nat nat) (BIT1 (BIT0 (BIT1 (BIT0 (BIT0 (BIT0 (BIT1 0))))))) (@pair nat nat (BIT0 (BIT0 (BIT0 (BIT1 (BIT1 (BIT0 (BIT1 0))))))) (BIT0 (BIT0 (BIT0 (BIT0 (BIT1 (BIT0 (BIT1 0)))))))))).
Proof.
generalize (@pair nat (prod nat nat) (BIT1 (BIT0 (BIT1 (BIT0 (BIT0 (BIT0 (BIT1 0))))))) (@pair nat nat (BIT0 (BIT0 (BIT0 (BIT1 (BIT1 (BIT0 (BIT1 0))))))) (BIT0 (BIT0 (BIT0 (BIT0 (BIT1 (BIT0 (BIT1 0))))))))); generalize (@prod nat (prod nat nat)); intros A a.
match goal with [|- _ = ε ?x _] => set (Q := x) end.
assert (i : exists q, Q q). exists (fun _ => Nat.pow). split; simpl; intro x; reflexivity.
generalize (ε_spec i a). intros [h0 hs].
apply fun_ext; intro x. apply fun_ext; intro y.
induction y; simpl. rewrite h0. reflexivity. rewrite hs, IHy. reflexivity.
Qed.
Lemma le_def : le = (@ε (arr (prod nat nat) (arr nat (arr nat Prop'))) (fun le' : (prod nat nat) -> nat -> nat -> Prop => forall _2241 : prod nat nat, (forall m : nat, (le' _2241 m ( 0)) = (m = ( 0))) /\ (forall m : nat, forall n : nat, (le' _2241 m (S n)) = ((m = (S n)) \/ (le' _2241 m n)))) (@pair nat nat ( (BIT0 (BIT0 (BIT1 (BIT1 (BIT1 (BIT1 0))))))) ( (BIT1 (BIT0 (BIT1 (BIT1 (BIT1 (BIT1 0))))))))).
Proof.
generalize (@pair nat nat ( (BIT0 (BIT0 (BIT1 (BIT1 (BIT1 (BIT1 0))))))) ( (BIT1 (BIT0 (BIT1 (BIT1 (BIT1 (BIT1 0)))))))); generalize (prod nat nat); intros A a.
match goal with [|- _ = ε ?x _] => set (Q := x) end.
assert (i : exists q, Q q). exists (fun _ => le). split; simpl; intro x.
apply prop_ext; intro h.
symmetry. apply Arith_base.le_n_0_eq_stt. exact h.
rewrite h. reflexivity.
intro n. apply prop_ext; lia.
generalize (ε_spec i a). intros [h0 hs].
apply fun_ext; intro x. apply fun_ext; intro y.
apply prop_ext.
induction 1.
induction x. rewrite h0. reflexivity. rewrite hs. left. reflexivity.
rewrite hs. right. assumption.
induction y.
rewrite h0. intro h. rewrite h. reflexivity.
rewrite hs. intros [h|h]. rewrite h. reflexivity. apply le_S. apply IHy.
exact h.
Qed.
Lemma lt_def : lt = (@ε (arr nat (arr nat (arr nat Prop'))) (fun lt : nat -> nat -> nat -> Prop => forall _2248 : nat, (forall m : nat, (lt _2248 m ( 0)) = False) /\ (forall m : nat, forall n : nat, (lt _2248 m (S n)) = ((m = n) \/ (lt _2248 m n)))) ( (BIT0 (BIT0 (BIT1 (BIT1 (BIT1 (BIT1 0)))))))).
Proof.
generalize ( (BIT0 (BIT0 (BIT1 (BIT1 (BIT1 (BIT1 0))))))); intro a.
match goal with [|- _ = ε ?x _] => set (Q := x) end.
assert (i : exists q, Q q). exists (fun _ => lt). split; intro x. apply prop_ext; lia. intro n. apply prop_ext; lia.
generalize (ε_spec i a). intros [h0 hs].
apply fun_ext; intro x. apply fun_ext; intro y.
apply prop_ext.
induction y. lia. rewrite hs. intro h. apply le_S_n in h.
apply le_eq_lt in h. destruct h as [h|h].
left. exact h. right. apply IHy. exact h.
induction y.
rewrite h0. lia.
rewrite hs. intros [h|h]. rewrite h. unfold lt. lia. generalize (IHy h).
lia.
Qed.
Lemma ge_def : ge = (fun _2249 : nat => fun _2250 : nat => le _2250 _2249).
Proof. apply fun_ext; intro x. apply fun_ext; intro y. reflexivity. Qed.
Lemma gt_def : gt = (fun _2261 : nat => fun _2262 : nat => lt _2262 _2261).
Proof. apply fun_ext; intro x. apply fun_ext; intro y. reflexivity. Qed.
Definition COND {A : Type'} := fun t : Prop => fun t1 : A => fun t2 : A => @ε A (fun x : A => ((t = True) -> x = t1) /\ ((t = False) -> x = t2)).
Lemma COND_True (A : Type') (x y : A) : COND True x y = x.
Proof.
unfold COND. match goal with [|- ε ?x = _] => set (Q := x) end.
assert (i : exists q, Q q). exists x. split; intro h.
reflexivity. apply False_rec. rewrite <- h. exact I.
generalize (ε_spec i). intros [h1 h2]. apply h1. reflexivity.
Qed.
Lemma COND_False (A : Type') (x y : A) : COND False x y = y.
Proof.
unfold COND. match goal with [|- ε ?x = _] => set (Q := x) end.
assert (i : exists q, Q q). exists y. split; intro h. apply False_rec.
rewrite h. exact I. reflexivity.
generalize (ε_spec i). intros [h1 h2]. apply h2. reflexivity.
Qed.
Lemma MAX_def : max = (fun _2273 : nat => fun _2274 : nat => @COND nat (Peano.le _2273 _2274) _2274 _2273).
Proof.
apply fun_ext; intro x. apply fun_ext. induction x; intro y; induction y.
rewrite _0_le_nat_is_True, COND_True. reflexivity.
simpl. rewrite _0_le_nat_is_True, COND_True. reflexivity.
rewrite S_le_0_is_False, COND_False. reflexivity.
simpl. rewrite IHx, S_le_S. destruct (prop_degen (x <= y)) as [h|h]; rewrite h.
rewrite! COND_True. reflexivity. rewrite! COND_False. reflexivity.
Qed.
Lemma MIN_def : min = (fun _2285 : nat => fun _2286 : nat => @COND nat (Peano.le _2285 _2286) _2285 _2286).
Proof.
apply fun_ext; intro x. apply fun_ext. induction x; intro y; induction y.
rewrite _0_le_nat_is_True, COND_True. reflexivity.
simpl. rewrite _0_le_nat_is_True, COND_True. reflexivity.
rewrite S_le_0_is_False, COND_False. reflexivity.
simpl. rewrite IHx, S_le_S. destruct (prop_degen (x <= y)) as [h|h]; rewrite h.
rewrite! COND_True. reflexivity. rewrite! COND_False. reflexivity.
Qed.
Lemma minus_def : Nat.sub = (@ε (arr nat (arr nat (arr nat nat'))) (fun pair' : nat -> nat -> nat -> nat => forall _2766 : nat, (forall m : nat, (pair' _2766 m ( 0)) = m) /\ (forall m : nat, forall n : nat, (pair' _2766 m (S n)) = (Nat.pred (pair' _2766 m n)))) ( (BIT1 (BIT0 (BIT1 (BIT1 (BIT0 (BIT1 0)))))))).
Proof.
generalize ( (BIT1 (BIT0 (BIT1 (BIT1 (BIT0 (BIT1 0))))))); intro a.
match goal with [|- _ = ε ?x _] => set (Q := x) end.
assert (i : exists q, Q q). exists (fun _ => minus). split; lia.
generalize (ε_spec i a). intros [h0 hs].
apply fun_ext; intro x. apply fun_ext. induction x.
intro y. induction y; simpl.
rewrite h0. reflexivity. rewrite hs, <- IHy. reflexivity.
intro y. induction y. rewrite h0. reflexivity. rewrite hs, <- IHy. lia.
Qed.
Lemma FACT_def : Factorial.fact = (@ε (arr (prod nat (prod nat (prod nat nat))) (arr nat nat')) (fun FACT' : (prod nat (prod nat (prod nat nat))) -> nat -> nat => forall _2944 : prod nat (prod nat (prod nat nat)), ((FACT' _2944 ( 0)) = ( (BIT1 0))) /\ (forall n : nat, (FACT' _2944 (S n)) = (Nat.mul (S n) (FACT' _2944 n)))) (@pair nat (prod nat (prod nat nat)) ( (BIT0 (BIT1 (BIT1 (BIT0 (BIT0 (BIT0 (BIT1 0)))))))) (@pair nat (prod nat nat) ( (BIT1 (BIT0 (BIT0 (BIT0 (BIT0 (BIT0 (BIT1 0)))))))) (@pair nat nat ( (BIT1 (BIT1 (BIT0 (BIT0 (BIT0 (BIT0 (BIT1 0)))))))) ( (BIT0 (BIT0 (BIT1 (BIT0 (BIT1 (BIT0 (BIT1 0)))))))))))).
Proof.
generalize (@pair nat (prod nat (prod nat nat)) ( (BIT0 (BIT1 (BIT1 (BIT0 (BIT0 (BIT0 (BIT1 0)))))))) (@pair nat (prod nat nat) ( (BIT1 (BIT0 (BIT0 (BIT0 (BIT0 (BIT0 (BIT1 0)))))))) (@pair nat nat ( (BIT1 (BIT1 (BIT0 (BIT0 (BIT0 (BIT0 (BIT1 0)))))))) ( (BIT0 (BIT0 (BIT1 (BIT0 (BIT1 (BIT0 (BIT1 0))))))))))); generalize (prod nat (prod nat (prod nat nat))); intros A a.
match goal with [|- _ = ε ?x _] => set (Q := x) end.
assert (i : exists q, Q q). exists (fun _ => Factorial.fact). split; reflexivity.
generalize (ε_spec i a). intros [h0 hs].
apply fun_ext; intro x. induction x. rewrite h0. reflexivity. rewrite hs, <- IHx. reflexivity.
Qed.
Lemma DIV_def : Nat.div = (@ε (arr (prod nat (prod nat nat)) (arr nat (arr nat nat'))) (fun q : (prod nat (prod nat nat)) -> nat -> nat -> nat => forall _3086 : prod nat (prod nat nat), exists r : nat -> nat -> nat, forall m : nat, forall n : nat, @COND Prop (n = ( 0)) (((q _3086 m n) = ( 0)) /\ ((r m n) = m)) ((m = (Nat.add (Nat.mul (q _3086 m n) n) (r m n))) /\ (lt (r m n) n))) (@pair nat (prod nat nat) ( (BIT0 (BIT0 (BIT1 (BIT0 (BIT0 (BIT0 (BIT1 0)))))))) (@pair nat nat ( (BIT1 (BIT0 (BIT0 (BIT1 (BIT0 (BIT0 (BIT1 0)))))))) ( (BIT0 (BIT1 (BIT1 (BIT0 (BIT1 (BIT0 (BIT1 0))))))))))).
Proof.
generalize (@pair nat (prod nat nat) ( (BIT0 (BIT0 (BIT1 (BIT0 (BIT0 (BIT0 (BIT1 0)))))))) (@pair nat nat ( (BIT1 (BIT0 (BIT0 (BIT1 (BIT0 (BIT0 (BIT1 0)))))))) ( (BIT0 (BIT1 (BIT1 (BIT0 (BIT1 (BIT0 (BIT1 0)))))))))); generalize (prod nat (prod nat (prod nat nat))); intros A a.
match goal with [|- _ = ε ?x _] => set (Q := x) end.
assert (i : exists q, Q q). exists (fun _ => Nat.div). intro x. exists Nat.modulo. intros m n.
destruct (prop_degen (n=0)) as [h|h]; rewrite h.
rewrite COND_True. rewrite is_True in h. rewrite h. simpl. split; reflexivity.
rewrite COND_False. rewrite (PeanoNat.Nat.div_mod_eq m n) at 1. split. lia.
rewrite is_False in h. apply PeanoNat.Nat.mod_bound_pos. lia. lia.
generalize (ε_spec i a). intros [mod' h].
apply fun_ext; intro x. apply fun_ext; intro y.
revert x. induction y.
intro x. generalize (h x 0). rewrite refl_is_True, COND_True. intros [h1 h2].
rewrite h1. reflexivity.
intro x. generalize (h x (S y)). rewrite S_eq_0_is_False, COND_False. intros [h1 h2].
simpl. generalize (Coq.Arith.PeanoNat.Nat.divmod_spec x y 0 y (le_n y)).
destruct (Nat.divmod x y 0 y) as [q r]. simpl.
rewrite PeanoNat.Nat.sub_diag, PeanoNat.Nat.mul_0_r, !PeanoNat.Nat.add_0_r. rewrite h1 at 1.
intros [i1 i2]. assert (h3 : y - r < S y). lia.
assert (e : q * S y = q + y * q). lia. rewrite <- e in i1.
generalize (divmod_unicity (ε Q a x (S y)) q (S y) (mod' x (S y)) (y - r) h2 h3 i1).
intros [j1 j2]. symmetry. exact j1.
Qed.
Lemma MOD_def : Nat.modulo = (@ε (arr (prod nat (prod nat nat)) (arr nat (arr nat nat'))) (fun r : (prod nat (prod nat nat)) -> nat -> nat -> nat => forall _3087 : prod nat (prod nat nat), forall m : nat, forall n : nat, @COND Prop (n = ( 0)) (((Nat.div m n) = ( 0)) /\ ((r _3087 m n) = m)) ((m = (Nat.add (Nat.mul (Nat.div m n) n) (r _3087 m n))) /\ (lt (r _3087 m n) n))) (@pair nat (prod nat nat) ( (BIT1 (BIT0 (BIT1 (BIT1 (BIT0 (BIT0 (BIT1 0)))))))) (@pair nat nat ( (BIT1 (BIT1 (BIT1 (BIT1 (BIT0 (BIT0 (BIT1 0)))))))) ( (BIT0 (BIT0 (BIT1 (BIT0 (BIT0 (BIT0 (BIT1 0))))))))))).
Proof.
generalize (@pair nat (prod nat nat) ( (BIT1 (BIT0 (BIT1 (BIT1 (BIT0 (BIT0 (BIT1 0)))))))) (@pair nat nat ( (BIT1 (BIT1 (BIT1 (BIT1 (BIT0 (BIT0 (BIT1 0)))))))) ( (BIT0 (BIT0 (BIT1 (BIT0 (BIT0 (BIT0 (BIT1 0)))))))))); generalize (prod nat (prod nat (prod nat nat))); intros A a.
match goal with [|- _ = ε ?x _] => set (Q := x) end.
assert (i : exists q, Q q). exists (fun _ => Nat.modulo). intros x m n. destruct n.
rewrite refl_is_True, COND_True. split; reflexivity.
rewrite S_eq_0_is_False, COND_False. split.
rewrite PeanoNat.Nat.mul_comm. apply Coq.Arith.PeanoNat.Nat.div_mod_eq.
apply PeanoNat.Nat.mod_bound_pos. lia. lia.
generalize (ε_spec i a). intro h.
apply fun_ext; intro x. apply fun_ext; intro y.
revert x. induction y.
intro x. generalize (h x 0). rewrite refl_is_True, COND_True. intros [h1 h2].
symmetry. exact h2.
intro x. generalize (h x (S y)). rewrite S_eq_0_is_False, COND_False.
intros [h1 h2].
generalize (Coq.Arith.PeanoNat.Nat.divmod_spec x y 0 y (le_n y)).
unfold Nat.modulo. destruct (Nat.divmod x y 0 y) as [q r].
rewrite PeanoNat.Nat.sub_diag, PeanoNat.Nat.mul_0_r, !PeanoNat.Nat.add_0_r.
rewrite h1 at 1.
intros [i1 i2]. assert (h3 : y - r < S y). lia.
rewrite (PeanoNat.Nat.mul_comm (S y) q) in i1.
generalize (divmod_unicity (Nat.div x (S y)) q (S y) (ε Q a x (S y)) (y - r) h2 h3 i1).
intros [j1 j2]. symmetry. exact j2.
Qed.
(****************************************************************************)
(* Mapping of the Even and Odd predicates. *)
(****************************************************************************)
Import PeanoNat.Nat Private_Parity.
Lemma odd_double n : odd (2 * n) = false.
Proof. rewrite odd_mul, odd_2. reflexivity. Qed.
Lemma even_double n : even (2 * n) = true.
Proof. rewrite even_spec. exists n. reflexivity. Qed.
Lemma Even_or_Odd x : Even x \/ Odd x.
Proof.
rewrite (div_mod_eq x 2). assert (h1: 0 <= x). lia. assert (h2: 0 < 2). lia.
generalize (mod_bound_pos x 2 h1 h2). generalize (x mod 2). intro n.
destruct n; intro h.
left. exists (x / 2). lia. destruct n. right. exists (x / 2). reflexivity. lia.
Qed.
Lemma not_Even_is_Odd x : (~Even x) = Odd x.
Proof.
apply prop_ext; intro h; generalize (Even_or_Odd x); intros [i|i].
apply False_rec. exact (h i). exact i. destruct h as [k hk].
destruct i as [l hl]. lia.
intros [k hk]. destruct i as [l hl]. lia.
Qed.
Lemma not_Odd_is_Even x : (~Odd x) = Even x.
Proof.
apply prop_ext; intro h; generalize (Even_or_Odd x); intros [i|i].
exact i. apply False_rec. exact (h i). destruct h as [k hk]. intro j.
destruct j as [l hl]. lia.
intros [k hk]. destruct h as [l hl]. lia.
Qed.
Lemma Even_S x : Even (S x) = Odd x.
Proof.
apply prop_ext; intros [k hk].
rewrite <- not_Even_is_Odd. intros [l hl]. lia.
rewrite <- not_Odd_is_Even. intros [l hl]. lia.
Qed.
Lemma Odd_S x : Odd (S x) = Even x.
Proof.
apply prop_ext; intros [k hk].
rewrite <- not_Odd_is_Even. intros [l hl]. lia.
rewrite <- not_Even_is_Odd. intros [l hl]. lia.
Qed.
Lemma EVEN_def : Even = (@ε (arr (prod nat (prod nat (prod nat nat))) (arr nat Prop')) (fun EVEN' : (prod nat (prod nat (prod nat nat))) -> nat -> Prop => forall _2603 : prod nat (prod nat (prod nat nat)), ((EVEN' _2603 (0)) = True) /\ (forall n : nat, (EVEN' _2603 (S n)) = (~ (EVEN' _2603 n)))) (@pair nat (prod nat (prod nat nat)) ((BIT1 (BIT0 (BIT1 (BIT0 (BIT0 (BIT0 (BIT1 0)))))))) (@pair nat (prod nat nat) ((BIT0 (BIT1 (BIT1 (BIT0 (BIT1 (BIT0 (BIT1 0)))))))) (@pair nat nat ((BIT1 (BIT0 (BIT1 (BIT0 (BIT0 (BIT0 (BIT1 0)))))))) ((BIT0 (BIT1 (BIT1 (BIT1 (BIT0 (BIT0 (BIT1 0)))))))))))).
Proof.
generalize (@pair nat (prod nat (prod nat nat)) ((BIT1 (BIT0 (BIT1 (BIT0 (BIT0 (BIT0 (BIT1 0)))))))) (@pair nat (prod nat nat) ((BIT0 (BIT1 (BIT1 (BIT0 (BIT1 (BIT0 (BIT1 0)))))))) (@pair nat nat ((BIT1 (BIT0 (BIT1 (BIT0 (BIT0 (BIT0 (BIT1 0)))))))) ((BIT0 (BIT1 (BIT1 (BIT1 (BIT0 (BIT0 (BIT1 0))))))))))); generalize (prod nat (prod nat (prod nat nat))); intros A a.
match goal with [|- _ = ε ?x _] => set (Q := x) end.
assert (i : exists q, Q q). exists (fun _ => Even). intro x. split.
rewrite is_True. exact Even_0. intro n. rewrite not_Even_is_Odd. apply Even_S.
generalize (ε_spec i a). intros [h1 h2].
apply fun_ext; intro x. induction x.
apply prop_ext; intro h. rewrite h1. exact I. exact Even_0.
rewrite h2, <- IHx, not_Even_is_Odd. apply Even_S.
Qed.
Lemma ODD_def : Odd = (@ε (arr (prod nat (prod nat nat)) (arr nat Prop')) (fun ODD' : (prod nat (prod nat nat)) -> nat -> Prop => forall _2607 : prod nat (prod nat nat), ((ODD' _2607 (0)) = False) /\ (forall n : nat, (ODD' _2607 (S n)) = (~ (ODD' _2607 n)))) (@pair nat (prod nat nat) ((BIT1 (BIT1 (BIT1 (BIT1 (BIT0 (BIT0 (BIT1 0)))))))) (@pair nat nat ((BIT0 (BIT0 (BIT1 (BIT0 (BIT0 (BIT0 (BIT1 0)))))))) ((BIT0 (BIT0 (BIT1 (BIT0 (BIT0 (BIT0 (BIT1 0))))))))))).
Proof.
generalize (@pair nat (prod nat nat) ((BIT1 (BIT1 (BIT1 (BIT1 (BIT0 (BIT0 (BIT1 0)))))))) (@pair nat nat ((BIT0 (BIT0 (BIT1 (BIT0 (BIT0 (BIT0 (BIT1 0)))))))) ((BIT0 (BIT0 (BIT1 (BIT0 (BIT0 (BIT0 (BIT1 0)))))))))); generalize (prod nat (prod nat (prod nat nat))); intros A a.
match goal with [|- _ = ε ?x _] => set (Q := x) end.
assert (i : exists q, Q q). exists (fun _ => Odd). intro x. split.
rewrite is_False. exact Odd_0. intro n. rewrite not_Odd_is_Even. apply Odd_S.
generalize (ε_spec i a). intros [h1 h2].
apply fun_ext; intro x. induction x.
apply prop_ext; intro h. rewrite h1. apply Odd_0. exact h.
apply False_rec. rewrite <- h1. exact h.
rewrite h2, <- IHx, not_Odd_is_Even. apply Odd_S.
Qed.
(****************************************************************************)
(* NUMPAIR(x,y) = 2^x(2y+1): bijection between N² and N-{0}. *)
(****************************************************************************)
Definition NUMPAIR := fun _17324 : nat => fun _17325 : nat => Nat.mul (Nat.pow (NUMERAL (BIT0 (BIT1 0))) _17324) (Nat.add (Nat.mul (NUMERAL (BIT0 (BIT1 0))) _17325) (NUMERAL (BIT1 0))).
Lemma NUMPAIR_INJ : forall x1 : nat, forall y1 : nat, forall x2 : nat, forall y2 : nat, ((NUMPAIR x1 y1) = (NUMPAIR x2 y2)) = ((x1 = x2) /\ (y1 = y2)).
Proof. (*thm_1052244*)
intros x1 y1 x2 y2. apply prop_ext. 2: intros [e1 e2]; subst; reflexivity.
unfold NUMPAIR, NUMERAL, BIT1, BIT0. intro e.
destruct (Compare_dec.lt_eq_lt_dec x1 x2) as [[h|h]|h].
apply False_rec. destruct (ge_is_add h) as [k hk]. subst x2.
generalize (f_equal (fun x => div x (2 ^ x1)) e).
rewrite DIV_MULT, pow_add, pow_succ_r, (mul_comm 2 (2 ^ x1)), <- !mul_assoc,
DIV_MULT. lia.
apply pow_nonzero. lia.
lia.
apply pow_nonzero. lia.
subst x2. split. reflexivity.
generalize (f_equal (fun x => div x (2 ^ x1)) e). rewrite !DIV_MULT. lia.
apply pow_nonzero. lia.
apply pow_nonzero. lia.
apply False_rec. destruct (ge_is_add h) as [k hk]. subst x1.
generalize (f_equal (fun x => div x (2 ^ x2)) e).
rewrite DIV_MULT, pow_add, pow_succ_r, (mul_comm 2 (2 ^ x2)), <- !mul_assoc,
DIV_MULT. lia.
apply pow_nonzero. lia.
lia.
apply pow_nonzero. lia.
Qed.
Lemma NUMPAIR_nonzero x y : NUMPAIR x y <> 0.
Proof.
unfold NUMPAIR, NUMERAL, BIT1, BIT0. rewrite mul_add_distr_l, mul_1_r.
assert (h: 2 ^ x <> 0). apply pow_nonzero. lia. lia.
Qed.
(****************************************************************************)
(* Definition of the inverse of NUMPAIR like in HOL-Light. *)
(****************************************************************************)
Lemma INJ_INVERSE2 {A B C : Type'} : forall P : A -> B -> C, (forall x1 : A, forall y1 : B, forall x2 : A, forall y2 : B, ((P x1 y1) = (P x2 y2)) = ((x1 = x2) /\ (y1 = y2))) -> exists X : C -> A, exists Y : C -> B, forall x : A, forall y : B, ((X (P x y)) = x) /\ ((Y (P x y)) = y).
Proof. (*thm_1046684*)
intros f h.
exists (fun c => ε (fun a => exists b, f a b = c)).
exists (fun c => ε (fun b => exists a, f a b = c)).
intros a b. split.
match goal with [|- ε ?x = _] => set (Q := x); set (q := ε Q) end.
assert (i : exists a, Q a). exists a. exists b. reflexivity.
generalize (ε_spec i). fold q. unfold Q. intros [b' j]. rewrite h in j.
destruct j as [j1 j2]. auto.
match goal with [|- ε ?x = _] => set (Q := x); set (q := ε Q) end.
assert (i : exists b, Q b). exists b. exists a. reflexivity.
generalize (ε_spec i). fold q. unfold Q. intros [a' j]. rewrite h in j.
destruct j as [j1 j2]. auto.
Qed.
Definition NUMFST0_pred := fun X : nat -> nat => exists Y : nat -> nat, forall x : nat, forall y : nat, ((X (NUMPAIR x y)) = x) /\ ((Y (NUMPAIR x y)) = y).
Definition NUMFST0 := ε NUMFST0_pred.
Lemma NUMFST0_NUMPAIR x y : NUMFST0 (NUMPAIR x y) = x.
Proof.
destruct (INJ_INVERSE2 _ NUMPAIR_INJ) as [fst [snd h]].
assert (i : exists q, NUMFST0_pred q). exists fst. exists snd. assumption.
generalize (ε_spec i). fold NUMFST0. unfold NUMFST0_pred.
intros [snd' h']. destruct (h' x y) as [j k]. assumption.
Qed.
Definition NUMSND0_pred := fun Y : nat -> nat => exists X : nat -> nat, forall x : nat, forall y : nat, ((X (NUMPAIR x y)) = x) /\ ((Y (NUMPAIR x y)) = y).
Definition NUMSND0 := ε NUMSND0_pred.
Lemma NUMSND0_NUMPAIR x y : NUMSND0 (NUMPAIR x y) = y.
Proof.
destruct (INJ_INVERSE2 _ NUMPAIR_INJ) as [fst [snd h]].
assert (i : exists x, NUMSND0_pred x). exists snd. exists fst. assumption.
generalize (ε_spec i). fold NUMSND0. unfold NUMSND0_pred.
intros [fst' h']. destruct (h' x y) as [j k]. assumption.
Qed.
Definition NUMFST := @ε ((prod nat (prod nat (prod nat (prod nat (prod nat nat))))) -> nat -> nat) (fun X : (prod nat (prod nat (prod nat (prod nat (prod nat nat))))) -> nat -> nat => forall _17340 : prod nat (prod nat (prod nat (prod nat (prod nat nat)))), exists Y : nat -> nat, forall x : nat, forall y : nat, ((X _17340 (NUMPAIR x y)) = x) /\ ((Y (NUMPAIR x y)) = y)) (@pair nat (prod nat (prod nat (prod nat (prod nat nat)))) (NUMERAL (BIT0 (BIT1 (BIT1 (BIT1 (BIT0 (BIT0 (BIT1 0)))))))) (@pair nat (prod nat (prod nat (prod nat nat))) (NUMERAL (BIT1 (BIT0 (BIT1 (BIT0 (BIT1 (BIT0 (BIT1 0)))))))) (@pair nat (prod nat (prod nat nat)) (NUMERAL (BIT1 (BIT0 (BIT1 (BIT1 (BIT0 (BIT0 (BIT1 0)))))))) (@pair nat (prod nat nat) (NUMERAL (BIT0 (BIT1 (BIT1 (BIT0 (BIT0 (BIT0 (BIT1 0)))))))) (@pair nat nat (NUMERAL (BIT1 (BIT1 (BIT0 (BIT0 (BIT1 (BIT0 (BIT1 0)))))))) (NUMERAL (BIT0 (BIT0 (BIT1 (BIT0 (BIT1 (BIT0 (BIT1 0))))))))))))).
Lemma NUMFST_NUMPAIR x y : NUMFST (NUMPAIR x y) = x.
Proof.
unfold NUMFST.
generalize (NUMERAL (BIT0 (BIT1 (BIT1 (BIT1 (BIT0 (BIT0 (BIT1 0))))))),
(NUMERAL (BIT1 (BIT0 (BIT1 (BIT0 (BIT1 (BIT0 (BIT1 0))))))),
(NUMERAL (BIT1 (BIT0 (BIT1 (BIT1 (BIT0 (BIT0 (BIT1 0))))))),
(NUMERAL (BIT0 (BIT1 (BIT1 (BIT0 (BIT0 (BIT0 (BIT1 0))))))),
(NUMERAL (BIT1 (BIT1 (BIT0 (BIT0 (BIT1 (BIT0 (BIT1 0))))))),
NUMERAL (BIT0 (BIT0 (BIT1 (BIT0 (BIT1 (BIT0 (BIT1 0)))))))))))).
generalize (prod nat (prod nat (prod nat (prod nat (prod nat nat))))); intros A a.
match goal with |- ε ?x _ _ = _ => set (Q := x); set (fst := ε Q) end.
assert (i: exists x, Q x). exists (fun _ => NUMFST0). unfold Q. intros _. exists NUMSND0.
intros x' y'. rewrite NUMFST0_NUMPAIR, NUMSND0_NUMPAIR. auto.
generalize (ε_spec i). change (Q fst -> fst a (NUMPAIR x y) = x). intro h.
destruct (h a) as [snd j]. destruct (j x y) as [j1 j2]. exact j1.
Qed.
Definition NUMSND1_pred := fun Y : nat -> nat => forall x : nat, forall y : nat, ((NUMFST (NUMPAIR x y)) = x) /\ ((Y (NUMPAIR x y)) = y).
Definition NUMSND1 := ε NUMSND1_pred.
Lemma NUMSND1_NUMPAIR x y : NUMSND1 (NUMPAIR x y) = y.
Proof.
destruct (INJ_INVERSE2 _ NUMPAIR_INJ) as [fst [snd h]].
assert (i : exists x, NUMSND1_pred x). exists snd. unfold NUMSND1_pred.
intros x' y'. rewrite NUMFST_NUMPAIR. destruct (h x' y') as [h1 h2]. auto.
generalize (ε_spec i). fold NUMSND1. unfold NUMSND1_pred. intro j.
destruct (j x y) as [j1 j2]. exact j2.
Qed.
Definition NUMSND := @ε ((prod nat (prod nat (prod nat (prod nat (prod nat nat))))) -> nat -> nat) (fun Y : (prod nat (prod nat (prod nat (prod nat (prod nat nat))))) -> nat -> nat => forall _17341 : prod nat (prod nat (prod nat (prod nat (prod nat nat)))), forall x : nat, forall y : nat, ((NUMFST (NUMPAIR x y)) = x) /\ ((Y _17341 (NUMPAIR x y)) = y)) (@pair nat (prod nat (prod nat (prod nat (prod nat nat)))) (NUMERAL (BIT0 (BIT1 (BIT1 (BIT1 (BIT0 (BIT0 (BIT1 0)))))))) (@pair nat (prod nat (prod nat (prod nat nat))) (NUMERAL (BIT1 (BIT0 (BIT1 (BIT0 (BIT1 (BIT0 (BIT1 0)))))))) (@pair nat (prod nat (prod nat nat)) (NUMERAL (BIT1 (BIT0 (BIT1 (BIT1 (BIT0 (BIT0 (BIT1 0)))))))) (@pair nat (prod nat nat) (NUMERAL (BIT1 (BIT1 (BIT0 (BIT0 (BIT1 (BIT0 (BIT1 0)))))))) (@pair nat nat (NUMERAL (BIT0 (BIT1 (BIT1 (BIT1 (BIT0 (BIT0 (BIT1 0)))))))) (NUMERAL (BIT0 (BIT0 (BIT1 (BIT0 (BIT0 (BIT0 (BIT1 0))))))))))))).
Lemma NUMSND_NUMPAIR x y : NUMSND (NUMPAIR x y) = y.
Proof.
unfold NUMSND.
generalize (NUMERAL (BIT0 (BIT1 (BIT1 (BIT1 (BIT0 (BIT0 (BIT1 0))))))),
(NUMERAL (BIT1 (BIT0 (BIT1 (BIT0 (BIT1 (BIT0 (BIT1 0))))))),
(NUMERAL (BIT1 (BIT0 (BIT1 (BIT1 (BIT0 (BIT0 (BIT1 0))))))),
(NUMERAL (BIT1 (BIT1 (BIT0 (BIT0 (BIT1 (BIT0 (BIT1 0))))))),
(NUMERAL (BIT0 (BIT1 (BIT1 (BIT1 (BIT0 (BIT0 (BIT1 0))))))),
NUMERAL (BIT0 (BIT0 (BIT1 (BIT0 (BIT0 (BIT0 (BIT1 0)))))))))))).
generalize (prod nat (prod nat (prod nat (prod nat (prod nat nat))))); intros A a.
match goal with |- ε ?x _ _ = _ => set (Q := x); set (snd := ε Q) end.
assert (i: exists x, Q x). exists (fun _ => NUMSND1). unfold Q. intros _.
intros x' y'. rewrite NUMFST_NUMPAIR, NUMSND1_NUMPAIR. auto.
generalize (ε_spec i). change (Q snd -> snd a (NUMPAIR x y) = y). intro h.
destruct (h a x y) as [h1 h2]. exact h2.
Qed.
(****************************************************************************)
(* Explicit definition of the inverse of NUMSUM. *)
(****************************************************************************)
Definition NUMSUM := fun _17342 : Prop => fun _17343 : nat => @COND nat _17342 (S (Nat.mul (NUMERAL (BIT0 (BIT1 0))) _17343)) (Nat.mul (NUMERAL (BIT0 (BIT1 0))) _17343).
Definition NUMLEFT n := if even n then False else True.
Definition NUMRIGHT n := if even n then n / 2 else (n - 1) / 2.
Lemma NUMLEFT_NUMSUM b n : NUMLEFT (NUMSUM b n) = b.
Proof.
unfold NUMSUM, NUMERAL, BIT1, BIT0, NUMLEFT. destruct (prop_degen b); subst.
rewrite COND_True, even_succ, odd_mul, odd_2. reflexivity.
rewrite COND_False, even_double. reflexivity.
Qed.
Lemma NUMRIGHT_NUMSUM b n : NUMRIGHT (NUMSUM b n) = n.
Proof.