src/HOL/Code_Numeral.thy
author haftmann
Fri Feb 24 22:46:44 2012 +0100 (2012-02-24)
changeset 46664 1f6c140f9c72
parent 46638 fc315796794e
child 47108 2a1953f0d20d
permissions -rw-r--r--
moved predicate relations and conversion rules between set and predicate relations from Predicate.thy to Relation.thy; moved Predicate.thy upwards in theory hierarchy
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"
wenzelm@45694
    17
  morphisms nat_of of_nat ..
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
huffman@46547
    74
definition Suc where [simp]:
huffman@46547
    75
  "Suc k = of_nat (Nat.Suc (nat_of k))"
haftmann@26140
    76
huffman@46547
    77
rep_datatype "0 \<Colon> code_numeral" Suc
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
huffman@46547
    82
  assume "\<And>k. P k \<Longrightarrow> P (Suc k)"
huffman@46547
    83
    then have "\<And>n. P (of_nat n) \<Longrightarrow> P (Suc (of_nat n))" .
huffman@46547
    84
    then have step: "\<And>n. P (of_nat n) \<Longrightarrow> P (of_nat (Nat.Suc n))" by simp
haftmann@29815
    85
  from init step have "P (of_nat (nat_of k))"
berghofe@34915
    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]:
huffman@46547
    94
  "k \<noteq> of_nat 0 \<Longrightarrow> nat_of k - Nat.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)"
huffman@46547
   102
    by (induct k rule: code_numeral.induct) (simp_all del: zero_code_numeral_def Suc_def, simp_all)
berghofe@34915
   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"
