src/HOL/Library/Executable_Rat.thy
author wenzelm
Fri, 17 Aug 2007 00:03:50 +0200
changeset 24300 e170cee91c66
parent 24223 fa9421d52c74
permissions -rw-r--r--
proper signature for Meson;
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
23854
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
     1
(*  Title:      HOL/Library/Executable_Rat.thy
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
     2
    ID:         $Id$
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
     3
    Author:     Florian Haftmann, TU Muenchen
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
     4
*)
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
     5
24197
c9e3cb5e5681 proper implementation of rational numbers
haftmann
parents: 23854
diff changeset
     6
header {* Implementation of rational numbers as pairs of integers *}
23854
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
     7
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
     8
theory Executable_Rat
24197
c9e3cb5e5681 proper implementation of rational numbers
haftmann
parents: 23854
diff changeset
     9
imports Abstract_Rat "~~/src/HOL/Real/Rational"
23854
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
    10
begin
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
    11
24197
c9e3cb5e5681 proper implementation of rational numbers
haftmann
parents: 23854
diff changeset
    12
hide (open) const Rat
23854
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
    13
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
    14
definition
24197
c9e3cb5e5681 proper implementation of rational numbers
haftmann
parents: 23854
diff changeset
    15
  Rat :: "int \<times> int \<Rightarrow> rat"
c9e3cb5e5681 proper implementation of rational numbers
haftmann
parents: 23854
diff changeset
    16
where
c9e3cb5e5681 proper implementation of rational numbers
haftmann
parents: 23854
diff changeset
    17
  "Rat = INum"
c9e3cb5e5681 proper implementation of rational numbers
haftmann
parents: 23854
diff changeset
    18
c9e3cb5e5681 proper implementation of rational numbers
haftmann
parents: 23854
diff changeset
    19
code_datatype Rat
23854
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
    20
24197
c9e3cb5e5681 proper implementation of rational numbers
haftmann
parents: 23854
diff changeset
    21
lemma Rat_simp:
c9e3cb5e5681 proper implementation of rational numbers
haftmann
parents: 23854
diff changeset
    22
  "Rat (k, l) = rat_of_int k / rat_of_int l"
c9e3cb5e5681 proper implementation of rational numbers
haftmann
parents: 23854
diff changeset
    23
  unfolding Rat_def INum_def by simp
23854
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
    24
24197
c9e3cb5e5681 proper implementation of rational numbers
haftmann
parents: 23854
diff changeset
    25
lemma Rat_zero [simp]: "Rat 0\<^sub>N = 0"
c9e3cb5e5681 proper implementation of rational numbers
haftmann
parents: 23854
diff changeset
    26
  by (simp add: Rat_simp)
c9e3cb5e5681 proper implementation of rational numbers
haftmann
parents: 23854
diff changeset
    27
c9e3cb5e5681 proper implementation of rational numbers
haftmann
parents: 23854
diff changeset
    28
lemma Rat_lit [simp]: "Rat i\<^sub>N = rat_of_int i"
c9e3cb5e5681 proper implementation of rational numbers
haftmann
parents: 23854
diff changeset
    29
  by (simp add: Rat_simp)
c9e3cb5e5681 proper implementation of rational numbers
haftmann
parents: 23854
diff changeset
    30
c9e3cb5e5681 proper implementation of rational numbers
haftmann
parents: 23854
diff changeset
    31
lemma zero_rat_code [code]:
c9e3cb5e5681 proper implementation of rational numbers
haftmann
parents: 23854
diff changeset
    32
  "0 = Rat 0\<^sub>N" by simp
23854
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
    33
24197
c9e3cb5e5681 proper implementation of rational numbers
haftmann
parents: 23854
diff changeset
    34
lemma zero_rat_code [code]:
c9e3cb5e5681 proper implementation of rational numbers
haftmann
parents: 23854
diff changeset
    35
  "1 = Rat 1\<^sub>N" by simp
23854
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
    36
24197
c9e3cb5e5681 proper implementation of rational numbers
haftmann
parents: 23854
diff changeset
    37
lemma [code, code unfold]:
c9e3cb5e5681 proper implementation of rational numbers
haftmann
parents: 23854
diff changeset
    38
  "number_of k = rat_of_int (number_of k)"
c9e3cb5e5681 proper implementation of rational numbers
haftmann
parents: 23854
diff changeset
    39
  by (simp add: number_of_is_id rat_number_of_def)
