This file is indexed.

/usr/share/hol-light/Multivariate/gamma.ml is in hol-light 20170706-0ubuntu4.

This file is owned by root:root, with mode 0o644.

The actual contents of the file can be viewed below.

   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
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141
3142
3143
3144
3145
3146
3147
3148
3149
3150
3151
3152
3153
3154
3155
3156
3157
3158
3159
3160
3161
3162
3163
3164
3165
3166
3167
3168
3169
3170
3171
3172
3173
3174
3175
3176
3177
3178
3179
3180
3181
3182
3183
3184
3185
3186
3187
3188
3189
3190
3191
3192
3193
3194
3195
3196
3197
3198
3199
3200
3201
3202
3203
3204
3205
3206
3207
3208
3209
3210
3211
3212
3213
3214
3215
3216
3217
3218
3219
3220
3221
3222
3223
3224
3225
3226
3227
3228
3229
3230
3231
3232
3233
3234
3235
3236
3237
3238
3239
3240
3241
3242
3243
3244
3245
3246
3247
3248
3249
3250
3251
3252
3253
3254
3255
3256
3257
3258
3259
3260
3261
3262
3263
3264
3265
3266
3267
3268
3269
3270
3271
3272
3273
3274
3275
3276
3277
3278
3279
3280
3281
3282
3283
3284
3285
3286
3287
3288
3289
3290
3291
3292
3293
3294
3295
3296
3297
3298
3299
3300
3301
3302
3303
3304
3305
3306
3307
3308
3309
3310
3311
3312
3313
3314
3315
3316
3317
3318
3319
3320
3321
3322
3323
3324
3325
3326
3327
3328
3329
3330
3331
3332
3333
3334
3335
3336
3337
3338
3339
3340
3341
3342
3343
3344
3345
3346
3347
3348
3349
3350
3351
3352
3353
3354
3355
3356
3357
3358
3359
3360
3361
3362
3363
3364
3365
3366
3367
3368
3369
3370
3371
3372
3373
3374
3375
3376
3377
3378
3379
3380
3381
3382
3383
3384
3385
3386
3387
3388
3389
3390
3391
3392
3393
3394
3395
3396
3397
3398
3399
3400
3401
3402
3403
3404
3405
3406
3407
3408
3409
3410
3411
3412
3413
3414
3415
3416
3417
3418
3419
3420
3421
3422
3423
3424
3425
3426
3427
3428
3429
3430
3431
3432
3433
3434
3435
3436
3437
3438
3439
3440
3441
3442
3443
3444
3445
3446
3447
3448
3449
3450
3451
3452
3453
3454
3455
3456
3457
3458
3459
3460
3461
3462
3463
3464
3465
3466
3467
3468
3469
3470
3471
3472
3473
3474
3475
3476
3477
3478
3479
3480
3481
3482
3483
3484
3485
3486
3487
3488
3489
3490
3491
3492
3493
3494
3495
3496
3497
3498
3499
3500
3501
3502
3503
3504
3505
3506
3507
3508
3509
3510
3511
3512
3513
3514
3515
3516
3517
3518
3519
3520
3521
3522
3523
3524
3525
3526
3527
3528
3529
3530
3531
3532
3533
3534
3535
3536
3537
3538
3539
3540
3541
3542
3543
3544
3545
3546
3547
3548
3549
3550
3551
3552
3553
3554
3555
3556
3557
3558
3559
3560
3561
3562
3563
3564
3565
3566
3567
3568
3569
3570
3571
3572
3573
3574
3575
3576
3577
3578
3579
3580
3581
3582
3583
3584
3585
3586
3587
3588
3589
3590
3591
3592
3593
3594
3595
3596
3597
3598
3599
3600
3601
3602
3603
3604
3605
3606
3607
3608
3609
3610
3611
3612
3613
3614
3615
3616
3617
3618
3619
3620
3621
3622
3623
3624
3625
3626
3627
3628
3629
3630
3631
3632
3633
3634
3635
3636
3637
3638
3639
3640
3641
3642
3643
3644
3645
3646
3647
3648
3649
3650
3651
3652
3653
3654
3655
3656
3657
3658
3659
3660
3661
3662
3663
3664
3665
3666
3667
3668
3669
3670
3671
3672
3673
3674
3675
3676
3677
3678
3679
3680
3681
3682
3683
3684
3685
3686
3687
3688
3689
3690
3691
3692
3693
3694
3695
3696
3697
3698
3699
3700
3701
3702
3703
3704
3705
3706
3707
3708
3709
3710
3711
3712
3713
3714
3715
3716
3717
3718
3719
3720
3721
3722
3723
3724
3725
3726
3727
3728
3729
3730
3731
3732
3733
3734
3735
3736
3737
3738
3739
3740
3741
3742
3743
3744
3745
3746
3747
3748
3749
3750
3751
3752
3753
3754
3755
3756
3757
3758
3759
3760
3761
3762
3763
3764
3765
3766
3767
3768
3769
3770
3771
3772
3773
3774
3775
3776
3777
3778
(* ========================================================================= *)
(* The real and complex gamma functions and Euler-Mascheroni constant.       *)
(* ========================================================================= *)

needs "Multivariate/cauchy.ml";;

(* ------------------------------------------------------------------------- *)
(* Euler-Mascheroni constant.                                                *)
(* ------------------------------------------------------------------------- *)

let euler_mascheroni = new_definition
  `euler_mascheroni =
   reallim sequentially (\n. sum(1..n) (\k. inv(&k)) - log(&n))`;;

let EULER_MASCHERONI = prove
 (`((\n. sum(1..n) (\k. inv(&k)) - log(&n)) ---> euler_mascheroni)
   sequentially`,
  REWRITE_TAC[euler_mascheroni; reallim] THEN CONV_TAC SELECT_CONV THEN
  SUBGOAL_THEN
   `real_summable (from 1) (\k. inv(&k) + (log(&k) - log(&(k + 1))))`
  MP_TAC THENL
   [MATCH_MP_TAC REAL_SUMMABLE_COMPARISON THEN
    EXISTS_TAC `\k. &2 / &k pow 2` THEN CONJ_TAC THENL
     [REWRITE_TAC[real_div] THEN MATCH_MP_TAC REAL_SUMMABLE_LMUL THEN
      MATCH_MP_TAC REAL_SUMMABLE_ZETA_INTEGER THEN REWRITE_TAC[LE_REFL];
      EXISTS_TAC `2` THEN REWRITE_TAC[GE; IN_FROM] THEN
      X_GEN_TAC `n:num` THEN STRIP_TAC THEN
      ONCE_REWRITE_TAC[REAL_ARITH `a + (b - c):real = a - (c - b)`] THEN
      ASM_SIMP_TAC[GSYM LOG_DIV; REAL_OF_NUM_LT; LE_1;
                   ARITH_RULE `0 < n + 1`] THEN
      ASM_SIMP_TAC[GSYM REAL_OF_NUM_ADD; REAL_OF_NUM_LT; LE_1; REAL_FIELD
       `&0 < n ==> (n + &1) / n = &1 + inv(n)`] THEN
      MP_TAC(ISPECL [`1`; `Cx(inv(&n))`] TAYLOR_CLOG)];
    REWRITE_TAC[real_summable; real_sums; FROM_INTER_NUMSEG] THEN
    MATCH_MP_TAC MONO_EXISTS THEN X_GEN_TAC `g:real` THEN
    MATCH_MP_TAC(REWRITE_RULE[IMP_CONJ] REALLIM_TRANSFORM) THEN
    REWRITE_TAC[SUM_ADD_NUMSEG; REAL_ARITH
     `(x + y) - (x - z):real = y + z`] THEN
    REWRITE_TAC[SUM_DIFFS; LOG_1; COND_RAND; COND_RATOR] THEN
    REWRITE_TAC[REAL_ARITH `&0 - x + y = --(x - y)`] THEN
    MATCH_MP_TAC REALLIM_NULL_COMPARISON THEN
    EXISTS_TAC `\n. &2 / &n` THEN CONJ_TAC THENL
     [REWRITE_TAC[EVENTUALLY_SEQUENTIALLY] THEN EXISTS_TAC `2` THEN
      X_GEN_TAC `n:num` THEN DISCH_TAC THEN
      COND_CASES_TAC THENL [ALL_TAC; ASM_ARITH_TAC] THEN
      ASM_SIMP_TAC[REAL_ABS_NEG; GSYM LOG_DIV; REAL_OF_NUM_LT; LE_1;
                   ARITH_RULE `0 < n + 1`] THEN
      ASM_SIMP_TAC[GSYM REAL_OF_NUM_ADD; REAL_OF_NUM_LT; LE_1; REAL_FIELD
       `&0 < n ==> (n + &1) / n = &1 + inv(n)`] THEN
      MP_TAC(ISPECL [`0`; `Cx(inv(&n))`] TAYLOR_CLOG);
      REWRITE_TAC[real_div] THEN MATCH_MP_TAC REALLIM_NULL_LMUL THEN
      REWRITE_TAC[REALLIM_1_OVER_N]]] THEN
  REWRITE_TAC[GSYM CX_ADD; VSUM_SING_NUMSEG; COMPLEX_NORM_CX] THEN
  REWRITE_TAC[VSUM_CLAUSES_NUMSEG] THEN CONV_TAC NUM_REDUCE_CONV THEN
  REWRITE_TAC[REAL_ABS_INV; REAL_ABS_NUM; COMPLEX_DIV_1] THEN
  ASM_SIMP_TAC[COMPLEX_POW_1; REAL_INV_LT_1; REAL_OF_NUM_LT;
               ARITH_RULE `1 < n <=> 2 <= n`] THEN
  REWRITE_TAC[COMPLEX_POW_2; COMPLEX_MUL_LNEG; COMPLEX_MUL_LID] THEN
  ASM_SIMP_TAC[COMPLEX_NEG_NEG; GSYM CX_LOG; REAL_LT_ADD; REAL_OF_NUM_LT;
               LE_1; ARITH; REAL_LT_INV_EQ; GSYM CX_SUB] THEN
  REWRITE_TAC[REAL_POW_1; VECTOR_SUB_RZERO] THEN
  REWRITE_TAC[COMPLEX_NORM_CX] THEN REWRITE_TAC[REAL_ABS_SUB] THEN
  MATCH_MP_TAC(REWRITE_RULE[IMP_CONJ_ALT] REAL_LE_TRANS) THEN
  REWRITE_TAC[real_div; REAL_POW_INV] THEN
  GEN_REWRITE_TAC RAND_CONV [REAL_MUL_SYM] THEN
  MATCH_MP_TAC REAL_LE_LMUL THEN
  SIMP_TAC[REAL_LE_INV_EQ; REAL_POW_LE; REAL_POS] THEN
  GEN_REWRITE_TAC RAND_CONV [GSYM REAL_INV_INV] THEN
  MATCH_MP_TAC REAL_LE_INV2 THEN
  CONV_TAC REAL_RAT_REDUCE_CONV THEN
  REWRITE_TAC[REAL_ARITH `&1 / &2 <= &1 - &1 / n <=> inv(n) <= inv(&2)`] THEN
  MATCH_MP_TAC REAL_LE_INV2 THEN
  REWRITE_TAC[REAL_OF_NUM_LE; REAL_OF_NUM_LT] THEN ASM_ARITH_TAC);;

(* ------------------------------------------------------------------------- *)
(* Simple-minded estimation of gamma using Euler-Maclaurin summation.        *)
(* ------------------------------------------------------------------------- *)

let LOG2_APPROX_40 = prove
 (`abs(log(&2) - &381061692393 / &549755813888) <= inv(&2 pow 40)`,
  MP_TAC(SPECL [`41`; `Cx(--inv(&2))`] TAYLOR_CLOG) THEN
  SIMP_TAC[GSYM CX_DIV; GSYM CX_POW; GSYM CX_NEG; GSYM CX_ADD; GSYM CX_MUL;
           VSUM_CX; COMPLEX_NORM_CX; GSYM CX_LOG; GSYM CX_SUB;
            REAL_ARITH `&0 < &1 + --inv(&2)`] THEN
  CONV_TAC NUM_REDUCE_CONV THEN CONV_TAC REAL_RAT_REDUCE_CONV THEN
  REWRITE_TAC[REAL_ARITH `a * b / c:real = a / c * b`] THEN
  CONV_TAC(ONCE_DEPTH_CONV EXPAND_SUM_CONV) THEN
  CONV_TAC NUM_REDUCE_CONV THEN CONV_TAC REAL_RAT_REDUCE_CONV THEN
  REWRITE_TAC[real_div; REAL_MUL_LID] THEN
  SIMP_TAC[LOG_INV; REAL_ARITH `&0 < &2`] THEN REAL_ARITH_TAC);;

let EULER_MASCHERONI_APPROX_32 = prove
 (`abs(euler_mascheroni - &2479122403 / &4294967296) <= inv(&2 pow 32)`,
  let lemma1 = prove
   (`!m n. 1 <= m /\ m <= n
           ==> abs((sum (1..n) (\k. inv(&k)) - log(&n)) -
                   ((sum (1..m - 1) (\k. inv(&k)) - log(&m)) +
                    (inv(&m) + inv(&n)) / &2 +
                    &1 / &12 * (inv(&m pow 2) - inv(&n pow 2)) +
                    -- &1 / &120 * (inv(&m pow 4) - inv(&n pow 4))))
                <= inv(&60 * &m pow 5)`,
    REPEAT STRIP_TAC THEN
    MP_TAC(SPECL [`\k. inv(&k)`; `1:num`; `m - 1`; `n:num`] SUM_COMBINE_R) THEN
    ANTS_TAC THENL [ASM_ARITH_TAC; DISCH_THEN(SUBST1_TAC o SYM)] THEN
    ASM_SIMP_TAC[ARITH_RULE `1 <= m ==> m - 1 + 1 = m`] THEN
    MP_TAC(ISPECL
     [`\n x. --(&1) pow n * &(FACT n) / x pow (n + 1)`;
      `m:num`; `n:num`; `2`] REAL_EULER_MACLAURIN) THEN
    ASM_REWRITE_TAC[] THEN CONV_TAC NUM_REDUCE_CONV THEN ANTS_TAC THENL
     [REWRITE_TAC[IN_REAL_INTERVAL] THEN REPEAT
      STRIP_TAC THEN REAL_DIFF_TAC THEN
      REWRITE_TAC[REAL_MUL_LZERO; ADD_SUB; REAL_MUL_RID; REAL_SUB_LZERO] THEN
      REWRITE_TAC[real_div; GSYM REAL_MUL_ASSOC; REAL_POW_EQ_0;
                  REAL_MUL_LNEG; REAL_MUL_RNEG] THEN
      RULE_ASSUM_TAC(REWRITE_RULE[GSYM REAL_OF_NUM_LE]) THEN
      ASM_CASES_TAC `x = &0` THENL [ASM_REAL_ARITH_TAC; ALL_TAC] THEN
      REWRITE_TAC[GSYM real_div; REAL_POW_POW] THEN
      ASM_SIMP_TAC[REAL_DIV_POW2_ALT; ARITH_RULE `~((k + 1) * 2 < k)`] THEN
      REWRITE_TAC[ARITH_RULE `(k + 1) * 2 - k = SUC(SUC k)`;
                  ARITH_RULE `(k + 1) + 1 = SUC(SUC k)`] THEN
      REWRITE_TAC[REAL_POW_ADD; REAL_POW_1] THEN
      REWRITE_TAC[REAL_MUL_RNEG; REAL_MUL_LNEG; REAL_MUL_RID] THEN
      AP_TERM_TAC THEN AP_TERM_TAC THEN REWRITE_TAC[real_div] THEN
      REWRITE_TAC[REAL_MUL_ASSOC] THEN AP_THM_TAC THEN AP_TERM_TAC THEN
      REWRITE_TAC[GSYM ADD1; FACT; REAL_OF_NUM_MUL; MULT_AC];
      DISCH_THEN(MP_TAC o CONJUNCT2) THEN
      REWRITE_TAC[real_div; REAL_MUL_LID; real_pow] THEN
      CONV_TAC(ONCE_DEPTH_CONV EXPAND_SUM_CONV) THEN
      CONV_TAC NUM_REDUCE_CONV THEN
      CONV_TAC(ONCE_DEPTH_CONV BERNOULLI_CONV) THEN
      REWRITE_TAC[GSYM(BERNOULLI_CONV `bernoulli 5 x`)] THEN
      CONV_TAC REAL_RAT_REDUCE_CONV THEN
      REWRITE_TAC[REAL_MUL_LNEG; REAL_MUL_LID; REAL_POW_1] THEN
      REWRITE_TAC[real_div; REAL_MUL_LID] THEN DISCH_THEN SUBST1_TAC] THEN
    MP_TAC(ISPECL [`\x. log x`; `\x:real. inv x`; `&m`; `&n`]
          REAL_FUNDAMENTAL_THEOREM_OF_CALCULUS) THEN
    ASM_REWRITE_TAC[REAL_OF_NUM_ADD; REAL_OF_NUM_LE] THEN
    ANTS_TAC THENL
     [REWRITE_TAC[IN_REAL_INTERVAL; GSYM REAL_OF_NUM_ADD] THEN
      REPEAT STRIP_TAC THEN REAL_DIFF_TAC THEN
      RULE_ASSUM_TAC(REWRITE_RULE[GSYM REAL_OF_NUM_LE]) THEN
      ASM_REAL_ARITH_TAC;
      DISCH_THEN(SUBST1_TAC o MATCH_MP REAL_INTEGRAL_UNIQUE)] THEN
    MATCH_MP_TAC REAL_LE_TRANS THEN
    EXISTS_TAC
     `&1 / &120 *
      abs(real_integral (real_interval[&m,&n])
                        (\x. bernoulli 5 (frac x) *
                             --(&120 * inv(x pow 6))))` THEN
    CONJ_TAC THENL [REAL_ARITH_TAC; ALL_TAC] THEN
    REWRITE_TAC[REAL_ARITH `b * --(n * inv x):real = --n * b / x`] THEN
    SUBGOAL_THEN
     `(\x. bernoulli 5 (frac x) / x pow 6)
      real_measurable_on real_interval[&m,&n]`
    ASSUME_TAC THENL
     [MATCH_MP_TAC
       REAL_CONTINUOUS_AE_IMP_MEASURABLE_ON_LEBESGUE_MEASURABLE_SUBSET THEN
      EXISTS_TAC `integer` THEN
      REWRITE_TAC[REAL_LEBESGUE_MEASURABLE_INTERVAL] THEN
      SIMP_TAC[REAL_NEGLIGIBLE_COUNTABLE; COUNTABLE_INTEGER] THEN
      REWRITE_TAC[REAL_CONTINUOUS_ON_EQ_CONTINUOUS_WITHIN] THEN
      REWRITE_TAC[IN_DIFF; IN_REAL_INTERVAL] THEN REWRITE_TAC[IN] THEN
      X_GEN_TAC `x:real` THEN STRIP_TAC THEN
      MATCH_MP_TAC REAL_CONTINUOUS_ATREAL_WITHINREAL THEN
      MATCH_MP_TAC REAL_DIFFERENTIABLE_IMP_CONTINUOUS_ATREAL THEN
      REAL_DIFFERENTIABLE_TAC THEN ASM_REWRITE_TAC[REAL_POW_EQ_0] THEN
      RULE_ASSUM_TAC(REWRITE_RULE[GSYM REAL_OF_NUM_LE]) THEN
      ASM_REAL_ARITH_TAC;
      ALL_TAC] THEN
    MP_TAC(ISPECL
     [`\x. inv(-- &60 * x pow 5)`; `\x. inv(&12 * x pow 6)`;
      `&m`; `&n`] REAL_FUNDAMENTAL_THEOREM_OF_CALCULUS) THEN
    ASM_REWRITE_TAC[REAL_OF_NUM_LE] THEN ANTS_TAC THENL
     [REWRITE_TAC[IN_REAL_INTERVAL] THEN REPEAT STRIP_TAC THEN
      REAL_DIFF_TAC THEN CONV_TAC NUM_REDUCE_CONV THEN
      SUBGOAL_THEN `~(x = &0)` MP_TAC THENL
       [RULE_ASSUM_TAC(REWRITE_RULE[GSYM REAL_OF_NUM_LE]) THEN
        ASM_REAL_ARITH_TAC;
        CONV_TAC REAL_FIELD];
      REWRITE_TAC[REAL_INV_MUL; REAL_ARITH
       `inv(-- &60) * x - inv(-- &60) * y = (y - x) / &60`] THEN
      REWRITE_TAC[GSYM REAL_INV_MUL] THEN DISCH_TAC] THEN
    SUBGOAL_THEN
     `!x. x IN real_interval[&m,&n]
          ==> abs(bernoulli 5 (frac x) / x pow 6) <= inv(&12 * x pow 6)`
    ASSUME_TAC THENL
     [REWRITE_TAC[IN_REAL_INTERVAL] THEN REPEAT STRIP_TAC THEN
      REWRITE_TAC[REAL_INV_MUL; real_div; REAL_ABS_MUL] THEN
      MATCH_MP_TAC REAL_LE_MUL2 THEN REWRITE_TAC[REAL_ABS_POS] THEN
      CONJ_TAC THENL
       [MP_TAC(ISPECL [`5`; `frac x`] BERNOULLI_BOUND) THEN
        SIMP_TAC[IN_REAL_INTERVAL; FLOOR_FRAC; REAL_LT_IMP_LE] THEN
        CONV_TAC NUM_REDUCE_CONV THEN
        REWRITE_TAC[BERNOULLI_CONV `bernoulli 4 (&0)`] THEN
        REAL_ARITH_TAC;
        REWRITE_TAC[REAL_ARITH `abs x <= x <=> &0 <= x`] THEN
        REWRITE_TAC[REAL_LE_INV_EQ] THEN MATCH_MP_TAC REAL_POW_LE THEN
        RULE_ASSUM_TAC(REWRITE_RULE[GSYM REAL_OF_NUM_LE]) THEN
        ASM_REAL_ARITH_TAC];
      ALL_TAC] THEN
    SUBGOAL_THEN
     `(\x. bernoulli 5 (frac x) / x pow 6)
      real_integrable_on real_interval[&m,&n]`
    ASSUME_TAC THENL
     [MATCH_MP_TAC REAL_MEASURABLE_BOUNDED_BY_INTEGRABLE_IMP_INTEGRABLE THEN
      EXISTS_TAC `\x:real. inv(&12 * x pow 6)` THEN
      ASM_REWRITE_TAC[REAL_INTEGRABLE_CONST] THEN
      ASM_MESON_TAC[real_integrable_on];
      ASM_SIMP_TAC[REAL_INTEGRAL_LMUL; REAL_ABS_MUL; REAL_MUL_ASSOC]] THEN
    CONV_TAC REAL_RAT_REDUCE_CONV THEN REWRITE_TAC[REAL_MUL_LID] THEN
    TRANS_TAC REAL_LE_TRANS
     `real_integral (real_interval [&m,&n]) (\x. inv(&12 * x pow 6))` THEN
    CONJ_TAC THENL
     [MATCH_MP_TAC REAL_INTEGRAL_ABS_BOUND_INTEGRAL THEN
      ASM_REWRITE_TAC[] THEN ASM_MESON_TAC[real_integrable_on];
      FIRST_ASSUM(SUBST1_TAC o MATCH_MP REAL_INTEGRAL_UNIQUE) THEN
      REWRITE_TAC[REAL_INV_MUL] THEN
      REWRITE_TAC[REAL_LE_INV_EQ; REAL_ARITH
       `(x - y) / &60 <= inv(&60) * x <=> &0 <= y`] THEN
      SIMP_TAC[REAL_POW_LE; REAL_POS]])
  and lemma2 = prove
   (`!f g l m d e k.
          (f ---> l) sequentially
          ==> (g ---> m) sequentially /\
              eventually (\n. abs(f n - g n) <= d) sequentially /\
              abs(m - k) <= e - d
              ==> abs(l - k) <= e`,
    REPEAT GEN_TAC THEN DISCH_TAC THEN
    REPEAT(DISCH_THEN(CONJUNCTS_THEN2 ASSUME_TAC MP_TAC)) THEN
    MATCH_MP_TAC(REAL_ARITH
     `abs(l - m) <= d ==> abs (m - k) <= e - d ==> abs (l - k) <= e`) THEN
    REWRITE_TAC[REAL_ABS_BOUNDS] THEN CONJ_TAC THENL
     [MATCH_MP_TAC(ISPEC `sequentially` REALLIM_LBOUND);
      MATCH_MP_TAC(ISPEC `sequentially` REALLIM_UBOUND)] THEN
    EXISTS_TAC `(\n. f n - g n):num->real` THEN
    ASM_SIMP_TAC[REALLIM_SUB; TRIVIAL_LIMIT_SEQUENTIALLY] THEN
    FIRST_X_ASSUM(MATCH_MP_TAC o MATCH_MP (ONCE_REWRITE_RULE[IMP_CONJ_ALT]
       EVENTUALLY_MONO)) THEN
    REAL_ARITH_TAC) in
  MATCH_MP_TAC(MATCH_MP lemma2 EULER_MASCHERONI) THEN
  MATCH_MP_TAC(MESON[]
   `(?a b c m:num. P (a m) (b m) (c m)) ==> ?a b c. P a b c`) THEN
  EXISTS_TAC
    `\m n. (sum(1..m - 1) (\k. inv(&k)) - log(&m)) +
           (inv(&m) + inv(&n)) / &2 +
           &1 / &12 * (inv(&m pow 2) - inv(&n pow 2)) +
           -- &1 / &120 * (inv(&m pow 4) - inv(&n pow 4))` THEN
  EXISTS_TAC
   `\m. (sum(1..m - 1) (\k. inv(&k)) - log(&m)) +
         inv(&m) / &2 +
         &1 / &12 * inv(&m pow 2) +
         -- &1 / &120 * inv(&m pow 4)` THEN
  EXISTS_TAC `\m. inv (&60 * &m pow 5)` THEN REWRITE_TAC[GSYM CONJ_ASSOC] THEN
  MATCH_MP_TAC(MESON[] `(!n. P n) /\ (?n. Q n) ==> (?n. P n /\ Q n)`) THEN
  REWRITE_TAC[] THEN REPEAT CONJ_TAC THENL
   [GEN_TAC THEN
    REPEAT(MATCH_MP_TAC REALLIM_ADD THEN CONJ_TAC) THEN
    REWRITE_TAC[REALLIM_CONST] THEN
    REWRITE_TAC[REAL_ARITH `x / &2 = inv(&2) * x`] THEN
    MATCH_MP_TAC REALLIM_LMUL THEN REWRITE_TAC[real_sub] THEN
    GEN_REWRITE_TAC LAND_CONV [GSYM REAL_ADD_RID] THEN
    MATCH_MP_TAC REALLIM_ADD THEN REWRITE_TAC[REALLIM_CONST] THEN
    REWRITE_TAC[REALLIM_NULL_NEG] THEN
    REWRITE_TAC[REALLIM_1_OVER_N] THEN
    MATCH_MP_TAC REALLIM_1_OVER_POW THEN CONV_TAC NUM_REDUCE_CONV;
    ALL_TAC] THEN
  REWRITE_TAC[EVENTUALLY_SEQUENTIALLY] THEN
  MATCH_MP_TAC(MESON[] `(?a. P a a /\ Q a) ==> ?a. (?b. P a b) /\ Q a`) THEN
  EXISTS_TAC `64` THEN CONJ_TAC THENL
   [REPEAT STRIP_TAC THEN MATCH_MP_TAC lemma1 THEN ASM_ARITH_TAC; ALL_TAC] THEN
  SUBGOAL_THEN `log(&64) = &6 * log(&2)` SUBST1_TAC THENL
   [SIMP_TAC[GSYM LOG_POW; REAL_ARITH `&0 < &2`] THEN
    AP_TERM_TAC THEN CONV_TAC REAL_RAT_REDUCE_CONV;
    CONV_TAC NUM_REDUCE_CONV THEN CONV_TAC REAL_RAT_REDUCE_CONV THEN
    CONV_TAC(ONCE_DEPTH_CONV EXPAND_SUM_CONV) THEN
    CONV_TAC REAL_RAT_REDUCE_CONV THEN
    MP_TAC LOG2_APPROX_40 THEN REAL_ARITH_TAC]);;

(* ------------------------------------------------------------------------- *)
(* Start with the log-gamma function. It's otherwise quite tedious to repeat *)
(* essentially the same argument when we want the logarithm of the gamma     *)
(* function, since we can't just take the usual principal value of log.      *)
(* ------------------------------------------------------------------------- *)

let lgamma = new_definition
 `lgamma z = lim sequentially
               (\n. z * clog(Cx(&n)) - clog z -
                    vsum(1..n) (\m. clog((Cx(&m) + z) / Cx(&m))))`;;

let LGAMMA,COMPLEX_DIFFERENTIABLE_AT_LGAMMA = (CONJ_PAIR o prove)
 (`(!z. (!n. ~(z + Cx(&n) = Cx(&0)))
        ==> ((\n. z * clog(Cx(&n)) - clog z -
                  vsum(1..n) (\m. clog((Cx(&m) + z) / Cx(&m))))
             --> lgamma(z)) sequentially) /\
   (!z. (Im z = &0 ==> &0 < Re z) ==> lgamma complex_differentiable at z)`,
  SUBGOAL_THEN `open {z | !n. ~(z + Cx(&n) = Cx(&0))}` ASSUME_TAC THENL
   [REWRITE_TAC[SET_RULE `{z | !n. P n z} = UNIV DIFF {z | ?n. ~P n z}`] THEN
    REWRITE_TAC[GSYM closed] THEN MATCH_MP_TAC DISCRETE_IMP_CLOSED THEN
    EXISTS_TAC `&1` THEN REWRITE_TAC[REAL_LT_01; IMP_CONJ] THEN
    REWRITE_TAC[IN_ELIM_THM; LEFT_IMP_EXISTS_THM] THEN
    SIMP_TAC[COMPLEX_RING `x + y = Cx(&0) <=> x = --y`] THEN
    REWRITE_TAC[COMPLEX_RING `--x - --y:complex = y - x`] THEN
    REWRITE_TAC[COMPLEX_EQ_NEG2; CX_INJ; GSYM CX_SUB; COMPLEX_NORM_CX] THEN
    SIMP_TAC[GSYM REAL_EQ_INTEGERS; INTEGER_CLOSED];
    ALL_TAC] THEN
  SUBGOAL_THEN
   `!y.  (!n. ~(y + Cx(&n) = Cx(&0)))
         ==> ?d l. &0 < d /\
                   !e. &0 < e
                       ==> ?N. !n z. N <= n /\ z IN cball(y,d)
                                   ==> dist(z * clog(Cx(&n)) -
                                            vsum(1..n)
                                             (\m. clog((Cx(&m) + z) / Cx(&m))),
                                            l z) < e`
  MP_TAC THENL
   [REPEAT STRIP_TAC THEN
    FIRST_X_ASSUM(MP_TAC o GEN_REWRITE_RULE I [OPEN_CONTAINS_BALL]) THEN
    REWRITE_TAC[OPEN_CONTAINS_CBALL] THEN
    DISCH_THEN(MP_TAC o SPEC `y:complex`) THEN
    ASM_REWRITE_TAC[IN_ELIM_THM] THEN
    MATCH_MP_TAC MONO_EXISTS THEN X_GEN_TAC `d:real` THEN
    STRIP_TAC THEN ASM_REWRITE_TAC[] THEN
    REWRITE_TAC[UNIFORMLY_CONVERGENT_EQ_CAUCHY_ALT] THEN
    X_GEN_TAC `e:real` THEN DISCH_TAC THEN
    SUBGOAL_THEN
     `summable (from 1)
         (\m. Cx(&2 * ((norm(y:complex) + d) +
                      (norm(y) + d) pow 2)) / Cx(&m) pow 2)`
    MP_TAC THENL
     [REWRITE_TAC[complex_div; COMPLEX_MUL_ASSOC] THEN
      MATCH_MP_TAC SUMMABLE_COMPLEX_LMUL THEN
      MATCH_MP_TAC SUMMABLE_ZETA_INTEGER THEN REWRITE_TAC[LE_REFL];
      ALL_TAC] THEN
    REWRITE_TAC[summable; SERIES_CAUCHY; GE] THEN
    DISCH_THEN(MP_TAC o SPEC `e:real`) THEN ASM_REWRITE_TAC[] THEN
    DISCH_THEN(X_CHOOSE_THEN `M:num` (LABEL_TAC "M")) THEN
    MP_TAC(SPEC `&2 * (norm(y:complex) + d) + &1` REAL_ARCH_SIMPLE) THEN
    DISCH_THEN(X_CHOOSE_THEN `N:num` (LABEL_TAC "N")) THEN
    EXISTS_TAC `MAX (MAX 1 2) (MAX M N)` THEN
    MAP_EVERY X_GEN_TAC [`m:num`; `n:num`; `z:complex`] THEN
    REWRITE_TAC[GE; ARITH_RULE `MAX a b <= c <=> a <= c /\ b <= c`] THEN
    STRIP_TAC THEN FIRST_X_ASSUM(MP_TAC o SPECL [`m + 1`; `n:num`]) THEN
    ANTS_TAC THENL [ASM_ARITH_TAC; ALL_TAC] THEN
    REWRITE_TAC[FROM_INTER_NUMSEG_MAX; ARITH_RULE `MAX 1 (m + 1) = m + 1`] THEN
    REWRITE_TAC[dist] THEN
    SUBGOAL_THEN
     `!n. 1 <= n
          ==> z * clog(Cx(&n)) - vsum(1..n) (\m. clog((Cx(&m) + z) / Cx(&m))) =
              vsum(1..n) (\m. z * (clog(Cx(&(m + 1) - &1)) -
                                   clog(Cx(&m - &1))) -
                              clog((Cx(&m) + z) / Cx(&m))) +
              z * clog(Cx(&0))`
     (fun th -> ASM_SIMP_TAC[th])
    THENL
     [REWRITE_TAC[VSUM_SUB_NUMSEG] THEN
      ASM_SIMP_TAC[VSUM_COMPLEX_LMUL; FINITE_NUMSEG; VSUM_DIFFS_ALT] THEN
      REWRITE_TAC[GSYM REAL_OF_NUM_ADD; REAL_ARITH `(x + &1) - &1 = x`;
                  REAL_SUB_REFL] THEN
      REPEAT STRIP_TAC THEN CONV_TAC COMPLEX_RING;
      ALL_TAC] THEN
    MATCH_MP_TAC(REWRITE_RULE[IMP_CONJ] REAL_LET_TRANS) THEN
    SUBGOAL_THEN `1 <= m + 1 /\ m <= n` MP_TAC THENL
     [ASM_ARITH_TAC;
      DISCH_THEN(fun th -> REWRITE_TAC[GSYM(MATCH_MP VSUM_COMBINE_R th)])] THEN
    REWRITE_TAC[COMPLEX_RING `(x + a) - ((x + y) + a):complex = --y`] THEN
    REWRITE_TAC[NORM_NEG] THEN MATCH_MP_TAC COMPLEX_NORM_VSUM_BOUND THEN
    REWRITE_TAC[FINITE_NUMSEG; IN_NUMSEG] THEN X_GEN_TAC `k:num` THEN
    STRIP_TAC THEN REWRITE_TAC[GSYM CX_POW; GSYM CX_DIV; REAL_CX; RE_CX] THEN
    REWRITE_TAC[COMPLEX_RING `z * (a - b) - c:complex = --(z * (b - a) + c)`;
      NORM_NEG; GSYM REAL_OF_NUM_ADD; REAL_ARITH `(x + &1) - &1 = x`] THEN
    SUBGOAL_THEN `1 <= k /\ 1 < k /\ 2 <= k` STRIP_ASSUME_TAC THENL
     [ASM_ARITH_TAC; ALL_TAC] THEN
    SUBGOAL_THEN
     `clog(Cx(&k - &1)) - clog(Cx(&k)) = clog(Cx(&1) - inv(Cx(&k)))`
    SUBST1_TAC THENL
     [ASM_SIMP_TAC[GSYM CX_LOG; REAL_OF_NUM_LE; REAL_SUB_LT; REAL_INV_LT_1;
                   REAL_ARITH `&2 <= x ==> &0 < x /\ &1 < x /\ &0 < x - &1`;
                   GSYM CX_SUB; GSYM CX_INV; GSYM LOG_DIV] THEN
      AP_TERM_TAC THEN AP_TERM_TAC THEN UNDISCH_TAC `2 <= k` THEN
      REWRITE_TAC[GSYM REAL_OF_NUM_LE] THEN CONV_TAC REAL_FIELD;
      ALL_TAC] THEN
    MP_TAC(ISPECL [`1`; `z / Cx(&k)`] TAYLOR_CLOG) THEN
    MP_TAC(ISPECL [`1`; `--inv(Cx(&k))`] TAYLOR_CLOG) THEN
    REWRITE_TAC[VSUM_SING_NUMSEG] THEN CONV_TAC NUM_REDUCE_CONV THEN
    REWRITE_TAC[GSYM VECTOR_SUB; NORM_NEG] THEN
    REWRITE_TAC[COMPLEX_NORM_INV; COMPLEX_NORM_DIV; COMPLEX_NORM_CX] THEN
    REWRITE_TAC[COMPLEX_POW_NEG; ARITH; REAL_ABS_NUM; COMPLEX_POW_ONE] THEN
    ASM_SIMP_TAC[REAL_INV_LT_1; REAL_OF_NUM_LT; COMPLEX_DIV_1] THEN
    ASM_SIMP_TAC[REAL_LT_LDIV_EQ; REAL_OF_NUM_LT; LE_1; COMPLEX_POW_1] THEN
    REWRITE_TAC[REAL_MUL_LID; COMPLEX_MUL_LID] THEN
    DISCH_THEN(MP_TAC o SPEC `norm(z:complex)` o MATCH_MP
      (REWRITE_RULE[IMP_CONJ_ALT] REAL_LE_LMUL)) THEN
    REWRITE_TAC[NORM_POS_LE; GSYM COMPLEX_NORM_MUL] THEN
    SUBGOAL_THEN `norm(z:complex) < &k` ASSUME_TAC THENL
     [FIRST_X_ASSUM(MP_TAC o GEN_REWRITE_RULE I [IN_CBALL]) THEN
      FIRST_X_ASSUM(K ALL_TAC o check (is_forall o concl)) THEN
      REPEAT(POP_ASSUM MP_TAC) THEN
      REWRITE_TAC[GSYM REAL_OF_NUM_LE; GSYM REAL_OF_NUM_ADD] THEN
      CONV_TAC NORM_ARITH;
      ASM_REWRITE_TAC[COMPLEX_SUB_LDISTRIB]] THEN
    ASM_SIMP_TAC[CX_INJ; REAL_OF_NUM_EQ; LE_1; COMPLEX_FIELD
      `~(k = Cx(&0)) ==> (k + z) / k = Cx(&1) + z / k`] THEN
    MATCH_MP_TAC(NORM_ARITH
     `x' = --y' /\ d + e <= f
      ==> norm(x - x') <= d ==> norm(y - y') <= e
          ==> norm(x + y) <= f`) THEN
    REWRITE_TAC[complex_div; COMPLEX_MUL_LNEG; COMPLEX_MUL_RNEG] THEN
    REWRITE_TAC[REAL_POW_DIV; REAL_POW_INV; REAL_ARITH
     `n * inv k / d + n pow 2 / k / e <= (&2 * (x + x pow 2)) / k <=>
      (n * (&1 / d + n / e)) / k <= (x * (&2 + &2 * x)) / k`] THEN
    ASM_SIMP_TAC[REAL_LE_DIV2_EQ; REAL_OF_NUM_LT; LE_1; REAL_POW_LT] THEN
    MATCH_MP_TAC REAL_LE_MUL2 THEN REWRITE_TAC[NORM_POS_LE] THEN
    SUBGOAL_THEN `norm(z:complex) <= norm(y:complex) + d` ASSUME_TAC THENL
     [FIRST_X_ASSUM(MP_TAC o GEN_REWRITE_RULE I [IN_CBALL]) THEN
      CONV_TAC NORM_ARITH;
      ALL_TAC] THEN
    REPEAT CONJ_TAC THENL
     [FIRST_X_ASSUM(MP_TAC o GEN_REWRITE_RULE I [IN_CBALL]) THEN
      CONV_TAC NORM_ARITH;
      MATCH_MP_TAC REAL_LE_ADD THEN CONJ_TAC THEN
      MATCH_MP_TAC REAL_LE_DIV THEN
      REWRITE_TAC[REAL_SUB_LE; NORM_POS_LE; REAL_POS] THEN
      REWRITE_TAC[REAL_ARITH `inv x = &1 / x`] THEN
      ASM_SIMP_TAC[REAL_LE_LDIV_EQ; REAL_OF_NUM_LT; LE_1];
      REWRITE_TAC[REAL_ARITH `&2 + &2 * x = &1 * &2 + x * &2`] THEN
      ONCE_REWRITE_TAC[real_div] THEN MATCH_MP_TAC REAL_LE_ADD2 THEN
      CONJ_TAC THEN MATCH_MP_TAC REAL_LE_MUL2 THEN
      ASM_REWRITE_TAC[NORM_POS_LE; REAL_POS; REAL_LE_REFL; REAL_LE_INV_EQ] THEN
      REWRITE_TAC[REAL_SUB_LE] THEN
      REWRITE_TAC[REAL_ARITH `inv x = &1 / x`] THEN
      ASM_SIMP_TAC[REAL_LE_LDIV_EQ; REAL_OF_NUM_LT; LE_1] THEN
      ASM_REWRITE_TAC[REAL_MUL_LID; REAL_OF_NUM_LE] THEN
      TRY(CONJ_TAC THENL
      [RULE_ASSUM_TAC(REWRITE_RULE
          [GSYM REAL_OF_NUM_LT; GSYM REAL_OF_NUM_ADD]) THEN
       ASM_REAL_ARITH_TAC; ALL_TAC]) THEN
      REWRITE_TAC[real_div; REAL_MUL_LID] THEN
      GEN_REWRITE_TAC RAND_CONV [GSYM REAL_INV_INV] THEN
      MATCH_MP_TAC REAL_LE_INV2 THEN CONV_TAC REAL_RAT_REDUCE_CONV THEN
      REWRITE_TAC[REAL_ARITH `&1 / &2 <= &1 - x <=> x <= &1 / &2`] THEN
      REWRITE_TAC[real_div; REAL_MUL_LID] THEN REWRITE_TAC[GSYM real_div] THEN
      REWRITE_TAC[REAL_ARITH `inv x = &1 / x`] THEN
      ASM_SIMP_TAC[REAL_LE_LDIV_EQ; REAL_OF_NUM_LT; LE_1]] THEN
    FIRST_X_ASSUM(MP_TAC o GEN_REWRITE_RULE I [IN_CBALL]) THEN
    FIRST_X_ASSUM(K ALL_TAC o check (is_forall o concl)) THEN
    REPEAT(POP_ASSUM MP_TAC) THEN
    REWRITE_TAC[GSYM REAL_OF_NUM_LE; GSYM REAL_OF_NUM_ADD] THEN
    CONV_TAC NORM_ARITH;
    GEN_REWRITE_TAC (LAND_CONV o REDEPTH_CONV) [RIGHT_IMP_EXISTS_THM] THEN
    REWRITE_TAC[SKOLEM_THM; LEFT_IMP_EXISTS_THM] THEN MAP_EVERY X_GEN_TAC
     [`dd:complex->real`; `ll:complex->complex->complex`] THEN
    DISCH_THEN(LABEL_TAC "*")] THEN
  SUBGOAL_THEN
   `!z. (!n. ~(z + Cx(&n) = Cx(&0)))
        ==> ((\n. z * clog(Cx(&n)) -
                  vsum (1..n) (\m. clog((Cx(&m) + z) / Cx(&m)))) --> ll z z)
            sequentially`
  ASSUME_TAC THENL
   [X_GEN_TAC `z:complex` THEN DISCH_TAC THEN
    FIRST_X_ASSUM(MP_TAC o SPEC `z:complex`) THEN
    ASM_REWRITE_TAC[LIM_SEQUENTIALLY; GSYM SKOLEM_THM] THEN
    MESON_TAC[CENTRE_IN_CBALL; REAL_LT_IMP_LE];
    ALL_TAC] THEN
  MATCH_MP_TAC(TAUT `p /\ (p ==> q) ==> p /\ q`) THEN CONJ_TAC THENL
   [REPEAT STRIP_TAC THEN
    REWRITE_TAC[lgamma; lim] THEN CONV_TAC SELECT_CONV THEN
    EXISTS_TAC `(ll:complex->complex->complex) z z - clog z` THEN
    ONCE_REWRITE_TAC[COMPLEX_RING `w - z - v:complex = (w - v) - z`] THEN
    MATCH_MP_TAC LIM_SUB THEN REWRITE_TAC[LIM_CONST] THEN ASM_MESON_TAC[];
    DISCH_TAC] THEN
  X_GEN_TAC `z:complex` THEN DISCH_TAC THEN
  SUBGOAL_THEN `!n. ~(z + Cx(&n) = Cx(&0))` ASSUME_TAC THENL
   [GEN_TAC THEN
    REWRITE_TAC[COMPLEX_RING `z + x = Cx(&0) <=> z = --x`] THEN
    DISCH_THEN SUBST_ALL_TAC THEN
    FIRST_X_ASSUM(MP_TAC o check (is_imp o concl)) THEN
    REWRITE_TAC[IM_NEG; RE_NEG; IM_CX; RE_CX] THEN REAL_ARITH_TAC;
    ALL_TAC] THEN
  MATCH_MP_TAC COMPLEX_DIFFERENTIABLE_TRANSFORM_AT THEN
  EXISTS_TAC `\z. (ll:complex->complex->complex) z z - clog z` THEN
  FIRST_X_ASSUM(MP_TAC o GEN_REWRITE_RULE I [OPEN_CONTAINS_BALL]) THEN
  REWRITE_TAC[OPEN_CONTAINS_BALL] THEN
  DISCH_THEN(MP_TAC o SPEC `z:complex`) THEN
  ASM_REWRITE_TAC[IN_ELIM_THM; SUBSET; IN_BALL] THEN
  MATCH_MP_TAC MONO_EXISTS THEN X_GEN_TAC `e:real` THEN STRIP_TAC THEN
  ASM_REWRITE_TAC[] THEN CONJ_TAC THENL
   [X_GEN_TAC `w:complex` THEN ONCE_REWRITE_TAC[DIST_SYM] THEN DISCH_TAC THEN
    MATCH_MP_TAC(ISPEC `sequentially` LIM_UNIQUE) THEN
    REWRITE_TAC[TRIVIAL_LIMIT_SEQUENTIALLY] THEN
    EXISTS_TAC `\n. w * clog(Cx(&n)) - clog w -
                    vsum(1..n) (\m. clog((Cx(&m) + w) / Cx(&m)))` THEN
    ASM_SIMP_TAC[] THEN
    ONCE_REWRITE_TAC[COMPLEX_RING `w - z - v:complex = (w - v) - z`] THEN
    MATCH_MP_TAC LIM_SUB THEN REWRITE_TAC[LIM_CONST] THEN ASM_MESON_TAC[];
    ALL_TAC] THEN
  MATCH_MP_TAC COMPLEX_DIFFERENTIABLE_SUB THEN
  ASM_SIMP_TAC[COMPLEX_DIFFERENTIABLE_AT_CLOG] THEN
  MATCH_MP_TAC COMPLEX_DIFFERENTIABLE_TRANSFORM_AT THEN
  EXISTS_TAC `(ll:complex->complex->complex) z` THEN
  EXISTS_TAC `min e (dd(z:complex))` THEN
  ASM_SIMP_TAC[REAL_LT_MIN] THEN CONJ_TAC THENL
   [X_GEN_TAC `w:complex` THEN ONCE_REWRITE_TAC[DIST_SYM] THEN
    STRIP_TAC THEN
    MATCH_MP_TAC(ISPEC `sequentially` LIM_UNIQUE) THEN
    EXISTS_TAC `\n. w * clog(Cx(&n)) -
                    vsum(1..n) (\m. clog((Cx(&m) + w) / Cx(&m)))` THEN
    REWRITE_TAC[TRIVIAL_LIMIT_SEQUENTIALLY; LIM_SEQUENTIALLY] THEN
    CONJ_TAC THEN X_GEN_TAC `r:real` THEN STRIP_TAC THENL
     [REMOVE_THEN "*" (MP_TAC o SPEC `z:complex`);
      REMOVE_THEN "*" (MP_TAC o SPEC `w:complex`)] THEN
    ASM_SIMP_TAC[] THEN
    REWRITE_TAC[GSYM SKOLEM_THM; RIGHT_EXISTS_IMP_THM] THEN
    DISCH_THEN(CONJUNCTS_THEN2 ASSUME_TAC (MP_TAC o SPEC `r:real`)) THEN
    ASM_REWRITE_TAC[] THEN MATCH_MP_TAC MONO_EXISTS THEN
    REPEAT STRIP_TAC THEN FIRST_X_ASSUM MATCH_MP_TAC THEN
    ASM_SIMP_TAC[CENTRE_IN_CBALL; REAL_LT_IMP_LE] THEN
    ASM_SIMP_TAC[IN_CBALL; REAL_LT_IMP_LE];
    ALL_TAC] THEN
  SUBGOAL_THEN `open {z | Im z = &0 ==> &0 < Re z}` MP_TAC THENL
   [SUBGOAL_THEN
     `{z | Im z = &0 ==> &0 < Re z} =
      (:complex) DIFF ({z | Im z = &0} INTER {z | Re z <= &0})`
     (fun th -> SIMP_TAC[th; CLOSED_HALFSPACE_RE_LE; GSYM closed;
                      CLOSED_HALFSPACE_IM_EQ; CLOSED_INTER]) THEN
    REWRITE_TAC[IN_ELIM_THM; IN_UNIV; IN_DIFF; IN_INTER; EXTENSION] THEN
    REAL_ARITH_TAC;
    REWRITE_TAC[OPEN_CONTAINS_CBALL; IN_ELIM_THM; SUBSET] THEN
    DISCH_THEN(MP_TAC o SPEC `z:complex`) THEN ASM_REWRITE_TAC[] THEN
    DISCH_THEN(X_CHOOSE_THEN `r:real` STRIP_ASSUME_TAC)] THEN
  SUBGOAL_THEN
   `(ll:complex->complex->complex) z continuous_on cball(z,min r (dd z)) /\
    ll z holomorphic_on ball(z,min r (dd z))`
  MP_TAC THENL
   [MATCH_MP_TAC(ISPEC `sequentially` HOLOMORPHIC_UNIFORM_LIMIT) THEN
    EXISTS_TAC `\n z. z * clog(Cx(&n)) -
                      vsum(1..n) (\m. clog((Cx(&m) + z) / Cx(&m)))` THEN
    ASM_REWRITE_TAC[TRIVIAL_LIMIT_SEQUENTIALLY; EVENTUALLY_SEQUENTIALLY] THEN
    REWRITE_TAC[CBALL_MIN_INTER; IN_INTER] THEN
    CONJ_TAC THENL [ALL_TAC; SIMP_TAC[GSYM dist] THEN ASM_MESON_TAC[]] THEN
    EXISTS_TAC `1` THEN X_GEN_TAC `n:num` THEN DISCH_TAC THEN
    MATCH_MP_TAC(MESON[HOLOMORPHIC_ON_IMP_CONTINUOUS_ON;
                       HOLOMORPHIC_ON_SUBSET]
     `t SUBSET s /\ f holomorphic_on s
      ==> f continuous_on s /\ f holomorphic_on t`) THEN
    REWRITE_TAC[BALL_SUBSET_CBALL; GSYM CBALL_MIN_INTER] THEN
    MATCH_MP_TAC HOLOMORPHIC_ON_SUB THEN
    SIMP_TAC[HOLOMORPHIC_ON_MUL; HOLOMORPHIC_ON_ID;
             HOLOMORPHIC_ON_CONST] THEN
    MATCH_MP_TAC HOLOMORPHIC_ON_VSUM THEN
    REWRITE_TAC[FINITE_NUMSEG; IN_NUMSEG] THEN
    X_GEN_TAC `m:num` THEN STRIP_TAC THEN
    GEN_REWRITE_TAC LAND_CONV [GSYM o_DEF] THEN
    MATCH_MP_TAC HOLOMORPHIC_ON_COMPOSE THEN
    SIMP_TAC[HOLOMORPHIC_ON_ADD; HOLOMORPHIC_ON_ID;
             HOLOMORPHIC_ON_CONST; complex_div; HOLOMORPHIC_ON_MUL] THEN
    MATCH_MP_TAC HOLOMORPHIC_ON_CLOG THEN
    REWRITE_TAC[FORALL_IN_IMAGE; GSYM complex_div; IMP_CONJ] THEN
    ASM_SIMP_TAC[RE_DIV_CX; IM_DIV_CX; REAL_DIV_EQ_0; RE_ADD; IM_ADD] THEN
    X_GEN_TAC `w:complex` THEN DISCH_TAC THEN
    ASM_SIMP_TAC[REAL_LT_IMP_NZ; REAL_OF_NUM_LT; LE_1] THEN
    REWRITE_TAC[IM_CX; RE_CX; REAL_ADD_LID] THEN DISCH_TAC THEN
    MATCH_MP_TAC REAL_LT_DIV THEN
    ASM_SIMP_TAC[REAL_LT_IMP_NZ; REAL_OF_NUM_LT; LE_1] THEN
    MATCH_MP_TAC(REAL_ARITH `&0 < x ==> &0 < &m + x`) THEN
    RULE_ASSUM_TAC(REWRITE_RULE[CBALL_MIN_INTER; IN_INTER]) THEN
    ASM_MESON_TAC[];
    SIMP_TAC[HOLOMORPHIC_ON_OPEN; OPEN_BALL; complex_differentiable] THEN
    DISCH_THEN(MATCH_MP_TAC o CONJUNCT2) THEN
    ASM_SIMP_TAC[CENTRE_IN_BALL; REAL_LT_MIN]]);;

let LGAMMA_ALT = prove
 (`!z. (!n. ~(z + Cx(&n) = Cx(&0)))
        ==> ((\n. (z * clog(Cx(&n)) + clog(Cx(&(FACT n)))) -
                  vsum(0..n) (\m. clog(Cx(&m) + z)))
             --> lgamma(z)) sequentially`,
  REPEAT STRIP_TAC THEN FIRST_ASSUM(MP_TAC o MATCH_MP LGAMMA) THEN
  MATCH_MP_TAC(REWRITE_RULE[IMP_CONJ] LIM_TRANSFORM) THEN
  MATCH_MP_TAC LIM_EVENTUALLY THEN REWRITE_TAC[EVENTUALLY_SEQUENTIALLY] THEN
  EXISTS_TAC `1` THEN X_GEN_TAC `n:num` THEN DISCH_TAC THEN
  SIMP_TAC[VECTOR_SUB_EQ; VSUM_CLAUSES_LEFT; LE_0; COMPLEX_ADD_LID] THEN
  MATCH_MP_TAC(COMPLEX_RING
   `a:complex = d - c ==> x - y - a = (x + c) - (y + d)`) THEN
  REWRITE_TAC[GSYM NPRODUCT_FACT; ADD_CLAUSES] THEN
  SIMP_TAC[REAL_OF_NUM_NPRODUCT; FINITE_NUMSEG; GSYM CX_LOG; LOG_PRODUCT;
           PRODUCT_POS_LT; IN_NUMSEG; REAL_OF_NUM_LT; LE_1; GSYM VSUM_CX] THEN
  REWRITE_TAC[GSYM VSUM_SUB_NUMSEG] THEN MATCH_MP_TAC VSUM_EQ_NUMSEG THEN
  REWRITE_TAC[complex_div] THEN ONCE_REWRITE_TAC[COMPLEX_ADD_SYM] THEN
  ASM_SIMP_TAC[CLOG_MUL_CX; REAL_LT_INV_EQ; REAL_OF_NUM_LT; LE_1;
               GSYM CX_INV; LOG_INV; CX_NEG; GSYM complex_sub]);;

let LGAMMA_ALT2 = prove
 (`!z. (!n. ~(z + Cx(&n) = Cx(&0)))
        ==> ((\n. vsum(1..n) (\m. z * clog(Cx(&1) + Cx(&1) / Cx(&m)) -
                                  clog(Cx(&1) + z / Cx(&m))) -
                  clog(z))
             --> lgamma(z)) sequentially`,
  REPEAT STRIP_TAC THEN
  SIMP_TAC[CX_INJ; REAL_OF_NUM_EQ; LE_1;
   COMPLEX_FIELD `~(m = Cx(&0)) ==> Cx(&1) + z / m = (z + m) / m`] THEN
  REWRITE_TAC[GSYM CX_ADD; GSYM CX_DIV] THEN
  SIMP_TAC[GSYM CX_LOG; LOG_DIV; REAL_LT_DIV; REAL_ARITH `&0 < &1 + &m`;
           REAL_OF_NUM_LT; LE_1] THEN
  SIMP_TAC[VSUM_SUB_NUMSEG; VSUM_COMPLEX_LMUL; FINITE_NUMSEG] THEN
  REWRITE_TAC[CX_SUB; REAL_OF_NUM_ADD; ARITH_RULE `1 + m = m + 1`] THEN
  REWRITE_TAC[VSUM_DIFFS_ALT; LOG_1; COMPLEX_SUB_RZERO] THEN
  FIRST_ASSUM(MP_TAC o MATCH_MP LGAMMA) THEN
  DISCH_THEN(MP_TAC o MATCH_MP (REWRITE_RULE[IMP_CONJ_ALT]
    LIM_SUBSEQUENCE)) THEN
  DISCH_THEN(MP_TAC o SPEC `\r. r + 1`) THEN
  REWRITE_TAC[] THEN ANTS_TAC THENL [ARITH_TAC; ALL_TAC] THEN
  MATCH_MP_TAC(REWRITE_RULE[IMP_CONJ] LIM_TRANSFORM) THEN
  REWRITE_TAC[o_DEF] THEN
  MATCH_MP_TAC LIM_TRANSFORM_EVENTUALLY THEN
  EXISTS_TAC `\n. --clog((Cx(&(n + 1)) + z) / Cx(&(n + 1)))` THEN
  CONJ_TAC THENL
   [REWRITE_TAC[EVENTUALLY_SEQUENTIALLY] THEN EXISTS_TAC `1` THEN
    X_GEN_TAC `n:num` THEN DISCH_TAC THEN ASM_REWRITE_TAC[] THEN
    REWRITE_TAC[GSYM ADD1; VSUM_CLAUSES_NUMSEG; ARITH_RULE `1 <= SUC n`] THEN
    REWRITE_TAC[COMPLEX_RING `Cx(&n) + z = z + Cx(&n)`] THEN
    SIMP_TAC[CX_LOG; REAL_OF_NUM_LT; LT_0] THEN
    CONV_TAC COMPLEX_RING;
    REWRITE_TAC[COMPLEX_VEC_0] THEN MATCH_MP_TAC LIM_NULL_COMPLEX_NEG THEN
    SIMP_TAC[REAL_OF_NUM_EQ; CX_INJ; ARITH_EQ; ADD_EQ_0;
      COMPLEX_FIELD `~(y = Cx(&0)) ==> (y + z) / y = Cx(&1) + z / y`] THEN
    SUBGOAL_THEN `Cx(&0) = clog (Cx (&1) + Cx(&0))` SUBST1_TAC THENL
     [REWRITE_TAC[COMPLEX_ADD_RID; CLOG_1]; ALL_TAC] THEN
    MP_TAC(ISPECL [`\z. clog(Cx(&1) + z)`; `sequentially`]
        LIM_CONTINUOUS_FUNCTION) THEN
    REWRITE_TAC[] THEN DISCH_THEN MATCH_MP_TAC THEN CONJ_TAC THENL
     [MATCH_MP_TAC COMPLEX_DIFFERENTIABLE_IMP_CONTINUOUS_AT THEN
      COMPLEX_DIFFERENTIABLE_TAC THEN REWRITE_TAC[RE_ADD; RE_CX] THEN
      REWRITE_TAC[REAL_ADD_RID; REAL_LT_01];
      REWRITE_TAC[complex_div] THEN MATCH_MP_TAC LIM_NULL_COMPLEX_LMUL THEN
      MATCH_MP_TAC(REWRITE_RULE[o_DEF]
        (ISPECL [`f:num->complex`; `\n. n + 1`] LIM_SUBSEQUENCE)) THEN
      CONJ_TAC THENL [ARITH_TAC; ALL_TAC] THEN
      REWRITE_TAC[GSYM CX_INV; REWRITE_RULE[o_DEF] (GSYM REALLIM_COMPLEX)] THEN
      REWRITE_TAC[REALLIM_1_OVER_N]]]);;

(* ------------------------------------------------------------------------- *)
(* The complex gamma function (defined using the Gauss/Euler product).       *)
(* Note that this totalizes it with the value zero at the poles.             *)
(* ------------------------------------------------------------------------- *)

let cgamma = new_definition
 `cgamma(z) = lim sequentially (\n. (Cx(&n) cpow z * Cx(&(FACT n))) /
                                    cproduct(0..n) (\m. z + Cx(&m)))`;;

let [CGAMMA;CGAMMA_EQ_0;CGAMMA_LGAMMA] = (CONJUNCTS o prove)
 (`(!z. ((\n. (Cx(&n) cpow z * Cx(&(FACT n))) / cproduct(0..n) (\m. z + Cx(&m)))
         --> cgamma(z)) sequentially) /\
   (!z. cgamma(z) = Cx(&0) <=> ?n. z + Cx(&n) = Cx(&0)) /\
   (!z. cgamma(z) = if ?n. z + Cx(&n) = Cx(&0) then Cx(&0)
                    else cexp(lgamma z))`,
  REWRITE_TAC[AND_FORALL_THM] THEN X_GEN_TAC `y:complex` THEN
  ASM_CASES_TAC `?n. y + Cx(&n) = Cx(&0)` THENL
   [ASM_REWRITE_TAC[GSYM NOT_EXISTS_THM] THEN
    FIRST_X_ASSUM(X_CHOOSE_TAC `N:num`) THEN
    REWRITE_TAC[cgamma; lim] THEN MATCH_MP_TAC(MESON[LIM_UNIQUE]
     `~trivial_limit net /\ (f --> a) net
      ==> (f --> @a. (f --> a) net) net /\ (@a. (f --> a) net) = a`) THEN
    REWRITE_TAC[TRIVIAL_LIMIT_SEQUENTIALLY] THEN
    MATCH_MP_TAC LIM_EVENTUALLY THEN REWRITE_TAC[EVENTUALLY_SEQUENTIALLY] THEN
    EXISTS_TAC `N:num` THEN X_GEN_TAC `n:num` THEN REWRITE_TAC[GE] THEN
    DISCH_TAC THEN
    SIMP_TAC[COMPLEX_ENTIRE; COMPLEX_DIV_EQ_0; CPRODUCT_EQ_0; FINITE_NUMSEG;
             IN_NUMSEG; LE_0] THEN
    ASM_MESON_TAC[LE_REFL];
    ASM_REWRITE_TAC[] THEN RULE_ASSUM_TAC(REWRITE_RULE[NOT_EXISTS_THM])] THEN
  SUBGOAL_THEN
   `((\n. (Cx(&n) cpow y * Cx(&(FACT n))) / cproduct(0..n) (\m. y + Cx(&m)))
     --> cexp(lgamma y)) sequentially`
  ASSUME_TAC THENL
   [MATCH_MP_TAC LIM_TRANSFORM_EVENTUALLY THEN
    EXISTS_TAC `\n. cexp(y * clog(Cx(&n)) - clog y -
                         vsum(1..n) (\m. clog((Cx(&m) + y) / Cx(&m))))` THEN
    CONJ_TAC THENL
     [REWRITE_TAC[EVENTUALLY_SEQUENTIALLY] THEN EXISTS_TAC `1` THEN
      X_GEN_TAC `n:num` THEN DISCH_TAC THEN
      ASM_SIMP_TAC[CEXP_SUB; cpow; CX_INJ; REAL_OF_NUM_EQ; LE_1] THEN
      REWRITE_TAC[complex_div; GSYM COMPLEX_MUL_ASSOC] THEN AP_TERM_TAC THEN
      SIMP_TAC[GSYM NPRODUCT_FACT; REAL_OF_NUM_NPRODUCT; FINITE_NUMSEG] THEN
      SIMP_TAC[CX_PRODUCT; FINITE_NUMSEG; GSYM CPRODUCT_INV] THEN
      SIMP_TAC[CPRODUCT_CLAUSES_LEFT; LE_0] THEN
      GEN_REWRITE_TAC RAND_CONV [COMPLEX_RING
       `a * b * c:complex = b * a * c`] THEN
      REWRITE_TAC[COMPLEX_ADD_RID] THEN BINOP_TAC THENL
       [ASM_MESON_TAC[COMPLEX_ADD_RID; CEXP_CLOG]; ALL_TAC] THEN
      SIMP_TAC[ADD_CLAUSES; GSYM CPRODUCT_MUL; FINITE_NUMSEG] THEN
      REWRITE_TAC[GSYM CEXP_NEG; GSYM VSUM_NEG] THEN
      SIMP_TAC[CEXP_VSUM; FINITE_NUMSEG] THEN MATCH_MP_TAC CPRODUCT_EQ THEN
      REWRITE_TAC[IN_NUMSEG] THEN X_GEN_TAC `m:num` THEN STRIP_TAC THEN
      REWRITE_TAC[CEXP_NEG] THEN
      GEN_REWRITE_TAC (RAND_CONV o LAND_CONV) [GSYM COMPLEX_INV_INV] THEN
      REWRITE_TAC[GSYM COMPLEX_INV_MUL] THEN AP_TERM_TAC THEN
      GEN_REWRITE_TAC RAND_CONV [COMPLEX_MUL_SYM] THEN
      GEN_REWRITE_TAC (RAND_CONV o ONCE_DEPTH_CONV) [COMPLEX_ADD_SYM] THEN
      MATCH_MP_TAC CEXP_CLOG THEN
      ONCE_REWRITE_TAC[COMPLEX_ADD_SYM] THEN
      ASM_REWRITE_TAC[COMPLEX_ENTIRE; COMPLEX_INV_EQ_0] THEN
      REWRITE_TAC[CX_INJ; REAL_OF_NUM_EQ] THEN ASM_ARITH_TAC;
      MATCH_MP_TAC(ISPEC `cexp` LIM_CONTINUOUS_FUNCTION) THEN
      ASM_SIMP_TAC[LGAMMA; CONTINUOUS_AT_CEXP]];
    MATCH_MP_TAC(TAUT `p /\ (p ==> q) ==> p /\ q`) THEN CONJ_TAC THENL
     [REWRITE_TAC[cgamma; lim] THEN CONV_TAC SELECT_CONV THEN ASM_MESON_TAC[];
      DISCH_TAC] THEN
    MATCH_MP_TAC(TAUT `q /\ (q ==> p) ==> p /\ q`) THEN CONJ_TAC THENL
     [MATCH_MP_TAC(ISPEC `sequentially` LIM_UNIQUE) THEN
      ASM_MESON_TAC[TRIVIAL_LIMIT_SEQUENTIALLY];
      SIMP_TAC[CEXP_NZ]]]);;

let CGAMMA_RECURRENCE_ALT = prove
 (`!z. cgamma(z) = cgamma(z + Cx(&1)) / z`,
  GEN_TAC THEN ASM_CASES_TAC `?n.  z + Cx(&n) = Cx(&0)` THENL
   [FIRST_ASSUM(fun th -> REWRITE_TAC[REWRITE_RULE[GSYM CGAMMA_EQ_0] th]) THEN
    CONV_TAC SYM_CONV THEN REWRITE_TAC[COMPLEX_DIV_EQ_0] THEN
    ASM_CASES_TAC `z = Cx(&0)` THEN ASM_REWRITE_TAC[] THEN
    FIRST_X_ASSUM(MP_TAC o check (is_exists o concl)) THEN
    REWRITE_TAC[LEFT_IMP_EXISTS_THM] THEN INDUCT_TAC THEN
    ASM_REWRITE_TAC[COMPLEX_ADD_RID; CGAMMA_EQ_0] THEN
    REWRITE_TAC[GSYM COMPLEX_ADD_ASSOC; GSYM CX_ADD; REAL_OF_NUM_ADD] THEN
    MESON_TAC[ADD1; ADD_SYM];
    RULE_ASSUM_TAC(REWRITE_RULE[NOT_EXISTS_THM])] THEN
  SUBGOAL_THEN `!n. ~((z + Cx(&1)) + Cx(&n) = Cx(&0))` ASSUME_TAC THENL
   [REWRITE_TAC[GSYM COMPLEX_ADD_ASSOC; GSYM CX_ADD; REAL_OF_NUM_ADD] THEN
    ASM_MESON_TAC[];
    ALL_TAC] THEN
  SUBGOAL_THEN `~(z = Cx(&0))` ASSUME_TAC THENL
   [ASM_MESON_TAC[COMPLEX_ADD_LID]; ALL_TAC] THEN
  MATCH_MP_TAC(COMPLEX_FIELD
   `(a * b) / c = Cx(&1) /\ ~(a = Cx(&0)) /\ ~(c = Cx(&0)) ==> b = c / a`) THEN
  ASM_REWRITE_TAC[CGAMMA_EQ_0] THEN
  MATCH_MP_TAC(ISPEC `sequentially` LIM_UNIQUE) THEN
  EXISTS_TAC
   `\n. (z * (Cx(&(n + 1)) cpow z * Cx(&(FACT(n + 1)))) /
             cproduct(0..n+1) (\m. z + Cx(&m))) /
        ((Cx(&n) cpow (z + Cx(&1)) * Cx(&(FACT n))) /
         cproduct(0..n) (\m. (z + Cx(&1)) + Cx(&m)))` THEN
  REWRITE_TAC[TRIVIAL_LIMIT_SEQUENTIALLY] THEN CONJ_TAC THENL
   [MATCH_MP_TAC LIM_COMPLEX_DIV THEN
    ASM_REWRITE_TAC[CGAMMA; CGAMMA_EQ_0] THEN
    MATCH_MP_TAC LIM_COMPLEX_LMUL THEN
    MP_TAC(SPEC `z:complex` CGAMMA) THEN
    DISCH_THEN(MP_TAC o SPEC `1` o MATCH_MP SEQ_OFFSET) THEN
    REWRITE_TAC[];
    ALL_TAC] THEN
  REWRITE_TAC[GSYM COMPLEX_ADD_ASSOC; GSYM CX_ADD; REAL_OF_NUM_ADD] THEN
  REWRITE_TAC[ARITH_RULE `1 + n = n + 1`] THEN
  SIMP_TAC[SYM(ISPECL [`f:num->complex`; `m:num`; `1`] CPRODUCT_OFFSET)] THEN
  SIMP_TAC[CPRODUCT_CLAUSES_LEFT; LE_0; ADD_CLAUSES] THEN
  REWRITE_TAC[GSYM ADD1; FACT; CX_MUL; COMPLEX_MUL_ASSOC; GSYM REAL_OF_NUM_MUL;
          ONCE_REWRITE_RULE[COMPLEX_MUL_SYM] (GSYM CPOW_SUC)] THEN
  REWRITE_TAC[complex_div; COMPLEX_INV_MUL; COMPLEX_INV_INV] THEN
  REWRITE_TAC[COMPLEX_ADD_RID; GSYM COMPLEX_MUL_ASSOC] THEN
  REWRITE_TAC[COMPLEX_RING
   `a * b * c * d * e * f * g * h:complex =
    (a * d) * (c * g) * (h * e) * (b * f)`] THEN
  ASM_SIMP_TAC[GSYM complex_div; COMPLEX_DIV_REFL; CX_INJ;
               REAL_OF_NUM_EQ; FACT_NZ; COMPLEX_MUL_LID] THEN
  GEN_REWRITE_TAC LAND_CONV [GSYM COMPLEX_MUL_LID] THEN
  MATCH_MP_TAC LIM_COMPLEX_MUL THEN CONJ_TAC THENL
   [MATCH_MP_TAC LIM_EVENTUALLY THEN MATCH_MP_TAC ALWAYS_EVENTUALLY THEN
    X_GEN_TAC `n:num` THEN REWRITE_TAC[] THEN
    MATCH_MP_TAC COMPLEX_DIV_REFL THEN
    SIMP_TAC[CPRODUCT_EQ_0; FINITE_NUMSEG] THEN ASM_MESON_TAC[];
    ALL_TAC] THEN
  MATCH_MP_TAC LIM_TRANSFORM_EVENTUALLY THEN
  EXISTS_TAC `\n. cexp((z + Cx(&1)) * clog(Cx(&1) + inv(Cx(&n))))` THEN
  CONJ_TAC THENL
   [REWRITE_TAC[EVENTUALLY_SEQUENTIALLY] THEN EXISTS_TAC `1` THEN
    SIMP_TAC[cpow; CX_INJ; REAL_OF_NUM_EQ; NOT_SUC; LE_1] THEN
    REPEAT STRIP_TAC THEN REWRITE_TAC[GSYM CEXP_SUB] THEN AP_TERM_TAC THEN
    REWRITE_TAC[GSYM COMPLEX_SUB_LDISTRIB] THEN AP_TERM_TAC THEN
    ASM_SIMP_TAC[CX_INJ; REAL_OF_NUM_EQ; LE_1; COMPLEX_FIELD
     `~(n = Cx(&0)) ==> Cx(&1) + inv n = (n + Cx(&1)) / n`] THEN
    REWRITE_TAC[GSYM REAL_OF_NUM_SUC; GSYM CX_ADD] THEN
    ASM_SIMP_TAC[REAL_OF_NUM_ADD; GSYM CX_DIV; GSYM CX_LOG; LE_1; LOG_DIV;
                 REAL_OF_NUM_LT; REAL_LT_DIV; ARITH_RULE `0 < n + 1`] THEN
    REWRITE_TAC[CX_SUB];
    ALL_TAC] THEN
  GEN_REWRITE_TAC LAND_CONV [GSYM CEXP_0] THEN
  MATCH_MP_TAC(ISPEC `cexp` LIM_CONTINUOUS_FUNCTION) THEN
  REWRITE_TAC[CONTINUOUS_AT_CEXP] THEN
  MATCH_MP_TAC LIM_NULL_COMPLEX_LMUL THEN
  MATCH_MP_TAC LIM_NULL_COMPARISON_COMPLEX THEN
  EXISTS_TAC `\n. Cx(&2) * inv(Cx(&n))` THEN
  SIMP_TAC[LIM_INV_N; LIM_NULL_COMPLEX_LMUL] THEN
  REWRITE_TAC[EVENTUALLY_SEQUENTIALLY] THEN EXISTS_TAC `2` THEN
  REWRITE_TAC[GE; IN_FROM] THEN X_GEN_TAC `n:num` THEN STRIP_TAC THEN
  MP_TAC(ISPECL [`0`; `Cx(inv(&n))`] TAYLOR_CLOG) THEN
  SIMP_TAC[VSUM_CLAUSES_NUMSEG; ARITH; VECTOR_SUB_RZERO] THEN
  REWRITE_TAC[REAL_POW_1; GSYM CX_ADD; COMPLEX_NORM_CX] THEN
  REWRITE_TAC[REAL_ABS_INV; REAL_ABS_NUM] THEN ANTS_TAC THENL
   [MATCH_MP_TAC REAL_INV_LT_1 THEN REWRITE_TAC[REAL_OF_NUM_LT] THEN
    ASM_ARITH_TAC;
    REWRITE_TAC[CX_ADD; CX_INV]] THEN
  MATCH_MP_TAC(REWRITE_RULE[IMP_CONJ_ALT] REAL_LE_TRANS) THEN
  ONCE_REWRITE_TAC[COMPLEX_MUL_SYM] THEN
  REWRITE_TAC[real_div; COMPLEX_NORM_MUL] THEN
  REWRITE_TAC[COMPLEX_NORM_INV; COMPLEX_NORM_CX; REAL_ABS_NUM] THEN
  MATCH_MP_TAC REAL_LE_LMUL THEN
  REWRITE_TAC[REAL_LE_INV_EQ; REAL_POS] THEN
  GEN_REWRITE_TAC RAND_CONV [GSYM REAL_INV_INV] THEN
  MATCH_MP_TAC REAL_LE_INV2 THEN CONV_TAC REAL_RAT_REDUCE_CONV THEN
  REWRITE_TAC[REAL_ARITH `&1 / &2 <= &1 - x <=> x <= inv(&2)`] THEN
  REWRITE_TAC[real_div; REAL_MUL_LID] THEN
  MATCH_MP_TAC REAL_LE_INV2 THEN
  REWRITE_TAC[REAL_OF_NUM_LT; REAL_OF_NUM_LE] THEN ASM_ARITH_TAC);;

let CGAMMA_1 = prove
 (`cgamma(Cx(&1)) = Cx(&1)`,
  MP_TAC(SPEC `Cx(&1)` CGAMMA) THEN
  MATCH_MP_TAC(REWRITE_RULE[IMP_CONJ_ALT; TRIVIAL_LIMIT_SEQUENTIALLY]
     (ISPEC `sequentially` LIM_UNIQUE)) THEN
  REWRITE_TAC[GSYM CX_ADD; REAL_OF_NUM_ADD; ARITH_RULE `1 + n = n + 1`] THEN
  SIMP_TAC[SYM(ISPECL [`f:num->complex`; `m:num`; `1`] CPRODUCT_OFFSET)] THEN
  MATCH_MP_TAC LIM_TRANSFORM_EVENTUALLY THEN
  EXISTS_TAC `\n. Cx(&n / (&n + &1))` THEN CONJ_TAC THENL
   [REWRITE_TAC[EVENTUALLY_SEQUENTIALLY] THEN EXISTS_TAC `1` THEN
    X_GEN_TAC `n:num` THEN DISCH_TAC THEN REWRITE_TAC[CX_DIV] THEN
    ASM_SIMP_TAC[CPOW_N; CX_INJ; REAL_OF_NUM_EQ; LE_1; COMPLEX_POW_1] THEN
    REWRITE_TAC[complex_div; GSYM COMPLEX_MUL_ASSOC] THEN AP_TERM_TAC THEN
    ONCE_REWRITE_TAC [GSYM COMPLEX_INV_INV] THEN
    AP_TERM_TAC THEN REWRITE_TAC[COMPLEX_INV_INV; COMPLEX_INV_MUL] THEN
    MATCH_MP_TAC(COMPLEX_FIELD
     `a * b = c /\ ~(b = Cx(&0)) ==> a = inv b * c`) THEN
    REWRITE_TAC[GSYM CX_MUL; CX_INJ; REAL_OF_NUM_EQ; FACT_NZ] THEN
    REWRITE_TAC[REAL_OF_NUM_SUC; REAL_OF_NUM_MUL; GSYM(CONJUNCT2 FACT)] THEN
    REWRITE_TAC[ADD_CLAUSES; ADD1] THEN SPEC_TAC(`n + 1`,`m:num`) THEN
    INDUCT_TAC THEN REWRITE_TAC[FACT; CPRODUCT_CLAUSES_NUMSEG; ARITH] THEN
    ASM_REWRITE_TAC[CX_MUL; GSYM REAL_OF_NUM_MUL] THEN
    REWRITE_TAC[ARITH_RULE `1 <= SUC n`; COMPLEX_MUL_SYM];
    ALL_TAC] THEN
  SIMP_TAC[REAL_FIELD `&n / (&n + &1) = &1 - inv(&n + &1)`] THEN
  SUBST1_TAC(COMPLEX_RING `Cx(&1) = Cx(&1) - Cx(&0)`) THEN
  REWRITE_TAC[CX_SUB] THEN MATCH_MP_TAC LIM_SUB THEN
  REWRITE_TAC[LIM_CONST; REAL_OF_NUM_ADD] THEN
  MATCH_MP_TAC(ISPECL [`f:num->complex`; `l:complex`; `1`] SEQ_OFFSET) THEN
  REWRITE_TAC[CX_INV; LIM_INV_N]);;

let CGAMMA_RECURRENCE = prove
 (`!z. cgamma(z + Cx(&1)) = if z = Cx(&0) then Cx(&1) else z * cgamma(z)`,
  GEN_TAC THEN COND_CASES_TAC THEN
  ASM_REWRITE_TAC[COMPLEX_ADD_LID; CGAMMA_1] THEN
  MATCH_MP_TAC(COMPLEX_FIELD `a = b / c /\ ~(c = Cx(&0)) ==> b = c * a`) THEN
  ASM_MESON_TAC[CGAMMA_RECURRENCE_ALT]);;

let CGAMMA_FACT = prove
 (`!n. cgamma(Cx(&(n + 1))) = Cx(&(FACT n))`,
  INDUCT_TAC THEN REWRITE_TAC[FACT; ADD_CLAUSES; CGAMMA_1] THEN
  REWRITE_TAC[GSYM REAL_OF_NUM_SUC] THEN REWRITE_TAC[CX_ADD] THEN
  REWRITE_TAC[CGAMMA_RECURRENCE; CX_INJ; REAL_OF_NUM_EQ; ADD_EQ_0; ARITH] THEN
  ASM_REWRITE_TAC[ADD1; GSYM REAL_OF_NUM_MUL; CX_MUL]);;

let CGAMMA_POLES = prove
 (`!n. cgamma(--(Cx(&n))) = Cx(&0)`,
  REWRITE_TAC[CGAMMA_EQ_0] THEN MESON_TAC[COMPLEX_ADD_LINV]);;

let COMPLEX_DIFFERENTIABLE_AT_CGAMMA = prove
 (`!z. (!n. ~(z + Cx(&n) = Cx(&0))) ==> cgamma complex_differentiable at z`,
  let lemma = prove
   (`!z. (!n. ~(z + Cx(&n) = Cx(&0))) /\
         cgamma complex_differentiable at (z + Cx(&1))
         ==> cgamma complex_differentiable at z`,
    REPEAT STRIP_TAC THEN
    MATCH_MP_TAC COMPLEX_DIFFERENTIABLE_TRANSFORM_AT THEN
    MAP_EVERY EXISTS_TAC [`\z. cgamma(z + Cx(&1)) / z`; `&1`] THEN
    REWRITE_TAC[REAL_LT_01] THEN
    CONJ_TAC THENL [REWRITE_TAC[GSYM CGAMMA_RECURRENCE_ALT]; ALL_TAC] THEN
    MATCH_MP_TAC COMPLEX_DIFFERENTIABLE_DIV_AT THEN
    REWRITE_TAC[COMPLEX_DIFFERENTIABLE_ID] THEN
    CONJ_TAC THENL [ALL_TAC; ASM_MESON_TAC[COMPLEX_ADD_RID]] THEN
    GEN_REWRITE_TAC LAND_CONV [GSYM o_DEF] THEN
    MATCH_MP_TAC COMPLEX_DIFFERENTIABLE_COMPOSE_AT THEN
    ASM_REWRITE_TAC[] THEN COMPLEX_DIFFERENTIABLE_TAC) in
  REPEAT STRIP_TAC THEN MP_TAC(SPEC `abs(Re z) + &1` REAL_ARCH_SIMPLE) THEN
  DISCH_THEN(X_CHOOSE_TAC `N:num`) THEN
  SUBGOAL_THEN
   `!n. n <= N ==> cgamma complex_differentiable (at (z + Cx(&N) - Cx(&n)))`
  MP_TAC THENL
   [ALL_TAC; MESON_TAC[LE_REFL; COMPLEX_SUB_REFL; COMPLEX_ADD_RID]] THEN
  INDUCT_TAC THENL
   [DISCH_TAC THEN REWRITE_TAC[COMPLEX_SUB_RZERO] THEN
    MATCH_MP_TAC COMPLEX_DIFFERENTIABLE_TRANSFORM_AT THEN
    EXISTS_TAC `\z. cexp(lgamma z)` THEN
    SUBGOAL_THEN `open {z | !n. ~(z + Cx(&n) = Cx(&0))}` MP_TAC THENL
     [REWRITE_TAC[SET_RULE `{z | !n. P n z} = UNIV DIFF {z | ?n. ~P n z}`] THEN
      REWRITE_TAC[GSYM closed] THEN MATCH_MP_TAC DISCRETE_IMP_CLOSED THEN
      EXISTS_TAC `&1` THEN REWRITE_TAC[REAL_LT_01; IMP_CONJ] THEN
      REWRITE_TAC[IN_ELIM_THM; LEFT_IMP_EXISTS_THM] THEN
      SIMP_TAC[COMPLEX_RING `x + y = Cx(&0) <=> x = --y`] THEN
      REWRITE_TAC[COMPLEX_RING `--x - --y:complex = y - x`] THEN
      REWRITE_TAC[COMPLEX_EQ_NEG2; CX_INJ; GSYM CX_SUB; COMPLEX_NORM_CX] THEN
      SIMP_TAC[GSYM REAL_EQ_INTEGERS; INTEGER_CLOSED];
      REWRITE_TAC[open_def; IN_ELIM_THM] THEN
      DISCH_THEN(MP_TAC o SPEC `z:complex`) THEN ASM_REWRITE_TAC[] THEN
      MATCH_MP_TAC MONO_EXISTS THEN X_GEN_TAC `e:real` THEN
      STRIP_TAC THEN ASM_REWRITE_TAC[] THEN CONJ_TAC THENL
       [X_GEN_TAC `w:complex` THEN REWRITE_TAC[dist] THEN DISCH_TAC THEN
        FIRST_X_ASSUM(MP_TAC o SPEC `w - Cx(&N)`) THEN
        ASM_SIMP_TAC[dist; COMPLEX_RING `w - n - z:complex = w - (z + n)`] THEN
        REWRITE_TAC[CGAMMA_LGAMMA] THEN COND_CASES_TAC THEN REWRITE_TAC[] THEN
        FIRST_X_ASSUM(X_CHOOSE_TAC `n:num`) THEN
        DISCH_THEN(MP_TAC o SPEC `n + N:num`) THEN
        REWRITE_TAC[GSYM REAL_OF_NUM_ADD; CX_ADD] THEN
        ASM_REWRITE_TAC[COMPLEX_RING `w - N + n + N:complex = w + n`];
        GEN_REWRITE_TAC LAND_CONV [GSYM o_DEF] THEN
        MATCH_MP_TAC COMPLEX_DIFFERENTIABLE_COMPOSE_AT THEN
        REWRITE_TAC[COMPLEX_DIFFERENTIABLE_AT_CEXP] THEN
        MATCH_MP_TAC COMPLEX_DIFFERENTIABLE_AT_LGAMMA THEN
        REWRITE_TAC[RE_ADD; RE_CX] THEN ASM_REAL_ARITH_TAC]];
    DISCH_TAC THEN REWRITE_TAC[GSYM REAL_OF_NUM_SUC; CX_ADD] THEN
    MATCH_MP_TAC lemma THEN
    REWRITE_TAC[COMPLEX_RING `(z + N - (n + w)) + w:complex = z + N - n`] THEN
    CONJ_TAC THENL
     [ALL_TAC; FIRST_X_ASSUM MATCH_MP_TAC THEN ASM_ARITH_TAC] THEN
    X_GEN_TAC `m:num` THEN
    FIRST_X_ASSUM(MP_TAC o SPEC `(N + m) - (n + 1)`) THEN
    SUBGOAL_THEN `n + 1 <= N + m`
      (fun th -> SIMP_TAC[th; GSYM REAL_OF_NUM_SUB])
    THENL [ASM_ARITH_TAC; ALL_TAC] THEN
    REWRITE_TAC[GSYM REAL_OF_NUM_ADD; CX_ADD; CX_SUB] THEN
    CONV_TAC COMPLEX_RING]);;

let COMPLEX_DIFFERENTIABLE_WITHIN_CGAMMA = prove
 (`!z s. (!n. ~(z + Cx(&n) = Cx(&0)))
         ==> cgamma complex_differentiable at z within s`,
  SIMP_TAC[COMPLEX_DIFFERENTIABLE_AT_WITHIN;
           COMPLEX_DIFFERENTIABLE_AT_CGAMMA]);;

let HOLOMORPHIC_ON_CGAMMA = prove
 (`!s. s SUBSET {z | !n. ~(z + Cx(&n) = Cx(&0))}
       ==> cgamma holomorphic_on s`,
  REPEAT STRIP_TAC THEN REWRITE_TAC[HOLOMORPHIC_ON_DIFFERENTIABLE] THEN
  REPEAT STRIP_TAC THEN MATCH_MP_TAC COMPLEX_DIFFERENTIABLE_WITHIN_CGAMMA THEN
  ASM SET_TAC[]);;

let CONTINUOUS_AT_CGAMMA = prove
 (`!z. (!n. ~(z + Cx(&n) = Cx(&0))) ==> cgamma continuous at z`,
  SIMP_TAC[COMPLEX_DIFFERENTIABLE_IMP_CONTINUOUS_AT;
           COMPLEX_DIFFERENTIABLE_AT_CGAMMA]);;

let CONTINUOUS_WITHIN_CGAMMA = prove
 (`!z s. (!n. ~(z + Cx(&n) = Cx(&0)))
         ==> cgamma continuous at z within s`,
  SIMP_TAC[COMPLEX_DIFFERENTIABLE_IMP_CONTINUOUS_WITHIN;
           COMPLEX_DIFFERENTIABLE_WITHIN_CGAMMA]);;

let CONTINUOUS_ON_CGAMMA = prove
 (`!s. s SUBSET {z | !n. ~(z + Cx(&n) = Cx(&0))}
       ==> cgamma continuous_on s`,
  SIMP_TAC[HOLOMORPHIC_ON_IMP_CONTINUOUS_ON; HOLOMORPHIC_ON_CGAMMA]);;

let CGAMMA_SIMPLE_POLES = prove
 (`!n. ((\z. (z + Cx(&n)) * cgamma z) --> --Cx(&1) pow n / Cx(&(FACT n)))
       (at(--Cx(&n)))`,
  INDUCT_TAC THENL
   [REWRITE_TAC[COMPLEX_ADD_RID; COMPLEX_NEG_0; FACT; complex_pow;
                COMPLEX_DIV_1] THEN
    ONCE_REWRITE_TAC[CGAMMA_RECURRENCE_ALT] THEN
    MATCH_MP_TAC LIM_TRANSFORM_AWAY_AT THEN
    MAP_EVERY EXISTS_TAC [`\z. cgamma(z + Cx(&1))`; `Cx(&1)`] THEN
    REWRITE_TAC[CONJ_ASSOC] THEN
    CONJ_TAC THENL [CONV_TAC COMPLEX_FIELD; ALL_TAC] THEN
    SUBGOAL_THEN `(\z. cgamma (z + Cx(&1))) continuous at (Cx(&0))`
    MP_TAC THENL
     [GEN_REWRITE_TAC LAND_CONV [GSYM o_DEF] THEN
      MATCH_MP_TAC CONTINUOUS_AT_COMPOSE THEN
      CONJ_TAC THENL [CONTINUOUS_TAC; ALL_TAC] THEN
      MATCH_MP_TAC CONTINUOUS_AT_CGAMMA THEN
      REWRITE_TAC[GSYM CX_ADD; CX_INJ] THEN REAL_ARITH_TAC;
      REWRITE_TAC[CONTINUOUS_AT; COMPLEX_ADD_LID; CGAMMA_1]];
    REWRITE_TAC[FACT; CX_MUL; GSYM REAL_OF_NUM_MUL] THEN
    ONCE_REWRITE_TAC[CGAMMA_RECURRENCE_ALT] THEN
    REWRITE_TAC[complex_div; complex_pow; COMPLEX_INV_MUL] THEN
    REWRITE_TAC[SIMPLE_COMPLEX_ARITH
     `(--Cx(&1) * p) * is * i = (p * i) * --is`] THEN
    REWRITE_TAC[COMPLEX_MUL_ASSOC] THEN MATCH_MP_TAC LIM_COMPLEX_MUL THEN
    CONJ_TAC THENL
     [FIRST_X_ASSUM(MP_TAC o
        ISPECL [`at (--Cx(&(SUC n)))`; `\z.  z + Cx(&1)`] o
        MATCH_MP (ONCE_REWRITE_RULE[IMP_CONJ_ALT]
         (REWRITE_RULE[CONJ_ASSOC] LIM_COMPOSE_AT))) THEN
      REWRITE_TAC[o_DEF; GSYM REAL_OF_NUM_SUC; CX_ADD] THEN
      REWRITE_TAC[COMPLEX_RING `(z + Cx(&1)) + w = z + (w + Cx(&1))`] THEN
      REWRITE_TAC[GSYM complex_div] THEN DISCH_THEN MATCH_MP_TAC THEN
      CONJ_TAC THENL
       [LIM_TAC THEN CONV_TAC COMPLEX_RING;
        REWRITE_TAC[EVENTUALLY_AT; GSYM DIST_NZ] THEN
        EXISTS_TAC `&1` THEN REWRITE_TAC[REAL_LT_01] THEN
        CONV_TAC COMPLEX_RING];
      REWRITE_TAC[COMPLEX_NEG_INV; GSYM CONTINUOUS_AT] THEN
      GEN_REWRITE_TAC LAND_CONV [GSYM ETA_AX] THEN
      MATCH_MP_TAC CONTINUOUS_COMPLEX_INV_AT THEN
      REWRITE_TAC[GSYM CX_NEG; CX_INJ; GSYM REAL_OF_NUM_SUC] THEN
      REWRITE_TAC[CONTINUOUS_AT_ID] THEN REAL_ARITH_TAC]]);;

let CNJ_CGAMMA = prove
 (`!z. cnj(cgamma z) = cgamma(cnj z)`,
  GEN_TAC THEN MP_TAC(SPEC `cnj z` CGAMMA) THEN
  MATCH_MP_TAC(REWRITE_RULE[TAUT `a /\ b /\ c ==> d <=> a /\ b ==> c ==> d`]
        LIM_UNIQUE) THEN
  REWRITE_TAC[TRIVIAL_LIMIT_SEQUENTIALLY] THEN
  GEN_REWRITE_TAC I [GSYM LIM_CNJ] THEN REWRITE_TAC[CNJ_CNJ] THEN
  MP_TAC(SPEC `z:complex` CGAMMA) THEN
  MATCH_MP_TAC(REWRITE_RULE[IMP_CONJ] LIM_TRANSFORM_EVENTUALLY) THEN
  SIMP_TAC[CNJ_DIV; CNJ_MUL; CNJ_CX; CNJ_CPRODUCT; FINITE_NUMSEG] THEN
  REWRITE_TAC[CNJ_ADD; CNJ_CNJ; CNJ_CX] THEN
  REWRITE_TAC[EVENTUALLY_SEQUENTIALLY] THEN EXISTS_TAC `1` THEN
  X_GEN_TAC `n:num` THEN REWRITE_TAC[cpow; CNJ_EQ_0] THEN
  DISCH_TAC THEN COND_CASES_TAC THEN REWRITE_TAC[CNJ_CX; CNJ_CEXP] THEN
  REWRITE_TAC[CNJ_MUL; CNJ_CNJ] THEN
  ASM_SIMP_TAC[CNJ_CLOG; RE_CX; REAL_OF_NUM_LT; LE_1; CNJ_CX]);;

let REAL_GAMMA = prove
 (`!z. real z ==> real(cgamma z)`,
  SIMP_TAC[REAL_CNJ; CNJ_CGAMMA]);;

let RE_POS_CGAMMA_REAL = prove
 (`!z. real z /\ &0 <= Re z ==> &0 <= Re(cgamma z)`,
  REWRITE_TAC[REAL_EXISTS; LEFT_IMP_EXISTS_THM; IMP_CONJ] THEN
  GEN_TAC THEN X_GEN_TAC `x:real` THEN DISCH_THEN SUBST1_TAC THEN
  REWRITE_TAC[RE_CX] THEN DISCH_TAC THEN MP_TAC(SPEC `Cx x` CGAMMA) THEN
  MATCH_MP_TAC(REWRITE_RULE[TAUT `p /\ q /\ r ==> s <=> p /\ r ==> q ==> s`]
        LIM_RE_LBOUND) THEN
  REWRITE_TAC[TRIVIAL_LIMIT_SEQUENTIALLY; EVENTUALLY_SEQUENTIALLY] THEN
  EXISTS_TAC `1` THEN X_GEN_TAC `n:num` THEN DISCH_TAC THEN
  ASM_SIMP_TAC[cpow; CX_INJ; REAL_OF_NUM_EQ; LE_1; GSYM CX_LOG; GSYM CX_INV;
               REAL_OF_NUM_LT; GSYM CX_MUL; GSYM CX_EXP; RE_MUL_CX; RE_CX;
               complex_div; GSYM CX_ADD; GSYM CX_PRODUCT; FINITE_NUMSEG] THEN
  MATCH_MP_TAC REAL_LE_MUL THEN
  SIMP_TAC[REAL_LE_MUL; REAL_EXP_POS_LE; REAL_POS; REAL_LE_INV_EQ] THEN
  MATCH_MP_TAC PRODUCT_POS_LE_NUMSEG THEN ASM_REAL_ARITH_TAC);;

let CGAMMA_LEGENDRE_ALT = prove
 (`!z. cgamma(z) * cgamma(z + Cx(&1) / Cx(&2)) =
       Cx(&2) cpow (Cx(&1) - Cx(&2) * z) *
       cgamma(Cx(&1) / Cx(&2)) * cgamma(Cx(&2) * z)`,
  REWRITE_TAC[GSYM CX_DIV] THEN
  SUBGOAL_THEN
   `?f. !z. (!n. ~(Cx(&2) * z + Cx(&n) = Cx(&0)))
            ==> (f --> (cgamma(z) * cgamma(z + Cx(&1 / &2))) /
                     (Cx(&2) cpow (Cx(&1) - Cx(&2) * z) * cgamma(Cx(&2) * z)))
                sequentially`
  CHOOSE_TAC THENL
   [EXISTS_TAC
     `\n. (Cx(&n) cpow Cx(&1 / &2) * inv (Cx(&2))) *
          (Cx(&(FACT n)) pow 2 * inv (Cx(&(FACT (2 * n))))) *
          Cx(&4) pow (n + 1) *
          inv(Cx(&(2 * n + 1)))` THEN
    REPEAT STRIP_TAC THEN MP_TAC(SPEC `Cx(&2) * z` CGAMMA) THEN
    DISCH_THEN(MP_TAC o SPEC `\n. 2 * n` o MATCH_MP
     (REWRITE_RULE[IMP_CONJ_ALT] LIM_SUBSEQUENCE)) THEN
    REWRITE_TAC[o_DEF] THEN ANTS_TAC THENL [ARITH_TAC; ALL_TAC] THEN
    DISCH_THEN(MP_TAC o SPEC `Cx(&2) cpow (Cx(&1) - Cx(&2) * z)` o
          MATCH_MP LIM_COMPLEX_LMUL) THEN
    MP_TAC(CONJ (SPEC `z:complex` CGAMMA) (SPEC `z + Cx(&1 / &2)` CGAMMA)) THEN
    DISCH_THEN(MP_TAC o MATCH_MP LIM_COMPLEX_MUL) THEN
    REWRITE_TAC[IMP_IMP] THEN DISCH_THEN(MP_TAC o MATCH_MP
     (ONCE_REWRITE_RULE[IMP_CONJ]
         (REWRITE_RULE[CONJ_ASSOC] LIM_COMPLEX_DIV))) THEN
    ASM_REWRITE_TAC[COMPLEX_ENTIRE; CPOW_EQ_0; CGAMMA_EQ_0] THEN
    REWRITE_TAC[CX_INJ; REAL_OF_NUM_EQ; ARITH_EQ] THEN
    SUBGOAL_THEN `((\n. (Cx(&2) * z + Cx(&(2 * n + 1))) / Cx(&(2 * n + 1)))
                   --> Cx(&1)) sequentially`
    MP_TAC THENL
     [SIMP_TAC[complex_div; COMPLEX_MUL_RINV; CX_INJ; REAL_OF_NUM_EQ;
               COMPLEX_ADD_RDISTRIB; ARITH_RULE `~(2 * n + 1 = 0)`] THEN
      ONCE_REWRITE_TAC[LIM_NULL_COMPLEX] THEN
      REWRITE_TAC[COMPLEX_RING `(a + b) - b:complex = a`] THEN
      MATCH_MP_TAC LIM_NULL_COMPLEX_LMUL THEN
      MP_TAC(SPEC `\n. 2 * n + 1`
       (MATCH_MP (REWRITE_RULE[IMP_CONJ_ALT] LIM_SUBSEQUENCE) LIM_INV_N)) THEN
      REWRITE_TAC[o_DEF] THEN DISCH_THEN MATCH_MP_TAC THEN ARITH_TAC;
      REWRITE_TAC[IMP_IMP] THEN
      DISCH_THEN(MP_TAC o MATCH_MP LIM_COMPLEX_MUL)] THEN
    REWRITE_TAC[COMPLEX_MUL_LID] THEN
    MATCH_MP_TAC(REWRITE_RULE[IMP_CONJ] LIM_TRANSFORM_EVENTUALLY) THEN
    REWRITE_TAC[EVENTUALLY_SEQUENTIALLY] THEN
    EXISTS_TAC `1` THEN X_GEN_TAC `n:num` THEN DISCH_TAC THEN
    REWRITE_TAC[CPOW_ADD; complex_div; COMPLEX_INV_MUL; COMPLEX_INV_INV] THEN
    REWRITE_TAC[GSYM REAL_OF_NUM_MUL; CX_MUL] THEN
    SIMP_TAC[CPOW_MUL_REAL; REAL_CX; RE_CX; REAL_POS] THEN
    REWRITE_TAC[CPOW_SUB; CPOW_N; CX_INJ; REAL_OF_NUM_EQ; ARITH_EQ] THEN
    SIMP_TAC[COMPLEX_POW_1; complex_div; COMPLEX_INV_INV; COMPLEX_INV_MUL] THEN
    REWRITE_TAC[GSYM COMPLEX_MUL_ASSOC] THEN REWRITE_TAC[COMPLEX_RING
      `x * y * a * b * c * d * e * f * g * h * i * j * k * l * m:complex =
       (x * y) * (e * h) * ((a * d) * k) *
       ((b * f) * l) * (i * j) * (m * c * g)`] THEN
    REWRITE_TAC[GSYM COMPLEX_POW_2; COMPLEX_MUL_2; CPOW_ADD] THEN
    ASM_SIMP_TAC[COMPLEX_MUL_RINV; COMPLEX_POW_EQ_0; CPOW_EQ_0;
                 CX_INJ; REAL_OF_NUM_EQ; ARITH_EQ; LE_1; COMPLEX_MUL_LID] THEN
    REWRITE_TAC[GSYM COMPLEX_MUL_2; GSYM COMPLEX_INV_MUL] THEN
    SIMP_TAC[GSYM CPRODUCT_MUL; FINITE_NUMSEG] THEN
    REWRITE_TAC[COMPLEX_RING
     `(z + m) * ((z + Cx(&1 / &2)) + m) =
      inv(Cx(&4)) *
      (Cx(&2) * z + Cx(&2) * m) * (Cx(&2) * z + Cx(&2) * m + Cx(&1))`] THEN
    SIMP_TAC[CPRODUCT_MUL; FINITE_NUMSEG; CPRODUCT_CONST_NUMSEG] THEN
    SIMP_TAC[GSYM CPRODUCT_MUL; SUB_0; FINITE_NUMSEG] THEN
    REWRITE_TAC[GSYM CX_ADD; GSYM CX_MUL;
                REAL_OF_NUM_ADD; REAL_OF_NUM_MUL] THEN
    REWRITE_TAC[GSYM CPRODUCT_PAIR] THEN
    SIMP_TAC[GSYM ADD1; CPRODUCT_CLAUSES_NUMSEG] THEN
    REWRITE_TAC[ADD1] THEN CONV_TAC NUM_REDUCE_CONV THEN
    SIMP_TAC[LE_0; COMPLEX_INV_MUL; COMPLEX_INV_INV; GSYM COMPLEX_POW_INV] THEN
    ONCE_REWRITE_TAC[COMPLEX_RING
     `x * a * b * c * d * e * f:complex = (c * e) * a * b * d * f * x`] THEN
    ASM_SIMP_TAC[COMPLEX_MUL_RINV; CPRODUCT_EQ_0; FINITE_NUMSEG] THEN
    REWRITE_TAC[COMPLEX_MUL_LID; COMPLEX_MUL_ASSOC] THEN
    AP_THM_TAC THEN AP_TERM_TAC THEN
    ASM_SIMP_TAC[GSYM COMPLEX_MUL_ASSOC; COMPLEX_MUL_LINV] THEN
    CONV_TAC COMPLEX_RING;
    ALL_TAC] THEN
  X_GEN_TAC `z:complex` THEN
  ASM_CASES_TAC `!n. ~(Cx(&2) * z + Cx(&n) = Cx(&0))` THENL
   [FIRST_X_ASSUM(fun th ->
      MP_TAC(SPEC `z:complex` th) THEN MP_TAC(SPEC `Cx(&1 / &2)` th)) THEN
    ASM_REWRITE_TAC[GSYM CX_ADD; GSYM CX_MUL; GSYM CX_SUB; CX_INJ] THEN
    ANTS_TAC THENL [REAL_ARITH_TAC; CONV_TAC REAL_RAT_REDUCE_CONV] THEN
    REWRITE_TAC[CGAMMA_1; CPOW_N; CX_INJ; REAL_OF_NUM_EQ; ARITH_EQ] THEN
    REWRITE_TAC[complex_pow; COMPLEX_MUL_RID; COMPLEX_DIV_1; IMP_IMP] THEN
    DISCH_THEN(MP_TAC o MATCH_MP (ONCE_REWRITE_RULE[IMP_CONJ_ALT]
        LIM_UNIQUE)) THEN
    REWRITE_TAC[TRIVIAL_LIMIT_SEQUENTIALLY] THEN MATCH_MP_TAC(COMPLEX_FIELD
     `~(p = Cx(&0)) /\ ~(z2 = Cx(&0))
      ==> a = (z * z') / (p * z2) ==> z * z' = p * a * z2`) THEN
    ASM_REWRITE_TAC[CGAMMA_EQ_0; CPOW_EQ_0; CX_INJ; REAL_OF_NUM_EQ; ARITH_EQ];
    MATCH_MP_TAC(COMPLEX_RING
     `z = Cx(&0) /\ ((w = Cx(&0)) \/ (y = Cx(&0)))
      ==> w * y = p * q * z`) THEN
    REWRITE_TAC[CGAMMA_EQ_0] THEN
    CONJ_TAC THENL [ASM_MESON_TAC[]; ALL_TAC] THEN
    REWRITE_TAC[OR_EXISTS_THM; GSYM COMPLEX_ADD_ASSOC] THEN
    ONCE_REWRITE_TAC[COMPLEX_RING
     `z + n = Cx(&0) <=> Cx(&2) * z + Cx(&2) * n = Cx(&0)`] THEN
    REWRITE_TAC[GSYM CX_ADD; GSYM CX_MUL] THEN
    REWRITE_TAC[REAL_ARITH `&2 * (&1 / &2 + n) = &2 * n + &1`] THEN
    REWRITE_TAC[REAL_OF_NUM_SUC; REAL_OF_NUM_MUL] THEN
    FIRST_X_ASSUM(MP_TAC o GEN_REWRITE_RULE I [NOT_FORALL_THM]) THEN
    DISCH_THEN(X_CHOOSE_THEN `n:num` MP_TAC) THEN
    MP_TAC(SPEC `n:num` EVEN_OR_ODD) THEN
    MESON_TAC[ODD_EXISTS; EVEN_EXISTS]]);;

let CGAMMA_REFLECTION = prove
 (`!z. cgamma(z) * cgamma(Cx(&1) - z) = Cx pi / csin(Cx pi * z)`,
  let lemma = prove
   (`!w z. (?n. integer n /\ w = Cx n) /\ (?n. integer n /\ z = Cx n) /\
           dist(w,z) < &1
           ==> w = z`,
    REPEAT GEN_TAC THEN
    REPEAT(DISCH_THEN(CONJUNCTS_THEN2 STRIP_ASSUME_TAC MP_TAC)) THEN
    ASM_REWRITE_TAC[DIST_CX] THEN ASM_MESON_TAC[REAL_EQ_INTEGERS_IMP]) in
  ABBREV_TAC
   `g = \z. if ?n. integer n /\ z = Cx n then Cx pi
            else cgamma(z) * cgamma(Cx(&1) - z) * csin(Cx pi * z)` THEN
  SUBGOAL_THEN `!z. g(z + Cx(&1)):complex = g(z)` ASSUME_TAC THENL
   [GEN_TAC THEN EXPAND_TAC "g" THEN
    MATCH_MP_TAC(MESON[] `(p <=> p') /\ a = a' /\ b = b'
                    ==> (if p then a else b) = (if p' then a' else b')`) THEN
    REWRITE_TAC[COMPLEX_RING `z + Cx(&1) = w <=> z = w - Cx(&1)`] THEN
    CONJ_TAC THENL
     [REWRITE_TAC[GSYM CX_SUB] THEN
      MESON_TAC[INTEGER_CLOSED; REAL_ARITH `(n + &1) - &1 = n`];
      GEN_REWRITE_TAC (RAND_CONV o LAND_CONV) [CGAMMA_RECURRENCE_ALT] THEN
      REWRITE_TAC[COMPLEX_RING `a - (z + a):complex = --z`] THEN
      GEN_REWRITE_TAC (LAND_CONV o RAND_CONV o LAND_CONV)
       [CGAMMA_RECURRENCE_ALT] THEN
      REWRITE_TAC[COMPLEX_ADD_LDISTRIB; COMPLEX_MUL_RID; CSIN_ADD] THEN
      REWRITE_TAC[GSYM CX_SIN; GSYM CX_COS; SIN_PI; COS_PI] THEN
      REWRITE_TAC[COMPLEX_RING `--z + Cx(&1) = Cx(&1) - z`] THEN
      REWRITE_TAC[complex_div; COMPLEX_INV_NEG; COMPLEX_MUL_AC;
                  CX_NEG; COMPLEX_MUL_LID; COMPLEX_MUL_LZERO] THEN
      REWRITE_TAC[COMPLEX_ADD_RID; COMPLEX_MUL_LNEG; COMPLEX_MUL_RNEG] THEN
      REWRITE_TAC[COMPLEX_MUL_LID; COMPLEX_NEG_NEG] THEN
      REWRITE_TAC[COMPLEX_MUL_AC]];
    ALL_TAC] THEN
  SUBGOAL_THEN `!n z. integer n ==> g(z + Cx n):complex = g z` ASSUME_TAC THENL
   [SUBGOAL_THEN `!n z. g(z + Cx(&n)):complex = g z` ASSUME_TAC THENL
     [INDUCT_TAC THEN
      ASM_REWRITE_TAC[GSYM REAL_OF_NUM_SUC; COMPLEX_ADD_RID] THEN
      ASM_REWRITE_TAC[CX_ADD; COMPLEX_ADD_ASSOC];
      REWRITE_TAC[integer] THEN REPEAT STRIP_TAC THEN
      FIRST_X_ASSUM(DISJ_CASES_THEN SUBST1_TAC o MATCH_MP
       (REAL_ARITH `abs x = a ==> x = a \/ x = --a`)) THEN
      ASM_REWRITE_TAC[CX_NEG; GSYM complex_sub] THEN
      ASM_MESON_TAC[COMPLEX_RING `(z - w) + w:complex = z`]];
    ALL_TAC] THEN
  SUBGOAL_THEN `!z. ~(?n. integer n /\ z = Cx n)
                    ==> g complex_differentiable (at z)`
  ASSUME_TAC THENL
   [REPEAT STRIP_TAC THEN MATCH_MP_TAC COMPLEX_DIFFERENTIABLE_TRANSFORM_AT THEN
    EXISTS_TAC `\z. cgamma z * cgamma(Cx(&1) - z) * csin(Cx pi * z)` THEN
    SUBGOAL_THEN `closed {z | ?n. integer n /\ z = Cx n}` MP_TAC THENL
     [MATCH_MP_TAC DISCRETE_IMP_CLOSED THEN EXISTS_TAC `&1` THEN
      REWRITE_TAC[REAL_LT_01; IN_ELIM_THM] THEN MESON_TAC[lemma; dist];
      REWRITE_TAC[closed; OPEN_CONTAINS_BALL; IN_UNIV; IN_DIFF]] THEN
    DISCH_THEN(MP_TAC o SPEC `z:complex`) THEN
    ASM_REWRITE_TAC[IN_ELIM_THM] THEN MATCH_MP_TAC MONO_EXISTS THEN
    REWRITE_TAC[SUBSET; IN_BALL; IN_DIFF; IN_ELIM_THM; IN_UNIV] THEN
    X_GEN_TAC `e:real` THEN STRIP_TAC THEN ASM_REWRITE_TAC[] THEN
    ONCE_REWRITE_TAC[DIST_SYM] THEN EXPAND_TAC "g" THEN REWRITE_TAC[] THEN
    ASM_SIMP_TAC[] THEN
    REPEAT(MATCH_MP_TAC COMPLEX_DIFFERENTIABLE_MUL_AT THEN CONJ_TAC) THENL
     [ALL_TAC;
      GEN_REWRITE_TAC LAND_CONV [GSYM o_DEF] THEN
      MATCH_MP_TAC COMPLEX_DIFFERENTIABLE_COMPOSE_AT THEN
      CONJ_TAC;
      ALL_TAC] THEN
     (MATCH_MP_TAC COMPLEX_DIFFERENTIABLE_AT_CGAMMA ORELSE
      COMPLEX_DIFFERENTIABLE_TAC) THEN
     REWRITE_TAC[COMPLEX_RING `z + a:complex = b <=> z = b - a`;
                 COMPLEX_RING `Cx(&1) - z = a - b <=> z = b - a + Cx(&1)`] THEN
     REWRITE_TAC[GSYM CX_ADD; GSYM CX_SUB] THEN ASM_MESON_TAC[INTEGER_CLOSED];
     ALL_TAC] THEN
  SUBGOAL_THEN `g complex_differentiable at (Cx(&0))` ASSUME_TAC THENL
   [MATCH_MP_TAC HOLOMORPHIC_ON_IMP_DIFFERENTIABLE_AT THEN
    EXISTS_TAC `ball(Cx(&0),&1)` THEN
    REWRITE_TAC[OPEN_BALL; CENTRE_IN_BALL; REAL_LT_01] THEN
    MATCH_MP_TAC NO_ISOLATED_SINGULARITY THEN EXISTS_TAC `{Cx(&0)}` THEN
    REWRITE_TAC[OPEN_BALL; FINITE_SING] THEN CONJ_TAC THENL
     [ALL_TAC;
      REWRITE_TAC[HOLOMORPHIC_ON_DIFFERENTIABLE; IN_DIFF; IN_SING] THEN
      X_GEN_TAC `z:complex` THEN REWRITE_TAC[COMPLEX_IN_BALL_0] THEN
      STRIP_TAC THEN MATCH_MP_TAC COMPLEX_DIFFERENTIABLE_AT_WITHIN THEN
      FIRST_X_ASSUM MATCH_MP_TAC THEN UNDISCH_TAC `~(z = Cx(&0))` THEN
      REWRITE_TAC[CONTRAPOS_THM] THEN DISCH_TAC THEN MATCH_MP_TAC lemma THEN
      ASM_REWRITE_TAC[dist; COMPLEX_SUB_RZERO] THEN
      MESON_TAC[INTEGER_CLOSED]] THEN
    REWRITE_TAC[CONTINUOUS_ON_EQ_CONTINUOUS_WITHIN] THEN
    X_GEN_TAC `z:complex` THEN REWRITE_TAC[COMPLEX_IN_BALL_0] THEN
    DISCH_TAC THEN MATCH_MP_TAC CONTINUOUS_AT_WITHIN THEN
    ASM_CASES_TAC `z = Cx(&0)` THENL
     [ALL_TAC;
      MATCH_MP_TAC COMPLEX_DIFFERENTIABLE_IMP_CONTINUOUS_AT THEN
      FIRST_X_ASSUM MATCH_MP_TAC THEN UNDISCH_TAC `~(z = Cx(&0))` THEN
      REWRITE_TAC[CONTRAPOS_THM] THEN DISCH_TAC THEN MATCH_MP_TAC lemma THEN
      ASM_REWRITE_TAC[dist; COMPLEX_SUB_RZERO] THEN
      MESON_TAC[INTEGER_CLOSED]] THEN
    FIRST_X_ASSUM SUBST_ALL_TAC THEN REWRITE_TAC[CONTINUOUS_AT] THEN
    EXPAND_TAC "g" THEN
    REWRITE_TAC[MESON[INTEGER_CLOSED] `?n. integer n /\ Cx(&0) = Cx(n)`] THEN
    MATCH_MP_TAC LIM_TRANSFORM_WITHIN_OPEN THEN
    EXISTS_TAC `\z. Cx pi * (z * cgamma(z)) * cgamma(Cx(&1) - z) *
                    csin(Cx pi * z) / (Cx pi * z)` THEN
    EXISTS_TAC `ball(Cx(&0),&1)` THEN
    REWRITE_TAC[OPEN_BALL; CENTRE_IN_BALL; REAL_LT_01] THEN CONJ_TAC THENL
     [X_GEN_TAC `w:complex` THEN REWRITE_TAC[COMPLEX_IN_BALL_0] THEN
      STRIP_TAC THEN COND_CASES_TAC THENL
       [UNDISCH_TAC `~(w = Cx(&0))` THEN
        MATCH_MP_TAC(TAUT `p ==> ~p ==> q`) THEN MATCH_MP_TAC lemma THEN
        ASM_REWRITE_TAC[dist; COMPLEX_SUB_RZERO] THEN
        MESON_TAC[INTEGER_CLOSED];
        UNDISCH_TAC `~(w = Cx(&0))` THEN MP_TAC PI_NZ THEN
        REWRITE_TAC[GSYM CX_INJ] THEN CONV_TAC COMPLEX_FIELD];
      GEN_REWRITE_TAC LAND_CONV [COMPLEX_RING
       `p = p * Cx(&1) * Cx(&1) * Cx(&1)`] THEN
      MATCH_MP_TAC LIM_COMPLEX_LMUL THEN MATCH_MP_TAC LIM_COMPLEX_MUL THEN
      CONJ_TAC THENL
       [MP_TAC(SPEC `0` CGAMMA_SIMPLE_POLES) THEN
        REWRITE_TAC[FACT; COMPLEX_DIV_1; complex_pow; COMPLEX_ADD_RID] THEN
        REWRITE_TAC[COMPLEX_NEG_0];
        ALL_TAC] THEN
      MATCH_MP_TAC LIM_COMPLEX_MUL THEN CONJ_TAC THENL
       [SUBGOAL_THEN `(cgamma o (\z. Cx(&1) - z)) continuous (at (Cx(&0)))`
        MP_TAC THENL
         [MATCH_MP_TAC CONTINUOUS_AT_COMPOSE THEN
          CONJ_TAC THENL [CONTINUOUS_TAC; ALL_TAC] THEN
          MATCH_MP_TAC CONTINUOUS_AT_CGAMMA THEN
          REWRITE_TAC[GSYM CX_ADD; GSYM CX_SUB; CX_INJ] THEN
          REAL_ARITH_TAC;
          REWRITE_TAC[CONTINUOUS_AT; o_DEF; COMPLEX_SUB_RZERO; CGAMMA_1]];
        SUBGOAL_THEN
         `(\z. csin(Cx pi * z) / (Cx pi * z)) =
          (\z. csin z / z) o (\w. Cx pi * w)`
        SUBST1_TAC THENL [REWRITE_TAC[o_DEF]; ALL_TAC] THEN
        MATCH_MP_TAC LIM_COMPOSE_AT THEN
        EXISTS_TAC `Cx(&0)` THEN REWRITE_TAC[LIM_CSIN_OVER_X] THEN
        SIMP_TAC[EVENTUALLY_AT; COMPLEX_ENTIRE; CX_INJ; PI_NZ;
                 GSYM DIST_NZ] THEN
        CONJ_TAC THENL [ALL_TAC; MESON_TAC[REAL_LT_01]] THEN
        LIM_TAC THEN CONV_TAC COMPLEX_RING]];
    ALL_TAC] THEN
  SUBGOAL_THEN `g holomorphic_on (:complex)` ASSUME_TAC THENL
   [REWRITE_TAC[HOLOMORPHIC_ON_DIFFERENTIABLE; WITHIN_UNIV; IN_UNIV] THEN
    X_GEN_TAC `z:complex` THEN ASM_CASES_TAC `?n. integer n /\ z = Cx n` THEN
    ASM_SIMP_TAC[] THEN
    FIRST_X_ASSUM(X_CHOOSE_THEN `n:real` STRIP_ASSUME_TAC) THEN
    MATCH_MP_TAC COMPLEX_DIFFERENTIABLE_TRANSFORM_AT THEN
    EXISTS_TAC `(g:complex->complex) o (\z. z - Cx n)` THEN
    EXISTS_TAC `&1` THEN REWRITE_TAC[REAL_LT_01] THEN CONJ_TAC THENL
     [REWRITE_TAC[o_THM] THEN
      ASM_MESON_TAC[COMPLEX_RING `(z - w) + w:complex = z`];
      MATCH_MP_TAC COMPLEX_DIFFERENTIABLE_COMPOSE_AT THEN
      ASM_REWRITE_TAC[COMPLEX_SUB_REFL] THEN
      COMPLEX_DIFFERENTIABLE_TAC];
    ALL_TAC] THEN
  SUBGOAL_THEN
   `!z. g(z / Cx(&2)) * g((z + Cx(&1)) / Cx(&2)) =
        cgamma(Cx(&1 / &2)) pow 2 * g(z)`
  ASSUME_TAC THENL
   [MATCH_MP_TAC(SET_RULE
     `!s. s = UNIV /\ (!x. x IN s ==> P x) ==> !x. P x`) THEN
    EXISTS_TAC `closure {z | !n. ~(integer n /\ z = Cx n)}` THEN
    CONJ_TAC THENL
     [REWRITE_TAC[CLOSURE_INTERIOR] THEN
      MATCH_MP_TAC(SET_RULE `s = {} ==> t DIFF s = t`) THEN
      MATCH_MP_TAC COUNTABLE_EMPTY_INTERIOR THEN
      MATCH_MP_TAC DISCRETE_IMP_COUNTABLE THEN
      REWRITE_TAC[IN_DIFF; IN_UNIV; IN_ELIM_THM] THEN
      REPEAT STRIP_TAC THEN EXISTS_TAC `&1` THEN
      REWRITE_TAC[GSYM REAL_NOT_LT; GSYM dist; REAL_LT_01] THEN
      ASM_MESON_TAC[lemma];
      ALL_TAC] THEN
    ONCE_REWRITE_TAC[GSYM COMPLEX_SUB_0] THEN REWRITE_TAC[GSYM IN_SING] THEN
    MATCH_MP_TAC FORALL_IN_CLOSURE THEN REWRITE_TAC[CLOSED_SING] THEN
    CONJ_TAC THENL
     [MATCH_MP_TAC CONTINUOUS_ON_SUB THEN CONJ_TAC THEN
      MATCH_MP_TAC CONTINUOUS_ON_COMPLEX_MUL THEN CONJ_TAC THEN
      REWRITE_TAC[CONTINUOUS_ON_CONST] THEN
      TRY(GEN_REWRITE_TAC LAND_CONV [GSYM o_DEF] THEN
          MATCH_MP_TAC CONTINUOUS_ON_COMPOSE THEN CONJ_TAC) THEN
      FIRST_ASSUM(MP_TAC o MATCH_MP HOLOMORPHIC_ON_IMP_CONTINUOUS_ON) THEN
      REWRITE_TAC[CONTINUOUS_ON_EQ_CONTINUOUS_WITHIN] THEN
      REWRITE_TAC[IN_UNIV; WITHIN_UNIV] THEN REPEAT STRIP_TAC THEN
      MATCH_MP_TAC CONTINUOUS_AT_WITHIN THEN ASM_REWRITE_TAC[] THEN
      REWRITE_TAC[complex_div] THEN MATCH_MP_TAC CONTINUOUS_COMPLEX_MUL THEN
      CONJ_TAC THEN CONTINUOUS_TAC;
      ALL_TAC] THEN
    X_GEN_TAC `z:complex` THEN
    REWRITE_TAC[IN_ELIM_THM; IN_SING; COMPLEX_SUB_0] THEN DISCH_TAC THEN
    EXPAND_TAC "g" THEN REWRITE_TAC[] THEN
    REWRITE_TAC[COMPLEX_RING `z / Cx(&2) = w <=> z = Cx(&2) * w`] THEN
    REWRITE_TAC[COMPLEX_RING `z + Cx(&1) = w <=> z = w - Cx(&1)`] THEN
    REWRITE_TAC[GSYM CX_MUL; GSYM CX_SUB] THEN
    REPEAT(COND_CASES_TAC THENL [ASM_MESON_TAC[INTEGER_CLOSED]; ALL_TAC]) THEN
    REWRITE_TAC[COMPLEX_RING
     `(a * b * c) * (d * e * f):complex =
      (a * d) * (e * b) * (c * f)`] THEN
    REWRITE_TAC[COMPLEX_RING
     `Cx(&1) - (z + Cx(&1)) / Cx(&2) = (Cx(&1) - z) / Cx(&2)`] THEN
    REWRITE_TAC[COMPLEX_RING
     `(z + Cx(&1)) / Cx(&2) = z / Cx(&2) + Cx(&1) / Cx(&2) /\
      Cx(&1) - z / Cx(&2) = ((Cx(&1) - z) / Cx(&2)) + Cx(&1) / Cx(&2)`] THEN
    REWRITE_TAC[CGAMMA_LEGENDRE_ALT] THEN
    MP_TAC(ISPEC `Cx(&1 / &2) * z * Cx pi` CSIN_DOUBLE) THEN
    MP_TAC(SPECL [`Cx(&2)`; `z:complex`; `--z:complex`] CPOW_ADD) THEN
    REWRITE_TAC[COMPLEX_ADD_RINV] THEN
    CONV_TAC(DEPTH_BINOP_CONV `==>`
     (BINOP_CONV(DEPTH_BINOP_CONV `complex_mul`
     (RAND_CONV COMPLEX_POLY_CONV)))) THEN
    REWRITE_TAC[CPOW_ADD; CX_NEG; COMPLEX_MUL_LNEG; COMPLEX_MUL_LID] THEN
    REWRITE_TAC[CPOW_N; CX_INJ; REAL_OF_NUM_EQ; ARITH_EQ; CSIN_ADD] THEN
    REWRITE_TAC[GSYM CX_MUL; GSYM CX_SIN; GSYM CX_COS; SIN_PI2; COS_PI2;
                REAL_ARITH `&1 / &2 * x = x / &2`] THEN
    CONV_TAC COMPLEX_RING;
    ALL_TAC] THEN
  SUBGOAL_THEN
   `?h. h holomorphic_on (:complex) /\
        !z. z IN (:complex) ==> g z = cexp(h z)`
  MP_TAC THENL
   [MATCH_MP_TAC(REWRITE_RULE[RIGHT_IMP_FORALL_THM; IMP_IMP]
      CONTRACTIBLE_IMP_HOLOMORPHIC_LOG) THEN
    ASM_REWRITE_TAC[CONTRACTIBLE_UNIV; IN_UNIV] THEN
    X_GEN_TAC `z:complex` THEN EXPAND_TAC "g" THEN
    COND_CASES_TAC THEN REWRITE_TAC[CX_INJ; PI_NZ] THEN
    REWRITE_TAC[CGAMMA_EQ_0; CSIN_EQ_0; COMPLEX_ENTIRE] THEN
    REWRITE_TAC[ONCE_REWRITE_RULE[REAL_MUL_SYM] CX_MUL] THEN
    REWRITE_TAC[COMPLEX_RING `a - z + b = Cx(&0) <=> z = a + b`] THEN
    REWRITE_TAC[COMPLEX_EQ_MUL_LCANCEL; CX_INJ; PI_NZ] THEN
    REWRITE_TAC[COMPLEX_RING `z + a =  Cx(&0) <=> z = --a`] THEN
    REWRITE_TAC[GSYM CX_ADD; GSYM CX_NEG] THEN ASM_MESON_TAC[INTEGER_CLOSED];
    REWRITE_TAC[IN_UNIV] THEN DISCH_THEN(STRIP_ASSUME_TAC o GSYM)] THEN
  MP_TAC(ISPECL [`h:complex->complex`; `(:complex)`]
        HOLOMORPHIC_ON_OPEN) THEN
  ASM_REWRITE_TAC[OPEN_UNIV; IN_UNIV; SKOLEM_THM; LEFT_IMP_EXISTS_THM] THEN
  X_GEN_TAC `h':complex->complex` THEN DISCH_TAC THEN
  SUBGOAL_THEN
   `!z. (h'(z / Cx(&2)) + h'((z + Cx(&1)) / Cx(&2))) / Cx(&2) = h'(z)`
  ASSUME_TAC THENL
   [X_GEN_TAC `z:complex` THEN MATCH_MP_TAC
     (COMPLEX_RING `!a. ~(a = Cx(&0)) /\ a * x = a * y ==> x = y`) THEN
    EXISTS_TAC `g(z / Cx(&2)) * g((z + Cx(&1)) / Cx(&2))` THEN
    REWRITE_TAC[COMPLEX_ENTIRE] THEN
    CONJ_TAC THENL [ASM_MESON_TAC[CEXP_NZ]; ALL_TAC] THEN
    MATCH_MP_TAC COMPLEX_DERIVATIVE_UNIQUE_AT THEN
    EXISTS_TAC `\z. g (z / Cx(&2)) * g ((z + Cx(&1)) / Cx(&2)):complex` THEN
    EXISTS_TAC `z:complex` THEN CONJ_TAC THENL
     [REWRITE_TAC[]; ASM_REWRITE_TAC[]] THEN
    FIRST_ASSUM(SUBST1_TAC o SYM o GEN_REWRITE_RULE I [GSYM FUN_EQ_THM]) THEN
    REWRITE_TAC[] THEN
    ASM GEN_COMPLEX_DIFF_TAC [] THEN
    (CONJ_TAC THENL [ASM_REWRITE_TAC[]; CONV_TAC COMPLEX_RING]);
    ALL_TAC] THEN
  MP_TAC(ISPECL [`h:complex->complex`; `h':complex->complex`; `(:complex)`]
        HOLOMORPHIC_DERIVATIVE) THEN
  ASM_REWRITE_TAC[OPEN_UNIV] THEN DISCH_TAC THEN
  MP_TAC(ISPECL [`h':complex->complex`; `(:complex)`]
        HOLOMORPHIC_ON_OPEN) THEN
  ASM_REWRITE_TAC[OPEN_UNIV; IN_UNIV; SKOLEM_THM; LEFT_IMP_EXISTS_THM] THEN
  X_GEN_TAC `h'':complex->complex` THEN DISCH_TAC THEN
  SUBGOAL_THEN
   `!z. (h''(z / Cx(&2)) + h''((z + Cx(&1)) / Cx(&2))) / Cx(&4) = h''(z)`
  ASSUME_TAC THENL
   [X_GEN_TAC `z:complex` THEN
    MATCH_MP_TAC COMPLEX_DERIVATIVE_UNIQUE_AT THEN
    EXISTS_TAC `\z. (h'(z / Cx(&2)) + h'((z + Cx(&1)) / Cx(&2))) / Cx(&2)` THEN
    EXISTS_TAC `z:complex` THEN CONJ_TAC THENL
     [REWRITE_TAC[]; ASM_REWRITE_TAC[]] THEN
    ASM GEN_COMPLEX_DIFF_TAC [] THEN
    (CONJ_TAC THENL [ASM_REWRITE_TAC[ETA_AX]; CONV_TAC COMPLEX_RING]);
    ALL_TAC] THEN
  MP_TAC(ISPECL [`h':complex->complex`; `h'':complex->complex`; `(:complex)`]
        HOLOMORPHIC_DERIVATIVE) THEN
  ASM_REWRITE_TAC[OPEN_UNIV] THEN DISCH_TAC THEN
  SUBGOAL_THEN `!z. z IN (:complex) ==> h''(z) = Cx(&0)` MP_TAC THENL
   [MATCH_MP_TAC ANALYTIC_CONTINUATION THEN
    EXISTS_TAC `cball(Cx(&0),&1)` THEN EXISTS_TAC `Cx(&0)` THEN
    ASM_REWRITE_TAC[CONNECTED_UNIV; SUBSET_UNIV; IN_UNIV] THEN
    SIMP_TAC[INTERIOR_LIMIT_POINT; INTERIOR_CBALL; CENTRE_IN_BALL;
             OPEN_UNIV; REAL_LT_01] THEN
    MP_TAC(ISPECL [`\z. norm((h'':complex->complex) z)`; `cball(Cx(&0),&1)`]
        CONTINUOUS_ATTAINS_SUP) THEN
    ASM_REWRITE_TAC[COMPLEX_IN_CBALL_0; COMPACT_CBALL; CBALL_EQ_EMPTY] THEN
    REWRITE_TAC[o_DEF] THEN CONV_TAC REAL_RAT_REDUCE_CONV THEN ANTS_TAC THENL
     [MATCH_MP_TAC CONTINUOUS_ON_LIFT_NORM_COMPOSE THEN
      ASM_MESON_TAC[HOLOMORPHIC_ON_IMP_CONTINUOUS_ON; CONTINUOUS_ON_SUBSET;
                    SUBSET_UNIV];
      DISCH_THEN(X_CHOOSE_THEN `w:complex` STRIP_ASSUME_TAC) THEN
      REPEAT STRIP_TAC THEN REWRITE_TAC[GSYM COMPLEX_VEC_0] THEN
      MATCH_MP_TAC(NORM_ARITH
       `!w:complex. norm(w) <= norm(w) / &2 /\ norm(z) <= norm(w)
                    ==> z = vec 0`) THEN
      EXISTS_TAC `(h'':complex->complex) w` THEN ASM_SIMP_TAC[] THEN
      FIRST_X_ASSUM(fun th ->
        GEN_REWRITE_TAC (LAND_CONV o RAND_CONV) [GSYM th]) THEN
      REWRITE_TAC[COMPLEX_NORM_DIV; COMPLEX_NORM_CX; REAL_ABS_NUM] THEN
      MATCH_MP_TAC(NORM_ARITH
       `norm(a) <= e /\ norm(b) <= e ==> norm(a + b) / &4 <= e / &2`) THEN
      CONJ_TAC THEN FIRST_X_ASSUM MATCH_MP_TAC THEN
      REWRITE_TAC[COMPLEX_NORM_DIV; COMPLEX_NORM_CX; REAL_ABS_NUM] THEN
      UNDISCH_TAC `norm(w:complex) <= &1` THEN
      MP_TAC(SPEC `&1` COMPLEX_NORM_CX) THEN CONV_TAC NORM_ARITH];
      REWRITE_TAC[IN_UNIV] THEN DISCH_TAC] THEN
  MP_TAC(ISPECL [`h':complex->complex`; `(:complex)`]
        HAS_COMPLEX_DERIVATIVE_ZERO_CONSTANT) THEN
  REWRITE_TAC[CONVEX_UNIV; IN_UNIV] THEN ANTS_TAC THENL
   [ASM_MESON_TAC[HAS_COMPLEX_DERIVATIVE_AT_WITHIN]; ALL_TAC] THEN
  DISCH_THEN(X_CHOOSE_TAC `a:complex`) THEN
  MP_TAC(ISPECL [`\z. (h:complex->complex) z - a * z`; `(:complex)`]
        HAS_COMPLEX_DERIVATIVE_ZERO_CONSTANT) THEN
  REWRITE_TAC[CONVEX_UNIV; IN_UNIV] THEN ANTS_TAC THENL
   [GEN_TAC THEN ASM GEN_COMPLEX_DIFF_TAC[] THEN
    ASM_REWRITE_TAC[] THEN CONV_TAC COMPLEX_RING;
    REWRITE_TAC[COMPLEX_RING `a - b:complex = c <=> a = b + c`] THEN
    DISCH_THEN(X_CHOOSE_TAC `b:complex`)] THEN
  FIRST_X_ASSUM(MP_TAC o SPECL [`&1`; `Cx(&0)`]) THEN
  MP_TAC(ASSUME `!z:complex. cexp (h z) = g z`) THEN
  DISCH_THEN(fun th -> REWRITE_TAC[GSYM th]) THEN
  REWRITE_TAC[ ASSUME`!x. (h:complex->complex) x = a * x + b`] THEN
  REWRITE_TAC[INTEGER_CLOSED; COMPLEX_ADD_LID; COMPLEX_MUL_RZERO] THEN
  REWRITE_TAC[CEXP_ADD; COMPLEX_MUL_RID] THEN
  REWRITE_TAC[COMPLEX_RING `a * b = b <=> a = Cx(&1) \/ b = Cx(&0)`] THEN
  REWRITE_TAC[CEXP_NZ; CEXP_EQ_1] THEN
  DISCH_THEN(CONJUNCTS_THEN2 ASSUME_TAC
   (X_CHOOSE_THEN `n:real` STRIP_ASSUME_TAC)) THEN
  UNDISCH_TAC `!z:complex. cexp(h z) = g z` THEN
  ASM_REWRITE_TAC[] THEN ASM_CASES_TAC `n = &0` THENL
   [SUBST1_TAC(SYM(SPEC `a:complex` COMPLEX)) THEN
    ASM_REWRITE_TAC[REAL_MUL_LZERO; REAL_MUL_RZERO; GSYM CX_DEF] THEN
    REWRITE_TAC[COMPLEX_MUL_LZERO; COMPLEX_ADD_LID] THEN
    DISCH_THEN(ASSUME_TAC o GSYM) THEN X_GEN_TAC `z:complex` THEN
    ASM_CASES_TAC `?n. integer n /\ z = Cx n` THENL
     [REWRITE_TAC[complex_div] THEN
      MATCH_MP_TAC(COMPLEX_RING
       `z = Cx(&0) /\ ((w = Cx(&0)) \/ (y = Cx(&0)))
        ==> w * y = p * z`) THEN
      REWRITE_TAC[CGAMMA_EQ_0; CSIN_EQ_0; COMPLEX_INV_EQ_0] THEN
      CONJ_TAC THENL [ASM_MESON_TAC[REAL_MUL_SYM; CX_MUL]; ALL_TAC] THEN
      REWRITE_TAC[OR_EXISTS_THM; GSYM COMPLEX_ADD_ASSOC] THEN
      REWRITE_TAC[COMPLEX_RING `c - z + n = Cx(&0) <=> z = n + c`] THEN
      FIRST_X_ASSUM(X_CHOOSE_THEN `m:real`
       (CONJUNCTS_THEN2 MP_TAC SUBST1_TAC)) THEN
      REWRITE_TAC[integer] THEN DISCH_THEN(X_CHOOSE_THEN `p:num`
        (MP_TAC o MATCH_MP (REAL_ARITH `abs x = n ==> x = n \/ x = --n`))) THEN
      DISCH_THEN(DISJ_CASES_THEN SUBST_ALL_TAC) THEN
      REWRITE_TAC[GSYM CX_ADD; CX_INJ; REAL_OF_NUM_ADD; REAL_OF_NUM_EQ;
                  REAL_ARITH `--p + n = &0 <=> p = n`] THEN
      REWRITE_TAC[EXISTS_OR_THM; GSYM EXISTS_REFL] THEN
      REWRITE_TAC[ADD_EQ_0; RIGHT_EXISTS_AND_THM; EXISTS_REFL] THEN
      MESON_TAC[num_CASES; ADD1];
      SUBGOAL_THEN `(g:complex->complex) z = g(Cx(&0))` MP_TAC THENL
       [ASM_REWRITE_TAC[]; EXPAND_TAC "g"] THEN
      ASM_REWRITE_TAC[] THEN
      COND_CASES_TAC THENL [ALL_TAC; ASM_MESON_TAC[INTEGER_CLOSED]] THEN
      MP_TAC PI_NZ THEN REWRITE_TAC[GSYM CX_INJ] THEN CONV_TAC COMPLEX_FIELD];
    DISCH_THEN(fun th ->
      MP_TAC(SPEC `Cx(inv(&4 * n))` th) THEN MP_TAC(SPEC `Cx(&0)` th)) THEN
    REWRITE_TAC[COMPLEX_MUL_RZERO; COMPLEX_ADD_LID] THEN
    SUBGOAL_THEN `g(Cx(&0)) = Cx pi` SUBST1_TAC THENL
     [ASM_MESON_TAC[INTEGER_CLOSED]; ALL_TAC] THEN
    REWRITE_TAC[CEXP_ADD] THEN DISCH_THEN SUBST1_TAC THEN
    SUBST1_TAC(SYM(SPEC `a:complex` COMPLEX)) THEN
    ASM_REWRITE_TAC[] THEN
    GEN_REWRITE_TAC (funpow 3 LAND_CONV o RAND_CONV) [complex_mul] THEN
    REWRITE_TAC[RE; IM; REAL_MUL_LZERO; IM_CX; RE_CX] THEN
    REWRITE_TAC[CEXP_COMPLEX; REAL_ADD_LID] THEN ASM_SIMP_TAC[REAL_FIELD
     `~(n = &0) ==> (&2 * n * pi) * inv(&4 * n) = pi / &2`] THEN
    REWRITE_TAC[SIN_PI2; COS_PI2; GSYM ii] THEN
    REWRITE_TAC[REAL_MUL_RZERO; REAL_SUB_REFL; REAL_EXP_0] THEN
    REWRITE_TAC[COMPLEX_MUL_LID] THEN
    MATCH_MP_TAC(TAUT `~p ==> p ==> q`) THEN EXPAND_TAC "g" THEN
    ASM_SIMP_TAC[CX_INJ; REAL_FIELD
     `~(n = &0) ==> (inv(&4 * n) = m <=> &4 * m * n = &1)`] THEN
    COND_CASES_TAC THENL
     [FIRST_X_ASSUM(X_CHOOSE_THEN `m:real` STRIP_ASSUME_TAC) THEN
      FIRST_X_ASSUM(MP_TAC o AP_TERM `abs`) THEN
      REWRITE_TAC[REAL_ABS_MUL; REAL_ABS_NUM] THEN
      REPEAT(FIRST_X_ASSUM(CHOOSE_TAC o GEN_REWRITE_RULE I [integer])) THEN
      ASM_REWRITE_TAC[REAL_OF_NUM_MUL; REAL_OF_NUM_EQ; MULT_EQ_1] THEN
      CONV_TAC NUM_REDUCE_CONV;
      MATCH_MP_TAC(MESON[] `real y /\ ~real x ==> ~(x = y)`) THEN
      SIMP_TAC[GSYM CX_SUB; GSYM CX_MUL; REAL_CX; REAL_SIN; REAL_GAMMA;
               REAL_MUL; ONCE_REWRITE_RULE[COMPLEX_MUL_SYM] REAL_MUL_CX] THEN
      REWRITE_TAC[PI_NZ; real; IM_II] THEN ARITH_TAC]]);;

let CGAMMA_HALF = prove
 (`cgamma(Cx(&1) / Cx(&2)) = Cx(sqrt pi)`,
  MP_TAC(SPEC `Cx(&1) / Cx(&2)` CGAMMA_REFLECTION) THEN
  REWRITE_TAC[COMPLEX_RING `Cx(&1) - Cx(&1) / Cx(&2) = Cx(&1) / Cx(&2)`] THEN
  REWRITE_TAC[GSYM CX_DIV; GSYM CX_MUL; GSYM CX_SIN] THEN
  REWRITE_TAC[REAL_ARITH `x * &1 / &2 = x / &2`; SIN_PI2; REAL_DIV_1] THEN
  SUBGOAL_THEN `Cx pi = Cx(sqrt pi) pow 2` SUBST1_TAC THENL
   [REWRITE_TAC[GSYM CX_POW] THEN AP_TERM_TAC THEN CONV_TAC SYM_CONV THEN
    REWRITE_TAC[SQRT_POW2; PI_POS_LE];
    REWRITE_TAC[COMPLEX_RING
     `a * a:complex = b pow 2 <=> a = b \/ a = --b`] THEN
    STRIP_TAC THEN ASM_REWRITE_TAC[] THEN
    MP_TAC(SPEC `Cx(&1 / &2)` RE_POS_CGAMMA_REAL) THEN
    ASM_REWRITE_TAC[REAL_CX; RE_CX; RE_NEG] THEN
    CONV_TAC REAL_RAT_REDUCE_CONV THEN MATCH_MP_TAC(TAUT `~p ==> p ==> q`) THEN
    REWRITE_TAC[REAL_ARITH `~(&0 <= --x) <=> &0 < x`] THEN
    MATCH_MP_TAC SQRT_POS_LT THEN REWRITE_TAC[PI_POS]]);;

let CGAMMA_LEGENDRE = prove
 (`!z. cgamma(z) * cgamma(z + Cx(&1) / Cx(&2)) =
       Cx(&2) cpow (Cx(&1) - Cx(&2) * z) * Cx(sqrt pi) * cgamma(Cx(&2) * z)`,
  REWRITE_TAC[CGAMMA_LEGENDRE_ALT; CGAMMA_HALF]);;

(* ------------------------------------------------------------------------- *)
(* Thw Weierstrass product definition.                                       *)
(* ------------------------------------------------------------------------- *)

let CGAMMA_WEIERSTRASS = prove
 (`!z. ((\n. cexp(--(Cx euler_mascheroni) * z) / z *
             cproduct(1..n) (\k. cexp(z / Cx(&k)) / (Cx(&1) + z / Cx(&k))))
       --> cgamma z) sequentially`,
  GEN_TAC THEN SIMP_TAC[complex_div; CPRODUCT_MUL; FINITE_NUMSEG] THEN
  MATCH_MP_TAC LIM_TRANSFORM_EVENTUALLY THEN
  EXISTS_TAC `\n. (cexp(--Cx euler_mascheroni * z) *
                   cproduct(1..n) (\k. cexp(z * inv(Cx(&k)))) *
                   cexp(--Cx(log(&n)) * z)) *
                  (Cx(&n) cpow z / z *
                   cproduct (1..n) (\k. inv(Cx(&1) + z * inv(Cx(&k)))))` THEN
  REWRITE_TAC[EVENTUALLY_SEQUENTIALLY] THEN CONJ_TAC THENL
   [EXISTS_TAC `1` THEN REPEAT STRIP_TAC THEN REWRITE_TAC[complex_div] THEN
    MATCH_MP_TAC(COMPLEX_RING
     `c * c' = Cx(&1) ==> (a * b * c) * (c' * d) * e = ((a * d) * b * e)`) THEN
    ASM_SIMP_TAC[cpow; CX_INJ; REAL_OF_NUM_EQ; LE_1; GSYM CEXP_ADD] THEN
    REWRITE_TAC[GSYM CEXP_0] THEN AP_TERM_TAC THEN
    ASM_SIMP_TAC[CX_LOG; REAL_OF_NUM_LT; LE_1] THEN CONV_TAC COMPLEX_RING;
    ALL_TAC] THEN
  GEN_REWRITE_TAC LAND_CONV [GSYM COMPLEX_MUL_LID] THEN
  MATCH_MP_TAC LIM_COMPLEX_MUL THEN CONJ_TAC THENL
   [SIMP_TAC[GSYM CEXP_VSUM; FINITE_NUMSEG; GSYM CEXP_ADD] THEN
    REWRITE_TAC[GSYM CEXP_0] THEN
    MATCH_MP_TAC(ISPEC `cexp` LIM_CONTINUOUS_FUNCTION) THEN
    SIMP_TAC[CONTINUOUS_AT_CEXP; VSUM_COMPLEX_LMUL; FINITE_NUMSEG] THEN
    REWRITE_TAC[COMPLEX_RING
     `--w * z + z * x + --y * z:complex = z * ((x - y) - w)`] THEN
    MATCH_MP_TAC LIM_NULL_COMPLEX_LMUL THEN
    REWRITE_TAC[GSYM LIM_NULL_COMPLEX] THEN
    REWRITE_TAC[GSYM CX_INV; VSUM_CX] THEN
    REWRITE_TAC[GSYM CX_SUB; REWRITE_RULE[o_DEF] (GSYM REALLIM_COMPLEX)] THEN
    REWRITE_TAC[EULER_MASCHERONI];
    MATCH_MP_TAC LIM_TRANSFORM_EVENTUALLY THEN EXISTS_TAC
     `\n. (Cx(&n) cpow z * Cx(&(FACT n))) /
          cproduct(0..n) (\m. z + Cx(&m))` THEN
    REWRITE_TAC[CGAMMA] THEN
    REWRITE_TAC[EVENTUALLY_SEQUENTIALLY] THEN EXISTS_TAC `1` THEN
    X_GEN_TAC `n:num` THEN DISCH_TAC THEN
    SIMP_TAC[CPRODUCT_CLAUSES_LEFT; LE_0] THEN
    REWRITE_TAC[COMPLEX_ADD_RID; ADD_CLAUSES] THEN
    REWRITE_TAC[complex_div; COMPLEX_INV_MUL; GSYM COMPLEX_MUL_ASSOC] THEN
    AP_TERM_TAC THEN GEN_REWRITE_TAC LAND_CONV
     [COMPLEX_RING `a * b * c:complex = b * a * c`] THEN
    AP_TERM_TAC THEN SIMP_TAC[CPRODUCT_INV; FINITE_NUMSEG] THEN
    GEN_REWRITE_TAC (LAND_CONV o LAND_CONV) [GSYM COMPLEX_INV_INV] THEN
    REWRITE_TAC[GSYM COMPLEX_INV_MUL] THEN AP_TERM_TAC THEN
    MATCH_MP_TAC(COMPLEX_FIELD
     `~(z = Cx(&0)) /\ z * x = y ==> inv z * y = x`) THEN
    REWRITE_TAC[CX_INJ; REAL_OF_NUM_EQ; FACT_NZ] THEN
    CONV_TAC SYM_CONV THEN SPEC_TAC(`n:num`,`p:num`) THEN
    INDUCT_TAC THEN REWRITE_TAC[FACT; CPRODUCT_CLAUSES_NUMSEG; ARITH] THEN
    REWRITE_TAC[ARITH_RULE `1 <= SUC n`; COMPLEX_MUL_LID] THEN
    ASM_REWRITE_TAC[CX_MUL; GSYM REAL_OF_NUM_MUL] THEN
    MATCH_MP_TAC(COMPLEX_RING
     `d * e:complex = c ==> (a * b) * c = (d * a) * b * e`) THEN
    MATCH_MP_TAC(COMPLEX_FIELD
     `~(p = Cx(&0)) ==> p * (Cx(&1) + z * inv p) = z + p`) THEN
    REWRITE_TAC[CX_INJ; REAL_OF_NUM_EQ; NOT_SUC]]);;

(* ------------------------------------------------------------------------- *)
(* Sometimes the reciprocal function is convenient, since it's entire.       *)
(* ------------------------------------------------------------------------- *)

let COMPLEX_DIFFERENTIABLE_AT_RECIP_CGAMMA = prove
 (`!z. (inv o cgamma) complex_differentiable (at z)`,
  GEN_TAC THEN ASM_CASES_TAC `!n. ~(z + Cx(&n) = Cx(&0))` THENL
   [MATCH_MP_TAC COMPLEX_DIFFERENTIABLE_COMPOSE_AT THEN
    ASM_SIMP_TAC[COMPLEX_DIFFERENTIABLE_AT_CGAMMA] THEN
    GEN_REWRITE_TAC LAND_CONV [GSYM ETA_AX] THEN
    MATCH_MP_TAC COMPLEX_DIFFERENTIABLE_INV_AT THEN
    REWRITE_TAC[COMPLEX_DIFFERENTIABLE_ID; CGAMMA_EQ_0] THEN
    ASM_MESON_TAC[];
    FIRST_X_ASSUM(MP_TAC o GEN_REWRITE_RULE I [NOT_FORALL_THM]) THEN
    REWRITE_TAC[COMPLEX_RING `z + w = Cx(&0) <=> z = --w`] THEN
    DISCH_THEN(X_CHOOSE_THEN `n:num` SUBST1_TAC) THEN
    REWRITE_TAC[complex_differentiable; HAS_COMPLEX_DERIVATIVE_AT] THEN
    REWRITE_TAC[o_DEF; CGAMMA_POLES; COMPLEX_INV_0; complex_div;
                COMPLEX_MUL_LZERO; COMPLEX_SUB_RZERO] THEN
    SIMP_TAC[GSYM COMPLEX_INV_MUL; COMPLEX_RING `x - --z:complex = x + z`] THEN
    EXISTS_TAC `inv(--Cx(&1) pow n / Cx(&(FACT n)))` THEN
    MATCH_MP_TAC LIM_COMPLEX_INV THEN
    REWRITE_TAC[COMPLEX_DIV_EQ_0; COMPLEX_POW_EQ_0; CX_INJ] THEN
    REWRITE_TAC[REAL_OF_NUM_EQ; FACT_NZ] THEN
    ONCE_REWRITE_TAC[COMPLEX_MUL_SYM] THEN
    REWRITE_TAC[CGAMMA_SIMPLE_POLES] THEN CONV_TAC COMPLEX_RING]);;

let COMPLEX_DIFFERENTIABLE_WITHIN_RECIP_CGAMMA = prove
 (`!z s. (inv o cgamma) complex_differentiable at z within s`,
  SIMP_TAC[COMPLEX_DIFFERENTIABLE_AT_WITHIN;
           COMPLEX_DIFFERENTIABLE_AT_RECIP_CGAMMA]);;

let HOLOMORPHIC_ON_RECIP_CGAMMA = prove
 (`!s. (inv o cgamma) holomorphic_on s`,
  REWRITE_TAC[HOLOMORPHIC_ON_DIFFERENTIABLE] THEN
  REWRITE_TAC[COMPLEX_DIFFERENTIABLE_WITHIN_RECIP_CGAMMA]);;

let CONTINUOUS_AT_RECIP_CGAMMA = prove
 (`!z. (inv o cgamma) continuous at z`,
  SIMP_TAC[COMPLEX_DIFFERENTIABLE_IMP_CONTINUOUS_AT;
           COMPLEX_DIFFERENTIABLE_AT_RECIP_CGAMMA]);;

let CONTINUOUS_WITHIN_RECIP_CGAMMA = prove
 (`!z s. (inv o cgamma) continuous at z within s`,
  SIMP_TAC[COMPLEX_DIFFERENTIABLE_IMP_CONTINUOUS_WITHIN;
           COMPLEX_DIFFERENTIABLE_WITHIN_RECIP_CGAMMA]);;

let CONTINUOUS_ON_RECIP_CGAMMA = prove
 (`!s. (inv o cgamma) continuous_on s`,
  SIMP_TAC[HOLOMORPHIC_ON_IMP_CONTINUOUS_ON; HOLOMORPHIC_ON_RECIP_CGAMMA]);;

let RECIP_CGAMMA = prove
 (`!z. ((\n. cproduct(0..n) (\m. z + Cx(&m)) /
            (Cx(&n) cpow z * Cx(&(FACT n)))) --> inv(cgamma z))
       sequentially`,
  GEN_TAC THEN ASM_CASES_TAC `!n. ~(z + Cx(&n) = Cx(&0))` THENL
   [ONCE_REWRITE_TAC[GSYM COMPLEX_INV_INV] THEN
    MATCH_MP_TAC LIM_COMPLEX_INV THEN
    REWRITE_TAC[COMPLEX_INV_INV; CGAMMA; COMPLEX_INV_DIV; CGAMMA_EQ_0] THEN
    ASM_MESON_TAC[];
    SUBGOAL_THEN `cgamma z = Cx(&0)` SUBST1_TAC THENL
     [ASM_MESON_TAC[CGAMMA_EQ_0]; REWRITE_TAC[COMPLEX_INV_0]] THEN
    MATCH_MP_TAC LIM_EVENTUALLY THEN
    REWRITE_TAC[EVENTUALLY_SEQUENTIALLY; COMPLEX_DIV_EQ_0; COMPLEX_ENTIRE] THEN
    SIMP_TAC[CPRODUCT_EQ_0; IN_NUMSEG; FINITE_NUMSEG; LE_0] THEN
    ASM_MESON_TAC[]]);;

let RECIP_CGAMMA_WEIERSTRASS = prove
 (`!n. ((\n. (z * cexp(Cx euler_mascheroni * z) *
             cproduct(1..n) (\k. (Cx(&1) + z / Cx(&k)) / cexp(z / Cx(&k)))))
        --> inv(cgamma z)) sequentially`,
  GEN_TAC THEN ASM_CASES_TAC `?n. z + Cx(&n) = Cx(&0)` THENL
   [FIRST_X_ASSUM(X_CHOOSE_TAC `N:num`) THEN
    MATCH_MP_TAC LIM_EVENTUALLY THEN REWRITE_TAC[EVENTUALLY_SEQUENTIALLY] THEN
    EXISTS_TAC `N:num` THEN X_GEN_TAC `n:num` THEN REWRITE_TAC[GE] THEN
    DISCH_TAC THEN SUBGOAL_THEN `cgamma(z) = Cx(&0)` SUBST1_TAC THENL
     [ASM_MESON_TAC[CGAMMA_EQ_0]; REWRITE_TAC[COMPLEX_INV_0]] THEN
    REWRITE_TAC[COMPLEX_ENTIRE; COMPLEX_DIV_EQ_0] THEN
    ASM_CASES_TAC `N = 0` THENL [ASM_MESON_TAC[COMPLEX_ADD_RID]; ALL_TAC] THEN
    REPEAT DISJ2_TAC THEN
    SIMP_TAC[CPRODUCT_EQ_0; FINITE_NUMSEG; IN_NUMSEG] THEN
    EXISTS_TAC `N:num` THEN ASM_SIMP_TAC[LE_1; COMPLEX_DIV_EQ_0] THEN
    DISJ1_TAC THEN MATCH_MP_TAC(COMPLEX_FIELD
     `x + n = Cx(&0) /\ ~(n = Cx(&0)) ==> Cx(&1) + x / n = Cx(&0)`) THEN
    ASM_REWRITE_TAC[CX_INJ; REAL_OF_NUM_EQ];
    GEN_REWRITE_TAC (RATOR_CONV o LAND_CONV o ABS_CONV)
     [GSYM COMPLEX_INV_INV] THEN
    MATCH_MP_TAC LIM_COMPLEX_INV THEN
    ASM_REWRITE_TAC[CGAMMA_EQ_0] THEN
    SIMP_TAC[COMPLEX_INV_MUL; GSYM CEXP_NEG; GSYM CPRODUCT_INV;
             FINITE_NUMSEG; GSYM COMPLEX_MUL_LNEG; COMPLEX_INV_DIV] THEN
    REWRITE_TAC[SIMPLE_COMPLEX_ARITH `inv z * a * b:complex = a / z * b`] THEN
    REWRITE_TAC[CGAMMA_WEIERSTRASS]]);;

(* ------------------------------------------------------------------------- *)
(* The real gamma function.                                                  *)
(* ------------------------------------------------------------------------- *)

let gamma = new_definition
 `gamma(x) = Re(cgamma(Cx x))`;;

let CX_GAMMA = prove
 (`!x. Cx(gamma x) = cgamma(Cx x)`,
    REWRITE_TAC[gamma] THEN MESON_TAC[REAL; REAL_CX; REAL_GAMMA]);;

let GAMMA = prove
 (`!x. ((\n. (&n rpow x * &(FACT n)) / product(0..n) (\m. x + &m))
        ---> gamma(x)) sequentially`,
  REWRITE_TAC[REALLIM_COMPLEX; o_DEF; CX_GAMMA; CX_DIV] THEN
  X_GEN_TAC `x:real` THEN MP_TAC(SPEC `Cx x` CGAMMA) THEN
  MATCH_MP_TAC(REWRITE_RULE[IMP_CONJ] LIM_TRANSFORM_EVENTUALLY) THEN
  REWRITE_TAC[EVENTUALLY_SEQUENTIALLY] THEN EXISTS_TAC `1` THEN
  SIMP_TAC[CX_MUL; CX_PRODUCT; FINITE_NUMSEG; CX_ADD; cpow; rpow;
           CX_INJ; REAL_OF_NUM_EQ; REAL_OF_NUM_LT; LE_1; CX_LOG; CX_EXP]);;

let GAMMA_EQ_0 = prove
 (`!x. gamma(x) = &0 <=> ?n. x + &n = &0`,
  REWRITE_TAC[GSYM CX_INJ; CX_ADD; CX_GAMMA; CGAMMA_EQ_0]);;

let REAL_DIFFERENTIABLE_AT_GAMMA = prove
 (`!x. (!n. ~(x + &n = &0)) ==> gamma real_differentiable atreal x`,
  REPEAT STRIP_TAC THEN
  GEN_REWRITE_TAC LAND_CONV [GSYM ETA_AX] THEN
  REWRITE_TAC[gamma] THEN
  MATCH_MP_TAC (REWRITE_RULE[o_DEF] REAL_DIFFERENTIABLE_FROM_COMPLEX_AT) THEN
  REWRITE_TAC[REAL_GAMMA] THEN
  MATCH_MP_TAC COMPLEX_DIFFERENTIABLE_AT_CGAMMA THEN
  ASM_REWRITE_TAC[GSYM CX_ADD; CX_INJ]);;

let REAL_DIFFERENTIABLE_WITHIN_GAMMA = prove
 (`!x s. (!n. ~(x + &n = &0)) ==> gamma real_differentiable atreal x within s`,
  SIMP_TAC[REAL_DIFFERENTIABLE_AT_GAMMA;
           REAL_DIFFERENTIABLE_ATREAL_WITHIN]);;

let REAL_DIFFERENTIABLE_ON_GAMMA = prove
 (`!s. s SUBSET {x | !n. ~(x + &n = &0)} ==> gamma real_differentiable_on s`,
  SIMP_TAC[SUBSET; IN_ELIM_THM; REAL_DIFFERENTIABLE_ON_DIFFERENTIABLE;
           REAL_DIFFERENTIABLE_WITHIN_GAMMA]);;

let REAL_CONTINUOUS_ATREAL_GAMMA = prove
 (`!x. (!n. ~(x + &n = &0)) ==> gamma real_continuous atreal x`,
  REPEAT STRIP_TAC THEN REWRITE_TAC[REAL_COMPLEX_CONTINUOUS_ATREAL] THEN
  REWRITE_TAC[o_DEF; CX_GAMMA] THEN
  GEN_REWRITE_TAC LAND_CONV [GSYM o_DEF] THEN
  MATCH_MP_TAC CONTINUOUS_WITHIN_COMPOSE THEN
  SIMP_TAC[LINEAR_CONTINUOUS_WITHIN; REWRITE_RULE[o_DEF] LINEAR_CX_RE] THEN
  MATCH_MP_TAC CONTINUOUS_AT_WITHIN THEN
  MATCH_MP_TAC CONTINUOUS_AT_CGAMMA THEN
  ASM_REWRITE_TAC[GSYM CX_ADD; RE_CX; CX_INJ]);;

let REAL_CONTINUOUS_WITHIN_GAMMA = prove
 (`!x s. (!n. ~(x + &n = &0)) ==> gamma real_continuous atreal x within s`,
  SIMP_TAC[REAL_CONTINUOUS_ATREAL_WITHINREAL; REAL_CONTINUOUS_ATREAL_GAMMA]);;

let REAL_CONTINUOUS_ON_GAMMA = prove
 (`!s. s SUBSET {x | !n. ~(x + &n = &0)} ==> gamma real_continuous_on s`,
  SIMP_TAC[SUBSET; IN_ELIM_THM; REAL_CONTINUOUS_ON_EQ_CONTINUOUS_WITHIN;
           REAL_CONTINUOUS_WITHIN_GAMMA]);;

let REAL_DIFFERENTIABLE_ATREAL_RECIP_GAMMA = prove
 (`!x. (inv o gamma) real_differentiable (atreal x)`,
  REPEAT STRIP_TAC THEN
  REWRITE_TAC[o_DEF; gamma] THEN
  MATCH_MP_TAC REAL_DIFFERENTIABLE_TRANSFORM_ATREAL THEN
  EXISTS_TAC `\x. Re(inv(cgamma(Cx x)))` THEN
  EXISTS_TAC `&1` THEN REWRITE_TAC[REAL_LT_01] THEN CONJ_TAC THENL
   [REWRITE_TAC[GSYM CX_GAMMA; GSYM CX_INV; RE_CX];
    MATCH_MP_TAC (REWRITE_RULE[o_DEF] REAL_DIFFERENTIABLE_FROM_COMPLEX_AT) THEN
    SIMP_TAC[REAL_GAMMA; REAL_INV; GSYM o_DEF] THEN
    REWRITE_TAC[COMPLEX_DIFFERENTIABLE_AT_RECIP_CGAMMA]]);;

let REAL_DIFFERENTIABLE_WITHIN_RECIP_GAMMA = prove
 (`!x s. (inv o gamma) real_differentiable atreal x within s`,
  SIMP_TAC[REAL_DIFFERENTIABLE_ATREAL_WITHIN;
           REAL_DIFFERENTIABLE_ATREAL_RECIP_GAMMA]);;

let REAL_DIFFERENTIABLE_ON_RECIP_GAMMA = prove
 (`!s. (inv o gamma) real_differentiable_on s`,
  REWRITE_TAC[REAL_DIFFERENTIABLE_ON_DIFFERENTIABLE;
              REAL_DIFFERENTIABLE_WITHIN_RECIP_GAMMA]);;

let REAL_CONTINUOUS_ATREAL_RECIP_GAMMA = prove
 (`!x. (inv o gamma) real_continuous atreal x`,
  SIMP_TAC[REAL_DIFFERENTIABLE_IMP_CONTINUOUS_ATREAL;
           REAL_DIFFERENTIABLE_ATREAL_RECIP_GAMMA]);;

let REAL_CONTINUOUS_WITHINREAL_RECIP_GAMMA = prove
 (`!x s. (inv o gamma) real_continuous atreal x within s`,
  SIMP_TAC[REAL_CONTINUOUS_ATREAL_WITHINREAL;
           REAL_CONTINUOUS_ATREAL_RECIP_GAMMA]);;

let REAL_CONTINUOUS_ON_RECIP_GAMMA = prove
 (`!s. (inv o gamma) real_continuous_on s`,
  REWRITE_TAC[REAL_CONTINUOUS_ON_EQ_CONTINUOUS_WITHIN;
              REAL_CONTINUOUS_WITHINREAL_RECIP_GAMMA]);;

let GAMMA_RECURRENCE_ALT = prove
 (`!x. gamma(x) = gamma(x + &1) / x`,
  REWRITE_TAC[GSYM CX_INJ; CX_DIV; CX_GAMMA; CX_ADD] THEN
  REWRITE_TAC[GSYM CGAMMA_RECURRENCE_ALT]);;

let GAMMA_1 = prove
 (`gamma(&1) = &1`,
  REWRITE_TAC[GSYM CX_INJ; CX_GAMMA; CGAMMA_1]);;

let GAMMA_RECURRENCE = prove
 (`!x. gamma(x + &1) = if x = &0 then &1 else x * gamma(x)`,
  REWRITE_TAC[GSYM CX_INJ; CX_GAMMA] THEN
  GEN_TAC THEN GEN_REWRITE_TAC RAND_CONV [COND_RAND] THEN
  REWRITE_TAC[CX_MUL; CX_GAMMA; CX_ADD; GSYM CGAMMA_RECURRENCE]);;

let GAMMA_FACT = prove
 (`!n. gamma(&(n + 1)) = &(FACT n)`,
  REWRITE_TAC[GSYM CX_INJ; CX_GAMMA; CGAMMA_FACT]);;

let GAMMA_POLES = prove
 (`!n. gamma(--(&n)) = &0`,
  REWRITE_TAC[GSYM CX_INJ; CX_GAMMA; CGAMMA_POLES; CX_NEG]);;

let GAMMA_SIMPLE_POLES = prove
 (`!n. ((\x. (x + &n) * gamma x) ---> -- &1 pow n / &(FACT n))
       (atreal(-- &n))`,
  REWRITE_TAC[REALLIM_COMPLEX; o_DEF; LIM_ATREAL_ATCOMPLEX] THEN
  GEN_TAC THEN
  REWRITE_TAC[CX_MUL; CX_ADD; CX_GAMMA; CX_DIV; CX_POW; CX_NEG] THEN
  SUBGOAL_THEN
   `(\z. (Cx(Re z) + Cx(&n)) * cgamma(Cx(Re z))) =
    (\z. (z + Cx(&n)) * cgamma z) o (Cx o Re)`
  SUBST1_TAC THENL [REWRITE_TAC[o_DEF]; ALL_TAC] THEN
  MATCH_MP_TAC LIM_COMPOSE_WITHIN THEN
  MAP_EVERY EXISTS_TAC [`(:complex)`; `--Cx(&n)`] THEN
  REWRITE_TAC[CGAMMA_SIMPLE_POLES; WITHIN_UNIV] THEN
  REWRITE_TAC[EVENTUALLY_WITHIN; o_DEF; IN_UNIV; GSYM DIST_NZ] THEN
  REWRITE_TAC[real; IN; GSYM CX_NEG; CX_INJ] THEN
  REWRITE_TAC[COMPLEX_EQ; RE_CX; IM_CX] THEN
  CONJ_TAC THENL [ALL_TAC; MESON_TAC[REAL_LT_01]] THEN
  MATCH_MP_TAC LIM_AT_WITHIN THEN LIM_TAC THEN REWRITE_TAC[RE_CX] THEN
  MATCH_MP_TAC LINEAR_CONTINUOUS_AT THEN
  REWRITE_TAC[REWRITE_RULE[o_DEF] LINEAR_CX_RE]);;

let GAMMA_POS_LE = prove
 (`!x. &0 <= x ==> &0 <= gamma x`,
  SIMP_TAC[gamma; RE_POS_CGAMMA_REAL; RE_CX; REAL_CX]);;

let GAMMA_POS_LT = prove
 (`!x. &0 < x ==> &0 < gamma x`,
  SIMP_TAC[REAL_ARITH `&0 < x <=> &0 <= x /\ ~(x = &0)`; GAMMA_POS_LE] THEN
  REWRITE_TAC[GAMMA_EQ_0] THEN REAL_ARITH_TAC);;

let GAMMA_LEGENDRE_ALT = prove
 (`!x. gamma(x) * gamma(x + &1 / &2) =
       &2 rpow (&1 - &2 * x) *  gamma(&1 / &2) * gamma(&2 * x)`,
  REWRITE_TAC[GSYM CX_INJ; CX_GAMMA; CX_ADD;
              CX_DIV; CGAMMA_LEGENDRE_ALT; CX_MUL] THEN
  REWRITE_TAC[cpow; rpow; CX_INJ; REAL_OF_NUM_LT; REAL_OF_NUM_EQ; ARITH] THEN
  SIMP_TAC[CX_MUL; CX_EXP; CX_SUB; CX_LOG; REAL_OF_NUM_LT; ARITH]);;

let GAMMA_REFLECTION = prove
 (`!x. gamma(x) * gamma(&1 - x) = pi / sin(pi * x)`,
  SIMP_TAC[GSYM CX_INJ; CX_MUL; CX_DIV; CX_GAMMA; CX_SIN; CX_SUB] THEN
  REWRITE_TAC[CGAMMA_REFLECTION]);;

let GAMMA_HALF = prove
 (`gamma(&1 / &2) = sqrt pi`,
  REWRITE_TAC[GSYM CX_INJ; CX_DIV; CX_GAMMA; CGAMMA_HALF]);;

let GAMMA_LEGENDRE = prove
 (`!x. gamma(x) * gamma(x + &1 / &2) =
       &2 rpow (&1 - &2 * x) * sqrt pi * gamma(&2 * x)`,
  REWRITE_TAC[GAMMA_LEGENDRE_ALT; GAMMA_HALF]);;

let GAMMA_WEIERSTRASS = prove
 (`!x. ((\n. exp(--(euler_mascheroni) * x) / x *
             product(1..n) (\k. exp(x / &k) / (&1 + x / &k)))
       ---> gamma x) sequentially`,
  REWRITE_TAC[REALLIM_COMPLEX; o_DEF; CX_GAMMA; CX_DIV] THEN
  X_GEN_TAC `x:real` THEN MP_TAC(SPEC `Cx x` CGAMMA_WEIERSTRASS) THEN
  MATCH_MP_TAC(REWRITE_RULE[IMP_CONJ] LIM_TRANSFORM_EVENTUALLY) THEN
  REWRITE_TAC[EVENTUALLY_SEQUENTIALLY] THEN EXISTS_TAC `1` THEN
  SIMP_TAC[CX_MUL; CX_DIV; CX_EXP; CX_NEG; CX_PRODUCT; FINITE_NUMSEG;
           CX_ADD]);;

(* ------------------------------------------------------------------------- *)
(* Characterization of the real gamma function using log-convexity.          *)
(* ------------------------------------------------------------------------- *)

let REAL_LOG_CONVEX_GAMMA = prove
 (`!s. (!x. x IN s ==> &0 < x) ==> gamma real_log_convex_on s`,
  REPEAT STRIP_TAC THEN MATCH_MP_TAC REAL_LOG_CONVEX_ON_SUBSET THEN
  EXISTS_TAC `{x | &0 < x}` THEN
  ASM_REWRITE_TAC[SUBSET; IN_ELIM_THM] THEN POP_ASSUM(K ALL_TAC) THEN
  MATCH_MP_TAC(ISPEC `sequentially` REAL_LOG_CONVEX_LIM) THEN
  EXISTS_TAC `\n x. (&n rpow x * &(FACT n)) / product(0..n) (\m. x + &m)` THEN
  REWRITE_TAC[GAMMA; TRIVIAL_LIMIT_SEQUENTIALLY; EVENTUALLY_SEQUENTIALLY] THEN
  EXISTS_TAC `1` THEN X_GEN_TAC `n:num` THEN DISCH_TAC THEN
  REWRITE_TAC[real_div; GSYM REAL_MUL_ASSOC] THEN
  REPEAT(MATCH_MP_TAC REAL_LOG_CONVEX_MUL THEN CONJ_TAC) THEN
  SIMP_TAC[REAL_LOG_CONVEX_CONST; REAL_POS] THEN
  ASM_SIMP_TAC[REAL_LOG_CONVEX_RPOW_RIGHT; REAL_OF_NUM_LT; LE_1] THEN
  SIMP_TAC[GSYM PRODUCT_INV; FINITE_NUMSEG] THEN
  MATCH_MP_TAC REAL_LOG_CONVEX_PRODUCT THEN
  REWRITE_TAC[FINITE_NUMSEG; IN_NUMSEG] THEN X_GEN_TAC `k:num` THEN
  STRIP_TAC THEN
  MATCH_MP_TAC MIDPOINT_REAL_LOG_CONVEX THEN
  ASM_SIMP_TAC[IN_ELIM_THM; REAL_LT_INV_EQ; REAL_LTE_ADD; REAL_POS] THEN
  REPEAT CONJ_TAC THENL
   [MATCH_MP_TAC REAL_DIFFERENTIABLE_ON_IMP_REAL_CONTINUOUS_ON THEN
    REWRITE_TAC[REAL_DIFFERENTIABLE_ON_DIFFERENTIABLE; IN_ELIM_THM] THEN
    REPEAT STRIP_TAC THEN REAL_DIFFERENTIABLE_TAC THEN ASM_REAL_ARITH_TAC;
    REWRITE_TAC[is_realinterval; IN_ELIM_THM] THEN REAL_ARITH_TAC;
    REPEAT STRIP_TAC THEN REWRITE_TAC[GSYM REAL_INV_MUL; REAL_POW_INV] THEN
    MATCH_MP_TAC REAL_LE_INV2 THEN
    ASM_SIMP_TAC[REAL_LT_MUL; REAL_LTE_ADD; REAL_POS] THEN
    REWRITE_TAC[REAL_LE_POW_2; REAL_ARITH
     `(x + &k) * (y + &k) <= ((x + y) / &2 + &k) pow 2 <=>
      &0 <= (x - y) pow 2`]]);;

let GAMMA_REAL_LOG_CONVEX_UNIQUE = prove
 (`!f:real->real.
        f(&1) = &1 /\ (!x. &0 < x ==> f(x + &1) = x * f(x)) /\
        (!x. &0 < x ==> &0 < f x) /\ f real_log_convex_on {x | &0 < x}
        ==> !x. &0 < x ==> f x = gamma x`,
  GEN_TAC THEN STRIP_TAC THEN
  SUBGOAL_THEN `!x. &0 < x /\ x <= &1 ==> f x = gamma x` ASSUME_TAC THENL
   [ALL_TAC;
    SUBGOAL_THEN
     `!y. &0 < y /\ y <= &1 ==> !n. f(&n + y) = gamma(&n + y)`
    ASSUME_TAC THENL
     [GEN_TAC THEN STRIP_TAC THEN
      INDUCT_TAC THEN ASM_SIMP_TAC[REAL_ADD_LID] THEN
      REWRITE_TAC[GSYM REAL_OF_NUM_SUC] THEN
      REWRITE_TAC[REAL_ARITH `(n + &1) + x = (n + x) + &1`] THEN
      ASM_SIMP_TAC[GAMMA_RECURRENCE; REAL_LET_ADD; REAL_POS] THEN
      ASM_REAL_ARITH_TAC;
      X_GEN_TAC `x:real` THEN DISCH_TAC THEN
      MP_TAC(SPEC `x:real` FLOOR_POS) THEN ASM_SIMP_TAC[REAL_LT_IMP_LE] THEN
      DISCH_THEN(X_CHOOSE_TAC `n:num`) THEN
      MP_TAC(SPEC `x:real` FLOOR_FRAC) THEN ASM_REWRITE_TAC[] THEN
      ASM_CASES_TAC `frac x = &0` THEN ASM_REWRITE_TAC[REAL_LT_LE] THENL
       [ASM_CASES_TAC `n = 0` THEN
        ASM_SIMP_TAC[REAL_ADD_RID; REAL_LT_IMP_NZ] THEN
        SUBGOAL_THEN `&(n - 1) + &1 = &n`
         (fun th -> ASM_MESON_TAC[th; REAL_LE_REFL; REAL_LT_01]) THEN
        ASM_SIMP_TAC[GSYM REAL_OF_NUM_SUB; LE_1] THEN REAL_ARITH_TAC;
        STRIP_TAC THEN FIRST_X_ASSUM(MP_TAC o SPEC `frac x`) THEN
        ANTS_TAC THENL [ASM_REAL_ARITH_TAC; ASM_MESON_TAC[]]]]] THEN
  X_GEN_TAC `x:real` THEN STRIP_TAC THEN
  ASM_CASES_TAC `x = &1` THEN ASM_REWRITE_TAC[GAMMA_1] THEN
  SUBGOAL_THEN `&0 < x` ASSUME_TAC THENL [ASM_REAL_ARITH_TAC; ALL_TAC] THEN
  MATCH_MP_TAC(REAL_FIELD `&0 < g /\ f / g = &1 ==> f = g`) THEN
  ASM_SIMP_TAC[GAMMA_POS_LT] THEN
  MATCH_MP_TAC(ISPEC `sequentially` REALLIM_UNIQUE) THEN
  EXISTS_TAC
   `\n. f(x) / ((&n rpow x * &(FACT n)) / product (0..n) (\m. x + &m))` THEN
  REWRITE_TAC[TRIVIAL_LIMIT_SEQUENTIALLY] THEN
  ASM_SIMP_TAC[REALLIM_DIV; REALLIM_CONST; GAMMA_POS_LT;
               GAMMA; REAL_LT_IMP_NZ] THEN
  ONCE_REWRITE_TAC[REALLIM_NULL] THEN
  MATCH_MP_TAC REALLIM_NULL_COMPARISON THEN
  EXISTS_TAC `\n. x * inv(&n)` THEN
  SIMP_TAC[REALLIM_NULL_LMUL; REALLIM_1_OVER_N] THEN
  REWRITE_TAC[EVENTUALLY_SEQUENTIALLY] THEN EXISTS_TAC `2` THEN
  SUBGOAL_THEN
   `!n. &2 <= &n
        ==> log(f(&n)) - log(f(&n - &1))
              <= (log(f(&n + x)) - log(f(&n))) / x /\
            (log(f(&n + x)) - log(f(&n))) / x
              <= log(f(&n + &1)) - log(f(&n))`
  MP_TAC THENL
   [MP_TAC(SPECL [`f:real->real`; `{x | &0 < x}`] REAL_LOG_CONVEX_ON) THEN
    ASM_REWRITE_TAC[IN_ELIM_THM] THEN ANTS_TAC THENL
     [REWRITE_TAC[is_realinterval; IN_ELIM_THM] THEN REAL_ARITH_TAC;
      DISCH_TAC] THEN
    MAP_EVERY (MP_TAC o SPECL [`log o f:real->real`; `{x | &0 < x}`])
     [REAL_CONVEX_ON_LEFT_SECANT; REAL_CONVEX_ON_RIGHT_SECANT] THEN
    ASM_REWRITE_TAC[] THEN
    DISCH_THEN(LABEL_TAC "L") THEN DISCH_THEN(LABEL_TAC "R") THEN
    REPEAT STRIP_TAC THENL
     [USE_THEN "L" (MP_TAC o SPECL [`&n - &1`; `&n + x`; `&n`]) THEN
      USE_THEN "R" (MP_TAC o SPECL [`&n - &1`; `&n + x`; `&n`]) THEN
      REWRITE_TAC[IN_ELIM_THM; IN_REAL_SEGMENT; o_THM] THEN
      ANTS_TAC THENL [ASM_REAL_ARITH_TAC; ALL_TAC] THEN
      REWRITE_TAC[REAL_ARITH `abs(x - (x - &1)) = &1`; REAL_DIV_1] THEN
      DISCH_TAC THEN ANTS_TAC THENL [ASM_REAL_ARITH_TAC; ALL_TAC] THEN
      ASM_SIMP_TAC[REAL_ARITH `&0 < x ==> abs((n + x) - n) = x`] THEN
      ASM_REAL_ARITH_TAC;
      ASM_CASES_TAC `x = &1` THEN
      ASM_REWRITE_TAC[REAL_LE_REFL; REAL_DIV_1] THEN
      USE_THEN "R" (MP_TAC o SPECL [`&n`; `&n + &1`; `&n + x`]) THEN
      REWRITE_TAC[IN_ELIM_THM; IN_REAL_SEGMENT; o_THM] THEN
      ANTS_TAC THENL [ASM_REAL_ARITH_TAC; ALL_TAC] THEN
      REWRITE_TAC[REAL_ARITH `abs((n + &1) - n) = &1`; REAL_DIV_1] THEN
      ASM_SIMP_TAC[REAL_ARITH `&0 < x ==> abs((n + x) - n) = x`] THEN
      ASM_REAL_ARITH_TAC];
    ALL_TAC] THEN
  ASM_SIMP_TAC[GSYM LOG_DIV; REAL_LT_MUL;
    REAL_ARITH `&2 <= x ==> &0 < x /\ &0 < x + &1 /\ &0 < x - &1`;
    REAL_FIELD `&0 < x ==> (n * x) / x = n`] THEN
  SUBGOAL_THEN
   `!n. &2 <= n ==> f(n - &1) = f(n) / (n - &1)` (fun th -> SIMP_TAC[th])
  THENL
   [REPEAT STRIP_TAC THEN
    SUBGOAL_THEN `(f:real->real) n = f((n - &1) + &1)` SUBST1_TAC THENL
     [AP_TERM_TAC THEN REAL_ARITH_TAC;
      ASM_SIMP_TAC[REAL_ARITH `&2 <= n ==> &0 < n - &1`] THEN
      UNDISCH_TAC `&2 <= n` THEN CONV_TAC REAL_FIELD];
    ASM_SIMP_TAC[REAL_ARITH `&2 <= x ==> &0 < x`; REAL_FIELD
     `&0 < x /\ &2 <= y ==> x / (x / (y - &1)) = y - &1`] THEN
    ASM_SIMP_TAC[REAL_LE_LDIV_EQ; REAL_LE_RDIV_EQ]] THEN
  SIMP_TAC[REAL_OF_NUM_LE; REAL_OF_NUM_SUB;
           ARITH_RULE `2 <= n ==> 1 <= n`] THEN
  REWRITE_TAC[REAL_LE_SUB_LADD; REAL_LE_SUB_RADD] THEN
  SUBGOAL_THEN
   `!n. 0 < n ==> f(&n) = &(FACT(n - 1))`
   (fun th -> SIMP_TAC[ARITH_RULE `2 <= n ==> 0 < n /\ 0 < n - 1`; th])
  THENL
   [INDUCT_TAC THEN REWRITE_TAC[ARITH; LT_0] THEN
    ASM_CASES_TAC `n = 0` THEN
    ASM_REWRITE_TAC[REAL_ADD_LID; FACT; ARITH] THEN
    ASM_SIMP_TAC[ARITH_RULE `~(n = 0) ==> SUC n - 1 = SUC(n - 1)`] THEN
    ASM_SIMP_TAC[REAL_OF_NUM_LT; GSYM REAL_OF_NUM_SUC; LE_1; FACT] THEN
    ASM_SIMP_TAC[ARITH_RULE `~(n = 0) ==> SUC(n - 1) = n`] THEN
    REWRITE_TAC[REAL_OF_NUM_MUL; MULT_SYM];
    ALL_TAC] THEN
  GEN_REWRITE_TAC (LAND_CONV o BINDER_CONV o RAND_CONV o BINOP_CONV)
   [GSYM REAL_EXP_MONO_LE] THEN
  REWRITE_TAC[GSYM REAL_OF_NUM_LE] THEN
  ASM_SIMP_TAC[REAL_EXP_ADD; EXP_LOG; REAL_OF_NUM_LT; LE_1; FACT_NZ;
               ARITH_RULE `&2 <= &n /\ &0 < x ==> &0 < &n + x`] THEN
  REWRITE_TAC[REAL_OF_NUM_LE] THEN
  SUBGOAL_THEN
   `!n. 0 < n ==> f(&n + x) = product(0..n-1) (\k. x + &k) * f(x)`
   (fun th -> SIMP_TAC[ARITH_RULE `2 <= n ==> 0 < n`; th])
  THENL
   [INDUCT_TAC THEN REWRITE_TAC[ARITH] THEN
    DISCH_TAC THEN REWRITE_TAC[GSYM REAL_OF_NUM_SUC] THEN
    ASM_CASES_TAC `n = 0` THENL
     [ASM_SIMP_TAC[REAL_ARITH `(&0 + &1) + x = x + &1`; ARITH] THEN
      REWRITE_TAC[PRODUCT_SING_NUMSEG; REAL_ADD_RID];
      ASM_SIMP_TAC[REAL_ARITH `(n + &1) + x = (n + x) + &1`;
                   REAL_LT_ADD; REAL_OF_NUM_LT; LE_1] THEN
      ASM_SIMP_TAC[ARITH_RULE `~(n = 0) ==> SUC n - 1 = SUC(n - 1)`] THEN
      REWRITE_TAC[PRODUCT_CLAUSES_NUMSEG; LE_0] THEN
      ASM_SIMP_TAC[ARITH_RULE `~(n = 0) ==> SUC(n - 1) = n`] THEN
      REWRITE_TAC[REAL_MUL_AC; REAL_ADD_AC]];
    DISCH_TAC] THEN
  X_GEN_TAC `n:num` THEN DISCH_TAC THEN
  MATCH_MP_TAC(REAL_ARITH
   `&1 <= x /\ x <= &1 + e ==> abs(x - &1) <= e`) THEN
  ASM_SIMP_TAC[REAL_OF_NUM_LE; REAL_FIELD
   `&2 <= n ==> &1 + x * inv n = (x + n) / n`] THEN
  REWRITE_TAC[real_div; REAL_INV_MUL; REAL_INV_INV] THEN
  ONCE_REWRITE_TAC[REAL_ARITH `f * (r * n) * p:real = (p * f) * r * n`] THEN
  REWRITE_TAC[GSYM REAL_INV_MUL] THEN REWRITE_TAC[GSYM real_div] THEN
  SUBGOAL_THEN `&0 < &n rpow x * &(FACT n)` ASSUME_TAC THENL
   [ASM_SIMP_TAC[REAL_LT_MUL; REAL_OF_NUM_LT; LE_1; FACT_NZ;
                 RPOW_POS_LT; ARITH_RULE `2 <= x ==> 0 < x`];
    ALL_TAC]  THEN
  CONJ_TAC THENL
   [ASM_SIMP_TAC[REAL_LE_RDIV_EQ; REAL_MUL_LID] THEN
    FIRST_X_ASSUM(MP_TAC o SPEC `n + 1`) THEN
    ANTS_TAC THENL [ASM_ARITH_TAC; DISCH_THEN(MP_TAC o CONJUNCT1)] THEN
    REWRITE_TAC[ADD_SUB] THEN
    ASM_SIMP_TAC[rpow; REAL_OF_NUM_LE; REAL_ARITH `&2 <= x ==> &0 < x`] THEN
    REWRITE_TAC[REAL_MUL_AC];
    ASM_SIMP_TAC[PRODUCT_CLAUSES_RIGHT; LE_0;
                 ARITH_RULE `2 <= n ==> 0 < n`] THEN
    REWRITE_TAC[real_div; GSYM REAL_MUL_ASSOC] THEN
    GEN_REWRITE_TAC LAND_CONV
     [REAL_ARITH `a * b * c * d:real = b * (a * c) * d`] THEN
    MATCH_MP_TAC REAL_LE_LMUL THEN
    CONJ_TAC THENL [ASM_REAL_ARITH_TAC; ALL_TAC] THEN
    ASM_SIMP_TAC[GSYM real_div; REAL_LE_LDIV_EQ] THEN
    FIRST_X_ASSUM(MP_TAC o SPEC `n:num`) THEN ASM_REWRITE_TAC[] THEN
    DISCH_THEN(MP_TAC o CONJUNCT2) THEN
    MATCH_MP_TAC(REWRITE_RULE[IMP_CONJ_ALT] REAL_LE_TRANS) THEN
    SUBGOAL_THEN `FACT n = FACT(SUC(n - 1))` SUBST1_TAC THENL
     [AP_TERM_TAC THEN ASM_ARITH_TAC; REWRITE_TAC[FACT]] THEN
    ASM_SIMP_TAC[ARITH_RULE `2 <= n ==> SUC(n - 1) = n`] THEN
    MATCH_MP_TAC REAL_EQ_IMP_LE THEN
    SUBGOAL_THEN `&0 < &n` MP_TAC THENL
     [REWRITE_TAC[REAL_OF_NUM_LT] THEN ASM_ARITH_TAC;
      SIMP_TAC[rpow; GSYM REAL_OF_NUM_MUL; REAL_MUL_AC] THEN
      CONV_TAC REAL_FIELD]]);;

(* ------------------------------------------------------------------------- *)
(* The integral definition, the current usual one and Euler's original one.  *)
(* ------------------------------------------------------------------------- *)

let EULER_HAS_INTEGRAL_CGAMMA = prove
 (`!z. &0 < Re z
       ==> ((\t.  Cx(drop t) cpow (z - Cx(&1)) / cexp(Cx(drop t)))
            has_integral cgamma(z))
           {t | &0 <= drop t}`,
  let lemma0 = prove
   (`!z a b. &0 < Re z /\ &0 <= drop a
             ==> (\t. Cx(drop t) cpow z) continuous_on interval [a,b]`,
    REPEAT STRIP_TAC THEN
    REWRITE_TAC[CONTINUOUS_ON_EQ_CONTINUOUS_WITHIN] THEN
    X_GEN_TAC `t:real^1` THEN REWRITE_TAC[IN_INTERVAL_1] THEN STRIP_TAC THEN
    ASM_CASES_TAC `t:real^1 = vec 0` THENL
     [ASM_SIMP_TAC[CONTINUOUS_WITHIN; cpow; CX_INJ; GSYM LIFT_EQ; LIFT_NUM;
                   LIFT_DROP] THEN
      ONCE_REWRITE_TAC[LIM_NULL_COMPLEX_NORM] THEN
      REWRITE_TAC[NORM_CEXP] THEN
      MATCH_MP_TAC LIM_TRANSFORM_WITHIN THEN
      EXISTS_TAC `\t. Cx(exp(Re z * log(drop t)))` THEN
      EXISTS_TAC `&1` THEN REWRITE_TAC[REAL_LT_01] THEN CONJ_TAC THENL
       [X_GEN_TAC `u:real^1` THEN
        REWRITE_TAC[IN_INTERVAL_1; DIST_0; NORM_POS_LT; GSYM DROP_EQ;
                    LIFT_DROP; DROP_VEC] THEN
        STRIP_TAC THEN SUBGOAL_THEN `&0 < drop u`
         (fun th -> SIMP_TAC[GSYM CX_LOG; RE_MUL_CX; th]) THEN
        ASM_REAL_ARITH_TAC;
        REWRITE_TAC[LIM_WITHIN; IN_INTERVAL_1; DIST_CX; REAL_SUB_RZERO] THEN
        X_GEN_TAC `e:real` THEN DISCH_TAC THEN
        EXISTS_TAC `e rpow inv(Re z)` THEN
        ASM_SIMP_TAC[RPOW_POS_LT; DIST_0; REAL_ABS_EXP] THEN
        X_GEN_TAC `u:real^1` THEN STRIP_TAC THEN
        SUBGOAL_THEN `e = exp(log e)` SUBST1_TAC THENL
         [ASM_MESON_TAC[EXP_LOG]; REWRITE_TAC[REAL_EXP_MONO_LT]] THEN
        ONCE_REWRITE_TAC[REAL_MUL_SYM] THEN
        ASM_SIMP_TAC[GSYM REAL_LT_RDIV_EQ] THEN
        REWRITE_TAC[REAL_ARITH `x / y:real = inv y * x`] THEN
        ASM_SIMP_TAC[GSYM LOG_RPOW] THEN MATCH_MP_TAC LOG_MONO_LT_IMP THEN
        SUBGOAL_THEN `norm u = drop u` (fun th -> ASM_MESON_TAC[th]) THEN
        REWRITE_TAC[NORM_REAL; GSYM drop] THEN ASM_REAL_ARITH_TAC];
      MATCH_MP_TAC DIFFERENTIABLE_IMP_CONTINUOUS_WITHIN THEN
      SUBGOAL_THEN
       `(\a. Cx(drop a) cpow z) = (\w. w cpow z) o Cx o drop`
      SUBST1_TAC THENL [REWRITE_TAC[o_DEF]; ALL_TAC] THEN
      MATCH_MP_TAC DIFFERENTIABLE_CHAIN_WITHIN THEN CONJ_TAC THENL
       [MATCH_MP_TAC DIFFERENTIABLE_LINEAR THEN
        REWRITE_TAC[linear; o_DEF; DROP_ADD; DROP_CMUL; CX_ADD;
                    COMPLEX_CMUL; GSYM CX_MUL];
        MATCH_MP_TAC COMPLEX_DIFFERENTIABLE_IMP_DIFFERENTIABLE THEN
        COMPLEX_DIFFERENTIABLE_TAC THEN DISCH_THEN(K ALL_TAC) THEN
        REWRITE_TAC[o_DEF; RE_CX] THEN
        ASM_REWRITE_TAC[REAL_LT_LE; GSYM LIFT_EQ; LIFT_NUM; LIFT_DROP] THEN
        ASM_REAL_ARITH_TAC]]) in
  let lemma1 = prove
   (`!n z. &0 < Re z
           ==> ((\t. Cx(drop t) cpow (z - Cx(&1)) * Cx(&1 - drop t) pow n)
                has_integral Cx(&(FACT n)) / cproduct (0..n) (\m. z + Cx(&m)))
                (interval[vec 0,vec 1])`,
    INDUCT_TAC THEN X_GEN_TAC `z:complex` THEN
    ASM_CASES_TAC `z = Cx(&0)` THEN ASM_REWRITE_TAC[RE_CX; REAL_LT_REFL] THEN
    DISCH_TAC THENL
     [REWRITE_TAC[complex_pow; COMPLEX_MUL_RID; CPRODUCT_CLAUSES_NUMSEG] THEN
      MP_TAC(ISPECL
       [`\t. Cx(drop t) cpow z / z`;
        `\t. Cx(drop t) cpow (z - Cx(&1))`;
        `vec 0:real^1`; `vec 1:real^1`]
          FUNDAMENTAL_THEOREM_OF_CALCULUS_INTERIOR) THEN
      REWRITE_TAC[DROP_VEC; CPOW_1; CPOW_0; FACT; complex_div] THEN
      REWRITE_TAC[COMPLEX_MUL_LZERO; COMPLEX_ADD_RID; COMPLEX_SUB_RZERO] THEN
      DISCH_THEN MATCH_MP_TAC THEN REWRITE_TAC[REAL_POS] THEN CONJ_TAC THENL
       [MATCH_MP_TAC CONTINUOUS_ON_COMPLEX_RMUL THEN
        MATCH_MP_TAC lemma0 THEN ASM_REWRITE_TAC[DROP_VEC; REAL_POS];
        REWRITE_TAC[IN_INTERVAL_1; DROP_VEC] THEN REPEAT STRIP_TAC THEN
        REWRITE_TAC[CX_SUB] THEN
        MATCH_MP_TAC HAS_VECTOR_DERIVATIVE_REAL_COMPLEX THEN
        COMPLEX_DIFF_TAC THEN ASM_REWRITE_TAC[RE_CX] THEN
        UNDISCH_TAC `~(z = Cx(&0))` THEN CONV_TAC COMPLEX_FIELD];
      FIRST_X_ASSUM(MP_TAC o SPEC `z + Cx(&1)`) THEN
      REWRITE_TAC[RE_ADD; RE_CX; COMPLEX_RING `(z + Cx(&1)) - Cx(&1) = z`] THEN
      ANTS_TAC THENL [ASM_REAL_ARITH_TAC; DISCH_TAC] THEN
      MP_TAC(ISPECL
       [`complex_mul`;
        `\t. Cx(drop t) cpow z`;
        `\t. Cx(&1) / Cx(&n + &1) * Cx(&1 - drop t) pow (n + 1)`;
        `\t. z *  Cx(drop t) cpow (z - Cx(&1))`;
        `\t. --(Cx(&1 - drop t) pow n)`;
        `vec 0:real^1`; `vec 1:real^1`;
        `{vec 0:real^1}`;
        `Cx(&(FACT n)) / cproduct (0..n) (\m. (z + Cx(&1)) + Cx(&m))`]
        INTEGRATION_BY_PARTS) THEN
      REWRITE_TAC[BILINEAR_COMPLEX_MUL; DROP_VEC;
                  REAL_POS; COUNTABLE_SING] THEN
      REWRITE_TAC[CPOW_0; REAL_SUB_REFL; COMPLEX_POW_ZERO;
                  ADD_EQ_0; ARITH] THEN
      REWRITE_TAC[COMPLEX_MUL_LZERO; COMPLEX_MUL_RZERO; COMPLEX_SUB_LZERO] THEN
      REWRITE_TAC[COMPLEX_MUL_RNEG; COMPLEX_RING `--Cx(&0) - y = --y`] THEN
      ANTS_TAC THENL
       [REPEAT CONJ_TAC THENL
         [MATCH_MP_TAC CONTINUOUS_ON_COMPLEX_MUL THEN
          ASM_SIMP_TAC[lemma0; DROP_VEC; REAL_POS] THEN
          MATCH_MP_TAC CONTINUOUS_ON_COMPLEX_LMUL THEN
          MATCH_MP_TAC CONTINUOUS_ON_COMPLEX_POW THEN
          REWRITE_TAC[CONTINUOUS_ON_CX_LIFT; LIFT_SUB; LIFT_DROP] THEN
          SIMP_TAC[CONTINUOUS_ON_SUB; CONTINUOUS_ON_ID; CONTINUOUS_ON_CONST];
          SIMP_TAC[IN_INTERVAL_1; DROP_VEC; IN_DIFF;
                   IN_SING; GSYM DROP_EQ] THEN
          REPEAT STRIP_TAC THEN REWRITE_TAC[CX_SUB] THEN
          MATCH_MP_TAC HAS_VECTOR_DERIVATIVE_REAL_COMPLEX THEN
          COMPLEX_DIFF_TAC THEN
          ASM_REWRITE_TAC[RE_CX; COMPLEX_MUL_LID] THEN
          REWRITE_TAC[REAL_OF_NUM_ADD; ADD_SUB; COMPLEX_MUL_ASSOC] THEN
          MP_TAC(ARITH_RULE `~(n + 1 = 0)`) THEN
          REWRITE_TAC[GSYM REAL_OF_NUM_EQ; GSYM CX_INJ] THEN
          CONV_TAC COMPLEX_FIELD;
          MATCH_MP_TAC HAS_INTEGRAL_NEG THEN ASM_REWRITE_TAC[]];
        DISCH_THEN(MP_TAC o SPEC `Cx(&n + &1) / z` o
          MATCH_MP HAS_INTEGRAL_COMPLEX_LMUL) THEN
        ASM_SIMP_TAC[REAL_ARITH `~(&n + &1 = &0)`; CX_INJ; COMPLEX_FIELD
          `~(n = Cx(&0)) /\ ~(z = Cx(&0))
           ==> n / z * (z * p) * Cx(&1) / n * q = p * q`] THEN
        REWRITE_TAC[GSYM ADD1] THEN MATCH_MP_TAC EQ_IMP THEN
        AP_THM_TAC THEN AP_TERM_TAC THEN
        SIMP_TAC[FACT; GSYM REAL_OF_NUM_MUL; CX_MUL; GSYM REAL_OF_NUM_SUC] THEN
        REWRITE_TAC[complex_div; GSYM COMPLEX_MUL_ASSOC] THEN AP_TERM_TAC THEN
        SIMP_TAC[CPRODUCT_CLAUSES_LEFT; ARITH_RULE `0 <= SUC n`] THEN
        REWRITE_TAC[ADD1; COMPLEX_INV_MUL; COMPLEX_ADD_RID] THEN
        REWRITE_TAC[ISPECL [`f:num->complex`; `m:num`; `1`]
                    CPRODUCT_OFFSET] THEN
        REWRITE_TAC[GSYM COMPLEX_ADD_ASSOC; GSYM CX_ADD; REAL_OF_NUM_ADD] THEN
        REWRITE_TAC[ARITH_RULE `n + 1 = 1 + n`; COMPLEX_MUL_AC]]]) in
  let lemma2 = prove
   (`!z n. &0 < Re z
           ==> ((\t. if drop t <= &n
                     then Cx(drop t) cpow (z - Cx(&1)) *
                          Cx(&1 - drop t / &n) pow n
                     else Cx(&0))
                has_integral (Cx(&n) cpow z * Cx(&(FACT n))) /
                             cproduct (0..n) (\m. z + Cx(&m)))
               {t | &0 <= drop t}`,
    REPEAT STRIP_TAC THEN ASM_CASES_TAC `n = 0` THENL
     [ASM_REWRITE_TAC[CPOW_0; COMPLEX_MUL_LZERO; complex_div] THEN
      MATCH_MP_TAC HAS_INTEGRAL_SPIKE THEN
      MAP_EVERY EXISTS_TAC [`\t:real^1. Cx(&0)`; `{vec 0:real^1}`] THEN
      REWRITE_TAC[GSYM COMPLEX_VEC_0; HAS_INTEGRAL_0; NEGLIGIBLE_SING] THEN
      REWRITE_TAC[IN_DIFF; IN_SING; GSYM DROP_EQ; DROP_VEC; IN_ELIM_THM] THEN
      REAL_ARITH_TAC;
      REPEAT STRIP_TAC THEN REWRITE_TAC[GSYM COMPLEX_VEC_0] THEN
      ONCE_REWRITE_TAC[SET_RULE `drop x <= b <=> x IN {x | drop x <= b}`] THEN
      REWRITE_TAC[HAS_INTEGRAL_RESTRICT_INTER] THEN
      FIRST_ASSUM(MP_TAC o SPEC `n:num` o MATCH_MP lemma1) THEN
      DISCH_THEN(MP_TAC o SPECL [`inv(&n)`; `vec 0:real^1`] o
       MATCH_MP (REWRITE_RULE[IMP_CONJ] HAS_INTEGRAL_AFFINITY)) THEN
      REWRITE_TAC[DIMINDEX_1; REAL_POW_1] THEN
      ASM_SIMP_TAC[REAL_INV_EQ_0; REAL_OF_NUM_EQ; LE_1; REAL_ABS_INV] THEN
      REWRITE_TAC[IMAGE_AFFINITY_INTERVAL; REAL_INV_INV; REAL_POS] THEN
      REWRITE_TAC[UNIT_INTERVAL_NONEMPTY; VECTOR_ADD_RID; REAL_ABS_NUM] THEN
      REWRITE_TAC[VECTOR_MUL_RZERO; VECTOR_NEG_0; VECTOR_ADD_RID] THEN
      REWRITE_TAC[DROP_CMUL; COMPLEX_CMUL] THEN
      DISCH_THEN(MP_TAC o SPEC `Cx(&n) cpow (z - Cx(&1))` o
            MATCH_MP HAS_INTEGRAL_COMPLEX_LMUL) THEN
      REWRITE_TAC[COMPLEX_MUL_ASSOC;
                  GSYM(ONCE_REWRITE_RULE[COMPLEX_MUL_SYM] CPOW_SUC)] THEN
      REWRITE_TAC[complex_div; GSYM COMPLEX_MUL_ASSOC; GSYM LIFT_EQ_CMUL] THEN
      REWRITE_TAC[ONCE_REWRITE_RULE[INTER_COMM] INTER; IN_ELIM_THM] THEN
      REWRITE_TAC[GSYM DROP_VEC; LIFT_DROP; GSYM IN_INTERVAL_1] THEN
      REWRITE_TAC[DROP_VEC; SET_RULE `{x | x IN s} = s`] THEN
      REWRITE_TAC[COMPLEX_RING `(z - Cx(&1)) + Cx(&1) = z`] THEN
      MATCH_MP_TAC(REWRITE_RULE[IMP_CONJ] HAS_INTEGRAL_EQ) THEN
      REWRITE_TAC[FORALL_LIFT; IN_INTERVAL_1; LIFT_DROP; DROP_VEC] THEN
      X_GEN_TAC `t:real` THEN STRIP_TAC THEN
      REWRITE_TAC[COMPLEX_MUL_ASSOC; REAL_ARITH `inv x * y:real = y / x`] THEN
      AP_THM_TAC THEN AP_TERM_TAC THEN IMP_REWRITE_TAC[GSYM CPOW_MUL_REAL] THEN
      ASM_SIMP_TAC[REAL_CX; RE_CX; REAL_LE_DIV; REAL_POS; GSYM CX_MUL] THEN
      ASM_SIMP_TAC[REAL_DIV_LMUL; REAL_OF_NUM_EQ; LE_1]]) in
  let lemma3 = prove
   (`f integrable_on s
     ==> (\x:real^N. lift(Re(f x))) integrable_on s /\
         integral s (\x. lift(Re(f x))) = lift(Re(integral s f))`,
    SUBGOAL_THEN `!z. lift(Re z) = (lift o Re) z`
     (fun th -> REWRITE_TAC[th]) THENL [REWRITE_TAC[o_DEF]; ALL_TAC] THEN
    ONCE_REWRITE_TAC[GSYM o_DEF] THEN REPEAT STRIP_TAC THENL
     [MATCH_MP_TAC INTEGRABLE_LINEAR; MATCH_MP_TAC INTEGRAL_LINEAR] THEN
    ASM_REWRITE_TAC[linear; COMPLEX_CMUL; o_THM; RE_ADD; RE_MUL_CX] THEN
    REWRITE_TAC[LIFT_CMUL; LIFT_ADD]) in
  REPEAT STRIP_TAC THEN
  MP_TAC(ISPECL
   [`\n t. if drop t <= &n
           then Cx(drop t) cpow (z - Cx(&1)) * Cx(&1 - drop t / &n) pow n
           else Cx(&0)`;
    `\t. Cx(drop t) cpow (z - Cx(&1)) / cexp(Cx(drop t))`;
    `\t. lift(Re(Cx(drop t) cpow (Cx(Re z) - Cx(&1)) / cexp(Cx(drop t))))`;
    `{t | &0 <= drop t}`] DOMINATED_CONVERGENCE) THEN
  ASM_SIMP_TAC[REWRITE_RULE[HAS_INTEGRAL_INTEGRABLE_INTEGRAL] lemma2] THEN
  ANTS_TAC THENL
   [ALL_TAC;
    SIMP_TAC[HAS_INTEGRAL_INTEGRABLE_INTEGRAL] THEN
    MESON_TAC[CGAMMA; LIM_UNIQUE; TRIVIAL_LIMIT_SEQUENTIALLY]] THEN
  REWRITE_TAC[IN_ELIM_THM; LIFT_DROP] THEN REPEAT CONJ_TAC THENL
   [ALL_TAC;
    REWRITE_TAC[FORALL_LIFT; LIFT_DROP] THEN
    MAP_EVERY X_GEN_TAC [`n:num`; `t:real`] THEN DISCH_TAC THEN
    REWRITE_TAC[cpow; CX_INJ] THEN ASM_CASES_TAC `t = &0` THEN
    ASM_REWRITE_TAC[COMPLEX_MUL_LZERO; COND_ID; RE_CX; CEXP_0; COMPLEX_DIV_1;
                    COMPLEX_NORM_0; REAL_LE_REFL] THEN
    ASM_SIMP_TAC[GSYM CX_LOG; REAL_LT_LE; GSYM CX_EXP; GSYM CX_SUB;
                 GSYM CX_MUL; RE_CX; GSYM CX_DIV; GSYM REAL_EXP_SUB] THEN
    COND_CASES_TAC THEN REWRITE_TAC[COMPLEX_NORM_0; REAL_EXP_POS_LE] THEN
    REWRITE_TAC[COMPLEX_NORM_MUL; REAL_EXP_SUB; NORM_CEXP] THEN
    REWRITE_TAC[RE_MUL_CX; RE_SUB; RE_CX; COMPLEX_NORM_POW] THEN
    REWRITE_TAC[real_div] THEN SIMP_TAC[REAL_LE_LMUL_EQ; REAL_EXP_POS_LT] THEN
    REWRITE_TAC[COMPLEX_NORM_CX] THEN
    SUBGOAL_THEN `~(&n = &0)` MP_TAC THENL [ASM_REAL_ARITH_TAC; ALL_TAC] THEN
    REWRITE_TAC[GSYM real_div; REAL_OF_NUM_EQ] THEN DISCH_TAC THEN
    ASM_SIMP_TAC[real_abs; REAL_SUB_LE; REAL_LE_LDIV_EQ; REAL_MUL_LID;
                 REAL_OF_NUM_LT; LE_1] THEN
    TRANS_TAC REAL_LE_TRANS `exp(--t / &n) pow n` THEN CONJ_TAC THENL
     [MATCH_MP_TAC REAL_POW_LE2 THEN
      ASM_SIMP_TAC[REAL_SUB_LE; REAL_LE_LDIV_EQ; REAL_MUL_LID;
                   REAL_OF_NUM_LT; LE_1] THEN
      REWRITE_TAC[REAL_ARITH `&1 - t / n = &1 + --t / n`; REAL_EXP_LE_X];
      REWRITE_TAC[GSYM REAL_EXP_N; GSYM REAL_EXP_NEG] THEN
      ASM_SIMP_TAC[REAL_DIV_LMUL; REAL_OF_NUM_EQ; REAL_LE_REFL]];
    REWRITE_TAC[FORALL_LIFT; LIFT_DROP] THEN X_GEN_TAC `t:real` THEN
    DISCH_TAC THEN
    MATCH_MP_TAC LIM_TRANSFORM_EVENTUALLY THEN
    EXISTS_TAC `\k. Cx t cpow (z - Cx(&1)) * Cx(&1 - t / &k) pow k` THEN
    CONJ_TAC THENL
     [MP_TAC(SPEC `t + &1` REAL_ARCH_SIMPLE) THEN
      REWRITE_TAC[EVENTUALLY_SEQUENTIALLY; GSYM REAL_OF_NUM_LE] THEN
      MATCH_MP_TAC MONO_EXISTS THEN REAL_ARITH_TAC;
      REWRITE_TAC[complex_div] THEN MATCH_MP_TAC LIM_COMPLEX_LMUL THEN
      REWRITE_TAC[CX_SUB; CX_DIV; GSYM CEXP_NEG] THEN
      REWRITE_TAC[SIMPLE_COMPLEX_ARITH `a - x / y:complex = a + --x / y`] THEN
      REWRITE_TAC[CEXP_LIMIT]]] THEN
  MP_TAC(ISPECL
   [`\n t. lift(Re(if drop t <= &n
                   then Cx(drop t) cpow (Cx(Re z) - Cx(&1)) *
                        Cx(&1 - drop t / &n) pow n
                   else Cx(&0)))`;
    `\t. lift(Re(Cx(drop t) cpow (Cx(Re z) - Cx(&1)) / cexp(Cx(drop t))))`;
    `{t | &0 <= drop t}`]
  MONOTONE_CONVERGENCE_INCREASING) THEN
  REWRITE_TAC[] THEN ANTS_TAC THENL [ALL_TAC; MESON_TAC[]] THEN
  ASM_SIMP_TAC[lemma3; REWRITE_RULE[HAS_INTEGRAL_INTEGRABLE_INTEGRAL] lemma2;
   RE_CX; bounded; FORALL_IN_GSPEC] THEN
  REWRITE_TAC[IN_UNIV; FORALL_LIFT; LIFT_DROP; NORM_LIFT] THEN
  REPEAT CONJ_TAC THENL
   [MAP_EVERY X_GEN_TAC [`n:num`; `t:real`] THEN DISCH_TAC THEN
    ASM_CASES_TAC `t = &0` THEN
    ASM_SIMP_TAC[CPOW_0; COMPLEX_MUL_LZERO; COND_ID; REAL_LE_REFL] THEN
    ASM_REWRITE_TAC[cpow; CX_INJ] THEN
    ASM_SIMP_TAC[cpow; CX_INJ; GSYM CX_SUB; GSYM CX_EXP; GSYM CX_LOG;
                 REAL_LT_LE; GSYM CX_MUL; GSYM CX_DIV; RE_CX; GSYM CX_POW] THEN
    REWRITE_TAC[GSYM REAL_OF_NUM_SUC] THEN ASM_CASES_TAC `t <= &n` THEN
    ASM_SIMP_TAC[REAL_ARITH `x <= n ==> x <= n + &1`] THENL
     [SIMP_TAC[RE_CX; REAL_LE_LMUL_EQ; REAL_EXP_POS_LT] THEN
      REWRITE_TAC[GSYM RPOW_POW; GSYM REAL_OF_NUM_SUC] THEN
      MATCH_MP_TAC REAL_EXP_LIMIT_RPOW_LE THEN ASM_REAL_ARITH_TAC;
      COND_CASES_TAC THEN REWRITE_TAC[REAL_LE_REFL; RE_CX] THEN
      MATCH_MP_TAC REAL_LE_MUL THEN REWRITE_TAC[REAL_EXP_POS_LE] THEN
      MATCH_MP_TAC REAL_POW_LE THEN REWRITE_TAC[REAL_SUB_LE] THEN
      SIMP_TAC[REAL_LE_LDIV_EQ; REAL_ARITH `&0 < &n + &1`] THEN
      ASM_REAL_ARITH_TAC];
    X_GEN_TAC `t:real` THEN DISCH_TAC THEN REWRITE_TAC[] THEN
    MATCH_MP_TAC LIM_TRANSFORM_EVENTUALLY THEN EXISTS_TAC
     `\k. lift(Re(Cx t cpow (Cx(Re z) - Cx(&1)) *
                  Cx(&1 - t / &k) pow k))` THEN
    CONJ_TAC THENL
     [MP_TAC(SPEC `t + &1` REAL_ARCH_SIMPLE) THEN
      REWRITE_TAC[EVENTUALLY_SEQUENTIALLY; GSYM REAL_OF_NUM_LE] THEN
      MATCH_MP_TAC MONO_EXISTS THEN REAL_ARITH_TAC;
      REWRITE_TAC[cpow; CX_INJ] THEN
      ASM_CASES_TAC `t = &0` THEN
      ASM_REWRITE_TAC[COMPLEX_MUL_LZERO; complex_div; LIM_CONST] THEN
      ASM_SIMP_TAC[cpow; CX_INJ; GSYM CX_SUB; GSYM CX_EXP; GSYM CX_LOG;
              REAL_LT_LE; GSYM CX_MUL; GSYM CX_INV; RE_CX; GSYM CX_POW] THEN
      REWRITE_TAC[real_div; LIFT_CMUL; RE_CX] THEN MATCH_MP_TAC LIM_CMUL THEN
      REWRITE_TAC[REAL_ARITH `&1 - t * inv x = &1 + --t / x`] THEN
      REWRITE_TAC[GSYM REAL_EXP_NEG] THEN
      REWRITE_TAC[GSYM(REWRITE_RULE[o_DEF] TENDSTO_REAL)] THEN
      REWRITE_TAC[EXP_LIMIT]];
    MP_TAC(MATCH_MP CONVERGENT_IMP_BOUNDED (SPEC `Cx(Re z)` CGAMMA)) THEN
    REWRITE_TAC[bounded; FORALL_IN_IMAGE; IN_UNIV] THEN
    MESON_TAC[COMPLEX_NORM_GE_RE_IM; REAL_LE_TRANS]]);;

let EULER_INTEGRAL = prove
 (`!z. &0 < Re z
       ==> integral {t | &0 <= drop t}
                    (\t.  Cx(drop t) cpow (z - Cx(&1)) / cexp(Cx(drop t))) =
           cgamma(z)`,
  MESON_TAC[INTEGRAL_UNIQUE; EULER_HAS_INTEGRAL_CGAMMA]);;

let EULER_INTEGRABLE = prove
 (`!z. &0 < Re z
       ==> (\t.  Cx(drop t) cpow (z - Cx(&1)) / cexp(Cx(drop t))) integrable_on
           {t | &0 <= drop t}`,
  MESON_TAC[EULER_HAS_INTEGRAL_CGAMMA; integrable_on]);;

let EULER_HAS_REAL_INTEGRAL_GAMMA = prove
 (`!x. &0 < x
       ==> ((\t. t rpow (x - &1) / exp t) has_real_integral gamma(x))
           {t | &0 <= t}`,
  REPEAT STRIP_TAC THEN REWRITE_TAC[has_real_integral] THEN
  MP_TAC(SPEC `Cx x` EULER_HAS_INTEGRAL_CGAMMA) THEN
  ASM_REWRITE_TAC[gamma; o_DEF; RE_CX] THEN
  DISCH_THEN(MP_TAC o ISPEC `lift o Re` o MATCH_MP
   (REWRITE_RULE[IMP_CONJ] HAS_INTEGRAL_LINEAR)) THEN
  REWRITE_TAC[o_DEF] THEN ANTS_TAC THENL
   [REWRITE_TAC[linear; RE_ADD; LIFT_ADD; COMPLEX_CMUL; RE_MUL_CX] THEN
    REWRITE_TAC[LIFT_CMUL];
    ALL_TAC] THEN
  SUBGOAL_THEN `IMAGE lift {t | &0 <= t} = {t | &0 <= drop t}`
  SUBST1_TAC THENL
   [MATCH_MP_TAC SURJECTIVE_IMAGE_EQ THEN
    REWRITE_TAC[IN_ELIM_THM; LIFT_DROP] THEN MESON_TAC[LIFT_DROP];
    ALL_TAC] THEN
  MATCH_MP_TAC(ONCE_REWRITE_RULE[IMP_CONJ] (REWRITE_RULE[CONJ_ASSOC]
        HAS_INTEGRAL_SPIKE)) THEN
  EXISTS_TAC `{vec 0:real^1}` THEN
  REWRITE_TAC[NEGLIGIBLE_SING; FORALL_LIFT; IN_DIFF; IN_ELIM_THM;
              LIFT_DROP; IN_SING; GSYM DROP_EQ; DROP_VEC] THEN
  SIMP_TAC[cpow; CX_INJ; rpow; REAL_LT_LE; GSYM CX_LOG; GSYM CX_SUB;
           GSYM CX_EXP; GSYM CX_MUL; RE_CX; GSYM CX_DIV]);;

let EULER_REAL_INTEGRAL = prove
 (`!x. &0 < x
       ==> real_integral {t | &0 <= t} (\t. t rpow (x - &1) / exp t) =
           gamma(x)`,
  MESON_TAC[REAL_INTEGRAL_UNIQUE; EULER_HAS_REAL_INTEGRAL_GAMMA]);;

let EULER_REAL_INTEGRABLE = prove
 (`!x. &0 < x
       ==> (\t. t rpow (x - &1) / exp t) real_integrable_on {t | &0 <= t}`,
  MESON_TAC[EULER_HAS_REAL_INTEGRAL_GAMMA; real_integrable_on]);;

let EULER_ORIGINAL_REAL_INTEGRABLE = prove
 (`!x. &0 < x
       ==> (\t. (--log t) rpow (x - &1))
                real_integrable_on real_interval[&0,&1]`,
  SUBGOAL_THEN
   `!x. &0 < x
        ==> ((\t. (--log t) rpow (x - &1))
             real_integrable_on real_interval[&0,&1] <=>
             (\t. (--log t) rpow x)
             real_integrable_on real_interval[&0,&1])`
  ASSUME_TAC THENL
   [GEN_TAC THEN ASM_CASES_TAC `x = &0` THEN
    ASM_REWRITE_TAC[REAL_LT_REFL] THEN DISCH_TAC THEN MP_TAC(ISPECL
     [`\t. inv(x) * (--log t) rpow x`;
      `\t:real. t`;
      `\t. --(&1) / t * (--log t) rpow (x - &1)`;
      `\t:real. &1`; `&0`; `&1`; `{&0,&1}`]
        REAL_INTEGRABLE_BY_PARTS_EQ) THEN
    REWRITE_TAC[COUNTABLE_INSERT; COUNTABLE_EMPTY; REAL_MUL_RID] THEN
    ANTS_TAC THENL
     [CONJ_TAC THENL
       [REWRITE_TAC[GSYM REAL_MUL_ASSOC] THEN
        MATCH_MP_TAC REAL_CONTINUOUS_ON_LMUL THEN
        REWRITE_TAC[REAL_CONTINUOUS_ON_EQ_CONTINUOUS_WITHIN] THEN
        X_GEN_TAC `t:real` THEN REWRITE_TAC[IN_REAL_INTERVAL] THEN
        STRIP_TAC THEN
        FIRST_ASSUM(STRIP_ASSUME_TAC o MATCH_MP (REAL_ARITH
         `&0 <= x ==> &0 < x \/ x = &0`))
        THENL
         [MATCH_MP_TAC REAL_CONTINUOUS_MUL THEN
          REWRITE_TAC[REAL_CONTINUOUS_WITHIN_ID] THEN
          MATCH_MP_TAC REAL_CONTINUOUS_ATREAL_WITHINREAL THEN
          SUBGOAL_THEN
           `(\y. --log(y) rpow x) = (\y. y rpow x) o (\x. --x) o log`
          SUBST1_TAC THENL [REWRITE_TAC[o_DEF]; ALL_TAC] THEN
          REPEAT(MATCH_MP_TAC REAL_CONTINUOUS_ATREAL_COMPOSE THEN
                 CONJ_TAC) THEN
          ASM_SIMP_TAC[REAL_CONTINUOUS_NEG; REAL_CONTINUOUS_AT_ID;
                       REAL_CONTINUOUS_AT_LOG] THEN
          MATCH_MP_TAC REAL_CONTINUOUS_AT_RPOW THEN
          ASM_SIMP_TAC[REAL_LE_LT];
          FIRST_X_ASSUM SUBST_ALL_TAC THEN
          REWRITE_TAC[REAL_CONTINUOUS_WITHINREAL; REAL_MUL_RZERO] THEN
          MATCH_MP_TAC REALLIM_TRANSFORM_EVENTUALLY THEN
          EXISTS_TAC `\y. (--x * log(y rpow inv x) * y rpow inv x) rpow x` THEN
          CONJ_TAC THENL
           [REWRITE_TAC[EVENTUALLY_WITHINREAL] THEN
            REWRITE_TAC[REAL_ARITH `&0 < abs(x - a) <=> ~(x = a)`] THEN
            EXISTS_TAC `&1` THEN REWRITE_TAC[REAL_LT_01] THEN
            X_GEN_TAC `y:real` THEN REWRITE_TAC[IN_REAL_INTERVAL] THEN
            STRIP_TAC THEN
            SUBGOAL_THEN `&0 < y` ASSUME_TAC THENL
             [ASM_REAL_ARITH_TAC; ALL_TAC] THEN
            ASM_SIMP_TAC[LOG_RPOW; REAL_LT_INV_EQ; REAL_FIELD
              `~(x = &0) ==> --x * (inv x * y) * z = --y * z`] THEN
            ASM_SIMP_TAC[RPOW_MUL; RPOW_RPOW; REAL_MUL_LINV] THEN
            REWRITE_TAC[RPOW_POW; REAL_POW_1];
            SUBGOAL_THEN `&0 = &0 rpow x`
             (fun th -> GEN_REWRITE_TAC LAND_CONV [th])
            THENL [ASM_SIMP_TAC[rpow; REAL_LT_IMP_NZ; REAL_LT_REFL];
                   ALL_TAC] THEN
            MATCH_MP_TAC(REWRITE_RULE[] (ISPEC `\y. y rpow x`
                REALLIM_REAL_CONTINUOUS_FUNCTION)) THEN
            ASM_SIMP_TAC[REAL_CONTINUOUS_AT_RPOW; REAL_LT_IMP_LE] THEN
            MATCH_MP_TAC REALLIM_NULL_LMUL THEN
            SUBGOAL_THEN
             `(\y. log (y rpow inv x) * y rpow inv x) =
              (\y. log y * y) o (\y. y rpow inv x)`
            SUBST1_TAC THENL [REWRITE_TAC[o_DEF]; ALL_TAC] THEN
            MATCH_MP_TAC REALLIM_COMPOSE_WITHIN THEN
            EXISTS_TAC `{x | &0 <= x}` THEN EXISTS_TAC `&0 rpow inv x` THEN
            ASM_REWRITE_TAC[REAL_ENTIRE; RPOW_EQ_0; REAL_INV_EQ_0] THEN
            REPEAT CONJ_TAC THENL
             [MATCH_MP_TAC(REWRITE_RULE[] (ISPEC `\y. y rpow x`
                REALLIM_REAL_CONTINUOUS_FUNCTION)) THEN
              ASM_SIMP_TAC[REAL_CONTINUOUS_AT_RPOW; REAL_LT_IMP_LE;
                           REAL_LE_INV_EQ] THEN
              MP_TAC(ISPEC `&0` REAL_CONTINUOUS_AT_ID) THEN
              SIMP_TAC[REAL_CONTINUOUS_ATREAL; REALLIM_ATREAL_WITHINREAL];
              REWRITE_TAC[EVENTUALLY_WITHINREAL] THEN
              SIMP_TAC[RPOW_POS_LE; IN_REAL_INTERVAL; IN_ELIM_THM] THEN
              MESON_TAC[REAL_LT_01];
              ASM_REWRITE_TAC[RPOW_ZERO; REAL_INV_EQ_0] THEN
              ONCE_REWRITE_TAC[REAL_MUL_SYM] THEN
              REWRITE_TAC[REALLIM_X_TIMES_LOG]]]];
        REWRITE_TAC[IN_REAL_INTERVAL; IN_DIFF; IN_INSERT; NOT_IN_EMPTY] THEN
        X_GEN_TAC `t:real` THEN REWRITE_TAC[DE_MORGAN_THM] THEN STRIP_TAC THEN
        CONJ_TAC THEN REAL_DIFF_TAC THEN ASM_REWRITE_TAC[] THEN
        ASM_SIMP_TAC[GSYM LOG_INV; REAL_INV_1_LT; LOG_POS_LT] THEN
        REPEAT(POP_ASSUM MP_TAC) THEN CONV_TAC REAL_FIELD];
      ASM_REWRITE_TAC[REAL_INTEGRABLE_LMUL_EQ; REAL_INV_EQ_0] THEN
      DISCH_THEN SUBST1_TAC THEN
      REWRITE_TAC[real_div; GSYM REAL_MUL_ASSOC] THEN
      REWRITE_TAC[REAL_INTEGRABLE_LMUL_EQ] THEN
      CONV_TAC REAL_RAT_REDUCE_CONV THEN
      MATCH_MP_TAC REAL_INTEGRABLE_SPIKE_EQ THEN EXISTS_TAC `{&0}` THEN
      REWRITE_TAC[REAL_NEGLIGIBLE_SING; IN_DIFF; IN_SING] THEN
      CONV_TAC REAL_FIELD];
    ALL_TAC] THEN
  SUBGOAL_THEN
   `!n. (\t. --log t pow n) real_integrable_on real_interval[&0,&1]`
  ASSUME_TAC THENL
   [MATCH_MP_TAC num_INDUCTION THEN
    REWRITE_TAC[CONJUNCT1 real_pow; REAL_INTEGRABLE_CONST] THEN
    REWRITE_TAC[GSYM RPOW_POW] THEN X_GEN_TAC `n:num` THEN
    SUBGOAL_THEN `&n = &(SUC n) - &1` SUBST1_TAC THENL
     [REWRITE_TAC[GSYM REAL_OF_NUM_SUC] THEN REAL_ARITH_TAC;
      MATCH_MP_TAC EQ_IMP THEN FIRST_X_ASSUM MATCH_MP_TAC THEN
      REWRITE_TAC[REAL_OF_NUM_LT; LT_0]];
    ALL_TAC] THEN
  SUBGOAL_THEN
   `!x. &0 < x
        ==> (\t. --log t rpow x) real_integrable_on real_interval[&0,&1]`
   (fun th -> ASM_MESON_TAC[th]) THEN
  X_GEN_TAC `x:real` THEN DISCH_TAC THEN
  MP_TAC(SPEC `x:real` REAL_ARCH_SIMPLE) THEN
  DISCH_THEN(X_CHOOSE_TAC `n:num`) THEN
  MATCH_MP_TAC REAL_MEASURABLE_BOUNDED_AE_BY_INTEGRABLE_IMP_INTEGRABLE THEN
  EXISTS_TAC `\t. max (&1) (--log t pow n)` THEN EXISTS_TAC `{&0}` THEN
  REWRITE_TAC[REAL_NEGLIGIBLE_SING] THEN REPEAT CONJ_TAC THENL
   [MATCH_MP_TAC REAL_MEASURABLE_ON_RPOW THEN
    ASM_REWRITE_TAC[] THEN MATCH_MP_TAC INTEGRABLE_IMP_REAL_MEASURABLE THEN
    FIRST_X_ASSUM(MP_TAC o SPEC `1`) THEN REWRITE_TAC[REAL_POW_1];
    MATCH_MP_TAC ABSOLUTELY_REAL_INTEGRABLE_IMP_INTEGRABLE THEN
    MATCH_MP_TAC ABSOLUTELY_REAL_INTEGRABLE_MAX THEN
    REWRITE_TAC[ABSOLUTELY_REAL_INTEGRABLE_CONST] THEN MATCH_MP_TAC
      ABSOLUTELY_REAL_INTEGRABLE_ABSOLUTELY_REAL_INTEGRABLE_LBOUND THEN
    EXISTS_TAC `\x:real. min (--log(&0) pow n) (&0)` THEN
    ASM_REWRITE_TAC[ABSOLUTELY_REAL_INTEGRABLE_CONST] THEN
    REWRITE_TAC[IN_REAL_INTERVAL] THEN X_GEN_TAC `t:real` THEN
    STRIP_TAC THEN ASM_CASES_TAC `t = &0` THEN ASM_REWRITE_TAC[] THENL
     [ASM_REAL_ARITH_TAC; ALL_TAC] THEN
    SUBGOAL_THEN `&0 < t` ASSUME_TAC THENL [ASM_REAL_ARITH_TAC; ALL_TAC] THEN
    MATCH_MP_TAC(REAL_ARITH `b <= c ==> min a b <= c`) THEN
    MATCH_MP_TAC REAL_POW_LE THEN ASM_SIMP_TAC[GSYM LOG_INV] THEN
    MATCH_MP_TAC LOG_POS THEN MATCH_MP_TAC REAL_INV_1_LE THEN
    ASM_REWRITE_TAC[];
    REWRITE_TAC[IN_REAL_INTERVAL; IN_DIFF; IN_SING] THEN
    X_GEN_TAC `t:real` THEN STRIP_TAC THEN
    SUBGOAL_THEN `&0 < t` ASSUME_TAC THENL [ASM_REAL_ARITH_TAC; ALL_TAC] THEN
    SUBGOAL_THEN `&0 <= --log t` ASSUME_TAC THENL
     [ASM_SIMP_TAC[GSYM LOG_INV] THEN MATCH_MP_TAC LOG_POS THEN
      MATCH_MP_TAC REAL_INV_1_LE THEN ASM_REAL_ARITH_TAC;
      ASM_SIMP_TAC[real_abs; RPOW_POS_LE]] THEN
    ASM_CASES_TAC `--(log t) <= &1` THENL
     [MATCH_MP_TAC(REAL_ARITH `x <= y ==> x <= max y z`) THEN
      MATCH_MP_TAC RPOW_1_LE THEN ASM_REAL_ARITH_TAC;
      MATCH_MP_TAC(REAL_ARITH `x <= z ==> x <= max y z`) THEN
      REWRITE_TAC[GSYM RPOW_POW] THEN MATCH_MP_TAC RPOW_MONO_LE THEN
      ASM_REAL_ARITH_TAC]]);;

let EULER_ORIGINAL_INTEGRABLE = prove
 (`!z. &0 < Re z
       ==> (\t. (--clog(Cx(drop t))) cpow (z - Cx(&1)))
           integrable_on interval[vec 0,vec 1]`,
  REPEAT STRIP_TAC THEN
  MATCH_MP_TAC MEASURABLE_BOUNDED_AE_BY_INTEGRABLE_IMP_INTEGRABLE THEN
  EXISTS_TAC `\t. lift((--log(drop t)) rpow (Re z - &1))` THEN
  EXISTS_TAC `{vec 0:real^1,vec 1}` THEN
  REWRITE_TAC[NEGLIGIBLE_INSERT; NEGLIGIBLE_EMPTY] THEN REPEAT CONJ_TAC THENL
   [MATCH_MP_TAC
     CONTINUOUS_AE_IMP_MEASURABLE_ON_LEBESGUE_MEASURABLE_SUBSET THEN
    EXISTS_TAC `{vec 0:real^1,vec 1}` THEN
    REWRITE_TAC[NEGLIGIBLE_INSERT; NEGLIGIBLE_EMPTY;
                LEBESGUE_MEASURABLE_INTERVAL] THEN
    SUBGOAL_THEN
     `(\t. --clog (Cx(drop t)) cpow (z - Cx(&1))) =
      ((\w. w cpow (z - Cx(&1))) o (--) o clog) o (\x. Cx(drop x))`
    SUBST1_TAC THENL [REWRITE_TAC[o_DEF]; ALL_TAC] THEN
    MATCH_MP_TAC CONTINUOUS_ON_COMPOSE THEN
    SIMP_TAC[CONTINUOUS_ON_CX_DROP; CONTINUOUS_ON_ID] THEN
    MATCH_MP_TAC CONTINUOUS_AT_IMP_CONTINUOUS_ON THEN
    REWRITE_TAC[IN_DIFF; IN_INSERT; NOT_IN_EMPTY; DE_MORGAN_THM;
                IMP_CONJ; FORALL_IN_IMAGE] THEN
    REWRITE_TAC[FORALL_LIFT; IN_INTERVAL_1; LIFT_DROP] THEN
    REWRITE_TAC[GSYM DROP_EQ; LIFT_DROP; DROP_VEC; o_DEF] THEN
    REPEAT STRIP_TAC THEN
    MATCH_MP_TAC COMPLEX_DIFFERENTIABLE_IMP_CONTINUOUS_AT THEN
    COMPLEX_DIFFERENTIABLE_TAC THEN
    ASM_SIMP_TAC[GSYM CX_LOG; RE_CX; REAL_LT_LE; RE_NEG; GSYM LOG_INV] THEN
    DISCH_TAC THEN REWRITE_TAC[GSYM REAL_LT_LE] THEN
    MATCH_MP_TAC LOG_POS_LT THEN MATCH_MP_TAC REAL_INV_1_LT THEN
    ASM_REAL_ARITH_TAC;
    REWRITE_TAC[INTERVAL_REAL_INTERVAL; DROP_VEC;
                REWRITE_RULE[o_DEF] (GSYM REAL_INTEGRABLE_ON)] THEN
    MATCH_MP_TAC EULER_ORIGINAL_REAL_INTEGRABLE THEN ASM_REWRITE_TAC[];

    REWRITE_TAC[FORALL_LIFT; IN_DIFF; IN_INSERT; GSYM DROP_EQ; LIFT_DROP;
                IN_INTERVAL_1; DROP_VEC; NOT_IN_EMPTY] THEN
    X_GEN_TAC `x:real` THEN DISCH_TAC THEN
    SUBGOAL_THEN `&0 < x /\ x < &1` STRIP_ASSUME_TAC THENL
     [ASM_REAL_ARITH_TAC; ALL_TAC] THEN
    ASM_SIMP_TAC[GSYM CX_LOG; cpow; rpow; GSYM CX_NEG; REAL_LT_IMP_NZ; RE_CX;
          REAL_LT_IMP_LE; COMPLEX_NORM_MUL; NORM_CEXP; RE_SUB; CX_INJ;
          REAL_LE_REFL; RE_MUL_CX; GSYM LOG_INV; LOG_POS_LT; REAL_INV_1_LT]]);;

let EULER_ORIGINAL_HAS_INTEGRAL_CGAMMA = prove
 (`!z. &0 < Re z
       ==> ((\t. (--clog(Cx(drop t))) cpow (z - Cx(&1)))
            has_integral cgamma(z)) (interval[vec 0,vec 1])`,
  REPEAT STRIP_TAC THEN
  FIRST_ASSUM(ASSUME_TAC o MATCH_MP EULER_ORIGINAL_INTEGRABLE) THEN
  FIRST_ASSUM(MP_TAC o MATCH_MP INDEFINITE_INTEGRAL_CONTINUOUS_LEFT) THEN
  REWRITE_TAC[CONTINUOUS_ON_EQ_CONTINUOUS_WITHIN] THEN
  DISCH_THEN(MP_TAC o SPEC `vec 0:real^1`) THEN
  REWRITE_TAC[ENDS_IN_UNIT_INTERVAL; CONTINUOUS_WITHIN] THEN DISCH_TAC THEN
  ASM_REWRITE_TAC[HAS_INTEGRAL_INTEGRABLE_INTEGRAL] THEN
  FIRST_X_ASSUM(MATCH_MP_TAC o MATCH_MP (ONCE_REWRITE_RULE
   [TAUT `a /\ b /\ c ==> d <=> b ==> a /\ c ==> d`] LIM_UNIQUE)) THEN
  REWRITE_TAC[TRIVIAL_LIMIT_WITHIN] THEN CONJ_TAC THENL
   [MATCH_MP_TAC CONNECTED_IMP_PERFECT THEN
    REWRITE_TAC[ENDS_IN_UNIT_INTERVAL; CONNECTED_INTERVAL] THEN
    MATCH_MP_TAC(SET_RULE
     `{a,b} SUBSET interval[a,b] /\ ~(a = b)
      ==> ~(?x. interval[a,b] = {x})`) THEN
    REWRITE_TAC[INSERT_SUBSET; EMPTY_SUBSET; ENDS_IN_UNIT_INTERVAL] THEN
    REWRITE_TAC[VEC_EQ; ARITH_EQ];
    ALL_TAC] THEN
  MATCH_MP_TAC LIM_TRANSFORM_EVENTUALLY THEN
  EXISTS_TAC `\x. integral(interval[vec 0,lift(--log(drop x))])
          (\t. Cx(drop t) cpow (z - Cx(&1)) / cexp(Cx(drop t)))` THEN
  REWRITE_TAC[EVENTUALLY_WITHIN] THEN CONJ_TAC THENL
   [EXISTS_TAC `&1` THEN
    REWRITE_TAC[REAL_LT_01; DIST_0; NORM_POS_LT; IN_INTERVAL_1; DROP_VEC;
                GSYM DROP_EQ; DROP_VEC; FORALL_LIFT; LIFT_DROP] THEN
    X_GEN_TAC `x:real` THEN STRIP_TAC THEN
    SUBGOAL_THEN `&0 < x` ASSUME_TAC THENL [ASM_REAL_ARITH_TAC; ALL_TAC] THEN
    MP_TAC(ISPECL
     [`\t. (--clog(Cx(drop(--t)))) cpow (z - Cx(&1))`;
      `\t. --lift(exp(--drop t))`;
      `\t. lift(exp(--drop t))`;
      `vec 0:real^1`; `lift(--log x):real^1`; `--vec 1:real^1`; `vec 0:real^1`;
      `{vec 0:real^1,vec 1}`]
          HAS_INTEGRAL_SUBSTITUTION_STRONG) THEN
    REWRITE_TAC[LIFT_DROP; DROP_VEC; GSYM DROP_NEG] THEN
    REWRITE_TAC[GSYM REFLECT_INTERVAL; GSYM INTEGRAL_REFLECT_GEN] THEN
    REWRITE_TAC[DROP_NEG; LIFT_DROP; REAL_LE_NEG2; REAL_EXP_MONO_LE] THEN
    REWRITE_TAC[REAL_NEG_NEG; DROP_VEC; REAL_EXP_0; REAL_NEG_0] THEN
    SIMP_TAC[GSYM CX_LOG; REAL_EXP_POS_LT; LOG_EXP] THEN ANTS_TAC THENL
     [REWRITE_TAC[REAL_POS; COUNTABLE_INSERT; COUNTABLE_EMPTY] THEN
      CONV_TAC REAL_RAT_REDUCE_CONV THEN
      ASM_SIMP_TAC[GSYM LOG_INV; LOG_POS; REAL_INV_1_LE] THEN
      REWRITE_TAC[GSYM DROP_NEG; INTEGRABLE_REFLECT_GEN] THEN
      REWRITE_TAC[REFLECT_INTERVAL; VECTOR_NEG_NEG; VECTOR_NEG_0] THEN
      ASM_SIMP_TAC[EULER_ORIGINAL_INTEGRABLE] THEN REPEAT CONJ_TAC THENL
       [REWRITE_TAC[GSYM LIFT_NEG; DROP_NEG] THEN
        REWRITE_TAC[GSYM LIFT_NUM; GSYM IMAGE_LIFT_REAL_INTERVAL] THEN
        REWRITE_TAC[REWRITE_RULE[o_DEF] (GSYM REAL_CONTINUOUS_ON)] THEN
        MATCH_MP_TAC REAL_DIFFERENTIABLE_ON_IMP_REAL_CONTINUOUS_ON THEN
        REWRITE_TAC[REAL_DIFFERENTIABLE_ON_DIFFERENTIABLE] THEN
        REPEAT STRIP_TAC THEN REAL_DIFFERENTIABLE_TAC;
        REWRITE_TAC[SUBSET; FORALL_IN_IMAGE; IN_INTERVAL_1; DROP_VEC;
                    FORALL_LIFT; LIFT_DROP; DROP_NEG] THEN
        X_GEN_TAC `y:real` THEN STRIP_TAC THEN
        REWRITE_TAC[REAL_ARITH `--x <= &0 <=> &0 <= x`; REAL_EXP_POS_LE] THEN
        REWRITE_TAC[REAL_EXP_NEG; REAL_LE_NEG2] THEN
        MATCH_MP_TAC REAL_INV_LE_1 THEN
        GEN_REWRITE_TAC LAND_CONV [GSYM REAL_EXP_0] THEN
        ASM_REWRITE_TAC[REAL_EXP_MONO_LE];
        REWRITE_TAC[SUBSET; FORALL_IN_IMAGE; IN_INTERVAL_1; DROP_VEC;
                    FORALL_LIFT; LIFT_DROP; DROP_NEG; IN_DIFF; IN_INSERT;
                    NOT_IN_EMPTY; GSYM DROP_EQ; DE_MORGAN_THM] THEN
        X_GEN_TAC `y:real` THEN REPEAT STRIP_TAC THENL
         [MATCH_MP_TAC HAS_VECTOR_DERIVATIVE_AT_WITHIN THEN
          REWRITE_TAC[GSYM LIFT_NEG; REWRITE_RULE[o_DEF]
           (GSYM HAS_REAL_VECTOR_DERIVATIVE_AT)] THEN
          REAL_DIFF_TAC THEN REAL_ARITH_TAC;
          MATCH_MP_TAC CONTINUOUS_AT_WITHIN THEN
          SUBGOAL_THEN
           `(\t. --clog(Cx(--drop t)) cpow (z - Cx(&1))) =
            (\w. --clog w cpow (z - Cx(&1))) o (\t. Cx(drop(--t)))`
          SUBST1_TAC THENL [REWRITE_TAC[o_DEF; DROP_NEG]; ALL_TAC] THEN
          MATCH_MP_TAC CONTINUOUS_AT_COMPOSE THEN CONJ_TAC THENL
           [MATCH_MP_TAC CONTINUOUS_CX_DROP THEN
            GEN_REWRITE_TAC LAND_CONV [GSYM ETA_AX] THEN
            SIMP_TAC[CONTINUOUS_NEG; CONTINUOUS_AT_ID];
            MATCH_MP_TAC COMPLEX_DIFFERENTIABLE_IMP_CONTINUOUS_AT THEN
            COMPLEX_DIFFERENTIABLE_TAC THEN
            REWRITE_TAC[RE_CX; VECTOR_NEG_NEG; LIFT_DROP] THEN
            SIMP_TAC[REAL_EXP_POS_LT; GSYM CX_LOG; LOG_EXP] THEN
            REWRITE_TAC[RE_NEG; RE_CX; REAL_NEG_NEG] THEN
            ASM_REAL_ARITH_TAC]]];
      DISCH_THEN(MP_TAC o MATCH_MP INTEGRAL_UNIQUE) THEN
      ASM_SIMP_TAC[EXP_LOG; LIFT_NUM; COMPLEX_CMUL; CX_EXP; CEXP_NEG; CX_NEG;
                   COMPLEX_NEG_NEG] THEN
      DISCH_THEN(SUBST1_TAC o SYM) THEN
      ONCE_REWRITE_TAC[COMPLEX_MUL_SYM] THEN REWRITE_TAC[complex_div]];
   FIRST_ASSUM(MP_TAC o MATCH_MP EULER_HAS_INTEGRAL_CGAMMA) THEN
   GEN_REWRITE_TAC LAND_CONV [HAS_INTEGRAL_ALT] THEN
   REWRITE_TAC[INTEGRABLE_RESTRICT_INTER; INTEGRAL_RESTRICT_INTER] THEN
   DISCH_THEN(ASSUME_TAC o CONJUNCT2) THEN REWRITE_TAC[LIM_WITHIN] THEN
   X_GEN_TAC `e:real` THEN DISCH_TAC THEN
   FIRST_X_ASSUM(MP_TAC o SPEC `e:real`) THEN ASM_REWRITE_TAC[] THEN
   DISCH_THEN(X_CHOOSE_THEN `B:real` STRIP_ASSUME_TAC) THEN
   EXISTS_TAC `exp(--B)` THEN ASM_REWRITE_TAC[REAL_EXP_POS_LT] THEN
   REWRITE_TAC[REAL_LT_01; DIST_0; NORM_POS_LT; IN_INTERVAL_1; DROP_VEC;
               GSYM DROP_EQ; DROP_VEC; FORALL_LIFT; LIFT_DROP; NORM_LIFT] THEN
   X_GEN_TAC `x:real` THEN STRIP_TAC THEN
   SUBGOAL_THEN `&0 < x` ASSUME_TAC THENL [ASM_REAL_ARITH_TAC; ALL_TAC] THEN
   FIRST_X_ASSUM(MP_TAC o SPECL [`--lift B`; `lift(--log x)`]) THEN
   ANTS_TAC THENL
    [REWRITE_TAC[BALL_1; SUBSET_INTERVAL_1; DROP_ADD; DROP_SUB; DROP_VEC] THEN
     REWRITE_TAC[LIFT_DROP; DROP_NEG] THEN DISJ2_TAC THEN
     REPEAT(CONJ_TAC THENL [ASM_REAL_ARITH_TAC; ALL_TAC]) THEN
     REWRITE_TAC[REAL_ADD_LID] THEN
     ONCE_REWRITE_TAC[GSYM REAL_EXP_MONO_LE] THEN
     ASM_SIMP_TAC[REAL_EXP_NEG; EXP_LOG] THEN
     GEN_REWRITE_TAC LAND_CONV [GSYM REAL_INV_INV] THEN
     ONCE_REWRITE_TAC[GSYM REAL_EXP_NEG] THEN
     MATCH_MP_TAC REAL_LE_INV2 THEN ASM_REAL_ARITH_TAC;
     MATCH_MP_TAC(NORM_ARITH
      `i = j ==> norm(i - g) < e ==> dist(j,g) < e`) THEN
     AP_THM_TAC THEN AP_TERM_TAC THEN
     REWRITE_TAC[EXTENSION; IN_INTER; IN_ELIM_THM; IN_INTERVAL_1] THEN
     REWRITE_TAC[DROP_NEG; LIFT_DROP; DROP_VEC] THEN ASM_REAL_ARITH_TAC]]);;

let EULER_ORIGINAL_INTEGRAL = prove
 (`!z. &0 < Re z
       ==> integral (interval[vec 0,vec 1])
                    (\t. (--clog(Cx(drop t))) cpow (z - Cx(&1))) =
           cgamma(z)`,
  REPEAT STRIP_TAC THEN MATCH_MP_TAC INTEGRAL_UNIQUE THEN
  MATCH_MP_TAC EULER_ORIGINAL_HAS_INTEGRAL_CGAMMA THEN
  ASM_REWRITE_TAC[]);;

let EULER_ORIGINAL_HAS_REAL_INTEGRAL_GAMMA = prove
 (`!x. &0 < x
       ==> ((\t. (--log t) rpow (x - &1)) has_real_integral gamma(x))
           (real_interval[&0,&1])`,
  REPEAT STRIP_TAC THEN REWRITE_TAC[has_real_integral] THEN
  MP_TAC(SPEC `Cx x` EULER_ORIGINAL_HAS_INTEGRAL_CGAMMA) THEN
  ASM_REWRITE_TAC[gamma; o_DEF; RE_CX] THEN
  DISCH_THEN(MP_TAC o ISPEC `lift o Re` o MATCH_MP
   (REWRITE_RULE[IMP_CONJ] HAS_INTEGRAL_LINEAR)) THEN
  REWRITE_TAC[o_DEF] THEN ANTS_TAC THENL
   [REWRITE_TAC[linear; RE_ADD; LIFT_ADD; COMPLEX_CMUL; RE_MUL_CX] THEN
    REWRITE_TAC[LIFT_CMUL];
    REWRITE_TAC[IMAGE_LIFT_REAL_INTERVAL; LIFT_NUM]] THEN
  MATCH_MP_TAC(ONCE_REWRITE_RULE[IMP_CONJ] (REWRITE_RULE[CONJ_ASSOC]
        HAS_INTEGRAL_SPIKE)) THEN
  EXISTS_TAC `{vec 0:real^1,vec 1}` THEN
  REWRITE_TAC[NEGLIGIBLE_INSERT; FORALL_LIFT; IN_INTERVAL_1; DE_MORGAN_THM;
    LIFT_DROP; IN_INSERT; NOT_IN_EMPTY; GSYM DROP_EQ; DROP_VEC; IN_DIFF] THEN
  REWRITE_TAC[NEGLIGIBLE_EMPTY] THEN X_GEN_TAC `y:real` THEN STRIP_TAC THEN
  SUBGOAL_THEN `&0 < y` ASSUME_TAC THENL [ASM_REAL_ARITH_TAC; ALL_TAC] THEN
  SUBGOAL_THEN `&0 < --log y` ASSUME_TAC THENL
   [ASM_SIMP_TAC[GSYM LOG_INV] THEN MATCH_MP_TAC LOG_POS_LT THEN
    MATCH_MP_TAC REAL_INV_1_LT THEN ASM_REAL_ARITH_TAC;
    ASM_SIMP_TAC[GSYM CX_LOG; cpow; rpow; COMPLEX_NEG_EQ_0; CX_INJ;
                 REAL_LT_IMP_NZ] THEN
    ASM_SIMP_TAC[GSYM CX_NEG; GSYM CX_LOG; GSYM CX_SUB; GSYM CX_MUL] THEN
    COND_CASES_TAC THENL [ASM_REAL_ARITH_TAC; ALL_TAC] THEN
    REWRITE_TAC[GSYM CX_EXP; RE_CX]]);;

let EULER_ORIGINAL_REAL_INTEGRAL = prove
 (`!x. &0 < x
       ==> real_integral (real_interval[&0,&1])
                         (\t. (--log t) rpow (x - &1)) =
           gamma(x)`,
  MESON_TAC[REAL_INTEGRAL_UNIQUE; EULER_ORIGINAL_HAS_REAL_INTEGRAL_GAMMA]);;

(* ------------------------------------------------------------------------- *)
(* Stirling's approximation.                                                 *)
(* ------------------------------------------------------------------------- *)

let LGAMMA_STIRLING_INTEGRALS_EXIST,LGAMMA_STIRLING = (CONJ_PAIR o prove)
 (`(!z n. 1 <= n /\ (Im z = &0 ==> &0 < Re z)
          ==> (\t. Cx(bernoulli n (frac (drop t))) / (z + Cx(drop t)) pow n)
              integrable_on {t | &0 <= drop t}) /\
   (!z p.
      (Im z = &0 ==> &0 < Re z)
      ==> lgamma(z) =
          ((z - Cx(&1) / Cx(&2)) * clog(z) - z + Cx(log(&2 * pi) / &2)) +
          vsum(1..p) (\k. Cx(bernoulli (2 * k) (&0) /
                             (&4 * &k pow 2 - &2 * &k)) / z pow (2 * k - 1)) -
          integral {t | &0 <= drop t}
                   (\t. Cx(bernoulli (2 * p + 1) (frac(drop t))) /
                        (z + Cx(drop t)) pow (2 * p + 1)) /
          Cx(&(2 * p + 1)))`,
  let lemma1 = prove
   (`!p n z. (Im z = &0 ==> &0 < Re z)
             ==> (\x. Cx(bernoulli (2 * p + 1) (frac(drop x))) *
                   Cx(&(FACT(2 * p))) / (z + Cx(drop x)) pow (2 * p + 1))
                 integrable_on interval [lift(&0),lift(&n)] /\
                 vsum(0..n) (\m. clog(Cx(&m) + z)) =
                 (z + Cx(&n) + Cx(&1) / Cx(&2)) * clog(z + Cx(&n)) -
                 (z - Cx(&1) / Cx(&2)) * clog(z) - Cx(&n) +
                 vsum(1..p)
                  (\k. Cx(bernoulli (2 * k) (&0) / &(FACT(2 * k))) *
                       (Cx(&(FACT(2 * k - 2))) / (z + Cx(&n)) pow (2 * k - 1) -
                        Cx(&(FACT(2 * k - 2))) / z pow (2 * k - 1))) +
                 integral (interval[lift(&0),lift(&n)])
                   (\x. Cx(bernoulli (2 * p + 1) (frac(drop x))) *
                        Cx(&(FACT(2 * p))) / (z + Cx(drop x)) pow (2 * p + 1)) /
                 Cx(&(FACT(2 * p + 1)))`,
    REPEAT GEN_TAC THEN STRIP_TAC THEN
    REWRITE_TAC[COMPLEX_RING `Cx(&n) + z = z + Cx(&n)`] THEN MP_TAC(ISPECL
     [`\n w. if n = 0 then (z + w) * clog(z + w) - (z + w)
             else if n = 1 then clog(z + w)
             else Cx(--(&1) pow n * &(FACT(n - 2))) /
                  (z + w) pow (n - 1)`;
      `0`; `n:num`; `p:num`] COMPLEX_EULER_MACLAURIN_ANTIDERIVATIVE) THEN
    ASM_REWRITE_TAC[ADD_EQ_0; MULT_EQ_0; MULT_EQ_1] THEN
    CONV_TAC NUM_REDUCE_CONV THEN ANTS_TAC THENL
     [REWRITE_TAC[IN_REAL_INTERVAL; LE_0] THEN
      MAP_EVERY X_GEN_TAC [`k:num`; `x:real`] THEN REPEAT STRIP_TAC THEN
      SUBGOAL_THEN `~(z + Cx x = Cx(&0))` ASSUME_TAC THENL
       [REWRITE_TAC[COMPLEX_EQ; IM_ADD; RE_ADD; IM_CX; RE_CX] THEN
        ASM_REAL_ARITH_TAC;
        ALL_TAC] THEN
      REWRITE_TAC[o_THM; ARITH_RULE `k + 1 = 1 <=> k = 0`] THEN
      ASM_CASES_TAC `k = 0` THEN ASM_REWRITE_TAC[] THENL
       [COMPLEX_DIFF_TAC THEN CONJ_TAC THENL
         [REWRITE_TAC[RE_ADD; IM_ADD; RE_CX; IM_CX] THEN ASM_REAL_ARITH_TAC;
          UNDISCH_TAC `~(z + Cx x = Cx(&0))` THEN CONV_TAC COMPLEX_FIELD];
        ALL_TAC] THEN
      ASM_CASES_TAC `k = 1` THEN ASM_REWRITE_TAC[] THEN COMPLEX_DIFF_TAC THEN
      CONV_TAC NUM_REDUCE_CONV THEN CONV_TAC REAL_RAT_REDUCE_CONV THEN
      REWRITE_TAC[complex_div; COMPLEX_ADD_LID; COMPLEX_MUL_LID;
                  COMPLEX_POW_1; IM_ADD; RE_ADD; IM_CX; RE_CX] THEN
      (CONJ_TAC ORELSE ASM_REAL_ARITH_TAC) THEN
      ASM_REWRITE_TAC[COMPLEX_POW_EQ_0] THEN
      REWRITE_TAC[REAL_POW_ADD; REAL_POW_1; COMPLEX_MUL_LZERO] THEN
      REWRITE_TAC[REAL_MUL_RNEG; REAL_MUL_RID; REAL_NEG_NEG; REAL_MUL_LNEG] THEN
      REWRITE_TAC[COMPLEX_MUL_RID; CX_NEG; COMPLEX_POW_POW] THEN
      REWRITE_TAC[GSYM COMPLEX_MUL_ASSOC; COMPLEX_SUB_LZERO; COMPLEX_NEG_NEG;
                  COMPLEX_MUL_LNEG] THEN
      REWRITE_TAC[GSYM complex_div] THEN AP_TERM_TAC THEN
      FIRST_X_ASSUM(K ALL_TAC o check (is_imp o concl)) THEN
      ASM_SIMP_TAC[GSYM complex_div; COMPLEX_DIV_POW2] THEN COND_CASES_TAC THENL
       [MATCH_MP_TAC(TAUT `F ==> p`) THEN ASM_ARITH_TAC; ALL_TAC] THEN
      ASM_SIMP_TAC[ADD_SUB; ARITH_RULE
       `~(k = 0) /\ ~(k = 1) ==> (k - 1) * 2 - (k - 1 - 1) = k`] THEN
      REWRITE_TAC[COMPLEX_MUL_ASSOC; complex_div] THEN
      AP_THM_TAC THEN AP_TERM_TAC THEN REWRITE_TAC[GSYM CX_MUL] THEN
      AP_TERM_TAC THEN REWRITE_TAC[GSYM REAL_MUL_ASSOC] THEN AP_TERM_TAC THEN
      ASM_SIMP_TAC[ARITH_RULE `~(k = 0) ==> (k + 1) - 2 = k - 1`] THEN
      ASM_SIMP_TAC[ARITH_RULE
       `~(k = 0) /\ ~(k = 1) ==> k - 1 = SUC(k - 2)`] THEN
      REWRITE_TAC[FACT; REAL_OF_NUM_MUL] THEN REWRITE_TAC[MULT_SYM];
      REWRITE_TAC[ARITH_RULE `~(2 * p + 2 = 1)`] THEN
      DISCH_THEN(CONJUNCTS_THEN2 MP_TAC SUBST1_TAC)] THEN
    SIMP_TAC[LE_1] THEN
    REWRITE_TAC[ARITH_RULE `(2 * p + 2) - 1 = 2 * p + 1`; ADD_SUB] THEN
    REWRITE_TAC[REAL_POW_NEG; EVEN_ADD; EVEN_MULT; ARITH_EVEN] THEN
    REWRITE_TAC[REAL_POW_ONE; REAL_MUL_LID] THEN
    DISCH_TAC THEN ASM_REWRITE_TAC[COMPLEX_ADD_RID] THEN
    CONV_TAC COMPLEX_RING) in
  let lemma2 = prove
   (`!z n. 2 <= n /\ (Im z = &0 ==> &0 < Re z)
           ==> (\t. inv(z + Cx(drop t)) pow n)
               absolutely_integrable_on {t | &0 <= drop t}`,
    REPEAT STRIP_TAC THEN REWRITE_TAC[COMPLEX_POW_INV] THEN
    MATCH_MP_TAC
      MEASURABLE_BOUNDED_BY_INTEGRABLE_IMP_ABSOLUTELY_INTEGRABLE THEN
    EXISTS_TAC `\t. lift(inv(max (abs(Im z)) (Re z + drop t)) pow n)` THEN
    REWRITE_TAC[] THEN REPEAT CONJ_TAC THENL
     [MATCH_MP_TAC CONTINUOUS_IMP_MEASURABLE_ON_LEBESGUE_MEASURABLE_SUBSET THEN
      CONJ_TAC THENL
       [MATCH_MP_TAC CONTINUOUS_ON_COMPLEX_INV THEN
        SIMP_TAC[CONTINUOUS_ON_COMPLEX_POW; CONTINUOUS_ON_ADD;
          CONTINUOUS_ON_CONST; CONTINUOUS_ON_ID; CONTINUOUS_ON_CX_DROP] THEN
        REWRITE_TAC[IN_ELIM_THM; GSYM FORALL_DROP] THEN
        REWRITE_TAC[COMPLEX_POW_EQ_0] THEN
        REWRITE_TAC[COMPLEX_EQ; RE_ADD; IM_ADD; RE_CX; IM_CX] THEN
        ASM_REAL_ARITH_TAC;
        MATCH_MP_TAC LEBESGUE_MEASURABLE_CONVEX THEN
        MATCH_MP_TAC IS_INTERVAL_CONVEX THEN
        REWRITE_TAC[IS_INTERVAL_1; IN_ELIM_THM] THEN REAL_ARITH_TAC];
      ALL_TAC;
      REWRITE_TAC[FORALL_IN_GSPEC; GSYM FORALL_DROP; LIFT_DROP] THEN
      X_GEN_TAC `x:real` THEN DISCH_TAC THEN
     REWRITE_TAC[GSYM COMPLEX_POW_INV; COMPLEX_NORM_POW] THEN
     MATCH_MP_TAC REAL_POW_LE2 THEN
     REWRITE_TAC[NORM_POS_LE; COMPLEX_NORM_INV] THEN
     MATCH_MP_TAC REAL_LE_INV2 THEN
     MP_TAC(SPEC `z + Cx x` COMPLEX_NORM_GE_RE_IM) THEN
     REWRITE_TAC[RE_ADD; IM_ADD; RE_CX; IM_CX] THEN ASM_REAL_ARITH_TAC] THEN
    REWRITE_TAC[REAL_ARITH `max m n = if n <= m then m else n`] THEN
    REWRITE_TAC[COND_RAND; COND_RATOR] THEN
    MATCH_MP_TAC INTEGRABLE_CASES THEN REWRITE_TAC[IN_ELIM_THM] THEN
    CONJ_TAC THENL
     [SUBGOAL_THEN
       `{t | &0 <= drop t /\ Re z + drop t <= abs (Im z)} =
        interval[vec 0,lift(abs(Im z) - Re z)]`
       (fun th -> REWRITE_TAC[th; INTEGRABLE_CONST]) THEN
      SIMP_TAC[EXTENSION; IN_ELIM_THM; IN_INTERVAL_1; DROP_VEC; LIFT_DROP] THEN
      REAL_ARITH_TAC;
      ALL_TAC] THEN
    MATCH_MP_TAC(REWRITE_RULE[IMP_IMP] INTEGRABLE_SPIKE_SET) THEN
    EXISTS_TAC
     `{t | abs(Im z) - Re z <= drop t} INTER {t | &0 <= drop t}` THEN
    CONJ_TAC THENL
     [MATCH_MP_TAC NEGLIGIBLE_SUBSET THEN
      EXISTS_TAC `{lift(abs(Im z) - Re z)}` THEN
      REWRITE_TAC[NEGLIGIBLE_SING] THEN
      REWRITE_TAC[SUBSET; IN_DIFF; IN_UNION;
                  IN_ELIM_THM; IN_SING; IN_INTER] THEN
      REWRITE_TAC[GSYM DROP_EQ; LIFT_DROP] THEN ASM_REAL_ARITH_TAC;
      ALL_TAC] THEN
    REWRITE_TAC[GSYM INTEGRABLE_RESTRICT_INTER] THEN
    REWRITE_TAC[integrable_on] THEN
    ONCE_REWRITE_TAC[HAS_INTEGRAL_LIM_AT_POSINFINITY] THEN
    REWRITE_TAC[INTEGRABLE_RESTRICT_INTER; INTEGRAL_RESTRICT_INTER] THEN
    SUBGOAL_THEN
     `!a. {t | abs(Im z) - Re z <= drop t} INTER interval[vec 0,a] =
          interval[lift(max (&0) (abs (Im z) - Re z)),a]`
     (fun th -> REWRITE_TAC[th])
    THENL
     [REWRITE_TAC[EXTENSION; IN_INTER; IN_ELIM_THM; IN_INTERVAL_1] THEN
      REWRITE_TAC[LIFT_DROP; DROP_VEC] THEN REAL_ARITH_TAC;
      ALL_TAC] THEN
    SUBGOAL_THEN
     `!b. &0 <= drop b /\ abs (Im z) - Re z <= drop b
           ==> ((\x. lift(inv (Re z + drop x) pow n)) has_integral
                lift
                (inv((&1 - &n) * (Re z + drop b) pow (n - 1)) -
                 inv((&1 - &n) *
                     (Re z + max(&0) (abs(Im z) - Re z)) pow (n - 1))))
               (interval[lift(max (&0) (abs (Im z) - Re z)),b])`
    ASSUME_TAC THENL
     [REPEAT STRIP_TAC THEN
      MP_TAC(ISPECL
       [`\x. lift(inv((&1 - &n) * (Re z + drop x) pow (n - 1)))`;
        `\x. lift(inv(Re z + drop x) pow n)`;
        `lift(max (&0) (abs (Im z) - Re z))`;
        `b:real^1`] FUNDAMENTAL_THEOREM_OF_CALCULUS) THEN
      ASM_REWRITE_TAC[IN_INTERVAL_1; LIFT_DROP; REAL_MAX_LE] THEN
      ANTS_TAC THENL
       [REWRITE_TAC[FORALL_LIFT; LIFT_DROP; INTERVAL_REAL_INTERVAL] THEN
        REWRITE_TAC[REWRITE_RULE[o_DEF]
         (GSYM HAS_REAL_VECTOR_DERIVATIVE_WITHIN)] THEN
        REWRITE_TAC[REAL_INV_MUL; REAL_INV_POW] THEN REPEAT STRIP_TAC THEN
        REAL_DIFF_TAC THEN
        ASM_SIMP_TAC[GSYM REAL_OF_NUM_SUB; ARITH_RULE `2 <= n ==> 1 <= n`] THEN
        CONJ_TAC THENL [ASM_REAL_ARITH_TAC; ALL_TAC] THEN
        ASM_SIMP_TAC[real_div; REAL_OF_NUM_LE; REAL_FIELD
         `&2 <= x ==> inv(&1 - x) * (x - &1) * y * --(&0 + &1) * p =
                     y * p`] THEN
        REWRITE_TAC[GSYM REAL_INV_MUL; GSYM REAL_INV_POW] THEN
        AP_TERM_TAC THEN REWRITE_TAC[GSYM REAL_POW_ADD] THEN
        AP_TERM_TAC THEN ASM_ARITH_TAC;
        REWRITE_TAC[LIFT_DROP; GSYM LIFT_SUB]];
      ALL_TAC] THEN
    EXISTS_TAC `lift(inv
                  ((&n - &1) *
                   (Re z + max (&0) (abs(Im z) - Re z)) pow (n - 1)))` THEN
    CONJ_TAC THENL
     [X_GEN_TAC `b:real^1` THEN
      ASM_CASES_TAC `&0 <= drop b /\ abs (Im z) - Re z <= drop b` THENL
       [ASM_MESON_TAC[integrable_on]; MATCH_MP_TAC INTEGRABLE_ON_NULL] THEN
      REWRITE_TAC[CONTENT_EQ_0_1; LIFT_DROP] THEN ASM_REAL_ARITH_TAC;
      ALL_TAC] THEN
    MATCH_MP_TAC LIM_TRANSFORM_EVENTUALLY THEN
    EXISTS_TAC
     `\b. lift(inv((&1 - &n) * (Re z + b) pow (n - 1)) -
               inv((&1 - &n) *
                   (Re z + max (&0) (abs(Im z) - Re z)) pow (n - 1)))` THEN
    CONJ_TAC THENL
     [REWRITE_TAC[EVENTUALLY_AT_POSINFINITY; real_ge] THEN
      EXISTS_TAC `max(&0) (abs(Im z) - Re z)` THEN
      REWRITE_TAC[FORALL_DROP; LIFT_DROP] THEN
      REPEAT STRIP_TAC THEN CONV_TAC SYM_CONV THEN
      MATCH_MP_TAC INTEGRAL_UNIQUE THEN FIRST_X_ASSUM MATCH_MP_TAC THEN
      ASM_REAL_ARITH_TAC;
      ALL_TAC] THEN
    REWRITE_TAC[LIFT_SUB; VECTOR_SUB] THEN
    GEN_REWRITE_TAC LAND_CONV [GSYM VECTOR_ADD_LID] THEN
    MATCH_MP_TAC LIM_ADD THEN
    REWRITE_TAC[GSYM LIFT_NEG; GSYM REAL_INV_NEG; GSYM REAL_MUL_LNEG] THEN
    REWRITE_TAC[REAL_NEG_SUB; LIM_CONST] THEN
    REWRITE_TAC[REAL_INV_MUL; LIFT_CMUL] THEN
    MATCH_MP_TAC LIM_NULL_CMUL THEN
    REWRITE_TAC[GSYM LIFT_NUM; GSYM LIM_CX_LIFT] THEN
    REWRITE_TAC[CX_INV; CX_POW; GSYM COMPLEX_POW_INV; CX_ADD] THEN
    ONCE_REWRITE_TAC[COMPLEX_ADD_SYM] THEN
    MATCH_MP_TAC LIM_INV_X_POW_OFFSET THEN ASM_ARITH_TAC) in
  let lemma3 = prove
   (`!z n. 2 <= n /\ (Im z = &0 ==> &0 < Re z)
           ==> (\t. Cx(bernoulli n (frac (drop t))) / (z + Cx(drop t)) pow n)
               integrable_on {t | &0 <= drop t}`,
    REPEAT STRIP_TAC THEN
    REWRITE_TAC[complex_div; GSYM COMPLEX_CMUL] THEN
    MATCH_MP_TAC ABSOLUTELY_INTEGRABLE_IMP_INTEGRABLE THEN
    MATCH_MP_TAC ABSOLUTELY_INTEGRABLE_ON_MUL_BERNOULLI_FRAC THEN
    ASM_SIMP_TAC[GSYM COMPLEX_POW_INV; lemma2]) in
  let lemma4 = prove
   (`!z p. (Im z = &0 ==> &0 < Re z) /\ 1 <= p
           ==> ((\t. Cx(bernoulli 1 (frac(drop t))) / (z + Cx(drop t)))
                has_integral
                (integral {t | &0 <= drop t}
                          (\t. Cx(bernoulli (2 * p + 1) (frac(drop t))) /
                               (z + Cx(drop t)) pow (2 * p + 1)) /
                 Cx(&(2 * p + 1)) -
                 vsum(1..p) (\k. Cx(bernoulli (2 * k) (&0) /
                                    (&4 * &k pow 2 - &2 * &k)) /
                                 z pow (2 * k - 1))))
                {t | &0 <= drop t}`,
    REPEAT STRIP_TAC THEN
    MATCH_MP_TAC HAS_INTEGRAL_LIM_SEQUENTIALLY THEN CONJ_TAC THENL
     [REWRITE_TAC[o_DEF; LIFT_DROP; complex_div; COMPLEX_VEC_0] THEN
      MATCH_MP_TAC LIM_NULL_COMPLEX_LMUL_BOUNDED THEN
      EXISTS_TAC `&1 / &2` THEN CONJ_TAC THENL
       [MATCH_MP_TAC ALWAYS_EVENTUALLY THEN X_GEN_TAC `x:real` THEN
        REWRITE_TAC[BERNOULLI_CONV `bernoulli 1 x`] THEN DISJ1_TAC THEN
        REWRITE_TAC[COMPLEX_NORM_CX] THEN
        MP_TAC(SPEC `x:real` FLOOR_FRAC) THEN REAL_ARITH_TAC;
        MATCH_MP_TAC(REWRITE_RULE[o_DEF] LIM_INFINITY_POSINFINITY_CX) THEN
        ONCE_REWRITE_TAC[COMPLEX_ADD_SYM] THEN REWRITE_TAC[LIM_INV_Z_OFFSET]];
      ALL_TAC] THEN
    MP_TAC(GEN `n:num` (CONJ
     (SPECL [`0`; `n:num`; `z:complex`] lemma1)
     (SPECL [`p:num`; `n:num`; `z:complex`] lemma1))) THEN
    ASM_REWRITE_TAC[FORALL_AND_THM] THEN ONCE_REWRITE_TAC[IMP_CONJ] THEN
    REWRITE_TAC[VSUM_CLAUSES_NUMSEG] THEN CONV_TAC NUM_REDUCE_CONV THEN
    REWRITE_TAC[SIMPLE_COMPLEX_ARITH `a * Cx(&1) / b = a / b`] THEN
    REWRITE_TAC[LIFT_NUM; COMPLEX_POW_1; COMPLEX_DIV_1] THEN
    DISCH_THEN(fun th -> ONCE_REWRITE_TAC[th]) THEN
    REWRITE_TAC[VECTOR_ARITH `a + vec 0 + x:real^N = a + y <=> x = y`] THEN
    DISCH_THEN(fun th -> ONCE_REWRITE_TAC[th]) THEN
    GEN_REWRITE_TAC LAND_CONV [COMPLEX_RING `a - b:complex = --b + a`] THEN
    MATCH_MP_TAC LIM_ADD THEN CONJ_TAC THENL
     [REWRITE_TAC[GSYM VSUM_NEG] THEN MATCH_MP_TAC LIM_VSUM THEN
      REWRITE_TAC[FINITE_NUMSEG; IN_NUMSEG] THEN
      X_GEN_TAC `k:num` THEN STRIP_TAC THEN
      REWRITE_TAC[CX_DIV; complex_div; GSYM COMPLEX_MUL_RNEG] THEN
      REWRITE_TAC[GSYM COMPLEX_MUL_ASSOC] THEN
      MATCH_MP_TAC LIM_COMPLEX_LMUL THEN
      REWRITE_TAC[SIMPLE_COMPLEX_ARITH
       `inv x * (y * w - y * z):complex = y / x * (w - z)`] THEN
      SUBGOAL_THEN
       `Cx(&(FACT(2 * k - 2))) / Cx(&(FACT(2 * k))) =
        inv(Cx(&4 * &k pow 2 - &2 * &k))`
       (fun th -> ONCE_REWRITE_TAC[th])
      THENL
       [MATCH_MP_TAC(COMPLEX_FIELD
         `~(y = Cx(&0)) /\ ~(z = Cx(&0)) /\ x * z = y
          ==> x / y = inv(z)`) THEN
        REWRITE_TAC[CX_INJ; REAL_OF_NUM_EQ; FACT_NZ] THEN
        REWRITE_TAC[REAL_ENTIRE; REAL_ARITH
         `&4 * x pow 2 - &2 * x = (&2 * x) * (&2 * x - &1)`] THEN
        ASM_SIMP_TAC[ARITH_RULE `1 <= k ==> 1 <= 2 * k`; REAL_OF_NUM_SUB;
                     REAL_OF_NUM_MUL; GSYM CX_MUL; CX_INJ; REAL_OF_NUM_EQ] THEN
        CONJ_TAC THENL [ASM_ARITH_TAC; ALL_TAC] THEN
        UNDISCH_TAC `1 <= k` THEN SPEC_TAC(`k:num`,`k:num`) THEN
        INDUCT_TAC THEN REWRITE_TAC[ADD_CLAUSES; MULT_CLAUSES] THEN
        CONV_TAC NUM_REDUCE_CONV THEN DISCH_THEN (K ALL_TAC) THEN
        REWRITE_TAC[ARITH_RULE `(2 + k) - 2 = k /\ (2 + k) - 1 = k + 1`] THEN
        REWRITE_TAC[ARITH_RULE `2 + k = SUC(SUC k)`; FACT] THEN ARITH_TAC;
        MATCH_MP_TAC LIM_COMPLEX_LMUL THEN
        ONCE_REWRITE_TAC[COMPLEX_RING `--z = Cx(&0) - z`] THEN
        MATCH_MP_TAC LIM_SUB THEN REWRITE_TAC[LIM_CONST] THEN
        REWRITE_TAC[GSYM COMPLEX_POW_INV] THEN
        ONCE_REWRITE_TAC[COMPLEX_ADD_SYM] THEN
        MATCH_MP_TAC LIM_INV_N_POW_OFFSET THEN ASM_ARITH_TAC];
      MP_TAC(SPECL [`z:complex`; `2 * p + 1`] lemma3) THEN
      ASM_REWRITE_TAC[] THEN ANTS_TAC THENL [ASM_ARITH_TAC; DISCH_TAC] THEN
      FIRST_ASSUM(MP_TAC o SPEC `Cx(&(FACT(2 * p)))` o
          MATCH_MP INTEGRABLE_COMPLEX_LMUL) THEN
      DISCH_THEN(MP_TAC o MATCH_MP INTEGRABLE_INTEGRAL) THEN
      ASM_SIMP_TAC[INTEGRAL_COMPLEX_LMUL] THEN
      REWRITE_TAC[HAS_INTEGRAL_LIM_AT_POSINFINITY] THEN
      DISCH_THEN(MP_TAC o
          MATCH_MP LIM_POSINFINITY_SEQUENTIALLY o CONJUNCT2) THEN
      DISCH_THEN(MP_TAC o SPEC `inv(Cx(&(FACT(2 * p + 1))))` o
          MATCH_MP LIM_COMPLEX_RMUL) THEN
      MATCH_MP_TAC EQ_IMP THEN AP_THM_TAC THEN BINOP_TAC THENL
       [REWRITE_TAC[LIFT_NUM; FUN_EQ_THM] THEN
        X_GEN_TAC `n:num` THEN REWRITE_TAC[complex_div] THEN
        AP_THM_TAC THEN AP_TERM_TAC THEN MATCH_MP_TAC INTEGRAL_EQ THEN
        REWRITE_TAC[complex_div; COMPLEX_MUL_AC];

        REWRITE_TAC[complex_div] THEN MATCH_MP_TAC(COMPLEX_RING
         `x * y:complex = z ==> (x * i) * y = i * z`) THEN
        REWRITE_TAC[GSYM ADD1; FACT; GSYM REAL_OF_NUM_MUL; CX_MUL] THEN
        MATCH_MP_TAC(COMPLEX_FIELD
         `~(a = Cx(&0)) /\ ~(b = Cx(&0)) ==> a * inv(b * a) = inv b`) THEN
        REWRITE_TAC[CX_INJ; REAL_OF_NUM_EQ; FACT_NZ; NOT_SUC]]]) in
  let lemma5 = prove
   (`!z n. 1 <= n /\ (Im z = &0 ==> &0 < Re z)
           ==> (\t. Cx(bernoulli n (frac (drop t))) / (z + Cx(drop t)) pow n)
               integrable_on {t | &0 <= drop t}`,
    REPEAT STRIP_TAC THEN FIRST_X_ASSUM(DISJ_CASES_TAC o MATCH_MP (ARITH_RULE
     `1 <= n ==> n = 1 \/ 2 <= n`)) THEN
    ASM_SIMP_TAC[lemma3; COMPLEX_POW_1] THEN
    REWRITE_TAC[integrable_on] THEN
    ASM_MESON_TAC[LE_REFL; lemma4]) in
  let lemma6 = prove
   (`!z. (Im z = &0 ==> &0 < Re z)
         ==> lgamma(z) =
             ((z - Cx(&1) / Cx(&2)) * clog(z) - z + Cx(&1)) +
             (integral {t | &0 <= drop t}
                       (\t. Cx(bernoulli 1 (frac(drop t))) /
                            (Cx(&1) + Cx(drop t))) -
              integral {t | &0 <= drop t}
                       (\t. Cx(bernoulli 1 (frac(drop t))) /
                            (z + Cx(drop t))))`,
    REPEAT STRIP_TAC THEN
    SUBGOAL_THEN `!n. ~(z + Cx(&n) = Cx(&0))` ASSUME_TAC THENL
     [REWRITE_TAC[COMPLEX_EQ; IM_ADD; RE_ADD; IM_CX; RE_CX] THEN
      ASM_REAL_ARITH_TAC;
      ALL_TAC] THEN
    SUBGOAL_THEN `~(z = Cx(&0))` ASSUME_TAC THENL
     [ASM_MESON_TAC[COMPLEX_ADD_RID]; ALL_TAC] THEN
    FIRST_ASSUM(MP_TAC o MATCH_MP LGAMMA_ALT) THEN
    MATCH_MP_TAC(REWRITE_RULE[IMP_CONJ_ALT]
     (REWRITE_RULE[TRIVIAL_LIMIT_SEQUENTIALLY]
          (ISPEC `sequentially` LIM_UNIQUE))) THEN
    MATCH_MP_TAC LIM_TRANSFORM_EVENTUALLY THEN EXISTS_TAC
     `\n. z * clog (Cx(&n)) - clog (Cx(&n + &1)) +
          ((Cx(&1) + Cx(&n) + Cx(&1) / Cx(&2)) * clog (Cx(&1) + Cx(&n)) -
           (Cx(&1) - Cx(&1) / Cx(&2)) * clog (Cx(&1)) -
           Cx(&n) +
           integral (interval [lift (&0),lift (&n)])
           (\x. Cx(bernoulli 1 (frac (drop x))) *
                Cx(&1) / (Cx(&1) + Cx(drop x)) pow 1) /
           Cx(&1)) -
          ((z + Cx(&n) + Cx(&1) / Cx(&2)) * clog (z + Cx(&n)) -
           (z - Cx(&1) / Cx(&2)) * clog z -
           Cx(&n) +
           integral (interval [lift (&0),lift (&n)])
           (\x. Cx(bernoulli 1 (frac (drop x))) *
                Cx(&1) / (z + Cx(drop x)) pow 1) /
           Cx(&1))` THEN
    REWRITE_TAC[] THEN CONJ_TAC THENL
     [REWRITE_TAC[EVENTUALLY_SEQUENTIALLY] THEN
      EXISTS_TAC `1` THEN X_GEN_TAC `n:num` THEN DISCH_TAC THEN
      SUBGOAL_THEN
       `clog(Cx(&(FACT n))) =
        vsum(0..n) (\m. clog(Cx(&m) + Cx(&1))) - clog(Cx(&n + &1))`
      SUBST1_TAC THENL
       [REWRITE_TAC[REAL_OF_NUM_ADD; GSYM CX_ADD] THEN
        REWRITE_TAC[GSYM(SPEC `1` VSUM_OFFSET); ADD_CLAUSES] THEN
        SIMP_TAC[GSYM NPRODUCT_FACT; REAL_OF_NUM_NPRODUCT; FINITE_NUMSEG;
                 GSYM CX_LOG; LOG_PRODUCT; PRODUCT_POS_LT; IN_NUMSEG;
                 REAL_OF_NUM_LT; LE_1; GSYM VSUM_CX] THEN
        REWRITE_TAC[GSYM ADD1; VSUM_CLAUSES_NUMSEG] THEN
        REWRITE_TAC[ARITH_RULE `1 <= SUC n`; REAL_OF_NUM_SUC] THEN
        SIMP_TAC[CX_LOG; REAL_OF_NUM_LT; LT_0] THEN
        REWRITE_TAC[COMPLEX_RING `(a + b) - b:complex = a`];
        ONCE_REWRITE_TAC[COMPLEX_RING
         `(a + b - c) - d:complex = (a - c) + (b - d)`]] THEN
      MP_TAC(SPECL [`0`; `n:num`] lemma1) THEN
      CONV_TAC NUM_REDUCE_CONV THEN
      ASM_REWRITE_TAC[VSUM_CLAUSES_NUMSEG; ARITH; VECTOR_ADD_LID] THEN
      ASM_SIMP_TAC[RE_CX; REAL_LT_01];
      ALL_TAC] THEN
    REWRITE_TAC[COMPLEX_POW_1; COMPLEX_DIV_1] THEN
    ONCE_REWRITE_TAC[COMPLEX_RING
     `a + (b - c - n + x) - (d - e - n + y):complex =
      (a + (b - c) - (d - e)) + (x - y)`] THEN
    MATCH_MP_TAC LIM_ADD THEN CONJ_TAC THENL
     [REWRITE_TAC[CLOG_1; COMPLEX_MUL_RZERO; COMPLEX_SUB_RZERO] THEN
      ONCE_REWRITE_TAC[LIM_NULL_COMPLEX] THEN REWRITE_TAC[] THEN
      ONCE_REWRITE_TAC[REAL_ADD_SYM] THEN REWRITE_TAC[CX_ADD] THEN
      MATCH_MP_TAC LIM_TRANSFORM_EVENTUALLY THEN EXISTS_TAC
       `\n. z * (clog(Cx(&n)) - clog(z + Cx(&n))) +
                (Cx(&n) + Cx(&1) / Cx(&2)) *
                (clog(Cx(&1) + Cx(&n)) - clog(z + Cx(&n))) -
                (Cx(&1) - z)` THEN
      CONJ_TAC THENL
       [MATCH_MP_TAC ALWAYS_EVENTUALLY THEN CONV_TAC COMPLEX_RING;
        ALL_TAC] THEN
      MATCH_MP_TAC LIM_NULL_COMPLEX_ADD THEN CONJ_TAC THENL
       [MATCH_MP_TAC LIM_NULL_COMPLEX_LMUL THEN
        GEN_REWRITE_TAC (RATOR_CONV o LAND_CONV o BINDER_CONV o
                         LAND_CONV o RAND_CONV) [GSYM COMPLEX_ADD_LID] THEN
        ONCE_REWRITE_TAC[COMPLEX_ADD_SYM] THEN REWRITE_TAC[LIM_SUB_CLOG];
        ONCE_REWRITE_TAC[COMPLEX_RING
         `(a + h) * x - y:complex = h * x + a * x - y`] THEN
        MATCH_MP_TAC LIM_NULL_COMPLEX_ADD THEN REWRITE_TAC[] THEN
        ONCE_REWRITE_TAC[COMPLEX_ADD_SYM] THEN
        SIMP_TAC[LIM_NULL_COMPLEX_LMUL; LIM_SUB_CLOG] THEN
        REWRITE_TAC[GSYM LIM_NULL_COMPLEX; LIM_N_MUL_SUB_CLOG]];
      REWRITE_TAC[complex_div; COMPLEX_MUL_LID] THEN
      REWRITE_TAC[LIFT_NUM] THEN MATCH_MP_TAC LIM_SUB THEN
      CONJ_TAC THEN REWRITE_TAC[GSYM LIFT_NUM] THEN
      MATCH_MP_TAC LIM_POSINFINITY_SEQUENTIALLY THEN
      REWRITE_TAC[LIFT_NUM] THEN
      MATCH_MP_TAC(MATCH_MP (TAUT `(p <=> q /\ r) ==> (p ==> r)`)
          (SPEC_ALL HAS_INTEGRAL_LIM_AT_POSINFINITY)) THEN
      REWRITE_TAC[GSYM HAS_INTEGRAL_INTEGRAL; GSYM complex_div] THEN
      ONCE_REWRITE_TAC[COMPLEX_RING `z + x:complex = (z + x) pow 1`] THEN
      MATCH_MP_TAC lemma5 THEN
      ASM_REWRITE_TAC[LE_REFL; RE_CX; REAL_LT_01]]) in
  let lemma7 = prove
   (`((\y. integral {t | &0 <= drop t}
            (\t. Cx (bernoulli 1 (frac (drop t))) / (ii * Cx y + Cx(drop t))))
      --> Cx(&0)) at_posinfinity`,
    MATCH_MP_TAC LIM_TRANSFORM_EVENTUALLY THEN
    EXISTS_TAC
     `\y. integral {t | &0 <= drop t}
                   (\t. Cx(bernoulli 3 (frac (drop t))) /
                        (ii * Cx y + Cx(drop t)) pow 3) / Cx(&3) -
          Cx(bernoulli 2 (&0) / &2) / (ii * Cx y)` THEN
    CONJ_TAC THENL
     [REWRITE_TAC[EVENTUALLY_AT_POSINFINITY; real_ge] THEN
      EXISTS_TAC `&1` THEN X_GEN_TAC `y:real` THEN DISCH_TAC THEN
      SUBGOAL_THEN `&0 < y` ASSUME_TAC THENL [ASM_REAL_ARITH_TAC; ALL_TAC] THEN
      MP_TAC(ISPECL [`ii * Cx y`; `1`] lemma4) THEN
      ASM_SIMP_TAC[IM_MUL_II; RE_CX; REAL_LT_IMP_NZ; LE_REFL] THEN
      REWRITE_TAC[VSUM_SING_NUMSEG] THEN
      CONV_TAC NUM_REDUCE_CONV THEN CONV_TAC REAL_RAT_REDUCE_CONV THEN
      REWRITE_TAC[COMPLEX_POW_1] THEN
      DISCH_THEN(SUBST1_TAC o MATCH_MP INTEGRAL_UNIQUE) THEN REFL_TAC;
      ALL_TAC] THEN
    MATCH_MP_TAC LIM_NULL_COMPLEX_SUB THEN CONJ_TAC THENL
     [ALL_TAC;
      REWRITE_TAC[complex_div] THEN MATCH_MP_TAC LIM_NULL_COMPLEX_LMUL THEN
      REWRITE_TAC[COMPLEX_INV_MUL] THEN MATCH_MP_TAC LIM_NULL_COMPLEX_LMUL THEN
      REWRITE_TAC[LIM_INV_X]] THEN
    ONCE_REWRITE_TAC[complex_div] THEN MATCH_MP_TAC LIM_NULL_COMPLEX_RMUL THEN
    MATCH_MP_TAC LIM_NULL_COMPARISON_COMPLEX THEN
    EXISTS_TAC
     `\y. Cx(&1 / &2 / y) *
          integral {t | &0 <= drop t}
                  (\t. inv(Cx(&1) + Cx(drop t)) pow 2)` THEN
    CONJ_TAC THENL
     [ALL_TAC;
      MATCH_MP_TAC LIM_NULL_COMPLEX_RMUL THEN
      REWRITE_TAC[real_div; CX_MUL] THEN
      MATCH_MP_TAC LIM_NULL_COMPLEX_LMUL THEN
      REWRITE_TAC[LIM_INV_X; CX_INV]] THEN
    REWRITE_TAC[EVENTUALLY_AT_POSINFINITY; real_ge] THEN
    EXISTS_TAC `&1` THEN X_GEN_TAC `y:real` THEN DISCH_TAC THEN
    SIMP_TAC[GSYM INTEGRAL_COMPLEX_LMUL; lemma2;
             ABSOLUTELY_INTEGRABLE_IMP_INTEGRABLE; ARITH;
             RE_CX; REAL_OF_NUM_LT] THEN
    MATCH_MP_TAC(REAL_ARITH
      `abs(Re z) <= norm z /\ x <= Re z ==> x <= norm z`) THEN
    REWRITE_TAC[COMPLEX_NORM_GE_RE_IM] THEN REWRITE_TAC[RE_DEF] THEN
    MATCH_MP_TAC INTEGRAL_NORM_BOUND_INTEGRAL_COMPONENT THEN
    REWRITE_TAC[DIMINDEX_2; ARITH] THEN REPEAT CONJ_TAC THENL
     [MATCH_MP_TAC lemma5 THEN
      REWRITE_TAC[ARITH; IM_MUL_II; RE_CX] THEN ASM_REAL_ARITH_TAC;
      MATCH_MP_TAC INTEGRABLE_COMPLEX_LMUL THEN
      MATCH_MP_TAC ABSOLUTELY_INTEGRABLE_IMP_INTEGRABLE THEN
      MATCH_MP_TAC lemma2 THEN
      REWRITE_TAC[RE_CX; REAL_LT_01] THEN ARITH_TAC;
      ALL_TAC] THEN
    REWRITE_TAC[FORALL_LIFT; IN_ELIM_THM; LIFT_DROP; GSYM RE_DEF] THEN
    X_GEN_TAC `x:real` THEN DISCH_TAC THEN
    REWRITE_TAC[GSYM CX_ADD; GSYM CX_INV; GSYM CX_MUL; GSYM CX_POW] THEN
    REWRITE_TAC[COMPLEX_NORM_DIV; COMPLEX_NORM_CX; RE_CX] THEN
    REWRITE_TAC[REAL_ARITH `&1 / &2 / y * x = (&1 / &4) * (&2 / y * x)`] THEN
    REWRITE_TAC[real_div] THEN MATCH_MP_TAC REAL_LE_MUL2 THEN
    REWRITE_TAC[REAL_ABS_POS; REAL_LE_INV_EQ; NORM_POS_LE] THEN CONJ_TAC THENL
     [MP_TAC(SPECL [`3`; `frac x`] BERNOULLI_BOUND) THEN
      CONV_TAC NUM_REDUCE_CONV THEN
      REWRITE_TAC[BERNOULLI_CONV `bernoulli 2 (&0)`] THEN
      ANTS_TAC THENL [ALL_TAC; REAL_ARITH_TAC] THEN
      SIMP_TAC[IN_REAL_INTERVAL; REAL_LT_IMP_LE; FLOOR_FRAC];
      ALL_TAC] THEN
    REWRITE_TAC[REAL_POW_INV; GSYM REAL_INV_MUL; GSYM REAL_MUL_ASSOC] THEN
    GEN_REWRITE_TAC (RAND_CONV o LAND_CONV) [GSYM REAL_INV_INV] THEN
    REWRITE_TAC[GSYM REAL_INV_MUL] THEN MATCH_MP_TAC REAL_LE_INV2 THEN
    CONJ_TAC THENL
     [MATCH_MP_TAC REAL_LT_MUL THEN CONV_TAC REAL_RAT_REDUCE_CONV THEN
      MATCH_MP_TAC REAL_LT_MUL THEN
      CONJ_TAC THENL [ASM_REAL_ARITH_TAC; MATCH_MP_TAC REAL_POW_LT] THEN
      ASM_REAL_ARITH_TAC;
      ALL_TAC] THEN
    REWRITE_TAC[COMPLEX_NORM_POW; REAL_ARITH
       `x pow 3 = (x:real) * x pow 2`] THEN
    ONCE_REWRITE_TAC[REAL_ARITH `inv(&2) * y * x = y * x / &2`] THEN
    MATCH_MP_TAC REAL_LE_MUL2 THEN REPEAT CONJ_TAC THENL
     [ASM_REAL_ARITH_TAC;
      W(MP_TAC o PART_MATCH (rand o rand) COMPLEX_NORM_GE_RE_IM o
        rand o snd) THEN
      REWRITE_TAC[IM_ADD; IM_MUL_II; RE_CX; IM_CX] THEN REAL_ARITH_TAC;
      REWRITE_TAC[REAL_ARITH `&0 <= x / &2 <=> &0 <= x`] THEN
      MATCH_MP_TAC REAL_POW_LE THEN ASM_REAL_ARITH_TAC;

      REWRITE_TAC[COMPLEX_SQNORM] THEN
      REWRITE_TAC[RE_ADD; IM_ADD; RE_MUL_II; IM_MUL_II; IM_CX; RE_CX] THEN
      REWRITE_TAC[REAL_ADD_LID; REAL_ADD_RID; REAL_NEG_0] THEN
      MATCH_MP_TAC(REAL_ARITH
       `&1 pow 2 <= y pow 2 /\ &0 <= (x - &1) pow 2
       ==> (&1 + x) pow 2 / &2 <= x pow 2 + y pow 2`) THEN
      REWRITE_TAC[REAL_LE_POW_2] THEN MATCH_MP_TAC REAL_POW_LE2 THEN
      ASM_REAL_ARITH_TAC]) in
  let lemma8 = prove
   (`integral {t | &0 <= drop t}
              (\t. Cx (bernoulli 1 (frac (drop t))) / (Cx(&1) + Cx(drop t))) =
     Cx(log(&2 * pi) / &2 - &1)`,
    MATCH_MP_TAC(MESON[REAL]
     `real z /\ Re z = y ==> z = Cx y`) THEN
    CONJ_TAC THENL
     [MATCH_MP_TAC REAL_COMPLEX_INTEGRAL THEN CONJ_TAC THENL
       [ONCE_REWRITE_TAC[COMPLEX_RING `z + x:complex = (z + x) pow 1`] THEN
        MATCH_MP_TAC lemma5 THEN
        ASM_REWRITE_TAC[LE_REFL; RE_CX; REAL_LT_01];
        REWRITE_TAC[GSYM CX_ADD; GSYM CX_DIV; REAL_CX]];
      GEN_REWRITE_TAC I [GSYM CX_INJ]] THEN
    MATCH_MP_TAC(ISPEC `at_posinfinity` LIM_UNIQUE) THEN
    EXISTS_TAC
     `\y:real. Cx(Re(integral {t | &0 <= drop t}
          (\t. Cx(bernoulli 1 (frac (drop t))) / (Cx(&1) + Cx(drop t)))))` THEN
    REWRITE_TAC[TRIVIAL_LIMIT_AT_POSINFINITY; LIM_CONST] THEN
    MATCH_MP_TAC LIM_TRANSFORM_EVENTUALLY THEN
    EXISTS_TAC
     `\y. Cx(Re(lgamma(ii * Cx y) -
          ((ii * Cx y - Cx(&1) / Cx(&2)) * clog(ii * Cx y) -
           ii * Cx y + Cx(&1)) +
          integral {t | &0 <= drop t}
                   (\t. Cx(bernoulli 1 (frac(drop t))) /
                        (ii * Cx y + Cx(drop t)))))` THEN
    REWRITE_TAC[EVENTUALLY_AT_POSINFINITY; real_ge] THEN CONJ_TAC THENL
     [EXISTS_TAC `&1` THEN X_GEN_TAC `y:real` THEN DISCH_TAC THEN
      AP_TERM_TAC THEN AP_TERM_TAC THEN
      MP_TAC(SPEC `ii * Cx y` lemma6) THEN
      ASM_SIMP_TAC[IM_MUL_II; RE_CX; REAL_LT_IMP_NZ] THEN
      ANTS_TAC THENL [ASM_REAL_ARITH_TAC; DISCH_THEN SUBST1_TAC] THEN
      REWRITE_TAC[COMPLEX_RING `(s + i - j) - s + j:complex = i`];
      ALL_TAC] THEN
    REWRITE_TAC[RE_ADD; RE_SUB; CX_ADD; CX_SUB] THEN
    GEN_REWRITE_TAC LAND_CONV [GSYM COMPLEX_ADD_RID] THEN
    MATCH_MP_TAC LIM_ADD THEN CONJ_TAC THENL
     [ALL_TAC;
      MATCH_MP_TAC LIM_NULL_COMPARISON_COMPLEX THEN
      EXISTS_TAC `\y. integral {t | &0 <= drop t}
         (\t. Cx(bernoulli 1 (frac(drop t))) / (ii * Cx y + Cx(drop t)))` THEN
      REWRITE_TAC[lemma7] THEN MATCH_MP_TAC ALWAYS_EVENTUALLY THEN
      REWRITE_TAC[COMPLEX_NORM_CX; COMPLEX_NORM_GE_RE_IM]] THEN
    REWRITE_TAC[RE_MUL_II; IM_CX; REAL_NEG_0; COMPLEX_SUB_RZERO; RE_CX] THEN
    MATCH_MP_TAC LIM_TRANSFORM_EVENTUALLY THEN
    EXISTS_TAC
     `\y. Cx(log(norm(cgamma(ii * Cx y)))) -
          (Cx(--(pi * y + log y) / &2) + Cx(&1))` THEN
    REWRITE_TAC[EVENTUALLY_AT_POSINFINITY; real_ge] THEN CONJ_TAC THENL
     [EXISTS_TAC `&1` THEN X_GEN_TAC `y:real` THEN DISCH_TAC THEN
      SUBGOAL_THEN `&0 < y` ASSUME_TAC THENL [ASM_REAL_ARITH_TAC; ALL_TAC] THEN
      BINOP_TAC THENL
       [MP_TAC(SPEC `ii * Cx y` CGAMMA_LGAMMA) THEN COND_CASES_TAC THENL
         [FIRST_X_ASSUM(CHOOSE_THEN (MP_TAC o AP_TERM `Im`)) THEN
          REWRITE_TAC[IM_ADD; IM_MUL_II; IM_CX; RE_CX] THEN ASM_REAL_ARITH_TAC;
          DISCH_THEN SUBST1_TAC THEN REWRITE_TAC[NORM_CEXP; LOG_EXP]];
        AP_THM_TAC THEN AP_TERM_TAC THEN
        AP_TERM_TAC THEN CONV_TAC SYM_CONV THEN
        REWRITE_TAC[COMPLEX_SUB_RDISTRIB; RE_SUB; GSYM CX_DIV] THEN
        REWRITE_TAC[GSYM COMPLEX_MUL_ASSOC; RE_MUL_II; IM_MUL_II; RE_MUL_CX;
                    IM_MUL_CX] THEN
        ASM_SIMP_TAC[RE_CX; IM_CX; REAL_LT_IMP_LE; CX_INJ; REAL_LT_IMP_NZ;
                     GSYM CX_LOG; CLOG_MUL_II] THEN
        REWRITE_TAC[RE_ADD; IM_ADD; RE_CX; IM_CX; RE_MUL_II; IM_MUL_II] THEN
        REAL_ARITH_TAC];
      ALL_TAC] THEN
    MATCH_MP_TAC LIM_TRANSFORM_EVENTUALLY THEN
    EXISTS_TAC
     `\y. Cx((log(&2 * pi) - log(&1 - inv(exp(pi * y) pow 2))) / &2 - &1)` THEN
    REWRITE_TAC[EVENTUALLY_AT_POSINFINITY; real_ge] THEN CONJ_TAC THENL
     [EXISTS_TAC `&1` THEN X_GEN_TAC `y:real` THEN DISCH_TAC THEN
      SUBGOAL_THEN `&0 < y` ASSUME_TAC THENL [ASM_REAL_ARITH_TAC; ALL_TAC] THEN
      MP_TAC(SPEC `ii * Cx y + Cx(&1)` CGAMMA_REFLECTION) THEN
      REWRITE_TAC[CGAMMA_RECURRENCE; COMPLEX_ENTIRE; II_NZ; CX_INJ] THEN
      ASM_SIMP_TAC[REAL_LT_IMP_NZ] THEN
      REWRITE_TAC[COMPLEX_RING `Cx(&1) - (z + Cx(&1)) = --z`] THEN
      MP_TAC(SPEC `cgamma(ii * Cx y)` COMPLEX_NORM_POW_2) THEN
      REWRITE_TAC[CNJ_MUL; CNJ_II; CNJ_CX; CNJ_CGAMMA] THEN
      REWRITE_TAC[COMPLEX_MUL_LNEG; GSYM COMPLEX_MUL_ASSOC] THEN
      DISCH_THEN(SUBST1_TAC o SYM) THEN
      REWRITE_TAC[COMPLEX_RING `w * (z + Cx(&1)) = w * z + w`] THEN
      REWRITE_TAC[CSIN_ADD; GSYM CX_SIN; GSYM CX_COS; SIN_PI; COS_PI] THEN
      REWRITE_TAC[COMPLEX_MUL_RZERO; CX_NEG; COMPLEX_MUL_RNEG] THEN
      REWRITE_TAC[COMPLEX_ADD_RID; COMPLEX_MUL_RID] THEN
      REWRITE_TAC[csin; COMPLEX_RING `--ii * x * ii * y = x * y`] THEN
      REWRITE_TAC[COMPLEX_RING `ii * x * ii * y = --(x * y)`] THEN
      REWRITE_TAC[complex_div; COMPLEX_INV_INV; COMPLEX_INV_MUL;
                  COMPLEX_INV_NEG; COMPLEX_MUL_RNEG] THEN
      ASM_SIMP_TAC[CX_INJ; REAL_LT_IMP_NZ; COMPLEX_FIELD
       `~(y = Cx(&0))
        ==> (ii * y * z = --(p * i * Cx(&2) * ii) <=>
             z = --(Cx(&2) * p) * inv y * i)`] THEN
      REWRITE_TAC[GSYM COMPLEX_INV_MUL; GSYM CX_MUL;
                  GSYM CX_EXP; CEXP_NEG] THEN
      REWRITE_TAC[COMPLEX_MUL_LNEG; GSYM COMPLEX_MUL_RNEG;
                  COMPLEX_NEG_INV] THEN
      REWRITE_TAC[COMPLEX_NEG_SUB] THEN
      REWRITE_TAC[GSYM CX_INV; GSYM CX_SUB; GSYM CX_MUL; GSYM CX_INV] THEN
      REWRITE_TAC[CX_INJ; GSYM CX_POW] THEN
      DISCH_THEN(MP_TAC o AP_TERM `sqrt`) THEN
      REWRITE_TAC[POW_2_SQRT_ABS; REAL_ABS_NORM] THEN
      DISCH_THEN SUBST1_TAC THEN REWRITE_TAC[REAL_INV_MUL] THEN
      SUBGOAL_THEN `&0 < exp(pi * y) - inv(exp(pi * y))` ASSUME_TAC THENL
       [MATCH_MP_TAC(REAL_ARITH `x < &1 /\ &1 < y ==> &0 < y - x`) THEN
        CONJ_TAC THENL [MATCH_MP_TAC REAL_INV_LT_1; ALL_TAC] THEN
        MATCH_MP_TAC REAL_EXP_LT_1 THEN MATCH_MP_TAC REAL_LT_MUL THEN
        ASM_REWRITE_TAC[PI_POS];
        ASM_SIMP_TAC[LOG_MUL; REAL_LT_INV_EQ; PI_POS; REAL_LT_MUL;
                 REAL_ARITH `&0 < &2 * x <=> &0 < x`; LOG_SQRT; LOG_INV]] THEN
      REWRITE_TAC[GSYM CX_ADD; GSYM CX_SUB] THEN
      REWRITE_TAC[REAL_ARITH
      `(l2 + --l + x) / &2 - (--(py + l) / &2 + &1) =
       (l2 + py + x) / &2 - &1`] THEN
      SIMP_TAC[REAL_EXP_NZ; REAL_FIELD
       `~(e = &0) ==> e - inv e = e * (&1 - inv(e pow 2))`] THEN
      SUBGOAL_THEN `inv (exp (pi * y) pow 2) < &1` ASSUME_TAC THENL
       [MATCH_MP_TAC REAL_INV_LT_1 THEN
        MATCH_MP_TAC REAL_POW_LT_1 THEN
        REWRITE_TAC[ARITH] THEN
        MATCH_MP_TAC REAL_EXP_LT_1 THEN
        MATCH_MP_TAC REAL_LT_MUL THEN
        ASM_SIMP_TAC[REAL_LT_MUL; PI_POS];
        ASM_SIMP_TAC[LOG_MUL; REAL_EXP_POS_LT; REAL_SUB_LT; LOG_EXP]] THEN
      REWRITE_TAC[CX_INJ] THEN REAL_ARITH_TAC;
      ALL_TAC] THEN
    REWRITE_TAC[CX_SUB; CX_DIV] THEN MATCH_MP_TAC LIM_SUB THEN
    REWRITE_TAC[LIM_CONST] THEN
    MATCH_MP_TAC LIM_COMPLEX_DIV THEN REWRITE_TAC[LIM_CONST] THEN
    CONJ_TAC THENL [ALL_TAC; CONV_TAC COMPLEX_RING] THEN
    GEN_REWRITE_TAC LAND_CONV [GSYM COMPLEX_SUB_RZERO] THEN
    MATCH_MP_TAC LIM_SUB THEN REWRITE_TAC[LIM_CONST] THEN
    MP_TAC(ISPECL
     [`clog`; `at_posinfinity`;
      `\y. Cx(&1 - inv(exp(pi * y) pow 2))`;
      `Cx(&1)`] LIM_CONTINUOUS_FUNCTION) THEN
    REWRITE_TAC[CLOG_1] THEN ANTS_TAC THENL
     [SIMP_TAC[CONTINUOUS_AT_CLOG; RE_CX; REAL_LT_01; CX_SUB] THEN
      GEN_REWRITE_TAC LAND_CONV [GSYM COMPLEX_SUB_RZERO] THEN
      MATCH_MP_TAC LIM_SUB THEN REWRITE_TAC[LIM_CONST] THEN
      REWRITE_TAC[CX_INV; CX_EXP; CX_POW; GSYM COMPLEX_POW_INV] THEN
      MATCH_MP_TAC LIM_NULL_COMPLEX_POW THEN CONV_TAC NUM_REDUCE_CONV THEN
      REWRITE_TAC[LIM_AT_POSINFINITY] THEN
      X_GEN_TAC `e:real` THEN DISCH_TAC THEN
      EXISTS_TAC `&1 + inv(e)` THEN REWRITE_TAC[dist; real_ge] THEN
      X_GEN_TAC `x:real` THEN DISCH_TAC THEN
      REWRITE_TAC[COMPLEX_SUB_RZERO; COMPLEX_NORM_INV] THEN
      MATCH_MP_TAC REAL_LT_LINV THEN ASM_REWRITE_TAC[NORM_CEXP; RE_CX] THEN
      FIRST_ASSUM(MATCH_MP_TAC o MATCH_MP (REAL_ARITH
       `&1 + e <= x
        ==> &1 + pi * x <= z /\ &0 <= x * (pi - &1)  ==> e < z`)) THEN
      REWRITE_TAC[REAL_EXP_LE_X] THEN MATCH_MP_TAC REAL_LE_MUL THEN
      MP_TAC(SPEC `e:real` REAL_LT_INV_EQ) THEN
      MP_TAC PI_APPROX_32 THEN ASM_REAL_ARITH_TAC;
      MATCH_MP_TAC(REWRITE_RULE[IMP_CONJ] LIM_TRANSFORM_EVENTUALLY) THEN
      REWRITE_TAC[EVENTUALLY_AT_POSINFINITY; real_ge] THEN
      EXISTS_TAC `&1` THEN X_GEN_TAC `y:real` THEN DISCH_TAC THEN
      SUBGOAL_THEN `&0 < y` ASSUME_TAC THENL [ASM_REAL_ARITH_TAC; ALL_TAC] THEN
      MATCH_MP_TAC(GSYM CX_LOG) THEN REWRITE_TAC[REAL_SUB_LT] THEN
      MATCH_MP_TAC REAL_INV_LT_1 THEN MATCH_MP_TAC REAL_POW_LT_1 THEN
      REWRITE_TAC[ARITH] THEN MATCH_MP_TAC REAL_EXP_LT_1 THEN
      MATCH_MP_TAC REAL_LT_MUL THEN ASM_SIMP_TAC[REAL_LT_MUL; PI_POS]]) in
  CONJ_TAC THENL [MATCH_ACCEPT_TAC lemma5; ALL_TAC] THEN
  REPEAT STRIP_TAC THEN ASM_SIMP_TAC[lemma6] THEN
  REWRITE_TAC[lemma8; CX_SUB] THEN
  REWRITE_TAC[COMPLEX_RING
   `(x + Cx(&1)) + (a - Cx(&1)) - b = (x + a) - b`] THEN
  REWRITE_TAC[complex_sub; GSYM COMPLEX_ADD_ASSOC] THEN AP_TERM_TAC THEN
  AP_TERM_TAC THEN AP_TERM_TAC THEN  ASM_CASES_TAC `p = 0` THENL
   [ASM_REWRITE_TAC[VSUM_CLAUSES_NUMSEG] THEN
    CONV_TAC NUM_REDUCE_CONV THEN
    REWRITE_TAC[COMPLEX_POW_1; COMPLEX_DIV_1; VECTOR_ADD_LID];
    MP_TAC(SPECL [`z:complex`; `p:num`] lemma4) THEN
    ASM_SIMP_TAC[LE_1] THEN
    DISCH_THEN(SUBST1_TAC o MATCH_MP INTEGRAL_UNIQUE) THEN
    REWRITE_TAC[complex_sub; COMPLEX_NEG_ADD; VECTOR_NEG_NEG] THEN
    REWRITE_TAC[COMPLEX_ADD_SYM]]);;

let LOG_GAMMA_STIRLING = prove
 (`!x p. &0 < x
         ==> log(gamma x) =
             ((x - &1 / &2) * log(x) - x + log(&2 * pi) / &2) +
             sum(1..p) (\k. bernoulli (2 * k) (&0) /
                            (&4 * &k pow 2 - &2 * &k) / x pow (2 * k - 1)) -
             real_integral {t | &0 <= t}
                           (\t. bernoulli (2 * p + 1) (frac t) /
                                (x + t) pow (2 * p + 1)) /
             &(2 * p + 1)`,
  REPEAT STRIP_TAC THEN
  GEN_REWRITE_TAC I [GSYM REAL_EXP_INJ] THEN
  ASM_SIMP_TAC[EXP_LOG; GAMMA_POS_LT] THEN
  GEN_REWRITE_TAC I [GSYM CX_INJ] THEN REWRITE_TAC[CX_GAMMA] THEN
  MP_TAC(ISPEC `Cx x` CGAMMA_LGAMMA) THEN
  COND_CASES_TAC THENL
   [FIRST_ASSUM(CHOOSE_THEN (MP_TAC o AP_TERM `Re`)) THEN
    REWRITE_TAC[RE_ADD; RE_CX] THEN ASM_REAL_ARITH_TAC;
    ALL_TAC] THEN
  DISCH_THEN SUBST1_TAC THEN REWRITE_TAC[CX_EXP] THEN AP_TERM_TAC THEN
  MP_TAC(ISPECL [`Cx x`; `p:num`] LGAMMA_STIRLING) THEN
  ASM_REWRITE_TAC[RE_CX; CX_GAMMA] THEN DISCH_THEN SUBST1_TAC THEN
  REWRITE_TAC[CX_ADD; CX_SUB; CX_DIV; CX_MUL; GSYM VSUM_CX] THEN
  ASM_SIMP_TAC[GSYM CX_LOG; CX_POW] THEN AP_TERM_TAC THEN AP_TERM_TAC THEN
  AP_THM_TAC THEN AP_TERM_TAC THEN
  REWRITE_TAC[COMPLEX_EQ; RE_CX; IM_CX] THEN
  REWRITE_TAC[RE_DEF; IM_DEF] THEN CONJ_TAC THEN
  W(MP_TAC o PART_MATCH (lhand o rand) INTEGRAL_COMPONENT o lhand o snd) THEN
  ASM_SIMP_TAC[LGAMMA_STIRLING_INTEGRALS_EXIST;
               ARITH_RULE `1 <= 2 * p + 1`; RE_CX] THEN
  DISCH_THEN SUBST1_TAC THEN REWRITE_TAC[GSYM RE_DEF; GSYM IM_DEF] THEN
  REWRITE_TAC[GSYM CX_ADD; GSYM CX_POW; GSYM CX_DIV; RE_CX; IM_CX] THEN
  REWRITE_TAC[LIFT_NUM; INTEGRAL_0; DROP_VEC] THEN
  SUBGOAL_THEN
   `{t | &0 <= drop t} = IMAGE lift {t | &0 <= t}`
  SUBST1_TAC THENL
   [REWRITE_TAC[EXTENSION; IN_IMAGE; IN_ELIM_THM] THEN MESON_TAC[LIFT_DROP];
    MATCH_MP_TAC(GSYM(REWRITE_RULE[o_DEF] REAL_INTEGRAL))] THEN
  REWRITE_TAC[REAL_INTEGRABLE_ON; o_DEF] THEN
  MP_TAC(SPECL [`Cx x`; `2 * p + 1`] LGAMMA_STIRLING_INTEGRALS_EXIST) THEN
  ASM_REWRITE_TAC[RE_CX; ARITH_RULE `1 <= 2 * p + 1`] THEN
  GEN_REWRITE_TAC LAND_CONV [INTEGRABLE_COMPONENTWISE] THEN
  DISCH_THEN(MP_TAC o SPEC `1`) THEN REWRITE_TAC[DIMINDEX_2; ARITH] THEN
  REWRITE_TAC[GSYM CX_ADD; GSYM CX_POW; GSYM CX_DIV; RE_CX; GSYM RE_DEF] THEN
  MATCH_MP_TAC EQ_IMP THEN AP_TERM_TAC THEN
  REWRITE_TAC[EXTENSION; IN_IMAGE; IN_ELIM_THM] THEN MESON_TAC[LIFT_DROP]);;

(* ------------------------------------------------------------------------- *)
(* Some other mathematical facts that don't directly involve the gamma       *)
(* function can now be proved relatively easily: Euler's product for sin,    *)
(* Wallis's product for pi, and the Gaussian integral.                       *)
(* ------------------------------------------------------------------------- *)

let CSIN_PRODUCT = prove
 (`!z. ((\n. z * cproduct(1..n) (\m. Cx(&1) - (z / Cx(pi * &m)) pow 2))
       --> csin(z)) sequentially`,
  GEN_TAC THEN REWRITE_TAC[CX_MUL; complex_div; COMPLEX_INV_MUL] THEN
  REWRITE_TAC[COMPLEX_MUL_ASSOC] THEN REWRITE_TAC[GSYM complex_div] THEN
  ABBREV_TAC `w = z / Cx pi` THEN
  SUBGOAL_THEN `Cx pi * w = z` ASSUME_TAC THENL
   [EXPAND_TAC "w" THEN MP_TAC PI_NZ THEN REWRITE_TAC[GSYM CX_INJ] THEN
    CONV_TAC COMPLEX_FIELD;
    EXPAND_TAC "z" THEN
    SUBGOAL_THEN `csin (Cx pi * w) = Cx pi * csin(Cx pi * w) / Cx pi`
    SUBST1_TAC THENL
     [MP_TAC PI_NZ THEN REWRITE_TAC[GSYM CX_INJ] THEN CONV_TAC COMPLEX_FIELD;
      REWRITE_TAC[GSYM COMPLEX_MUL_ASSOC] THEN
      MATCH_MP_TAC LIM_COMPLEX_LMUL THEN
      ONCE_REWRITE_TAC[GSYM COMPLEX_INV_DIV] THEN
      REWRITE_TAC[GSYM CGAMMA_REFLECTION] THEN
      REWRITE_TAC[COMPLEX_INV_DIV; COMPLEX_INV_MUL] THEN
      POP_ASSUM_LIST(K ALL_TAC) THEN SPEC_TAC(`w:complex`,`z:complex`)]] THEN
  X_GEN_TAC `z:complex` THEN
  MP_TAC(SPEC `z:complex` RECIP_CGAMMA) THEN
  MP_TAC(SPEC `Cx(&1) - z` RECIP_CGAMMA) THEN
  REWRITE_TAC[COMPLEX_RING `Cx(&1) - z + m = (m + Cx(&1)) - z`] THEN
  REWRITE_TAC[GSYM CX_ADD; REAL_OF_NUM_ADD] THEN
  REWRITE_TAC[GSYM
   (ISPECL [`f:num->complex`; `m:num`; `1`] CPRODUCT_OFFSET)] THEN
  SIMP_TAC[CPRODUCT_CLAUSES_LEFT; LE_0] THEN
  REWRITE_TAC[ADD_CLAUSES; COMPLEX_ADD_RID; GSYM IMP_CONJ_ALT] THEN
  SIMP_TAC[CPRODUCT_CLAUSES_RIGHT; ARITH_RULE `0 < n + 1 /\ 1 <= n + 1`] THEN
  DISCH_THEN(MP_TAC o MATCH_MP LIM_COMPLEX_MUL) THEN
  REWRITE_TAC[GSYM REAL_OF_NUM_ADD; CX_ADD; ADD_SUB] THEN
  SUBGOAL_THEN `((\n. Cx(&n) / (Cx(&n) + Cx(&1) - z)) --> Cx(&1)) sequentially`
  MP_TAC THENL
   [GEN_REWRITE_TAC LAND_CONV [GSYM COMPLEX_INV_1] THEN
    ONCE_REWRITE_TAC[GSYM COMPLEX_INV_DIV] THEN
    MATCH_MP_TAC LIM_COMPLEX_INV THEN
    CONJ_TAC THENL [ALL_TAC; CONV_TAC COMPLEX_RING] THEN
    REWRITE_TAC[complex_div; COMPLEX_ADD_RDISTRIB] THEN
    GEN_REWRITE_TAC LAND_CONV [GSYM COMPLEX_ADD_RID] THEN
    MATCH_MP_TAC LIM_ADD THEN
    SIMP_TAC[LIM_INV_N; LIM_NULL_COMPLEX_LMUL] THEN
    MATCH_MP_TAC LIM_EVENTUALLY THEN REWRITE_TAC[EVENTUALLY_SEQUENTIALLY] THEN
    EXISTS_TAC `1` THEN
    SIMP_TAC[COMPLEX_MUL_RINV; CX_INJ; REAL_OF_NUM_EQ; LE_1];
    REWRITE_TAC[IMP_IMP] THEN
    DISCH_THEN(MP_TAC o MATCH_MP LIM_COMPLEX_MUL)] THEN
  REWRITE_TAC[COMPLEX_MUL_LID] THEN
  MATCH_MP_TAC(REWRITE_RULE[IMP_CONJ] LIM_TRANSFORM_EVENTUALLY) THEN
  REWRITE_TAC[EVENTUALLY_SEQUENTIALLY] THEN
  MP_TAC(ISPEC `norm(z:complex)` REAL_ARCH_SIMPLE) THEN
  DISCH_THEN(X_CHOOSE_TAC `N:num`) THEN
  EXISTS_TAC `MAX 1 N` THEN X_GEN_TAC `n:num` THEN
  REWRITE_TAC[ARITH_RULE `MAX a b <= c <=> a <= c /\ b <= c`] THEN
  STRIP_TAC THEN REWRITE_TAC[CPOW_SUB; CPOW_N; COMPLEX_POW_1] THEN
  ASM_SIMP_TAC[CX_INJ; REAL_OF_NUM_EQ; LE_1] THEN
  REWRITE_TAC[complex_div; COMPLEX_INV_MUL; COMPLEX_INV_INV] THEN
  REWRITE_TAC[GSYM COMPLEX_MUL_ASSOC] THEN REWRITE_TAC[COMPLEX_RING
   `a * b * c * d * e * f * g * h * i * j * k :complex =
    (a * i) * (j * e) * (h * b) * c * (d * g) * (f * k)`] THEN
  SUBGOAL_THEN `~((Cx(&n) + Cx(&1)) - z = Cx(&0))` ASSUME_TAC THENL
   [REWRITE_TAC[COMPLEX_SUB_0; GSYM CX_ADD] THEN
    DISCH_THEN(MP_TAC o AP_TERM `norm:complex->real`) THEN
    REWRITE_TAC[COMPLEX_NORM_CX] THEN
    RULE_ASSUM_TAC(REWRITE_RULE[GSYM REAL_OF_NUM_LE]) THEN
    ASM_REAL_ARITH_TAC;
    REWRITE_TAC[COMPLEX_RING `n + Cx(&1) - z = (n + Cx(&1)) - z`]] THEN
  ASM_SIMP_TAC[COMPLEX_MUL_RINV; CX_INJ; REAL_OF_NUM_EQ; CPOW_EQ_0; LE_1] THEN
  REWRITE_TAC[COMPLEX_MUL_LID] THEN AP_TERM_TAC THEN
  SIMP_TAC[GSYM NPRODUCT_FACT; REAL_OF_NUM_NPRODUCT; CX_PRODUCT; FINITE_NUMSEG;
           GSYM CPRODUCT_INV; GSYM CPRODUCT_MUL] THEN
  MATCH_MP_TAC CPRODUCT_EQ THEN X_GEN_TAC `k:num` THEN
  REWRITE_TAC[IN_NUMSEG] THEN STRIP_TAC THEN
  SUBGOAL_THEN `~(Cx(&k) = Cx(&0))` MP_TAC THENL
   [ASM_SIMP_TAC[CX_INJ; REAL_OF_NUM_EQ; LE_1];
    CONV_TAC COMPLEX_FIELD]);;

let SIN_PRODUCT = prove
 (`!x. ((\n. x * product(1..n) (\m. &1 - (x / (pi * &m)) pow 2)) ---> sin(x))
       sequentially`,
  GEN_TAC THEN MP_TAC(SPEC `Cx x` CSIN_PRODUCT) THEN
  REWRITE_TAC[REALLIM_COMPLEX; o_DEF] THEN
  SIMP_TAC[CX_MUL; CX_PRODUCT; FINITE_NUMSEG; CX_SIN] THEN
  REWRITE_TAC[CX_SUB; CX_DIV; CX_POW; CX_MUL]);;

let WALLIS_ALT = prove
 (`((\n. product(1..n) (\k. (&2 * &k) / (&2 * &k - &1) *
                            (&2 * &k) / (&2 * &k + &1))) ---> pi / &2)
   sequentially`,
  ONCE_REWRITE_TAC[GSYM REAL_INV_INV] THEN MATCH_MP_TAC REALLIM_INV THEN
  CONJ_TAC THENL [ALL_TAC; MP_TAC PI_POS THEN CONV_TAC REAL_FIELD] THEN
  MP_TAC(SPEC `pi / &2` SIN_PRODUCT) THEN REWRITE_TAC[SIN_PI2] THEN
  REWRITE_TAC[real_div; REAL_INV_MUL; REAL_MUL_ASSOC; REAL_INV_INV] THEN
  DISCH_THEN(MP_TAC o SPEC `inv pi * &2` o MATCH_MP REALLIM_LMUL) THEN
  SIMP_TAC[REAL_MUL_RID; PI_NZ; REAL_FIELD
   `~(pi = &0) ==> (pi * x) * inv pi = x /\
                   (inv pi * &2) * (pi * inv(&2)) * y = y`] THEN
  MATCH_MP_TAC(REWRITE_RULE[IMP_CONJ] REALLIM_TRANSFORM_EVENTUALLY) THEN
  REWRITE_TAC[EVENTUALLY_SEQUENTIALLY] THEN EXISTS_TAC `1` THEN
  X_GEN_TAC `n:num` THEN DISCH_TAC THEN
  SIMP_TAC[GSYM PRODUCT_INV; FINITE_NUMSEG] THEN
  MATCH_MP_TAC PRODUCT_EQ_NUMSEG THEN REWRITE_TAC[GSYM REAL_OF_NUM_LE] THEN
  CONV_TAC REAL_FIELD);;

let WALLIS = prove
 (`((\n. (&2 pow n * &(FACT n)) pow 4 / (&(FACT(2 * n)) * &(FACT(2 * n + 1))))
    ---> pi / &2) sequentially`,
  MP_TAC WALLIS_ALT THEN
  MATCH_MP_TAC(REWRITE_RULE[IMP_CONJ] REALLIM_TRANSFORM_EVENTUALLY) THEN
  MATCH_MP_TAC ALWAYS_EVENTUALLY THEN REWRITE_TAC[] THEN
  MATCH_MP_TAC num_INDUCTION THEN REWRITE_TAC[PRODUCT_CLAUSES_NUMSEG] THEN
  CONV_TAC NUM_REDUCE_CONV THEN CONV_TAC REAL_RAT_REDUCE_CONV THEN
  X_GEN_TAC `n:num` THEN DISCH_TAC THEN
  ASM_REWRITE_TAC[ARITH_RULE `1 <= SUC n`] THEN
  REWRITE_TAC[GSYM ADD1; ARITH_RULE `2 * SUC n = SUC(SUC(2 * n))`] THEN
  REWRITE_TAC[FACT; real_pow; GSYM REAL_OF_NUM_MUL] THEN
  REWRITE_TAC[GSYM REAL_OF_NUM_SUC; GSYM REAL_OF_NUM_MUL] THEN
  MAP_EVERY (MP_TAC o C SPEC FACT_NZ) [`n:num`; `2 * n`] THEN
  REWRITE_TAC[GSYM REAL_OF_NUM_EQ] THEN CONV_TAC REAL_FIELD);;

let GAUSSIAN_INTEGRAL = prove
 (`((\x. exp(--(x pow 2))) has_real_integral sqrt pi) (:real)`,
  SUBGOAL_THEN
   `((\x. exp(--(x pow 2))) has_real_integral gamma(&1 / &2) / &2)
    {x | &0 <= x}`
  ASSUME_TAC THENL
   [ALL_TAC;
    SUBGOAL_THEN
     `(:real) = {x | &0 <= x} UNION IMAGE (--) {x | &0 <= x}`
    SUBST1_TAC THENL
     [REWRITE_TAC[EXTENSION; IN_UNION; IN_IMAGE; IN_UNIV; IN_ELIM_THM] THEN
      REWRITE_TAC[REAL_ARITH `x:real = --y <=> --x = y`; UNWIND_THM1] THEN
      REAL_ARITH_TAC;
      ONCE_REWRITE_TAC[REAL_ARITH `sqrt x = sqrt x / &2 + sqrt x / &2`]] THEN
    MATCH_MP_TAC HAS_REAL_INTEGRAL_UNION THEN
    REWRITE_TAC[GSYM HAS_REAL_INTEGRAL_REFLECT_GEN] THEN
    ASM_SIMP_TAC[REAL_POW_NEG; ARITH; GSYM GAMMA_HALF] THEN
    MATCH_MP_TAC REAL_NEGLIGIBLE_SUBSET THEN EXISTS_TAC `{&0}` THEN
    REWRITE_TAC[REAL_NEGLIGIBLE_SING; IN_ELIM_THM; SET_RULE
    `s INTER IMAGE f s SUBSET {a} <=> !x. x IN s /\ f x IN s ==> f x = a`] THEN
    REAL_ARITH_TAC] THEN
  MP_TAC(SPEC `&1 / &2` EULER_HAS_REAL_INTEGRAL_GAMMA) THEN
  CONV_TAC REAL_RAT_REDUCE_CONV THEN
  DISCH_THEN(MP_TAC o SPEC `inv(&2)` o MATCH_MP HAS_REAL_INTEGRAL_RMUL) THEN
  REWRITE_TAC[GSYM real_div] THEN
  ONCE_REWRITE_TAC[HAS_REAL_INTEGRAL_ALT] THEN
  REWRITE_TAC[HAS_REAL_INTEGRAL_RESTRICT_INTER;
           REAL_INTEGRABLE_RESTRICT_INTER; REAL_INTEGRAL_RESTRICT_INTER] THEN
  REWRITE_TAC[SUBSET_REAL_INTERVAL] THEN
  REWRITE_TAC[TAUT `p /\ q <=> ~(p ==> ~q)`] THEN
  SIMP_TAC[REAL_ARITH `&0 < B ==> --B <= B /\ ~(B <= --B)`] THEN
  REWRITE_TAC[NOT_IMP; GSYM CONJ_ASSOC; INTER; IN_REAL_INTERVAL] THEN
  REWRITE_TAC[IN_ELIM_THM; REAL_ARITH
   `&0 <= x /\ (a <= x /\ x <= b) <=> max (&0) a <= x /\ x <= b`] THEN
  REWRITE_TAC[GSYM real_interval] THEN
  ONCE_REWRITE_TAC[REAL_ARITH
   `a <= --B /\ --B < B /\ B <= b <=>
    max (&0) a = &0 /\ &0 < B /\ a <= --B /\ B <= b`] THEN
  SIMP_TAC[] THEN
  REWRITE_TAC[REAL_ARITH
   `max (&0) a = &0 /\ &0 < B /\ a <= --B /\ B <= b <=>
    a <= --B /\ &0 < B /\ B <= b`] THEN
  GEN_REWRITE_TAC (BINOP_CONV o ONCE_DEPTH_CONV) [IMP_CONJ] THEN
  REWRITE_TAC[RIGHT_FORALL_IMP_THM; LEFT_FORALL_IMP_THM] THEN
  REWRITE_TAC[MESON[REAL_LE_REFL] `?a. a <= B`] THEN
  ONCE_REWRITE_TAC[
   MESON[REAL_ARITH `a <= max a b /\ (&0 <= a ==> max (&0) a = a)`]
   `(!a. P (max (&0) a)) <=> (!a. &0 <= a ==> P a)`] THEN
  REWRITE_TAC[RIGHT_IMP_FORALL_THM] THEN STRIP_TAC THEN
  SUBGOAL_THEN
   `!a b. &0 <= a /\ a <= b
          ==> ((\x. exp(--(x pow 2))) has_real_integral
               real_integral (real_interval[a pow 2,b pow 2])
                             (\t. t rpow (-- &1 / &2) / exp t / &2))
              (real_interval[a,b])`
  MP_TAC THENL
   [REPEAT STRIP_TAC THEN
    MP_TAC(ISPECL
     [`\t. t rpow (&1 / &2 - &1) / exp t / &2`;
      `\x:real. x pow 2`; `\x. &2 * x`;
      `a:real`; `b:real`; `(a:real) pow 2`; `(b:real) pow 2`; `{&0}`]
     HAS_REAL_INTEGRAL_SUBSTITUTION_STRONG) THEN
    REWRITE_TAC[] THEN CONV_TAC REAL_RAT_REDUCE_CONV THEN
    ASM_SIMP_TAC[REAL_LE_REFL; REAL_LE_POW_2; COUNTABLE_SING;
                 REAL_POW_LE2] THEN
    ANTS_TAC THENL
     [REPEAT CONJ_TAC THENL
       [MATCH_MP_TAC REAL_DIFFERENTIABLE_ON_IMP_REAL_CONTINUOUS_ON THEN
        REWRITE_TAC[REAL_DIFFERENTIABLE_ON_DIFFERENTIABLE] THEN
        REPEAT STRIP_TAC THEN REAL_DIFFERENTIABLE_TAC;
        REWRITE_TAC[SUBSET; FORALL_IN_IMAGE; IN_REAL_INTERVAL] THEN
        SIMP_TAC[REAL_LE_POW_2; GSYM REAL_LE_SQUARE_ABS] THEN
        ASM_REAL_ARITH_TAC;
        REWRITE_TAC[IN_DIFF; IN_SING; IN_REAL_INTERVAL] THEN
        REPEAT STRIP_TAC THENL
         [REAL_DIFF_TAC THEN CONV_TAC NUM_REDUCE_CONV THEN
          REAL_ARITH_TAC;
          MATCH_MP_TAC REAL_DIFFERENTIABLE_IMP_CONTINUOUS_WITHINREAL THEN
          REAL_DIFFERENTIABLE_TAC THEN
          ASM_REWRITE_TAC[REAL_LT_POW_2; REAL_EXP_NZ]]];
      MATCH_MP_TAC(ONCE_REWRITE_RULE[IMP_CONJ]
       (REWRITE_RULE[CONJ_ASSOC] HAS_REAL_INTEGRAL_SPIKE)) THEN
      EXISTS_TAC `{&0}` THEN REWRITE_TAC[REAL_NEGLIGIBLE_SING] THEN
      X_GEN_TAC `x:real` THEN
      REWRITE_TAC[IN_REAL_INTERVAL; IN_DIFF; IN_SING] THEN STRIP_TAC THEN
      SUBGOAL_THEN `&0 <= x` ASSUME_TAC THENL
       [ASM_REAL_ARITH_TAC; ALL_TAC] THEN
      REWRITE_TAC[REAL_EXP_NEG; GSYM RPOW_POW] THEN
      ASM_SIMP_TAC[RPOW_RPOW] THEN CONV_TAC REAL_RAT_REDUCE_CONV THEN
      REWRITE_TAC[RPOW_NEG; RPOW_POW; REAL_POW_1] THEN
      MP_TAC(SPEC `(x:real) pow 2` REAL_EXP_NZ) THEN
      UNDISCH_TAC `~(x = &0)` THEN CONV_TAC REAL_FIELD];
    FIRST_X_ASSUM(K ALL_TAC o SPECL [`a:real`; `b:real`]) THEN
    DISCH_THEN(LABEL_TAC "*")] THEN
  CONJ_TAC THENL
   [MAP_EVERY X_GEN_TAC [`a:real`; `b:real`] THEN STRIP_TAC THEN
    DISJ_CASES_TAC(REAL_ARITH `b <= a \/ a <= b`) THEN
    ASM_SIMP_TAC[REAL_INTEGRABLE_ON_NULL] THEN
    REWRITE_TAC[real_integrable_on] THEN ASM_MESON_TAC[];
    ALL_TAC] THEN
  X_GEN_TAC `e:real` THEN DISCH_TAC THEN
  REMOVE_THEN "*" (MP_TAC o SPEC `&0`) THEN
  REWRITE_TAC[REAL_LE_REFL; HAS_REAL_INTEGRAL_INTEGRABLE_INTEGRAL] THEN
  ONCE_REWRITE_TAC[REAL_ARITH
   `&0 < B /\ B <= b <=> &0 < B /\ B <= b /\ &0 <= b`] THEN
  SIMP_TAC[] THEN DISCH_THEN(K ALL_TAC) THEN CONV_TAC REAL_RAT_REDUCE_CONV THEN
  FIRST_X_ASSUM(MP_TAC o SPEC `e:real`) THEN ASM_REWRITE_TAC[] THEN
  DISCH_THEN(X_CHOOSE_THEN `B:real` STRIP_ASSUME_TAC) THEN
  EXISTS_TAC `max B (&1)` THEN
  ASM_REWRITE_TAC[REAL_LT_MAX; REAL_MAX_LE] THEN
  X_GEN_TAC `b:real` THEN STRIP_TAC THEN FIRST_X_ASSUM MATCH_MP_TAC THEN
  ASM_REWRITE_TAC[] THEN TRANS_TAC REAL_LE_TRANS `(b:real) pow 1` THEN
  CONJ_TAC THENL [ASM_REAL_ARITH_TAC; ALL_TAC] THEN
  MATCH_MP_TAC REAL_POW_MONO THEN ASM_REWRITE_TAC[ARITH]);;