src/HOL/Rat.thy
author wenzelm
Fri Jul 15 11:07:51 2016 +0200 (2016-07-15)
changeset 63494 ac0a3b9c6dae
parent 63326 9d2470571719
child 63502 e3d7dc9f7452
permissions -rw-r--r--
misc tuning and modernization;
wenzelm@63494
     1
(*  Title:      HOL/Rat.thy
wenzelm@63494
     2
    Author:     Markus Wenzel, TU Muenchen
paulson@14365
     3
*)
paulson@14365
     4
wenzelm@60758
     5
section \<open>Rational numbers\<close>
paulson@14365
     6
haftmann@35372
     7
theory Rat
wenzelm@63494
     8
  imports GCD Archimedean_Field
nipkow@15131
     9
begin
paulson@14365
    10
wenzelm@60758
    11
subsection \<open>Rational numbers as quotient\<close>
paulson@14365
    12
wenzelm@60758
    13
subsubsection \<open>Construction of the type of rational numbers\<close>
huffman@18913
    14
wenzelm@63326
    15
definition ratrel :: "(int \<times> int) \<Rightarrow> (int \<times> int) \<Rightarrow> bool"
wenzelm@63326
    16
  where "ratrel = (\<lambda>x y. snd x \<noteq> 0 \<and> snd y \<noteq> 0 \<and> fst x * snd y = fst y * snd x)"
paulson@14365
    17
wenzelm@63326
    18
lemma ratrel_iff [simp]: "ratrel x y \<longleftrightarrow> snd x \<noteq> 0 \<and> snd y \<noteq> 0 \<and> fst x * snd y = fst y * snd x"
haftmann@27551
    19
  by (simp add: ratrel_def)
paulson@14365
    20
huffman@47906
    21
lemma exists_ratrel_refl: "\<exists>x. ratrel x x"
huffman@47906
    22
  by (auto intro!: one_neq_zero)
huffman@18913
    23
huffman@47906
    24
lemma symp_ratrel: "symp ratrel"
huffman@47906
    25
  by (simp add: ratrel_def symp_def)
paulson@14365
    26
huffman@47906
    27
lemma transp_ratrel: "transp ratrel"
huffman@47906
    28
proof (rule transpI, unfold split_paired_all)
haftmann@27551
    29
  fix a b a' b' a'' b'' :: int
wenzelm@63494
    30
  assume *: "ratrel (a, b) (a', b')"
wenzelm@63494
    31
  assume **: "ratrel (a', b') (a'', b'')"
haftmann@27551
    32
  have "b' * (a * b'') = b'' * (a * b')" by simp
wenzelm@63494
    33
  also from * have "a * b' = a' * b" by auto
haftmann@27551
    34
  also have "b'' * (a' * b) = b * (a' * b'')" by simp
wenzelm@63494
    35
  also from ** have "a' * b'' = a'' * b'" by auto
haftmann@27551
    36
  also have "b * (a'' * b') = b' * (a'' * b)" by simp
haftmann@27551
    37
  finally have "b' * (a * b'') = b' * (a'' * b)" .
wenzelm@63494
    38
  moreover from ** have "b' \<noteq> 0" by auto
haftmann@27551
    39
  ultimately have "a * b'' = a'' * b" by simp
wenzelm@63494
    40
  with * ** show "ratrel (a, b) (a'', b'')" by auto
haftmann@27551
    41
qed
haftmann@27551
    42
huffman@47906
    43
lemma part_equivp_ratrel: "part_equivp ratrel"
huffman@47906
    44
  by (rule part_equivpI [OF exists_ratrel_refl symp_ratrel transp_ratrel])
huffman@47906
    45
huffman@47906
    46
quotient_type rat = "int \<times> int" / partial: "ratrel"
huffman@47906
    47
  morphisms Rep_Rat Abs_Rat
huffman@47906
    48
  by (rule part_equivp_ratrel)
haftmann@27551
    49
kuncar@53012
    50
lemma Domainp_cr_rat [transfer_domain_rule]: "Domainp pcr_rat = (\<lambda>x. snd x \<noteq> 0)"
wenzelm@63326
    51
  by (simp add: rat.domain_eq)
wenzelm@63326
    52
haftmann@27551
    53
wenzelm@60758
    54
subsubsection \<open>Representation and basic operations\<close>
haftmann@27551
    55
huffman@47906
    56
lift_definition Fract :: "int \<Rightarrow> int \<Rightarrow> rat"
huffman@47906
    57
  is "\<lambda>a b. if b = 0 then (0, 1) else (a, b)"
huffman@47906
    58
  by simp
haftmann@27551
    59
haftmann@27551
    60
lemma eq_rat:
wenzelm@63326
    61
  "\<And>a b c d. b \<noteq> 0 \<Longrightarrow> d \<noteq> 0 \<Longrightarrow> Fract a b = Fract c d \<longleftrightarrow> a * d = c * b"
wenzelm@63326
    62
  "\<And>a. Fract a 0 = Fract 0 1"
wenzelm@63326
    63
  "\<And>a c. Fract 0 a = Fract 0 c"
huffman@47906
    64
  by (transfer, simp)+
haftmann@27551
    65
haftmann@35369
    66
lemma Rat_cases [case_names Fract, cases type: rat]:
wenzelm@63326
    67
  assumes that: "\<And>a b. q = Fract a b \<Longrightarrow> b > 0 \<Longrightarrow> coprime a b \<Longrightarrow> C"
haftmann@35369
    68
  shows C
haftmann@35369
    69
proof -
wenzelm@63326
    70
  obtain a b :: int where q: "q = Fract a b" and b: "b \<noteq> 0"
huffman@47906
    71
    by transfer simp
haftmann@35369
    72
  let ?a = "a div gcd a b"
haftmann@35369
    73
  let ?b = "b div gcd a b"
wenzelm@63326
    74
  from b have "?b * gcd a b = b"
haftmann@58834
    75
    by simp
wenzelm@63326
    76
  with b have "?b \<noteq> 0"
wenzelm@63326
    77
    by fastforce
wenzelm@63326
    78
  with q b have q2: "q = Fract ?a ?b"
haftmann@57512
    79
    by (simp add: eq_rat dvd_div_mult mult.commute [of a])
wenzelm@63326
    80
  from b have coprime: "coprime ?a ?b"
haftmann@62348
    81
    by (auto intro: div_gcd_coprime)
wenzelm@63326
    82
  show C
wenzelm@63326
    83
  proof (cases "b > 0")
haftmann@35369
    84
    case True
wenzelm@63326
    85
    then have "?b > 0"
wenzelm@63326
    86
      by (simp add: nonneg1_imp_zdiv_pos_iff)
wenzelm@63326
    87
    from q2 this coprime show C by (rule that)
haftmann@35369
    88
  next
haftmann@35369
    89
    case False
wenzelm@63326
    90
    have "q = Fract (- ?a) (- ?b)"
wenzelm@63326
    91
      unfolding q2 by transfer simp
wenzelm@63326
    92
    moreover from False b have "- ?b > 0"
wenzelm@63326
    93
      by (simp add: pos_imp_zdiv_neg_iff)
wenzelm@63326
    94
    moreover from coprime have "coprime (- ?a) (- ?b)"
wenzelm@63326
    95
      by simp
wenzelm@63326
    96
    ultimately show C
wenzelm@63326
    97
      by (rule that)
haftmann@35369
    98
  qed
haftmann@35369
    99
qed
haftmann@35369
   100
haftmann@35369
   101
lemma Rat_induct [case_names Fract, induct type: rat]:
haftmann@35369
   102
  assumes "\<And>a b. b > 0 \<Longrightarrow> coprime a b \<Longrightarrow> P (Fract a b)"
haftmann@35369
   103
  shows "P q"
haftmann@35369
   104
  using assms by (cases q) simp
haftmann@35369
   105
haftmann@59867
   106
instantiation rat :: field
haftmann@25571
   107
begin
haftmann@25571
   108
huffman@47906
   109
lift_definition zero_rat :: "rat" is "(0, 1)"
huffman@47906
   110
  by simp
huffman@47906
   111
huffman@47906
   112
lift_definition one_rat :: "rat" is "(1, 1)"
huffman@47906
   113
  by simp
paulson@14365
   114
huffman@47906
   115
lemma Zero_rat_def: "0 = Fract 0 1"
huffman@47906
   116
  by transfer simp
huffman@18913
   117
huffman@47906
   118
lemma One_rat_def: "1 = Fract 1 1"
huffman@47906
   119
  by transfer simp
huffman@47906
   120
huffman@47906
   121
lift_definition plus_rat :: "rat \<Rightarrow> rat \<Rightarrow> rat"
huffman@47906
   122
  is "\<lambda>x y. (fst x * snd y + fst y * snd x, snd x * snd y)"
wenzelm@63494
   123
  by (auto simp: distrib_right) (simp add: ac_simps)
haftmann@27551
   124
haftmann@27652
   125
lemma add_rat [simp]:
haftmann@27551
   126
  assumes "b \<noteq> 0" and "d \<noteq> 0"
haftmann@27551
   127
  shows "Fract a b + Fract c d = Fract (a * d + c * b) (b * d)"
huffman@47906
   128
  using assms by transfer simp
huffman@18913
   129
huffman@47906
   130
lift_definition uminus_rat :: "rat \<Rightarrow> rat" is "\<lambda>x. (- fst x, snd x)"
huffman@47906
   131
  by simp
haftmann@27551
   132
haftmann@35369
   133
lemma minus_rat [simp]: "- Fract a b = Fract (- a) b"
huffman@47906
   134
  by transfer simp
haftmann@27551
   135
haftmann@27652
   136
lemma minus_rat_cancel [simp]: "Fract (- a) (- b) = Fract a b"
haftmann@27551
   137
  by (cases "b = 0") (simp_all add: eq_rat)
haftmann@25571
   138
wenzelm@63326
   139
definition diff_rat_def: "q - r = q + - r" for q r :: rat
huffman@18913
   140
haftmann@27652
   141
lemma diff_rat [simp]:
wenzelm@63494
   142
  "b \<noteq> 0 \<Longrightarrow> d \<noteq> 0 \<Longrightarrow> Fract a b - Fract c d = Fract (a * d - c * b) (b * d)"
wenzelm@63494
   143
  by (simp add: diff_rat_def)
haftmann@25571
   144
huffman@47906
   145
lift_definition times_rat :: "rat \<Rightarrow> rat \<Rightarrow> rat"
huffman@47906
   146
  is "\<lambda>x y. (fst x * fst y, snd x * snd y)"
haftmann@57514
   147
  by (simp add: ac_simps)
paulson@14365
   148
haftmann@27652
   149
lemma mult_rat [simp]: "Fract a b * Fract c d = Fract (a * c) (b * d)"
huffman@47906
   150
  by transfer simp
paulson@14365
   151
wenzelm@63494
   152
lemma mult_rat_cancel: "c \<noteq> 0 \<Longrightarrow> Fract (c * a) (c * b) = Fract a b"
wenzelm@63494
   153
  by transfer simp
