src/HOL/Real/Float.thy
author nipkow
Tue Oct 23 23:27:23 2007 +0200 (2007-10-23)
changeset 25162 ad4d5365d9d8
parent 24653 3d3ebc0c927c
child 26076 b9c716a9fb5f
permissions -rw-r--r--
went back to >0
obua@16782
     1
(*  Title: HOL/Real/Float.thy
obua@16782
     2
    ID:    $Id$
obua@16782
     3
    Author: Steven Obua
obua@16782
     4
*)
obua@16782
     5
huffman@20717
     6
header {* Floating Point Representation of the Reals *}
huffman@20717
     7
haftmann@20485
     8
theory Float
wenzelm@21256
     9
imports Real Parity
haftmann@23251
    10
uses "~~/src/Tools/float.ML" ("float_arith.ML")
haftmann@20485
    11
begin
obua@16782
    12
wenzelm@19765
    13
definition
wenzelm@21404
    14
  pow2 :: "int \<Rightarrow> real" where
wenzelm@19765
    15
  "pow2 a = (if (0 <= a) then (2^(nat a)) else (inverse (2^(nat (-a)))))"
wenzelm@21404
    16
wenzelm@21404
    17
definition
wenzelm@21404
    18
  float :: "int * int \<Rightarrow> real" where
wenzelm@19765
    19
  "float x = real (fst x) * pow2 (snd x)"
obua@16782
    20
obua@16782
    21
lemma pow2_0[simp]: "pow2 0 = 1"
obua@16782
    22
by (simp add: pow2_def)
obua@16782
    23
obua@16782
    24
lemma pow2_1[simp]: "pow2 1 = 2"
obua@16782
    25
by (simp add: pow2_def)
obua@16782
    26
obua@16782
    27
lemma pow2_neg: "pow2 x = inverse (pow2 (-x))"
obua@16782
    28
by (simp add: pow2_def)
obua@16782
    29
wenzelm@19765
    30
lemma pow2_add1: "pow2 (1 + a) = 2 * (pow2 a)"
obua@16782
    31
proof -
obua@16782
    32
  have h: "! n. nat (2 + int n) - Suc 0 = nat (1 + int n)" by arith
obua@16782
    33
  have g: "! a b. a - -1 = a + (1::int)" by arith
obua@16782
    34
  have pos: "! n. pow2 (int n + 1) = 2 * pow2 (int n)"
obua@16782
    35
    apply (auto, induct_tac n)
obua@16782
    36
    apply (simp_all add: pow2_def)
obua@16782
    37
    apply (rule_tac m1="2" and n1="nat (2 + int na)" in ssubst[OF realpow_num_eq_if])
huffman@23431
    38
    by (auto simp add: h)
obua@16782
    39
  show ?thesis
obua@16782
    40
  proof (induct a)
obua@16782
    41
    case (1 n)
nipkow@23477
    42
    from pos show ?case by (simp add: ring_simps)
obua@16782
    43
  next
obua@16782
    44
    case (2 n)
obua@16782
    45
    show ?case
obua@16782
    46
      apply (auto)
obua@16782
    47
      apply (subst pow2_neg[of "- int n"])
huffman@23431
    48
      apply (subst pow2_neg[of "-1 - int n"])
obua@16782
    49
      apply (auto simp add: g pos)
obua@16782
    50
      done
wenzelm@19765
    51
  qed
obua@16782
    52
qed
wenzelm@19765
    53
obua@16782
    54
lemma pow2_add: "pow2 (a+b) = (pow2 a) * (pow2 b)"
obua@16782
    55
proof (induct b)
wenzelm@19765
    56
  case (1 n)
obua@16782
    57
  show ?case
obua@16782
    58
  proof (induct n)
obua@16782
    59
    case 0
obua@16782
    60
    show ?case by simp
obua@16782
    61
  next
obua@16782
    62
    case (Suc m)
nipkow@23477
    63
    show ?case by (auto simp add: ring_simps pow2_add1 prems)
obua@16782
    64
  qed
obua@16782
    65
next
obua@16782
    66
  case (2 n)
wenzelm@19765
    67
  show ?case
obua@16782
    68
  proof (induct n)
obua@16782
    69
    case 0
wenzelm@19765
    70
    show ?case
obua@16782
    71
      apply (auto)
obua@16782
    72
      apply (subst pow2_neg[of "a + -1"])
obua@16782
    73
      apply (subst pow2_neg[of "-1"])
obua@16782
    74
      apply (simp)
obua@16782
    75
      apply (insert pow2_add1[of "-a"])
