src/HOL/Library/Efficient_Nat.thy
author haftmann
Tue Sep 25 21:08:34 2007 +0200 (2007-09-25)
changeset 24715 f96d86cdbe5a
parent 24630 351a308ab58d
child 24994 c385c4eabb3b
permissions -rw-r--r--
Efficient_Nat and Pretty_Int integrated with ML_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@23854
     9
imports Main Pretty_Int
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@24715
    36
  "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@23854
    58
  assumes "Numeral.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@23854
    77
  "nat_case \<equiv> (\<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@23854
    85
  show "nat_case \<equiv> (\<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@24715
   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@24715
   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@24715
   168
lemma ml_int_of_nat_code [code func, code inline]:
haftmann@24715
   169
  "ml_int_of_nat n = ml_int_of_int (int_of_nat n)"
haftmann@24715
   170
  unfolding ml_int_of_nat_def int_of_nat_def by simp
haftmann@23854
   171
haftmann@24715
   172
lemma nat_of_mlt_int_code [code func, code inline]:
haftmann@24715
   173
  "nat_of_ml_int k = nat (int_of_ml_int k)"
haftmann@24715
   174
  unfolding nat_of_ml_int_def by simp
haftmann@23854
   175
haftmann@23854
   176
haftmann@23854
   177
subsection {* Code generator setup for basic functions *}
haftmann@23854
   178
haftmann@23854
   179
text {*
haftmann@23854
   180
  @{typ nat} is no longer a datatype but embedded into the integers.
haftmann@23854
   181
*}
haftmann@23854
   182
haftmann@23854
   183
code_type nat
wenzelm@24630
   184
  (SML "int")
haftmann@23854
   185
  (OCaml "Big'_int.big'_int")
haftmann@23854
   186
  (Haskell "Integer")
haftmann@23854
   187
haftmann@23854
   188
types_code
haftmann@23854
   189
  nat ("int")
haftmann@23854
   190
attach (term_of) {*
wenzelm@24630
   191
val term_of_nat = HOLogic.mk_number HOLogic.natT;
haftmann@23854
   192
*}
haftmann@23854
   193
attach (test) {*
haftmann@23854
   194
fun gen_nat i = random_range 0 i;
haftmann@23854
   195
*}
haftmann@23854
   196
haftmann@23854
   197
consts_code
haftmann@23854
   198
  "0 \<Colon> nat" ("0")
haftmann@23854
   199
  Suc ("(_ + 1)")
haftmann@23854
   200
haftmann@23854
   201
text {*
haftmann@23854
   202
  Since natural numbers are implemented
haftmann@23854
   203
  using integers, the coercion function @{const "int"} of type
haftmann@23854
   204
  @{typ "nat \<Rightarrow> int"} is simply implemented by the identity function,
haftmann@23854
   205
  likewise @{const nat_of_int} of type @{typ "int \<Rightarrow> nat"}.
haftmann@23854
   206
  For the @{const "nat"} function for converting an integer to a natural
haftmann@23854
   207
  number, we give a specific implementation using an ML function that
haftmann@23854
   208
  returns its input value, provided that it is non-negative, and otherwise
haftmann@23854
   209
  returns @{text "0"}.
haftmann@23854
   210
*}
haftmann@23854
   211
haftmann@23854
   212
consts_code
haftmann@24715
   213
  int_of_nat ("(_)")
haftmann@23854
   214
  nat ("\<module>nat")
haftmann@23854
   215
attach {*
haftmann@23854
   216
fun nat i = if i < 0 then 0 else i;
haftmann@23854
   217
*}
haftmann@23854
   218
haftmann@24715
   219
code_const int_of_nat
haftmann@23854
   220
  (SML "_")
haftmann@23854
   221
  (OCaml "_")
haftmann@23854
   222
  (Haskell "_")
haftmann@23854
   223
haftmann@23854
   224
code_const nat_of_int
haftmann@23854
   225
  (SML "_")
haftmann@23854
   226
  (OCaml "_")
haftmann@23854
   227
  (Haskell "_")
haftmann@23854
   228
haftmann@23854
   229
haftmann@23854
   230
subsection {* Preprocessors *}
haftmann@23854
   231
haftmann@23854
   232
