src/HOL/Library/Efficient_Nat.thy
author haftmann
Tue Jan 15 16:19:23 2008 +0100 (2008-01-15)
changeset 25919 8b1c0d434824
parent 25885 6fbc3f54f819
child 25931 b1d1ab3e5a2e
permissions -rw-r--r--
joined theories IntDef, Numeral, IntArith to theory Int
haftmann@23854
     1
(*  Title:      HOL/Library/Efficient_Nat.thy
haftmann@23854
     2
    ID:         $Id$
haftmann@23854
     3
    Author:     Stefan Berghofer, TU Muenchen
haftmann@23854
     4
*)
haftmann@23854
     5
haftmann@23854
     6
header {* Implementation of natural numbers by integers *}
haftmann@23854
     7
haftmann@23854
     8
theory Efficient_Nat
haftmann@25919
     9
imports Main Code_Integer Code_Index
haftmann@23854
    10
begin
haftmann@23854
    11
haftmann@23854
    12
text {*
haftmann@23854
    13
When generating code for functions on natural numbers, the canonical
haftmann@23854
    14
representation using @{term "0::nat"} and @{term "Suc"} is unsuitable for
haftmann@23854
    15
computations involving large numbers. The efficiency of the generated
haftmann@23854
    16
code can be improved drastically by implementing natural numbers by
haftmann@23854
    17
integers. To do this, just include this theory.
haftmann@23854
    18
*}
haftmann@23854
    19
haftmann@23854
    20
subsection {* Logical rewrites *}
haftmann@23854
    21
haftmann@23854
    22
text {*
haftmann@23854
    23
  An int-to-nat conversion
haftmann@23854
    24
  restricted to non-negative ints (in contrast to @{const nat}).
haftmann@23854
    25
  Note that this restriction has no logical relevance and
haftmann@23854
    26
  is just a kind of proof hint -- nothing prevents you from 
haftmann@23854
    27
  writing nonsense like @{term "nat_of_int (-4)"}
haftmann@23854
    28
*}
haftmann@23854
    29
haftmann@23854
    30
definition
haftmann@23854
    31
  nat_of_int :: "int \<Rightarrow> nat" where
haftmann@23854
    32
  "k \<ge> 0 \<Longrightarrow> nat_of_int k = nat k"
haftmann@23854
    33
haftmann@23854
    34
definition
haftmann@24715
    35
  int_of_nat :: "nat \<Rightarrow> int" where
haftmann@25488
    36
  [code func del]: "int_of_nat n = of_nat n"
haftmann@23854
    37
haftmann@24715
    38
lemma int_of_nat_Suc [simp]:
haftmann@24715
    39
  "int_of_nat (Suc n) = 1 + int_of_nat n"
haftmann@24715
    40
  unfolding int_of_nat_def by simp
haftmann@23854
    41
haftmann@24715
    42
lemma int_of_nat_add:
haftmann@24715
    43
  "int_of_nat (m + n) = int_of_nat m + int_of_nat n"
haftmann@24715
    44
  unfolding int_of_nat_def by (rule of_nat_add)
haftmann@23854
    45
haftmann@24715
    46
lemma int_of_nat_mult:
haftmann@24715
    47
  "int_of_nat (m * n) = int_of_nat m * int_of_nat n"
haftmann@24715
    48
  unfolding int_of_nat_def by (rule of_nat_mult)
haftmann@23854
    49
haftmann@23854
    50
lemma nat_of_int_of_number_of:
haftmann@23854
    51
  fixes k
haftmann@23854
    52
  assumes "k \<ge> 0"
haftmann@23854
    53
  shows "number_of k = nat_of_int (number_of k)"
haftmann@23854
    54
  unfolding nat_of_int_def [OF assms] nat_number_of_def number_of_is_id ..
haftmann@23854
    55
haftmann@23854
    56
lemma nat_of_int_of_number_of_aux:
haftmann@23854
    57
  fixes k
haftmann@25919
    58
  assumes "Int.Pls \<le> k \<equiv> True"
haftmann@23854
    59
  shows "k \<ge> 0"
haftmann@23854
    60
  using assms unfolding Pls_def by simp
haftmann@23854
    61
haftmann@23854
    62
