src/HOL/Library/Efficient_Nat.thy
author haftmann
Mon, 16 Feb 2009 19:11:16 +0100
changeset 29874 ffed4bd4bfad
parent 29869 a2594b5c945a
child 30663 0b6aff7451b2
permissions -rw-r--r--
faster preprocessor
haftmann@23854
     1
(*  Title:      HOL/Library/Efficient_Nat.thy
haftmann@25931
     2
    Author:     Stefan Berghofer, Florian Haftmann, TU Muenchen
haftmann@23854
     3
*)
haftmann@23854
     4
haftmann@25931
     5
header {* Implementation of natural numbers by target-language integers *}
haftmann@23854
     6
haftmann@23854
     7
theory Efficient_Nat
haftmann@28694
     8
imports Code_Index Code_Integer
haftmann@23854
     9
begin
haftmann@23854
    10
haftmann@23854
    11
text {*
haftmann@25931
    12
  When generating code for functions on natural numbers, the
haftmann@25931
    13
  canonical representation using @{term "0::nat"} and
haftmann@25931
    14
  @{term "Suc"} is unsuitable for computations involving large
haftmann@25931
    15
  numbers.  The efficiency of the generated code can be improved
haftmann@25931
    16
  drastically by implementing natural numbers by target-language
haftmann@25931
    17
  integers.  To do this, just include this theory.
haftmann@23854
    18
*}
haftmann@23854
    19
haftmann@25931
    20
subsection {* Basic arithmetic *}
haftmann@23854
    21
haftmann@23854
    22
text {*
haftmann@25931
    23
  Most standard arithmetic functions on natural numbers are implemented
haftmann@25931
    24
  using their counterparts on the integers:
haftmann@23854
    25
*}
haftmann@23854
    26
haftmann@25931
    27
code_datatype number_nat_inst.number_of_nat
haftmann@23854
    28
haftmann@28522
    29
lemma zero_nat_code [code, code inline]:
haftmann@25931
    30
  "0 = (Numeral0 :: nat)"
haftmann@25931
    31
  by simp
haftmann@25931
    32
lemmas [code post] = zero_nat_code [symmetric]
haftmann@23854
    33
haftmann@28522
    34
lemma one_nat_code [code, code inline]:
haftmann@25931
    35
  "1 = (Numeral1 :: nat)"
haftmann@25931
    36
  by simp
haftmann@25931
    37
lemmas [code post] = one_nat_code [symmetric]
haftmann@23854
    38
haftmann@25931
    39
lemma Suc_code [code]:
haftmann@25931
    40
  "Suc n = n + 1"
haftmann@25931
    41
  by simp
haftmann@23854
    42
haftmann@25931
    43
lemma plus_nat_code [code]:
haftmann@25931
    44
  "n + m = nat (of_nat n + of_nat m)"
haftmann@25931
    45
  by simp
haftmann@23854
    46
haftmann@25931
    47
lemma minus_nat_code [code]:
haftmann@25931
    48
  "n - m = nat (of_nat n - of_nat m)"
haftmann@25931
    49
  by simp
haftmann@23854
    50
haftmann@25931
    51
lemma times_nat_code [code]:
haftmann@25931
    52
  "n * m = nat (of_nat n * of_nat m)"
haftmann@25931
    53
  unfolding of_nat_mult [symmetric] by simp
haftmann@23854
    54
haftmann@26009
    55
text {* Specialized @{term "op div \<Colon> nat \<Rightarrow> nat \<Rightarrow> nat"} 
haftmann@26009
    56
  and @{term "op mod \<Colon> nat \<Rightarrow> nat \<Rightarrow> nat"} operations. *}
haftmann@23854
    57
haftmann@28694
    58
definition divmod_aux ::  "nat \<Rightarrow> nat \<Rightarrow> nat \<times> nat" where
haftmann@29657
    59
  [code del]: "divmod_aux = Divides.divmod"
haftmann@26009
    60
haftmann@28522
    61
lemma [code]:
haftmann@29657
    62
  "Divides.divmod n m = (if m = 0 then (0, n) else divmod_aux n m)"
haftmann@26100
    63
  unfolding divmod_aux_def divmod_div_mod by simp
haftmann@26009
    64
haftmann@26100
    65
