src/HOL/Code_Numeral.thy
author haftmann
Fri, 10 Sep 2010 10:21:25 +0200
changeset 39499 0b61951d2682
parent 39086 97775f3e8722
child 40060 ff9e9d5ac171
permissions -rw-r--r--
Haskell == is infix, not infixl
haftmann@29752
     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@33301
     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@29752
    17
  morphisms nat_of of_nat by rule
haftmann@24999
    18
haftmann@29752
    19
lemma of_nat_nat_of [simp]:
haftmann@29752
    20
  "of_nat (nat_of k) = k"
haftmann@29752
    21
  by (rule nat_of_inverse)
haftmann@25967
    22
haftmann@29752
    23
lemma nat_of_of_nat [simp]:
haftmann@29752
    24
  "nat_of (of_nat n) = n"
haftmann@29752
    25
  by (rule of_nat_inverse) (rule UNIV_I)
haftmann@24999
    26
haftmann@28708
    27
lemma [measure_function]:
haftmann@29752
    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@29752
    35
  then show "PROP P (of_nat n)" .
haftmann@24999
    36
next
haftmann@31205
    37
  fix n :: code_numeral
haftmann@29752
    38
  assume "\<And>n\<Colon>nat. PROP P (of_nat n)"
haftmann@29752
    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@29752
    44
  assumes "\<And>n. k = of_nat n \<Longrightarrow> P"
haftmann@26140
    45
  shows P
haftmann@29752
    46
  by (rule assms [of "nat_of k"]) simp
haftmann@26140
    47
haftmann@31205
    48
lemma code_numeral_induct_raw:
haftmann@29752
    49
  assumes "\<And>n. P (of_nat n)"
haftmann@26140
    50
  shows "P k"
haftmann@26140
    51
proof -
haftmann@29752
    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@29752
    56
lemma nat_of_inject [simp]:
haftmann@29752
    57
  "nat_of k = nat_of l \<longleftrightarrow> k = l"
haftmann@29752
    58
  by (rule nat_of_inject)
haftmann@26140
    59
haftmann@29752
    60
lemma of_nat_inject [simp]:
haftmann@29752
    61
  "of_nat n = of_nat m \<longleftrightarrow> n = m"
haftmann@29752
    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@29752
    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@29752
    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@29752
    84
    then have step: "\<And>n. P (of_nat n) \<Longrightarrow> P (of_nat (Suc n))" by simp
haftmann@29752
    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]:
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)
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@29752
   108
  "size = nat_of"
haftmann@26140
   109
proof (rule ext)
haftmann@26140
   110
  fix k
haftmann@29752
   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@39086
   118
  "HOL.equal k l \<longleftrightarrow> HOL.equal (nat_of k) (nat_of l)"
haftmann@39086
   119
  by (cases k, cases l) (simp add: equal)
haftmann@24999
   120
haftmann@28351
   121
lemma [code nbe]:
haftmann@39086
   122
  "HOL.equal (k::code_numeral) k \<longleftrightarrow> True"
haftmann@39086
   123
  by (rule equal_refl)
haftmann@28351
   124
haftmann@24999
   125
haftmann@38195
   126
subsection {* Code numerals as datatype of ints *}
haftmann@24999
   127
haftmann@31205
   128
instantiation code_numeral :: number
haftmann@25767
   129
begin
haftmann@25767
   130
haftmann@25767
   131
definition
haftmann@29752
   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@29752
   138
lemma nat_of_number [simp]:
haftmann@29752
   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@28708
   150
definition [simp, code del]:
haftmann@31205
   151
  "(1\<Colon>code_numeral) = of_nat 1"
haftmann@28708
   152
haftmann@28708
   153
definition [simp, code del]:
haftmann@29752
   154
  "n + m = of_nat (nat_of n + nat_of m)"
haftmann@28708
   155
haftmann@28708
   156
definition [simp, code del]:
haftmann@29752
   157
  "n - m = of_nat (nat_of n - nat_of m)"
haftmann@28708
   158
haftmann@28708
   159
definition [simp, code del]:
haftmann@29752
   160
  "n * m = of_nat (nat_of n * nat_of m)"
haftmann@28708
   161
haftmann@28708
   162
definition [simp, code del]:
haftmann@29752
   163
  "n div m = of_nat (nat_of n div nat_of m)"
haftmann@28708
   164
haftmann@28708
   165
definition [simp, code del]:
haftmann@29752
   166
  "n mod m = of_nat (nat_of n mod nat_of m)"
haftmann@28708
   167
haftmann@28708
   168
definition [simp, code del]:
haftmann@29752
   169
  "n \<le> m \<longleftrightarrow> nat_of n \<le> nat_of m"
haftmann@28708
   170
haftmann@28708
   171
definition [simp, code del]:
haftmann@29752
   172
  "n < m \<longleftrightarrow> nat_of n < nat_of m"
