src/HOL/Library/Executable_Real.thy
author chaieb
Mon Jun 11 11:06:04 2007 +0200 (2007-06-11)
changeset 23315 df3a7e9ebadb
parent 23030 c7ff1537c4bf
child 24197 c9e3cb5e5681
permissions -rw-r--r--
tuned Proof
chaieb@22981
     1
(*  Title:      HOL/Library/Executable_Real.thy
chaieb@22981
     2
    ID:         $Id$
chaieb@22981
     3
    Author:     Amine Chaieb, TU Muenchen
chaieb@22981
     4
*)
chaieb@22981
     5
chaieb@22981
     6
header {* Implementation of rational real numbers as pairs of integers *}
chaieb@22981
     7
chaieb@22981
     8
theory Executable_Real
chaieb@22981
     9
imports GCD "~~/src/HOL/Real/Real"
chaieb@22981
    10
begin
chaieb@22981
    11
chaieb@22981
    12
subsection {* Implementation of operations on pair of integers *}
chaieb@22981
    13
chaieb@22981
    14
types Num = "int * int"
chaieb@22981
    15
syntax "_Num0" :: "Num" ("0\<^sub>N")
chaieb@22981
    16
translations "0\<^sub>N" \<rightleftharpoons> "(0,0)"
chaieb@22981
    17
syntax "_Numi" :: "int \<Rightarrow> Num" ("_\<^sub>N")
chaieb@22981
    18
translations "i\<^sub>N" \<rightleftharpoons> "(i,1)::Num"
chaieb@22981
    19
chaieb@22981
    20
constdefs isnormNum :: "Num \<Rightarrow> bool"
chaieb@22981
    21
  "isnormNum \<equiv> \<lambda>(a,b). (if a = 0 then b = 0 else b > 0 \<and> igcd a b = 1)"
chaieb@22981
    22
chaieb@22981
    23
constdefs normNum :: "Num \<Rightarrow> Num"
chaieb@22981
    24
  "normNum \<equiv> \<lambda>(a,b). (if a=0 \<or> b = 0 then (0,0) else 
chaieb@22981
    25
  (let g = igcd a b 
chaieb@22981
    26
   in if b > 0 then (a div g, b div g) else (- (a div g), - (b div g))))"
chaieb@23315
    27
chaieb@22981
    28
lemma normNum_isnormNum[simp]: "isnormNum (normNum x)"
chaieb@22981
    29
proof-
chaieb@22981
    30
  have " \<exists> a b. x = (a,b)" by auto
chaieb@22981
    31
  then obtain a b where x[simp]: "x = (a,b)" by blast
chaieb@22981
    32
  {assume "a=0 \<or> b = 0" hence ?thesis by (simp add: normNum_def isnormNum_def)}  
chaieb@22981
    33
  moreover
