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