src/HOL/Library/Code_Index.thy
author haftmann
Tue Jan 29 10:19:56 2008 +0100 (2008-01-29)
changeset 26009 b6a64fe38634
parent 25967 dd602eb20f3f
child 26086 3c243098b64a
permissions -rw-r--r--
treating division by zero properly
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@25691
     8
imports ATP_Linkup
haftmann@24999
     9
begin
haftmann@24999
    10
haftmann@24999
    11
text {*
haftmann@25767
    12
  Indices are isomorphic to HOL @{typ nat} but
haftmann@24999
    13
  mapped to target-language builtin integers
haftmann@24999
    14
*}
haftmann@24999
    15
haftmann@24999
    16
subsection {* Datatype of indices *}
haftmann@24999
    17
haftmann@25767
    18
datatype index = index_of_nat nat
haftmann@24999
    19
haftmann@25967
    20
lemma [code func]:
haftmann@25967
    21
  "index_size k = 0"
haftmann@25967
    22
  by (cases k) simp
haftmann@25967
    23
haftmann@24999
    24
lemmas [code func del] = index.recs index.cases
haftmann@24999
    25
haftmann@25767
    26
primrec
haftmann@25767
    27
  nat_of_index :: "index \<Rightarrow> nat"
haftmann@24999
    28
where
haftmann@25767
    29
  "nat_of_index (index_of_nat k) = k"
haftmann@25767
    30
lemmas [code func del] = nat_of_index.simps
haftmann@24999
    31
haftmann@24999
    32
lemma index_id [simp]:
haftmann@25767
    33
  "index_of_nat (nat_of_index n) = n"
haftmann@25767
    34
  by (cases n) simp_all
haftmann@25767
    35
haftmann@25767
    36
lemma nat_of_index_inject [simp]:
haftmann@25767
    37
  "nat_of_index n = nat_of_index m \<longleftrightarrow> n = m"
haftmann@25767
    38
  by (cases n) auto
haftmann@24999
    39
haftmann@24999
    40
lemma index:
haftmann@25767
    41
  "(\<And>n\<Colon>index. PROP P n) \<equiv> (\<And>n\<Colon>nat. PROP P (index_of_nat n))"
haftmann@24999
    42
proof
haftmann@25767
    43
  fix n :: nat
haftmann@25767
    44
  assume "\<And>n\<Colon>index. PROP P n"
haftmann@25767
    45
  then show "PROP P (index_of_nat n)" .
haftmann@24999
    46
next
haftmann@25767
    47
  fix n :: index
haftmann@25767
    48
  assume "\<And>n\<Colon>nat. PROP P (index_of_nat n)"
haftmann@25767
    49
  then have "PROP P (index_of_nat (nat_of_index n))" .
haftmann@25767
    50
  then show "PROP P n" by simp
haftmann@24999
    51
qed
haftmann@24999
    52
haftmann@25767
    53
lemma [code func]: "size (n\<Colon>index) = 0"
haftmann@25767
    54
  by (cases n) simp_all
haftmann@24999
    55
haftmann@24999
    56
haftmann@25767
    57
subsection {* Indices as datatype of ints *}
haftmann@25767
    58
haftmann@25767
    59
instantiation index :: number
haftmann@25767
    60
begin
haftmann@24999
    61
haftmann@25767
    62
definition
haftmann@25767
    63
  "number_of = index_of_nat o nat"
haftmann@25767
    64
haftmann@25767
    65
instance ..
haftmann@25767
    66
haftmann@25767
    67
end
haftmann@24999
    68
haftmann@24999
    69
code_datatype "number_of \<Colon> int \<Rightarrow> index"
haftmann@24999
    70
haftmann@24999
    71
haftmann@24999
    72
subsection {* Basic arithmetic *}
haftmann@24999
    73
haftmann@25767
    74
instantiation index :: "{minus, ordered_semidom, Divides.div, linorder}"
haftmann@25767
    75
begin
haftmann@24999
    76
haftmann@25767
    77
