src/HOL/Library/Code_Index.thy
author haftmann
Mon, 21 Jan 2008 08:43:27 +0100
changeset 25928 042e877d9841
parent 25918 82dd239e0f65
child 25941 0929d6d08dd3
permissions -rw-r--r--
tuned code setup
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
24999
haftmann
parents:
diff changeset
     1
(*  ID:         $Id$
haftmann
parents:
diff changeset
     2
    Author:     Florian Haftmann, TU Muenchen
haftmann
parents:
diff changeset
     3
*)
haftmann
parents:
diff changeset
     4
haftmann
parents:
diff changeset
     5
header {* Type of indices *}
haftmann
parents:
diff changeset
     6
haftmann
parents:
diff changeset
     7
theory Code_Index
25691
8f8d83af100a switched from PreList to ATP_Linkup
haftmann
parents: 25502
diff changeset
     8
imports ATP_Linkup
24999
haftmann
parents:
diff changeset
     9
begin
haftmann
parents:
diff changeset
    10
haftmann
parents:
diff changeset
    11
text {*
25767
852bce03412a index now a copy of nat rather than int
haftmann
parents: 25691
diff changeset
    12
  Indices are isomorphic to HOL @{typ nat} but
24999
haftmann
parents:
diff changeset
    13
  mapped to target-language builtin integers
haftmann
parents:
diff changeset
    14
*}
haftmann
parents:
diff changeset
    15
haftmann
parents:
diff changeset
    16
subsection {* Datatype of indices *}
haftmann
parents:
diff changeset
    17
25767
852bce03412a index now a copy of nat rather than int
haftmann
parents: 25691
diff changeset
    18
datatype index = index_of_nat nat
24999
haftmann
parents:
diff changeset
    19
haftmann
parents:
diff changeset
    20
lemmas [code func del] = index.recs index.cases
haftmann
parents:
diff changeset
    21
25767
852bce03412a index now a copy of nat rather than int
haftmann
parents: 25691
diff changeset
    22
primrec
852bce03412a index now a copy of nat rather than int
haftmann
parents: 25691
diff changeset
    23
  nat_of_index :: "index \<Rightarrow> nat"
24999
haftmann
parents:
diff changeset
    24
where
25767
852bce03412a index now a copy of nat rather than int
haftmann
parents: 25691
diff changeset
    25
  "nat_of_index (index_of_nat k) = k"
852bce03412a index now a copy of nat rather than int
haftmann
parents: 25691
diff changeset
    26
lemmas [code func del] = nat_of_index.simps
24999
haftmann
parents:
diff changeset
    27
haftmann
parents:
diff changeset
    28
lemma index_id [simp]:
25767
852bce03412a index now a copy of nat rather than int
haftmann
parents: 25691
diff changeset
    29
  "index_of_nat (nat_of_index n) = n"
852bce03412a index now a copy of nat rather than int
haftmann
parents: 25691
diff changeset
    30
  by (cases n) simp_all
852bce03412a index now a copy of nat rather than int
haftmann
parents: 25691
diff changeset
    31
852bce03412a index now a copy of nat rather than int
haftmann
parents: 25691
diff changeset
    32
lemma nat_of_index_inject [simp]:
852bce03412a index now a copy of nat rather than int
haftmann
parents: 25691
diff changeset
    33
  "nat_of_index n = nat_of_index m \<longleftrightarrow> n = m"
852bce03412a index now a copy of nat rather than int
haftmann
parents: 25691
diff changeset
    34
  by (cases n) auto
24999
haftmann
parents:
diff changeset
    35
haftmann
parents:
diff changeset
    36
lemma index:
25767
852bce03412a index now a copy of nat rather than int
haftmann
parents: 25691
diff changeset
    37
  "(\<And>n\<Colon>index. PROP P n) \<equiv> (\<And>n\<Colon>nat. PROP P (index_of_nat n))"
24999
haftmann
parents:
diff changeset
    38
proof
25767
852bce03412a index now a copy of nat rather than int
haftmann
parents: 25691
diff changeset
    39
  fix n :: nat
852bce03412a index now a copy of nat rather than int
haftmann
parents: 25691
diff changeset
    40
  assume "\<And>n\<Colon>index. PROP P n"
