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