-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtp_dubois_pfm.lp
1343 lines (1186 loc) · 34.8 KB
/
tp_dubois_pfm.lp
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
// Cours et exercices de la découverte de lambdapi
// Inspiré de la version Coq de Catherine Dubois
// (elle-même inspirée de Software Foundations)
// Tout ce qui est en commentaire est du Coq qui compile.
// Ma traduction vers LP suit.
require open AL_library.Notation
////////////////////////////////////
// Avant de prendre la route...
////////////////////////////////////
// Inductive route : Type :=
// | departementale : route
// | nationale : route
// | autoroute : route.
constant symbol route : Set
definition R ≔ τ route
constant symbol departementale : R
constant symbol nationale : R
constant symbol autoroute : R
// Definition agrandir (r : route) :=
// match r with
// | departementale => nationale
// | nationale => autoroute
// | autoroute => autoroute
// end.
symbol agrandir : R ⇒ R
rule agrandir departementale → nationale
and agrandir nationale → autoroute
and agrandir autoroute → autoroute
// Eval compute in (agrandir (agrandir nationale)).
compute agrandir (agrandir nationale)
theorem agrandir_test : // version théorème
π (agrandir (agrandir nationale) = autoroute)
proof
reflexivity
qed
//Inductive terrain : Type :=
//| t_terre : terrain
//| t_route : route -> terrain
//| t_batiment : terrain.
constant symbol terrain : Set
definition Ter ≔ τ terrain
constant symbol t_terre : Ter
constant symbol t_route : R ⇒ Ter
constant symbol t_batiment : Ter
//Check (t_route nationale).
type t_route nationale
/////////////////////////////
// Exercice 1 - De premiers exemples
/////////////////////////////
// ///////////////////////// Booléen
//Check true.
type true
//Print bool.
// ??
//Definition not b :=
// match b with
// | true => false
// | false => true
// end.
symbol not : B ⇒ B
rule not true → false
and not false → true
// Definition andb b1 b2 :=
// match b1 with
// | true => b2
// | false => false
// end.
symbol andb : B ⇒ B ⇒ B
rule andb false _ → false
and andb _ false → false
and andb &b1 true → &b1
and andb true &b2 → &b2
// Definition or b1 b2 :=
// match b1 with
// | true => true
// | false => b2
// end.
symbol or : B ⇒ B ⇒ B
rule or true _ → true
and or _ true → true
and or &b1 false → &b1
and or false &b2 → &b2
// /////////////////////// Entiers naturels
// Inductive nat : Type :=
// | O : nat
// | S : nat -> nat.
//Cf notation.lp
// Check O.
type O
// Check S.
type S
//Notation "0" := O.
//Notation "1" := (S O).
//Cf notation.lp
//Fixpoint plus n m :=
// match n with
// | 0 => m
// | S n' => S (plus n' m)
// end.
symbol plus : N ⇒ N ⇒ N
// Notation "n + m" := (plus n m).
set infix right 6 "+" ≔ plus
rule 0 + &m → &m
and S &v + &m → S (&v + &m)
// Eval compute in (0 + 1).
compute 0+1
// Eval compute in (plus 1 1).
compute plus 1 1
// ///////////////////////////// Liste de booléens
// Inductive list_bool : Type :=
// | nilb : list_bool
// | consb : bool -> list_bool -> list_bool.
constant symbol list_bool : Set
definition Lb ≔ τ list_bool // équivalent à ce qui a été fait avant o:-)
constant symbol nilb : Lb
constant symbol consb : B ⇒ Lb ⇒ Lb
// symbol listb_ind p
//: π (p nilb) ⇒ (∀x l, π (p l) ⇒ π (p (consb x l))) ⇒ ∀l, π (p l)
// Check nilb.
type nilb
//Definition example := consb true (consb false (consb true nilb)).
definition example ≔ consb true (consb false (consb true nilb))
//Check example.
type example
// Fixpoint appb l1 l2 :=
// match l1 with
// | nilb => l2
// | consb a l => consb a (appb l l2)
// end.
symbol appb : Lb ⇒ Lb ⇒ Lb
rule appb nilb &l2 → &l2
and appb (consb &a &l) &l2 → consb &a (appb &l &l2)
// Check appb.
type appb
// ///////////////////////////////////// Suite booléen
//Definition apply_neg (f : _ -> _ -> bool) b1 b2 :=
// f (not b1) (not b2).
definition apply_neg (f: _ ⇒ _ ⇒ B) b1 b2 ≔ f (not b1) (not b2)
// Check apply_neg.
type apply_neg
// Definition nor := apply_neg andb.
definition nor ≔ apply_neg andb
// Check nor.
type nor
// Definition nor :=
// apply_neg (fun b1 => fun b2 => b2).
definition nor_lambda ≔ apply_neg (λ_ b2, b2)
// Eval compute in (nor true false).
compute nor true false
// Definition idb : bool -> bool := fun b => b.
definition idb : B ⇒ B ≔ λb, b
// identique à
// Definition idb (b : bool) := b.
definition idb_bis b:B ≔ b
//Definition my_first := apply_neg (fun b1 => fun b2 => b1).
definition my_first ≔ apply_neg (λb1 _, b1)
//Eval compute in (my_first true false).
compute (my_first true false)
// ////////////////////////////////// Liste polymorphe
// Inductive list (A:Type) : Type :=
// | nil : list A
// | cons : A -> list A -> list A .
// Traduction de manière stricte :
// constant symbol list : Set ⇒ Set
// definition L a ≔ τ (list a)
// constant symbol nil a : L a
// constant symbol cons a : τ a ⇒ L a ⇒ L a
// Mais j'allège un peu :
constant symbol list : Set ⇒ Set
definition L a ≔ τ (list a)
constant symbol nil {a} : L a
constant symbol cons {a} : τ a ⇒ L a ⇒ L a
set infix right 4 "⸬" ≔ cons
assert λA (l:L A) x y, (cons x (cons y l)) ≡ λA (l:L A) x y, (x⸬(y⸬l))
symbol list_ind {a} p
: π(p nil) ⇒ (∀(x:τ a) l, π(p l) ⇒ π( p (x⸬l))) ⇒ ∀l, π(p l)
// Eval compute in (cons nat 1 (cons nat (S 1) (nil nat))).
compute (1⸬(S 1)⸬nil)
// Eval compute in (cons bool true (cons bool false (nil bool))).
compute (true⸬false ⸬nil)
// Definition id (A:Type) (x:A) := x.
definition id A x:A ≔ x
// Fixpoint length (A:Type) (l:list A) :=
// match l with
// | nil _ => 0
// | cons _ _ t => S (length A t)
// end.
symbol length {A} : L A ⇒ N
rule length nil → 0
and length (cons _ &t) → S (length &t)
//Eval compute in (length bool
// (cons bool true
// (cons bool false (nil bool)))).
compute length (true⸬false ⸬nil)
//Fixpoint app (A:Type) l1 l2 :=
// match l1 with
// | nil _ => l2
// | cons _ a l => cons A a (app A l l2)
// end.
symbol app {A} : L A ⇒ L A ⇒ L A
rule app nil &l2 → &l2
and app (cons &x &l) &l2 → cons &x (app &l &l2)
//Definition apply_neg_gen (A:Type) (f:_->_->A) b1 b2 :=
// f (not b1) (not b2).
definition apply_neg_gen A (f : _ ⇒ _ ⇒ A) b1 b2 ≔
f (not b1) (not b2)
// Definition nor2 := apply_neg_gen bool andb.
definition nor2 ≔ apply_neg_gen B andb
// Definition apply_neg_gen2 {A:Type} (f:_->_->A) b1 b2 :=
// f (not b1) (not b2).
definition apply_neg_gen2 {A} (f : _ ⇒ _ ⇒ A) b1 b2 ≔
f (not b1) (not b2)
// Definition nor3 := apply_neg_gen2 andb.
definition nor3 ≔ apply_neg_gen2 andb
// Definition toto (b1 b2 : bool) := 0.
definition toto (_ _ : B) ≔ 0
// Eval compute in apply_neg_gen nat toto true false.
compute apply_neg_gen N toto true false
// //////////////////////////////////////////// Logique sur les booléens
require open amelie.Logic
definition equiv (p q : Prop) ≔ imp p q ∧ imp q p
set infix left 7 "⇔" ≔ equiv
// Check True. (* True : Prop *)
type true // (* true : B *)
// Check True -> False. (* True -> False : Prop *)
type (imp ⊤ ⊥) // imp ⊤ ⊥ : Prop
type _ ⇒ _ // ?201 ⇒ ?199 : TYPE
// Lemma bool_trivial : forall (P : bool -> Prop) b, P b -> P b.
// Admitted.
theorem bool_trivial : ∀p:B⇒Prop,∀b, π (p b) ⇒ π (p b)
//π (imp (P b) (P b))
proof
assume P b H
apply H
qed
// Induction principle on B.
symbol bool_ind : ∀p, π(p true) ⇒ π(p false) ⇒ ∀b, π(p b)
//Lemma bool_ext : forall (P : bool -> Prop), P true /\ P false ->
// forall b, P b.
//Admitted.
theorem bool_ext :
∀p, π (p true ∧ p false) ⇒ ∀b, π(p b)
proof
assume P Hp b
refine bool_ind (λz, P z) _ _ _
apply conj_elim_left _ (P false)
refine Hp
apply conj_elim_right (P true) _
apply Hp
qed
//Lemma bool_ext2 : forall (P : bool -> Prop), P true -> P false ->
// forall b, P b.
//Admitted.
theorem bool_ext2 :
∀p, π (p true) ⇒ π (p false) ⇒ ∀b, π (p b)
proof
assume P HPT HPF b
refine bool_ind (λz, P z) _ _ _
apply HPT
apply HPF
qed
// Première preuve
//Lemma andb_prop : forall b1 b2,
// andb b1 b2 = true -> b1 = true /\ b2 = true.
//Proof.
// intros b1 b2 H.
// (* introduit les hypothèses *)
// split. (* sépare le but en deux sous-buts *)
// - destruct b1. (* raisonnement par cas *)
// + reflexivity. (* true = true *)
// + simpl in H. discriminate H. (* false <> true *)
// - destruct b2. (* raisonnement par cas *)
// + reflexivity. (* true = true *)
// + destruct b1 . (* raisonnement par cas *)
// * simpl in H. discriminate H. (* false <> true *)
// * simpl in H. discriminate H. (* false <> true *)
//Qed.
//set declared "<>"
//definition diff a b ≔ ¬(eq a b)
//set infix left 6 <> ≔ diff
// symbol false_elim p : π ⊥ ⇒ π p
symbol my_P : B ⇒ Prop
rule my_P true → ⊤
and my_P false → ⊥
theorem discr : π (false = true) ⇒ π ⊥
proof
assume H
apply eq_ind false true H my_P
apply I
qed
//theorem andb_prop : // Il me manque un outil pour y arriver avec cet énoncé
//∀b1 b2, π (eq {bool} (andb b1 b2) true) ⇒
// π ((eq {bool} b1 true) ∧ (eq {bool} b2 true))
theorem andb_prop b1 b2
: π (imp (andb b1 b2 = true) ((b1 = true) ∧ (b2 = true)))
proof
assume b1 b2
refine bool_ind (λz, imp (andb z b2 = true) ((z = true) ∧ (b2 = true))) _ _ _
// Case b1 = true
simpl
assume H
apply conj_intro
reflexivity
apply H
// Case b1 = false
simpl
assume H
apply false_elim (false = true ∧ b2 = true)
apply discr apply H
qed
/////////////////////////////////////
// Exercice 2 - De premières preuves
/////////////////////////////////////
//[andb_true_iff : forall b1 b2,andb b1 b2 = true <-> b1 = true /\ b2 = true]
// Montrer [or_true_iff], un résultat similaire sur [or].
theorem andb_true_iff b1 b2
: π ((andb b1 b2 = true) ⇔ ((b1 = true) ∧ (b2 = true)))
proof
assume b1 b2
refine bool_ind (λz, imp ((andb z b2) = true)
(z = true ∧ b2 = true) ∧
imp (z = true ∧ b2 = true)
((andb z b2) = true)) _ _ _
// Case b1 = true
simpl
apply conj_intro
assume H apply conj_intro
reflexivity
apply H
assume H apply conj_elim_right (true = true) apply H
// Case b1 = false
simpl
apply conj_intro
assume H
apply false_elim (false = true ∧ b2 = true)
apply discr apply H
assume H apply conj_elim_left _ (b2 = true) apply H
qed
theorem or_true_iff b1 b2 : π (((or b1 b2) = true) ⇔ (b1 = true ∨ b2 = true))
proof
assume b1 b2
refine bool_ind (λz, imp ((or z b2) = true)
(z = true ∨ b2 = true) ∧
imp (z = true ∨ b2 = true)
((or z b2) = true)) _ _ _
// Case b1 = true
simpl
apply conj_intro
assume H apply disj_intro_left apply H
assume H reflexivity
// Case b1 = false
simpl
apply conj_intro
assume H apply disj_intro_right apply H
assume H apply disj_elim (false = true) (b2 = true) (b2 = true) apply H
assume Hfalse apply false_elim (b2 = true) apply discr apply Hfalse
assume H2 apply H2
qed
// //////////////////////////////////////////// Retour sur les entiers naturels
//Lemma plus_0_n : forall n, 0 + n = n.
//Proof.
//intro n. simpl. reflexivity.
//Qed.
theorem plus_0_n : ∀n, π ((plus O n) = n)
proof
assume n
reflexivity
qed
// Induction principle
symbol nat_ind p : π (p 0) ⇒ (∀x, π (p x) ⇒ π (p (S x))) ⇒ ∀x, π (p x)
// Injective principle
symbol nat_inj : ∀n m, π ((S n) = (S m)) ⇒ π (n = m) // A voir !
//Lemma plus_n_0 : forall n, n + O = n.
//Proof.
//induction n.
//+ simpl. reflexivity.
//+ simpl. rewrite IHn. reflexivity.
//Qed.
theorem plus_n_0 : ∀n, π ((plus n O) = n)
proof
assume n
refine nat_ind (λz, (plus z O) = z) _ _ _
// Case n = O
refine eq_refl O
// Case = S n'
assume n' Hn'
simpl
rewrite Hn'
reflexivity
qed
//Fixpoint mult (n m : nat) : nat :=
// match n with
// | O => 0
// | S n' => m + (mult n' m)
// end.
symbol mult : N ⇒ N ⇒ N
rule mult O _ → 0
and mult (S &v) &m → plus &m (mult &v &m)
//Notation "n * m" := (mult n m).
set infix left 6 "*" ≔ mult
//Lemma mult_0_n : forall n : nat, 0*n=0.
//Proof.
//intros. simpl. reflexivity.
//Qed.
theorem mult_0_n : ∀n, π ((0*n) = 0)
proof
assume n
refine eq_refl O
qed
//Lemma mult_n_0 : forall n, n*0= 0.
//Proof.
//induction n.
//+ apply mult_0_n.
//+ simpl. exact IHn.
// (* ou assumption*)
//Qed.
theorem mult_n_0 : ∀n, π ((n*0) = 0)
proof
refine nat_ind (λz, (z*0) = 0) _ _
// Case n = O
refine eq_refl O
// Case = S n'
assume n' Hn'
simpl
apply Hn'
qed
//Lemma plus_assoc : forall n m p,
// n+(m+p) = n+m+p.
//Proof.
//induction n ; intros m p.
//+ simpl. reflexivity.
//+ simpl. rewrite IHn. reflexivity.
//Qed.
theorem plus_assoc :
//∀(n m p:N), π (eq {nat} (n+(m+p)) (n+m+p))
∀(n m p:N), π ((plus n (plus m p)) = (plus (plus n m) p))
proof
assume n m p
//refine nat_ind (λz, eq {nat} (z+(m+p)) (z+m+p)) _ _ n
refine nat_ind (λz, (plus z (plus m p)) = (plus (plus z m) p)) _ _ n
reflexivity
assume n' Hn'
simpl
rewrite Hn'
reflexivity
qed
//Lemma plus_n_Sm : forall n m,
// n + S m = S (n + m).
//Proof.
//induction n ; intro m ; simpl.
//+ reflexivity.
//+ rewrite IHn. reflexivity.
//Qed.
theorem plus_n_Sm : ∀n m, π ((plus n (S m)) = (S (plus n m)))
proof
assume n m
refine nat_ind (λz, ((plus z (S m)) = (S (plus z m)))) _ _ n
reflexivity
simpl
assume n' Hn'
rewrite Hn'
reflexivity
qed
//Lemma plus_comm : forall n m, n+m=m+n.
//Proof.
//induction n ; intros m0.
//+ simpl. rewrite plus_n_0. reflexivity.
//+ simpl. rewrite IHn. rewrite plus_n_Sm. reflexivity.
//Qed.
theorem plus_comm : ∀n m, π((plus n m) = (plus m n))
proof
assume n m
refine nat_ind (λz, ((plus z m) = (plus m z))) _ _ n
simpl rewrite plus_n_0 reflexivity
simpl assume n' Hn' rewrite Hn' rewrite plus_n_Sm reflexivity
qed
//Lemma mult_n_Sm : forall n m, n*S m=n+n*m.
//Proof.
//induction n ; intros m0 ; simpl.
//+ reflexivity.
//+ rewrite IHn.
// do 2 (rewrite plus_assoc).
// rewrite plus_comm with (n:=n) (m := m0). reflexivity.
//Qed.
//theorem mult_n_Sm : ∀n m, π (eq {nat} (n*S m) (n+n*m))
theorem mult_n_Sm : ∀n m, π ((mult n (S m)) = (plus n (mult n m)))
proof
assume n m
refine nat_ind (λz, ((mult z (S m)) = (plus z (mult z m)))) _ _ n
reflexivity
assume n' Hn' simpl rewrite Hn'
rewrite plus_assoc rewrite plus_assoc rewrite plus_comm n' m reflexivity
qed
//Lemma mult_comm : forall n m, n *m=m*n.
//Proof.
//induction n ; intros m0 ; simpl.
//+ rewrite mult_n_0. reflexivity.
//+ rewrite IHn. rewrite mult_n_Sm. reflexivity.
//Qed.
theorem mult_comm : ∀n m, π ((mult n m) = (mult m n))
proof
assume n m
refine nat_ind (λz, ((mult z m) = (mult m z))) _ _ n
rewrite mult_n_0 reflexivity
assume n' Hn' simpl rewrite Hn'
rewrite mult_n_Sm reflexivity
qed
//Lemma mult_plus_distr_r : forall n m p,
// (n+m)*p = n*p+m*p.
//Proof.
//induction n ; intros m p; simpl.
//+ reflexivity.
//+ rewrite IHn. apply plus_assoc.
//Qed.
theorem mult_plus_distr_r :
∀n m p, π ((mult (plus n m) p) = (plus (mult n p) (mult m p)))
proof
assume n m p
refine nat_ind
(λz, ((mult (plus z m) p) = (plus (mult z p) (mult m p)))) _ _ n
reflexivity
assume n' Hn' simpl rewrite Hn'
apply plus_assoc p (mult n' p) (mult m p) // pas inféré
qed
//Lemma mult_assoc : forall n m p, n*(m*p) = (n*m)*p.
//Proof.
//induction n ; intros m p; simpl.
//+ reflexivity.
//+ rewrite IHn. rewrite mult_plus_distr_r. reflexivity.
//Qed.
theorem mult_assoc : ∀ n m p, π ((n*(m*p)) = ((n*m)*p))
proof
assume n m p
refine nat_ind (λz, ((z*(m*p)) = ((z*m)*p))) _ _ n
reflexivity
simpl assume n' Hn' // simpl
rewrite Hn'
rewrite mult_plus_distr_r // m (mult n' m) p
reflexivity
qed
// Prouver directement
//Lemma mult_plus_distr_l : forall n m p,
// n*(m+p)=n*m+n*p.
//Proof.
//induction n; intros m p ; simpl.
//+ reflexivity.
//+ rewrite IHn.
// rewrite plus_comm with (n:=p) (m:=n*p).
// rewrite <- plus_assoc with (n:=m) (m:=n*m) (p:=n*p+p).
// rewrite plus_comm with (n:=m) (m:=n*m + (n*p + p)).
// do 2 (rewrite plus_assoc).
// rewrite plus_comm with (n:=m) (m:=p).
// rewrite <- plus_assoc with (n:=p + m) (m:=n*m) (p:=n*p).
// rewrite plus_comm with (n:=p + m) (m:=n*m + n*p).
// rewrite plus_assoc. reflexivity.
//Qed.
theorem mult_plus_distr_l :
∀n m p, π ((mult n (plus m p)) = (plus (mult n m) (mult n p)))
proof
assume n m p
refine nat_ind (λz, (mult z (plus m p)) = (plus (mult z m) (mult z p))) _ _ n
reflexivity
simpl assume n' IHn' rewrite IHn'
rewrite plus_comm p (n'*p)
symmetry
//rewrite plus_assoc m (mult n' m) (plus (mult n' p) p)
//symmetry
//rewrite plus_comm p (n'*p)
//symmetry
//rewrite plus_comm m (plus (mult n' m) (plus (mult n' p) p))
//rewrite plus_assoc rewrite plus_assoc
//rewrite plus_comm m p
//rewrite <- plus_assoc (p + m) (n*m) (n*p)
//rewrite plus_comm (p + m) (n*m + n*p)
//rewrite plus_assoc reflexivity
//qed // @TODO_1
admit
// Prouver en utilisant les lemmes précédents
// (mais bien sûr pas [mult_plus_distr_l])
//Lemma mult_plus_distr_l_bis : forall n m p,
// n*(m+p)=n*m+n*p.
//Proof.
//intros n m p.
//rewrite mult_comm with (n := n) (m := m).
//rewrite mult_comm with (n := n) (m := p).
//rewrite <- mult_plus_distr_r.
//apply mult_comm.
//Qed.
theorem mult_plus_distr_l_bis :
∀n m p, π ((mult n (plus m p)) = (plus (mult n m) (mult n p)))
proof
assume n m p
rewrite mult_comm n m
rewrite mult_comm n p
rewrite mult_comm n (plus m p)
symmetry
rewrite mult_plus_distr_r
reflexivity
qed
////////////////////////////
// Exercice 3 - Un peu de logique !
////////////////////////////
//Lemma impl_and : forall P Q R : Prop, (P->Q->R) -> (P /\ Q -> R).
//Proof.
//intros P Q R Hyp1 Hyp2.
//destruct Hyp2 as [Hp Hq].
//apply Hyp1 ; assumption.
//Qed.
theorem impl_and : ∀P Q R : Prop, π (imp (imp P (imp Q R)) (imp (P ∧ Q) R))
proof
assume P Q R Himpl Hand
apply Himpl
apply conj_elim_left P Q apply Hand
apply conj_elim_right P Q apply Hand
qed
//Lemma conj_impl : forall P Q R : Prop, (P /\ Q -> R) -> (P -> Q -> R).
//Proof.
//intros P Q R Hyp1 H1 H2.
//apply Hyp1.
//split ; assumption.
//Qed.
theorem conj_impl : ∀P Q R : Prop, π (imp (imp (P ∧ Q) R) (imp P (imp Q R)))
proof
assume P Q R Hand HP HQ
// Goal π (imp (P ∧ Q) R)
apply Hand
// Goal π (P ∧ Q)
apply conj_intro P Q
apply HP
apply HQ
qed
//Lemma conj_impl_iff : forall P Q R : Prop, (P /\ Q -> R) <-> (P -> Q -> R).
//Proof.
//split.
//+ apply conj_impl.
//+ apply impl_and.
//Qed.
theorem conj_impl_iff : ∀P Q R : Prop,
π ((imp (P ∧ Q) R) ⇔ (imp P (imp Q R)))
proof
assume P Q R
apply conj_intro
simpl
apply conj_impl P Q R
apply impl_and P Q
qed
//Lemma impl_not : forall P : Prop, P -> ~ ~ P.
//Proof.
//intros P Hyp.
//unfold not.
//intro Hyp1.
//apply Hyp1.
//assumption.
//Qed.
theorem impl_not : ∀P : Prop, π P ⇒ π (¬ (¬ P))
proof
assume P HP
//apply neg_intro (¬ P)
assume HnotP
//apply neg_elim P
apply HnotP
apply HP
qed
//Lemma modus_ponens : forall P Q : Prop, P -> (P -> Q) -> Q.
//Proof.
//intros P Q H1 H2.
//apply H2.
//assumption.
//Qed.
theorem modus_ponens : ∀P Q : Prop, π P ⇒ (π P ⇒ π Q) ⇒ π Q
proof
assume P Q HP Himp
apply Himp
apply HP
qed
// en utilisant [modus_ponens]
//Lemma impl_neg_bis : forall P : Prop, P -> ~ ~ P.
//Proof.
//intro P. unfold not.
//apply modus_ponens.
//Qed.
theorem impl_neg_bis : ∀P : Prop, π P ⇒ π (¬ (¬ P))
proof
assume P
refine modus_ponens P ⊥
qed
// à partir de [impl_not]
//Lemma P_notP_contradiction : forall P : Prop, ~ (P /\ ~ P).
//Proof.
//intro P.
//unfold not .
//(*apply impl_and.
//apply modus_ponens.*)
//intro H1.
//destruct H1 as [HH1 HH2].
//apply HH2. assumption.
//Qed.
theorem P_notP_contradiction : ∀P : Prop, π (¬(P ∧ ¬P))
proof
assume P Hand
//apply neg_intro (P ∧ ¬P)
apply neg_elim P
//apply conj_elim_right P _ simpl apply Hand
//apply conj_elim_left _ (¬P) apply Hand
admit // @TODO 2 : problème avec les nouvelles règles
//Lemma neg_and : forall P Q : Prop, P \/ Q -> ~(~ P /\ ~Q).
//Proof.
//Admitted.
theorem neg_and : ∀P Q : Prop, π (P ∨ Q) ⇒ π (¬(¬P ∧ ¬Q))
proof
assume P Q Hor Hand
refine disj_elim P Q ⊥ _ _ _
// Case π (P ∨ Q)
apply Hor
// Case π P ⇒ π ⊥
assume HP
//refine conj_elim_left (¬P) (¬Q) _ apply Hand
//apply HP
// Case π Q ⇒ π ⊥
//assume HQ apply neg_elim Q
//refine conj_elim_right (¬P) (¬Q) _ apply Hand
//apply HQ
admit // @TODO 3 probleme avec les nouvelles règles
//Lemma exists_neg_forall : forall P : nat -> Prop,
//(exists n, P n) -> ~(forall n,~ (P n)).
//Proof.
//intros P H H1.
//destruct H as [n0 H0].
//specialize (H1 n0).
//contradiction.
//Qed.
theorem exists_neg_forall :
∀(P : N ⇒ Prop), π(∃P) ⇒ π (¬(forall (λm:N, ¬ (P m))))
proof
assume P Hexists Hforall
//apply H2
apply ex_elim {nat} P ⊥
apply Hexists
//apply Hforall
assume x H apply Hforall x H
qed
//@DONE
////////////////////////
// Exercice 4 - Retour que les listes polymorphes
////////////////////////
//Check list.
type list
// Notation "[]" := nil. //@TODO 4
// set declared "[]"
// rule [] → nil
//Notation "[ x ]" := (cons x nil).
//Notation "[ x ; y ; .. ; z ]" := (cons x (cons y .. (cons z nil) ..)).
// possible ?
// Montrer que le nombre d'éléments de la concaténation de deux listes est
// la somme des nombres des éléments de chacune.
// Lemma equal_length : forall l1 l2,
// (length l1) + (length l2) = length (app l1 l2).
// Proof.
// induction l1.
// * simpl. reflexivity.
// * simpl. intro l2. rewrite IHl1. reflexivity.
// Defined.
theorem sum_length :
∀A (l1 l2 : L A), π ((plus (length l1) (length l2)) = (length (app l1 l2)))
proof
assume A l1 l2
apply list_ind
(λz, (plus (length z) (length l2)) = (length (app z l2))) _ _ l1
reflexivity
assume x l2 IHl1
simpl rewrite IHl1 reflexivity
qed
// Définir la fonction qui renverse une liste (on utilisera la fonction
// précédente app)
// et montrer que reverse(reverse l) = l pour tout l, par induction sur l
symbol reverse {A} : L A ⇒ L A
rule reverse nil → nil
and reverse (cons &v &q) → app (reverse &q) (cons &v nil)
// Lemma app_nil : forall l, app l [] = l.
// Proof.
// induction l.
// + simpl. reflexivity.
// + simpl. rewrite IHl. reflexivity.
// Defined.
theorem app_nil : ∀A (l:L A), π (app l nil = l)
proof
assume A l
apply list_ind (λz, app z nil = z) _ _
// 0. π (app nil nil = nil)
reflexivity
// 1. ∀(x:τ A) (l0:τ (list A)),
// π (app l0 nil = l0) ⇒ π (app (x ⸬ l0) nil = (x ⸬ l0))
assume x l IHl
simpl
rewrite IHl
reflexivity
qed
// Lemma app_assoc : forall l1 l2 l3,
// app (app l1 l2) l3 = app l1 (app l2 l3).
// Proof.
// induction l1.
// + simpl. reflexivity.
// + simpl. intros l2 l3. rewrite IHl1. reflexivity.
// Defined.
theorem app_assoc :
∀A (l1 l2 l3 : L A), π (app (app l1 l2) l3 = app l1 (app l2 l3))
proof
assume A l1 l2 l3
apply list_ind (λz, app (app z l2) l3 = app z (app l2 l3)) _ _ l1
// 0. π (app (app nil l2) l3 = app nil (app l2 l3))
reflexivity
// 1. ∀(x:τ A) (l:τ (list A)), π (app (app l l2) l3 = app l (app l2 l3)) ⇒
// π (app (app (x ⸬ l) l2) l3 = app (x ⸬ l) (app l2 l3))
assume A
simpl
assume l IHl
rewrite IHl reflexivity
qed
// Lemma special_case : forall l a, cons a l = app [a] l.
// Proof.
// intro l. simpl. reflexivity.
// Defined.
theorem special_case : ∀A (l:L A) a, π (app (cons a nil) l = cons a l)
proof
assume A l x simpl reflexivity
qed
// Lemma reverse_bis_concat : forall l1 l2,
// reverse_bis (app l1 l2) =
// app (reverse_bis l2) (reverse_bis l1).
// Proof.
// induction l1.
// + simpl. intro l2. rewrite app_nil. reflexivity.
// + simpl. intro l2.
// rewrite IHl1.
// apply app_assoc.
// Defined.
theorem reverse_concat :
∀A (l1 l2:L A), π ((reverse (app l1 l2)) = app (reverse l2) (reverse l1))
proof
assume A l1 l2
apply list_ind
(λz, (reverse (app z l2)) = (app (reverse l2) (reverse z))) _ _ l1
simpl
//refine eq_sym (reverse l2) (app (reverse l2) nil)
rewrite app_nil A (reverse l2) reflexivity
simpl assume x l2 IHl1 rewrite IHl1
apply app_assoc A (reverse l2) (reverse l3) (cons x nil)
qed
// Lemma fixpoint_prop_bis : forall l,
// reverse_bis (reverse_bis l) = l.
// Proof.
// induction l.
// * reflexivity.
// * rewrite special_case.
// repeat rewrite reverse_bis_concat.
// simpl. rewrite IHl. reflexivity.
// Defined.
theorem fixpoint_reverse : ∀A (l:L A), π (reverse (reverse l) = l)
proof
assume A l
apply list_ind (λz, reverse (reverse z) = z) _ _
// 0. π (reverse (reverse nil) = nil)
reflexivity
// 1. ∀(x:τ A) (l0:τ (list A)),
// π (reverse (reverse l0) = l0) ⇒ π (reverse (reverse (x ⸬ l0)) = (x ⸬ l0))
assume x l0 IHl
simpl rewrite reverse_concat A //(reverse l0) (cons x nil)
simpl rewrite IHl reflexivity
qed
//////////////////////////
// Exercice 5 - Prédicat inductif
//////////////////////////
//Definition is_even n := exists p, n = 2 * p.
definition is_even n ≔ ∃(λp, n = (mult 2 p))