Datasets:

Modalities:
Text
Languages:
English
Libraries:
Datasets
License:
File size: 63,303 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
/-
Copyright (c) 2021 Kexing Ying. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Kexing Ying
-/
import measure_theory.measure.complex
import measure_theory.measure.sub
import measure_theory.decomposition.jordan
import measure_theory.measure.with_density_vector_measure
import measure_theory.function.ae_eq_of_integral

/-!
# Lebesgue decomposition

This file proves the Lebesgue decomposition theorem. The Lebesgue decomposition theorem states that,
given two σ-finite measures `μ` and `ν`, there exists a σ-finite measure `ξ` and a measurable
function `f` such that `μ = ξ + fν` and `ξ` is mutually singular with respect to `ν`.

The Lebesgue decomposition provides the Radon-Nikodym theorem readily.

## Main definitions

* `measure_theory.measure.have_lebesgue_decomposition` : A pair of measures `μ` and `ν` is said
  to `have_lebesgue_decomposition` if there exist a measure `ξ` and a measurable function `f`,
  such that `ξ` is mutually singular with respect to `ν` and `μ = ξ + ν.with_density f`
* `measure_theory.measure.singular_part` : If a pair of measures `have_lebesgue_decomposition`,
  then `singular_part` chooses the measure from `have_lebesgue_decomposition`, otherwise it
  returns the zero measure.
* `measure_theory.measure.rn_deriv`: If a pair of measures
  `have_lebesgue_decomposition`, then `rn_deriv` chooses the measurable function from
  `have_lebesgue_decomposition`, otherwise it returns the zero function.
* `measure_theory.signed_measure.have_lebesgue_decomposition` : A signed measure `s` and a
  measure `μ` is said to `have_lebesgue_decomposition` if both the positive part and negative
  part of `s` `have_lebesgue_decomposition` with respect to `μ`.
* `measure_theory.signed_measure.singular_part` : The singular part between a signed measure `s`
  and a measure `μ` is simply the singular part of the positive part of `s` with respect to `μ`
  minus the singular part of the negative part of `s` with respect to `μ`.
* `measure_theory.signed_measure.rn_deriv` : The Radon-Nikodym derivative of a signed
  measure `s` with respect to a measure `μ` is the Radon-Nikodym derivative of the positive part of
  `s` with respect to `μ` minus the Radon-Nikodym derivative of the negative part of `s` with
  respect to `μ`.

## Main results

* `measure_theory.measure.have_lebesgue_decomposition_of_sigma_finite` :
  the Lebesgue decomposition theorem.
* `measure_theory.measure.eq_singular_part` : Given measures `μ` and `ν`, if `s` is a measure
  mutually singular to `ν` and `f` is a measurable function such that `μ = s + fν`, then
  `s = μ.singular_part ν`.
* `measure_theory.measure.eq_rn_deriv` : Given measures `μ` and `ν`, if `s` is a
  measure mutually singular to `ν` and `f` is a measurable function such that `μ = s + fν`,
  then `f = μ.rn_deriv ν`.
* `measure_theory.signed_measure.singular_part_add_with_density_rn_deriv_eq` :
  the Lebesgue decomposition theorem between a signed measure and a σ-finite positive measure.

# Tags

Lebesgue decomposition theorem
-/

noncomputable theory
open_locale classical measure_theory nnreal ennreal
open set

variables {α β : Type*} {m : measurable_space α} {μ ν : measure_theory.measure α}
include m

namespace measure_theory

namespace measure

/-- A pair of measures `μ` and `ν` is said to `have_lebesgue_decomposition` if there exists a
measure `ξ` and a measurable function `f`, such that `ξ` is mutually singular with respect to
`ν` and `μ = ξ + ν.with_density f`. -/
class have_lebesgue_decomposition (μ ν : measure α) : Prop :=
(lebesgue_decomposition :
  ∃ (p : measure α × (α → ℝ≥0∞)), measurable p.2 ∧ p.1 ⊥ₘ ν ∧ μ = p.1 + ν.with_density p.2)

/-- If a pair of measures `have_lebesgue_decomposition`, then `singular_part` chooses the
measure from `have_lebesgue_decomposition`, otherwise it returns the zero measure. For sigma-finite
measures, `μ = μ.singular_part ν + ν.with_density (μ.rn_deriv ν)`. -/
@[irreducible]
def singular_part (μ ν : measure α) : measure α :=
if h : have_lebesgue_decomposition μ ν then (classical.some h.lebesgue_decomposition).1 else 0

/-- If a pair of measures `have_lebesgue_decomposition`, then `rn_deriv` chooses the
measurable function from `have_lebesgue_decomposition`, otherwise it returns the zero function.
For sigma-finite measures, `μ = μ.singular_part ν + ν.with_density (μ.rn_deriv ν)`.-/
@[irreducible]
def rn_deriv (μ ν : measure α) : α → ℝ≥0∞ :=
if h : have_lebesgue_decomposition μ ν then (classical.some h.lebesgue_decomposition).2 else 0

lemma have_lebesgue_decomposition_spec (μ ν : measure α)
  [h : have_lebesgue_decomposition μ ν] :
  measurable (μ.rn_deriv ν) ∧ (μ.singular_part ν) ⊥ₘ ν ∧
  μ = (μ.singular_part ν) + ν.with_density (μ.rn_deriv ν) :=
begin
  rw [singular_part, rn_deriv, dif_pos h, dif_pos h],
  exact classical.some_spec h.lebesgue_decomposition,
end

lemma have_lebesgue_decomposition_add (μ ν : measure α)
  [have_lebesgue_decomposition μ ν] :
  μ = (μ.singular_part ν) + ν.with_density (μ.rn_deriv ν) :=
(have_lebesgue_decomposition_spec μ ν).2.2

instance have_lebesgue_decomposition_smul
  (μ ν : measure α) [have_lebesgue_decomposition μ ν] (r : ℝ≥0) :
  (r • μ).have_lebesgue_decomposition ν :=
{ lebesgue_decomposition :=
  begin
    obtain ⟨hmeas, hsing, hadd⟩ := have_lebesgue_decomposition_spec μ ν,
    refine ⟨⟨r • μ.singular_part ν, r • μ.rn_deriv ν⟩, _, hsing.smul _, _⟩,
    { change measurable ((r : ℝ≥0∞) • _), -- cannot remove this line
      exact hmeas.const_smul _ },
    { change _ = (r : ℝ≥0∞) • _ + ν.with_density ((r : ℝ≥0∞) • _),
      rw [with_density_smul _ hmeas, ← smul_add, ← hadd],
      refl }
  end }

@[measurability]
lemma measurable_rn_deriv (μ ν : measure α) :
  measurable $ μ.rn_deriv ν :=
begin
  by_cases h : have_lebesgue_decomposition μ ν,
  { exactI (have_lebesgue_decomposition_spec μ ν).1 },
  { rw [rn_deriv, dif_neg h],
    exact measurable_zero }
end

lemma mutually_singular_singular_part (μ ν : measure α) :
  μ.singular_part ν ⊥ₘ ν :=
begin
  by_cases h : have_lebesgue_decomposition μ ν,
  { exactI (have_lebesgue_decomposition_spec μ ν).2.1 },
  { rw [singular_part, dif_neg h],
    exact mutually_singular.zero_left }
end

lemma singular_part_le (μ ν : measure α) : μ.singular_part ν ≤ μ :=
begin
  by_cases hl : have_lebesgue_decomposition μ ν,
  { casesI (have_lebesgue_decomposition_spec μ ν).2 with _ h,
    conv_rhs { rw h },
    exact measure.le_add_right le_rfl },
  { rw [singular_part, dif_neg hl],
    exact measure.zero_le μ }
end

lemma with_density_rn_deriv_le (μ ν : measure α) :
  ν.with_density (μ.rn_deriv ν) ≤ μ :=
begin
  by_cases hl : have_lebesgue_decomposition μ ν,
  { casesI (have_lebesgue_decomposition_spec μ ν).2 with _ h,
    conv_rhs { rw h },
    exact measure.le_add_left le_rfl },
  { rw [rn_deriv, dif_neg hl, with_density_zero],
    exact measure.zero_le μ }
end

instance [is_finite_measure μ] : is_finite_measure (μ.singular_part ν) :=
is_finite_measure_of_le μ $ singular_part_le μ ν

instance [sigma_finite μ] : sigma_finite (μ.singular_part ν) :=
sigma_finite_of_le μ $ singular_part_le μ ν

instance [topological_space α] [is_locally_finite_measure μ] :
  is_locally_finite_measure (μ.singular_part ν) :=
is_locally_finite_measure_of_le $ singular_part_le μ ν

instance [is_finite_measure μ] : is_finite_measure (ν.with_density $ μ.rn_deriv ν) :=
is_finite_measure_of_le μ $ with_density_rn_deriv_le μ ν

instance [sigma_finite μ] : sigma_finite (ν.with_density $ μ.rn_deriv ν) :=
sigma_finite_of_le μ $ with_density_rn_deriv_le μ ν

instance [topological_space α] [is_locally_finite_measure μ] :
  is_locally_finite_measure (ν.with_density $ μ.rn_deriv ν) :=
is_locally_finite_measure_of_le $ with_density_rn_deriv_le μ ν

lemma lintegral_rn_deriv_lt_top_of_measure_ne_top
  {μ : measure α} (ν : measure α) {s : set α} (hs : μ s ≠ ∞) :
  ∫⁻ x in s, μ.rn_deriv ν x ∂ν < ∞ :=
