src/HOL/Real/Rational.thy
author huffman
Wed Jul 09 20:18:06 2008 +0200 (2008-07-09)
changeset 27509 63161d5f8f29
parent 26732 6ea9de67e576
child 27551 9a5543d4cc24
permissions -rw-r--r--
rearrange instantiations
paulson@14365
     1
(*  Title: HOL/Library/Rational.thy
paulson@14365
     2
    ID:    $Id$
paulson@14365
     3
    Author: Markus Wenzel, TU Muenchen
paulson@14365
     4
*)
paulson@14365
     5
wenzelm@14691
     6
header {* Rational numbers *}
paulson@14365
     7
nipkow@15131
     8
theory Rational
haftmann@25762
     9
imports "~~/src/HOL/Library/Abstract_Rat"
haftmann@16417
    10
uses ("rat_arith.ML")
nipkow@15131
    11
begin
paulson@14365
    12
huffman@27509
    13
subsection {* Equivalence of fractions *}
paulson@14365
    14
wenzelm@19765
    15
definition
wenzelm@21404
    16
  fraction :: "(int \<times> int) set" where
wenzelm@19765
    17
  "fraction = {x. snd x \<noteq> 0}"
huffman@18913
    18
wenzelm@21404
    19
definition
wenzelm@21404
    20
  ratrel :: "((int \<times> int) \<times> (int \<times> int)) set" where
wenzelm@19765
    21
  "ratrel = {(x,y). snd x \<noteq> 0 \<and> snd y \<noteq> 0 \<and> fst x * snd y = fst y * snd x}"
paulson@14365
    22
huffman@18913
    23
lemma fraction_iff [simp]: "(x \<in> fraction) = (snd x \<noteq> 0)"
huffman@18913
    24
by (simp add: fraction_def)
paulson@14365
    25
huffman@18913
    26
lemma ratrel_iff [simp]:
huffman@18913
    27
  "((x,y) \<in> ratrel) =
huffman@18913
    28
   (snd x \<noteq> 0 \<and> snd y \<noteq> 0 \<and> fst x * snd y = fst y * snd x)"
huffman@18913
    29
by (simp add: ratrel_def)
paulson@14365
    30
huffman@18913
    31
lemma refl_ratrel: "refl fraction ratrel"
huffman@18913
    32
by (auto simp add: refl_def fraction_def ratrel_def)
huffman@18913
    33
huffman@18913
    34
lemma sym_ratrel: "sym ratrel"
huffman@18913
    35
by (simp add: ratrel_def sym_def)
huffman@18913
    36
huffman@18913
    37
lemma trans_ratrel_lemma:
huffman@18913
    38
  assumes 1: "a * b' = a' * b"
huffman@18913
    39
  assumes 2: "a' * b'' = a'' * b'"
huffman@18913
    40
  assumes 3: "b' \<noteq> (0::int)"
huffman@18913
    41
  shows "a * b'' = a'' * b"
huffman@18913
    42
proof -
huffman@18913
    43
  have "b' * (a * b'') = b'' * (a * b')" by simp
huffman@18913
    44
  also note 1
huffman@18913
    45
  also have "b'' * (a' * b) = b * (a' * b'')" by simp
huffman@18913
    46
  also note 2
huffman@18913
    47
  also have "b * (a'' * b') = b' * (a'' * b)" by simp
huffman@18913
    48
  finally have "b' * (a * b'') = b' * (a'' * b)" .
huffman@18913
    49
  with 3 show "a * b'' = a'' * b" by simp
paulson@14365
    50
qed
paulson@14365
    51
huffman@18913
    52
lemma trans_ratrel: "trans ratrel"
huffman@18913
    53
by (auto simp add: trans_def elim: trans_ratrel_lemma)
huffman@18913
    54
huffman@18913
    55
lemma equiv_ratrel: "equiv fraction ratrel"
huffman@18913
    56
by (rule equiv.intro [OF refl_ratrel sym_ratrel trans_ratrel])
huffman@18913
    57
huffman@18913
    58
lemmas equiv_ratrel_iff [iff] = eq_equiv_class_iff [OF equiv_ratrel]
huffman@18913
    59
huffman@18913
    60
lemma equiv_ratrel_iff2:
huffman@18913
    61
  "\<lbrakk>snd x \<noteq> 0; snd y \<noteq> 0\<rbrakk>
huffman@18913
    62
    \<Longrightarrow> (ratrel `` {x} = ratrel `` {y}) = ((x,y) \<in> ratrel)"
huffman@18913
    63
by (rule eq_equiv_class_iff [OF equiv_ratrel], simp_all)
paulson@14365
    64
paulson@14365
    65
huffman@27509
    66
subsection {* The type of rational numbers *}
paulson@14365
    67
huffman@18913
    68
typedef (Rat) rat = "fraction//ratrel"
huffman@18913
    69
proof
huffman@18913
    70
  have "(0,1) \<in> fraction" by (simp add: fraction_def)
huffman@18913
    71
  thus "ratrel``{(0,1)} \<in> fraction//ratrel" by (rule quotientI)
paulson@14365
    72
qed
paulson@14365
    73
huffman@18913
    74
lemma ratrel_in_Rat [simp]: "snd x \<noteq> 0 \<Longrightarrow> ratrel``{x} \<in> Rat"
huffman@18913
    75
by (simp add: Rat_def quotientI)
huffman@18913
    76
huffman@18913
    77
declare Abs_Rat_inject [simp]  Abs_Rat_inverse [simp]
huffman@18913
    78
huffman@18913
    79
wenzelm@19765
    80
definition
wenzelm@21404
    81
  Fract :: "int \<Rightarrow> int \<Rightarrow> rat" where
haftmann@24198
    82
  [code func del]: "Fract a b = Abs_Rat (ratrel``{(a,b)})"
haftmann@24198
    83
haftmann@24198
    84
lemma Fract_zero:
haftmann@24198
    85
  "Fract k 0 = Fract l 0"
haftmann@24198
    86
  by (simp add: Fract_def ratrel_def)
huffman@18913
    87
huffman@18913
    88
theorem Rat_cases [case_names Fract, cases type: rat]:
wenzelm@21404
    89
    "(!!a b. q = Fract a b ==> b \<noteq> 0 ==> C) ==> C"
wenzelm@21404
    90
  by (cases q) (clarsimp simp add: Fract_def Rat_def fraction_def quotient_def)
huffman@18913
    91
huffman@18913
    92
