src/HOL/Code_Numeral.thy
author haftmann
Tue May 19 16:54:55 2009 +0200 (2009-05-19)
changeset 31205 98370b26c2ce
parent 31203 src/HOL/Code_Index.thy@5c8fb4fd67e0
child 31266 55e70b6d812e
permissions -rw-r--r--
String.literal replaces message_string, code_numeral replaces (code_)index
haftmann@29815
     1
(* Author: Florian Haftmann, TU Muenchen *)
haftmann@24999
     2
haftmann@31205
     3
header {* Type of target language numerals *}
haftmann@24999
     4
haftmann@31205
     5
theory Code_Numeral
haftmann@31203
     6
imports Nat_Numeral
haftmann@24999
     7
begin
haftmann@24999
     8
haftmann@24999
     9
text {*
haftmann@31205
    10
  Code numerals are isomorphic to HOL @{typ nat} but
haftmann@31205
    11
  mapped to target-language builtin numerals.
haftmann@24999
    12
*}
haftmann@24999
    13
haftmann@31205
    14
subsection {* Datatype of target language numerals *}
haftmann@24999
    15
haftmann@31205
    16
typedef (open) code_numeral = "UNIV \<Colon> nat set"
haftmann@29815
    17
  morphisms nat_of of_nat by rule
haftmann@24999
    18
haftmann@29815
    19
lemma of_nat_nat_of [simp]:
haftmann@29815
    20
  "of_nat (nat_of k) = k"
haftmann@29815
    21
  by (rule nat_of_inverse)
haftmann@24999
    22
haftmann@29815
    23
lemma nat_of_of_nat [simp]:
haftmann@29815
    24
  "nat_of (of_nat n) = n"
haftmann@29815
    25
  by (rule of_nat_inverse) (rule UNIV_I)
haftmann@24999
    26
haftmann@28708
    27
lemma [measure_function]:
haftmann@29815
    28
  "is_measure nat_of" by (rule is_measure_trivial)
haftmann@28708
    29
haftmann@31205
    30
lemma code_numeral:
haftmann@31205
    31
  "(\<And>n\<Colon>code_numeral. PROP P n) \<equiv> (\<And>n\<Colon>nat. PROP P (of_nat n))"
haftmann@24999
    32
proof
haftmann@25767
    33
  fix n :: nat
haftmann@31205
    34
  assume "\<And>n\<Colon>code_numeral. PROP P n"
haftmann@29815
    35
  then show "PROP P (of_nat n)" .
haftmann@24999
    36
next
haftmann@31205
    37
  fix n :: code_numeral
haftmann@29815
    38
  assume "\<And>n\<Colon>nat. PROP P (of_nat n)"
haftmann@29815
    39
  then have "PROP P (of_nat (nat_of n))" .
haftmann@25767
    40
  then show "PROP P n" by simp
haftmann@24999
    41
qed
haftmann@24999
    42
haftmann@31205
    43
lemma code_numeral_case:
haftmann@29815
    44
  assumes "\<And>n. k = of_nat n \<Longrightarrow> P"
haftmann@26140
    45
  shows P
haftmann@29815
    46
  by (rule assms [of "nat_of k"]) simp
haftmann@26140
    47
haftmann@31205
    48
lemma code_numeral_induct_raw:
haftmann@29815
    49
  assumes "\<And>n. P (of_nat n)"
haftmann@26140
    50
  shows "P k"
haftmann@26140
    51
proof -
haftmann@29815
    52
  from assms have "P (of_nat (nat_of k))" .
haftmann@26140
    53
  then show ?thesis by simp
haftmann@26140
    54
qed
haftmann@26140
    55
haftmann@29815
    56
lemma nat_of_inject [simp]:
haftmann@29815
    57
  "nat_of k = nat_of l \<longleftrightarrow> k = l"
haftmann@29815
    58
  by (rule nat_of_inject)
haftmann@26140
    59
haftmann@29815
    60
lemma of_nat_inject [simp]:
haftmann@29815
    61
  "of_nat n = of_nat m \<longleftrightarrow> n = m"