huffman@47906
   154
huffman@47906
   155
lift_definition inverse_rat :: "rat \<Rightarrow> rat"
huffman@47906
   156
  is "\<lambda>x. if fst x = 0 then (0, 1) else (snd x, fst x)"
haftmann@57512
   157
  by (auto simp add: mult.commute)
huffman@47906
   158
huffman@47906
   159
lemma inverse_rat [simp]: "inverse (Fract a b) = Fract b a"
huffman@47906
   160
  by transfer simp
huffman@47906
   161
wenzelm@63326
   162
definition divide_rat_def: "q div r = q * inverse r" for q r :: rat
huffman@47906
   163
haftmann@60429
   164
lemma divide_rat [simp]: "Fract a b div Fract c d = Fract (a * d) (b * c)"
huffman@47906
   165
  by (simp add: divide_rat_def)
huffman@27509
   166
wenzelm@63326
   167
instance
wenzelm@63326
   168
proof
huffman@47906
   169
  fix q r s :: rat
huffman@47906
   170
  show "(q * r) * s = q * (r * s)"
huffman@47906
   171
    by transfer simp
huffman@47906
   172
  show "q * r = r * q"
huffman@47906
   173
    by transfer simp
huffman@47906
   174
  show "1 * q = q"
huffman@47906
   175
    by transfer simp
huffman@47906
   176
  show "(q + r) + s = q + (r + s)"
huffman@47906
   177
    by transfer (simp add: algebra_simps)
huffman@47906
   178
  show "q + r = r + q"
huffman@47906
   179
    by transfer simp
huffman@47906
   180
  show "0 + q = q"
huffman@47906
   181
    by transfer simp
huffman@47906
   182
  show "- q + q = 0"
huffman@47906
   183
    by transfer simp
huffman@47906
   184
  show "q - r = q + - r"
huffman@47906
   185
    by (fact diff_rat_def)
huffman@47906
   186
  show "(q + r) * s = q * s + r * s"
huffman@47906
   187
    by transfer (simp add: algebra_simps)
huffman@47906
   188
  show "(0::rat) \<noteq> 1"
huffman@47906
   189
    by transfer simp
wenzelm@63326
   190
  show "inverse q * q = 1" if "q \<noteq> 0"
wenzelm@63326
   191
    using that by transfer simp
haftmann@60429
   192
  show "q div r = q * inverse r"
huffman@47906
   193
    by (fact divide_rat_def)
huffman@47906
   194
  show "inverse 0 = (0::rat)"
huffman@47906
   195
    by transfer simp
huffman@27509
   196
qed
huffman@27509
   197
huffman@27509
   198
end
huffman@27509
   199
haftmann@27551
   200
lemma of_nat_rat: "of_nat k = Fract (of_nat k) 1"
haftmann@27652
   201
  by (induct k) (simp_all add: Zero_rat_def One_rat_def)
haftmann@27551
   202
haftmann@27551
   203
lemma of_int_rat: "of_int k = Fract k 1"
haftmann@27652
   204
  by (cases k rule: int_diff_cases) (simp add: of_nat_rat)
haftmann@27551
   205
haftmann@27551
   206
lemma Fract_of_nat_eq: "Fract (of_nat k) 1 = of_nat k"
haftmann@27551
   207
  by (rule of_nat_rat [symmetric])
haftmann@27551
   208
haftmann@27551
   209
lemma Fract_of_int_eq: "Fract k 1 = of_int k"
haftmann@27551
   210
  by (rule of_int_rat [symmetric])
haftmann@27551
   211
haftmann@35369
   212
lemma rat_number_collapse:
haftmann@27551
   213
  "Fract 0 k = 0"
haftmann@27551
   214
  "Fract 1 1 = 1"
huffman@47108
   215
  "Fract (numeral w) 1 = numeral w"
haftmann@54489
   216
  "Fract (- numeral w) 1 = - numeral w"
haftmann@54489
   217
  "Fract (- 1) 1 = - 1"
haftmann@27551
   218
  "Fract k 0 = 0"
huffman@47108
   219
  using Fract_of_int_eq [of "numeral w"]
wenzelm@63494
   220
    and Fract_of_int_eq [of "- numeral w"]
huffman@47108
   221
  by (simp_all add: Zero_rat_def One_rat_def eq_rat)
haftmann@27551
   222
huffman@47108
   223
lemma rat_number_expand:
haftmann@27551
   224
  "0 = Fract 0 1"
haftmann@27551
   225
  "1 = Fract 1 1"
huffman@47108
   226
  "numeral k = Fract (numeral k) 1"
haftmann@54489
   227
  "- 1 = Fract (- 1) 1"
haftmann@54489
   228
  "- numeral k = Fract (- numeral k) 1"
haftmann@27551
   229
  by (simp_all add: rat_number_collapse)
haftmann@27551
   230
haftmann@27551
   231
lemma Rat_cases_nonzero [case_names Fract 0]:
haftmann@35369
   232
  assumes Fract: "\<And>a b. q = Fract a b \<Longrightarrow> b > 0 \<Longrightarrow> a \<noteq> 0 \<Longrightarrow> coprime a b \<Longrightarrow> C"
wenzelm@63326
   233
    and 0: "q = 0 \<Longrightarrow> C"
haftmann@27551
   234
  shows C
haftmann@27551
   235
proof (cases "q = 0")
wenzelm@63326
   236
  case True
wenzelm@63326
   237
  then show C using 0 by auto
haftmann@27551
   238
next
haftmann@27551
   239
  case False
wenzelm@63326
   240
  then obtain a b where *: "q = Fract a b" "b > 0" "coprime a b"
wenzelm@63326
   241
    by (cases q) auto
wenzelm@63326
   242
  with False have "0 \<noteq> Fract a b"
wenzelm@63326
   243
    by simp
wenzelm@63326
   244
  with \<open>b > 0\<close> have "a \<noteq> 0"
wenzelm@63326
   245
    by (simp add: Zero_rat_def eq_rat)
wenzelm@63326
   246
  with Fract * show C by blast
haftmann@27551
   247
qed
haftmann@27551
   248
wenzelm@63326
   249
wenzelm@61799
   250
subsubsection \<open>Function \<open>normalize\<close>\<close>
nipkow@33805
   251
haftmann@35369
   252
lemma Fract_coprime: "Fract (a div gcd a b) (b div gcd a b) = Fract a b"
haftmann@35369
   253
proof (cases "b = 0")
wenzelm@63326
   254
  case True
wenzelm@63494
   255
  then show ?thesis
wenzelm@63494
   256
    by (simp add: eq_rat)
haftmann@35369
   257
next
haftmann@35369
   258
  case False
haftmann@35369
   259
  moreover have "b div gcd a b * gcd a b = b"
haftmann@35369
   260
    by (rule dvd_div_mult_self) simp
wenzelm@63326
   261
  ultimately have "b div gcd a b * gcd a b \<noteq> 0"
wenzelm@63326
   262
    by simp
wenzelm@63326
   263
  then have "b div gcd a b \<noteq> 0"
wenzelm@63326
   264
    by fastforce
wenzelm@63326
   265
  with False show ?thesis
wenzelm@63326
   266
    by (simp add: eq_rat dvd_div_mult mult.commute [of a])
haftmann@35369
   267
qed
nipkow@33805
   268
wenzelm@63326
   269
definition normalize :: "int \<times> int \<Rightarrow> int \<times> int"
wenzelm@63326
   270
  where "normalize p =
wenzelm@63326
   271
   (if snd p > 0 then (let a = gcd (fst p) (snd p) in (fst p div a, snd p div a))
haftmann@35369
   272
    else if snd p = 0 then (0, 1)
haftmann@35369
   273
    else (let a = - gcd (fst p) (snd p) in (fst p div a, snd p div a)))"
haftmann@35369
   274
haftmann@35369
   275
lemma normalize_crossproduct:
haftmann@35369
   276
  assumes "q \<noteq> 0" "s \<noteq> 0"
haftmann@35369
   277
  assumes "normalize (p, q) = normalize (r, s)"
haftmann@35369
   278
  shows "p * s = r * q"
haftmann@35369
   279
proof -
wenzelm@63326
   280
  have *: "p * s = q * r"
wenzelm@63326
   281
    if "p * gcd r s = sgn (q * s) * r * gcd p q" and "q * gcd r s = sgn (q * s) * s * gcd p q"
haftmann@35369
   282
  proof -
wenzelm@63494
   283
    from that have "(p * gcd r s) * (sgn (q * s) * s * gcd p q) =
wenzelm@63494
   284
        (q * gcd r s) * (sgn (q * s) * r * gcd p q)"
wenzelm@63326
   285
      by simp
wenzelm@63326
   286
    with assms show ?thesis
wenzelm@63326
   287
      by (auto simp add: ac_simps sgn_times sgn_0_0)
haftmann@35369
   288
  qed
haftmann@35369
   289
  from assms show ?thesis
wenzelm@63494
   290
    by (auto simp: normalize_def Let_def dvd_div_div_eq_mult mult.commute sgn_times
wenzelm@63326
   291
        split: if_splits intro: *)
nipkow@33805
   292
qed
nipkow@33805
   293
haftmann@35369
   294
lemma normalize_eq: "normalize (a, b) = (p, q) \<Longrightarrow> Fract p q = Fract a b"
wenzelm@63494
   295
  by (auto simp: normalize_def Let_def Fract_coprime dvd_div_neg rat_number_collapse
wenzelm@63326
   296
      split: if_split_asm)
haftmann@35369
   297
haftmann@35369
   298
lemma normalize_denom_pos: "normalize r = (p, q) \<Longrightarrow> q > 0"
wenzelm@63494
   299
  by (auto simp: normalize_def Let_def dvd_div_neg pos_imp_zdiv_neg_iff nonneg1_imp_zdiv_pos_iff
wenzelm@63326
   300
      split: if_split_asm)
haftmann@35369
   301
haftmann@35369
   302
lemma normalize_coprime: "normalize r = (p, q) \<Longrightarrow> coprime p q"
wenzelm@63494
   303
  by (auto simp: normalize_def Let_def dvd_div_neg div_gcd_coprime split: if_split_asm)
haftmann@35369
   304
wenzelm@63326
   305
lemma normalize_stable [simp]: "q > 0 \<Longrightarrow> coprime p q \<Longrightarrow> normalize (p, q) = (p, q)"
haftmann@35369
   306
  by (simp add: normalize_def)
haftmann@35369
   307
wenzelm@63326
   308
lemma normalize_denom_zero [simp]: "normalize (p, 0) = (0, 1)"
haftmann@35369
   309
  by (simp add: normalize_def)
haftmann@35369
   310
wenzelm@63326
   311
lemma normalize_negative [simp]: "q < 0 \<Longrightarrow> normalize (p, q) = normalize (- p, - q)"
haftmann@35369
   312
  by (simp add: normalize_def Let_def dvd_div_neg dvd_neg_div)