lemma nat_of_int_int:
haftmann@24715
    63
  "nat_of_int (int_of_nat n) = n"
haftmann@24715
    64
  using nat_of_int_def int_of_nat_def by simp
haftmann@23854
    65
haftmann@24715
    66
lemma eq_nat_of_int: "int_of_nat n = x \<Longrightarrow> n = nat_of_int x"
haftmann@23854
    67
by (erule subst, simp only: nat_of_int_int)
haftmann@23854
    68
haftmann@24423
    69
code_datatype nat_of_int
haftmann@24423
    70
haftmann@23854
    71
text {*
haftmann@23854
    72
  Case analysis on natural numbers is rephrased using a conditional
haftmann@23854
    73
  expression:
haftmann@23854
    74
*}
haftmann@23854
    75
haftmann@23854
    76
lemma [code unfold, code inline del]:
haftmann@25615
    77
  "nat_case = (\<lambda>f g n. if n = 0 then f else g (n - 1))"
haftmann@23854
    78
proof -
haftmann@23854
    79
  have rewrite: "\<And>f g n. nat_case f g n = (if n = 0 then f else g (n - 1))"
haftmann@23854
    80
  proof -
haftmann@23854
    81
    fix f g n
haftmann@23854
    82
    show "nat_case f g n = (if n = 0 then f else g (n - 1))"
haftmann@23854
    83
      by (cases n) simp_all
haftmann@23854
    84
  qed
haftmann@25615
    85
  show "nat_case = (\<lambda>f g n. if n = 0 then f else g (n - 1))"
haftmann@23854
    86
    by (rule eq_reflection ext rewrite)+ 
haftmann@23854
    87
qed
haftmann@23854
    88
haftmann@23854
    89
lemma [code inline]:
haftmann@24715
    90
  "nat_case = (\<lambda>f g n. if n = 0 then f else g (nat_of_int (int_of_nat n - 1)))"
haftmann@23854
    91
proof (rule ext)+
haftmann@23854
    92
  fix f g n
haftmann@24715
    93
  show "nat_case f g n = (if n = 0 then f else g (nat_of_int (int_of_nat n - 1)))"
haftmann@23854
    94
  by (cases n) (simp_all add: nat_of_int_int)
haftmann@23854
    95
qed
haftmann@23854
    96
haftmann@23854
    97
text {*
haftmann@23854
    98
  Most standard arithmetic functions on natural numbers are implemented
haftmann@23854
    99
  using their counterparts on the integers:
haftmann@23854
   100
*}
haftmann@23854
   101
haftmann@23854
   102
lemma [code func]: "0 = nat_of_int 0"
haftmann@23854
   103
  by (simp add: nat_of_int_def)
haftmann@24715
   104
haftmann@23854
   105
lemma [code func, code inline]:  "1 = nat_of_int 1"
haftmann@23854
   106
  by (simp add: nat_of_int_def)
haftmann@24715
   107
haftmann@24715
   108
lemma [code func]: "Suc n = nat_of_int (int_of_nat n + 1)"
haftmann@23854
   109
  by (simp add: eq_nat_of_int)
haftmann@24715
   110
haftmann@24715
   111
lemma [code]: "m + n = nat (int_of_nat m + int_of_nat n)"
haftmann@24715
   112
  by (simp add: int_of_nat_def nat_eq_iff2)
haftmann@24715
   113
haftmann@24715
   114
lemma [code func, code inline]: "m + n = nat_of_int (int_of_nat m + int_of_nat n)"
haftmann@24715
   115
  by (simp add: eq_nat_of_int int_of_nat_add)
haftmann@24715
   116
haftmann@24715
   117
lemma [code, code inline]: "m - n = nat (int_of_nat m - int_of_nat n)"
haftmann@24715
   118
  by (simp add: int_of_nat_def nat_eq_iff2 of_nat_diff)
haftmann@24715
   119
haftmann@24715
   120
lemma [code]: "m * n = nat (int_of_nat m * int_of_nat n)"
haftmann@24715
   121
  unfolding int_of_nat_def
haftmann@23854
   122
  by (simp add: of_nat_mult [symmetric] del: of_nat_mult)
haftmann@24715
   123
