src/HOL/Decision_Procs/Rat_Pair.thy
author nipkow
Thu Jun 14 15:45:53 2018 +0200 (10 months ago)
changeset 68442 477b3f7067c9
parent 67123 3fe40ff1b921
child 69597 ff784d5a5bfb
permissions -rw-r--r--
tuned
     1 (*  Title:      HOL/Decision_Procs/Rat_Pair.thy
     2     Author:     Amine Chaieb
     3 *)
     4 
     5 section \<open>Rational numbers as pairs\<close>
     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"
    20   where "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"
    23   where "normNum = (\<lambda>(a,b).
    24     (if a = 0 \<or> b = 0 then (0, 0)
    25      else
    26       (let g = gcd a b
    27        in if b > 0 then (a div g, b div g) else (- (a div g), - (b div g)))))"
    28 
    29 declare gcd_dvd1[presburger] gcd_dvd2[presburger]
    30 
    31 lemma normNum_isnormNum [simp]: "isnormNum (normNum x)"
    32 proof -
    33   obtain a b where x: "x = (a, b)" by (cases x)
    34   consider "a = 0 \<or> b = 0" | "a \<noteq> 0" "b \<noteq> 0"
    35     by blast
    36   then show ?thesis
    37   proof cases
    38     case 1
    39     then show ?thesis
    40       by (simp add: x normNum_def isnormNum_def)
    41   next
    42     case ab: 2
    43     let ?g = "gcd a b"
    44     let ?a' = "a div ?g"
    45     let ?b' = "b div ?g"
    46     let ?g' = "gcd ?a' ?b'"
    47     from ab have "?g \<noteq> 0" by simp
    48     with gcd_ge_0_int[of a b] have gpos: "?g > 0" by arith
    49     have gdvd: "?g dvd a" "?g dvd b" by arith+
    50     from dvd_mult_div_cancel[OF gdvd(1)] dvd_mult_div_cancel[OF gdvd(2)] ab
    51     have nz': "?a' \<noteq> 0" "?b' \<noteq> 0" by - (rule notI, simp)+
    52     from ab have stupid: "a \<noteq> 0 \<or> b \<noteq> 0" by arith
    53     from div_gcd_coprime[OF stupid] have gp1: "?g' = 1"
    54       by simp
    55     from ab consider "b < 0" | "b > 0" by arith
    56     then show ?thesis
    57     proof cases
    58       case b: 1
    59       have False if b': "?b' \<ge> 0"
    60       proof -
    61         from gpos have th: "?g \<ge> 0" by arith
    62         from mult_nonneg_nonneg[OF th b'] dvd_mult_div_cancel[OF gdvd(2)]
    63         show ?thesis using b by arith
    64       qed
    65       then have b': "?b' < 0" by (presburger add: linorder_not_le[symmetric])
    66       from ab(1) nz' b b' gp1 show ?thesis
    67         by (simp add: x isnormNum_def normNum_def Let_def split_def)
    68     next
    69       case b: 2
    70       then have "?b' \<ge> 0"
    71         by (presburger add: pos_imp_zdiv_nonneg_iff[OF gpos])
    72       with nz' have b': "?b' > 0" by arith
    73       from b b' ab(1) nz' gp1 show ?thesis
    74         by (simp add: x isnormNum_def normNum_def Let_def split_def)
    75     qed
    76   qed
    77 qed
    78 
    79 text \<open>Arithmetic over Num\<close>
    80 
    81 definition Nadd :: "Num \<Rightarrow> Num \<Rightarrow> Num"  (infixl "+\<^sub>N" 60)
    82 where
    83   "Nadd = (\<lambda>(a, b) (a', b').
    84     if a = 0 \<or> b = 0 then normNum (a', b')
    85     else if a' = 0 \<or> b' = 0 then normNum (a, b)
    86     else normNum (a * b' + b * a', b * b'))"
    87 
    88 definition Nmul :: "Num \<Rightarrow> Num \<Rightarrow> Num"  (infixl "*\<^sub>N" 60)
    89 where
    90   "Nmul = (\<lambda>(a, b) (a', b').
    91     let g = gcd (a * a') (b * b')
    92     in (a * a' div g, b * b' div g))"
    93 
    94 definition Nneg :: "Num \<Rightarrow> Num" ("~\<^sub>N")
    95   where "Nneg = (\<lambda>(a, b). (- a, b))"
    96 
    97 definition Nsub :: "Num \<Rightarrow> Num \<Rightarrow> Num"  (infixl "-\<^sub>N" 60)
    98   where "Nsub = (\<lambda>a b. a +\<^sub>N ~\<^sub>N b)"
    99 
   100 definition Ninv :: "Num \<Rightarrow> Num"
   101   where "Ninv = (\<lambda>(a, b). if a < 0 then (- b, \<bar>a\<bar>) else (b, a))"
   102 
   103 definition Ndiv :: "Num \<Rightarrow> Num \<Rightarrow> Num"  (infixl "\<div>\<^sub>N" 60)
   104   where "Ndiv = (\<lambda>a b. a *\<^sub>N Ninv b)"
   105 
   106 lemma Nneg_normN[simp]: "isnormNum x \<Longrightarrow> isnormNum (~\<^sub>N x)"
   107   by (simp add: isnormNum_def Nneg_def split_def)
   108 
   109 lemma Nadd_normN[simp]: "isnormNum (x +\<^sub>N y)"
   110   by (simp add: Nadd_def split_def)
   111 
   112 lemma Nsub_normN[simp]: "isnormNum y \<Longrightarrow> isnormNum (x -\<^sub>N y)"
   113   by (simp add: Nsub_def split_def)
   114 
   115 lemma Nmul_normN[simp]:
   116   assumes xn: "isnormNum x"
   117     and yn: "isnormNum y"
   118   shows "isnormNum (x *\<^sub>N y)"
   119 proof -
   120   obtain a b where x: "x = (a, b)" by (cases x)
   121   obtain a' b' where y: "y = (a', b')" by (cases y)
   122   consider "a = 0" | "a' = 0" | "a \<noteq> 0" "a' \<noteq> 0" by blast
   123   then show ?thesis
   124   proof cases
   125     case 1
   126     then show ?thesis
   127       using xn x y by (simp add: isnormNum_def Let_def Nmul_def split_def)
   128   next
   129     case 2
   130     then show ?thesis
   131       using yn x y by (simp add: isnormNum_def Let_def Nmul_def split_def)
   132   next
   133     case aa': 3
   134     then have bp: "b > 0" "b' > 0"
   135       using xn yn x y by (simp_all add: isnormNum_def)
   136     from bp have "x *\<^sub>N y = normNum (a * a', b * b')"
   137       using x y aa' bp by (simp add: Nmul_def Let_def split_def normNum_def)
   138     then show ?thesis by simp
   139   qed
   140 qed
   141 
   142 lemma Ninv_normN[simp]: "isnormNum x \<Longrightarrow> isnormNum (Ninv x)"
   143   apply (simp add: Ninv_def isnormNum_def split_def)
   144   apply (cases "fst x = 0")
   145   apply (auto simp add: gcd.commute)
   146   done
   147 
   148 lemma isnormNum_int[simp]: "isnormNum 0\<^sub>N" "isnormNum ((1::int)\<^sub>N)" "i \<noteq> 0 \<Longrightarrow> isnormNum (i)\<^sub>N"
   149   by (simp_all add: isnormNum_def)
   150 
   151 
   152 text \<open>Relations over Num\<close>
   153 
   154 definition Nlt0:: "Num \<Rightarrow> bool"  ("0>\<^sub>N")
   155   where "Nlt0 = (\<lambda>(a, b). a < 0)"
   156 
   157 definition Nle0:: "Num \<Rightarrow> bool"  ("0\<ge>\<^sub>N")
   158   where "Nle0 = (\<lambda>(a, b). a \<le> 0)"
   159 
   160 definition Ngt0:: "Num \<Rightarrow> bool"  ("0<\<^sub>N")
   161   where "Ngt0 = (\<lambda>(a, b). a > 0)"
   162 
   163 definition Nge0:: "Num \<Rightarrow> bool"  ("0\<le>\<^sub>N")
   164   where "Nge0 = (\<lambda>(a, b). a \<ge> 0)"
   165 
   166 definition Nlt :: "Num \<Rightarrow> Num \<Rightarrow> bool"  (infix "<\<^sub>N" 55)
   167   where "Nlt = (\<lambda>a b. 0>\<^sub>N (a -\<^sub>N b))"
   168 
   169 definition Nle :: "Num \<Rightarrow> Num \<Rightarrow> bool"  (infix "\<le>\<^sub>N" 55)
   170   where "Nle = (\<lambda>a b. 0\<ge>\<^sub>N (a -\<^sub>N b))"
   171 
   172 definition "INum = (\<lambda>(a, b). of_int a / of_int b)"
   173 
   174 lemma INum_int [simp]: "INum (i)\<^sub>N = (of_int i ::'a::field)" "INum 0\<^sub>N = (0::'a::field)"
   175   by (simp_all add: INum_def)
   176 
   177 lemma isnormNum_unique[simp]:
   178   assumes na: "isnormNum x"
   179     and nb: "isnormNum y"
   180   shows "(INum x ::'a::field_char_0) = INum y \<longleftrightarrow> x = y"
   181   (is "?lhs = ?rhs")
   182 proof
   183   obtain a b where x: "x = (a, b)" by (cases x)
   184   obtain a' b' where y: "y = (a', b')" by (cases y)
   185   consider "a = 0 \<or> b = 0 \<or> a' = 0 \<or> b' = 0" | "a \<noteq> 0" "b \<noteq> 0" "a' \<noteq> 0" "b' \<noteq> 0"
   186     by blast
   187   then show ?rhs if H: ?lhs
   188   proof cases
   189     case 1
   190     then show ?thesis
   191       using na nb H by (simp add: x y INum_def split_def isnormNum_def split: if_split_asm)
   192   next
   193     case 2
   194     with na nb have pos: "b > 0" "b' > 0"
   195       by (simp_all add: x y isnormNum_def)
   196     from H \<open>b \<noteq> 0\<close> \<open>b' \<noteq> 0\<close> have eq: "a * b' = a' * b"
   197       by (simp add: x y INum_def eq_divide_eq divide_eq_eq of_int_mult[symmetric] del: of_int_mult)
   198     from \<open>a \<noteq> 0\<close> \<open>a' \<noteq> 0\<close> na nb
   199     have gcd1: "gcd a b = 1" "gcd b a = 1" "gcd a' b' = 1" "gcd b' a' = 1"
   200       by (simp_all add: x y isnormNum_def add: gcd.commute)
   201     then have "coprime a b" "coprime b a" "coprime a' b'" "coprime b' a'"
   202       by (simp_all add: coprime_iff_gcd_eq_1)
   203     from eq have raw_dvd: "a dvd a' * b" "b dvd b' * a" "a' dvd a * b'" "b' dvd b * a'"
   204       apply -
   205       apply algebra
   206       apply algebra
   207       apply simp
   208       apply algebra
   209       done
   210     then have eq1: "b = b'"
   211       using pos \<open>coprime b a\<close> \<open>coprime b' a'\<close>
   212       by (auto simp add: coprime_dvd_mult_left_iff intro: associated_eqI)
   213     with eq have "a = a'" using pos by simp
   214     with \<open>b = b'\<close> show ?thesis by (simp add: x y)
   215   qed
   216   show ?lhs if ?rhs
   217     using that by simp
   218 qed
   219 
   220 lemma isnormNum0[simp]: "isnormNum x \<Longrightarrow> INum x = (0::'a::field_char_0) \<longleftrightarrow> x = 0\<^sub>N"
   221   unfolding INum_int(2)[symmetric]
   222   by (rule isnormNum_unique) simp_all
   223 
   224 lemma of_int_div_aux:
   225   assumes "d \<noteq> 0"
   226   shows "(of_int x ::'a::field_char_0) / of_int d =
   227     of_int (x div d) + (of_int (x mod d)) / of_int d"
   228 proof -
   229   let ?t = "of_int (x div d) * (of_int d ::'a) + of_int (x mod d)"
   230   let ?f = "\<lambda>x. x / of_int d"
   231   have "x = (x div d) * d + x mod d"
   232     by auto
   233   then have eq: "of_int x = ?t"
   234     by (simp only: of_int_mult[symmetric] of_int_add [symmetric])
   235   then have "of_int x / of_int d = ?t / of_int d"
   236     using cong[OF refl[of ?f] eq] by simp
   237   then show ?thesis
   238     by (simp add: add_divide_distrib algebra_simps \<open>d \<noteq> 0\<close>)
   239 qed
   240 
   241 lemma of_int_div:
   242   fixes d :: int
   243   assumes "d \<noteq> 0" "d dvd n"
   244   shows "(of_int (n div d) ::'a::field_char_0) = of_int n / of_int d"
   245   using assms of_int_div_aux [of d n, where ?'a = 'a] by simp
   246 
   247 lemma normNum[simp]: "INum (normNum x) = (INum x :: 'a::field_char_0)"
   248 proof -
   249   obtain a b where x: "x = (a, b)" by (cases x)
   250   consider "a = 0 \<or> b = 0" | "a \<noteq> 0" "b \<noteq> 0" by blast
   251   then show ?thesis
   252   proof cases
   253     case 1
   254     then show ?thesis
   255       by (simp add: x INum_def normNum_def split_def Let_def)
   256   next
   257     case ab: 2
   258     let ?g = "gcd a b"
   259     from ab have g: "?g \<noteq> 0"by simp
   260     from of_int_div[OF g, where ?'a = 'a] show ?thesis
   261       by (auto simp add: x INum_def normNum_def split_def Let_def)
   262   qed
   263 qed
   264 
   265 lemma INum_normNum_iff: "(INum x ::'a::field_char_0) = INum y \<longleftrightarrow> normNum x = normNum y"
   266   (is "?lhs \<longleftrightarrow> _")
   267 proof -
   268   have "normNum x = normNum y \<longleftrightarrow> (INum (normNum x) :: 'a) = INum (normNum y)"
   269     by (simp del: normNum)
   270   also have "\<dots> = ?lhs" by simp
   271   finally show ?thesis by simp
   272 qed
   273 
   274 lemma Nadd[simp]: "INum (x +\<^sub>N y) = INum x + (INum y :: 'a :: field_char_0)"
   275 proof -
   276   let ?z = "0::'a"
   277   obtain a b where x: "x = (a, b)" by (cases x)
   278   obtain a' b' where y: "y = (a', b')" by (cases y)
   279   consider "a = 0 \<or> a'= 0 \<or> b =0 \<or> b' = 0" | "a \<noteq> 0" "a'\<noteq> 0" "b \<noteq> 0" "b' \<noteq> 0"
   280     by blast
   281   then show ?thesis
   282   proof cases
   283     case 1
   284     then show ?thesis
   285       apply (cases "a = 0")
   286       apply (simp_all add: x y Nadd_def)
   287       apply (cases "b = 0")
   288       apply (simp_all add: INum_def)
   289       apply (cases "a'= 0")
   290       apply simp_all
   291       apply (cases "b'= 0")
   292       apply simp_all
   293       done
   294   next
   295     case neq: 2
   296     show ?thesis
   297     proof (cases "a * b' + b * a' = 0")
   298       case True
   299       then have "of_int (a * b' + b * a') / (of_int b * of_int b') = ?z"
   300         by simp
   301       then have "of_int b' * of_int a / (of_int b * of_int b') +
   302           of_int b * of_int a' / (of_int b * of_int b') = ?z"
   303         by (simp add: add_divide_distrib)
   304       then have th: "of_int a / of_int b + of_int a' / of_int b' = ?z"
   305         using neq by simp
   306       from True neq show ?thesis
   307         by (simp add: x y th Nadd_def normNum_def INum_def split_def)
   308     next
   309       case False
   310       let ?g = "gcd (a * b' + b * a') (b * b')"
   311       have gz: "?g \<noteq> 0"
   312         using False by simp
   313       show ?thesis
   314         using neq False gz
   315           of_int_div [where ?'a = 'a, OF gz gcd_dvd1 [of "a * b' + b * a'" "b * b'"]]
   316           of_int_div [where ?'a = 'a, OF gz gcd_dvd2 [of "a * b' + b * a'" "b * b'"]]
   317         by (simp add: x y Nadd_def INum_def normNum_def Let_def) (simp add: field_simps)
   318     qed
   319   qed
   320 qed
   321 
   322 lemma Nmul[simp]: "INum (x *\<^sub>N y) = INum x * (INum y:: 'a::field_char_0)"
   323 proof -
   324   let ?z = "0::'a"
   325   obtain a b where x: "x = (a, b)" by (cases x)
   326   obtain a' b' where y: "y = (a', b')" by (cases y)
   327   consider "a = 0 \<or> a' = 0 \<or> b = 0 \<or> b' = 0" | "a \<noteq> 0" "a' \<noteq> 0" "b \<noteq> 0" "b' \<noteq> 0"
   328     by blast
   329   then show ?thesis
   330   proof cases
   331     case 1
   332     then show ?thesis
   333       by (auto simp add: x y Nmul_def INum_def)
   334   next
   335     case neq: 2
   336     let ?g = "gcd (a * a') (b * b')"
   337     have gz: "?g \<noteq> 0"
   338       using neq by simp
   339     from neq of_int_div [where ?'a = 'a, OF gz gcd_dvd1 [of "a * a'" "b * b'"]]
   340       of_int_div [where ?'a = 'a , OF gz gcd_dvd2 [of "a * a'" "b * b'"]]
   341     show ?thesis
   342       by (simp add: Nmul_def x y Let_def INum_def)
   343   qed
   344 qed
   345 
   346 lemma Nneg[simp]: "INum (~\<^sub>N x) = - (INum x :: 'a::field)"
   347   by (simp add: Nneg_def split_def INum_def)
   348 
   349 lemma Nsub[simp]: "INum (x -\<^sub>N y) = INum x - (INum y:: 'a::field_char_0)"
   350   by (simp add: Nsub_def split_def)
   351 
   352 lemma Ninv[simp]: "INum (Ninv x) = (1 :: 'a::field) / INum x"
   353   by (simp add: Ninv_def INum_def split_def)
   354 
   355 lemma Ndiv[simp]: "INum (x \<div>\<^sub>N y) = INum x / (INum y :: 'a::field_char_0)"
   356   by (simp add: Ndiv_def)
   357 
   358 lemma Nlt0_iff[simp]:
   359   assumes nx: "isnormNum x"
   360   shows "((INum x :: 'a::{field_char_0,linordered_field}) < 0) = 0>\<^sub>N x"
   361 proof -
   362   obtain a b where x: "x = (a, b)" by (cases x)
   363   show ?thesis
   364   proof (cases "a = 0")
   365     case True
   366     then show ?thesis
   367       by (simp add: x Nlt0_def INum_def)
   368   next
   369     case False
   370     then have b: "(of_int b::'a) > 0"
   371       using nx by (simp add: x isnormNum_def)
   372     from pos_divide_less_eq[OF b, where b="of_int a" and a="0::'a"]
   373     show ?thesis
   374       by (simp add: x Nlt0_def INum_def)
   375   qed
   376 qed
   377 
   378 lemma Nle0_iff[simp]:
   379   assumes nx: "isnormNum x"
   380   shows "((INum x :: 'a::{field_char_0,linordered_field}) \<le> 0) = 0\<ge>\<^sub>N x"
   381 proof -
   382   obtain a b where x: "x = (a, b)" by (cases x)
   383   show ?thesis
   384   proof (cases "a = 0")
   385     case True
   386     then show ?thesis
   387       by (simp add: x Nle0_def INum_def)
   388   next
   389     case False
   390     then have b: "(of_int b :: 'a) > 0"
   391       using nx by (simp add: x isnormNum_def)
   392     from pos_divide_le_eq[OF b, where b="of_int a" and a="0::'a"]
   393     show ?thesis
   394       by (simp add: x Nle0_def INum_def)
   395   qed
   396 qed
   397 
   398 lemma Ngt0_iff[simp]:
   399   assumes nx: "isnormNum x"
   400   shows "((INum x :: 'a::{field_char_0,linordered_field}) > 0) = 0<\<^sub>N x"
   401 proof -
   402   obtain a b where x: "x = (a, b)" by (cases x)
   403   show ?thesis
   404   proof (cases "a = 0")
   405     case True
   406     then show ?thesis
   407       by (simp add: x Ngt0_def INum_def)
   408   next
   409     case False
   410     then have b: "(of_int b::'a) > 0"
   411       using nx by (simp add: x isnormNum_def)
   412     from pos_less_divide_eq[OF b, where b="of_int a" and a="0::'a"]
   413     show ?thesis
   414       by (simp add: x Ngt0_def INum_def)
   415   qed
   416 qed
   417 
   418 lemma Nge0_iff[simp]:
   419   assumes nx: "isnormNum x"
   420   shows "(INum x :: 'a::{field_char_0,linordered_field}) \<ge> 0 \<longleftrightarrow> 0\<le>\<^sub>N x"
   421 proof -
   422   obtain a b where x: "x = (a, b)" by (cases x)
   423   show ?thesis
   424   proof (cases "a = 0")
   425     case True
   426     then show ?thesis
   427       by (simp add: x Nge0_def INum_def)
   428   next
   429     case False
   430     then have b: "(of_int b::'a) > 0"
   431       using nx by (simp add: x isnormNum_def)
   432     from pos_le_divide_eq[OF b, where b="of_int a" and a="0::'a"]
   433     show ?thesis
   434       by (simp add: x Nge0_def INum_def)
   435   qed
   436 qed
   437 
   438 lemma Nlt_iff[simp]:
   439   assumes nx: "isnormNum x"
   440     and ny: "isnormNum y"
   441   shows "((INum x :: 'a::{field_char_0,linordered_field}) < INum y) \<longleftrightarrow> x <\<^sub>N y"
   442 proof -
   443   let ?z = "0::'a"
   444   have "((INum x ::'a) < INum y) \<longleftrightarrow> INum (x -\<^sub>N y) < ?z"
   445     using nx ny by simp
   446   also have "\<dots> \<longleftrightarrow> 0>\<^sub>N (x -\<^sub>N y)"
   447     using Nlt0_iff[OF Nsub_normN[OF ny]] by simp
   448   finally show ?thesis
   449     by (simp add: Nlt_def)
   450 qed
   451 
   452 lemma Nle_iff[simp]:
   453   assumes nx: "isnormNum x"
   454     and ny: "isnormNum y"
   455   shows "((INum x :: 'a::{field_char_0,linordered_field}) \<le> INum y) \<longleftrightarrow> x \<le>\<^sub>N y"
   456 proof -
   457   have "((INum x ::'a) \<le> INum y) \<longleftrightarrow> INum (x -\<^sub>N y) \<le> (0::'a)"
   458     using nx ny by simp
   459   also have "\<dots> \<longleftrightarrow> 0\<ge>\<^sub>N (x -\<^sub>N y)"
   460     using Nle0_iff[OF Nsub_normN[OF ny]] by simp
   461   finally show ?thesis
   462     by (simp add: Nle_def)
   463 qed
   464 
   465 lemma Nadd_commute:
   466   assumes "SORT_CONSTRAINT('a::field_char_0)"
   467   shows "x +\<^sub>N y = y +\<^sub>N x"
   468 proof -
   469   have n: "isnormNum (x +\<^sub>N y)" "isnormNum (y +\<^sub>N x)"
   470     by simp_all
   471   have "(INum (x +\<^sub>N y)::'a) = INum (y +\<^sub>N x)"
   472     by simp
   473   with isnormNum_unique[OF n] show ?thesis
   474     by simp
   475 qed
   476 
   477 lemma [simp]:
   478   assumes "SORT_CONSTRAINT('a::field_char_0)"
   479   shows "(0, b) +\<^sub>N y = normNum y"
   480     and "(a, 0) +\<^sub>N y = normNum y"
   481     and "x +\<^sub>N (0, b) = normNum x"
   482     and "x +\<^sub>N (a, 0) = normNum x"
   483   apply (simp add: Nadd_def split_def)
   484   apply (simp add: Nadd_def split_def)
   485   apply (subst Nadd_commute)
   486   apply (simp add: Nadd_def split_def)
   487   apply (subst Nadd_commute)
   488   apply (simp add: Nadd_def split_def)
   489   done
   490 
   491 lemma normNum_nilpotent_aux[simp]:
   492   assumes "SORT_CONSTRAINT('a::field_char_0)"
   493   assumes nx: "isnormNum x"
   494   shows "normNum x = x"
   495 proof -
   496   let ?a = "normNum x"
   497   have n: "isnormNum ?a" by simp
   498   have th: "INum ?a = (INum x ::'a)" by simp
   499   with isnormNum_unique[OF n nx] show ?thesis by simp
   500 qed
   501 
   502 lemma normNum_nilpotent[simp]:
   503   assumes "SORT_CONSTRAINT('a::field_char_0)"
   504   shows "normNum (normNum x) = normNum x"
   505   by simp
   506 
   507 lemma normNum0[simp]: "normNum (0, b) = 0\<^sub>N" "normNum (a, 0) = 0\<^sub>N"
   508   by (simp_all add: normNum_def)
   509 
   510 lemma normNum_Nadd:
   511   assumes "SORT_CONSTRAINT('a::field_char_0)"
   512   shows "normNum (x +\<^sub>N y) = x +\<^sub>N y"
   513   by simp
   514 
   515 lemma Nadd_normNum1[simp]:
   516   assumes "SORT_CONSTRAINT('a::field_char_0)"
   517   shows "normNum x +\<^sub>N y = x +\<^sub>N y"
   518 proof -
   519   have n: "isnormNum (normNum x +\<^sub>N y)" "isnormNum (x +\<^sub>N y)"
   520     by simp_all
   521   have "INum (normNum x +\<^sub>N y) = INum x + (INum y :: 'a)"
   522     by simp
   523   also have "\<dots> = INum (x +\<^sub>N y)"
   524     by simp
   525   finally show ?thesis
   526     using isnormNum_unique[OF n] by simp
   527 qed
   528 
   529 lemma Nadd_normNum2[simp]:
   530   assumes "SORT_CONSTRAINT('a::field_char_0)"
   531   shows "x +\<^sub>N normNum y = x +\<^sub>N y"
   532 proof -
   533   have n: "isnormNum (x +\<^sub>N normNum y)" "isnormNum (x +\<^sub>N y)"
   534     by simp_all
   535   have "INum (x +\<^sub>N normNum y) = INum x + (INum y :: 'a)"
   536     by simp
   537   also have "\<dots> = INum (x +\<^sub>N y)"
   538     by simp
   539   finally show ?thesis
   540     using isnormNum_unique[OF n] by simp
   541 qed
   542 
   543 lemma Nadd_assoc:
   544   assumes "SORT_CONSTRAINT('a::field_char_0)"
   545   shows "x +\<^sub>N y +\<^sub>N z = x +\<^sub>N (y +\<^sub>N z)"
   546 proof -
   547   have n: "isnormNum (x +\<^sub>N y +\<^sub>N z)" "isnormNum (x +\<^sub>N (y +\<^sub>N z))"
   548     by simp_all
   549   have "INum (x +\<^sub>N y +\<^sub>N z) = (INum (x +\<^sub>N (y +\<^sub>N z)) :: 'a)"
   550     by simp
   551   with isnormNum_unique[OF n] show ?thesis
   552     by simp
   553 qed
   554 
   555 lemma Nmul_commute: "isnormNum x \<Longrightarrow> isnormNum y \<Longrightarrow> x *\<^sub>N y = y *\<^sub>N x"
   556   by (simp add: Nmul_def split_def Let_def gcd.commute mult.commute)
   557 
   558 lemma Nmul_assoc:
   559   assumes "SORT_CONSTRAINT('a::field_char_0)"
   560   assumes nx: "isnormNum x"
   561     and ny: "isnormNum y"
   562     and nz: "isnormNum z"
   563   shows "x *\<^sub>N y *\<^sub>N z = x *\<^sub>N (y *\<^sub>N z)"
   564 proof -
   565   from nx ny nz have n: "isnormNum (x *\<^sub>N y *\<^sub>N z)" "isnormNum (x *\<^sub>N (y *\<^sub>N z))"
   566     by simp_all
   567   have "INum (x +\<^sub>N y +\<^sub>N z) = (INum (x +\<^sub>N (y +\<^sub>N z)) :: 'a)"
   568     by simp
   569   with isnormNum_unique[OF n] show ?thesis
   570     by simp
   571 qed
   572 
   573 lemma Nsub0:
   574   assumes "SORT_CONSTRAINT('a::field_char_0)"
   575   assumes x: "isnormNum x"
   576     and y: "isnormNum y"
   577   shows "x -\<^sub>N y = 0\<^sub>N \<longleftrightarrow> x = y"
   578 proof -
   579   from isnormNum_unique[where 'a = 'a, OF Nsub_normN[OF y], where y="0\<^sub>N"]
   580   have "x -\<^sub>N y = 0\<^sub>N \<longleftrightarrow> INum (x -\<^sub>N y) = (INum 0\<^sub>N :: 'a)"
   581     by simp
   582   also have "\<dots> \<longleftrightarrow> INum x = (INum y :: 'a)"
   583     by simp
   584   also have "\<dots> \<longleftrightarrow> x = y"
   585     using x y by simp
   586   finally show ?thesis .
   587 qed
   588 
   589 lemma Nmul0[simp]: "c *\<^sub>N 0\<^sub>N = 0\<^sub>N" " 0\<^sub>N *\<^sub>N c = 0\<^sub>N"
   590   by (simp_all add: Nmul_def Let_def split_def)
   591 
   592 lemma Nmul_eq0[simp]:
   593   assumes "SORT_CONSTRAINT('a::field_char_0)"
   594   assumes nx: "isnormNum x"
   595     and ny: "isnormNum y"
   596   shows "x*\<^sub>N y = 0\<^sub>N \<longleftrightarrow> x = 0\<^sub>N \<or> y = 0\<^sub>N"
   597 proof -
   598   obtain a b where x: "x = (a, b)" by (cases x)
   599   obtain a' b' where y: "y = (a', b')" by (cases y)
   600   have n0: "isnormNum 0\<^sub>N" by simp
   601   show ?thesis using nx ny
   602     apply (simp only: isnormNum_unique[where ?'a = 'a, OF  Nmul_normN[OF nx ny] n0, symmetric]
   603       Nmul[where ?'a = 'a])
   604     apply (simp add: x y INum_def split_def isnormNum_def split: if_split_asm)
   605     done
   606 qed
   607 
   608 lemma Nneg_Nneg[simp]: "~\<^sub>N (~\<^sub>N c) = c"
   609   by (simp add: Nneg_def split_def)
   610 
   611 lemma Nmul1[simp]: "isnormNum c \<Longrightarrow> (1)\<^sub>N *\<^sub>N c = c" "isnormNum c \<Longrightarrow> c *\<^sub>N (1)\<^sub>N = c"
   612   apply (simp_all add: Nmul_def Let_def split_def isnormNum_def)
   613   apply (cases "fst c = 0", simp_all, cases c, simp_all)+
   614   done
   615 
   616 end