haftmann@28708
   173
haftmann@29752
   174
instance proof
haftmann@33335
   175
qed (auto simp add: code_numeral left_distrib intro: mult_commute)
haftmann@28708
   176
haftmann@28708
   177
end
haftmann@28708
   178
haftmann@32061
   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@25767
   184
haftmann@32061
   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@25767
   190
haftmann@31205
   191
lemma plus_code_numeral_code [code nbe]:
haftmann@29752
   192
  "of_nat n + of_nat m = of_nat (n + m)"
haftmann@24999
   193
  by simp
haftmann@24999
   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@24999
   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@24999
   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@29752
   207
  "of_nat n * of_nat m = of_nat (n * m)"
haftmann@25767
   208
  by simp
haftmann@25335
   209
haftmann@31205
   210
lemma less_eq_code_numeral_code [code nbe]:
haftmann@29752
   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@29752
   215
  "of_nat n < of_nat m \<longleftrightarrow> n < m"
haftmann@25767
   216
  by simp
haftmann@24999
   217
haftmann@31259
   218
lemma code_numeral_zero_minus_one:
haftmann@31259
   219
  "(0::code_numeral) - 1 = 0"
haftmann@31259
   220
  by simp
haftmann@31259
   221
haftmann@31259
   222
lemma Suc_code_numeral_minus_one:
haftmann@31259
   223
  "Suc_code_numeral n - 1 = n"
haftmann@31259
   224
  by simp
haftmann@26140
   225
haftmann@29752
   226
lemma of_nat_code [code]:
haftmann@29752
   227
  "of_nat = Nat.of_nat"
haftmann@25918
   228
proof
haftmann@25918
   229
  fix n :: nat
haftmann@29752
   230
  have "Nat.of_nat n = of_nat n"
haftmann@25918
   231
    by (induct n) simp_all
haftmann@29752
   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@29752
   240
  "nat_of_aux i n = nat_of i + n"
haftmann@25928
   241
haftmann@29752
   242
lemma nat_of_aux_code [code]:
haftmann@29752
   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@29752
   246
lemma nat_of_code [code]:
haftmann@29752
   247
  "nat_of i = nat_of_aux i 0"
haftmann@29752
   248
  by (simp add: nat_of_aux_def)
haftmann@25918
   249
haftmann@35687
   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@26009
   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@33335
   271
proof -
haftmann@33335
   272
  have "(nat_of k div 2) * 2 + nat_of k mod 2 = nat_of k" 
haftmann@33335
   273
    by (rule mod_div_equality)
haftmann@33335
   274
  then have "int ((nat_of k div 2) * 2 + nat_of k mod 2) = int (nat_of k)" 
haftmann@33335
   275
    by simp
haftmann@33335
   276
  then have "int (nat_of k) = int (nat_of k div 2) * 2 + int (nat_of k mod 2)" 
haftmann@33335
   277
    unfolding int_mult zadd_int [symmetric] by simp
haftmann@33335
   278
  then show ?thesis by (auto simp add: int_of_def mult_ac)
haftmann@33335
   279
qed
haftmann@28708
   280
wenzelm@36176
   281
hide_const (open) of_nat nat_of int_of
haftmann@28708
   282
bulwahn@36049
   283
subsubsection {* Lazy Evaluation of an indexed function *}
bulwahn@36049
   284
bulwahn@36049
   285
function iterate_upto :: "(code_numeral => 'a) => code_numeral => code_numeral => 'a Predicate.pred"
bulwahn@36049
   286
where
bulwahn@36049
   287
  "iterate_upto f n m = Predicate.Seq (%u. if n > m then Predicate.Empty else Predicate.Insert (f n) (iterate_upto f (n + 1) m))"
bulwahn@36049
   288
by pat_completeness auto
bulwahn@36049
   289
bulwahn@36049
   290
termination by (relation "measure (%(f, n, m). Code_Numeral.nat_of (m + 1 - n))") auto
bulwahn@36049
   291
wenzelm@36176
   292
hide_const (open) iterate_upto
haftmann@28708
   293
haftmann@28228
   294
subsection {* Code generator setup *}
haftmann@24999
   295
haftmann@38195
   296
text {* Implementation of code numerals by bounded integers *}
haftmann@25767
   297
haftmann@31205
   298
code_type code_numeral
haftmann@24999
   299
  (SML "int")
haftmann@31377
   300
  (OCaml "Big'_int.big'_int")
haftmann@38185
   301
  (Haskell "Integer")
haftmann@38195
   302
  (Scala "BigInt")
haftmann@24999
   303
haftmann@39086
   304
code_instance code_numeral :: equal
haftmann@24999
   305
  (Haskell -)
haftmann@24999
   306
haftmann@24999
   307