852bce03412a index now a copy of nat rather than int
haftmann
parents: 25691
diff changeset
    41
  then show "PROP P (index_of_nat n)" .
24999
haftmann
parents:
diff changeset
    42
next
25767
852bce03412a index now a copy of nat rather than int
haftmann
parents: 25691
diff changeset
    43
  fix n :: index
852bce03412a index now a copy of nat rather than int
haftmann
parents: 25691
diff changeset
    44
  assume "\<And>n\<Colon>nat. PROP P (index_of_nat n)"
852bce03412a index now a copy of nat rather than int
haftmann
parents: 25691
diff changeset
    45
  then have "PROP P (index_of_nat (nat_of_index n))" .
852bce03412a index now a copy of nat rather than int
haftmann
parents: 25691
diff changeset
    46
  then show "PROP P n" by simp
24999
haftmann
parents:
diff changeset
    47
qed
haftmann
parents:
diff changeset
    48
25767
852bce03412a index now a copy of nat rather than int
haftmann
parents: 25691
diff changeset
    49
lemma [code func]: "size (n\<Colon>index) = 0"
852bce03412a index now a copy of nat rather than int
haftmann
parents: 25691
diff changeset
    50
  by (cases n) simp_all
24999
haftmann
parents:
diff changeset
    51
haftmann
parents:
diff changeset
    52
25767
852bce03412a index now a copy of nat rather than int
haftmann
parents: 25691
diff changeset
    53
subsection {* Indices as datatype of ints *}
852bce03412a index now a copy of nat rather than int
haftmann
parents: 25691
diff changeset
    54
852bce03412a index now a copy of nat rather than int
haftmann
parents: 25691
diff changeset
    55
instantiation index :: number
852bce03412a index now a copy of nat rather than int
haftmann
parents: 25691
diff changeset
    56
begin
24999
haftmann
parents:
diff changeset
    57
25767
852bce03412a index now a copy of nat rather than int
haftmann
parents: 25691
diff changeset
    58
definition
852bce03412a index now a copy of nat rather than int
haftmann
parents: 25691
diff changeset
    59
  "number_of = index_of_nat o nat"
852bce03412a index now a copy of nat rather than int
haftmann
parents: 25691
diff changeset
    60
852bce03412a index now a copy of nat rather than int
haftmann
parents: 25691
diff changeset
    61
instance ..
852bce03412a index now a copy of nat rather than int
haftmann
parents: 25691
diff changeset
    62
852bce03412a index now a copy of nat rather than int
haftmann
parents: 25691
diff changeset
    63
end
24999
haftmann
parents:
diff changeset
    64
haftmann
parents:
diff changeset
    65
code_datatype "number_of \<Colon> int \<Rightarrow> index"
haftmann
parents:
diff changeset
    66
haftmann
parents:
diff changeset
    67
haftmann
parents:
diff changeset
    68
subsection {* Basic arithmetic *}
haftmann
parents:
diff changeset
    69
25767
852bce03412a index now a copy of nat rather than int
haftmann
parents: 25691
diff changeset
    70
instantiation index :: "{minus, ordered_semidom, Divides.div, linorder}"
852bce03412a index now a copy of nat rather than int
haftmann
parents: 25691
diff changeset
    71
begin
24999
haftmann
parents:
diff changeset
    72
25767
852bce03412a index now a copy of nat rather than int
haftmann
parents: 25691
diff changeset
    73
definition [simp, code func del]:
852bce03412a index now a copy of nat rather than int
haftmann
parents: 25691
diff changeset
    74
  "(0\<Colon>index) = index_of_nat 0"
24999
haftmann
parents:
diff changeset
    75
haftmann
parents:
diff changeset
    76
lemma zero_index_code [code inline, code func]:
haftmann
parents:
diff changeset
    77
  "(0\<Colon>index) = Numeral0"
25767
852bce03412a index now a copy of nat rather than int
haftmann
parents: 25691
diff changeset
    78
  by (simp add: number_of_index_def Pls_def)
852bce03412a index now a copy of nat rather than int
haftmann
parents: 25691
diff changeset
    79
852bce03412a index now a copy of nat rather than int
haftmann
parents: 25691
diff changeset
    80
