24999

1 
(* ID: $Id$


2 
Author: Florian Haftmann, TU Muenchen


3 
*)


4 


5 
header {* Type of indices *}


6 


7 
theory Code_Index

25691

8 
imports ATP_Linkup

24999

9 
begin


10 


11 
text {*

25767

12 
Indices are isomorphic to HOL @{typ nat} but

24999

13 
mapped to targetlanguage builtin integers


14 
*}


15 


16 
subsection {* Datatype of indices *}


17 

25767

18 
datatype index = index_of_nat nat

24999

19 

25967

20 
lemma [code func]:


21 
"index_size k = 0"


22 
by (cases k) simp


23 

24999

24 
lemmas [code func del] = index.recs index.cases


25 

25767

26 
primrec


27 
nat_of_index :: "index \<Rightarrow> nat"

24999

28 
where

25767

29 
"nat_of_index (index_of_nat k) = k"


30 
lemmas [code func del] = nat_of_index.simps

24999

31 


32 
lemma index_id [simp]:

25767

33 
"index_of_nat (nat_of_index n) = n"


34 
by (cases n) simp_all


35 


36 
lemma nat_of_index_inject [simp]:


37 
"nat_of_index n = nat_of_index m \<longleftrightarrow> n = m"


38 
by (cases n) auto

24999

39 


40 
lemma index:

25767

41 
"(\<And>n\<Colon>index. PROP P n) \<equiv> (\<And>n\<Colon>nat. PROP P (index_of_nat n))"

24999

42 
proof

25767

43 
fix n :: nat


44 
assume "\<And>n\<Colon>index. PROP P n"


45 
then show "PROP P (index_of_nat n)" .

24999

46 
next

25767

47 
fix n :: index


48 
assume "\<And>n\<Colon>nat. PROP P (index_of_nat n)"


49 
then have "PROP P (index_of_nat (nat_of_index n))" .


50 
then show "PROP P n" by simp

24999

51 
qed


52 

25767

53 
lemma [code func]: "size (n\<Colon>index) = 0"


54 
by (cases n) simp_all

24999

55 


56 

25767

57 
subsection {* Indices as datatype of ints *}


58 


59 
instantiation index :: number


60 
begin

24999

61 

25767

62 
definition


63 
"number_of = index_of_nat o nat"


64 


65 
instance ..


66 


67 
end

24999

68 


69 
code_datatype "number_of \<Colon> int \<Rightarrow> index"


70 


71 


72 
subsection {* Basic arithmetic *}


73 

25767

74 
instantiation index :: "{minus, ordered_semidom, Divides.div, linorder}"


75 
begin

24999

76 

25767

77 
definition [simp, code func del]:


78 
"(0\<Colon>index) = index_of_nat 0"

24999

79 


80 
lemma zero_index_code [code inline, code func]:


81 
"(0\<Colon>index) = Numeral0"

25767

82 
by (simp add: number_of_index_def Pls_def)

25967

83 
lemma [code post]: "Numeral0 = (0\<Colon>index)"


84 
using zero_index_code ..

25767

85 


86 
definition [simp, code func del]:


87 
"(1\<Colon>index) = index_of_nat 1"

24999

88 


89 
lemma one_index_code [code inline, code func]:


90 
"(1\<Colon>index) = Numeral1"

25767

91 
by (simp add: number_of_index_def Pls_def Bit_def)

25967

92 
lemma [code post]: "Numeral1 = (1\<Colon>index)"


93 
using one_index_code ..

25767

94 


95 
definition [simp, code func del]:


96 
"n + m = index_of_nat (nat_of_index n + nat_of_index m)"


97 


98 
lemma plus_index_code [code func]:


99 
"index_of_nat n + index_of_nat m = index_of_nat (n + m)"


100 
by simp


101 


102 
definition [simp, code func del]:


103 
"n  m = index_of_nat (nat_of_index n  nat_of_index m)"


104 


105 
definition [simp, code func del]:


106 
"n * m = index_of_nat (nat_of_index n * nat_of_index m)"


107 


108 
lemma times_index_code [code func]:


109 
"index_of_nat n * index_of_nat m = index_of_nat (n * m)"

24999

110 
by simp


111 

25767

112 
definition [simp, code func del]:


113 
"n div m = index_of_nat (nat_of_index n div nat_of_index m)"

24999

114 

25767

115 
definition [simp, code func del]:


116 
"n mod m = index_of_nat (nat_of_index n mod nat_of_index m)"

24999

117 

25767

118 
lemma div_index_code [code func]:


119 
"index_of_nat n div index_of_nat m = index_of_nat (n div m)"


120 
by simp

25335

121 

25767

122 
lemma mod_index_code [code func]:


123 
"index_of_nat n mod index_of_nat m = index_of_nat (n mod m)"


124 
by simp

24999

125 

25767

126 
definition [simp, code func del]:


127 
"n \<le> m \<longleftrightarrow> nat_of_index n \<le> nat_of_index m"

24999

128 

25767

