src/HOL/Library/Code_Index.thy
author haftmann
Wed Mar 12 19:38:13 2008 +0100 (2008-03-12)
changeset 26264 89e25cc8da7a
parent 26140 e45375135052
child 26304 02fbd0e7954a
permissions -rw-r--r--
yet another useful lemma
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@26140
    18
typedef index = "UNIV \<Colon> nat set"
haftmann@26140
    19
  morphisms nat_of_index index_of_nat by rule
haftmann@24999
    20
haftmann@26140
    21
lemma index_of_nat_nat_of_index [simp]:
haftmann@26140
    22
  "index_of_nat (nat_of_index k) = k"
haftmann@26140
    23
  by (rule nat_of_index_inverse)
haftmann@24999
    24
haftmann@26140
    25
lemma nat_of_index_index_of_nat [simp]:
haftmann@26140
    26
  "nat_of_index (index_of_nat n) = n"
haftmann@26140
    27
  by (rule index_of_nat_inverse) 
haftmann@26140
    28
    (unfold index_def, rule UNIV_I)
haftmann@24999
    29
haftmann@24999
    30
lemma index:
haftmann@25767
    31
  "(\<And>n\<Colon>index. PROP P n) \<equiv> (\<And>n\<Colon>nat. PROP P (index_of_nat n))"
haftmann@24999
    32
proof
haftmann@25767
    33
  fix n :: nat
haftmann@25767
    34
  assume "\<And>n\<Colon>index. PROP P n"
haftmann@25767
    35
  then show "PROP P (index_of_nat n)" .
haftmann@24999
    36
next
haftmann@25767
    37
  fix n :: index
haftmann@25767
    38
  assume "\<And>n\<Colon>nat. PROP P (index_of_nat n)"
haftmann@25767
    39
  then have "PROP P (index_of_nat (nat_of_index n))" .
haftmann@25767
    40
  then show "PROP P n" by simp
haftmann@24999
    41
qed
haftmann@24999
    42
haftmann@26140
    43
lemma index_case:
haftmann@26140
    44
  assumes "\<And>n. k = index_of_nat n \<Longrightarrow> P"
haftmann@26140
    45
  shows P
haftmann@26140
    46
  by (rule assms [of "nat_of_index k"]) simp
haftmann@26140
    47
haftmann@26140
    48
lemma index_induct:
haftmann@26140
    49
  assumes "\<And>n. P (index_of_nat n)"
haftmann@26140
    50
  shows "P k"
haftmann@26140
    51
proof -
haftmann@26140
    52
  from assms have "P (index_of_nat (nat_of_index k))" .
haftmann@26140
    53
  then show ?thesis by simp
haftmann@26140
    54
qed
haftmann@26140
    55
haftmann@26140
    56
lemma nat_of_index_inject [simp]:
haftmann@26140
    57
  "nat_of_index k = nat_of_index l \<longleftrightarrow> k = l"
haftmann@26140
    58
  by (rule nat_of_index_inject)
haftmann@26140
    59
haftmann@26140
    60
lemma index_of_nat_inject [simp]:
haftmann@26140
    61
  "index_of_nat n = index_of_nat m \<longleftrightarrow> n = m"
haftmann@26140
    62
  by (auto intro!: index_of_nat_inject simp add: index_def)
haftmann@26140
    63
haftmann@26140
    64
instantiation index :: zero
haftmann@26140
    65
begin
haftmann@26140
    66
haftmann@26140
    67
definition [simp, code func del]:
haftmann@26140
    68
  "0 = index_of_nat 0"
haftmann@26140
    69
haftmann@26140
    70
instance ..
haftmann@26140
    71
haftmann@26140
    72
end
haftmann@26140
    73
haftmann@26140
    74
definition [simp]:
haftmann@26140
    75
  "Suc_index k = index_of_nat (Suc (nat_of_index k))"
haftmann@26140
    76
haftmann@26140
    77
lemma index_induct: "P 0 \<Longrightarrow> (\<And>k. P k \<Longrightarrow> P (Suc_index k)) \<Longrightarrow> P k"
haftmann@26140
    78
