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