haftmann@24999
|
1 |
(* ID: $Id$
|
haftmann@24999
|
2 |
Author: Florian Haftmann, TU Muenchen
|
haftmann@24999
|
3 |
*)
|
haftmann@24999
|
4 |
|
haftmann@24999
|
5 |
header {* Type of indices *}
|
haftmann@24999
|
6 |
|
haftmann@24999
|
7 |
theory Code_Index
|
haftmann@28228
|
8 |
imports Plain "~~/src/HOL/Code_Eval" "~~/src/HOL/Presburger"
|
haftmann@24999
|
9 |
begin
|
haftmann@24999
|
10 |
|
haftmann@24999
|
11 |
text {*
|
haftmann@25767
|
12 |
Indices are isomorphic to HOL @{typ nat} but
|
haftmann@27104
|
13 |
mapped to target-language builtin integers.
|
haftmann@24999
|
14 |
*}
|
haftmann@24999
|
15 |
|
haftmann@24999
|
16 |
subsection {* Datatype of indices *}
|
haftmann@24999
|
17 |
|
haftmann@26140
|
18 |
typedef index = "UNIV \<Colon> nat set"
|
haftmann@26140
|
19 |
morphisms nat_of_index index_of_nat by rule
|
haftmann@24999
|
20 |
|
haftmann@26140
|
21 |
lemma index_of_nat_nat_of_index [simp]:
|
haftmann@26140
|
22 |
"index_of_nat (nat_of_index k) = k"
|
haftmann@26140
|
23 |
by (rule nat_of_index_inverse)
|
haftmann@25967
|
24 |
|
haftmann@26140
|
25 |
lemma nat_of_index_index_of_nat [simp]:
|
haftmann@26140
|
26 |
"nat_of_index (index_of_nat n) = n"
|
haftmann@26140
|
27 |
by (rule index_of_nat_inverse)
|
haftmann@26140
|
28 |
(unfold index_def, rule UNIV_I)
|
haftmann@24999
|
29 |
|
haftmann@24999
|
30 |
lemma index:
|
haftmann@25767
|
31 |
"(\<And>n\<Colon>index. PROP P n) \<equiv> (\<And>n\<Colon>nat. PROP P (index_of_nat n))"
|
haftmann@24999
|
32 |
proof
|
haftmann@25767
|
33 |
fix n :: nat
|
haftmann@25767
|
34 |
assume "\<And>n\<Colon>index. PROP P n"
|
haftmann@25767
|
35 |
then show "PROP P (index_of_nat n)" .
|
haftmann@24999
|
36 |
next
|
haftmann@25767
|
37 |
fix n :: index
|
haftmann@25767
|
38 |
assume "\<And>n\<Colon>nat. PROP P (index_of_nat n)"
|
haftmann@25767
|
39 |
then have "PROP P (index_of_nat (nat_of_index n))" .
|
haftmann@25767
|
40 |
then show "PROP P n" by simp
|
haftmann@24999
|
41 |
qed
|
haftmann@24999
|
42 |
|
haftmann@26140
|
43 |
lemma index_case:
|
haftmann@26140
|
44 |
assumes "\<And>n. k = index_of_nat n \<Longrightarrow> P"
|
haftmann@26140
|
45 |
shows P
|
haftmann@26140
|
46 |
by (rule assms [of "nat_of_index k"]) simp
|
haftmann@26140
|
47 |
|
wenzelm@26304
|
48 |
lemma index_induct_raw:
|
haftmann@26140
|
49 |
assumes "\<And>n. P (index_of_nat n)"
|
haftmann@26140
|
50 |
shows "P k"
|
haftmann@26140
|
51 |
proof -
|
haftmann@26140
|
52 |
from assms have "P (index_of_nat (nat_of_index k))" .
|
haftmann@26140
|
53 |
then show ?thesis by simp
|
haftmann@26140
|
54 |
qed
|
haftmann@26140
|
55 |
|
haftmann@26140
|
56 |
lemma nat_of_index_inject [simp]:
|
haftmann@26140
|
57 |
"nat_of_index k = nat_of_index l \<longleftrightarrow> k = l"
|
haftmann@26140
|
58 |
by (rule nat_of_index_inject)
|
haftmann@26140
|
59 |
|
haftmann@26140
|
60 |
lemma index_of_nat_inject [simp]:
|
haftmann@26140
|
61 |
"index_of_nat n = index_of_nat m \<longleftrightarrow> n = m"
|
haftmann@26140
|
62 |
by (auto intro!: index_of_nat_inject simp add: index_def)
|
haftmann@26140
|
63 |
|
haftmann@26140
|
64 |
instantiation index :: zero
|
haftmann@26140
|
65 |
begin
|
haftmann@26140
|
66 |
|
haftmann@28562
|
67 |
definition [simp, code del]:
|
haftmann@26140
|
68 |
"0 = index_of_nat 0"
|
haftmann@26140
|
69 |
|
haftmann@26140
|
70 |
instance ..
|
haftmann@26140
|
71 |
|
haftmann@26140
|
72 |
end
|
haftmann@26140
|
73 |
|
haftmann@26140
|
74 |
definition [simp]:
|
haftmann@26140
|
75 |
"Suc_index k = index_of_nat (Suc (nat_of_index k))"
|
haftmann@26140
|
76 |
|
haftmann@27104
|
77 |
rep_datatype "0 \<Colon> index" Suc_index
|
haftmann@26140
|
78 |
proof -
|
haftmann@27104
|
79 |
fix P :: "index \<Rightarrow> bool"
|
haftmann@27104
|
80 |
fix k :: index
|
haftmann@26140
|
81 |
assume "P 0" then have init: "P (index_of_nat 0)" by simp
|
haftmann@26140
|
82 |
assume "\<And>k. P k \<Longrightarrow> P (Suc_index k)"
|
haftmann@27104
|
83 |
then have "\<And>n. P (index_of_nat n) \<Longrightarrow> P (Suc_index (index_of_nat n))" .
|
haftmann@26140
|
84 |
then have step: "\<And>n. P (index_of_nat n) \<Longrightarrow> P (index_of_nat (Suc n))" by simp
|
haftmann@26140
|
85 |
from init step have "P (index_of_nat (nat_of_index k))"
|
haftmann@26140
|
86 |
by (induct "nat_of_index k") simp_all
|
haftmann@26140
|
87 |
then show "P k" by simp
|
haftmann@27104
|
88 |
qed simp_all
|
haftmann@26140
|
89 |
|
haftmann@28562
|
90 |
lemmas [code del] = index.recs index.cases
|
haftmann@26140
|
91 |
|
haftmann@26140
|
92 |
declare index_case [case_names nat, cases type: index]
|
haftmann@27104
|
93 |
declare index.induct [case_names nat, induct type: index]
|
haftmann@26140
|
94 |
|
haftmann@28562
|
95 |
lemma [code]:
|
haftmann@26140
|
96 |
"index_size = nat_of_index"
|
haftmann@26140
|
97 |
proof (rule ext)
|
haftmann@26140
|
98 |
fix k
|
haftmann@26140
|
99 |
have "index_size k = nat_size (nat_of_index k)"
|
haftmann@26140
|
100 |
by (induct k rule: index.induct) (simp_all del: zero_index_def Suc_index_def, simp_all)
|
haftmann@26140
|
101 |
also have "nat_size (nat_of_index k) = nat_of_index k" by (induct "nat_of_index k") simp_all
|
haftmann@26140
|
102 |
finally show "index_size k = nat_of_index k" .
|
haftmann@26140
|
103 |
qed
|
haftmann@26140
|
104 |
|
haftmann@28562
|
105 |
lemma [code]:
|
haftmann@26140
|
106 |
"size = nat_of_index"
|
haftmann@26140
|
107 |
proof (rule ext)
|
haftmann@26140
|
108 |
fix k
|
haftmann@26140
|
109 |
show "size k = nat_of_index k"
|
haftmann@26140
|
110 |
by (induct k) (simp_all del: zero_index_def Suc_index_def, simp_all)
|
haftmann@26140
|
111 |
qed
|
haftmann@26140
|
112 |
|
haftmann@28562
|
113 |
lemma [code]:
|
haftmann@28346
|
114 |
"eq_class.eq k l \<longleftrightarrow> eq_class.eq (nat_of_index k) (nat_of_index l)"
|
haftmann@28346
|
115 |
by (cases k, cases l) (simp add: eq)
|
haftmann@24999
|
116 |
|
haftmann@28351
|
117 |
lemma [code nbe]:
|
haftmann@28351
|
118 |
"eq_class.eq (k::index) k \<longleftrightarrow> True"
|
haftmann@28351
|
119 |
by (rule HOL.eq_refl)
|
haftmann@28351
|
120 |
|
haftmann@24999
|
121 |
|
haftmann@25767
|
122 |
subsection {* Indices as datatype of ints *}
|
haftmann@24999
|
123 |
|
haftmann@25767
|
124 |
instantiation index :: number
|
haftmann@25767
|
125 |
begin
|
haftmann@25767
|
126 |
|
haftmann@25767
|
127 |
definition
|
haftmann@25767
|
128 |
"number_of = index_of_nat o nat"
|
haftmann@25767
|
129 |
|
haftmann@25767
|
130 |
instance ..
|
haftmann@25767
|
131 |
|
haftmann@25767
|
132 |
end
|
haftmann@24999
|
133 |
|
haftmann@26264
|
134 |
lemma nat_of_index_number [simp]:
|
haftmann@26264
|
135 |
"nat_of_index (number_of k) = number_of k"
|
haftmann@26264
|
136 |
by (simp add: number_of_index_def nat_number_of_def number_of_is_id)
|
haftmann@26264
|
137 |
|
haftmann@24999
|
138 |
code_datatype "number_of \<Colon> int \<Rightarrow> index"
|
haftmann@24999
|
139 |
|
haftmann@24999
|
140 |
|
haftmann@24999
|
141 |
subsection {* Basic arithmetic *}
|
haftmann@24999
|
142 |
|
haftmann@25767
|
143 |
instantiation index :: "{minus, ordered_semidom, Divides.div, linorder}"
|
haftmann@25767
|
144 |
begin
|
haftmann@24999
|
145 |
|
haftmann@28562
|
146 |
lemma zero_index_code [code inline, code]:
|
haftmann@24999
|
147 |
"(0\<Colon>index) = Numeral0"
|
haftmann@25767
|
148 |
by (simp add: number_of_index_def Pls_def)
|
haftmann@25967
|
149 |
lemma [code post]: "Numeral0 = (0\<Colon>index)"
|
haftmann@25967
|
150 |
using zero_index_code ..
|
haftmann@25767
|
151 |
|
haftmann@28562
|
152 |
definition [simp, code del]:
|
haftmann@25767
|
153 |
"(1\<Colon>index) = index_of_nat 1"
|
haftmann@24999
|
154 |
|
haftmann@28562
|
155 |
lemma one_index_code [code inline, code]:
|
haftmann@24999
|
156 |
"(1\<Colon>index) = Numeral1"
|
huffman@26086
|
157 |
by (simp add: number_of_index_def Pls_def Bit1_def)
|
haftmann@25967
|
158 |
lemma [code post]: "Numeral1 = (1\<Colon>index)"
|
haftmann@25967
|
159 |
using one_index_code ..
|
haftmann@25767
|
160 |
|
haftmann@28562
|
161 |
definition [simp, code del]:
|
haftmann@25767
|
162 |
"n + m = index_of_nat (nat_of_index n + nat_of_index m)"
|
haftmann@25767
|
163 |
|
haftmann@28562
|
164 |
lemma plus_index_code [code]:
|
haftmann@25767
|
165 |
"index_of_nat n + index_of_nat m = index_of_nat (n + m)"
|
haftmann@24999
|
166 |
by simp
|
haftmann@24999
|
167 |
|
haftmann@28562
|
168 |
definition [simp, code del]:
|
haftmann@25767
|
169 |
"n - m = index_of_nat (nat_of_index n - nat_of_index m)"
|
haftmann@24999
|
170 |
|
haftmann@28562
|
171 |
definition [simp, code del]:
|
haftmann@25767
|
172 |
"n * m = index_of_nat (nat_of_index n * nat_of_index m)"
|
haftmann@24999
|
173 |
|
haftmann@28562
|
174 |
lemma times_index_code [code]:
|
haftmann@25767
|
175 |
"index_of_nat n * index_of_nat m = index_of_nat (n * m)"
|
haftmann@25767
|
176 |
by simp
|
haftmann@25335
|
177 |
|
haftmann@28562
|
178 |
definition [simp, code del]:
|
haftmann@25767
|
179 |
"n div m = index_of_nat (nat_of_index n div nat_of_index m)"
|
haftmann@24999
|
180 |
|
haftmann@28562
|
181 |
definition [simp, code del]:
|
haftmann@25767
|
182 |
"n mod m = index_of_nat (nat_of_index n mod nat_of_index m)"
|
haftmann@24999
|
183 |
|
haftmann@28562
|
184 |
lemma div_index_code [code]:
|
haftmann@25767
|
185 |
"index_of_nat n div index_of_nat m = index_of_nat (n div m)"
|
haftmann@25767
|
186 |
by simp
|
haftmann@24999
|
187 |
|
haftmann@28562
|
188 |
lemma mod_index_code [code]:
|
haftmann@25767
|
189 |
"index_of_nat n mod index_of_nat m = index_of_nat (n mod m)"
|
haftmann@25767
|
190 |
by simp
|
haftmann@24999
|
191 |
|
haftmann@28562
|
192 |
definition [simp, code del]:
|
haftmann@25767
|
193 |
"n \<le> m \<longleftrightarrow> nat_of_index n \<le> nat_of_index m"
|
haftmann@24999
|
194 |
|
haftmann@28562
|
195 |
definition [simp, code del]:
|
haftmann@25767
|
196 |
"n < m \<longleftrightarrow> nat_of_index n < nat_of_index m"
|
haftmann@24999
|
197 |
|
haftmann@28562
|
198 |
lemma less_eq_index_code [code]:
|
haftmann@25767
|
199 |
"index_of_nat n \<le> index_of_nat m \<longleftrightarrow> n \<le> m"
|
haftmann@25767
|
200 |
by simp
|
haftmann@24999
|
201 |
|
haftmann@28562
|
202 |
lemma less_index_code [code]:
|
haftmann@25767
|
203 |
"index_of_nat n < index_of_nat m \<longleftrightarrow> n < m"
|
haftmann@25767
|
204 |
by simp
|
haftmann@24999
|
205 |
|
haftmann@25767
|
206 |
instance by default (auto simp add: left_distrib index)
|
haftmann@25767
|
207 |
|
haftmann@25767
|
208 |
end
|
haftmann@24999
|
209 |
|
haftmann@26140
|
210 |
lemma Suc_index_minus_one: "Suc_index n - 1 = n" by simp
|
haftmann@26140
|
211 |
|
haftmann@25928
|
212 |
lemma index_of_nat_code [code]:
|
haftmann@25918
|
213 |
"index_of_nat = of_nat"
|
haftmann@25918
|
214 |
proof
|
haftmann@25918
|
215 |
fix n :: nat
|
haftmann@25918
|
216 |
have "of_nat n = index_of_nat n"
|
haftmann@25918
|
217 |
by (induct n) simp_all
|
haftmann@25918
|
218 |
then show "index_of_nat n = of_nat n"
|
haftmann@25918
|
219 |
by (rule sym)
|
haftmann@25918
|
220 |
qed
|
haftmann@25918
|
221 |
|
haftmann@25928
|
222 |
lemma index_not_eq_zero: "i \<noteq> index_of_nat 0 \<longleftrightarrow> i \<ge> 1"
|
haftmann@25928
|
223 |
by (cases i) auto
|
haftmann@25928
|
224 |
|
haftmann@25928
|
225 |
definition
|
haftmann@25928
|
226 |
nat_of_index_aux :: "index \<Rightarrow> nat \<Rightarrow> nat"
|
haftmann@25928
|
227 |
where
|
haftmann@25928
|
228 |
"nat_of_index_aux i n = nat_of_index i + n"
|
haftmann@25928
|
229 |
|
haftmann@25928
|
230 |
lemma nat_of_index_aux_code [code]:
|
haftmann@25928
|
231 |
"nat_of_index_aux i n = (if i = 0 then n else nat_of_index_aux (i - 1) (Suc n))"
|
haftmann@25928
|
232 |
by (auto simp add: nat_of_index_aux_def index_not_eq_zero)
|
haftmann@25928
|
233 |
|
haftmann@25928
|
234 |
lemma nat_of_index_code [code]:
|
haftmann@25928
|
235 |
"nat_of_index i = nat_of_index_aux i 0"
|
haftmann@25928
|
236 |
by (simp add: nat_of_index_aux_def)
|
haftmann@25918
|
237 |
|
haftmann@24999
|
238 |
|
krauss@28042
|
239 |
text {* Measure function (for termination proofs) *}
|
krauss@28042
|
240 |
|
haftmann@28228
|
241 |
lemma [measure_function]:
|
haftmann@28228
|
242 |
"is_measure nat_of_index" by (rule is_measure_trivial)
|
krauss@28042
|
243 |
|
haftmann@24999
|
244 |
subsection {* ML interface *}
|
haftmann@24999
|
245 |
|
haftmann@24999
|
246 |
ML {*
|
haftmann@24999
|
247 |
structure Index =
|
haftmann@24999
|
248 |
struct
|
haftmann@24999
|
249 |
|
haftmann@25928
|
250 |
fun mk k = HOLogic.mk_number @{typ index} k;
|
haftmann@24999
|
251 |
|
haftmann@24999
|
252 |
end;
|
haftmann@24999
|
253 |
*}
|
haftmann@24999
|
254 |
|
haftmann@24999
|
255 |
|
haftmann@26009
|
256 |
subsection {* Specialized @{term "op - \<Colon> index \<Rightarrow> index \<Rightarrow> index"},
|
haftmann@26009
|
257 |
@{term "op div \<Colon> index \<Rightarrow> index \<Rightarrow> index"} and @{term "op mod \<Colon> index \<Rightarrow> index \<Rightarrow> index"}
|
haftmann@26009
|
258 |
operations *}
|
haftmann@26009
|
259 |
|
haftmann@26009
|
260 |
definition
|
haftmann@26009
|
261 |
minus_index_aux :: "index \<Rightarrow> index \<Rightarrow> index"
|
haftmann@26009
|
262 |
where
|
haftmann@28562
|
263 |
[code del]: "minus_index_aux = op -"
|
haftmann@26009
|
264 |
|
haftmann@28562
|
265 |
lemma [code]: "op - = minus_index_aux"
|
haftmann@26009
|
266 |
using minus_index_aux_def ..
|
haftmann@26009
|
267 |
|
haftmann@26009
|
268 |
definition
|
haftmann@26009
|
269 |
div_mod_index :: "index \<Rightarrow> index \<Rightarrow> index \<times> index"
|
haftmann@26009
|
270 |
where
|
haftmann@28562
|
271 |
[code del]: "div_mod_index n m = (n div m, n mod m)"
|
haftmann@26009
|
272 |
|
haftmann@28562
|
273 |
lemma [code]:
|
haftmann@26009
|
274 |
"div_mod_index n m = (if m = 0 then (0, n) else (n div m, n mod m))"
|
haftmann@26009
|
275 |
unfolding div_mod_index_def by auto
|
haftmann@26009
|
276 |
|
haftmann@28562
|
277 |
lemma [code]:
|
haftmann@26009
|
278 |
"n div m = fst (div_mod_index n m)"
|
haftmann@26009
|
279 |
unfolding div_mod_index_def by simp
|
haftmann@26009
|
280 |
|
haftmann@28562
|
281 |
lemma [code]:
|
haftmann@26009
|
282 |
"n mod m = snd (div_mod_index n m)"
|
haftmann@26009
|
283 |
unfolding div_mod_index_def by simp
|
haftmann@26009
|
284 |
|
haftmann@26009
|
285 |
|
haftmann@28228
|
286 |
subsection {* Code generator setup *}
|
haftmann@24999
|
287 |
|
haftmann@25767
|
288 |
text {* Implementation of indices by bounded integers *}
|
haftmann@25767
|
289 |
|
haftmann@24999
|
290 |
code_type index
|
haftmann@24999
|
291 |
(SML "int")
|
haftmann@24999
|
292 |
(OCaml "int")
|
haftmann@25967
|
293 |
(Haskell "Int")
|
haftmann@24999
|
294 |
|
haftmann@24999
|
295 |
code_instance index :: eq
|
haftmann@24999
|
296 |
(Haskell -)
|
haftmann@24999
|
297 |
|
haftmann@24999
|
298 |
setup {*
|
haftmann@25928
|
299 |
fold (Numeral.add_code @{const_name number_index_inst.number_of_index}
|
haftmann@25928
|
300 |
false false) ["SML", "OCaml", "Haskell"]
|
haftmann@24999
|
301 |
*}
|
haftmann@24999
|
302 |
|
haftmann@25918
|
303 |
code_reserved SML Int int
|
haftmann@25918
|
304 |
code_reserved OCaml Pervasives int
|
haftmann@24999
|
305 |
|
haftmann@24999
|
306 |
code_const "op + \<Colon> index \<Rightarrow> index \<Rightarrow> index"
|
haftmann@25928
|
307 |
(SML "Int.+/ ((_),/ (_))")
|
haftmann@25967
|
308 |
(OCaml "Pervasives.( + )")
|
haftmann@24999
|
309 |
(Haskell infixl 6 "+")
|
haftmann@24999
|
310 |
|
haftmann@26009
|
311 |
code_const "minus_index_aux \<Colon> index \<Rightarrow> index \<Rightarrow> index"
|
haftmann@25918
|
312 |
(SML "Int.max/ (_/ -/ _,/ 0 : int)")
|
haftmann@25918
|
313 |
(OCaml "Pervasives.max/ (_/ -/ _)/ (0 : int) ")
|
haftmann@25918
|
314 |
(Haskell "max/ (_/ -/ _)/ (0 :: Int)")
|
haftmann@24999
|
315 |
|
haftmann@24999
|
316 |
code_const "op * \<Colon> index \<Rightarrow> index \<Rightarrow> index"
|
haftmann@25928
|
317 |
(SML "Int.*/ ((_),/ (_))")
|
haftmann@25967
|
318 |
(OCaml "Pervasives.( * )")
|
haftmann@24999
|
319 |
(Haskell infixl 7 "*")
|
haftmann@24999
|
320 |
|
haftmann@26009
|
321 |
code_const div_mod_index
|
haftmann@26009
|
322 |
(SML "(fn n => fn m =>/ (n div m, n mod m))")
|
haftmann@26009
|
323 |
(OCaml "(fun n -> fun m ->/ (n '/ m, n mod m))")
|
haftmann@26009
|
324 |
(Haskell "divMod")
|
haftmann@25928
|
325 |
|
haftmann@28346
|
326 |
code_const "eq_class.eq \<Colon> index \<Rightarrow> index \<Rightarrow> bool"
|
haftmann@24999
|
327 |
(SML "!((_ : Int.int) = _)")
|
haftmann@25967
|
328 |
(OCaml "!((_ : int) = _)")
|
haftmann@24999
|
329 |
(Haskell infixl 4 "==")
|
haftmann@24999
|
330 |
|
haftmann@24999
|
331 |
code_const "op \<le> \<Colon> index \<Rightarrow> index \<Rightarrow> bool"
|
haftmann@25928
|
332 |
(SML "Int.<=/ ((_),/ (_))")
|
haftmann@25967
|
333 |
(OCaml "!((_ : int) <= _)")
|
haftmann@24999
|
334 |
(Haskell infix 4 "<=")
|
haftmann@24999
|
335 |
|
haftmann@24999
|
336 |
code_const "op < \<Colon> index \<Rightarrow> index \<Rightarrow> bool"
|
haftmann@25928
|
337 |
(SML "Int.</ ((_),/ (_))")
|
haftmann@25967
|
338 |
(OCaml "!((_ : int) < _)")
|
haftmann@24999
|
339 |
(Haskell infix 4 "<")
|
haftmann@24999
|
340 |
|
haftmann@28228
|
341 |
text {* Evaluation *}
|
haftmann@28228
|
342 |
|
haftmann@28562
|
343 |
lemma [code, code del]:
|
haftmann@28228
|
344 |
"(Code_Eval.term_of \<Colon> index \<Rightarrow> term) = Code_Eval.term_of" ..
|
haftmann@28228
|
345 |
|
haftmann@28228
|
346 |
code_const "Code_Eval.term_of \<Colon> index \<Rightarrow> term"
|
haftmann@28228
|
347 |
(SML "HOLogic.mk'_number/ HOLogic.indexT/ (IntInf.fromInt/ _)")
|
haftmann@28228
|
348 |
|
haftmann@24999
|
349 |
end
|