c9e3cb5e5681 proper implementation of rational numbers
haftmann
parents: 23854
diff changeset
    40
c9e3cb5e5681 proper implementation of rational numbers
haftmann
parents: 23854
diff changeset
    41
definition
c9e3cb5e5681 proper implementation of rational numbers
haftmann
parents: 23854
diff changeset
    42
  [code func del]: "Fract' (b\<Colon>bool) k l = Fract k l"
23854
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
    43
24197
c9e3cb5e5681 proper implementation of rational numbers
haftmann
parents: 23854
diff changeset
    44
lemma [code]:
c9e3cb5e5681 proper implementation of rational numbers
haftmann
parents: 23854
diff changeset
    45
  "Fract k l = Fract' (l \<noteq> 0) k l"
c9e3cb5e5681 proper implementation of rational numbers
haftmann
parents: 23854
diff changeset
    46
  unfolding Fract'_def ..
23854
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
    47
24197
c9e3cb5e5681 proper implementation of rational numbers
haftmann
parents: 23854
diff changeset
    48
lemma [code]:
c9e3cb5e5681 proper implementation of rational numbers
haftmann
parents: 23854
diff changeset
    49
  "Fract' True k l = (if l \<noteq> 0 then Rat (k, l) else Fract 1 0)"
c9e3cb5e5681 proper implementation of rational numbers
haftmann
parents: 23854
diff changeset
    50
  by (simp add: Fract'_def Rat_simp Fract_of_int_quotient [of k l])
23854
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
    51
24197
c9e3cb5e5681 proper implementation of rational numbers
haftmann
parents: 23854
diff changeset
    52
lemma [code]:
c9e3cb5e5681 proper implementation of rational numbers
haftmann
parents: 23854
diff changeset
    53
  "of_rat (Rat (k, l)) = (if l \<noteq> 0 then of_int k / of_int l else 0)"
c9e3cb5e5681 proper implementation of rational numbers
haftmann
parents: 23854
diff changeset
    54
  by (cases "l = 0")
c9e3cb5e5681 proper implementation of rational numbers
haftmann
parents: 23854
diff changeset
    55
    (auto simp add: Rat_simp of_rat_rat [simplified Fract_of_int_quotient [of k l], symmetric])
23854
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
    56
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
    57
instance rat :: eq ..
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
    58
24197
c9e3cb5e5681 proper implementation of rational numbers
haftmann
parents: 23854
diff changeset
    59
lemma rat_eq_code [code]: "Rat x = Rat y \<longleftrightarrow> normNum x = normNum y"
c9e3cb5e5681 proper implementation of rational numbers
haftmann
parents: 23854
diff changeset
    60
  unfolding Rat_def INum_normNum_iff ..
c9e3cb5e5681 proper implementation of rational numbers
haftmann
parents: 23854
diff changeset
    61
c9e3cb5e5681 proper implementation of rational numbers
haftmann
parents: 23854
diff changeset
    62
lemma rat_less_eq_code [code]: "Rat x \<le> Rat y \<longleftrightarrow> normNum x \<le>\<^sub>N normNum y"
c9e3cb5e5681 proper implementation of rational numbers
haftmann
parents: 23854
diff changeset
    63
proof -
c9e3cb5e5681 proper implementation of rational numbers
haftmann
parents: 23854
diff changeset
    64
  have "normNum x \<le>\<^sub>N normNum y \<longleftrightarrow> Rat (normNum x) \<le> Rat (normNum y)" 
c9e3cb5e5681 proper implementation of rational numbers
haftmann
parents: 23854
diff changeset
    65
    by (simp add: Rat_def del: normNum)
c9e3cb5e5681 proper implementation of rational numbers
haftmann
parents: 23854
diff changeset
    66
  also have "\<dots> = (Rat x \<le> Rat y)" by (simp add: Rat_def)
c9e3cb5e5681 proper implementation of rational numbers
haftmann
parents: 23854
diff changeset
    67
  finally show ?thesis by simp
c9e3cb5e5681 proper implementation of rational numbers
haftmann
parents: 23854
diff changeset
    68
qed
c9e3cb5e5681 proper implementation of rational numbers
haftmann
parents: 23854
diff changeset
    69
c9e3cb5e5681 proper implementation of rational numbers
haftmann
parents: 23854
diff changeset
    70
lemma rat_less_code [code]: "Rat x < Rat y \<longleftrightarrow> normNum x <\<^sub>N normNum y"
c9e3cb5e5681 proper implementation of rational numbers
haftmann
parents: 23854
diff changeset
    71
proof -
c9e3cb5e5681 proper implementation of rational numbers
haftmann
parents: 23854
diff changeset
    72
  have "normNum x <\<^sub>N normNum y \<longleftrightarrow> Rat (normNum x) < Rat (normNum y)" 
c9e3cb5e5681 proper implementation of rational numbers
haftmann
parents: 23854
diff changeset
    73
    by (simp add: Rat_def del: normNum)
c9e3cb5e5681 proper implementation of rational numbers
haftmann
parents: 23854
diff changeset
    74
  also have "\<dots> = (Rat x < Rat y)" by (simp add: Rat_def)
c9e3cb5e5681 proper implementation of rational numbers
haftmann
parents: 23854
diff changeset
    75
  finally show ?thesis by simp
c9e3cb5e5681 proper implementation of rational numbers
haftmann
parents: 23854
diff changeset
    76
qed
c9e3cb5e5681 proper implementation of rational numbers
haftmann
parents: 23854
diff changeset
    77
c9e3cb5e5681 proper implementation of rational numbers
haftmann
parents: 23854
diff changeset
    78
lemma rat_add_code [code]: "Rat x + Rat y = Rat (x +\<^sub>N y)"
c9e3cb5e5681 proper implementation of rational numbers
haftmann
parents: 23854
diff changeset
    79
  unfolding Rat_def by simp
c9e3cb5e5681 proper implementation of rational numbers
haftmann
parents: 23854
diff changeset
    80
c9e3cb5e5681 proper implementation of rational numbers
haftmann
parents: 23854
diff changeset
    81
lemma rat_mul_code [code]: "Rat x * Rat y = Rat (x *\<^sub>N y)"
c9e3cb5e5681 proper implementation of rational numbers
haftmann
parents: 23854
diff changeset
    82
  unfolding Rat_def by simp
c9e3cb5e5681 proper implementation of rational numbers
haftmann
parents: 23854
diff changeset
    83
c9e3cb5e5681 proper implementation of rational numbers
haftmann
parents: 23854
diff changeset
    84
lemma rat_neg_code [code]: "- Rat x = Rat (~\<^sub>N x)"
c9e3cb5e5681 proper implementation of rational numbers
haftmann
parents: 23854
diff changeset
    85
  unfolding Rat_def by simp
c9e3cb5e5681 proper implementation of rational numbers
haftmann
parents: 23854
diff changeset
    86
c9e3cb5e5681 proper implementation of rational numbers
haftmann
parents: 23854
diff changeset
    87
lemma rat_sub_code [code]: "Rat x - Rat y = Rat (x -\<^sub>N y)"
c9e3cb5e5681 proper implementation of rational numbers
haftmann
parents: 23854
diff changeset
    88
  unfolding Rat_def by simp
c9e3cb5e5681 proper implementation of rational numbers
haftmann
parents: 23854
diff changeset
    89
c9e3cb5e5681 proper implementation of rational numbers
haftmann
parents: 23854
diff changeset
    90
lemma rat_inv_code [code]: "inverse (Rat x) = Rat (Ninv x)"
c9e3cb5e5681 proper implementation of rational numbers
haftmann
parents: 23854
diff changeset
    91
  unfolding Rat_def Ninv divide_rat_def by simp
c9e3cb5e5681 proper implementation of rational numbers
haftmann
parents: 23854
diff changeset
    92
c9e3cb5e5681 proper implementation of rational numbers
haftmann
parents: 23854
diff changeset
    93
lemma rat_div_code [code]: "Rat x / Rat y = Rat (x \<div>\<^sub>N y)"
c9e3cb5e5681 proper implementation of rational numbers
haftmann
parents: 23854
diff changeset
    94
  unfolding Rat_def by simp
23854
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
    95
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
    96
code_modulename SML
24223
fa9421d52c74 corrected code generator module names
haftmann
parents: 24197
diff changeset
    97
  Rational Rational
23854
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
    98
  Executable_Rat Rational
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
    99
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
   100
code_modulename OCaml
24223
fa9421d52c74 corrected code generator module names
haftmann
parents: 24197
diff changeset
   101
  Rational Rational
23854
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
   102
  Executable_Rat Rational
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
   103
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
   104
code_modulename Haskell
24223
fa9421d52c74 corrected code generator module names
haftmann
parents: 24197
diff changeset
   105
  Rational Rational
23854
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
   106
  Executable_Rat Rational
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
   107
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
   108
end