proof -
haftmann@26140
    79
  assume "P 0" then have init: "P (index_of_nat 0)" by simp
haftmann@26140
    80
  assume "\<And>k. P k \<Longrightarrow> P (Suc_index k)"
haftmann@26140
    81
    then have "\<And>n. P (index_of_nat n) \<Longrightarrow> P (Suc_index (index_of_nat (n)))" .
haftmann@26140
    82
    then have step: "\<And>n. P (index_of_nat n) \<Longrightarrow> P (index_of_nat (Suc n))" by simp
haftmann@26140
    83
  from init step have "P (index_of_nat (nat_of_index k))"
haftmann@26140
    84
    by (induct "nat_of_index k") simp_all
haftmann@26140
    85
  then show "P k" by simp
haftmann@26140
    86
qed
haftmann@26140
    87
haftmann@26140
    88
lemma Suc_not_Zero_index: "Suc_index k \<noteq> 0"
haftmann@26140
    89
  by simp
haftmann@26140
    90
haftmann@26140
    91
lemma Zero_not_Suc_index: "0 \<noteq> Suc_index k"
haftmann@26140
    92
  by simp
haftmann@26140
    93
haftmann@26140
    94
lemma Suc_Suc_index_eq: "Suc_index k = Suc_index l \<longleftrightarrow> k = l"
haftmann@26140
    95
  by simp
haftmann@26140
    96
haftmann@26140
    97
rep_datatype index
haftmann@26140
    98
  distinct  Suc_not_Zero_index Zero_not_Suc_index
haftmann@26140
    99
  inject    Suc_Suc_index_eq
haftmann@26140
   100
  induction index_induct
haftmann@26140
   101
haftmann@26140
   102
lemmas [code func del] = index.recs index.cases
haftmann@26140
   103
haftmann@26140
   104
declare index_case [case_names nat, cases type: index]
haftmann@26140
   105
declare index_induct [case_names nat, induct type: index]
haftmann@26140
   106
haftmann@26140
   107
lemma [code func]:
haftmann@26140
   108
  "index_size = nat_of_index"
haftmann@26140
   109
proof (rule ext)
haftmann@26140
   110
  fix k
haftmann@26140
   111
  have "index_size k = nat_size (nat_of_index k)"
haftmann@26140
   112
    by (induct k rule: index.induct) (simp_all del: zero_index_def Suc_index_def, simp_all)
haftmann@26140
   113
  also have "nat_size (nat_of_index k) = nat_of_index k" by (induct "nat_of_index k") simp_all
haftmann@26140
   114
  finally show "index_size k = nat_of_index k" .
haftmann@26140
   115
qed
haftmann@26140
   116
haftmann@26140
   117
lemma [code func]:
haftmann@26140
   118
  "size = nat_of_index"
haftmann@26140
   119
proof (rule ext)
haftmann@26140
   120
  fix k
haftmann@26140
   121
  show "size k = nat_of_index k"
haftmann@26140
   122
  by (induct k) (simp_all del: zero_index_def Suc_index_def, simp_all)
haftmann@26140
   123
qed
haftmann@26140
   124
haftmann@26140
   125
lemma [code func]:
haftmann@26140
   126
  "k = l \<longleftrightarrow> nat_of_index k = nat_of_index l"
haftmann@26140
   127
  by (cases k, cases l) simp
haftmann@24999
   128
haftmann@24999
   129
haftmann@25767
   130
subsection {* Indices as datatype of ints *}
haftmann@25767
   131
haftmann@25767
   132
instantiation index :: number
haftmann@25767
   133
begin
haftmann@24999
   134
haftmann@25767
   135
definition
haftmann@25767
   136
  "number_of = index_of_nat o nat"
haftmann@25767
   137
haftmann@25767
   138
instance ..
haftmann@25767
   139
haftmann@25767
   140
end
haftmann@24999
   141
haftmann@26264
   142
lemma nat_of_index_number [simp]:
haftmann@26264
   143
  "nat_of_index (number_of k) = number_of k"
haftmann@26264
   144
  by (simp add: number_of_index_def nat_number_of_def number_of_is_id)
haftmann@26264
   145
haftmann@24999
   146
