-
Notifications
You must be signed in to change notification settings - Fork 9
/
tform.hl
1340 lines (1242 loc) · 58.3 KB
/
tform.hl
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
(* ========================================================================== *)
(* Formal verification of FPTaylor certificates *)
(* *)
(* Author: Alexey Solovyev, University of Utah *)
(* *)
(* This file is distributed under the terms of the MIT licence *)
(* ========================================================================== *)
(* -------------------------------------------------------------------------- *)
(* Definitions and theorems for Taylor forms *)
(* -------------------------------------------------------------------------- *)
needs "lib.hl";;
needs "ipow.hl";;
needs "rounding.hl";;
module Tform = struct
open Lib;;
open Ipow;;
open Rounding;;
prioritize_real();;
parse_as_infix ("::", (12, "right"));;
override_interface ("::", `CONS`);;
make_overloadable "++" `:A->A->A`;;
overload_interface ("++", `APPEND`);;
let tform_exists = prove
(`?s:(real^N->real) # ((real^N->real) # (real^N->real) # real)list. T`,
REWRITE_TAC[]);;
let tform_type_bij = new_type_definition "tform" ("mk_tform", "dest_tform") tform_exists;;
let mk_tform, dest_tform = list_to_pair (CONJUNCTS (REWRITE_RULE[] tform_type_bij));;
let tform_f0 = new_definition `tform_f0 t = FST (dest_tform t)`;;
let tform_list = new_definition `tform_list t = SND (dest_tform t)`;;
let tform_dim = new_definition `tform_dim t = LENGTH (tform_list t)`;;
let tform_f1 = new_definition
`tform_f1 t = \x:real^N. sum_list (tform_list t) (\ (f1,e1,e2). f1 x * e1 x)`;;
let mul_f1 = new_definition
`mul_f1 g list = MAP (\ (f1,e1,e2). (\x. g x * f1 x), e1, e2) list`;;
let dest_components = prove
(`!t:(N)tform. dest_tform t = (tform_f0 t, tform_list t)`,
REWRITE_TAC[tform_f0; tform_list]);;
let mk_components = prove
(`!t:(N)tform. mk_tform (tform_f0 t, tform_list t) = t`,
REWRITE_TAC[GSYM dest_components; mk_tform]);;
let f0_mk = prove
(`!(f0:real^N->real) r. tform_f0 (mk_tform (f0, r)) = f0`,
REWRITE_TAC[tform_f0; dest_tform]);;
let list_mk = prove
(`!(f0:real^N->real) r. tform_list (mk_tform (f0, r)) = r`,
REWRITE_TAC[tform_list; dest_tform]);;
let f1_mk = prove
(`!(f0:real^N->real) r. tform_f1 (mk_tform (f0, r)) = \x. sum_list r (\ (f1,e1,e2). f1 x * e1 x)`,
REWRITE_TAC[tform_f1; list_mk]);;
let approx = new_definition
`approx (dom:real^N->bool) h (t:(N)tform) <=>
(!x. x IN dom ==>
h x = tform_f0 t x + tform_f1 t x /\
ALL (\(f1, e1, e2). abs (e1 x) <= e2) (tform_list t))`;;
let triple_exists = prove
(`!x'. ?(f1:real^N->real) (e1:real^N->real) (e2:real). x' = f1, e1, e2`,
MESON_TAC[PAIR_SURJECTIVE]);;
let sum_list_mul1 = prove
(`!g list (x:real^N). sum_list (mul_f1 g list) (\ (f1,e1,e2:real). (f1 x * e1 x)) =
g x * sum_list list (\ (f1,e1,e2). (f1 x * e1 x))`,
REWRITE_TAC[mul_f1; sum_list_map; GSYM sum_list_lmul] THEN REPEAT GEN_TAC THEN
MATCH_MP_TAC sum_list_eq THEN REWRITE_TAC[GSYM ALL_MEM] THEN REPEAT STRIP_TAC THEN
STRIP_ASSUME_TAC (SPEC_ALL triple_exists) THEN ASM_REWRITE_TAC[o_THM; REAL_MUL_ASSOC]);;
let all_e12_mul1 = prove
(`!g list P. ALL (\ (f1:real^N->real,e1:real^N->real,e2:real). P e1 e2) list ==>
ALL (\ (f1,e1,e2). P e1 e2) (mul_f1 g list)`,
REPEAT GEN_TAC THEN REWRITE_TAC[GSYM ALL_MEM; mul_f1; MEM_MAP] THEN
REPEAT STRIP_TAC THEN ASM_REWRITE_TAC[] THEN POP_ASSUM (K ALL_TAC) THEN
POP_ASSUM (fun th -> FIRST_X_ASSUM (MP_TAC o C MATCH_MP th)) THEN
STRIP_ASSUME_TAC (SPEC_ALL triple_exists) THEN ASM_REWRITE_TAC[]);;
let approx_subset = prove
(`!s1 s2 h (t:(N)tform).
approx s1 h t /\ s2 SUBSET s1 ==> approx s2 h t`,
REWRITE_TAC[approx; SUBSET] THEN REPEAT STRIP_TAC THEN
FIRST_X_ASSUM (MP_TAC o SPEC `x:real^N`) THEN ASM_SIMP_TAC[]);;
let approx_tform_f1 = prove
(`!s h t. approx s h t ==>
!x:real^N. x IN s ==>
abs (tform_f1 t x) <= sum_list (tform_list t) (\ (f1, e1, e2). abs (f1 x) * e2)`,
REWRITE_TAC[approx; tform_f1; GSYM ALL_EL] THEN REPEAT STRIP_TAC THEN
MATCH_MP_TAC sum_list_abs_le THEN REWRITE_TAC[GSYM ALL_EL] THEN
REPEAT STRIP_TAC THEN FIRST_X_ASSUM (MP_TAC o SPEC `x:real^N`) THEN ASM_REWRITE_TAC[] THEN
STRIP_TAC THEN FIRST_X_ASSUM (MP_TAC o SPEC `i:num`) THEN ASM_REWRITE_TAC[] THEN
STRIP_ASSUME_TAC (SPEC `EL i (tform_list (t:(N)tform))` triple_exists) THEN
ASM_REWRITE_TAC[REAL_ABS_MUL] THEN DISCH_TAC THEN
MATCH_MP_TAC REAL_LE_LMUL THEN ASM_REWRITE_TAC[REAL_ABS_POS]);;
let approx_tform_f1_simple = prove
(`!s s2 h t ms. approx s h t /\
s SUBSET s2 /\
(!x. x IN s2 ==> ALL2 (\ (f1,e1,e2) m. abs (f1 x) * e2 <= m) (tform_list t) ms)
==> !x:real^N. x IN s ==>
abs (tform_f1 t x) <= sum_list ms I`,
REWRITE_TAC[SUBSET] THEN REPEAT STRIP_TAC THEN
MP_TAC (SPEC_ALL approx_tform_f1) THEN ASM_REWRITE_TAC[] THEN
DISCH_THEN (MP_TAC o SPEC `x:real^N`) THEN ASM_REWRITE_TAC[] THEN DISCH_TAC THEN
MATCH_MP_TAC REAL_LE_TRANS THEN
EXISTS_TAC `sum_list (tform_list t) (\ (f1,e1,e2). abs (f1 (x:real^N)) * e2)` THEN
ASM_REWRITE_TAC[] THEN MATCH_MP_TAC sum_list_le_gen THEN
FIRST_X_ASSUM (MP_TAC o SPEC `x:real^N`) THEN ASM_SIMP_TAC[I_THM] THEN
REWRITE_TAC[all2_el] THEN REPEAT STRIP_TAC THEN ASM_REWRITE_TAC[] THEN
STRIP_ASSUME_TAC (SPEC `EL i (tform_list (t:(N)tform))` triple_exists) THEN
FIRST_X_ASSUM (MP_TAC o SPEC `i:num`) THEN ASM_SIMP_TAC[]);;
let approx_bound_gen = prove
(`!s h t. approx s h t ==>
!x:real^N. x IN s ==>
abs (h x - tform_f0 t x)
<= sum_list (tform_list t) (\ (f1, e1, e2). abs (f1 x) * e2)`,
REPEAT GEN_TAC THEN DISCH_TAC THEN FIRST_ASSUM MP_TAC THEN
FIRST_X_ASSUM (ASSUME_TAC o MATCH_MP approx_tform_f1) THEN
REWRITE_TAC[approx] THEN REPEAT STRIP_TAC THEN ASM_SIMP_TAC[] THEN
ASM_SIMP_TAC[REAL_ARITH `(a + b) - a = b:real`]);;
let approx_bound = prove
(`!s s2 h t m. approx s h t /\
s SUBSET s2 /\
(!x. x IN s2 ==> sum_list (tform_list t) (\ (f1,e1,e2). abs (f1 x) * e2) <= m)
==> !x:real^N. x IN s ==> abs (h x - tform_f0 t x) <= m`,
REWRITE_TAC[SUBSET] THEN REPEAT STRIP_TAC THEN
MP_TAC (SPECL[`s:real^N->bool`; `h:real^N->real`; `t:(N)tform`] approx_bound_gen) THEN
ASM_SIMP_TAC[] THEN
DISCH_THEN (MP_TAC o SPEC `x:real^N`) THEN ASM_REWRITE_TAC[] THEN DISCH_TAC THEN
ASM_MESON_TAC[REAL_LE_TRANS]);;
let approx_bound_simple = prove
(`!s s2 h t ms. approx s h t /\
s SUBSET s2 /\
(!x. x IN s2 ==> ALL2 (\ (f1,e1,e2) m. abs (f1 x) * e2 <= m) (tform_list t) ms)
==> !x:real^N. x IN s ==>
abs (h x - tform_f0 t x) <= sum_list ms I`,
REPEAT GEN_TAC THEN STRIP_TAC THEN
MATCH_MP_TAC approx_bound THEN EXISTS_TAC `s2:real^N->bool` THEN
ASM_REWRITE_TAC[] THEN REPEAT STRIP_TAC THEN
MATCH_MP_TAC sum_list_le_gen THEN
FIRST_X_ASSUM (MP_TAC o SPEC `x:real^N`) THEN ASM_REWRITE_TAC[I_THM] THEN
REWRITE_TAC[all2_el] THEN REPEAT STRIP_TAC THEN ASM_REWRITE_TAC[] THEN
STRIP_ASSUME_TAC (SPEC `EL i (tform_list (t:(N)tform))` triple_exists) THEN
FIRST_X_ASSUM (MP_TAC o SPEC `i:num`) THEN ASM_SIMP_TAC[]);;
(* ---------------------------- *)
(* const *)
(* ---------------------------- *)
let approx_const = prove
(`!s c. approx s (\x:real^N. c)
(mk_tform ((\x. c), []))`,
REWRITE_TAC[approx; tform_f1; f0_mk; list_mk; sum_list_nil; ALL; REAL_ADD_RID]);;
let approx_rnd_bin_const = prove
(`!s c rnd a e2 d2 s2 n b. is_rnd_bin(a,e2,d2) s2 rnd /\ c IN s2 /\
abs c <= &2 ipow n /\
~(e2 = &0) /\ &0 < a /\
a * (&2 ipow (n - &1) + d2 / e2) <= b
==> approx s (\x:real^N. rnd c)
(mk_tform ((\x. c), [(\x. b), (\x. (rnd c - c) / b), e2]))`,
REWRITE_TAC[approx; tform_f1; f0_mk; list_mk; is_rnd_bin] THEN
REPEAT GEN_TAC THEN STRIP_TAC THEN GEN_TAC THEN DISCH_TAC THEN
SUBGOAL_THEN `&0 < e2 /\ &0 <= d2` ASSUME_TAC THENL [
FIRST_X_ASSUM (MP_TAC o SPEC `c:real`) THEN ASM_REWRITE_TAC[] THEN
STRIP_TAC THEN ASM_ARITH_TAC;
ALL_TAC
] THEN
SUBGOAL_THEN `&0 < b` ASSUME_TAC THENL [
MATCH_MP_TAC REAL_LTE_TRANS THEN EXISTS_TAC `a * (&2 ipow (n - &1) + d2 / e2)` THEN
ASM_REWRITE_TAC[] THEN MATCH_MP_TAC REAL_LT_MUL THEN ASM_REWRITE_TAC[] THEN
MATCH_MP_TAC REAL_LTE_ADD THEN
CONJ_TAC THENL [ MATCH_MP_TAC IPOW_LT_0 THEN REAL_ARITH_TAC; ALL_TAC ] THEN
MATCH_MP_TAC REAL_LE_DIV THEN ASM_SIMP_TAC[REAL_LT_IMP_LE];
ALL_TAC
] THEN
SUBGOAL_THEN `~(b = &0)` ASSUME_TAC THENL [ ASM_ARITH_TAC; ALL_TAC ] THEN
REWRITE_TAC[sum_list_nil; sum_list_cons; ALL] THEN
ASM_SIMP_TAC[REAL_ARITH `b * x / b = x * b / b`; REAL_DIV_REFL] THEN
CONJ_TAC THENL [ REAL_ARITH_TAC; ALL_TAC ] THEN
FIRST_X_ASSUM (MP_TAC o SPEC `c:real`) THEN ASM_REWRITE_TAC[] THEN
STRIP_TAC THEN ASM_REWRITE_TAC[REAL_ARITH `(c + a) - c = a`] THEN
ASM_SIMP_TAC[REAL_ABS_DIV; REAL_ARITH `&0 < b ==> abs b = b`] THEN
ASM_SIMP_TAC[REAL_LE_LDIV_EQ] THEN
MATCH_MP_TAC REAL_LE_TRANS THEN EXISTS_TAC `e2 * a * (&2 ipow (n - &1) + d2 / e2)` THEN
CONJ_TAC THENL [
REWRITE_TAC[REAL_ARITH `e2 * a * (x + d2 / e2) = a * (x * e2 + d2 * (e2 / e2))`] THEN
ASM_SIMP_TAC[REAL_DIV_REFL; REAL_MUL_RID] THEN
ASM_SIMP_TAC[REAL_ABS_MUL; REAL_ARITH `&0 < a ==> abs a = a`; REAL_LE_LMUL_EQ] THEN
MATCH_MP_TAC REAL_LE_TRANS THEN EXISTS_TAC `abs (p2max c * e) + abs d` THEN
REWRITE_TAC[REAL_ABS_TRIANGLE] THEN
MATCH_MP_TAC REAL_LE_ADD2 THEN ASM_SIMP_TAC[REAL_ABS_MUL] THEN
MATCH_MP_TAC REAL_LE_MUL2 THEN ASM_SIMP_TAC[p2max_abs; p2max_bound] THEN
ASM_SIMP_TAC[p2max_pos; REAL_ABS_POS];
ALL_TAC
] THEN
MATCH_MP_TAC REAL_LE_MUL2 THEN ASM_REWRITE_TAC[REAL_LE_REFL] THEN
ASM_SIMP_TAC[REAL_LT_IMP_LE; REAL_LE_MUL_EQ] THEN
MATCH_MP_TAC REAL_LE_ADD THEN ASM_SIMP_TAC[REAL_LE_DIV; REAL_LT_IMP_LE] THEN
MATCH_MP_TAC IPOW_LE_0 THEN REAL_ARITH_TAC);;
(* ---------------------------- *)
(* var *)
(* ---------------------------- *)
let approx_var = prove
(`!s i. approx s (\x:real^N. x$i)
(mk_tform ((\x. x$i), []))`,
REWRITE_TAC[approx; tform_f1; f0_mk; list_mk; sum_list_nil; ALL; REAL_ADD_RID]);;
let approx_rnd_bin_var = prove
(`!s i rnd a e2 d2 s2 n b. is_rnd_bin(a,e2,d2) s2 rnd /\
(!x. x IN s ==> abs (x$i) <= &2 ipow n /\ x$i IN s2) /\
~(e2 = &0) /\ &0 < a /\
a * (&2 ipow (n - &1) + d2 / e2) <= b
==> approx s (\x:real^N. rnd (x$i))
(mk_tform ((\x. x$i), [(\x. b), (\x. (rnd (x$i) - x$i) / b), e2]))`,
REWRITE_TAC[approx; tform_f1; f0_mk; list_mk; is_rnd_bin] THEN
REPEAT GEN_TAC THEN STRIP_TAC THEN GEN_TAC THEN DISCH_TAC THEN
SUBGOAL_THEN `&0 < e2 /\ &0 <= d2` ASSUME_TAC THENL [
FIRST_X_ASSUM (MP_TAC o SPEC `(x:real^N)$i`) THEN ASM_SIMP_TAC[] THEN
STRIP_TAC THEN ASM_ARITH_TAC;
ALL_TAC
] THEN
SUBGOAL_THEN `&0 < b` ASSUME_TAC THENL [
MATCH_MP_TAC REAL_LTE_TRANS THEN EXISTS_TAC `a * (&2 ipow (n - &1) + d2 / e2)` THEN
ASM_REWRITE_TAC[] THEN MATCH_MP_TAC REAL_LT_MUL THEN ASM_REWRITE_TAC[] THEN
MATCH_MP_TAC REAL_LTE_ADD THEN
CONJ_TAC THENL [ MATCH_MP_TAC IPOW_LT_0 THEN REAL_ARITH_TAC; ALL_TAC ] THEN
MATCH_MP_TAC REAL_LE_DIV THEN ASM_SIMP_TAC[REAL_LT_IMP_LE];
ALL_TAC
] THEN
SUBGOAL_THEN `~(b = &0)` ASSUME_TAC THENL [ ASM_ARITH_TAC; ALL_TAC ] THEN
REWRITE_TAC[sum_list_nil; sum_list_cons; ALL] THEN
ASM_SIMP_TAC[REAL_ARITH `b * x / b = x * b / b`; REAL_DIV_REFL] THEN
CONJ_TAC THENL [ REAL_ARITH_TAC; ALL_TAC ] THEN
FIRST_X_ASSUM (MP_TAC o SPEC `(x:real^N)$i`) THEN ASM_SIMP_TAC[] THEN
STRIP_TAC THEN ASM_REWRITE_TAC[REAL_ARITH `(c + a) - c = a`] THEN
ASM_SIMP_TAC[REAL_ABS_DIV; REAL_ARITH `&0 < b ==> abs b = b`] THEN
ASM_SIMP_TAC[REAL_LE_LDIV_EQ] THEN
MATCH_MP_TAC REAL_LE_TRANS THEN EXISTS_TAC `e2 * a * (&2 ipow (n - &1) + d2 / e2)` THEN
CONJ_TAC THENL [
REWRITE_TAC[REAL_ARITH `e2 * a * (x + d2 / e2) = a * (x * e2 + d2 * (e2 / e2))`] THEN
REWRITE_TAC[REAL_ARITH `r * a * e + a * d = a * (r * e + d)`] THEN
ASM_SIMP_TAC[REAL_DIV_REFL; REAL_MUL_RID] THEN
ASM_SIMP_TAC[REAL_ABS_MUL; REAL_ARITH `&0 < a ==> abs a = a`; REAL_LE_LMUL_EQ] THEN
MATCH_MP_TAC REAL_LE_TRANS THEN EXISTS_TAC `abs (p2max (x:real^N$i) * e) + abs d` THEN
REWRITE_TAC[REAL_ABS_TRIANGLE] THEN
MATCH_MP_TAC REAL_LE_ADD2 THEN ASM_SIMP_TAC[REAL_ABS_MUL] THEN
MATCH_MP_TAC REAL_LE_MUL2 THEN ASM_SIMP_TAC[p2max_abs; p2max_bound] THEN
ASM_SIMP_TAC[p2max_pos; REAL_ABS_POS];
ALL_TAC
] THEN
MATCH_MP_TAC REAL_LE_MUL2 THEN ASM_REWRITE_TAC[REAL_LE_REFL] THEN
ASM_SIMP_TAC[REAL_LT_IMP_LE; REAL_LE_MUL_EQ] THEN
MATCH_MP_TAC REAL_LE_ADD THEN ASM_SIMP_TAC[REAL_LE_DIV; REAL_LT_IMP_LE] THEN
MATCH_MP_TAC IPOW_LE_0 THEN REAL_ARITH_TAC);;
(* ---------------------------- *)
(* neg *)
(* ---------------------------- *)
let approx_neg = prove
(`!s h t. approx s h t
==> approx s (\x:real^N. --h x)
(mk_tform ((\x. --tform_f0 t x),
MAP (\ (f1,e1,e2). (\x. --f1 x), e1, e2) (tform_list t)))`,
REWRITE_TAC[approx; tform_f1] THEN REPEAT STRIP_TAC THEN ASM_SIMP_TAC[f0_mk; list_mk] THENL [
REWRITE_TAC[REAL_ARITH `(--(a + b) = --a + c) <=> --b = c`] THEN
REWRITE_TAC[sum_list_map; sum_list_neg] THEN
MATCH_MP_TAC sum_list_eq THEN REWRITE_TAC[GSYM ALL_EL] THEN REPEAT STRIP_TAC THEN
STRIP_ASSUME_TAC (SPEC `EL i (tform_list (t:(N)tform))` triple_exists) THEN
ASM_REWRITE_TAC[o_THM; REAL_MUL_LNEG];
ALL_TAC
] THEN
FIRST_X_ASSUM (MP_TAC o SPEC `x:real^N`) THEN ASM_REWRITE_TAC[] THEN
REWRITE_TAC[GSYM ALL_EL; LENGTH_MAP] THEN REPEAT STRIP_TAC THEN
FIRST_X_ASSUM (MP_TAC o SPEC `i:num`) THEN ASM_REWRITE_TAC[] THEN
STRIP_ASSUME_TAC (SPEC `EL i (tform_list (t:(N)tform))` triple_exists) THEN
ASM_SIMP_TAC[EL_MAP]);;
(* ---------------------------- *)
(* add *)
(* ---------------------------- *)
let approx_add = prove
(`!s h1 h2 t1 t2. approx s h1 t1 /\ approx s h2 t2
==> approx s (\x:real^N. h1 x + h2 x)
(mk_tform ((\x. tform_f0 t1 x + tform_f0 t2 x),
tform_list t1 ++ tform_list t2))`,
REWRITE_TAC[approx; tform_f1] THEN REPEAT STRIP_TAC THEN
ASM_SIMP_TAC[f0_mk; list_mk; ALL_APPEND] THEN
REWRITE_TAC[sum_list_append] THEN REAL_ARITH_TAC);;
(* ---------------------------- *)
(* sub *)
(* ---------------------------- *)
let approx_sub = prove
(`!s h1 h2 t1 t2. approx s h1 t1 /\ approx s h2 t2
==> approx s (\x:real^N. h1 x - h2 x)
(mk_tform ((\x. tform_f0 t1 x - tform_f0 t2 x),
tform_list t1 ++ MAP (\(f1,e1,e2). (\x. --f1 x), e1, e2) (tform_list t2)))`,
REPEAT STRIP_TAC THEN REWRITE_TAC[real_sub] THEN
ABBREV_TAC `f1 = MAP (\(f1,e1,e2). (\x:real^N. --f1 x),e1,e2) (tform_list t2)` THEN
ABBREV_TAC `t3 = mk_tform ((\x:real^N. --tform_f0 t2 x), f1)` THEN
SUBGOAL_THEN `f1 = tform_list (t3:(N)tform) /\ (!x. --tform_f0 t2 x = tform_f0 t3 x)` ASSUME_TAC THENL [
EXPAND_TAC "t3" THEN REWRITE_TAC[f0_mk; list_mk];
ALL_TAC
] THEN
ASM_REWRITE_TAC[] THEN MATCH_MP_TAC approx_add THEN ASM_REWRITE_TAC[] THEN
EXPAND_TAC "t3" THEN EXPAND_TAC "f1" THEN MATCH_MP_TAC approx_neg THEN ASM_REWRITE_TAC[]);;
(* ---------------------------- *)
(* mul *)
(* ---------------------------- *)
let approx_mul_0 = prove
(`!s h1 h2 t1 t2.
approx s h1 t1 /\ approx s h2 t2 /\
(tform_list t1 = [] \/ tform_list t2 = [])
==> approx s (\x:real^N. h1 x * h2 x)
(mk_tform ((\x. tform_f0 t1 x * tform_f0 t2 x),
(mul_f1 (tform_f0 t2) (tform_list t1) ++
mul_f1 (tform_f0 t1) (tform_list t2))))`,
REWRITE_TAC[approx; tform_f1] THEN REPEAT GEN_TAC THEN REWRITE_TAC[f0_mk; list_mk] THEN
REPEAT STRIP_TAC THEN
ASM_SIMP_TAC[sum_list_nil; sum_list_append; sum_list_mul1; ALL_APPEND; all_e12_mul1] THEN
REAL_ARITH_TAC);;
let approx_mul = prove
(`!s h1 h2 t1 t2 m2 e2.
approx s h1 t1 /\ approx s h2 t2 /\
(!x. x IN s ==> abs (tform_f1 t1 x * tform_f1 t2 x) <= m2 * e2) /\
&0 < e2
==> approx s (\x:real^N. h1 x * h2 x)
(mk_tform ((\x. tform_f0 t1 x * tform_f0 t2 x),
(mul_f1 (tform_f0 t2) (tform_list t1) ++
mul_f1 (tform_f0 t1) (tform_list t2) ++
[(\x. m2), (\x. (tform_f1 t1 x * tform_f1 t2 x) / m2), e2])))`,
REWRITE_TAC[approx; tform_f1] THEN REPEAT GEN_TAC THEN ASM_SIMP_TAC[f0_mk; list_mk] THEN
STRIP_TAC THEN GEN_TAC THEN DISCH_TAC THEN
ABBREV_TAC `r = \x:real^N. tform_f1 t1 x * tform_f1 t2 x` THEN
STRIP_TAC THENL [
REWRITE_TAC[sum_list_append; sum_list_mul1] THEN
REWRITE_TAC[REAL_ARITH `(a + b) * (c + d) = a * c + c * b + a * d + b * d`] THEN
ASM_REWRITE_TAC[REAL_EQ_ADD_LCANCEL; sum_list_sing] THEN
SUBGOAL_THEN `m2 * r (x:real^N) / m2 = r x` MP_TAC THENL [
ASM_CASES_TAC `m2 = &0` THENL [
FIRST_X_ASSUM (MP_TAC o SPEC `x:real^N`) THEN ASM_REWRITE_TAC[REAL_MUL_LZERO] THEN
EXPAND_TAC "r" THEN REWRITE_TAC[tform_f1] THEN REAL_ARITH_TAC;
ALL_TAC
] THEN
ASM_SIMP_TAC[REAL_ARITH `m2 * a / m2 = (m2 / m2) * a`; REAL_DIV_REFL; REAL_MUL_LID];
ALL_TAC
] THEN
EXPAND_TAC "r" THEN SIMP_TAC[tform_f1];
ALL_TAC
] THEN
REWRITE_TAC[ALL_APPEND; ALL] THEN ASM_SIMP_TAC[all_e12_mul1] THEN
ASM_CASES_TAC `m2 = &0` THENL [
ASM_REWRITE_TAC[real_div; REAL_INV_0; REAL_MUL_RZERO] THEN
ASM_SIMP_TAC[REAL_ABS_0; REAL_LT_IMP_LE];
ALL_TAC
] THEN
MATCH_MP_TAC REAL_LE_LCANCEL_IMP THEN EXISTS_TAC `m2:real` THEN
SUBGOAL_THEN `&0 < m2` ASSUME_TAC THENL [
SUBGOAL_THEN `&0 <= m2 * e2` MP_TAC THENL [
FIRST_X_ASSUM (MP_TAC o SPEC `x:real^N`) THEN ASM_REWRITE_TAC[] THEN REAL_ARITH_TAC;
ALL_TAC
] THEN
ASM_SIMP_TAC[REAL_LE_MUL_EQ] THEN
POP_ASSUM MP_TAC THEN REAL_ARITH_TAC;
ALL_TAC
] THEN
ASM_SIMP_TAC[REAL_ABS_DIV; REAL_ARITH `&0 < m2 ==> abs m2 = m2`] THEN
REWRITE_TAC[REAL_ARITH `a * b / c = a / c * b`; real_div] THEN
ASM_SIMP_TAC[REAL_MUL_RINV; REAL_MUL_LID]);;
(* ---------------------------- *)
(* rounding *)
(* ---------------------------- *)
let approx_rnd_e0 = prove
(`!s h t rnd a d2 s2. approx s h t /\ is_rnd(a,&0,d2) s2 rnd /\
(!x:real^N. x IN s ==> h x IN s2) /\
&0 < a
==> approx s (\x. rnd (h x))
(mk_tform (tform_f0 t,
(((\x. a), (\x. (rnd (h x) - h x) / a), d2) :: tform_list t)))`,
REWRITE_TAC[approx; tform_f1; f0_mk; list_mk; is_rnd] THEN
REPEAT GEN_TAC THEN STRIP_TAC THEN GEN_TAC THEN DISCH_TAC THEN
SUBGOAL_THEN `~(a = &0)` ASSUME_TAC THENL [ ASM_ARITH_TAC; ALL_TAC ] THEN
FIRST_X_ASSUM (MP_TAC o SPEC `h (x:real^N):real`) THEN
ANTS_TAC THENL [ ASM_SIMP_TAC[]; ALL_TAC] THEN STRIP_TAC THEN
SUBGOAL_THEN `e = &0` ASSUME_TAC THENL [ ASM_ARITH_TAC; ALL_TAC ] THEN
CONJ_TAC THENL [
REWRITE_TAC[sum_list_cons; REAL_ARITH `a * b / a = (a / a) * b`] THEN
ASM_SIMP_TAC[REAL_ARITH `(a + b + c) - a = b + c`; REAL_MUL_RZERO] THEN
ASM_SIMP_TAC[REAL_DIV_REFL] THEN REAL_ARITH_TAC;
ALL_TAC
] THEN
ASM_SIMP_TAC[ALL; REAL_ARITH `(h + a * t) - h = a * t`; REAL_MUL_RZERO] THEN
REWRITE_TAC[REAL_ARITH `(a * (&0 + d)) / a = (a / a) * d`] THEN
ASM_SIMP_TAC[REAL_DIV_REFL; REAL_MUL_LID]);;
let approx_rnd_0 = prove
(`!s h t rnd c e2 d2 s2. approx s h t /\ is_rnd(c,e2,d2) s2 rnd /\
(!x:real^N. x IN s ==> h x IN s2) /\
tform_list t = []
==> let e, d = select_rnd(c,e2,d2) s rnd h in
approx s (\x. rnd (h x))
(mk_tform (tform_f0 t,
[((\x. c * tform_f0 t x), e, e2); ((\x. c), d, d2)]))`,
REWRITE_TAC[approx; tform_f1] THEN REPEAT STRIP_TAC THEN REWRITE_TAC[f0_mk; list_mk] THEN
REPEAT LET_TAC THEN
MP_TAC ((SPEC_ALL o ISPECL[`rnd:real->real`; `h:real^N->real`]) is_rnd_select) THEN
ASM_REWRITE_TAC[] THEN DISCH_THEN (ASSUME_TAC o CONV_RULE let_CONV) THEN
GEN_TAC THEN DISCH_TAC THEN
ASM_SIMP_TAC[sum_list_nil; sum_list_cons; ALL] THEN
REAL_ARITH_TAC);;
(*
let approx_rnd_alt = prove
(`!s h t rnd e2 d2 s2 m2. approx s h t /\ is_rnd(e2,d2) s2 rnd /\
(!x:real^N. x IN s ==> h x IN s2) /\
(!x. x IN s ==> abs (tform_f1 t x) <= m2)
==> let e, d = select_rnd(e2,d2) s rnd h in
approx s (\x. rnd (h x))
(mk_tform (tform_f0 t,
((tform_f0 t, e, e2) :: ((\x. m2), (\x. (e x * tform_f1 t x) / m2), e2)
:: ((\x. &1), d, d2) :: tform_list t)))`,
REWRITE_TAC[approx; tform_f1] THEN REPEAT STRIP_TAC THEN REWRITE_TAC[f0_mk; list_mk] THEN
REPEAT LET_TAC THEN
MP_TAC ((SPEC_ALL o ISPECL[`rnd:real->real`; `h:real^N->real`]) is_rnd_select) THEN
ASM_REWRITE_TAC[] THEN DISCH_THEN (ASSUME_TAC o CONV_RULE let_CONV) THEN
GEN_TAC THEN DISCH_TAC THEN
ABBREV_TAC `p = sum_list (tform_list (t:(N)tform)) (\(f1,e1,dd). f1 x * e1 x)` THEN
REPEAT STRIP_TAC THENL [
ASM_SIMP_TAC[o_THM; sum_list_cons] THEN
SUBGOAL_THEN `m2 * (e (x:real^N) * p) / m2 = e x * p` (fun th -> REWRITE_TAC[th]) THENL [
ASM_CASES_TAC `m2 = &0` THENL [
ASM_REWRITE_TAC[REAL_MUL_LZERO; EQ_SYM_EQ] THEN
REWRITE_TAC[REAL_ENTIRE] THEN DISJ2_TAC THEN
FIRST_X_ASSUM (K ALL_TAC o SPEC `x:real^N`) THEN
FIRST_X_ASSUM (MP_TAC o SPEC `x:real^N`) THEN ASM_REWRITE_TAC[] THEN
REAL_ARITH_TAC;
ALL_TAC
] THEN
POP_ASSUM MP_TAC THEN CONV_TAC REAL_FIELD;
ALL_TAC
] THEN
REAL_ARITH_TAC;
ALL_TAC
] THEN
ASM_SIMP_TAC[ALL] THEN
ASM_CASES_TAC `m2 = &0` THENL [
ASM_REWRITE_TAC[real_div; REAL_INV_0; REAL_MUL_RZERO; REAL_ABS_0] THEN
FIRST_X_ASSUM (MP_TAC o SPEC `x:real^N`) THEN ASM_REWRITE_TAC[] THEN
REAL_ARITH_TAC;
ALL_TAC
] THEN
REWRITE_TAC[REAL_ARITH `(a * b) / c = a * (b / c)`; REAL_ABS_MUL] THEN
MATCH_MP_TAC REAL_LE_TRANS THEN EXISTS_TAC `e2 * &1` THEN
CONJ_TAC THENL [ ALL_TAC; REAL_ARITH_TAC ] THEN
MATCH_MP_TAC REAL_LE_MUL2 THEN ASM_SIMP_TAC[REAL_ABS_POS] THEN
MATCH_MP_TAC REAL_LE_LCANCEL_IMP THEN EXISTS_TAC `abs m2` THEN
CONJ_TAC THENL [ POP_ASSUM MP_TAC THEN REAL_ARITH_TAC; ALL_TAC ] THEN
REWRITE_TAC[REAL_ABS_DIV; REAL_ARITH `a * b / c = (a / c) * b`] THEN
ASM_SIMP_TAC[real_div; REAL_MUL_RINV; REAL_ABS_ZERO] THEN
FIRST_X_ASSUM (K ALL_TAC o SPEC `x:real^N`) THEN
FIRST_X_ASSUM (MP_TAC o SPEC `x:real^N`) THEN ASM_REWRITE_TAC[] THEN
REAL_ARITH_TAC);;
*)
let approx_rnd = prove
(`!s h t rnd c e2 d2 s2 m2 b. approx s h t /\ is_rnd(c,e2,d2) s2 rnd /\
(!x:real^N. x IN s ==> h x IN s2) /\
(!x. x IN s ==> abs (tform_f1 t x) <= m2) /\
~(e2 = &0) /\ &0 < c /\
c * (m2 + d2 / e2) <= b
==> let e, d = select_rnd(c,e2,d2) s rnd h in
let r = (\x. e x * sum_list (tform_list t) (\ (f1,e1,dd). f1 x * e1 x) + d x) in
approx s (\x. rnd (h x))
(mk_tform (tform_f0 t,
CONS ((\x. c * tform_f0 t x), e, e2)
(CONS ((\x. b), (\x. (c * r x) / b), e2)
(tform_list t))))`,
REWRITE_TAC[approx; tform_f1] THEN REPEAT STRIP_TAC THEN REWRITE_TAC[f0_mk; list_mk] THEN
REPEAT LET_TAC THEN
MP_TAC ((SPEC_ALL o ISPECL[`rnd:real->real`; `h:real^N->real`]) is_rnd_select) THEN
ASM_REWRITE_TAC[] THEN DISCH_THEN (ASSUME_TAC o CONV_RULE let_CONV) THEN
GEN_TAC THEN DISCH_TAC THEN
ABBREV_TAC `p = sum_list (tform_list (t:(N)tform)) (\(f1,e1,dd). f1 x * e1 x)` THEN
SUBGOAL_THEN `&0 <= m2 /\ &0 < e2 /\ &0 <= d2 /\ ~(c = &0)` (LABEL_TAC "ineqs") THENL [
FIRST_X_ASSUM (MP_TAC o SPEC `x:real^N`) THEN
FIRST_X_ASSUM (MP_TAC o SPEC `x:real^N`) THEN
UNDISCH_TAC `~(e2 = &0)` THEN UNDISCH_TAC `&0 < c` THEN
ASM_REWRITE_TAC[] THEN REAL_ARITH_TAC;
ALL_TAC
] THEN
SUBGOAL_THEN `c * (m2 + d2 / e2) = &0 ==> c * r (x:real^N) = &0` ASSUME_TAC THENL [
ASM_REWRITE_TAC[REAL_ENTIRE] THEN DISCH_TAC THEN
SUBGOAL_THEN `m2 = &0 /\ d2 = &0` ASSUME_TAC THENL [
SUBGOAL_THEN `&0 <= d2 / e2` ASSUME_TAC THENL [
MATCH_MP_TAC REAL_LE_DIV THEN ASM_SIMP_TAC[REAL_LT_IMP_LE];
ALL_TAC
] THEN
MP_TAC (REAL_ARITH `m2 + d2 / e2 = &0 /\ &0 <= m2 /\ &0 <= d2 / e2 ==> m2 = &0 /\ d2 / e2 = &0`) THEN
ASM_SIMP_TAC[] THEN
UNDISCH_TAC `~(e2 = &0)` THEN CONV_TAC REAL_FIELD;
ALL_TAC
] THEN
ASM_REWRITE_TAC[REAL_MUL_LZERO; EQ_SYM_EQ] THEN EXPAND_TAC "r" THEN
FIRST_X_ASSUM (MP_TAC o SPEC `x:real^N`) THEN
FIRST_X_ASSUM (MP_TAC o SPEC `x:real^N`) THEN ASM_REWRITE_TAC[] THEN
SIMP_TAC[REAL_ARITH `!t. abs t <= &0 ==> t = &0`] THEN REAL_ARITH_TAC;
ALL_TAC
] THEN
REPEAT STRIP_TAC THENL [
ASM_SIMP_TAC[o_THM; sum_list_cons] THEN
SUBGOAL_THEN `b * (c * r (x:real^N)) / b = c * r x` (fun th -> REWRITE_TAC[th]) THENL [
ASM_CASES_TAC `b = &0` THENL [
ASM_REWRITE_TAC[real_div; REAL_INV_0; REAL_MUL_RZERO] THEN
ONCE_REWRITE_TAC[EQ_SYM_EQ] THEN FIRST_X_ASSUM MATCH_MP_TAC THEN
MATCH_MP_TAC (REAL_ARITH `b = &0 /\ a <= b /\ b <= a ==> a = &0`) THEN
ASM_REWRITE_TAC[] THEN MATCH_MP_TAC REAL_LE_MUL THEN
ASM_SIMP_TAC[REAL_LT_IMP_LE] THEN
MATCH_MP_TAC REAL_LE_ADD THEN ASM_REWRITE_TAC[] THEN
MATCH_MP_TAC REAL_LE_DIV THEN ASM_SIMP_TAC[REAL_LT_IMP_LE];
ALL_TAC
] THEN
POP_ASSUM MP_TAC THEN CONV_TAC REAL_FIELD;
ALL_TAC
] THEN
EXPAND_TAC "r" THEN ASM_REWRITE_TAC[] THEN REAL_ARITH_TAC;
ALL_TAC
] THEN
ASM_SIMP_TAC[ALL] THEN
ASM_CASES_TAC `c * (m2 + d2 / e2) = &0` THENL [
ASM_SIMP_TAC[real_div; REAL_MUL_LZERO; REAL_ABS_0; REAL_LT_IMP_LE];
ALL_TAC
] THEN
SUBGOAL_THEN `&0 < c * (m2 + d2 / e2)` ASSUME_TAC THENL [
ASM_REWRITE_TAC[REAL_ARITH `&0 < a <=> ~(a = &0) /\ &0 <= a`] THEN
MATCH_MP_TAC REAL_LE_MUL THEN ASM_SIMP_TAC[REAL_LT_IMP_LE] THEN
MATCH_MP_TAC REAL_LE_ADD THEN ASM_REWRITE_TAC[] THEN
MATCH_MP_TAC REAL_LE_DIV THEN ASM_SIMP_TAC[REAL_LT_IMP_LE];
ALL_TAC
] THEN
SUBGOAL_THEN `&0 < b` ASSUME_TAC THENL [
POP_ASSUM MP_TAC THEN UNDISCH_TAC `c * (m2 + d2 / e2) <= b` THEN REAL_ARITH_TAC;
ALL_TAC
] THEN
EXPAND_TAC "r" THEN ASM_REWRITE_TAC[] THEN
MATCH_MP_TAC REAL_LE_TRANS THEN
EXISTS_TAC `abs ((c * (e2 * m2 + e2 * d2 / e2)) / (c * (m2 + d2 / e2)))` THEN
CONJ_TAC THENL [
ASM_SIMP_TAC[REAL_ABS_DIV; REAL_ARITH `&0 < t ==> abs t = t`; RAT_LEMMA4] THEN
MATCH_MP_TAC REAL_LE_MUL2 THEN ASM_SIMP_TAC[REAL_ABS_POS; REAL_LT_IMP_LE] THEN
ASM_SIMP_TAC[REAL_ABS_MUL; REAL_ARITH `&0 < c ==> &0 < abs c`; REAL_LE_LMUL_EQ] THEN
MATCH_MP_TAC REAL_LE_TRANS THEN EXISTS_TAC `e2 * m2 + d2` THEN
CONJ_TAC THENL [
MATCH_MP_TAC REAL_LE_TRANS THEN EXISTS_TAC `abs (e x * p) + abs (d (x:real^N))` THEN
REWRITE_TAC[REAL_ABS_TRIANGLE] THEN MATCH_MP_TAC REAL_LE_ADD2 THEN
ASM_SIMP_TAC[REAL_ABS_MUL] THEN MATCH_MP_TAC REAL_LE_MUL2 THEN
FIRST_X_ASSUM (MP_TAC o SPEC `x:real^N`) THEN
FIRST_X_ASSUM (MP_TAC o SPEC `x:real^N`) THEN
ASM_SIMP_TAC[REAL_ABS_POS];
ALL_TAC
] THEN
SUBGOAL_THEN `&0 <= e2 * m2` MP_TAC THENL [
MATCH_MP_TAC REAL_LE_MUL THEN ASM_SIMP_TAC[REAL_LT_IMP_LE];
ALL_TAC
] THEN
ASM_SIMP_TAC[REAL_ARITH `e2 * d2 / e2 = (e2 / e2) * d2`; REAL_DIV_REFL] THEN
REMOVE_THEN "ineqs" MP_TAC THEN REAL_ARITH_TAC;
ALL_TAC
] THEN
REWRITE_TAC[REAL_ARITH `(c * (e2 * m2 + e2 * d2 / e2)) / r = e2 * (c * (m2 + d2 / e2)) / r`] THEN
ASM_SIMP_TAC[real_div; REAL_MUL_RINV; REAL_MUL_RID; REAL_ARITH `&0 < e2 ==> abs e2 <= e2`]);;
(* --------------------------- *)
(* Taylor 2 *)
(* --------------------------- *)
let taylor2 = prove
(`!f f' f'' a t.
(!x. abs x <= abs t ==>
(f has_real_derivative f'(a + x)) (atreal (a + x)) /\
(f' has_real_derivative f''(a + x)) (atreal (a + x)))
==> ?p. abs p <= abs t /\
f(a + t) = f(a) + f'(a) * t + f''(a + p) * t pow 2 / &2`,
REPEAT STRIP_TAC THEN
ABBREV_TAC `g = \i. EL i [f:real->real; f'; f'']` THEN
SUBGOAL_THEN `g 0 = (f:real->real) /\ g 1 = f' /\ g 2 = f''` ASSUME_TAC THENL [
EXPAND_TAC "g" THEN REWRITE_TAC[ONE; TWO; EL; HD; TL];
ALL_TAC
] THEN
ASM_CASES_TAC `t = &0` THENL [
EXISTS_TAC `&0` THEN ASM_REWRITE_TAC[REAL_LE_REFL] THEN
REWRITE_TAC[REAL_MUL_RZERO; REAL_ADD_RID; REAL_POW_2; real_div; REAL_MUL_LZERO];
ALL_TAC
] THEN
ASM_CASES_TAC `&0 < t` THENL [
MP_TAC (SPECL[`g:num->real->real`; `a:real`; `a + t:real`; `1`] REAL_TAYLOR_MVT_POS) THEN
ANTS_TAC THENL [
CONJ_TAC THENL [ POP_ASSUM MP_TAC THEN REAL_ARITH_TAC; ALL_TAC ] THEN
REWRITE_TAC[IN_REAL_INTERVAL; ARITH_RULE `i <= 1 <=> i = 0 \/ i = 1`] THEN
REPEAT STRIP_TAC THEN ASM_REWRITE_TAC[ADD; ARITH_RULE `1 + 1 = 2`] THENL [
MATCH_MP_TAC HAS_REAL_DERIVATIVE_ATREAL_WITHIN THEN
FIRST_X_ASSUM (MP_TAC o SPEC `t' - a:real`) THEN
ANTS_TAC THENL [ ASM_ARITH_TAC; ALL_TAC ] THEN
SIMP_TAC[REAL_ARITH `a + t' - a = t'`];
ALL_TAC
] THEN
MATCH_MP_TAC HAS_REAL_DERIVATIVE_ATREAL_WITHIN THEN
FIRST_X_ASSUM (MP_TAC o SPEC `t' - a:real`) THEN
ANTS_TAC THENL [ ASM_ARITH_TAC; ALL_TAC ] THEN
SIMP_TAC[REAL_ARITH `a + t' - a = t'`];
ALL_TAC
] THEN
CONV_TAC ((ONCE_DEPTH_CONV EXPAND_SUM_CONV) THENC NUM_REDUCE_CONV) THEN
ASM_REWRITE_TAC[IN_REAL_INTERVAL; real_pow; REAL_POW_1] THEN STRIP_TAC THEN
EXISTS_TAC `t' - a:real` THEN
ASM_REWRITE_TAC[REAL_ARITH `!t. (a + t) - a = t /\ a + t - a = t`] THEN
ASM_ARITH_TAC;
ALL_TAC
] THEN
MP_TAC (SPECL[`g:num->real->real`; `a:real`; `a + t:real`; `1`] REAL_TAYLOR_MVT_NEG) THEN
ANTS_TAC THENL [
CONJ_TAC THENL [ ASM_ARITH_TAC; ALL_TAC ] THEN
REWRITE_TAC[IN_REAL_INTERVAL; ARITH_RULE `i <= 1 <=> i = 0 \/ i = 1`] THEN
REPEAT STRIP_TAC THEN ASM_REWRITE_TAC[ADD; ARITH_RULE `1 + 1 = 2`] THENL [
MATCH_MP_TAC HAS_REAL_DERIVATIVE_ATREAL_WITHIN THEN
FIRST_X_ASSUM (MP_TAC o SPEC `t' - a:real`) THEN
ANTS_TAC THENL [ ASM_ARITH_TAC; ALL_TAC ] THEN
SIMP_TAC[REAL_ARITH `a + t' - a = t'`];
ALL_TAC
] THEN
MATCH_MP_TAC HAS_REAL_DERIVATIVE_ATREAL_WITHIN THEN
FIRST_X_ASSUM (MP_TAC o SPEC `t' - a:real`) THEN
ANTS_TAC THENL [ ASM_ARITH_TAC; ALL_TAC ] THEN
SIMP_TAC[REAL_ARITH `a + t' - a = t'`];
ALL_TAC
] THEN
CONV_TAC ((ONCE_DEPTH_CONV EXPAND_SUM_CONV) THENC NUM_REDUCE_CONV) THEN
ASM_REWRITE_TAC[IN_REAL_INTERVAL; real_pow; REAL_POW_1] THEN STRIP_TAC THEN
EXISTS_TAC `t' - a:real` THEN
ASM_REWRITE_TAC[REAL_ARITH `!t. (a + t) - a = t /\ a + t - a = t`] THEN
ASM_ARITH_TAC);;
let diff2_sin_cos = prove
(`!x. (sin has_real_derivative cos x) (atreal x) /\
(cos has_real_derivative --sin x) (atreal x) /\
((\x. --sin x) has_real_derivative --cos x) (atreal x)`,
GEN_TAC THEN REWRITE_TAC[HAS_REAL_DERIVATIVE_SIN; HAS_REAL_DERIVATIVE_COS] THEN
MATCH_MP_TAC HAS_REAL_DERIVATIVE_NEG THEN REWRITE_TAC[HAS_REAL_DERIVATIVE_SIN]);;
let diff2_atn = prove
(`!x. (atn has_real_derivative inv (&1 + x pow 2)) (atreal x) /\
((\x. inv (&1 + x pow 2)) has_real_derivative -- &2 * x * inv(&1 + x pow 2) pow 2) (atreal x)`,
GEN_TAC THEN REWRITE_TAC[HAS_REAL_DERIVATIVE_ATN] THEN
REAL_DIFF_TAC THEN
REWRITE_TAC[ARITH_RULE `2 - 1 = 1`; REAL_POW_1; real_div; REAL_INV_POW] THEN
CONJ_TAC THENL [ ALL_TAC; REAL_ARITH_TAC ] THEN
MATCH_MP_TAC (REAL_ARITH `&0 <= x ==> ~(&1 + x = &0)`) THEN
REWRITE_TAC[REAL_LE_POW_2]);;
let diff2_acs = prove
(`!x. abs x < &1 ==>
(acs has_real_derivative --inv (sqrt (&1 - x pow 2))) (atreal x) /\
((\x. --inv (sqrt (&1 - x pow 2))) has_real_derivative --x * inv (sqrt (&1 - x pow 2) pow 3)) (atreal x)`,
REPEAT STRIP_TAC THENL [
ASM_SIMP_TAC[HAS_REAL_DERIVATIVE_ACS];
REAL_DIFF_TAC THEN REWRITE_TAC[SQRT_EQ_0] THEN
ABBREV_TAC `r = &1 - x pow 2` THEN
SUBGOAL_THEN `&0 < r` ASSUME_TAC THENL [
SUBGOAL_THEN `abs (x pow 2) < &1` MP_TAC THENL [
ASM_REWRITE_TAC[REAL_ABS_POW; ABS_SQUARE_LT_1; REAL_ABS_ABS];
ALL_TAC
] THEN
EXPAND_TAC "r" THEN REAL_ARITH_TAC;
ALL_TAC
] THEN
ASM_SIMP_TAC[REAL_ARITH `&0 < r ==> ~(r = &0)`] THEN
REWRITE_TAC[real_div; REAL_INV_POW; REAL_INV_MUL; ARITH_RULE `2 - 1 = 1`] THEN
REAL_ARITH_TAC
]);;
let diff2_asn = prove
(`!x. abs x < &1 ==>
(asn has_real_derivative inv (sqrt (&1 - x pow 2))) (atreal x) /\
((\x. inv (sqrt (&1 - x pow 2))) has_real_derivative x * inv (sqrt (&1 - x pow 2) pow 3)) (atreal x)`,
REPEAT STRIP_TAC THENL [
ASM_SIMP_TAC[HAS_REAL_DERIVATIVE_ASN];
REAL_DIFF_TAC THEN REWRITE_TAC[SQRT_EQ_0] THEN
ABBREV_TAC `r = &1 - x pow 2` THEN
SUBGOAL_THEN `&0 < r` ASSUME_TAC THENL [
SUBGOAL_THEN `abs (x pow 2) < &1` MP_TAC THENL [
ASM_REWRITE_TAC[REAL_ABS_POW; ABS_SQUARE_LT_1; REAL_ABS_ABS];
ALL_TAC
] THEN
EXPAND_TAC "r" THEN REAL_ARITH_TAC;
ALL_TAC
] THEN
ASM_SIMP_TAC[REAL_ARITH `&0 < r ==> ~(r = &0)`] THEN
REWRITE_TAC[real_div; REAL_INV_POW; REAL_INV_MUL; ARITH_RULE `2 - 1 = 1`] THEN
REAL_ARITH_TAC
]);;
let diff2_exp = prove
(`!x. (exp has_real_derivative exp x) (atreal x)`,
REWRITE_TAC[HAS_REAL_DERIVATIVE_EXP]);;
let diff2_log = prove
(`!x. &0 < x ==>
(log has_real_derivative inv x) (atreal x) /\
(inv has_real_derivative --inv (x pow 2)) (atreal x)`,
REPEAT STRIP_TAC THENL [
ASM_SIMP_TAC[HAS_REAL_DERIVATIVE_LOG];
ASM_SIMP_TAC[HAS_REAL_DERIVATIVE_INV_BASIC; REAL_ARITH `&0 < t ==> ~(t = &0)`];
]);;
let diff2_inv = prove
(`!x. ~(x = &0) ==>
(inv has_real_derivative --inv (x pow 2)) (atreal x) /\
((\x. --inv (x pow 2)) has_real_derivative &2 * inv (x pow 3)) (atreal x)`,
REPEAT STRIP_TAC THENL [
ASM_SIMP_TAC[HAS_REAL_DERIVATIVE_INV_BASIC];
REAL_DIFF_TAC THEN ASM_SIMP_TAC[REAL_POW_2; REAL_ENTIRE] THEN
REWRITE_TAC[ARITH_RULE `2 - 1 = 1`; GSYM REAL_POW_2; REAL_POW_POW] THEN
REWRITE_TAC[REAL_ARITH `--(--(&2 * t * &1) / r) = &2 * (t / r)`] THEN
ASM_SIMP_TAC[REAL_DIV_POW2; ARITH_RULE `~(2 * 2 <= 1)`; ARITH_RULE `2 * 2 - 1 = 3`]
]);;
let diff2_sqrt = prove
(`!x. &0 < x ==>
(sqrt has_real_derivative inv (&2 * sqrt x)) (atreal x) /\
((\x. inv (&2 * sqrt x)) has_real_derivative --inv (&4 * sqrt x pow 3)) (atreal x)`,
REPEAT STRIP_TAC THENL [
ASM_SIMP_TAC[HAS_REAL_DERIVATIVE_SQRT];
REAL_DIFF_TAC THEN ASM_SIMP_TAC[REAL_POW_2; REAL_ENTIRE; SQRT_EQ_0] THEN
ASM_SIMP_TAC[REAL_ARITH `&0 < x ==> ~(x = &0)`; REAL_ARITH `~(&2 = &0)`] THEN
REWRITE_TAC[REAL_INV_MUL; real_div; REAL_INV_POW] THEN
REAL_ARITH_TAC
]);;
let taylor2_sin = prove
(`!a t. ?p. abs p <= abs t /\
sin (a + t) = sin a + cos a * t - sin (a + p) * t pow 2 / &2`,
REPEAT STRIP_TAC THEN
MP_TAC (SPECL[`sin`; `cos`; `\x. --sin x`; `a:real`; `t:real`] taylor2) THEN
REWRITE_TAC[diff2_sin_cos] THEN STRIP_TAC THEN
EXISTS_TAC `p:real` THEN ASM_REWRITE_TAC[] THEN REAL_ARITH_TAC);;
let taylor2_cos = prove
(`!a t. ?p. abs p <= abs t /\
cos (a + t) = cos a - sin a * t - cos (a + p) * t pow 2 / &2`,
REPEAT STRIP_TAC THEN
MP_TAC (SPECL[`cos`; `\x. --sin x`; `\x. --cos x`; `a:real`; `t:real`] taylor2) THEN
REWRITE_TAC[diff2_sin_cos] THEN STRIP_TAC THEN
EXISTS_TAC `p:real` THEN ASM_REWRITE_TAC[] THEN REAL_ARITH_TAC);;
let taylor2_atn = prove
(`!a t. ?p. abs p <= abs t /\
atn (a + t) = atn a + inv (&1 + a pow 2) * t
- ((a + p) / (&1 + (a + p) pow 2) pow 2) * t pow 2`,
REPEAT STRIP_TAC THEN
MP_TAC (SPECL[`atn`; `\x. inv (&1 + x pow 2)`; `\x. -- &2 * x * inv (&1 + x pow 2) pow 2`; `a:real`; `t:real`] taylor2) THEN
REWRITE_TAC[diff2_atn] THEN STRIP_TAC THEN
EXISTS_TAC `p:real` THEN ASM_REWRITE_TAC[real_div; REAL_INV_POW] THEN
REAL_ARITH_TAC);;
let taylor2_acs = prove
(`!a t. (!x. abs x <= abs t ==> abs (a + x) < &1)
==> ?p. abs p <= abs t /\
acs (a + t) = acs a - inv (sqrt (&1 - a pow 2)) * t
- (a + p) * inv (sqrt (&1 - (a + p) pow 2) pow 3) * t pow 2 / &2`,
REPEAT STRIP_TAC THEN
MP_TAC (SPECL[`acs`; `\x. --inv (sqrt (&1 - x pow 2))`; `\x. --x * inv (sqrt (&1 - x pow 2) pow 3)`; `a:real`; `t:real`] taylor2) THEN
ASM_SIMP_TAC[diff2_acs] THEN STRIP_TAC THEN
EXISTS_TAC `p:real` THEN ASM_REWRITE_TAC[real_div; REAL_INV_POW] THEN
REAL_ARITH_TAC);;
let taylor2_asn = prove
(`!a t. (!x. abs x <= abs t ==> abs (a + x) < &1)
==> ?p. abs p <= abs t /\
asn (a + t) = asn a + inv (sqrt (&1 - a pow 2)) * t
+ (a + p) * inv (sqrt (&1 - (a + p) pow 2) pow 3) * t pow 2 / &2`,
REPEAT STRIP_TAC THEN
MP_TAC (SPECL[`asn`; `\x. inv (sqrt (&1 - x pow 2))`; `\x. x * inv (sqrt (&1 - x pow 2) pow 3)`; `a:real`; `t:real`] taylor2) THEN
ASM_SIMP_TAC[diff2_asn] THEN STRIP_TAC THEN
EXISTS_TAC `p:real` THEN ASM_REWRITE_TAC[real_div; REAL_INV_POW] THEN
REAL_ARITH_TAC);;
let taylor2_exp = prove
(`!a t. ?p. abs p <= abs t /\
exp (a + t) = exp a + exp a * t + exp (a + p) * t pow 2 / &2`,
REPEAT STRIP_TAC THEN
MP_TAC (SPECL[`exp`; `exp`; `exp`; `a:real`; `t:real`] taylor2) THEN
REWRITE_TAC[diff2_exp] THEN STRIP_TAC THEN
EXISTS_TAC `p:real` THEN ASM_REWRITE_TAC[]);;
let taylor2_inv = prove
(`!a t. (!x. abs x <= abs t ==> ~(a + x = &0))
==> ?p. abs p <= abs t /\
inv (a + t) = inv a - inv(a pow 2) * t + inv((a + p) pow 3) * t pow 2`,
REPEAT STRIP_TAC THEN
MP_TAC (SPECL[`inv`; `\x. --inv (x pow 2)`; `\x. &2 * inv (x pow 3)`; `a:real`; `t:real`] taylor2) THEN
ASM_SIMP_TAC[diff2_inv] THEN STRIP_TAC THEN
EXISTS_TAC `p:real` THEN ASM_REWRITE_TAC[] THEN REAL_ARITH_TAC);;
let taylor2_sqrt = prove
(`!a t. (!x. abs x <= abs t ==> &0 < a + x)
==> ?p. abs p <= abs t /\
sqrt (a + t) = sqrt a + inv(sqrt a) * t / &2 - inv(sqrt (a + p) pow 3) * t pow 2 / &8`,
REPEAT STRIP_TAC THEN
MP_TAC (SPECL[`sqrt`; `\x. inv (&2 * sqrt x)`; `\x. --inv(&4 * sqrt x pow 3)`; `a:real`; `t:real`] taylor2) THEN
ASM_SIMP_TAC[diff2_sqrt] THEN STRIP_TAC THEN
EXISTS_TAC `p:real` THEN ASM_REWRITE_TAC[REAL_INV_MUL; real_div] THEN
REAL_ARITH_TAC);;
let taylor2_log = prove
(`!a t. (!x. abs x <= abs t ==> &0 < a + x)
==> ?p. abs p <= abs t /\
log (a + t) = log a + inv a * t - inv((a + p) pow 2) * t pow 2 / &2`,
REPEAT STRIP_TAC THEN
MP_TAC (SPECL[`log`; `inv`; `\x. --inv(x pow 2)`; `a:real`; `t:real`] taylor2) THEN
ASM_SIMP_TAC[diff2_log] THEN STRIP_TAC THEN
EXISTS_TAC `p:real` THEN ASM_REWRITE_TAC[] THEN REAL_ARITH_TAC);;
(* --------------------------- *)
(* Approx Taylor 2 *)
(* --------------------------- *)
let approx_taylor2 = prove
(`!f f' f'' s h t m1 m2 e2 b m3.
(!x. x IN s ==> abs (tform_f1 t x) <= m1 /\ abs (tform_f1 t x pow 2) <= m2 * e2) /\
(!x y. x IN s /\ abs y <= m1 ==>
let z = tform_f0 t x + y in
(f has_real_derivative f' z) (atreal z) /\
(f' has_real_derivative f'' z) (atreal z) /\
abs (f'' z / &2) <= b) /\
approx s h t /\
&0 < b /\ &0 <= m2 /\ &0 <= e2 /\ b * m2 <= m3
==> approx s (\x:real^N. f (h x))
(mk_tform ((\x. f (tform_f0 t x)),
(mul_f1 (\x. f' (tform_f0 t x)) (tform_list t) ++
[(\x. m3),
(\x. (f (h x) - f (tform_f0 t x) - f' (tform_f0 t x) * tform_f1 t x) / m3),
e2])))`,
REWRITE_TAC[approx; tform_f1] THEN REPEAT GEN_TAC THEN ASM_SIMP_TAC[f0_mk; list_mk] THEN
CONV_TAC (DEPTH_CONV let_CONV) THEN STRIP_TAC THEN GEN_TAC THEN DISCH_TAC THEN
ABBREV_TAC `r = sum_list (tform_list t) (\ (f1,e1,e2). f1 (x:real^N) * e1 x)` THEN
SUBGOAL_THEN `&0 <= b * m2` ASSUME_TAC THENL [
MATCH_MP_TAC REAL_LE_MUL THEN ASM_SIMP_TAC[REAL_LT_IMP_LE];
ALL_TAC
] THEN
SUBGOAL_THEN `b * m2 = &0 ==> r = &0` ASSUME_TAC THENL [
DISCH_TAC THEN
SUBGOAL_THEN `m2 = &0` ASSUME_TAC THENL [
POP_ASSUM MP_TAC THEN
ASM_SIMP_TAC[REAL_ENTIRE; REAL_ARITH `&0 < b ==> ~(b = &0)`];
ALL_TAC
] THEN
REPEAT (FIRST_X_ASSUM (MP_TAC o SPEC `x:real^N`)) THEN
ASM_REWRITE_TAC[REAL_MUL_LZERO; REAL_ABS_MUL; REAL_POW_2] THEN
SUBGOAL_THEN `abs r * abs r <= &0 ==> r = &0` ASSUME_TAC THENL [
REWRITE_TAC[REAL_ARITH `a * b <= &0 <=> &0 <= (--a) * b`] THEN
REWRITE_TAC[REAL_MUL_POS_LE] THEN REAL_ARITH_TAC;
ALL_TAC
] THEN
ASM_SIMP_TAC[];
ALL_TAC
] THEN
STRIP_TAC THENL [
REWRITE_TAC[sum_list_append; sum_list_mul1; sum_list_sing] THEN
ASM_CASES_TAC `m3 = &0` THENL [
SUBGOAL_THEN `b * m2 = &0` ASSUME_TAC THENL [ ASM_ARITH_TAC; ALL_TAC ] THEN
ASM_SIMP_TAC[real_div; REAL_INV_0; REAL_MUL_LZERO; REAL_ADD_RID; REAL_MUL_RZERO];
ALL_TAC
] THEN
ASM_SIMP_TAC[REAL_ARITH `x * y / x = (x / x) * y`; REAL_DIV_REFL] THEN
REAL_ARITH_TAC;
ALL_TAC
] THEN
ASM_SIMP_TAC[ALL_APPEND; all_e12_mul1; ALL] THEN
ASM_CASES_TAC `b * m2 = &0` THENL [
ASM_SIMP_TAC[REAL_MUL_RZERO; REAL_SUB_RZERO; REAL_ADD_RID; REAL_SUB_REFL] THEN
ASM_REWRITE_TAC[real_div; REAL_MUL_LZERO; REAL_ABS_0];
ALL_TAC
] THEN
MATCH_MP_TAC REAL_LE_TRANS THEN
EXISTS_TAC `abs ((f (tform_f0 t x + r) - f (tform_f0 (t:(N)tform) x) - f' (tform_f0 t x) * r) / (b * m2))` THEN
CONJ_TAC THENL [
REWRITE_TAC[real_div; REAL_ABS_MUL] THEN
MATCH_MP_TAC REAL_LE_MUL2 THEN REWRITE_TAC[REAL_ABS_POS; REAL_LE_REFL] THEN
REWRITE_TAC[REAL_ABS_INV; REAL_LE_INV] THEN
MATCH_MP_TAC REAL_LE_INV2 THEN ASM_ARITH_TAC;
ALL_TAC
] THEN
MP_TAC (SPECL[`f:real->real`; `f':real->real`; `f'':real->real`; `tform_f0 (t:(N)tform) x`; `r:real`] taylor2) THEN
ANTS_TAC THENL [
GEN_TAC THEN DISCH_TAC THEN
REPEAT (FIRST_X_ASSUM (MP_TAC o SPEC `x:real^N`)) THEN ASM_REWRITE_TAC[] THEN
DISCH_TAC THEN DISCH_THEN (MP_TAC o SPEC `x':real`) THEN
ANTS_TAC THENL [ POP_ASSUM MP_TAC THEN POP_ASSUM MP_TAC THEN REAL_ARITH_TAC; ALL_TAC ] THEN
SIMP_TAC[];
ALL_TAC
] THEN
STRIP_TAC THEN
ASM_REWRITE_TAC[REAL_ARITH `(a + b + c) - a - b = c`] THEN
MATCH_MP_TAC REAL_LE_LCANCEL_IMP THEN EXISTS_TAC `b * m2` THEN
CONJ_TAC THENL [
MATCH_MP_TAC REAL_LT_MUL THEN
UNDISCH_TAC `~(b * m2 = &0)` THEN ASM_REWRITE_TAC[REAL_ENTIRE; DE_MORGAN_THM] THEN
UNDISCH_TAC `&0 <= m2` THEN REAL_ARITH_TAC;
ALL_TAC
] THEN
REWRITE_TAC[REAL_ABS_DIV; REAL_ABS_NEG; REAL_ABS_MUL; REAL_ABS_NUM] THEN
SUBGOAL_THEN `abs b = b /\ abs m2 = m2` (fun th -> REWRITE_TAC[th]) THENL [
ASM_SIMP_TAC[REAL_ABS_REFL; REAL_LT_IMP_LE];
ALL_TAC
] THEN
ASM_SIMP_TAC[REAL_ARITH `x * y / x = (x / x) * y`; REAL_DIV_REFL; REAL_MUL_LID] THEN
REWRITE_TAC[REAL_ARITH `abs x * y / &2 = abs (x / &2) * y`] THEN
REWRITE_TAC[GSYM REAL_MUL_ASSOC] THEN MATCH_MP_TAC REAL_LE_MUL2 THEN
EXPAND_TAC "r" THEN ASM_SIMP_TAC[REAL_ABS_POS] THEN
FIRST_X_ASSUM (MP_TAC o SPECL[`x:real^N`; `p:real`]) THEN ANTS_TAC THENL [
ASM_REWRITE_TAC[] THEN
MATCH_MP_TAC REAL_LE_TRANS THEN EXISTS_TAC `abs r` THEN
EXPAND_TAC "r" THEN ASM_SIMP_TAC[];
ALL_TAC
] THEN
SIMP_TAC[]);;
(* --------------------------- *)
(* inv *)
(* --------------------------- *)
let approx_inv = prove
(`!s h t m1 m2 e2 b m3.
approx s h t /\
(!x. x IN s ==> abs (tform_f1 t x) <= m1 /\ abs (tform_f1 t x pow 2) <= m2 * e2) /\
(!x y. x IN s /\ abs y <= m1 ==> abs (inv ((tform_f0 t x + y) pow 3)) <= b /\
~(tform_f0 t x + y = &0)) /\
&0 < b /\ &0 <= m2 /\ &0 <= e2 /\ b * m2 <= m3
==> approx s (\x:real^N. inv (h x))
(mk_tform ((\x. inv (tform_f0 t x)),
(mul_f1 (\x. --inv (tform_f0 t x pow 2)) (tform_list t) ++
[(\x. m3),
(\x. (inv (h x) - inv (tform_f0 t x) + inv (tform_f0 t x pow 2) * tform_f1 t x) /
m3),
e2])))`,
REPEAT STRIP_TAC THEN
MP_TAC ((SPEC_ALL o SPECL[`inv`; `\x. --inv (x pow 2)`; `\x. &2 * inv (x pow 3)`]) approx_taylor2) THEN
ASM_REWRITE_TAC[] THEN ANTS_TAC THENL [
REPEAT STRIP_TAC THEN LET_TAC THEN
FIRST_X_ASSUM (MP_TAC o SPECL[`x:real^N`; `y:real`]) THEN ASM_REWRITE_TAC[] THEN
SIMP_TAC[diff2_inv; REAL_ARITH `(&2 * x) / &2 = x`];
ALL_TAC
] THEN
REWRITE_TAC[REAL_ARITH `a - b - --c * d = a - b + c * d`]);;
(* --------------------------- *)
(* sqrt *)
(* --------------------------- *)
let approx_sqrt = prove
(`!s h t m1 m2 e2 b m3.
approx s h t /\
(!x. x IN s ==> abs (tform_f1 t x) <= m1 /\ abs (tform_f1 t x pow 2) <= m2 * e2) /\
(!x y. x IN s /\ abs y <= m1 ==> abs (inv (sqrt (tform_f0 t x + y) pow 3) / &8) <= b /\
&0 < tform_f0 t x + y) /\
&0 < b /\ &0 <= m2 /\ &0 <= e2 /\ b * m2 <= m3
==> approx s (\x:real^N. sqrt (h x))
(mk_tform ((\x. sqrt (tform_f0 t x)),