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