haftmann@24715
   124
lemma [code func, code inline]: "m * n = nat_of_int (int_of_nat m * int_of_nat n)"
haftmann@24715
   125
  by (simp add: eq_nat_of_int int_of_nat_mult)
haftmann@24715
   126
haftmann@24715
   127
lemma [code]: "m div n = nat (int_of_nat m div int_of_nat n)"
haftmann@24715
   128
  unfolding int_of_nat_def zdiv_int [symmetric] by simp
haftmann@24715
   129
haftmann@24715
   130
lemma div_nat_code [code func]:
haftmann@24715
   131
  "m div k = nat_of_int (fst (divAlg (int_of_nat m, int_of_nat k)))"
haftmann@24715
   132
  unfolding div_def [symmetric] int_of_nat_def zdiv_int [symmetric]
haftmann@24715
   133
  unfolding int_of_nat_def [symmetric] nat_of_int_int ..
haftmann@24715
   134
haftmann@24715
   135
lemma [code]: "m mod n = nat (int_of_nat m mod int_of_nat n)"
haftmann@24715
   136
  unfolding int_of_nat_def zmod_int [symmetric] by simp
haftmann@24715
   137
haftmann@24715
   138
lemma mod_nat_code [code func]:
haftmann@24715
   139
  "m mod k = nat_of_int (snd (divAlg (int_of_nat m, int_of_nat k)))"
haftmann@24715
   140
  unfolding mod_def [symmetric] int_of_nat_def zmod_int [symmetric]
haftmann@24715
   141
  unfolding int_of_nat_def [symmetric] nat_of_int_int ..
haftmann@24715
   142
haftmann@25615
   143
lemma [code, code inline]: "m < n \<longleftrightarrow> int_of_nat m < int_of_nat n"
haftmann@24715
   144
  unfolding int_of_nat_def by simp
haftmann@24715
   145
haftmann@25615
   146
lemma [code func, code inline]: "m \<le> n \<longleftrightarrow> int_of_nat m \<le> int_of_nat n"
haftmann@24715
   147
  unfolding int_of_nat_def by simp
haftmann@24715
   148
haftmann@24715
   149
lemma [code func, code inline]: "m = n \<longleftrightarrow> int_of_nat m = int_of_nat n"
haftmann@24715
   150
  unfolding int_of_nat_def by simp
haftmann@24715
   151
haftmann@23854
   152
lemma [code func]: "nat k = (if k < 0 then 0 else nat_of_int k)"
haftmann@24715
   153
  by (cases "k < 0") (simp, simp add: nat_of_int_def)
haftmann@24715
   154
haftmann@23854
   155
lemma [code func]:
haftmann@24715
   156
  "int_aux n i = (if int_of_nat n = 0 then i else int_aux (nat_of_int (int_of_nat n - 1)) (i + 1))"
haftmann@23854
   157
proof -
haftmann@24715
   158
  have "0 < n \<Longrightarrow> int_of_nat n = 1 + int_of_nat (nat_of_int (int_of_nat n - 1))"
haftmann@23854
   159
  proof -
haftmann@23854
   160
    assume prem: "n > 0"
haftmann@24715
   161
    then have "int_of_nat n - 1 \<ge> 0" unfolding int_of_nat_def by auto
haftmann@24715
   162
    then have "nat_of_int (int_of_nat n - 1) = nat (int_of_nat n - 1)" by (simp add: nat_of_int_def)
haftmann@24715
   163
    with prem show "int_of_nat n = 1 + int_of_nat (nat_of_int (int_of_nat n - 1))" unfolding int_of_nat_def by simp
haftmann@23854
   164
  qed
haftmann@24715
   165
  then show ?thesis unfolding int_aux_def int_of_nat_def by auto
haftmann@23854
   166
qed
haftmann@23854
   167
haftmann@23854
   168
haftmann@23854
   169
subsection {* Code generator setup for basic functions *}
haftmann@23854
   170
haftmann@23854
   171
text {*
haftmann@23854
   172
  @{typ nat} is no longer a datatype but embedded into the integers.
haftmann@23854
   173
*}
haftmann@23854
   174
haftmann@23854
   175
