Datasets:

Modalities:
Text
Languages:
English
Libraries:
Datasets
License:
File size: 74,763 Bytes
4365a98
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
/-
Copyright (c) 2019 SΓ©bastien GouΓ«zel. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: SΓ©bastien GouΓ«zel, Yury Kudryashov
-/
import analysis.calculus.local_extr
import analysis.convex.slope
import analysis.convex.topology
import data.complex.is_R_or_C

/-!
# The mean value inequality and equalities

In this file we prove the following facts:

* `convex.norm_image_sub_le_of_norm_deriv_le` : if `f` is differentiable on a convex set `s`
  and the norm of its derivative is bounded by `C`, then `f` is Lipschitz continuous on `s` with
  constant `C`; also a variant in which what is bounded by `C` is the norm of the difference of the
  derivative from a fixed linear map. This lemma and its versions are formulated using `is_R_or_C`,
  so they work both for real and complex derivatives.

* `image_le_of*`, `image_norm_le_of_*` : several similar lemmas deducing `f x ≀ B x` or
  `βˆ₯f xβˆ₯ ≀ B x` from upper estimates on `f'` or `βˆ₯f'βˆ₯`, respectively. These lemmas differ by
  their assumptions:

  * `of_liminf_*` lemmas assume that limit inferior of some ratio is less than `B' x`;
  * `of_deriv_right_*`, `of_norm_deriv_right_*` lemmas assume that the right derivative
    or its norm is less than `B' x`;
  * `of_*_lt_*` lemmas assume a strict inequality whenever `f x = B x` or `βˆ₯f xβˆ₯ = B x`;
  * `of_*_le_*` lemmas assume a non-strict inequality everywhere on `[a, b)`;
  * name of a lemma ends with `'` if (1) it assumes that `B` is continuous on `[a, b]`
    and has a right derivative at every point of `[a, b)`, and (2) the lemma has
    a counterpart assuming that `B` is differentiable everywhere on `ℝ`

* `norm_image_sub_le_*_segment` : if derivative of `f` on `[a, b]` is bounded above
  by a constant `C`, then `βˆ₯f x - f aβˆ₯ ≀ C * βˆ₯x - aβˆ₯`; several versions deal with
  right derivative and derivative within `[a, b]` (`has_deriv_within_at` or `deriv_within`).

* `convex.is_const_of_fderiv_within_eq_zero` : if a function has derivative `0` on a convex set `s`,
  then it is a constant on `s`.

* `exists_ratio_has_deriv_at_eq_ratio_slope` and `exists_ratio_deriv_eq_ratio_slope` :
  Cauchy's Mean Value Theorem.

* `exists_has_deriv_at_eq_slope` and `exists_deriv_eq_slope` : Lagrange's Mean Value Theorem.

* `domain_mvt` : Lagrange's Mean Value Theorem, applied to a segment in a convex domain.

* `convex.image_sub_lt_mul_sub_of_deriv_lt`, `convex.mul_sub_lt_image_sub_of_lt_deriv`,
  `convex.image_sub_le_mul_sub_of_deriv_le`, `convex.mul_sub_le_image_sub_of_le_deriv`,
  if `βˆ€ x, C (</≀/>/β‰₯) (f' x)`, then `C * (y - x) (</≀/>/β‰₯) (f y - f x)` whenever `x < y`.

* `convex.monotone_on_of_deriv_nonneg`, `convex.antitone_on_of_deriv_nonpos`,
  `convex.strict_mono_of_deriv_pos`, `convex.strict_anti_of_deriv_neg` :
  if the derivative of a function is non-negative/non-positive/positive/negative, then
  the function is monotone/antitone/strictly monotone/strictly monotonically
  decreasing.

* `convex_on_of_deriv_monotone_on`, `convex_on_of_deriv2_nonneg` : if the derivative of a function
  is increasing or its second derivative is nonnegative, then the original function is convex.

* `strict_fderiv_of_cont_diff` : a C^1 function over the reals is strictly differentiable.  (This
  is a corollary of the mean value inequality.)
-/


variables {E : Type*} [normed_add_comm_group E] [normed_space ℝ E]
          {F : Type*} [normed_add_comm_group F] [normed_space ℝ F]

open metric set asymptotics continuous_linear_map filter
open_locale classical topological_space nnreal

/-! ### One-dimensional fencing inequalities -/

/-- General fencing theorem for continuous functions with an estimate on the derivative.
Let `f` and `B` be continuous functions on `[a, b]` such that

* `f a ≀ B a`;
* `B` has right derivative `B'` at every point of `[a, b)`;
* for each `x ∈ [a, b)` the right-side limit inferior of `(f z - f x) / (z - x)`
  is bounded above by a function `f'`;
* we have `f' x < B' x` whenever `f x = B x`.

Then `f x ≀ B x` everywhere on `[a, b]`. -/
lemma image_le_of_liminf_slope_right_lt_deriv_boundary' {f f' : ℝ β†’ ℝ} {a b : ℝ}
  (hf : continuous_on f (Icc a b))
  -- `hf'` actually says `liminf (f z - f x) / (z - x) ≀ f' x`
  (hf' : βˆ€ x ∈ Ico a b, βˆ€ r, f' x < r β†’ βˆƒαΆ  z in 𝓝[>] x, slope f x z < r)
  {B B' : ℝ β†’ ℝ} (ha : f a ≀ B a) (hB : continuous_on B (Icc a b))
  (hB' : βˆ€ x ∈ Ico a b, has_deriv_within_at B (B' x) (Ici x) x)
  (bound : βˆ€ x ∈ Ico a b, f x = B x β†’ f' x < B' x) :
  βˆ€ ⦃x⦄, x ∈ Icc a b β†’ f x ≀ B x :=