haftmann@29815
    62
  by (rule of_nat_inject) (rule UNIV_I)+
haftmann@26140
    63
haftmann@31205
    64
instantiation code_numeral :: zero
haftmann@26140
    65
begin
haftmann@26140
    66
haftmann@28562
    67
definition [simp, code del]:
haftmann@29815
    68
  "0 = 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@31205
    75
  "Suc_code_numeral k = of_nat (Suc (nat_of k))"
haftmann@26140
    76
haftmann@31205
    77
rep_datatype "0 \<Colon> code_numeral" Suc_code_numeral
haftmann@26140
    78
proof -
haftmann@31205
    79
  fix P :: "code_numeral \<Rightarrow> bool"
haftmann@31205
    80
  fix k :: code_numeral
haftmann@29815
    81
  assume "P 0" then have init: "P (of_nat 0)" by simp
haftmann@31205
    82
  assume "\<And>k. P k \<Longrightarrow> P (Suc_code_numeral k)"
haftmann@31205
    83
    then have "\<And>n. P (of_nat n) \<Longrightarrow> P (Suc_code_numeral (of_nat n))" .
haftmann@29815
    84
    then have step: "\<And>n. P (of_nat n) \<Longrightarrow> P (of_nat (Suc n))" by simp
haftmann@29815
    85
  from init step have "P (of_nat (nat_of k))"
haftmann@29815
    86
    by (induct "nat_of k") simp_all
haftmann@26140
    87
  then show "P k" by simp
haftmann@27104
    88
qed simp_all
haftmann@26140
    89
haftmann@31205
    90
declare code_numeral_case [case_names nat, cases type: code_numeral]
haftmann@31205
    91
declare code_numeral.induct [case_names nat, induct type: code_numeral]
haftmann@26140
    92
haftmann@31205
    93
lemma code_numeral_decr [termination_simp]:
haftmann@31205
    94
  "k \<noteq> of_nat 0 \<Longrightarrow> nat_of k - Suc 0 < nat_of k"
haftmann@30245
    95
  by (cases k) simp
haftmann@30245
    96
haftmann@30245
    97
lemma [simp, code]:
haftmann@31205
    98
  "code_numeral_size = nat_of"
haftmann@26140
    99
proof (rule ext)
haftmann@26140
   100
  fix k
haftmann@31205
   101
  have "code_numeral_size k = nat_size (nat_of k)"
haftmann@31205
   102
    by (induct k rule: code_numeral.induct) (simp_all del: zero_code_numeral_def Suc_code_numeral_def, simp_all)
haftmann@29815
   103
  also have "nat_size (nat_of k) = nat_of k" by (induct "nat_of k") simp_all
haftmann@31205
   104
  finally show "code_numeral_size k = nat_of k" .
haftmann@26140
   105
qed
haftmann@26140
   106
haftmann@30245
   107
lemma [simp, code]:
haftmann@29815
   108
  "size = nat_of"
haftmann@26140
   109
proof (rule ext)
haftmann@26140
   110
  fix k
haftmann@29815
   111
  show "size k = nat_of k"
haftmann@31205
   112
  by (induct k) (simp_all del: zero_code_numeral_def Suc_code_numeral_def, simp_all)
haftmann@26140
   113
qed
haftmann@26140
   114
haftmann@31205
   115
lemmas [code del] = code_numeral.recs code_numeral.cases
haftmann@30245
   116
haftmann@28562
   117
lemma [code]:
haftmann@29815
   118
  "eq_class.eq k l \<longleftrightarrow> eq_class.eq (nat_of k) (nat_of l)"
haftmann@28346
   119
  by (cases k, cases l) (simp add: eq)
haftmann@24999
   120
haftmann@28351
   121
lemma [code nbe]:
haftmann@31205
   122
  "eq_class.eq (k::code_numeral) k \<longleftrightarrow> True"
haftmann@28351
   123
  by (rule HOL.eq_refl)
haftmann@28351
   124
haftmann@24999
   125
haftmann@25767
   126
subsection {* Indices as datatype of ints *}
haftmann@25767
   127