text {*
haftmann@23854
   233
  Natural numerals should be expressed using @{const nat_of_int}.
haftmann@23854
   234
*}
haftmann@23854
   235
haftmann@23854
   236
lemmas [code inline del] = nat_number_of_def
haftmann@23854
   237
haftmann@23854
   238
ML {*
haftmann@23854
   239
fun nat_of_int_of_number_of thy cts =
haftmann@23854
   240
  let
haftmann@23854
   241
    val simplify_less = Simplifier.rewrite 
haftmann@23854
   242
      (HOL_basic_ss addsimps (@{thms less_numeral_code} @ @{thms less_eq_numeral_code}));
haftmann@23854
   243
    fun mk_rew (t, ty) =
wenzelm@24630
   244
      if ty = HOLogic.natT andalso 0 <= HOLogic.dest_numeral t then
haftmann@23854
   245
        Thm.capply @{cterm "(op \<le>) Numeral.Pls"} (Thm.cterm_of thy t)
haftmann@23854
   246
        |> simplify_less
haftmann@23854
   247
        |> (fn thm => @{thm nat_of_int_of_number_of_aux} OF [thm])
haftmann@23854
   248
        |> (fn thm => @{thm nat_of_int_of_number_of} OF [thm])
haftmann@23854
   249
        |> (fn thm => @{thm eq_reflection} OF [thm])
haftmann@23854
   250
        |> SOME
haftmann@23854
   251
      else NONE
haftmann@23854
   252
  in
haftmann@23854
   253
    fold (HOLogic.add_numerals o Thm.term_of) cts []
haftmann@23854
   254
    |> map_filter mk_rew
haftmann@23854
   255
  end;
haftmann@23854
   256
*}
haftmann@23854
   257
haftmann@23854
   258
setup {*
haftmann@24222
   259
  Code.add_inline_proc ("nat_of_int_of_number_of", nat_of_int_of_number_of)
haftmann@23854
   260
*}
haftmann@23854
   261
haftmann@23854
   262
text {*
haftmann@23854
   263
  In contrast to @{term "Suc n"}, the term @{term "n + (1::nat)"} is no longer
haftmann@23854
   264
  a constructor term. Therefore, all occurrences of this term in a position
haftmann@23854
   265
  where a pattern is expected (i.e.\ on the left-hand side of a recursion
haftmann@23854
   266
  equation or in the arguments of an inductive relation in an introduction
haftmann@23854
   267
  rule) must be eliminated.
haftmann@23854
   268
  This can be accomplished by applying the following transformation rules:
haftmann@23854
   269
*}
haftmann@23854
   270
haftmann@23854
   271
theorem Suc_if_eq: "(\<And>n. f (Suc n) = h n) \<Longrightarrow> f 0 = g \<Longrightarrow>
haftmann@23854
   272
  f n = (if n = 0 then g else h (n - 1))"
haftmann@23854
   273
  by (case_tac n) simp_all
haftmann@23854
   274
haftmann@23854
   275
theorem Suc_clause: "(\<And>n. P n (Suc n)) \<Longrightarrow> n \<noteq> 0 \<Longrightarrow> P (n - 1) n"
haftmann@23854
   276
  by (case_tac n) simp_all
haftmann@23854
   277
haftmann@23854
   278
text {*
haftmann@23854
   279
  The rules above are built into a preprocessor that is plugged into
haftmann@23854
   280
  the code generator. Since the preprocessor for introduction rules
haftmann@23854
   281
  does not know anything about modes, some of the modes that worked
haftmann@23854
   282
  for the canonical representation of natural numbers may no longer work.
haftmann@23854
   283
*}
haftmann@23854
   284
haftmann@23854
   285
(*<*)
haftmann@23854
   286
haftmann@23854
   287