nipkow@23477
    76
      apply (simp add: ring_simps)
obua@16782
    77
      apply (subst pow2_neg[of "-a"])
obua@16782
    78
      apply (simp)
obua@16782
    79
      done
obua@16782
    80
    case (Suc m)
wenzelm@19765
    81
    have a: "int m - (a + -2) =  1 + (int m - a + 1)" by arith
obua@16782
    82
    have b: "int m - -2 = 1 + (int m + 1)" by arith
obua@16782
    83
    show ?case
obua@16782
    84
      apply (auto)
obua@16782
    85
      apply (subst pow2_neg[of "a + (-2 - int m)"])
obua@16782
    86
      apply (subst pow2_neg[of "-2 - int m"])
nipkow@23477
    87
      apply (auto simp add: ring_simps)
obua@16782
    88
      apply (subst a)
obua@16782
    89
      apply (subst b)
obua@16782
    90
      apply (simp only: pow2_add1)
obua@16782
    91
      apply (subst pow2_neg[of "int m - a + 1"])
obua@16782
    92
      apply (subst pow2_neg[of "int m + 1"])
obua@16782
    93
      apply auto
obua@16782
    94
      apply (insert prems)
nipkow@23477
    95
      apply (auto simp add: ring_simps)
obua@16782
    96
      done
obua@16782
    97
  qed
obua@16782
    98
qed
obua@16782
    99
wenzelm@19765
   100
lemma "float (a, e) + float (b, e) = float (a + b, e)"
nipkow@23477
   101
by (simp add: float_def ring_simps)
obua@16782
   102
wenzelm@19765
   103
definition
wenzelm@21404
   104
  int_of_real :: "real \<Rightarrow> int" where
wenzelm@19765
   105
  "int_of_real x = (SOME y. real y = x)"
wenzelm@21404
   106
wenzelm@21404
   107
definition
wenzelm@21404
   108
  real_is_int :: "real \<Rightarrow> bool" where
wenzelm@19765
   109
  "real_is_int x = (EX (u::int). x = real u)"
obua@16782
   110
obua@16782
   111
lemma real_is_int_def2: "real_is_int x = (x = real (int_of_real x))"
obua@16782
   112
by (auto simp add: real_is_int_def int_of_real_def)
obua@16782
   113
obua@16782
   114
lemma float_transfer: "real_is_int ((real a)*(pow2 c)) \<Longrightarrow> float (a, b) = float (int_of_real ((real a)*(pow2 c)), b - c)"
obua@16782
   115
by (simp add: float_def real_is_int_def2 pow2_add[symmetric])
obua@16782
   116
obua@16782
   117
lemma pow2_int: "pow2 (int c) = (2::real)^c"
obua@16782
   118
by (simp add: pow2_def)
obua@16782
   119
wenzelm@19765
   120
lemma float_transfer_nat: "float (a, b) = float (a * 2^c, b - int c)"
obua@16782
   121
by (simp add: float_def pow2_int[symmetric] pow2_add[symmetric])
obua@16782
   122
obua@16782
   123
lemma real_is_int_real[simp]: "real_is_int (real (x::int))"
obua@16782
   124
by (auto simp add: real_is_int_def int_of_real_def)
obua@16782
   125
obua@16782
   126
lemma int_of_real_real[simp]: "int_of_real (real x) = x"
obua@16782
   127
by (simp add: int_of_real_def)
obua@16782
   128
obua@16782
   129
lemma real_int_of_real[simp]: "real_is_int x \<Longrightarrow> real (int_of_real x) = x"
obua@16782
   130
by (auto simp add: int_of_real_def real_is_int_def)
obua@16782
   131
obua@16782
   132
lemma real_is_int_add_int_of_real: "real_is_int a \<Longrightarrow> real_is_int b \<Longrightarrow> (int_of_real (a+b)) = (int_of_real a) + (int_of_real b)"
obua@16782
   133
by (auto simp add: int_of_real_def real_is_int_def)
obua@16782
   134
obua@16782
   135
lemma real_is_int_add[simp]: "real_is_int a \<Longrightarrow> real_is_int b \<Longrightarrow> real_is_int (a+b)"
obua@16782
   136
apply (subst real_is_int_def2)
obua@16782
   137
apply (simp add: real_is_int_add_int_of_real real_int_of_real)
obua@16782
   138
done
obua@16782
   139
obua@16782
   140
lemma int_of_real_sub: "real_is_int a \<Longrightarrow> real_is_int b \<Longrightarrow> (int_of_real (a-b)) = (int_of_real a) - (int_of_real b)"
obua@16782
   141
