src/HOL/Library/Code_Index.thy
author haftmann
Fri Jan 25 14:54:46 2008 +0100 (2008-01-25)
changeset 25967 dd602eb20f3f
parent 25945 ddc6a3312636
child 26009 b6a64fe38634
permissions -rw-r--r--
fixed and tuned
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@24999
   183
subsection {* Code serialization *}
haftmann@24999
   184
haftmann@25767
   185
text {* Implementation of indices by bounded integers *}
haftmann@25767
   186
haftmann@24999
   187
code_type index
haftmann@24999
   188
  (SML "int")
haftmann@24999
   189
  (OCaml "int")
haftmann@25967
   190
  (Haskell "Int")
haftmann@24999
   191
haftmann@24999
   192
code_instance index :: eq
haftmann@24999
   193
  (Haskell -)
haftmann@24999
   194
haftmann@24999
   195
setup {*
haftmann@25928
   196
  fold (Numeral.add_code @{const_name number_index_inst.number_of_index}
haftmann@25928
   197
    false false) ["SML", "OCaml", "Haskell"]
haftmann@24999
   198
*}
haftmann@24999
   199
haftmann@25918
   200
code_reserved SML Int int
haftmann@25918
   201
code_reserved OCaml Pervasives int
haftmann@24999
   202
haftmann@24999
   203
code_const "op + \<Colon> index \<Rightarrow> index \<Rightarrow> index"
haftmann@25928
   204
  (SML "Int.+/ ((_),/ (_))")
haftmann@25967
   205
  (OCaml "Pervasives.( + )")
haftmann@24999
   206
  (Haskell infixl 6 "+")
haftmann@24999
   207
haftmann@25918
   208
code_const "op - \<Colon> index \<Rightarrow> index \<Rightarrow> index"
haftmann@25918
   209
  (SML "Int.max/ (_/ -/ _,/ 0 : int)")
haftmann@25918
   210
  (OCaml "Pervasives.max/ (_/ -/ _)/ (0 : int) ")
haftmann@25918
   211
  (Haskell "max/ (_/ -/ _)/ (0 :: Int)")
haftmann@24999
   212
haftmann@24999
   213
code_const "op * \<Colon> index \<Rightarrow> index \<Rightarrow> index"
haftmann@25928
   214
  (SML "Int.*/ ((_),/ (_))")
haftmann@25967
   215
  (OCaml "Pervasives.( * )")
haftmann@24999
   216
  (Haskell infixl 7 "*")
haftmann@24999
   217
haftmann@25928
   218
code_const "op div \<Colon> index \<Rightarrow> index \<Rightarrow> index"
haftmann@25928
   219
  (SML "Int.div/ ((_),/ (_))")
haftmann@25967
   220
  (OCaml "Pervasives.( / )")
haftmann@25928
   221
  (Haskell "div")
haftmann@25928
   222
haftmann@25928
   223
code_const "op mod \<Colon> index \<Rightarrow> index \<Rightarrow> index"
haftmann@25928
   224
  (SML "Int.mod/ ((_),/ (_))")
haftmann@25945
   225
  (OCaml "Pervasives.( mod )")
haftmann@25928
   226
  (Haskell "mod")
haftmann@25928
   227
haftmann@24999
   228
code_const "op = \<Colon> index \<Rightarrow> index \<Rightarrow> bool"
haftmann@24999
   229
  (SML "!((_ : Int.int) = _)")
haftmann@25967
   230
  (OCaml "!((_ : int) = _)")
haftmann@24999
   231
  (Haskell infixl 4 "==")
haftmann@24999
   232
haftmann@24999
   233
code_const "op \<le> \<Colon> index \<Rightarrow> index \<Rightarrow> bool"
haftmann@25928
   234
  (SML "Int.<=/ ((_),/ (_))")
haftmann@25967
   235
  (OCaml "!((_ : int) <= _)")
haftmann@24999
   236
  (Haskell infix 4 "<=")
haftmann@24999
   237
haftmann@24999
   238
code_const "op < \<Colon> index \<Rightarrow> index \<Rightarrow> bool"
haftmann@25928
   239
  (SML "Int.</ ((_),/ (_))")
haftmann@25967
   240
  (OCaml "!((_ : int) < _)")
haftmann@24999
   241
  (Haskell infix 4 "<")
haftmann@24999
   242
haftmann@24999
   243
end