begin
  by_cases hl : have_lebesgue_decomposition μ ν,
  { haveI := hl,
    obtain ⟨-, -, hadd⟩ := have_lebesgue_decomposition_spec μ ν,
    suffices : ∫⁻ x in to_measurable μ s, μ.rn_deriv ν x ∂ν < ∞,
      from lt_of_le_of_lt (lintegral_mono_set (subset_to_measurable _ _)) this,
    rw [← with_density_apply _ (measurable_set_to_measurable _ _)],
    refine lt_of_le_of_lt
      (le_add_left le_rfl : _ ≤ μ.singular_part ν (to_measurable μ s) +
        ν.with_density (μ.rn_deriv ν) (to_measurable μ s)) _,
    rw [← measure.add_apply, ← hadd, measure_to_measurable],
    exact hs.lt_top },
  { erw [measure.rn_deriv, dif_neg hl, lintegral_zero],
    exact with_top.zero_lt_top },
end

lemma lintegral_rn_deriv_lt_top
  (μ ν : measure α) [is_finite_measure μ] :
  ∫⁻ x, μ.rn_deriv ν x ∂ν < ∞ :=
begin
  rw [← set_lintegral_univ],
  exact lintegral_rn_deriv_lt_top_of_measure_ne_top _ (measure_lt_top _ _).ne,
end

/-- The Radon-Nikodym derivative of a sigma-finite measure `μ` with respect to another
measure `ν` is `ν`-almost everywhere finite. -/
theorem rn_deriv_lt_top (μ ν : measure α) [sigma_finite μ] :
  ∀ᵐ x ∂ν, μ.rn_deriv ν x < ∞ :=
begin
  suffices : ∀ n, ∀ᵐ x ∂ν, x ∈ spanning_sets μ n → μ.rn_deriv ν x < ∞,
  { filter_upwards [ae_all_iff.2 this] with _ hx using hx _ (mem_spanning_sets_index _ _), },
  assume n,
  rw ← ae_restrict_iff' (measurable_spanning_sets _ _),
  apply ae_lt_top (measurable_rn_deriv _ _),
  refine (lintegral_rn_deriv_lt_top_of_measure_ne_top _ _).ne,
  exact (measure_spanning_sets_lt_top _ _).ne
end

/-- Given measures `μ` and `ν`, if `s` is a measure mutually singular to `ν` and `f` is a
measurable function such that `μ = s + fν`, then `s = μ.singular_part μ`.

This theorem provides the uniqueness of the `singular_part` in the Lebesgue decomposition theorem,
while `measure_theory.measure.eq_rn_deriv` provides the uniqueness of the
`rn_deriv`. -/
theorem eq_singular_part {s : measure α} {f : α → ℝ≥0∞} (hf : measurable f)
  (hs : s ⊥ₘ ν) (hadd : μ = s + ν.with_density f) :
  s = μ.singular_part ν :=
