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