begin
  change Icc a b βŠ† {x | f x ≀ B x},
  set s := {x | f x ≀ B x} ∩ Icc a b,
  have A : continuous_on (Ξ» x, (f x, B x)) (Icc a b), from hf.prod hB,
  have : is_closed s,
  { simp only [s, inter_comm],
    exact A.preimage_closed_of_closed is_closed_Icc order_closed_topology.is_closed_le' },
  apply this.Icc_subset_of_forall_exists_gt ha,
  rintros x ⟨hxB : f x ≀ B x, xab⟩ y hy,
  cases hxB.lt_or_eq with hxB hxB,
  { -- If `f x < B x`, then all we need is continuity of both sides
    refine nonempty_of_mem (inter_mem _ (Ioc_mem_nhds_within_Ioi ⟨le_rfl, hy⟩)),
    have : βˆ€αΆ  x in 𝓝[Icc a b] x, f x < B x,
      from A x (Ico_subset_Icc_self xab)
        (is_open.mem_nhds (is_open_lt continuous_fst continuous_snd) hxB),
    have : βˆ€αΆ  x in 𝓝[>] x, f x < B x,
      from nhds_within_le_of_mem (Icc_mem_nhds_within_Ioi xab) this,
    exact this.mono (Ξ» y, le_of_lt) },
  { rcases exists_between (bound x xab hxB) with ⟨r, hfr, hrB⟩,
    specialize hf' x xab r hfr,
    have HB : βˆ€αΆ  z in 𝓝[>] x, r < slope B x z,
      from (has_deriv_within_at_iff_tendsto_slope' $ lt_irrefl x).1
        (hB' x xab).Ioi_of_Ici (Ioi_mem_nhds hrB),
    obtain ⟨z, hfz, hzB, hz⟩ :
      βˆƒ z, slope f x z < r ∧ r < slope B x z ∧ z ∈ Ioc x y,
      from (hf'.and_eventually (HB.and (Ioc_mem_nhds_within_Ioi ⟨le_rfl, hy⟩))).exists,
    refine ⟨z, _, hz⟩,
    have := (hfz.trans hzB).le,
    rwa [slope_def_field, slope_def_field, div_le_div_right (sub_pos.2 hz.1), hxB,
      sub_le_sub_iff_right] at this }
end

/-- General fencing theorem for continuous functions with an estimate on the derivative.
Let `f` and `B` be continuous functions on `[a, b]` such that

* `f a ≀ B a`;
* `B` has derivative `B'` everywhere on `ℝ`;
* for each `x ∈ [a, b)` the right-side limit inferior of `(f z - f x) / (z - x)`
  is bounded above by a function `f'`;
* we have `f' x < B' x` whenever `f x = B x`.

Then `f x ≀ B x` everywhere on `[a, b]`. -/
lemma image_le_of_liminf_slope_right_lt_deriv_boundary {f f' : ℝ β†’ ℝ} {a b : ℝ}
  (hf : continuous_on f (Icc a b))
  -- `hf'` actually says `liminf (f z - f x) / (z - x) ≀ f' x`
  (hf' : βˆ€ x ∈ Ico a b, βˆ€ r, f' x < r β†’ βˆƒαΆ  z in 𝓝[>] x, slope f x z < r)
  {B B' : ℝ β†’ ℝ} (ha : f a ≀ B a) (hB : βˆ€ x, has_deriv_at B (B' x) x)
  (bound : βˆ€ x ∈ Ico a b, f x = B x β†’ f' x < B' x) :
  βˆ€ ⦃x⦄, x ∈ Icc a b β†’ f x ≀ B x :=
image_le_of_liminf_slope_right_lt_deriv_boundary' hf hf' ha
  (Ξ» x hx, (hB x).continuous_at.continuous_within_at)
  (Ξ» x hx, (hB x).has_deriv_within_at) bound

/-- General fencing theorem for continuous functions with an estimate on the derivative.
Let `f` and `B` be continuous functions on `[a, b]` such that

* `f a ≀ B a`;
* `B` has right derivative `B'` at every point of `[a, b)`;
* for each `x ∈ [a, b)` the right-side limit inferior of `(f z - f x) / (z - x)`
  is bounded above by `B'`.

Then `f x ≀ B x` everywhere on `[a, b]`. -/
lemma image_le_of_liminf_slope_right_le_deriv_boundary {f : ℝ β†’ ℝ} {a b : ℝ}
  (hf : continuous_on f (Icc a b))
  {B B' : ℝ β†’ ℝ} (ha : f a ≀ B a) (hB : continuous_on B (Icc a b))
  (hB' : βˆ€ x ∈ Ico a b, has_deriv_within_at B (B' x) (Ici x) x)
  -- `bound` actually says `liminf (f z - f x) / (z - x) ≀ B' x`
  (bound : βˆ€ x ∈ Ico a b, βˆ€ r, B' x < r β†’ βˆƒαΆ  z in 𝓝[>] x, slope f x z < r) :
  βˆ€ ⦃x⦄, x ∈ Icc a b β†’ f x ≀ B x :=
begin
  have Hr : βˆ€ x ∈ Icc a b, βˆ€ r > 0, f x ≀ B x + r * (x - a),
  { intros x hx r hr,
    apply image_le_of_liminf_slope_right_lt_deriv_boundary' hf bound,
    { rwa [sub_self, mul_zero, add_zero] },
    { exact hB.add (continuous_on_const.mul
        (continuous_id.continuous_on.sub continuous_on_const)) },
    { assume x hx,
      exact (hB' x hx).add (((has_deriv_within_at_id x (Ici x)).sub_const a).const_mul r) },
    { assume x hx _,
      rw [mul_one],
      exact (lt_add_iff_pos_right _).2 hr },
    exact hx },
  assume x hx,
  have : continuous_within_at (Ξ» r, B x + r * (x - a)) (Ioi 0) 0,
    from continuous_within_at_const.add (continuous_within_at_id.mul continuous_within_at_const),
  convert continuous_within_at_const.closure_le _ this (Hr x hx); simp
end

/-- General fencing theorem for continuous functions with an estimate on the derivative.
Let `f` and `B` be continuous functions on `[a, b]` such that

* `f a ≀ B a`;
* `B` has right derivative `B'` at every point of `[a, b)`;
* `f` has right derivative `f'` at every point of `[a, b)`;
* we have `f' x < B' x` whenever `f x = B x`.

Then `f x ≀ B x` everywhere on `[a, b]`. -/
lemma image_le_of_deriv_right_lt_deriv_boundary' {f f' : ℝ β†’ ℝ} {a b : ℝ}
  (hf : continuous_on f (Icc a b))
  (hf' : βˆ€ x ∈ Ico a b, has_deriv_within_at f (f' x) (Ici x) x)
  {B B' : ℝ β†’ ℝ} (ha : f a ≀ B a) (hB : continuous_on B (Icc a b))
  (hB' : βˆ€ x ∈ Ico a b, has_deriv_within_at B (B' x) (Ici x) x)
  (bound : βˆ€ x ∈ Ico a b, f x = B x β†’ f' x < B' x) :
  βˆ€ ⦃x⦄, x ∈ Icc a b β†’ f x ≀ B x :=
image_le_of_liminf_slope_right_lt_deriv_boundary' hf
  (Ξ» x hx r hr, (hf' x hx).liminf_right_slope_le hr) ha hB hB' bound

/-- General fencing theorem for continuous functions with an estimate on the derivative.
Let `f` and `B` be continuous functions on `[a, b]` such that

* `f a ≀ B a`;
* `B` has derivative `B'` everywhere on `ℝ`;
* `f` has right derivative `f'` at every point of `[a, b)`;
* we have `f' x < B' x` whenever `f x = B x`.

Then `f x ≀ B x` everywhere on `[a, b]`. -/
lemma image_le_of_deriv_right_lt_deriv_boundary {f f' : ℝ β†’ ℝ} {a b : ℝ}
  (hf : continuous_on f (Icc a b))
  (hf' : βˆ€ x ∈ Ico a b, has_deriv_within_at f (f' x) (Ici x) x)
  {B B' : ℝ β†’ ℝ} (ha : f a ≀ B a) (hB : βˆ€ x, has_deriv_at B (B' x) x)
  (bound : βˆ€ x ∈ Ico a b, f x = B x β†’ f' x < B' x) :
  βˆ€ ⦃x⦄, x ∈ Icc a b β†’ f x ≀ B x :=
image_le_of_deriv_right_lt_deriv_boundary' hf hf' ha
  (Ξ» x hx, (hB x).continuous_at.continuous_within_at)
  (Ξ» x hx, (hB x).has_deriv_within_at) bound

/-- General fencing theorem for continuous functions with an estimate on the derivative.
Let `f` and `B` be continuous functions on `[a, b]` such that

* `f a ≀ B a`;
* `B` has derivative `B'` everywhere on `ℝ`;
* `f` has right derivative `f'` at every point of `[a, b)`;
* we have `f' x ≀ B' x` on `[a, b)`.

Then `f x ≀ B x` everywhere on `[a, b]`. -/
lemma image_le_of_deriv_right_le_deriv_boundary {f f' : ℝ β†’ ℝ} {a b : ℝ}
  (hf : continuous_on f (Icc a b))
  (hf' : βˆ€ x ∈ Ico a b, has_deriv_within_at f (f' x) (Ici x) x)
  {B B' : ℝ β†’ ℝ} (ha : f a ≀ B a) (hB : continuous_on B (Icc a b))
  (hB' : βˆ€ x ∈ Ico a b, has_deriv_within_at B (B' x) (Ici x) x)
  (bound : βˆ€ x ∈ Ico a b, f' x ≀ B' x) :
  βˆ€ ⦃x⦄, x ∈ Icc a b β†’ f x ≀ B x :=
image_le_of_liminf_slope_right_le_deriv_boundary hf ha hB hB' $
assume x hx r hr, (hf' x hx).liminf_right_slope_le (lt_of_le_of_lt (bound x hx) hr)

/-! ### Vector-valued functions `f : ℝ β†’ E` -/

section

variables {f : ℝ β†’ E} {a b : ℝ}

/-- General fencing theorem for continuous functions with an estimate on the derivative.
Let `f` and `B` be continuous functions on `[a, b]` such that

* `βˆ₯f aβˆ₯ ≀ B a`;
* `B` has right derivative at every point of `[a, b)`;
* for each `x ∈ [a, b)` the right-side limit inferior of `(βˆ₯f zβˆ₯ - βˆ₯f xβˆ₯) / (z - x)`
  is bounded above by a function `f'`;
* we have `f' x < B' x` whenever `βˆ₯f xβˆ₯ = B x`.

Then `βˆ₯f xβˆ₯ ≀ B x` everywhere on `[a, b]`. -/
lemma image_norm_le_of_liminf_right_slope_norm_lt_deriv_boundary {E : Type*}
  [normed_add_comm_group E] {f : ℝ β†’ E} {f' : ℝ β†’ ℝ} (hf : continuous_on f (Icc a b))
  -- `hf'` actually says `liminf (βˆ₯f zβˆ₯ - βˆ₯f xβˆ₯) / (z - x) ≀ f' x`
  (hf' : βˆ€ x ∈ Ico a b, βˆ€ r, f' x < r β†’
    βˆƒαΆ  z in 𝓝[>] x, slope (norm ∘ f) x z < r)
  {B B' : ℝ β†’ ℝ} (ha : βˆ₯f aβˆ₯ ≀ B a) (hB : continuous_on B (Icc a b))
  (hB' : βˆ€ x ∈ Ico a b, has_deriv_within_at B (B' x) (Ici x) x)
  (bound : βˆ€ x ∈ Ico a b, βˆ₯f xβˆ₯ = B x β†’ f' x < B' x) :
  βˆ€ ⦃x⦄, x ∈ Icc a b β†’ βˆ₯f xβˆ₯ ≀ B x :=
image_le_of_liminf_slope_right_lt_deriv_boundary' (continuous_norm.comp_continuous_on hf) hf'
    ha hB hB' bound

/-- General fencing theorem for continuous functions with an estimate on the norm of the derivative.
Let `f` and `B` be continuous functions on `[a, b]` such that

* `βˆ₯f aβˆ₯ ≀ B a`;
* `f` and `B` have right derivatives `f'` and `B'` respectively at every point of `[a, b)`;
* the norm of `f'` is strictly less than `B'` whenever `βˆ₯f xβˆ₯ = B x`.

Then `βˆ₯f xβˆ₯ ≀ B x` everywhere on `[a, b]`. We use one-sided derivatives in the assumptions
to make this theorem work for piecewise differentiable functions.
-/
lemma image_norm_le_of_norm_deriv_right_lt_deriv_boundary' {f' : ℝ β†’ E}
  (hf : continuous_on f (Icc a b))
  (hf' : βˆ€ x ∈ Ico a b, has_deriv_within_at f (f' x) (Ici x) x)
  {B B' : ℝ β†’ ℝ} (ha : βˆ₯f aβˆ₯ ≀ B a) (hB : continuous_on B (Icc a b))
  (hB' : βˆ€ x ∈ Ico a b, has_deriv_within_at B (B' x) (Ici x) x)
  (bound : βˆ€ x ∈ Ico a b, βˆ₯f xβˆ₯ = B x β†’ βˆ₯f' xβˆ₯ < B' x) :
  βˆ€ ⦃x⦄, x ∈ Icc a b β†’ βˆ₯f xβˆ₯ ≀ B x :=
image_norm_le_of_liminf_right_slope_norm_lt_deriv_boundary hf
  (Ξ» x hx r hr, (hf' x hx).liminf_right_slope_norm_le hr) ha hB hB' bound

/-- General fencing theorem for continuous functions with an estimate on the norm of the derivative.
Let `f` and `B` be continuous functions on `[a, b]` such that

* `βˆ₯f aβˆ₯ ≀ B a`;
* `f` has right derivative `f'` at every point of `[a, b)`;
* `B` has derivative `B'` everywhere on `ℝ`;
* the norm of `f'` is strictly less than `B'` whenever `βˆ₯f xβˆ₯ = B x`.

Then `βˆ₯f xβˆ₯ ≀ B x` everywhere on `[a, b]`. We use one-sided derivatives in the assumptions
to make this theorem work for piecewise differentiable functions.
-/
lemma image_norm_le_of_norm_deriv_right_lt_deriv_boundary {f' : ℝ β†’ E}
  (hf : continuous_on f (Icc a b))
  (hf' : βˆ€ x ∈ Ico a b, has_deriv_within_at f (f' x) (Ici x) x)
  {B B' : ℝ β†’ ℝ} (ha : βˆ₯f aβˆ₯ ≀ B a) (hB : βˆ€ x, has_deriv_at B (B' x) x)
  (bound : βˆ€ x ∈ Ico a b, βˆ₯f xβˆ₯ = B x β†’ βˆ₯f' xβˆ₯ < B' x) :
  βˆ€ ⦃x⦄, x ∈ Icc a b β†’ βˆ₯f xβˆ₯ ≀ B x :=
image_norm_le_of_norm_deriv_right_lt_deriv_boundary' hf hf' ha
  (Ξ» x hx, (hB x).continuous_at.continuous_within_at)
  (Ξ» x hx, (hB x).has_deriv_within_at) bound

/-- General fencing theorem for continuous functions with an estimate on the norm of the derivative.
Let `f` and `B` be continuous functions on `[a, b]` such that

* `βˆ₯f aβˆ₯ ≀ B a`;
* `f` and `B` have right derivatives `f'` and `B'` respectively at every point of `[a, b)`;
* we have `βˆ₯f' xβˆ₯ ≀ B x` everywhere on `[a, b)`.

Then `βˆ₯f xβˆ₯ ≀ B x` everywhere on `[a, b]`. We use one-sided derivatives in the assumptions
to make this theorem work for piecewise differentiable functions.
-/
lemma image_norm_le_of_norm_deriv_right_le_deriv_boundary' {f' : ℝ β†’ E}
  (hf : continuous_on f (Icc a b))
  (hf' : βˆ€ x ∈ Ico a b, has_deriv_within_at f (f' x) (Ici x) x)
  {B B' : ℝ β†’ ℝ} (ha : βˆ₯f aβˆ₯ ≀ B a) (hB : continuous_on B (Icc a b))
  (hB' : βˆ€ x ∈ Ico a b, has_deriv_within_at B (B' x) (Ici x) x)
  (bound : βˆ€ x ∈ Ico a b, βˆ₯f' xβˆ₯ ≀ B' x) :
  βˆ€ ⦃x⦄, x ∈ Icc a b β†’ βˆ₯f xβˆ₯ ≀ B x :=
image_le_of_liminf_slope_right_le_deriv_boundary (continuous_norm.comp_continuous_on hf) ha hB hB' $
  (Ξ» x hx r hr, (hf' x hx).liminf_right_slope_norm_le (lt_of_le_of_lt (bound x hx) hr))

/-- General fencing theorem for continuous functions with an estimate on the norm of the derivative.
Let `f` and `B` be continuous functions on `[a, b]` such that

* `βˆ₯f aβˆ₯ ≀ B a`;
* `f` has right derivative `f'` at every point of `[a, b)`;
* `B` has derivative `B'` everywhere on `ℝ`;
* we have `βˆ₯f' xβˆ₯ ≀ B x` everywhere on `[a, b)`.

Then `βˆ₯f xβˆ₯ ≀ B x` everywhere on `[a, b]`. We use one-sided derivatives in the assumptions
to make this theorem work for piecewise differentiable functions.
-/
lemma image_norm_le_of_norm_deriv_right_le_deriv_boundary {f' : ℝ β†’ E}
  (hf : continuous_on f (Icc a b))
  (hf' : βˆ€ x ∈ Ico a b, has_deriv_within_at f (f' x) (Ici x) x)
  {B B' : ℝ β†’ ℝ} (ha : βˆ₯f aβˆ₯ ≀ B a) (hB : βˆ€ x, has_deriv_at B (B' x) x)
  (bound : βˆ€ x ∈ Ico a b, βˆ₯f' xβˆ₯ ≀ B' x) :
  βˆ€ ⦃x⦄, x ∈ Icc a b β†’ βˆ₯f xβˆ₯ ≀ B x :=
image_norm_le_of_norm_deriv_right_le_deriv_boundary' hf hf' ha
  (Ξ» x hx, (hB x).continuous_at.continuous_within_at)
  (Ξ» x hx, (hB x).has_deriv_within_at) bound

/-- A function on `[a, b]` with the norm of the right derivative bounded by `C`
satisfies `βˆ₯f x - f aβˆ₯ ≀ C * (x - a)`. -/
theorem norm_image_sub_le_of_norm_deriv_right_le_segment {f' : ℝ β†’ E} {C : ℝ}
  (hf : continuous_on f (Icc a b))
  (hf' : βˆ€ x ∈ Ico a b, has_deriv_within_at f (f' x) (Ici x) x)
  (bound : βˆ€x ∈ Ico a b, βˆ₯f' xβˆ₯ ≀ C) :
  βˆ€ x ∈ Icc a b, βˆ₯f x - f aβˆ₯ ≀ C * (x - a) :=
begin
  let g := Ξ» x, f x - f a,
  have hg : continuous_on g (Icc a b), from hf.sub continuous_on_const,
  have hg' : βˆ€ x ∈ Ico a b, has_deriv_within_at g (f' x) (Ici x) x,
  { assume x hx,
    simpa using (hf' x hx).sub (has_deriv_within_at_const _ _ _) },
  let B := Ξ» x, C * (x - a),
  have hB : βˆ€ x, has_deriv_at B C x,
  { assume x,
    simpa using (has_deriv_at_const x C).mul ((has_deriv_at_id x).sub (has_deriv_at_const x a)) },
  convert image_norm_le_of_norm_deriv_right_le_deriv_boundary hg hg' _ hB bound,
  simp only [g, B], rw [sub_self, norm_zero, sub_self, mul_zero]
end

/-- A function on `[a, b]` with the norm of the derivative within `[a, b]`
bounded by `C` satisfies `βˆ₯f x - f aβˆ₯ ≀ C * (x - a)`, `has_deriv_within_at`
version. -/
theorem norm_image_sub_le_of_norm_deriv_le_segment' {f' : ℝ β†’ E} {C : ℝ}
  (hf : βˆ€ x ∈ Icc a b, has_deriv_within_at f (f' x) (Icc a b) x)
  (bound : βˆ€x ∈ Ico a b, βˆ₯f' xβˆ₯ ≀ C) :
  βˆ€ x ∈ Icc a b, βˆ₯f x - f aβˆ₯ ≀ C * (x - a) :=
begin
  refine norm_image_sub_le_of_norm_deriv_right_le_segment
    (Ξ» x hx, (hf x hx).continuous_within_at) (Ξ» x hx, _) bound,
  exact (hf x $ Ico_subset_Icc_self hx).nhds_within (Icc_mem_nhds_within_Ici hx)
end

/-- A function on `[a, b]` with the norm of the derivative within `[a, b]`
bounded by `C` satisfies `βˆ₯f x - f aβˆ₯ ≀ C * (x - a)`, `deriv_within`
version. -/
theorem norm_image_sub_le_of_norm_deriv_le_segment {C : ℝ} (hf : differentiable_on ℝ f (Icc a b))
  (bound : βˆ€x ∈ Ico a b, βˆ₯deriv_within f (Icc a b) xβˆ₯ ≀ C) :
  βˆ€ x ∈ Icc a b, βˆ₯f x - f aβˆ₯ ≀ C * (x - a) :=
begin
  refine norm_image_sub_le_of_norm_deriv_le_segment' _ bound,
  exact Ξ» x hx, (hf x  hx).has_deriv_within_at
end

/-- A function on `[0, 1]` with the norm of the derivative within `[0, 1]`
bounded by `C` satisfies `βˆ₯f 1 - f 0βˆ₯ ≀ C`, `has_deriv_within_at`
version. -/
theorem norm_image_sub_le_of_norm_deriv_le_segment_01' {f' : ℝ β†’ E} {C : ℝ}
  (hf : βˆ€ x ∈ Icc (0:ℝ) 1, has_deriv_within_at f (f' x) (Icc (0:ℝ) 1) x)
  (bound : βˆ€x ∈ Ico (0:ℝ) 1, βˆ₯f' xβˆ₯ ≀ C) :
  βˆ₯f 1 - f 0βˆ₯ ≀ C :=
by simpa only [sub_zero, mul_one]
  using norm_image_sub_le_of_norm_deriv_le_segment' hf bound 1 (right_mem_Icc.2 zero_le_one)

/-- A function on `[0, 1]` with the norm of the derivative within `[0, 1]`
bounded by `C` satisfies `βˆ₯f 1 - f 0βˆ₯ ≀ C`, `deriv_within` version. -/
theorem norm_image_sub_le_of_norm_deriv_le_segment_01 {C : ℝ}
  (hf : differentiable_on ℝ f (Icc (0:ℝ) 1))
  (bound : βˆ€x ∈ Ico (0:ℝ) 1, βˆ₯deriv_within f (Icc (0:ℝ) 1) xβˆ₯ ≀ C) :
  βˆ₯f 1 - f 0βˆ₯ ≀ C :=
by simpa only [sub_zero, mul_one]
  using norm_image_sub_le_of_norm_deriv_le_segment hf bound 1 (right_mem_Icc.2 zero_le_one)

theorem constant_of_has_deriv_right_zero (hcont : continuous_on f (Icc a b))
  (hderiv : βˆ€ x ∈ Ico a b, has_deriv_within_at f 0 (Ici x) x) :
  βˆ€ x ∈ Icc a b, f x = f a :=
by simpa only [zero_mul, norm_le_zero_iff, sub_eq_zero] using
  Ξ» x hx, norm_image_sub_le_of_norm_deriv_right_le_segment
    hcont hderiv (Ξ» y hy, by rw norm_le_zero_iff) x hx

theorem constant_of_deriv_within_zero (hdiff : differentiable_on ℝ f (Icc a b))
  (hderiv : βˆ€ x ∈ Ico a b, deriv_within f (Icc a b) x = 0) :
  βˆ€ x ∈ Icc a b, f x = f a :=
begin
  have H : βˆ€ x ∈ Ico a b, βˆ₯deriv_within f (Icc a b) xβˆ₯ ≀ 0 :=
    by simpa only [norm_le_zero_iff] using Ξ» x hx, hderiv x hx,
  simpa only [zero_mul, norm_le_zero_iff, sub_eq_zero] using
    Ξ» x hx, norm_image_sub_le_of_norm_deriv_le_segment hdiff H x hx,
end

variables {f' g : ℝ β†’ E}

/-- If two continuous functions on `[a, b]` have the same right derivative and are equal at `a`,
  then they are equal everywhere on `[a, b]`. -/
theorem eq_of_has_deriv_right_eq
  (derivf : βˆ€ x ∈ Ico a b, has_deriv_within_at f (f' x) (Ici x) x)
  (derivg : βˆ€ x ∈ Ico a b, has_deriv_within_at g (f' x) (Ici x) x)
  (fcont : continuous_on f (Icc a b)) (gcont : continuous_on g (Icc a b))
  (hi : f a = g a) :
  βˆ€ y ∈ Icc a b, f y = g y :=
begin
  simp only [← @sub_eq_zero _ _ (f _)] at hi ⊒,
  exact hi β–Έ constant_of_has_deriv_right_zero (fcont.sub gcont)
    (Ξ» y hy, by simpa only [sub_self] using (derivf y hy).sub (derivg y hy)),
end

/-- If two differentiable functions on `[a, b]` have the same derivative within `[a, b]` everywhere
  on `[a, b)` and are equal at `a`, then they are equal everywhere on `[a, b]`. -/
theorem eq_of_deriv_within_eq (fdiff : differentiable_on ℝ f (Icc a b))
  (gdiff : differentiable_on ℝ g (Icc a b))
  (hderiv : eq_on (deriv_within f (Icc a b)) (deriv_within g (Icc a b)) (Ico a b))
  (hi : f a = g a) :
  βˆ€ y ∈ Icc a b, f y = g y :=
begin
  have A : βˆ€ y ∈ Ico a b, has_deriv_within_at f (deriv_within f (Icc a b) y) (Ici y) y :=
    Ξ» y hy, (fdiff y (mem_Icc_of_Ico hy)).has_deriv_within_at.nhds_within
      (Icc_mem_nhds_within_Ici hy),
  have B : βˆ€ y ∈ Ico a b, has_deriv_within_at g (deriv_within g (Icc a b) y) (Ici y) y :=
    Ξ» y hy, (gdiff y (mem_Icc_of_Ico hy)).has_deriv_within_at.nhds_within
      (Icc_mem_nhds_within_Ici hy),
  exact eq_of_has_deriv_right_eq A (Ξ» y hy, (hderiv hy).symm β–Έ B y hy) fdiff.continuous_on
    gdiff.continuous_on hi
end

end

/-!
### Vector-valued functions `f : E β†’ G`

Theorems in this section work both for real and complex differentiable functions. We use assumptions
`[is_R_or_C π•œ] [normed_space π•œ E] [normed_space π•œ G]` to achieve this result. For the domain `E` we
also assume `[normed_space ℝ E]` to have a notion of a `convex` set. -/

section

variables {π•œ G : Type*} [is_R_or_C π•œ] [normed_space π•œ E] [normed_add_comm_group G]
  [normed_space π•œ G]

namespace convex

variables  {f : E β†’ G} {C : ℝ} {s : set E} {x y : E} {f' : E β†’ E β†’L[π•œ] G} {Ο† : E β†’L[π•œ] G}

/-- The mean value theorem on a convex set: if the derivative of a function is bounded by `C`, then
the function is `C`-Lipschitz. Version with `has_fderiv_within`. -/
theorem norm_image_sub_le_of_norm_has_fderiv_within_le
  (hf : βˆ€ x ∈ s, has_fderiv_within_at f (f' x) s x) (bound : βˆ€x∈s, βˆ₯f' xβˆ₯ ≀ C)
  (hs : convex ℝ s) (xs : x ∈ s) (ys : y ∈ s) : βˆ₯f y - f xβˆ₯ ≀ C * βˆ₯y - xβˆ₯ :=
begin
  letI : normed_space ℝ G := restrict_scalars.normed_space ℝ π•œ G,
  /- By composition with `t ↦ x + t β€’ (y-x)`, we reduce to a statement for functions defined
  on `[0,1]`, for which it is proved in `norm_image_sub_le_of_norm_deriv_le_segment`.
  We just have to check the differentiability of the composition and bounds on its derivative,
  which is straightforward but tedious for lack of automation. -/
  have C0 : 0 ≀ C := le_trans (norm_nonneg _) (bound x xs),
  set g : ℝ β†’ E := Ξ» t, x + t β€’ (y - x),
  have Dg : βˆ€ t, has_deriv_at g (y-x) t,
  { assume t,
    simpa only [one_smul] using ((has_deriv_at_id t).smul_const (y - x)).const_add x },
  have segm : Icc 0 1 βŠ† g ⁻¹' s,
  { rw [← image_subset_iff, ← segment_eq_image'],
    apply hs.segment_subset xs ys },
  have : f x = f (g 0), by { simp only [g], rw [zero_smul, add_zero] },
  rw this,
  have : f y = f (g 1), by { simp only [g], rw [one_smul, add_sub_cancel'_right] },
  rw this,
  have D2: βˆ€ t ∈ Icc (0:ℝ) 1, has_deriv_within_at (f ∘ g) (f' (g t) (y - x)) (Icc 0 1) t,
  { intros t ht,
    have : has_fderiv_within_at f ((f' (g t)).restrict_scalars ℝ) s (g t),
      from hf (g t) (segm ht),
    exact this.comp_has_deriv_within_at _ (Dg t).has_deriv_within_at segm },
  apply norm_image_sub_le_of_norm_deriv_le_segment_01' D2,
  refine Ξ» t ht, le_of_op_norm_le _ _ _,
  exact bound (g t) (segm $ Ico_subset_Icc_self ht)
end

/-- The mean value theorem on a convex set: if the derivative of a function is bounded by `C` on
`s`, then the function is `C`-Lipschitz on `s`. Version with `has_fderiv_within` and
`lipschitz_on_with`. -/
theorem lipschitz_on_with_of_nnnorm_has_fderiv_within_le {C : ℝβ‰₯0}
  (hf : βˆ€ x ∈ s, has_fderiv_within_at f (f' x) s x) (bound : βˆ€x∈s, βˆ₯f' xβˆ₯β‚Š ≀ C)
  (hs : convex ℝ s) : lipschitz_on_with C f s :=
begin
  rw lipschitz_on_with_iff_norm_sub_le,
  intros x x_in y y_in,
  exact hs.norm_image_sub_le_of_norm_has_fderiv_within_le hf bound y_in x_in
end

/-- Let `s` be a convex set in a real normed vector space `E`, let `f : E β†’ G` be a function
differentiable within `s` in a neighborhood of `x : E` with derivative `f'`. Suppose that `f'` is
continuous within `s` at `x`. Then for any number `K : ℝβ‰₯0` larger than `βˆ₯f' xβˆ₯β‚Š`, `f` is
`K`-Lipschitz on some neighborhood of `x` within `s`. See also
`convex.exists_nhds_within_lipschitz_on_with_of_has_fderiv_within_at` for a version that claims
existence of `K` instead of an explicit estimate. -/
lemma exists_nhds_within_lipschitz_on_with_of_has_fderiv_within_at_of_nnnorm_lt
  (hs : convex ℝ s) {f : E β†’ G} (hder : βˆ€αΆ  y in 𝓝[s] x, has_fderiv_within_at f (f' y) s y)
  (hcont : continuous_within_at f' s x) (K : ℝβ‰₯0) (hK : βˆ₯f' xβˆ₯β‚Š < K) :
  βˆƒ t ∈ 𝓝[s] x, lipschitz_on_with K f t :=
begin
  obtain ⟨Ρ, Ρ0, hΡ⟩ :
    βˆƒ Ξ΅ > 0, ball x Ξ΅ ∩ s βŠ† {y | has_fderiv_within_at f (f' y) s y ∧ βˆ₯f' yβˆ₯β‚Š < K},
    from mem_nhds_within_iff.1 (hder.and $ hcont.nnnorm.eventually (gt_mem_nhds hK)),
  rw inter_comm at hΞ΅,
  refine ⟨s ∩ ball x Ρ, inter_mem_nhds_within _ (ball_mem_nhds _ Ρ0), _⟩,
  exact (hs.inter (convex_ball _ _)).lipschitz_on_with_of_nnnorm_has_fderiv_within_le
    (Ξ» y hy, (hΞ΅ hy).1.mono (inter_subset_left _ _)) (Ξ» y hy, (hΞ΅ hy).2.le)
end

/-- Let `s` be a convex set in a real normed vector space `E`, let `f : E β†’ G` be a function
differentiable within `s` in a neighborhood of `x : E` with derivative `f'`. Suppose that `f'` is
continuous within `s` at `x`. Then for any number `K : ℝβ‰₯0` larger than `βˆ₯f' xβˆ₯β‚Š`, `f` is Lipschitz
on some neighborhood of `x` within `s`. See also
`convex.exists_nhds_within_lipschitz_on_with_of_has_fderiv_within_at_of_nnnorm_lt` for a version
with an explicit estimate on the Lipschitz constant. -/
lemma exists_nhds_within_lipschitz_on_with_of_has_fderiv_within_at
  (hs : convex ℝ s) {f : E β†’ G} (hder : βˆ€αΆ  y in 𝓝[s] x, has_fderiv_within_at f (f' y) s y)
  (hcont : continuous_within_at f' s x) :
  βˆƒ K (t ∈ 𝓝[s] x), lipschitz_on_with K f t :=
(exists_gt _).imp $
  hs.exists_nhds_within_lipschitz_on_with_of_has_fderiv_within_at_of_nnnorm_lt hder hcont

/-- The mean value theorem on a convex set: if the derivative of a function within this set is
bounded by `C`, then the function is `C`-Lipschitz. Version with `fderiv_within`. -/
theorem norm_image_sub_le_of_norm_fderiv_within_le
  (hf : differentiable_on π•œ f s) (bound : βˆ€x∈s, βˆ₯fderiv_within π•œ f s xβˆ₯ ≀ C)
  (hs : convex ℝ s) (xs : x ∈ s) (ys : y ∈ s) : βˆ₯f y - f xβˆ₯ ≀ C * βˆ₯y - xβˆ₯ :=
hs.norm_image_sub_le_of_norm_has_fderiv_within_le (Ξ» x hx, (hf x hx).has_fderiv_within_at)
bound xs ys

/-- The mean value theorem on a convex set: if the derivative of a function is bounded by `C` on
`s`, then the function is `C`-Lipschitz on `s`. Version with `fderiv_within` and
`lipschitz_on_with`. -/
theorem lipschitz_on_with_of_nnnorm_fderiv_within_le {C : ℝβ‰₯0}
  (hf : differentiable_on π•œ f s) (bound : βˆ€ x ∈ s, βˆ₯fderiv_within π•œ f s xβˆ₯β‚Š ≀ C)
  (hs : convex ℝ s) : lipschitz_on_with C f s:=
hs.lipschitz_on_with_of_nnnorm_has_fderiv_within_le (Ξ» x hx, (hf x hx).has_fderiv_within_at) bound

/-- The mean value theorem on a convex set: if the derivative of a function is bounded by `C`,
then the function is `C`-Lipschitz. Version with `fderiv`. -/
theorem norm_image_sub_le_of_norm_fderiv_le
  (hf : βˆ€ x ∈ s, differentiable_at π•œ f x) (bound : βˆ€x∈s, βˆ₯fderiv π•œ f xβˆ₯ ≀ C)
  (hs : convex ℝ s) (xs : x ∈ s) (ys : y ∈ s) : βˆ₯f y - f xβˆ₯ ≀ C * βˆ₯y - xβˆ₯ :=
hs.norm_image_sub_le_of_norm_has_fderiv_within_le
(Ξ» x hx, (hf x hx).has_fderiv_at.has_fderiv_within_at) bound xs ys

/-- The mean value theorem on a convex set: if the derivative of a function is bounded by `C` on
`s`, then the function is `C`-Lipschitz on `s`. Version with `fderiv` and `lipschitz_on_with`. -/
theorem lipschitz_on_with_of_nnnorm_fderiv_le {C : ℝβ‰₯0}
  (hf : βˆ€ x ∈ s, differentiable_at π•œ f x) (bound : βˆ€x∈s, βˆ₯fderiv π•œ f xβˆ₯β‚Š ≀ C)
  (hs : convex ℝ s) : lipschitz_on_with C f s :=
hs.lipschitz_on_with_of_nnnorm_has_fderiv_within_le
(Ξ» x hx, (hf x hx).has_fderiv_at.has_fderiv_within_at) bound

/-- Variant of the mean value inequality on a convex set, using a bound on the difference between
the derivative and a fixed linear map, rather than a bound on the derivative itself. Version with
`has_fderiv_within`. -/
theorem norm_image_sub_le_of_norm_has_fderiv_within_le'
  (hf : βˆ€ x ∈ s, has_fderiv_within_at f (f' x) s x) (bound : βˆ€x∈s, βˆ₯f' x - Ο†βˆ₯ ≀ C)
  (hs : convex ℝ s) (xs : x ∈ s) (ys : y ∈ s) : βˆ₯f y - f x - Ο† (y - x)βˆ₯ ≀ C * βˆ₯y - xβˆ₯ :=
begin
  /- We subtract `Ο†` to define a new function `g` for which `g' = 0`, for which the previous theorem
  applies, `convex.norm_image_sub_le_of_norm_has_fderiv_within_le`. Then, we just need to glue
  together the pieces, expressing back `f` in terms of `g`. -/
  let g := Ξ»y, f y - Ο† y,
  have hg : βˆ€ x ∈ s, has_fderiv_within_at g (f' x - Ο†) s x :=
    Ξ» x xs, (hf x xs).sub Ο†.has_fderiv_within_at,
  calc βˆ₯f y - f x - Ο† (y - x)βˆ₯ = βˆ₯f y - f x - (Ο† y - Ο† x)βˆ₯ : by simp
  ... = βˆ₯(f y - Ο† y) - (f x - Ο† x)βˆ₯ : by abel
  ... = βˆ₯g y - g xβˆ₯ : by simp
  ... ≀ C * βˆ₯y - xβˆ₯ : convex.norm_image_sub_le_of_norm_has_fderiv_within_le hg bound hs xs ys,
end

/-- Variant of the mean value inequality on a convex set. Version with `fderiv_within`. -/
theorem norm_image_sub_le_of_norm_fderiv_within_le'
  (hf : differentiable_on π•œ f s) (bound : βˆ€x∈s, βˆ₯fderiv_within π•œ f s x - Ο†βˆ₯ ≀ C)
  (hs : convex ℝ s) (xs : x ∈ s) (ys : y ∈ s) : βˆ₯f y - f x - Ο† (y - x)βˆ₯ ≀ C * βˆ₯y - xβˆ₯ :=
hs.norm_image_sub_le_of_norm_has_fderiv_within_le' (Ξ» x hx, (hf x hx).has_fderiv_within_at)
bound xs ys

/-- Variant of the mean value inequality on a convex set. Version with `fderiv`. -/
theorem norm_image_sub_le_of_norm_fderiv_le'
  (hf : βˆ€ x ∈ s, differentiable_at π•œ f x) (bound : βˆ€x∈s, βˆ₯fderiv π•œ f x - Ο†βˆ₯ ≀ C)
  (hs : convex ℝ s) (xs : x ∈ s) (ys : y ∈ s) : βˆ₯f y - f x - Ο† (y - x)βˆ₯ ≀ C * βˆ₯y - xβˆ₯ :=
hs.norm_image_sub_le_of_norm_has_fderiv_within_le'
(Ξ» x hx, (hf x hx).has_fderiv_at.has_fderiv_within_at) bound xs ys

/-- If a function has zero FrΓ©chet derivative at every point of a convex set,
then it is a constant on this set. -/
theorem is_const_of_fderiv_within_eq_zero (hs : convex ℝ s) (hf : differentiable_on π•œ f s)
  (hf' : βˆ€ x ∈ s, fderiv_within π•œ f s x = 0) (hx : x ∈ s) (hy : y ∈ s) :
  f x = f y :=
have bound : βˆ€ x ∈ s, βˆ₯fderiv_within π•œ f s xβˆ₯ ≀ 0,
  from Ξ» x hx, by simp only [hf' x hx, norm_zero],
by simpa only [(dist_eq_norm _ _).symm, zero_mul, dist_le_zero, eq_comm]
  using hs.norm_image_sub_le_of_norm_fderiv_within_le hf bound hx hy

theorem _root_.is_const_of_fderiv_eq_zero (hf : differentiable π•œ f) (hf' : βˆ€ x, fderiv π•œ f x = 0)
  (x y : E) :
  f x = f y :=
convex_univ.is_const_of_fderiv_within_eq_zero hf.differentiable_on
  (Ξ» x _, by rw fderiv_within_univ; exact hf' x) trivial trivial

end convex

namespace convex

variables {f f' : π•œ β†’ G} {s : set π•œ} {x y : π•œ}

/-- The mean value theorem on a convex set in dimension 1: if the derivative of a function is
bounded by `C`, then the function is `C`-Lipschitz. Version with `has_deriv_within`. -/
theorem norm_image_sub_le_of_norm_has_deriv_within_le {C : ℝ}
  (hf : βˆ€ x ∈ s, has_deriv_within_at f (f' x) s x) (bound : βˆ€x∈s, βˆ₯f' xβˆ₯ ≀ C)
  (hs : convex ℝ s) (xs : x ∈ s) (ys : y ∈ s) : βˆ₯f y - f xβˆ₯ ≀ C * βˆ₯y - xβˆ₯ :=
convex.norm_image_sub_le_of_norm_has_fderiv_within_le (Ξ» x hx, (hf x hx).has_fderiv_within_at)
(Ξ» x hx, le_trans (by simp) (bound x hx)) hs xs ys

/-- The mean value theorem on a convex set in dimension 1: if the derivative of a function is
bounded by `C` on `s`, then the function is `C`-Lipschitz on `s`.
Version with `has_deriv_within` and `lipschitz_on_with`. -/
theorem lipschitz_on_with_of_nnnorm_has_deriv_within_le {C : ℝβ‰₯0} (hs : convex ℝ s)
  (hf : βˆ€ x ∈ s, has_deriv_within_at f (f' x) s x) (bound : βˆ€x∈s, βˆ₯f' xβˆ₯β‚Š ≀ C) :
  lipschitz_on_with C f s :=
convex.lipschitz_on_with_of_nnnorm_has_fderiv_within_le (Ξ» x hx, (hf x hx).has_fderiv_within_at)
(Ξ» x hx, le_trans (by simp) (bound x hx)) hs

/-- The mean value theorem on a convex set in dimension 1: if the derivative of a function within
this set is bounded by `C`, then the function is `C`-Lipschitz. Version with `deriv_within` -/
theorem norm_image_sub_le_of_norm_deriv_within_le {C : ℝ}
  (hf : differentiable_on π•œ f s) (bound : βˆ€x∈s, βˆ₯deriv_within f s xβˆ₯ ≀ C)
  (hs : convex ℝ s) (xs : x ∈ s) (ys : y ∈ s) : βˆ₯f y - f xβˆ₯ ≀ C * βˆ₯y - xβˆ₯ :=
hs.norm_image_sub_le_of_norm_has_deriv_within_le (Ξ» x hx, (hf x hx).has_deriv_within_at)
bound xs ys

/-- The mean value theorem on a convex set in dimension 1: if the derivative of a function is
bounded by `C` on `s`, then the function is `C`-Lipschitz on `s`.
Version with `deriv_within` and `lipschitz_on_with`. -/
theorem lipschitz_on_with_of_nnnorm_deriv_within_le {C : ℝβ‰₯0} (hs : convex ℝ s)
  (hf : differentiable_on π•œ f s) (bound : βˆ€x∈s, βˆ₯deriv_within f s xβˆ₯β‚Š ≀ C) :
  lipschitz_on_with C f s :=
hs.lipschitz_on_with_of_nnnorm_has_deriv_within_le (Ξ» x hx, (hf x hx).has_deriv_within_at) bound

/-- The mean value theorem on a convex set in dimension 1: if the derivative of a function is
bounded by `C`, then the function is `C`-Lipschitz. Version with `deriv`. -/
theorem norm_image_sub_le_of_norm_deriv_le {C : ℝ}
  (hf : βˆ€ x ∈ s, differentiable_at π•œ f x) (bound : βˆ€x∈s, βˆ₯deriv f xβˆ₯ ≀ C)
  (hs : convex ℝ s) (xs : x ∈ s) (ys : y ∈ s) : βˆ₯f y - f xβˆ₯ ≀ C * βˆ₯y - xβˆ₯ :=
hs.norm_image_sub_le_of_norm_has_deriv_within_le
(Ξ» x hx, (hf x hx).has_deriv_at.has_deriv_within_at) bound xs ys

/-- The mean value theorem on a convex set in dimension 1: if the derivative of a function is
bounded by `C` on `s`, then the function is `C`-Lipschitz on `s`.
Version with `deriv` and `lipschitz_on_with`. -/
theorem lipschitz_on_with_of_nnnorm_deriv_le {C : ℝβ‰₯0}
  (hf : βˆ€ x ∈ s, differentiable_at π•œ f x) (bound : βˆ€x∈s, βˆ₯deriv f xβˆ₯β‚Š ≀ C)
  (hs : convex ℝ s) : lipschitz_on_with C f s :=
hs.lipschitz_on_with_of_nnnorm_has_deriv_within_le
(Ξ» x hx, (hf x hx).has_deriv_at.has_deriv_within_at) bound

/-- The mean value theorem set in dimension 1: if the derivative of a function is bounded by `C`,
then the function is `C`-Lipschitz.  Version with `deriv` and `lipschitz_with`. -/
theorem _root_.lipschitz_with_of_nnnorm_deriv_le {C : ℝβ‰₯0} (hf : differentiable π•œ f)
  (bound : βˆ€ x, βˆ₯deriv f xβˆ₯β‚Š ≀ C) : lipschitz_with C f :=
lipschitz_on_univ.1 $ convex_univ.lipschitz_on_with_of_nnnorm_deriv_le (Ξ» x hx, hf x)
  (Ξ» x hx, bound x)

/-- If `f : π•œ β†’ G`, `π•œ = R` or `π•œ = β„‚`, is differentiable everywhere and its derivative equal zero,
then it is a constant function. -/
theorem _root_.is_const_of_deriv_eq_zero (hf : differentiable π•œ f) (hf' : βˆ€ x, deriv f x = 0)
  (x y : π•œ) :
  f x = f y :=
is_const_of_fderiv_eq_zero hf (Ξ» z, by { ext, simp [← deriv_fderiv, hf'] }) _ _

end convex

end

/-! ### Functions `[a, b] β†’ ℝ`. -/

section interval

-- Declare all variables here to make sure they come in a correct order
variables (f f' : ℝ β†’ ℝ) {a b : ℝ} (hab : a < b) (hfc : continuous_on f (Icc a b))
  (hff' : βˆ€ x ∈ Ioo a b, has_deriv_at f (f' x) x) (hfd : differentiable_on ℝ f (Ioo a b))
  (g g' : ℝ β†’ ℝ) (hgc : continuous_on g (Icc a b)) (hgg' : βˆ€ x ∈ Ioo a b, has_deriv_at g (g' x) x)
  (hgd : differentiable_on ℝ g (Ioo a b))

include hab hfc hff' hgc hgg'

/-- Cauchy's **Mean Value Theorem**, `has_deriv_at` version. -/
lemma exists_ratio_has_deriv_at_eq_ratio_slope :
  βˆƒ c ∈ Ioo a b, (g b - g a) * f' c = (f b - f a) * g' c :=
begin
  let h := Ξ» x, (g b - g a) * f x - (f b - f a) * g x,
  have hI : h a = h b,
  { simp only [h], ring },
  let h' := Ξ» x, (g b - g a) * f' x - (f b - f a) * g' x,
  have hhh' : βˆ€ x ∈ Ioo a b, has_deriv_at h (h' x) x,
    from Ξ» x hx, ((hff' x hx).const_mul (g b - g a)).sub ((hgg' x hx).const_mul (f b - f a)),
  have hhc : continuous_on h (Icc a b),
    from (continuous_on_const.mul hfc).sub (continuous_on_const.mul hgc),
  rcases exists_has_deriv_at_eq_zero h h' hab hhc hI hhh' with ⟨c, cmem, hc⟩,
  exact ⟨c, cmem, sub_eq_zero.1 hc⟩
end

omit hfc hgc

/-- Cauchy's **Mean Value Theorem**, extended `has_deriv_at` version. -/
lemma exists_ratio_has_deriv_at_eq_ratio_slope' {lfa lga lfb lgb : ℝ}
  (hff' : βˆ€ x ∈ Ioo a b, has_deriv_at f (f' x) x) (hgg' : βˆ€ x ∈ Ioo a b, has_deriv_at g (g' x) x)
  (hfa : tendsto f (𝓝[>] a) (𝓝 lfa)) (hga : tendsto g (𝓝[>] a) (𝓝 lga))
  (hfb : tendsto f (𝓝[<] b) (𝓝 lfb)) (hgb : tendsto g (𝓝[<] b) (𝓝 lgb)) :
  βˆƒ c ∈ Ioo a b, (lgb - lga) * (f' c) = (lfb - lfa) * (g' c) :=
begin
  let h := Ξ» x, (lgb - lga) * f x - (lfb - lfa) * g x,
  have hha : tendsto h (𝓝[>] a) (𝓝 $ lgb * lfa - lfb * lga),
  { have : tendsto h (𝓝[>] a)(𝓝 $ (lgb - lga) * lfa - (lfb - lfa) * lga) :=
      (tendsto_const_nhds.mul hfa).sub (tendsto_const_nhds.mul hga),
    convert this using 2,
    ring },
  have hhb : tendsto h (𝓝[<] b) (𝓝 $ lgb * lfa - lfb * lga),
  { have : tendsto h (𝓝[<] b)(𝓝 $ (lgb - lga) * lfb - (lfb - lfa) * lgb) :=
      (tendsto_const_nhds.mul hfb).sub (tendsto_const_nhds.mul hgb),
    convert this using 2,
    ring },
  let h' := Ξ» x, (lgb - lga) * f' x - (lfb - lfa) * g' x,
  have hhh' : βˆ€ x ∈ Ioo a b, has_deriv_at h (h' x) x,
  { intros x hx,
    exact ((hff' x hx).const_mul _ ).sub (((hgg' x hx)).const_mul _) },
  rcases exists_has_deriv_at_eq_zero' hab hha hhb hhh' with ⟨c, cmem, hc⟩,
  exact ⟨c, cmem, sub_eq_zero.1 hc⟩
end

include hfc

omit hgg'

/-- Lagrange's Mean Value Theorem, `has_deriv_at` version -/
lemma exists_has_deriv_at_eq_slope : βˆƒ c ∈ Ioo a b, f' c = (f b - f a) / (b - a) :=
begin
  rcases exists_ratio_has_deriv_at_eq_ratio_slope f f' hab hfc hff'
    id 1 continuous_id.continuous_on (λ x hx, has_deriv_at_id x) with ⟨c, cmem, hc⟩,
  use [c, cmem],
  simp only [_root_.id, pi.one_apply, mul_one] at hc,
  rw [← hc, mul_div_cancel_left],
  exact ne_of_gt (sub_pos.2 hab)
end

omit hff'

/-- Cauchy's Mean Value Theorem, `deriv` version. -/
lemma exists_ratio_deriv_eq_ratio_slope :
  βˆƒ c ∈ Ioo a b, (g b - g a) * (deriv f c) = (f b - f a) * (deriv g c) :=
exists_ratio_has_deriv_at_eq_ratio_slope f (deriv f) hab hfc
  (Ξ» x hx, ((hfd x hx).differentiable_at $ is_open.mem_nhds is_open_Ioo hx).has_deriv_at)
  g (deriv g) hgc $
    Ξ» x hx, ((hgd x hx).differentiable_at $ is_open.mem_nhds is_open_Ioo hx).has_deriv_at

omit hfc

/-- Cauchy's Mean Value Theorem, extended `deriv` version. -/
lemma exists_ratio_deriv_eq_ratio_slope' {lfa lga lfb lgb : ℝ}
  (hdf : differentiable_on ℝ f $ Ioo a b) (hdg : differentiable_on ℝ g $ Ioo a b)
  (hfa : tendsto f (𝓝[>] a) (𝓝 lfa)) (hga : tendsto g (𝓝[>] a) (𝓝 lga))
  (hfb : tendsto f (𝓝[<] b) (𝓝 lfb)) (hgb : tendsto g (𝓝[<] b) (𝓝 lgb)) :
  βˆƒ c ∈ Ioo a b, (lgb - lga) * (deriv f c) = (lfb - lfa) * (deriv g c) :=
exists_ratio_has_deriv_at_eq_ratio_slope' _ _ hab _ _
  (Ξ» x hx, ((hdf x hx).differentiable_at $ Ioo_mem_nhds hx.1 hx.2).has_deriv_at)
  (Ξ» x hx, ((hdg x hx).differentiable_at $ Ioo_mem_nhds hx.1 hx.2).has_deriv_at)
  hfa hga hfb hgb

/-- Lagrange's **Mean Value Theorem**, `deriv` version. -/
lemma exists_deriv_eq_slope : βˆƒ c ∈ Ioo a b, deriv f c = (f b - f a) / (b - a) :=
exists_has_deriv_at_eq_slope f (deriv f) hab hfc
  (Ξ» x hx, ((hfd x hx).differentiable_at $ is_open.mem_nhds is_open_Ioo hx).has_deriv_at)

end interval

/-- Let `f` be a function continuous on a convex (or, equivalently, connected) subset `D`
of the real line. If `f` is differentiable on the interior of `D` and `C < f'`, then
`f` grows faster than `C * x` on `D`, i.e., `C * (y - x) < f y - f x` whenever `x, y ∈ D`,
`x < y`. -/
theorem convex.mul_sub_lt_image_sub_of_lt_deriv {D : set ℝ} (hD : convex ℝ D) {f : ℝ β†’ ℝ}
  (hf : continuous_on f D) (hf' : differentiable_on ℝ f (interior D))
  {C} (hf'_gt : βˆ€ x ∈ interior D, C < deriv f x) :
  βˆ€ x y ∈ D, x < y β†’ C * (y - x) < f y - f x :=
begin
  assume x hx y hy hxy,
  have hxyD : Icc x y βŠ† D, from hD.ord_connected.out hx hy,
  have hxyD' : Ioo x y βŠ† interior D,
    from subset_sUnion_of_mem ⟨is_open_Ioo, subset.trans Ioo_subset_Icc_self hxyD⟩,
  obtain ⟨a, a_mem, ha⟩ : βˆƒ a ∈ Ioo x y, deriv f a = (f y - f x) / (y - x),
    from exists_deriv_eq_slope f hxy (hf.mono hxyD) (hf'.mono hxyD'),
  have : C < (f y - f x) / (y - x), by { rw [← ha], exact hf'_gt _ (hxyD' a_mem) },
  exact (lt_div_iff (sub_pos.2 hxy)).1 this
end

/-- Let `f : ℝ β†’ ℝ` be a differentiable function. If `C < f'`, then `f` grows faster than
`C * x`, i.e., `C * (y - x) < f y - f x` whenever `x < y`. -/
theorem mul_sub_lt_image_sub_of_lt_deriv {f : ℝ β†’ ℝ} (hf : differentiable ℝ f)
  {C} (hf'_gt : βˆ€ x, C < deriv f x) ⦃x y⦄ (hxy : x < y) :
  C * (y - x) < f y - f x :=
convex_univ.mul_sub_lt_image_sub_of_lt_deriv hf.continuous.continuous_on hf.differentiable_on
  (Ξ» x _, hf'_gt x) x trivial y trivial hxy

/-- Let `f` be a function continuous on a convex (or, equivalently, connected) subset `D`
of the real line. If `f` is differentiable on the interior of `D` and `C ≀ f'`, then
`f` grows at least as fast as `C * x` on `D`, i.e., `C * (y - x) ≀ f y - f x` whenever `x, y ∈ D`,
`x ≀ y`. -/
theorem convex.mul_sub_le_image_sub_of_le_deriv {D : set ℝ} (hD : convex ℝ D) {f : ℝ β†’ ℝ}
  (hf : continuous_on f D) (hf' : differentiable_on ℝ f (interior D))
  {C} (hf'_ge : βˆ€ x ∈ interior D, C ≀ deriv f x) :
  βˆ€ x y ∈ D, x ≀ y β†’ C * (y - x) ≀ f y - f x :=
begin
  assume x hx y hy hxy,
  cases eq_or_lt_of_le hxy with hxy' hxy', by rw [hxy', sub_self, sub_self, mul_zero],
  have hxyD : Icc x y βŠ† D, from hD.ord_connected.out hx hy,
  have hxyD' : Ioo x y βŠ† interior D,
    from subset_sUnion_of_mem ⟨is_open_Ioo, subset.trans Ioo_subset_Icc_self hxyD⟩,
  obtain ⟨a, a_mem, ha⟩ : βˆƒ a ∈ Ioo x y, deriv f a = (f y - f x) / (y - x),
    from exists_deriv_eq_slope f hxy' (hf.mono hxyD) (hf'.mono hxyD'),
  have : C ≀ (f y - f x) / (y - x), by { rw [← ha], exact hf'_ge _ (hxyD' a_mem) },
  exact (le_div_iff (sub_pos.2 hxy')).1 this
end

/-- Let `f : ℝ β†’ ℝ` be a differentiable function. If `C ≀ f'`, then `f` grows at least as fast
as `C * x`, i.e., `C * (y - x) ≀ f y - f x` whenever `x ≀ y`. -/
theorem mul_sub_le_image_sub_of_le_deriv {f : ℝ β†’ ℝ} (hf : differentiable ℝ f)
  {C} (hf'_ge : βˆ€ x, C ≀ deriv f x) ⦃x y⦄ (hxy : x ≀ y) :
  C * (y - x) ≀ f y - f x :=
convex_univ.mul_sub_le_image_sub_of_le_deriv hf.continuous.continuous_on hf.differentiable_on
  (Ξ» x _, hf'_ge x) x trivial y trivial hxy

/-- Let `f` be a function continuous on a convex (or, equivalently, connected) subset `D`
of the real line. If `f` is differentiable on the interior of `D` and `f' < C`, then
`f` grows slower than `C * x` on `D`, i.e., `f y - f x < C * (y - x)` whenever `x, y ∈ D`,
`x < y`. -/
theorem convex.image_sub_lt_mul_sub_of_deriv_lt {D : set ℝ} (hD : convex ℝ D) {f : ℝ β†’ ℝ}
  (hf : continuous_on f D) (hf' : differentiable_on ℝ f (interior D))
  {C} (lt_hf' : βˆ€ x ∈ interior D, deriv f x < C) :
  βˆ€ x y ∈ D, x < y β†’ f y - f x < C * (y - x) :=
begin
  assume x hx y hy hxy,
  have hf'_gt : βˆ€ x ∈ interior D, -C < deriv (Ξ» y, -f y) x,
  { assume x hx,
    rw [deriv.neg, neg_lt_neg_iff],
    exact lt_hf' x hx },
  simpa [-neg_lt_neg_iff]
    using neg_lt_neg (hD.mul_sub_lt_image_sub_of_lt_deriv hf.neg hf'.neg hf'_gt x hx y hy hxy)
end

/-- Let `f : ℝ β†’ ℝ` be a differentiable function. If `f' < C`, then `f` grows slower than
`C * x` on `D`, i.e., `f y - f x < C * (y - x)` whenever `x < y`. -/
theorem image_sub_lt_mul_sub_of_deriv_lt {f : ℝ β†’ ℝ} (hf : differentiable ℝ f)
  {C} (lt_hf' : βˆ€ x, deriv f x < C) ⦃x y⦄ (hxy : x < y) :
  f y - f x < C * (y - x) :=
convex_univ.image_sub_lt_mul_sub_of_deriv_lt hf.continuous.continuous_on hf.differentiable_on
  (Ξ» x _, lt_hf' x) x trivial y trivial hxy

/-- Let `f` be a function continuous on a convex (or, equivalently, connected) subset `D`
of the real line. If `f` is differentiable on the interior of `D` and `f' ≀ C`, then
`f` grows at most as fast as `C * x` on `D`, i.e., `f y - f x ≀ C * (y - x)` whenever `x, y ∈ D`,
`x ≀ y`. -/
theorem convex.image_sub_le_mul_sub_of_deriv_le {D : set ℝ} (hD : convex ℝ D) {f : ℝ β†’ ℝ}
  (hf : continuous_on f D) (hf' : differentiable_on ℝ f (interior D))
  {C} (le_hf' : βˆ€ x ∈ interior D, deriv f x ≀ C) :
  βˆ€ x y ∈ D, x ≀ y β†’ f y - f x ≀ C * (y - x) :=
begin
  assume x hx y hy hxy,
  have hf'_ge : βˆ€ x ∈ interior D, -C ≀ deriv (Ξ» y, -f y) x,
  { assume x hx,
    rw [deriv.neg, neg_le_neg_iff],
    exact le_hf' x hx },
  simpa [-neg_le_neg_iff]
    using neg_le_neg (hD.mul_sub_le_image_sub_of_le_deriv hf.neg hf'.neg hf'_ge x hx y hy hxy)
end

/-- Let `f : ℝ β†’ ℝ` be a differentiable function. If `f' ≀ C`, then `f` grows at most as fast
as `C * x`, i.e., `f y - f x ≀ C * (y - x)` whenever `x ≀ y`. -/
theorem image_sub_le_mul_sub_of_deriv_le {f : ℝ β†’ ℝ} (hf : differentiable ℝ f)
  {C} (le_hf' : βˆ€ x, deriv f x ≀ C) ⦃x y⦄ (hxy : x ≀ y) :
  f y - f x ≀ C * (y - x) :=
convex_univ.image_sub_le_mul_sub_of_deriv_le hf.continuous.continuous_on hf.differentiable_on
  (Ξ» x _, le_hf' x) x trivial y trivial hxy

/-- Let `f` be a function continuous on a convex (or, equivalently, connected) subset `D`
of the real line. If `f` is differentiable on the interior of `D` and `f'` is positive, then
`f` is a strictly monotone function on `D`.
Note that we don't require differentiability explicitly as it already implied by the derivative
being strictly positive. -/
theorem convex.strict_mono_on_of_deriv_pos {D : set ℝ} (hD : convex ℝ D) {f : ℝ β†’ ℝ}
  (hf : continuous_on f D) (hf' : βˆ€ x ∈ interior D, 0 < deriv f x) :
  strict_mono_on f D :=
begin
  rintro x hx y hy,
  simpa only [zero_mul, sub_pos] using hD.mul_sub_lt_image_sub_of_lt_deriv hf _ hf' x hx y hy,
  exact Ξ» z hz, (differentiable_at_of_deriv_ne_zero (hf' z hz).ne').differentiable_within_at,
end

/-- Let `f : ℝ β†’ ℝ` be a differentiable function. If `f'` is positive, then
`f` is a strictly monotone function.
Note that we don't require differentiability explicitly as it already implied by the derivative
being strictly positive. -/
theorem strict_mono_of_deriv_pos {f : ℝ β†’ ℝ} (hf' : βˆ€ x, 0 < deriv f x) : strict_mono f :=
strict_mono_on_univ.1 $ convex_univ.strict_mono_on_of_deriv_pos
  (Ξ» z _, (differentiable_at_of_deriv_ne_zero (hf' z).ne').differentiable_within_at
    .continuous_within_at)
  (Ξ» x _, hf' x)

/-- Let `f` be a function continuous on a convex (or, equivalently, connected) subset `D`
of the real line. If `f` is differentiable on the interior of `D` and `f'` is nonnegative, then
`f` is a monotone function on `D`. -/
theorem convex.monotone_on_of_deriv_nonneg {D : set ℝ} (hD : convex ℝ D) {f : ℝ β†’ ℝ}
  (hf : continuous_on f D) (hf' : differentiable_on ℝ f (interior D))
  (hf'_nonneg : βˆ€ x ∈ interior D, 0 ≀ deriv f x) :
  monotone_on f D :=
Ξ» x hx y hy hxy, by simpa only [zero_mul, sub_nonneg]
  using hD.mul_sub_le_image_sub_of_le_deriv hf hf' hf'_nonneg x hx y hy hxy

/-- Let `f : ℝ β†’ ℝ` be a differentiable function. If `f'` is nonnegative, then
`f` is a monotone function. -/
theorem monotone_of_deriv_nonneg {f : ℝ β†’ ℝ} (hf : differentiable ℝ f) (hf' : βˆ€ x, 0 ≀ deriv f x) :
  monotone f :=
monotone_on_univ.1 $ convex_univ.monotone_on_of_deriv_nonneg hf.continuous.continuous_on
  hf.differentiable_on (Ξ» x _, hf' x)

/-- Let `f` be a function continuous on a convex (or, equivalently, connected) subset `D`
of the real line. If `f` is differentiable on the interior of `D` and `f'` is negative, then
`f` is a strictly antitone function on `D`. -/
theorem convex.strict_anti_on_of_deriv_neg {D : set ℝ} (hD : convex ℝ D) {f : ℝ β†’ ℝ}
  (hf : continuous_on f D) (hf' : βˆ€ x ∈ interior D, deriv f x < 0) :
  strict_anti_on f D :=
Ξ» x hx y, by simpa only [zero_mul, sub_lt_zero]
  using hD.image_sub_lt_mul_sub_of_deriv_lt hf
  (Ξ» z hz, (differentiable_at_of_deriv_ne_zero (hf' z hz).ne).differentiable_within_at) hf' x hx y

/-- Let `f : ℝ β†’ ℝ` be a differentiable function. If `f'` is negative, then
`f` is a strictly antitone function.
Note that we don't require differentiability explicitly as it already implied by the derivative
being strictly negative. -/
theorem strict_anti_of_deriv_neg {f : ℝ β†’ ℝ} (hf' : βˆ€ x, deriv f x < 0) :
  strict_anti f :=
strict_anti_on_univ.1 $ convex_univ.strict_anti_on_of_deriv_neg
  (Ξ» z _, (differentiable_at_of_deriv_ne_zero (hf' z).ne).differentiable_within_at
    .continuous_within_at)
  (Ξ» x _, hf' x)

/-- Let `f` be a function continuous on a convex (or, equivalently, connected) subset `D`
of the real line. If `f` is differentiable on the interior of `D` and `f'` is nonpositive, then
`f` is an antitone function on `D`. -/
theorem convex.antitone_on_of_deriv_nonpos {D : set ℝ} (hD : convex ℝ D) {f : ℝ β†’ ℝ}
  (hf : continuous_on f D) (hf' : differentiable_on ℝ f (interior D))
  (hf'_nonpos : βˆ€ x ∈ interior D, deriv f x ≀ 0) :
  antitone_on f D :=
Ξ» x hx y hy hxy, by simpa only [zero_mul, sub_nonpos]
  using hD.image_sub_le_mul_sub_of_deriv_le hf hf' hf'_nonpos x hx y hy hxy

/-- Let `f : ℝ β†’ ℝ` be a differentiable function. If `f'` is nonpositive, then
`f` is an antitone function. -/
theorem antitone_of_deriv_nonpos {f : ℝ β†’ ℝ} (hf : differentiable ℝ f) (hf' : βˆ€ x, deriv f x ≀ 0) :
  antitone f :=
antitone_on_univ.1 $ convex_univ.antitone_on_of_deriv_nonpos hf.continuous.continuous_on
  hf.differentiable_on (Ξ» x _, hf' x)

/-- If a function `f` is continuous on a convex set `D βŠ† ℝ`, is differentiable on its interior,
and `f'` is monotone on the interior, then `f` is convex on `D`. -/
theorem monotone_on.convex_on_of_deriv {D : set ℝ} (hD : convex ℝ D) {f : ℝ β†’ ℝ}
  (hf : continuous_on f D) (hf' : differentiable_on ℝ f (interior D))
  (hf'_mono : monotone_on (deriv f) (interior D)) :
  convex_on ℝ D f :=
convex_on_of_slope_mono_adjacent hD
begin
  intros x y z hx hz hxy hyz,
  -- First we prove some trivial inclusions
  have hxzD : Icc x z βŠ† D, from hD.ord_connected.out hx hz,
  have hxyD : Icc x y βŠ† D, from subset.trans (Icc_subset_Icc_right $ le_of_lt hyz) hxzD,
  have hxyD' : Ioo x y βŠ† interior D,
    from subset_sUnion_of_mem ⟨is_open_Ioo, subset.trans Ioo_subset_Icc_self hxyD⟩,
  have hyzD : Icc y z βŠ† D, from subset.trans (Icc_subset_Icc_left $ le_of_lt hxy) hxzD,
  have hyzD' : Ioo y z βŠ† interior D,
    from subset_sUnion_of_mem ⟨is_open_Ioo, subset.trans Ioo_subset_Icc_self hyzD⟩,
  -- Then we apply MVT to both `[x, y]` and `[y, z]`
  obtain ⟨a, ⟨hxa, hay⟩, ha⟩ : βˆƒ a ∈ Ioo x y, deriv f a = (f y - f x) / (y - x),
    from exists_deriv_eq_slope f hxy (hf.mono hxyD) (hf'.mono hxyD'),
  obtain ⟨b, ⟨hyb, hbz⟩, hb⟩ : βˆƒ b ∈ Ioo y z, deriv f b = (f z - f y) / (z - y),
    from exists_deriv_eq_slope f hyz (hf.mono hyzD) (hf'.mono hyzD'),
  rw [← ha, ← hb],
  exact hf'_mono (hxyD' ⟨hxa, hay⟩) (hyzD' ⟨hyb, hbz⟩) (hay.trans hyb).le
end

/-- If a function `f` is continuous on a convex set `D βŠ† ℝ`, is differentiable on its interior,
and `f'` is antitone on the interior, then `f` is concave on `D`. -/
theorem antitone_on.concave_on_of_deriv {D : set ℝ} (hD : convex ℝ D) {f : ℝ β†’ ℝ}
  (hf : continuous_on f D) (hf' : differentiable_on ℝ f (interior D))
  (h_anti : antitone_on (deriv f) (interior D)) :
  concave_on ℝ D f :=
begin
  have : monotone_on (deriv (-f)) (interior D),
  { intros x hx y hy hxy,
    convert neg_le_neg (h_anti hx hy hxy);
    convert deriv.neg },
  exact neg_convex_on_iff.mp (this.convex_on_of_deriv hD hf.neg hf'.neg),
end

lemma strict_mono_on.exists_slope_lt_deriv_aux {x y : ℝ} {f : ℝ β†’ ℝ}
  (hf : continuous_on f (Icc x y)) (hxy : x < y)
  (hf'_mono : strict_mono_on (deriv f) (Ioo x y)) (h : βˆ€ w ∈ Ioo x y, deriv f w β‰  0) :
  βˆƒ a ∈ Ioo x y, (f y - f x) / (y - x) < deriv f a :=
begin
  have A : differentiable_on ℝ f (Ioo x y),
    from Ξ» w wmem, (differentiable_at_of_deriv_ne_zero (h w wmem)).differentiable_within_at,
  obtain ⟨a, ⟨hxa, hay⟩, ha⟩ : βˆƒ a ∈ Ioo x y, deriv f a = (f y - f x) / (y - x),
    from exists_deriv_eq_slope f hxy hf A,
  rcases nonempty_Ioo.2 hay with ⟨b, ⟨hab, hby⟩⟩,
  refine ⟨b, ⟨hxa.trans hab, hby⟩, _⟩,
  rw ← ha,
  exact hf'_mono ⟨hxa, hay⟩ ⟨hxa.trans hab, hby⟩ hab
end

lemma strict_mono_on.exists_slope_lt_deriv {x y : ℝ} {f : ℝ β†’ ℝ}
  (hf : continuous_on f (Icc x y)) (hxy : x < y)
  (hf'_mono : strict_mono_on (deriv f) (Ioo x y)) :
  βˆƒ a ∈ Ioo x y, (f y - f x) / (y - x) < deriv f a :=
begin
  by_cases h : βˆ€ w ∈ Ioo x y, deriv f w β‰  0,
  { apply strict_mono_on.exists_slope_lt_deriv_aux hf hxy hf'_mono h },
  { push_neg at h,
    rcases h with ⟨w, ⟨hxw, hwy⟩, hw⟩,
    obtain ⟨a, ⟨hxa, haw⟩, ha⟩ : βˆƒ (a : ℝ) (H : a ∈ Ioo x w), (f w - f x) / (w - x) < deriv f a,
    { apply strict_mono_on.exists_slope_lt_deriv_aux _ hxw _ _,
      { exact hf.mono (Icc_subset_Icc le_rfl hwy.le) },
      { exact hf'_mono.mono (Ioo_subset_Ioo le_rfl hwy.le) },
      { assume z hz,
        rw ← hw,
        apply ne_of_lt,
        exact hf'_mono ⟨hz.1, hz.2.trans hwy⟩ ⟨hxw, hwy⟩ hz.2 } },
    obtain ⟨b, ⟨hwb, hby⟩, hb⟩ : βˆƒ (b : ℝ) (H : b ∈ Ioo w y), (f y - f w) / (y - w) < deriv f b,
    { apply strict_mono_on.exists_slope_lt_deriv_aux _ hwy _ _,
      { refine hf.mono (Icc_subset_Icc hxw.le le_rfl), },
      { exact hf'_mono.mono (Ioo_subset_Ioo hxw.le le_rfl) },
      { assume z hz,
        rw ← hw,
        apply ne_of_gt,
        exact hf'_mono ⟨hxw, hwy⟩ ⟨hxw.trans hz.1, hz.2⟩ hz.1, } },
    refine ⟨b, ⟨hxw.trans hwb, hby⟩, _⟩,
    simp only [div_lt_iff, hxy, hxw, hwy, sub_pos] at ⊒ ha hb,
    have : deriv f a * (w - x) < deriv f b * (w - x),
    { apply mul_lt_mul _ le_rfl (sub_pos.2 hxw) _,
      { exact hf'_mono ⟨hxa, haw.trans hwy⟩ ⟨hxw.trans hwb, hby⟩ (haw.trans hwb) },
      { rw ← hw,
        exact (hf'_mono ⟨hxw, hwy⟩ ⟨hxw.trans hwb, hby⟩ hwb).le } },
    linarith }
end

lemma strict_mono_on.exists_deriv_lt_slope_aux {x y : ℝ} {f : ℝ β†’ ℝ}
  (hf : continuous_on f (Icc x y)) (hxy : x < y)
  (hf'_mono : strict_mono_on (deriv f) (Ioo x y)) (h : βˆ€ w ∈ Ioo x y, deriv f w β‰  0) :
  βˆƒ a ∈ Ioo x y, deriv f a < (f y - f x) / (y - x) :=
begin
  have A : differentiable_on ℝ f (Ioo x y),
    from Ξ» w wmem, (differentiable_at_of_deriv_ne_zero (h w wmem)).differentiable_within_at,
  obtain ⟨a, ⟨hxa, hay⟩, ha⟩ : βˆƒ a ∈ Ioo x y, deriv f a = (f y - f x) / (y - x),
    from exists_deriv_eq_slope f hxy hf A,
  rcases nonempty_Ioo.2 hxa with ⟨b, ⟨hxb, hba⟩⟩,
  refine ⟨b, ⟨hxb, hba.trans hay⟩, _⟩,
  rw ← ha,
  exact hf'_mono ⟨hxb, hba.trans hay⟩ ⟨hxa, hay⟩ hba
end

lemma strict_mono_on.exists_deriv_lt_slope {x y : ℝ} {f : ℝ β†’ ℝ}
  (hf : continuous_on f (Icc x y)) (hxy : x < y)
  (hf'_mono : strict_mono_on (deriv f) (Ioo x y)) :
  βˆƒ a ∈ Ioo x y, deriv f a < (f y - f x) / (y - x) :=
begin
  by_cases h : βˆ€ w ∈ Ioo x y, deriv f w β‰  0,
  { apply strict_mono_on.exists_deriv_lt_slope_aux hf hxy hf'_mono h },
  { push_neg at h,
    rcases h with ⟨w, ⟨hxw, hwy⟩, hw⟩,
    obtain ⟨a, ⟨hxa, haw⟩, ha⟩ : βˆƒ (a : ℝ) (H : a ∈ Ioo x w), deriv f a < (f w - f x) / (w - x),
    { apply strict_mono_on.exists_deriv_lt_slope_aux _ hxw _ _,
      { exact hf.mono (Icc_subset_Icc le_rfl hwy.le) },
      { exact hf'_mono.mono (Ioo_subset_Ioo le_rfl hwy.le) },
      { assume z hz,
        rw ← hw,
        apply ne_of_lt,
        exact hf'_mono ⟨hz.1, hz.2.trans hwy⟩ ⟨hxw, hwy⟩ hz.2 } },
    obtain ⟨b, ⟨hwb, hby⟩, hb⟩ : βˆƒ (b : ℝ) (H : b ∈ Ioo w y), deriv f b < (f y - f w) / (y - w),
    { apply strict_mono_on.exists_deriv_lt_slope_aux _ hwy _ _,
      { refine hf.mono (Icc_subset_Icc hxw.le le_rfl), },
      { exact hf'_mono.mono (Ioo_subset_Ioo hxw.le le_rfl) },
      { assume z hz,
        rw ← hw,
        apply ne_of_gt,
        exact hf'_mono ⟨hxw, hwy⟩ ⟨hxw.trans hz.1, hz.2⟩ hz.1, } },
    refine ⟨a, ⟨hxa, haw.trans hwy⟩, _⟩,
    simp only [lt_div_iff, hxy, hxw, hwy, sub_pos] at ⊒ ha hb,
    have : deriv f a * (y - w) < deriv f b * (y - w),
    { apply mul_lt_mul _ le_rfl (sub_pos.2 hwy) _,
      { exact hf'_mono ⟨hxa, haw.trans hwy⟩ ⟨hxw.trans hwb, hby⟩ (haw.trans hwb) },
      { rw ← hw,
        exact (hf'_mono ⟨hxw, hwy⟩ ⟨hxw.trans hwb, hby⟩ hwb).le } },
    linarith }
end

/-- If a function `f` is continuous on a convex set `D βŠ† ℝ`, and `f'` is strictly monotone on the
interior, then `f` is strictly convex on `D`.
Note that we don't require differentiability, since it is guaranteed at all but at most
one point by the strict monotonicity of `f'`. -/
lemma strict_mono_on.strict_convex_on_of_deriv {D : set ℝ} (hD : convex ℝ D) {f : ℝ β†’ ℝ}
  (hf : continuous_on f D) (hf' : strict_mono_on (deriv f) (interior D)) :
  strict_convex_on ℝ D f :=
strict_convex_on_of_slope_strict_mono_adjacent hD
begin
  intros x y z hx hz hxy hyz,
  -- First we prove some trivial inclusions
  have hxzD : Icc x z βŠ† D, from hD.ord_connected.out hx hz,
  have hxyD : Icc x y βŠ† D, from subset.trans (Icc_subset_Icc_right $ le_of_lt hyz) hxzD,
  have hxyD' : Ioo x y βŠ† interior D,
    from subset_sUnion_of_mem ⟨is_open_Ioo, subset.trans Ioo_subset_Icc_self hxyD⟩,
  have hyzD : Icc y z βŠ† D, from subset.trans (Icc_subset_Icc_left $ le_of_lt hxy) hxzD,
  have hyzD' : Ioo y z βŠ† interior D,
    from subset_sUnion_of_mem ⟨is_open_Ioo, subset.trans Ioo_subset_Icc_self hyzD⟩,
  -- Then we get points `a` and `b` in each interval `[x, y]` and `[y, z]` where the derivatives
  -- can be compared to the slopes between `x, y` and `y, z` respectively.
  obtain ⟨a, ⟨hxa, hay⟩, ha⟩ : βˆƒ a ∈ Ioo x y, (f y - f x) / (y - x) < deriv f a,
    from strict_mono_on.exists_slope_lt_deriv (hf.mono hxyD) hxy (hf'.mono hxyD'),
  obtain ⟨b, ⟨hyb, hbz⟩, hb⟩ : βˆƒ b ∈ Ioo y z, deriv f b < (f z - f y) / (z - y),
    from strict_mono_on.exists_deriv_lt_slope (hf.mono hyzD) hyz (hf'.mono hyzD'),
  apply ha.trans (lt_trans _ hb),
  exact hf' (hxyD' ⟨hxa, hay⟩) (hyzD' ⟨hyb, hbz⟩) (hay.trans hyb),
end

/-- If a function `f` is continuous on a convex set `D βŠ† ℝ` and `f'` is strictly antitone on the
interior, then `f` is strictly concave on `D`.
Note that we don't require differentiability, since it is guaranteed at all but at most
one point by the strict antitonicity of `f'`. -/
lemma strict_anti_on.strict_concave_on_of_deriv {D : set ℝ} (hD : convex ℝ D) {f : ℝ β†’ ℝ}
  (hf : continuous_on f D) (h_anti : strict_anti_on (deriv f) (interior D)) :
  strict_concave_on ℝ D f :=
begin
  have : strict_mono_on (deriv (-f)) (interior D),
  { intros x hx y hy hxy,
    convert neg_lt_neg (h_anti hx hy hxy);
    convert deriv.neg },
  exact neg_strict_convex_on_iff.mp (this.strict_convex_on_of_deriv hD hf.neg),
end

/-- If a function `f` is differentiable and `f'` is monotone on `ℝ` then `f` is convex. -/
theorem monotone.convex_on_univ_of_deriv {f : ℝ β†’ ℝ} (hf : differentiable ℝ f)
  (hf'_mono : monotone (deriv f)) : convex_on ℝ univ f :=
(hf'_mono.monotone_on _).convex_on_of_deriv convex_univ hf.continuous.continuous_on
  hf.differentiable_on

/-- If a function `f` is differentiable and `f'` is antitone on `ℝ` then `f` is concave. -/
theorem antitone.concave_on_univ_of_deriv {f : ℝ β†’ ℝ} (hf : differentiable ℝ f)
  (hf'_anti : antitone (deriv f)) : concave_on ℝ univ f :=
(hf'_anti.antitone_on _).concave_on_of_deriv convex_univ hf.continuous.continuous_on
  hf.differentiable_on

/-- If a function `f` is continuous and `f'` is strictly monotone on `ℝ` then `f` is strictly
convex. Note that we don't require differentiability, since it is guaranteed at all but at most
one point by the strict monotonicity of `f'`. -/
lemma strict_mono.strict_convex_on_univ_of_deriv {f : ℝ β†’ ℝ} (hf : continuous f)
  (hf'_mono : strict_mono (deriv f)) : strict_convex_on ℝ univ f :=
(hf'_mono.strict_mono_on _).strict_convex_on_of_deriv convex_univ hf.continuous_on

/-- If a function `f` is continuous and `f'` is strictly antitone on `ℝ` then `f` is strictly
concave. Note that we don't require differentiability, since it is guaranteed at all but at most
one point by the strict antitonicity of `f'`. -/
lemma strict_anti.strict_concave_on_univ_of_deriv {f : ℝ β†’ ℝ} (hf : continuous f)
  (hf'_anti : strict_anti (deriv f)) : strict_concave_on ℝ univ f :=
(hf'_anti.strict_anti_on _).strict_concave_on_of_deriv convex_univ hf.continuous_on

/-- If a function `f` is continuous on a convex set `D βŠ† ℝ`, is twice differentiable on its
interior, and `f''` is nonnegative on the interior, then `f` is convex on `D`. -/
theorem convex_on_of_deriv2_nonneg {D : set ℝ} (hD : convex ℝ D) {f : ℝ β†’ ℝ}
  (hf : continuous_on f D) (hf' : differentiable_on ℝ f (interior D))
  (hf'' : differentiable_on ℝ (deriv f) (interior D))
  (hf''_nonneg : βˆ€ x ∈ interior D, 0 ≀ (deriv^[2] f x)) :
  convex_on ℝ D f :=
(hD.interior.monotone_on_of_deriv_nonneg hf''.continuous_on (by rwa interior_interior)
  $ by rwa interior_interior).convex_on_of_deriv hD hf hf'

/-- If a function `f` is continuous on a convex set `D βŠ† ℝ`, is twice differentiable on its
interior, and `f''` is nonpositive on the interior, then `f` is concave on `D`. -/
theorem concave_on_of_deriv2_nonpos {D : set ℝ} (hD : convex ℝ D) {f : ℝ β†’ ℝ}
  (hf : continuous_on f D) (hf' : differentiable_on ℝ f (interior D))
  (hf'' : differentiable_on ℝ (deriv f) (interior D))
  (hf''_nonpos : βˆ€ x ∈ interior D, deriv^[2] f x ≀ 0) :
  concave_on ℝ D f :=
(hD.interior.antitone_on_of_deriv_nonpos hf''.continuous_on (by rwa interior_interior)
  $ by rwa interior_interior).concave_on_of_deriv hD hf hf'

/-- If a function `f` is continuous on a convex set `D βŠ† ℝ` and `f''` is strictly positive on the
interior, then `f` is strictly convex on `D`.
Note that we don't require twice differentiability explicitly as it is already implied by the second
derivative being strictly positive, except at at most one point. -/
lemma strict_convex_on_of_deriv2_pos {D : set ℝ} (hD : convex ℝ D) {f : ℝ β†’ ℝ}
  (hf : continuous_on f D) (hf'' : βˆ€ x ∈ interior D, 0 < (deriv^[2] f) x) :
  strict_convex_on ℝ D f :=
(hD.interior.strict_mono_on_of_deriv_pos (Ξ» z hz,
  (differentiable_at_of_deriv_ne_zero (hf'' z hz).ne').differentiable_within_at
   .continuous_within_at) $ by rwa interior_interior).strict_convex_on_of_deriv hD hf

/-- If a function `f` is continuous on a convex set `D βŠ† ℝ` and `f''` is strictly negative on the
interior, then `f` is strictly concave on `D`.
Note that we don't require twice differentiability explicitly as it already implied by the second
derivative being strictly negative, except at at most one point. -/
lemma strict_concave_on_of_deriv2_neg {D : set ℝ} (hD : convex ℝ D) {f : ℝ β†’ ℝ}
  (hf : continuous_on f D) (hf'' : βˆ€ x ∈ interior D, deriv^[2] f x < 0) :
  strict_concave_on ℝ D f :=
(hD.interior.strict_anti_on_of_deriv_neg (Ξ» z hz,
  (differentiable_at_of_deriv_ne_zero (hf'' z hz).ne).differentiable_within_at
   .continuous_within_at) $ by rwa interior_interior).strict_concave_on_of_deriv hD hf

/-- If a function `f` is twice differentiable on a open convex set `D βŠ† ℝ` and
`f''` is nonnegative on `D`, then `f` is convex on `D`. -/
theorem convex_on_of_deriv2_nonneg' {D : set ℝ} (hD : convex ℝ D) {f : ℝ β†’ ℝ}
  (hf' : differentiable_on ℝ f D) (hf'' : differentiable_on ℝ (deriv f) D)
  (hf''_nonneg : βˆ€ x ∈ D, 0 ≀ (deriv^[2] f) x) : convex_on ℝ D f :=
convex_on_of_deriv2_nonneg hD hf'.continuous_on (hf'.mono interior_subset)
  (hf''.mono interior_subset) (Ξ» x hx, hf''_nonneg x (interior_subset hx))

/-- If a function `f` is twice differentiable on an open convex set `D βŠ† ℝ` and
`f''` is nonpositive on `D`, then `f` is concave on `D`. -/
theorem concave_on_of_deriv2_nonpos' {D : set ℝ} (hD : convex ℝ D) {f : ℝ β†’ ℝ}
  (hf' : differentiable_on ℝ f D) (hf'' : differentiable_on ℝ (deriv f) D)
  (hf''_nonpos : βˆ€ x ∈ D, deriv^[2] f x ≀ 0) : concave_on ℝ D f :=
concave_on_of_deriv2_nonpos hD hf'.continuous_on (hf'.mono interior_subset)
  (hf''.mono interior_subset) (Ξ» x hx, hf''_nonpos x (interior_subset hx))

/-- If a function `f` is continuous on a convex set `D βŠ† ℝ` and `f''` is strictly positive on `D`,
then `f` is strictly convex on `D`.
Note that we don't require twice differentiability explicitly as it is already implied by the second
derivative being strictly positive, except at at most one point. -/
lemma strict_convex_on_of_deriv2_pos' {D : set ℝ} (hD : convex ℝ D)
  {f : ℝ β†’ ℝ} (hf : continuous_on f D) (hf'' : βˆ€ x ∈ D, 0 < (deriv^[2] f) x) :
  strict_convex_on ℝ D f :=
strict_convex_on_of_deriv2_pos hD hf $ Ξ» x hx, hf'' x (interior_subset hx)

/-- If a function `f` is continuous on a convex set `D βŠ† ℝ` and `f''` is strictly negative on `D`,
then `f` is strictly concave on `D`.
Note that we don't require twice differentiability explicitly as it is already implied by the second
derivative being strictly negative, except at at most one point. -/
lemma strict_concave_on_of_deriv2_neg' {D : set ℝ} (hD : convex ℝ D)
  {f : ℝ β†’ ℝ} (hf : continuous_on f D) (hf'' : βˆ€ x ∈ D, deriv^[2] f x < 0) :
  strict_concave_on ℝ D f :=
strict_concave_on_of_deriv2_neg hD hf $ Ξ» x hx, hf'' x (interior_subset hx)

/-- If a function `f` is twice differentiable on `ℝ`, and `f''` is nonnegative on `ℝ`,
then `f` is convex on `ℝ`. -/
theorem convex_on_univ_of_deriv2_nonneg {f : ℝ β†’ ℝ} (hf' : differentiable ℝ f)
  (hf'' : differentiable ℝ (deriv f)) (hf''_nonneg : βˆ€ x, 0 ≀ (deriv^[2] f) x) :
  convex_on ℝ univ f :=
convex_on_of_deriv2_nonneg' convex_univ hf'.differentiable_on
  hf''.differentiable_on (Ξ» x _, hf''_nonneg x)

/-- If a function `f` is twice differentiable on `ℝ`, and `f''` is nonpositive on `ℝ`,
then `f` is concave on `ℝ`. -/
theorem concave_on_univ_of_deriv2_nonpos {f : ℝ β†’ ℝ} (hf' : differentiable ℝ f)
  (hf'' : differentiable ℝ (deriv f)) (hf''_nonpos : βˆ€ x, deriv^[2] f x ≀ 0) :
  concave_on ℝ univ f :=
concave_on_of_deriv2_nonpos' convex_univ hf'.differentiable_on
  hf''.differentiable_on (Ξ» x _, hf''_nonpos x)

/-- If a function `f` is continuous on `ℝ`, and `f''` is strictly positive on `ℝ`,
then `f` is strictly convex on `ℝ`.
Note that we don't require twice differentiability explicitly as it is already implied by the second
derivative being strictly positive, except at at most one point.  -/
lemma strict_convex_on_univ_of_deriv2_pos {f : ℝ β†’ ℝ} (hf : continuous f)
  (hf'' : βˆ€ x, 0 < (deriv^[2] f) x) :
  strict_convex_on ℝ univ f :=
strict_convex_on_of_deriv2_pos' convex_univ hf.continuous_on $ Ξ» x _, hf'' x

/-- If a function `f` is continuous on `ℝ`, and `f''` is strictly negative on `ℝ`,
then `f` is strictly concave on `ℝ`.
Note that we don't require twice differentiability explicitly as it is already implied by the second
derivative being strictly negative, except at at most one point.  -/
lemma strict_concave_on_univ_of_deriv2_neg {f : ℝ β†’ ℝ} (hf : continuous f)
  (hf'' : βˆ€ x, deriv^[2] f x < 0) :
  strict_concave_on ℝ univ f :=
strict_concave_on_of_deriv2_neg' convex_univ hf.continuous_on $ Ξ» x _, hf'' x

/-! ### Functions `f : E β†’ ℝ` -/

/-- Lagrange's Mean Value Theorem, applied to convex domains. -/
theorem domain_mvt
  {f : E β†’ ℝ} {s : set E} {x y : E} {f' : E β†’ (E β†’L[ℝ] ℝ)}
  (hf : βˆ€ x ∈ s, has_fderiv_within_at f (f' x) s x) (hs : convex ℝ s) (xs : x ∈ s) (ys : y ∈ s) :
  βˆƒ z ∈ segment ℝ x y, f y - f x = f' z (y - x) :=
begin
  have hIccIoo := @Ioo_subset_Icc_self ℝ _ 0 1,
-- parametrize segment
  set g : ℝ β†’ E := Ξ» t, x + t β€’ (y - x),
  have hseg : βˆ€ t ∈ Icc (0:ℝ) 1, g t ∈ segment ℝ x y,
  { rw segment_eq_image',
    simp only [mem_image, and_imp, add_right_inj],
    intros t ht, exact ⟨t, ht, rfl⟩ },
  have hseg' : Icc 0 1 βŠ† g ⁻¹' s,
  { rw ← image_subset_iff, unfold image, change βˆ€ _, _,
    intros z Hz, rw mem_set_of_eq at Hz, rcases Hz with ⟨t, Ht, hgt⟩,
    rw ← hgt, exact hs.segment_subset xs ys (hseg t Ht) },
-- derivative of pullback of f under parametrization
  have hfg: βˆ€ t ∈ Icc (0:ℝ) 1, has_deriv_within_at (f ∘ g)
    ((f' (g t) : E β†’ ℝ) (y-x)) (Icc (0:ℝ) 1) t,
  { intros t Ht,
    have hg : has_deriv_at g (y-x) t,
    { have := ((has_deriv_at_id t).smul_const (y - x)).const_add x,
      rwa one_smul at this },
    exact (hf (g t) $ hseg' Ht).comp_has_deriv_within_at _ hg.has_deriv_within_at hseg' },
-- apply 1-variable mean value theorem to pullback
  have hMVT : βˆƒ (t ∈ Ioo (0:ℝ) 1), ((f' (g t) : E β†’ ℝ) (y-x)) = (f (g 1) - f (g 0)) / (1 - 0),
  { refine exists_has_deriv_at_eq_slope (f ∘ g) _ (by norm_num) _ _,
    { exact Ξ» t Ht, (hfg t Ht).continuous_within_at },
    { exact Ξ» t Ht, (hfg t $ hIccIoo Ht).has_deriv_at (Icc_mem_nhds Ht.1 Ht.2) } },
-- reinterpret on domain
  rcases hMVT with ⟨t, Ht, hMVT'⟩,
  use g t, refine ⟨hseg t $ hIccIoo Ht, _⟩,
  simp [g, hMVT'],
end


section is_R_or_C

/-!
### Vector-valued functions `f : E β†’ F`.  Strict differentiability.

A `C^1` function is strictly differentiable, when the field is `ℝ` or `β„‚`. This follows from the
mean value inequality on balls, which is a particular case of the above results after restricting
the scalars to `ℝ`. Note that it does not make sense to talk of a convex set over `β„‚`, but balls
make sense and are enough. Many formulations of the mean value inequality could be generalized to
balls over `ℝ` or `β„‚`. For now, we only include the ones that we need.
-/

variables {π•œ : Type*} [is_R_or_C π•œ] {G : Type*} [normed_add_comm_group G] [normed_space π•œ G]
  {H : Type*} [normed_add_comm_group H] [normed_space π•œ H] {f : G β†’ H} {f' : G β†’ G β†’L[π•œ] H} {x : G}

/-- Over the reals or the complexes, a continuously differentiable function is strictly
differentiable. -/
lemma has_strict_fderiv_at_of_has_fderiv_at_of_continuous_at
  (hder : βˆ€αΆ  y in 𝓝 x, has_fderiv_at f (f' y) y) (hcont : continuous_at f' x) :
  has_strict_fderiv_at f (f' x) x :=
begin
-- turn little-o definition of strict_fderiv into an epsilon-delta statement
  refine is_o_iff.mpr (Ξ» c hc, metric.eventually_nhds_iff_ball.mpr _),
-- the correct Ξ΅ is the modulus of continuity of f'
  rcases metric.mem_nhds_iff.mp (inter_mem hder (hcont $ ball_mem_nhds _ hc)) with ⟨Ρ, Ρ0, hΡ⟩,
  refine ⟨Ρ, Ρ0, _⟩,
-- simplify formulas involving the product E Γ— E
  rintros ⟨a, b⟩ h,
  rw [← ball_prod_same, prod_mk_mem_set_prod_eq] at h,
-- exploit the choice of Ξ΅ as the modulus of continuity of f'
  have hf' : βˆ€ x' ∈ ball x Ξ΅, βˆ₯f' x' - f' xβˆ₯ ≀ c,
  { intros x' H', rw ← dist_eq_norm, exact le_of_lt (hΞ΅ H').2 },
-- apply mean value theorem
  letI : normed_space ℝ G := restrict_scalars.normed_space ℝ π•œ G,
  refine (convex_ball _ _).norm_image_sub_le_of_norm_has_fderiv_within_le' _ hf' h.2 h.1,
  exact Ξ» y hy, (hΞ΅ hy).1.has_fderiv_within_at
end

/-- Over the reals or the complexes, a continuously differentiable function is strictly
differentiable. -/
lemma has_strict_deriv_at_of_has_deriv_at_of_continuous_at {f f' : π•œ β†’ G} {x : π•œ}
  (hder : βˆ€αΆ  y in 𝓝 x, has_deriv_at f (f' y) y) (hcont : continuous_at f' x) :
  has_strict_deriv_at f (f' x) x :=
has_strict_fderiv_at_of_has_fderiv_at_of_continuous_at (hder.mono (Ξ» y hy, hy.has_fderiv_at)) $
  (smul_rightL π•œ π•œ G 1).continuous.continuous_at.comp hcont

end is_R_or_C