ML {*
haftmann@23854
   288
fun remove_suc thy thms =
haftmann@23854
   289
  let
haftmann@23854
   290
    val vname = Name.variant (map fst
haftmann@23854
   291
      (fold (Term.add_varnames o Thm.full_prop_of) thms [])) "x";
haftmann@23854
   292
    val cv = cterm_of thy (Var ((vname, 0), HOLogic.natT));
haftmann@23854
   293
    fun lhs_of th = snd (Thm.dest_comb
haftmann@23854
   294
      (fst (Thm.dest_comb (snd (Thm.dest_comb (cprop_of th))))));
haftmann@23854
   295
    fun rhs_of th = snd (Thm.dest_comb (snd (Thm.dest_comb (cprop_of th))));
haftmann@23854
   296
    fun find_vars ct = (case term_of ct of
haftmann@23854
   297
        (Const ("Suc", _) $ Var _) => [(cv, snd (Thm.dest_comb ct))]
haftmann@23854
   298
      | _ $ _ =>
haftmann@23854
   299
        let val (ct1, ct2) = Thm.dest_comb ct
haftmann@23854
   300
        in 
haftmann@23854
   301
          map (apfst (fn ct => Thm.capply ct ct2)) (find_vars ct1) @
haftmann@23854
   302
          map (apfst (Thm.capply ct1)) (find_vars ct2)
haftmann@23854
   303
        end
haftmann@23854
   304
      | _ => []);
haftmann@23854
   305
    val eqs = maps
haftmann@23854
   306
      (fn th => map (pair th) (find_vars (lhs_of th))) thms;
haftmann@23854
   307
    fun mk_thms (th, (ct, cv')) =
haftmann@23854
   308
      let
haftmann@23854
   309
        val th' =
haftmann@23854
   310
          Thm.implies_elim
haftmann@23854
   311
           (Conv.fconv_rule (Thm.beta_conversion true)
haftmann@23854
   312
             (Drule.instantiate'
haftmann@23854
   313
               [SOME (ctyp_of_term ct)] [SOME (Thm.cabs cv ct),
haftmann@23854
   314
                 SOME (Thm.cabs cv' (rhs_of th)), NONE, SOME cv']
haftmann@24222
   315
               @{thm Suc_if_eq})) (Thm.forall_intr cv' th)
haftmann@23854
   316
      in
haftmann@23854
   317
        case map_filter (fn th'' =>
haftmann@23854
   318
            SOME (th'', singleton
haftmann@23854
   319
              (Variable.trade (K (fn [th'''] => [th''' RS th'])) (Variable.thm_context th'')) th'')
haftmann@23854
   320
          handle THM _ => NONE) thms of
haftmann@23854
   321
            [] => NONE
haftmann@23854
   322
          | thps =>
haftmann@23854
   323
              let val (ths1, ths2) = split_list thps
haftmann@23854
   324
              in SOME (subtract Thm.eq_thm (th :: ths1) thms @ ths2) end
haftmann@23854
   325
      end
haftmann@23854
   326
  in
haftmann@23854
   327
    case get_first mk_thms eqs of
haftmann@23854
   328
      NONE => thms
haftmann@23854
   329
    | SOME x => remove_suc thy x
haftmann@23854
   330
  end;
haftmann@23854
   331
haftmann@23854
   332
fun eqn_suc_preproc thy ths =
haftmann@23854
   333
  let
haftmann@24222
   334
    val dest = fst o HOLogic.dest_eq o HOLogic.dest_Trueprop o prop_of;
haftmann@24222
   335
    fun contains_suc t = member (op =) (term_consts t) @{const_name Suc};
haftmann@23854
   336
  in
haftmann@23854
   337
    if forall (can dest) ths andalso
haftmann@23854
   338
      exists (contains_suc o dest) ths
haftmann@23854
   339
    then remove_suc thy ths else ths
haftmann@23854
   340
  end;
haftmann@23854
   341
haftmann@23854
   342
fun remove_suc_clause thy thms =
haftmann@23854
   343
  let
haftmann@23854
   344
    val vname = Name.variant (map fst
haftmann@23854
   345
      (fold (Term.add_varnames o Thm.full_prop_of) thms [])) "x";
haftmann@24222
   346
    fun find_var (t as Const (@{const_name Suc}, _) $ (v as Var _)) = SOME (t, v)
haftmann@23854
   347
      | find_var (t $ u) = (case find_var t of NONE => find_var u | x => x)
haftmann@23854
   348
      | find_var _ = NONE;
haftmann@23854
   349
    fun find_thm th =
haftmann@23854
   350
      let val th' = Conv.fconv_rule ObjectLogic.atomize th
haftmann@23854
   351
      in Option.map (pair (th, th')) (find_var (prop_of th')) end
haftmann@23854
   352
  in
haftmann@23854
   353
    case get_first find_thm thms of
haftmann@23854
   354
      NONE => thms
haftmann@23854
   355
    | SOME ((th, th'), (Sucv, v)) =>
haftmann@23854
   356
        let
haftmann@23854
   357
          val cert = cterm_of (Thm.theory_of_thm th);
haftmann@23854
   358
          val th'' = ObjectLogic.rulify (Thm.implies_elim
haftmann@23854
   359
            (Conv.fconv_rule (Thm.beta_conversion true)
haftmann@23854
   360
              (Drule.instantiate' []
haftmann@23854
   361
                [SOME (cert (lambda v (Abs ("x", HOLogic.natT,
haftmann@23854
   362
                   abstract_over (Sucv,
haftmann@23854
   363
                     HOLogic.dest_Trueprop (prop_of th')))))),
haftmann@24222
   364
                 SOME (cert v)] @{thm Suc_clause}))
haftmann@23854
   365
            (Thm.forall_intr (cert v) th'))
haftmann@23854
   366
        in
haftmann@23854
   367
          remove_suc_clause thy (map (fn th''' =>
haftmann@23854
   368
            if (op = o pairself prop_of) (th''', th) then th'' else th''') thms)
haftmann@23854
   369
        end
haftmann@23854
   370
  end;
haftmann@23854
   371
haftmann@23854
   372
fun clause_suc_preproc thy ths =
haftmann@23854
   373
  let
haftmann@23854
   374
    val dest = fst o HOLogic.dest_mem o HOLogic.dest_Trueprop
haftmann@23854
   375
  in
haftmann@23854
   376
    if forall (can (dest o concl_of)) ths andalso
haftmann@23854
   377
      exists (fn th => member (op =) (foldr add_term_consts
haftmann@23854
   378
        [] (map_filter (try dest) (concl_of th :: prems_of th))) "Suc") ths
haftmann@23854
   379
    then remove_suc_clause thy ths else ths
haftmann@23854
   380
  end;
haftmann@23854
   381
haftmann@23854
   382
fun lift_obj_eq f thy =
haftmann@23854
   383
  map (fn thm => thm RS @{thm meta_eq_to_obj_eq})
haftmann@23854
   384
  #> f thy
haftmann@23854
   385
  #> map (fn thm => thm RS @{thm eq_reflection})
haftmann@23854
   386
  #> map (Conv.fconv_rule Drule.beta_eta_conversion)
haftmann@23854
   387
*}
haftmann@23854
   388
haftmann@23854
   389
setup {*
haftmann@23854
   390
  Codegen.add_preprocessor eqn_suc_preproc
haftmann@23854
   391
  #> Codegen.add_preprocessor clause_suc_preproc
haftmann@24222
   392
  #> Code.add_preproc ("eqn_Suc", lift_obj_eq eqn_suc_preproc)
haftmann@24222
   393
  #> Code.add_preproc ("clause_Suc", lift_obj_eq clause_suc_preproc)
haftmann@23854
   394
*}
haftmann@23854
   395
(*>*)
haftmann@23854
   396
haftmann@23854
   397
haftmann@23854
   398
subsection {* Module names *}
haftmann@23854
   399
haftmann@23854
   400
code_modulename SML
haftmann@23854
   401
  Nat Integer
haftmann@23854
   402
  Divides Integer
haftmann@23854
   403
  Efficient_Nat Integer
haftmann@23854
   404
haftmann@23854
   405
code_modulename OCaml
haftmann@23854
   406
  Nat Integer
haftmann@23854
   407
  Divides Integer
haftmann@23854
   408
  Efficient_Nat Integer
haftmann@23854
   409
haftmann@23854
   410
code_modulename Haskell
haftmann@23854
   411
  Nat Integer
haftmann@24195
   412
  Divides Integer
haftmann@23854
   413
  Efficient_Nat Integer
haftmann@23854
   414
haftmann@24715
   415
hide const nat_of_int int_of_nat
haftmann@23854
   416
haftmann@23854
   417
end