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