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