huffman@46547
   112
  by (induct k) (simp_all del: zero_code_numeral_def Suc_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@38857
   118
  "HOL.equal k l \<longleftrightarrow> HOL.equal (nat_of k) (nat_of l)"
haftmann@38857
   119
  by (cases k, cases l) (simp add: equal)
haftmann@24999
   120
haftmann@28351
   121
lemma [code nbe]:
haftmann@38857
   122
  "HOL.equal (k::code_numeral) k \<longleftrightarrow> True"
haftmann@38857
   123
  by (rule equal_refl)
haftmann@28351
   124
haftmann@24999
   125
haftmann@37958
   126
subsection {* Code numerals 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@35028
   147
instantiation code_numeral :: "{minus, linordered_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@33340
   175
qed (auto simp add: code_numeral left_distrib intro: mult_commute)
haftmann@28708
   176
haftmann@28708
   177
end
haftmann@28708
   178
haftmann@46028
   179
lemma zero_code_numeral_code [code]:
haftmann@31205
   180
  "(0\<Colon>code_numeral) = Numeral0"
haftmann@31205
   181
  by (simp add: number_of_code_numeral_def Pls_def)
haftmann@46028
   182
haftmann@46028
   183
lemma [code_abbrev]: "Numeral0 = (0\<Colon>code_numeral)"
haftmann@31205
   184
  using zero_code_numeral_code ..
haftmann@28708
   185
haftmann@46028
   186
lemma one_code_numeral_code [code]:
haftmann@31205
   187
  "(1\<Colon>code_numeral) = Numeral1"
haftmann@31205
   188
  by (simp add: number_of_code_numeral_def Pls_def Bit1_def)
haftmann@46028
   189
haftmann@46028
   190
lemma [code_abbrev]: "Numeral1 = (1\<Colon>code_numeral)"
haftmann@31205
   191
  using one_code_numeral_code ..
haftmann@28708
   192
haftmann@31205
   193
lemma plus_code_numeral_code [code nbe]:
haftmann@29815
   194
  "of_nat n + of_nat m = of_nat (n + m)"
haftmann@28708
   195
  by simp
haftmann@28708
   196
huffman@46547
   197
definition subtract :: "code_numeral \<Rightarrow> code_numeral \<Rightarrow> code_numeral" where
huffman@46547
   198
  [simp]: "subtract = minus"
haftmann@28708
   199
huffman@46547
   200
lemma subtract_code [code nbe]:
huffman@46547
   201
  "subtract (of_nat n) (of_nat m) = of_nat (n - m)"
haftmann@28708
   202
  by simp
haftmann@28708
   203
haftmann@31205
   204
lemma minus_code_numeral_code [code]:
huffman@46547
   205
  "minus = subtract"
haftmann@28708
   206
  by simp
haftmann@28708
   207
haftmann@31205
   208
lemma times_code_numeral_code [code nbe]:
haftmann@29815
   209
  "of_nat n * of_nat m = of_nat (n * m)"
haftmann@28708
   210
  by simp
haftmann@28708
   211
haftmann@31205
   212
lemma less_eq_code_numeral_code [code nbe]:
haftmann@29815
   213
  "of_nat n \<le> of_nat m \<longleftrightarrow> n \<le> m"
haftmann@25767
   214
  by simp
haftmann@24999
   215
haftmann@31205
   216
lemma less_code_numeral_code [code nbe]:
haftmann@29815
   217
  "of_nat n < of_nat m \<longleftrightarrow> n < m"
haftmann@25767
   218
  by simp
haftmann@24999
   219
haftmann@31266
   220
lemma code_numeral_zero_minus_one:
haftmann@31266
   221
  "(0::code_numeral) - 1 = 0"
haftmann@31266
   222
  by simp
haftmann@31266
   223
haftmann@31266
   224
lemma Suc_code_numeral_minus_one:
huffman@46547
   225
  "Suc n - 1 = n"
haftmann@31266
   226
  by simp
haftmann@26140
   227
haftmann@29815
   228
lemma of_nat_code [code]:
haftmann@29815
   229
  "of_nat = Nat.of_nat"
haftmann@25918
   230
proof
haftmann@25918
   231
  fix n :: nat
haftmann@29815
   232
  have "Nat.of_nat n = of_nat n"
haftmann@25918
   233
    by (induct n) simp_all
haftmann@29815
   234
  then show "of_nat n = Nat.of_nat n"
haftmann@25918
   235
    by (rule sym)
haftmann@25918
   236
qed
haftmann@25918
   237
haftmann@31205
   238
lemma code_numeral_not_eq_zero: "i \<noteq> of_nat 0 \<longleftrightarrow> i \<ge> 1"
haftmann@25928
   239
  by (cases i) auto
haftmann@25928
   240
haftmann@31205
   241
definition nat_of_aux :: "code_numeral \<Rightarrow> nat \<Rightarrow> nat" where
haftmann@29815
   242
  "nat_of_aux i n = nat_of i + n"
haftmann@25928
   243
haftmann@29815
   244
lemma nat_of_aux_code [code]:
huffman@46547
   245
  "nat_of_aux i n = (if i = 0 then n else nat_of_aux (i - 1) (Nat.Suc n))"
haftmann@31205
   246
  by (auto simp add: nat_of_aux_def code_numeral_not_eq_zero)
haftmann@25928
   247
haftmann@29815
   248
lemma nat_of_code [code]:
haftmann@29815
   249
  "nat_of i = nat_of_aux i 0"
haftmann@29815
   250
  by (simp add: nat_of_aux_def)
haftmann@25918
   251
huffman@46547
   252
definition div_mod :: "code_numeral \<Rightarrow> code_numeral \<Rightarrow> code_numeral \<times> code_numeral" where
huffman@46547
   253
  [code del]: "div_mod n m = (n div m, n mod m)"
haftmann@26009
   254
haftmann@28562
   255
lemma [code]:
huffman@46547
   256
  "div_mod n m = (if m = 0 then (0, n) else (n div m, n mod m))"
huffman@46547
   257
  unfolding div_mod_def by auto
haftmann@26009
   258
haftmann@28562
   259
lemma [code]:
huffman@46547
   260
  "n div m = fst (div_mod n m)"
huffman@46547
   261
  unfolding div_mod_def by simp
haftmann@26009
   262
haftmann@28562
   263
lemma [code]:
huffman@46547
   264
  "n mod m = snd (div_mod n m)"
huffman@46547
   265
  unfolding div_mod_def by simp
haftmann@26009
   266
haftmann@31205
   267
definition int_of :: "code_numeral \<Rightarrow> int" where
haftmann@31192
   268
  "int_of = Nat.of_nat o nat_of"
haftmann@28708
   269
haftmann@31192
   270
lemma int_of_code [code]:
haftmann@31192
   271
  "int_of k = (if k = 0 then 0
haftmann@31192
   272
    else (if k mod 2 = 0 then 2 * int_of (k div 2) else 2 * int_of (k div 2) + 1))"
haftmann@33340
   273
proof -
haftmann@33340
   274
  have "(nat_of k div 2) * 2 + nat_of k mod 2 = nat_of k" 
haftmann@33340
   275
    by (rule mod_div_equality)
haftmann@33340
   276
  then have "int ((nat_of k div 2) * 2 + nat_of k mod 2) = int (nat_of k)" 
haftmann@33340
   277
    by simp
haftmann@33340
   278
  then have "int (nat_of k) = int (nat_of k div 2) * 2 + int (nat_of k mod 2)" 
huffman@44821
   279
    unfolding of_nat_mult of_nat_add by simp
haftmann@33340
   280
  then show ?thesis by (auto simp add: int_of_def mult_ac)
haftmann@33340
   281
qed
haftmann@28708
   282
haftmann@28708
   283
haftmann@46664
   284
hide_const (open) of_nat nat_of Suc subtract int_of
huffman@46547
   285
haftmann@28708
   286
haftmann@28228
   287
subsection {* Code generator setup *}
haftmann@24999
   288
haftmann@37958
   289
text {* Implementation of code numerals by bounded integers *}
haftmann@25767
   290
haftmann@31205
   291
code_type code_numeral
haftmann@24999
   292
  (SML "int")
haftmann@31377
   293
  (OCaml "Big'_int.big'_int")
haftmann@37947
   294
  (Haskell "Integer")
haftmann@37958
   295
  (Scala "BigInt")
haftmann@24999
   296
haftmann@38857
   297
code_instance code_numeral :: equal
haftmann@24999
   298
  (Haskell -)
haftmann@24999
   299
haftmann@24999
   300
setup {*
haftmann@37958
   301
  Numeral.add_code @{const_name number_code_numeral_inst.number_of_code_numeral}
haftmann@37958
   302
    false Code_Printer.literal_naive_numeral "SML"
haftmann@37947
   303
  #> fold (Numeral.add_code @{const_name number_code_numeral_inst.number_of_code_numeral}
haftmann@37958
   304
    false Code_Printer.literal_numeral) ["OCaml", "Haskell", "Scala"]
haftmann@24999
   305
*}
haftmann@24999
   306