haftmann@35369
   313
wenzelm@60758
   314
text\<open>
haftmann@35369
   315
  Decompose a fraction into normalized, i.e. coprime numerator and denominator:
wenzelm@60758
   316
\<close>
haftmann@35369
   317
wenzelm@63326
   318
definition quotient_of :: "rat \<Rightarrow> int \<times> int"
wenzelm@63326
   319
  where "quotient_of x =
wenzelm@63326
   320
    (THE pair. x = Fract (fst pair) (snd pair) \<and> snd pair > 0 \<and> coprime (fst pair) (snd pair))"
haftmann@35369
   321
wenzelm@63326
   322
lemma quotient_of_unique: "\<exists>!p. r = Fract (fst p) (snd p) \<and> snd p > 0 \<and> coprime (fst p) (snd p)"
haftmann@35369
   323
proof (cases r)
haftmann@35369
   324
  case (Fract a b)
wenzelm@63494
   325
  then have "r = Fract (fst (a, b)) (snd (a, b)) \<and>
wenzelm@63494
   326
      snd (a, b) > 0 \<and> coprime (fst (a, b)) (snd (a, b))"
wenzelm@63326
   327
    by auto
wenzelm@63326
   328
  then show ?thesis
wenzelm@63326
   329
  proof (rule ex1I)
haftmann@35369
   330
    fix p
wenzelm@63326
   331
    obtain c d :: int where p: "p = (c, d)"
wenzelm@63326
   332
      by (cases p)
haftmann@35369
   333
    assume "r = Fract (fst p) (snd p) \<and> snd p > 0 \<and> coprime (fst p) (snd p)"
wenzelm@63326
   334
    with p have Fract': "r = Fract c d" "d > 0" "coprime c d"
wenzelm@63326
   335
      by simp_all
haftmann@35369
   336
    have "c = a \<and> d = b"
haftmann@35369
   337
    proof (cases "a = 0")
wenzelm@63326
   338
      case True
wenzelm@63326
   339
      with Fract Fract' show ?thesis
wenzelm@63326
   340
        by (simp add: eq_rat)
haftmann@35369
   341
    next
haftmann@35369
   342
      case False
wenzelm@63326
   343
      with Fract Fract' have *: "c * b = a * d" and "c \<noteq> 0"
wenzelm@63326
   344
        by (auto simp add: eq_rat)
wenzelm@63326
   345
      then have "c * b > 0 \<longleftrightarrow> a * d > 0"
wenzelm@63326
   346
        by auto
wenzelm@63326
   347
      with \<open>b > 0\<close> \<open>d > 0\<close> have "a > 0 \<longleftrightarrow> c > 0"
wenzelm@63326
   348
        by (simp add: zero_less_mult_iff)
wenzelm@63326
   349
      with \<open>a \<noteq> 0\<close> \<open>c \<noteq> 0\<close> have sgn: "sgn a = sgn c"
wenzelm@63326
   350
        by (auto simp add: not_less)
wenzelm@60758
   351
      from \<open>coprime a b\<close> \<open>coprime c d\<close> have "\<bar>a\<bar> * \<bar>d\<bar> = \<bar>c\<bar> * \<bar>b\<bar> \<longleftrightarrow> \<bar>a\<bar> = \<bar>c\<bar> \<and> \<bar>d\<bar> = \<bar>b\<bar>"
haftmann@35369
   352
        by (simp add: coprime_crossproduct_int)
wenzelm@63326
   353
      with \<open>b > 0\<close> \<open>d > 0\<close> have "\<bar>a\<bar> * d = \<bar>c\<bar> * b \<longleftrightarrow> \<bar>a\<bar> = \<bar>c\<bar> \<and> d = b"
wenzelm@63326
   354
        by simp
wenzelm@63326
   355
      then have "a * sgn a * d = c * sgn c * b \<longleftrightarrow> a * sgn a = c * sgn c \<and> d = b"
wenzelm@63326
   356
        by (simp add: abs_sgn)
wenzelm@63326
   357
      with sgn * show ?thesis
wenzelm@63326
   358
        by (auto simp add: sgn_0_0)
nipkow@33805
   359
    qed
wenzelm@63326
   360
    with p show "p = (a, b)"
wenzelm@63326
   361
      by simp
nipkow@33805
   362
  qed
nipkow@33805
   363
qed
nipkow@33805
   364
wenzelm@63326
   365
lemma quotient_of_Fract [code]: "quotient_of (Fract a b) = normalize (a, b)"
haftmann@35369
   366
proof -
haftmann@35369
   367
  have "Fract a b = Fract (fst (normalize (a, b))) (snd (normalize (a, b)))" (is ?Fract)
haftmann@35369
   368
    by (rule sym) (auto intro: normalize_eq)
wenzelm@52146
   369
  moreover have "0 < snd (normalize (a, b))" (is ?denom_pos)
haftmann@35369
   370
    by (cases "normalize (a, b)") (rule normalize_denom_pos, simp)
haftmann@35369
   371
  moreover have "coprime (fst (normalize (a, b))) (snd (normalize (a, b)))" (is ?coprime)
haftmann@35369
   372
    by (rule normalize_coprime) simp
haftmann@35369
   373
  ultimately have "?Fract \<and> ?denom_pos \<and> ?coprime" by blast
wenzelm@63326
   374
  with quotient_of_unique
wenzelm@63326
   375
  have "(THE p. Fract a b = Fract (fst p) (snd p) \<and> 0 < snd p \<and>
wenzelm@63326
   376
    coprime (fst p) (snd p)) = normalize (a, b)" by (rule the1_equality)
haftmann@35369
   377
  then show ?thesis by (simp add: quotient_of_def)
haftmann@35369
   378
qed
haftmann@35369
   379
haftmann@35369
   380
lemma quotient_of_number [simp]:
haftmann@35369
   381
  "quotient_of 0 = (0, 1)"
haftmann@35369
   382
  "quotient_of 1 = (1, 1)"
huffman@47108
   383
  "quotient_of (numeral k) = (numeral k, 1)"
haftmann@54489
   384
  "quotient_of (- 1) = (- 1, 1)"
haftmann@54489
   385
  "quotient_of (- numeral k) = (- numeral k, 1)"
haftmann@35369
   386
  by (simp_all add: rat_number_expand quotient_of_Fract)
nipkow@33805
   387
haftmann@35369
   388
lemma quotient_of_eq: "quotient_of (Fract a b) = (p, q) \<Longrightarrow> Fract p q = Fract a b"
haftmann@35369
   389
  by (simp add: quotient_of_Fract normalize_eq)
haftmann@35369
   390
haftmann@35369
   391
lemma quotient_of_denom_pos: "quotient_of r = (p, q) \<Longrightarrow> q > 0"
haftmann@35369
   392
  by (cases r) (simp add: quotient_of_Fract normalize_denom_pos)
haftmann@35369
   393
haftmann@35369
   394
lemma quotient_of_coprime: "quotient_of r = (p, q) \<Longrightarrow> coprime p q"
haftmann@35369
   395
  by (cases r) (simp add: quotient_of_Fract normalize_coprime)
nipkow@33805
   396
haftmann@35369
   397
lemma quotient_of_inject:
haftmann@35369
   398
  assumes "quotient_of a = quotient_of b"
haftmann@35369
   399
  shows "a = b"
haftmann@35369
   400
proof -
wenzelm@63326
   401
  obtain p q r s where a: "a = Fract p q" and b: "b = Fract r s" and "q > 0" and "s > 0"
wenzelm@63326
   402
    by (cases a, cases b)
wenzelm@63326
   403
  with assms show ?thesis
wenzelm@63326
   404
    by (simp add: eq_rat quotient_of_Fract normalize_crossproduct)
haftmann@35369
   405
qed
haftmann@35369
   406
wenzelm@63326
   407
lemma quotient_of_inject_eq: "quotient_of a = quotient_of b \<longleftrightarrow> a = b"
haftmann@35369
   408
  by (auto simp add: quotient_of_inject)
nipkow@33805
   409
haftmann@27551
   410
wenzelm@60758
   411
subsubsection \<open>Various\<close>
haftmann@27551
   412
haftmann@27551
   413
lemma Fract_of_int_quotient: "Fract k l = of_int k / of_int l"
haftmann@27652
   414
  by (simp add: Fract_of_int_eq [symmetric])
haftmann@27551
   415
wenzelm@63326
   416
lemma Fract_add_one: "n \<noteq> 0 \<Longrightarrow> Fract (m + n) n = Fract m n + 1"
huffman@47108
   417
  by (simp add: rat_number_expand)
haftmann@27551
   418
hoelzl@50178
   419
lemma quotient_of_div:
hoelzl@50178
   420
  assumes r: "quotient_of r = (n,d)"
hoelzl@50178
   421
  shows "r = of_int n / of_int d"
hoelzl@50178
   422
proof -
hoelzl@50178
   423
  from theI'[OF quotient_of_unique[of r], unfolded r[unfolded quotient_of_def]]
hoelzl@50178
   424
  have "r = Fract n d" by simp
wenzelm@63326
   425
  then show ?thesis using Fract_of_int_quotient
wenzelm@63326
   426
    by simp
hoelzl@50178
   427
qed
haftmann@27551
   428
wenzelm@63326
   429
wenzelm@60758
   430
subsubsection \<open>The ordered field of rational numbers\<close>
huffman@27509
   431
huffman@47907
   432
lift_definition positive :: "rat \<Rightarrow> bool"
huffman@47907
   433
  is "\<lambda>x. 0 < fst x * snd x"
wenzelm@63326
   434
proof clarsimp
huffman@47907
   435
  fix a b c d :: int
huffman@47907
   436
  assume "b \<noteq> 0" and "d \<noteq> 0" and "a * d = c * b"
wenzelm@63326
   437
  then have "a * d * b * d = c * b * b * d"
huffman@47907
   438
    by simp
wenzelm@63326
   439
  then have "a * b * d\<^sup>2 = c * d * b\<^sup>2"
haftmann@57514
   440
    unfolding power2_eq_square by (simp add: ac_simps)
wenzelm@63326
   441
  then have "0 < a * b * d\<^sup>2 \<longleftrightarrow> 0 < c * d * b\<^sup>2"
huffman@47907
   442
    by simp
wenzelm@63326
   443
  then show "0 < a * b \<longleftrightarrow> 0 < c * d"
wenzelm@60758
   444
    using \<open>b \<noteq> 0\<close> and \<open>d \<noteq> 0\<close>
huffman@47907
   445
    by (simp add: zero_less_mult_iff)
huffman@47907
   446
qed
huffman@47907
   447
huffman@47907
   448
lemma positive_zero: "\<not> positive 0"
huffman@47907
   449
  by transfer simp
huffman@47907
   450
wenzelm@63326
   451
lemma positive_add: "positive x \<Longrightarrow> positive y \<Longrightarrow> positive (x + y)"
wenzelm@63326
   452
  apply transfer