begin
  haveI : have_lebesgue_decomposition μ ν := ⟨⟨⟨s, f⟩, hf, hs, hadd⟩⟩,
  obtain ⟨hmeas, hsing, hadd'⟩ := have_lebesgue_decomposition_spec μ ν,
  obtain ⟨⟨S, hS₁, hS₂, hS₃⟩, ⟨T, hT₁, hT₂, hT₃⟩⟩ := ⟨hs, hsing⟩,
  rw hadd' at hadd,
  have hνinter : ν (S ∩ T)ᶜ = 0,
  { rw compl_inter,
    refine nonpos_iff_eq_zero.1 (le_trans (measure_union_le _ _) _),
    rw [hT₃, hS₃, add_zero],
    exact le_rfl },
  have heq : s.restrict (S ∩ T)ᶜ = (μ.singular_part ν).restrict (S ∩ T)ᶜ,
  { ext1 A hA,
    have hf : ν.with_density f (A ∩ (S ∩ T)ᶜ) = 0,
    { refine with_density_absolutely_continuous ν _ _,
      rw ← nonpos_iff_eq_zero,
      exact hνinter ▸ measure_mono (inter_subset_right _ _) },
    have hrn : ν.with_density (μ.rn_deriv ν) (A ∩ (S ∩ T)ᶜ) = 0,
    { refine with_density_absolutely_continuous ν _ _,
      rw ← nonpos_iff_eq_zero,
      exact hνinter ▸ measure_mono (inter_subset_right _ _) },
    rw [restrict_apply hA, restrict_apply hA, ← add_zero (s (A ∩ (S ∩ T)ᶜ)), ← hf,
        ← add_apply, ← hadd, add_apply, hrn, add_zero] },
  have heq' : ∀ A : set α, measurable_set A → s A = s.restrict (S ∩ T)ᶜ A,
  { intros A hA,
    have hsinter : s (A ∩ (S ∩ T)) = 0,
    { rw ← nonpos_iff_eq_zero,
      exact hS₂ ▸ measure_mono ((inter_subset_right _ _).trans (inter_subset_left _ _)) },
    rw [restrict_apply hA, ← diff_eq, ae_disjoint.measure_diff_left hsinter] },
  ext1 A hA,
  have hμinter : μ.singular_part ν (A ∩ (S ∩ T)) = 0,
  { rw ← nonpos_iff_eq_zero,
    exact hT₂ ▸ measure_mono ((inter_subset_right _ _).trans (inter_subset_right _ _)) },
  rw [heq' A hA, heq, restrict_apply hA, ← diff_eq, ae_disjoint.measure_diff_left hμinter]
end

lemma singular_part_zero (ν : measure α) : (0 : measure α).singular_part ν = 0 :=
begin
  refine (eq_singular_part measurable_zero mutually_singular.zero_left _).symm,
  rw [zero_add, with_density_zero],
end

lemma singular_part_smul (μ ν : measure α) (r : ℝ≥0) :
  (r • μ).singular_part ν = r • (μ.singular_part ν) :=
begin
  by_cases hr : r = 0,
  { rw [hr, zero_smul, zero_smul, singular_part_zero] },
  by_cases hl : have_lebesgue_decomposition μ ν,
  { haveI := hl,
    refine (eq_singular_part ((measurable_rn_deriv μ ν).const_smul (r : ℝ≥0∞))
      (mutually_singular.smul r (have_lebesgue_decomposition_spec _ _).2.1) _).symm,
    rw [with_density_smul _ (measurable_rn_deriv _ _), ← smul_add,
      ← have_lebesgue_decomposition_add μ ν, ennreal.smul_def] },
  { rw [singular_part, singular_part, dif_neg hl, dif_neg, smul_zero],
    refine λ hl', hl _,
    rw ← inv_smul_smul₀ hr μ,
    exact @measure.have_lebesgue_decomposition_smul _ _ _ _ hl' _ }
end

lemma singular_part_add (μ₁ μ₂ ν : measure α)
  [have_lebesgue_decomposition μ₁ ν] [have_lebesgue_decomposition μ₂ ν] :
  (μ₁ + μ₂).singular_part ν = μ₁.singular_part ν + μ₂.singular_part ν :=
begin
  refine (eq_singular_part
    ((measurable_rn_deriv μ₁ ν).add (measurable_rn_deriv μ₂ ν))
    ((have_lebesgue_decomposition_spec _ _).2.1.add_left (have_lebesgue_decomposition_spec _ _).2.1)
    _).symm,
  erw with_density_add_left (measurable_rn_deriv μ₁ ν),
  conv_rhs { rw [add_assoc, add_comm (μ₂.singular_part ν), ← add_assoc, ← add_assoc] },
  rw [← have_lebesgue_decomposition_add μ₁ ν, add_assoc,
      add_comm (ν.with_density (μ₂.rn_deriv ν)),
      ← have_lebesgue_decomposition_add μ₂ ν]
end

lemma singular_part_with_density (ν : measure α) {f : α → ℝ≥0∞} (hf : measurable f) :
  (ν.with_density f).singular_part ν = 0 :=
begin
  have : ν.with_density f = 0 + ν.with_density f, by rw zero_add,
  exact (eq_singular_part hf mutually_singular.zero_left this).symm,
end

/-- Given measures `μ` and `ν`, if `s` is a measure mutually singular to `ν` and `f` is a
measurable function such that `μ = s + fν`, then `f = μ.rn_deriv ν`.

This theorem provides the uniqueness of the `rn_deriv` in the Lebesgue decomposition
theorem, while `measure_theory.measure.eq_singular_part` provides the uniqueness of the
`singular_part`. Here, the uniqueness is given in terms of the measures, while the uniqueness in
terms of the functions is given in `eq_rn_deriv`. -/
theorem eq_with_density_rn_deriv {s : measure α} {f : α → ℝ≥0∞} (hf : measurable f)
  (hs : s ⊥ₘ ν) (hadd : μ = s + ν.with_density f) :
  ν.with_density f = ν.with_density (μ.rn_deriv ν) :=
begin
  haveI : have_lebesgue_decomposition μ ν := ⟨⟨⟨s, f⟩, hf, hs, hadd⟩⟩,
  obtain ⟨hmeas, hsing, hadd'⟩ := have_lebesgue_decomposition_spec μ ν,
  obtain ⟨⟨S, hS₁, hS₂, hS₃⟩, ⟨T, hT₁, hT₂, hT₃⟩⟩ := ⟨hs, hsing⟩,
  rw hadd' at hadd,
  have hνinter : ν (S ∩ T)ᶜ = 0,
  { rw compl_inter,
    refine nonpos_iff_eq_zero.1 (le_trans (measure_union_le _ _) _),
    rw [hT₃, hS₃, add_zero],
    exact le_rfl },
  have heq : (ν.with_density f).restrict (S ∩ T) =
              (ν.with_density (μ.rn_deriv ν)).restrict (S ∩ T),
  { ext1 A hA,
    have hs : s (A ∩ (S ∩ T)) = 0,
    { rw ← nonpos_iff_eq_zero,
      exact hS₂ ▸ measure_mono ((inter_subset_right _ _).trans (inter_subset_left _ _)) },
    have hsing : μ.singular_part ν (A ∩ (S ∩ T)) = 0,
    { rw ← nonpos_iff_eq_zero,
      exact hT₂ ▸ measure_mono
        ((inter_subset_right _ _).trans (inter_subset_right _ _)) },
    rw [restrict_apply hA, restrict_apply hA, ← add_zero (ν.with_density f (A ∩ (S ∩ T))),
        ← hs, ← add_apply, add_comm, ← hadd, add_apply, hsing, zero_add] },
  have heq' : ∀ A : set α, measurable_set A →
    ν.with_density f A = (ν.with_density f).restrict (S ∩ T) A,
  { intros A hA,
    have hνfinter : ν.with_density f (A ∩ (S ∩ T)ᶜ) = 0,
    { rw ← nonpos_iff_eq_zero,
      exact with_density_absolutely_continuous ν f hνinter ▸
        measure_mono (inter_subset_right _ _) },
    rw [restrict_apply hA, ← add_zero (ν.with_density f (A ∩ (S ∩ T))), ← hνfinter,
        ← diff_eq, measure_inter_add_diff _ (hS₁.inter hT₁)] },
  ext1 A hA,
  have hνrn : ν.with_density (μ.rn_deriv ν) (A ∩ (S ∩ T)ᶜ) = 0,
  { rw ← nonpos_iff_eq_zero,
    exact with_density_absolutely_continuous ν (μ.rn_deriv ν) hνinter ▸
      measure_mono (inter_subset_right _ _) },
  rw [heq' A hA, heq, ← add_zero ((ν.with_density (μ.rn_deriv ν)).restrict (S ∩ T) A),
      ← hνrn, restrict_apply hA, ← diff_eq, measure_inter_add_diff _ (hS₁.inter hT₁)]
end

/-- Given measures `μ` and `ν`, if `s` is a measure mutually singular to `ν` and `f` is a
measurable function such that `μ = s + fν`, then `f = μ.rn_deriv ν`.

This theorem provides the uniqueness of the `rn_deriv` in the Lebesgue decomposition
theorem, while `measure_theory.measure.eq_singular_part` provides the uniqueness of the
`singular_part`. Here, the uniqueness is given in terms of the functions, while the uniqueness in
terms of the functions is given in `eq_with_density_rn_deriv`. -/
theorem eq_rn_deriv [sigma_finite ν] {s : measure α} {f : α → ℝ≥0∞} (hf : measurable f)
  (hs : s ⊥ₘ ν) (hadd : μ = s + ν.with_density f) :
  f =ᵐ[ν] μ.rn_deriv ν :=
begin
  refine ae_eq_of_forall_set_lintegral_eq_of_sigma_finite hf (measurable_rn_deriv μ ν) _,
  assume a ha h'a,
  calc ∫⁻ (x : α) in a, f x ∂ν = ν.with_density f a : (with_density_apply f ha).symm
  ... = ν.with_density (μ.rn_deriv ν) a : by rw eq_with_density_rn_deriv hf hs hadd
  ... = ∫⁻ (x : α) in a, μ.rn_deriv ν x ∂ν : with_density_apply _ ha
end

/-- The Radon-Nikodym derivative of `f ν` with respect to `ν` is `f`. -/
theorem rn_deriv_with_density (ν : measure α) [sigma_finite ν] {f : α → ℝ≥0∞} (hf : measurable f) :
  (ν.with_density f).rn_deriv ν =ᵐ[ν] f :=
begin
  have : ν.with_density f = 0 + ν.with_density f, by rw zero_add,
  exact (eq_rn_deriv hf mutually_singular.zero_left this).symm,
end

/-- The Radon-Nikodym derivative of the restriction of a measure to a measurable set is the
indicator function of this set. -/
theorem rn_deriv_restrict (ν : measure α) [sigma_finite ν] {s : set α} (hs : measurable_set s) :
  (ν.restrict s).rn_deriv ν =ᵐ[ν] s.indicator 1 :=
begin
  rw ← with_density_indicator_one hs,
  exact rn_deriv_with_density _ (measurable_one.indicator hs)
end

open vector_measure signed_measure

/-- If two finite measures `μ` and `ν` are not mutually singular, there exists some `ε > 0` and
a measurable set `E`, such that `ν(E) > 0` and `E` is positive with respect to `μ - εν`.

This lemma is useful for the Lebesgue decomposition theorem. -/
lemma exists_positive_of_not_mutually_singular
  (μ ν : measure α) [is_finite_measure μ] [is_finite_measure ν] (h : ¬ μ ⊥ₘ ν) :
  ∃ ε : ℝ≥0, 0 < ε ∧ ∃ E : set α, measurable_set E ∧ 0 < ν E ∧
  0 ≤[E] μ.to_signed_measure - (ε • ν).to_signed_measure :=
begin
  -- for all `n : ℕ`, obtain the Hahn decomposition for `μ - (1 / n) ν`
  have : ∀ n : ℕ, ∃ i : set α, measurable_set i ∧
    0 ≤[i] (μ.to_signed_measure - ((1 / (n + 1) : ℝ≥0) • ν).to_signed_measure) ∧
    (μ.to_signed_measure - ((1 / (n + 1) : ℝ≥0) • ν).to_signed_measure) ≤[iᶜ] 0,
  { intro, exact exists_compl_positive_negative _ },
  choose f hf₁ hf₂ hf₃ using this,
  -- set `A` to be the intersection of all the negative parts of obtained Hahn decompositions
  -- and we show that `μ A = 0`
  set A := ⋂ n, (f n)ᶜ with hA₁,
  have hAmeas : measurable_set A,
  { exact measurable_set.Inter (λ n, (hf₁ n).compl) },
  have hA₂ : ∀ n : ℕ, (μ.to_signed_measure - ((1 / (n + 1) : ℝ≥0) • ν).to_signed_measure) ≤[A] 0,
  { intro n, exact restrict_le_restrict_subset _ _ (hf₁ n).compl (hf₃ n) (Inter_subset _ _) },
  have hA₃ : ∀ n : ℕ, μ A ≤ (1 / (n + 1) : ℝ≥0) * ν A,
  { intro n,
    have := nonpos_of_restrict_le_zero _ (hA₂ n),
    rwa [to_signed_measure_sub_apply hAmeas, sub_nonpos, ennreal.to_real_le_to_real] at this,
    exacts [ne_of_lt (measure_lt_top _ _), ne_of_lt (measure_lt_top _ _)] },
  have hμ : μ A = 0,
  { lift μ A to ℝ≥0 using ne_of_lt (measure_lt_top _ _) with μA,
    lift ν A to ℝ≥0 using ne_of_lt (measure_lt_top _ _) with νA,
    rw ennreal.coe_eq_zero,
    by_cases hb : 0 < νA,
    { suffices : ∀ b, 0 < b → μA ≤ b,
      { by_contra,
        have h' := this (μA / 2) (nnreal.half_pos (zero_lt_iff.2 h)),
        rw ← @not_not (μA ≤ μA / 2) at h',
        exact h' (not_le.2 (nnreal.half_lt_self h)) },
      intros c hc,
      have : ∃ n : ℕ, 1 / (n + 1 : ℝ) < c * νA⁻¹, refine exists_nat_one_div_lt _,
      { refine mul_pos hc _,
        rw _root_.inv_pos, exact hb },
      rcases this with ⟨n, hn⟩,
      have hb₁ : (0 : ℝ) < νA⁻¹, { rw _root_.inv_pos, exact hb },
      have h' : 1 / (↑n + 1) * νA < c,
      { rw [← nnreal.coe_lt_coe, ← mul_lt_mul_right hb₁, nnreal.coe_mul, mul_assoc,
            ← nnreal.coe_inv, ← nnreal.coe_mul, _root_.mul_inv_cancel, ← nnreal.coe_mul,
            mul_one, nnreal.coe_inv],
        { exact hn },
        { exact ne.symm (ne_of_lt hb) } },
      refine le_trans _ (le_of_lt h'),
      rw [← ennreal.coe_le_coe, ennreal.coe_mul],
      exact hA₃ n },
    { rw [not_lt, le_zero_iff] at hb,
      specialize hA₃ 0,
      simp [hb, le_zero_iff] at hA₃,
      assumption } },
  -- since `μ` and `ν` are not mutually singular, `μ A = 0` implies `ν Aᶜ > 0`
  rw mutually_singular at h, push_neg at h,
  have := h _ hAmeas hμ,
  simp_rw [hA₁, compl_Inter, compl_compl] at this,
  -- as `Aᶜ = ⋃ n, f n`, `ν Aᶜ > 0` implies there exists some `n` such that `ν (f n) > 0`
  obtain ⟨n, hn⟩ := exists_measure_pos_of_not_measure_Union_null this,
  -- thus, choosing `f n` as the set `E` suffices
  exact ⟨1 / (n + 1), by simp, f n, hf₁ n, hn, hf₂ n⟩,
end

namespace lebesgue_decomposition

/-- Given two measures `μ` and `ν`, `measurable_le μ ν` is the set of measurable
functions `f`, such that, for all measurable sets `A`, `∫⁻ x in A, f x ∂μ ≤ ν A`.

This is useful for the Lebesgue decomposition theorem. -/
def measurable_le (μ ν : measure α) : set (α → ℝ≥0∞) :=
{ f | measurable f ∧ ∀ (A : set α) (hA : measurable_set A), ∫⁻ x in A, f x ∂μ ≤ ν A }

lemma zero_mem_measurable_le : (0 : α → ℝ≥0∞) ∈ measurable_le μ ν :=
⟨measurable_zero, λ A hA, by simp⟩

lemma sup_mem_measurable_le {f g : α → ℝ≥0∞}
  (hf : f ∈ measurable_le μ ν) (hg : g ∈ measurable_le μ ν) :
  (λ a, f a ⊔ g a) ∈ measurable_le μ ν :=
begin
  simp_rw ennreal.sup_eq_max,
  refine ⟨measurable.max hf.1 hg.1, λ A hA, _⟩,
  have h₁ := hA.inter (measurable_set_le hf.1 hg.1),
  have h₂ := hA.inter (measurable_set_lt hg.1 hf.1),
  rw [set_lintegral_max hf.1 hg.1],
  refine (add_le_add (hg.2 _ h₁) (hf.2 _ h₂)).trans_eq _,
  { simp only [← not_le, ← compl_set_of, ← diff_eq],
    exact measure_inter_add_diff _ (measurable_set_le hf.1 hg.1) }
end

lemma supr_succ_eq_sup {α} (f : ℕ → α → ℝ≥0∞) (m : ℕ) (a : α) :
  (⨆ (k : ℕ) (hk : k ≤ m + 1), f k a) = f m.succ a ⊔ ⨆ (k : ℕ) (hk : k ≤ m), f k a :=
begin
  ext x,
  simp only [option.mem_def, ennreal.some_eq_coe],
  split; intro h; rw ← h, symmetry,
  all_goals
  { set c := (⨆ (k : ℕ) (hk : k ≤ m + 1), f k a) with hc,
    set d := (f m.succ a ⊔ ⨆ (k : ℕ) (hk : k ≤ m), f k a) with hd,
    rw [@le_antisymm_iff ℝ≥0∞, hc, hd], -- Specifying the type is weirdly necessary
    refine ⟨_, _⟩,
    { refine supr₂_le (λ n hn, _),
      rcases nat.of_le_succ hn with (h | h),
      { exact le_sup_of_le_right (le_supr₂ n h) },
      { exact h ▸ le_sup_left } },
    { refine sup_le _ (bsupr_mono $ λ n hn, hn.trans m.le_succ),
      convert @le_supr₂ _ _ (λ i, i ≤ m + 1) _ _ m.succ le_rfl,
      refl } }
end

lemma supr_mem_measurable_le
  (f : ℕ → α → ℝ≥0∞) (hf : ∀ n, f n ∈ measurable_le μ ν) (n : ℕ) :
  (λ x, ⨆ k (hk : k ≤ n), f k x) ∈ measurable_le μ ν :=
begin
  induction n with m hm,
  { refine ⟨_, _⟩,
    { simp [(hf 0).1] },
    { intros A hA, simp [(hf 0).2 A hA] } },
  { have : (λ (a : α), ⨆ (k : ℕ) (hk : k ≤ m + 1), f k a) =
      (λ a, f m.succ a ⊔ ⨆ (k : ℕ) (hk : k ≤ m), f k a),
    { exact funext (λ _, supr_succ_eq_sup _ _ _) },
    refine ⟨measurable_supr (λ n, measurable.supr_Prop _ (hf n).1), λ A hA, _⟩,
    rw this, exact (sup_mem_measurable_le (hf m.succ) hm).2 A hA }
end

lemma supr_mem_measurable_le'
  (f : ℕ → α → ℝ≥0∞) (hf : ∀ n, f n ∈ measurable_le μ ν) (n : ℕ) :
  (⨆ k (hk : k ≤ n), f k) ∈ measurable_le μ ν :=
begin
  convert supr_mem_measurable_le f hf n,
  ext, simp
end

section supr_lemmas --TODO: these statements should be moved elsewhere
omit m

lemma supr_monotone {α : Type*} (f : ℕ → α → ℝ≥0∞) :
  monotone (λ n x, ⨆ k (hk : k ≤ n), f k x) :=
λ n m hnm x, bsupr_mono $ λ i, ge_trans hnm

lemma supr_monotone' {α : Type*} (f : ℕ → α → ℝ≥0∞) (x : α) :
  monotone (λ n, ⨆ k (hk : k ≤ n), f k x) :=
λ n m hnm, supr_monotone f hnm x

lemma supr_le_le {α : Type*} (f : ℕ → α → ℝ≥0∞) (n k : ℕ) (hk : k ≤ n) :
  f k ≤ λ x, ⨆ k (hk : k ≤ n), f k x :=
λ x, le_supr₂ k hk

end supr_lemmas

/-- `measurable_le_eval μ ν` is the set of `∫⁻ x, f x ∂μ` for all `f ∈ measurable_le μ ν`. -/
def measurable_le_eval (μ ν : measure α) : set ℝ≥0∞ :=
(λ f : α → ℝ≥0∞, ∫⁻ x, f x ∂μ) '' measurable_le μ ν

end lebesgue_decomposition

open lebesgue_decomposition

/-- Any pair of finite measures `μ` and `ν`, `have_lebesgue_decomposition`. That is to say,
there exist a measure `ξ` and a measurable function `f`, such that `ξ` is mutually singular
with respect to `ν` and `μ = ξ + ν.with_density f`.

This is not an instance since this is also shown for the more general σ-finite measures with
`measure_theory.measure.have_lebesgue_decomposition_of_sigma_finite`. -/
theorem have_lebesgue_decomposition_of_finite_measure [is_finite_measure μ] [is_finite_measure ν] :
  have_lebesgue_decomposition μ ν :=
⟨begin
  have h := @exists_seq_tendsto_Sup _ _ _ _ _ (measurable_le_eval ν μ)
    ⟨0, 0, zero_mem_measurable_le, by simp⟩ (order_top.bdd_above _),
  choose g hmono hg₂ f hf₁ hf₂ using h,
  -- we set `ξ` to be the supremum of an increasing sequence of functions obtained from above
  set ξ := ⨆ n k (hk : k ≤ n), f k with hξ,
  -- we see that `ξ` has the largest integral among all functions in `measurable_le`
  have hξ₁ : Sup (measurable_le_eval ν μ) = ∫⁻ a, ξ a ∂ν,
  { have := @lintegral_tendsto_of_tendsto_of_monotone _ _ ν
      (λ n, ⨆ k (hk : k ≤ n), f k) (⨆ n k (hk : k ≤ n), f k) _ _ _,
    { refine tendsto_nhds_unique _ this,
      refine tendsto_of_tendsto_of_tendsto_of_le_of_le hg₂ tendsto_const_nhds _ _,
      { intro n, rw ← hf₂ n,
        apply lintegral_mono,
        simp only [supr_apply, supr_le_le f n n le_rfl] },
      { intro n,
        exact le_Sup ⟨⨆ (k : ℕ) (hk : k ≤ n), f k, supr_mem_measurable_le' _ hf₁ _, rfl⟩ } },
    { intro n,
      refine measurable.ae_measurable _,
      convert (supr_mem_measurable_le _ hf₁ n).1,
      ext, simp },
    { refine filter.eventually_of_forall (λ a, _),
      simp [supr_monotone' f _] },
    { refine filter.eventually_of_forall (λ a, _),
      simp [tendsto_at_top_supr (supr_monotone' f a)] } },
  have hξm : measurable ξ,
  { convert measurable_supr (λ n, (supr_mem_measurable_le _ hf₁ n).1),
    ext, simp [hξ] },
  -- `ξ` is the `f` in the theorem statement and we set `μ₁` to be `μ - ν.with_density ξ`
  -- since we need `μ₁ + ν.with_density ξ = μ`
  set μ₁ := μ - ν.with_density ξ with hμ₁,
  have hle : ν.with_density ξ ≤ μ,
  { intros B hB,
    rw [hξ, with_density_apply _ hB],
    simp_rw [supr_apply],
    rw lintegral_supr (λ i, (supr_mem_measurable_le _ hf₁ i).1) (supr_monotone _),
    exact supr_le (λ i, (supr_mem_measurable_le _ hf₁ i).2 B hB) },
  haveI : is_finite_measure (ν.with_density ξ),
  { refine is_finite_measure_with_density _,
    have hle' := hle univ measurable_set.univ,
    rw [with_density_apply _ measurable_set.univ, measure.restrict_univ] at hle',
    exact ne_top_of_le_ne_top (measure_ne_top _ _) hle' },
  refine ⟨⟨μ₁, ξ⟩, hξm, _, _⟩,
  { by_contra,
  -- if they are not mutually singular, then from `exists_positive_of_not_mutually_singular`,
  -- there exists some `ε > 0` and a measurable set `E`, such that `μ(E) > 0` and `E` is
  -- positive with respect to `ν - εμ`
    obtain ⟨ε, hε₁, E, hE₁, hE₂, hE₃⟩ := exists_positive_of_not_mutually_singular μ₁ ν h,
    simp_rw hμ₁ at hE₃,
    have hξle : ∀ A, measurable_set A → ∫⁻ a in A, ξ a ∂ν ≤ μ A,
    { intros A hA, rw hξ,
      simp_rw [supr_apply],
      rw lintegral_supr (λ n, (supr_mem_measurable_le _ hf₁ n).1) (supr_monotone _),
      exact supr_le (λ n, (supr_mem_measurable_le _ hf₁ n).2 A hA) },
  -- since `E` is positive, we have `∫⁻ a in A ∩ E, ε + ξ a ∂ν ≤ μ (A ∩ E)` for all `A`
    have hε₂ : ∀ A : set α, measurable_set A → ∫⁻ a in A ∩ E, ε + ξ a ∂ν ≤ μ (A ∩ E),
    { intros A hA,
      have := subset_le_of_restrict_le_restrict _ _ hE₁ hE₃ (inter_subset_right A E),
      rwa [zero_apply, to_signed_measure_sub_apply (hA.inter hE₁),
            measure.sub_apply (hA.inter hE₁) hle,
            ennreal.to_real_sub_of_le _ (ne_of_lt (measure_lt_top _ _)), sub_nonneg,
            le_sub_iff_add_le, ← ennreal.to_real_add, ennreal.to_real_le_to_real,
            measure.coe_smul, pi.smul_apply, with_density_apply _ (hA.inter hE₁),
            show ε • ν (A ∩ E) = (ε : ℝ≥0∞) * ν (A ∩ E), by refl,
            ← set_lintegral_const, ← lintegral_add_left measurable_const] at this,
      { rw [ne.def, ennreal.add_eq_top, not_or_distrib],
        exact ⟨ne_of_lt (measure_lt_top _ _), ne_of_lt (measure_lt_top _ _)⟩ },
      { exact ne_of_lt (measure_lt_top _ _) },
      { exact ne_of_lt (measure_lt_top _ _) },
      { exact ne_of_lt (measure_lt_top _ _) },
      { rw with_density_apply _ (hA.inter hE₁),
        exact hξle (A ∩ E) (hA.inter hE₁) },
      { apply_instance } },
  -- from this, we can show `ξ + ε * E.indicator` is a function in `measurable_le` with
  -- integral greater than `ξ`
    have hξε : ξ + E.indicator (λ _, ε) ∈ measurable_le ν μ,
    { refine ⟨measurable.add hξm (measurable.indicator measurable_const hE₁), λ A hA, _⟩,
      have : ∫⁻ a in A, (ξ + E.indicator (λ _, ε)) a ∂ν =
            ∫⁻ a in A ∩ E, ε + ξ a ∂ν + ∫⁻ a in A \ E, ξ a ∂ν,
      { simp only [lintegral_add_left measurable_const, lintegral_add_left hξm,
          set_lintegral_const, add_assoc, lintegral_inter_add_diff _ _ hE₁, pi.add_apply,
          lintegral_indicator _ hE₁, restrict_apply hE₁],
        rw [inter_comm, add_comm] },
      rw [this, ← measure_inter_add_diff A hE₁],
      exact add_le_add (hε₂ A hA) (hξle (A \ E) (hA.diff hE₁)) },
      have : ∫⁻ a, ξ a + E.indicator (λ _, ε) a ∂ν ≤ Sup (measurable_le_eval ν μ) :=
        le_Sup ⟨ξ + E.indicator (λ _, ε), hξε, rfl⟩,
  -- but this contradicts the maximality of `∫⁻ x, ξ x ∂ν`
      refine not_lt.2 this _,
      rw [hξ₁, lintegral_add_left hξm, lintegral_indicator _ hE₁, set_lintegral_const],
      refine ennreal.lt_add_right _ (ennreal.mul_pos_iff.2 ⟨ennreal.coe_pos.2 hε₁, hE₂⟩).ne',
      have := measure_ne_top (ν.with_density ξ) univ,
      rwa [with_density_apply _ measurable_set.univ, measure.restrict_univ] at this },
  -- since `ν.with_density ξ ≤ μ`, it is clear that `μ = μ₁ + ν.with_density ξ`
  { rw hμ₁, ext1 A hA,
    rw [measure.coe_add, pi.add_apply, measure.sub_apply hA hle,
        add_comm, add_tsub_cancel_of_le (hle A hA)] },
end⟩

local attribute [instance] have_lebesgue_decomposition_of_finite_measure

instance {S : μ.finite_spanning_sets_in {s : set α | measurable_set s}} (n : ℕ) :
  is_finite_measure (μ.restrict $ S.set n) :=
⟨by { rw [restrict_apply measurable_set.univ, univ_inter], exact S.finite _ }⟩

/-- **The Lebesgue decomposition theorem**: Any pair of σ-finite measures `μ` and `ν`
`have_lebesgue_decomposition`. That is to say, there exist a measure `ξ` and a measurable function
`f`, such that `ξ` is mutually singular with respect to `ν` and `μ = ξ + ν.with_density f` -/
@[priority 100] -- see Note [lower instance priority]
instance have_lebesgue_decomposition_of_sigma_finite
  (μ ν : measure α) [sigma_finite μ] [sigma_finite ν] :
  have_lebesgue_decomposition μ ν :=
⟨begin
  -- Since `μ` and `ν` are both σ-finite, there exists a sequence of pairwise disjoint spanning
  -- sets which are finite with respect to both `μ` and `ν`
  obtain ⟨S, T, h₁, h₂⟩ := exists_eq_disjoint_finite_spanning_sets_in μ ν,
  have h₃ : pairwise (disjoint on T.set) := h₁ ▸ h₂,
  -- We define `μn` and `νn` as sequences of measures such that `μn n = μ ∩ S n` and
  -- `νn n = ν ∩ S n` where `S` is the aforementioned finite spanning set sequence.
  -- Since `S` is spanning, it is clear that `sum μn = μ` and `sum νn = ν`
  set μn : ℕ → measure α := λ n, μ.restrict (S.set n) with hμn,
  have hμ : μ = sum μn,
    { rw [hμn, ← restrict_Union h₂ S.set_mem, S.spanning, restrict_univ] },
  set νn : ℕ → measure α := λ n, ν.restrict (T.set n) with hνn,
  have hν : ν = sum νn,
    { rw [hνn, ← restrict_Union h₃ T.set_mem, T.spanning, restrict_univ] },
  -- As `S` is finite with respect to both `μ` and `ν`, it is clear that `μn n` and `νn n` are
  -- finite measures for all `n : ℕ`. Thus, we may apply the finite Lebesgue decomposition theorem
  -- to `μn n` and `νn n`. We define `ξ` as the sum of the singular part of the Lebesgue
  -- decompositions of `μn n` and `νn n`, and `f` as the sum of the Radon-Nikodym derviatives of
  -- `μn n` and `νn n` restricted on `S n`
  set ξ := sum (λ n, singular_part (μn n) (νn n)) with hξ,
  set f := ∑' n, (S.set n).indicator (rn_deriv (μn n) (νn n)) with hf,
  -- I claim `ξ` and `f` form a Lebesgue decomposition of `μ` and `ν`
  refine ⟨⟨ξ, f⟩, _, _, _⟩,
  { exact measurable.ennreal_tsum' (λ n, measurable.indicator
      (measurable_rn_deriv (μn n) (νn n)) (S.set_mem n)) },
  -- We show that `ξ` is mutually singular with respect to `ν`
  { choose A hA₁ hA₂ hA₃ using λ n, mutually_singular_singular_part (μn n) (νn n),
    simp only [hξ],
  -- We use the set `B := ⋃ j, (S.set j) ∩ A j` where `A n` is the set provided as
  -- `singular_part (μn n) (νn n) ⊥ₘ νn n`
    refine ⟨⋃ j, (S.set j) ∩ A j,
      measurable_set.Union (λ n, (S.set_mem n).inter (hA₁ n)), _, _⟩,
  -- `ξ B = 0` since `ξ B = ∑ i j, singular_part (μn j) (νn j) (S.set i ∩ A i)`
  --                     `= ∑ i, singular_part (μn i) (νn i) (S.set i ∩ A i)`
  --                     `≤ ∑ i, singular_part (μn i) (νn i) (A i) = 0`
  -- where the second equality follows as `singular_part (μn j) (νn j) (S.set i ∩ A i)` vanishes
  -- for all `i ≠ j`
    { rw [measure_Union],
      { have : ∀ i, (sum (λ n, (μn n).singular_part (νn n))) (S.set i ∩ A i) =
          (μn i).singular_part (νn i) (S.set i ∩ A i),
        { intro i, rw [sum_apply _ ((S.set_mem i).inter (hA₁ i)), tsum_eq_single i],
          { intros j hij,
            rw [hμn, ← nonpos_iff_eq_zero],
            refine le_trans ((singular_part_le _ _) _ ((S.set_mem i).inter (hA₁ i))) (le_of_eq _),
            rw [restrict_apply ((S.set_mem i).inter (hA₁ i)), inter_comm, ← inter_assoc],
            have : disjoint (S.set j) (S.set i) := h₂ j i hij,
            rw disjoint_iff_inter_eq_empty at this,
            rw [this, empty_inter, measure_empty] },
          { apply_instance } },
        simp_rw [this, tsum_eq_zero_iff ennreal.summable],
        intro n, exact measure_mono_null (inter_subset_right _ _) (hA₂ n) },
      { exact h₂.mono (λ i j, disjoint.mono inf_le_left inf_le_left) },
      { exact λ n, (S.set_mem n).inter (hA₁ n) } },
  -- We will now show `ν Bᶜ = 0`. This follows since `Bᶜ = ⋃ n, S.set n ∩ (A n)ᶜ` and thus,
  -- `ν Bᶜ = ∑ i, ν (S.set i ∩ (A i)ᶜ) = ∑ i, (νn i) (A i)ᶜ = 0`
    { have hcompl : is_compl (⋃ n, (S.set n ∩ A n)) (⋃ n, S.set n ∩ (A n)ᶜ),
      { split,
        { rintro x ⟨hx₁, hx₂⟩, rw mem_Union at hx₁ hx₂,
          obtain ⟨⟨i, hi₁, hi₂⟩, ⟨j, hj₁, hj₂⟩⟩ := ⟨hx₁, hx₂⟩,
          have : i = j,
          { by_contra hij, exact h₂ i j hij ⟨hi₁, hj₁⟩ },
          exact hj₂ (this ▸ hi₂) },
        { intros x hx,
          simp only [mem_Union, sup_eq_union, mem_inter_eq,
                    mem_union_eq, mem_compl_eq, or_iff_not_imp_left],
          intro h, push_neg at h,
          rw [top_eq_univ, ← S.spanning, mem_Union] at hx,
          obtain ⟨i, hi⟩ := hx,
          exact ⟨i, hi, h i hi⟩ } },
      rw [hcompl.compl_eq, measure_Union, tsum_eq_zero_iff ennreal.summable],
      { intro n, rw [inter_comm, ← restrict_apply (hA₁ n).compl, ← hA₃ n, hνn, h₁] },
      { exact h₂.mono (λ i j, disjoint.mono inf_le_left inf_le_left) },
      { exact λ n, (S.set_mem n).inter (hA₁ n).compl } } },
  -- Finally, it remains to show `μ = ξ + ν.with_density f`. Since `μ = sum μn`, and
  -- `ξ + ν.with_density f = ∑ n, singular_part (μn n) (νn n)`
  --                        `+ ν.with_density (rn_deriv (μn n) (νn n)) ∩ (S.set n)`,
  -- it suffices to show that the individual summands are equal. This follows by the
  -- Lebesgue decomposition properties on the individual `μn n` and `νn n`
  { simp only [hξ, hf, hμ],
    rw [with_density_tsum _, sum_add_sum],
    { refine sum_congr (λ n, _),
      conv_lhs { rw have_lebesgue_decomposition_add (μn n) (νn n) },
      suffices heq : (νn n).with_density ((μn n).rn_deriv (νn n)) =
        ν.with_density ((S.set n).indicator ((μn n).rn_deriv (νn n))),
      { rw heq },
      rw [hν, with_density_indicator (S.set_mem n), restrict_sum _ (S.set_mem n)],
      suffices hsumeq : sum (λ (i : ℕ), (νn i).restrict (S.set n)) = νn n,
      { rw hsumeq },
      ext1 s hs,
      rw [sum_apply _ hs, tsum_eq_single n, hνn, h₁,
          restrict_restrict (T.set_mem n), inter_self],
      { intros m hm,
        rw [hνn, h₁, restrict_restrict (T.set_mem n),
            disjoint_iff_inter_eq_empty.1 (h₃ n m hm.symm), restrict_empty,
            coe_zero, pi.zero_apply] },
      { apply_instance } },
    { exact λ n, measurable.indicator (measurable_rn_deriv _ _) (S.set_mem n) } },
end⟩

end measure

namespace signed_measure

open measure

/-- A signed measure `s` is said to `have_lebesgue_decomposition` with respect to a measure `μ`
if the positive part and the negative part of `s` both `have_lebesgue_decomposition` with
respect to `μ`. -/
class have_lebesgue_decomposition (s : signed_measure α) (μ : measure α) : Prop :=
(pos_part : s.to_jordan_decomposition.pos_part.have_lebesgue_decomposition μ)
(neg_part : s.to_jordan_decomposition.neg_part.have_lebesgue_decomposition μ)

attribute [instance] have_lebesgue_decomposition.pos_part
attribute [instance] have_lebesgue_decomposition.neg_part

lemma not_have_lebesgue_decomposition_iff (s : signed_measure α)
  (μ : measure α) :
  ¬ s.have_lebesgue_decomposition μ ↔
  ¬ s.to_jordan_decomposition.pos_part.have_lebesgue_decomposition μ ∨
  ¬ s.to_jordan_decomposition.neg_part.have_lebesgue_decomposition μ :=
⟨λ h, not_or_of_imp (λ hp hn, h ⟨hp, hn⟩), λ h hl, (not_and_distrib.2 h) ⟨hl.1, hl.2⟩⟩

-- `infer_instance` directly does not work
@[priority 100] -- see Note [lower instance priority]
instance have_lebesgue_decomposition_of_sigma_finite
  (s : signed_measure α) (μ : measure α) [sigma_finite μ] :
  s.have_lebesgue_decomposition μ :=
{ pos_part := infer_instance,
  neg_part := infer_instance }

instance have_lebesgue_decomposition_neg
  (s : signed_measure α) (μ : measure α) [s.have_lebesgue_decomposition μ] :
  (-s).have_lebesgue_decomposition μ :=
{ pos_part :=
    by { rw [to_jordan_decomposition_neg, jordan_decomposition.neg_pos_part],
         apply_instance },
  neg_part :=
    by { rw [to_jordan_decomposition_neg, jordan_decomposition.neg_neg_part],
         apply_instance } }

instance have_lebesgue_decomposition_smul
  (s : signed_measure α) (μ : measure α) [s.have_lebesgue_decomposition μ] (r : ℝ≥0) :
  (r • s).have_lebesgue_decomposition μ :=
{ pos_part :=
    by { rw [to_jordan_decomposition_smul, jordan_decomposition.smul_pos_part],
         apply_instance },
  neg_part :=
    by { rw [to_jordan_decomposition_smul, jordan_decomposition.smul_neg_part],
         apply_instance } }

instance have_lebesgue_decomposition_smul_real
  (s : signed_measure α) (μ : measure α) [s.have_lebesgue_decomposition μ] (r : ℝ) :
  (r • s).have_lebesgue_decomposition μ :=
begin
  by_cases hr : 0 ≤ r,
  { lift r to ℝ≥0 using hr,
    exact s.have_lebesgue_decomposition_smul μ _ },
  { rw not_le at hr,
    refine
    { pos_part :=
      by { rw [to_jordan_decomposition_smul_real,
               jordan_decomposition.real_smul_pos_part_neg _ _ hr],
           apply_instance },
      neg_part :=
      by { rw [to_jordan_decomposition_smul_real,
               jordan_decomposition.real_smul_neg_part_neg _ _ hr],
           apply_instance } } }
end

/-- Given a signed measure `s` and a measure `μ`, `s.singular_part μ` is the signed measure
such that `s.singular_part μ + μ.with_densityᵥ (s.rn_deriv μ) = s` and
`s.singular_part μ` is mutually singular with respect to `μ`. -/
def singular_part (s : signed_measure α) (μ : measure α) : signed_measure α :=
(s.to_jordan_decomposition.pos_part.singular_part μ).to_signed_measure -
(s.to_jordan_decomposition.neg_part.singular_part μ).to_signed_measure

section

lemma singular_part_mutually_singular (s : signed_measure α) (μ : measure α) :
  s.to_jordan_decomposition.pos_part.singular_part μ ⊥ₘ
  s.to_jordan_decomposition.neg_part.singular_part μ :=
begin
  by_cases hl : s.have_lebesgue_decomposition μ,
  { haveI := hl,
    obtain ⟨i, hi, hpos, hneg⟩ := s.to_jordan_decomposition.mutually_singular,
    rw s.to_jordan_decomposition.pos_part.have_lebesgue_decomposition_add μ at hpos,
    rw s.to_jordan_decomposition.neg_part.have_lebesgue_decomposition_add μ at hneg,
    rw [add_apply, add_eq_zero_iff] at hpos hneg,
    exact ⟨i, hi, hpos.1, hneg.1⟩ },
  { rw not_have_lebesgue_decomposition_iff at hl,
    cases hl with hp hn,
    { rw [measure.singular_part, dif_neg hp],
      exact mutually_singular.zero_left },
    { rw [measure.singular_part, measure.singular_part, dif_neg hn],
      exact mutually_singular.zero_right } }
end

lemma singular_part_total_variation (s : signed_measure α) (μ : measure α) :
  (s.singular_part μ).total_variation =
  s.to_jordan_decomposition.pos_part.singular_part μ +
  s.to_jordan_decomposition.neg_part.singular_part μ :=
begin
  have : (s.singular_part μ).to_jordan_decomposition =
    ⟨s.to_jordan_decomposition.pos_part.singular_part μ,
     s.to_jordan_decomposition.neg_part.singular_part μ, singular_part_mutually_singular s μ⟩,
  { refine jordan_decomposition.to_signed_measure_injective _,
    rw to_signed_measure_to_jordan_decomposition,
    refl },
  { rw [total_variation, this] },
end

lemma mutually_singular_singular_part (s : signed_measure α) (μ : measure α) :
  singular_part s μ ⊥ᵥ μ.to_ennreal_vector_measure :=
begin
  rw [mutually_singular_ennreal_iff, singular_part_total_variation],
  change _ ⊥ₘ vector_measure.equiv_measure.to_fun (vector_measure.equiv_measure.inv_fun μ),
  rw vector_measure.equiv_measure.right_inv μ,
  exact (mutually_singular_singular_part _ _).add_left (mutually_singular_singular_part _ _)
end

end

/-- The Radon-Nikodym derivative between a signed measure and a positive measure.

`rn_deriv s μ` satisfies `μ.with_densityᵥ (s.rn_deriv μ) = s`
if and only if `s` is absolutely continuous with respect to `μ` and this fact is known as
`measure_theory.signed_measure.absolutely_continuous_iff_with_density_rn_deriv_eq`
and can be found in `measure_theory.decomposition.radon_nikodym`. -/
def rn_deriv (s : signed_measure α) (μ : measure α) : α → ℝ := λ x,
(s.to_jordan_decomposition.pos_part.rn_deriv μ x).to_real -
(s.to_jordan_decomposition.neg_part.rn_deriv μ x).to_real

variables {s t : signed_measure α}

@[measurability]
lemma measurable_rn_deriv (s : signed_measure α) (μ : measure α) :
  measurable (rn_deriv s μ) :=
begin
  rw [rn_deriv],
  measurability,
end

lemma integrable_rn_deriv (s : signed_measure α) (μ : measure α) :
  integrable (rn_deriv s μ) μ :=
begin
  refine integrable.sub _ _;
  { split,
    { apply measurable.ae_strongly_measurable, measurability },
    exact has_finite_integral_to_real_of_lintegral_ne_top
      (lintegral_rn_deriv_lt_top _ μ).ne }
end

variables (s μ)

/-- **The Lebesgue Decomposition theorem between a signed measure and a measure**:
Given a signed measure `s` and a σ-finite measure `μ`, there exist a signed measure `t` and a
measurable and integrable function `f`, such that `t` is mutually singular with respect to `μ`
and `s = t + μ.with_densityᵥ f`. In this case `t = s.singular_part μ` and
`f = s.rn_deriv μ`. -/
theorem singular_part_add_with_density_rn_deriv_eq
  [s.have_lebesgue_decomposition μ] :
  s.singular_part μ + μ.with_densityᵥ (s.rn_deriv μ) = s :=
begin
  conv_rhs { rw [← to_signed_measure_to_jordan_decomposition s,
                 jordan_decomposition.to_signed_measure] },
  rw [singular_part, rn_deriv, with_densityᵥ_sub'
        (integrable_to_real_of_lintegral_ne_top _ _) (integrable_to_real_of_lintegral_ne_top _ _),
      with_densityᵥ_to_real, with_densityᵥ_to_real, sub_eq_add_neg, sub_eq_add_neg,
      add_comm (s.to_jordan_decomposition.pos_part.singular_part μ).to_signed_measure, ← add_assoc,
      add_assoc (-(s.to_jordan_decomposition.neg_part.singular_part μ).to_signed_measure),
      ← to_signed_measure_add, add_comm, ← add_assoc, ← neg_add, ← to_signed_measure_add,
      add_comm, ← sub_eq_add_neg],
  convert rfl, -- `convert rfl` much faster than `congr`
  { exact (s.to_jordan_decomposition.pos_part.have_lebesgue_decomposition_add μ) },
  { rw add_comm,
    exact (s.to_jordan_decomposition.neg_part.have_lebesgue_decomposition_add μ) },
  all_goals { exact (lintegral_rn_deriv_lt_top _ _).ne <|> measurability }
end

variables {s μ}

lemma jordan_decomposition_add_with_density_mutually_singular
  {f : α → ℝ} (hf : measurable f) (htμ : t ⊥ᵥ μ.to_ennreal_vector_measure) :
  t.to_jordan_decomposition.pos_part + μ.with_density (λ (x : α), ennreal.of_real (f x)) ⊥ₘ
  t.to_jordan_decomposition.neg_part + μ.with_density (λ (x : α), ennreal.of_real (-f x)) :=
begin
  rw [mutually_singular_ennreal_iff, total_variation_mutually_singular_iff] at htμ,
  change _ ⊥ₘ vector_measure.equiv_measure.to_fun (vector_measure.equiv_measure.inv_fun μ) ∧
         _ ⊥ₘ vector_measure.equiv_measure.to_fun (vector_measure.equiv_measure.inv_fun μ) at htμ,
  rw [vector_measure.equiv_measure.right_inv] at htμ,
  exact ((jordan_decomposition.mutually_singular _).add_right
    (htμ.1.mono_ac (refl _) (with_density_absolutely_continuous _ _))).add_left
    ((htμ.2.symm.mono_ac (with_density_absolutely_continuous _ _) (refl _)).add_right
    (with_density_of_real_mutually_singular hf))
end

lemma to_jordan_decomposition_eq_of_eq_add_with_density
  {f : α → ℝ} (hf : measurable f) (hfi : integrable f μ)
  (htμ : t ⊥ᵥ μ.to_ennreal_vector_measure) (hadd : s = t + μ.with_densityᵥ f) :
  s.to_jordan_decomposition = @jordan_decomposition.mk α _
    (t.to_jordan_decomposition.pos_part + μ.with_density (λ x, ennreal.of_real (f x)))
    (t.to_jordan_decomposition.neg_part + μ.with_density (λ x, ennreal.of_real (- f x)))
    (by { haveI := is_finite_measure_with_density_of_real hfi.2, apply_instance })
    (by { haveI := is_finite_measure_with_density_of_real hfi.neg.2, apply_instance })
    (jordan_decomposition_add_with_density_mutually_singular hf htμ) :=
begin
  haveI := is_finite_measure_with_density_of_real hfi.2,
  haveI := is_finite_measure_with_density_of_real hfi.neg.2,
  refine to_jordan_decomposition_eq _,
  simp_rw [jordan_decomposition.to_signed_measure, hadd],
  ext i hi,
  rw [vector_measure.sub_apply, to_signed_measure_apply_measurable hi,
      to_signed_measure_apply_measurable hi, add_apply, add_apply,
      ennreal.to_real_add, ennreal.to_real_add, add_sub_add_comm,
      ← to_signed_measure_apply_measurable hi, ← to_signed_measure_apply_measurable hi,
      ← vector_measure.sub_apply, ← jordan_decomposition.to_signed_measure,
      to_signed_measure_to_jordan_decomposition, vector_measure.add_apply,
      ← to_signed_measure_apply_measurable hi, ← to_signed_measure_apply_measurable hi,
      with_densityᵥ_eq_with_density_pos_part_sub_with_density_neg_part hfi,
      vector_measure.sub_apply];
  exact (measure_lt_top _ _).ne
end

private lemma have_lebesgue_decomposition_mk' (μ : measure α)
  {f : α → ℝ} (hf : measurable f) (hfi : integrable f μ)
  (htμ : t ⊥ᵥ μ.to_ennreal_vector_measure) (hadd : s = t + μ.with_densityᵥ f) :
  s.have_lebesgue_decomposition μ :=
begin
  have htμ' := htμ,
  rw mutually_singular_ennreal_iff at htμ,
  change _ ⊥ₘ vector_measure.equiv_measure.to_fun (vector_measure.equiv_measure.inv_fun μ) at htμ,
  rw [vector_measure.equiv_measure.right_inv, total_variation_mutually_singular_iff] at htμ,
  refine
    { pos_part :=
      by { use ⟨t.to_jordan_decomposition.pos_part, λ x, ennreal.of_real (f x)⟩,
          refine ⟨hf.ennreal_of_real, htμ.1, _⟩,
          rw to_jordan_decomposition_eq_of_eq_add_with_density hf hfi htμ' hadd },
      neg_part :=
      by { use ⟨t.to_jordan_decomposition.neg_part, λ x, ennreal.of_real (-f x)⟩,
           refine ⟨hf.neg.ennreal_of_real, htμ.2, _⟩,
           rw to_jordan_decomposition_eq_of_eq_add_with_density hf hfi htμ' hadd } }
end

lemma have_lebesgue_decomposition_mk (μ : measure α) {f : α → ℝ} (hf : measurable f)
  (htμ : t ⊥ᵥ μ.to_ennreal_vector_measure) (hadd : s = t + μ.with_densityᵥ f) :
  s.have_lebesgue_decomposition μ :=
begin
  by_cases hfi : integrable f μ,
  { exact have_lebesgue_decomposition_mk' μ hf hfi htμ hadd },
  { rw [with_densityᵥ, dif_neg hfi, add_zero] at hadd,
    refine have_lebesgue_decomposition_mk' μ measurable_zero (integrable_zero _ _ μ) htμ _,
    rwa [with_densityᵥ_zero, add_zero] }
end

private theorem eq_singular_part'
  (t : signed_measure α) {f : α → ℝ} (hf : measurable f) (hfi : integrable f μ)
  (htμ : t ⊥ᵥ μ.to_ennreal_vector_measure) (hadd : s = t + μ.with_densityᵥ f) :
  t = s.singular_part μ :=
begin
  have htμ' := htμ,
  rw [mutually_singular_ennreal_iff, total_variation_mutually_singular_iff] at htμ,
  change _ ⊥ₘ vector_measure.equiv_measure.to_fun (vector_measure.equiv_measure.inv_fun μ) ∧
         _ ⊥ₘ vector_measure.equiv_measure.to_fun (vector_measure.equiv_measure.inv_fun μ) at htμ,
  rw [vector_measure.equiv_measure.right_inv] at htμ,
  { rw [singular_part, ← t.to_signed_measure_to_jordan_decomposition,
        jordan_decomposition.to_signed_measure],
    congr,
    { have hfpos : measurable (λ x, ennreal.of_real (f x)), { measurability },
      refine eq_singular_part hfpos htμ.1 _,
      rw to_jordan_decomposition_eq_of_eq_add_with_density hf hfi htμ' hadd },
    { have hfneg : measurable (λ x, ennreal.of_real (-f x)), { measurability },
      refine eq_singular_part hfneg htμ.2 _,
      rw to_jordan_decomposition_eq_of_eq_add_with_density hf hfi htμ' hadd } },
end

/-- Given a measure `μ`, signed measures `s` and `t`, and a function `f` such that `t` is
mutually singular with respect to `μ` and `s = t + μ.with_densityᵥ f`, we have
`t = singular_part s μ`, i.e. `t` is the singular part of the Lebesgue decomposition between
`s` and `μ`. -/
theorem eq_singular_part (t : signed_measure α) (f : α → ℝ)
  (htμ : t ⊥ᵥ μ.to_ennreal_vector_measure) (hadd : s = t + μ.with_densityᵥ f) :
  t = s.singular_part μ :=
begin
  by_cases hfi : integrable f μ,
  { refine eq_singular_part' t hfi.1.measurable_mk (hfi.congr hfi.1.ae_eq_mk) htμ _,
    convert hadd using 2,
    exact with_densityᵥ_eq.congr_ae hfi.1.ae_eq_mk.symm },
  { rw [with_densityᵥ, dif_neg hfi, add_zero] at hadd,
    refine eq_singular_part' t measurable_zero (integrable_zero _ _ μ) htμ _,
    rwa [with_densityᵥ_zero, add_zero] }
end

lemma singular_part_zero (μ : measure α) : (0 : signed_measure α).singular_part μ = 0 :=
begin
  refine (eq_singular_part 0 0
    vector_measure.mutually_singular.zero_left _).symm,
  rw [zero_add, with_densityᵥ_zero],
end

lemma singular_part_neg (s : signed_measure α) (μ : measure α) :
  (-s).singular_part μ = - s.singular_part μ :=
begin
  have h₁ : ((-s).to_jordan_decomposition.pos_part.singular_part μ).to_signed_measure =
    (s.to_jordan_decomposition.neg_part.singular_part μ).to_signed_measure,
  { refine to_signed_measure_congr _,
    rw [to_jordan_decomposition_neg, jordan_decomposition.neg_pos_part] },
  have h₂ : ((-s).to_jordan_decomposition.neg_part.singular_part μ).to_signed_measure =
    (s.to_jordan_decomposition.pos_part.singular_part μ).to_signed_measure,
  { refine to_signed_measure_congr _,
    rw [to_jordan_decomposition_neg, jordan_decomposition.neg_neg_part] },
  rw [singular_part, singular_part, neg_sub, h₁, h₂],
end

lemma singular_part_smul_nnreal (s : signed_measure α) (μ : measure α) (r : ℝ≥0) :
  (r • s).singular_part μ = r • s.singular_part μ :=
begin
  rw [singular_part, singular_part, smul_sub, ← to_signed_measure_smul, ← to_signed_measure_smul],
  conv_lhs { congr, congr,
             rw [to_jordan_decomposition_smul, jordan_decomposition.smul_pos_part,
                 singular_part_smul], skip, congr,
             rw [to_jordan_decomposition_smul, jordan_decomposition.smul_neg_part,
                 singular_part_smul] }
end

lemma singular_part_smul (s : signed_measure α) (μ : measure α) (r : ℝ) :
  (r • s).singular_part μ = r • s.singular_part μ :=
begin
  by_cases hr : 0 ≤ r,
  { lift r to ℝ≥0 using hr,
    exact singular_part_smul_nnreal s μ r },
  { rw [singular_part, singular_part],
    conv_lhs { congr, congr,
      rw [to_jordan_decomposition_smul_real,
          jordan_decomposition.real_smul_pos_part_neg _ _ (not_le.1 hr), singular_part_smul],
              skip, congr,
      rw [to_jordan_decomposition_smul_real,
          jordan_decomposition.real_smul_neg_part_neg _ _ (not_le.1 hr), singular_part_smul] },
    rw [to_signed_measure_smul, to_signed_measure_smul, ← neg_sub, ← smul_sub],
    change -(((-r).to_nnreal : ℝ) • _) = _,
    rw [← neg_smul, real.coe_to_nnreal _ (le_of_lt (neg_pos.mpr (not_le.1 hr))), neg_neg] }
end

lemma singular_part_add (s t : signed_measure α) (μ : measure α)
  [s.have_lebesgue_decomposition μ] [t.have_lebesgue_decomposition μ] :
  (s + t).singular_part μ = s.singular_part μ + t.singular_part μ :=
begin
  refine (eq_singular_part _ (s.rn_deriv μ + t.rn_deriv μ)
    ((mutually_singular_singular_part s μ).add_left (mutually_singular_singular_part t μ)) _).symm,
  erw [with_densityᵥ_add (integrable_rn_deriv s μ) (integrable_rn_deriv t μ)],
  rw [add_assoc, add_comm (t.singular_part μ), add_assoc, add_comm _ (t.singular_part μ),
      singular_part_add_with_density_rn_deriv_eq, ← add_assoc,
      singular_part_add_with_density_rn_deriv_eq],
end

lemma singular_part_sub (s t : signed_measure α) (μ : measure α)
  [s.have_lebesgue_decomposition μ] [t.have_lebesgue_decomposition μ] :
  (s - t).singular_part μ = s.singular_part μ - t.singular_part μ :=
by { rw [sub_eq_add_neg, sub_eq_add_neg, singular_part_add, singular_part_neg] }

/-- Given a measure `μ`, signed measures `s` and `t`, and a function `f` such that `t` is
mutually singular with respect to `μ` and `s = t + μ.with_densityᵥ f`, we have
`f = rn_deriv s μ`, i.e. `f` is the Radon-Nikodym derivative of `s` and `μ`. -/
theorem eq_rn_deriv (t : signed_measure α) (f : α → ℝ) (hfi : integrable f μ)
  (htμ : t ⊥ᵥ μ.to_ennreal_vector_measure) (hadd : s = t + μ.with_densityᵥ f) :
  f =ᵐ[μ] s.rn_deriv μ :=
begin
  set f' := hfi.1.mk f,
  have hadd' : s = t + μ.with_densityᵥ f',
  { convert hadd using 2,
    exact with_densityᵥ_eq.congr_ae hfi.1.ae_eq_mk.symm },
  haveI := have_lebesgue_decomposition_mk μ hfi.1.measurable_mk htμ hadd',
  refine (integrable.ae_eq_of_with_densityᵥ_eq (integrable_rn_deriv _ _) hfi _).symm,
  rw [← add_right_inj t, ← hadd, eq_singular_part _ f htμ hadd,
      singular_part_add_with_density_rn_deriv_eq],
end

lemma rn_deriv_neg (s : signed_measure α) (μ : measure α) [s.have_lebesgue_decomposition μ] :
  (-s).rn_deriv μ =ᵐ[μ] - s.rn_deriv μ :=
begin
  refine integrable.ae_eq_of_with_densityᵥ_eq
    (integrable_rn_deriv _ _) (integrable_rn_deriv _ _).neg _,
  rw [with_densityᵥ_neg, ← add_right_inj ((-s).singular_part μ),
      singular_part_add_with_density_rn_deriv_eq, singular_part_neg, ← neg_add,
      singular_part_add_with_density_rn_deriv_eq]
end

lemma rn_deriv_smul (s : signed_measure α) (μ : measure α) [s.have_lebesgue_decomposition μ]
  (r : ℝ) :
  (r • s).rn_deriv μ =ᵐ[μ] r • s.rn_deriv μ :=
begin
  refine integrable.ae_eq_of_with_densityᵥ_eq
    (integrable_rn_deriv _ _) ((integrable_rn_deriv _ _).smul r) _,
  change _ = μ.with_densityᵥ ((r : ℝ) • s.rn_deriv μ),
  rw [with_densityᵥ_smul (rn_deriv s μ) (r : ℝ),
      ← add_right_inj ((r • s).singular_part μ),
      singular_part_add_with_density_rn_deriv_eq, singular_part_smul],
  change _ = _ + r • _,
  rw [← smul_add, singular_part_add_with_density_rn_deriv_eq],
end

lemma rn_deriv_add (s t : signed_measure α) (μ : measure α)
  [s.have_lebesgue_decomposition μ] [t.have_lebesgue_decomposition μ]
  [(s + t).have_lebesgue_decomposition μ] :
  (s + t).rn_deriv μ =ᵐ[μ] s.rn_deriv μ + t.rn_deriv μ :=
begin
  refine integrable.ae_eq_of_with_densityᵥ_eq
    (integrable_rn_deriv _ _)
    ((integrable_rn_deriv _ _).add (integrable_rn_deriv _ _)) _,
  rw [← add_right_inj ((s + t).singular_part μ),
      singular_part_add_with_density_rn_deriv_eq,
      with_densityᵥ_add (integrable_rn_deriv _ _) (integrable_rn_deriv _ _),
      singular_part_add, add_assoc, add_comm (t.singular_part μ), add_assoc,
      add_comm _ (t.singular_part μ), singular_part_add_with_density_rn_deriv_eq,
      ← add_assoc, singular_part_add_with_density_rn_deriv_eq],
end

lemma rn_deriv_sub (s t : signed_measure α) (μ : measure α)
  [s.have_lebesgue_decomposition μ] [t.have_lebesgue_decomposition μ]
  [hst : (s - t).have_lebesgue_decomposition μ] :
  (s - t).rn_deriv μ =ᵐ[μ] s.rn_deriv μ - t.rn_deriv μ :=
begin
  rw sub_eq_add_neg at hst,
  rw [sub_eq_add_neg, sub_eq_add_neg],
  exactI ae_eq_trans (rn_deriv_add _ _ _)
    (filter.eventually_eq.add (ae_eq_refl _) (rn_deriv_neg _ _)),
end

end signed_measure

namespace complex_measure

/-- A complex measure is said to `have_lebesgue_decomposition` with respect to a positive measure
if both its real and imaginary part `have_lebesgue_decomposition` with respect to that measure. -/
class have_lebesgue_decomposition (c : complex_measure α) (μ : measure α) : Prop :=
(re_part : c.re.have_lebesgue_decomposition μ)
(im_part : c.im.have_lebesgue_decomposition μ)

attribute [instance] have_lebesgue_decomposition.re_part
attribute [instance] have_lebesgue_decomposition.im_part

/-- The singular part between a complex measure `c` and a positive measure `μ` is the complex
measure satisfying `c.singular_part μ + μ.with_densityᵥ (c.rn_deriv μ) = c`. This property is given
by `measure_theory.complex_measure.singular_part_add_with_density_rn_deriv_eq`. -/
def singular_part (c : complex_measure α) (μ : measure α) : complex_measure α :=
(c.re.singular_part μ).to_complex_measure (c.im.singular_part μ)

/-- The Radon-Nikodym derivative between a complex measure and a positive measure. -/
def rn_deriv (c : complex_measure α) (μ : measure α) : α → ℂ :=
λ x, ⟨c.re.rn_deriv μ x, c.im.rn_deriv μ x⟩

variable {c : complex_measure α}

lemma integrable_rn_deriv (c : complex_measure α) (μ : measure α) :
  integrable (c.rn_deriv μ) μ :=
begin
  rw [← mem_ℒp_one_iff_integrable, ← mem_ℒp_re_im_iff],
  exact ⟨mem_ℒp_one_iff_integrable.2 (signed_measure.integrable_rn_deriv _ _),
         mem_ℒp_one_iff_integrable.2 (signed_measure.integrable_rn_deriv _ _)⟩
end

theorem singular_part_add_with_density_rn_deriv_eq [c.have_lebesgue_decomposition μ] :
  c.singular_part μ + μ.with_densityᵥ (c.rn_deriv μ) = c :=
begin
  conv_rhs { rw [← c.to_complex_measure_to_signed_measure] },
  ext i hi : 1,
  rw [vector_measure.add_apply, signed_measure.to_complex_measure_apply],
  ext,
  { rw [complex.add_re, with_densityᵥ_apply (c.integrable_rn_deriv μ) hi,
      ←is_R_or_C.re_eq_complex_re, ←integral_re (c.integrable_rn_deriv μ).integrable_on,
      is_R_or_C.re_eq_complex_re, ← with_densityᵥ_apply _ hi],
    { change (c.re.singular_part μ + μ.with_densityᵥ (c.re.rn_deriv μ)) i = _,
      rw c.re.singular_part_add_with_density_rn_deriv_eq μ },
    { exact (signed_measure.integrable_rn_deriv _ _) } },
  { rw [complex.add_im, with_densityᵥ_apply (c.integrable_rn_deriv μ) hi,
      ←is_R_or_C.im_eq_complex_im, ←integral_im (c.integrable_rn_deriv μ).integrable_on,
      is_R_or_C.im_eq_complex_im, ← with_densityᵥ_apply _ hi],
    { change (c.im.singular_part μ + μ.with_densityᵥ (c.im.rn_deriv μ)) i = _,
      rw c.im.singular_part_add_with_density_rn_deriv_eq μ },
    { exact (signed_measure.integrable_rn_deriv _ _) } },
end

end complex_measure

end measure_theory