theorem Rat_induct [case_names Fract, induct type: rat]:
huffman@18913
    93
    "(!!a b. b \<noteq> 0 ==> P (Fract a b)) ==> P q"
huffman@18913
    94
  by (cases q) simp
huffman@18913
    95
huffman@18913
    96
huffman@27509
    97
subsection {* Congruence lemmas *}
paulson@14365
    98
huffman@18913
    99
lemma add_congruent2:
huffman@18913
   100
     "(\<lambda>x y. ratrel``{(fst x * snd y + fst y * snd x, snd x * snd y)})
huffman@18913
   101
      respects2 ratrel"
huffman@18913
   102
apply (rule equiv_ratrel [THEN congruent2_commuteI])
huffman@18913
   103
apply (simp_all add: left_distrib)
huffman@18913
   104
done
huffman@18913
   105
huffman@18913
   106
lemma minus_congruent:
huffman@18913
   107
  "(\<lambda>x. ratrel``{(- fst x, snd x)}) respects ratrel"
huffman@18913
   108
by (simp add: congruent_def)
huffman@18913
   109
huffman@18913
   110
lemma mult_congruent2:
huffman@18913
   111
  "(\<lambda>x y. ratrel``{(fst x * fst y, snd x * snd y)}) respects2 ratrel"
huffman@18913
   112
by (rule equiv_ratrel [THEN congruent2_commuteI], simp_all)
huffman@18913
   113
huffman@18913
   114
lemma inverse_congruent:
huffman@18913
   115
  "(\<lambda>x. ratrel``{if fst x=0 then (0,1) else (snd x, fst x)}) respects ratrel"
huffman@18913
   116
by (auto simp add: congruent_def mult_commute)
huffman@18913
   117
huffman@18913
   118
lemma le_congruent2:
huffman@18982
   119
  "(\<lambda>x y. {(fst x * snd y)*(snd x * snd y) \<le> (fst y * snd x)*(snd x * snd y)})
huffman@18913
   120
   respects2 ratrel"
huffman@18913
   121
proof (clarsimp simp add: congruent2_def)
huffman@18913
   122
  fix a b a' b' c d c' d'::int
paulson@14365
   123
  assume neq: "b \<noteq> 0"  "b' \<noteq> 0"  "d \<noteq> 0"  "d' \<noteq> 0"
huffman@18913
   124
  assume eq1: "a * b' = a' * b"
huffman@18913
   125
  assume eq2: "c * d' = c' * d"
paulson@14365
   126
paulson@14365
   127
  let ?le = "\<lambda>a b c d. ((a * d) * (b * d) \<le> (c * b) * (b * d))"
paulson@14365
   128
  {
paulson@14365
   129
    fix a b c d x :: int assume x: "x \<noteq> 0"
paulson@14365
   130
    have "?le a b c d = ?le (a * x) (b * x) c d"
paulson@14365
   131
    proof -
paulson@14365
   132
      from x have "0 < x * x" by (auto simp add: zero_less_mult_iff)
paulson@14365
   133
      hence "?le a b c d =
paulson@14365
   134
          ((a * d) * (b * d) * (x * x) \<le> (c * b) * (b * d) * (x * x))"
paulson@14365
   135
        by (simp add: mult_le_cancel_right)
paulson@14365
   136
      also have "... = ?le (a * x) (b * x) c d"
paulson@14365
   137
        by (simp add: mult_ac)
paulson@14365
   138
      finally show ?thesis .
paulson@14365
   139
    qed
paulson@14365
   140
  } note le_factor = this
paulson@14365
   141
paulson@14365
   142
  let ?D = "b * d" and ?D' = "b' * d'"
paulson@14365
   143
  from neq have D: "?D \<noteq> 0" by simp
paulson@14365
   144
  from neq have "?D' \<noteq> 0" by simp
paulson@14365
   145
  hence "?le a b c d = ?le (a * ?D') (b * ?D') c d"
paulson@14365
   146
    by (rule le_factor)
paulson@14365
   147
  also have "... = ((a * b') * ?D * ?D' * d * d' \<le> (c * d') * ?D * ?D' * b * b')"
paulson@14365
   148
    by (simp add: mult_ac)
paulson@14365
   149
  also have "... = ((a' * b) * ?D * ?D' * d * d' \<le> (c' * d) * ?D * ?D' * b * b')"
paulson@14365
   150
    by (simp only: eq1 eq2)
paulson@14365
   151
  also have "... = ?le (a' * ?D) (b' * ?D) c' d'"
paulson@14365
   152
    by (simp add: mult_ac)
paulson@14365
   153
  also from D have "... = ?le a' b' c' d'"
paulson@14365
   154
    by (rule le_factor [symmetric])
huffman@18913
   155
  finally show "?le a b c d = ?le a' b' c' d'" .
paulson@14365
   156
qed
paulson@14365
   157
huffman@18913
   158
lemmas UN_ratrel = UN_equiv_class [OF equiv_ratrel]
huffman@18913
   159
lemmas UN_ratrel2 = UN_equiv_class2 [OF equiv_ratrel equiv_ratrel]
paulson@14365
   160
paulson@14365
   161
huffman@27509
   162
subsection {* Rationals are a field *}
paulson@14365
   163
huffman@27509
   164
instantiation rat :: field
haftmann@25571
   165
begin
haftmann@25571
   166
haftmann@25571
   167
definition
haftmann@25571
   168
  Zero_rat_def [code func del]: "0 = Fract 0 1"
paulson@14365
   169
haftmann@25571
   170
definition
haftmann@25571
   171
  One_rat_def [code func del]: "1 = Fract 1 1"
huffman@18913
   172
haftmann@25571
   173
definition
haftmann@25571
   174
  add_rat_def [code func del]:
haftmann@25571
   175
   "q + r =
huffman@18913
   176
       Abs_Rat (\<Union>x \<in> Rep_Rat q. \<Union>y \<in> Rep_Rat r.
haftmann@25571
   177
           ratrel``{(fst x * snd y + fst y * snd x, snd x * snd y)})"
huffman@18913
   178
haftmann@25571
   179
definition
haftmann@25571
   180
  minus_rat_def [code func del]:
haftmann@25571
   181
    "- q = Abs_Rat (\<Union>x \<in> Rep_Rat q. ratrel``{(- fst x, snd x)})"
haftmann@25571
   182
haftmann@25571
   183
definition
haftmann@25571
   184
  diff_rat_def [code func del]: "q - r = q + - (r::rat)"