haftmann@31205
   128
instantiation code_numeral :: number
haftmann@25767
   129
begin
haftmann@24999
   130
haftmann@25767
   131
definition
haftmann@29815
   132
  "number_of = of_nat o nat"
haftmann@25767
   133
haftmann@25767
   134
instance ..
haftmann@25767
   135
haftmann@25767
   136
end
haftmann@24999
   137
haftmann@29815
   138
lemma nat_of_number [simp]:
haftmann@29815
   139
  "nat_of (number_of k) = number_of k"
haftmann@31205
   140
  by (simp add: number_of_code_numeral_def nat_number_of_def number_of_is_id)
haftmann@26264
   141
haftmann@31205
   142
code_datatype "number_of \<Colon> int \<Rightarrow> code_numeral"
haftmann@24999
   143
haftmann@24999
   144
haftmann@24999
   145
subsection {* Basic arithmetic *}
haftmann@24999
   146
haftmann@31205
   147
instantiation code_numeral :: "{minus, ordered_semidom, semiring_div, linorder}"
haftmann@25767
   148
begin
haftmann@24999
   149
haftmann@28562
   150
definition [simp, code del]:
haftmann@31205
   151
  "(1\<Colon>code_numeral) = of_nat 1"
haftmann@24999
   152
haftmann@28562
   153
definition [simp, code del]:
haftmann@29815
   154
  "n + m = of_nat (nat_of n + nat_of m)"
haftmann@25767
   155
haftmann@28562
   156
definition [simp, code del]:
haftmann@29815
   157
  "n - m = of_nat (nat_of n - nat_of m)"
haftmann@25767
   158
haftmann@28562
   159
definition [simp, code del]:
haftmann@29815
   160
  "n * m = of_nat (nat_of n * nat_of m)"
haftmann@25767
   161
haftmann@28562
   162
definition [simp, code del]:
haftmann@29815
   163
  "n div m = of_nat (nat_of n div nat_of m)"
haftmann@24999
   164
haftmann@28562
   165
definition [simp, code del]:
haftmann@29815
   166
  "n mod m = of_nat (nat_of n mod nat_of m)"
haftmann@24999
   167
haftmann@28562
   168
definition [simp, code del]:
haftmann@29815
   169
  "n \<le> m \<longleftrightarrow> nat_of n \<le> nat_of m"
haftmann@24999
   170
haftmann@28562
   171
definition [simp, code del]:
haftmann@29815
   172
  "n < m \<longleftrightarrow> nat_of n < nat_of m"
haftmann@24999
   173
haftmann@29815
   174
instance proof
haftmann@31205
   175
qed (auto simp add: code_numeral left_distrib div_mult_self1)
haftmann@28708
   176
haftmann@28708
   177
end
haftmann@28708
   178
haftmann@31205
   179
lemma zero_code_numeral_code [code inline, code]:
haftmann@31205
   180
  "(0\<Colon>code_numeral) = Numeral0"
haftmann@31205
   181
  by (simp add: number_of_code_numeral_def Pls_def)
haftmann@31205
   182
lemma [code post]: "Numeral0 = (0\<Colon>code_numeral)"
haftmann@31205
   183
  using zero_code_numeral_code ..
haftmann@28708
   184
haftmann@31205
   185
lemma one_code_numeral_code [code inline, code]:
haftmann@31205
   186
  "(1\<Colon>code_numeral) = Numeral1"
haftmann@31205
   187
  by (simp add: number_of_code_numeral_def Pls_def Bit1_def)
haftmann@31205
   188
lemma [code post]: "Numeral1 = (1\<Colon>code_numeral)"
haftmann@31205
   189
  using one_code_numeral_code ..
haftmann@28708
   190
haftmann@31205
   191
lemma plus_code_numeral_code [code nbe]:
haftmann@29815
   192
  "of_nat n + of_nat m = of_nat (n + m)"
haftmann@28708
   193
  by simp
haftmann@28708
   194
haftmann@31205
   195