by (auto simp add: int_of_real_def real_is_int_def)
obua@16782
   142
obua@16782
   143
lemma real_is_int_sub[simp]: "real_is_int a \<Longrightarrow> real_is_int b \<Longrightarrow> real_is_int (a-b)"
obua@16782
   144
apply (subst real_is_int_def2)
obua@16782
   145
apply (simp add: int_of_real_sub real_int_of_real)
obua@16782
   146
done
obua@16782
   147
obua@16782
   148
lemma real_is_int_rep: "real_is_int x \<Longrightarrow> ?! (a::int). real a = x"
obua@16782
   149
by (auto simp add: real_is_int_def)
obua@16782
   150
wenzelm@19765
   151
lemma int_of_real_mult:
obua@16782
   152
  assumes "real_is_int a" "real_is_int b"
obua@16782
   153
  shows "(int_of_real (a*b)) = (int_of_real a) * (int_of_real b)"
obua@16782
   154
proof -
obua@16782
   155
  from prems have a: "?! (a'::int). real a' = a" by (rule_tac real_is_int_rep, auto)
obua@16782
   156
  from prems have b: "?! (b'::int). real b' = b" by (rule_tac real_is_int_rep, auto)
obua@16782
   157
  from a obtain a'::int where a':"a = real a'" by auto
obua@16782
   158
  from b obtain b'::int where b':"b = real b'" by auto
obua@16782
   159
  have r: "real a' * real b' = real (a' * b')" by auto
obua@16782
   160
  show ?thesis
obua@16782
   161
    apply (simp add: a' b')
obua@16782
   162
    apply (subst r)
obua@16782
   163
    apply (simp only: int_of_real_real)
obua@16782
   164
    done
obua@16782
   165
qed
obua@16782
   166
obua@16782
   167
lemma real_is_int_mult[simp]: "real_is_int a \<Longrightarrow> real_is_int b \<Longrightarrow> real_is_int (a*b)"
obua@16782
   168
apply (subst real_is_int_def2)
obua@16782
   169
apply (simp add: int_of_real_mult)
obua@16782
   170
done
obua@16782
   171
obua@16782
   172
lemma real_is_int_0[simp]: "real_is_int (0::real)"
obua@16782
   173
by (simp add: real_is_int_def int_of_real_def)
obua@16782
   174
obua@16782
   175
lemma real_is_int_1[simp]: "real_is_int (1::real)"
obua@16782
   176
proof -
obua@16782
   177
  have "real_is_int (1::real) = real_is_int(real (1::int))" by auto
obua@16782
   178
  also have "\<dots> = True" by (simp only: real_is_int_real)
obua@16782
   179
  ultimately show ?thesis by auto
obua@16782
   180
qed
obua@16782
   181
obua@16782
   182
lemma real_is_int_n1: "real_is_int (-1::real)"
obua@16782
   183
proof -
obua@16782
   184
  have "real_is_int (-1::real) = real_is_int(real (-1::int))" by auto
obua@16782
   185
  also have "\<dots> = True" by (simp only: real_is_int_real)
obua@16782
   186
  ultimately show ?thesis by auto
obua@16782
   187
qed
obua@16782
   188
haftmann@20485
   189
lemma real_is_int_number_of[simp]: "real_is_int ((number_of \<Colon> int \<Rightarrow> real) x)"
obua@16782
   190
proof -
obua@16782
   191
  have neg1: "real_is_int (-1::real)"
obua@16782
   192
  proof -
obua@16782
   193
    have "real_is_int (-1::real) = real_is_int(real (-1::int))" by auto
obua@16782
   194
    also have "\<dots> = True" by (simp only: real_is_int_real)
obua@16782
   195
    ultimately show ?thesis by auto
obua@16782
   196
  qed
wenzelm@19765
   197
wenzelm@19765
   198
  {
haftmann@20485
   199
    fix x :: int
haftmann@20485
   200
    have "real_is_int ((number_of \<Colon> int \<Rightarrow> real) x)"
haftmann@20485
   201
      unfolding number_of_eq
obua@16782
   202
      apply (induct x)
obua@16782
   203
      apply (induct_tac n)
obua@16782
   204
      apply (simp)
obua@16782
   205
      apply (simp)
obua@16782
   206
      apply (induct_tac n)
obua@16782
   207
      apply (simp add: neg1)
obua@16782
   208
    proof -
obua@16782
   209
      fix n :: nat
obua@16782
   210
      assume rn: "(real_is_int (of_int (- (int (Suc n)))))"
obua@16782
   211
      have s: "-(int (Suc (Suc n))) = -1 + - (int (Suc n))" by simp
obua@16782
   212
      show "real_is_int (of_int (- (int (Suc (Suc n)))))"
wenzelm@19765
   213
        apply (simp only: s of_int_add)
wenzelm@19765
   214
        apply (rule real_is_int_add)
wenzelm@19765
   215
        apply (simp add: neg1)
wenzelm@19765
   216
        apply (simp only: rn)
wenzelm@19765
   217
        done
obua@16782
   218
    qed
obua@16782
   219
  }