definition [simp, code func del]:
852bce03412a index now a copy of nat rather than int
haftmann
parents: 25691
diff changeset
    81
  "(1\<Colon>index) = index_of_nat 1"
24999
haftmann
parents:
diff changeset
    82
haftmann
parents:
diff changeset
    83
lemma one_index_code [code inline, code func]:
haftmann
parents:
diff changeset
    84
  "(1\<Colon>index) = Numeral1"
25767
852bce03412a index now a copy of nat rather than int
haftmann
parents: 25691
diff changeset
    85
  by (simp add: number_of_index_def Pls_def Bit_def)
852bce03412a index now a copy of nat rather than int
haftmann
parents: 25691
diff changeset
    86
852bce03412a index now a copy of nat rather than int
haftmann
parents: 25691
diff changeset
    87
definition [simp, code func del]:
852bce03412a index now a copy of nat rather than int
haftmann
parents: 25691
diff changeset
    88
  "n + m = index_of_nat (nat_of_index n + nat_of_index m)"
852bce03412a index now a copy of nat rather than int
haftmann
parents: 25691
diff changeset
    89
852bce03412a index now a copy of nat rather than int
haftmann
parents: 25691
diff changeset
    90
lemma plus_index_code [code func]:
852bce03412a index now a copy of nat rather than int
haftmann
parents: 25691
diff changeset
    91
  "index_of_nat n + index_of_nat m = index_of_nat (n + m)"
852bce03412a index now a copy of nat rather than int
haftmann
parents: 25691
diff changeset
    92
  by simp
852bce03412a index now a copy of nat rather than int
haftmann
parents: 25691
diff changeset
    93
852bce03412a index now a copy of nat rather than int
haftmann
parents: 25691
diff changeset
    94
definition [simp, code func del]:
852bce03412a index now a copy of nat rather than int
haftmann
parents: 25691
diff changeset
    95
  "n - m = index_of_nat (nat_of_index n - nat_of_index m)"
852bce03412a index now a copy of nat rather than int
haftmann
parents: 25691
diff changeset
    96
852bce03412a index now a copy of nat rather than int
haftmann
parents: 25691
diff changeset
    97
definition [simp, code func del]:
852bce03412a index now a copy of nat rather than int
haftmann
parents: 25691
diff changeset
    98
  "n * m = index_of_nat (nat_of_index n * nat_of_index m)"
852bce03412a index now a copy of nat rather than int
haftmann
parents: 25691
diff changeset
    99
852bce03412a index now a copy of nat rather than int
haftmann
parents: 25691
diff changeset
   100
lemma times_index_code [code func]:
852bce03412a index now a copy of nat rather than int
haftmann
parents: 25691
diff changeset
   101
  "index_of_nat n * index_of_nat m = index_of_nat (n * m)"
24999
haftmann
parents:
diff changeset
   102
  by simp
haftmann
parents:
diff changeset
   103
25767
852bce03412a index now a copy of nat rather than int
haftmann
parents: 25691
diff changeset
   104
definition [simp, code func del]:
852bce03412a index now a copy of nat rather than int
haftmann
parents: 25691
diff changeset
   105
  "n div m = index_of_nat (nat_of_index n div nat_of_index m)"
24999
haftmann
parents:
diff changeset
   106
25767
852bce03412a index now a copy of nat rather than int
haftmann
parents: 25691
diff changeset
   107
definition [simp, code func del]:
852bce03412a index now a copy of nat rather than int
haftmann
parents: 25691
diff changeset
   108
  "n mod m = index_of_nat (nat_of_index n mod nat_of_index m)"
24999
haftmann
parents:
diff changeset
   109
25767
852bce03412a index now a copy of nat rather than int
haftmann
parents: 25691
diff changeset
   110
lemma div_index_code [code func]:
852bce03412a index now a copy of nat rather than int
haftmann
parents: 25691
diff changeset
   111
  "index_of_nat n div index_of_nat m = index_of_nat (n div m)"
852bce03412a index now a copy of nat rather than int
haftmann
parents: 25691
diff changeset
   112
  by simp
25335
182a001a7ea4 duv, mod, int conversion
haftmann
parents: 24999
diff changeset
   113