definition [simp, code func del]:
haftmann@25767
    78
  "(0\<Colon>index) = index_of_nat 0"
haftmann@24999
    79
haftmann@24999
    80
lemma zero_index_code [code inline, code func]:
haftmann@24999
    81
  "(0\<Colon>index) = Numeral0"
haftmann@25767
    82
  by (simp add: number_of_index_def Pls_def)
haftmann@25967
    83
lemma [code post]: "Numeral0 = (0\<Colon>index)"
haftmann@25967
    84
  using zero_index_code ..
haftmann@25767
    85
haftmann@25767
    86
definition [simp, code func del]:
haftmann@25767
    87
  "(1\<Colon>index) = index_of_nat 1"
haftmann@24999
    88
haftmann@24999
    89
lemma one_index_code [code inline, code func]:
haftmann@24999
    90
  "(1\<Colon>index) = Numeral1"
haftmann@25767
    91
  by (simp add: number_of_index_def Pls_def Bit_def)
haftmann@25967
    92
lemma [code post]: "Numeral1 = (1\<Colon>index)"
haftmann@25967
    93
  using one_index_code ..
haftmann@25767
    94
haftmann@25767
    95
definition [simp, code func del]:
haftmann@25767
    96
  "n + m = index_of_nat (nat_of_index n + nat_of_index m)"
haftmann@25767
    97
haftmann@25767
    98
lemma plus_index_code [code func]:
haftmann@25767
    99
  "index_of_nat n + index_of_nat m = index_of_nat (n + m)"
haftmann@25767
   100
  by simp
haftmann@25767
   101
haftmann@25767
   102
definition [simp, code func del]:
haftmann@25767
   103
  "n - m = index_of_nat (nat_of_index n - nat_of_index m)"
haftmann@25767
   104
haftmann@25767
   105
definition [simp, code func del]:
haftmann@25767
   106
  "n * m = index_of_nat (nat_of_index n * nat_of_index m)"
haftmann@25767
   107
haftmann@25767
   108
lemma times_index_code [code func]:
haftmann@25767
   109
  "index_of_nat n * index_of_nat m = index_of_nat (n * m)"
haftmann@24999
   110
  by simp
haftmann@24999
   111
haftmann@25767
   112
definition [simp, code func del]:
haftmann@25767
   113
  "n div m = index_of_nat (nat_of_index n div nat_of_index m)"
haftmann@24999
   114
haftmann@25767
   115
definition [simp, code func del]:
haftmann@25767
   116
  "n mod m = index_of_nat (nat_of_index n mod nat_of_index m)"
haftmann@24999
   117
haftmann@25767
   118
lemma div_index_code [code func]:
haftmann@25767
   119
  "index_of_nat n div index_of_nat m = index_of_nat (n div m)"
haftmann@25767
   120
  by simp
haftmann@25335
   121
haftmann@25767
   122
lemma mod_index_code [code func]:
haftmann@25767
   123
  "index_of_nat n mod index_of_nat m = index_of_nat (n mod m)"
haftmann@25767
   124
  by simp
haftmann@24999
   125
haftmann@25767
   126
definition [simp, code func del]:
haftmann@25767
   127
  "n \<le> m \<longleftrightarrow> nat_of_index n \<le> nat_of_index m"
haftmann@24999
   128
haftmann@25767
   129
definition [simp, code func del]:
haftmann@25767
   130
  "n < m \<longleftrightarrow> nat_of_index n < nat_of_index m"
haftmann@24999
   131
haftmann@25767
   132
lemma less_eq_index_code [code func]:
haftmann@25767
   133
  "index_of_nat n \<le> index_of_nat m \<longleftrightarrow> n \<le> m"
haftmann@25767
   134
  by simp
haftmann@24999
   135
haftmann@25767
   136
lemma less_index_code [code func]:
haftmann@25767
   137
  "index_of_nat n < index_of_nat m \<longleftrightarrow> n < m"
haftmann@25767
   138
  by simp
haftmann@24999
   139
haftmann@25767
   140