definition subtract_code_numeral :: "code_numeral \<Rightarrow> code_numeral \<Rightarrow> code_numeral" where
haftmann@31205
   196
  [simp, code del]: "subtract_code_numeral = op -"
haftmann@28708
   197
haftmann@31205
   198
lemma subtract_code_numeral_code [code nbe]:
haftmann@31205
   199
  "subtract_code_numeral (of_nat n) (of_nat m) = of_nat (n - m)"
haftmann@28708
   200
  by simp
haftmann@28708
   201
haftmann@31205
   202
lemma minus_code_numeral_code [code]:
haftmann@31205
   203
  "n - m = subtract_code_numeral n m"
haftmann@28708
   204
  by simp
haftmann@28708
   205
haftmann@31205
   206
lemma times_code_numeral_code [code nbe]:
haftmann@29815
   207
  "of_nat n * of_nat m = of_nat (n * m)"
haftmann@28708
   208
  by simp
haftmann@28708
   209
haftmann@31205
   210
lemma less_eq_code_numeral_code [code nbe]:
haftmann@29815
   211
  "of_nat n \<le> of_nat m \<longleftrightarrow> n \<le> m"
haftmann@25767
   212
  by simp
haftmann@24999
   213
haftmann@31205
   214
lemma less_code_numeral_code [code nbe]:
haftmann@29815
   215
  "of_nat n < of_nat m \<longleftrightarrow> n < m"
haftmann@25767
   216
  by simp
haftmann@24999
   217
haftmann@31205
   218
lemma Suc_code_numeral_minus_one: "Suc_code_numeral n - 1 = n" by simp
haftmann@26140
   219
haftmann@29815
   220
lemma of_nat_code [code]:
haftmann@29815
   221
  "of_nat = Nat.of_nat"
haftmann@25918
   222
proof
haftmann@25918
   223
  fix n :: nat
haftmann@29815
   224
  have "Nat.of_nat n = of_nat n"
haftmann@25918
   225
    by (induct n) simp_all
haftmann@29815
   226
  then show "of_nat n = Nat.of_nat n"
haftmann@25918
   227
    by (rule sym)
haftmann@25918
   228
qed
haftmann@25918
   229
haftmann@31205
   230
lemma code_numeral_not_eq_zero: "i \<noteq> of_nat 0 \<longleftrightarrow> i \<ge> 1"
haftmann@25928
   231
  by (cases i) auto
haftmann@25928
   232
haftmann@31205
   233
definition nat_of_aux :: "code_numeral \<Rightarrow> nat \<Rightarrow> nat" where
haftmann@29815
   234
  "nat_of_aux i n = nat_of i + n"
haftmann@25928
   235
haftmann@29815
   236
lemma nat_of_aux_code [code]:
haftmann@29815
   237
  "nat_of_aux i n = (if i = 0 then n else nat_of_aux (i - 1) (Suc n))"
haftmann@31205
   238
  by (auto simp add: nat_of_aux_def code_numeral_not_eq_zero)
haftmann@25928
   239
haftmann@29815
   240
lemma nat_of_code [code]:
haftmann@29815
   241
  "nat_of i = nat_of_aux i 0"
haftmann@29815
   242
  by (simp add: nat_of_aux_def)
haftmann@25918
   243
haftmann@31205
   244
definition div_mod_code_numeral ::  "code_numeral \<Rightarrow> code_numeral \<Rightarrow> code_numeral \<times> code_numeral" where
haftmann@31205
   245
  [code del]: "div_mod_code_numeral n m = (n div m, n mod m)"
haftmann@26009
   246
haftmann@28562
   247
lemma [code]:
haftmann@31205
   248
  "div_mod_code_numeral n m = (if m = 0 then (0, n) else (n div m, n mod m))"
haftmann@31205
   249
  unfolding div_mod_code_numeral_def by auto
haftmann@26009
   250
haftmann@28562
   251
lemma [code]:
haftmann@31205
   252
  "n div m = fst (div_mod_code_numeral n m)"
haftmann@31205
   253
  unfolding div_mod_code_numeral_def by simp
haftmann@26009
   254