haftmann@25918
   307
code_reserved SML Int int
haftmann@37958
   308
code_reserved Eval Integer
haftmann@24999
   309
huffman@46547
   310
code_const "plus \<Colon> code_numeral \<Rightarrow> code_numeral \<Rightarrow> code_numeral"
haftmann@25928
   311
  (SML "Int.+/ ((_),/ (_))")
haftmann@31377
   312
  (OCaml "Big'_int.add'_big'_int")
haftmann@24999
   313
  (Haskell infixl 6 "+")
haftmann@34886
   314
  (Scala infixl 7 "+")
haftmann@37958
   315
  (Eval infixl 8 "+")
haftmann@24999
   316
huffman@46547
   317
code_const "Code_Numeral.subtract \<Colon> code_numeral \<Rightarrow> code_numeral \<Rightarrow> code_numeral"
haftmann@25918
   318
  (SML "Int.max/ (_/ -/ _,/ 0 : int)")
haftmann@31377
   319
  (OCaml "Big'_int.max'_big'_int/ (Big'_int.sub'_big'_int/ _/ _)/ Big'_int.zero'_big'_int")
haftmann@37947
   320
  (Haskell "max/ (_/ -/ _)/ (0 :: Integer)")
haftmann@34886
   321
  (Scala "!(_/ -/ _).max(0)")
