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