obua@16782
   220
  note Abs_Bin = this
obua@16782
   221
  {
haftmann@20485
   222
    fix x :: int
haftmann@20485
   223
    have "? u. x = u"
haftmann@20485
   224
      apply (rule exI[where x = "x"])
haftmann@20485
   225
      apply (simp)
obua@16782
   226
      done
obua@16782
   227
  }
haftmann@20485
   228
  then obtain u::int where "x = u" by auto
obua@16782
   229
  with Abs_Bin show ?thesis by auto
obua@16782
   230
qed
obua@16782
   231
obua@16782
   232
lemma int_of_real_0[simp]: "int_of_real (0::real) = (0::int)"
obua@16782
   233
by (simp add: int_of_real_def)
obua@16782
   234
obua@16782
   235
lemma int_of_real_1[simp]: "int_of_real (1::real) = (1::int)"
wenzelm@19765
   236
proof -
obua@16782
   237
  have 1: "(1::real) = real (1::int)" by auto
obua@16782
   238
  show ?thesis by (simp only: 1 int_of_real_real)
obua@16782
   239
qed
obua@16782
   240
obua@16782
   241
lemma int_of_real_number_of[simp]: "int_of_real (number_of b) = number_of b"
obua@16782
   242
proof -
obua@16782
   243
  have "real_is_int (number_of b)" by simp
obua@16782
   244
  then have uu: "?! u::int. number_of b = real u" by (auto simp add: real_is_int_rep)
obua@16782
   245
  then obtain u::int where u:"number_of b = real u" by auto
wenzelm@19765
   246
  have "number_of b = real ((number_of b)::int)"
obua@16782
   247
    by (simp add: number_of_eq real_of_int_def)
wenzelm@19765
   248
  have ub: "number_of b = real ((number_of b)::int)"
obua@16782
   249
    by (simp add: number_of_eq real_of_int_def)
obua@16782
   250
  from uu u ub have unb: "u = number_of b"
obua@16782
   251
    by blast
obua@16782
   252
  have "int_of_real (number_of b) = u" by (simp add: u)
obua@16782
   253
  with unb show ?thesis by simp
obua@16782
   254
qed
obua@16782
   255
obua@16782
   256
lemma float_transfer_even: "even a \<Longrightarrow> float (a, b) = float (a div 2, b+1)"
obua@16782
   257
  apply (subst float_transfer[where a="a" and b="b" and c="-1", simplified])
nipkow@23477
   258
  apply (simp_all add: pow2_def even_def real_is_int_def ring_simps)
obua@16782
   259
  apply (auto)
obua@16782
   260
proof -
obua@16782
   261
  fix q::int
obua@16782
   262
  have a:"b - (-1\<Colon>int) = (1\<Colon>int) + b" by arith
wenzelm@19765
   263
  show "(float (q, (b - (-1\<Colon>int)))) = (float (q, ((1\<Colon>int) + b)))"
obua@16782
   264
    by (simp add: a)
obua@16782
   265
qed
wenzelm@19765
   266
obua@16782
   267
consts
obua@16782
   268
  norm_float :: "int*int \<Rightarrow> int*int"
obua@16782
   269
obua@16782
   270
lemma int_div_zdiv: "int (a div b) = (int a) div (int b)"
huffman@23431
   271
by (rule zdiv_int)
obua@16782
   272
obua@16782
   273
lemma int_mod_zmod: "int (a mod b) = (int a) mod (int b)"
huffman@23431
   274
by (rule zmod_int)
obua@16782
   275
obua@16782
   276
lemma abs_div_2_less: "a \<noteq> 0 \<Longrightarrow> a \<noteq> -1 \<Longrightarrow> abs((a::int) div 2) < abs a"
obua@16782
   277
by arith
obua@16782
   278
obua@16782
   279
lemma terminating_norm_float: "\<forall>a. (a::int) \<noteq> 0 \<and> even a \<longrightarrow> a \<noteq> 0 \<and> \<bar>a div 2\<bar> < \<bar>a\<bar>"
obua@16782
   280
