src/HOL/Matrix_LP/ComputeFloat.thy
author haftmann
Fri Oct 10 19:55:32 2014 +0200 (2014-10-10)
changeset 58646 cd63a4b12a33
parent 56255 968667bbb8d2
child 58889 5b7a9633cfa8
permissions -rw-r--r--
specialized specification: avoid trivial instances
wenzelm@47455
     1
(*  Title:      HOL/Matrix_LP/ComputeFloat.thy
wenzelm@41959
     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
wenzelm@41413
     8
imports Complex_Main "~~/src/HOL/Library/Lattice_Algebras"
haftmann@20485
     9
begin
obua@16782
    10
wenzelm@48891
    11
ML_file "~~/src/Tools/float.ML"
wenzelm@48891
    12
wenzelm@38273
    13
definition int_of_real :: "real \<Rightarrow> int"
wenzelm@38273
    14
  where "int_of_real x = (SOME y. real y = x)"
wenzelm@21404
    15
wenzelm@38273
    16
definition real_is_int :: "real \<Rightarrow> bool"
wenzelm@38273
    17
  where "real_is_int x = (EX (u::int). x = real u)"
obua@16782
    18
obua@16782
    19
lemma real_is_int_def2: "real_is_int x = (x = real (int_of_real x))"
hoelzl@45495
    20
  by (auto simp add: real_is_int_def int_of_real_def)
obua@16782
    21
obua@16782
    22
lemma real_is_int_real[simp]: "real_is_int (real (x::int))"
obua@16782
    23
by (auto simp add: real_is_int_def int_of_real_def)
obua@16782
    24
obua@16782
    25
lemma int_of_real_real[simp]: "int_of_real (real x) = x"
obua@16782
    26
by (simp add: int_of_real_def)
obua@16782
    27
obua@16782
    28
lemma real_int_of_real[simp]: "real_is_int x \<Longrightarrow> real (int_of_real x) = x"
obua@16782
    29
by (auto simp add: int_of_real_def real_is_int_def)
obua@16782
    30
obua@16782
    31
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
    32
by (auto simp add: int_of_real_def real_is_int_def)
obua@16782
    33
obua@16782
    34
lemma real_is_int_add[simp]: "real_is_int a \<Longrightarrow> real_is_int b \<Longrightarrow> real_is_int (a+b)"
obua@16782
    35
apply (subst real_is_int_def2)
obua@16782
    36
apply (simp add: real_is_int_add_int_of_real real_int_of_real)
obua@16782
    37
done
obua@16782
    38
obua@16782
    39
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
    40
by (auto simp add: int_of_real_def real_is_int_def)
obua@16782
    41
obua@16782
    42
lemma real_is_int_sub[simp]: "real_is_int a \<Longrightarrow> real_is_int b \<Longrightarrow> real_is_int (a-b)"
obua@16782
    43
apply (subst real_is_int_def2)
obua@16782
    44
apply (simp add: int_of_real_sub real_int_of_real)
obua@16782
    45
done
obua@16782
    46
obua@16782
    47
lemma real_is_int_rep: "real_is_int x \<Longrightarrow> ?! (a::int). real a = x"
obua@16782
    48
by (auto simp add: real_is_int_def)
obua@16782
    49
wenzelm@19765
    50
lemma int_of_real_mult:
obua@16782
    51
  assumes "real_is_int a" "real_is_int b"
obua@16782
    52
  shows "(int_of_real (a*b)) = (int_of_real a) * (int_of_real b)"
hoelzl@45495
    53
  using assms
hoelzl@45495
    54
  by (auto simp add: real_is_int_def real_of_int_mult[symmetric]
hoelzl@45495
    55
           simp del: real_of_int_mult)
obua@16782
    56
obua@16782
    57
lemma real_is_int_mult[simp]: "real_is_int a \<Longrightarrow> real_is_int b \<Longrightarrow> real_is_int (a*b)"
obua@16782
    58
apply (subst real_is_int_def2)
obua@16782
    59
apply (simp add: int_of_real_mult)
obua@16782
    60
done
obua@16782
    61
obua@16782
    62
lemma real_is_int_0[simp]: "real_is_int (0::real)"
obua@16782
    63
by (simp add: real_is_int_def int_of_real_def)
obua@16782
    64
obua@16782
    65
lemma real_is_int_1[simp]: "real_is_int (1::real)"
obua@16782
    66
proof -
obua@16782
    67
  have "real_is_int (1::real) = real_is_int(real (1::int))" by auto
obua@16782
    68
  also have "\<dots> = True" by (simp only: real_is_int_real)
obua@16782
    69
  ultimately show ?thesis by auto
obua@16782
    70
qed
obua@16782
    71
obua@16782
    72
lemma real_is_int_n1: "real_is_int (-1::real)"
obua@16782
    73
proof -
obua@16782
    74
  have "real_is_int (-1::real) = real_is_int(real (-1::int))" by auto
obua@16782
    75
  also have "\<dots> = True" by (simp only: real_is_int_real)
obua@16782
    76
  ultimately show ?thesis by auto
obua@16782
    77
qed
obua@16782
    78
huffman@47108
    79
lemma real_is_int_numeral[simp]: "real_is_int (numeral x)"
huffman@47108
    80
  by (auto simp: real_is_int_def intro!: exI[of _ "numeral x"])
huffman@47108
    81
haftmann@54489
    82
lemma real_is_int_neg_numeral[simp]: "real_is_int (- numeral x)"
haftmann@54489
    83
  by (auto simp: real_is_int_def intro!: exI[of _ "- numeral x"])
obua@16782
    84
obua@16782
    85
lemma int_of_real_0[simp]: "int_of_real (0::real) = (0::int)"
obua@16782
    86
by (simp add: int_of_real_def)
obua@16782
    87
obua@16782
    88
lemma int_of_real_1[simp]: "int_of_real (1::real) = (1::int)"
wenzelm@19765
    89
proof -
obua@16782
    90
  have 1: "(1::real) = real (1::int)" by auto
obua@16782
    91
  show ?thesis by (simp only: 1 int_of_real_real)
obua@16782
    92
qed
obua@16782
    93
huffman@47108
    94
lemma int_of_real_numeral[simp]: "int_of_real (numeral b) = numeral b"
huffman@47108
    95
  unfolding int_of_real_def
huffman@47108
    96
  by (intro some_equality)
huffman@47108
    97
     (auto simp add: real_of_int_inject[symmetric] simp del: real_of_int_inject)
huffman@47108
    98
haftmann@54489
    99
lemma int_of_real_neg_numeral[simp]: "int_of_real (- numeral b) = - numeral b"
hoelzl@45495
   100
  unfolding int_of_real_def
hoelzl@45495
   101
  by (intro some_equality)
hoelzl@45495
   102
     (auto simp add: real_of_int_inject[symmetric] simp del: real_of_int_inject)
wenzelm@19765
   103
obua@16782
   104
lemma int_div_zdiv: "int (a div b) = (int a) div (int b)"
huffman@23431
   105
by (rule zdiv_int)
obua@16782
   106
obua@16782
   107
lemma int_mod_zmod: "int (a mod b) = (int a) mod (int b)"
huffman@23431
   108
by (rule zmod_int)
obua@16782
   109
obua@16782
   110
lemma abs_div_2_less: "a \<noteq> 0 \<Longrightarrow> a \<noteq> -1 \<Longrightarrow> abs((a::int) div 2) < abs a"
obua@16782
   111
by arith
obua@16782
   112
huffman@47108
   113
lemma norm_0_1: "(1::_::numeral) = Numeral1"
obua@16782
   114
  by auto
wenzelm@19765
   115
obua@16782
   116
lemma add_left_zero: "0 + a = (a::'a::comm_monoid_add)"
obua@16782
   117
  by simp
obua@16782
   118
obua@16782
   119
lemma add_right_zero: "a + 0 = (a::'a::comm_monoid_add)"
obua@16782
   120
  by simp
obua@16782
   121
obua@16782
   122
lemma mult_left_one: "1 * a = (a::'a::semiring_1)"
obua@16782
   123
  by simp
obua@16782
   124
obua@16782
   125
lemma mult_right_one: "a * 1 = (a::'a::semiring_1)"
obua@16782
   126
  by simp
obua@16782
   127
huffman@47108
   128
lemma int_pow_0: "(a::int)^0 = 1"
obua@16782
   129
  by simp
obua@16782
   130
obua@16782
   131
lemma int_pow_1: "(a::int)^(Numeral1) = a"
obua@16782
   132
  by simp
obua@16782
   133
huffman@47108
   134
lemma one_eq_Numeral1_nring: "(1::'a::numeral) = Numeral1"
obua@16782
   135
  by simp
obua@16782
   136
obua@16782
   137
lemma one_eq_Numeral1_nat: "(1::nat) = Numeral1"
obua@16782
   138
  by simp
obua@16782
   139
huffman@47108
   140
lemma zpower_Pls: "(z::int)^0 = Numeral1"
obua@16782
   141
  by simp
obua@16782
   142
obua@16782
   143
lemma fst_cong: "a=a' \<Longrightarrow> fst (a,b) = fst (a',b)"
obua@16782
   144
  by simp
obua@16782
   145
obua@16782
   146
lemma snd_cong: "b=b' \<Longrightarrow> snd (a,b) = snd (a,b')"
obua@16782
   147
  by simp
obua@16782
   148
obua@16782
   149
lemma lift_bool: "x \<Longrightarrow> x=True"
obua@16782
   150
  by simp
obua@16782
   151
obua@16782
   152
lemma nlift_bool: "~x \<Longrightarrow> x=False"
obua@16782
   153
  by simp
obua@16782
   154
obua@16782
   155
lemma not_false_eq_true: "(~ False) = True" by simp
obua@16782
   156
obua@16782
   157
lemma not_true_eq_false: "(~ True) = False" by simp
obua@16782
   158
huffman@47108
   159
lemmas powerarith = nat_numeral zpower_numeral_even
huffman@47108
   160
  zpower_numeral_odd zpower_Pls
obua@16782
   161
hoelzl@45495
   162
definition float :: "(int \<times> int) \<Rightarrow> real" where
hoelzl@45495
   163
  "float = (\<lambda>(a, b). real a * 2 powr real b)"
hoelzl@45495
   164
hoelzl@45495
   165
lemma float_add_l0: "float (0, e) + x = x"
hoelzl@45495
   166
  by (simp add: float_def)
hoelzl@45495
   167
hoelzl@45495
   168
lemma float_add_r0: "x + float (0, e) = x"
hoelzl@45495
   169
  by (simp add: float_def)
hoelzl@45495
   170
hoelzl@45495
   171
lemma float_add:
hoelzl@45495
   172
  "float (a1, e1) + float (a2, e2) =
hoelzl@45495
   173
  (if e1<=e2 then float (a1+a2*2^(nat(e2-e1)), e1) else float (a1*2^(nat (e1-e2))+a2, e2))"
hoelzl@45495
   174
  by (simp add: float_def algebra_simps powr_realpow[symmetric] powr_divide2[symmetric])
hoelzl@45495
   175
hoelzl@45495
   176
lemma float_mult_l0: "float (0, e) * x = float (0, 0)"
hoelzl@45495
   177
  by (simp add: float_def)
hoelzl@45495
   178
hoelzl@45495
   179
lemma float_mult_r0: "x * float (0, e) = float (0, 0)"
hoelzl@45495
   180
  by (simp add: float_def)
hoelzl@45495
   181
hoelzl@45495
   182
lemma float_mult:
hoelzl@45495
   183
  "float (a1, e1) * float (a2, e2) = (float (a1 * a2, e1 + e2))"
hoelzl@45495
   184
  by (simp add: float_def powr_add)
hoelzl@45495
   185
hoelzl@45495
   186
lemma float_minus:
hoelzl@45495
   187
  "- (float (a,b)) = float (-a, b)"
hoelzl@45495
   188
  by (simp add: float_def)
hoelzl@45495
   189
hoelzl@45495
   190
lemma zero_le_float:
hoelzl@45495
   191
  "(0 <= float (a,b)) = (0 <= a)"
hoelzl@45495
   192
  using powr_gt_zero[of 2 "real b", arith]
hoelzl@45495
   193
  by (simp add: float_def zero_le_mult_iff)
hoelzl@45495
   194
hoelzl@45495
   195
lemma float_le_zero:
hoelzl@45495
   196
  "(float (a,b) <= 0) = (a <= 0)"
hoelzl@45495
   197
  using powr_gt_zero[of 2 "real b", arith]
hoelzl@45495
   198
  by (simp add: float_def mult_le_0_iff)
hoelzl@45495
   199
hoelzl@45495
   200
lemma float_abs:
hoelzl@45495
   201
  "abs (float (a,b)) = (if 0 <= a then (float (a,b)) else (float (-a,b)))"
hoelzl@45495
   202
  using powr_gt_zero[of 2 "real b", arith]
hoelzl@45495
   203
  by (simp add: float_def abs_if mult_less_0_iff)
hoelzl@45495
   204
hoelzl@45495
   205
lemma float_zero:
hoelzl@45495
   206
  "float (0, b) = 0"
hoelzl@45495
   207
  by (simp add: float_def)
hoelzl@45495
   208
hoelzl@45495
   209
lemma float_pprt:
hoelzl@45495
   210
  "pprt (float (a, b)) = (if 0 <= a then (float (a,b)) else (float (0, b)))"
hoelzl@45495
   211
  by (auto simp add: zero_le_float float_le_zero float_zero)
hoelzl@45495
   212
hoelzl@45495
   213
lemma float_nprt:
hoelzl@45495
   214
  "nprt (float (a, b)) = (if 0 <= a then (float (0,b)) else (float (a, b)))"
hoelzl@45495
   215
  by (auto simp add: zero_le_float float_le_zero float_zero)
hoelzl@45495
   216
hoelzl@45495
   217
definition lbound :: "real \<Rightarrow> real"
hoelzl@45495
   218
  where "lbound x = min 0 x"
hoelzl@45495
   219
hoelzl@45495
   220
definition ubound :: "real \<Rightarrow> real"
hoelzl@45495
   221
  where "ubound x = max 0 x"
hoelzl@45495
   222
hoelzl@45495
   223
lemma lbound: "lbound x \<le> x"   
hoelzl@45495
   224
  by (simp add: lbound_def)
hoelzl@45495
   225
hoelzl@45495
   226
lemma ubound: "x \<le> ubound x"
hoelzl@45495
   227
  by (simp add: ubound_def)
hoelzl@45495
   228
hoelzl@45495
   229
lemma pprt_lbound: "pprt (lbound x) = float (0, 0)"
hoelzl@45495
   230
  by (auto simp: float_def lbound_def)
hoelzl@45495
   231
hoelzl@45495
   232
lemma nprt_ubound: "nprt (ubound x) = float (0, 0)"
hoelzl@45495
   233
  by (auto simp: float_def ubound_def)
hoelzl@45495
   234
obua@24301
   235
lemmas floatarith[simplified norm_0_1] = float_add float_add_l0 float_add_r0 float_mult float_mult_l0 float_mult_r0 
obua@24653
   236
          float_minus float_abs zero_le_float float_pprt float_nprt pprt_lbound nprt_ubound
obua@16782
   237
obua@16782
   238
(* for use with the compute oracle *)
huffman@47108
   239
lemmas arith = arith_simps rel_simps diff_nat_numeral nat_0
huffman@47108
   240
  nat_neg_numeral powerarith floatarith not_false_eq_true not_true_eq_false
obua@16782
   241
wenzelm@56255
   242
ML_file "float_arith.ML"
wenzelm@20771
   243
obua@16782
   244
end