-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathextrema.v
1965 lines (1800 loc) · 58.2 KB
/
extrema.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
Set Implicit Arguments.
Unset Strict Implicit.
Require Import Reals.
Require Import mathcomp.ssreflect.ssreflect.
From mathcomp Require Import all_ssreflect.
From mathcomp Require Import all_algebra.
Import GRing.Theory Num.Def Num.Theory.
Require Import OUVerT.numerics.
Local Open Scope ring_scope.
Import OUVerT.numerics.Numerics.
(** This file defines generic notions of extrema. *)
Section Extrema.
(** The primary parameters are:
- [rty : realFieldType] A real field
- [I : finType] A finite type
- [P : pred I] A subset of [I]
- [F : I -> rty] A "valuation" function over [I]
The module implements the following functions:
- [arg_min] An [i : I \in P] that minimizes [F]
- [arg_max] An [i : I \in P] that maximizes [F]
- [min] := [F arg_min]
- [max] := [F arg_max]
*)
Variable rty : realFieldType.
Variables (I : finType) (P : pred I) (F : I -> rty).
Section getOrd.
Variable ord : rel rty.
Hypothesis ord_refl : reflexive ord.
Hypothesis ord_trans : transitive ord.
Hypothesis ord_total : total ord.
Fixpoint getOrd (i0 : I) (l : list I) : I :=
if l is (i :: l') then
if ord (F i0) (F i) then getOrd i0 l' else getOrd i l'
else i0.
Lemma getOrd_mono i1 i2 l :
ord (F i1) (F i2) ->
ord (F (getOrd i1 l)) (F (getOrd i2 l)).
Proof.
move: i1 i2; elim: l=> // a l IH i1 i2 H /=.
case H2: (ord (F i1) (F a)).
{ by case H3: (ord (F i2) (F a)); apply: IH.
}
case H3: (ord (F i2) _)=> //.
apply: IH.
have H4: ord (F i1) (F a).
{ by apply: ord_trans; first by apply: H.
}
by rewrite H4 in H2.
Qed.
Lemma getOrd_minimalIn i0 l :
[&& ord (F (getOrd i0 l)) (F i0)
& [forall (t | t \in l), ord (F (getOrd i0 l)) (F t)]].
Proof.
move: i0; elim: l.
{ move=> i0; apply/andP; split=> //.
by apply/forallP.
}
move=> a l IH i0.
apply/andP; split.
{ simpl; case H2: (ord (F i0) _)=> //.
by case: (andP (IH i0)).
apply: ord_trans.
case: (andP (IH a))=> H3 _; apply: H3.
by case: (orP (ord_total (F i0) (F a))); first by rewrite H2.
}
apply/forallP=> x; apply/implyP.
move: (in_cons a l x)=> ->; case/orP.
{ move/eqP=> ?; subst x=> /=.
case H4: (ord (F i0) _).
case: (andP (IH i0))=> H2 _.
by apply: ord_trans; first by apply: H2.
by case: (andP (IH a)).
}
move=> H /=.
case H2: (ord (F i0) _).
{ case: (andP (IH i0))=> H0; move/forallP; move/(_ x).
by move/implyP; move/(_ H)=> H3.
}
case: (andP (IH a))=> H3; move/forallP; move/(_ x).
by move/implyP; move/(_ H)=> H4.
Qed.
Definition getOrd_tot i0 := getOrd i0 (enum I).
Lemma getOrd_totP i0 : [forall i, ord (F (getOrd_tot i0)) (F i)].
Proof.
case: (andP (getOrd_minimalIn i0 (enum I)))=> H H2.
apply/forallP=> x; apply/implyP=> H3.
suff H4: false by [].
apply: H3; move: (forallP H2 x); move/implyP; apply.
by rewrite mem_enum.
Qed.
Definition getOrd_sub i0 := getOrd i0 (filter P (enum I)).
Lemma getOrd_sub_hasP i0 (Hi0 : P i0) : P (getOrd_sub i0).
Proof.
rewrite /getOrd_sub; move: (enum I)=> l.
elim: l=> // a l /=.
case H: (P a)=> //=.
case: (ord _ _)=> //.
elim: l a H i0 Hi0 => //= a0 l IH a H i0 Hi0.
case H2: (P a0)=> //=.
case: (ord _ _).
case: (ord _ _)=> //.
by apply: IH.
by apply: IH.
case: (ord _ _)=> //.
by apply: IH.
by apply: IH.
Qed.
Lemma getOrd_subP i0 (Hi0 : P i0) :
[&& P (getOrd_sub i0)
& [forall (i | P i), ord (F (getOrd_sub i0)) (F i)]].
Proof.
case: (andP (getOrd_minimalIn i0 (filter P (enum I))))=> H H2.
apply/andP; split; first by apply: getOrd_sub_hasP.
apply/forallP=> x; apply/implyP=> H3.
move: (forallP H2 x); move/implyP; apply.
by rewrite mem_filter; apply/andP; split=> //; rewrite mem_enum.
Qed.
End getOrd.
Section default.
Variable i0 : I.
Hypothesis H : P i0.
Definition arg_max := getOrd_sub ger i0.
Lemma arg_maxP : [&& P arg_max & [forall (i | P i), F arg_max >= F i]].
Proof.
apply: getOrd_subP=> //; rewrite /ger.
by apply: lerr.
by move=> x y z /= H2 H3; apply: (ler_trans H3 H2).
by move=> x y /=; move: (ler_total x y); rewrite orbC.
Qed.
Definition max := F arg_max.
Lemma maxP : [forall (i | P i), max >= F i].
Proof.
rewrite /max.
by case: (andP arg_maxP).
Qed.
Definition arg_min := getOrd_sub ler i0.
Lemma arg_minP : [&& P arg_min & [forall (i | P i), F arg_min <= F i]].
Proof.
apply: getOrd_subP=> //.
by apply: ler_trans.
by apply: ler_total.
Qed.
Definition min := F arg_min.
Lemma minP : [forall (i | P i), min <= F i].
Proof.
rewrite /min.
by case: (andP arg_minP).
Qed.
Lemma min_le_max : min <= max.
Proof.
rewrite /min /max.
case: (andP arg_minP)=> H2; move/forallP=> H3.
case: (andP arg_maxP)=> H4; move/forallP=> H5.
move: (implyP (H3 i0)); move/(_ H)=> Hx.
move: (implyP (H5 i0)); move/(_ H)=> Hy.
apply: ler_trans.
apply: Hx.
apply: Hy.
Qed.
End default.
End Extrema.
Arguments arg_min [rty I] P F i0.
Arguments arg_max [rty I] P F i0.
Arguments arg_minP [rty I P] F [i0] _.
Arguments arg_maxP [rty I P] F [i0] _.
Arguments min [rty I] P F i0.
Arguments max [rty I] P F i0.
Arguments minP [rty I P] F [i0] _.
Arguments maxP [rty I P] F [i0] _.
Arguments min_le_max [rty I P] F [i0] _.
Lemma max_ge (rty : realFieldType) (I : finType) (f : I -> rty) (def i : I) :
f i <= max xpredT f def.
Proof.
have H: xpredT i by [].
move: (forallP (@maxP rty I xpredT f def H)); move/(_ i).
by move/implyP; apply.
Qed.
Lemma min_le (rty : realFieldType) (I : finType) (f : I -> rty) (def i : I) :
min xpredT f def <= f i.
Proof.
have H: xpredT i by [].
move: (forallP (@minP rty I xpredT f def H)); move/(_ i).
by move/implyP; apply.
Qed.
Section min_lems.
Variables (rty : realFieldType) (I : finType).
Lemma arg_min_ext (p1 p2 : pred I) (f g : I -> rty) d1 d2 :
p1 =1 p2 ->
f =1 g ->
d1 = d2 ->
arg_min p1 f d1 = arg_min p2 g d2 .
Proof.
move => H1 H2 ->.
rewrite /arg_min/getOrd_sub.
have ->:
[seq x <- enum I | p1 x] =
[seq x <- enum I | p2 x].
{ rewrite (eq_in_filter (a2:=p2)) => //. }
move: ([seq x <- _ | _]) d2; elim => // a l /= IH.
move => d2; rewrite !H2; case: (_ <= _) => //.
Qed.
Lemma min_ext (p1 p2 : pred I) (f g : I -> rty) d1 d2 :
p1 =1 p2 ->
f =1 g ->
d1 = d2 ->
min p1 f d1 = min p2 g d2 .
Proof.
move => H1 H2 ->; rewrite /min.
have ->: arg_min p1 f d2 = arg_min p2 g d2 by apply: arg_min_ext.
by apply: H2.
Qed.
Lemma ler_const_inv (c x y : rat) :
0 < c ->
((c * x <= c * y) = (x <= y)).
Proof.
move => Hpos; rewrite -(ler_pdivl_mull _ _ Hpos) mulrA mulVf.
{ by rewrite mul1r. }
by apply/eqP => H; rewrite H in Hpos.
Qed.
Lemma arg_min_const (p : pred I) (f : I -> rat) (c : rat) d :
0 < c ->
arg_min p (fun x => c * f x) d = arg_min p f d.
Proof.
move => Hpos; rewrite /arg_min/getOrd_sub.
move: ([seq x <- enum I | p x]) d; elim => // a l IH /= d.
rewrite (ler_const_inv _ _ Hpos). case: (f d <= f a) => //.
Qed.
Lemma min_const (p : pred I) (f : I -> rat) (c : rat) d :
0 < c ->
min p (fun x => c * f x) d = c * min p f d.
Proof.
move => Hpos; rewrite /min; f_equal.
rewrite arg_min_const //.
Qed.
End min_lems.
Local Open Scope Numeric_scope.
Delimit Scope Numerics_scope with Num.
Module num_Extrema.
Section extrema_defs.
Context (Nt:Type) `{Numeric Nt}.
Fixpoint max (l : list Nt) : option Nt :=
match l with
| List.nil => None
| x :: l' =>
match max l' with
| None => Some x
| Some x' =>
Some (if leb x x' then x' else x)
end
end.
Definition max_default (l : list Nt) (def : Nt) : Nt :=
match max l with
| None => def
| Some x => x
end.
Fixpoint argmax {T : Type} (l : list T) (f : T -> Nt) : option T :=
match l with
| List.nil => None
| x :: l' =>
(match argmax l' f with
| None => Some x
| Some x' =>
Some (if leb (f x) (f x') then x' else x)
end)
end.
Definition argmax_default {T : Type} (l : list T) (f : T -> Nt) (def : T) : T :=
match argmax l f with
| None => def
| Some x => x
end.
Definition argmax_ne {T : Type} (l : list T) (f : T-> Nt) (h: O <> (length l)) : T.
destruct l.
{ simpl in h. exfalso. auto. }
destruct (argmax (t :: l) f) eqn:e.
{ exact t0. }
simpl in e.
destruct (argmax l f); inversion e.
Defined.
Fixpoint mapmax {T : Type} (l : list T) (f : T->Nt) : option Nt :=
match l with
| List.nil => None
| x :: l' => match mapmax l' f with
| None => Some (f x)
| Some x' => Some (if leb (f x) x' then x' else (f x))
end
end.
Lemma argmax_ne_ok: forall {T : Type} (l : list T) (f : T-> Nt) (h : O <> (length l)),
(argmax l f) = Some (argmax_ne f h).
Proof.
intros.
destruct l.
{ exfalso. apply h. auto. }
destruct l; auto.
simpl in *.
destruct (argmax l f); auto.
Qed.
Definition max_ne (l : list Nt) (h : O <> (length l)) : Nt.
destruct l.
{ exfalso; auto. }
destruct (max (n ::l)) eqn:e.
{ exact n0. }
simpl in e.
destruct (max l); inversion e.
Defined.
Definition mapmax_ne (T : Type) (l : list T) (f : T->Nt) (h : O <> (length l)) : Nt.
destruct l.
{ exfalso; auto. }
destruct (mapmax (t ::l) f) eqn:e.
{ exact n. }
simpl in e.
destruct (mapmax l f); inversion e.
Defined.
Fixpoint nth_max (l : list Nt) (n : nat) : option Nt :=
match n with
| O => max l
| S n' =>
match (max l) with
| None => None
| Some m => nth_max (filter (fun x => ltb x m) l) n'
end
end.
End extrema_defs.
Section use_Numerics.
Context {Nt : Type} `{Numerics.Numeric_Props Nt}.
Lemma mapmax_ne_ok: forall (T : Type) (l : list T) (f : T->Nt) (h : O <> (length l)), mapmax l f = Some (mapmax_ne f h).
Proof.
intros.
destruct l.
{ exfalso. apply h; auto. }
destruct l; auto.
simpl.
destruct (mapmax l f); auto.
Qed.
Lemma max_ne_ok: forall (l : list Nt) (h : O <> (length l)), max l = Some (max_ne h).
Proof.
intros.
destruct l.
{ exfalso. apply h. auto. }
destruct l; auto.
simpl.
destruct (max l); auto.
Qed.
Lemma max_ne_some: forall (l : list Nt), O <> length l <-> exists x, max l = Some x.
Proof.
intros.
split; intros.
{
destruct l.
{
simpl in *.
exfalso.
auto.
}
simpl.
destruct (max l); eauto.
}
destruct l; simpl; auto.
simpl in H0. inversion H0. inversion H1.
Qed.
Lemma argmax_ne_some: forall (T : Type) (l : list T) (f : T->Nt), O <> length l <-> exists x , argmax l f= Some x.
Proof.
intros.
split; intros.
{
destruct l.
{
simpl in *.
exfalso.
auto.
}
simpl.
destruct (argmax l); eauto.
}
destruct l; simpl; auto.
simpl in H0. inversion H0. inversion H1.
Qed.
Lemma mapmax_map_max: forall (T : Type) (l : list T) (f : T-> Nt), mapmax l f = max (map f l).
Proof.
intros.
induction l; auto.
simpl.
rewrite IHl.
auto.
Qed.
Lemma mapmax_ne_some: forall (T : Type) (l : list T) (f : T->Nt), O <> length l <-> exists x , mapmax l f = Some x.
Proof.
intros.
assert (forall (T : Type) (l : list T), length l = size l). auto.
rewrite H0.
rewrite mapmax_map_max.
rewrite <- size_map with T Nt f _.
rewrite <- H0.
apply max_ne_some.
Qed.
Lemma argmax_mapmax: forall (T : Type) (l : list T) (f : T -> Nt), O <> length l ->
(exists x, argmax l f = Some x /\ Some (f x) = mapmax l f).
Proof.
intros.
induction l.
{ exfalso. apply H0. auto. }
simpl in *.
destruct l.
{ simpl. exists a. split; auto. }
destruct IHl; simpl; auto.
destruct H1.
inversion H1.
destruct (length l) eqn:e.
{
rewrite -> List.length_zero_iff_nil in e. rewrite e; simpl.
eexists. split; auto.
destruct (leb (f a) (f t)); auto.
}
assert (O <> length l).
{ unfold not. intros. rewrite e in H3. inversion H3. }
assert (exists x', argmax l f = Some x').
apply argmax_ne_some; auto.
destruct H5.
rewrite H5.
assert (exists x', mapmax l f = Some x').
apply mapmax_ne_some; auto.
destruct H6.
rewrite H6.
eexists; split; auto.
simpl in H1.
simpl in H2.
rewrite H5 in H1.
rewrite H6 in H2.
destruct (leb (f t) x1) eqn:e2.
{
inversion H2.
rewrite <- H8 in e2.
inversion H1.
destruct (leb (f t) (f x0)).
destruct (leb (f a) (f x0)); auto.
destruct (leb (f a) (f t)); auto.
}
inversion H1.
destruct (leb (f t) (f x0)) eqn:e3.
{
inversion H1.
inversion H2.
destruct (leb (f a) (f x)); auto.
}
destruct (leb (f a) (f t)); auto.
Qed.
Lemma argmax_ne_mapmax_ne: forall (T : Type) (l : list T) (f : T -> Nt) (h : O <> length l),
mapmax_ne f h = f (argmax_ne f h).
Proof.
intros.
destruct argmax_mapmax with T l f; auto.
destruct H0.
rewrite argmax_ne_ok in H0.
inversion H0.
rewrite H3.
rewrite mapmax_ne_ok in H1.
inversion H1.
auto.
Qed.
Lemma mapmax_ext: forall (T : Type) (f g : T->Nt) (l : list T), (forall x : T, f x = g x) -> mapmax l f = mapmax l g.
Proof.
intros.
induction l; auto.
simpl.
rewrite IHl.
rewrite H0.
auto.
Qed.
Lemma argmax_ext: forall (T : Type) (f g : T->Nt) (l : list T), (forall x : T, f x = g x) -> argmax l f = argmax l g.
Proof.
intros.
induction l; auto.
simpl.
rewrite IHl.
repeat rewrite H0.
destruct (argmax l g); auto.
repeat rewrite H0.
auto.
Qed.
Lemma argmax_ext': forall (T : Type) (f g : T->Nt) (l : list T), (forall x y: T, f x <= f y <-> g x <= g y) -> argmax l f = argmax l g.
Proof.
intros.
induction l; auto.
simpl.
rewrite IHl.
destruct (argmax l g); auto.
destruct total_order_T with (f a) (f t).
{
destruct s.
{
apply le_lt_weak in l0.
assert (leb (f a) (f t) ).
apply leb_true_iff. auto.
apply H0 in l0.
apply leb_true_iff in l0.
rewrite l0.
rewrite H1.
auto.
}
rewrite e.
rewrite leb_refl.
assert (f a <= f t). { unfold le. auto. }
apply H0 in H1.
apply leb_true_iff in H1.
rewrite H1.
auto.
}
assert ( ~ f a <= f t).
apply lt_not_le. auto.
assert ( ~ g a <= g t).
unfold not. intros. apply H1. apply H0. auto.
apply leb_false_iff in H1.
apply leb_false_iff in H2.
rewrite H1.
rewrite H2.
auto.
Qed.
Lemma mapmax_ne_ext: forall (T : Type) (f g : T->Nt) (l : list T) (H : O <> length l), (forall x : T, f x = g x) -> mapmax_ne f H = mapmax_ne g H.
Proof.
intros.
assert (mapmax l f = Some (mapmax_ne f H0)).
apply mapmax_ne_ok.
assert (mapmax l g = Some (mapmax_ne g H0)).
apply mapmax_ne_ok.
assert ( mapmax l f = mapmax l g).
apply mapmax_ext. auto.
rewrite H2 in H4.
rewrite H3 in H4.
inversion H4.
auto.
Qed.
Lemma argmax_ne_ext: forall (T : Type) (f g : T->Nt) (l : list T) (H : O <> length l), (forall x : T, f x = g x) -> argmax_ne f H = argmax_ne g H.
Proof.
intros.
assert (argmax l f = Some (argmax_ne f H0)).
apply argmax_ne_ok.
assert (argmax l g = Some (argmax_ne g H0)).
apply argmax_ne_ok.
assert ( argmax l f = argmax l g).
apply argmax_ext. auto.
rewrite H2 in H4.
rewrite H3 in H4.
inversion H4.
auto.
Qed.
Lemma argmax_ne_ext': forall (T : Type) (f g : T->Nt) (l : list T) (H : O <> length l), (forall x y: T, f x <= f y <-> g x <= g y) -> argmax_ne f H = argmax_ne g H.
Proof.
intros.
assert (argmax l f = Some (argmax_ne f H0)).
apply argmax_ne_ok.
assert (argmax l g = Some (argmax_ne g H0)).
apply argmax_ne_ok.
assert ( argmax l f = argmax l g).
apply argmax_ext'. auto.
rewrite H2 in H4.
rewrite H3 in H4.
inversion H4.
auto.
Qed.
Lemma argmax_plus_const_r: forall (T : Type) (l : list T) (f : T -> Nt) (x : Nt), argmax l f = argmax l (fun n => f n + x).
Proof.
intros.
apply argmax_ext'.
intros.
split; intros.
{ apply plus_le_compat; auto. apply le_refl. }
apply plus_le_compat_l with (-x) (f x0 + x) (f y + x) in H0.
rewrite -> plus_comm with (f x0) x in H0.
rewrite -> plus_comm with (f y) x in H0.
repeat rewrite plus_assoc in H0.
rewrite plus_neg_l in H0.
repeat rewrite plus_id_l in H0.
auto.
Qed.
Lemma argmax_plus_const_l: forall (T : Type) (l : list T) (f : T -> Nt) (x : Nt), argmax l f = argmax l (fun n => x + f n).
Proof.
intros.
apply argmax_ext'.
intros.
split; intros.
{ apply plus_le_compat; auto. apply le_refl. }
apply plus_le_compat_l_reverse in H0.
auto.
Qed.
Lemma argmax_mult_pos_r: forall (T : Type) (l : list T) (f : T -> Nt) (x : Nt), 0 < x -> argmax l f = argmax l (fun n => f n * x).
Proof.
intros.
apply argmax_ext'.
intros.
split; intros.
{ apply mult_le_compat_r; auto. apply le_lt_weak. auto. }
apply mult_le_compat_r_reverse in H1; auto.
Qed.
Lemma argmax_mult_pos_l: forall (T : Type) (l : list T) (f : T -> Nt) (x : Nt), 0 < x -> argmax l f = argmax l (fun n => x * f n).
Proof.
intros.
apply argmax_ext'.
intros.
split; intros.
{ apply mult_le_compat_l; auto. apply le_lt_weak. auto. }
apply mult_le_compat_l_reverse in H1; auto.
Qed.
Lemma argmax_ne_mult_pos_r: forall (T : Type) (l : list T) (f : T -> Nt) (x : Nt) (H : O <> length l), 0 < x -> argmax_ne f H = argmax_ne (fun n => f n * x) H.
Proof.
intros.
assert(argmax l f = Some (argmax_ne f H0) ).
apply argmax_ne_ok.
assert(argmax l (fun n : T => f n * x) = Some (argmax_ne (fun n : T => f n * x) H0) ).
apply argmax_ne_ok.
rewrite <- argmax_mult_pos_r in H3; auto.
rewrite H2 in H3.
inversion H3.
auto.
Qed.
Lemma argmax_ne_mult_pos_l: forall (T : Type) (l : list T) (f : T -> Nt) (x : Nt) (H : O <> length l), 0 < x -> argmax_ne f H = argmax_ne (fun n => x * f n) H.
Proof.
intros.
assert(argmax l f = Some (argmax_ne f H0) ).
apply argmax_ne_ok.
assert(argmax l (fun n : T => x * f n) = Some (argmax_ne (fun n : T => x * f n) H0) ).
apply argmax_ne_ok.
rewrite <- argmax_mult_pos_l in H3; auto.
rewrite H2 in H3.
inversion H3.
auto.
Qed.
Lemma mapmax_const: forall (T : Type) (l : list T) (x : Nt), (O <> length l) -> mapmax l (fun _ => x) = Some x.
Proof.
intros.
induction l; auto.
{ exfalso. apply H0; auto. }
simpl in *.
destruct l; auto.
simpl in *.
rewrite IHl; auto.
destruct (leb x x); auto.
Qed.
Lemma mapmax_ne_const: forall (T : Type) (l : list T) (x : Nt) (H : O <> length l), mapmax_ne (fun _ => x) H = x.
Proof.
intros.
assert(mapmax l (fun _ : T => x) = Some (mapmax_ne (fun _ : T => x) H0)).
apply mapmax_ne_ok.
assert (mapmax l (fun _ : T => x) = Some x).
apply mapmax_const; auto.
rewrite H1 in H2.
inversion H2.
rewrite H4.
auto.
Qed.
Lemma mapmax_ne_mult_pos_r: forall (T : Type) (l : list T) (f : T -> Nt) (x : Nt) (H : O <> length l), 0 <= x -> mapmax_ne f H * x= mapmax_ne (fun n => f n * x) H.
Proof.
intros.
destruct H1.
2: {
rewrite <- H1. rewrite mult_plus_id_r.
assert (mapmax_ne (l:=l) (fun n : T => f n * 0) H0 = mapmax_ne (l:=l) (fun n : T => 0) H0).
{ apply mapmax_ne_ext. intros. apply mult_plus_id_r. }
rewrite H2.
rewrite mapmax_ne_const.
auto.
}
repeat rewrite argmax_ne_mapmax_ne.
rewrite <- argmax_ne_mult_pos_r; auto.
Qed.
Lemma mapmax_ne_mult_pos_l: forall (T : Type) (l : list T) (f : T -> Nt) (x : Nt) (H : O <> length l), 0 <= x -> x * mapmax_ne f H = mapmax_ne (fun n => x * f n) H.
Proof.
intros.
destruct H1.
2: {
rewrite <- H1. rewrite mult_plus_id_l.
assert (mapmax_ne (l:=l) (fun n : T => 0 * f n ) H0 = mapmax_ne (l:=l) (fun n : T => 0) H0).
{ apply mapmax_ne_ext. intros. apply mult_plus_id_l. }
rewrite H2.
rewrite mapmax_ne_const.
auto.
}
repeat rewrite argmax_ne_mapmax_ne.
rewrite <- argmax_ne_mult_pos_l; auto.
Qed.
Lemma argmax_ne_plus_const_r: forall (T : Type) (l : list T) (f : T -> Nt) (x : Nt) (H : O <> length l), argmax_ne f H = argmax_ne (fun n => f n + x) H.
Proof.
intros.
apply argmax_ne_ext'.
intros.
split; intros.
{ apply Numerics.plus_le_compat_r. auto. }
apply Numerics.plus_le_compat_r_reverse in H1.
auto.
Qed.
Lemma mapmax_ne_plus_const_r: forall (T : Type) (l : list T) (f : T -> Nt) (x : Nt) (H : O <> length l), mapmax_ne f H + x= mapmax_ne (fun n => f n + x) H.
Proof.
intros.
repeat rewrite argmax_ne_mapmax_ne.
rewrite <- argmax_ne_plus_const_r; auto.
Qed.
Lemma argmax_ne_plus_const_l: forall (T : Type) (l : list T) (f : T -> Nt) (x : Nt) (H : O <> length l), argmax_ne f H = argmax_ne (fun n => x + f n) H.
Proof.
intros.
apply argmax_ne_ext'.
intros.
split; intros.
{ apply Numerics.plus_le_compat_l. auto. }
apply Numerics.plus_le_compat_l_reverse in H1.
auto.
Qed.
Lemma mapmax_ne_plus_const_l: forall (T : Type) (l : list T) (f : T -> Nt) (x : Nt) (H : O <> length l), x + mapmax_ne f H = mapmax_ne (fun n => x + f n) H.
Proof.
intros.
repeat rewrite argmax_ne_mapmax_ne.
rewrite <- argmax_ne_plus_const_l; auto.
Qed.
Lemma mapmax_ne_map_max_ne: forall (T : Type) (l : list T) (f : T->Nt) (H0 : O <> length l) (H1 : O <> length (map f l)),
mapmax_ne f H0 = max_ne H1.
Proof.
intros.
assert(mapmax l f = Some (mapmax_ne f H0)).
apply mapmax_ne_ok.
assert(max (map f l) = Some (max_ne H1)).
apply max_ne_ok.
rewrite <- mapmax_map_max in H3.
rewrite H2 in H3.
inversion H3.
auto.
Qed.
Lemma max_correct: forall (l : list Nt) (n : Nt), List.In n l -> (exists m, Some m = max l /\ n <= m).
Proof.
intros.
induction l.
{ inversion H0. }
simpl in *.
destruct H0.
{
destruct l.
{
simpl.
exists a.
split; auto.
rewrite H0.
apply Numerics.le_refl.
}
rewrite H0.
destruct (max (n0 :: l)) eqn:H1.
{
exists (if Numerics.leb n n1 then n1 else n).
split; auto.
destruct (Numerics.leb n n1) eqn:e2.
{ apply Numerics.leb_true_iff. auto. }
apply Numerics.le_refl.
}
exists n.
split; auto.
apply Numerics.le_refl.
}
destruct IHl; auto.
destruct H1.
rewrite <- H1.
exists (if Numerics.leb a x then x else a).
split; auto.
destruct (Numerics.leb a x) eqn:H3; auto.
apply Numerics.leb_false_iff in H3.
apply Numerics.not_le_lt in H3.
apply Numerics.le_lt_weak.
apply Numerics.le_lt_trans with x; auto.
Qed.
Lemma max_in: forall (l : list Nt) (x : Nt), max l = Some x -> List.In x l.
Proof.
intros.
induction l.
{ inversion H0. }
simpl in *.
destruct (max l) eqn:e.
2: { inversion H0. auto. }
destruct (leb a n); auto.
inversion H0. auto.
Qed.
Lemma nth_max_in: forall (l : list Nt) (n : nat) (x : Nt),
nth_max l n = Some x -> List.In x l.
Proof.
intros.
generalize dependent x.
generalize dependent l.
induction n; intros.
{
simpl in H0.
apply max_in.
auto.
}
simpl in H0.
destruct (max l) eqn:e.
2:{ inversion H0. }
apply IHn in H0.
apply List.filter_In in H0.
destruct H0. auto.
Qed.
Lemma max_none: forall (l : list Nt), max l = None <-> l = [::].
Proof.
intros.
split; intros.
{
destruct l; auto.
simpl in H0.
destruct (max l);
inversion H0.
}
rewrite H0. auto.
Qed.
Lemma filter_none: forall {T : Type} (l : list T) (f : T->bool),
filter f l = [::] <-> (forall x : T, List.In x l -> f x = false).
Proof.
intros.
split; intros.
{
induction l.
{ inversion H1. }
simpl in H0.
destruct (f a) eqn:e.
inversion H0.
destruct H1; auto.
rewrite <- H1. auto.
}
induction l; auto.
simpl.
rewrite H0; simpl; auto.
apply IHl.
simpl in H0.
intros.
destruct H0 with x; auto.
Qed.
Lemma second_max_none: forall (l : list Nt), nth_max l 1 = None ->
(forall (x : Nt), List.In x l -> max l = Some x).
Proof.
intros.
simpl in H0.
destruct l.
{ inversion H1. }
remember H1.
clear Heqi.
apply max_correct in i.
destruct i.
destruct H2.
rewrite <- H2 in H0.
destruct H3.
2:{ rewrite H3. auto. }
rewrite -> max_none in H0.
rewrite -> filter_none in H0.
apply ltb_true_iff in H3.
apply H0 in H1.
rewrite H1 in H3. inversion H3.
Qed.
Lemma second_max_some_max_some: forall (l : list Nt) (x : Nt), nth_max l 1 = Some x ->
exists y : Nt, max l = Some y.
Proof.
intros.
simpl in H0.
destruct (max l) eqn:e; eauto.
Qed.
Lemma gt_second_max: forall (l : list Nt) (x y z: Nt), max l = Some x -> nth_max l 1 = Some y ->
List.In z l -> y < z -> x = z.
Proof.
intros.
simpl in *.
destruct l.
{ inversion H2. }
rewrite H0 in H1.
remember H2. clear Heqi.
apply max_correct in i.
destruct i. destruct H4.
rewrite H0 in H4. inversion H4.
clear H4. rewrite H7 in H5.
clear H7. clear x0.
destruct H5; auto.
exfalso. apply lt_not_le with y z; auto.
assert(List.In z ([seq x0 <- n :: l | ltb x0 x])).
rewrite -> List.filter_In. split; auto. apply ltb_true_iff. auto.
apply max_correct in H5. destruct H5. destruct H5.
rewrite H1 in H5. inversion H5. rewrite <- H8. auto.
Qed.
Lemma second_max_lt_max: forall (l : list Nt) (x y : Nt), max l = Some x ->
nth_max l 1 = Some y -> y < x.
Proof.
intros.
simpl in H1.
destruct (max l) eqn:e.
2:{ inversion H0. }
inversion H0.
rewrite H3 in H1. rewrite H3 in e.
clear H3. clear H0. clear n.
apply max_in in H1.
apply List.filter_In in H1.
destruct H1. apply ltb_true_iff.
auto.
Qed.