apply (auto)
obua@16782
   281
apply (rule abs_div_2_less)
obua@16782
   282
apply (auto)
obua@16782
   283
done
obua@16782
   284
wenzelm@24124
   285
declare [[simp_depth_limit = 2]]
obua@16782
   286
recdef norm_float "measure (% (a,b). nat (abs a))"
obua@16782
   287
  "norm_float (a,b) = (if (a \<noteq> 0) & (even a) then norm_float (a div 2, b+1) else (if a=0 then (0,0) else (a,b)))"
chaieb@23315
   288
(hints simp: even_def terminating_norm_float)
wenzelm@24124
   289
declare [[simp_depth_limit = 100]]
obua@16782
   290
obua@16782
   291
lemma norm_float: "float x = float (norm_float x)"
obua@16782
   292
proof -
obua@16782
   293
  {
wenzelm@19765
   294
    fix a b :: int
wenzelm@19765
   295
    have norm_float_pair: "float (a,b) = float (norm_float (a,b))"
obua@16782
   296
    proof (induct a b rule: norm_float.induct)
obua@16782
   297
      case (1 u v)
wenzelm@19765
   298
      show ?case
obua@16782
   299
      proof cases
wenzelm@19765
   300
        assume u: "u \<noteq> 0 \<and> even u"
wenzelm@19765
   301
        with prems have ind: "float (u div 2, v + 1) = float (norm_float (u div 2, v + 1))" by auto
wenzelm@19765
   302
        with u have "float (u,v) = float (u div 2, v+1)" by (simp add: float_transfer_even)
wenzelm@19765
   303
        then show ?thesis
wenzelm@19765
   304
          apply (subst norm_float.simps)
wenzelm@19765
   305
          apply (simp add: ind)
wenzelm@19765
   306
          done
obua@16782
   307
      next
wenzelm@19765
   308
        assume "~(u \<noteq> 0 \<and> even u)"
wenzelm@19765
   309
        then show ?thesis
wenzelm@19765
   310
          by (simp add: prems float_def)
obua@16782
   311
      qed
obua@16782
   312
    qed
obua@16782
   313
  }
obua@16782
   314
  note helper = this
obua@16782
   315
  have "? a b. x = (a,b)" by auto
obua@16782
   316
  then obtain a b where "x = (a, b)" by blast
obua@16782
   317
  then show ?thesis by (simp only: helper)
obua@16782
   318
qed
obua@16782
   319
obua@16782
   320
lemma pow2_int: "pow2 (int n) = 2^n"
obua@16782
   321
  by (simp add: pow2_def)
obua@16782
   322
obua@24301
   323
lemma float_add_l0: "float (0, e) + x = x"
obua@24301
   324
  by (simp add: float_def)
obua@24301
   325
obua@24301
   326
lemma float_add_r0: "x + float (0, e) = x"
obua@24301
   327
  by (simp add: float_def)
obua@24301
   328
wenzelm@19765
   329
lemma float_add:
wenzelm@19765
   330
  "float (a1, e1) + float (a2, e2) =
wenzelm@19765
   331
  (if e1<=e2 then float (a1+a2*2^(nat(e2-e1)), e1)
obua@16782
   332
  else float (a1*2^(nat (e1-e2))+a2, e2))"
nipkow@23477
   333
  apply (simp add: float_def ring_simps)
obua@16782
   334
  apply (auto simp add: pow2_int[symmetric] pow2_add[symmetric])
obua@16782
   335
  done
obua@16782
   336
obua@24301
   337
lemma float_add_assoc1:
obua@24301
   338
  "(x + float (y1, e1)) + float (y2, e2) = (float (y1, e1) + float (y2, e2)) + x"
obua@24301
   339
  by simp
obua@24301
   340
obua@24301
   341
lemma float_add_assoc2:
obua@24301
   342
  "(float (y1, e1) + x) + float (y2, e2) = (float (y1, e1) + float (y2, e2)) + x"
obua@24301
   343
  by simp
obua@24301
   344
obua@24301
   345
lemma float_add_assoc3:
obua@24301
   346
  "float (y1, e1) + (x + float (y2, e2)) = (float (y1, e1) + float (y2, e2)) + x"
obua@24301
   347
  by simp
obua@24301
   348
obua@24301
   349
lemma float_add_assoc4:
obua@24301
   350
  "float (y1, e1) + (float (y2, e2) + x) = (float (y1, e1) + float (y2, e2)) + x"
obua@24301
   351
  by simp
obua@24301
   352
obua@24301
   353
