ErrorValidationScript.sml 165 KB
Newer Older
1
 (**
Heiko Becker's avatar
Heiko Becker committed
2
   This file contains the HOL4 implementation of the error bound validator as well
3 4 5 6 7 8 9
   as its soundness proof. The function validErrorbound is the Error bound
   validator from the certificate checking process. Under the assumption that a
   valid range arithmetic result has been computed, it can validate error bounds
   encoded in the analysis result. The validator is used in the file
   CertificateChecker.v to build the complete checker.
 **)
open preamble
10
open simpLib realTheory realLib RealArith pred_setTheory
='s avatar
= committed
11 12
open AbbrevsTheory ExpressionsTheory RealSimpsTheory DaisyTactics MachineTypeTheory
open ExpressionAbbrevsTheory ErrorBoundsTheory IntervalArithTheory TypingTheory
13
open IntervalValidationTheory EnvironmentsTheory CommandsTheory ssaPrgsTheory
14 15

val _ = new_theory "ErrorValidation";
16

17
val _ = Parse.hide "delta"; (* so that it can be used as a variable *)
18
val _ = temp_overload_on("abs",``real$abs``);
19

20
val validErrorbound_def = Define `
='s avatar
= committed
21
  validErrorbound e (typeMap: real exp -> mType option) (absenv:analysisResult) (dVars:num_set)=
22
    let (intv, err) = absenv e in
='s avatar
= committed
23 24 25 26 27
    let mopt = typeMap e in
    case mopt of
      | NONE => F
      | SOME m =>
        if (0 <= err) then
28
          case e of
29 30
            | Var v => if (lookup v dVars = SOME ()) then T else (maxAbs intv * (mTypeToQ m) <= err)
            | Const _ n => (maxAbs intv * (mTypeToQ m) <= err)
31
            | Unop Neg f =>
='s avatar
= committed
32
              if (validErrorbound f typeMap absenv dVars) then
33 34 35 36
                err = (SND (absenv f))
              else
                  F
            | Unop Inv f => F
37
            | Binop op f1 f2 =>
='s avatar
= committed
38
              (if (validErrorbound f1 typeMap absenv dVars /\ validErrorbound f2 typeMap absenv dVars) then
39 40 41 42 43 44 45
                  let (ive1, err1) = absenv f1 in
                  let (ive2, err2) = absenv f2 in
                  let errIve1 = widenInterval ive1 err1 in
                  let errIve2 = widenInterval ive2 err2 in
                  let upperBoundE1 = maxAbs ive1 in
                  let upperBoundE2 = maxAbs ive2 in
                    case op of
46 47 48
                      | Plus => err1 + err2 + (maxAbs (addInterval errIve1 errIve2) * (mTypeToQ m)) <= err
                      | Sub => err1 + err2 + (maxAbs (subtractInterval errIve1 errIve2) * (mTypeToQ m)) <= err
                      | Mult => (upperBoundE1 * err2 + upperBoundE2 * err1 + err1 * err2) + (maxAbs (multInterval errIve1 errIve2) * (mTypeToQ m)) <= err
49 50 51 52 53 54
                      | Div =>
                        (if (IVhi errIve2 < 0 \/ 0 < IVlo errIve2)
                          then
                            let upperBoundInvE2 = maxAbs (invertInterval ive2) in
                            let minAbsIve2 = minAbsFun (errIve2) in
                            let errInv = (1 / (minAbsIve2 * minAbsIve2)) * err2 in
55
                              ((upperBoundE1 * errInv + upperBoundInvE2 * err1 + err1 * errInv) + (maxAbs (divideInterval errIve1 errIve2) * (mTypeToQ m)) <= err)
56 57
                          else F)
                  else F)
58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74
            | Fma f1 f2 f3 =>
              (if (validErrorbound f1 typeMap absenv dVars /\
                    validErrorbound f2 typeMap absenv dVars /\
                    validErrorbound f3 typeMap absenv dVars) then
                  let (ive1, err1) = absenv f1 in
                  let (ive2, err2) = absenv f2 in
                  let (ive3, err3) = absenv f3 in
                  let errIve1 = widenInterval ive1 err1 in
                  let errIve2 = widenInterval ive2 err2 in
                  let errIve3 = widenInterval ive3 err3 in
                  let upperBoundE1 = maxAbs ive1 in
                  let upperBoundE2 = maxAbs ive2 in
                  let upperBoundE3 = maxAbs ive3 in
                  let errIntv_prod = multInterval errIve2 errIve3 in
                  let mult_error_bound = (upperBoundE2 * err3 + upperBoundE3 * err2 + err2 * err3) in
                  (err1 + mult_error_bound + (maxAbs (addInterval errIve1 errIntv_prod)) * (mTypeToQ m)) <= err
               else F)
='s avatar
= committed
75 76 77 78
            | Downcast m1 e1 =>
              let (ive1, err1) = absenv e1 in
              let rec_res = validErrorbound e1 typeMap absenv dVars in
              let errIve1 = widenInterval ive1 err1 in
79
              rec_res /\ ( (err1 + maxAbs errIve1 * (mTypeToQ m1)) <= err)
80
        else F`;
81

Heiko Becker's avatar
Heiko Becker committed
82
val validErrorboundCmd_def = Define `
='s avatar
= committed
83
  validErrorboundCmd (f:real cmd) (typeMap: real exp -> mType option) (env:analysisResult) (dVars:num_set)=
84
    case f of
='s avatar
= committed
85 86 87 88 89 90
      | Let m x e g  =>
        if (validErrorbound e typeMap env dVars /\ (env e = env (Var x))) then
            validErrorboundCmd g typeMap env (insert x () dVars)
        else F
      | Ret e =>
        validErrorbound e typeMap env dVars`;
Heiko Becker's avatar
Heiko Becker committed
91

92 93
val err_always_positive = store_thm (
  "err_always_positive",
='s avatar
= committed
94 95
  ``!(e:real exp) (absenv:analysisResult) (iv:interval) (err:real) dVars  (tmap: real exp -> mType option).
      (validErrorbound e tmap absenv dVars) /\
Heiko Becker's avatar
Heiko Becker committed
96
      ((iv,err) = absenv e) ==>
97
      0 <= err``,
98 99
  once_rewrite_tac [validErrorbound_def] \\ rpt strip_tac \\
  Cases_on `e` \\
='s avatar
= committed
100 101 102 103 104
  qpat_assum `(iv,err) = absenv e` (fn thm => fs [GSYM thm])
  >- (Cases_on `tmap (Var n)` \\ fs [])
  >- (Cases_on `tmap (Const m v)` \\ fs [])
  >- (Cases_on `tmap (Unop u e')` \\ fs [])
  >- (Cases_on `tmap (Binop b e' e0)` \\ fs [])
105
  >- (Cases_on `tmap (Fma e' e0 e1)` \\ fs [])
='s avatar
= committed
106 107
  >- (Cases_on `tmap (Downcast m e')` \\ fs []));

108 109 110 111 112 113
val validErrorboundCorrectVariable_eval = store_thm (
  "validErrorboundCorrectVariable_eval",
  ``!E1 E2 absenv v e nR nlo nhi P fVars dVars Gamma expTypes.
      eval_exp E1 (toRMap Gamma) (toREval (Var v)) nR M0 /\
      typeCheck (Var v) Gamma expTypes /\
      approxEnv E1 Gamma absenv fVars dVars E2 /\
Heiko Becker's avatar
Heiko Becker committed
114
      validIntervalbounds (Var v) absenv P dVars /\
115 116 117 118 119 120
      (domain (usedVars ((Var v):real exp)) DIFF (domain dVars)) SUBSET (domain fVars) /\
      (!v. v IN domain dVars ==>
         ?r. E1 v = SOME r /\
           FST(FST(absenv (Var v))) <= r /\ r <= SND (FST (absenv (Var v)))) /\
      (!v. v IN domain fVars ==>
         ?r. E1 v = SOME r /\
Heiko Becker's avatar
Heiko Becker committed
121
           FST (P v) <= r /\ r <= SND (P v)) /\
122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144
      (!v. v IN ((domain fVars) UNION (domain dVars)) ==>
         ?m. Gamma v = SOME m) /\
      absenv (Var v) = ((nlo, nhi),e) ==>
      ? nF m.
        eval_exp E2 Gamma (Var v) nF m``,
  rpt strip_tac
  \\ `?n. eval_exp E1 (toRMap Gamma) (toREval (Var v)) n M0 /\
        FST(FST(absenv (Var v))) <= n /\ n <= SND (FST (absenv (Var v)))`
       by (irule validIntervalbounds_sound
           \\ qexistsl_tac[`P`, `dVars`, `fVars`]
           \\ fs[SUBSET_DEF, domain_union]
           \\ rpt strip_tac \\ first_x_assum irule \\ fs[])
  \\ `nR = n` by (metis_tac[meps_0_deterministic])
  \\ rveq
  \\ fs[toREval_def]
  \\ inversion `eval_exp E1 _ _ _ _` eval_exp_cases
  \\ `?m. Gamma v = SOME m` by (Cases_on `Gamma v` \\ fs [toRMap_def])
  \\ `?vF. E2 v = SOME vF`
       by (irule approxEnv_gives_value
           \\ qexistsl_tac [`E1`, `Gamma`, `absenv`, `dVars`, `fVars`, `n`]
           \\ fs[domain_union, SUBSET_DEF, usedVars_def]
           \\ Cases_on `v IN (domain dVars)` \\ fs[])
  \\ qexistsl_tac [`vF`, `m`] \\ fs[eval_exp_cases]);
Heiko Becker's avatar
Heiko Becker committed
145

146 147 148 149 150 151 152
val validErrorboundCorrectVariable = store_thm (
  "validErrorboundCorrectVariable",
  ``!(E1 E2:env) absenv fVars dVars  (v:num) (nR nF err nlo nhi:real) (P:precond) m expTypes Gamma.
      eval_exp E1 (toRMap Gamma) (toREval (Var v)) nR M0 /\
      eval_exp E2 Gamma (Var v) nF m /\
      typeCheck (Var v) Gamma expTypes /\
      approxEnv E1 Gamma absenv fVars dVars E2 /\
Heiko Becker's avatar
Heiko Becker committed
153
      validIntervalbounds (Var v) absenv P dVars /\
154 155
      validErrorbound (Var v) expTypes absenv dVars /\
      (domain (usedVars ((Var v):real exp)) DIFF domain dVars) SUBSET domain fVars /\
Heiko Becker's avatar
Heiko Becker committed
156 157 158 159 160 161 162 163 164 165 166
      (!v.
         v IN domain dVars ==>
         ?r.
           (E1 v = SOME r) /\
           FST (FST (absenv (Var v))) <= r /\
           r <= SND (FST (absenv (Var v)))) /\
      (!v.
         v IN domain fVars ==>
         ?r.
           (E1 v = SOME r) /\
           FST (P v) <= r /\ r <= SND (P v)) /\
167 168 169
      (!v.
         v IN domain fVars \/ v IN domain dVars ==>
         ?m. Gamma v = SOME m) /\
Heiko Becker's avatar
Heiko Becker committed
170 171
      (absenv (Var v) = ((nlo, nhi),err)) ==>
      abs (nR - nF) <= err``,
Heiko Becker's avatar
Heiko Becker committed
172
  rpt strip_tac
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
  \\ `?vR. eval_exp E1 (toRMap Gamma) (toREval (Var v)) vR M0 /\
        FST (FST (absenv (Var v))) <= vR /\ vR <= SND(FST(absenv (Var v)))`
       by (irule validIntervalbounds_sound
           \\ qexistsl_tac [`P`, `dVars`, `fVars`]
           \\ fs[] \\ first_x_assum MATCH_ACCEPT_TAC)
  \\ `vR = nR` by (metis_tac[meps_0_deterministic]) \\ rveq
  \\ fs[toREval_def]
  \\ rpt (inversion `eval_exp _ _ _ _ _` eval_exp_cases)
  \\ rw_thm_asm `typeCheck _ _ _` typeCheck_def
  \\ rw_thm_asm `validErrorbound _ _ _ _` validErrorbound_def
  \\ rw_asm_star `absenv (Var v) = _`
  \\ rw_asm_star `Gamma v = _`
  \\ Cases_on `expTypes (Var v)`
     >- (fs[])
     >- (Cases_on `lookup v dVars` \\ fs[]
         >- (fs[usedVars_def,domain_lookup]
             \\ irule REAL_LE_TRANS
             \\ qexists_tac `maxAbs (nlo,nhi) * mTypeToQ x`
             \\ fs[]
             \\ `abs (nR - nF) <= abs nR * mTypeToQ m`
                  by (irule approxEnv_fVar_bounded
                      \\ qexistsl_tac [`E1`, `E2`, `Gamma`, `absenv`, `dVars`, `fVars`, `v`]
                      \\ fs[domain_lookup])
             \\ irule REAL_LE_TRANS
             \\ qexists_tac `abs nR * mTypeToQ m` \\ fs[]
             \\ irule REAL_LE_RMUL_IMP
             >- (irule contained_leq_maxAbs
                 \\ fs[contained_def, IVlo_def, IVhi_def])
             >- (irule mTypeToQ_pos))
         >- (irule approxEnv_dVar_bounded
             \\ qexistsl_tac [`E1`, `E2`, `Gamma`, `absenv`, `dVars`, `fVars`, `m`, `v`]
             \\ fs[domain_lookup])));