huffman@18913
   185
haftmann@25571
   186
definition
haftmann@25571
   187
  mult_rat_def [code func del]:
haftmann@25571
   188
   "q * r =
huffman@18913
   189
       Abs_Rat (\<Union>x \<in> Rep_Rat q. \<Union>y \<in> Rep_Rat r.
haftmann@25571
   190
           ratrel``{(fst x * fst y, snd x * snd y)})"
huffman@18913
   191
haftmann@25571
   192
definition
haftmann@25571
   193
  inverse_rat_def [code func del]:
haftmann@25571
   194
    "inverse q =
huffman@18913
   195
        Abs_Rat (\<Union>x \<in> Rep_Rat q.
huffman@18913
   196
            ratrel``{if fst x=0 then (0,1) else (snd x, fst x)})"
haftmann@25571
   197
haftmann@25571
   198
definition
haftmann@25571
   199
  divide_rat_def [code func del]: "q / r = q * inverse (r::rat)"
huffman@18913
   200
huffman@18913
   201
theorem eq_rat: "b \<noteq> 0 ==> d \<noteq> 0 ==>
huffman@18913
   202
  (Fract a b = Fract c d) = (a * d = c * b)"
huffman@18913
   203
by (simp add: Fract_def)
paulson@14365
   204
paulson@14365
   205
theorem add_rat: "b \<noteq> 0 ==> d \<noteq> 0 ==>
paulson@14365
   206
  Fract a b + Fract c d = Fract (a * d + c * b) (b * d)"
huffman@18913
   207
by (simp add: Fract_def add_rat_def add_congruent2 UN_ratrel2)
paulson@14365
   208
paulson@14365
   209
theorem minus_rat: "b \<noteq> 0 ==> -(Fract a b) = Fract (-a) b"
huffman@18913
   210
by (simp add: Fract_def minus_rat_def minus_congruent UN_ratrel)
paulson@14365
   211
paulson@14365
   212
theorem diff_rat: "b \<noteq> 0 ==> d \<noteq> 0 ==>
paulson@14365
   213
    Fract a b - Fract c d = Fract (a * d - c * b) (b * d)"
huffman@18913
   214
by (simp add: diff_rat_def add_rat minus_rat)
paulson@14365
   215
paulson@14365
   216
theorem mult_rat: "b \<noteq> 0 ==> d \<noteq> 0 ==>
paulson@14365
   217
  Fract a b * Fract c d = Fract (a * c) (b * d)"
huffman@18913
   218
by (simp add: Fract_def mult_rat_def mult_congruent2 UN_ratrel2)
paulson@14365
   219
huffman@18913
   220
theorem inverse_rat: "a \<noteq> 0 ==> b \<noteq> 0 ==>
paulson@14365
   221
  inverse (Fract a b) = Fract b a"
huffman@18913
   222
by (simp add: Fract_def inverse_rat_def inverse_congruent UN_ratrel)
paulson@14365
   223
huffman@18913
   224
theorem divide_rat: "c \<noteq> 0 ==> b \<noteq> 0 ==> d \<noteq> 0 ==>
paulson@14365
   225
  Fract a b / Fract c d = Fract (a * d) (b * c)"
huffman@18913
   226
by (simp add: divide_rat_def inverse_rat mult_rat)
paulson@14365
   227
huffman@27509
   228
instance proof
paulson@14365
   229
  fix q r s :: rat
paulson@14365
   230
  show "(q + r) + s = q + (r + s)"
huffman@18913
   231
    by (induct q, induct r, induct s)
huffman@18913
   232
       (simp add: add_rat add_ac mult_ac int_distrib)
paulson@14365
   233
  show "q + r = r + q"
paulson@14365
   234
    by (induct q, induct r) (simp add: add_rat add_ac mult_ac)
paulson@14365
   235
  show "0 + q = q"
haftmann@23879
   236
    by (induct q) (simp add: Zero_rat_def add_rat)
paulson@14365
   237
  show "(-q) + q = 0"
haftmann@23879
   238
    by (induct q) (simp add: Zero_rat_def minus_rat add_rat eq_rat)
paulson@14365
   239
  show "q - r = q + (-r)"
paulson@14365
   240
    by (induct q, induct r) (simp add: add_rat minus_rat diff_rat)
paulson@14365
   241
  show "(q * r) * s = q * (r * s)"
paulson@14365
   242
    by (induct q, induct r, induct s) (simp add: mult_rat mult_ac)
paulson@14365
   243
  show "q * r = r * q"
paulson@14365
   244
    by (induct q, induct r) (simp add: mult_rat mult_ac)
paulson@14365
   245
  show "1 * q = q"
haftmann@23879
   246
    by (induct q) (simp add: One_rat_def mult_rat)
paulson@14365
   247
  show "(q + r) * s = q * s + r * s"
wenzelm@14691
   248
    by (induct q, induct r, induct s)
paulson@14365
   249
       (simp add: add_rat mult_rat eq_rat int_distrib)
paulson@14365
   250
  show "q \<noteq> 0 ==> inverse q * q = 1"
haftmann@23879
   251
    by (induct q) (simp add: inverse_rat mult_rat One_rat_def Zero_rat_def eq_rat)
paulson@14430
   252
  show "q / r = q * inverse r"
wenzelm@14691
   253
    by (simp add: divide_rat_def)
paulson@14365
   254
  show "0 \<noteq> (1::rat)"
haftmann@23879
   255
    by (simp add: Zero_rat_def One_rat_def eq_rat)
paulson@14365
   256
qed
paulson@14365
   257
huffman@27509
   258
end
huffman@27509
   259
huffman@27509
   260
instantiation rat :: recpower
huffman@27509
   261
begin
huffman@27509
   262
huffman@27509
   263
primrec power_rat
huffman@27509
   264
where
huffman@27509
   265
  rat_power_0:     "q ^ 0       = (1\<Colon>rat)"
huffman@27509
   266
  | rat_power_Suc: "q ^ (Suc n) = (q\<Colon>rat) * (q ^ n)"
huffman@27509
   267
huffman@27509
   268
instance proof
huffman@27509
   269
  fix q :: rat
huffman@27509
   270
  fix n :: nat
huffman@27509
   271
  show "q ^ 0 = 1" by simp
huffman@27509
   272
  show "q ^ (Suc n) = q * (q ^ n)" by simp
huffman@27509
   273
qed
huffman@27509
   274
huffman@27509
   275