25767
852bce03412a index now a copy of nat rather than int
haftmann
parents: 25691
diff changeset
   114
lemma mod_index_code [code func]:
852bce03412a index now a copy of nat rather than int
haftmann
parents: 25691
diff changeset
   115
  "index_of_nat n mod index_of_nat m = index_of_nat (n mod m)"
852bce03412a index now a copy of nat rather than int
haftmann
parents: 25691
diff changeset
   116
  by simp
24999
haftmann
parents:
diff changeset
   117
25767
852bce03412a index now a copy of nat rather than int
haftmann
parents: 25691
diff changeset
   118
definition [simp, code func del]:
852bce03412a index now a copy of nat rather than int
haftmann
parents: 25691
diff changeset
   119
  "n \<le> m \<longleftrightarrow> nat_of_index n \<le> nat_of_index m"
24999
haftmann
parents:
diff changeset
   120
25767
852bce03412a index now a copy of nat rather than int
haftmann
parents: 25691
diff changeset
   121
definition [simp, code func del]:
852bce03412a index now a copy of nat rather than int
haftmann
parents: 25691
diff changeset
   122
  "n < m \<longleftrightarrow> nat_of_index n < nat_of_index m"
24999
haftmann
parents:
diff changeset
   123
25767
852bce03412a index now a copy of nat rather than int
haftmann
parents: 25691
diff changeset
   124
lemma less_eq_index_code [code func]:
852bce03412a index now a copy of nat rather than int
haftmann
parents: 25691
diff changeset
   125
  "index_of_nat n \<le> index_of_nat m \<longleftrightarrow> n \<le> m"
852bce03412a index now a copy of nat rather than int
haftmann
parents: 25691
diff changeset
   126
  by simp
24999
haftmann
parents:
diff changeset
   127
25767
852bce03412a index now a copy of nat rather than int
haftmann
parents: 25691
diff changeset
   128
lemma less_index_code [code func]:
852bce03412a index now a copy of nat rather than int
haftmann
parents: 25691
diff changeset
   129
  "index_of_nat n < index_of_nat m \<longleftrightarrow> n < m"
852bce03412a index now a copy of nat rather than int
haftmann
parents: 25691
diff changeset
   130
  by simp
24999
haftmann
parents:
diff changeset
   131
25767
852bce03412a index now a copy of nat rather than int
haftmann
parents: 25691
diff changeset
   132
instance by default (auto simp add: left_distrib index)
852bce03412a index now a copy of nat rather than int
haftmann
parents: 25691
diff changeset
   133
852bce03412a index now a copy of nat rather than int
haftmann
parents: 25691
diff changeset
   134
end
24999
haftmann
parents:
diff changeset
   135
25928
042e877d9841 tuned code setup
haftmann
parents: 25918
diff changeset
   136
lemma index_of_nat_code [code]:
25918
haftmann
parents: 25767
diff changeset
   137
  "index_of_nat = of_nat"
haftmann
parents: 25767
diff changeset
   138
proof
haftmann
parents: 25767
diff changeset
   139
  fix n :: nat
haftmann
parents: 25767
diff changeset
   140
  have "of_nat n = index_of_nat n"
haftmann
parents: 25767
diff changeset
   141
    by (induct n) simp_all
haftmann
parents: 25767
diff changeset
   142
  then show "index_of_nat n = of_nat n"
haftmann
parents: 25767
diff changeset
   143
    by (rule sym)
haftmann
parents: 25767
diff changeset
   144
qed
haftmann
parents: 25767
diff changeset
   145
25928
042e877d9841 tuned code setup
haftmann
parents: 25918
diff changeset
   146
lemma index_not_eq_zero: "i \<noteq> index_of_nat 0 \<longleftrightarrow> i \<ge> 1"
042e877d9841 tuned code setup
haftmann
parents: 25918
diff changeset
   147
  by (cases i) auto
042e877d9841 tuned code setup
haftmann
parents: 25918
diff changeset
   148
042e877d9841 tuned code setup
haftmann
parents: 25918
diff changeset
   149
definition
042e877d9841 tuned code setup
haftmann
parents: 25918
diff changeset
   150
  nat_of_index_aux :: "index \<Rightarrow> nat \<Rightarrow> nat"
