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