lemma float_mult_l0: "float (0, e) * x = float (0, 0)"
obua@24301
   354
  by (simp add: float_def)
obua@24301
   355
obua@24301
   356
lemma float_mult_r0: "x * float (0, e) = float (0, 0)"
obua@24301
   357
  by (simp add: float_def)
obua@24301
   358
obua@24301
   359
definition 
obua@24301
   360
  lbound :: "real \<Rightarrow> real"
obua@24301
   361
where
obua@24301
   362
  "lbound x = min 0 x"
obua@24301
   363
obua@24301
   364
definition
obua@24301
   365
  ubound :: "real \<Rightarrow> real"
obua@24301
   366
where
obua@24301
   367
  "ubound x = max 0 x"
obua@24301
   368
obua@24301
   369
lemma lbound: "lbound x \<le> x"   
obua@24301
   370
  by (simp add: lbound_def)
obua@24301
   371
obua@24301
   372
lemma ubound: "x \<le> ubound x"
obua@24301
   373
  by (simp add: ubound_def)
obua@24301
   374
obua@16782
   375
lemma float_mult:
wenzelm@19765
   376
  "float (a1, e1) * float (a2, e2) =
obua@16782
   377
  (float (a1 * a2, e1 + e2))"
obua@16782
   378
  by (simp add: float_def pow2_add)
obua@16782
   379
obua@16782
   380
lemma float_minus:
obua@16782
   381
  "- (float (a,b)) = float (-a, b)"
obua@16782
   382
  by (simp add: float_def)
obua@16782
   383
obua@16782
   384
lemma zero_less_pow2:
obua@16782
   385
  "0 < pow2 x"
obua@16782
   386
proof -
obua@16782
   387
  {
obua@16782
   388
    fix y
wenzelm@19765
   389
    have "0 <= y \<Longrightarrow> 0 < pow2 y"
obua@16782
   390
      by (induct y, induct_tac n, simp_all add: pow2_add)
obua@16782
   391
  }
obua@16782
   392
  note helper=this
obua@16782
   393
  show ?thesis
obua@16782
   394
    apply (case_tac "0 <= x")
obua@16782
   395
    apply (simp add: helper)
obua@16782
   396
    apply (subst pow2_neg)
obua@16782
   397
    apply (simp add: helper)
obua@16782
   398
    done
obua@16782
   399
qed
obua@16782
   400
obua@16782
   401
lemma zero_le_float:
obua@16782
   402
  "(0 <= float (a,b)) = (0 <= a)"
obua@16782
   403
  apply (auto simp add: float_def)
wenzelm@19765
   404
  apply (auto simp add: zero_le_mult_iff zero_less_pow2)
obua@16782
   405
  apply (insert zero_less_pow2[of b])
obua@16782
   406
  apply (simp_all)
obua@16782
   407
  done
obua@16782
   408
obua@16782
   409
lemma float_le_zero:
obua@16782
   410
  "(float (a,b) <= 0) = (a <= 0)"
obua@16782
   411
  apply (auto simp add: float_def)
obua@16782
   412
  apply (auto simp add: mult_le_0_iff)
obua@16782
   413
  apply (insert zero_less_pow2[of b])
obua@16782
   414
  apply auto
obua@16782
   415
  done
obua@16782
   416
obua@16782
   417
lemma float_abs:
obua@16782
   418
  "abs (float (a,b)) = (if 0 <= a then (float (a,b)) else (float (-a,b)))"
obua@16782
   419
  apply (auto simp add: abs_if)
obua@16782
   420
  apply (simp_all add: zero_le_float[symmetric, of a b] float_minus)
obua@16782
   421
  done
obua@16782
   422
obua@16782
   423
lemma float_zero:
obua@16782
   424
  "float (0, b) = 0"
obua@16782
   425
  by (simp add: float_def)
obua@16782
   426
obua@16782
   427
lemma float_pprt:
obua@16782
   428
  "pprt (float (a, b)) = (if 0 <= a then (float (a,b)) else (float (0, b)))"
obua@16782
   429
  by (auto simp add: zero_le_float float_le_zero float_zero)
obua@16782
   430
obua@24301
   431
lemma pprt_lbound: "pprt (lbound x) = float (0, 0)"
obua@24301
   432
  apply (simp add: float_def)
obua@24301
   433
  apply (rule pprt_eq_0)
obua@24301
   434
  apply (simp add: lbound_def)
obua@24301
   435
  done
obua@24301
   436
obua@24301
   437
lemma nprt_ubound: "nprt (ubound x) = float (0, 0)"
obua@24301
   438
  apply (simp add: float_def)