code_type nat
wenzelm@24630
   176
  (SML "int")
haftmann@23854
   177
  (OCaml "Big'_int.big'_int")
haftmann@23854
   178
  (Haskell "Integer")
haftmann@23854
   179
haftmann@23854
   180
types_code
haftmann@23854
   181
  nat ("int")
haftmann@23854
   182
attach (term_of) {*
wenzelm@24630
   183
val term_of_nat = HOLogic.mk_number HOLogic.natT;
haftmann@23854
   184
*}
haftmann@23854
   185
attach (test) {*
berghofe@25885
   186
fun gen_nat i =
berghofe@25885
   187
  let val n = random_range 0 i
berghofe@25885
   188
  in (n, fn () => term_of_nat n) end;
haftmann@23854
   189
*}
haftmann@23854
   190
haftmann@23854
   191
consts_code
haftmann@23854
   192
  "0 \<Colon> nat" ("0")
haftmann@23854
   193
  Suc ("(_ + 1)")
haftmann@23854
   194
haftmann@23854
   195
text {*
haftmann@23854
   196
  Since natural numbers are implemented
haftmann@23854
   197
  using integers, the coercion function @{const "int"} of type
haftmann@23854
   198
  @{typ "nat \<Rightarrow> int"} is simply implemented by the identity function,
haftmann@23854
   199
  likewise @{const nat_of_int} of type @{typ "int \<Rightarrow> nat"}.
haftmann@23854
   200
  For the @{const "nat"} function for converting an integer to a natural
haftmann@23854
   201
  number, we give a specific implementation using an ML function that
haftmann@23854
   202
  returns its input value, provided that it is non-negative, and otherwise
haftmann@23854
   203
  returns @{text "0"}.
haftmann@23854
   204
*}
haftmann@23854
   205
haftmann@23854
   206
consts_code
haftmann@24715
   207
  int_of_nat ("(_)")
haftmann@23854
   208
  nat ("\<module>nat")
haftmann@23854
   209
attach {*
haftmann@23854
   210
fun nat i = if i < 0 then 0 else i;
haftmann@23854
   211
*}
haftmann@23854
   212
haftmann@24715
   213
code_const int_of_nat
haftmann@23854
   214
  (SML "_")
haftmann@23854
   215
  (OCaml "_")
haftmann@23854
   216
  (Haskell "_")
haftmann@23854
   217
haftmann@25767
   218
code_const index_of_nat
haftmann@25767
   219
  (SML "_")
haftmann@25767
   220
  (OCaml "_")
haftmann@25767
   221
  (Haskell "_")
haftmann@25767
   222
haftmann@23854
   223
code_const nat_of_int
haftmann@23854
   224
  (SML "_")
haftmann@23854
   225
  (OCaml "_")
haftmann@23854
   226
  (Haskell "_")
haftmann@23854
   227
haftmann@25767
   228
code_const nat_of_index
haftmann@25767
   229
  (SML "_")
haftmann@25767
   230
  (OCaml "_")
haftmann@25767
   231
  (Haskell "_")
haftmann@25767
   232
haftmann@25767
   233
haftmann@25615
   234
text {* Using target language div and mod *}
haftmann@25615
   235
haftmann@25615
   236
code_const "op div \<Colon> nat \<Rightarrow> nat \<Rightarrow> nat"
haftmann@25615
   237
  (SML "IntInf.div ((_), (_))")
haftmann@25615
   238
  (OCaml "Big'_int.div'_big'_int")
haftmann@25615
   239
  (Haskell "div")
haftmann@25615
   240
haftmann@25615
   241
code_const "op mod \<Colon> nat \<Rightarrow> nat \<Rightarrow> nat"
haftmann@25615
   242
  (SML "IntInf.mod ((_), (_))")
haftmann@25615
   243
  (OCaml "Big'_int.mod'_big'_int")
haftmann@25615
   244
  (Haskell "mod")
haftmann@25615
   245
haftmann@23854
   246
haftmann@23854
   247
subsection {* Preprocessors *}
haftmann@23854
   248
haftmann@23854
   249
text {*
haftmann@23854
   250
  Natural numerals should be expressed using @{const nat_of_int}.
haftmann@23854
   251
*}
haftmann@23854
   252