haftmann@28562
   255
lemma [code]:
haftmann@31205
   256
  "n mod m = snd (div_mod_code_numeral n m)"
haftmann@31205
   257
  unfolding div_mod_code_numeral_def by simp
haftmann@26009
   258
haftmann@31205
   259
definition int_of :: "code_numeral \<Rightarrow> int" where
haftmann@31192
   260
  "int_of = Nat.of_nat o nat_of"
haftmann@28708
   261
haftmann@31192
   262
lemma int_of_code [code]:
haftmann@31192
   263
  "int_of k = (if k = 0 then 0
haftmann@31192
   264
    else (if k mod 2 = 0 then 2 * int_of (k div 2) else 2 * int_of (k div 2) + 1))"
haftmann@31192
   265
  by (auto simp add: int_of_def mod_div_equality')
haftmann@28708
   266
haftmann@31192
   267
hide (open) const of_nat nat_of int_of
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@31205
   274
code_type code_numeral
haftmann@24999
   275
  (SML "int")
haftmann@24999
   276
  (OCaml "int")
haftmann@25967
   277
  (Haskell "Int")
haftmann@24999
   278
haftmann@31205
   279
code_instance code_numeral :: eq
haftmann@24999
   280
  (Haskell -)
haftmann@24999
   281
haftmann@24999
   282
setup {*
haftmann@31205
   283
  fold (Numeral.add_code @{const_name number_code_numeral_inst.number_of_code_numeral}
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@31205
   290
code_const "op + \<Colon> code_numeral \<Rightarrow> code_numeral \<Rightarrow> code_numeral"
haftmann@25928
   291
  (SML "Int.+/ ((_),/ (_))")
haftmann@25967
   292
  (OCaml "Pervasives.( + )")
haftmann@24999
   293
  (Haskell infixl 6 "+")
haftmann@24999
   294
haftmann@31205
   295
code_const "subtract_code_numeral \<Colon> code_numeral \<Rightarrow> code_numeral \<Rightarrow> code_numeral"
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@31205
   300
code_const "op * \<Colon> code_numeral \<Rightarrow> code_numeral \<Rightarrow> code_numeral"
haftmann@25928
   301
  (SML "Int.*/ ((_),/ (_))")
haftmann@25967
   302
  (OCaml "Pervasives.( * )")
haftmann@24999
   303
  (Haskell infixl 7 "*")
haftmann@24999
   304
haftmann@31205
   305
code_const div_mod_code_numeral
haftmann@29823
   306
  (SML "(fn n => fn m =>/ if m = 0/ then (0, n) else/ (n div m, n mod m))")
haftmann@29823
   307
  (OCaml "(fun n -> fun m ->/ if m = 0/ then (0, n) else/ (n '/ m, n mod m))")
haftmann@26009
   308
  (Haskell "divMod")
haftmann@25928
   309
haftmann@31205
   310
code_const "eq_class.eq \<Colon> code_numeral \<Rightarrow> code_numeral \<Rightarrow> bool"
haftmann@24999
   311
  (SML "!((_ : Int.int) = _)")
haftmann@25967
   312
  (OCaml "!((_ : int) = _)")
haftmann@24999
   313
  (Haskell infixl 4 "==")
haftmann@24999
   314
haftmann@31205
   315
code_const "op \<le> \<Colon> code_numeral \<Rightarrow> code_numeral \<Rightarrow> bool"
haftmann@25928
   316
  (SML "Int.<=/ ((_),/ (_))")
haftmann@25967
   317
  (OCaml "!((_ : int) <= _)")
haftmann@24999
   318
  (Haskell infix 4 "<=")
haftmann@24999
   319
haftmann@31205
   320
code_const "op < \<Colon> code_numeral \<Rightarrow> code_numeral \<Rightarrow> bool"
haftmann@25928
   321
  (SML "Int.</ ((_),/ (_))")
haftmann@25967
   322
  (OCaml "!((_ : int) < _)")
haftmann@24999
   323
  (Haskell infix 4 "<")
haftmann@24999
   324
haftmann@24999
   325
end