lemma divmod_aux_code [code]:
haftmann@26100
    66
  "divmod_aux n m = (nat (of_nat n div of_nat m), nat (of_nat n mod of_nat m))"
haftmann@26100
    67
  unfolding divmod_aux_def divmod_div_mod zdiv_int [symmetric] zmod_int [symmetric] by simp
haftmann@23854
    68
haftmann@25931
    69
lemma eq_nat_code [code]:
haftmann@28351
    70
  "eq_class.eq n m \<longleftrightarrow> eq_class.eq (of_nat n \<Colon> int) (of_nat m)"
haftmann@28351
    71
  by (simp add: eq)
haftmann@28351
    72
haftmann@28351
    73
lemma eq_nat_refl [code nbe]:
haftmann@28351
    74
  "eq_class.eq (n::nat) n \<longleftrightarrow> True"
haftmann@28351
    75
  by (rule HOL.eq_refl)
haftmann@25931
    76
haftmann@25931
    77
lemma less_eq_nat_code [code]:
haftmann@25931
    78
  "n \<le> m \<longleftrightarrow> (of_nat n \<Colon> int) \<le> of_nat m"
haftmann@25931
    79
  by simp
haftmann@25931
    80
haftmann@25931
    81
lemma less_nat_code [code]:
haftmann@25931
    82
  "n < m \<longleftrightarrow> (of_nat n \<Colon> int) < of_nat m"
haftmann@25931
    83
  by simp
haftmann@25931
    84
haftmann@25931
    85
subsection {* Case analysis *}
haftmann@24423
    86
haftmann@23854
    87
text {*
haftmann@23854
    88
  Case analysis on natural numbers is rephrased using a conditional
haftmann@23854
    89
  expression:
haftmann@23854
    90
*}
haftmann@23854
    91
haftmann@28522
    92
lemma [code, code unfold]:
haftmann@25615
    93
  "nat_case = (\<lambda>f g n. if n = 0 then f else g (n - 1))"
haftmann@25931
    94
  by (auto simp add: expand_fun_eq dest!: gr0_implies_Suc)
haftmann@25615
    95
haftmann@23854
    96
haftmann@23854
    97
subsection {* Preprocessors *}
haftmann@23854
    98
haftmann@23854
    99
text {*
haftmann@23854
   100
  In contrast to @{term "Suc n"}, the term @{term "n + (1::nat)"} is no longer
haftmann@23854
   101
  a constructor term. Therefore, all occurrences of this term in a position
haftmann@23854
   102
  where a pattern is expected (i.e.\ on the left-hand side of a recursion
haftmann@23854
   103
  equation or in the arguments of an inductive relation in an introduction
haftmann@23854
   104
  rule) must be eliminated.
haftmann@23854
   105
  This can be accomplished by applying the following transformation rules:
haftmann@23854
   106
*}
haftmann@23854
   107
haftmann@29874
   108
lemma Suc_if_eq': "(\<And>n. f (Suc n) = h n) \<Longrightarrow> f 0 = g \<Longrightarrow>
haftmann@23854
   109
  f n = (if n = 0 then g else h (n - 1))"
haftmann@29869
   110
  by (cases n) simp_all
haftmann@23854
   111
haftmann@29874
   112
lemma Suc_if_eq: "(\<And>n. f (Suc n) \<equiv> h n) \<Longrightarrow> f 0 \<equiv> g \<Longrightarrow>
haftmann@29874
   113
  f n \<equiv> if n = 0 then g else h (n - 1)"
