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