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