haftmann@29874
   114
  by (rule eq_reflection, rule Suc_if_eq')
haftmann@29874
   115
    (rule meta_eq_to_obj_eq, assumption,
haftmann@29874
   116
     rule meta_eq_to_obj_eq, assumption)
haftmann@29874
   117
haftmann@25931
   118
lemma Suc_clause: "(\<And>n. P n (Suc n)) \<Longrightarrow> n \<noteq> 0 \<Longrightarrow> P (n - 1) n"
haftmann@29869
   119
  by (cases n) simp_all
haftmann@23854
   120
haftmann@23854
   121
text {*
haftmann@23854
   122
  The rules above are built into a preprocessor that is plugged into
haftmann@23854
   123
  the code generator. Since the preprocessor for introduction rules
haftmann@23854
   124
  does not know anything about modes, some of the modes that worked
haftmann@23854
   125
  for the canonical representation of natural numbers may no longer work.
haftmann@23854
   126
*}
haftmann@23854
   127
haftmann@23854
   128
(*<*)
haftmann@27609
   129
setup {*
haftmann@27609
   130
let
haftmann@23854
   131
haftmann@29874
   132
fun gen_remove_suc Suc_if_eq dest_judgement thy thms =
haftmann@23854
   133
  let
haftmann@23854
   134
    val vname = Name.variant (map fst
haftmann@29874
   135
      (fold (Term.add_var_names o Thm.full_prop_of) thms [])) "n";
haftmann@23854
   136
    val cv = cterm_of thy (Var ((vname, 0), HOLogic.natT));
haftmann@23854
   137
    fun lhs_of th = snd (Thm.dest_comb
haftmann@29874
   138
      (fst (Thm.dest_comb (dest_judgement (cprop_of th)))));
haftmann@29874
   139
    fun rhs_of th = snd (Thm.dest_comb (dest_judgement (cprop_of th)));
haftmann@23854
   140
    fun find_vars ct = (case term_of ct of
haftmann@29869
   141
        (Const (@{const_name Suc}, _) $ Var _) => [(cv, snd (Thm.dest_comb ct))]
haftmann@23854
   142
      | _ $ _ =>
haftmann@23854
   143
        let val (ct1, ct2) = Thm.dest_comb ct
haftmann@23854
   144
        in 
haftmann@23854
   145
          map (apfst (fn ct => Thm.capply ct ct2)) (find_vars ct1) @
haftmann@23854
   146
          map (apfst (Thm.capply ct1)) (find_vars ct2)
haftmann@23854
   147
        end
haftmann@23854
   148
      | _ => []);
haftmann@23854
   149
    val eqs = maps
haftmann@23854
   150
      (fn th => map (pair th) (find_vars (lhs_of th))) thms;
haftmann@23854
   151
    fun mk_thms (th, (ct, cv')) =
haftmann@23854
   152
      let
haftmann@23854
   153
        val th' =
haftmann@23854
   154
          Thm.implies_elim
haftmann@23854
   155
           (Conv.fconv_rule (Thm.beta_conversion true)
haftmann@23854
   156
             (Drule.instantiate'
haftmann@23854
   157
               [SOME (ctyp_of_term ct)] [SOME (Thm.cabs cv ct),
haftmann@23854
   158
                 SOME (Thm.cabs cv' (rhs_of th)), NONE, SOME cv']
haftmann@29874
   159
               Suc_if_eq)) (Thm.forall_intr cv' th)
haftmann@23854
   160
      in
haftmann@23854
   161
        case map_filter (fn th'' =>
haftmann@23854
   162
            SOME (th'', singleton
haftmann@23854
   163
              (Variable.trade (K (fn [th'''] => [th''' RS th'])) (Variable.thm_context th'')) th'')
haftmann@23854
   164
          handle THM _ => NONE) thms of
haftmann@23854
   165
            [] => NONE
haftmann@23854
   166
          | thps =>
haftmann@23854
   167
              let val (ths1, ths2) = split_list thps
haftmann@23854
   168
              in SOME (subtract Thm.eq_thm (th :: ths1) thms @ ths2) end
haftmann@23854
   169
      end
haftmann@29874
   170
  in get_first mk_thms eqs end;
haftmann@29874
   171
haftmann@29874
   172
fun gen_eqn_suc_preproc Suc_if_eq dest_judgement dest_lhs thy thms =
haftmann@29874
   173
  let
haftmann@29874
   174
    val dest = dest_lhs o prop_of;
haftmann@29874
   175
    val contains_suc = exists_Const (fn (c, _) => c = @{const_name Suc});
haftmann@29874
   176
  in
haftmann@29874
   177
    if forall (can dest) thms andalso exists (contains_suc o dest) thms
haftmann@29874
   178
      then perhaps_loop (gen_remove_suc Suc_if_eq dest_judgement thy) thms
haftmann@29874
   179
       else NONE
haftmann@23854
   180
  end;
haftmann@23854
   181
haftmann@29874
   182
fun eqn_suc_preproc thy = map fst
haftmann@29874
   183
  #> gen_eqn_suc_preproc
haftmann@29874
   184
      @{thm Suc_if_eq} I (fst o Logic.dest_equals) thy
haftmann@29874
   185
  #> (Option.map o map) (Code_Unit.mk_eqn thy);
haftmann@29874
   186
haftmann@29874
   187
fun eqn_suc_preproc' thy thms = gen_eqn_suc_preproc
haftmann@29874
   188
  @{thm Suc_if_eq'} (snd o Thm.dest_comb) (fst o HOLogic.dest_eq o HOLogic.dest_Trueprop) thy thms
haftmann@29874
   189
  |> the_default thms;
haftmann@23854
   190
haftmann@23854
   191
fun remove_suc_clause thy thms =
haftmann@23854
   192
  let
haftmann@23854
   193
    val vname = Name.variant (map fst
wenzelm@29258
   194
      (fold (Term.add_var_names o Thm.full_prop_of) thms [])) "x";
haftmann@24222
   195
    fun find_var (t as Const (@{const_name Suc}, _) $ (v as Var _)) = SOME (t, v)
haftmann@23854
   196
      | find_var (t $ u) = (case find_var t of NONE => find_var u | x => x)
haftmann@23854
   197
      | find_var _ = NONE;
haftmann@23854
   198
    fun find_thm th =
haftmann@23854
   199
      let val th' = Conv.fconv_rule ObjectLogic.atomize th
haftmann@23854
   200
      in Option.map (pair (th, th')) (find_var (prop_of th')) end
haftmann@23854
   201
  in
haftmann@23854
   202
    case get_first find_thm thms of
haftmann@23854
   203
      NONE => thms
haftmann@23854
   204
    | SOME ((th, th'), (Sucv, v)) =>
haftmann@23854
   205
        let
haftmann@23854
   206
          val cert = cterm_of (Thm.theory_of_thm th);
haftmann@23854
   207
          val th'' = ObjectLogic.rulify (Thm.implies_elim
haftmann@23854
   208
            (Conv.fconv_rule (Thm.beta_conversion true)
haftmann@23854
   209
              (Drule.instantiate' []
haftmann@23854
   210
                [SOME (cert (lambda v (Abs ("x", HOLogic.natT,
haftmann@23854
   211
                   abstract_over (Sucv,
haftmann@23854
   212
                     HOLogic.dest_Trueprop (prop_of th')))))),
haftmann@24222
   213
                 SOME (cert v)] @{thm Suc_clause}))
haftmann@23854
   214
            (Thm.forall_intr (cert v) th'))
haftmann@23854
   215
        in
haftmann@23854
   216
          remove_suc_clause thy (map (fn th''' =>
haftmann@23854
   217
            if (op = o pairself prop_of) (th''', th) then th'' else th''') thms)
haftmann@23854
   218
        end
haftmann@23854
   219
  end;
haftmann@23854
   220
haftmann@23854
   221
fun clause_suc_preproc thy ths =
haftmann@23854
   222
  let
haftmann@23854
   223
    val dest = fst o HOLogic.dest_mem o HOLogic.dest_Trueprop
haftmann@23854
   224
  in
haftmann@23854
   225
    if forall (can (dest o concl_of)) ths andalso
wenzelm@29287
   226
      exists (fn th => exists (exists_Const (fn (c, _) => c = @{const_name Suc}))
wenzelm@29287
   227
        (map_filter (try dest) (concl_of th :: prems_of th))) ths
haftmann@23854
   228
    then remove_suc_clause thy ths else ths
haftmann@23854
   229
  end;
haftmann@27609
   230
in
haftmann@27609
   231
haftmann@29874
   232
  Codegen.add_preprocessor eqn_suc_preproc'
haftmann@23854
   233
  #> Codegen.add_preprocessor clause_suc_preproc
haftmann@29874
   234
  #> Code.add_functrans ("eqn_Suc", eqn_suc_preproc)
haftmann@27609
   235
haftmann@27609
   236
end;
haftmann@23854
   237
*}
haftmann@23854
   238
(*>*)
haftmann@23854
   239
haftmann@27609
   240
haftmann@25931
   241
subsection {* Target language setup *}
haftmann@25931
   242
haftmann@25931
   243
text {*
haftmann@25967
   244
  For ML, we map @{typ nat} to target language integers, where we
haftmann@25931
   245
  assert that values are always non-negative.
haftmann@25931
   246
*}
haftmann@25931
   247
haftmann@25931
   248
code_type nat
haftmann@27496
   249
  (SML "IntInf.int")
haftmann@25931
   250
  (OCaml "Big'_int.big'_int")
haftmann@25931
   251
haftmann@25931
   252
types_code
haftmann@25931
   253
  nat ("int")
haftmann@25931
   254
attach (term_of) {*
haftmann@25931
   255
val term_of_nat = HOLogic.mk_number HOLogic.natT;
haftmann@25931
   256
*}
haftmann@25931
   257
attach (test) {*
haftmann@25931
   258
fun gen_nat i =
haftmann@25931
   259
  let val n = random_range 0 i
haftmann@25931
   260
  in (n, fn () => term_of_nat n) end;
haftmann@25931
   261
*}
haftmann@25931
   262
haftmann@25931
   263
text {*
haftmann@25967
   264
  For Haskell we define our own @{typ nat} type.  The reason
haftmann@25967
   265
  is that we have to distinguish type class instances
haftmann@25967
   266
  for @{typ nat} and @{typ int}.
haftmann@25967
   267
*}
haftmann@25967
   268
haftmann@25967
   269
code_include Haskell "Nat" {*
haftmann@25967
   270
newtype Nat = Nat Integer deriving (Show, Eq);
haftmann@25967
   271
haftmann@25967
   272
instance Num Nat where {
haftmann@25967
   273
  fromInteger k = Nat (if k >= 0 then k else 0);
haftmann@25967
   274
  Nat n + Nat m = Nat (n + m);
haftmann@25967
   275
  Nat n - Nat m = fromInteger (n - m);
haftmann@25967
   276
  Nat n * Nat m = Nat (n * m);
haftmann@25967
   277
  abs n = n;
haftmann@25967
   278
  signum _ = 1;
haftmann@25967
   279
  negate n = error "negate Nat";
haftmann@25967
   280
};
haftmann@25967
   281
haftmann@25967
   282
instance Ord Nat where {
haftmann@25967
   283
  Nat n <= Nat m = n <= m;
haftmann@25967
   284
  Nat n < Nat m = n < m;
haftmann@25967
   285
};
haftmann@25967
   286
haftmann@25967
   287
instance Real Nat where {
haftmann@25967
   288
  toRational (Nat n) = toRational n;
haftmann@25967
   289
};
haftmann@25967
   290
haftmann@25967
   291
instance Enum Nat where {
haftmann@25967
   292
  toEnum k = fromInteger (toEnum k);
haftmann@25967
   293
  fromEnum (Nat n) = fromEnum n;
haftmann@25967
   294
};
haftmann@25967
   295
haftmann@25967
   296
instance Integral Nat where {
haftmann@25967
   297
  toInteger (Nat n) = n;
haftmann@25967
   298
  divMod n m = quotRem n m;
haftmann@25967
   299
  quotRem (Nat n) (Nat m) = (Nat k, Nat l) where (k, l) = quotRem n m;
haftmann@25967
   300
};
haftmann@25967
   301
*}
haftmann@25967
   302
haftmann@25967
   303
code_reserved Haskell Nat
haftmann@25967
   304
haftmann@25967
   305
code_type nat
haftmann@29730
   306
  (Haskell "Nat.Nat")
haftmann@25967
   307
haftmann@25967
   308
code_instance nat :: eq
haftmann@25967
   309
  (Haskell -)
haftmann@25967
   310
haftmann@25967
   311
text {*
haftmann@25931
   312
  Natural numerals.
haftmann@25931
   313
*}
haftmann@25931
   314
haftmann@25967
   315
lemma [code inline, symmetric, code post]:
haftmann@25931
   316
  "nat (number_of i) = number_nat_inst.number_of_nat i"
haftmann@25931
   317
  -- {* this interacts as desired with @{thm nat_number_of_def} *}
haftmann@25931
   318
  by (simp add: number_nat_inst.number_of_nat)
haftmann@25931
   319
haftmann@25931
   320
setup {*
haftmann@25931
   321
  fold (Numeral.add_code @{const_name number_nat_inst.number_of_nat}
haftmann@25967
   322
    true false) ["SML", "OCaml", "Haskell"]
haftmann@25931
   323
*}
haftmann@25931
   324
haftmann@25931
   325
text {*
haftmann@25931
   326
  Since natural numbers are implemented
haftmann@25967
   327
  using integers in ML, the coercion function @{const "of_nat"} of type
haftmann@25931
   328
  @{typ "nat \<Rightarrow> int"} is simply implemented by the identity function.
haftmann@25931
   329
  For the @{const "nat"} function for converting an integer to a natural
haftmann@25931
   330
  number, we give a specific implementation using an ML function that
haftmann@25931
   331
  returns its input value, provided that it is non-negative, and otherwise
haftmann@25931
   332
  returns @{text "0"}.
haftmann@25931
   333
*}
haftmann@25931
   334
haftmann@25931
   335
definition
haftmann@25931
   336
  int :: "nat \<Rightarrow> int"
haftmann@25931
   337
where
haftmann@28562
   338
  [code del]: "int = of_nat"
haftmann@25931
   339
haftmann@28562
   340
lemma int_code' [code]:
haftmann@25931
   341
  "int (number_of l) = (if neg (number_of l \<Colon> int) then 0 else number_of l)"
haftmann@25931
   342
  unfolding int_nat_number_of [folded int_def] ..
haftmann@25931
   343
haftmann@28562
   344
lemma nat_code' [code]:
haftmann@25931
   345
  "nat (number_of l) = (if neg (number_of l \<Colon> int) then 0 else number_of l)"
huffman@28969
   346
  unfolding nat_number_of_def number_of_is_id neg_def by simp
haftmann@25931
   347
haftmann@25931
   348
lemma of_nat_int [code unfold]:
haftmann@25931
   349
  "of_nat = int" by (simp add: int_def)
haftmann@25967
   350
declare of_nat_int [symmetric, code post]
haftmann@25931
   351
haftmann@25931
   352
code_const int
haftmann@25931
   353
  (SML "_")
haftmann@25931
   354
  (OCaml "_")
haftmann@25931
   355
haftmann@25931
   356
consts_code
haftmann@25931
   357
  int ("(_)")
haftmann@25931
   358
  nat ("\<module>nat")
haftmann@25931
   359
attach {*
haftmann@25931
   360
fun nat i = if i < 0 then 0 else i;
haftmann@25931
   361
*}
haftmann@25931
   362
haftmann@25967
   363
code_const nat
haftmann@25967
   364
  (SML "IntInf.max/ (/0,/ _)")
haftmann@25967
   365
  (OCaml "Big'_int.max'_big'_int/ Big'_int.zero'_big'_int")
haftmann@25967
   366
haftmann@25967
   367
text {* For Haskell, things are slightly different again. *}
haftmann@25967
   368
haftmann@25967
   369
code_const int and nat
haftmann@25967
   370
  (Haskell "toInteger" and "fromInteger")
haftmann@25931
   371
haftmann@25931
   372
text {* Conversion from and to indices. *}
haftmann@25931
   373
haftmann@29752
   374
code_const Code_Index.of_nat
haftmann@25967
   375
  (SML "IntInf.toInt")
haftmann@25967
   376
  (OCaml "Big'_int.int'_of'_big'_int")
haftmann@27673
   377
  (Haskell "fromEnum")
haftmann@25967
   378
haftmann@29752
   379
code_const Code_Index.nat_of
haftmann@25931
   380
  (SML "IntInf.fromInt")
haftmann@25931
   381
  (OCaml "Big'_int.big'_int'_of'_int")
haftmann@27673
   382
  (Haskell "toEnum")
haftmann@25931
   383
haftmann@25931
   384
text {* Using target language arithmetic operations whenever appropriate *}
haftmann@25931
   385
haftmann@25931
   386
code_const "op + \<Colon> nat \<Rightarrow> nat \<Rightarrow> nat"
haftmann@25931
   387
  (SML "IntInf.+ ((_), (_))")
haftmann@25931
   388
  (OCaml "Big'_int.add'_big'_int")
haftmann@25931
   389
  (Haskell infixl 6 "+")
haftmann@25931
   390
haftmann@25931
   391
code_const "op * \<Colon> nat \<Rightarrow> nat \<Rightarrow> nat"
haftmann@25931
   392
  (SML "IntInf.* ((_), (_))")
haftmann@25931
   393
  (OCaml "Big'_int.mult'_big'_int")
haftmann@25931
   394
  (Haskell infixl 7 "*")
haftmann@25931
   395
haftmann@26100
   396
code_const divmod_aux
haftmann@26009
   397
  (SML "IntInf.divMod/ ((_),/ (_))")
haftmann@26009
   398
  (OCaml "Big'_int.quomod'_big'_int")
haftmann@26009
   399
  (Haskell "divMod")
haftmann@25931
   400
haftmann@28346
   401
code_const "eq_class.eq \<Colon> nat \<Rightarrow> nat \<Rightarrow> bool"
haftmann@25931
   402
  (SML "!((_ : IntInf.int) = _)")
haftmann@25931
   403
  (OCaml "Big'_int.eq'_big'_int")
haftmann@25931
   404
  (Haskell infixl 4 "==")
haftmann@25931
   405
haftmann@25931
   406
code_const "op \<le> \<Colon> nat \<Rightarrow> nat \<Rightarrow> bool"
haftmann@25931
   407
  (SML "IntInf.<= ((_), (_))")
haftmann@25931
   408
  (OCaml "Big'_int.le'_big'_int")
haftmann@25931
   409
  (Haskell infix 4 "<=")
haftmann@25931
   410
haftmann@25931
   411
code_const "op < \<Colon> nat \<Rightarrow> nat \<Rightarrow> bool"
haftmann@25931
   412
  (SML "IntInf.< ((_), (_))")
haftmann@25931
   413
  (OCaml "Big'_int.lt'_big'_int")
haftmann@25931
   414
  (Haskell infix 4 "<")
haftmann@25931
   415
haftmann@25931
   416
consts_code
haftmann@28522
   417
  "0::nat"                     ("0")
haftmann@28522
   418
  "1::nat"                     ("1")
haftmann@25931
   419
  Suc                          ("(_ +/ 1)")
haftmann@25931
   420
  "op + \<Colon>  nat \<Rightarrow> nat \<Rightarrow> nat"   ("(_ +/ _)")
haftmann@25931
   421
  "op * \<Colon>  nat \<Rightarrow> nat \<Rightarrow> nat"   ("(_ */ _)")
haftmann@25931
   422
  "op \<le> \<Colon>  nat \<Rightarrow> nat \<Rightarrow> bool"  ("(_ <=/ _)")
haftmann@25931
   423
  "op < \<Colon>  nat \<Rightarrow> nat \<Rightarrow> bool"  ("(_ </ _)")
haftmann@25931
   424
haftmann@25931
   425
haftmann@28228
   426
text {* Evaluation *}
haftmann@28228
   427
haftmann@28562
   428
lemma [code, code del]:
haftmann@28228
   429
  "(Code_Eval.term_of \<Colon> nat \<Rightarrow> term) = Code_Eval.term_of" ..
haftmann@28228
   430
haftmann@28228
   431
code_const "Code_Eval.term_of \<Colon> nat \<Rightarrow> term"
haftmann@28228
   432
  (SML "HOLogic.mk'_number/ HOLogic.natT")
haftmann@28228
   433
haftmann@28228
   434
haftmann@25931
   435
text {* Module names *}
haftmann@23854
   436
haftmann@23854
   437
code_modulename SML
haftmann@23854
   438
  Nat Integer
haftmann@23854
   439
  Divides Integer
haftmann@28683
   440
  Ring_and_Field Integer
haftmann@23854
   441
  Efficient_Nat Integer
haftmann@23854
   442
haftmann@23854
   443
code_modulename OCaml
haftmann@23854
   444
  Nat Integer
haftmann@23854
   445
  Divides Integer
haftmann@28683
   446
  Ring_and_Field Integer
haftmann@23854
   447
  Efficient_Nat Integer
haftmann@23854
   448
haftmann@23854
   449
code_modulename Haskell
haftmann@23854
   450
  Nat Integer
haftmann@24195
   451
  Divides Integer
haftmann@28683
   452
  Ring_and_Field Integer
haftmann@23854
   453
  Efficient_Nat Integer
haftmann@23854
   454
haftmann@25931
   455
hide const int
haftmann@23854
   456
haftmann@23854
   457
end