src/HOL/Library/Abstract_Rat.thy
author wenzelm
Thu Oct 16 22:44:24 2008 +0200 (2008-10-16)
changeset 28615 4c8fa015ec7f
parent 27668 6eb20b2cecf8
child 29667 53103fc8ffa3
permissions -rw-r--r--
explicit SORT_CONSTRAINT for proofs depending implicitly on certain sorts;
     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 Plain GCD
    10 begin
    11 
    12 types Num = "int \<times> int"
    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)"
    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> zgcd 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 = zgcd a b 
    32    in if b > 0 then (a div g, b div g) else (- (a div g), - (b div g)))))"
    33 
    34 declare zgcd_zdvd1[presburger] 
    35 declare zgcd_zdvd2[presburger]
    36 lemma normNum_isnormNum [simp]: "isnormNum (normNum x)"
    37 proof -
    38   have " \<exists> a b. x = (a,b)" by auto
    39   then obtain a b where x[simp]: "x = (a,b)" by blast
    40   {assume "a=0 \<or> b = 0" hence ?thesis by (simp add: normNum_def isnormNum_def)}  
    41   moreover
    42   {assume anz: "a \<noteq> 0" and bnz: "b \<noteq> 0" 
    43     let ?g = "zgcd a b"
    44     let ?a' = "a div ?g"
    45     let ?b' = "b div ?g"
    46     let ?g' = "zgcd ?a' ?b'"
    47     from anz bnz have "?g \<noteq> 0" by simp  with zgcd_pos[of a b] 
    48     have gpos: "?g > 0"  by arith
    49     have gdvd: "?g dvd a" "?g dvd b" by arith+ 
    50     from zdvd_mult_div_cancel[OF gdvd(1)] zdvd_mult_div_cancel[OF gdvd(2)]
    51     anz bnz
    52     have nz':"?a' \<noteq> 0" "?b' \<noteq> 0" 
    53       by - (rule notI,simp add:zgcd_def)+
    54     from anz bnz have stupid: "a \<noteq> 0 \<or> b \<noteq> 0" by arith 
    55     from div_zgcd_relprime[OF stupid] have gp1: "?g' = 1" .
    56     from bnz have "b < 0 \<or> b > 0" by arith
    57     moreover
    58     {assume b: "b > 0"
    59       from b have "?b' \<ge> 0" 
    60 	by (presburger add: pos_imp_zdiv_nonneg_iff[OF gpos])  
    61       with nz' have b': "?b' > 0" by arith 
    62       from b b' anz bnz nz' gp1 have ?thesis 
    63 	by (simp add: isnormNum_def normNum_def Let_def split_def fst_conv snd_conv)}
    64     moreover {assume b: "b < 0"
    65       {assume b': "?b' \<ge> 0" 
    66 	from gpos have th: "?g \<ge> 0" by arith
    67 	from mult_nonneg_nonneg[OF th b'] zdvd_mult_div_cancel[OF gdvd(2)]
    68 	have False using b by arith }
    69       hence b': "?b' < 0" by (presburger add: linorder_not_le[symmetric]) 
    70       from anz bnz nz' b b' gp1 have ?thesis 
    71 	by (simp add: isnormNum_def normNum_def Let_def split_def fst_conv snd_conv)}
    72     ultimately have ?thesis by blast
    73   }
    74   ultimately show ?thesis by blast
    75 qed
    76 
    77 text {* Arithmetic over Num *}
    78 
    79 definition
    80   Nadd :: "Num \<Rightarrow> Num \<Rightarrow> Num" (infixl "+\<^sub>N" 60)
    81 where
    82   "Nadd = (\<lambda>(a,b) (a',b'). if a = 0 \<or> b = 0 then normNum(a',b') 
    83     else if a'=0 \<or> b' = 0 then normNum(a,b) 
    84     else normNum(a*b' + b*a', b*b'))"
    85 
    86 definition
    87   Nmul :: "Num \<Rightarrow> Num \<Rightarrow> Num" (infixl "*\<^sub>N" 60)
    88 where
    89   "Nmul = (\<lambda>(a,b) (a',b'). let g = zgcd (a*a') (b*b') 
    90     in (a*a' div g, b*b' div g))"
    91 
    92 definition
    93   Nneg :: "Num \<Rightarrow> Num" ("~\<^sub>N")
    94 where
    95   "Nneg \<equiv> (\<lambda>(a,b). (-a,b))"
    96 
    97 definition
    98   Nsub :: "Num \<Rightarrow> Num \<Rightarrow> Num" (infixl "-\<^sub>N" 60)
    99 where
   100   "Nsub = (\<lambda>a b. a +\<^sub>N ~\<^sub>N b)"
   101 
   102 definition
   103   Ninv :: "Num \<Rightarrow> Num" 
   104 where
   105   "Ninv \<equiv> \<lambda>(a,b). if a < 0 then (-b, \<bar>a\<bar>) else (b,a)"
   106 
   107 definition
   108   Ndiv :: "Num \<Rightarrow> Num \<Rightarrow> Num" (infixl "\<div>\<^sub>N" 60)
   109 where
   110   "Ndiv \<equiv> \<lambda>a b. a *\<^sub>N Ninv b"
   111 
   112 lemma Nneg_normN[simp]: "isnormNum x \<Longrightarrow> isnormNum (~\<^sub>N x)"
   113   by(simp add: isnormNum_def Nneg_def split_def)
   114 lemma Nadd_normN[simp]: "isnormNum (x +\<^sub>N y)"
   115   by (simp add: Nadd_def split_def)
   116 lemma Nsub_normN[simp]: "\<lbrakk> isnormNum y\<rbrakk> \<Longrightarrow> isnormNum (x -\<^sub>N y)"
   117   by (simp add: Nsub_def split_def)
   118 lemma Nmul_normN[simp]: assumes xn:"isnormNum x" and yn: "isnormNum y"
   119   shows "isnormNum (x *\<^sub>N y)"
   120 proof-
   121   have "\<exists>a b. x = (a,b)" and "\<exists> a' b'. y = (a',b')" by auto
   122   then obtain a b a' b' where ab: "x = (a,b)"  and ab': "y = (a',b')" by blast 
   123   {assume "a = 0"
   124     hence ?thesis using xn ab ab'
   125       by (simp add: zgcd_def isnormNum_def Let_def Nmul_def split_def)}
   126   moreover
   127   {assume "a' = 0"
   128     hence ?thesis using yn ab ab' 
   129       by (simp add: zgcd_def isnormNum_def Let_def Nmul_def split_def)}
   130   moreover
   131   {assume a: "a \<noteq>0" and a': "a'\<noteq>0"
   132     hence bp: "b > 0" "b' > 0" using xn yn ab ab' by (simp_all add: isnormNum_def)
   133     from mult_pos_pos[OF bp] have "x *\<^sub>N y = normNum (a*a', b*b')" 
   134       using ab ab' a a' bp by (simp add: Nmul_def Let_def split_def normNum_def)
   135     hence ?thesis by simp}
   136   ultimately show ?thesis by blast
   137 qed
   138 
   139 lemma Ninv_normN[simp]: "isnormNum x \<Longrightarrow> isnormNum (Ninv x)"
   140   by (simp add: Ninv_def isnormNum_def split_def)
   141     (cases "fst x = 0", auto simp add: zgcd_commute)
   142 
   143 lemma isnormNum_int[simp]: 
   144   "isnormNum 0\<^sub>N" "isnormNum (1::int)\<^sub>N" "i \<noteq> 0 \<Longrightarrow> isnormNum i\<^sub>N"
   145   by (simp_all add: isnormNum_def zgcd_def)
   146 
   147 
   148 text {* Relations over Num *}
   149 
   150 definition
   151   Nlt0:: "Num \<Rightarrow> bool" ("0>\<^sub>N")
   152 where
   153   "Nlt0 = (\<lambda>(a,b). a < 0)"
   154 
   155 definition
   156   Nle0:: "Num \<Rightarrow> bool" ("0\<ge>\<^sub>N")
   157 where
   158   "Nle0 = (\<lambda>(a,b). a \<le> 0)"
   159 
   160 definition
   161   Ngt0:: "Num \<Rightarrow> bool" ("0<\<^sub>N")
   162 where
   163   "Ngt0 = (\<lambda>(a,b). a > 0)"
   164 
   165 definition
   166   Nge0:: "Num \<Rightarrow> bool" ("0\<le>\<^sub>N")
   167 where
   168   "Nge0 = (\<lambda>(a,b). a \<ge> 0)"
   169 
   170 definition
   171   Nlt :: "Num \<Rightarrow> Num \<Rightarrow> bool" (infix "<\<^sub>N" 55)
   172 where
   173   "Nlt = (\<lambda>a b. 0>\<^sub>N (a -\<^sub>N b))"
   174 
   175 definition
   176   Nle :: "Num \<Rightarrow> Num \<Rightarrow> bool" (infix "\<le>\<^sub>N" 55)
   177 where
   178   "Nle = (\<lambda>a b. 0\<ge>\<^sub>N (a -\<^sub>N b))"
   179 
   180 definition
   181   "INum = (\<lambda>(a,b). of_int a / of_int b)"
   182 
   183 lemma INum_int [simp]: "INum i\<^sub>N = ((of_int i) ::'a::field)" "INum 0\<^sub>N = (0::'a::field)"
   184   by (simp_all add: INum_def)
   185 
   186 lemma isnormNum_unique[simp]: 
   187   assumes na: "isnormNum x" and nb: "isnormNum y" 
   188   shows "((INum x ::'a::{ring_char_0,field, division_by_zero}) = INum y) = (x = y)" (is "?lhs = ?rhs")
   189 proof
   190   have "\<exists> a b a' b'. x = (a,b) \<and> y = (a',b')" by auto
   191   then obtain a b a' b' where xy[simp]: "x = (a,b)" "y=(a',b')" by blast
   192   assume H: ?lhs 
   193   {assume "a = 0 \<or> b = 0 \<or> a' = 0 \<or> b' = 0" hence ?rhs
   194       using na nb H
   195       apply (simp add: INum_def split_def isnormNum_def)
   196       apply (cases "a = 0", simp_all)
   197       apply (cases "b = 0", simp_all)
   198       apply (cases "a' = 0", simp_all)
   199       apply (cases "a' = 0", simp_all add: of_int_eq_0_iff)
   200       done}
   201   moreover
   202   { assume az: "a \<noteq> 0" and bz: "b \<noteq> 0" and a'z: "a'\<noteq>0" and b'z: "b'\<noteq>0"
   203     from az bz a'z b'z na nb have pos: "b > 0" "b' > 0" by (simp_all add: isnormNum_def)
   204     from prems have eq:"a * b' = a'*b" 
   205       by (simp add: INum_def  eq_divide_eq divide_eq_eq of_int_mult[symmetric] del: of_int_mult)
   206     from prems have gcd1: "zgcd a b = 1" "zgcd b a = 1" "zgcd a' b' = 1" "zgcd b' a' = 1"       
   207       by (simp_all add: isnormNum_def add: zgcd_commute)
   208     from eq have raw_dvd: "a dvd a'*b" "b dvd b'*a" "a' dvd a*b'" "b' dvd b*a'"
   209       apply - 
   210       apply algebra
   211       apply algebra
   212       apply simp
   213       apply algebra
   214       done
   215     from zdvd_dvd_eq[OF bz zrelprime_dvd_mult[OF gcd1(2) raw_dvd(2)]
   216       zrelprime_dvd_mult[OF gcd1(4) raw_dvd(4)]]
   217       have eq1: "b = b'" using pos by arith  
   218       with eq have "a = a'" using pos by simp
   219       with eq1 have ?rhs by simp}
   220   ultimately show ?rhs by blast
   221 next
   222   assume ?rhs thus ?lhs by simp
   223 qed
   224 
   225 
   226 lemma isnormNum0[simp]: "isnormNum x \<Longrightarrow> (INum x = (0::'a::{ring_char_0, field,division_by_zero})) = (x = 0\<^sub>N)"
   227   unfolding INum_int(2)[symmetric]
   228   by (rule isnormNum_unique, simp_all)
   229 
   230 lemma of_int_div_aux: "d ~= 0 ==> ((of_int x)::'a::{field, ring_char_0}) / (of_int d) = 
   231     of_int (x div d) + (of_int (x mod d)) / ((of_int d)::'a)"
   232 proof -
   233   assume "d ~= 0"
   234   hence dz: "of_int d \<noteq> (0::'a)" by (simp add: of_int_eq_0_iff)
   235   let ?t = "of_int (x div d) * ((of_int d)::'a) + of_int(x mod d)"
   236   let ?f = "\<lambda>x. x / of_int d"
   237   have "x = (x div d) * d + x mod d"
   238     by auto
   239   then have eq: "of_int x = ?t"
   240     by (simp only: of_int_mult[symmetric] of_int_add [symmetric])
   241   then have "of_int x / of_int d = ?t / of_int d" 
   242     using cong[OF refl[of ?f] eq] by simp
   243   then show ?thesis by (simp add: add_divide_distrib ring_simps prems)
   244 qed
   245 
   246 lemma of_int_div: "(d::int) ~= 0 ==> d dvd n ==>
   247     (of_int(n div d)::'a::{field, ring_char_0}) = of_int n / of_int d"
   248   apply (frule of_int_div_aux [of d n, where ?'a = 'a])
   249   apply simp
   250   apply (simp add: zdvd_iff_zmod_eq_0)
   251 done
   252 
   253 
   254 lemma normNum[simp]: "INum (normNum x) = (INum x :: 'a::{ring_char_0,field, division_by_zero})"
   255 proof-
   256   have "\<exists> a b. x = (a,b)" by auto
   257   then obtain a b where x[simp]: "x = (a,b)" by blast
   258   {assume "a=0 \<or> b = 0" hence ?thesis
   259       by (simp add: INum_def normNum_def split_def Let_def)}
   260   moreover 
   261   {assume a: "a\<noteq>0" and b: "b\<noteq>0"
   262     let ?g = "zgcd a b"
   263     from a b have g: "?g \<noteq> 0"by simp
   264     from of_int_div[OF g, where ?'a = 'a]
   265     have ?thesis by (auto simp add: INum_def normNum_def split_def Let_def)}
   266   ultimately show ?thesis by blast
   267 qed
   268 
   269 lemma INum_normNum_iff: "(INum x ::'a::{field, division_by_zero, ring_char_0}) = INum y \<longleftrightarrow> normNum x = normNum y" (is "?lhs = ?rhs")
   270 proof -
   271   have "normNum x = normNum y \<longleftrightarrow> (INum (normNum x) :: 'a) = INum (normNum y)"
   272     by (simp del: normNum)
   273   also have "\<dots> = ?lhs" by simp
   274   finally show ?thesis by simp
   275 qed
   276 
   277 lemma Nadd[simp]: "INum (x +\<^sub>N y) = INum x + (INum y :: 'a :: {ring_char_0,division_by_zero,field})"
   278 proof-
   279 let ?z = "0:: 'a"
   280   have " \<exists> a b. x = (a,b)" " \<exists> a' b'. y = (a',b')" by auto
   281   then obtain a b a' b' where x[simp]: "x = (a,b)" 
   282     and y[simp]: "y = (a',b')" by blast
   283   {assume "a=0 \<or> a'= 0 \<or> b =0 \<or> b' = 0" hence ?thesis 
   284       apply (cases "a=0",simp_all add: Nadd_def)
   285       apply (cases "b= 0",simp_all add: INum_def)
   286        apply (cases "a'= 0",simp_all)
   287        apply (cases "b'= 0",simp_all)
   288        done }
   289   moreover 
   290   {assume aa':"a \<noteq> 0" "a'\<noteq> 0" and bb': "b \<noteq> 0" "b' \<noteq> 0" 
   291     {assume z: "a * b' + b * a' = 0"
   292       hence "of_int (a*b' + b*a') / (of_int b* of_int b') = ?z" by simp
   293       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) 
   294       hence th: "of_int a / of_int b + of_int a' / of_int b' = ?z" using bb' aa' by simp 
   295       from z aa' bb' have ?thesis 
   296 	by (simp add: th Nadd_def normNum_def INum_def split_def)}
   297     moreover {assume z: "a * b' + b * a' \<noteq> 0"
   298       let ?g = "zgcd (a * b' + b * a') (b*b')"
   299       have gz: "?g \<noteq> 0" using z by simp
   300       have ?thesis using aa' bb' z gz
   301 	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,
   302 	OF gz zgcd_zdvd2[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="zgcd (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 zgcd_zdvd1[where i="a*a'" and j="b*b'"]] 
   324       of_int_div[where ?'a = 'a , OF gz zgcd_zdvd2[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:
   408   assumes "SORT_CONSTRAINT('a::{ring_char_0,division_by_zero,field})"
   409   shows "x +\<^sub>N y = y +\<^sub>N x"
   410 proof-
   411   have n: "isnormNum (x +\<^sub>N y)" "isnormNum (y +\<^sub>N x)" by simp_all
   412   have "(INum (x +\<^sub>N y)::'a) = INum (y +\<^sub>N x)" by simp
   413   with isnormNum_unique[OF n] show ?thesis by simp
   414 qed
   415 
   416 lemma [simp]:
   417   assumes "SORT_CONSTRAINT('a::{ring_char_0,division_by_zero,field})"
   418   shows "(0, b) +\<^sub>N y = normNum y"
   419     and "(a, 0) +\<^sub>N y = normNum y" 
   420     and "x +\<^sub>N (0, b) = normNum x"
   421     and "x +\<^sub>N (a, 0) = normNum x"
   422   apply (simp add: Nadd_def split_def)
   423   apply (simp add: Nadd_def split_def)
   424   apply (subst Nadd_commute, simp add: Nadd_def split_def)
   425   apply (subst Nadd_commute, simp add: Nadd_def split_def)
   426   done
   427 
   428 lemma normNum_nilpotent_aux[simp]:
   429   assumes "SORT_CONSTRAINT('a::{ring_char_0,division_by_zero,field})"
   430   assumes nx: "isnormNum x" 
   431   shows "normNum x = x"
   432 proof-
   433   let ?a = "normNum x"
   434   have n: "isnormNum ?a" by simp
   435   have th:"INum ?a = (INum x ::'a)" by simp
   436   with isnormNum_unique[OF n nx]  
   437   show ?thesis by simp
   438 qed
   439 
   440 lemma normNum_nilpotent[simp]:
   441   assumes "SORT_CONSTRAINT('a::{ring_char_0,division_by_zero,field})"
   442   shows "normNum (normNum x) = normNum x"
   443   by simp
   444 
   445 lemma normNum0[simp]: "normNum (0,b) = 0\<^sub>N" "normNum (a,0) = 0\<^sub>N"
   446   by (simp_all add: normNum_def)
   447 
   448 lemma normNum_Nadd:
   449   assumes "SORT_CONSTRAINT('a::{ring_char_0,division_by_zero,field})"
   450   shows "normNum (x +\<^sub>N y) = x +\<^sub>N y" by simp
   451 
   452 lemma Nadd_normNum1[simp]:
   453   assumes "SORT_CONSTRAINT('a::{ring_char_0,division_by_zero,field})"
   454   shows "normNum x +\<^sub>N y = x +\<^sub>N y"
   455 proof-
   456   have n: "isnormNum (normNum x +\<^sub>N y)" "isnormNum (x +\<^sub>N y)" by simp_all
   457   have "INum (normNum x +\<^sub>N y) = INum x + (INum y :: 'a)" by simp
   458   also have "\<dots> = INum (x +\<^sub>N y)" by simp
   459   finally show ?thesis using isnormNum_unique[OF n] by simp
   460 qed
   461 
   462 lemma Nadd_normNum2[simp]:
   463   assumes "SORT_CONSTRAINT('a::{ring_char_0,division_by_zero,field})"
   464   shows "x +\<^sub>N normNum y = x +\<^sub>N y"
   465 proof-
   466   have n: "isnormNum (x +\<^sub>N normNum y)" "isnormNum (x +\<^sub>N y)" by simp_all
   467   have "INum (x +\<^sub>N normNum y) = INum x + (INum y :: 'a)" by simp
   468   also have "\<dots> = INum (x +\<^sub>N y)" by simp
   469   finally show ?thesis using isnormNum_unique[OF n] by simp
   470 qed
   471 
   472 lemma Nadd_assoc:
   473   assumes "SORT_CONSTRAINT('a::{ring_char_0,division_by_zero,field})"
   474   shows "x +\<^sub>N y +\<^sub>N z = x +\<^sub>N (y +\<^sub>N z)"
   475 proof-
   476   have n: "isnormNum (x +\<^sub>N y +\<^sub>N z)" "isnormNum (x +\<^sub>N (y +\<^sub>N z))" by simp_all
   477   have "INum (x +\<^sub>N y +\<^sub>N z) = (INum (x +\<^sub>N (y +\<^sub>N z)) :: 'a)" by simp
   478   with isnormNum_unique[OF n] show ?thesis by simp
   479 qed
   480 
   481 lemma Nmul_commute: "isnormNum x \<Longrightarrow> isnormNum y \<Longrightarrow> x *\<^sub>N y = y *\<^sub>N x"
   482   by (simp add: Nmul_def split_def Let_def zgcd_commute mult_commute)
   483 
   484 lemma Nmul_assoc:
   485   assumes "SORT_CONSTRAINT('a::{ring_char_0,division_by_zero,field})"
   486   assumes nx: "isnormNum x" and ny:"isnormNum y" and nz:"isnormNum z"
   487   shows "x *\<^sub>N y *\<^sub>N z = x *\<^sub>N (y *\<^sub>N z)"
   488 proof-
   489   from nx ny nz have n: "isnormNum (x *\<^sub>N y *\<^sub>N z)" "isnormNum (x *\<^sub>N (y *\<^sub>N z))" 
   490     by simp_all
   491   have "INum (x +\<^sub>N y +\<^sub>N z) = (INum (x +\<^sub>N (y +\<^sub>N z)) :: 'a)" by simp
   492   with isnormNum_unique[OF n] show ?thesis by simp
   493 qed
   494 
   495 lemma Nsub0:
   496   assumes "SORT_CONSTRAINT('a::{ring_char_0,division_by_zero,field})"
   497   assumes x: "isnormNum x" and y:"isnormNum y" shows "(x -\<^sub>N y = 0\<^sub>N) = (x = y)"
   498 proof-
   499   { fix h :: 'a
   500     from isnormNum_unique[where 'a = 'a, OF Nsub_normN[OF y], where y="0\<^sub>N"] 
   501     have "(x -\<^sub>N y = 0\<^sub>N) = (INum (x -\<^sub>N y) = (INum 0\<^sub>N :: 'a)) " by simp
   502     also have "\<dots> = (INum x = (INum y :: 'a))" by simp
   503     also have "\<dots> = (x = y)" using x y by simp
   504     finally show ?thesis . }
   505 qed
   506 
   507 lemma Nmul0[simp]: "c *\<^sub>N 0\<^sub>N = 0\<^sub>N" " 0\<^sub>N *\<^sub>N c = 0\<^sub>N"
   508   by (simp_all add: Nmul_def Let_def split_def)
   509 
   510 lemma Nmul_eq0[simp]:
   511   assumes "SORT_CONSTRAINT('a::{ring_char_0,division_by_zero,field})"
   512   assumes nx:"isnormNum x" and ny: "isnormNum y"
   513   shows "(x*\<^sub>N y = 0\<^sub>N) = (x = 0\<^sub>N \<or> y = 0\<^sub>N)"
   514 proof-
   515   { fix h :: 'a
   516     have " \<exists> a b a' b'. x = (a,b) \<and> y= (a',b')" by auto
   517     then obtain a b a' b' where xy[simp]: "x = (a,b)" "y = (a',b')" by blast
   518     have n0: "isnormNum 0\<^sub>N" by simp
   519     show ?thesis using nx ny 
   520       apply (simp only: isnormNum_unique[where ?'a = 'a, OF  Nmul_normN[OF nx ny] n0, symmetric] Nmul[where ?'a = 'a])
   521       apply (simp add: INum_def split_def isnormNum_def fst_conv snd_conv)
   522       apply (cases "a=0",simp_all)
   523       apply (cases "a'=0",simp_all)
   524       done
   525   }
   526 qed
   527 lemma Nneg_Nneg[simp]: "~\<^sub>N (~\<^sub>N c) = c"
   528   by (simp add: Nneg_def split_def)
   529 
   530 lemma Nmul1[simp]: 
   531   "isnormNum c \<Longrightarrow> 1\<^sub>N *\<^sub>N c = c" 
   532   "isnormNum c \<Longrightarrow> c *\<^sub>N 1\<^sub>N  = c" 
   533   apply (simp_all add: Nmul_def Let_def split_def isnormNum_def)
   534   apply (cases "fst c = 0", simp_all, cases c, simp_all)+
   535   done
   536 
   537 end