code_datatype "number_of \<Colon> int \<Rightarrow> index"
haftmann@24999
   147
haftmann@24999
   148
haftmann@24999
   149
subsection {* Basic arithmetic *}
haftmann@24999
   150
haftmann@25767
   151
instantiation index :: "{minus, ordered_semidom, Divides.div, linorder}"
haftmann@25767
   152
begin
haftmann@24999
   153
haftmann@24999
   154
lemma zero_index_code [code inline, code func]:
haftmann@24999
   155
  "(0\<Colon>index) = Numeral0"
haftmann@25767
   156
  by (simp add: number_of_index_def Pls_def)
haftmann@25967
   157
lemma [code post]: "Numeral0 = (0\<Colon>index)"
haftmann@25967
   158
  using zero_index_code ..
haftmann@25767
   159
haftmann@25767
   160
definition [simp, code func del]:
haftmann@25767
   161
  "(1\<Colon>index) = index_of_nat 1"
haftmann@24999
   162
haftmann@24999
   163
lemma one_index_code [code inline, code func]:
haftmann@24999
   164
  "(1\<Colon>index) = Numeral1"
huffman@26086
   165
  by (simp add: number_of_index_def Pls_def Bit1_def)
haftmann@25967
   166
lemma [code post]: "Numeral1 = (1\<Colon>index)"
haftmann@25967
   167
  using one_index_code ..
haftmann@25767
   168
haftmann@25767
   169
definition [simp, code func del]:
haftmann@25767
   170
  "n + m = index_of_nat (nat_of_index n + nat_of_index m)"
haftmann@25767
   171
haftmann@25767
   172
lemma plus_index_code [code func]:
haftmann@25767
   173
  "index_of_nat n + index_of_nat m = index_of_nat (n + m)"
haftmann@25767
   174
  by simp
haftmann@25767
   175
haftmann@25767
   176
definition [simp, code func del]:
haftmann@25767
   177
  "n - m = index_of_nat (nat_of_index n - nat_of_index m)"
haftmann@25767
   178
haftmann@25767
   179
definition [simp, code func del]:
haftmann@25767
   180
  "n * m = index_of_nat (nat_of_index n * nat_of_index m)"
haftmann@25767
   181
haftmann@25767
   182
lemma times_index_code [code func]:
haftmann@25767
   183
  "index_of_nat n * index_of_nat m = index_of_nat (n * m)"
haftmann@24999
   184
  by simp
haftmann@24999
   185
haftmann@25767
   186
definition [simp, code func del]:
haftmann@25767
   187
  "n div m = index_of_nat (nat_of_index n div nat_of_index m)"
haftmann@24999
   188
haftmann@25767
   189
definition [simp, code func del]:
haftmann@25767
   190
  "n mod m = index_of_nat (nat_of_index n mod nat_of_index m)"
haftmann@24999
   191
haftmann@25767
   192
lemma div_index_code [code func]:
haftmann@25767
   193
  "index_of_nat n div index_of_nat m = index_of_nat (n div m)"
haftmann@25767
   194
  by simp
haftmann@25335
   195
haftmann@25767
   196
lemma mod_index_code [code func]:
haftmann@25767
   197
  "index_of_nat n mod index_of_nat m = index_of_nat (n mod m)"
haftmann@25767
   198
  by simp
haftmann@24999
   199
haftmann@25767
   200
definition [simp, code func del]:
haftmann@25767
   201
  "n \<le> m \<longleftrightarrow> nat_of_index n \<le> nat_of_index m"
haftmann@24999
   202
haftmann@25767
   203
definition [simp, code func del]:
haftmann@25767
   204
  "n < m \<longleftrightarrow> nat_of_index n < nat_of_index m"
haftmann@24999
   205
haftmann@25767
   206
lemma less_eq_index_code [code func]:
haftmann@25767
   207
  "index_of_nat n \<le> index_of_nat m \<longleftrightarrow> n \<le> m"
haftmann@25767
   208
  by simp
haftmann@24999
   209
haftmann@25767
   210
lemma less_index_code [code func]:
haftmann@25767
   211
  "index_of_nat n < index_of_nat m \<longleftrightarrow> n < m"