instance by default (auto simp add: left_distrib index)
haftmann@25767
   141
haftmann@25767
   142
end
haftmann@24999
   143
haftmann@25928
   144
lemma index_of_nat_code [code]:
haftmann@25918
   145
  "index_of_nat = of_nat"
haftmann@25918
   146
proof
haftmann@25918
   147
  fix n :: nat
haftmann@25918
   148
  have "of_nat n = index_of_nat n"
haftmann@25918
   149
    by (induct n) simp_all
haftmann@25918
   150
  then show "index_of_nat n = of_nat n"
haftmann@25918
   151
    by (rule sym)
haftmann@25918
   152
qed
haftmann@25918
   153
haftmann@25928
   154
lemma index_not_eq_zero: "i \<noteq> index_of_nat 0 \<longleftrightarrow> i \<ge> 1"
haftmann@25928
   155
  by (cases i) auto
haftmann@25928
   156
haftmann@25928
   157
definition
haftmann@25928
   158
  nat_of_index_aux :: "index \<Rightarrow> nat \<Rightarrow> nat"
haftmann@25928
   159
where
haftmann@25928
   160
  "nat_of_index_aux i n = nat_of_index i + n"
haftmann@25928
   161
haftmann@25928
   162
lemma nat_of_index_aux_code [code]:
haftmann@25928
   163
  "nat_of_index_aux i n = (if i = 0 then n else nat_of_index_aux (i - 1) (Suc n))"
haftmann@25928
   164
  by (auto simp add: nat_of_index_aux_def index_not_eq_zero)
haftmann@25928
   165
haftmann@25928
   166
lemma nat_of_index_code [code]:
haftmann@25928
   167
  "nat_of_index i = nat_of_index_aux i 0"
haftmann@25928
   168
  by (simp add: nat_of_index_aux_def)
haftmann@25918
   169
haftmann@24999
   170
haftmann@24999
   171
subsection {* ML interface *}
haftmann@24999
   172
haftmann@24999
   173
ML {*
haftmann@24999
   174
structure Index =
haftmann@24999
   175
struct
haftmann@24999
   176
haftmann@25928
   177
fun mk k = HOLogic.mk_number @{typ index} k;
haftmann@24999
   178
haftmann@24999
   179
end;
haftmann@24999
   180
*}
haftmann@24999
   181
haftmann@24999
   182
haftmann@26009
   183
subsection {* Specialized @{term "op - \<Colon> index \<Rightarrow> index \<Rightarrow> index"},
haftmann@26009
   184
  @{term "op div \<Colon> index \<Rightarrow> index \<Rightarrow> index"} and @{term "op mod \<Colon> index \<Rightarrow> index \<Rightarrow> index"}
haftmann@26009
   185
  operations *}
haftmann@26009
   186
haftmann@26009
   187
definition
haftmann@26009
   188
  minus_index_aux :: "index \<Rightarrow> index \<Rightarrow> index"
haftmann@26009
   189
where
haftmann@26009
   190
  [code func del]: "minus_index_aux = op -"
haftmann@26009
   191
haftmann@26009
   192
lemma [code func]: "op - = minus_index_aux"
haftmann@26009
   193
  using minus_index_aux_def ..
haftmann@26009
   194
haftmann@26009
   195
definition
haftmann@26009
   196
  div_mod_index ::  "index \<Rightarrow> index \<Rightarrow> index \<times> index"
haftmann@26009
   197
where
haftmann@26009
   198
  [code func del]: "div_mod_index n m = (n div m, n mod m)"
haftmann@26009
   199
haftmann@26009
   200
lemma [code func]:
haftmann@26009
   201
  "div_mod_index n m = (if m = 0 then (0, n) else (n div m, n mod m))"
haftmann@26009
   202
  unfolding div_mod_index_def by auto
haftmann@26009
   203
haftmann@26009
   204
lemma [code func]:
haftmann@26009
   205
  "n div m = fst (div_mod_index n m)"
haftmann@26009
   206
  unfolding div_mod_index_def by simp