end
huffman@27509
   276
huffman@27509
   277
instance rat :: division_by_zero
paulson@14365
   278
proof
huffman@27509
   279
  show "inverse 0 = (0::rat)"
huffman@27509
   280
    by (simp add: Zero_rat_def Fract_def inverse_rat_def
huffman@27509
   281
                  inverse_congruent UN_ratrel)
huffman@27509
   282
qed
huffman@27509
   283
huffman@27509
   284
subsection {* Rationals are a linear order *}
huffman@27509
   285
huffman@27509
   286
instantiation rat :: linorder
huffman@27509
   287
begin
huffman@27509
   288
huffman@27509
   289
definition
huffman@27509
   290
  le_rat_def [code func del]:
huffman@27509
   291
   "q \<le> r \<longleftrightarrow> contents (\<Union>x \<in> Rep_Rat q. \<Union>y \<in> Rep_Rat r.
huffman@27509
   292
      {(fst x * snd y)*(snd x * snd y) \<le> (fst y * snd x)*(snd x * snd y)})"
huffman@27509
   293
huffman@27509
   294
definition
huffman@27509
   295
  less_rat_def [code func del]: "z < (w::rat) \<longleftrightarrow> z \<le> w \<and> z \<noteq> w"
huffman@27509
   296
huffman@27509
   297
theorem le_rat: "b \<noteq> 0 ==> d \<noteq> 0 ==>
huffman@27509
   298
  (Fract a b \<le> Fract c d) = ((a * d) * (b * d) \<le> (c * b) * (b * d))"
huffman@27509
   299
by (simp add: Fract_def le_rat_def le_congruent2 UN_ratrel2)
huffman@27509
   300
huffman@27509
   301
theorem less_rat: "b \<noteq> 0 ==> d \<noteq> 0 ==>
huffman@27509
   302
    (Fract a b < Fract c d) = ((a * d) * (b * d) < (c * b) * (b * d))"
huffman@27509
   303
by (simp add: less_rat_def le_rat eq_rat order_less_le)
huffman@27509
   304
huffman@27509
   305
instance proof
paulson@14365
   306
  fix q r s :: rat
paulson@14365
   307
  {
paulson@14365
   308
    assume "q \<le> r" and "r \<le> s"
paulson@14365
   309
    show "q \<le> s"
paulson@14365
   310
    proof (insert prems, induct q, induct r, induct s)
paulson@14365
   311
      fix a b c d e f :: int
paulson@14365
   312
      assume neq: "b \<noteq> 0"  "d \<noteq> 0"  "f \<noteq> 0"
paulson@14365
   313
      assume 1: "Fract a b \<le> Fract c d" and 2: "Fract c d \<le> Fract e f"
paulson@14365
   314
      show "Fract a b \<le> Fract e f"
paulson@14365
   315
      proof -
paulson@14365
   316
        from neq obtain bb: "0 < b * b" and dd: "0 < d * d" and ff: "0 < f * f"
paulson@14365
   317
          by (auto simp add: zero_less_mult_iff linorder_neq_iff)
paulson@14365
   318
        have "(a * d) * (b * d) * (f * f) \<le> (c * b) * (b * d) * (f * f)"
paulson@14365
   319
        proof -
paulson@14365
   320
          from neq 1 have "(a * d) * (b * d) \<le> (c * b) * (b * d)"
paulson@14365
   321
            by (simp add: le_rat)
paulson@14365
   322
          with ff show ?thesis by (simp add: mult_le_cancel_right)
paulson@14365
   323
        qed
paulson@14365
   324
        also have "... = (c * f) * (d * f) * (b * b)"
paulson@14365
   325
          by (simp only: mult_ac)
paulson@14365
   326
        also have "... \<le> (e * d) * (d * f) * (b * b)"
paulson@14365
   327
        proof -
paulson@14365
   328
          from neq 2 have "(c * f) * (d * f) \<le> (e * d) * (d * f)"
paulson@14365
   329
            by (simp add: le_rat)
paulson@14365
   330
          with bb show ?thesis by (simp add: mult_le_cancel_right)
paulson@14365
   331
        qed
paulson@14365
   332
        finally have "(a * f) * (b * f) * (d * d) \<le> e * b * (b * f) * (d * d)"
paulson@14365
   333
          by (simp only: mult_ac)
paulson@14365
   334
        with dd have "(a * f) * (b * f) \<le> (e * b) * (b * f)"
paulson@14365
   335
          by (simp add: mult_le_cancel_right)
paulson@14365
   336
        with neq show ?thesis by (simp add: le_rat)
paulson@14365
   337
      qed
paulson@14365
   338
    qed
paulson@14365
   339
  next
paulson@14365
   340
    assume "q \<le> r" and "r \<le> q"
paulson@14365
   341
    show "q = r"
paulson@14365
   342
    proof (insert prems, induct q, induct r)
paulson@14365
   343
      fix a b c d :: int
paulson@14365
   344
      assume neq: "b \<noteq> 0"  "d \<noteq> 0"
paulson@14365
   345
      assume 1: "Fract a b \<le> Fract c d" and 2: "Fract c d \<le> Fract a b"
paulson@14365
   346
      show "Fract a b = Fract c d"
paulson@14365
   347
      proof -
paulson@14365
   348
        from neq 1 have "(a * d) * (b * d) \<le> (c * b) * (b * d)"
paulson@14365
   349
          by (simp add: le_rat)
paulson@14365
   350
        also have "... \<le> (a * d) * (b * d)"
paulson@14365
   351
        proof -
paulson@14365
   352
          from neq 2 have "(c * b) * (d * b) \<le> (a * d) * (d * b)"
paulson@14365
   353
            by (simp add: le_rat)
paulson@14365
   354
          thus ?thesis by (simp only: mult_ac)
paulson@14365
   355
        qed
paulson@14365
   356
        finally have "(a * d) * (b * d) = (c * b) * (b * d)" .
paulson@14365
   357
        moreover from neq have "b * d \<noteq> 0" by simp
paulson@14365
   358
        ultimately have "a * d = c * b" by simp
paulson@14365
   359
        with neq show ?thesis by (simp add: eq_rat)
paulson@14365
   360
      qed
paulson@14365
   361
    qed
paulson@14365
   362
  next
paulson@14365
   363
    show "q \<le> q"
paulson@14365
   364
      by (induct q) (simp add: le_rat)
paulson@14365
   365
    show "(q < r) = (q \<le> r \<and> q \<noteq> r)"
paulson@14365
   366
      by (simp only: less_rat_def)
paulson@14365
   367
    show "q \<le> r \<or> r \<le> q"
huffman@18913
   368
      by (induct q, induct r)
huffman@18913
   369
         (simp add: le_rat mult_commute, rule linorder_linear)
paulson@14365
   370
  }
