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