-
Notifications
You must be signed in to change notification settings - Fork 22
/
RecTutorial.v
1107 lines (824 loc) · 24.3 KB
/
RecTutorial.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
Require Import Arith.
Require Import Vector.
Require Import List.
Require Import Extraction.
Theorem zero_leq_three: 0 <= 3.
Proof.
constructor 2.
constructor 2.
constructor 2.
constructor 1.
Qed.
Print zero_leq_three.
Lemma zero_leq_three': 0 <= 3.
repeat constructor.
Qed.
Lemma zero_lt_three : 0 < 3.
Proof.
unfold lt; repeat constructor.
Qed.
(** Tests :
*)
Check nil (A:= nat -> nat).
Check fun A: Type => (cons (A:=A)).
Check cons 3 (cons 2 nil).
Check Vector.nil nat.
Check fun (A:Type) (a:A) => Vector.cons _ a _ (Vector.nil _).
Check Vector.cons _ 5 _ (Vector.cons _ 3 _ (Vector.nil _)).
Lemma eq_3_3 : 2 + 1 = 3.
Proof. reflexivity. Qed.
(* Tests :
Print eq_3_3.
*)
Lemma eq_proof_proof : refl_equal (2*6) = refl_equal (3*4).
Proof. reflexivity. Qed.
(* Tests :
Print eq_proof_proof.
*)
Lemma eq_lt_le : ( 2 < 4) = (3 <= 4).
Proof. reflexivity. Qed.
Lemma eq_nat_nat : nat = nat.
Proof. reflexivity. Qed.
Lemma eq_Set_Set : Set = Set.
Proof. reflexivity. Qed.
Lemma eq_Type_Type : Type = Type.
Proof. reflexivity. Qed.
Require Import ZArith.
Require Import Compare_dec.
Definition max (n p :nat) := match le_lt_dec n p with
| left _ => p
| right _ => n
end.
Theorem le_max : forall n p, n <= p -> max n p = p.
Proof.
intros n p ; unfold max ; case (le_lt_dec n p); simpl.
- trivial.
- intros; absurd (p < p); eauto with arith.
Qed.
(**
Extraction max.
*)
Inductive tree(A:Type) : Type :=
node : A -> forest A -> tree A
with
forest (A: Type) : Type :=
nochild : forest A |
addchild : tree A -> forest A -> forest A.
Inductive
even : nat->Prop :=
evenO : even O |
evenS : forall n, odd n -> even (S n)
with
odd : nat->Prop :=
oddS : forall n, even n -> odd (S n).
Lemma odd_49 : odd (7 * 7).
Proof.
repeat constructor.
Qed.
Definition nat_case :=
fun (Q : Type)(g0 : Q)(g1 : nat -> Q)(n:nat) =>
match n return Q with
| 0 => g0
| S p => g1 p
end.
(* Tests :
Eval cbn in (nat_case nat 0 (fun p => p) 34).
Eval cbn in (fun g0 g1 => nat_case nat g0 g1 34).
Eval cbn in (fun g0 g1 => nat_case nat g0 g1 0).
*)
Definition pred (n:nat) := match n with O => O | S m => m end.
Definition xorb (b1 b2:bool) :=
match b1, b2 with
| false, true | true, false => true
| _ , _ => false
end.
Definition pred_spec (n:nat) := {m:nat | n=0 /\ m=0 \/ n = S m}.
Definition predecessor : forall n:nat, pred_spec n.
Proof.
intro n;case n.
- exists 0;auto.
- intro n0;exists n0; now right.
Defined.
(**
Extraction predecessor.
*)
Theorem nat_expand :
forall n:nat, n = match n with 0 => 0 | S p => S p end.
Proof.
intro n;case n;simpl;auto.
Qed.
(** Tests:
Check (fun p:False => match p return 2=3 with end).
*)
Section equality_elimination.
Variables (A: Type)
(a b : A)
(p : a = b)
(Q : A -> Type).
(** Tests:
Check (fun H : Q a =>
match p in (eq _ y) return Q y with
refl_equal => H
end).
*)
End equality_elimination.
Theorem eq_trans : forall n m p:nat, n = m -> m = p -> n = p.
Proof.
intros n m p eqnm; now case eqnm.
Qed.
Lemma Rw : forall x y: nat, y = y * x -> y * x * x = y.
Proof.
intros x y e; do 2 rewrite <- e; reflexivity.
Qed.
Lemma mult_distr_S : forall n p : nat, n * p + p = S n * p.
Proof.
simpl; auto with arith.
Qed.
Lemma four_n : forall n:nat, n+n+n+n = 4*n.
Proof.
intro n; pattern n at 1.
rewrite <- Nat.mul_1_l.
now repeat rewrite mult_distr_S.
Qed.
Section Le_case_analysis.
Variables (n p : nat)
(H : n <= p)
(Q : nat -> Prop)
(H0 : Q n)
(HS : forall m, n <= m -> Q (S m)).
(** Tests:
Check (
match H in (_ <= q) return (Q q) with
| le_n _ => H0
| le_S _ m Hm => HS m Hm
end
).
*)
End Le_case_analysis.
Lemma predecessor_of_positive : forall n, 1 <= n -> exists p:nat, n = S p.
Proof.
intros n H; case H.
- exists 0; trivial.
- intros m Hm; exists m;trivial.
Qed.
Definition Vtail_total
(A : Type) (n : nat) (v : t A n) : t A (pred n):=
match v in (Vector.t _ n0) return (Vector.t A (pred n0)) with
| Vector.nil _ => Vector.nil A
| Vector.cons _ _ n0 v0 => v0
end.
Definition Vtail' (A:Type)(n:nat)(v:Vector.t A n) : Vector.t A (pred n).
Proof.
case v.
- exact (Vector.nil A).
- auto.
Defined.
(** Demo about positive condition
Inductive Lambda : Set :=
lambda : (Lambda -> False) -> Lambda.
Error: Non strictly positive occurrence of "Lambda" in
"(Lambda -> False) -> Lambda"
*)
Section Paradox.
Variable Lambda : Set.
Variable lambda : (Lambda -> False) ->Lambda.
Variable matchL : Lambda -> forall Q:Prop, ((Lambda ->False) -> Q) -> Q.
(*
understand matchL Q l (fun h : Lambda -> False => t)
as match l return Q with lambda h => t end
*)
Definition application (f x: Lambda) :False :=
matchL f False (fun h => h x).
Definition Delta : Lambda := lambda (fun x : Lambda => application x x).
Definition loop : False := application Delta Delta.
Theorem two_is_three : 2 = 3.
Proof.
elim loop.
Qed.
End Paradox.
Require Import ZArith.
Inductive itree : Set :=
| ileaf : itree
| inode : Z-> (nat -> itree) -> itree.
Definition isingle l := inode l (fun i => ileaf).
Definition t1 := inode 0 (fun n => isingle (Z_of_nat (2*n))).
Definition t2 := inode 0
(fun n : nat =>
inode (Z_of_nat n)
(fun p => isingle (Z_of_nat (n*p)))).
Inductive itree_le : itree-> itree -> Prop :=
| le_leaf : forall t, itree_le ileaf t
| le_node : forall l l' s s',
Z.le l l' ->
(forall i, exists j:nat, itree_le (s i) (s' j)) ->
itree_le (inode l s) (inode l' s').
Theorem itree_le_trans :
forall t t', itree_le t t' ->
forall t'', itree_le t' t'' -> itree_le t t''.
induction t.
- constructor 1.
- intros t'; case t'.
+ inversion 1.
+ intros z0 i0 H0; intro t'';case t''.
* inversion 1.
* intros z1 i1 H1; inversion_clear H1.
constructor 2.
inversion_clear H0;eauto with zarith.
inversion_clear H0.
intro i2; case (H4 i2).
intros x H5; generalize (H i2 _ H5); intro H6.
case (H3 x);intros x0 Hx0.
exists x0;auto.
Qed.
Inductive itree_le' : itree-> itree -> Prop :=
| le_leaf' : forall t, itree_le' ileaf t
| le_node' : forall l l' s s' g,
Z.le l l' ->
(forall i, itree_le' (s i) (s' (g i))) ->
itree_le' (inode l s) (inode l' s').
Lemma t1_le_t2 : itree_le t1 t2.
Proof.
unfold t1, t2; constructor; auto with zarith.
intro i; exists (2 * i); unfold isingle; constructor.
- auto with zarith.
- exists i;constructor.
Qed.
Lemma t1_le'_t2 : itree_le' t1 t2.
Proof.
unfold t1, t2; constructor 2 with (fun i : nat => 2 * i);
auto with zarith.
unfold isingle;
intro i ; constructor 2 with (fun i :nat => i); auto with zarith.
constructor .
Qed.
Require Import List.
Inductive ltree (A:Set) : Set :=
lnode : A -> list (ltree A) -> ltree A.
Inductive prop : Prop :=
prop_intro : Prop -> prop.
Lemma prop_inject: prop.
Proof prop_intro prop.
Inductive ex_Prop (P : Prop -> Prop) : Prop :=
exP_intro : forall X : Prop, P X -> ex_Prop P.
Lemma ex_Prop_inhabitant : ex_Prop (fun P => P -> P).
Proof.
now exists (ex_Prop (fun P => P -> P)).
Qed.
(*
Check (fun (P:Prop->Prop)(p: ex_Prop P) =>
match p with exP_intro X HX => X end).
Error:
Incorrect elimination of "p" in the inductive type
"ex_Prop", the return type has sort "Type" while it should be
"Prop"
Elimination of an inductive object of sort "Prop"
is not allowed on a predicate in sort "Type"
because proofs can be eliminated only to build proofs
*)
(*
Check (match prop_inject with (prop_intro P p) => P end).
Error:
Incorrect elimination of "prop_inject" in the inductive type
"prop", the return type has sort "Type" while it should be
"Prop"
Elimination of an inductive object of sort "Prop"
is not allowed on a predicate in sort "Type"
because proofs can be eliminated only to build proofs
prop_inject =
prop_inject = prop_intro prop (fun H : prop => H)
: prop
Inductive typ : Type :=
typ_intro : Type -> typ.
Definition typ_inject: typ.
split.
exact typ.
Defined.
Error: Universe Inconsistency.
Abort.
Inductive aSet : Set :=
aSet_intro: Set -> aSet.
User error: Large non-propositional inductive types must be in Type
*)
Inductive ex_Set (P : Set -> Prop) : Type :=
exS_intro : forall X : Set, P X -> ex_Set P.
Module Bad.
Inductive comes_from_the_left {P Q:Prop} : P \/ Q -> Prop :=
c1 : forall p, comes_from_the_left (or_introl (A:=P) Q p).
Goal comes_from_the_left (or_introl True I).
split.
Qed.
Goal ~ comes_from_the_left (or_intror True I).
intro H.
(* discriminate H.
*)
Abort.
End Bad.
(**
Definition comes_from_the_left (P Q:Prop)(H:P \/ Q): Prop :=
match H with
| or_introl p => True
| or_intror q => False
end.
Error:
Incorrect elimination of "H" in the inductive type
"or", the return type has sort "Type" while it should be
"Prop"
Elimination of an inductive object of sort "Prop"
is not allowed on a predicate in sort "Type"
because proofs can be eliminated only to build proofs
*)
Definition comes_from_the_left_sumbool
(P Q:Prop)(x:{P}+{Q}): Prop :=
match x with
| left p => True
| right q => False
end.
Close Scope Z_scope.
Definition Is_zero (x:nat):= match x with
| 0 => True
| _ => False
end.
Lemma O_is_zero : forall m, m = 0 -> Is_zero m.
Proof.
intros m H; subst m.
(*
============================
Is_ zero 0
*)
simpl;trivial.
Qed.
Theorem S_is_not_O : forall n, S n <> 0.
red; intros n Hn.
apply O_is_zero with (m := S n).
assumption.
Qed.
Theorem disc2 : forall n, S (S n) <> 1.
Proof.
intros n Hn; discriminate.
Qed.
Theorem disc3 : forall n, S (S n) = 0 -> forall Q:Prop, Q.
Proof.
intros n Hn Q.
discriminate.
Qed.
Lemma inj_pred : forall n m, n = m -> pred n = pred m.
Proof.
intros n m eq_n_m.
rewrite eq_n_m.
trivial.
Qed.
Theorem inj_succ : forall n m, S n = S m -> n = m.
Proof.
intros n m eq_Sn_Sm.
apply inj_pred with (n:= S n) (m := S m); assumption.
Qed.
Lemma list_inject : forall (A:Set)(a b :A)(l l':list A),
a :: b :: l = b :: a :: l' -> a = b /\ l = l'.
Proof.
intros A a b l l' e.
injection e.
auto.
Qed.
Theorem not_le_Sn_0 : forall n:nat, ~ (S n <= 0).
Proof.
red; intros n H.
case H.
Abort.
Lemma not_le_Sn_0_with_constraints :
forall n p , S n <= p -> p = 0 -> False.
Proof.
intros n p H; case H ;
intros; discriminate.
Qed.
Theorem not_le_Sn_0 : forall n:nat, ~ (S n <= 0).
Proof.
red; intros n H.
eapply not_le_Sn_0_with_constraints; eauto.
Qed.
Theorem not_le_Sn_0' : forall n:nat, ~ (S n <= 0).
Proof.
red; intros n H ; inversion H.
Qed.
Derive Inversion le_Sn_0_inv with (forall n :nat, S n <= 0).
Check le_Sn_0_inv.
Theorem le_Sn_0'' : forall n p : nat, ~ S n <= 0 .
Proof.
intros n p H;
inversion H using le_Sn_0_inv.
Qed.
Derive Inversion_clear le_Sn_0_inv' with (forall n :nat, S n <= 0).
Check le_Sn_0_inv'.
Theorem le_reverse_rules :
forall n m:nat, n <= m ->
n = m \/
exists p, n <= p /\ m = S p.
Proof.
(* intros n m H; inversion H.
left;trivial.
right; exists m0; split; trivial.
Restart.
*)
intros n m H; inversion_clear H.
left;trivial.
right; exists m0; split; trivial.
Qed.
Inductive ArithExp : Set :=
Zero : ArithExp
| Succ : ArithExp -> ArithExp
| Plus : ArithExp -> ArithExp -> ArithExp.
Inductive RewriteRel : ArithExp -> ArithExp -> Prop :=
RewSucc : forall e1 e2 :ArithExp,
RewriteRel e1 e2 -> RewriteRel (Succ e1) (Succ e2)
| RewPlus0 : forall e:ArithExp,
RewriteRel (Plus Zero e) e
| RewPlusS : forall e1 e2:ArithExp,
RewriteRel e1 e2 ->
RewriteRel (Plus (Succ e1) e2) (Succ (Plus e1 e2)).
Fixpoint plus (n p:nat) {struct n} : nat :=
match n with
| 0 => p
| S m => S (plus m p)
end.
Fixpoint plus' (n p:nat) {struct p} : nat :=
match p with
| 0 => n
| S q => S (plus' n q)
end.
Fixpoint plus'' (n p:nat) {struct n} : nat :=
match n with
| 0 => p
| S m => plus'' m (S p)
end.
Fixpoint even_test_0 (n:nat) : bool :=
match n
with 0 => true
| 1 => false
| S (S p) => even_test_0 p
end.
Fixpoint even_test (n:nat) : bool :=
match n
with
| 0 => true
| S p => odd_test p
end
with odd_test (n:nat) : bool :=
match n
with
| 0 => false
| S p => even_test p
end.
Eval simpl in even_test.
Eval simpl in (fun x : nat => even_test x).
Eval simpl in (fun x : nat => plus 5 x).
Eval simpl in (fun x : nat => even_test (plus 5 x)).
Eval simpl in (fun x : nat => even_test (plus x 5)).
Section Principle_of_Induction.
Variable P : nat -> Prop.
Hypothesis base_case : P 0.
Hypothesis inductive_step : forall n:nat, P n -> P (S n).
Fixpoint nat_ind (n:nat) : (P n) :=
match n return P n with
| 0 => base_case
| S m => inductive_step m (nat_ind m)
end.
End Principle_of_Induction.
Scheme Even_induction := Minimality for even Sort Prop
with Odd_induction := Minimality for odd Sort Prop.
Theorem even_plus_four : forall n:nat, even n -> even (4+n).
Proof.
intros n H.
elim H using Even_induction with (P0 := fun n => odd (4+n));
simpl;repeat constructor;assumption.
Qed.
Section Principle_of_Double_Induction.
Variable P : nat -> nat ->Prop.
Hypothesis base_case1 : forall x:nat, P 0 x.
Hypothesis base_case2 : forall x:nat, P (S x) 0.
Hypothesis inductive_step : forall n m:nat, P n m -> P (S n) (S m).
Fixpoint nat_double_ind (n m:nat){struct n} : P n m :=
match n, m return P n m with
| 0 , x => base_case1 x
| (S x), 0 => base_case2 x
| (S x), (S y) => inductive_step x y (nat_double_ind x y)
end.
End Principle_of_Double_Induction.
Section Principle_of_Double_Recursion.
Variable P : nat -> nat -> Set.
Hypothesis base_case1 : forall x:nat, P 0 x.
Hypothesis base_case2 : forall x:nat, P (S x) 0.
Hypothesis inductive_step : forall n m:nat, P n m -> P (S n) (S m).
Fixpoint nat_double_rec (n m:nat){struct n} : P n m :=
match n, m return P n m with
| 0 , x => base_case1 x
| (S x), 0 => base_case2 x
| (S x), (S y) => inductive_step x y (nat_double_rec x y)
end.
End Principle_of_Double_Recursion.
Definition min : nat -> nat -> nat :=
nat_double_rec (fun (x y:nat) => nat)
(fun (x:nat) => 0)
(fun (y:nat) => 0)
(fun (x y r:nat) => S r).
Eval compute in (min 5 8).
Eval compute in (min 8 5).
Lemma not_circular : forall n:nat, n <> S n.
Proof.
intro n.
apply nat_ind with (P:= fun n => n <> S n).
discriminate.
red; intros n0 Hn0 eqn0Sn0;injection eqn0Sn0;trivial.
Qed.
Definition eq_nat_dec : forall n p:nat , {n=p}+{n <> p}.
Proof.
intros n p.
(* apply nat_double_rec with (P:= fun (n q:nat) => {q=p}+{q <> p}).
Undo. *)
pattern p, n.
elim n using nat_double_rec.
destruct x; auto.
destruct x; auto.
intros n0 m H; case H.
intro eq; rewrite eq ; auto.
intro neg; right; red ; injection 1; auto.
Defined.
Definition eq_nat_dec' : forall n p:nat, {n=p}+{n <> p}.
decide equality.
Defined.
Print Acc.
(*
Fixpoint div (x y:nat){struct x}: nat :=
if eq_nat_dec x 0
then 0
else if eq_nat_dec y 0
then x
else S (div (x-y) y).
Error:
Recursive definition of div is ill-formed.
In environment
div : nat -> nat -> nat
x : nat
y : nat
_ : x <> 0
_ : y <> 0
Recursive call to div has principal argument equal to
"x - y"
instead of a subterm of x
*)
Lemma minus_smaller_S: forall x y:nat, x - y < S x.
Proof.
intros x y; pattern y, x;
elim x using nat_double_ind.
destruct x0; auto with arith.
simpl; auto with arith.
simpl; auto with arith.
Qed.
Lemma minus_smaller_positive : forall x y:nat, x <>0 -> y <> 0 ->
x - y < x.
Proof.
destruct x; destruct y;
( simpl;intros; apply minus_smaller_S ||
intros; absurd (0=0); auto).
Qed.
Definition minus_decrease : forall x y:nat, Acc lt x ->
x <> 0 ->
y <> 0 ->
Acc lt (x-y).
Proof.
intros x y H; case H.
intros H0 H1 H2;apply H0; apply minus_smaller_positive; assumption.
Defined.
Print minus_decrease.
Definition div_aux : forall (x y:nat)(H: Acc lt x), nat.
fix div_aux 3.
intros.
refine (if eq_nat_dec x 0
then 0
else if eq_nat_dec y 0
then y
else div_aux (x-y) y _).
apply (minus_decrease x y H);assumption.
Defined.
Print div_aux.
(*
div_aux =
(fix div_aux (x y : nat) (H : Acc lt x) {struct H} : nat :=
match eq_nat_dec x 0 with
| left _ => 0
| right _ =>
match eq_nat_dec y 0 with
| left _ => y
| right _0 => div_aux (x - y) y (minus_decrease x y H _ _0)
end
end)
: forall x : nat, nat -> Acc lt x -> nat
*)
Require Import Wf_nat.
Definition div x y := div_aux x y (lt_wf x).
Extraction div.
(*
let div x y =
div_aux x y
*)
Extraction div_aux.
(*
let rec div_aux x y =
match eq_nat_dec x O with
| Left -> O
| Right ->
(match eq_nat_dec y O with
| Left -> y
| Right -> div_aux (minus x y) y)
*)
Lemma vector0_is_vnil : forall (A:Set)(v:Vector.t A 0), v = Vector.nil A.
Proof.
intros A v;inversion v.
Abort.
(*
Lemma vector0_is_vnil_aux : forall (A:Set)(n:nat)(v:vector A n),
n= 0 -> v = Vnil A.
Toplevel input, characters 40281-40287
> Lemma vector0_is_vnil_aux : forall (A:Set)(n:nat)(v:vector A n), n= 0 -> v = Vnil A.
> ^^^^^^
Error: In environment
A : Set
n : nat
v : vector A n
e : n = 0
The term "Vnil A" has type "vector A 0" while it is expected to have type
"vector A n"
*)
Require Import JMeq.
Lemma vector0_is_vnil_aux : forall (A:Set)(n:nat)(v:Vector.t A n),
n= 0 -> JMeq v (Vector.nil A).
Proof.
destruct v.
auto.
intro; discriminate.
Qed.
Lemma vector0_is_vnil : forall (A:Set)(v:Vector.t A 0), v = Vector.nil A.
Proof.
intros a v;apply JMeq_eq.
apply vector0_is_vnil_aux.
trivial.
Qed.
Arguments Vector.cons {A} _ {n} _ .
Arguments Vector.nil {A}.
Definition Vid : forall (A : Set)(n:nat), Vector.t A n -> Vector.t A n.
Proof.
destruct n; intro v.
exact Vector.nil.
exact (Vector.cons (Vector.hd v) (Vector.tl v)).
Defined.
Eval simpl in (fun (A:Set)(v:Vector.t A 0) => (Vid _ _ v)).
Eval simpl in (fun (A:Set)(v:Vector.t A 0) => v).
Lemma Vid_eq : forall (n:nat) (A:Set)(v:Vector.t A n), v=(Vid _ n v).
Proof.
destruct v.
reflexivity.
reflexivity.
Defined.
Theorem zero_nil : forall (A:Set) (v:Vector.t A 0), v = Vector.nil.
Proof.
intros.
change (Vector.nil (A:=A)) with (Vid _ 0 v).
apply Vid_eq.
Defined.
Theorem decomp :
forall (A : Set) (n : nat) (v : Vector.t A (S n)),
v = Vector.cons (Vector.hd v) (Vector.tl v).
Proof.
intros.
change (Vector.cons (Vector.hd v) (Vector.tl v)) with (Vid _ (S n) v).
apply Vid_eq.
Defined.
Definition vector_double_rect :
forall (A:Set) (P: forall (n:nat),(Vector.t A n)->(Vector.t A n) -> Type),
P 0 Vector.nil Vector.nil ->
(forall n (v1 v2 : Vector.t A n) a b, P n v1 v2 ->
P (S n) (Vector.cons a v1) (Vector.cons b v2)) ->
forall n (v1 v2 : Vector.t A n), P n v1 v2.
induction n.
intros; rewrite (zero_nil _ v1); rewrite (zero_nil _ v2).
auto.
intros v1 v2; rewrite (decomp _ _ v1);rewrite (decomp _ _ v2).
apply X0; auto.
Defined.
Require Import Bool.
Definition bitwise_or n v1 v2 : Vector.t bool n :=
vector_double_rect bool (fun n v1 v2 => Vector.t bool n)
Vector.nil
(fun n v1 v2 a b r => Vector.cons (orb a b) r) n v1 v2.
Fixpoint vector_nth (A:Set)(n:nat)(p:nat)(v:Vector.t A p){struct v}
: option A :=
match n,v with
_ , Vector.nil => None
| 0 , Vector.cons b _ => Some b
| S n', @Vector.cons _ _ p' v' => vector_nth A n' p' v'
end.
Arguments vector_nth [A] n [p].
Lemma nth_bitwise : forall (n:nat) (v1 v2: Vector.t bool n) i a b,
vector_nth i v1 = Some a ->
vector_nth i v2 = Some b ->
vector_nth i (bitwise_or _ v1 v2) = Some (orb a b).
Proof.
intros n v1 v2; pattern n,v1,v2.
apply vector_double_rect.
simpl.
destruct i; discriminate 1.
destruct i; simpl;auto.
injection 1; injection 2;intros; subst a; subst b; auto.
Qed.
Set Implicit Arguments.
CoInductive Stream (A:Set) : Set :=
| Cons : A -> Stream A -> Stream A.
CoInductive LList (A: Set) : Set :=
| LNil : LList A
| LCons : A -> LList A -> LList A.
Definition head (A:Set)(s : Stream A) := match s with Cons a s' => a end.
Definition tail (A : Set)(s : Stream A) :=
match s with Cons a s' => s' end.
CoFixpoint repeat (A:Set)(a:A) : Stream A := Cons a (repeat a).
CoFixpoint iterate (A: Set)(f: A -> A)(a : A) : Stream A:=
Cons a (iterate f (f a)).
CoFixpoint map (A B:Set)(f: A -> B)(s : Stream A) : Stream B:=