paulson@14365
   371
qed
paulson@14365
   372
huffman@27509
   373
end
huffman@27509
   374
haftmann@25571
   375
instantiation rat :: distrib_lattice
haftmann@25571
   376
begin
haftmann@25571
   377
haftmann@25571
   378
definition
haftmann@25571
   379
  "(inf \<Colon> rat \<Rightarrow> rat \<Rightarrow> rat) = min"
haftmann@25571
   380
haftmann@25571
   381
definition
haftmann@25571
   382
  "(sup \<Colon> rat \<Rightarrow> rat \<Rightarrow> rat) = max"
haftmann@25571
   383
haftmann@25571
   384
instance
haftmann@22456
   385
  by default (auto simp add: min_max.sup_inf_distrib1 inf_rat_def sup_rat_def)
haftmann@22456
   386
haftmann@25571
   387
end
haftmann@25571
   388
huffman@27509
   389
subsection {* Rationals are an ordered field *}
huffman@27509
   390
huffman@27509
   391
instantiation rat :: ordered_field
huffman@27509
   392
begin
huffman@27509
   393
huffman@27509
   394
definition
huffman@27509
   395
  abs_rat_def: "\<bar>q\<bar> = (if q < 0 then -q else (q::rat))"
huffman@27509
   396
huffman@27509
   397
definition
huffman@27509
   398
  sgn_rat_def: "sgn (q::rat) = (if q=0 then 0 else if 0<q then 1 else - 1)"
huffman@27509
   399
huffman@27509
   400
theorem abs_rat: "b \<noteq> 0 ==> \<bar>Fract a b\<bar> = Fract \<bar>a\<bar> \<bar>b\<bar>"
huffman@27509
   401
  by (simp add: abs_rat_def minus_rat Zero_rat_def less_rat eq_rat)
huffman@27509
   402
     (auto simp add: mult_less_0_iff zero_less_mult_iff order_le_less
huffman@27509
   403
                split: abs_split)
huffman@27509
   404
huffman@27509
   405
instance proof
paulson@14365
   406
  fix q r s :: rat
paulson@14365
   407
  show "q \<le> r ==> s + q \<le> s + r"
paulson@14365
   408
  proof (induct q, induct r, induct s)
paulson@14365
   409
    fix a b c d e f :: int
paulson@14365
   410
    assume neq: "b \<noteq> 0"  "d \<noteq> 0"  "f \<noteq> 0"
paulson@14365
   411
    assume le: "Fract a b \<le> Fract c d"
paulson@14365
   412
    show "Fract e f + Fract a b \<le> Fract e f + Fract c d"
paulson@14365
   413
    proof -
paulson@14365
   414
      let ?F = "f * f" from neq have F: "0 < ?F"
paulson@14365
   415
        by (auto simp add: zero_less_mult_iff)
paulson@14365
   416
      from neq le have "(a * d) * (b * d) \<le> (c * b) * (b * d)"
paulson@14365
   417
        by (simp add: le_rat)
paulson@14365
   418
      with F have "(a * d) * (b * d) * ?F * ?F \<le> (c * b) * (b * d) * ?F * ?F"
paulson@14365
   419
        by (simp add: mult_le_cancel_right)
paulson@14365
   420
      with neq show ?thesis by (simp add: add_rat le_rat mult_ac int_distrib)
paulson@14365
   421
    qed
paulson@14365
   422
  qed
paulson@14365
   423
  show "q < r ==> 0 < s ==> s * q < s * r"
paulson@14365
   424
  proof (induct q, induct r, induct s)
paulson@14365
   425
    fix a b c d e f :: int
paulson@14365
   426
    assume neq: "b \<noteq> 0"  "d \<noteq> 0"  "f \<noteq> 0"
paulson@14365
   427
    assume le: "Fract a b < Fract c d"
paulson@14365
   428
    assume gt: "0 < Fract e f"
paulson@14365
   429
    show "Fract e f * Fract a b < Fract e f * Fract c d"
paulson@14365
   430
    proof -
paulson@14365
   431
      let ?E = "e * f" and ?F = "f * f"
paulson@14365
   432
      from neq gt have "0 < ?E"
haftmann@23879
   433
        by (auto simp add: Zero_rat_def less_rat le_rat order_less_le eq_rat)
paulson@14365
   434
      moreover from neq have "0 < ?F"
paulson@14365
   435
        by (auto simp add: zero_less_mult_iff)
paulson@14365
   436
      moreover from neq le have "(a * d) * (b * d) < (c * b) * (b * d)"
paulson@14365
   437
        by (simp add: less_rat)
paulson@14365
   438
      ultimately have "(a * d) * (b * d) * ?E * ?F < (c * b) * (b * d) * ?E * ?F"
paulson@14365
   439
        by (simp add: mult_less_cancel_right)
paulson@14365
   440
      with neq show ?thesis
paulson@14365
   441
        by (simp add: less_rat mult_rat mult_ac)
paulson@14365
   442
    qed
paulson@14365
   443
  qed
paulson@14365
   444
  show "\<bar>q\<bar> = (if q < 0 then -q else q)"
paulson@14365
   445
    by (simp only: abs_rat_def)
nipkow@24506
   446
qed (auto simp: sgn_rat_def)
paulson@14365
   447
huffman@27509
   448
end
paulson@14365
   449
paulson@14365
   450
subsection {* Various Other Results *}
paulson@14365
   451
paulson@14365
   452
lemma minus_rat_cancel [simp]: "b \<noteq> 0 ==> Fract (-a) (-b) = Fract a b"
huffman@18913
   453
by (simp add: eq_rat)
paulson@14365
   454
paulson@14365
   455
theorem Rat_induct_pos [case_names Fract, induct type: rat]:
paulson@14365
   456
  assumes step: "!!a b. 0 < b ==> P (Fract a b)"
paulson@14365
   457
    shows "P q"
paulson@14365
   458
proof (cases q)
paulson@14365
   459
  have step': "!!a b. b < 0 ==> P (Fract a b)"
paulson@14365
   460
  proof -
paulson@14365
   461
    fix a::int and b::int