Heiko Becker's avatar
Heiko Becker committed
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
val validErrorboundCorrectConstant_eval = store_thm (
  "validErrorboundCorrectConstant_eval",
  ``!(E1 E2:env) (absenv:analysisResult) (n nR nF e nlo nhi:real) dVars m expTypes Gamma.
      eval_exp E1 (toRMap Gamma) (toREval (Const m n)) nR M0 /\
      typeCheck (Const m n) Gamma expTypes /\
      validErrorbound (Const m n) expTypes absenv dVars /\
      FST (FST (absenv (Const m n))) <= nR /\
      nR <= SND (FST (absenv (Const m n))) /\
      (absenv (Const m n) = ((nlo,nhi),e)) ==>
      ?nF m1.
        eval_exp E2 Gamma (Const m n) nF m1``,
  rpt strip_tac
  \\ qexistsl_tac [`perturb n (mTypeToQ m)`,`m`] \\ irule Const_dist'
  \\ fs[]
  \\ qexists_tac `mTypeToQ m`
  \\ fs[realTheory.abs, mTypeToQ_pos]);

val validErrorboundCorrectConstant = store_thm (
  "validErrorboundCorrectConstant",
  ``!(E1 E2:env) (absenv:analysisResult) (n nR nF e nlo nhi:real) dVars m expTypes Gamma.
      eval_exp E1 (toRMap Gamma) (toREval (Const m n)) nR M0 /\
      eval_exp E2 Gamma (Const m n) nF m /\
      typeCheck (Const m n) Gamma expTypes /\
      validErrorbound (Const m n) expTypes absenv dVars /\
='s avatar
= committed
230 231 232
      FST (FST (absenv (Const m n))) <= nR /\
      nR <= SND (FST (absenv (Const m n))) /\
      (absenv (Const m n) = ((nlo,nhi),e)) ==>
233
      (abs (nR - nF)) <= e``,
234 235
  once_rewrite_tac [validErrorbound_def]
  \\ rpt strip_tac \\ fs[]
236 237
  \\ fs [toREval_def]
  \\ inversion `eval_exp _ _ _ _ M0` eval_exp_cases
='s avatar
= committed
238
  \\ simp [delta_M0_deterministic]
239
  \\ inversion `eval_exp _ _ _ _ m` eval_exp_cases
240
  \\ simp[perturb_def]
241
  \\ rename1 `abs deltaF <= (mTypeToQ m)`
242
  \\ simp [Rabs_err_simpl, ABS_MUL]
='s avatar
= committed
243
  \\ fs [typeCheck_def]
244 245
  \\ `expTypes (Const m n) = SOME m`
       by (Cases_on `expTypes (Const m n)` \\ fs [] \\ rveq)
='s avatar
= committed
246
  \\ fs []
247
  \\ match_mp_tac REAL_LE_TRANS
248
  \\ qexists_tac `maxAbs (nlo, nhi) * (mTypeToQ m)` \\ conj_tac \\ simp[]
249 250 251
  \\ match_mp_tac REAL_LE_MUL2 \\ rpt (conj_tac) \\ TRY (simp[ABS_POS])
  \\ simp[maxAbs_def]
  \\ match_mp_tac maxAbs
='s avatar
= committed
252
  \\ qspecl_then [`delta`] (fn thm => fs [thm]) delta_M0_deterministic
253 254
  \\ qpat_x_assum `absenv _ = _` (fn thm => rule_assum_tac (fn thm2 => REWRITE_RULE [thm] thm2))
  \\ simp[]);
255

256 257
val validErrorboundCorrectAddition = store_thm (
  "validErrorboundCorrectAddition",
258
  ``!(E1 E2:env) (absenv:analysisResult) (e1:real exp) (e2:real exp)
259 260 261 262 263 264 265
     (nR nR1 nR2 nF nF1 nF2:real) (e err1 err2:real) (alo ahi e1lo e1hi e2lo e2hi :real) dVars m m1 m2 expTypes Gamma.
       (m = join m1 m2) /\
       eval_exp E1 (toRMap Gamma) (toREval e1) nR1 M0 /\
       eval_exp E1 (toRMap Gamma) (toREval e2) nR2 M0 /\
       eval_exp E1 (toRMap Gamma) (toREval (Binop Plus e1 e2)) nR M0 /\
       eval_exp E2 Gamma e1 nF1 m1 /\
       eval_exp E2 Gamma e2 nF2 m2 /\
='s avatar
= committed
266
       eval_exp (updEnv 2 nF2 (updEnv 1 nF1 emptyEnv))
267
                (updDefVars 2 m2 (updDefVars 1 m1 Gamma))
='s avatar
= committed
268
                (Binop Plus (Var 1) (Var 2)) nF m /\
269 270
       typeCheck (Binop Plus e1 e2) Gamma expTypes /\
       validErrorbound (Binop Plus e1 e2) expTypes absenv dVars /\
271 272 273 274
       FST (FST (absenv e1)) <= nR1 /\
       nR1 <= SND (FST (absenv e1)) /\
       FST (FST (absenv e2)) <= nR2 /\
       nR2 <= SND (FST (absenv e2)) /\
Heiko Becker's avatar
Heiko Becker committed
275 276 277 278 279 280
       (absenv e1 = ((e1lo,e1hi),err1)) /\
       (absenv e2 = ((e2lo, e2hi),err2)) /\
       (absenv (Binop Plus e1 e2) = ((alo,ahi),e)) /\
       abs (nR1 - nF1) <= err1 /\
       abs (nR2 - nF2) <= err2 ==>
       abs (nR - nF) <= e``,
281 282 283 284
  once_rewrite_tac [validErrorbound_def]
  \\ rpt strip_tac \\ fs[]
  \\ rw_asm `absenv _ = _`
  \\ rw_asm `absenv e1 = _`
='s avatar
= committed
285
  \\ fs [Once typeCheck_def]
286 287 288 289 290
  \\ Cases_on `expTypes (Binop Plus e1 e2)` \\ rveq \\ fs []
  \\ Cases_on `expTypes e1` \\ rveq \\ fs []
  \\ Cases_on `expTypes e2` \\ rveq \\ fs []
  \\ `expTypes e1 = SOME m1` by (match_mp_tac typingSoundnessExp \\ metis_tac [])
  \\ `expTypes e2 = SOME m2` by (match_mp_tac typingSoundnessExp \\ metis_tac [])
='s avatar
= committed
291
  \\ fs [] \\ rveq
292
  \\ match_mp_tac REAL_LE_TRANS
293
  \\ qexists_tac `err1 + err2 + (abs (nF1 + nF2) * (mTypeToQ (join m1 m2)))`
294 295
  \\ conj_tac
     >- (match_mp_tac add_abs_err_bounded
296
         \\ qexistsl_tac [`e1`, `nR1`, `e2`, `nR2`, `E1`, `E2`, `m1`, `m2`, `Gamma`]
297 298 299 300 301
         \\ rpt (conj_tac) \\ simp[])
     >- (match_mp_tac REAL_LE_TRANS
         \\ qexists_tac
              `err1 + err2 + maxAbs (
                 addInterval (widenInterval (e1lo,e1hi) err1)
302
                             (widenInterval (e2lo,e2hi) err2)) * (mTypeToQ (join m1 m2))`
303 304
         \\ conj_tac \\ simp[maxAbs_def]
         \\ once_rewrite_tac [REAL_MUL_COMM] \\ match_mp_tac REAL_LE_LMUL_IMP
305
         \\ conj_tac \\ simp[mTypeToQ_def,mTypeToQ_pos]
306 307 308 309 310 311 312 313 314 315 316 317 318 319
         \\ match_mp_tac maxAbs
         \\ `contained nF1 (widenInterval (e1lo,e1hi) err1)`
              by (match_mp_tac distance_gives_iv
                  \\ qexists_tac `nR1` \\ conj_tac
                  \\ simp[contained_def, IVlo_def, IVhi_def])
         \\ `contained nF2 (widenInterval (e2lo,e2hi) err2)`
              by (match_mp_tac distance_gives_iv
                  \\ qexists_tac `nR2` \\ conj_tac
                  \\ simp[contained_def, IVlo_def, IVhi_def])
         \\ `contained (nF1 + nF2) (addInterval (widenInterval (e1lo, e1hi) err1) (widenInterval (e2lo, e2hi) err2))`
              by (match_mp_tac (ONCE_REWRITE_RULE [validIntervalAdd_def] interval_addition_valid)
                  \\ conj_tac \\ simp[])
         \\ rule_assum_tac (fn thm => REWRITE_RULE [contained_def, IVlo_def, IVhi_def] thm)
         \\ simp[]));
Heiko Becker's avatar
Heiko Becker committed
320

Heiko Becker's avatar
Heiko Becker committed
321
val validErrorboundCorrectSubtraction = store_thm ("validErrorboundCorrectSubtraction",
322
  ``!(E1 E2:env) (absenv:analysisResult) (e1:real exp) (e2:real exp)
323 324 325 326 327 328 329
     (nR nR1 nR2 nF nF1 nF2:real) (e err1 err2:real) (alo ahi e1lo e1hi e2lo e2hi:real) dVars m m1 m2 expTypes Gamma.
       (m = join m1 m2) /\
       eval_exp E1 (toRMap Gamma) (toREval e1) nR1 M0 /\
       eval_exp E1 (toRMap Gamma) (toREval e2) nR2 M0 /\
       eval_exp E1 (toRMap Gamma) (toREval (Binop Sub e1 e2)) nR M0 /\
       eval_exp E2 Gamma e1 nF1 m1 /\
       eval_exp E2 Gamma e2 nF2 m2 /\
='s avatar
= committed
330
       eval_exp (updEnv 2 nF2 (updEnv 1 nF1 emptyEnv))
331
                (updDefVars 2 m2 (updDefVars 1 m1 Gamma))
='s avatar
= committed
332
                (Binop Sub (Var 1) (Var 2)) nF m /\
333 334
       typeCheck (Binop Sub e1 e2) Gamma expTypes /\
       validErrorbound (Binop Sub e1 e2) expTypes absenv dVars /\
335 336 337 338
       FST (FST (absenv e1)) <= nR1 /\
       nR1 <= SND (FST (absenv e1)) /\
       FST (FST (absenv e2)) <= nR2 /\
       nR2 <= SND (FST (absenv e2)) /\
Heiko Becker's avatar
Heiko Becker committed
339 340 341 342 343 344
       (absenv e1 = ((e1lo,e1hi),err1)) /\
       (absenv e2 = ((e2lo, e2hi),err2)) /\
       (absenv (Binop Sub e1 e2) = ((alo,ahi),e)) /\
       abs (nR1 - nF1) <= err1 /\
       abs (nR2 - nF2) <= err2 ==>
       abs (nR - nF) <= e``,
345 346
  once_rewrite_tac [validErrorbound_def]
  \\ rpt strip_tac \\ fs[]
='s avatar
= committed
347
  \\ fs [Once typeCheck_def]
348 349 350 351 352
  \\ Cases_on `expTypes (Binop Sub e1 e2)` \\ rveq \\ fs []
  \\ Cases_on `expTypes e1` \\ rveq \\ fs []
  \\ Cases_on `expTypes e2` \\ rveq \\ fs []
  \\ `expTypes e1 = SOME m1` by (match_mp_tac typingSoundnessExp \\ metis_tac [])
  \\ `expTypes e2 = SOME m2` by (match_mp_tac typingSoundnessExp \\ metis_tac [])
='s avatar
= committed
353
  \\ fs [] \\ rveq
354
  \\ match_mp_tac REAL_LE_TRANS
355
  \\ qexists_tac `err1 + err2 + (abs (nF1 - nF2) * (mTypeToQ (join m1 m2)))`
356 357
  \\ conj_tac
  >- (match_mp_tac subtract_abs_err_bounded
358
      \\ qexistsl_tac [`e1`, `nR1`, `e2`, `nR2`, `E1`, `E2`, `m1`, `m2`, `Gamma`]
359 360 361 362 363
      \\ rpt (conj_tac) \\ simp[])
  >- (match_mp_tac REAL_LE_TRANS
      \\ qexists_tac
           `err1 + err2 + maxAbs (
                            subtractInterval (widenInterval (e1lo,e1hi) err1)
364
                                             (widenInterval (e2lo,e2hi) err2)) * (mTypeToQ (join m1 m2))`
365 366
      \\ conj_tac \\ simp[maxAbs_def]
      \\ once_rewrite_tac [REAL_MUL_COMM] \\ match_mp_tac REAL_LE_LMUL_IMP
367
      \\ conj_tac \\ simp[mTypeToQ_def,mTypeToQ_pos]
368 369 370 371 372 373 374 375 376 377 378 379 380 381
      \\ match_mp_tac maxAbs
      \\ `contained nF1 (widenInterval (e1lo,e1hi) err1)`
            by (match_mp_tac distance_gives_iv
                \\ qexists_tac `nR1` \\ conj_tac
                \\ simp[contained_def, IVlo_def, IVhi_def])
      \\ `contained nF2 (widenInterval (e2lo,e2hi) err2)`
           by (match_mp_tac distance_gives_iv
               \\ qexists_tac `nR2` \\ conj_tac
               \\ simp[contained_def, IVlo_def, IVhi_def])
      \\ `contained (nF1 - nF2) (subtractInterval (widenInterval (e1lo, e1hi) err1) (widenInterval (e2lo, e2hi) err2))`
           by (match_mp_tac (ONCE_REWRITE_RULE [validIntervalSub_def] interval_subtraction_valid)
               \\ conj_tac \\ simp[])
      \\ rule_assum_tac (fn thm => REWRITE_RULE [contained_def, IVlo_def, IVhi_def] thm)
      \\ simp[]));
