src/HOL/Code_Numeral.thy
author haftmann
Thu Oct 29 11:41:36 2009 +0100 (2009-10-29)
changeset 33318 ddd97d9dfbfb
parent 33296 a3924d1069e5
child 33340 a165b97f3658
permissions -rw-r--r--
moved Nat_Transfer before Divides; distributed Nat_Transfer setup accordingly
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@33318
     6
imports Nat_Numeral Nat_Transfer Divides
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@32069
   179
lemma zero_code_numeral_code [code, code_unfold]:
haftmann@31205
   180
  "(0\<Colon>code_numeral) = Numeral0"
haftmann@31205
   181
  by (simp add: number_of_code_numeral_def Pls_def)
haftmann@31998
   182
lemma [code_post]: "Numeral0 = (0\<Colon>code_numeral)"
haftmann@31205
   183
  using zero_code_numeral_code ..
haftmann@28708
   184
haftmann@32069
   185
lemma one_code_numeral_code [code, code_unfold]:
haftmann@31205
   186
  "(1\<Colon>code_numeral) = Numeral1"
haftmann@31205
   187
  by (simp add: number_of_code_numeral_def Pls_def Bit1_def)
haftmann@31998
   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@31266
   218
lemma code_numeral_zero_minus_one:
haftmann@31266
   219
  "(0::code_numeral) - 1 = 0"
haftmann@31266
   220
  by simp
haftmann@31266
   221
haftmann@31266
   222
lemma Suc_code_numeral_minus_one:
haftmann@31266
   223
  "Suc_code_numeral n - 1 = n"
haftmann@31266
   224
  by simp
haftmann@26140
   225
haftmann@29815
   226
lemma of_nat_code [code]:
haftmann@29815
   227
  "of_nat = Nat.of_nat"
haftmann@25918
   228
proof
haftmann@25918
   229
  fix n :: nat
haftmann@29815
   230
  have "Nat.of_nat n = of_nat n"
haftmann@25918
   231
    by (induct n) simp_all
haftmann@29815
   232
  then show "of_nat n = Nat.of_nat n"
haftmann@25918
   233
    by (rule sym)
haftmann@25918
   234
qed
haftmann@25918
   235
haftmann@31205
   236
lemma code_numeral_not_eq_zero: "i \<noteq> of_nat 0 \<longleftrightarrow> i \<ge> 1"
haftmann@25928
   237
  by (cases i) auto
haftmann@25928
   238
haftmann@31205
   239
definition nat_of_aux :: "code_numeral \<Rightarrow> nat \<Rightarrow> nat" where
haftmann@29815
   240
  "nat_of_aux i n = nat_of i + n"
haftmann@25928
   241
haftmann@29815
   242
lemma nat_of_aux_code [code]:
haftmann@29815
   243
  "nat_of_aux i n = (if i = 0 then n else nat_of_aux (i - 1) (Suc n))"
haftmann@31205
   244
  by (auto simp add: nat_of_aux_def code_numeral_not_eq_zero)
haftmann@25928
   245
haftmann@29815
   246
lemma nat_of_code [code]:
haftmann@29815
   247
  "nat_of i = nat_of_aux i 0"
haftmann@29815
   248
  by (simp add: nat_of_aux_def)
haftmann@25918
   249
haftmann@31205
   250
definition div_mod_code_numeral ::  "code_numeral \<Rightarrow> code_numeral \<Rightarrow> code_numeral \<times> code_numeral" where
haftmann@31205
   251
  [code del]: "div_mod_code_numeral n m = (n div m, n mod m)"
haftmann@26009
   252
haftmann@28562
   253
lemma [code]:
haftmann@31205
   254
  "div_mod_code_numeral n m = (if m = 0 then (0, n) else (n div m, n mod m))"
haftmann@31205
   255
  unfolding div_mod_code_numeral_def by auto
haftmann@26009
   256
haftmann@28562
   257
lemma [code]:
haftmann@31205
   258
  "n div m = fst (div_mod_code_numeral n m)"
haftmann@31205
   259
  unfolding div_mod_code_numeral_def by simp
haftmann@26009
   260
haftmann@28562
   261
lemma [code]:
haftmann@31205
   262
  "n mod m = snd (div_mod_code_numeral n m)"
haftmann@31205
   263
  unfolding div_mod_code_numeral_def by simp
haftmann@26009
   264
haftmann@31205
   265
definition int_of :: "code_numeral \<Rightarrow> int" where
haftmann@31192
   266
  "int_of = Nat.of_nat o nat_of"
haftmann@28708
   267
haftmann@31192
   268
lemma int_of_code [code]:
haftmann@31192
   269
  "int_of k = (if k = 0 then 0