haftmann@23854
   253
lemmas [code inline del] = nat_number_of_def
haftmann@23854
   254
haftmann@23854
   255
ML {*
haftmann@23854
   256
fun nat_of_int_of_number_of thy cts =
haftmann@23854
   257
  let
haftmann@23854
   258
    val simplify_less = Simplifier.rewrite 
haftmann@23854
   259
      (HOL_basic_ss addsimps (@{thms less_numeral_code} @ @{thms less_eq_numeral_code}));
haftmann@23854
   260
    fun mk_rew (t, ty) =
wenzelm@24630
   261
      if ty = HOLogic.natT andalso 0 <= HOLogic.dest_numeral t then
haftmann@25919
   262
        Thm.capply @{cterm "(op \<le>) Int.Pls"} (Thm.cterm_of thy t)
haftmann@23854
   263
        |> simplify_less
haftmann@23854
   264
        |> (fn thm => @{thm nat_of_int_of_number_of_aux} OF [thm])
haftmann@23854
   265
        |> (fn thm => @{thm nat_of_int_of_number_of} OF [thm])
haftmann@23854
   266
        |> (fn thm => @{thm eq_reflection} OF [thm])
haftmann@23854
   267
        |> SOME
haftmann@23854
   268
      else NONE
haftmann@23854
   269
  in
haftmann@23854
   270
    fold (HOLogic.add_numerals o Thm.term_of) cts []
haftmann@23854
   271
    |> map_filter mk_rew
haftmann@23854
   272
  end;
haftmann@23854
   273
*}
haftmann@23854
   274
haftmann@23854
   275
setup {*
haftmann@24222
   276
  Code.add_inline_proc ("nat_of_int_of_number_of", nat_of_int_of_number_of)
haftmann@23854
   277
*}
haftmann@23854
   278
haftmann@23854
   279
text {*
haftmann@23854
   280
  In contrast to @{term "Suc n"}, the term @{term "n + (1::nat)"} is no longer
haftmann@23854
   281
  a constructor term. Therefore, all occurrences of this term in a position
haftmann@23854
   282
  where a pattern is expected (i.e.\ on the left-hand side of a recursion
haftmann@23854
   283
  equation or in the arguments of an inductive relation in an introduction
haftmann@23854
   284
  rule) must be eliminated.
haftmann@23854
   285
  This can be accomplished by applying the following transformation rules:
haftmann@23854
   286
*}
haftmann@23854
   287
haftmann@23854
   288
theorem Suc_if_eq: "(\<And>n. f (Suc n) = h n) \<Longrightarrow> f 0 = g \<Longrightarrow>
haftmann@23854
   289
  f n = (if n = 0 then g else h (n - 1))"
haftmann@23854
   290
  by (case_tac n) simp_all
haftmann@23854
   291
haftmann@23854
   292
theorem Suc_clause: "(\<And>n. P n (Suc n)) \<Longrightarrow> n \<noteq> 0 \<Longrightarrow> P (n - 1) n"
haftmann@23854
   293
  by (case_tac n) simp_all
haftmann@23854
   294
haftmann@23854
   295
text {*
haftmann@23854
   296
  The rules above are built into a preprocessor that is plugged into
haftmann@23854
   297
  the code generator. Since the preprocessor for introduction rules
haftmann@23854
   298
  does not know anything about modes, some of the modes that worked
haftmann@23854
   299
  for the canonical representation of natural numbers may no longer work.
haftmann@23854
   300
*}
haftmann@23854
   301
haftmann@23854
   302
(*<*)
haftmann@23854
   303
haftmann@23854
   304