Heiko Becker's avatar
Heiko Becker committed
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
val multiplicationErroBounded = store_thm ("multiplicationErrorBounded",
  ``!(nR1 nR2 nF1 nF2: real) (err1 err2: real) (e1lo e1hi e2lo e2hi: real).
        e1lo  nR1 /\
        nR1  e1hi /\
        e2lo  nR2 /\
        nR2  e2hi /\
        abs (nR1  nF1)  err1 /\
        abs (nR2  nF2)  err2 /\
        0  err1 /\
        0  err2 ==>
        abs (nR1 * nR2  nF1 * nF2) 
            maxAbs (e1lo,e1hi) * err2 + maxAbs (e2lo,e2hi) * err1 + err1 * err2``,
  (rpt strip_tac
  \\`nR1 <= maxAbs (e1lo, e1hi)`
    by (match_mp_tac contained_leq_maxAbs_val
        \\ fs[contained_def, IVlo_def, IVhi_def])
  \\ `nR2 <= maxAbs (e2lo, e2hi)`
       by (match_mp_tac contained_leq_maxAbs_val
           \\ fs[contained_def, IVlo_def, IVhi_def])
  \\`-nR1 <= maxAbs (e1lo, e1hi)`
       by (match_mp_tac contained_leq_maxAbs_neg_val
           \\ fs[contained_def, IVlo_def, IVhi_def])
  \\ `-nR2 <= maxAbs (e2lo, e2hi)`
       by (match_mp_tac contained_leq_maxAbs_neg_val
           \\ fs[contained_def, IVlo_def, IVhi_def])
  \\ `nR1 * err2 <= maxAbs (e1lo, e1hi) * err2`
       by (match_mp_tac REAL_LE_RMUL_IMP \\ fs[])
  \\ `-nR1 * err2 <= maxAbs (e1lo, e1hi) * err2`
       by (match_mp_tac REAL_LE_RMUL_IMP \\ fs[])
  \\ `nR2 * err1 <= maxAbs (e2lo, e2hi) * err1`
       by (match_mp_tac REAL_LE_RMUL_IMP \\ fs[])
  \\ `-nR2 * err1 <= maxAbs (e2lo, e2hi) * err1`
       by (match_mp_tac REAL_LE_RMUL_IMP \\ fs[])
  \\ `- (err1 * err2) <= err1 * err2`
       by (fs[REAL_NEG_LMUL] \\ match_mp_tac REAL_LE_RMUL_IMP \\ REAL_ASM_ARITH_TAC)
  \\ `0 <= maxAbs (e1lo, e1hi) * err2` by REAL_ASM_ARITH_TAC
  \\ `maxAbs (e1lo, e1hi) * err2 <= maxAbs (e1lo, e1hi) * err2 + maxAbs (e2lo, e2hi) * err1`
       by REAL_ASM_ARITH_TAC
  \\ `maxAbs (e1lo, e1hi) * err2 + maxAbs (e2lo, e2hi) * err1 <=
        maxAbs (e1lo, e1hi) * err2 + maxAbs (e2lo, e2hi) * err1 + err1 * err2`
          by REAL_ASM_ARITH_TAC
  \\ rpt (qpat_x_assum `eval_exp _ _ _ _ _` kall_tac)
  \\ rpt (qpat_x_assum `validErrorbound _ _` kall_tac)
  \\ `! (x:real). ((abs x = x) /\ 0 < x) \/ ((abs x = - x) /\ x <= 0)` by REAL_ASM_ARITH_TAC
  (* Large case distinction for
     a) different cases of the value of Rabs (...) and
     b) wether arguments of multiplication in (nf1 * nF2) are < or >= 0  *)
 \\ qpat_assum `!x. (A /\ B) \/ C` (fn thm => qspecl_then [`nR1 - nF1` ] DISJ_CASES_TAC thm)
 \\ qpat_assum `!x. (A /\ B) \/ C` (fn thm => qspecl_then [`nR2 - nF2` ] DISJ_CASES_TAC thm)
 \\ fs[]
 \\ rpt (qpat_x_assum `abs _ = _` (fn thm => RULE_ASSUM_TAC (fn thm2 => ONCE_REWRITE_RULE [thm] thm2)))
  (* All positive *)
  >- (`nF1 <= nR1 + err1` by (match_mp_tac err_up \\ simp[])
      \\ `nF2 <= nR2 + err2` by (match_mp_tac err_up \\ simp[])
      \\ qpat_assum `!x. (A /\ B) \/ C`
           (fn thm => qspecl_then [`nR1 * nR2 - nF1 * nF2` ] DISJ_CASES_TAC thm)
      \\ fs[real_sub]
      (* Absolute value positive *)
      >-(qspecl_then [`nF1`, `0`] DISJ_CASES_TAC REAL_LTE_TOTAL
         >- (match_mp_tac REAL_LE_TRANS \\ qexists_tac `nR1 * nR2 + nF1 * (- (nR2 + err2))` \\ conj_tac
            >- (fs [REAL_NEG_RMUL] \\ match_mp_tac REAL_MUL_LE_COMPAT_NEG_L \\ conj_tac
                >- (match_mp_tac REAL_LT_IMP_LE \\ simp[])
                >- (simp[REAL_LE_NEG]))
            >- (qspecl_then [`- (nR2 + err2)`, `0`] DISJ_CASES_TAC REAL_LTE_TOTAL
                >- (match_mp_tac REAL_LE_TRANS \\ qexists_tac `nR1 * nR2 + (nR1 - err1) * - (nR2 + err2)` \\ conj_tac
                    >- (fs [REAL_NEG_RMUL] \\ once_rewrite_tac [REAL_MUL_COMM] \\
                        match_mp_tac REAL_MUL_LE_COMPAT_NEG_L \\ conj_tac
                       >- (fs[] \\ match_mp_tac REAL_LT_IMP_LE \\ simp[])
                       >- (match_mp_tac REAL_LE_ADD_FLIP \\ simp[real_sub]))
                    >- (`nR1 * nR2 + (nR1 - err1) * - (nR2 + err2) = - nR1 * err2 + nR2 * err1 + err1 * err2`
                          by (fs[real_sub,REAL_RDISTRIB] \\
                              fs [GSYM REAL_SUB_LNEG, real_sub, REAL_LDISTRIB, REAL_NEG_MUL2, REAL_ADD_ASSOC] \\
                              fs [GSYM real_sub, REAL_SUB_REFL, GSYM REAL_NEG_RMUL, REAL_MUL_COMM]) \\
                        simp[] \\ match_mp_tac REAL_LE_ADD2 \\ conj_tac \\simp[]))
                >- (match_mp_tac REAL_LE_TRANS \\ qexists_tac `nR1 * nR2 + (nR1 + err1) * - (nR2 + err2)` \\ conj_tac
                    >- (fs [REAL_NEG_RMUL] \\ once_rewrite_tac [REAL_MUL_COMM] \\
                        match_mp_tac REAL_LE_LMUL_IMP \\ conj_tac \\ fs[])
                    >- (`nR1 * nR2 + (nR1 + err1) * - (nR2 + err2) = - nR1 * err2 + - nR2 * err1 + - err1 * err2`
                          by (fs[real_sub,REAL_RDISTRIB] \\
                              fs [GSYM REAL_SUB_LNEG, real_sub, REAL_LDISTRIB, REAL_NEG_MUL2, REAL_ADD_ASSOC] \\
                              fs [GSYM real_sub, REAL_SUB_REFL, GSYM REAL_NEG_RMUL, REAL_MUL_COMM]) \\
                        simp[] \\ match_mp_tac REAL_LE_ADD2 \\ conj_tac \\ TRY(simp[GSYM REAL_NEG_LMUL]) \\
                        match_mp_tac REAL_LE_ADD2 \\ conj_tac \\ simp[REAL_NEG_LMUL]))))
         >- (match_mp_tac REAL_LE_TRANS \\ qexists_tac `nR1 * nR2 + nF1 * - (nR2 - err2)` \\ conj_tac
            >- (fs [REAL_NEG_RMUL] \\ match_mp_tac REAL_LE_LMUL_IMP \\ conj_tac \\ simp[REAL_LE_NEG] \\
                match_mp_tac REAL_LE_ADD_FLIP \\ simp[real_sub])
            >- (qspecl_then [`- (nR2 - err2)`, `0`] DISJ_CASES_TAC REAL_LTE_TOTAL
                >- (match_mp_tac REAL_LE_TRANS \\ qexists_tac `nR1 * nR2 + (nR1 - err1) * - (nR2 - err2)` \\ conj_tac
                    >- (fs [REAL_NEG_RMUL] \\ once_rewrite_tac [REAL_MUL_COMM] \\
                        match_mp_tac REAL_MUL_LE_COMPAT_NEG_L \\ conj_tac
                       >- (fs[] \\ match_mp_tac REAL_LT_IMP_LE \\ simp[])
                       >- (match_mp_tac REAL_LE_ADD_FLIP \\ simp[real_sub]))
                    >- (`nR1 * nR2 + (nR1 - err1) * - (nR2 - err2) = nR1 * err2 + nR2 * err1 + - err1 * err2`
                          by (fs[real_sub,REAL_RDISTRIB] \\
                              fs [GSYM REAL_SUB_LNEG, real_sub, REAL_LDISTRIB, REAL_NEG_MUL2, REAL_ADD_ASSOC] \\
                              fs [GSYM real_sub, REAL_SUB_REFL, GSYM REAL_NEG_RMUL, REAL_MUL_COMM]) \\
                        simp[] \\ match_mp_tac REAL_LE_ADD2 \\ conj_tac \\ simp[GSYM REAL_NEG_LMUL] \\
                        match_mp_tac REAL_LE_ADD2 \\ conj_tac \\ simp[REAL_NEG_LMUL]))
                >- (match_mp_tac REAL_LE_TRANS \\ qexists_tac `nR1 * nR2 + (nR1 + err1) * - (nR2 - err2)` \\ conj_tac
                    >- (fs [REAL_NEG_RMUL] \\ once_rewrite_tac [REAL_MUL_COMM] \\
                        match_mp_tac REAL_LE_LMUL_IMP \\ conj_tac \\ fs[])
                    >- (`nR1 * nR2 + (nR1 + err1) * - (nR2 - err2) = nR1 * err2 + - nR2 * err1 + err1 * err2`
                          by (fs[real_sub,REAL_RDISTRIB] \\
                              fs [GSYM REAL_SUB_LNEG, real_sub, REAL_LDISTRIB, REAL_NEG_MUL2, REAL_ADD_ASSOC] \\
                              fs [GSYM real_sub, REAL_SUB_REFL, GSYM REAL_NEG_RMUL, REAL_MUL_COMM]) \\
                        simp[] \\ match_mp_tac REAL_LE_ADD2 \\ conj_tac \\ simp[GSYM REAL_NEG_LMUL] )))))
      (* Absolute value negative *)
      >- (simp[REAL_NEG_ADD] \\
                          qspecl_then [`nF1`, `0`] DISJ_CASES_TAC REAL_LTE_TOTAL
         >- (match_mp_tac REAL_LE_TRANS \\ qexists_tac `-(nR1 * nR2) + nF1 * (nR2 - err2)` \\ conj_tac
            >- (fs [REAL_NEG_RMUL] \\ match_mp_tac REAL_MUL_LE_COMPAT_NEG_L \\ conj_tac
                >- (match_mp_tac REAL_LT_IMP_LE \\ simp[])
                >- (match_mp_tac REAL_LE_ADD_FLIP \\ simp[real_sub]))
            >- (qspecl_then [`nR2 - err2`, `0`] DISJ_CASES_TAC REAL_LTE_TOTAL
                >- (match_mp_tac REAL_LE_TRANS \\ qexists_tac `-(nR1 * nR2) + (nR1 - err1) * (nR2 - err2)` \\ conj_tac
                    >- (fs [REAL_NEG_RMUL] \\ once_rewrite_tac [REAL_MUL_COMM] \\
                        match_mp_tac REAL_MUL_LE_COMPAT_NEG_L \\ conj_tac
                       >- (fs[] \\ match_mp_tac REAL_LT_IMP_LE \\ simp[])
                       >- (match_mp_tac REAL_LE_ADD_FLIP \\ simp[real_sub]))
                    >- (`-(nR1 * nR2) + (nR1 - err1) * (nR2 - err2) = - nR1 * err2 + - nR2 * err1 + err1 * err2`
                          by (fs[real_sub,REAL_RDISTRIB] \\
                              fs [GSYM REAL_SUB_LNEG, real_sub, REAL_LDISTRIB, REAL_NEG_MUL2, REAL_ADD_ASSOC] \\
                              fs [GSYM real_sub, REAL_SUB_REFL, GSYM REAL_NEG_RMUL, REAL_MUL_COMM]) \\
                        simp[] \\ match_mp_tac REAL_LE_ADD2 \\ conj_tac \\simp[]))
                >- (match_mp_tac REAL_LE_TRANS \\ qexists_tac `-(nR1 * nR2) + (nR1 + err1) * (nR2 - err2)` \\ conj_tac
                    >- (fs [REAL_NEG_RMUL] \\ once_rewrite_tac [REAL_MUL_COMM] \\
                        match_mp_tac REAL_LE_LMUL_IMP \\ conj_tac \\ fs[])
                    >- (`-(nR1 * nR2) + (nR1 + err1) * (nR2 - err2) = - nR1 * err2 + nR2 * err1 + - err1 * err2`
                          by (fs[real_sub,REAL_RDISTRIB] \\
                              fs [GSYM REAL_SUB_LNEG, real_sub, REAL_LDISTRIB, REAL_NEG_MUL2, REAL_ADD_ASSOC] \\
                              fs [GSYM real_sub, REAL_SUB_REFL, GSYM REAL_NEG_RMUL, REAL_MUL_COMM]) \\
                        simp[] \\ match_mp_tac REAL_LE_ADD2 \\ conj_tac \\ TRY(simp[GSYM REAL_NEG_LMUL]) \\
                        match_mp_tac REAL_LE_ADD2 \\ conj_tac \\ simp[REAL_NEG_LMUL]))))
         >- (match_mp_tac REAL_LE_TRANS \\ qexists_tac `-(nR1 * nR2) + nF1 * (nR2 + err2)` \\ conj_tac
            >- (fs [REAL_NEG_RMUL] \\ match_mp_tac REAL_LE_LMUL_IMP \\ conj_tac \\ simp[REAL_LE_NEG])
            >- (qspecl_then [`nR2 + err2`, `0`] DISJ_CASES_TAC REAL_LTE_TOTAL
                >- (match_mp_tac REAL_LE_TRANS \\ qexists_tac `-(nR1 * nR2) + (nR1 - err1) * (nR2 + err2)` \\ conj_tac
                    >- (fs [REAL_NEG_RMUL] \\ once_rewrite_tac [REAL_MUL_COMM] \\
                        match_mp_tac REAL_MUL_LE_COMPAT_NEG_L \\ conj_tac
                       >- (fs[] \\ match_mp_tac REAL_LT_IMP_LE \\ simp[])
                       >- (match_mp_tac REAL_LE_ADD_FLIP \\ simp[real_sub]))
                    >- (`-(nR1 * nR2) + (nR1 - err1) * (nR2 + err2) = nR1 * err2 + - nR2 * err1 + - err1 * err2`
                          by (fs[real_sub,REAL_RDISTRIB] \\
                              fs [GSYM REAL_SUB_LNEG, real_sub, REAL_LDISTRIB, REAL_NEG_MUL2, REAL_ADD_ASSOC] \\
                              fs [GSYM real_sub, REAL_SUB_REFL, GSYM REAL_NEG_RMUL, REAL_MUL_COMM]) \\
                        simp[] \\ match_mp_tac REAL_LE_ADD2 \\ conj_tac \\ simp[GSYM REAL_NEG_LMUL] \\
                        match_mp_tac REAL_LE_ADD2 \\ conj_tac \\ simp[REAL_NEG_LMUL]))
                >- (match_mp_tac REAL_LE_TRANS \\ qexists_tac `-(nR1 * nR2) + (nR1 + err1) * (nR2 + err2)` \\ conj_tac
                    >- (fs [REAL_NEG_RMUL] \\ once_rewrite_tac [REAL_MUL_COMM] \\
                        match_mp_tac REAL_LE_LMUL_IMP \\ conj_tac \\ fs[])
                    >- (`-(nR1 * nR2) + (nR1 + err1) * (nR2 + err2) = nR1 * err2 + nR2 * err1 + err1 * err2`
                          by (fs[real_sub,REAL_RDISTRIB] \\
                              fs [GSYM REAL_SUB_LNEG, real_sub, REAL_LDISTRIB, REAL_NEG_MUL2, REAL_ADD_ASSOC] \\
                              fs [GSYM real_sub, REAL_SUB_REFL, GSYM REAL_NEG_RMUL, REAL_MUL_COMM]) \\
                        simp[] \\ match_mp_tac REAL_LE_ADD2 \\ conj_tac \\ simp[GSYM REAL_NEG_LMUL]))))))
  (* First positive, second negative *)
  >- (`nF1 <= nR1 + err1` by (match_mp_tac err_up \\ simp[]) \\
                  `nF2 <= nR2 + err2`
                        by (once_rewrite_tac[REAL_ADD_COMM] \\ simp [GSYM REAL_LE_SUB_RADD] \\
                            once_rewrite_tac [REAL_ADD_COMM, GSYM REAL_NEG_SUB] \\ simp[] ) \\
      qpat_assum `!x. (A /\ B) \/ C` (fn thm => qspecl_then [`nR1 * nR2 - nF1 * nF2` ] DISJ_CASES_TAC thm) \\
                  fs[real_sub]
                  (* Absolute value positive *)
      >-(qspecl_then [`nF1`, `0`] DISJ_CASES_TAC REAL_LTE_TOTAL
         >- (match_mp_tac REAL_LE_TRANS \\ qexists_tac `nR1 * nR2 + nF1 * (- (nR2 + err2))` \\ conj_tac
            >- (fs [REAL_NEG_RMUL] \\ match_mp_tac REAL_MUL_LE_COMPAT_NEG_L \\ conj_tac
                >- (match_mp_tac REAL_LT_IMP_LE \\ simp[])
                >- (simp[REAL_LE_NEG]))
            >- (qspecl_then [`- (nR2 + err2)`, `0`] DISJ_CASES_TAC REAL_LTE_TOTAL
                >- (match_mp_tac REAL_LE_TRANS \\ qexists_tac `nR1 * nR2 + (nR1 - err1) * - (nR2 + err2)` \\ conj_tac
                    >- (fs [REAL_NEG_RMUL] \\ once_rewrite_tac [REAL_MUL_COMM] \\
                        match_mp_tac REAL_MUL_LE_COMPAT_NEG_L \\ conj_tac
                       >- (fs[] \\ match_mp_tac REAL_LT_IMP_LE \\ simp[])
                       >- (match_mp_tac REAL_LE_ADD_FLIP \\ simp[real_sub]))
                    >- (`nR1 * nR2 + (nR1 - err1) * - (nR2 + err2) = - nR1 * err2 + nR2 * err1 + err1 * err2`
                          by (fs[real_sub,REAL_RDISTRIB] \\
                              fs [GSYM REAL_SUB_LNEG, real_sub, REAL_LDISTRIB, REAL_NEG_MUL2, REAL_ADD_ASSOC] \\
                              fs [GSYM real_sub, REAL_SUB_REFL, GSYM REAL_NEG_RMUL, REAL_MUL_COMM]) \\
                        simp[] \\ match_mp_tac REAL_LE_ADD2 \\ conj_tac \\simp[]))
                >- (match_mp_tac REAL_LE_TRANS \\ qexists_tac `nR1 * nR2 + (nR1 + err1) * - (nR2 + err2)` \\ conj_tac
                    >- (fs [REAL_NEG_RMUL] \\ once_rewrite_tac [REAL_MUL_COMM] \\
                        match_mp_tac REAL_LE_LMUL_IMP \\ conj_tac \\ fs[])
                    >- (`nR1 * nR2 + (nR1 + err1) * - (nR2 + err2) = - nR1 * err2 + - nR2 * err1 + - err1 * err2`
                          by (fs[real_sub,REAL_RDISTRIB] \\
                              fs [GSYM REAL_SUB_LNEG, real_sub, REAL_LDISTRIB, REAL_NEG_MUL2, REAL_ADD_ASSOC] \\
                              fs [GSYM real_sub, REAL_SUB_REFL, GSYM REAL_NEG_RMUL, REAL_MUL_COMM]) \\
                        simp[] \\ match_mp_tac REAL_LE_ADD2 \\ conj_tac \\ TRY(simp[GSYM REAL_NEG_LMUL]) \\
                        match_mp_tac REAL_LE_ADD2 \\ conj_tac \\ simp[REAL_NEG_LMUL]))))
         >- (match_mp_tac REAL_LE_TRANS \\ qexists_tac `nR1 * nR2 + nF1 * -nR2` \\ conj_tac
            >- (fs [REAL_NEG_RMUL] \\ match_mp_tac REAL_LE_LMUL_IMP \\ conj_tac \\ simp[REAL_LE_NEG] \\
                                        qpat_x_assum `nR2 + - nF2 <= _ `
                                          (fn thm => assume_tac (SIMP_RULE bool_ss [GSYM real_sub, REAL_LE_SUB_RADD, REAL_ADD_LID] thm))\\
                                        simp[])
            >- (qspecl_then [`- nR2`, `0`] DISJ_CASES_TAC REAL_LTE_TOTAL
                >- (match_mp_tac REAL_LE_TRANS \\ qexists_tac `nR1 * nR2 + (nR1 - err1) * - nR2` \\ conj_tac
                    >- (fs [REAL_NEG_RMUL] \\ once_rewrite_tac [REAL_MUL_COMM] \\
                        match_mp_tac REAL_MUL_LE_COMPAT_NEG_L \\ conj_tac
                       >- (fs[] \\ match_mp_tac REAL_LT_IMP_LE \\ simp[])
                       >- (match_mp_tac REAL_LE_ADD_FLIP \\ simp[real_sub]))
                    >- (`nR1 * nR2 + (nR1 - err1) * - nR2 = nR2 * err1`
                          by (fs[real_sub,REAL_RDISTRIB]
                              \\ fs [GSYM REAL_SUB_LNEG, real_sub, REAL_LDISTRIB, REAL_NEG_MUL2, REAL_ADD_ASSOC]
                              \\ fs [GSYM real_sub, REAL_SUB_REFL, GSYM REAL_NEG_RMUL, REAL_MUL_COMM])
                              \\ simp[] \\ match_mp_tac REAL_LE_TRANS
                              \\ qexists_tac `maxAbs (e2lo,e2hi) * err1` \\ conj_tac \\ simp[]
                              \\ match_mp_tac REAL_LE_TRANS
                              \\ qexists_tac `maxAbs (e1lo, e1hi) * err2 + maxAbs (e2lo, e2hi) * err1`
                              \\ conj_tac \\ simp[]
                              \\ once_rewrite_tac [REAL_ADD_COMM]
                              \\ simp [REAL_LE_ADDR]))
                >- (match_mp_tac REAL_LE_TRANS \\ qexists_tac `nR1 * nR2 + (nR1 + err1) * - nR2` \\ conj_tac
                    >- (fs [REAL_NEG_RMUL] \\ once_rewrite_tac [REAL_MUL_COMM] \\
                        match_mp_tac REAL_LE_LMUL_IMP \\ conj_tac \\ fs[])
                    >- (`nR1 * nR2 + (nR1 + err1) * - nR2 = - nR2 * err1`
                          by (fs[real_sub,REAL_RDISTRIB]
                              \\ fs [GSYM REAL_SUB_LNEG, real_sub, REAL_LDISTRIB, REAL_NEG_MUL2, REAL_ADD_ASSOC]
                              \\ fs [GSYM real_sub, REAL_SUB_REFL, GSYM REAL_NEG_RMUL, REAL_MUL_COMM])
                              \\ simp[] \\ match_mp_tac REAL_LE_TRANS
                              \\ qexists_tac `maxAbs (e2lo,e2hi) * err1` \\ conj_tac \\ simp[]
                              \\ match_mp_tac REAL_LE_TRANS
                              \\ qexists_tac `maxAbs (e1lo, e1hi) * err2 + maxAbs (e2lo, e2hi) * err1`
                              \\ conj_tac \\ simp[]
                              \\ once_rewrite_tac [REAL_ADD_COMM]
                              \\ simp [REAL_LE_ADDR])))))
      (* Absolute value negative *)
      >- (simp[REAL_NEG_ADD] \\
                          qspecl_then [`nF1`, `0`] DISJ_CASES_TAC REAL_LTE_TOTAL
         >- (match_mp_tac REAL_LE_TRANS \\ qexists_tac `-(nR1 * nR2) + nF1 * nR2` \\ conj_tac
            >- (fs [REAL_NEG_RMUL] \\ match_mp_tac REAL_MUL_LE_COMPAT_NEG_L \\ conj_tac
                >- (match_mp_tac REAL_LT_IMP_LE \\ simp[])
                >- (qpat_x_assum `nR2 + - nF2 <= _ `
                      (fn thm =>
                          assume_tac
                            (SIMP_RULE bool_ss [GSYM real_sub, REAL_LE_SUB_RADD, REAL_ADD_LID] thm))
                    \\ simp[]))
            >- (qspecl_then [`nR2`, `0`] DISJ_CASES_TAC REAL_LTE_TOTAL
                >- (match_mp_tac REAL_LE_TRANS \\ qexists_tac `-(nR1 * nR2) + (nR1 - err1) * nR2` \\ conj_tac
                    >- (fs [REAL_NEG_RMUL] \\ once_rewrite_tac [REAL_MUL_COMM] \\
                        match_mp_tac REAL_MUL_LE_COMPAT_NEG_L \\ conj_tac
                       >- (fs[] \\ match_mp_tac REAL_LT_IMP_LE \\ simp[])
                       >- (match_mp_tac REAL_LE_ADD_FLIP \\ simp[real_sub]))
                    >- (`-(nR1 * nR2) + (nR1 - err1) * nR2 = - nR2 * err1`
                          by (fs[real_sub,REAL_RDISTRIB]
                              \\ fs [GSYM REAL_SUB_LNEG, real_sub, REAL_LDISTRIB, REAL_NEG_MUL2, REAL_ADD_ASSOC]
                              \\ fs [GSYM real_sub, REAL_SUB_REFL, GSYM REAL_NEG_RMUL, REAL_MUL_COMM])
                              \\ simp[] \\ match_mp_tac REAL_LE_TRANS
                              \\ qexists_tac `maxAbs (e2lo,e2hi) * err1` \\ conj_tac \\ simp[]
                              \\ match_mp_tac REAL_LE_TRANS
                              \\ qexists_tac `maxAbs (e1lo, e1hi) * err2 + maxAbs (e2lo, e2hi) * err1`
                              \\ conj_tac \\ simp[]
                              \\ once_rewrite_tac [REAL_ADD_COMM]
                              \\ simp [REAL_LE_ADDR]))
                >- (match_mp_tac REAL_LE_TRANS \\ qexists_tac `-(nR1 * nR2) + (nR1 + err1) * nR2` \\ conj_tac
                    >- (fs [REAL_NEG_RMUL] \\ once_rewrite_tac [REAL_MUL_COMM] \\
                        match_mp_tac REAL_LE_LMUL_IMP \\ conj_tac \\ fs[])
                    >- (`-(nR1 * nR2) + (nR1 + err1) * nR2 = nR2 * err1`
                          by (fs[real_sub,REAL_RDISTRIB]
                              \\ fs [GSYM REAL_SUB_LNEG, real_sub, REAL_LDISTRIB, REAL_NEG_MUL2, REAL_ADD_ASSOC]
                              \\ fs [GSYM real_sub, REAL_SUB_REFL, GSYM REAL_NEG_RMUL, REAL_MUL_COMM])
                              \\ simp[] \\ match_mp_tac REAL_LE_TRANS
                              \\ qexists_tac `maxAbs (e2lo,e2hi) * err1` \\ conj_tac \\ simp[]
                              \\ match_mp_tac REAL_LE_TRANS
                              \\ qexists_tac `maxAbs (e1lo, e1hi) * err2 + maxAbs (e2lo, e2hi) * err1`
                              \\ conj_tac \\ simp[]
                              \\ once_rewrite_tac [REAL_ADD_COMM]
                              \\ simp [REAL_LE_ADDR]))))
         >- (match_mp_tac REAL_LE_TRANS \\ qexists_tac `-(nR1 * nR2) + nF1 * (nR2 + err2)` \\ conj_tac
            >- (fs [REAL_NEG_RMUL] \\ match_mp_tac REAL_LE_LMUL_IMP \\ conj_tac \\ simp[REAL_LE_NEG])
            >- (qspecl_then [`nR2 + err2`, `0`] DISJ_CASES_TAC REAL_LTE_TOTAL
                >- (match_mp_tac REAL_LE_TRANS \\ qexists_tac `-(nR1 * nR2) + (nR1 - err1) * (nR2 + err2)` \\ conj_tac
                    >- (fs [REAL_NEG_RMUL] \\ once_rewrite_tac [REAL_MUL_COMM] \\
                        match_mp_tac REAL_MUL_LE_COMPAT_NEG_L \\ conj_tac
                       >- (fs[] \\ match_mp_tac REAL_LT_IMP_LE \\ simp[])
                       >- (match_mp_tac REAL_LE_ADD_FLIP \\ simp[real_sub]))
                    >- (`-(nR1 * nR2) + (nR1 - err1) * (nR2 + err2) = nR1 * err2 + - nR2 * err1 + - err1 * err2`
                          by (fs[real_sub,REAL_RDISTRIB] \\
                              fs [GSYM REAL_SUB_LNEG, real_sub, REAL_LDISTRIB, REAL_NEG_MUL2, REAL_ADD_ASSOC] \\
                              fs [GSYM real_sub, REAL_SUB_REFL, GSYM REAL_NEG_RMUL, REAL_MUL_COMM]) \\
                        simp[] \\ match_mp_tac REAL_LE_ADD2 \\ conj_tac \\ simp[GSYM REAL_NEG_LMUL] \\
                        match_mp_tac REAL_LE_ADD2 \\ conj_tac \\ simp[REAL_NEG_LMUL]))
                >- (match_mp_tac REAL_LE_TRANS \\ qexists_tac `-(nR1 * nR2) + (nR1 + err1) * (nR2 + err2)` \\ conj_tac
                    >- (fs [REAL_NEG_RMUL] \\ once_rewrite_tac [REAL_MUL_COMM] \\
                        match_mp_tac REAL_LE_LMUL_IMP \\ conj_tac \\ fs[])
                    >- (`-(nR1 * nR2) + (nR1 + err1) * (nR2 + err2) = nR1 * err2 + nR2 * err1 + err1 * err2`
                          by (fs[real_sub,REAL_RDISTRIB] \\
                              fs [GSYM REAL_SUB_LNEG, real_sub, REAL_LDISTRIB, REAL_NEG_MUL2, REAL_ADD_ASSOC] \\
                              fs [GSYM real_sub, REAL_SUB_REFL, GSYM REAL_NEG_RMUL, REAL_MUL_COMM]) \\
                        simp[] \\ match_mp_tac REAL_LE_ADD2 \\ conj_tac \\ simp[GSYM REAL_NEG_LMUL]))))))
  (* First negative, second positive *)
  >- (`nF2 <= nR2 + err2` by (match_mp_tac err_up \\ simp[]) \\
                  `nF1 <= nR1 + err1`
                        by (once_rewrite_tac[REAL_ADD_COMM] \\ simp [GSYM REAL_LE_SUB_RADD] \\
                            once_rewrite_tac [REAL_ADD_COMM, GSYM REAL_NEG_SUB] \\ simp[]) \\
      qpat_assum `!x. (A /\ B) \/ C` (fn thm => qspecl_then [`nR1 * nR2 - nF1 * nF2` ] DISJ_CASES_TAC thm) \\
                  fs[real_sub]
                  (* Absolute value positive *)
      >-(qspecl_then [`nF1`, `0`] DISJ_CASES_TAC REAL_LTE_TOTAL
         >- (match_mp_tac REAL_LE_TRANS \\ qexists_tac `nR1 * nR2 + nF1 * - (nR2 + err2)` \\ conj_tac
            >- (fs [REAL_NEG_RMUL] \\ match_mp_tac REAL_MUL_LE_COMPAT_NEG_L \\ conj_tac
                >- (match_mp_tac REAL_LT_IMP_LE \\ simp[])
                >- (simp[REAL_LE_NEG]))
            >- (qspecl_then [`- (nR2 + err2)`, `0`] DISJ_CASES_TAC REAL_LTE_TOTAL
                >- (match_mp_tac REAL_LE_TRANS \\ qexists_tac `nR1 * nR2 + nR1 * - (nR2 + err2)` \\ conj_tac
                    >- (fs [REAL_NEG_RMUL] \\ once_rewrite_tac [REAL_MUL_COMM] \\
                        match_mp_tac REAL_MUL_LE_COMPAT_NEG_L \\ conj_tac
                       >- (fs[] \\ match_mp_tac REAL_LT_IMP_LE \\ simp[])
                       >- (qpat_x_assum `nR1 + - nF1 <= _ `
                                                                (fn thm => assume_tac (SIMP_RULE bool_ss [GSYM real_sub, REAL_LE_SUB_RADD, REAL_ADD_LID] thm))\\
                                                           simp[]))
                    >- (`nR1 * nR2 + nR1 * - (nR2 + err2) = - nR1 * err2`
                          by (fs[real_sub,REAL_RDISTRIB]
                              \\ fs [GSYM REAL_SUB_LNEG, real_sub, REAL_LDISTRIB, REAL_NEG_MUL2, REAL_ADD_ASSOC]
                              \\ fs [GSYM real_sub, REAL_SUB_REFL, GSYM REAL_NEG_RMUL, REAL_MUL_COMM])
                              \\ simp[] \\ match_mp_tac REAL_LE_TRANS
                              \\ qexists_tac `maxAbs (e1lo, e1hi) * err2` \\ conj_tac \\ simp[]
                              \\ match_mp_tac REAL_LE_TRANS
                              \\ qexists_tac `maxAbs (e1lo, e1hi) * err2 + maxAbs (e2lo, e2hi) * err1`
                              \\ conj_tac \\ simp[]))
                >- (match_mp_tac REAL_LE_TRANS \\ qexists_tac `nR1 * nR2 + (nR1 + err1) * - (nR2 + err2)` \\ conj_tac
                    >- (fs [REAL_NEG_RMUL] \\ once_rewrite_tac [REAL_MUL_COMM] \\
                        match_mp_tac REAL_LE_LMUL_IMP \\ conj_tac \\ fs[])
                    >- (`nR1 * nR2 + (nR1 + err1) * - (nR2 + err2) = - nR1 * err2 + - nR2 * err1 + - err1 * err2`
                          by (fs[real_sub,REAL_RDISTRIB]
                              \\ fs [GSYM REAL_SUB_LNEG, real_sub, REAL_LDISTRIB, REAL_NEG_MUL2, REAL_ADD_ASSOC]
                              \\ fs [GSYM real_sub, REAL_SUB_REFL, GSYM REAL_NEG_RMUL, REAL_MUL_COMM])
                              \\ simp[] \\ match_mp_tac REAL_LE_ADD2 \\ conj_tac \\ TRY(simp[GSYM REAL_NEG_LMUL])
                              \\ match_mp_tac REAL_LE_ADD2 \\ conj_tac \\ simp[REAL_NEG_LMUL]))))
         >- (match_mp_tac REAL_LE_TRANS \\ qexists_tac `nR1 * nR2 + nF1 * - (nR2 - err2)` \\ conj_tac
            >- (fs [REAL_NEG_RMUL] \\ match_mp_tac REAL_LE_LMUL_IMP \\ conj_tac \\ simp[REAL_LE_NEG] \\
                match_mp_tac REAL_LE_ADD_FLIP \\ simp[real_sub])
            >- (qspecl_then [`- (nR2 - err2)`, `0`] DISJ_CASES_TAC REAL_LTE_TOTAL
                >- (match_mp_tac REAL_LE_TRANS \\ qexists_tac `nR1 * nR2 + nR1 * - (nR2 - err2)` \\ conj_tac
                    >- (fs [REAL_NEG_RMUL] \\ once_rewrite_tac [REAL_MUL_COMM] \\
                        match_mp_tac REAL_MUL_LE_COMPAT_NEG_L \\ conj_tac
                       >- (fs[] \\ match_mp_tac REAL_LT_IMP_LE \\ simp[])
                       >- (qpat_x_assum `nR1 + - nF1 <= _ `
                             (fn thm =>
                                 assume_tac
                                   (SIMP_RULE bool_ss [GSYM real_sub, REAL_LE_SUB_RADD, REAL_ADD_LID] thm))
                           \\ simp[]))
                    >- (`nR1 * nR2 + nR1 * - (nR2 - err2) = nR1 * err2`
                          by (fs[real_sub,REAL_RDISTRIB]
                              \\ fs [GSYM REAL_SUB_LNEG, real_sub, REAL_LDISTRIB, REAL_NEG_MUL2, REAL_ADD_ASSOC]
                              \\ fs [GSYM real_sub, REAL_SUB_REFL, GSYM REAL_NEG_RMUL, REAL_MUL_COMM])
                              \\ simp[] \\ match_mp_tac REAL_LE_TRANS
                              \\ qexists_tac `maxAbs (e1lo, e1hi) * err2` \\ conj_tac \\ simp[]
                              \\ match_mp_tac REAL_LE_TRANS
                              \\ qexists_tac `maxAbs (e1lo, e1hi) * err2 + maxAbs (e2lo, e2hi) * err1`
                              \\ conj_tac \\ simp[]))
                >- (match_mp_tac REAL_LE_TRANS \\ qexists_tac `nR1 * nR2 + (nR1 + err1) * - (nR2 - err2)` \\ conj_tac
                    >- (fs [REAL_NEG_RMUL] \\ once_rewrite_tac [REAL_MUL_COMM] \\
                        match_mp_tac REAL_LE_LMUL_IMP \\ conj_tac \\ fs[])
                    >- (`nR1 * nR2 + (nR1 + err1) * - (nR2 - err2) = nR1 * err2 + - nR2 * err1 + err1 * err2`
                          by (fs[real_sub,REAL_RDISTRIB]
                              \\ fs [GSYM REAL_SUB_LNEG, real_sub, REAL_LDISTRIB, REAL_NEG_MUL2, REAL_ADD_ASSOC]
                              \\ fs [GSYM real_sub, REAL_SUB_REFL, GSYM REAL_NEG_RMUL, REAL_MUL_COMM])
                        \\ simp[] \\ match_mp_tac REAL_LE_ADD2 \\ conj_tac \\ simp[GSYM REAL_NEG_LMUL] )))))
      (* Absolute value negative *)
      >- (simp[REAL_NEG_ADD] \\
                          qspecl_then [`nF1`, `0`] DISJ_CASES_TAC REAL_LTE_TOTAL
         >- (match_mp_tac REAL_LE_TRANS \\ qexists_tac `-(nR1 * nR2) + nF1 * (nR2 - err2)` \\ conj_tac
            >- (fs [REAL_NEG_RMUL] \\ match_mp_tac REAL_MUL_LE_COMPAT_NEG_L \\ conj_tac
                >- (match_mp_tac REAL_LT_IMP_LE \\ simp[])
                >- (match_mp_tac REAL_LE_ADD_FLIP \\ simp[real_sub]))
            >- (qspecl_then [`nR2 - err2`, `0`] DISJ_CASES_TAC REAL_LTE_TOTAL
                >- (match_mp_tac REAL_LE_TRANS \\ qexists_tac `-(nR1 * nR2) + nR1 * (nR2 - err2)` \\ conj_tac
                    >- (fs [REAL_NEG_RMUL] \\ once_rewrite_tac [REAL_MUL_COMM] \\
                        match_mp_tac REAL_MUL_LE_COMPAT_NEG_L \\ conj_tac
                       >- (fs[] \\ match_mp_tac REAL_LT_IMP_LE \\ simp[])
                       >- (qpat_x_assum `nR1 + - nF1 <= _ `
                             (fn thm =>
                                  assume_tac
                                    (SIMP_RULE bool_ss [GSYM real_sub, REAL_LE_SUB_RADD, REAL_ADD_LID] thm))
                           \\ simp[]))
                    >- (`-(nR1 * nR2) + nR1 * (nR2 - err2) = - nR1 * err2`
                          by (fs[real_sub,REAL_RDISTRIB]
                              \\ fs [GSYM REAL_SUB_LNEG, real_sub, REAL_LDISTRIB, REAL_NEG_MUL2, REAL_ADD_ASSOC]
                              \\ fs [GSYM real_sub, REAL_SUB_REFL, GSYM REAL_NEG_RMUL, REAL_MUL_COMM])
                              \\ simp[] \\ match_mp_tac REAL_LE_TRANS
                              \\ qexists_tac `maxAbs (e1lo, e1hi) * err2` \\ conj_tac \\ simp[]
                              \\ match_mp_tac REAL_LE_TRANS
                              \\ qexists_tac `maxAbs (e1lo, e1hi) * err2 + maxAbs (e2lo, e2hi) * err1`
                              \\ conj_tac \\ simp[]))
                >- (match_mp_tac REAL_LE_TRANS \\ qexists_tac `-(nR1 * nR2) + (nR1 + err1) * (nR2 - err2)` \\ conj_tac
                    >- (fs [REAL_NEG_RMUL] \\ once_rewrite_tac [REAL_MUL_COMM] \\
                        match_mp_tac REAL_LE_LMUL_IMP \\ conj_tac \\ fs[])
                    >- (`-(nR1 * nR2) + (nR1 + err1) * (nR2 - err2) = - nR1 * err2 + nR2 * err1 + - err1 * err2`
                          by (fs[real_sub,REAL_RDISTRIB]
                              \\ fs [GSYM REAL_SUB_LNEG, real_sub, REAL_LDISTRIB, REAL_NEG_MUL2, REAL_ADD_ASSOC]
                              \\ fs [GSYM real_sub, REAL_SUB_REFL, GSYM REAL_NEG_RMUL, REAL_MUL_COMM])
                        \\ simp[] \\ match_mp_tac REAL_LE_ADD2
                        \\ conj_tac \\ TRY(simp[GSYM REAL_NEG_LMUL])
                        \\ match_mp_tac REAL_LE_ADD2 \\ conj_tac \\ simp[REAL_NEG_LMUL]))))
         >- (match_mp_tac REAL_LE_TRANS \\ qexists_tac `-(nR1 * nR2) + nF1 * (nR2 + err2)` \\ conj_tac
            >- (fs [REAL_NEG_RMUL] \\ match_mp_tac REAL_LE_LMUL_IMP \\ conj_tac \\ simp[REAL_LE_NEG])
            >- (qspecl_then [`nR2 + err2`, `0`] DISJ_CASES_TAC REAL_LTE_TOTAL
                >- (match_mp_tac REAL_LE_TRANS \\ qexists_tac `-(nR1 * nR2) + nR1 * (nR2 + err2)` \\ conj_tac
                    >- (fs [REAL_NEG_RMUL] \\ once_rewrite_tac [REAL_MUL_COMM] \\
                        match_mp_tac REAL_MUL_LE_COMPAT_NEG_L \\ conj_tac
                       >- (fs[] \\ match_mp_tac REAL_LT_IMP_LE \\ simp[])
                       >- (qpat_x_assum `nR1 + - nF1 <= _ `
                             (fn thm =>
                                 assume_tac
                                   (SIMP_RULE bool_ss [GSYM real_sub, REAL_LE_SUB_RADD, REAL_ADD_LID] thm))
                           \\ simp[]))
                    >- (`-(nR1 * nR2) + nR1 * (nR2 + err2) = nR1 * err2`
                          by (fs[real_sub,REAL_RDISTRIB]
                              \\ fs [GSYM REAL_SUB_LNEG, real_sub, REAL_LDISTRIB, REAL_NEG_MUL2, REAL_ADD_ASSOC]
                              \\ fs [GSYM real_sub, REAL_SUB_REFL, GSYM REAL_NEG_RMUL, REAL_MUL_COMM])
                              \\ simp[] \\ match_mp_tac REAL_LE_TRANS
                              \\ qexists_tac `maxAbs (e1lo, e1hi) * err2` \\ conj_tac \\ simp[]
                              \\ match_mp_tac REAL_LE_TRANS
                              \\ qexists_tac `maxAbs (e1lo, e1hi) * err2 + maxAbs (e2lo, e2hi) * err1`
                              \\ conj_tac \\ simp[]))
                >- (match_mp_tac REAL_LE_TRANS \\ qexists_tac `-(nR1 * nR2) + (nR1 + err1) * (nR2 + err2)` \\ conj_tac
                    >- (fs [REAL_NEG_RMUL] \\ once_rewrite_tac [REAL_MUL_COMM] \\
                        match_mp_tac REAL_LE_LMUL_IMP \\ conj_tac \\ fs[])
                    >- (`-(nR1 * nR2) + (nR1 + err1) * (nR2 + err2) = nR1 * err2 + nR2 * err1 + err1 * err2`
                          by (fs[real_sub,REAL_RDISTRIB] \\
                              fs [GSYM REAL_SUB_LNEG, real_sub, REAL_LDISTRIB, REAL_NEG_MUL2, REAL_ADD_ASSOC] \\
                              fs [GSYM real_sub, REAL_SUB_REFL, GSYM REAL_NEG_RMUL, REAL_MUL_COMM]) \\
                        simp[] \\ match_mp_tac REAL_LE_ADD2 \\ conj_tac \\ simp[GSYM REAL_NEG_LMUL]))))))
  (* Both negative *)
  >- (`nF1 <= nR1 + err1`
        by (once_rewrite_tac[REAL_ADD_COMM]
            \\ simp [GSYM REAL_LE_SUB_RADD]
            \\ once_rewrite_tac [REAL_ADD_COMM, GSYM REAL_NEG_SUB] \\ simp[])
      \\ `nF2 <= nR2 + err2`
           by (once_rewrite_tac[REAL_ADD_COMM]
               \\ simp [GSYM REAL_LE_SUB_RADD]
               \\ once_rewrite_tac [REAL_ADD_COMM, GSYM REAL_NEG_SUB] \\ simp[])
      \\ `nR1 <= nF1`
           by (qpat_x_assum `nR1 - nF1 <= _ `
                 (fn thm =>
                     assume_tac
                       (SIMP_RULE bool_ss [GSYM real_sub, REAL_LE_SUB_RADD, REAL_ADD_LID] thm))
               \\ simp[])
      \\ `nR2 <= nF2`
           by (qpat_x_assum `nR2 - nF2 <= _ `
                 (fn thm =>
                     assume_tac (SIMP_RULE bool_ss [GSYM real_sub, REAL_LE_SUB_RADD, REAL_ADD_LID] thm))
               \\ simp[])
      \\ qpat_assum `!x. (A /\ B) \/ C`
           (fn thm => qspecl_then [`nR1 * nR2 - nF1 * nF2` ] DISJ_CASES_TAC thm)
      \\ fs[real_sub]
      (* Absolute value positive *)
      >-(qspecl_then [`nF1`, `0`] DISJ_CASES_TAC REAL_LTE_TOTAL
         >- (match_mp_tac REAL_LE_TRANS \\ qexists_tac `nR1 * nR2 + nF1 * - (nR2 + err2)` \\ conj_tac
            >- (fs [REAL_NEG_RMUL] \\ match_mp_tac REAL_MUL_LE_COMPAT_NEG_L \\ conj_tac
                >- (match_mp_tac REAL_LT_IMP_LE \\ simp[])
                >- (simp[REAL_LE_NEG]))
            >- (qspecl_then [`- (nR2 + err2)`, `0`] DISJ_CASES_TAC REAL_LTE_TOTAL
                >- (match_mp_tac REAL_LE_TRANS \\ qexists_tac `nR1 * nR2 + nR1 * - (nR2 + err2)` \\ conj_tac
                    >- (fs [REAL_NEG_RMUL] \\ once_rewrite_tac [REAL_MUL_COMM] \\
                        match_mp_tac REAL_MUL_LE_COMPAT_NEG_L \\ conj_tac
                       >- (fs[] \\ match_mp_tac REAL_LT_IMP_LE \\ simp[])
                       >- (qpat_x_assum `nR1 + - nF1 <= _ `
                             (fn thm =>
                                 assume_tac
                                   (SIMP_RULE bool_ss [GSYM real_sub, REAL_LE_SUB_RADD, REAL_ADD_LID] thm))
                           \\ simp[]))
                    >- (`nR1 * nR2 + nR1 * - (nR2 + err2) = - nR1 * err2`
                          by (fs[real_sub,REAL_RDISTRIB]
                              \\ fs [GSYM REAL_SUB_LNEG, real_sub, REAL_LDISTRIB, REAL_NEG_MUL2, REAL_ADD_ASSOC]
                              \\ fs [GSYM real_sub, REAL_SUB_REFL, GSYM REAL_NEG_RMUL, REAL_MUL_COMM])
                              \\ simp[] \\ match_mp_tac REAL_LE_TRANS
                              \\ qexists_tac `maxAbs (e1lo, e1hi) * err2`
                              \\ conj_tac \\ simp[]
                              \\ match_mp_tac REAL_LE_TRANS
                              \\ qexists_tac `maxAbs (e1lo, e1hi) * err2 + maxAbs (e2lo, e2hi) * err1`
                              \\ conj_tac \\ simp[]))
                >- (match_mp_tac REAL_LE_TRANS
                    \\ qexists_tac `nR1 * nR2 + (nR1 + err1) * - (nR2 + err2)`
                    \\ conj_tac
                    >- (fs [REAL_NEG_RMUL]
                        \\ once_rewrite_tac [REAL_MUL_COMM]
                        \\ match_mp_tac REAL_LE_LMUL_IMP
                        \\ conj_tac \\ fs[])
                    >- (`nR1 * nR2 + (nR1 + err1) * - (nR2 + err2) = - nR1 * err2 + - nR2 * err1 + - err1 * err2`
                          by (fs[real_sub,REAL_RDISTRIB]
                              \\ fs [GSYM REAL_SUB_LNEG, real_sub, REAL_LDISTRIB, REAL_NEG_MUL2, REAL_ADD_ASSOC]
                              \\ fs [GSYM real_sub, REAL_SUB_REFL, GSYM REAL_NEG_RMUL, REAL_MUL_COMM])
                        \\ simp[] \\ match_mp_tac REAL_LE_ADD2
                        \\ conj_tac \\ TRY(simp[GSYM REAL_NEG_LMUL])
                        \\ match_mp_tac REAL_LE_ADD2
                        \\ conj_tac \\ simp[REAL_NEG_LMUL]))))
         >- (match_mp_tac REAL_LE_TRANS \\ qexists_tac `nR1 * nR2 + nF1 * - nR2` \\ conj_tac
            >- (fs [REAL_NEG_RMUL] \\ match_mp_tac REAL_LE_LMUL_IMP \\ conj_tac \\ simp[REAL_LE_NEG] \\
                match_mp_tac REAL_LE_ADD_FLIP \\ simp[real_sub])
            >- (qspecl_then [`- nR2`, `0`] DISJ_CASES_TAC REAL_LTE_TOTAL
                >- (match_mp_tac REAL_LE_TRANS \\ qexists_tac `nR1 * nR2 + nR1 * - nR2` \\ conj_tac
                    >- (fs [REAL_NEG_RMUL] \\ once_rewrite_tac [REAL_MUL_COMM] \\
                        match_mp_tac REAL_MUL_LE_COMPAT_NEG_L \\ conj_tac
                       >- (fs[] \\ match_mp_tac REAL_LT_IMP_LE \\ simp[])
                       >- (simp[]))
                    >- (`nR1 * nR2 + nR1 * - nR2 = 0`
                          by (fs[real_sub,REAL_RDISTRIB]
                              \\ fs [GSYM REAL_SUB_LNEG, real_sub, REAL_LDISTRIB, REAL_NEG_MUL2, REAL_ADD_ASSOC]
                              \\ fs [GSYM real_sub, REAL_SUB_REFL, GSYM REAL_NEG_RMUL, REAL_MUL_COMM])
                              \\ simp[] \\ match_mp_tac REAL_LE_TRANS
                              \\ qexists_tac `maxAbs (e1lo, e1hi) * err2`
                              \\ conj_tac \\ simp[]
                              \\ match_mp_tac REAL_LE_TRANS
                              \\ qexists_tac `maxAbs (e1lo, e1hi) * err2 + maxAbs (e2lo, e2hi) * err1`
                              \\ conj_tac \\ simp[]))
                >- (match_mp_tac REAL_LE_TRANS \\ qexists_tac `nR1 * nR2 + (nR1 + err1) * - nR2` \\ conj_tac
                    >- (fs [REAL_NEG_RMUL] \\ once_rewrite_tac [REAL_MUL_COMM] \\
                        match_mp_tac REAL_LE_LMUL_IMP \\ conj_tac \\ fs[])
                    >- (`nR1 * nR2 + (nR1 + err1) * - nR2 = - nR2 * err1`
                          by (fs[real_sub,REAL_RDISTRIB]
                              \\ fs [GSYM REAL_SUB_LNEG, real_sub, REAL_LDISTRIB, REAL_NEG_MUL2, REAL_ADD_ASSOC]
                              \\ fs [GSYM real_sub, REAL_SUB_REFL, GSYM REAL_NEG_RMUL, REAL_MUL_COMM])
                              \\ simp[] \\ match_mp_tac REAL_LE_TRANS
                              \\ qexists_tac `maxAbs (e2lo, e2hi) * err1` \\ conj_tac \\ simp[]
                              \\ match_mp_tac REAL_LE_TRANS
                              \\ qexists_tac `maxAbs (e1lo, e1hi) * err2 + maxAbs (e2lo, e2hi) * err1`
                              \\ conj_tac \\ simp[]
                              \\ once_rewrite_tac [REAL_ADD_COMM]
                              \\ simp[REAL_LE_ADDR])))))
      (* Absolute value negative *)
      >- (simp[REAL_NEG_ADD] \\
                          qspecl_then [`nF1`, `0`] DISJ_CASES_TAC REAL_LTE_TOTAL
         >- (match_mp_tac REAL_LE_TRANS \\ qexists_tac `-(nR1 * nR2) + nF1 * nR2` \\ conj_tac
            >- (fs [REAL_NEG_RMUL] \\ match_mp_tac REAL_MUL_LE_COMPAT_NEG_L \\ conj_tac
                >- (match_mp_tac REAL_LT_IMP_LE \\ simp[])
                >- (simp[]))
            >- (qspecl_then [`nR2`, `0`] DISJ_CASES_TAC REAL_LTE_TOTAL
                >- (match_mp_tac REAL_LE_TRANS \\ qexists_tac `-(nR1 * nR2) + nR1 * nR2` \\ conj_tac
                    >- (fs [REAL_NEG_RMUL] \\ once_rewrite_tac [REAL_MUL_COMM] \\
                        match_mp_tac REAL_MUL_LE_COMPAT_NEG_L \\ conj_tac
                       >- (fs[] \\ match_mp_tac REAL_LT_IMP_LE \\ simp[])
                       >- (simp[]))
                    >- (`-(nR1 * nR2) + nR1 * nR2 = 0`
                          by (fs[real_sub,REAL_RDISTRIB]
                              \\ fs [GSYM REAL_SUB_LNEG, real_sub, REAL_LDISTRIB, REAL_NEG_MUL2, REAL_ADD_ASSOC]
                              \\ fs [GSYM real_sub, REAL_SUB_REFL, GSYM REAL_NEG_RMUL, REAL_MUL_COMM])
                              \\ simp[] \\ match_mp_tac REAL_LE_TRANS
                              \\ qexists_tac `maxAbs (e1lo, e1hi) * err2` \\ conj_tac \\ simp[]
                              \\ match_mp_tac REAL_LE_TRANS
                              \\ qexists_tac `maxAbs (e1lo, e1hi) * err2 + maxAbs (e2lo, e2hi) * err1`
                              \\ conj_tac \\ simp[]))
                >- (match_mp_tac REAL_LE_TRANS \\ qexists_tac `-(nR1 * nR2) + (nR1 + err1) * nR2` \\ conj_tac
                    >- (fs [REAL_NEG_RMUL] \\ once_rewrite_tac [REAL_MUL_COMM] \\
                        match_mp_tac REAL_LE_LMUL_IMP \\ conj_tac \\ fs[])
                    >- (`-(nR1 * nR2) + (nR1 + err1) * nR2 = nR2 * err1`
                          by (fs[real_sub,REAL_RDISTRIB]
                              \\ fs [GSYM REAL_SUB_LNEG, real_sub, REAL_LDISTRIB, REAL_NEG_MUL2, REAL_ADD_ASSOC]
                              \\ fs [GSYM real_sub, REAL_SUB_REFL, GSYM REAL_NEG_RMUL, REAL_MUL_COMM])
                              \\ simp[] \\ match_mp_tac REAL_LE_TRANS
                              \\ qexists_tac `maxAbs (e2lo, e2hi) * err1`
                              \\ conj_tac \\ simp[]
                              \\ match_mp_tac REAL_LE_TRANS
                              \\ qexists_tac `maxAbs (e1lo, e1hi) * err2 + maxAbs (e2lo, e2hi) * err1`
                              \\ conj_tac \\ simp[]
                              \\ once_rewrite_tac [REAL_ADD_COMM]
                              \\ simp[REAL_LE_ADDR]))))
         >- (match_mp_tac REAL_LE_TRANS \\ qexists_tac `-(nR1 * nR2) + nF1 * (nR2 + err2)` \\ conj_tac
            >- (fs [REAL_NEG_RMUL] \\ match_mp_tac REAL_LE_LMUL_IMP \\ conj_tac \\ simp[REAL_LE_NEG])
            >- (qspecl_then [`nR2 + err2`, `0`] DISJ_CASES_TAC REAL_LTE_TOTAL
                >- (match_mp_tac REAL_LE_TRANS \\ qexists_tac `-(nR1 * nR2) + nR1 * (nR2 + err2)` \\ conj_tac
                    >- (fs [REAL_NEG_RMUL] \\ once_rewrite_tac [REAL_MUL_COMM] \\
                        match_mp_tac REAL_MUL_LE_COMPAT_NEG_L \\ conj_tac
                       >- (fs[] \\ match_mp_tac REAL_LT_IMP_LE \\ simp[])
                       >- (qpat_x_assum `nR1 + - nF1 <= _ `
                             (fn thm =>
                                 assume_tac
                                   (SIMP_RULE bool_ss [GSYM real_sub, REAL_LE_SUB_RADD, REAL_ADD_LID] thm))
                           \\ simp[]))
                    >- (`-(nR1 * nR2) + nR1 * (nR2 + err2) = nR1 * err2`
                          by (fs[real_sub,REAL_RDISTRIB]
                              \\ fs [GSYM REAL_SUB_LNEG, real_sub, REAL_LDISTRIB, REAL_NEG_MUL2, REAL_ADD_ASSOC]
                              \\ fs [GSYM real_sub, REAL_SUB_REFL, GSYM REAL_NEG_RMUL, REAL_MUL_COMM])
                              \\ simp[] \\ match_mp_tac REAL_LE_TRANS
                              \\ qexists_tac `maxAbs (e1lo, e1hi) * err2` \\ conj_tac \\ simp[]
                              \\ match_mp_tac REAL_LE_TRANS
                              \\ qexists_tac `maxAbs (e1lo, e1hi) * err2 + maxAbs (e2lo, e2hi) * err1`
                              \\ conj_tac \\ simp[]))
                >- (match_mp_tac REAL_LE_TRANS
                    \\ qexists_tac `-(nR1 * nR2) + (nR1 + err1) * (nR2 + err2)`
                    \\ conj_tac
                    >- (fs [REAL_NEG_RMUL]
                        \\ once_rewrite_tac [REAL_MUL_COMM]
                        \\ match_mp_tac REAL_LE_LMUL_IMP
                        \\ conj_tac \\ fs[])
                    >- (`-(nR1 * nR2) + (nR1 + err1) * (nR2 + err2) = nR1 * err2 + nR2 * err1 + err1 * err2`
                          by (fs[real_sub,REAL_RDISTRIB]
                              \\ fs [GSYM REAL_SUB_LNEG, real_sub, REAL_LDISTRIB, REAL_NEG_MUL2, REAL_ADD_ASSOC]
                              \\ fs [GSYM real_sub, REAL_SUB_REFL, GSYM REAL_NEG_RMUL, REAL_MUL_COMM])
                        \\ simp[] \\ match_mp_tac REAL_LE_ADD2
                        \\ conj_tac \\ simp[GSYM REAL_NEG_LMUL]))))))));

