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