wenzelm@63326
   453
  apply (simp add: zero_less_mult_iff)
wenzelm@63494
   454
  apply (elim disjE)
wenzelm@63494
   455
     apply (simp_all add: add_pos_pos add_neg_neg mult_pos_neg mult_neg_pos mult_neg_neg)
wenzelm@63326
   456
  done
huffman@47907
   457
wenzelm@63326
   458
lemma positive_mult: "positive x \<Longrightarrow> positive y \<Longrightarrow> positive (x * y)"
wenzelm@63326
   459
  apply transfer
wenzelm@63326
   460
  apply (drule (1) mult_pos_pos)
wenzelm@63326
   461
  apply (simp add: ac_simps)
wenzelm@63326
   462
  done
huffman@47907
   463
wenzelm@63326
   464
lemma positive_minus: "\<not> positive x \<Longrightarrow> x \<noteq> 0 \<Longrightarrow> positive (- x)"
wenzelm@63326
   465
  by transfer (auto simp: neq_iff zero_less_mult_iff mult_less_0_iff)
huffman@47907
   466
haftmann@59867
   467
instantiation rat :: linordered_field
huffman@27509
   468
begin
huffman@27509
   469
wenzelm@63326
   470
definition "x < y \<longleftrightarrow> positive (y - x)"
huffman@47907
   471
wenzelm@63326
   472
definition "x \<le> y \<longleftrightarrow> x < y \<or> x = y" for x y :: rat
huffman@47907
   473
wenzelm@63326
   474
definition "\<bar>a\<bar> = (if a < 0 then - a else a)" for a :: rat
huffman@47907
   475
wenzelm@63326
   476
definition "sgn a = (if a = 0 then 0 else if 0 < a then 1 else - 1)" for a :: rat
huffman@47906
   477
wenzelm@63326
   478
instance
wenzelm@63326
   479
proof
huffman@47907
   480
  fix a b c :: rat
huffman@47907
   481
  show "\<bar>a\<bar> = (if a < 0 then - a else a)"
huffman@47907
   482
    by (rule abs_rat_def)
huffman@47907
   483
  show "a < b \<longleftrightarrow> a \<le> b \<and> \<not> b \<le> a"
huffman@47907
   484
    unfolding less_eq_rat_def less_rat_def
wenzelm@63326
   485
    apply auto
wenzelm@63494
   486
     apply (drule (1) positive_add)
wenzelm@63494
   487
     apply (simp_all add: positive_zero)
wenzelm@63326
   488
    done
huffman@47907
   489
  show "a \<le> a"
huffman@47907
   490
    unfolding less_eq_rat_def by simp
huffman@47907
   491
  show "a \<le> b \<Longrightarrow> b \<le> c \<Longrightarrow> a \<le> c"
huffman@47907
   492
    unfolding less_eq_rat_def less_rat_def
wenzelm@63326
   493
    apply auto
wenzelm@63326
   494
    apply (drule (1) positive_add)
wenzelm@63326
   495
    apply (simp add: algebra_simps)
wenzelm@63326
   496
    done
huffman@47907
   497
  show "a \<le> b \<Longrightarrow> b \<le> a \<Longrightarrow> a = b"
huffman@47907
   498
    unfolding less_eq_rat_def less_rat_def
wenzelm@63326
   499
    apply auto
wenzelm@63326
   500
    apply (drule (1) positive_add)
wenzelm@63326
   501
    apply (simp add: positive_zero)
wenzelm@63326
   502
    done
huffman@47907
   503
  show "a \<le> b \<Longrightarrow> c + a \<le> c + b"
haftmann@54230
   504
    unfolding less_eq_rat_def less_rat_def by auto
huffman@47907
   505
  show "sgn a = (if a = 0 then 0 else if 0 < a then 1 else - 1)"
huffman@47907
   506
    by (rule sgn_rat_def)
huffman@47907
   507
  show "a \<le> b \<or> b \<le> a"
huffman@47907
   508
    unfolding less_eq_rat_def less_rat_def
huffman@47907
   509
    by (auto dest!: positive_minus)
huffman@47907
   510
  show "a < b \<Longrightarrow> 0 < c \<Longrightarrow> c * a < c * b"
huffman@47907
   511
    unfolding less_rat_def
wenzelm@63326
   512
    apply (drule (1) positive_mult)
wenzelm@63326
   513
    apply (simp add: algebra_simps)
wenzelm@63326
   514
    done
huffman@47906
   515
qed
haftmann@27551
   516
huffman@47907
   517
end
huffman@47907
   518
huffman@47907
   519
instantiation rat :: distrib_lattice
huffman@47907
   520
begin
huffman@47907
   521
wenzelm@63326
   522
definition "(inf :: rat \<Rightarrow> rat \<Rightarrow> rat) = min"
huffman@27509
   523
wenzelm@63326
   524
definition "(sup :: rat \<Rightarrow> rat \<Rightarrow> rat) = max"
huffman@47907
   525
wenzelm@63326
   526
instance
wenzelm@63326
   527
  by standard (auto simp add: inf_rat_def sup_rat_def max_min_distrib2)
huffman@47907
   528
huffman@47907
   529
end
huffman@47907
   530
huffman@47907
   531
lemma positive_rat: "positive (Fract a b) \<longleftrightarrow> 0 < a * b"
huffman@47907
   532
  by transfer simp
huffman@27509
   533
haftmann@27652
   534
lemma less_rat [simp]:
wenzelm@63494
   535
  "b \<noteq> 0 \<Longrightarrow> d \<noteq> 0 \<Longrightarrow> Fract a b < Fract c d \<longleftrightarrow> (a * d) * (b * d) < (c * b) * (b * d)"
wenzelm@63494
   536
  by (simp add: less_rat_def positive_rat algebra_simps)
huffman@27509
   537
huffman@47907
   538
lemma le_rat [simp]:
wenzelm@63494
   539
  "b \<noteq> 0 \<Longrightarrow> d \<noteq> 0 \<Longrightarrow> Fract a b \<le> Fract c d \<longleftrightarrow> (a * d) * (b * d) \<le> (c * b) * (b * d)"
wenzelm@63494
   540
  by (simp add: le_less eq_rat)
haftmann@27551
   541
haftmann@27652
   542
lemma abs_rat [simp, code]: "\<bar>Fract a b\<bar> = Fract \<bar>a\<bar> \<bar>b\<bar>"
huffman@35216
   543
  by (auto simp add: abs_rat_def zabs_def Zero_rat_def not_less le_less eq_rat zero_less_mult_iff)
haftmann@27551
   544
haftmann@27652
   545
lemma sgn_rat [simp, code]: "sgn (Fract a b) = of_int (sgn a * sgn b)"
haftmann@27551
   546
  unfolding Fract_of_int_eq
haftmann@27652
   547
  by (auto simp: zsgn_def sgn_rat_def Zero_rat_def eq_rat)
haftmann@27551
   548
    (auto simp: rat_number_collapse not_less le_less zero_less_mult_iff)
haftmann@27551
   549
haftmann@27551
   550
lemma Rat_induct_pos [case_names Fract, induct type: rat]:
haftmann@27551
   551
  assumes step: "\<And>a b. 0 < b \<Longrightarrow> P (Fract a b)"
haftmann@27551
   552
  shows "P q"
paulson@14365
   553
proof (cases q)
wenzelm@63326
   554
  case (Fract a b)
wenzelm@63326
   555
  have step': "P (Fract a b)" if b: "b < 0" for a b :: int
paulson@14365
   556
  proof -
wenzelm@63326
   557
    from b have "0 < - b"
wenzelm@63326
   558
      by simp
wenzelm@63326
   559
    then have "P (Fract (- a) (- b))"
wenzelm@63326
   560
      by (rule step)
wenzelm@63326
   561
    then show "P (Fract a b)"
wenzelm@63326
   562
      by (simp add: order_less_imp_not_eq [OF b])
paulson@14365
   563
  qed
wenzelm@63494
   564
  from Fract show "P q"
