src/HOL/Library/Code_Index.thy
author haftmann
Wed, 02 Jan 2008 15:14:23 +0100
changeset 25767 852bce03412a
parent 25691 8f8d83af100a
child 25918 82dd239e0f65
permissions -rw-r--r--
index now a copy of nat rather than int
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
haftmann
parents:
diff changeset
   136
haftmann
parents:
diff changeset
   137
subsection {* ML interface *}
haftmann
parents:
diff changeset
   138
haftmann
parents:
diff changeset
   139
ML {*
haftmann
parents:
diff changeset
   140
structure Index =
haftmann
parents:
diff changeset
   141
struct
haftmann
parents:
diff changeset
   142
25767
852bce03412a index now a copy of nat rather than int
haftmann
parents: 25691
diff changeset
   143
fun mk k = @{term index_of_nat} $ HOLogic.mk_number @{typ index} k;
24999
haftmann
parents:
diff changeset
   144
haftmann
parents:
diff changeset
   145
end;
haftmann
parents:
diff changeset
   146
*}
haftmann
parents:
diff changeset
   147
haftmann
parents:
diff changeset
   148
haftmann
parents:
diff changeset
   149
subsection {* Code serialization *}
haftmann
parents:
diff changeset
   150
25767
852bce03412a index now a copy of nat rather than int
haftmann
parents: 25691
diff changeset
   151
text {* Pecularity for operations with potentially negative result *}
852bce03412a index now a copy of nat rather than int
haftmann
parents: 25691
diff changeset
   152
852bce03412a index now a copy of nat rather than int
haftmann
parents: 25691
diff changeset
   153
definition
852bce03412a index now a copy of nat rather than int
haftmann
parents: 25691
diff changeset
   154
  minus_index' :: "index \<Rightarrow> index \<Rightarrow> index"
852bce03412a index now a copy of nat rather than int
haftmann
parents: 25691
diff changeset
   155
where
852bce03412a index now a copy of nat rather than int
haftmann
parents: 25691
diff changeset
   156
  [code func del]: "minus_index' = op -"
852bce03412a index now a copy of nat rather than int
haftmann
parents: 25691
diff changeset
   157
852bce03412a index now a copy of nat rather than int
haftmann
parents: 25691
diff changeset
   158
lemma minus_index_code [code func]:
852bce03412a index now a copy of nat rather than int
haftmann
parents: 25691
diff changeset
   159
  "n - m = (let q = minus_index' n m
852bce03412a index now a copy of nat rather than int
haftmann
parents: 25691
diff changeset
   160
    in if q < 0 then 0 else q)"
