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