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