852bce03412a index now a copy of nat rather than int
haftmann
parents: 25691
diff changeset
   161
  by (simp add: minus_index'_def Let_def)
852bce03412a index now a copy of nat rather than int
haftmann
parents: 25691
diff changeset
   162
852bce03412a index now a copy of nat rather than int
haftmann
parents: 25691
diff changeset
   163
text {* Implementation of indices by bounded integers *}
852bce03412a index now a copy of nat rather than int
haftmann
parents: 25691
diff changeset
   164
24999
haftmann
parents:
diff changeset
   165
code_type index
haftmann
parents:
diff changeset
   166
  (SML "int")
haftmann
parents:
diff changeset
   167
  (OCaml "int")
haftmann
parents:
diff changeset
   168
  (Haskell "Integer")
haftmann
parents:
diff changeset
   169
haftmann
parents:
diff changeset
   170
code_instance index :: eq
haftmann
parents:
diff changeset
   171
  (Haskell -)
haftmann
parents:
diff changeset
   172
haftmann
parents:
diff changeset
   173
setup {*
haftmann
parents:
diff changeset
   174
  fold (fn target => CodeTarget.add_pretty_numeral target true
haftmann
parents:
diff changeset
   175
    @{const_name number_index_inst.number_of_index}
haftmann
parents:
diff changeset
   176
    @{const_name Numeral.B0} @{const_name Numeral.B1}
haftmann
parents:
diff changeset
   177
    @{const_name Numeral.Pls} @{const_name Numeral.Min}
haftmann
parents:
diff changeset
   178
    @{const_name Numeral.Bit}
haftmann
parents:
diff changeset
   179
  ) ["SML", "OCaml", "Haskell"]
haftmann
parents:
diff changeset
   180
*}
haftmann
parents:
diff changeset
   181
haftmann
parents:
diff changeset
   182
code_reserved SML int
haftmann
parents:
diff changeset
   183
code_reserved OCaml int
haftmann
parents:
diff changeset
   184
haftmann
parents:
diff changeset
   185
code_const "op + \<Colon> index \<Rightarrow> index \<Rightarrow> index"
haftmann
parents:
diff changeset
   186
  (SML "Int.+ ((_), (_))")
haftmann
parents:
diff changeset
   187
  (OCaml "Pervasives.+")
haftmann
parents:
diff changeset
   188
  (Haskell infixl 6 "+")
haftmann
parents:
diff changeset
   189
25767
852bce03412a index now a copy of nat rather than int
haftmann
parents: 25691
diff changeset
   190
code_const "minus_index' \<Colon> index \<Rightarrow> index \<Rightarrow> index"
24999
haftmann
parents:
diff changeset
   191
  (SML "Int.- ((_), (_))")
haftmann
parents:
diff changeset
   192
  (OCaml "Pervasives.-")
haftmann
parents:
diff changeset
   193
  (Haskell infixl 6 "-")
haftmann
parents:
diff changeset
   194
haftmann
parents:
diff changeset
   195
code_const "op * \<Colon> index \<Rightarrow> index \<Rightarrow> index"
haftmann
parents:
diff changeset
   196
  (SML "Int.* ((_), (_))")
haftmann
parents:
diff changeset
   197
  (OCaml "Pervasives.*")
haftmann
parents:
diff changeset
   198
  (Haskell infixl 7 "*")
haftmann
parents:
diff changeset
   199
haftmann
parents:
diff changeset
   200
code_const "op = \<Colon> index \<Rightarrow> index \<Rightarrow> bool"
haftmann
parents:
diff changeset
   201
  (SML "!((_ : Int.int) = _)")
haftmann
parents:
diff changeset
   202
  (OCaml "!((_ : Pervasives.int) = _)")
haftmann
parents:
diff changeset
   203
  (Haskell infixl 4 "==")
haftmann
parents:
diff changeset
   204
haftmann
parents:
diff changeset
   205
code_const "op \<le> \<Colon> index \<Rightarrow> index \<Rightarrow> bool"
haftmann
parents:
diff changeset
   206
  (SML "Int.<= ((_), (_))")
haftmann
parents:
diff changeset
   207
  (OCaml "!((_ : Pervasives.int) <= _)")
haftmann
parents:
diff changeset
   208
  (Haskell infix 4 "<=")
haftmann
parents:
diff changeset
   209
haftmann
parents:
diff changeset
   210
code_const "op < \<Colon> index \<Rightarrow> index \<Rightarrow> bool"
haftmann
parents:
diff changeset
   211
  (SML "Int.< ((_), (_))")
haftmann
parents:
diff changeset
   212
  (OCaml "!((_ : Pervasives.int) < _)")
haftmann
parents:
diff changeset
   213
  (Haskell infix 4 "<")
haftmann
parents:
diff changeset
   214
25767
852bce03412a index now a copy of nat rather than int
haftmann
parents: 25691
diff changeset
   215
code_const "op div \<Colon> index \<Rightarrow> index \<Rightarrow> index"
852bce03412a index now a copy of nat rather than int
haftmann
parents: 25691
diff changeset
   216
  (SML "IntInf.div ((_), (_))")
852bce03412a index now a copy of nat rather than int
haftmann
parents: 25691
diff changeset
   217
  (OCaml "Big'_int.div'_big'_int")
852bce03412a index now a copy of nat rather than int
haftmann
parents: 25691
diff changeset
   218
  (Haskell "div")
852bce03412a index now a copy of nat rather than int
haftmann
parents: 25691
diff changeset
   219
852bce03412a index now a copy of nat rather than int
haftmann
parents: 25691
diff changeset
   220
code_const "op mod \<Colon> index \<Rightarrow> index \<Rightarrow> index"
852bce03412a index now a copy of nat rather than int
haftmann
parents: 25691
diff changeset
   221
  (SML "IntInf.mod ((_), (_))")
852bce03412a index now a copy of nat rather than int
haftmann
parents: 25691
diff changeset
   222
  (OCaml "Big'_int.mod'_big'_int")
852bce03412a index now a copy of nat rather than int
haftmann
parents: 25691
diff changeset
   223
  (Haskell "mod")
852bce03412a index now a copy of nat rather than int
haftmann
parents: 25691
diff changeset
   224
24999
haftmann
parents:
diff changeset
   225
code_reserved SML Int
haftmann
parents:
diff changeset
   226
code_reserved OCaml Pervasives
haftmann
parents:
diff changeset
   227
haftmann
parents:
diff changeset
   228
end