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