author | haftmann |
Mon, 27 Nov 2006 13:42:30 +0100 | |
changeset 21545 | 54cc492d80a9 |
parent 21454 | a1937c51ed88 |
child 21911 | e29bcab0c81c |
permissions | -rw-r--r-- |
19039 | 1 |
(* Title: HOL/Library/ExecutableRat.thy |
2 |
ID: $Id$ |
|
3 |
Author: Florian Haftmann, TU Muenchen |
|
4 |
*) |
|
5 |
||
6 |
header {* Executable implementation of rational numbers in HOL *} |
|
7 |
||
8 |
theory ExecutableRat |
|
9 |
imports "~~/src/HOL/Real/Rational" "~~/src/HOL/NumberTheory/IntPrimes" |
|
10 |
begin |
|
11 |
||
12 |
text {* |
|
13 |
Actually nothing is proved about the implementation. |
|
14 |
*} |
|
15 |
||
19791 | 16 |
|
17 |
section {* HOL definitions *} |
|
18 |
||
19039 | 19 |
datatype erat = Rat bool int int |
20 |
||
19233
77ca20b0ed77
renamed HOL + - * etc. to HOL.plus HOL.minus HOL.times etc.
haftmann
parents:
19137
diff
changeset
|
21 |
instance erat :: zero .. |
77ca20b0ed77
renamed HOL + - * etc. to HOL.plus HOL.minus HOL.times etc.
haftmann
parents:
19137
diff
changeset
|
22 |
instance erat :: one .. |
77ca20b0ed77
renamed HOL + - * etc. to HOL.plus HOL.minus HOL.times etc.
haftmann
parents:
19137
diff
changeset
|
23 |
instance erat :: plus .. |
77ca20b0ed77
renamed HOL + - * etc. to HOL.plus HOL.minus HOL.times etc.
haftmann
parents:
19137
diff
changeset
|
24 |
instance erat :: minus .. |
77ca20b0ed77
renamed HOL + - * etc. to HOL.plus HOL.minus HOL.times etc.
haftmann
parents:
19137
diff
changeset
|
25 |
instance erat :: times .. |
77ca20b0ed77
renamed HOL + - * etc. to HOL.plus HOL.minus HOL.times etc.
haftmann
parents:
19137
diff
changeset
|
26 |
instance erat :: inverse .. |
77ca20b0ed77
renamed HOL + - * etc. to HOL.plus HOL.minus HOL.times etc.
haftmann
parents:
19137
diff
changeset
|
27 |
instance erat :: ord .. |
19039 | 28 |
|
19609 | 29 |
definition |
21404
eb85850d3eb7
more robust syntax for definition/abbreviation/notation;
wenzelm
parents:
21191
diff
changeset
|
30 |
norm :: "erat \<Rightarrow> erat" where |
eb85850d3eb7
more robust syntax for definition/abbreviation/notation;
wenzelm
parents:
21191
diff
changeset
|
31 |
"norm r = (case r of (Rat a p q) \<Rightarrow> |
19039 | 32 |
if p = 0 then Rat True 0 1 |
33 |
else |
|
34 |
let |
|
35 |
absp = abs p |
|
36 |
in let |
|
37 |
m = zgcd (absp, q) |
|
19609 | 38 |
in Rat (a = (0 <= p)) (absp div m) (q div m))" |
21404
eb85850d3eb7
more robust syntax for definition/abbreviation/notation;
wenzelm
parents:
21191
diff
changeset
|
39 |
|
eb85850d3eb7
more robust syntax for definition/abbreviation/notation;
wenzelm
parents:
21191
diff
changeset
|
40 |
definition |
eb85850d3eb7
more robust syntax for definition/abbreviation/notation;
wenzelm
parents:
21191
diff
changeset
|
41 |
common :: "(int * int) * (int * int) \<Rightarrow> (int * int) * int" where |
eb85850d3eb7
more robust syntax for definition/abbreviation/notation;
wenzelm
parents:
21191
diff
changeset
|
42 |
"common r = (case r of ((p1, q1), (p2, q2)) \<Rightarrow> |
19039 | 43 |
let q' = q1 * q2 div int (gcd (nat q1, nat q2)) |
19609 | 44 |
in ((p1 * (q' div q1), p2 * (q' div q2)), q'))" |
21404
eb85850d3eb7
more robust syntax for definition/abbreviation/notation;
wenzelm
parents:
21191
diff
changeset
|
45 |
|
eb85850d3eb7
more robust syntax for definition/abbreviation/notation;
wenzelm
parents:
21191
diff
changeset
|
46 |
definition |
eb85850d3eb7
more robust syntax for definition/abbreviation/notation;
wenzelm
parents:
21191
diff
changeset
|
47 |
of_quotient :: "int \<Rightarrow> int \<Rightarrow> erat" where |
eb85850d3eb7
more robust syntax for definition/abbreviation/notation;
wenzelm
parents:
21191
diff
changeset
|
48 |
"of_quotient a b = norm (Rat True a b)" |
eb85850d3eb7
more robust syntax for definition/abbreviation/notation;
wenzelm
parents:
21191
diff
changeset
|
49 |
|
eb85850d3eb7
more robust syntax for definition/abbreviation/notation;
wenzelm
parents:
21191
diff
changeset
|
50 |
definition |
eb85850d3eb7
more robust syntax for definition/abbreviation/notation;
wenzelm
parents:
21191
diff
changeset
|
51 |
of_rat :: "rat \<Rightarrow> erat" where |
eb85850d3eb7
more robust syntax for definition/abbreviation/notation;
wenzelm
parents:
21191
diff
changeset
|
52 |
"of_rat r = split of_quotient (SOME s. s : Rep_Rat r)" |
eb85850d3eb7
more robust syntax for definition/abbreviation/notation;
wenzelm
parents:
21191
diff
changeset
|
53 |
|
eb85850d3eb7
more robust syntax for definition/abbreviation/notation;
wenzelm
parents:
21191
diff
changeset
|
54 |
definition |
eb85850d3eb7
more robust syntax for definition/abbreviation/notation;
wenzelm
parents:
21191
diff
changeset
|
55 |
to_rat :: "erat \<Rightarrow> rat" where |
eb85850d3eb7
more robust syntax for definition/abbreviation/notation;
wenzelm
parents:
21191
diff
changeset
|
56 |
"to_rat r = (case r of (Rat a p q) \<Rightarrow> |
19609 | 57 |
if a then Fract p q else Fract (uminus p) q)" |
21404
eb85850d3eb7
more robust syntax for definition/abbreviation/notation;
wenzelm
parents:
21191
diff
changeset
|
58 |
|
eb85850d3eb7
more robust syntax for definition/abbreviation/notation;
wenzelm
parents:
21191
diff
changeset
|
59 |
definition |
eb85850d3eb7
more robust syntax for definition/abbreviation/notation;
wenzelm
parents:
21191
diff
changeset
|
60 |
eq_erat :: "erat \<Rightarrow> erat \<Rightarrow> bool" where |
20597 | 61 |
"eq_erat r s = (norm r = norm s)" |
19039 | 62 |
|
20701 | 63 |
axiomatization |
64 |
div_zero :: erat |
|
65 |
||
19039 | 66 |
defs (overloaded) |
19791 | 67 |
zero_rat_def: "0 == Rat True 0 1" |
68 |
one_rat_def: "1 == Rat True 1 1" |
|
69 |
add_rat_def: "r + s == case r of Rat a1 p1 q1 \<Rightarrow> case s of Rat a2 p2 q2 \<Rightarrow> |
|
19039 | 70 |
let |
71 |
((r1, r2), den) = common ((p1, q1), (p2, q2)) |
|
72 |
in let |
|
73 |
num = (if a1 then r1 else -r1) + (if a2 then r2 else -r2) |
|
74 |
in norm (Rat True num den)" |
|
19791 | 75 |
uminus_rat_def: "- r == case r of Rat a p q \<Rightarrow> |
19039 | 76 |
if p = 0 then Rat a p q |
77 |
else Rat (\<not> a) p q" |
|
19791 | 78 |
times_rat_def: "r * s == case r of Rat a1 p1 q1 \<Rightarrow> case s of Rat a2 p2 q2 \<Rightarrow> |
19039 | 79 |
norm (Rat (a1 = a2) (p1 * p2) (q1 * q2))" |
19791 | 80 |
inverse_rat_def: "inverse r == case r of Rat a p q \<Rightarrow> |
20701 | 81 |
if p = 0 then div_zero |
19039 | 82 |
else Rat a q p" |
19791 | 83 |
le_rat_def: "r <= s == case r of Rat a1 p1 q1 \<Rightarrow> case s of Rat a2 p2 q2 \<Rightarrow> |
19039 | 84 |
(\<not> a1 \<and> a2) \<or> |
85 |
(\<not> (a1 \<and> \<not> a2) \<and> |
|
86 |
(let |
|
87 |
((r1, r2), dummy) = common ((p1, q1), (p2, q2)) |
|
88 |
in if a1 then r1 <= r2 else r2 <= r1))" |
|
89 |
||
19791 | 90 |
|
20701 | 91 |
section {* code lemmas *} |
92 |
||
20934 | 93 |
lemma number_of_rat [code unfold]: |
94 |
"(number_of k \<Colon> rat) \<equiv> Fract k 1" |
|
20701 | 95 |
unfolding Fract_of_int_eq rat_number_of_def by simp |
96 |
||
20934 | 97 |
declare divide_inverse [where ?'a = rat, code func] |
98 |
||
20701 | 99 |
instance rat :: eq .. |
20934 | 100 |
instance erat :: eq .. |
20701 | 101 |
|
102 |
||
103 |
section {* code names *} |
|
104 |
||
21191 | 105 |
code_modulename SML |
106 |
ExecutableRat Rational |
|
20701 | 107 |
|
108 |
||
20934 | 109 |
section {* rat as abstype *} |
110 |
||
21191 | 111 |
lemma [code func]: -- {* prevents eq constraint *} |
112 |
shows "All = All" |
|
113 |
and "contents = contents" by rule+ |
|
114 |
||
20934 | 115 |
code_abstype rat erat where |
116 |
Fract \<equiv> of_quotient |
|
117 |
"0 \<Colon> rat" \<equiv> "0 \<Colon> erat" |
|
118 |
"1 \<Colon> rat" \<equiv> "1 \<Colon> erat" |
|
119 |
"op + \<Colon> rat \<Rightarrow> rat \<Rightarrow> rat" \<equiv> "op + \<Colon> erat \<Rightarrow> erat \<Rightarrow> erat" |
|
120 |
"uminus \<Colon> rat \<Rightarrow> rat" \<equiv> "uminus \<Colon> erat \<Rightarrow> erat" |
|
121 |
"op * \<Colon> rat \<Rightarrow> rat \<Rightarrow> rat" \<equiv> "op * \<Colon> erat \<Rightarrow> erat \<Rightarrow> erat" |
|
122 |
"inverse \<Colon> rat \<Rightarrow> rat" \<equiv> "inverse \<Colon> erat \<Rightarrow> erat" |
|
123 |
"op \<le> \<Colon> rat \<Rightarrow> rat \<Rightarrow> bool" \<equiv> "op \<le> \<Colon> erat \<Rightarrow> erat \<Rightarrow> bool" |
|
21454 | 124 |
"op = \<Colon> rat \<Rightarrow> rat \<Rightarrow> bool" \<equiv> eq_erat |
20934 | 125 |
|
126 |
code_const div_zero |
|
127 |
(SML "raise/ (Fail/ \"Division by zero\")") |
|
128 |
(Haskell "error/ \"Division by zero\"") |
|
129 |
||
130 |
code_gen |
|
131 |
Fract |
|
132 |
"0 \<Colon> rat" |
|
133 |
"1 \<Colon> rat" |
|
134 |
"op + \<Colon> rat \<Rightarrow> rat \<Rightarrow> rat" |
|
135 |
"uminus \<Colon> rat \<Rightarrow> rat" |
|
136 |
"op * \<Colon> rat \<Rightarrow> rat \<Rightarrow> rat" |
|
137 |
"inverse \<Colon> rat \<Rightarrow> rat" |
|
138 |
"op \<le> \<Colon> rat \<Rightarrow> rat \<Rightarrow> bool" |
|
21454 | 139 |
"op = \<Colon> rat \<Rightarrow> rat \<Rightarrow> bool" |
21545 | 140 |
(SML #) |
20934 | 141 |
|
142 |
||
19791 | 143 |
section {* type serializations *} |
144 |
||
145 |
types_code |
|
146 |
rat ("{*erat*}") |
|
147 |
||
148 |
||
149 |
section {* const serializations *} |
|
150 |
||
151 |
consts_code |
|
20701 | 152 |
div_zero ("raise/ (Fail/ \"non-defined rational number\")") |
19791 | 153 |
Fract ("{*of_quotient*}") |
20713
823967ef47f1
renamed 0 and 1 to HOL.zero and HOL.one respectivly; introduced corresponding syntactic classes
haftmann
parents:
20701
diff
changeset
|
154 |
HOL.zero :: rat ("{*0::erat*}") |
823967ef47f1
renamed 0 and 1 to HOL.zero and HOL.one respectivly; introduced corresponding syntactic classes
haftmann
parents:
20701
diff
changeset
|
155 |
HOL.one :: rat ("{*1::erat*}") |
19791 | 156 |
HOL.plus :: "rat \<Rightarrow> rat \<Rightarrow> rat" ("{*op + :: erat \<Rightarrow> erat \<Rightarrow> erat*}") |
157 |
uminus :: "rat \<Rightarrow> rat" ("{*uminus :: erat \<Rightarrow> erat*}") |
|
158 |
HOL.times :: "rat \<Rightarrow> rat \<Rightarrow> rat" ("{*op * :: erat \<Rightarrow> erat \<Rightarrow> erat*}") |
|
159 |
inverse :: "rat \<Rightarrow> rat" ("{*inverse :: erat \<Rightarrow> erat*}") |
|
160 |
divide :: "rat \<Rightarrow> rat \<Rightarrow> rat" ("{*op * :: erat \<Rightarrow> erat \<Rightarrow> erat*}/ _/ ({*inverse :: erat \<Rightarrow> erat*}/ _)") |
|
161 |
Orderings.less_eq :: "rat \<Rightarrow> rat \<Rightarrow> bool" ("{*op <= :: erat \<Rightarrow> erat \<Rightarrow> bool*}") |
|
20934 | 162 |
"op =" :: "rat \<Rightarrow> rat \<Rightarrow> bool" ("{*eq_erat*}") |
19889 | 163 |
|
19039 | 164 |
end |