974
val validErrorboundCorrectMult = store_thm ("validErrorboundCorrectMult",
975
  ``!(E1 E2:env) (absenv:analysisResult) (e1:real exp) (e2:real exp)
976 977 978 979 980 981 982
     (nR nR1 nR2 nF nF1 nF2:real) (e err1 err2:real) (alo ahi e1lo e1hi e2lo e2hi :real) dVars m m1 m2 expTypes Gamma.
       (m = join m1 m2) /\
       eval_exp E1 (toRMap Gamma) (toREval e1) nR1 M0 /\
       eval_exp E1 (toRMap Gamma) (toREval e2) nR2 M0 /\
       eval_exp E1 (toRMap Gamma) (toREval (Binop Mult e1 e2)) nR M0 /\
       eval_exp E2 Gamma e1 nF1 m1 /\
       eval_exp E2 Gamma e2 nF2 m2 /\
='s avatar
= committed
983
       eval_exp (updEnv 2 nF2 (updEnv 1 nF1 emptyEnv))
984
                (updDefVars 2 m2 (updDefVars 1 m1 Gamma))
='s avatar
= committed
985
                (Binop Mult (Var 1) (Var 2)) nF m /\
986 987
       typeCheck (Binop Mult e1 e2) Gamma expTypes /\
       validErrorbound (Binop Mult e1 e2) expTypes absenv dVars /\
988 989 990 991
       FST (FST (absenv e1)) <= nR1 /\
       nR1 <= SND (FST (absenv e1)) /\
       FST (FST (absenv e2)) <= nR2 /\
       nR2 <= SND (FST (absenv e2)) /\
992 993 994 995 996 997
       (absenv e1 = ((e1lo,e1hi),err1)) /\
       (absenv e2 = ((e2lo, e2hi),err2)) /\
       (absenv (Binop Mult e1 e2) = ((alo,ahi),e)) /\
       abs (nR1 - nF1) <= err1 /\
       abs (nR2 - nF2) <= err2 ==>
       abs (nR - nF) <= e``,
998 999
  once_rewrite_tac [validErrorbound_def]
  \\ rpt strip_tac \\ fs[]
1000 1001 1002
  \\ qpat_x_assum `absenv (Binop _ _ _) = _` (fn thm => fs [thm] \\ assume_tac thm)
  \\ qpat_x_assum `absenv e1 = _` (fn thm => fs [thm] \\ assume_tac thm)
  \\ qpat_x_assum `absenv e2 = _` (fn thm => fs [thm] \\ assume_tac thm)
='s avatar
= committed
1003
  \\ fs [Once typeCheck_def]
1004 1005 1006 1007 1008
  \\ Cases_on `expTypes (Binop Mult e1 e2)` \\ rveq \\ fs []
  \\ Cases_on `expTypes e1` \\ rveq \\ fs []
  \\ Cases_on `expTypes e2` \\ rveq \\ fs []
  \\ `expTypes e1 = SOME m1` by (match_mp_tac typingSoundnessExp \\ metis_tac [])
  \\ `expTypes e2 = SOME m2` by (match_mp_tac typingSoundnessExp \\ metis_tac [])
='s avatar
= committed
1009
  \\ fs [] \\ rveq
1010 1011
  \\ `0 <= err1`
       by (match_mp_tac err_always_positive
1012
           \\ qexistsl_tac [`e1`, `absenv`, `(e1lo,e1hi)`, `dVars`, `expTypes`] \\ fs[])
1013 1014
  \\ `0 <= err2`
       by (match_mp_tac err_always_positive
1015
           \\ qexistsl_tac [`e2`, `absenv`, `(e2lo,e2hi)`, `dVars`, `expTypes`] \\ fs[])
1016
  \\ match_mp_tac REAL_LE_TRANS
1017
  \\ qexists_tac `abs (nR1 * nR2 - nF1 * nF2) + abs (nF1 * nF2) * (mTypeToQ (join m1 m2))`
1018 1019
  \\ conj_tac
  >- (match_mp_tac mult_abs_err_bounded
1020
      \\ qexistsl_tac [`e1`, `e2`, `err1`, `err2`, `E1`, `E2`, `m1`, `m2`, `Gamma`]
1021 1022 1023
      \\ fs [])
  >- (match_mp_tac REAL_LE_TRANS
      \\ qexists_tac `maxAbs (e1lo,e1hi) * err2 + maxAbs (e2lo,e2hi) * err1 +
1024
                   err1 * err2 +
1025
                   maxAbs (multInterval (widenInterval (e1lo,e1hi) err1)
1026
                     (widenInterval (e2lo,e2hi) err2)) * (mTypeToQ (join m1 m2))`
1027
      \\ conj_tac \\ TRY(simp[])
1028
      \\ match_mp_tac REAL_LE_ADD2 \\ conj_tac
1029
      >- (match_mp_tac multiplicationErroBounded \\ fs[])
1030 1031
      >- (simp[maxAbs_def]
          \\ once_rewrite_tac [REAL_MUL_COMM] \\ match_mp_tac REAL_LE_LMUL_IMP
1032
          \\ conj_tac \\ simp[mTypeToQ_def,mTypeToQ_pos]
1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044
          \\ match_mp_tac maxAbs
          \\ `contained nF1 (widenInterval (e1lo,e1hi) err1)`
               by (match_mp_tac distance_gives_iv
                   \\ qexists_tac `nR1` \\ conj_tac \\ simp[contained_def, IVlo_def, IVhi_def])
          \\ `contained nF2 (widenInterval (e2lo,e2hi) err2)`
            by (match_mp_tac distance_gives_iv
                \\ qexists_tac `nR2` \\ conj_tac \\ simp[contained_def, IVlo_def, IVhi_def])
          \\ `contained (nF1 * nF2) (multInterval (widenInterval (e1lo, e1hi) err1) (widenInterval (e2lo, e2hi) err2))`
            by (match_mp_tac interval_multiplication_valid
                \\ conj_tac \\ simp[])
          \\ rule_assum_tac (fn thm => REWRITE_RULE [contained_def, IVlo_def, IVhi_def] thm)
          \\ simp[])));