ML {*
haftmann@23854
   305
fun remove_suc thy thms =
haftmann@23854
   306
  let
haftmann@23854
   307
    val vname = Name.variant (map fst
haftmann@23854
   308
      (fold (Term.add_varnames o Thm.full_prop_of) thms [])) "x";
haftmann@23854
   309
    val cv = cterm_of thy (Var ((vname, 0), HOLogic.natT));
haftmann@23854
   310
    fun lhs_of th = snd (Thm.dest_comb
haftmann@23854
   311
      (fst (Thm.dest_comb (snd (Thm.dest_comb (cprop_of th))))));
haftmann@23854
   312
    fun rhs_of th = snd (Thm.dest_comb (snd (Thm.dest_comb (cprop_of th))));
haftmann@23854
   313
    fun find_vars ct = (case term_of ct of
haftmann@23854
   314
        (Const ("Suc", _) $ Var _) => [(cv, snd (Thm.dest_comb ct))]
haftmann@23854
   315
      | _ $ _ =>
haftmann@23854
   316
        let val (ct1, ct2) = Thm.dest_comb ct
haftmann@23854
   317
        in 
haftmann@23854
   318
          map (apfst (fn ct => Thm.capply ct ct2)) (find_vars ct1) @
haftmann@23854
   319
          map (apfst (Thm.capply ct1)) (find_vars ct2)
haftmann@23854
   320
        end
haftmann@23854
   321
      | _ => []);
haftmann@23854
   322
    val eqs = maps
haftmann@23854
   323
      (fn th => map (pair th) (find_vars (lhs_of th))) thms;
haftmann@23854
   324
    fun mk_thms (th, (ct, cv')) =
haftmann@23854
   325
      let
haftmann@23854
   326
        val th' =
haftmann@23854
   327
          Thm.implies_elim
haftmann@23854
   328
           (Conv.fconv_rule (Thm.beta_conversion true)
haftmann@23854
   329
             (Drule.instantiate'
haftmann@23854
   330
               [SOME (ctyp_of_term ct)] [SOME (Thm.cabs cv ct),
haftmann@23854
   331
                 SOME (Thm.cabs cv' (rhs_of th)), NONE, SOME cv']
haftmann@24222
   332
               @{thm Suc_if_eq})) (Thm.forall_intr cv' th)
haftmann@23854
   333
      in
haftmann@23854
   334
        case map_filter (fn th'' =>
haftmann@23854
   335
            SOME (th'', singleton
haftmann@23854
   336
              (Variable.trade (K (fn [th'''] => [th''' RS th'])) (Variable.thm_context th'')) th'')
haftmann@23854
   337
          handle THM _ => NONE) thms of
haftmann@23854
   338
            [] => NONE
haftmann@23854
   339
          | thps =>
haftmann@23854
   340
              let val (ths1, ths2) = split_list thps
haftmann@23854
   341
              in SOME (subtract Thm.eq_thm (th :: ths1) thms @ ths2) end
haftmann@23854
   342
      end
haftmann@23854
   343
  in
haftmann@23854
   344
    case get_first mk_thms eqs of
haftmann@23854
   345
      NONE => thms
haftmann@23854
   346
    | SOME x => remove_suc thy x
haftmann@23854
   347
  end;
haftmann@23854
   348
haftmann@23854
   349
fun eqn_suc_preproc thy ths =
haftmann@23854
   350
  let
haftmann@24222
   351
    val dest = fst o HOLogic.dest_eq o HOLogic.dest_Trueprop o prop_of;
haftmann@24222
   352
    fun contains_suc t = member (op =) (term_consts t) @{const_name Suc};
haftmann@23854
   353
  in
haftmann@23854
   354
    if forall (can dest) ths andalso
haftmann@23854
   355
      exists (contains_suc o dest) ths
haftmann@23854
   356
    then remove_suc thy ths else ths
haftmann@23854
   357
  end;
haftmann@23854
   358
haftmann@23854
   359
fun remove_suc_clause thy thms =
haftmann@23854
   360
  let
haftmann@23854
   361
    val vname = Name.variant (map fst
haftmann@23854
   362
      (fold (Term.add_varnames o Thm.full_prop_of) thms [])) "x";
haftmann@24222
   363
    fun find_var (t as Const (@{const_name Suc}, _) $ (v as Var _)) = SOME (t, v)
haftmann@23854
   364
      | find_var (t $ u) = (case find_var t of NONE => find_var u | x => x)
haftmann@23854
   365
      | find_var _ = NONE;
haftmann@23854
   366
    fun find_thm th =
haftmann@23854
   367
      let val th' = Conv.fconv_rule ObjectLogic.atomize th
haftmann@23854
   368
      in Option.map (pair (th, th')) (find_var (prop_of th')) end
haftmann@23854
   369
  in
haftmann@23854
   370
    case get_first find_thm thms of
haftmann@23854
   371
      NONE => thms
haftmann@23854
   372
    | SOME ((th, th'), (Sucv, v)) =>
haftmann@23854
   373
        let
haftmann@23854
   374
          val cert = cterm_of (Thm.theory_of_thm th);
haftmann@23854
   375
          val th'' = ObjectLogic.rulify (Thm.implies_elim
haftmann@23854
   376
            (Conv.fconv_rule (Thm.beta_conversion true)
haftmann@23854
   377
              (Drule.instantiate' []
haftmann@23854
   378
                [SOME (cert (lambda v (Abs ("x", HOLogic.natT,
haftmann@23854
   379
                   abstract_over (Sucv,
haftmann@23854
   380
                     HOLogic.dest_Trueprop (prop_of th')))))),
haftmann@24222
   381
                 SOME (cert v)] @{thm Suc_clause}))
haftmann@23854
   382
            (Thm.forall_intr (cert v) th'))
haftmann@23854
   383
        in
haftmann@23854
   384
          remove_suc_clause thy (map (fn th''' =>
haftmann@23854
   385
            if (op = o pairself prop_of) (th''', th) then th'' else th''') thms)
haftmann@23854
   386
        end
haftmann@23854
   387
  end;
haftmann@23854
   388
haftmann@23854
   389
fun clause_suc_preproc thy ths =
haftmann@23854
   390
  let
haftmann@23854
   391
    val dest = fst o HOLogic.dest_mem o HOLogic.dest_Trueprop
haftmann@23854
   392
  in
haftmann@23854
   393
    if forall (can (dest o concl_of)) ths andalso
haftmann@23854
   394
      exists (fn th => member (op =) (foldr add_term_consts
haftmann@23854
   395
        [] (map_filter (try dest) (concl_of th :: prems_of th))) "Suc") ths
haftmann@23854
   396
    then remove_suc_clause thy ths else ths
haftmann@23854
   397
  end;
haftmann@23854
   398
haftmann@23854
   399
fun lift_obj_eq f thy =
haftmann@23854
   400
  map (fn thm => thm RS @{thm meta_eq_to_obj_eq})
haftmann@23854
   401
  #> f thy
haftmann@23854
   402
  #> map (fn thm => thm RS @{thm eq_reflection})
haftmann@23854
   403
  #> map (Conv.fconv_rule Drule.beta_eta_conversion)
haftmann@23854
   404
*}
haftmann@23854
   405
haftmann@23854
   406
setup {*
haftmann@23854
   407
  Codegen.add_preprocessor eqn_suc_preproc
haftmann@23854
   408
  #> Codegen.add_preprocessor clause_suc_preproc
haftmann@24222
   409
  #> Code.add_preproc ("eqn_Suc", lift_obj_eq eqn_suc_preproc)
haftmann@24222
   410
  #> Code.add_preproc ("clause_Suc", lift_obj_eq clause_suc_preproc)
haftmann@23854
   411
*}
haftmann@23854
   412
(*>*)
haftmann@23854
   413
haftmann@23854
   414
haftmann@23854
   415
subsection {* Module names *}
haftmann@23854
   416
haftmann@23854
   417
code_modulename SML
haftmann@25919
   418
  Int Integer
haftmann@23854
   419
  Nat Integer
haftmann@23854
   420
  Divides Integer
haftmann@23854
   421
  Efficient_Nat Integer
haftmann@23854
   422
haftmann@23854
   423
code_modulename OCaml
haftmann@25919
   424
  Int Integer
haftmann@23854
   425
  Nat Integer
haftmann@23854
   426
  Divides Integer
haftmann@23854
   427
  Efficient_Nat Integer
haftmann@23854
   428
haftmann@23854
   429
code_modulename Haskell
haftmann@25919
   430
  Int Integer
haftmann@23854
   431
  Nat Integer
haftmann@24195
   432
  Divides Integer
haftmann@23854
   433
  Efficient_Nat Integer
haftmann@23854
   434
haftmann@24715
   435
hide const nat_of_int int_of_nat
haftmann@23854
   436
haftmann@23854
   437
end