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