haftmann@31192
   270
    else (if k mod 2 = 0 then 2 * int_of (k div 2) else 2 * int_of (k div 2) + 1))"
haftmann@31192
   271
  by (auto simp add: int_of_def mod_div_equality')
haftmann@28708
   272
haftmann@31192
   273
hide (open) const of_nat nat_of int_of
haftmann@28708
   274
haftmann@28708
   275
haftmann@28228
   276
subsection {* Code generator setup *}
haftmann@24999
   277
haftmann@25767
   278
text {* Implementation of indices by bounded integers *}
haftmann@25767
   279
haftmann@31205
   280
code_type code_numeral
haftmann@24999
   281
  (SML "int")
haftmann@31377
   282
  (OCaml "Big'_int.big'_int")
haftmann@25967
   283
  (Haskell "Int")
haftmann@24999
   284
haftmann@31205
   285
code_instance code_numeral :: eq
haftmann@24999
   286
  (Haskell -)
haftmann@24999
   287
haftmann@24999
   288
setup {*
haftmann@31205
   289
  fold (Numeral.add_code @{const_name number_code_numeral_inst.number_of_code_numeral}
haftmann@31377
   290
    false false) ["SML", "Haskell"]
haftmann@31377
   291
  #> Numeral.add_code @{const_name number_code_numeral_inst.number_of_code_numeral} false true "OCaml"
haftmann@24999
   292
*}
haftmann@24999
   293
haftmann@25918
   294
code_reserved SML Int int
haftmann@31377
   295
code_reserved OCaml Big_int
haftmann@24999
   296
haftmann@31205
   297
code_const "op + \<Colon> code_numeral \<Rightarrow> code_numeral \<Rightarrow> code_numeral"
haftmann@25928
   298
  (SML "Int.+/ ((_),/ (_))")
haftmann@31377
   299
  (OCaml "Big'_int.add'_big'_int")
haftmann@24999
   300
  (Haskell infixl 6 "+")
haftmann@24999
   301
haftmann@31205
   302
code_const "subtract_code_numeral \<Colon> code_numeral \<Rightarrow> code_numeral \<Rightarrow> code_numeral"
haftmann@25918
   303
  (SML "Int.max/ (_/ -/ _,/ 0 : int)")
haftmann@31377
   304
  (OCaml "Big'_int.max'_big'_int/ (Big'_int.sub'_big'_int/ _/ _)/ Big'_int.zero'_big'_int")
haftmann@25918
   305
  (Haskell "max/ (_/ -/ _)/ (0 :: Int)")
haftmann@24999
   306
haftmann@31205
   307
code_const "op * \<Colon> code_numeral \<Rightarrow> code_numeral \<Rightarrow> code_numeral"
haftmann@25928
   308
  (SML "Int.*/ ((_),/ (_))")
haftmann@31377
   309
  (OCaml "Big'_int.mult'_big'_int")
haftmann@24999
   310
  (Haskell infixl 7 "*")
haftmann@24999
   311
haftmann@31205
   312
code_const div_mod_code_numeral
haftmann@29823
   313
  (SML "(fn n => fn m =>/ if m = 0/ then (0, n) else/ (n div m, n mod m))")
haftmann@31377
   314
  (OCaml "(fun k -> fun l ->/ Big'_int.quomod'_big'_int/ (Big'_int.abs'_big'_int k)/ (Big'_int.abs'_big'_int l))")
haftmann@26009
   315
  (Haskell "divMod")
haftmann@25928
   316
haftmann@31205
   317
code_const "eq_class.eq \<Colon> code_numeral \<Rightarrow> code_numeral \<Rightarrow> bool"
haftmann@24999
   318
  (SML "!((_ : Int.int) = _)")
haftmann@31377
   319
  (OCaml "Big'_int.eq'_big'_int")
haftmann@24999
   320
  (Haskell infixl 4 "==")
haftmann@24999
   321
haftmann@31205
   322
code_const "op \<le> \<Colon> code_numeral \<Rightarrow> code_numeral \<Rightarrow> bool"
haftmann@25928
   323
  (SML "Int.<=/ ((_),/ (_))")
haftmann@31377
   324
  (OCaml "Big'_int.le'_big'_int")
haftmann@24999
   325
  (Haskell infix 4 "<=")
haftmann@24999
   326
haftmann@31205
   327
code_const "op < \<Colon> code_numeral \<Rightarrow> code_numeral \<Rightarrow> bool"
haftmann@25928
   328
  (SML "Int.</ ((_),/ (_))")
haftmann@31377
   329
  (OCaml "Big'_int.lt'_big'_int")
haftmann@24999
   330
  (Haskell infix 4 "<")
haftmann@24999
   331
haftmann@24999
   332
end