1045

1046
val validErrorboundCorrectDiv = store_thm ("validErrorboundCorrectDiv",
Heiko Becker's avatar
Heiko Becker committed
1047
  ``!(E1 E2:env) (absenv:analysisResult) (e1:real exp) (e2:real exp)
1048 1049 1050 1051 1052 1053 1054
     (nR nR1 nR2 nF nF1 nF2:real) (e err1 err2:real) (alo ahi e1lo e1hi e2lo e2hi :real) dVars m m1 m2 expTypes Gamma.
       (m = join m1 m2) /\
       eval_exp E1 (toRMap Gamma) (toREval e1) nR1 M0 /\
       eval_exp E1 (toRMap Gamma) (toREval e2) nR2 M0 /\
       eval_exp E1 (toRMap Gamma) (toREval (Binop Div e1 e2)) nR M0 /\
       eval_exp E2 Gamma e1 nF1 m1 /\
       eval_exp E2 Gamma e2 nF2 m2 /\
='s avatar
= committed
1055
       eval_exp (updEnv 2 nF2 (updEnv 1 nF1 emptyEnv))
1056
                (updDefVars 2 m2 (updDefVars 1 m1 Gamma))
='s avatar
= committed
1057
                (Binop Div (Var 1) (Var 2)) nF m /\
1058 1059
       typeCheck (Binop Div e1 e2) Gamma expTypes /\
       validErrorbound (Binop Div e1 e2) expTypes absenv dVars /\
1060 1061 1062 1063 1064
       (e2hi < 0 \/ 0 < e2lo) /\
       FST (FST (absenv e1)) <= nR1 /\
       nR1 <= SND (FST (absenv e1)) /\
       FST (FST (absenv e2)) <= nR2 /\
       nR2 <= SND (FST (absenv e2)) /\
1065 1066 1067 1068 1069 1070
       (absenv e1 = ((e1lo,e1hi),err1)) /\
       (absenv e2 = ((e2lo, e2hi),err2)) /\
       (absenv (Binop Div e1 e2) = ((alo,ahi),e)) /\
       abs (nR1 - nF1) <= err1 /\
       abs (nR2 - nF2) <= err2 ==>
       abs (nR - nF) <= e``,
Heiko Becker's avatar
Heiko Becker committed
1071 1072 1073
  rewrite_tac [Once validErrorbound_def, GSYM noDivzero_def]
  \\ rpt (strip_tac)
  \\ fs[]
='s avatar
= committed
1074
  \\ fs [Once typeCheck_def]
1075 1076 1077 1078 1079
  \\ Cases_on `expTypes (Binop Div e1 e2)` \\ rveq \\ fs []
  \\ Cases_on `expTypes e1` \\ rveq \\ fs []
  \\ Cases_on `expTypes e2` \\ rveq \\ fs []
  \\ `expTypes e1 = SOME m1` by (match_mp_tac typingSoundnessExp \\ metis_tac [])
  \\ `expTypes e2 = SOME m2` by (match_mp_tac typingSoundnessExp \\ metis_tac [])
='s avatar
= committed
1080
  \\ fs [] \\ rveq
Heiko Becker's avatar
Heiko Becker committed
1081 1082
  \\ `0 <= err1`
       by (match_mp_tac err_always_positive
1083
           \\ qexistsl_tac [`e1`, `absenv`, `(e1lo,e1hi)`, `dVars`, `expTypes`] \\ fs[])
Heiko Becker's avatar
Heiko Becker committed
1084 1085
  \\ `0 <= err2`
       by (match_mp_tac err_always_positive
1086
           \\ qexistsl_tac [`e2`, `absenv`, `(e2lo,e2hi)`, `dVars`, `expTypes`] \\ fs[])
Heiko Becker's avatar
Heiko Becker committed
1087
  \\ match_mp_tac REAL_LE_TRANS
1088
  \\ qexists_tac `abs (nR1 / nR2 - nF1 / nF2) + abs (nF1 / nF2) * (mTypeToQ (join m1 m2))`
Heiko Becker's avatar
Heiko Becker committed
1089 1090
  \\ conj_tac
  >- (match_mp_tac div_abs_err_bounded
1091
      \\ qexistsl_tac [`e1`, `e2`, `err1`, `err2`, `E1`, `E2`, `m1`, `m2`, `Gamma`]
Heiko Becker's avatar
Heiko Becker committed
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
      \\ fs [])
  >- (match_mp_tac REAL_LE_TRANS
      \\ once_rewrite_tac [CONJ_SYM]
      \\ asm_exists_tac
      \\ once_rewrite_tac [CONJ_SYM]
      \\ conj_tac \\ rw[]
      \\ `contained nF1 (widenInterval (e1lo,e1hi) err1)`
           by (match_mp_tac distance_gives_iv
               \\ qexists_tac `nR1` \\ conj_tac
               \\ simp[contained_def, IVlo_def, IVhi_def])
      \\ `contained nF2 (widenInterval (e2lo,e2hi) err2)`
            by (match_mp_tac distance_gives_iv
                \\ qexists_tac `nR2` \\ conj_tac
                \\ simp[contained_def, IVlo_def, IVhi_def])
      \\ match_mp_tac REAL_LE_ADD2 \\ conj_tac
      >- (rpt (qpat_x_assum `eval_exp _ _ _ _ _ _` kall_tac)
          \\ `contained (inv nR2) (invertInterval (e2lo, e2hi))`
                by (match_mp_tac interval_inversion_valid \\ conj_tac
                    \\ fs[contained_def, IVlo_def, IVhi_def, noDivzero_def])
          \\ `contained (inv nF2) (invertInterval (widenInterval (e2lo, e2hi) err2))`
                by (match_mp_tac interval_inversion_valid \\ conj_tac
                    \\ fs[contained_def, IVlo_def, IVhi_def, noDivzero_def])
          \\ `nR1 <= maxAbs (e1lo, e1hi)`
                by (match_mp_tac contained_leq_maxAbs_val
                    \\ fs[contained_def, IVlo_def, IVhi_def])
          \\ `inv nR2 <= maxAbs (invertInterval(e2lo, e2hi))`
                by (match_mp_tac contained_leq_maxAbs_val
                    \\ fs[contained_def, IVlo_def, IVhi_def])
          \\ `-nR1 <= maxAbs (e1lo, e1hi)`
                by (match_mp_tac contained_leq_maxAbs_neg_val
                    \\ fs[contained_def, IVlo_def, IVhi_def])
          \\ `- inv nR2 <= maxAbs (invertInterval (e2lo, e2hi))`
                by (match_mp_tac contained_leq_maxAbs_neg_val
                    \\ fs[contained_def, IVlo_def, IVhi_def])
          \\ `nR1 * err2 <= maxAbs (e1lo, e1hi) * err2`
                by (match_mp_tac REAL_LE_RMUL_IMP \\ fs[])
          \\ `-nR1 * err2 <= maxAbs (e1lo, e1hi) * err2`
                by (match_mp_tac REAL_LE_RMUL_IMP \\ fs[])
          \\ `inv nR2 * err1 <= maxAbs (invertInterval(e2lo, e2hi)) * err1`
                by (match_mp_tac REAL_LE_RMUL_IMP \\ fs[])
          \\ `- inv nR2 * err1 <= maxAbs (invertInterval(e2lo, e2hi)) * err1`
                by (match_mp_tac REAL_LE_RMUL_IMP \\ fs[])
          \\ `- (err1 * err2) <= err1 * err2`
                by (fs[REAL_NEG_LMUL] \\ match_mp_tac REAL_LE_RMUL_IMP
                    \\ REAL_ASM_ARITH_TAC)
          \\ `0 <= maxAbs (e1lo, e1hi) * err2` by REAL_ASM_ARITH_TAC
          \\ `0 <= maxAbs (invertInterval (e2lo, e2hi)) * err1` by REAL_ASM_ARITH_TAC
          \\ `maxAbs (e1lo, e1hi) * err2 <= maxAbs (e1lo, e1hi) * err2 + maxAbs (invertInterval (e2lo, e2hi)) * err1`
                by (REAL_ASM_ARITH_TAC)
          \\ `maxAbs (e1lo, e1hi) * err2 + maxAbs (invertInterval (e2lo, e2hi)) * err1 <=
1142
                 maxAbs (e1lo, e1hi) * err2 + maxAbs (invertInterval (e2lo, e2hi)) * err1 + err1 * err2`
Heiko Becker's avatar
Heiko Becker committed
1143
                   by REAL_ASM_ARITH_TAC
1144 1145
               (* Case distinction for divisor range
				  positive or negative in float and real valued execution *)
Heiko Becker's avatar
Heiko Becker committed
1146 1147 1148 1149
          \\ rpt (qpat_x_assum `validErrorbound _ _ ` kall_tac)
          \\ rpt (qpat_x_assum `absenv _ = _` kall_tac)
          \\ rpt (qpat_x_assum `isSupersetInterval _ _` kall_tac)
          \\ rpt (qpat_x_assum `maxAbs (e1lo,e1hi) *
1150 1151 1152
                                  (1 /
                                  (minAbsFun (widenInterval (e2lo,e2hi) err2) *
                                   minAbsFun (widenInterval (e2lo,e2hi) err2)) * err2) +
1153
                                  maxAbs (invertInterval (e2lo,e2hi)) * err1 +
1154 1155 1156 1157
                                  err1 *
                                  (1 /
                                   (minAbsFun (widenInterval (e2lo,e2hi) err2) *
                                    minAbsFun (widenInterval (e2lo,e2hi) err2)) * err2) +
1158
                                  maxAbs
1159
                                      (divideInterval (widenInterval (e1lo,e1hi) err1)
1160
                                                      (widenInterval (e2lo,e2hi) err2)) * machineEpsilon  <= e` kall_tac)
Heiko Becker's avatar
Heiko Becker committed
1161
          \\ fs [IVlo_def, IVhi_def, widenInterval_def, contained_def, noDivzero_def]
1162
     		   (* The range of the divisor lies in the range from -infinity until 0 *)
Heiko Becker's avatar
Heiko Becker committed
1163 1164
          >- (`abs (inv nR2 - inv nF2) <= err2 * inv ((e2hi + err2) * (e2hi + err2))`
                 by (match_mp_tac err_prop_inversion_neg \\ qexists_tac `e2lo` \\simp[])
Heiko Becker's avatar
Heiko Becker committed
1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175
              \\ fs [widenInterval_def, IVlo_def, IVhi_def]
              \\ `minAbsFun (e2lo - err2, e2hi + err2) = - (e2hi + err2)`
                    by (match_mp_tac minAbs_negative_iv_is_hi \\ REAL_ASM_ARITH_TAC)
              \\ simp[]
	      \\ qpat_x_assum `minAbsFun _ = _ ` kall_tac
              \\ `nF1 <= err1 + nR1` by REAL_ASM_ARITH_TAC
              \\ `nR1 - err1 <= nF1` by REAL_ASM_ARITH_TAC
              \\ `(nR2 - nF2 > 0 /\ nR2 - nF2 <= err2) \/ (nR2 - nF2 <= 0 /\ - (nR2 - nF2) <= err2)`
                    by REAL_ASM_ARITH_TAC
              (* Positive case for abs (nR2 - nF2) <= err2 *)
              >- (`nF2 < nR2` by REAL_ASM_ARITH_TAC
1176
                  \\ qpat_x_assum `nF2 < nR2` (fn thm => assume_tac (ONCE_REWRITE_RULE [GSYM REAL_LT_NEG] thm))
Heiko Becker's avatar
Heiko Becker committed
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
                  \\ `inv (- nF2) < inv (- nR2)` by (match_mp_tac REAL_LT_INV \\ REAL_ASM_ARITH_TAC)
                  \\ `inv (- nF2) = - (inv nF2)` by (match_mp_tac (GSYM REAL_NEG_INV) \\ REAL_ASM_ARITH_TAC)
                  \\ `inv (- nR2) = - (inv nR2)` by (match_mp_tac (GSYM REAL_NEG_INV) \\ REAL_ASM_ARITH_TAC)
		  \\ rpt (
                       qpat_x_assum `inv (- _) = - (inv _)`
                         (fn thm => rule_assum_tac (fn hyp => REWRITE_RULE [thm] hyp)))
                  \\ `inv nR2 < inv nF2` by REAL_ASM_ARITH_TAC
		  \\ qpat_x_assum `- _ < - _` kall_tac
                  \\ `inv nR2 - inv nF2 < 0` by REAL_ASM_ARITH_TAC
                  \\ `- (nR2⁻¹  nF2⁻¹)  err2 * ((e2hi + err2) * (e2hi + err2))⁻¹` by REAL_ASM_ARITH_TAC
                  \\ `inv nF2 <= inv nR2 + err2 * inv ((e2hi + err2) * (e2hi + err2))` by REAL_ASM_ARITH_TAC
                  \\ `inv nR2 - err2 * inv ((e2hi + err2) * (e2hi + err2)) <= inv nF2` by REAL_ASM_ARITH_TAC
                  (* Next do a case distinction for the absolute value *)
                  \\ `! (x:real). ((abs x = x) /\ 0 <= x) \/ ((abs x = - x) /\ x < 0)` by REAL_ASM_ARITH_TAC
		  \\ qpat_x_assum `!x. A /\ B \/ C`
                       (fn thm => qspec_then `(nR1:real / nR2:real) - (nF1:real /