042e877d9841 tuned code setup
haftmann
parents: 25918
diff changeset
   151
where
042e877d9841 tuned code setup
haftmann
parents: 25918
diff changeset
   152
  "nat_of_index_aux i n = nat_of_index i + n"
042e877d9841 tuned code setup
haftmann
parents: 25918
diff changeset
   153
042e877d9841 tuned code setup
haftmann
parents: 25918
diff changeset
   154
lemma nat_of_index_aux_code [code]:
042e877d9841 tuned code setup
haftmann
parents: 25918
diff changeset
   155
  "nat_of_index_aux i n = (if i = 0 then n else nat_of_index_aux (i - 1) (Suc n))"
042e877d9841 tuned code setup
haftmann
parents: 25918
diff changeset
   156
  by (auto simp add: nat_of_index_aux_def index_not_eq_zero)
042e877d9841 tuned code setup
haftmann
parents: 25918
diff changeset
   157
042e877d9841 tuned code setup
haftmann
parents: 25918
diff changeset
   158
lemma nat_of_index_code [code]:
042e877d9841 tuned code setup
haftmann
parents: 25918
diff changeset
   159
  "nat_of_index i = nat_of_index_aux i 0"
042e877d9841 tuned code setup
haftmann
parents: 25918
diff changeset
   160
  by (simp add: nat_of_index_aux_def)
25918
haftmann
parents: 25767
diff changeset
   161
24999
haftmann
parents:
diff changeset
   162
haftmann
parents:
diff changeset
   163
subsection {* ML interface *}
haftmann
parents:
diff changeset
   164
haftmann
parents:
diff changeset
   165
ML {*
haftmann
parents:
diff changeset
   166
structure Index =
haftmann
parents:
diff changeset
   167
struct
haftmann
parents:
diff changeset
   168
25928
042e877d9841 tuned code setup
haftmann
parents: 25918
diff changeset
   169
fun mk k = HOLogic.mk_number @{typ index} k;
24999
haftmann
parents:
diff changeset
   170
haftmann
parents:
diff changeset
   171
end;
haftmann
parents:
diff changeset
   172
*}
haftmann
parents:
diff changeset
   173
haftmann
parents:
diff changeset
   174
haftmann
parents:
diff changeset
   175
subsection {* Code serialization *}
haftmann
parents:
diff changeset
   176
25767
852bce03412a index now a copy of nat rather than int
haftmann
parents: 25691
diff changeset
   177
text {* Implementation of indices by bounded integers *}
852bce03412a index now a copy of nat rather than int
haftmann
parents: 25691
diff changeset
   178
24999
haftmann
parents:
diff changeset
   179
code_type index
haftmann
parents:
diff changeset
   180
  (SML "int")
haftmann
parents:
diff changeset
   181
  (OCaml "int")
haftmann
parents:
diff changeset
   182
  (Haskell "Integer")
haftmann
parents:
diff changeset
   183
haftmann
parents:
diff changeset
   184
code_instance index :: eq
haftmann
parents:
diff changeset
   185
  (Haskell -)
haftmann
parents:
diff changeset
   186
haftmann
parents:
diff changeset
   187
setup {*
25928
042e877d9841 tuned code setup
haftmann
parents: 25918
diff changeset
   188
  fold (Numeral.add_code @{const_name number_index_inst.number_of_index}
042e877d9841 tuned code setup
haftmann
parents: 25918
diff changeset
   189
    false false) ["SML", "OCaml", "Haskell"]
24999
haftmann
parents:
diff changeset
   190
*}
haftmann
parents:
diff changeset
   191
25918
haftmann
parents: 25767
diff changeset
   192
code_reserved SML Int int
haftmann
parents: 25767
diff changeset
   193
code_reserved OCaml Pervasives int
24999
haftmann
parents:
diff changeset
   194
haftmann
parents:
diff changeset
   195
code_const "op + \<Colon> index \<Rightarrow> index \<Rightarrow> index"
25928
042e877d9841 tuned code setup
haftmann
parents: 25918
diff changeset
   196
  (SML "Int.+/ ((_),/ (_))")
24999
haftmann
parents:
diff changeset
   197
  (OCaml "Pervasives.+")
