src/HOL/Library/Code_Index.thy
author haftmann
Tue Sep 16 09:21:24 2008 +0200 (2008-09-16)
changeset 28228 7ebe8dc06cbb
parent 28042 1471f2974eb1
child 28346 b8390cd56b8f
permissions -rw-r--r--
evaluation using code generator
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@28228
     8
imports Plain "~~/src/HOL/Code_Eval" "~~/src/HOL/Presburger"
haftmann@24999
     9
begin
haftmann@24999
    10
haftmann@24999
    11
text {*
haftmann@25767
    12
  Indices are isomorphic to HOL @{typ nat} but
haftmann@27104
    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
wenzelm@26304
    48
lemma index_induct_raw:
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@27104
    77
rep_datatype "0 \<Colon> index" Suc_index
haftmann@26140
    78
proof -
haftmann@27104
    79
  fix P :: "index \<Rightarrow> bool"
haftmann@27104
    80
  fix k :: index
haftmann@26140
    81
  assume "P 0" then have init: "P (index_of_nat 0)" by simp
haftmann@26140
    82
  assume "\<And>k. P k \<Longrightarrow> P (Suc_index k)"
haftmann@27104
    83
    then have "\<And>n. P (index_of_nat n) \<Longrightarrow> P (Suc_index (index_of_nat n))" .
haftmann@26140
    84
    then have step: "\<And>n. P (index_of_nat n) \<Longrightarrow> P (index_of_nat (Suc n))" by simp
haftmann@26140
    85
  from init step have "P (index_of_nat (nat_of_index k))"
haftmann@26140
    86
    by (induct "nat_of_index k") simp_all
haftmann@26140
    87
  then show "P k" by simp
haftmann@27104
    88
qed simp_all
haftmann@26140
    89
haftmann@26140
    90
lemmas [code func del] = index.recs index.cases
haftmann@26140
    91
haftmann@26140
    92
declare index_case [case_names nat, cases type: index]
haftmann@27104
    93
declare index.induct [case_names nat, induct type: index]
haftmann@26140
    94
haftmann@26140
    95
lemma [code func]:
haftmann@26140
    96
  "index_size = nat_of_index"
haftmann@26140
    97
proof (rule ext)
haftmann@26140
    98
  fix k
haftmann@26140
    99
  have "index_size k = nat_size (nat_of_index k)"
haftmann@26140
   100
    by (induct k rule: index.induct) (simp_all del: zero_index_def Suc_index_def, simp_all)
haftmann@26140
   101
  also have "nat_size (nat_of_index k) = nat_of_index k" by (induct "nat_of_index k") simp_all
haftmann@26140
   102
  finally show "index_size k = nat_of_index k" .
haftmann@26140
   103
qed
haftmann@26140
   104
haftmann@26140
   105
lemma [code func]:
haftmann@26140
   106
  "size = nat_of_index"
haftmann@26140
   107
proof (rule ext)
haftmann@26140
   108
  fix k
haftmann@26140
   109
  show "size k = nat_of_index k"
haftmann@26140
   110
  by (induct k) (simp_all del: zero_index_def Suc_index_def, simp_all)
haftmann@26140
   111
qed
haftmann@26140
   112
haftmann@26140
   113
lemma [code func]:
haftmann@26140
   114
  "k = l \<longleftrightarrow> nat_of_index k = nat_of_index l"
haftmann@26140
   115
  by (cases k, cases l) simp
haftmann@24999
   116
haftmann@24999
   117
haftmann@25767
   118
subsection {* Indices as datatype of ints *}
haftmann@25767
   119
haftmann@25767
   120
instantiation index :: number
haftmann@25767
   121
begin
haftmann@24999
   122
haftmann@25767
   123
definition
haftmann@25767
   124
  "number_of = index_of_nat o nat"
haftmann@25767
   125
haftmann@25767
   126
instance ..
haftmann@25767
   127
haftmann@25767
   128
end
haftmann@24999
   129
haftmann@26264
   130
lemma nat_of_index_number [simp]:
haftmann@26264
   131
  "nat_of_index (number_of k) = number_of k"
haftmann@26264
   132
  by (simp add: number_of_index_def nat_number_of_def number_of_is_id)
haftmann@26264
   133
haftmann@24999
   134
code_datatype "number_of \<Colon> int \<Rightarrow> index"
haftmann@24999
   135
haftmann@24999
   136
haftmann@24999
   137
subsection {* Basic arithmetic *}
haftmann@24999
   138
haftmann@25767
   139
instantiation index :: "{minus, ordered_semidom, Divides.div, linorder}"
haftmann@25767
   140
begin
haftmann@24999
   141
haftmann@24999
   142
lemma zero_index_code [code inline, code func]:
haftmann@24999
   143
  "(0\<Colon>index) = Numeral0"
haftmann@25767
   144
  by (simp add: number_of_index_def Pls_def)
haftmann@25967
   145
lemma [code post]: "Numeral0 = (0\<Colon>index)"
haftmann@25967
   146
  using zero_index_code ..
haftmann@25767
   147
haftmann@25767
   148
definition [simp, code func del]:
haftmann@25767
   149
  "(1\<Colon>index) = index_of_nat 1"
haftmann@24999
   150
haftmann@24999
   151
lemma one_index_code [code inline, code func]:
haftmann@24999
   152
  "(1\<Colon>index) = Numeral1"
huffman@26086
   153
  by (simp add: number_of_index_def Pls_def Bit1_def)
haftmann@25967
   154
lemma [code post]: "Numeral1 = (1\<Colon>index)"
haftmann@25967
   155
  using one_index_code ..
haftmann@25767
   156
haftmann@25767
   157
definition [simp, code func del]:
haftmann@25767
   158
  "n + m = index_of_nat (nat_of_index n + nat_of_index m)"
haftmann@25767
   159
haftmann@25767
   160
lemma plus_index_code [code func]:
haftmann@25767
   161
  "index_of_nat n + index_of_nat m = index_of_nat (n + m)"
haftmann@25767
   162
  by simp
haftmann@25767
   163
haftmann@25767
   164
definition [simp, code func del]:
haftmann@25767
   165
  "n - m = index_of_nat (nat_of_index n - nat_of_index m)"
haftmann@25767
   166
haftmann@25767
   167
definition [simp, code func del]:
haftmann@25767
   168
  "n * m = index_of_nat (nat_of_index n * nat_of_index m)"
haftmann@25767
   169
haftmann@25767
   170
lemma times_index_code [code func]:
haftmann@25767
   171
  "index_of_nat n * index_of_nat m = index_of_nat (n * m)"
haftmann@24999
   172
  by simp
haftmann@24999
   173
haftmann@25767
   174
definition [simp, code func del]:
haftmann@25767
   175
  "n div m = index_of_nat (nat_of_index n div nat_of_index m)"
haftmann@24999
   176
haftmann@25767
   177
definition [simp, code func del]:
haftmann@25767
   178
  "n mod m = index_of_nat (nat_of_index n mod nat_of_index m)"
haftmann@24999
   179
haftmann@25767
   180
lemma div_index_code [code func]:
haftmann@25767
   181
  "index_of_nat n div index_of_nat m = index_of_nat (n div m)"
haftmann@25767
   182
  by simp
haftmann@25335
   183
haftmann@25767
   184
lemma mod_index_code [code func]:
haftmann@25767
   185
  "index_of_nat n mod index_of_nat m = index_of_nat (n mod m)"
haftmann@25767
   186
  by simp
haftmann@24999
   187
haftmann@25767
   188
definition [simp, code func del]:
haftmann@25767
   189
  "n \<le> m \<longleftrightarrow> nat_of_index n \<le> nat_of_index m"
haftmann@24999
   190
haftmann@25767
   191
definition [simp, code func del]:
haftmann@25767
   192
  "n < m \<longleftrightarrow> nat_of_index n < nat_of_index m"
haftmann@24999
   193
haftmann@25767
   194
lemma less_eq_index_code [code func]:
haftmann@25767
   195
  "index_of_nat n \<le> index_of_nat m \<longleftrightarrow> n \<le> m"
haftmann@25767
   196
  by simp
haftmann@24999
   197
haftmann@25767
   198
lemma less_index_code [code func]:
haftmann@25767
   199
  "index_of_nat n < index_of_nat m \<longleftrightarrow> n < m"
haftmann@25767
   200
  by simp
haftmann@24999
   201
haftmann@25767
   202
instance by default (auto simp add: left_distrib index)
haftmann@25767
   203
haftmann@25767
   204
end
haftmann@24999
   205
haftmann@26140
   206
lemma Suc_index_minus_one: "Suc_index n - 1 = n" by simp
haftmann@26140
   207
haftmann@25928
   208
lemma index_of_nat_code [code]:
haftmann@25918
   209
  "index_of_nat = of_nat"
haftmann@25918
   210
proof
haftmann@25918
   211
  fix n :: nat
haftmann@25918
   212
  have "of_nat n = index_of_nat n"
haftmann@25918
   213
    by (induct n) simp_all
haftmann@25918
   214
  then show "index_of_nat n = of_nat n"
haftmann@25918
   215
    by (rule sym)
haftmann@25918
   216
qed
haftmann@25918
   217
haftmann@25928
   218
lemma index_not_eq_zero: "i \<noteq> index_of_nat 0 \<longleftrightarrow> i \<ge> 1"
haftmann@25928
   219
  by (cases i) auto
haftmann@25928
   220
haftmann@25928
   221
definition
haftmann@25928
   222
  nat_of_index_aux :: "index \<Rightarrow> nat \<Rightarrow> nat"
haftmann@25928
   223
where
haftmann@25928
   224
  "nat_of_index_aux i n = nat_of_index i + n"
haftmann@25928
   225
haftmann@25928
   226
lemma nat_of_index_aux_code [code]:
haftmann@25928
   227
  "nat_of_index_aux i n = (if i = 0 then n else nat_of_index_aux (i - 1) (Suc n))"
haftmann@25928
   228
  by (auto simp add: nat_of_index_aux_def index_not_eq_zero)
haftmann@25928
   229
haftmann@25928
   230
lemma nat_of_index_code [code]:
haftmann@25928
   231
  "nat_of_index i = nat_of_index_aux i 0"
haftmann@25928
   232
  by (simp add: nat_of_index_aux_def)
haftmann@25918
   233
haftmann@24999
   234
krauss@28042
   235
text {* Measure function (for termination proofs) *}
krauss@28042
   236
haftmann@28228
   237
lemma [measure_function]:
haftmann@28228
   238
  "is_measure nat_of_index" by (rule is_measure_trivial)
krauss@28042
   239
haftmann@24999
   240
subsection {* ML interface *}
haftmann@24999
   241
haftmann@24999
   242
ML {*
haftmann@24999
   243
structure Index =
haftmann@24999
   244
struct
haftmann@24999
   245
haftmann@25928
   246
fun mk k = HOLogic.mk_number @{typ index} k;
haftmann@24999
   247
haftmann@24999
   248
end;
haftmann@24999
   249
*}
haftmann@24999
   250
haftmann@24999
   251
haftmann@26009
   252
subsection {* Specialized @{term "op - \<Colon> index \<Rightarrow> index \<Rightarrow> index"},
haftmann@26009
   253
  @{term "op div \<Colon> index \<Rightarrow> index \<Rightarrow> index"} and @{term "op mod \<Colon> index \<Rightarrow> index \<Rightarrow> index"}
haftmann@26009
   254
  operations *}
haftmann@26009
   255
haftmann@26009
   256
definition
haftmann@26009
   257
  minus_index_aux :: "index \<Rightarrow> index \<Rightarrow> index"
haftmann@26009
   258
where
haftmann@26009
   259
  [code func del]: "minus_index_aux = op -"
haftmann@26009
   260
haftmann@26009
   261
lemma [code func]: "op - = minus_index_aux"
haftmann@26009
   262
  using minus_index_aux_def ..
haftmann@26009
   263
haftmann@26009
   264
definition
haftmann@26009
   265
  div_mod_index ::  "index \<Rightarrow> index \<Rightarrow> index \<times> index"
haftmann@26009
   266
where
haftmann@26009
   267
  [code func del]: "div_mod_index n m = (n div m, n mod m)"
haftmann@26009
   268
haftmann@26009
   269
lemma [code func]:
haftmann@26009
   270
  "div_mod_index n m = (if m = 0 then (0, n) else (n div m, n mod m))"
haftmann@26009
   271
  unfolding div_mod_index_def by auto
haftmann@26009
   272
haftmann@26009
   273
lemma [code func]:
haftmann@26009
   274
  "n div m = fst (div_mod_index n m)"
haftmann@26009
   275
  unfolding div_mod_index_def by simp
haftmann@26009
   276
haftmann@26009
   277
lemma [code func]:
haftmann@26009
   278
  "n mod m = snd (div_mod_index n m)"
haftmann@26009
   279
  unfolding div_mod_index_def by simp
haftmann@26009
   280
haftmann@26009
   281
haftmann@28228
   282
subsection {* Code generator setup *}
haftmann@24999
   283
haftmann@25767
   284
text {* Implementation of indices by bounded integers *}
haftmann@25767
   285
haftmann@24999
   286
code_type index
haftmann@24999
   287
  (SML "int")
haftmann@24999
   288
  (OCaml "int")
haftmann@25967
   289
  (Haskell "Int")
haftmann@24999
   290
haftmann@24999
   291
code_instance index :: eq
haftmann@24999
   292
  (Haskell -)
haftmann@24999
   293
haftmann@24999
   294
setup {*
haftmann@25928
   295
  fold (Numeral.add_code @{const_name number_index_inst.number_of_index}
haftmann@25928
   296
    false false) ["SML", "OCaml", "Haskell"]
haftmann@24999
   297
*}
haftmann@24999
   298
haftmann@25918
   299
code_reserved SML Int int
haftmann@25918
   300
code_reserved OCaml Pervasives int
haftmann@24999
   301
haftmann@24999
   302
code_const "op + \<Colon> index \<Rightarrow> index \<Rightarrow> index"
haftmann@25928
   303
  (SML "Int.+/ ((_),/ (_))")
haftmann@25967
   304
  (OCaml "Pervasives.( + )")
haftmann@24999
   305
  (Haskell infixl 6 "+")
haftmann@24999
   306
haftmann@26009
   307
code_const "minus_index_aux \<Colon> index \<Rightarrow> index \<Rightarrow> index"
haftmann@25918
   308
  (SML "Int.max/ (_/ -/ _,/ 0 : int)")
haftmann@25918
   309
  (OCaml "Pervasives.max/ (_/ -/ _)/ (0 : int) ")
haftmann@25918
   310
  (Haskell "max/ (_/ -/ _)/ (0 :: Int)")
haftmann@24999
   311
haftmann@24999
   312
code_const "op * \<Colon> index \<Rightarrow> index \<Rightarrow> index"
haftmann@25928
   313
  (SML "Int.*/ ((_),/ (_))")
haftmann@25967
   314
  (OCaml "Pervasives.( * )")
haftmann@24999
   315
  (Haskell infixl 7 "*")
haftmann@24999
   316
haftmann@26009
   317
code_const div_mod_index
haftmann@26009
   318
  (SML "(fn n => fn m =>/ (n div m, n mod m))")
haftmann@26009
   319
  (OCaml "(fun n -> fun m ->/ (n '/ m, n mod m))")
haftmann@26009
   320
  (Haskell "divMod")
haftmann@25928
   321
haftmann@24999
   322
code_const "op = \<Colon> index \<Rightarrow> index \<Rightarrow> bool"
haftmann@24999
   323
  (SML "!((_ : Int.int) = _)")
haftmann@25967
   324
  (OCaml "!((_ : int) = _)")
haftmann@24999
   325
  (Haskell infixl 4 "==")
haftmann@24999
   326
haftmann@24999
   327
code_const "op \<le> \<Colon> index \<Rightarrow> index \<Rightarrow> bool"
haftmann@25928
   328
  (SML "Int.<=/ ((_),/ (_))")
haftmann@25967
   329
  (OCaml "!((_ : int) <= _)")
haftmann@24999
   330
  (Haskell infix 4 "<=")
haftmann@24999
   331
haftmann@24999
   332
code_const "op < \<Colon> index \<Rightarrow> index \<Rightarrow> bool"
haftmann@25928
   333
  (SML "Int.</ ((_),/ (_))")
haftmann@25967
   334
  (OCaml "!((_ : int) < _)")
haftmann@24999
   335
  (Haskell infix 4 "<")
haftmann@24999
   336
haftmann@28228
   337
text {* Evaluation *}
haftmann@28228
   338
haftmann@28228
   339
lemma [code func, code func del]:
haftmann@28228
   340
  "(Code_Eval.term_of \<Colon> index \<Rightarrow> term) = Code_Eval.term_of" ..
haftmann@28228
   341
haftmann@28228
   342
code_const "Code_Eval.term_of \<Colon> index \<Rightarrow> term"
haftmann@28228
   343
  (SML "HOLogic.mk'_number/ HOLogic.indexT/ (IntInf.fromInt/ _)")
haftmann@28228
   344
haftmann@24999
   345
end