wenzelm@63494
   565
    by (auto simp add: linorder_neq_iff step step')
paulson@14365
   566
qed
paulson@14365
   567
wenzelm@63326
   568
lemma zero_less_Fract_iff: "0 < b \<Longrightarrow> 0 < Fract a b \<longleftrightarrow> 0 < a"
huffman@30095
   569
  by (simp add: Zero_rat_def zero_less_mult_iff)
huffman@30095
   570
wenzelm@63326
   571
lemma Fract_less_zero_iff: "0 < b \<Longrightarrow> Fract a b < 0 \<longleftrightarrow> a < 0"
huffman@30095
   572
  by (simp add: Zero_rat_def mult_less_0_iff)
huffman@30095
   573
wenzelm@63326
   574
lemma zero_le_Fract_iff: "0 < b \<Longrightarrow> 0 \<le> Fract a b \<longleftrightarrow> 0 \<le> a"
huffman@30095
   575
  by (simp add: Zero_rat_def zero_le_mult_iff)
huffman@30095
   576
wenzelm@63326
   577
lemma Fract_le_zero_iff: "0 < b \<Longrightarrow> Fract a b \<le> 0 \<longleftrightarrow> a \<le> 0"
huffman@30095
   578
  by (simp add: Zero_rat_def mult_le_0_iff)
huffman@30095
   579
wenzelm@63326
   580
lemma one_less_Fract_iff: "0 < b \<Longrightarrow> 1 < Fract a b \<longleftrightarrow> b < a"
huffman@30095
   581
  by (simp add: One_rat_def mult_less_cancel_right_disj)
huffman@30095
   582
wenzelm@63326
   583
lemma Fract_less_one_iff: "0 < b \<Longrightarrow> Fract a b < 1 \<longleftrightarrow> a < b"
huffman@30095
   584
  by (simp add: One_rat_def mult_less_cancel_right_disj)
huffman@30095
   585
wenzelm@63326
   586
lemma one_le_Fract_iff: "0 < b \<Longrightarrow> 1 \<le> Fract a b \<longleftrightarrow> b \<le> a"
huffman@30095
   587
  by (simp add: One_rat_def mult_le_cancel_right)
huffman@30095
   588
wenzelm@63326
   589
lemma Fract_le_one_iff: "0 < b \<Longrightarrow> Fract a b \<le> 1 \<longleftrightarrow> a \<le> b"
huffman@30095
   590
  by (simp add: One_rat_def mult_le_cancel_right)
paulson@14365
   591
paulson@14378
   592
wenzelm@60758
   593
subsubsection \<open>Rationals are an Archimedean field\<close>
huffman@30097
   594
wenzelm@63326
   595
lemma rat_floor_lemma: "of_int (a div b) \<le> Fract a b \<and> Fract a b < of_int (a div b + 1)"
huffman@30097
   596
proof -
huffman@30097
   597
  have "Fract a b = of_int (a div b) + Fract (a mod b) b"
wenzelm@63326
   598
    by (cases "b = 0") (simp, simp add: of_int_rat)
huffman@30097
   599
  moreover have "0 \<le> Fract (a mod b) b \<and> Fract (a mod b) b < 1"
huffman@35293
   600
    unfolding Fract_of_int_quotient
hoelzl@56571
   601
    by (rule linorder_cases [of b 0]) (simp_all add: divide_nonpos_neg)
huffman@30097
   602
  ultimately show ?thesis by simp
huffman@30097
   603
qed
huffman@30097
   604
huffman@30097
   605
instance rat :: archimedean_field
huffman@30097
   606
proof
wenzelm@63326
   607
  show "\<exists>z. r \<le> of_int z" for r :: rat
huffman@30097
   608
  proof (induct r)
huffman@30097
   609
    case (Fract a b)
huffman@35293
   610
    have "Fract a b \<le> of_int (a div b + 1)"
huffman@35293
   611
      using rat_floor_lemma [of a b] by simp
huffman@30097
   612
    then show "\<exists>z. Fract a b \<le> of_int z" ..
huffman@30097
   613
  qed
huffman@30097
   614
qed
huffman@30097
   615
bulwahn@43732
   616
instantiation rat :: floor_ceiling
bulwahn@43732
   617
begin
bulwahn@43732
   618
wenzelm@63326
   619
definition [code del]: "\<lfloor>x\<rfloor> = (THE z. of_int z \<le> x \<and> x < of_int (z + 1))" for x :: rat
bulwahn@43732
   620
wenzelm@61942
   621
instance
wenzelm@61942
   622
proof
wenzelm@63326
   623
  show "of_int \<lfloor>x\<rfloor> \<le> x \<and> x < of_int (\<lfloor>x\<rfloor> + 1)" for x :: rat
bulwahn@43732
   624
    unfolding floor_rat_def using floor_exists1 by (rule theI')
bulwahn@43732
   625
qed
bulwahn@43732
   626
bulwahn@43732
   627
end
bulwahn@43732
   628
wenzelm@61942
   629
lemma floor_Fract: "\<lfloor>Fract a b\<rfloor> = a div b"
haftmann@59984
   630
  by (simp add: Fract_of_int_quotient floor_divide_of_int_eq)
huffman@30097
   631
huffman@30097
   632
wenzelm@60758
   633
subsection \<open>Linear arithmetic setup\<close>
paulson@14387
   634
wenzelm@60758
   635
declaration \<open>
haftmann@31100
   636
  K (Lin_Arith.add_inj_thms [@{thm of_nat_le_iff} RS iffD2, @{thm of_nat_eq_iff} RS iffD2]
haftmann@31100
   637
    (* not needed because x < (y::nat) can be rewritten as Suc x <= y: of_nat_less_iff RS iffD2 *)
haftmann@31100
   638
  #> Lin_Arith.add_inj_thms [@{thm of_int_le_iff} RS iffD2, @{thm of_int_eq_iff} RS iffD2]
haftmann@31100
   639
    (* not needed because x < (y::int) can be rewritten as x + 1 <= y: of_int_less_iff RS iffD2 *)
haftmann@31100
   640
  #> Lin_Arith.add_simps [@{thm neg_less_iff_less},
haftmann@31100
   641
      @{thm True_implies_equals},
wenzelm@55143
   642
      @{thm distrib_left [where a = "numeral v" for v]},
wenzelm@55143
   643
      @{thm distrib_left [where a = "- numeral v" for v]},
haftmann@31100
   644
      @{thm divide_1}, @{thm divide_zero_left},
haftmann@31100
   645
      @{thm times_divide_eq_right}, @{thm times_divide_eq_left},
haftmann@31100
   646
      @{thm minus_divide_left} RS sym, @{thm minus_divide_right} RS sym,
haftmann@31100
   647
      @{thm of_int_minus}, @{thm of_int_diff},
haftmann@31100
   648
      @{thm of_int_of_nat_eq}]
wenzelm@61144
   649
  #> Lin_Arith.add_simprocs [Numeral_Simprocs.field_divide_cancel_numeral_factor]
wenzelm@63326
   650
  #> Lin_Arith.add_inj_const (@{const_name of_nat}, @{typ "nat \<Rightarrow> rat"})
wenzelm@63326
   651
  #> Lin_Arith.add_inj_const (@{const_name of_int}, @{typ "int \<Rightarrow> rat"}))
wenzelm@60758
   652
\<close>
paulson@14387
   653
huffman@23342
   654
wenzelm@60758
   655
subsection \<open>Embedding from Rationals to other Fields\<close>
huffman@23342
   656
haftmann@27551
   657
context field_char_0
haftmann@27551
   658
begin
haftmann@27551
   659
huffman@47906
   660
lift_definition of_rat :: "rat \<Rightarrow> 'a"
huffman@47906
   661
  is "\<lambda>x. of_int (fst x) / of_int (snd x)"
wenzelm@63494
   662
  by (auto simp: nonzero_divide_eq_eq nonzero_eq_divide_eq) (simp only: of_int_mult [symmetric])
huffman@23342
   663
huffman@47906
   664
end
huffman@47906
   665
haftmann@27551
   666
lemma of_rat_rat: "b \<noteq> 0 \<Longrightarrow> of_rat (Fract a b) = of_int a / of_int b"
huffman@47906
   667
  by transfer simp
huffman@23342
   668
huffman@23342
   669
lemma of_rat_0 [simp]: "of_rat 0 = 0"
huffman@47906
   670
  by transfer simp
huffman@23342
   671
huffman@23342
   672
lemma of_rat_1 [simp]: "of_rat 1 = 1"
huffman@47906
   673
  by transfer simp
huffman@23342
   674
huffman@23342
   675
lemma of_rat_add: "of_rat (a + b) = of_rat a + of_rat b"
huffman@47906
   676
  by transfer (simp add: add_frac_eq)
huffman@23342
   677
huffman@23343
   678
lemma of_rat_minus: "of_rat (- a) = - of_rat a"
hoelzl@56479
   679
  by transfer simp
huffman@23343
   680
wenzelm@63326
   681
lemma of_rat_neg_one [simp]: "of_rat (- 1) = - 1"
haftmann@54489
   682
  by (simp add: of_rat_minus)
haftmann@54489
   683
huffman@23343
   684
lemma of_rat_diff: "of_rat (a - b) = of_rat a - of_rat b"
haftmann@54230
   685
  using of_rat_add [of a "- b"] by (simp add: of_rat_minus)
huffman@23343
   686
huffman@23342
   687
lemma of_rat_mult: "of_rat (a * b) = of_rat a * of_rat b"
wenzelm@63326
   688
  by transfer (simp add: divide_inverse nonzero_inverse_mult_distrib ac_simps)
huffman@23342
   689
hoelzl@59000
   690
lemma of_rat_setsum: "of_rat (\<Sum>a\<in>A. f a) = (\<Sum>a\<in>A. of_rat (f a))"
hoelzl@59000
   691
  by (induct rule: infinite_finite_induct) (auto simp: of_rat_add)
hoelzl@59000
   692
hoelzl@59000
   693
lemma of_rat_setprod: "of_rat (\<Prod>a\<in>A. f a) = (\<Prod>a\<in>A. of_rat (f a))"
hoelzl@59000
   694
  by (induct rule: infinite_finite_induct) (auto simp: of_rat_mult)
hoelzl@59000
   695
wenzelm@63326
   696
lemma nonzero_of_rat_inverse: "a \<noteq> 0 \<Longrightarrow> of_rat (inverse a) = inverse (of_rat a)"
wenzelm@63326
   697
  by (rule inverse_unique [symmetric]) (simp add: of_rat_mult [symmetric])
huffman@23342
   698
wenzelm@63326
   699
lemma of_rat_inverse: "(of_rat (inverse a) :: 'a::{field_char_0,field}) = inverse (of_rat a)"
wenzelm@63326
   700
  by (cases "a = 0") (simp_all add: nonzero_of_rat_inverse)
huffman@23342
   701
wenzelm@63326
   702
lemma nonzero_of_rat_divide: "b \<noteq> 0 \<Longrightarrow> of_rat (a / b) = of_rat a / of_rat b"
wenzelm@63326
   703
  by (simp add: divide_inverse of_rat_mult nonzero_of_rat_inverse)
huffman@23342
   704
wenzelm@63326
   705
lemma of_rat_divide: "(of_rat (a / b) :: 'a::{field_char_0,field}) = of_rat a / of_rat b"
wenzelm@63326
   706
  by (cases "b = 0") (simp_all add: nonzero_of_rat_divide)
wenzelm@63326
   707
wenzelm@63326
   708
lemma of_rat_power: "(of_rat (a ^ n) :: 'a::field_char_0) = of_rat a ^ n"
wenzelm@63326
   709
  by (induct n) (simp_all add: of_rat_mult)
huffman@23342
   710
wenzelm@63326
   711
lemma of_rat_eq_iff [simp]: "of_rat a = of_rat b \<longleftrightarrow> a = b"
wenzelm@63326
   712
  apply transfer
wenzelm@63326
   713
  apply (simp add: nonzero_divide_eq_eq nonzero_eq_divide_eq)
wenzelm@63326
   714
  apply (simp only: of_int_mult [symmetric] of_int_eq_iff)
wenzelm@63326
   715
  done
huffman@23343
   716
wenzelm@63326
   717
lemma of_rat_eq_0_iff [simp]: "of_rat a = 0 \<longleftrightarrow> a = 0"
hoelzl@54409
   718
  using of_rat_eq_iff [of _ 0] by simp
hoelzl@54409
   719
wenzelm@63326
   720
lemma zero_eq_of_rat_iff [simp]: "0 = of_rat a \<longleftrightarrow> 0 = a"
hoelzl@54409
   721
  by simp
hoelzl@54409
   722
wenzelm@63326
   723
lemma of_rat_eq_1_iff [simp]: "of_rat a = 1 \<longleftrightarrow> a = 1"
hoelzl@54409
   724
  using of_rat_eq_iff [of _ 1] by simp
hoelzl@54409
   725
wenzelm@63326
   726
lemma one_eq_of_rat_iff [simp]: "1 = of_rat a \<longleftrightarrow> 1 = a"
hoelzl@54409
   727
  by simp
hoelzl@54409
   728
wenzelm@63326
   729
lemma of_rat_less: "(of_rat r :: 'a::linordered_field) < of_rat s \<longleftrightarrow> r < s"
haftmann@27652
   730
proof (induct r, induct s)
haftmann@27652
   731
  fix a b c d :: int
haftmann@27652
   732
  assume not_zero: "b > 0" "d > 0"
nipkow@56544
   733
  then have "b * d > 0" by simp
haftmann@27652
   734
  have of_int_divide_less_eq:
wenzelm@63326
   735
    "(of_int a :: 'a) / of_int b < of_int c / of_int d \<longleftrightarrow>
wenzelm@63326
   736
      (of_int a :: 'a) * of_int d < of_int c * of_int b"
haftmann@27652
   737
    using not_zero by (simp add: pos_less_divide_eq pos_divide_less_eq)
wenzelm@63326
   738
  show "(of_rat (Fract a b) :: 'a::linordered_field) < of_rat (Fract c d) \<longleftrightarrow>
wenzelm@63326
   739
      Fract a b < Fract c d"
wenzelm@60758
   740
    using not_zero \<open>b * d > 0\<close>
haftmann@27652
   741
    by (simp add: of_rat_rat of_int_divide_less_eq of_int_mult [symmetric] del: of_int_mult)
haftmann@27652
   742
qed
haftmann@27652
   743
wenzelm@63326
   744
lemma of_rat_less_eq: "(of_rat r :: 'a::linordered_field) \<le> of_rat s \<longleftrightarrow> r \<le> s"
haftmann@27652
   745
  unfolding le_less by (auto simp add: of_rat_less)
haftmann@27652
   746
wenzelm@63326
   747
lemma of_rat_le_0_iff [simp]: "(of_rat r :: 'a::linordered_field) \<le> 0 \<longleftrightarrow> r \<le> 0"
wenzelm@63326
   748
  using of_rat_less_eq [of r 0, where 'a = 'a] by simp
hoelzl@54409
   749
wenzelm@63326
   750
lemma zero_le_of_rat_iff [simp]: "0 \<le> (of_rat r :: 'a::linordered_field) \<longleftrightarrow> 0 \<le> r"
wenzelm@63326
   751
  using of_rat_less_eq [of 0 r, where 'a = 'a] by simp
hoelzl@54409
   752
wenzelm@63326
   753
lemma of_rat_le_1_iff [simp]: "(of_rat r :: 'a::linordered_field) \<le> 1 \<longleftrightarrow> r \<le> 1"
hoelzl@54409
   754
  using of_rat_less_eq [of r 1] by simp
hoelzl@54409
   755
wenzelm@63326
   756
lemma one_le_of_rat_iff [simp]: "1 \<le> (of_rat r :: 'a::linordered_field) \<longleftrightarrow> 1 \<le> r"
hoelzl@54409
   757
  using of_rat_less_eq [of 1 r] by simp
hoelzl@54409
   758
wenzelm@63326
   759
lemma of_rat_less_0_iff [simp]: "(of_rat r :: 'a::linordered_field) < 0 \<longleftrightarrow> r < 0"
wenzelm@63326
   760
  using of_rat_less [of r 0, where 'a = 'a] by simp
hoelzl@54409
   761
wenzelm@63326
   762
lemma zero_less_of_rat_iff [simp]: "0 < (of_rat r :: 'a::linordered_field) \<longleftrightarrow> 0 < r"
wenzelm@63326
   763
  using of_rat_less [of 0 r, where 'a = 'a] by simp
hoelzl@54409
   764
wenzelm@63326
   765
lemma of_rat_less_1_iff [simp]: "(of_rat r :: 'a::linordered_field) < 1 \<longleftrightarrow> r < 1"
hoelzl@54409
   766
  using of_rat_less [of r 1] by simp
hoelzl@54409
   767
wenzelm@63326
   768
lemma one_less_of_rat_iff [simp]: "1 < (of_rat r :: 'a::linordered_field) \<longleftrightarrow> 1 < r"
hoelzl@54409
   769
  using of_rat_less [of 1 r] by simp
huffman@23343
   770
haftmann@27652
   771
lemma of_rat_eq_id [simp]: "of_rat = id"
huffman@23343
   772
proof
wenzelm@63326
   773
  show "of_rat a = id a" for a
wenzelm@63326
   774
    by (induct a) (simp add: of_rat_rat Fract_of_int_eq [symmetric])
huffman@23343
   775
qed
huffman@23343
   776
wenzelm@63494
   777
text \<open>Collapse nested embeddings.\<close>
huffman@23343
   778
lemma of_rat_of_nat_eq [simp]: "of_rat (of_nat n) = of_nat n"
wenzelm@63326
   779
  by (induct n) (simp_all add: of_rat_add)
huffman@23343
   780
huffman@23343
   781
lemma of_rat_of_int_eq [simp]: "of_rat (of_int z) = of_int z"
wenzelm@63326
   782
  by (cases z rule: int_diff_cases) (simp add: of_rat_diff)
huffman@23343
   783
wenzelm@63326
   784
lemma of_rat_numeral_eq [simp]: "of_rat (numeral w) = numeral w"
wenzelm@63326
   785
  using of_rat_of_int_eq [of "numeral w"] by simp
huffman@47108
   786
wenzelm@63326
   787
lemma of_rat_neg_numeral_eq [simp]: "of_rat (- numeral w) = - numeral w"
wenzelm@63326
   788
  using of_rat_of_int_eq [of "- numeral w"] by simp
huffman@23343
   789
haftmann@23879
   790
lemmas zero_rat = Zero_rat_def
haftmann@23879
   791
lemmas one_rat = One_rat_def
haftmann@23879
   792
wenzelm@63326
   793
abbreviation rat_of_nat :: "nat \<Rightarrow> rat"
wenzelm@63326
   794
  where "rat_of_nat \<equiv> of_nat"
haftmann@24198
   795
wenzelm@63326
   796
abbreviation rat_of_int :: "int \<Rightarrow> rat"
wenzelm@63326
   797
  where "rat_of_int \<equiv> of_int"
wenzelm@63326
   798
haftmann@24198
   799
wenzelm@60758
   800
subsection \<open>The Set of Rational Numbers\<close>
berghofe@24533
   801
nipkow@28001
   802
context field_char_0
nipkow@28001
   803
begin
nipkow@28001
   804
wenzelm@61070
   805
definition Rats :: "'a set" ("\<rat>")
wenzelm@61070
   806
  where "\<rat> = range of_rat"
nipkow@28001
   807
nipkow@28001
   808
end
nipkow@28001
   809
wenzelm@61070
   810
lemma Rats_of_rat [simp]: "of_rat r \<in> \<rat>"
wenzelm@63326
   811
  by (simp add: Rats_def)
huffman@28010
   812
wenzelm@61070
   813
lemma Rats_of_int [simp]: "of_int z \<in> \<rat>"
wenzelm@63326
   814
  by (subst of_rat_of_int_eq [symmetric]) (rule Rats_of_rat)
huffman@28010
   815
wenzelm@61070
   816
lemma Rats_of_nat [simp]: "of_nat n \<in> \<rat>"
wenzelm@63326
   817
  by (subst of_rat_of_nat_eq [symmetric]) (rule Rats_of_rat)
huffman@28010
   818
wenzelm@61070
   819
lemma Rats_number_of [simp]: "numeral w \<in> \<rat>"
wenzelm@63326
   820
  by (subst of_rat_numeral_eq [symmetric]) (rule Rats_of_rat)
huffman@47108
   821
wenzelm@61070
   822
lemma Rats_0 [simp]: "0 \<in> \<rat>"
wenzelm@63326
   823
  unfolding Rats_def by (rule range_eqI) (rule of_rat_0 [symmetric])
huffman@28010
   824
wenzelm@61070
   825
lemma Rats_1 [simp]: "1 \<in> \<rat>"
wenzelm@63326
   826
  unfolding Rats_def by (rule range_eqI) (rule of_rat_1 [symmetric])
huffman@28010
   827
wenzelm@63326
   828
lemma Rats_add [simp]: "a \<in> \<rat> \<Longrightarrow> b \<in> \<rat> \<Longrightarrow> a + b \<in> \<rat>"
wenzelm@63326
   829
  apply (auto simp add: Rats_def)
wenzelm@63326
   830
  apply (rule range_eqI)
wenzelm@63326
   831
  apply (rule of_rat_add [symmetric])
wenzelm@63326
   832
  done
huffman@28010
   833
wenzelm@61070
   834
lemma Rats_minus [simp]: "a \<in> \<rat> \<Longrightarrow> - a \<in> \<rat>"
wenzelm@63326
   835
  apply (auto simp add: Rats_def)
wenzelm@63326
   836
  apply (rule range_eqI)
wenzelm@63326
   837
  apply (rule of_rat_minus [symmetric])
wenzelm@63326
   838
  done
huffman@28010
   839
wenzelm@63326
   840
lemma Rats_diff [simp]: "a \<in> \<rat> \<Longrightarrow> b \<in> \<rat> \<Longrightarrow> a - b \<in> \<rat>"
wenzelm@63326
   841
  apply (auto simp add: Rats_def)
wenzelm@63326
   842
  apply (rule range_eqI)
wenzelm@63326
   843
  apply (rule of_rat_diff [symmetric])
wenzelm@63326
   844
  done
huffman@28010
   845
wenzelm@63326
   846
lemma Rats_mult [simp]: "a \<in> \<rat> \<Longrightarrow> b \<in> \<rat> \<Longrightarrow> a * b \<in> \<rat>"
wenzelm@63326
   847
  apply (auto simp add: Rats_def)
wenzelm@63326
   848
  apply (rule range_eqI)
wenzelm@63326
   849
  apply (rule of_rat_mult [symmetric])
wenzelm@63326
   850
  done
huffman@28010
   851
wenzelm@63494
   852
lemma nonzero_Rats_inverse: "a \<in> \<rat> \<Longrightarrow> a \<noteq> 0 \<Longrightarrow> inverse a \<in> \<rat>"
wenzelm@63494
   853
  for a :: "'a::field_char_0"
wenzelm@63326
   854
  apply (auto simp add: Rats_def)
wenzelm@63326
   855
  apply (rule range_eqI)
wenzelm@63326
   856
  apply (erule nonzero_of_rat_inverse [symmetric])
wenzelm@63326
   857
  done
huffman@28010
   858
wenzelm@63494
   859
lemma Rats_inverse [simp]: "a \<in> \<rat> \<Longrightarrow> inverse a \<in> \<rat>"
wenzelm@63494
   860
  for a :: "'a::{field_char_0,field}"
wenzelm@63326
   861
  apply (auto simp add: Rats_def)
wenzelm@63326
   862
  apply (rule range_eqI)
wenzelm@63326
   863
  apply (rule of_rat_inverse [symmetric])
wenzelm@63326
   864
  done
huffman@28010
   865
wenzelm@63494
   866
lemma nonzero_Rats_divide: "a \<in> \<rat> \<Longrightarrow> b \<in> \<rat> \<Longrightarrow> b \<noteq> 0 \<Longrightarrow> a / b \<in> \<rat>"
wenzelm@63494
   867
  for a b :: "'a::field_char_0"
wenzelm@63326
   868
  apply (auto simp add: Rats_def)
wenzelm@63326
   869
  apply (rule range_eqI)
wenzelm@63326
   870
  apply (erule nonzero_of_rat_divide [symmetric])
wenzelm@63326
   871
  done
huffman@28010
   872
wenzelm@63494
   873
lemma Rats_divide [simp]: "a \<in> \<rat> \<Longrightarrow> b \<in> \<rat> \<Longrightarrow> a / b \<in> \<rat>"
wenzelm@63494
   874
  for a b :: "'a::{field_char_0, field}"
wenzelm@63326
   875
  apply (auto simp add: Rats_def)
wenzelm@63326
   876
  apply (rule range_eqI)
wenzelm@63326
   877
  apply (rule of_rat_divide [symmetric])
wenzelm@63326
   878
  done
huffman@28010
   879
wenzelm@63494
   880
lemma Rats_power [simp]: "a \<in> \<rat> \<Longrightarrow> a ^ n \<in> \<rat>"
wenzelm@63494
   881
  for a :: "'a::field_char_0"
wenzelm@63326
   882
  apply (auto simp add: Rats_def)
wenzelm@63326
   883
  apply (rule range_eqI)
wenzelm@63326
   884
  apply (rule of_rat_power [symmetric])
wenzelm@63326
   885
  done
huffman@28010
   886
huffman@28010
   887
lemma Rats_cases [cases set: Rats]:
huffman@28010
   888
  assumes "q \<in> \<rat>"
huffman@28010
   889
  obtains (of_rat) r where "q = of_rat r"
huffman@28010
   890
proof -
wenzelm@63494
   891
  from \<open>q \<in> \<rat>\<close> have "q \<in> range of_rat"
wenzelm@63494
   892
    by (simp only: Rats_def)
huffman@28010
   893
  then obtain r where "q = of_rat r" ..
huffman@28010
   894
  then show thesis ..
huffman@28010
   895
qed
huffman@28010
   896
wenzelm@63326
   897
lemma Rats_induct [case_names of_rat, induct set: Rats]: "q \<in> \<rat> \<Longrightarrow> (\<And>r. P (of_rat r)) \<Longrightarrow> P q"
huffman@28010
   898
  by (rule Rats_cases) auto
huffman@28010
   899
hoelzl@57275
   900
lemma Rats_infinite: "\<not> finite \<rat>"
hoelzl@57275
   901
  by (auto dest!: finite_imageD simp: inj_on_def infinite_UNIV_char_0 Rats_def)
nipkow@28001
   902
wenzelm@63326
   903
wenzelm@60758
   904
subsection \<open>Implementation of rational numbers as pairs of integers\<close>
berghofe@24533
   905
wenzelm@60758
   906
text \<open>Formal constructor\<close>
huffman@47108
   907
wenzelm@63326
   908
definition Frct :: "int \<times> int \<Rightarrow> rat"
wenzelm@63326
   909
  where [simp]: "Frct p = Fract (fst p) (snd p)"
haftmann@35369
   910
wenzelm@63326
   911
lemma [code abstype]: "Frct (quotient_of q) = q"
haftmann@36112
   912
  by (cases q) (auto intro: quotient_of_eq)
haftmann@35369
   913
huffman@47108
   914
wenzelm@60758
   915
text \<open>Numerals\<close>
haftmann@35369
   916
haftmann@35369
   917
declare quotient_of_Fract [code abstract]
haftmann@35369
   918
huffman@47108
   919
definition of_int :: "int \<Rightarrow> rat"
wenzelm@63326
   920
  where [code_abbrev]: "of_int = Int.of_int"
wenzelm@63326
   921
huffman@47108
   922
hide_const (open) of_int
huffman@47108
   923
wenzelm@63326
   924
lemma quotient_of_int [code abstract]: "quotient_of (Rat.of_int a) = (a, 1)"
huffman@47108
   925
  by (simp add: of_int_def of_int_rat quotient_of_Fract)
huffman@47108
   926
wenzelm@63326
   927
lemma [code_unfold]: "numeral k = Rat.of_int (numeral k)"
huffman@47108
   928
  by (simp add: Rat.of_int_def)
huffman@47108
   929
wenzelm@63326
   930
lemma [code_unfold]: "- numeral k = Rat.of_int (- numeral k)"
huffman@47108
   931
  by (simp add: Rat.of_int_def)
huffman@47108
   932
huffman@47108
   933
lemma Frct_code_post [code_post]:
huffman@47108
   934
  "Frct (0, a) = 0"
huffman@47108
   935
  "Frct (a, 0) = 0"
huffman@47108
   936
  "Frct (1, 1) = 1"
huffman@47108
   937
  "Frct (numeral k, 1) = numeral k"
huffman@47108
   938
  "Frct (1, numeral k) = 1 / numeral k"
huffman@47108
   939
  "Frct (numeral k, numeral l) = numeral k / numeral l"
haftmann@57576
   940
  "Frct (- a, b) = - Frct (a, b)"
haftmann@57576
   941
  "Frct (a, - b) = - Frct (a, b)"
haftmann@57576
   942
  "- (- Frct q) = Frct q"
huffman@47108
   943
  by (simp_all add: Fract_of_int_quotient)
huffman@47108
   944
huffman@47108
   945
wenzelm@60758
   946
text \<open>Operations\<close>
huffman@47108
   947
wenzelm@63326
   948
lemma rat_zero_code [code abstract]: "quotient_of 0 = (0, 1)"
haftmann@35369
   949
  by (simp add: Zero_rat_def quotient_of_Fract normalize_def)
haftmann@35369
   950
wenzelm@63326
   951
lemma rat_one_code [code abstract]: "quotient_of 1 = (1, 1)"
haftmann@35369
   952
  by (simp add: One_rat_def quotient_of_Fract normalize_def)
haftmann@35369
   953
haftmann@35369
   954
lemma rat_plus_code [code abstract]:
haftmann@35369
   955
  "quotient_of (p + q) = (let (a, c) = quotient_of p; (b, d) = quotient_of q
haftmann@35369
   956
     in normalize (a * d + b * c, c * d))"
haftmann@35369
   957
  by (cases p, cases q) (simp add: quotient_of_Fract)
haftmann@27652
   958
haftmann@35369
   959
lemma rat_uminus_code [code abstract]:
haftmann@35369
   960
  "quotient_of (- p) = (let (a, b) = quotient_of p in (- a, b))"
haftmann@35369
   961
  by (cases p) (simp add: quotient_of_Fract)
haftmann@35369
   962
haftmann@35369
   963
lemma rat_minus_code [code abstract]:
wenzelm@63326
   964
  "quotient_of (p - q) =
wenzelm@63326
   965
    (let (a, c) = quotient_of p; (b, d) = quotient_of q
haftmann@35369
   966
     in normalize (a * d - b * c, c * d))"
haftmann@35369
   967
  by (cases p, cases q) (simp add: quotient_of_Fract)
haftmann@35369
   968
haftmann@35369
   969
lemma rat_times_code [code abstract]:
wenzelm@63326
   970
  "quotient_of (p * q) =
wenzelm@63326
   971
    (let (a, c) = quotient_of p; (b, d) = quotient_of q
haftmann@35369
   972
     in normalize (a * b, c * d))"
haftmann@35369
   973
  by (cases p, cases q) (simp add: quotient_of_Fract)
berghofe@24533
   974
haftmann@35369
   975
lemma rat_inverse_code [code abstract]:
wenzelm@63326
   976
  "quotient_of (inverse p) =
wenzelm@63326
   977
    (let (a, b) = quotient_of p
wenzelm@63326
   978
     in if a = 0 then (0, 1) else (sgn a * b, \<bar>a\<bar>))"
haftmann@35369
   979
proof (cases p)
wenzelm@63326
   980
  case (Fract a b)
wenzelm@63326
   981
  then show ?thesis
haftmann@60688
   982
    by (cases "0::int" a rule: linorder_cases) (simp_all add: quotient_of_Fract gcd.commute)
haftmann@35369
   983
qed
haftmann@35369
   984
haftmann@35369
   985
lemma rat_divide_code [code abstract]:
wenzelm@63326
   986
  "quotient_of (p / q) =
wenzelm@63326
   987
    (let (a, c) = quotient_of p; (b, d) = quotient_of q
haftmann@35369
   988
     in normalize (a * d, c * b))"
haftmann@35369
   989
  by (cases p, cases q) (simp add: quotient_of_Fract)
haftmann@35369
   990
wenzelm@63326
   991
lemma rat_abs_code [code abstract]: "quotient_of \<bar>p\<bar> = (let (a, b) = quotient_of p in (\<bar>a\<bar>, b))"
haftmann@35369
   992
  by (cases p) (simp add: quotient_of_Fract)
haftmann@35369
   993
wenzelm@63326
   994
lemma rat_sgn_code [code abstract]: "quotient_of (sgn p) = (sgn (fst (quotient_of p)), 1)"
haftmann@35369
   995
proof (cases p)
wenzelm@63326
   996
  case (Fract a b)
wenzelm@63326
   997
  then show ?thesis
wenzelm@63326
   998
    by (cases "0::int" a rule: linorder_cases) (simp_all add: quotient_of_Fract)
haftmann@35369
   999
qed
berghofe@24533
  1000
wenzelm@63326
  1001
lemma rat_floor_code [code]: "\<lfloor>p\<rfloor> = (let (a, b) = quotient_of p in a div b)"
wenzelm@61942
  1002
  by (cases p) (simp add: quotient_of_Fract floor_Fract)
bulwahn@43733
  1003
haftmann@38857
  1004
instantiation rat :: equal
haftmann@26513
  1005
begin
haftmann@26513
  1006
wenzelm@63326
  1007
definition [code]: "HOL.equal a b \<longleftrightarrow> quotient_of a = quotient_of b"
haftmann@26513
  1008
wenzelm@63326
  1009
instance
wenzelm@63326
  1010
  by standard (simp add: equal_rat_def quotient_of_inject_eq)
haftmann@26513
  1011
wenzelm@63326
  1012
lemma rat_eq_refl [code nbe]: "HOL.equal (r::rat) r \<longleftrightarrow> True"
haftmann@38857
  1013
  by (rule equal_refl)
haftmann@28351
  1014
haftmann@26513
  1015
end
berghofe@24533
  1016
haftmann@35369
  1017
lemma rat_less_eq_code [code]:
haftmann@35369
  1018
  "p \<le> q \<longleftrightarrow> (let (a, c) = quotient_of p; (b, d) = quotient_of q in a * d \<le> c * b)"
haftmann@35726
  1019
  by (cases p, cases q) (simp add: quotient_of_Fract mult.commute)
berghofe@24533
  1020
haftmann@35369
  1021
lemma rat_less_code [code]:
haftmann@35369
  1022
  "p < q \<longleftrightarrow> (let (a, c) = quotient_of p; (b, d) = quotient_of q in a * d < c * b)"
haftmann@35726
  1023
  by (cases p, cases q) (simp add: quotient_of_Fract mult.commute)
berghofe@24533
  1024
wenzelm@63326
  1025
lemma [code]: "of_rat p = (let (a, b) = quotient_of p in of_int a / of_int b)"
haftmann@35369
  1026
  by (cases p) (simp add: quotient_of_Fract of_rat_rat)
haftmann@27652
  1027
huffman@47108
  1028
wenzelm@60758
  1029
text \<open>Quickcheck\<close>
huffman@47108
  1030
haftmann@31203
  1031
definition (in term_syntax)
wenzelm@63494
  1032
  valterm_fract :: "int \<times> (unit \<Rightarrow> Code_Evaluation.term) \<Rightarrow>
wenzelm@63494
  1033
    int \<times> (unit \<Rightarrow> Code_Evaluation.term) \<Rightarrow>
wenzelm@63326
  1034
    rat \<times> (unit \<Rightarrow> Code_Evaluation.term)"
wenzelm@63326
  1035
  where [code_unfold]: "valterm_fract k l = Code_Evaluation.valtermify Fract {\<cdot>} k {\<cdot>} l"
haftmann@31203
  1036
haftmann@37751
  1037
notation fcomp (infixl "\<circ>>" 60)
haftmann@37751
  1038
notation scomp (infixl "\<circ>\<rightarrow>" 60)
haftmann@31203
  1039
haftmann@31203
  1040
instantiation rat :: random
haftmann@31203
  1041
begin
haftmann@31203
  1042
haftmann@31203
  1043
definition
wenzelm@63326
  1044
  "Quickcheck_Random.random i =
wenzelm@63326
  1045
    Quickcheck_Random.random i \<circ>\<rightarrow> (\<lambda>num. Random.range i \<circ>\<rightarrow> (\<lambda>denom. Pair
wenzelm@63326
  1046
      (let j = int_of_integer (integer_of_natural (denom + 1))
wenzelm@63326
  1047
       in valterm_fract num (j, \<lambda>u. Code_Evaluation.term_of j))))"
haftmann@31203
  1048
haftmann@31203
  1049
instance ..
haftmann@31203
  1050
haftmann@31203
  1051
end
haftmann@31203
  1052
haftmann@37751
  1053
no_notation fcomp (infixl "\<circ>>" 60)
haftmann@37751
  1054
no_notation scomp (infixl "\<circ>\<rightarrow>" 60)
haftmann@31203
  1055
bulwahn@41920
  1056
instantiation rat :: exhaustive
bulwahn@41231
  1057
begin
bulwahn@41231
  1058
bulwahn@41231
  1059
definition
wenzelm@63326
  1060
  "exhaustive_rat f d =
wenzelm@63326
  1061
    Quickcheck_Exhaustive.exhaustive
wenzelm@63326
  1062
      (\<lambda>l. Quickcheck_Exhaustive.exhaustive
wenzelm@63326
  1063
        (\<lambda>k. f (Fract k (int_of_integer (integer_of_natural l) + 1))) d) d"
bulwahn@42311
  1064
bulwahn@42311
  1065
instance ..
bulwahn@42311
  1066
bulwahn@42311
  1067
end
bulwahn@42311
  1068
bulwahn@42311
  1069
instantiation rat :: full_exhaustive
bulwahn@42311
  1070
begin
bulwahn@42311
  1071
bulwahn@42311
  1072
definition
wenzelm@63326
  1073
  "full_exhaustive_rat f d =
wenzelm@63326
  1074
    Quickcheck_Exhaustive.full_exhaustive
wenzelm@63326
  1075
      (\<lambda>(l, _). Quickcheck_Exhaustive.full_exhaustive
wenzelm@63326
  1076
        (\<lambda>k. f
wenzelm@63326
  1077
          (let j = int_of_integer (integer_of_natural l) + 1
wenzelm@63326
  1078
           in valterm_fract k (j, \<lambda>_. Code_Evaluation.term_of j))) d) d"
bulwahn@43889
  1079
bulwahn@43889
  1080
instance ..
bulwahn@43889
  1081
bulwahn@43889
  1082
end
bulwahn@43889
  1083
wenzelm@63326
  1084
instance rat :: partial_term_of ..
wenzelm@63326
  1085
bulwahn@43889
  1086
lemma [code]:
wenzelm@63326
  1087
  "partial_term_of (ty :: rat itself) (Quickcheck_Narrowing.Narrowing_variable p tt) \<equiv>
wenzelm@63326
  1088
    Code_Evaluation.Free (STR ''_'') (Typerep.Typerep (STR ''Rat.rat'') [])"
wenzelm@63326
  1089
  "partial_term_of (ty :: rat itself) (Quickcheck_Narrowing.Narrowing_constructor 0 [l, k]) \<equiv>
wenzelm@63326
  1090
    Code_Evaluation.App
wenzelm@63326
  1091
      (Code_Evaluation.Const (STR ''Rat.Frct'')
wenzelm@63326
  1092
        (Typerep.Typerep (STR ''fun'')
wenzelm@63326
  1093
          [Typerep.Typerep (STR ''Product_Type.prod'')
wenzelm@63326
  1094
           [Typerep.Typerep (STR ''Int.int'') [], Typerep.Typerep (STR ''Int.int'') []],
wenzelm@63326
  1095
           Typerep.Typerep (STR ''Rat.rat'') []]))
wenzelm@63326
  1096
      (Code_Evaluation.App
wenzelm@63326
  1097
        (Code_Evaluation.App
wenzelm@63326
  1098
          (Code_Evaluation.Const (STR ''Product_Type.Pair'')
wenzelm@63326
  1099
            (Typerep.Typerep (STR ''fun'')
wenzelm@63326
  1100
              [Typerep.Typerep (STR ''Int.int'') [],
wenzelm@63326
  1101
               Typerep.Typerep (STR ''fun'')
wenzelm@63326
  1102
                [Typerep.Typerep (STR ''Int.int'') [],
wenzelm@63326
  1103
                 Typerep.Typerep (STR ''Product_Type.prod'')
wenzelm@63326
  1104
                 [Typerep.Typerep (STR ''Int.int'') [], Typerep.Typerep (STR ''Int.int'') []]]]))
wenzelm@63326
  1105
          (partial_term_of (TYPE(int)) l)) (partial_term_of (TYPE(int)) k))"
wenzelm@63326
  1106
  by (rule partial_term_of_anything)+
bulwahn@43889
  1107
bulwahn@43887
  1108
instantiation rat :: narrowing
bulwahn@43887
  1109
begin
bulwahn@43887
  1110
bulwahn@43887
  1111
definition
wenzelm@63326
  1112
  "narrowing =
wenzelm@63326
  1113
    Quickcheck_Narrowing.apply
wenzelm@63326
  1114
      (Quickcheck_Narrowing.apply
wenzelm@63326
  1115
        (Quickcheck_Narrowing.cons (\<lambda>nom denom. Fract nom denom)) narrowing) narrowing"
bulwahn@43887
  1116
bulwahn@43887
  1117
instance ..
bulwahn@43887
  1118
bulwahn@43887
  1119
end
bulwahn@43887
  1120
bulwahn@43887
  1121
wenzelm@60758
  1122
subsection \<open>Setup for Nitpick\<close>
berghofe@24533
  1123
wenzelm@60758
  1124
declaration \<open>
blanchet@38287
  1125
  Nitpick_HOL.register_frac_type @{type_name rat}
blanchet@62079
  1126
    [(@{const_name Abs_Rat}, @{const_name Nitpick.Abs_Frac}),
blanchet@62079
  1127
     (@{const_name zero_rat_inst.zero_rat}, @{const_name Nitpick.zero_frac}),
blanchet@62079
  1128
     (@{const_name one_rat_inst.one_rat}, @{const_name Nitpick.one_frac}),
blanchet@62079
  1129
     (@{const_name plus_rat_inst.plus_rat}, @{const_name Nitpick.plus_frac}),
blanchet@62079
  1130
     (@{const_name times_rat_inst.times_rat}, @{const_name Nitpick.times_frac}),
blanchet@62079
  1131
     (@{const_name uminus_rat_inst.uminus_rat}, @{const_name Nitpick.uminus_frac}),
blanchet@62079
  1132
     (@{const_name inverse_rat_inst.inverse_rat}, @{const_name Nitpick.inverse_frac}),
blanchet@62079
  1133
     (@{const_name ord_rat_inst.less_rat}, @{const_name Nitpick.less_frac}),
blanchet@62079
  1134
     (@{const_name ord_rat_inst.less_eq_rat}, @{const_name Nitpick.less_eq_frac}),
blanchet@62079
  1135
     (@{const_name field_char_0_class.of_rat}, @{const_name Nitpick.of_frac})]
wenzelm@60758
  1136
\<close>
blanchet@33197
  1137
wenzelm@63326
  1138
lemmas [nitpick_unfold] =
wenzelm@63326
  1139
  inverse_rat_inst.inverse_rat
huffman@47108
  1140
  one_rat_inst.one_rat ord_rat_inst.less_rat
blanchet@37397
  1141
  ord_rat_inst.less_eq_rat plus_rat_inst.plus_rat times_rat_inst.times_rat
blanchet@37397
  1142
  uminus_rat_inst.uminus_rat zero_rat_inst.zero_rat
blanchet@33197
  1143
wenzelm@52146
  1144
wenzelm@60758
  1145
subsection \<open>Float syntax\<close>
huffman@35343
  1146
huffman@35343
  1147
syntax "_Float" :: "float_const \<Rightarrow> 'a"    ("_")
huffman@35343
  1148
wenzelm@60758
  1149
parse_translation \<open>
wenzelm@52146
  1150
  let
wenzelm@52146
  1151
    fun mk_frac str =
wenzelm@52146
  1152
      let
wenzelm@52146
  1153
        val {mant = i, exp = n} = Lexicon.read_float str;
wenzelm@52146
  1154
        val exp = Syntax.const @{const_syntax Power.power};
haftmann@58410
  1155
        val ten = Numeral.mk_number_syntax 10;
haftmann@60352
  1156
        val exp10 = if n = 1 then ten else exp $ ten $ Numeral.mk_number_syntax n;
haftmann@60352
  1157
      in Syntax.const @{const_syntax Fields.inverse_divide} $ Numeral.mk_number_syntax i $ exp10 end;
wenzelm@52146
  1158
wenzelm@52146
  1159
    fun float_tr [(c as Const (@{syntax_const "_constrain"}, _)) $ t $ u] = c $ float_tr [t] $ u
wenzelm@52146
  1160
      | float_tr [t as Const (str, _)] = mk_frac str
wenzelm@52146
  1161
      | float_tr ts = raise TERM ("float_tr", ts);
wenzelm@52146
  1162
  in [(@{syntax_const "_Float"}, K float_tr)] end
wenzelm@60758
  1163
\<close>
huffman@35343
  1164
wenzelm@60758
  1165
text\<open>Test:\<close>
huffman@35343
  1166
lemma "123.456 = -111.111 + 200 + 30 + 4 + 5/10 + 6/100 + (7/1000::rat)"
wenzelm@52146
  1167
  by simp
huffman@35343
  1168
wenzelm@55974
  1169
wenzelm@60758
  1170
subsection \<open>Hiding implementation details\<close>
wenzelm@37143
  1171
huffman@47907
  1172
hide_const (open) normalize positive
wenzelm@37143
  1173
kuncar@53652
  1174
lifting_update rat.lifting
kuncar@53652
  1175
lifting_forget rat.lifting
huffman@47906
  1176
huffman@29880
  1177
end