129 
definition [simp, code func del]:


130 
"n < m \<longleftrightarrow> nat_of_index n < nat_of_index m"

24999

131 

25767

132 
lemma less_eq_index_code [code func]:


133 
"index_of_nat n \<le> index_of_nat m \<longleftrightarrow> n \<le> m"


134 
by simp

24999

135 

25767

136 
lemma less_index_code [code func]:


137 
"index_of_nat n < index_of_nat m \<longleftrightarrow> n < m"


138 
by simp

24999

139 

25767

140 
instance by default (auto simp add: left_distrib index)


141 


142 
end

24999

143 

25928

144 
lemma index_of_nat_code [code]:

25918

145 
"index_of_nat = of_nat"


146 
proof


147 
fix n :: nat


148 
have "of_nat n = index_of_nat n"


149 
by (induct n) simp_all


150 
then show "index_of_nat n = of_nat n"


151 
by (rule sym)


152 
qed


153 

25928

154 
lemma index_not_eq_zero: "i \<noteq> index_of_nat 0 \<longleftrightarrow> i \<ge> 1"


155 
by (cases i) auto


156 


157 
definition


158 
nat_of_index_aux :: "index \<Rightarrow> nat \<Rightarrow> nat"


159 
where


160 
"nat_of_index_aux i n = nat_of_index i + n"


161 


162 
lemma nat_of_index_aux_code [code]:


163 
"nat_of_index_aux i n = (if i = 0 then n else nat_of_index_aux (i  1) (Suc n))"


164 
by (auto simp add: nat_of_index_aux_def index_not_eq_zero)


165 


166 
lemma nat_of_index_code [code]:


167 
"nat_of_index i = nat_of_index_aux i 0"


168 
by (simp add: nat_of_index_aux_def)

25918

169 

24999

170 


171 
subsection {* ML interface *}


172 


173 
ML {*


174 
structure Index =


175 
struct


176 

25928

177 
fun mk k = HOLogic.mk_number @{typ index} k;

24999

178 


179 
end;


180 
*}


181 


182 


183 
subsection {* Code serialization *}


184 

25767

185 
text {* Implementation of indices by bounded integers *}


186 

24999

187 
code_type index


188 
(SML "int")


189 
(OCaml "int")

25967

190 
(Haskell "Int")

24999

191 


192 
code_instance index :: eq


193 
(Haskell )


194 


195 
setup {*

25928

196 
fold (Numeral.add_code @{const_name number_index_inst.number_of_index}


197 
false false) ["SML", "OCaml", "Haskell"]

24999

198 
*}


199 

25918

200 
code_reserved SML Int int


201 
code_reserved OCaml Pervasives int

24999

202 


203 
code_const "op + \<Colon> index \<Rightarrow> index \<Rightarrow> index"

25928

204 
(SML "Int.+/ ((_),/ (_))")

25967

205 
(OCaml "Pervasives.( + )")

24999

206 
(Haskell infixl 6 "+")


207 

25918

208 
code_const "op  \<Colon> index \<Rightarrow> index \<Rightarrow> index"


209 
(SML "Int.max/ (_/ / _,/ 0 : int)")


210 
(OCaml "Pervasives.max/ (_/ / _)/ (0 : int) ")


211 
(Haskell "max/ (_/ / _)/ (0 :: Int)")

24999

212 


213 
code_const "op * \<Colon> index \<Rightarrow> index \<Rightarrow> index"

25928

214 
(SML "Int.*/ ((_),/ (_))")

25967

215 
(OCaml "Pervasives.( * )")

24999

216 
(Haskell infixl 7 "*")


217 

25928

218 
code_const "op div \<Colon> index \<Rightarrow> index \<Rightarrow> index"


219 
(SML "Int.div/ ((_),/ (_))")

25967

220 
(OCaml "Pervasives.( / )")

25928

221 
(Haskell "div")


222 


223 
code_const "op mod \<Colon> index \<Rightarrow> index \<Rightarrow> index"


224 
(SML "Int.mod/ ((_),/ (_))")

25945

225 
(OCaml "Pervasives.( mod )")

25928

226 
(Haskell "mod")


227 

24999

228 
code_const "op = \<Colon> index \<Rightarrow> index \<Rightarrow> bool"


229 
(SML "!((_ : Int.int) = _)")

25967

230 
(OCaml "!((_ : int) = _)")

24999

231 
(Haskell infixl 4 "==")


232 


233 
code_const "op \<le> \<Colon> index \<Rightarrow> index \<Rightarrow> bool"

25928

234 
(SML "Int.<=/ ((_),/ (_))")

25967

235 
(OCaml "!((_ : int) <= _)")

24999

236 
(Haskell infix 4 "<=")


237 


238 
code_const "op < \<Colon> index \<Rightarrow> index \<Rightarrow> bool"

25928

239 
(SML "Int.</ ((_),/ (_))")

25967

240 
(OCaml "!((_ : int) < _)")

24999

241 
(Haskell infix 4 "<")


242 


243 
end