haftmann@26009
   207
haftmann@26009
   208
lemma [code func]:
haftmann@26009
   209
  "n mod m = snd (div_mod_index n m)"
haftmann@26009
   210
  unfolding div_mod_index_def by simp
haftmann@26009
   211
haftmann@26009
   212
haftmann@24999
   213
subsection {* Code serialization *}
haftmann@24999
   214
haftmann@25767
   215
text {* Implementation of indices by bounded integers *}
haftmann@25767
   216
haftmann@24999
   217
code_type index
haftmann@24999
   218
  (SML "int")
haftmann@24999
   219
  (OCaml "int")
haftmann@25967
   220
  (Haskell "Int")
haftmann@24999
   221
haftmann@24999
   222
code_instance index :: eq
haftmann@24999
   223
  (Haskell -)
haftmann@24999
   224
haftmann@24999
   225
setup {*
haftmann@25928
   226
  fold (Numeral.add_code @{const_name number_index_inst.number_of_index}
haftmann@25928
   227
    false false) ["SML", "OCaml", "Haskell"]
haftmann@24999
   228
*}
haftmann@24999
   229
haftmann@25918
   230
code_reserved SML Int int
haftmann@25918
   231
code_reserved OCaml Pervasives int
haftmann@24999
   232
haftmann@24999
   233
code_const "op + \<Colon> index \<Rightarrow> index \<Rightarrow> index"
haftmann@25928
   234
  (SML "Int.+/ ((_),/ (_))")
haftmann@25967
   235
  (OCaml "Pervasives.( + )")
haftmann@24999
   236
  (Haskell infixl 6 "+")
haftmann@24999
   237
haftmann@26009
   238
code_const "minus_index_aux \<Colon> index \<Rightarrow> index \<Rightarrow> index"
haftmann@25918
   239
  (SML "Int.max/ (_/ -/ _,/ 0 : int)")
haftmann@25918
   240
  (OCaml "Pervasives.max/ (_/ -/ _)/ (0 : int) ")
haftmann@25918
   241
  (Haskell "max/ (_/ -/ _)/ (0 :: Int)")
haftmann@24999
   242
haftmann@24999
   243
code_const "op * \<Colon> index \<Rightarrow> index \<Rightarrow> index"
haftmann@25928
   244
  (SML "Int.*/ ((_),/ (_))")
haftmann@25967
   245
  (OCaml "Pervasives.( * )")
haftmann@24999
   246
  (Haskell infixl 7 "*")
haftmann@24999
   247
haftmann@26009
   248
code_const div_mod_index
haftmann@26009
   249
  (SML "(fn n => fn m =>/ (n div m, n mod m))")
haftmann@26009
   250
  (OCaml "(fun n -> fun m ->/ (n '/ m, n mod m))")
haftmann@26009
   251
  (Haskell "divMod")
haftmann@25928
   252
haftmann@24999
   253
code_const "op = \<Colon> index \<Rightarrow> index \<Rightarrow> bool"
haftmann@24999
   254
  (SML "!((_ : Int.int) = _)")
haftmann@25967
   255
  (OCaml "!((_ : int) = _)")
haftmann@24999
   256
  (Haskell infixl 4 "==")
haftmann@24999
   257
haftmann@24999
   258
code_const "op \<le> \<Colon> index \<Rightarrow> index \<Rightarrow> bool"
haftmann@25928
   259
  (SML "Int.<=/ ((_),/ (_))")
haftmann@25967
   260
  (OCaml "!((_ : int) <= _)")
haftmann@24999
   261
  (Haskell infix 4 "<=")
haftmann@24999
   262
haftmann@24999
   263
code_const "op < \<Colon> index \<Rightarrow> index \<Rightarrow> bool"
haftmann@25928
   264
  (SML "Int.</ ((_),/ (_))")
haftmann@25967
   265
  (OCaml "!((_ : int) < _)")
haftmann@24999
   266
  (Haskell infix 4 "<")
haftmann@24999
   267
haftmann@24999
   268
end