262 have a:"b - (-1\<Colon>int) = (1\<Colon>int) + b" by arith |
262 have a:"b - (-1\<Colon>int) = (1\<Colon>int) + b" by arith |
263 show "(float (q, (b - (-1\<Colon>int)))) = (float (q, ((1\<Colon>int) + b)))" |
263 show "(float (q, (b - (-1\<Colon>int)))) = (float (q, ((1\<Colon>int) + b)))" |
264 by (simp add: a) |
264 by (simp add: a) |
265 qed |
265 qed |
266 |
266 |
267 consts |
|
268 norm_float :: "int*int \<Rightarrow> int*int" |
|
269 |
|
270 lemma int_div_zdiv: "int (a div b) = (int a) div (int b)" |
267 lemma int_div_zdiv: "int (a div b) = (int a) div (int b)" |
271 by (rule zdiv_int) |
268 by (rule zdiv_int) |
272 |
269 |
273 lemma int_mod_zmod: "int (a mod b) = (int a) mod (int b)" |
270 lemma int_mod_zmod: "int (a mod b) = (int a) mod (int b)" |
274 by (rule zmod_int) |
271 by (rule zmod_int) |
275 |
272 |
276 lemma abs_div_2_less: "a \<noteq> 0 \<Longrightarrow> a \<noteq> -1 \<Longrightarrow> abs((a::int) div 2) < abs a" |
273 lemma abs_div_2_less: "a \<noteq> 0 \<Longrightarrow> a \<noteq> -1 \<Longrightarrow> abs((a::int) div 2) < abs a" |
277 by arith |
274 by arith |
278 |
275 |
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>" |
276 function norm_float :: "int \<Rightarrow> int \<Rightarrow> int \<times> int" where |
280 apply (auto) |
277 "norm_float a b = (if a \<noteq> 0 \<and> even a then norm_float (a div 2) (b + 1) |
281 apply (rule abs_div_2_less) |
278 else if a = 0 then (0, 0) else (a, b))" |
282 apply (auto) |
279 by auto |
283 done |
280 |
284 |
281 termination by (relation "measure (nat o abs o fst)") |
285 declare [[simp_depth_limit = 2]] |
282 (auto intro: abs_div_2_less) |
286 recdef norm_float "measure (% (a,b). nat (abs a))" |
283 |
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)))" |
284 lemma norm_float: "float x = float (split norm_float x)" |
288 (hints simp: even_def terminating_norm_float) |
|
289 declare [[simp_depth_limit = 100]] |
|
290 |
|
291 lemma norm_float: "float x = float (norm_float x)" |
|
292 proof - |
285 proof - |
293 { |
286 { |
294 fix a b :: int |
287 fix a b :: int |
295 have norm_float_pair: "float (a,b) = float (norm_float (a,b))" |
288 have norm_float_pair: "float (a, b) = float (norm_float a b)" |
296 proof (induct a b rule: norm_float.induct) |
289 proof (induct a b rule: norm_float.induct) |
297 case (1 u v) |
290 case (1 u v) |
298 show ?case |
291 show ?case |
299 proof cases |
292 proof cases |
300 assume u: "u \<noteq> 0 \<and> even u" |
293 assume u: "u \<noteq> 0 \<and> even u" |
301 with prems have ind: "float (u div 2, v + 1) = float (norm_float (u div 2, v + 1))" by auto |
294 with prems have ind: "float (u div 2, v + 1) = float (norm_float (u div 2) (v + 1))" by auto |
302 with u have "float (u,v) = float (u div 2, v+1)" by (simp add: float_transfer_even) |
295 with u have "float (u,v) = float (u div 2, v+1)" by (simp add: float_transfer_even) |
303 then show ?thesis |
296 then show ?thesis |
304 apply (subst norm_float.simps) |
297 apply (subst norm_float.simps) |
305 apply (simp add: ind) |
298 apply (simp add: ind) |
306 done |
299 done |
312 qed |
305 qed |
313 } |
306 } |
314 note helper = this |
307 note helper = this |
315 have "? a b. x = (a,b)" by auto |
308 have "? a b. x = (a,b)" by auto |
316 then obtain a b where "x = (a, b)" by blast |
309 then obtain a b where "x = (a, b)" by blast |
317 then show ?thesis by (simp only: helper) |
310 then show ?thesis by (simp add: helper) |
318 qed |
311 qed |
319 |
312 |
320 lemma float_add_l0: "float (0, e) + x = x" |
313 lemma float_add_l0: "float (0, e) + x = x" |
321 by (simp add: float_def) |
314 by (simp add: float_def) |
322 |
315 |