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