haftmann@25767
   212
  by simp
haftmann@24999
   213
haftmann@25767
   214
instance by default (auto simp add: left_distrib index)
haftmann@25767
   215
haftmann@25767
   216
end
haftmann@24999
   217
haftmann@26140
   218
lemma Suc_index_minus_one: "Suc_index n - 1 = n" by simp
haftmann@26140
   219
haftmann@25928
   220
lemma index_of_nat_code [code]:
haftmann@25918
   221
  "index_of_nat = of_nat"
haftmann@25918
   222
proof
haftmann@25918
   223
  fix n :: nat
haftmann@25918
   224
  have "of_nat n = index_of_nat n"
haftmann@25918
   225
    by (induct n) simp_all
haftmann@25918
   226
  then show "index_of_nat n = of_nat n"
haftmann@25918
   227
    by (rule sym)
haftmann@25918
   228
qed
haftmann@25918
   229
haftmann@25928
   230
lemma index_not_eq_zero: "i \<noteq> index_of_nat 0 \<longleftrightarrow> i \<ge> 1"
haftmann@25928
   231
  by (cases i) auto
haftmann@25928
   232
haftmann@25928
   233
definition
haftmann@25928
   234
  nat_of_index_aux :: "index \<Rightarrow> nat \<Rightarrow> nat"
haftmann@25928
   235
where
haftmann@25928
   236
  "nat_of_index_aux i n = nat_of_index i + n"
haftmann@25928
   237
haftmann@25928
   238
lemma nat_of_index_aux_code [code]:
haftmann@25928
   239
  "nat_of_index_aux i n = (if i = 0 then n else nat_of_index_aux (i - 1) (Suc n))"
haftmann@25928
   240
  by (auto simp add: nat_of_index_aux_def index_not_eq_zero)
haftmann@25928
   241
haftmann@25928
   242
lemma nat_of_index_code [code]:
haftmann@25928
   243
  "nat_of_index i = nat_of_index_aux i 0"
haftmann@25928
   244
  by (simp add: nat_of_index_aux_def)
haftmann@25918
   245
haftmann@24999
   246
haftmann@24999
   247
subsection {* ML interface *}
haftmann@24999
   248
haftmann@24999
   249
ML {*
haftmann@24999
   250
structure Index =
haftmann@24999
   251
struct
haftmann@24999
   252
haftmann@25928
   253
fun mk k = HOLogic.mk_number @{typ index} k;
haftmann@24999
   254
haftmann@24999
   255
end;
haftmann@24999
   256
*}
haftmann@24999
   257
haftmann@24999
   258
haftmann@26009
   259
subsection {* Specialized @{term "op - \<Colon> index \<Rightarrow> index \<Rightarrow> index"},
haftmann@26009
   260
  @{term "op div \<Colon> index \<Rightarrow> index \<Rightarrow> index"} and @{term "op mod \<Colon> index \<Rightarrow> index \<Rightarrow> index"}
haftmann@26009
   261
  operations *}
haftmann@26009
   262
haftmann@26009
   263
definition
haftmann@26009
   264
  minus_index_aux :: "index \<Rightarrow> index \<Rightarrow> index"
haftmann@26009
   265
where
haftmann@26009
   266
  [code func del]: "minus_index_aux = op -"
haftmann@26009
   267
haftmann@26009
   268
lemma [code func]: "op - = minus_index_aux"
haftmann@26009
   269
  using minus_index_aux_def ..
haftmann@26009
   270
haftmann@26009
   271
definition
haftmann@26009
   272
  div_mod_index ::  "index \<Rightarrow> index \<Rightarrow> index \<times> index"
haftmann@26009
   273
where
haftmann@26009
   274
  [code func del]: "div_mod_index n m = (n div m, n mod m)"
haftmann@26009
   275
haftmann@26009
   276
lemma [code func]:
haftmann@26009
   277
  "div_mod_index n m = (if m = 0 then (0, n) else (n div m, n mod m))"
haftmann@26009
   278
  unfolding div_mod_index_def by auto
haftmann@26009
   279
haftmann@26009
   280
