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