paulson@14365
   462
    assume b: "b < 0"
paulson@14365
   463
    hence "0 < -b" by simp
paulson@14365
   464
    hence "P (Fract (-a) (-b))" by (rule step)
paulson@14365
   465
    thus "P (Fract a b)" by (simp add: order_less_imp_not_eq [OF b])
paulson@14365
   466
  qed
paulson@14365
   467
  case (Fract a b)
paulson@14365
   468
  thus "P q" by (force simp add: linorder_neq_iff step step')
paulson@14365
   469
qed
paulson@14365
   470
paulson@14365
   471
lemma zero_less_Fract_iff:
paulson@14365
   472
     "0 < b ==> (0 < Fract a b) = (0 < a)"
haftmann@23879
   473
by (simp add: Zero_rat_def less_rat order_less_imp_not_eq2 zero_less_mult_iff)
paulson@14365
   474
paulson@14378
   475
lemma Fract_add_one: "n \<noteq> 0 ==> Fract (m + n) n = Fract m n + 1"
paulson@14378
   476
apply (insert add_rat [of concl: m n 1 1])
haftmann@23879
   477
apply (simp add: One_rat_def [symmetric])
paulson@14378
   478
done
paulson@14378
   479
huffman@23429
   480
lemma of_nat_rat: "of_nat k = Fract (of_nat k) 1"
haftmann@23879
   481
by (induct k) (simp_all add: Zero_rat_def One_rat_def add_rat)
huffman@23429
   482
huffman@23429
   483
lemma of_int_rat: "of_int k = Fract k 1"
huffman@23429
   484
by (cases k rule: int_diff_cases, simp add: of_nat_rat diff_rat)
huffman@23429
   485
paulson@14378
   486
lemma Fract_of_nat_eq: "Fract (of_nat k) 1 = of_nat k"
huffman@23429
   487
by (rule of_nat_rat [symmetric])
paulson@14378
   488
paulson@14378
   489
lemma Fract_of_int_eq: "Fract k 1 = of_int k"
huffman@23429
   490
by (rule of_int_rat [symmetric])
paulson@14378
   491
haftmann@24198
   492
lemma Fract_of_int_quotient: "Fract k l = (if l = 0 then Fract 1 0 else of_int k / of_int l)"
haftmann@24198
   493
by (auto simp add: Fract_zero Fract_of_int_eq [symmetric] divide_rat)
haftmann@24198
   494
paulson@14378
   495
wenzelm@14691
   496
subsection {* Numerals and Arithmetic *}
paulson@14387
   497
haftmann@25571
   498
instantiation rat :: number_ring
haftmann@25571
   499
begin
paulson@14387
   500
haftmann@25571
   501
definition
haftmann@25965
   502
  rat_number_of_def [code func del]: "number_of w = (of_int w \<Colon> rat)"
haftmann@25571
   503
haftmann@25571
   504
instance
haftmann@25571
   505
  by default (simp add: rat_number_of_def)
haftmann@25571
   506
haftmann@25571
   507
end 
paulson@14387
   508
paulson@14387
   509
use "rat_arith.ML"
wenzelm@24075
   510
declaration {* K rat_arith_setup *}
paulson@14387
   511
huffman@23342
   512
huffman@23342
   513
subsection {* Embedding from Rationals to other Fields *}
huffman@23342
   514
haftmann@24198
   515
class field_char_0 = field + ring_char_0
huffman@23342
   516
haftmann@25571
   517
instance ordered_field < field_char_0 .. 
huffman@23342
   518
huffman@23342
   519
definition
huffman@23342
   520
  of_rat :: "rat \<Rightarrow> 'a::field_char_0"
huffman@23342
   521
where
haftmann@24198
   522
  [code func del]: "of_rat q = contents (\<Union>(a,b) \<in> Rep_Rat q. {of_int a / of_int b})"
huffman@23342
   523
huffman@23342
   524
lemma of_rat_congruent:
huffman@23342
   525
  "(\<lambda>(a, b). {of_int a / of_int b::'a::field_char_0}) respects ratrel"
huffman@23342
   526
apply (rule congruent.intro)
huffman@23342
   527
apply (clarsimp simp add: nonzero_divide_eq_eq nonzero_eq_divide_eq)
huffman@23342
   528
apply (simp only: of_int_mult [symmetric])
huffman@23342
   529
done
huffman@23342
   530
huffman@23342
   531
lemma of_rat_rat:
huffman@23342
   532
  "b \<noteq> 0 \<Longrightarrow> of_rat (Fract a b) = of_int a / of_int b"
huffman@23342
   533
unfolding Fract_def of_rat_def
huffman@23342
   534
by (simp add: UN_ratrel of_rat_congruent)
huffman@23342
   535
huffman@23342
   536
lemma of_rat_0 [simp]: "of_rat 0 = 0"
huffman@23342
   537
by (simp add: Zero_rat_def of_rat_rat)
huffman@23342
   538
huffman@23342
   539
lemma of_rat_1 [simp]: "of_rat 1 = 1"
huffman@23342
   540
by (simp add: One_rat_def of_rat_rat)
huffman@23342
   541
huffman@23342
   542
lemma of_rat_add: "of_rat (a + b) = of_rat a + of_rat b"
huffman@23342
   543
by (induct a, induct b, simp add: add_rat of_rat_rat add_frac_eq)
huffman@23342
   544
huffman@23343
   545
lemma of_rat_minus: "of_rat (- a) = - of_rat a"
huffman@23343
   546
by (induct a, simp add: minus_rat of_rat_rat)
huffman@23343
   547
huffman@23343
   548
lemma of_rat_diff: "of_rat (a - b) = of_rat a - of_rat b"
huffman@23343
   549
by (simp only: diff_minus of_rat_add of_rat_minus)
huffman@23343
   550
huffman@23342
   551
lemma of_rat_mult: "of_rat (a * b) = of_rat a * of_rat b"
huffman@23342
   552
apply (induct a, induct b, simp add: mult_rat of_rat_rat)
huffman@23342
   553
apply (simp add: divide_inverse nonzero_inverse_mult_distrib mult_ac)
huffman@23342
   554
done
huffman@23342
   555
huffman@23342
   556
lemma nonzero_of_rat_inverse:
huffman@23342
   557
  "a \<noteq> 0 \<Longrightarrow> of_rat (inverse a) = inverse (of_rat a)"
huffman@23343
   558
apply (rule inverse_unique [symmetric])
huffman@23343
   559
apply (simp add: of_rat_mult [symmetric])
huffman@23342
   560
done
huffman@23342
   561
huffman@23342
   562
lemma of_rat_inverse:
huffman@23342
   563
  "(of_rat (inverse a)::'a::{field_char_0,division_by_zero}) =
huffman@23342
   564
   inverse (of_rat a)"
huffman@23342
   565
by (cases "a = 0", simp_all add: nonzero_of_rat_inverse)
huffman@23342
   566
huffman@23342
   567
lemma nonzero_of_rat_divide:
huffman@23342
   568
  "b \<noteq> 0 \<Longrightarrow> of_rat (a / b) = of_rat a / of_rat b"
huffman@23342
   569
by (simp add: divide_inverse of_rat_mult nonzero_of_rat_inverse)
huffman@23342
   570
huffman@23342
   571
lemma of_rat_divide:
huffman@23342
   572
  "(of_rat (a / b)::'a::{field_char_0,division_by_zero})
huffman@23342
   573
   = of_rat a / of_rat b"
huffman@23342
   574
by (cases "b = 0", simp_all add: nonzero_of_rat_divide)
huffman@23342
   575
huffman@23343
   576
lemma of_rat_power:
huffman@23343
   577
  "(of_rat (a ^ n)::'a::{field_char_0,recpower}) = of_rat a ^ n"
huffman@23343
   578
by (induct n) (simp_all add: of_rat_mult power_Suc)
huffman@23343
   579
huffman@23343
   580
lemma of_rat_eq_iff [simp]: "(of_rat a = of_rat b) = (a = b)"
huffman@23343
   581
apply (induct a, induct b)
huffman@23343
   582
apply (simp add: of_rat_rat eq_rat)
huffman@23343
   583
apply (simp add: nonzero_divide_eq_eq nonzero_eq_divide_eq)
huffman@23343
   584
apply (simp only: of_int_mult [symmetric] of_int_eq_iff)
huffman@23343
   585
done
huffman@23343
   586
huffman@23343
   587
lemmas of_rat_eq_0_iff [simp] = of_rat_eq_iff [of _ 0, simplified]
huffman@23343
   588
huffman@23343
   589
lemma of_rat_eq_id [simp]: "of_rat = (id :: rat \<Rightarrow> rat)"
huffman@23343
   590
proof
huffman@23343
   591
  fix a
huffman@23343
   592
  show "of_rat a = id a"
huffman@23343
   593
  by (induct a)
huffman@23343
   594
     (simp add: of_rat_rat divide_rat Fract_of_int_eq [symmetric])
huffman@23343
   595
qed
huffman@23343
   596
huffman@23343
   597
text{*Collapse nested embeddings*}
huffman@23343
   598
lemma of_rat_of_nat_eq [simp]: "of_rat (of_nat n) = of_nat n"
huffman@23343
   599
by (induct n) (simp_all add: of_rat_add)
huffman@23343
   600
huffman@23343
   601
lemma of_rat_of_int_eq [simp]: "of_rat (of_int z) = of_int z"
huffman@23365
   602
by (cases z rule: int_diff_cases, simp add: of_rat_diff)
huffman@23343
   603
huffman@23343
   604
lemma of_rat_number_of_eq [simp]:
huffman@23343
   605
  "of_rat (number_of w) = (number_of w :: 'a::{number_ring,field_char_0})"
huffman@23343
   606
by (simp add: number_of_eq)
huffman@23343
   607
haftmann@23879
   608
lemmas zero_rat = Zero_rat_def
haftmann@23879
   609
lemmas one_rat = One_rat_def
haftmann@23879
   610
haftmann@24198
   611
abbreviation
haftmann@24198
   612
  rat_of_nat :: "nat \<Rightarrow> rat"
haftmann@24198
   613
where
haftmann@24198
   614
  "rat_of_nat \<equiv> of_nat"
haftmann@24198
   615
haftmann@24198
   616
abbreviation
haftmann@24198
   617
  rat_of_int :: "int \<Rightarrow> rat"
haftmann@24198
   618
where
haftmann@24198
   619
  "rat_of_int \<equiv> of_int"
haftmann@24198
   620
berghofe@24533
   621
berghofe@24533
   622
subsection {* Implementation of rational numbers as pairs of integers *}
berghofe@24533
   623
berghofe@24533
   624
definition
haftmann@24622
   625
  Rational :: "int \<times> int \<Rightarrow> rat"
berghofe@24533
   626
where
haftmann@24622
   627
  "Rational = INum"
berghofe@24533
   628
haftmann@24622
   629
code_datatype Rational
berghofe@24533
   630
haftmann@24622
   631
lemma Rational_simp:
haftmann@24622
   632
  "Rational (k, l) = rat_of_int k / rat_of_int l"
haftmann@24622
   633
  unfolding Rational_def INum_def by simp
berghofe@24533
   634
haftmann@24622
   635
lemma Rational_zero [simp]: "Rational 0\<^sub>N = 0"
haftmann@24622
   636
  by (simp add: Rational_simp)
berghofe@24533
   637
haftmann@24622
   638
lemma Rational_lit [simp]: "Rational i\<^sub>N = rat_of_int i"
haftmann@24622
   639
  by (simp add: Rational_simp)
berghofe@24533
   640
berghofe@24533
   641
lemma zero_rat_code [code, code unfold]:
haftmann@24622
   642
  "0 = Rational 0\<^sub>N" by simp
haftmann@25965
   643
declare zero_rat_code [symmetric, code post]
berghofe@24533
   644
haftmann@25965
   645
lemma one_rat_code [code, code unfold]:
haftmann@24622
   646
  "1 = Rational 1\<^sub>N" by simp
haftmann@25965
   647
declare one_rat_code [symmetric, code post]
berghofe@24533
   648
haftmann@25965
   649
lemma [code unfold, symmetric, code post]:
berghofe@24533
   650
  "number_of k = rat_of_int (number_of k)"
berghofe@24533
   651
  by (simp add: number_of_is_id rat_number_of_def)
berghofe@24533
   652
berghofe@24533
   653
definition
berghofe@24533
   654
  [code func del]: "Fract' (b\<Colon>bool) k l = Fract k l"
berghofe@24533
   655
berghofe@24533
   656
lemma [code]:
berghofe@24533
   657
  "Fract k l = Fract' (l \<noteq> 0) k l"
berghofe@24533
   658
  unfolding Fract'_def ..
berghofe@24533
   659
berghofe@24533
   660
lemma [code]:
haftmann@24622
   661
  "Fract' True k l = (if l \<noteq> 0 then Rational (k, l) else Fract 1 0)"
haftmann@24622
   662
  by (simp add: Fract'_def Rational_simp Fract_of_int_quotient [of k l])
berghofe@24533
   663
berghofe@24533
   664
lemma [code]:
haftmann@24622
   665
  "of_rat (Rational (k, l)) = (if l \<noteq> 0 then of_int k / of_int l else 0)"
berghofe@24533
   666
  by (cases "l = 0")
haftmann@24622
   667
    (auto simp add: Rational_simp of_rat_rat [simplified Fract_of_int_quotient [of k l], symmetric])
berghofe@24533
   668
haftmann@26513
   669
instantiation rat :: eq
haftmann@26513
   670
begin
haftmann@26513
   671
haftmann@26732
   672
definition [code func del]: "eq_class.eq (r\<Colon>rat) s \<longleftrightarrow> r = s"
berghofe@24533
   673
haftmann@26513
   674
instance by default (simp add: eq_rat_def)
haftmann@26513
   675
haftmann@26732
   676
lemma rat_eq_code [code]: "eq_class.eq (Rational x) (Rational y) \<longleftrightarrow> eq_class.eq (normNum x) (normNum y)"
haftmann@26513
   677
  unfolding Rational_def INum_normNum_iff eq ..
haftmann@26513
   678
haftmann@26513
   679
end
berghofe@24533
   680
haftmann@24622
   681
lemma rat_less_eq_code [code]: "Rational x \<le> Rational y \<longleftrightarrow> normNum x \<le>\<^sub>N normNum y"
berghofe@24533
   682
proof -
haftmann@24622
   683
  have "normNum x \<le>\<^sub>N normNum y \<longleftrightarrow> Rational (normNum x) \<le> Rational (normNum y)" 
haftmann@24622
   684
    by (simp add: Rational_def del: normNum)
haftmann@24622
   685
  also have "\<dots> = (Rational x \<le> Rational y)" by (simp add: Rational_def)
berghofe@24533
   686
  finally show ?thesis by simp
berghofe@24533
   687
qed
berghofe@24533
   688
haftmann@24622
   689
lemma rat_less_code [code]: "Rational x < Rational y \<longleftrightarrow> normNum x <\<^sub>N normNum y"
berghofe@24533
   690
proof -
haftmann@24622
   691
  have "normNum x <\<^sub>N normNum y \<longleftrightarrow> Rational (normNum x) < Rational (normNum y)" 
haftmann@24622
   692
    by (simp add: Rational_def del: normNum)
haftmann@24622
   693
  also have "\<dots> = (Rational x < Rational y)" by (simp add: Rational_def)
berghofe@24533
   694
  finally show ?thesis by simp
berghofe@24533
   695
qed
berghofe@24533
   696
haftmann@24622
   697
lemma rat_add_code [code]: "Rational x + Rational y = Rational (x +\<^sub>N y)"
haftmann@24622
   698
  unfolding Rational_def by simp
berghofe@24533
   699
haftmann@24622
   700
lemma rat_mul_code [code]: "Rational x * Rational y = Rational (x *\<^sub>N y)"
haftmann@24622
   701
  unfolding Rational_def by simp
berghofe@24533
   702
haftmann@24622
   703
lemma rat_neg_code [code]: "- Rational x = Rational (~\<^sub>N x)"
haftmann@24622
   704
  unfolding Rational_def by simp
berghofe@24533
   705
haftmann@24622
   706
lemma rat_sub_code [code]: "Rational x - Rational y = Rational (x -\<^sub>N y)"
haftmann@24622
   707
  unfolding Rational_def by simp
berghofe@24533
   708
haftmann@24622
   709
lemma rat_inv_code [code]: "inverse (Rational x) = Rational (Ninv x)"
haftmann@24622
   710
  unfolding Rational_def Ninv divide_rat_def by simp
berghofe@24533
   711
haftmann@24622
   712
lemma rat_div_code [code]: "Rational x / Rational y = Rational (x \<div>\<^sub>N y)"
haftmann@24622
   713
  unfolding Rational_def by simp
berghofe@24533
   714
haftmann@24622
   715
text {* Setup for SML code generator *}
berghofe@24533
   716
berghofe@24533
   717
types_code
berghofe@24533
   718
  rat ("(int */ int)")
berghofe@24533
   719
attach (term_of) {*
berghofe@24533
   720
fun term_of_rat (p, q) =
haftmann@24622
   721
  let
haftmann@24661
   722
    val rT = Type ("Rational.rat", [])
berghofe@24533
   723
  in
berghofe@24533
   724
    if q = 1 orelse p = 0 then HOLogic.mk_number rT p
berghofe@25885
   725
    else @{term "op / \<Colon> rat \<Rightarrow> rat \<Rightarrow> rat"} $
berghofe@24533
   726
      HOLogic.mk_number rT p $ HOLogic.mk_number rT q
berghofe@24533
   727
  end;
berghofe@24533
   728
*}
berghofe@24533
   729
attach (test) {*
berghofe@24533
   730
fun gen_rat i =
berghofe@24533
   731
  let
berghofe@24533
   732
    val p = random_range 0 i;
berghofe@24533
   733
    val q = random_range 1 (i + 1);
berghofe@24533
   734
    val g = Integer.gcd p q;
wenzelm@24630
   735
    val p' = p div g;
wenzelm@24630
   736
    val q' = q div g;
berghofe@25885
   737
    val r = (if one_of [true, false] then p' else ~ p',
berghofe@25885
   738
      if p' = 0 then 0 else q')
berghofe@24533
   739
  in
berghofe@25885
   740
    (r, fn () => term_of_rat r)
berghofe@24533
   741
  end;
berghofe@24533
   742
*}
berghofe@24533
   743
berghofe@24533
   744
consts_code
haftmann@24622
   745
  Rational ("(_)")
berghofe@24533
   746
berghofe@24533
   747
consts_code
berghofe@24533
   748
  "of_int :: int \<Rightarrow> rat" ("\<module>rat'_of'_int")
berghofe@24533
   749
attach {*
berghofe@24533
   750
fun rat_of_int 0 = (0, 0)
berghofe@24533
   751
  | rat_of_int i = (i, 1);
berghofe@24533
   752
*}
berghofe@24533
   753
paulson@14365
   754
end