haftmann@37958
   322
  (Eval "Integer.max/ (_/ -/ _)/ 0")
haftmann@24999
   323
huffman@46547
   324
code_const "times \<Colon> code_numeral \<Rightarrow> code_numeral \<Rightarrow> code_numeral"
haftmann@25928
   325
  (SML "Int.*/ ((_),/ (_))")
haftmann@31377
   326
  (OCaml "Big'_int.mult'_big'_int")
haftmann@24999
   327
  (Haskell infixl 7 "*")
haftmann@34886
   328
  (Scala infixl 8 "*")
haftmann@37958
   329
  (Eval infixl 8 "*")
haftmann@24999
   330
huffman@46547
   331
code_const Code_Numeral.div_mod
haftmann@37958
   332
  (SML "!(fn n => fn m =>/ if m = 0/ then (0, n) else/ (Int.div (n, m), Int.mod (n, m)))")
haftmann@34898
   333
  (OCaml "Big'_int.quomod'_big'_int/ (Big'_int.abs'_big'_int _)/ (Big'_int.abs'_big'_int _)")
haftmann@26009
   334
  (Haskell "divMod")
haftmann@37958
   335
  (Scala "!((k: BigInt) => (l: BigInt) =>/ if (l == 0)/ (BigInt(0), k) else/ (k.abs '/% l.abs))")
haftmann@39818
   336
  (Eval "!(fn n => fn m =>/ if m = 0/ then (0, n) else/ (Integer.div'_mod n m))")
haftmann@25928
   337
haftmann@38857
   338
code_const "HOL.equal \<Colon> code_numeral \<Rightarrow> code_numeral \<Rightarrow> bool"
haftmann@24999
   339
  (SML "!((_ : Int.int) = _)")
haftmann@31377
   340
  (OCaml "Big'_int.eq'_big'_int")
haftmann@39272
   341
  (Haskell infix 4 "==")
haftmann@34886
   342
  (Scala infixl 5 "==")
haftmann@37958
   343
  (Eval "!((_ : int) = _)")
haftmann@24999
   344
huffman@46547
   345
code_const "less_eq \<Colon> code_numeral \<Rightarrow> code_numeral \<Rightarrow> bool"
haftmann@25928
   346
  (SML "Int.<=/ ((_),/ (_))")
haftmann@31377
   347
  (OCaml "Big'_int.le'_big'_int")
haftmann@24999
   348
  (Haskell infix 4 "<=")
haftmann@34898
   349
  (Scala infixl 4 "<=")
haftmann@37958
   350
  (Eval infixl 6 "<=")
haftmann@24999
   351
huffman@46547
   352
code_const "less \<Colon> code_numeral \<Rightarrow> code_numeral \<Rightarrow> bool"
haftmann@25928
   353
  (SML "Int.</ ((_),/ (_))")
haftmann@31377
   354
  (OCaml "Big'_int.lt'_big'_int")
haftmann@24999
   355
  (Haskell infix 4 "<")
haftmann@34898
   356
  (Scala infixl 4 "<")
haftmann@37958
   357
  (Eval infixl 6 "<")
haftmann@24999
   358
huffman@46547
   359
code_modulename SML
huffman@46547
   360
  Code_Numeral Arith
huffman@46547
   361
huffman@46547
   362
code_modulename OCaml
huffman@46547
   363
  Code_Numeral Arith
huffman@46547
   364
huffman@46547
   365
code_modulename Haskell
huffman@46547
   366
  Code_Numeral Arith
huffman@46547
   367
haftmann@24999
   368
end
haftmann@46664
   369