obua@24301
   439
  apply (rule nprt_eq_0)
obua@24301
   440
  apply (simp add: ubound_def)
obua@24301
   441
  done
obua@24301
   442
obua@16782
   443
lemma float_nprt:
obua@16782
   444
  "nprt (float (a, b)) = (if 0 <= a then (float (0,b)) else (float (a, b)))"
obua@16782
   445
  by (auto simp add: zero_le_float float_le_zero float_zero)
obua@16782
   446
obua@16782
   447
lemma norm_0_1: "(0::_::number_ring) = Numeral0 & (1::_::number_ring) = Numeral1"
obua@16782
   448
  by auto
wenzelm@19765
   449
obua@16782
   450
lemma add_left_zero: "0 + a = (a::'a::comm_monoid_add)"
obua@16782
   451
  by simp
obua@16782
   452
obua@16782
   453
lemma add_right_zero: "a + 0 = (a::'a::comm_monoid_add)"
obua@16782
   454
  by simp
obua@16782
   455
obua@16782
   456
lemma mult_left_one: "1 * a = (a::'a::semiring_1)"
obua@16782
   457
  by simp
obua@16782
   458
obua@16782
   459
lemma mult_right_one: "a * 1 = (a::'a::semiring_1)"
obua@16782
   460
  by simp
obua@16782
   461
obua@16782
   462
lemma int_pow_0: "(a::int)^(Numeral0) = 1"
obua@16782
   463
  by simp
obua@16782
   464
obua@16782
   465
lemma int_pow_1: "(a::int)^(Numeral1) = a"
obua@16782
   466
  by simp
obua@16782
   467
obua@16782
   468
lemma zero_eq_Numeral0_nring: "(0::'a::number_ring) = Numeral0"
obua@16782
   469
  by simp
obua@16782
   470
obua@16782
   471
lemma one_eq_Numeral1_nring: "(1::'a::number_ring) = Numeral1"
obua@16782
   472
  by simp
obua@16782
   473
obua@16782
   474
lemma zero_eq_Numeral0_nat: "(0::nat) = Numeral0"
obua@16782
   475
  by simp
obua@16782
   476
obua@16782
   477
lemma one_eq_Numeral1_nat: "(1::nat) = Numeral1"
obua@16782
   478
  by simp
obua@16782
   479
obua@16782
   480
lemma zpower_Pls: "(z::int)^Numeral0 = Numeral1"
obua@16782
   481
  by simp
obua@16782
   482
obua@16782
   483
lemma zpower_Min: "(z::int)^((-1)::nat) = Numeral1"
obua@16782
   484
proof -
obua@16782
   485
  have 1:"((-1)::nat) = 0"
obua@16782
   486
    by simp
obua@16782
   487
  show ?thesis by (simp add: 1)
obua@16782
   488
qed
obua@16782
   489
obua@16782
   490
lemma fst_cong: "a=a' \<Longrightarrow> fst (a,b) = fst (a',b)"
obua@16782
   491
  by simp
obua@16782
   492
obua@16782
   493
lemma snd_cong: "b=b' \<Longrightarrow> snd (a,b) = snd (a,b')"
obua@16782
   494
  by simp
obua@16782
   495
obua@16782
   496
lemma lift_bool: "x \<Longrightarrow> x=True"
obua@16782
   497
  by simp
obua@16782
   498
obua@16782
   499
lemma nlift_bool: "~x \<Longrightarrow> x=False"
obua@16782
   500
  by simp
obua@16782
   501
obua@16782
   502
lemma not_false_eq_true: "(~ False) = True" by simp
obua@16782
   503
obua@16782
   504
lemma not_true_eq_false: "(~ True) = False" by simp
obua@16782
   505
wenzelm@19765
   506
lemmas binarith =
obua@16782
   507
  Pls_0_eq Min_1_eq
haftmann@20485
   508
  pred_Pls pred_Min pred_1 pred_0
haftmann@20485
   509
  succ_Pls succ_Min succ_1 succ_0
haftmann@20485
   510
  add_Pls add_Min add_BIT_0 add_BIT_10
haftmann@20485
   511
  add_BIT_11 minus_Pls minus_Min minus_1
haftmann@20485
   512
  minus_0 mult_Pls mult_Min mult_num1 mult_num0
haftmann@20485
   513
  add_Pls_right add_Min_right
obua@16782
   514
haftmann@20485
   515
lemma int_eq_number_of_eq:
haftmann@20485
   516
  "(((number_of v)::int)=(number_of w)) = iszero ((number_of (v + uminus w))::int)"