setup {*
haftmann@38195
   308
  Numeral.add_code @{const_name number_code_numeral_inst.number_of_code_numeral}
haftmann@38195
   309
    false Code_Printer.literal_naive_numeral "SML"
haftmann@38185
   310
  #> fold (Numeral.add_code @{const_name number_code_numeral_inst.number_of_code_numeral}
haftmann@38195
   311
    false Code_Printer.literal_numeral) ["OCaml", "Haskell", "Scala"]
haftmann@24999
   312
*}
haftmann@24999
   313
haftmann@25918
   314
code_reserved SML Int int
haftmann@38195
   315
code_reserved Eval Integer
haftmann@24999
   316
haftmann@31205
   317
code_const "op + \<Colon> code_numeral \<Rightarrow> code_numeral \<Rightarrow> code_numeral"
haftmann@25928
   318
  (SML "Int.+/ ((_),/ (_))")
haftmann@31377
   319
  (OCaml "Big'_int.add'_big'_int")
haftmann@24999
   320
  (Haskell infixl 6 "+")
haftmann@34886
   321
  (Scala infixl 7 "+")
haftmann@38195
   322
  (Eval infixl 8 "+")
haftmann@24999
   323
haftmann@31205
   324
code_const "subtract_code_numeral \<Colon> code_numeral \<Rightarrow> code_numeral \<Rightarrow> code_numeral"
haftmann@25918
   325
  (SML "Int.max/ (_/ -/ _,/ 0 : int)")
haftmann@31377
   326
  (OCaml "Big'_int.max'_big'_int/ (Big'_int.sub'_big'_int/ _/ _)/ Big'_int.zero'_big'_int")
haftmann@38185
   327
  (Haskell "max/ (_/ -/ _)/ (0 :: Integer)")
haftmann@34886
   328
  (Scala "!(_/ -/ _).max(0)")
haftmann@38195
   329
  (Eval "Integer.max/ (_/ -/ _)/ 0")
haftmann@24999
   330
haftmann@31205
   331
code_const "op * \<Colon> code_numeral \<Rightarrow> code_numeral \<Rightarrow> code_numeral"
haftmann@25928
   332
  (SML "Int.*/ ((_),/ (_))")
haftmann@31377
   333
  (OCaml "Big'_int.mult'_big'_int")
haftmann@24999
   334
  (Haskell infixl 7 "*")
haftmann@34886
   335
  (Scala infixl 8 "*")
haftmann@38195
   336
  (Eval infixl 8 "*")
haftmann@24999
   337
haftmann@31205
   338
code_const div_mod_code_numeral
haftmann@38195
   339
  (SML "!(fn n => fn m =>/ if m = 0/ then (0, n) else/ (Int.div (n, m), Int.mod (n, m)))")
haftmann@34898
   340
  (OCaml "Big'_int.quomod'_big'_int/ (Big'_int.abs'_big'_int _)/ (Big'_int.abs'_big'_int _)")
haftmann@26009
   341
  (Haskell "divMod")
haftmann@38195
   342
  (Scala "!((k: BigInt) => (l: BigInt) =>/ if (l == 0)/ (BigInt(0), k) else/ (k.abs '/% l.abs))")
haftmann@38195
   343
  (Eval "!(fn n => fn m =>/ if m = 0/ then (0, n) else/ (n div m, n mod m))")
haftmann@25928
   344
haftmann@39086
   345
code_const "HOL.equal \<Colon> code_numeral \<Rightarrow> code_numeral \<Rightarrow> bool"
haftmann@24999
   346
  (SML "!((_ : Int.int) = _)")
haftmann@31377
   347
  (OCaml "Big'_int.eq'_big'_int")
haftmann@39499
   348
  (Haskell infix 4 "==")
haftmann@34886
   349
  (Scala infixl 5 "==")
haftmann@38195
   350
  (Eval "!((_ : int) = _)")
haftmann@24999
   351
haftmann@31205
   352
code_const "op \<le> \<Colon> code_numeral \<Rightarrow> code_numeral \<Rightarrow> bool"
haftmann@25928
   353
  (SML "Int.<=/ ((_),/ (_))")
haftmann@31377
   354
  (OCaml "Big'_int.le'_big'_int")
haftmann@24999
   355
  (Haskell infix 4 "<=")
haftmann@34898
   356
  (Scala infixl 4 "<=")
haftmann@38195
   357
  (Eval infixl 6 "<=")
haftmann@24999
   358
haftmann@31205
   359
code_const "op < \<Colon> code_numeral \<Rightarrow> code_numeral \<Rightarrow> bool"
haftmann@25928
   360
  (SML "Int.</ ((_),/ (_))")
haftmann@31377
   361
  (OCaml "Big'_int.lt'_big'_int")
haftmann@24999
   362
  (Haskell infix 4 "<")
haftmann@34898
   363
  (Scala infixl 4 "<")
haftmann@38195
   364
  (Eval infixl 6 "<")
haftmann@24999
   365
haftmann@24999
   366
end