haftmann
parents:
diff changeset
   198
  (Haskell infixl 6 "+")
haftmann
parents:
diff changeset
   199
25918
haftmann
parents: 25767
diff changeset
   200
code_const "op - \<Colon> index \<Rightarrow> index \<Rightarrow> index"
haftmann
parents: 25767
diff changeset
   201
  (SML "Int.max/ (_/ -/ _,/ 0 : int)")
haftmann
parents: 25767
diff changeset
   202
  (OCaml "Pervasives.max/ (_/ -/ _)/ (0 : int) ")
haftmann
parents: 25767
diff changeset
   203
  (Haskell "max/ (_/ -/ _)/ (0 :: Int)")
24999
haftmann
parents:
diff changeset
   204
haftmann
parents:
diff changeset
   205
code_const "op * \<Colon> index \<Rightarrow> index \<Rightarrow> index"
25928
042e877d9841 tuned code setup
haftmann
parents: 25918
diff changeset
   206
  (SML "Int.*/ ((_),/ (_))")
24999
haftmann
parents:
diff changeset
   207
  (OCaml "Pervasives.*")
haftmann
parents:
diff changeset
   208
  (Haskell infixl 7 "*")
haftmann
parents:
diff changeset
   209
25928
042e877d9841 tuned code setup
haftmann
parents: 25918
diff changeset
   210
code_const "op div \<Colon> index \<Rightarrow> index \<Rightarrow> index"
042e877d9841 tuned code setup
haftmann
parents: 25918
diff changeset
   211
  (SML "Int.div/ ((_),/ (_))")
042e877d9841 tuned code setup
haftmann
parents: 25918
diff changeset
   212
  (OCaml "Pervasives.div")
042e877d9841 tuned code setup
haftmann
parents: 25918
diff changeset
   213
  (Haskell "div")
042e877d9841 tuned code setup
haftmann
parents: 25918
diff changeset
   214
042e877d9841 tuned code setup
haftmann
parents: 25918
diff changeset
   215
code_const "op mod \<Colon> index \<Rightarrow> index \<Rightarrow> index"
042e877d9841 tuned code setup
haftmann
parents: 25918
diff changeset
   216
  (SML "Int.mod/ ((_),/ (_))")
042e877d9841 tuned code setup
haftmann
parents: 25918
diff changeset
   217
  (OCaml "Pervasives.mod")
042e877d9841 tuned code setup
haftmann
parents: 25918
diff changeset
   218
  (Haskell "mod")
042e877d9841 tuned code setup
haftmann
parents: 25918
diff changeset
   219
24999
haftmann
parents:
diff changeset
   220
code_const "op = \<Colon> index \<Rightarrow> index \<Rightarrow> bool"
haftmann
parents:
diff changeset
   221
  (SML "!((_ : Int.int) = _)")
haftmann
parents:
diff changeset
   222
  (OCaml "!((_ : Pervasives.int) = _)")
haftmann
parents:
diff changeset
   223
  (Haskell infixl 4 "==")
haftmann
parents:
diff changeset
   224
haftmann
parents:
diff changeset
   225
code_const "op \<le> \<Colon> index \<Rightarrow> index \<Rightarrow> bool"
25928
042e877d9841 tuned code setup
haftmann
parents: 25918
diff changeset
   226
  (SML "Int.<=/ ((_),/ (_))")
24999
haftmann
parents:
diff changeset
   227
  (OCaml "!((_ : Pervasives.int) <= _)")
haftmann
parents:
diff changeset
   228
  (Haskell infix 4 "<=")
haftmann
parents:
diff changeset
   229
haftmann
parents:
diff changeset
   230
code_const "op < \<Colon> index \<Rightarrow> index \<Rightarrow> bool"
25928
042e877d9841 tuned code setup
haftmann
parents: 25918
diff changeset
   231
  (SML "Int.</ ((_),/ (_))")
24999
haftmann
parents:
diff changeset
   232
  (OCaml "!((_ : Pervasives.int) < _)")
haftmann
parents:
diff changeset
   233
  (Haskell infix 4 "<")
haftmann
parents:
diff changeset
   234
haftmann
parents:
diff changeset
   235
end