obua@16782
   517
  by simp
obua@16782
   518
wenzelm@19765
   519
lemma int_iszero_number_of_Pls: "iszero (Numeral0::int)"
obua@16782
   520
  by (simp only: iszero_number_of_Pls)
obua@16782
   521
obua@16782
   522
lemma int_nonzero_number_of_Min: "~(iszero ((-1)::int))"
obua@16782
   523
  by simp
obua@16782
   524
obua@16782
   525
lemma int_iszero_number_of_0: "iszero ((number_of (w BIT bit.B0))::int) = iszero ((number_of w)::int)"
obua@16782
   526
  by simp
obua@16782
   527
wenzelm@19765
   528
lemma int_iszero_number_of_1: "\<not> iszero ((number_of (w BIT bit.B1))::int)"
obua@16782
   529
  by simp
obua@16782
   530
haftmann@20485
   531
lemma int_less_number_of_eq_neg: "(((number_of x)::int) < number_of y) = neg ((number_of (x + (uminus y)))::int)"
obua@16782
   532
  by simp
obua@16782
   533
wenzelm@19765
   534
lemma int_not_neg_number_of_Pls: "\<not> (neg (Numeral0::int))"
obua@16782
   535
  by simp
obua@16782
   536
obua@16782
   537
lemma int_neg_number_of_Min: "neg (-1::int)"
obua@16782
   538
  by simp
obua@16782
   539
obua@16782
   540
lemma int_neg_number_of_BIT: "neg ((number_of (w BIT x))::int) = neg ((number_of w)::int)"
obua@16782
   541
  by simp
obua@16782
   542
haftmann@20485
   543
lemma int_le_number_of_eq: "(((number_of x)::int) \<le> number_of y) = (\<not> neg ((number_of (y + (uminus x)))::int))"
obua@16782
   544
  by simp
obua@16782
   545
wenzelm@19765
   546
lemmas intarithrel =
wenzelm@19765
   547
  int_eq_number_of_eq
wenzelm@19765
   548
  lift_bool[OF int_iszero_number_of_Pls] nlift_bool[OF int_nonzero_number_of_Min] int_iszero_number_of_0
obua@16782
   549
  lift_bool[OF int_iszero_number_of_1] int_less_number_of_eq_neg nlift_bool[OF int_not_neg_number_of_Pls] lift_bool[OF int_neg_number_of_Min]
obua@16782
   550
  int_neg_number_of_BIT int_le_number_of_eq
obua@16782
   551
haftmann@20485
   552
lemma int_number_of_add_sym: "((number_of v)::int) + number_of w = number_of (v + w)"
obua@16782
   553
  by simp
obua@16782
   554
haftmann@20485
   555
lemma int_number_of_diff_sym: "((number_of v)::int) - number_of w = number_of (v + (uminus w))"
obua@16782
   556
  by simp
obua@16782
   557
haftmann@20485
   558
lemma int_number_of_mult_sym: "((number_of v)::int) * number_of w = number_of (v * w)"
obua@16782
   559
  by simp
obua@16782
   560
haftmann@20485
   561
lemma int_number_of_minus_sym: "- ((number_of v)::int) = number_of (uminus v)"
obua@16782
   562
  by simp
obua@16782
   563
obua@16782
   564
lemmas intarith = int_number_of_add_sym int_number_of_minus_sym int_number_of_diff_sym int_number_of_mult_sym
obua@16782
   565
obua@16782
   566
lemmas natarith = add_nat_number_of diff_nat_number_of mult_nat_number_of eq_nat_number_of less_nat_number_of
obua@16782
   567
wenzelm@19765
   568
lemmas powerarith = nat_number_of zpower_number_of_even
wenzelm@19765
   569
  zpower_number_of_odd[simplified zero_eq_Numeral0_nring one_eq_Numeral1_nring]
obua@16782
   570
  zpower_Pls zpower_Min
obua@16782
   571
obua@24301
   572
lemmas floatarith[simplified norm_0_1] = float_add float_add_l0 float_add_r0 float_mult float_mult_l0 float_mult_r0 
obua@24653
   573
          float_minus float_abs zero_le_float float_pprt float_nprt pprt_lbound nprt_ubound
obua@16782
   574
obua@16782
   575
(* for use with the compute oracle *)
obua@16782
   576
lemmas arith = binarith intarith intarithrel natarith powerarith floatarith not_false_eq_true not_true_eq_false
obua@16782
   577
haftmann@22964
   578
use "float_arith.ML";
wenzelm@20771
   579
obua@16782
   580
end