lemma [code func]:
haftmann@26009
   281
  "n div m = fst (div_mod_index n m)"
haftmann@26009
   282
  unfolding div_mod_index_def by simp
haftmann@26009
   283
haftmann@26009
   284
lemma [code func]:
haftmann@26009
   285
  "n mod m = snd (div_mod_index n m)"
haftmann@26009
   286
  unfolding div_mod_index_def by simp
haftmann@26009
   287
haftmann@26009
   288
haftmann@24999
   289
subsection {* Code serialization *}
haftmann@24999
   290
haftmann@25767
   291
text {* Implementation of indices by bounded integers *}
haftmann@25767
   292
haftmann@24999
   293
code_type index
haftmann@24999
   294
  (SML "int")
haftmann@24999
   295
  (OCaml "int")
haftmann@25967
   296
  (Haskell "Int")
haftmann@24999
   297
haftmann@24999
   298
code_instance index :: eq
haftmann@24999
   299
  (Haskell -)
haftmann@24999
   300
haftmann@24999
   301
setup {*
haftmann@25928
   302
  fold (Numeral.add_code @{const_name number_index_inst.number_of_index}
haftmann@25928
   303
    false false) ["SML", "OCaml", "Haskell"]
haftmann@24999
   304
*}
haftmann@24999
   305
haftmann@25918
   306
code_reserved SML Int int
haftmann@25918
   307
code_reserved OCaml Pervasives int
haftmann@24999
   308
haftmann@24999
   309
code_const "op + \<Colon> index \<Rightarrow> index \<Rightarrow> index"
haftmann@25928
   310
  (SML "Int.+/ ((_),/ (_))")
haftmann@25967
   311
  (OCaml "Pervasives.( + )")
haftmann@24999
   312
  (Haskell infixl 6 "+")
haftmann@24999
   313
haftmann@26009
   314
code_const "minus_index_aux \<Colon> index \<Rightarrow> index \<Rightarrow> index"
haftmann@25918
   315
  (SML "Int.max/ (_/ -/ _,/ 0 : int)")
haftmann@25918
   316
  (OCaml "Pervasives.max/ (_/ -/ _)/ (0 : int) ")
haftmann@25918
   317
  (Haskell "max/ (_/ -/ _)/ (0 :: Int)")
haftmann@24999
   318
haftmann@24999
   319
code_const "op * \<Colon> index \<Rightarrow> index \<Rightarrow> index"
haftmann@25928
   320
  (SML "Int.*/ ((_),/ (_))")
haftmann@25967
   321
  (OCaml "Pervasives.( * )")
haftmann@24999
   322
  (Haskell infixl 7 "*")
haftmann@24999
   323
haftmann@26009
   324
code_const div_mod_index
haftmann@26009
   325
  (SML "(fn n => fn m =>/ (n div m, n mod m))")
haftmann@26009
   326
  (OCaml "(fun n -> fun m ->/ (n '/ m, n mod m))")
haftmann@26009
   327
  (Haskell "divMod")
haftmann@25928
   328
haftmann@24999
   329
code_const "op = \<Colon> index \<Rightarrow> index \<Rightarrow> bool"
haftmann@24999
   330
  (SML "!((_ : Int.int) = _)")
haftmann@25967
   331
  (OCaml "!((_ : int) = _)")
haftmann@24999
   332
  (Haskell infixl 4 "==")
haftmann@24999
   333
haftmann@24999
   334
code_const "op \<le> \<Colon> index \<Rightarrow> index \<Rightarrow> bool"
haftmann@25928
   335
  (SML "Int.<=/ ((_),/ (_))")
haftmann@25967
   336
  (OCaml "!((_ : int) <= _)")
haftmann@24999
   337
  (Haskell infix 4 "<=")
haftmann@24999
   338
haftmann@24999
   339
code_const "op < \<Colon> index \<Rightarrow> index \<Rightarrow> bool"
haftmann@25928
   340
  (SML "Int.</ ((_),/ (_))")
haftmann@25967
   341
  (OCaml "!((_ : int) < _)")
haftmann@24999
   342
  (Haskell infix 4 "<")
haftmann@24999
   343
haftmann@24999
   344
end