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