chaieb@22981
    34
  {assume anz: "a \<noteq> 0" and bnz: "b \<noteq> 0" 
chaieb@22981
    35
    let ?g = "igcd a b"
chaieb@22981
    36
    let ?a' = "a div ?g"
chaieb@22981
    37
    let ?b' = "b div ?g"
chaieb@22981
    38
    let ?g' = "igcd ?a' ?b'"
chaieb@22981
    39
    from anz bnz have "?g \<noteq> 0" by simp  with igcd_pos[of a b] 
chaieb@22981
    40
    have gpos: "?g > 0"  by arith
chaieb@22981
    41
    have gdvd: "?g dvd a" "?g dvd b" by (simp_all add: igcd_dvd1 igcd_dvd2)
chaieb@22981
    42
    from zdvd_mult_div_cancel[OF gdvd(1)] zdvd_mult_div_cancel[OF gdvd(2)]
chaieb@22981
    43
    anz bnz
chaieb@22981
    44
    have nz':"?a' \<noteq> 0" "?b' \<noteq> 0" 
chaieb@22981
    45
      by - (rule notI,simp add:igcd_def)+
chaieb@22981
    46
    from anz bnz have stupid: "a \<noteq> 0 \<or> b \<noteq> 0" by blast
chaieb@22981
    47
    from div_igcd_relprime[OF stupid] have gp1: "?g' = 1" .
chaieb@22981
    48
    from bnz have "b < 0 \<or> b > 0" by arith
chaieb@22981
    49
    moreover
chaieb@22981
    50
    {assume b: "b > 0"
chaieb@22981
    51
      from pos_imp_zdiv_nonneg_iff[OF gpos] b
chaieb@22981
    52
      have "?b' \<ge> 0" by simp
chaieb@22981
    53
      with nz' have b': "?b' > 0" by simp
chaieb@22981
    54
      from b b' anz bnz nz' gp1 have ?thesis 
chaieb@22981
    55
	by (simp add: isnormNum_def normNum_def Let_def split_def fst_conv snd_conv)}
chaieb@22981
    56
    moreover {assume b: "b < 0"
chaieb@22981
    57
      {assume b': "?b' \<ge> 0" 
chaieb@22981
    58
	from gpos have th: "?g \<ge> 0" by arith
chaieb@22981
    59
	from mult_nonneg_nonneg[OF th b'] zdvd_mult_div_cancel[OF gdvd(2)]
chaieb@22981
    60
	have False using b by simp }
chaieb@23315
    61
      hence b': "?b' < 0" by (auto simp add: linorder_not_le[symmetric])
chaieb@22981
    62
      from anz bnz nz' b b' gp1 have ?thesis 
chaieb@22981
    63
	by (simp add: isnormNum_def normNum_def Let_def split_def fst_conv snd_conv)}
chaieb@22981
    64
    ultimately have ?thesis by blast
chaieb@22981
    65
  }
chaieb@22981
    66
  ultimately show ?thesis by blast
chaieb@22981
    67
qed
chaieb@22981
    68
    (* Arithmetic over Num *)
chaieb@22981
    69
constdefs Nadd :: "Num \<Rightarrow> Num \<Rightarrow> Num" (infixl "+\<^sub>N" 60)
chaieb@22981
    70
  "Nadd \<equiv> \<lambda>(a,b) (a',b'). if a = 0 \<or> b = 0 then normNum(a',b') 
chaieb@22981
    71
  else if a'=0 \<or> b' = 0 then normNum(a,b) 
chaieb@22981
    72
  else normNum(a*b' + b*a', b*b')"
chaieb@22981
    73
constdefs Nmul :: "Num \<Rightarrow> Num \<Rightarrow> Num" (infixl "*\<^sub>N" 60)
chaieb@22981
    74
  "Nmul \<equiv> \<lambda>(a,b) (a',b'). let g = igcd (a*a') (b*b') 
chaieb@22981
    75
  in (a*a' div g, b*b' div g)"
chaieb@22981
    76
constdefs Nneg :: "Num \<Rightarrow> Num" ("~\<^sub>N")
chaieb@22981
    77
  "Nneg \<equiv> \<lambda>(a,b). (-a,b)"
chaieb@22981
    78
constdefs  Nsub :: "Num \<Rightarrow> Num \<Rightarrow> Num" (infixl "-\<^sub>N" 60)
chaieb@22981
    79
  "Nsub \<equiv> \<lambda>a b. a +\<^sub>N ~\<^sub>N b"
chaieb@22981
    80
constdefs Ninv :: "Num \<Rightarrow> Num" 
chaieb@22981
    81
"Ninv \<equiv> \<lambda>(a,b). if a < 0 then (-b, \<bar>a\<bar>) else (b,a)"
chaieb@22981
    82
constdefs Ndiv :: "Num \<Rightarrow> Num \<Rightarrow> Num" (infixl "\<div>\<^sub>N" 60)
chaieb@22981
    83
  "Ndiv \<equiv> \<lambda>a b. a *\<^sub>N Ninv b"
chaieb@22981
    84
chaieb@22981
    85
lemma Nneg_normN[simp]: "isnormNum x \<Longrightarrow> isnormNum (~\<^sub>N x)"
chaieb@22981
    86
  by(simp add: isnormNum_def Nneg_def split_def)
chaieb@22981
    87
lemma Nadd_normN[simp]: "isnormNum (x +\<^sub>N y)"
chaieb@22981
    88
  by (simp add: Nadd_def split_def)
chaieb@22981
    89
lemma Nsub_normN[simp]: "\<lbrakk> isnormNum y\<rbrakk> \<Longrightarrow> isnormNum (x -\<^sub>N y)"
chaieb@22981
    90
  by (simp add: Nsub_def split_def)
chaieb@22981
    91
lemma Nmul_normN[simp]: assumes xn:"isnormNum x" and yn: "isnormNum y"
chaieb@22981
    92
  shows "isnormNum (x *\<^sub>N y)"
chaieb@22981
    93
proof-
chaieb@22981
    94
  have "\<exists>a b. x = (a,b)" and "\<exists> a' b'. y = (a',b')" by auto
chaieb@22981
    95
  then obtain a b a' b' where ab: "x = (a,b)"  and ab': "y = (a',b')" by blast 
chaieb@22981
    96
  {assume "a = 0"
chaieb@22981
    97
    hence ?thesis using xn ab ab'
chaieb@22981
    98
      by (simp add: igcd_def isnormNum_def Let_def Nmul_def split_def)}
chaieb@22981
    99
  moreover
chaieb@22981
   100
  {assume "a' = 0"
chaieb@22981
   101
    hence ?thesis using yn ab ab' 
chaieb@22981
   102
      by (simp add: igcd_def isnormNum_def Let_def Nmul_def split_def)}
chaieb@22981
   103
  moreover
chaieb@22981
   104
  {assume a: "a \<noteq>0" and a': "a'\<noteq>0"
chaieb@22981
   105
    hence bp: "b > 0" "b' > 0" using xn yn ab ab' by (simp_all add: isnormNum_def)
chaieb@22981
   106
    from mult_pos_pos[OF bp] have "x *\<^sub>N y = normNum (a*a', b*b')" 
chaieb@22981
   107
      using ab ab' a a' bp by (simp add: Nmul_def Let_def split_def normNum_def)
chaieb@22981
   108
    hence ?thesis by simp}
chaieb@22981
   109
  ultimately show ?thesis by blast
chaieb@22981
   110
qed
chaieb@22981
   111
chaieb@22981
   112
lemma Ninv_normN[simp]: "isnormNum x \<Longrightarrow> isnormNum (Ninv x)"
chaieb@22981
   113
by (simp add: Ninv_def isnormNum_def split_def)
chaieb@22981
   114
(cases "fst x = 0",auto simp add: igcd_commute)
chaieb@22981
   115
chaieb@22981
   116
lemma isnormNum_int[simp]: 
chaieb@22981
   117
  "isnormNum 0\<^sub>N" "isnormNum (1::int)\<^sub>N" "i \<noteq> 0 \<Longrightarrow> isnormNum i\<^sub>N"
chaieb@22981
   118
 by (simp_all add: isnormNum_def igcd_def)
chaieb@22981
   119
chaieb@22981
   120
    (* Relations over Num *)
chaieb@22981
   121
constdefs Nlt0:: "Num \<Rightarrow> bool" ("0>\<^sub>N")
chaieb@22981
   122
  "Nlt0 \<equiv> \<lambda>(a,b). a < 0"
chaieb@22981
   123
constdefs Nle0:: "Num \<Rightarrow> bool" ("0\<ge>\<^sub>N")
chaieb@22981
   124
  "Nle0 \<equiv> \<lambda>(a,b). a \<le> 0"
chaieb@22981
   125
constdefs Ngt0:: "Num \<Rightarrow> bool" ("0<\<^sub>N")
chaieb@22981
   126
  "Ngt0 \<equiv> \<lambda>(a,b). a > 0"
chaieb@22981
   127
constdefs Nge0:: "Num \<Rightarrow> bool" ("0\<le>\<^sub>N")
chaieb@22981
   128
  "Nge0 \<equiv> \<lambda>(a,b). a \<ge> 0"
chaieb@22981
   129
constdefs Nlt :: "Num \<Rightarrow> Num \<Rightarrow> bool" (infix "<\<^sub>N" 55)
chaieb@22981
   130
  "Nlt \<equiv> \<lambda>a b. 0>\<^sub>N (a -\<^sub>N b)"
chaieb@22981
   131
constdefs Nle :: "Num \<Rightarrow> Num \<Rightarrow> bool" (infix "\<le>\<^sub>N" 55)
chaieb@22981
   132
  "Nle \<equiv> \<lambda>a b. 0\<ge>\<^sub>N (a -\<^sub>N b)"
chaieb@22981
   133
chaieb@22981
   134
chaieb@22982
   135
subsection {* Interpretation of the normalized rats in reals *}
chaieb@22981
   136
chaieb@22981
   137
definition
chaieb@22981
   138
  INum:: "Num \<Rightarrow> real"
chaieb@22981
   139
where
chaieb@22981
   140
  INum_def: "INum \<equiv> \<lambda>(a,b). real a / real b"
chaieb@22981
   141
chaieb@22981
   142
code_datatype INum
chaieb@22981
   143
instance real :: eq ..
chaieb@22981
   144
chaieb@22981
   145
definition
chaieb@22981
   146
  real_int :: "int \<Rightarrow> real"
chaieb@22981
   147
where
chaieb@22981
   148
  "real_int = real"
chaieb@22981
   149
lemmas [code unfold] = real_int_def [symmetric]
chaieb@22981
   150
chaieb@22981
   151
lemma [code unfold]:
chaieb@22981
   152
  "real = real_int o int"
chaieb@22981
   153
  by (auto simp add: real_int_def expand_fun_eq)
chaieb@22981
   154
chaieb@22981
   155
lemma INum_int [simp]: "INum i\<^sub>N = real i" "INum 0\<^sub>N = 0"
chaieb@22981
   156
  by (simp_all add: INum_def)
chaieb@22981
   157
lemmas [code, code unfold] = INum_int [unfolded real_int_def [symmetric], symmetric]
chaieb@22981
   158
chaieb@22981
   159
lemma [code, code unfold]: "1 = INum 1\<^sub>N" by simp
chaieb@22981
   160
chaieb@22981
   161
lemma isnormNum_unique[simp]: 
chaieb@22981
   162
  assumes na: "isnormNum x" and nb: "isnormNum y" 
chaieb@22981
   163
  shows "(INum x = INum y) = (x = y)" (is "?lhs = ?rhs")
chaieb@22981
   164
proof
chaieb@22981
   165
  have "\<exists> a b a' b'. x = (a,b) \<and> y = (a',b')" by auto
chaieb@22981
   166
  then obtain a b a' b' where xy[simp]: "x = (a,b)" "y=(a',b')" by blast
chaieb@22981
   167
  assume H: ?lhs 
chaieb@22981
   168
  {assume "a = 0 \<or> b = 0 \<or> a' = 0 \<or> b' = 0" hence ?rhs
chaieb@22981
   169
      using na nb H
chaieb@22981
   170
      by (simp add: INum_def split_def isnormNum_def)
chaieb@22981
   171
       (cases "a = 0", simp_all,cases "b = 0", simp_all,
chaieb@22981
   172
      cases "a' = 0", simp_all,cases "a' = 0", simp_all)}
chaieb@22981
   173
  moreover
chaieb@22981
   174
  { assume az: "a \<noteq> 0" and bz: "b \<noteq> 0" and a'z: "a'\<noteq>0" and b'z: "b'\<noteq>0"
chaieb@22981
   175
    from az bz a'z b'z na nb have pos: "b > 0" "b' > 0" by (simp_all add: isnormNum_def)
chaieb@22981
   176
    from prems have eq:"a * b' = a'*b" 
chaieb@22981
   177
      by (simp add: INum_def  eq_divide_eq divide_eq_eq real_of_int_mult[symmetric] del: real_of_int_mult)
chaieb@22981
   178
    from prems have gcd1: "igcd a b = 1" "igcd b a = 1" "igcd a' b' = 1" "igcd b' a' = 1"       
chaieb@22981
   179
      by (simp_all add: isnormNum_def add: igcd_commute)
chaieb@22981
   180
    from eq have raw_dvd: "a dvd a'*b" "b dvd b'*a" "a' dvd a*b'" "b' dvd b*a'" 
chaieb@22981
   181
      apply(unfold dvd_def)
chaieb@22981
   182
      apply (rule_tac x="b'" in exI, simp add: mult_ac)
chaieb@22981
   183
      apply (rule_tac x="a'" in exI, simp add: mult_ac)
chaieb@22981
   184
      apply (rule_tac x="b" in exI, simp add: mult_ac)
chaieb@22981
   185
      apply (rule_tac x="a" in exI, simp add: mult_ac)
chaieb@22981
   186
      done
chaieb@22981
   187
    from zdvd_dvd_eq[OF bz zrelprime_dvd_mult[OF gcd1(2) raw_dvd(2)]
chaieb@22981
   188
      zrelprime_dvd_mult[OF gcd1(4) raw_dvd(4)]]
chaieb@22981
   189
      have eq1: "b = b'" using pos by simp_all
chaieb@22981
   190
      with eq have "a = a'" using pos by simp
chaieb@22981
   191
      with eq1 have ?rhs by simp}
chaieb@22981
   192
  ultimately show ?rhs by blast
chaieb@22981
   193
next
chaieb@22981
   194
  assume ?rhs thus ?lhs by simp
chaieb@22981
   195
qed
chaieb@22981
   196
chaieb@22981
   197
chaieb@22981
   198
lemma isnormNum0[simp]: "isnormNum x \<Longrightarrow> (INum x = 0) = (x = 0\<^sub>N)"
chaieb@22981
   199
  unfolding INum_int(2)[symmetric]
chaieb@22981
   200
  by (rule isnormNum_unique, simp_all)
chaieb@22981
   201
chaieb@22981
   202
lemma normNum[simp]: "INum (normNum x) = INum x"
chaieb@22981
   203
proof-
chaieb@22981
   204
  have "\<exists> a b. x = (a,b)" by auto
chaieb@22981
   205
  then obtain a b where x[simp]: "x = (a,b)" by blast
chaieb@22981
   206
  {assume "a=0 \<or> b = 0" hence ?thesis
chaieb@22981
   207
      by (simp add: INum_def normNum_def split_def Let_def)}
chaieb@22981
   208
  moreover 
chaieb@22981
   209
  {assume a: "a\<noteq>0" and b: "b\<noteq>0"
chaieb@22981
   210
    let ?g = "igcd a b"
chaieb@22981
   211
    from a b have g: "?g \<noteq> 0"by simp
chaieb@22981
   212
    from real_of_int_div[OF g]
chaieb@22981
   213
    have ?thesis by (simp add: INum_def normNum_def split_def Let_def)}
chaieb@22981
   214
  ultimately show ?thesis by blast
chaieb@22981
   215
qed
chaieb@22981
   216
chaieb@22981
   217
lemma INum_normNum_iff [code]: "INum x = INum y \<longleftrightarrow> normNum x = normNum y" (is "?lhs = ?rhs")
chaieb@22981
   218
proof -
chaieb@22981
   219
  have "normNum x = normNum y \<longleftrightarrow> INum (normNum x) = INum (normNum y)"
chaieb@22981
   220
    by (simp del: normNum)
chaieb@22981
   221
  also have "\<dots> = ?lhs" by simp
chaieb@22981
   222
  finally show ?thesis by simp
chaieb@22981
   223
qed
chaieb@22981
   224
chaieb@22981
   225
lemma Nadd[simp]: "INum (x +\<^sub>N y) = INum x + INum y"
chaieb@22981
   226
proof-
chaieb@22981
   227
  have " \<exists> a b. x = (a,b)" " \<exists> a' b'. y = (a',b')" by auto
chaieb@22981
   228
  then obtain a b a' b' where x[simp]: "x = (a,b)" 
chaieb@22981
   229
    and y[simp]: "y = (a',b')" by blast
chaieb@22981
   230
  {assume "a=0 \<or> a'= 0 \<or> b =0 \<or> b' = 0" hence ?thesis 
chaieb@22981
   231
      apply (cases "a=0",simp_all add: Nadd_def)
chaieb@22981
   232
      apply (cases "b= 0",simp_all add: INum_def)
chaieb@22981
   233
       apply (cases "a'= 0",simp_all)
chaieb@22981
   234
       apply (cases "b'= 0",simp_all)
chaieb@22981
   235
       done }
chaieb@22981
   236
  moreover 
chaieb@22981
   237
  {assume aa':"a \<noteq> 0" "a'\<noteq> 0" and bb': "b \<noteq> 0" "b' \<noteq> 0" 
chaieb@22981
   238
    {assume z: "a * b' + b * a' = 0"
chaieb@22981
   239
      hence "real (a*b' + b*a') / (real b* real b') = 0" by simp
chaieb@22981
   240
      hence "real b' * real a / (real b * real b') + real b * real a' / (real b * real b') = 0"  by (simp add:add_divide_distrib) 
chaieb@22981
   241
      hence th: "real a / real b + real a' / real b' = 0" using bb' aa' by simp 
chaieb@22981
   242
      from z aa' bb' have ?thesis 
chaieb@22981
   243
	by (simp add: th Nadd_def normNum_def INum_def split_def)}
chaieb@22981
   244
    moreover {assume z: "a * b' + b * a' \<noteq> 0"
chaieb@22981
   245
      let ?g = "igcd (a * b' + b * a') (b*b')"
chaieb@22981
   246
      have gz: "?g \<noteq> 0" using z by simp
chaieb@22981
   247
      have ?thesis using aa' bb' z gz
chaieb@22981
   248
	real_of_int_div[OF gz igcd_dvd1[where i="a * b' + b * a'" and j="b*b'"]]
chaieb@22981
   249
	real_of_int_div[OF gz igcd_dvd2[where i="a * b' + b * a'" and j="b*b'"]]
chaieb@22981
   250
	by (simp add: x y Nadd_def INum_def normNum_def Let_def add_divide_distrib)}
chaieb@22981
   251
    ultimately have ?thesis using aa' bb' 
chaieb@22981
   252
      by (simp add: Nadd_def INum_def normNum_def x y Let_def) }
chaieb@22981
   253
  ultimately show ?thesis by blast
chaieb@22981
   254
qed
chaieb@22981
   255
lemmas [code] = Nadd [symmetric]
chaieb@22981
   256
chaieb@22981
   257
lemma Nmul[simp]: "INum (x *\<^sub>N y) = INum x * INum y"
chaieb@22981
   258
proof-
chaieb@22981
   259
  have " \<exists> a b. x = (a,b)" " \<exists> a' b'. y = (a',b')" by auto
chaieb@22981
   260
  then obtain a b a' b' where x: "x = (a,b)" and y: "y = (a',b')" by blast
chaieb@22981
   261
  {assume "a=0 \<or> a'= 0 \<or> b = 0 \<or> b' = 0" hence ?thesis 
chaieb@22981
   262
      apply (cases "a=0",simp_all add: x y Nmul_def INum_def Let_def)
chaieb@22981
   263
      apply (cases "b=0",simp_all)
chaieb@22981
   264
      apply (cases "a'=0",simp_all) 
chaieb@22981
   265
      done }
chaieb@22981
   266
  moreover
chaieb@22981
   267
  {assume z: "a \<noteq> 0" "a' \<noteq> 0" "b \<noteq> 0" "b' \<noteq> 0"
chaieb@22981
   268
    let ?g="igcd (a*a') (b*b')"
chaieb@22981
   269
    have gz: "?g \<noteq> 0" using z by simp
chaieb@22981
   270
    from z real_of_int_div[OF gz igcd_dvd1[where i="a*a'" and j="b*b'"]] 
chaieb@22981
   271
      real_of_int_div[OF gz igcd_dvd2[where i="a*a'" and j="b*b'"]] 
chaieb@22981
   272
    have ?thesis by (simp add: Nmul_def x y Let_def INum_def)}
chaieb@22981
   273
  ultimately show ?thesis by blast
chaieb@22981
   274
qed
chaieb@22981
   275
lemmas [code] = Nmul [symmetric]
chaieb@22981
   276
chaieb@22981
   277
lemma Nneg[simp]: "INum (~\<^sub>N x) = - INum x"
chaieb@22981
   278
  by (simp add: Nneg_def split_def INum_def)
chaieb@22981
   279
lemmas [code] = Nneg [symmetric]
chaieb@22981
   280
chaieb@22981
   281
lemma Nsub[simp]: shows "INum (x -\<^sub>N y) = INum x - INum y"
chaieb@22981
   282
  by (simp add: Nsub_def split_def)
chaieb@22981
   283
lemmas [code] = Nsub [symmetric]
chaieb@22981
   284
chaieb@22981
   285
lemma Ninv[simp]: "INum (Ninv x) = 1 / (INum x)"
chaieb@22981
   286
  by (simp add: Ninv_def INum_def split_def)
chaieb@22981
   287
lemmas [code] = Ninv [symmetric]
chaieb@22981
   288
chaieb@22981
   289
lemma Ndiv[simp]: "INum (x \<div>\<^sub>N y) = INum x / INum y" by (simp add: Ndiv_def)
chaieb@22981
   290
lemmas [code] = Ndiv [symmetric]
chaieb@22981
   291
chaieb@22981
   292
lemma Nlt0_iff[simp]: assumes nx: "isnormNum x" shows "(INum x < 0) = 0>\<^sub>N x "
chaieb@22981
   293
proof-
chaieb@22981
   294
  have " \<exists> a b. x = (a,b)" by simp
chaieb@22981
   295
  then obtain a b where x[simp]:"x = (a,b)" by blast
chaieb@22981
   296
  {assume "a = 0" hence ?thesis by (simp add: Nlt0_def INum_def) }
chaieb@22981
   297
  moreover
chaieb@22981
   298
  {assume a: "a\<noteq>0" hence b: "real b > 0" using nx by (simp add: isnormNum_def)
chaieb@22981
   299
    from pos_divide_less_eq[OF b, where b="real a" and a="0"]
chaieb@22981
   300
    have ?thesis by (simp add: Nlt0_def INum_def)}
chaieb@22981
   301
  ultimately show ?thesis by blast
chaieb@22981
   302
qed
chaieb@22981
   303
chaieb@22981
   304
lemma   Nle0_iff[simp]:assumes nx: "isnormNum x" shows "(INum x \<le> 0) = 0\<ge>\<^sub>N x"
chaieb@22981
   305
proof-
chaieb@22981
   306
  have " \<exists> a b. x = (a,b)" by simp
chaieb@22981
   307
  then obtain a b where x[simp]:"x = (a,b)" by blast
chaieb@22981
   308
  {assume "a = 0" hence ?thesis by (simp add: Nle0_def INum_def) }
chaieb@22981
   309
  moreover
chaieb@22981
   310
  {assume a: "a\<noteq>0" hence b: "real b > 0" using nx by (simp add: isnormNum_def)
chaieb@22981
   311
    from pos_divide_le_eq[OF b, where b="real a" and a="0"]
chaieb@22981
   312
    have ?thesis by (simp add: Nle0_def INum_def)}
chaieb@22981
   313
  ultimately show ?thesis by blast
chaieb@22981
   314
qed
chaieb@22981
   315
chaieb@22981
   316
lemma Ngt0_iff[simp]:assumes nx: "isnormNum x" shows "(INum x > 0) = 0<\<^sub>N x"
chaieb@22981
   317
proof-
chaieb@22981
   318
  have " \<exists> a b. x = (a,b)" by simp
chaieb@22981
   319
  then obtain a b where x[simp]:"x = (a,b)" by blast
chaieb@22981
   320
  {assume "a = 0" hence ?thesis by (simp add: Ngt0_def INum_def) }
chaieb@22981
   321
  moreover
chaieb@22981
   322
  {assume a: "a\<noteq>0" hence b: "real b > 0" using nx by (simp add: isnormNum_def)
chaieb@22981
   323
    from pos_less_divide_eq[OF b, where b="real a" and a="0"]
chaieb@22981
   324
    have ?thesis by (simp add: Ngt0_def INum_def)}
chaieb@22981
   325
  ultimately show ?thesis by blast
chaieb@22981
   326
qed
chaieb@22981
   327
lemma Nge0_iff[simp]:assumes nx: "isnormNum x" shows "(INum x \<ge> 0) = 0\<le>\<^sub>N x"
chaieb@22981
   328
proof-
chaieb@22981
   329
  have " \<exists> a b. x = (a,b)" by simp
chaieb@22981
   330
  then obtain a b where x[simp]:"x = (a,b)" by blast
chaieb@22981
   331
  {assume "a = 0" hence ?thesis by (simp add: Nge0_def INum_def) }
chaieb@22981
   332
  moreover
chaieb@22981
   333
  {assume a: "a\<noteq>0" hence b: "real b > 0" using nx by (simp add: isnormNum_def)
chaieb@22981
   334
    from pos_le_divide_eq[OF b, where b="real a" and a="0"]
chaieb@22981
   335
    have ?thesis by (simp add: Nge0_def INum_def)}
chaieb@22981
   336
  ultimately show ?thesis by blast
chaieb@22981
   337
qed
chaieb@22981
   338
chaieb@22981
   339
lemma Nlt_iff[simp]: assumes nx: "isnormNum x" and ny: "isnormNum y"
chaieb@22981
   340
  shows "(INum x < INum y) = (x <\<^sub>N y)"
chaieb@22981
   341
proof-
chaieb@22981
   342
  have "(INum x < INum y) = (INum (x -\<^sub>N y) < 0)" using nx ny by simp
chaieb@22981
   343
  also have "\<dots> = (0>\<^sub>N (x -\<^sub>N y))" using Nlt0_iff[OF Nsub_normN[OF ny]] by simp
chaieb@22981
   344
  finally show ?thesis by (simp add: Nlt_def)
chaieb@22981
   345
qed
chaieb@22981
   346
chaieb@22981
   347
lemma [code]: "INum x < INum y \<longleftrightarrow> normNum x <\<^sub>N normNum y"
chaieb@22981
   348
proof -
chaieb@22981
   349
  have "normNum x <\<^sub>N normNum y \<longleftrightarrow> INum (normNum x) < INum (normNum y)" 
chaieb@22981
   350
    by (simp del: normNum)
chaieb@22981
   351
  also have "\<dots> = (INum x < INum y)" by simp 
chaieb@22981
   352
  finally show ?thesis by simp
chaieb@22981
   353
qed
chaieb@22981
   354
chaieb@22981
   355
lemma Nle_iff[simp]: assumes nx: "isnormNum x" and ny: "isnormNum y"
chaieb@22981
   356
  shows "(INum x \<le> INum y) = (x \<le>\<^sub>N y)"
chaieb@22981
   357
proof-
chaieb@22981
   358
  have "(INum x \<le> INum y) = (INum (x -\<^sub>N y) \<le> 0)" using nx ny by simp
chaieb@22981
   359
  also have "\<dots> = (0\<ge>\<^sub>N (x -\<^sub>N y))" using Nle0_iff[OF Nsub_normN[OF ny]] by simp
chaieb@22981
   360
  finally show ?thesis by (simp add: Nle_def)
chaieb@22981
   361
qed
chaieb@22981
   362
chaieb@22981
   363
lemma [code]: "INum x \<le> INum y \<longleftrightarrow> normNum x \<le>\<^sub>N normNum y"
chaieb@22981
   364
proof -
chaieb@22981
   365
  have "normNum x \<le>\<^sub>N normNum y \<longleftrightarrow> INum (normNum x) \<le> INum (normNum y)" 
chaieb@22981
   366
    by (simp del: normNum)
chaieb@22981
   367
  also have "\<dots> = (INum x \<le> INum y)" by simp 
chaieb@22981
   368
  finally show ?thesis by simp
chaieb@22981
   369
qed
chaieb@22981
   370
chaieb@22981
   371
lemma Nadd_commute: "x +\<^sub>N y = y +\<^sub>N x"
chaieb@22981
   372
proof-
chaieb@22981
   373
  have n: "isnormNum (x +\<^sub>N y)" "isnormNum (y +\<^sub>N x)" by simp_all
chaieb@22981
   374
  have "INum (x +\<^sub>N y) = INum (y +\<^sub>N x)" by simp
chaieb@22981
   375
  with isnormNum_unique[OF n] show ?thesis by simp
chaieb@22981
   376
qed
chaieb@22981
   377
chaieb@22981
   378
lemma[simp]: "(0, b) +\<^sub>N y = normNum y" "(a, 0) +\<^sub>N y = normNum y" 
chaieb@22981
   379
  "x +\<^sub>N (0, b) = normNum x" "x +\<^sub>N (a, 0) = normNum x"
chaieb@22981
   380
  apply (simp add: Nadd_def split_def, simp add: Nadd_def split_def)
chaieb@22981
   381
  apply (subst Nadd_commute,simp add: Nadd_def split_def)
chaieb@22981
   382
  apply (subst Nadd_commute,simp add: Nadd_def split_def)
chaieb@22981
   383
  done
chaieb@22981
   384
chaieb@22981
   385
lemma normNum_nilpotent_aux[simp]: assumes nx: "isnormNum x" 
chaieb@22981
   386
  shows "normNum x = x"
chaieb@22981
   387
proof-
chaieb@22981
   388
  let ?a = "normNum x"
chaieb@22981
   389
  have n: "isnormNum ?a" by simp
chaieb@22981
   390
  have th:"INum ?a = INum x" by simp
chaieb@22981
   391
  with isnormNum_unique[OF n nx]  
chaieb@22981
   392
  show ?thesis by simp
chaieb@22981
   393
qed
chaieb@22981
   394
chaieb@22981
   395
lemma normNum_nilpotent[simp]: "normNum (normNum x) = normNum x"
chaieb@22981
   396
  by simp
chaieb@22981
   397
lemma normNum0[simp]: "normNum (0,b) = 0\<^sub>N" "normNum (a,0) = 0\<^sub>N"
chaieb@22981
   398
  by (simp_all add: normNum_def)
chaieb@22981
   399
lemma normNum_Nadd: "normNum (x +\<^sub>N y) = x +\<^sub>N y" by simp
chaieb@22981
   400
lemma Nadd_normNum1[simp]: "normNum x +\<^sub>N y = x +\<^sub>N y"
chaieb@22981
   401
proof-
chaieb@22981
   402
  have n: "isnormNum (normNum x +\<^sub>N y)" "isnormNum (x +\<^sub>N y)" by simp_all
chaieb@22981
   403
  have "INum (normNum x +\<^sub>N y) = INum x + INum y" by simp
chaieb@22981
   404
  also have "\<dots> = INum (x +\<^sub>N y)" by simp
chaieb@22981
   405
  finally show ?thesis using isnormNum_unique[OF n] by simp
chaieb@22981
   406
qed
chaieb@22981
   407
lemma Nadd_normNum2[simp]: "x +\<^sub>N normNum y = x +\<^sub>N y"
chaieb@22981
   408
proof-
chaieb@22981
   409
  have n: "isnormNum (x +\<^sub>N normNum y)" "isnormNum (x +\<^sub>N y)" by simp_all
chaieb@22981
   410
  have "INum (x +\<^sub>N normNum y) = INum x + INum y" by simp
chaieb@22981
   411
  also have "\<dots> = INum (x +\<^sub>N y)" by simp
chaieb@22981
   412
  finally show ?thesis using isnormNum_unique[OF n] by simp
chaieb@22981
   413
qed
chaieb@22981
   414
chaieb@22981
   415
lemma Nadd_assoc: "x +\<^sub>N y +\<^sub>N z = x +\<^sub>N (y +\<^sub>N z)"
chaieb@22981
   416
proof-
chaieb@22981
   417
  have n: "isnormNum (x +\<^sub>N y +\<^sub>N z)" "isnormNum (x +\<^sub>N (y +\<^sub>N z))" by simp_all
chaieb@22981
   418
  have "INum (x +\<^sub>N y +\<^sub>N z) = INum (x +\<^sub>N (y +\<^sub>N z))" by simp
chaieb@22981
   419
  with isnormNum_unique[OF n] show ?thesis by simp
chaieb@22981
   420
qed
chaieb@22981
   421
chaieb@22981
   422
lemma Nmul_commute: "isnormNum x \<Longrightarrow> isnormNum y \<Longrightarrow> x *\<^sub>N y = y *\<^sub>N x"
chaieb@22981
   423
  by (simp add: Nmul_def split_def Let_def igcd_commute mult_commute)
chaieb@22981
   424
chaieb@22981
   425
lemma Nmul_assoc: assumes nx: "isnormNum x" and ny:"isnormNum y" and nz:"isnormNum z"
chaieb@22981
   426
  shows "x *\<^sub>N y *\<^sub>N z = x *\<^sub>N (y *\<^sub>N z)"
chaieb@22981
   427
proof-
chaieb@22981
   428
  from nx ny nz have n: "isnormNum (x *\<^sub>N y *\<^sub>N z)" "isnormNum (x *\<^sub>N (y *\<^sub>N z))" 
chaieb@22981
   429
    by simp_all
chaieb@22981
   430
  have "INum (x +\<^sub>N y +\<^sub>N z) = INum (x +\<^sub>N (y +\<^sub>N z))" by simp
chaieb@22981
   431
  with isnormNum_unique[OF n] show ?thesis by simp
chaieb@22981
   432
qed
chaieb@22981
   433
chaieb@22981
   434
lemma Nsub0: assumes x: "isnormNum x" and y:"isnormNum y" shows "(x -\<^sub>N y = 0\<^sub>N) = (x = y)"
chaieb@22981
   435
proof-
chaieb@22981
   436
  from isnormNum_unique[OF Nsub_normN[OF y], where y="0\<^sub>N"] 
chaieb@22981
   437
  have "(x -\<^sub>N y = 0\<^sub>N) = (INum (x -\<^sub>N y) = INum 0\<^sub>N)" by simp
chaieb@22981
   438
  also have "\<dots> = (INum x = INum y)" by simp
chaieb@22981
   439
  also have "\<dots> = (x = y)" using x y by simp
chaieb@22981
   440
  finally show ?thesis .
chaieb@22981
   441
qed
chaieb@22981
   442
lemma Nmul0[simp]: "c *\<^sub>N 0\<^sub>N = 0\<^sub>N" " 0\<^sub>N *\<^sub>N c = 0\<^sub>N"
chaieb@22981
   443
  by (simp_all add: Nmul_def Let_def split_def)
chaieb@22981
   444
chaieb@22981
   445
lemma Nmul_eq0[simp]: assumes nx:"isnormNum x" and ny: "isnormNum y"
chaieb@22981
   446
  shows "(x*\<^sub>N y = 0\<^sub>N) = (x = 0\<^sub>N \<or> y = 0\<^sub>N)"
chaieb@22981
   447
proof-
chaieb@22981
   448
  have " \<exists> a b a' b'. x = (a,b) \<and> y= (a',b')" by auto
chaieb@22981
   449
  then obtain a b a' b' where xy[simp]: "x = (a,b)" "y = (a',b')" by blast
chaieb@22981
   450
  have n0: "isnormNum 0\<^sub>N" by simp
chaieb@22981
   451
  show ?thesis using nx ny 
chaieb@22981
   452
    apply (simp only: isnormNum_unique[OF  Nmul_normN[OF nx ny] n0, symmetric] Nmul)
chaieb@22981
   453
    apply (simp add: INum_def split_def isnormNum_def fst_conv snd_conv)
chaieb@22981
   454
    apply (cases "a=0",simp_all)
chaieb@22981
   455
    apply (cases "a'=0",simp_all)
chaieb@22981
   456
    done 
chaieb@22981
   457
qed
chaieb@22981
   458
lemma Nneg_Nneg[simp]: "~\<^sub>N (~\<^sub>N c) = c"
chaieb@22981
   459
  by (simp add: Nneg_def split_def)
chaieb@22981
   460
chaieb@22981
   461
lemma Nmul1[simp]: 
chaieb@22981
   462
  "isnormNum c \<Longrightarrow> 1\<^sub>N *\<^sub>N c = c" 
chaieb@22981
   463
  "isnormNum c \<Longrightarrow> c *\<^sub>N 1\<^sub>N  = c" 
chaieb@22981
   464
  apply (simp_all add: Nmul_def Let_def split_def isnormNum_def)
chaieb@22981
   465
  by (cases "fst c = 0", simp_all,cases c, simp_all)+
chaieb@22981
   466
chaieb@22981
   467
lemma [code, code unfold]:
chaieb@22981
   468
  "number_of k = real_int (number_of k)"
chaieb@22981
   469
  by (simp add: real_int_def)
chaieb@22981
   470
haftmann@23017
   471
code_modulename SML
haftmann@23017
   472
  RealDef Real
haftmann@23017
   473
  Executable_Real Real
haftmann@23017
   474
haftmann@23017
   475
code_modulename OCaml
haftmann@23017
   476
  RealDef Real
haftmann@23017
   477
  Executable_Real Real
haftmann@23017
   478
haftmann@23017
   479
code_modulename Haskell
haftmann@23017
   480
  RealDef Real
haftmann@23017
   481
  Executable_Real Real
haftmann@23017
   482
nipkow@23030
   483
(* There is already an implementation in RealDef
chaieb@22981
   484
types_code real ("{* int * int *}")
chaieb@22981
   485
attach (term_of) {*
chaieb@22981
   486
fun term_of_real (p, q) =
chaieb@22981
   487
  let 
chaieb@22981
   488
    val rT = HOLogic.realT;
chaieb@22981
   489
in if q = 1
chaieb@22981
   490
  then HOLogic.mk_number rT p
haftmann@22997
   491
  else @{term "op / \<Colon> real \<Rightarrow> real \<Rightarrow> real"} $
chaieb@22981
   492
    HOLogic.mk_number rT p $ HOLogic.mk_number rT q
chaieb@22981
   493
end;
chaieb@22981
   494
*}
chaieb@22981
   495
chaieb@22981
   496
consts_code INum ("")
nipkow@23030
   497
*)
nipkow@23030
   498
end